Merge tag 'mailbox-v4.14' of git://git.linaro.org/landing-teams/working/fujitsu/integ...
[sfrench/cifs-2.6.git] / drivers / input / misc / axp20x-pek.c
1 /*
2  * axp20x power button driver.
3  *
4  * Copyright (C) 2013 Carlo Caione <carlo@caione.org>
5  *
6  * This file is subject to the terms and conditions of the GNU General
7  * Public License. See the file "COPYING" in the main directory of this
8  * archive for more details.
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/acpi.h>
17 #include <linux/errno.h>
18 #include <linux/irq.h>
19 #include <linux/init.h>
20 #include <linux/input.h>
21 #include <linux/interrupt.h>
22 #include <linux/kernel.h>
23 #include <linux/mfd/axp20x.h>
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/regmap.h>
27 #include <linux/slab.h>
28
29 #define AXP20X_PEK_STARTUP_MASK         (0xc0)
30 #define AXP20X_PEK_SHUTDOWN_MASK        (0x03)
31
32 struct axp20x_pek {
33         struct axp20x_dev *axp20x;
34         struct input_dev *input;
35         int irq_dbr;
36         int irq_dbf;
37 };
38
39 struct axp20x_time {
40         unsigned int time;
41         unsigned int idx;
42 };
43
44 static const struct axp20x_time startup_time[] = {
45         { .time = 128,  .idx = 0 },
46         { .time = 1000, .idx = 2 },
47         { .time = 3000, .idx = 1 },
48         { .time = 2000, .idx = 3 },
49 };
50
51 static const struct axp20x_time shutdown_time[] = {
52         { .time = 4000,  .idx = 0 },
53         { .time = 6000,  .idx = 1 },
54         { .time = 8000,  .idx = 2 },
55         { .time = 10000, .idx = 3 },
56 };
57
58 struct axp20x_pek_ext_attr {
59         const struct axp20x_time *p_time;
60         unsigned int mask;
61 };
62
63 static struct axp20x_pek_ext_attr axp20x_pek_startup_ext_attr = {
64         .p_time = startup_time,
65         .mask   = AXP20X_PEK_STARTUP_MASK,
66 };
67
68 static struct axp20x_pek_ext_attr axp20x_pek_shutdown_ext_attr = {
69         .p_time = shutdown_time,
70         .mask   = AXP20X_PEK_SHUTDOWN_MASK,
71 };
72
73 static struct axp20x_pek_ext_attr *get_axp_ext_attr(struct device_attribute *attr)
74 {
75         return container_of(attr, struct dev_ext_attribute, attr)->var;
76 }
77
78 static ssize_t axp20x_show_ext_attr(struct device *dev,
79                                     struct device_attribute *attr, char *buf)
80 {
81         struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
82         struct axp20x_pek_ext_attr *axp20x_ea = get_axp_ext_attr(attr);
83         unsigned int val;
84         int ret, i;
85
86         ret = regmap_read(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY, &val);
87         if (ret != 0)
88                 return ret;
89
90         val &= axp20x_ea->mask;
91         val >>= ffs(axp20x_ea->mask) - 1;
92
93         for (i = 0; i < 4; i++)
94                 if (val == axp20x_ea->p_time[i].idx)
95                         val = axp20x_ea->p_time[i].time;
96
97         return sprintf(buf, "%u\n", val);
98 }
99
100 static ssize_t axp20x_store_ext_attr(struct device *dev,
101                                      struct device_attribute *attr,
102                                      const char *buf, size_t count)
103 {
104         struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
105         struct axp20x_pek_ext_attr *axp20x_ea = get_axp_ext_attr(attr);
106         char val_str[20];
107         size_t len;
108         int ret, i;
109         unsigned int val, idx = 0;
110         unsigned int best_err = UINT_MAX;
111
112         val_str[sizeof(val_str) - 1] = '\0';
113         strncpy(val_str, buf, sizeof(val_str) - 1);
114         len = strlen(val_str);
115
116         if (len && val_str[len - 1] == '\n')
117                 val_str[len - 1] = '\0';
118
119         ret = kstrtouint(val_str, 10, &val);
120         if (ret)
121                 return ret;
122
123         for (i = 3; i >= 0; i--) {
124                 unsigned int err;
125
126                 err = abs(axp20x_ea->p_time[i].time - val);
127                 if (err < best_err) {
128                         best_err = err;
129                         idx = axp20x_ea->p_time[i].idx;
130                 }
131
132                 if (!err)
133                         break;
134         }
135
136         idx <<= ffs(axp20x_ea->mask) - 1;
137         ret = regmap_update_bits(axp20x_pek->axp20x->regmap,
138                                  AXP20X_PEK_KEY,
139                                  axp20x_ea->mask, idx);
140         if (ret != 0)
141                 return -EINVAL;
142
143         return count;
144 }
145
146 static struct dev_ext_attribute axp20x_dev_attr_startup = {
147         .attr   = __ATTR(startup, 0644, axp20x_show_ext_attr, axp20x_store_ext_attr),
148         .var    = &axp20x_pek_startup_ext_attr,
149 };
150
151 static struct dev_ext_attribute axp20x_dev_attr_shutdown = {
152         .attr   = __ATTR(shutdown, 0644, axp20x_show_ext_attr, axp20x_store_ext_attr),
153         .var    = &axp20x_pek_shutdown_ext_attr,
154 };
155
156 static struct attribute *axp20x_attributes[] = {
157         &axp20x_dev_attr_startup.attr.attr,
158         &axp20x_dev_attr_shutdown.attr.attr,
159         NULL,
160 };
161
162 static const struct attribute_group axp20x_attribute_group = {
163         .attrs = axp20x_attributes,
164 };
165
166 static irqreturn_t axp20x_pek_irq(int irq, void *pwr)
167 {
168         struct input_dev *idev = pwr;
169         struct axp20x_pek *axp20x_pek = input_get_drvdata(idev);
170
171         /*
172          * The power-button is connected to ground so a falling edge (dbf)
173          * means it is pressed.
174          */
175         if (irq == axp20x_pek->irq_dbf)
176                 input_report_key(idev, KEY_POWER, true);
177         else if (irq == axp20x_pek->irq_dbr)
178                 input_report_key(idev, KEY_POWER, false);
179
180         input_sync(idev);
181
182         return IRQ_HANDLED;
183 }
184
185 static int axp20x_pek_probe_input_device(struct axp20x_pek *axp20x_pek,
186                                          struct platform_device *pdev)
187 {
188         struct axp20x_dev *axp20x = axp20x_pek->axp20x;
189         struct input_dev *idev;
190         int error;
191
192         axp20x_pek->irq_dbr = platform_get_irq_byname(pdev, "PEK_DBR");
193         if (axp20x_pek->irq_dbr < 0) {
194                 dev_err(&pdev->dev, "No IRQ for PEK_DBR, error=%d\n",
195                                 axp20x_pek->irq_dbr);
196                 return axp20x_pek->irq_dbr;
197         }
198         axp20x_pek->irq_dbr = regmap_irq_get_virq(axp20x->regmap_irqc,
199                                                   axp20x_pek->irq_dbr);
200
201         axp20x_pek->irq_dbf = platform_get_irq_byname(pdev, "PEK_DBF");
202         if (axp20x_pek->irq_dbf < 0) {
203                 dev_err(&pdev->dev, "No IRQ for PEK_DBF, error=%d\n",
204                                 axp20x_pek->irq_dbf);
205                 return axp20x_pek->irq_dbf;
206         }
207         axp20x_pek->irq_dbf = regmap_irq_get_virq(axp20x->regmap_irqc,
208                                                   axp20x_pek->irq_dbf);
209
210         axp20x_pek->input = devm_input_allocate_device(&pdev->dev);
211         if (!axp20x_pek->input)
212                 return -ENOMEM;
213
214         idev = axp20x_pek->input;
215
216         idev->name = "axp20x-pek";
217         idev->phys = "m1kbd/input2";
218         idev->dev.parent = &pdev->dev;
219
220         input_set_capability(idev, EV_KEY, KEY_POWER);
221
222         input_set_drvdata(idev, axp20x_pek);
223
224         error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbr,
225                                              axp20x_pek_irq, 0,
226                                              "axp20x-pek-dbr", idev);
227         if (error < 0) {
228                 dev_err(&pdev->dev, "Failed to request dbr IRQ#%d: %d\n",
229                         axp20x_pek->irq_dbr, error);
230                 return error;
231         }
232
233         error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbf,
234                                           axp20x_pek_irq, 0,
235                                           "axp20x-pek-dbf", idev);
236         if (error < 0) {
237                 dev_err(&pdev->dev, "Failed to request dbf IRQ#%d: %d\n",
238                         axp20x_pek->irq_dbf, error);
239                 return error;
240         }
241
242         error = input_register_device(idev);
243         if (error) {
244                 dev_err(&pdev->dev, "Can't register input device: %d\n",
245                         error);
246                 return error;
247         }
248
249         if (axp20x_pek->axp20x->variant == AXP288_ID)
250                 enable_irq_wake(axp20x_pek->irq_dbr);
251
252         return 0;
253 }
254
255 #ifdef CONFIG_ACPI
256 static bool axp20x_pek_should_register_input(struct axp20x_pek *axp20x_pek,
257                                              struct platform_device *pdev)
258 {
259         unsigned long long hrv = 0;
260         acpi_status status;
261
262         if (IS_ENABLED(CONFIG_INPUT_SOC_BUTTON_ARRAY) &&
263             axp20x_pek->axp20x->variant == AXP288_ID) {
264                 status = acpi_evaluate_integer(ACPI_HANDLE(pdev->dev.parent),
265                                                "_HRV", NULL, &hrv);
266                 if (ACPI_FAILURE(status))
267                         dev_err(&pdev->dev, "Failed to get PMIC hardware revision\n");
268
269                 /*
270                  * On Cherry Trail platforms (hrv == 3), do not register the
271                  * input device if there is an "INTCFD9" or "ACPI0011" gpio
272                  * button ACPI device, as that handles the power button too,
273                  * and otherwise we end up reporting all presses twice.
274                  */
275                 if (hrv == 3 && (acpi_dev_present("INTCFD9", NULL, -1) ||
276                                  acpi_dev_present("ACPI0011", NULL, -1)))
277                         return false;
278
279         }
280
281         return true;
282 }
283 #else
284 static bool axp20x_pek_should_register_input(struct axp20x_pek *axp20x_pek,
285                                              struct platform_device *pdev)
286 {
287         return true;
288 }
289 #endif
290
291 static int axp20x_pek_probe(struct platform_device *pdev)
292 {
293         struct axp20x_pek *axp20x_pek;
294         int error;
295
296         axp20x_pek = devm_kzalloc(&pdev->dev, sizeof(struct axp20x_pek),
297                                   GFP_KERNEL);
298         if (!axp20x_pek)
299                 return -ENOMEM;
300
301         axp20x_pek->axp20x = dev_get_drvdata(pdev->dev.parent);
302
303         if (axp20x_pek_should_register_input(axp20x_pek, pdev)) {
304                 error = axp20x_pek_probe_input_device(axp20x_pek, pdev);
305                 if (error)
306                         return error;
307         }
308
309         error = devm_device_add_group(&pdev->dev, &axp20x_attribute_group);
310         if (error) {
311                 dev_err(&pdev->dev, "Failed to create sysfs attributes: %d\n",
312                         error);
313                 return error;
314         }
315
316         platform_set_drvdata(pdev, axp20x_pek);
317
318         return 0;
319 }
320
321 static int __maybe_unused axp20x_pek_resume_noirq(struct device *dev)
322 {
323         struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
324
325         if (axp20x_pek->axp20x->variant != AXP288_ID)
326                 return 0;
327
328         /*
329          * Clear interrupts from button presses during suspend, to avoid
330          * a wakeup power-button press getting reported to userspace.
331          */
332         regmap_write(axp20x_pek->axp20x->regmap,
333                      AXP20X_IRQ1_STATE + AXP288_IRQ_POKN / 8,
334                      BIT(AXP288_IRQ_POKN % 8));
335
336         return 0;
337 }
338
339 static const struct dev_pm_ops axp20x_pek_pm_ops = {
340 #ifdef CONFIG_PM_SLEEP
341         .resume_noirq = axp20x_pek_resume_noirq,
342 #endif
343 };
344
345 static struct platform_driver axp20x_pek_driver = {
346         .probe          = axp20x_pek_probe,
347         .driver         = {
348                 .name           = "axp20x-pek",
349                 .pm             = &axp20x_pek_pm_ops,
350         },
351 };
352 module_platform_driver(axp20x_pek_driver);
353
354 MODULE_DESCRIPTION("axp20x Power Button");
355 MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
356 MODULE_LICENSE("GPL");
357 MODULE_ALIAS("platform:axp20x-pek");