1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * ON pin driver for Dialog DA9055 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
14 #include <linux/mfd/da9055/core.h>
15 #include <linux/mfd/da9055/reg.h>
16
17 struct da9055_onkey {
18 struct da9055 *da9055;
19 struct input_dev *input;
20 struct delayed_work work;
21 };
22
da9055_onkey_query(struct da9055_onkey * onkey)23 static void da9055_onkey_query(struct da9055_onkey *onkey)
24 {
25 int key_stat;
26
27 key_stat = da9055_reg_read(onkey->da9055, DA9055_REG_STATUS_A);
28 if (key_stat < 0) {
29 dev_err(onkey->da9055->dev,
30 "Failed to read onkey event %d\n", key_stat);
31 } else {
32 key_stat &= DA9055_NOKEY_STS;
33 /*
34 * Onkey status bit is cleared when onkey button is released.
35 */
36 if (!key_stat) {
37 input_report_key(onkey->input, KEY_POWER, 0);
38 input_sync(onkey->input);
39 }
40 }
41
42 /*
43 * Interrupt is generated only when the ONKEY pin is asserted.
44 * Hence the deassertion of the pin is simulated through work queue.
45 */
46 if (key_stat)
47 schedule_delayed_work(&onkey->work, msecs_to_jiffies(10));
48
49 }
50
da9055_onkey_work(struct work_struct * work)51 static void da9055_onkey_work(struct work_struct *work)
52 {
53 struct da9055_onkey *onkey = container_of(work, struct da9055_onkey,
54 work.work);
55
56 da9055_onkey_query(onkey);
57 }
58
da9055_onkey_irq(int irq,void * data)59 static irqreturn_t da9055_onkey_irq(int irq, void *data)
60 {
61 struct da9055_onkey *onkey = data;
62
63 input_report_key(onkey->input, KEY_POWER, 1);
64 input_sync(onkey->input);
65
66 da9055_onkey_query(onkey);
67
68 return IRQ_HANDLED;
69 }
70
da9055_onkey_probe(struct platform_device * pdev)71 static int da9055_onkey_probe(struct platform_device *pdev)
72 {
73 struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent);
74 struct da9055_onkey *onkey;
75 struct input_dev *input_dev;
76 int irq, err;
77
78 irq = platform_get_irq_byname(pdev, "ONKEY");
79 if (irq < 0)
80 return -EINVAL;
81
82 onkey = devm_kzalloc(&pdev->dev, sizeof(*onkey), GFP_KERNEL);
83 if (!onkey) {
84 dev_err(&pdev->dev, "Failed to allocate memory\n");
85 return -ENOMEM;
86 }
87
88 input_dev = input_allocate_device();
89 if (!input_dev) {
90 dev_err(&pdev->dev, "Failed to allocate memory\n");
91 return -ENOMEM;
92 }
93
94 onkey->input = input_dev;
95 onkey->da9055 = da9055;
96 input_dev->name = "da9055-onkey";
97 input_dev->phys = "da9055-onkey/input0";
98 input_dev->dev.parent = &pdev->dev;
99
100 input_dev->evbit[0] = BIT_MASK(EV_KEY);
101 __set_bit(KEY_POWER, input_dev->keybit);
102
103 INIT_DELAYED_WORK(&onkey->work, da9055_onkey_work);
104
105 err = request_threaded_irq(irq, NULL, da9055_onkey_irq,
106 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
107 "ONKEY", onkey);
108 if (err < 0) {
109 dev_err(&pdev->dev,
110 "Failed to register ONKEY IRQ %d, error = %d\n",
111 irq, err);
112 goto err_free_input;
113 }
114
115 err = input_register_device(input_dev);
116 if (err) {
117 dev_err(&pdev->dev, "Unable to register input device, %d\n",
118 err);
119 goto err_free_irq;
120 }
121
122 platform_set_drvdata(pdev, onkey);
123
124 return 0;
125
126 err_free_irq:
127 free_irq(irq, onkey);
128 cancel_delayed_work_sync(&onkey->work);
129 err_free_input:
130 input_free_device(input_dev);
131
132 return err;
133 }
134
da9055_onkey_remove(struct platform_device * pdev)135 static void da9055_onkey_remove(struct platform_device *pdev)
136 {
137 struct da9055_onkey *onkey = platform_get_drvdata(pdev);
138 int irq = platform_get_irq_byname(pdev, "ONKEY");
139
140 irq = regmap_irq_get_virq(onkey->da9055->irq_data, irq);
141 free_irq(irq, onkey);
142 cancel_delayed_work_sync(&onkey->work);
143 input_unregister_device(onkey->input);
144 }
145
146 static struct platform_driver da9055_onkey_driver = {
147 .probe = da9055_onkey_probe,
148 .remove_new = da9055_onkey_remove,
149 .driver = {
150 .name = "da9055-onkey",
151 },
152 };
153
154 module_platform_driver(da9055_onkey_driver);
155
156 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
157 MODULE_DESCRIPTION("Onkey driver for DA9055");
158 MODULE_LICENSE("GPL");
159 MODULE_ALIAS("platform:da9055-onkey");
160