b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * ON pin driver for Dialog DA9052 PMICs |
| 4 | * |
| 5 | * Copyright(c) 2012 Dialog Semiconductor Ltd. |
| 6 | * |
| 7 | * Author: David Dajun Chen <dchen@diasemi.com> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/input.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/workqueue.h> |
| 14 | |
| 15 | #include <linux/mfd/da9052/da9052.h> |
| 16 | #include <linux/mfd/da9052/reg.h> |
| 17 | |
| 18 | struct da9052_onkey { |
| 19 | struct da9052 *da9052; |
| 20 | struct input_dev *input; |
| 21 | struct delayed_work work; |
| 22 | }; |
| 23 | |
| 24 | static void da9052_onkey_query(struct da9052_onkey *onkey) |
| 25 | { |
| 26 | int ret; |
| 27 | |
| 28 | ret = da9052_reg_read(onkey->da9052, DA9052_STATUS_A_REG); |
| 29 | if (ret < 0) { |
| 30 | dev_err(onkey->da9052->dev, |
| 31 | "Failed to read onkey event err=%d\n", ret); |
| 32 | } else { |
| 33 | /* |
| 34 | * Since interrupt for deassertion of ONKEY pin is not |
| 35 | * generated, onkey event state determines the onkey |
| 36 | * button state. |
| 37 | */ |
| 38 | bool pressed = !(ret & DA9052_STATUSA_NONKEY); |
| 39 | |
| 40 | input_report_key(onkey->input, KEY_POWER, pressed); |
| 41 | input_sync(onkey->input); |
| 42 | |
| 43 | /* |
| 44 | * Interrupt is generated only when the ONKEY pin |
| 45 | * is asserted. Hence the deassertion of the pin |
| 46 | * is simulated through work queue. |
| 47 | */ |
| 48 | if (pressed) |
| 49 | schedule_delayed_work(&onkey->work, |
| 50 | msecs_to_jiffies(50)); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static void da9052_onkey_work(struct work_struct *work) |
| 55 | { |
| 56 | struct da9052_onkey *onkey = container_of(work, struct da9052_onkey, |
| 57 | work.work); |
| 58 | |
| 59 | da9052_onkey_query(onkey); |
| 60 | } |
| 61 | |
| 62 | static irqreturn_t da9052_onkey_irq(int irq, void *data) |
| 63 | { |
| 64 | struct da9052_onkey *onkey = data; |
| 65 | |
| 66 | da9052_onkey_query(onkey); |
| 67 | |
| 68 | return IRQ_HANDLED; |
| 69 | } |
| 70 | |
| 71 | static int da9052_onkey_probe(struct platform_device *pdev) |
| 72 | { |
| 73 | struct da9052 *da9052 = dev_get_drvdata(pdev->dev.parent); |
| 74 | struct da9052_onkey *onkey; |
| 75 | struct input_dev *input_dev; |
| 76 | int error; |
| 77 | |
| 78 | if (!da9052) { |
| 79 | dev_err(&pdev->dev, "Failed to get the driver's data\n"); |
| 80 | return -EINVAL; |
| 81 | } |
| 82 | |
| 83 | onkey = kzalloc(sizeof(*onkey), GFP_KERNEL); |
| 84 | input_dev = input_allocate_device(); |
| 85 | if (!onkey || !input_dev) { |
| 86 | dev_err(&pdev->dev, "Failed to allocate memory\n"); |
| 87 | error = -ENOMEM; |
| 88 | goto err_free_mem; |
| 89 | } |
| 90 | |
| 91 | onkey->input = input_dev; |
| 92 | onkey->da9052 = da9052; |
| 93 | INIT_DELAYED_WORK(&onkey->work, da9052_onkey_work); |
| 94 | |
| 95 | input_dev->name = "da9052-onkey"; |
| 96 | input_dev->phys = "da9052-onkey/input0"; |
| 97 | input_dev->dev.parent = &pdev->dev; |
| 98 | |
| 99 | input_dev->evbit[0] = BIT_MASK(EV_KEY); |
| 100 | __set_bit(KEY_POWER, input_dev->keybit); |
| 101 | |
| 102 | error = da9052_request_irq(onkey->da9052, DA9052_IRQ_NONKEY, "ONKEY", |
| 103 | da9052_onkey_irq, onkey); |
| 104 | if (error < 0) { |
| 105 | dev_err(onkey->da9052->dev, |
| 106 | "Failed to register ONKEY IRQ: %d\n", error); |
| 107 | goto err_free_mem; |
| 108 | } |
| 109 | |
| 110 | error = input_register_device(onkey->input); |
| 111 | if (error) { |
| 112 | dev_err(&pdev->dev, "Unable to register input device, %d\n", |
| 113 | error); |
| 114 | goto err_free_irq; |
| 115 | } |
| 116 | |
| 117 | platform_set_drvdata(pdev, onkey); |
| 118 | return 0; |
| 119 | |
| 120 | err_free_irq: |
| 121 | da9052_free_irq(onkey->da9052, DA9052_IRQ_NONKEY, onkey); |
| 122 | cancel_delayed_work_sync(&onkey->work); |
| 123 | err_free_mem: |
| 124 | input_free_device(input_dev); |
| 125 | kfree(onkey); |
| 126 | |
| 127 | return error; |
| 128 | } |
| 129 | |
| 130 | static int da9052_onkey_remove(struct platform_device *pdev) |
| 131 | { |
| 132 | struct da9052_onkey *onkey = platform_get_drvdata(pdev); |
| 133 | |
| 134 | da9052_free_irq(onkey->da9052, DA9052_IRQ_NONKEY, onkey); |
| 135 | cancel_delayed_work_sync(&onkey->work); |
| 136 | |
| 137 | input_unregister_device(onkey->input); |
| 138 | kfree(onkey); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static struct platform_driver da9052_onkey_driver = { |
| 144 | .probe = da9052_onkey_probe, |
| 145 | .remove = da9052_onkey_remove, |
| 146 | .driver = { |
| 147 | .name = "da9052-onkey", |
| 148 | }, |
| 149 | }; |
| 150 | module_platform_driver(da9052_onkey_driver); |
| 151 | |
| 152 | MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>"); |
| 153 | MODULE_DESCRIPTION("Onkey driver for DA9052"); |
| 154 | MODULE_LICENSE("GPL"); |
| 155 | MODULE_ALIAS("platform:da9052-onkey"); |