xref: /openbmc/linux/drivers/input/misc/axp20x-pek.c (revision f8e17c17)
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_info {
33 	const struct axp20x_time *startup_time;
34 	unsigned int startup_mask;
35 	const struct axp20x_time *shutdown_time;
36 	unsigned int shutdown_mask;
37 };
38 
39 struct axp20x_pek {
40 	struct axp20x_dev *axp20x;
41 	struct input_dev *input;
42 	struct axp20x_info *info;
43 	int irq_dbr;
44 	int irq_dbf;
45 };
46 
47 struct axp20x_time {
48 	unsigned int time;
49 	unsigned int idx;
50 };
51 
52 static const struct axp20x_time startup_time[] = {
53 	{ .time = 128,  .idx = 0 },
54 	{ .time = 1000, .idx = 2 },
55 	{ .time = 3000, .idx = 1 },
56 	{ .time = 2000, .idx = 3 },
57 };
58 
59 static const struct axp20x_time axp221_startup_time[] = {
60 	{ .time = 128,  .idx = 0 },
61 	{ .time = 1000, .idx = 1 },
62 	{ .time = 2000, .idx = 2 },
63 	{ .time = 3000, .idx = 3 },
64 };
65 
66 static const struct axp20x_time shutdown_time[] = {
67 	{ .time = 4000,  .idx = 0 },
68 	{ .time = 6000,  .idx = 1 },
69 	{ .time = 8000,  .idx = 2 },
70 	{ .time = 10000, .idx = 3 },
71 };
72 
73 static const struct axp20x_info axp20x_info = {
74 	.startup_time = startup_time,
75 	.startup_mask = AXP20X_PEK_STARTUP_MASK,
76 	.shutdown_time = shutdown_time,
77 	.shutdown_mask = AXP20X_PEK_SHUTDOWN_MASK,
78 };
79 
80 static const struct axp20x_info axp221_info = {
81 	.startup_time = axp221_startup_time,
82 	.startup_mask = AXP20X_PEK_STARTUP_MASK,
83 	.shutdown_time = shutdown_time,
84 	.shutdown_mask = AXP20X_PEK_SHUTDOWN_MASK,
85 };
86 
87 static ssize_t axp20x_show_attr(struct device *dev,
88 				const struct axp20x_time *time,
89 				unsigned int mask, char *buf)
90 {
91 	struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
92 	unsigned int val;
93 	int ret, i;
94 
95 	ret = regmap_read(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY, &val);
96 	if (ret != 0)
97 		return ret;
98 
99 	val &= mask;
100 	val >>= ffs(mask) - 1;
101 
102 	for (i = 0; i < 4; i++)
103 		if (val == time[i].idx)
104 			val = time[i].time;
105 
106 	return sprintf(buf, "%u\n", val);
107 }
108 
109 static ssize_t axp20x_show_attr_startup(struct device *dev,
110 					struct device_attribute *attr,
111 					char *buf)
112 {
113 	struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
114 
115 	return axp20x_show_attr(dev, axp20x_pek->info->startup_time,
116 				axp20x_pek->info->startup_mask, buf);
117 }
118 
119 static ssize_t axp20x_show_attr_shutdown(struct device *dev,
120 					 struct device_attribute *attr,
121 					 char *buf)
122 {
123 	struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
124 
125 	return axp20x_show_attr(dev, axp20x_pek->info->shutdown_time,
126 				axp20x_pek->info->shutdown_mask, buf);
127 }
128 
129 static ssize_t axp20x_store_attr(struct device *dev,
130 				 const struct axp20x_time *time,
131 				 unsigned int mask, const char *buf,
132 				 size_t count)
133 {
134 	struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
135 	char val_str[20];
136 	size_t len;
137 	int ret, i;
138 	unsigned int val, idx = 0;
139 	unsigned int best_err = UINT_MAX;
140 
141 	val_str[sizeof(val_str) - 1] = '\0';
142 	strncpy(val_str, buf, sizeof(val_str) - 1);
143 	len = strlen(val_str);
144 
145 	if (len && val_str[len - 1] == '\n')
146 		val_str[len - 1] = '\0';
147 
148 	ret = kstrtouint(val_str, 10, &val);
149 	if (ret)
150 		return ret;
151 
152 	for (i = 3; i >= 0; i--) {
153 		unsigned int err;
154 
155 		err = abs(time[i].time - val);
156 		if (err < best_err) {
157 			best_err = err;
158 			idx = time[i].idx;
159 		}
160 
161 		if (!err)
162 			break;
163 	}
164 
165 	idx <<= ffs(mask) - 1;
166 	ret = regmap_update_bits(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY,
167 				 mask, idx);
168 	if (ret != 0)
169 		return -EINVAL;
170 
171 	return count;
172 }
173 
174 static ssize_t axp20x_store_attr_startup(struct device *dev,
175 					 struct device_attribute *attr,
176 					 const char *buf, size_t count)
177 {
178 	struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
179 
180 	return axp20x_store_attr(dev, axp20x_pek->info->startup_time,
181 				 axp20x_pek->info->startup_mask, buf, count);
182 }
183 
184 static ssize_t axp20x_store_attr_shutdown(struct device *dev,
185 					  struct device_attribute *attr,
186 					  const char *buf, size_t count)
187 {
188 	struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
189 
190 	return axp20x_store_attr(dev, axp20x_pek->info->shutdown_time,
191 				 axp20x_pek->info->shutdown_mask, buf, count);
192 }
193 
194 static DEVICE_ATTR(startup, 0644, axp20x_show_attr_startup,
195 		   axp20x_store_attr_startup);
196 static DEVICE_ATTR(shutdown, 0644, axp20x_show_attr_shutdown,
197 		   axp20x_store_attr_shutdown);
198 
199 static struct attribute *axp20x_attrs[] = {
200 	&dev_attr_startup.attr,
201 	&dev_attr_shutdown.attr,
202 	NULL,
203 };
204 ATTRIBUTE_GROUPS(axp20x);
205 
206 static irqreturn_t axp20x_pek_irq(int irq, void *pwr)
207 {
208 	struct input_dev *idev = pwr;
209 	struct axp20x_pek *axp20x_pek = input_get_drvdata(idev);
210 
211 	/*
212 	 * The power-button is connected to ground so a falling edge (dbf)
213 	 * means it is pressed.
214 	 */
215 	if (irq == axp20x_pek->irq_dbf)
216 		input_report_key(idev, KEY_POWER, true);
217 	else if (irq == axp20x_pek->irq_dbr)
218 		input_report_key(idev, KEY_POWER, false);
219 
220 	input_sync(idev);
221 
222 	return IRQ_HANDLED;
223 }
224 
225 static int axp20x_pek_probe_input_device(struct axp20x_pek *axp20x_pek,
226 					 struct platform_device *pdev)
227 {
228 	struct axp20x_dev *axp20x = axp20x_pek->axp20x;
229 	struct input_dev *idev;
230 	int error;
231 
232 	axp20x_pek->irq_dbr = platform_get_irq_byname(pdev, "PEK_DBR");
233 	if (axp20x_pek->irq_dbr < 0)
234 		return axp20x_pek->irq_dbr;
235 	axp20x_pek->irq_dbr = regmap_irq_get_virq(axp20x->regmap_irqc,
236 						  axp20x_pek->irq_dbr);
237 
238 	axp20x_pek->irq_dbf = platform_get_irq_byname(pdev, "PEK_DBF");
239 	if (axp20x_pek->irq_dbf < 0)
240 		return axp20x_pek->irq_dbf;
241 	axp20x_pek->irq_dbf = regmap_irq_get_virq(axp20x->regmap_irqc,
242 						  axp20x_pek->irq_dbf);
243 
244 	axp20x_pek->input = devm_input_allocate_device(&pdev->dev);
245 	if (!axp20x_pek->input)
246 		return -ENOMEM;
247 
248 	idev = axp20x_pek->input;
249 
250 	idev->name = "axp20x-pek";
251 	idev->phys = "m1kbd/input2";
252 	idev->dev.parent = &pdev->dev;
253 
254 	input_set_capability(idev, EV_KEY, KEY_POWER);
255 
256 	input_set_drvdata(idev, axp20x_pek);
257 
258 	error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbr,
259 					     axp20x_pek_irq, 0,
260 					     "axp20x-pek-dbr", idev);
261 	if (error < 0) {
262 		dev_err(&pdev->dev, "Failed to request dbr IRQ#%d: %d\n",
263 			axp20x_pek->irq_dbr, error);
264 		return error;
265 	}
266 
267 	error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbf,
268 					  axp20x_pek_irq, 0,
269 					  "axp20x-pek-dbf", idev);
270 	if (error < 0) {
271 		dev_err(&pdev->dev, "Failed to request dbf IRQ#%d: %d\n",
272 			axp20x_pek->irq_dbf, error);
273 		return error;
274 	}
275 
276 	error = input_register_device(idev);
277 	if (error) {
278 		dev_err(&pdev->dev, "Can't register input device: %d\n",
279 			error);
280 		return error;
281 	}
282 
283 	device_init_wakeup(&pdev->dev, true);
284 
285 	return 0;
286 }
287 
288 #ifdef CONFIG_ACPI
289 static bool axp20x_pek_should_register_input(struct axp20x_pek *axp20x_pek,
290 					     struct platform_device *pdev)
291 {
292 	unsigned long long hrv = 0;
293 	acpi_status status;
294 
295 	if (IS_ENABLED(CONFIG_INPUT_SOC_BUTTON_ARRAY) &&
296 	    axp20x_pek->axp20x->variant == AXP288_ID) {
297 		status = acpi_evaluate_integer(ACPI_HANDLE(pdev->dev.parent),
298 					       "_HRV", NULL, &hrv);
299 		if (ACPI_FAILURE(status))
300 			dev_err(&pdev->dev, "Failed to get PMIC hardware revision\n");
301 
302 		/*
303 		 * On Cherry Trail platforms (hrv == 3), do not register the
304 		 * input device if there is an "INTCFD9" or "ACPI0011" gpio
305 		 * button ACPI device, as that handles the power button too,
306 		 * and otherwise we end up reporting all presses twice.
307 		 */
308 		if (hrv == 3 && (acpi_dev_present("INTCFD9", NULL, -1) ||
309 				 acpi_dev_present("ACPI0011", NULL, -1)))
310 			return false;
311 
312 	}
313 
314 	return true;
315 }
316 #else
317 static bool axp20x_pek_should_register_input(struct axp20x_pek *axp20x_pek,
318 					     struct platform_device *pdev)
319 {
320 	return true;
321 }
322 #endif
323 
324 static int axp20x_pek_probe(struct platform_device *pdev)
325 {
326 	struct axp20x_pek *axp20x_pek;
327 	const struct platform_device_id *match = platform_get_device_id(pdev);
328 	int error;
329 
330 	if (!match) {
331 		dev_err(&pdev->dev, "Failed to get platform_device_id\n");
332 		return -EINVAL;
333 	}
334 
335 	axp20x_pek = devm_kzalloc(&pdev->dev, sizeof(struct axp20x_pek),
336 				  GFP_KERNEL);
337 	if (!axp20x_pek)
338 		return -ENOMEM;
339 
340 	axp20x_pek->axp20x = dev_get_drvdata(pdev->dev.parent);
341 
342 	if (axp20x_pek_should_register_input(axp20x_pek, pdev)) {
343 		error = axp20x_pek_probe_input_device(axp20x_pek, pdev);
344 		if (error)
345 			return error;
346 	}
347 
348 	axp20x_pek->info = (struct axp20x_info *)match->driver_data;
349 
350 	platform_set_drvdata(pdev, axp20x_pek);
351 
352 	return 0;
353 }
354 
355 static int __maybe_unused axp20x_pek_suspend(struct device *dev)
356 {
357 	struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
358 
359 	/*
360 	 * As nested threaded IRQs are not automatically disabled during
361 	 * suspend, we must explicitly disable non-wakeup IRQs.
362 	 */
363 	if (device_may_wakeup(dev)) {
364 		enable_irq_wake(axp20x_pek->irq_dbf);
365 		enable_irq_wake(axp20x_pek->irq_dbr);
366 	} else {
367 		disable_irq(axp20x_pek->irq_dbf);
368 		disable_irq(axp20x_pek->irq_dbr);
369 	}
370 
371 	return 0;
372 }
373 
374 static int __maybe_unused axp20x_pek_resume(struct device *dev)
375 {
376 	struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
377 
378 	if (device_may_wakeup(dev)) {
379 		disable_irq_wake(axp20x_pek->irq_dbf);
380 		disable_irq_wake(axp20x_pek->irq_dbr);
381 	} else {
382 		enable_irq(axp20x_pek->irq_dbf);
383 		enable_irq(axp20x_pek->irq_dbr);
384 	}
385 
386 	return 0;
387 }
388 
389 static int __maybe_unused axp20x_pek_resume_noirq(struct device *dev)
390 {
391 	struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
392 
393 	if (axp20x_pek->axp20x->variant != AXP288_ID)
394 		return 0;
395 
396 	/*
397 	 * Clear interrupts from button presses during suspend, to avoid
398 	 * a wakeup power-button press getting reported to userspace.
399 	 */
400 	regmap_write(axp20x_pek->axp20x->regmap,
401 		     AXP20X_IRQ1_STATE + AXP288_IRQ_POKN / 8,
402 		     BIT(AXP288_IRQ_POKN % 8));
403 
404 	return 0;
405 }
406 
407 static const struct dev_pm_ops axp20x_pek_pm_ops = {
408 	SET_SYSTEM_SLEEP_PM_OPS(axp20x_pek_suspend, axp20x_pek_resume)
409 #ifdef CONFIG_PM_SLEEP
410 	.resume_noirq = axp20x_pek_resume_noirq,
411 #endif
412 };
413 
414 static const struct platform_device_id axp_pek_id_match[] = {
415 	{
416 		.name = "axp20x-pek",
417 		.driver_data = (kernel_ulong_t)&axp20x_info,
418 	},
419 	{
420 		.name = "axp221-pek",
421 		.driver_data = (kernel_ulong_t)&axp221_info,
422 	},
423 	{ /* sentinel */ }
424 };
425 MODULE_DEVICE_TABLE(platform, axp_pek_id_match);
426 
427 static struct platform_driver axp20x_pek_driver = {
428 	.probe		= axp20x_pek_probe,
429 	.id_table	= axp_pek_id_match,
430 	.driver		= {
431 		.name		= "axp20x-pek",
432 		.pm		= &axp20x_pek_pm_ops,
433 		.dev_groups	= axp20x_groups,
434 	},
435 };
436 module_platform_driver(axp20x_pek_driver);
437 
438 MODULE_DESCRIPTION("axp20x Power Button");
439 MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
440 MODULE_LICENSE("GPL");
441