WIP FPC-III support
[linux/fpc-iii.git] / drivers / input / misc / da9052_onkey.c
blob6d1152850a6d8f6df68101b9692763f143f87ab1
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * ON pin driver for Dialog DA9052 PMICs
5 * Copyright(c) 2012 Dialog Semiconductor Ltd.
7 * Author: David Dajun Chen <dchen@diasemi.com>
8 */
10 #include <linux/input.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/workqueue.h>
15 #include <linux/mfd/da9052/da9052.h>
16 #include <linux/mfd/da9052/reg.h>
18 struct da9052_onkey {
19 struct da9052 *da9052;
20 struct input_dev *input;
21 struct delayed_work work;
24 static void da9052_onkey_query(struct da9052_onkey *onkey)
26 int ret;
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 {
34 * Since interrupt for deassertion of ONKEY pin is not
35 * generated, onkey event state determines the onkey
36 * button state.
38 bool pressed = !(ret & DA9052_STATUSA_NONKEY);
40 input_report_key(onkey->input, KEY_POWER, pressed);
41 input_sync(onkey->input);
44 * Interrupt is generated only when the ONKEY pin
45 * is asserted. Hence the deassertion of the pin
46 * is simulated through work queue.
48 if (pressed)
49 schedule_delayed_work(&onkey->work,
50 msecs_to_jiffies(50));
54 static void da9052_onkey_work(struct work_struct *work)
56 struct da9052_onkey *onkey = container_of(work, struct da9052_onkey,
57 work.work);
59 da9052_onkey_query(onkey);
62 static irqreturn_t da9052_onkey_irq(int irq, void *data)
64 struct da9052_onkey *onkey = data;
66 da9052_onkey_query(onkey);
68 return IRQ_HANDLED;
71 static int da9052_onkey_probe(struct platform_device *pdev)
73 struct da9052 *da9052 = dev_get_drvdata(pdev->dev.parent);
74 struct da9052_onkey *onkey;
75 struct input_dev *input_dev;
76 int error;
78 if (!da9052) {
79 dev_err(&pdev->dev, "Failed to get the driver's data\n");
80 return -EINVAL;
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;
91 onkey->input = input_dev;
92 onkey->da9052 = da9052;
93 INIT_DELAYED_WORK(&onkey->work, da9052_onkey_work);
95 input_dev->name = "da9052-onkey";
96 input_dev->phys = "da9052-onkey/input0";
97 input_dev->dev.parent = &pdev->dev;
99 input_dev->evbit[0] = BIT_MASK(EV_KEY);
100 __set_bit(KEY_POWER, input_dev->keybit);
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;
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;
117 platform_set_drvdata(pdev, onkey);
118 return 0;
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);
127 return error;
130 static int da9052_onkey_remove(struct platform_device *pdev)
132 struct da9052_onkey *onkey = platform_get_drvdata(pdev);
134 da9052_free_irq(onkey->da9052, DA9052_IRQ_NONKEY, onkey);
135 cancel_delayed_work_sync(&onkey->work);
137 input_unregister_device(onkey->input);
138 kfree(onkey);
140 return 0;
143 static struct platform_driver da9052_onkey_driver = {
144 .probe = da9052_onkey_probe,
145 .remove = da9052_onkey_remove,
146 .driver = {
147 .name = "da9052-onkey",
150 module_platform_driver(da9052_onkey_driver);
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");