GNU Linux-libre 4.4.297-gnu1
[releases.git] / drivers / input / misc / da9063_onkey.c
1 /*
2  * OnKey device driver for DA9063 and DA9062 PMICs
3  * Copyright (C) 2015  Dialog Semiconductor Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include <linux/module.h>
17 #include <linux/errno.h>
18 #include <linux/input.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
21 #include <linux/workqueue.h>
22 #include <linux/regmap.h>
23 #include <linux/of.h>
24 #include <linux/mfd/da9063/core.h>
25 #include <linux/mfd/da9063/pdata.h>
26 #include <linux/mfd/da9063/registers.h>
27 #include <linux/mfd/da9062/core.h>
28 #include <linux/mfd/da9062/registers.h>
29
30 struct da906x_chip_config {
31         /* REGS */
32         int onkey_status;
33         int onkey_pwr_signalling;
34         int onkey_fault_log;
35         int onkey_shutdown;
36         /* MASKS */
37         int onkey_nonkey_mask;
38         int onkey_nonkey_lock_mask;
39         int onkey_key_reset_mask;
40         int onkey_shutdown_mask;
41         /* NAMES */
42         const char *name;
43 };
44
45 struct da9063_onkey {
46         struct delayed_work work;
47         struct input_dev *input;
48         struct device *dev;
49         struct regmap *regmap;
50         const struct da906x_chip_config *config;
51         char phys[32];
52         bool key_power;
53 };
54
55 static const struct da906x_chip_config da9063_regs = {
56         /* REGS */
57         .onkey_status = DA9063_REG_STATUS_A,
58         .onkey_pwr_signalling = DA9063_REG_CONTROL_B,
59         .onkey_fault_log = DA9063_REG_FAULT_LOG,
60         .onkey_shutdown = DA9063_REG_CONTROL_F,
61         /* MASKS */
62         .onkey_nonkey_mask = DA9063_NONKEY,
63         .onkey_nonkey_lock_mask = DA9063_NONKEY_LOCK,
64         .onkey_key_reset_mask = DA9063_KEY_RESET,
65         .onkey_shutdown_mask = DA9063_SHUTDOWN,
66         /* NAMES */
67         .name = DA9063_DRVNAME_ONKEY,
68 };
69
70 static const struct da906x_chip_config da9062_regs = {
71         /* REGS */
72         .onkey_status = DA9062AA_STATUS_A,
73         .onkey_pwr_signalling = DA9062AA_CONTROL_B,
74         .onkey_fault_log = DA9062AA_FAULT_LOG,
75         .onkey_shutdown = DA9062AA_CONTROL_F,
76         /* MASKS */
77         .onkey_nonkey_mask = DA9062AA_NONKEY_MASK,
78         .onkey_nonkey_lock_mask = DA9062AA_NONKEY_LOCK_MASK,
79         .onkey_key_reset_mask = DA9062AA_KEY_RESET_MASK,
80         .onkey_shutdown_mask = DA9062AA_SHUTDOWN_MASK,
81         /* NAMES */
82         .name = "da9062-onkey",
83 };
84
85 static const struct of_device_id da9063_compatible_reg_id_table[] = {
86         { .compatible = "dlg,da9063-onkey", .data = &da9063_regs },
87         { .compatible = "dlg,da9062-onkey", .data = &da9062_regs },
88         { },
89 };
90
91 static void da9063_poll_on(struct work_struct *work)
92 {
93         struct da9063_onkey *onkey = container_of(work,
94                                                 struct da9063_onkey,
95                                                 work.work);
96         const struct da906x_chip_config *config = onkey->config;
97         unsigned int val;
98         int fault_log = 0;
99         bool poll = true;
100         int error;
101
102         /* Poll to see when the pin is released */
103         error = regmap_read(onkey->regmap,
104                             config->onkey_status,
105                             &val);
106         if (error) {
107                 dev_err(onkey->dev,
108                         "Failed to read ON status: %d\n", error);
109                 goto err_poll;
110         }
111
112         if (!(val & config->onkey_nonkey_mask)) {
113                 error = regmap_update_bits(onkey->regmap,
114                                            config->onkey_pwr_signalling,
115                                            config->onkey_nonkey_lock_mask,
116                                            0);
117                 if (error) {
118                         dev_err(onkey->dev,
119                                 "Failed to reset the Key Delay %d\n", error);
120                         goto err_poll;
121                 }
122
123                 input_report_key(onkey->input, KEY_POWER, 0);
124                 input_sync(onkey->input);
125
126                 poll = false;
127         }
128
129         /*
130          * If the fault log KEY_RESET is detected, then clear it
131          * and shut down the system.
132          */
133         error = regmap_read(onkey->regmap,
134                             config->onkey_fault_log,
135                             &fault_log);
136         if (error) {
137                 dev_warn(&onkey->input->dev,
138                          "Cannot read FAULT_LOG: %d\n", error);
139         } else if (fault_log & config->onkey_key_reset_mask) {
140                 error = regmap_write(onkey->regmap,
141                                      config->onkey_fault_log,
142                                      config->onkey_key_reset_mask);
143                 if (error) {
144                         dev_warn(&onkey->input->dev,
145                                  "Cannot reset KEY_RESET fault log: %d\n",
146                                  error);
147                 } else {
148                         /* at this point we do any S/W housekeeping
149                          * and then send shutdown command
150                          */
151                         dev_dbg(&onkey->input->dev,
152                                 "Sending SHUTDOWN to DA9063 ...\n");
153                         error = regmap_write(onkey->regmap,
154                                              config->onkey_shutdown,
155                                              config->onkey_shutdown_mask);
156                         if (error)
157                                 dev_err(&onkey->input->dev,
158                                         "Cannot SHUTDOWN DA9063: %d\n",
159                                         error);
160                 }
161         }
162
163 err_poll:
164         if (poll)
165                 schedule_delayed_work(&onkey->work, msecs_to_jiffies(50));
166 }
167
168 static irqreturn_t da9063_onkey_irq_handler(int irq, void *data)
169 {
170         struct da9063_onkey *onkey = data;
171         const struct da906x_chip_config *config = onkey->config;
172         unsigned int val;
173         int error;
174
175         error = regmap_read(onkey->regmap,
176                             config->onkey_status,
177                             &val);
178         if (onkey->key_power && !error && (val & config->onkey_nonkey_mask)) {
179                 input_report_key(onkey->input, KEY_POWER, 1);
180                 input_sync(onkey->input);
181                 schedule_delayed_work(&onkey->work, 0);
182                 dev_dbg(onkey->dev, "KEY_POWER pressed.\n");
183         } else {
184                 input_report_key(onkey->input, KEY_SLEEP, 1);
185                 input_sync(onkey->input);
186                 input_report_key(onkey->input, KEY_SLEEP, 0);
187                 input_sync(onkey->input);
188                 dev_dbg(onkey->dev, "KEY_SLEEP pressed.\n");
189         }
190
191         return IRQ_HANDLED;
192 }
193
194 static void da9063_cancel_poll(void *data)
195 {
196         struct da9063_onkey *onkey = data;
197
198         cancel_delayed_work_sync(&onkey->work);
199 }
200
201 static int da9063_onkey_probe(struct platform_device *pdev)
202 {
203         struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
204         struct da9063_pdata *pdata = dev_get_platdata(da9063->dev);
205         struct da9063_onkey *onkey;
206         const struct of_device_id *match;
207         int irq;
208         int error;
209
210         match = of_match_node(da9063_compatible_reg_id_table,
211                               pdev->dev.of_node);
212         if (!match)
213                 return -ENXIO;
214
215         onkey = devm_kzalloc(&pdev->dev, sizeof(struct da9063_onkey),
216                              GFP_KERNEL);
217         if (!onkey) {
218                 dev_err(&pdev->dev, "Failed to allocate memory.\n");
219                 return -ENOMEM;
220         }
221
222         onkey->config = match->data;
223         onkey->dev = &pdev->dev;
224
225         onkey->regmap = dev_get_regmap(pdev->dev.parent, NULL);
226         if (!onkey->regmap) {
227                 dev_err(&pdev->dev, "Parent regmap unavailable.\n");
228                 return -ENXIO;
229         }
230
231         if (pdata)
232                 onkey->key_power = pdata->key_power;
233         else
234                 onkey->key_power =
235                         !of_property_read_bool(pdev->dev.of_node,
236                                                "dlg,disable-key-power");
237
238         onkey->input = devm_input_allocate_device(&pdev->dev);
239         if (!onkey->input) {
240                 dev_err(&pdev->dev, "Failed to allocated input device.\n");
241                 return -ENOMEM;
242         }
243
244         onkey->input->name = onkey->config->name;
245         snprintf(onkey->phys, sizeof(onkey->phys), "%s/input0",
246                  onkey->config->name);
247         onkey->input->phys = onkey->phys;
248         onkey->input->dev.parent = &pdev->dev;
249
250         if (onkey->key_power)
251                 input_set_capability(onkey->input, EV_KEY, KEY_POWER);
252
253         input_set_capability(onkey->input, EV_KEY, KEY_SLEEP);
254
255         INIT_DELAYED_WORK(&onkey->work, da9063_poll_on);
256
257         error = devm_add_action(&pdev->dev, da9063_cancel_poll, onkey);
258         if (error) {
259                 dev_err(&pdev->dev,
260                         "Failed to add cancel poll action: %d\n",
261                         error);
262                 return error;
263         }
264
265         irq = platform_get_irq_byname(pdev, "ONKEY");
266         if (irq < 0) {
267                 error = irq;
268                 dev_err(&pdev->dev, "Failed to get platform IRQ: %d\n", error);
269                 return error;
270         }
271
272         error = devm_request_threaded_irq(&pdev->dev, irq,
273                                           NULL, da9063_onkey_irq_handler,
274                                           IRQF_TRIGGER_LOW | IRQF_ONESHOT,
275                                           "ONKEY", onkey);
276         if (error) {
277                 dev_err(&pdev->dev,
278                         "Failed to request IRQ %d: %d\n", irq, error);
279                 return error;
280         }
281
282         error = input_register_device(onkey->input);
283         if (error) {
284                 dev_err(&pdev->dev,
285                         "Failed to register input device: %d\n", error);
286                 return error;
287         }
288
289         platform_set_drvdata(pdev, onkey);
290         return 0;
291 }
292
293 static struct platform_driver da9063_onkey_driver = {
294         .probe  = da9063_onkey_probe,
295         .driver = {
296                 .name   = DA9063_DRVNAME_ONKEY,
297                 .of_match_table = da9063_compatible_reg_id_table,
298         },
299 };
300 module_platform_driver(da9063_onkey_driver);
301
302 MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
303 MODULE_DESCRIPTION("Onkey device driver for Dialog DA9063 and DA9062");
304 MODULE_LICENSE("GPL");
305 MODULE_ALIAS("platform:" DA9063_DRVNAME_ONKEY);