xref: /openbmc/linux/drivers/input/keyboard/gpio_keys.c (revision 558a5e296a02266ef43d6e933ee35df9976de987)
178a56aabSPhil Blundell /*
278a56aabSPhil Blundell  * Driver for keys on GPIO lines capable of generating interrupts.
378a56aabSPhil Blundell  *
478a56aabSPhil Blundell  * Copyright 2005 Phil Blundell
578a56aabSPhil Blundell  *
678a56aabSPhil Blundell  * This program is free software; you can redistribute it and/or modify
778a56aabSPhil Blundell  * it under the terms of the GNU General Public License version 2 as
878a56aabSPhil Blundell  * published by the Free Software Foundation.
978a56aabSPhil Blundell  */
1078a56aabSPhil Blundell 
1178a56aabSPhil Blundell #include <linux/module.h>
1278a56aabSPhil Blundell 
1378a56aabSPhil Blundell #include <linux/init.h>
1478a56aabSPhil Blundell #include <linux/fs.h>
1578a56aabSPhil Blundell #include <linux/interrupt.h>
1678a56aabSPhil Blundell #include <linux/irq.h>
1778a56aabSPhil Blundell #include <linux/sched.h>
1878a56aabSPhil Blundell #include <linux/pm.h>
1978a56aabSPhil Blundell #include <linux/sysctl.h>
2078a56aabSPhil Blundell #include <linux/proc_fs.h>
2178a56aabSPhil Blundell #include <linux/delay.h>
2278a56aabSPhil Blundell #include <linux/platform_device.h>
2378a56aabSPhil Blundell #include <linux/input.h>
2449015beeSDavid Brownell #include <linux/gpio_keys.h>
25da0d03feSJani Nikula #include <linux/workqueue.h>
2678a56aabSPhil Blundell 
270d98f6bbSPhilipp Zabel #include <asm/gpio.h>
2878a56aabSPhil Blundell 
29a33466e3SDmitry Baryshkov struct gpio_button_data {
30a33466e3SDmitry Baryshkov 	struct gpio_keys_button *button;
31a33466e3SDmitry Baryshkov 	struct input_dev *input;
32ca865a77SJani Nikula 	struct timer_list timer;
33da0d03feSJani Nikula 	struct work_struct work;
34a33466e3SDmitry Baryshkov };
35a33466e3SDmitry Baryshkov 
36a33466e3SDmitry Baryshkov struct gpio_keys_drvdata {
37a33466e3SDmitry Baryshkov 	struct input_dev *input;
38a33466e3SDmitry Baryshkov 	struct gpio_button_data data[0];
39a33466e3SDmitry Baryshkov };
40a33466e3SDmitry Baryshkov 
41da0d03feSJani Nikula static void gpio_keys_report_event(struct work_struct *work)
4278a56aabSPhil Blundell {
43da0d03feSJani Nikula 	struct gpio_button_data *bdata =
44da0d03feSJani Nikula 		container_of(work, struct gpio_button_data, work);
45ce25d7e9SUwe Kleine-König 	struct gpio_keys_button *button = bdata->button;
46ce25d7e9SUwe Kleine-König 	struct input_dev *input = bdata->input;
4784767d00SRoman Moravcik 	unsigned int type = button->type ?: EV_KEY;
48a33466e3SDmitry Baryshkov 	int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
4984767d00SRoman Moravcik 
5084767d00SRoman Moravcik 	input_event(input, type, button->code, !!state);
5178a56aabSPhil Blundell 	input_sync(input);
52a33466e3SDmitry Baryshkov }
53a33466e3SDmitry Baryshkov 
54da0d03feSJani Nikula static void gpio_keys_timer(unsigned long _data)
55ca865a77SJani Nikula {
56ca865a77SJani Nikula 	struct gpio_button_data *data = (struct gpio_button_data *)_data;
57ca865a77SJani Nikula 
58da0d03feSJani Nikula 	schedule_work(&data->work);
59ca865a77SJani Nikula }
60ca865a77SJani Nikula 
61a33466e3SDmitry Baryshkov static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
62a33466e3SDmitry Baryshkov {
6357ffe9d5SUwe Kleine-König 	struct gpio_button_data *bdata = dev_id;
6457ffe9d5SUwe Kleine-König 	struct gpio_keys_button *button = bdata->button;
65a33466e3SDmitry Baryshkov 
6657ffe9d5SUwe Kleine-König 	BUG_ON(irq != gpio_to_irq(button->gpio));
67a33466e3SDmitry Baryshkov 
68ca865a77SJani Nikula 	if (button->debounce_interval)
69ca865a77SJani Nikula 		mod_timer(&bdata->timer,
70ca865a77SJani Nikula 			jiffies + msecs_to_jiffies(button->debounce_interval));
71ca865a77SJani Nikula 	else
72da0d03feSJani Nikula 		schedule_work(&bdata->work);
73a33466e3SDmitry Baryshkov 
741164ec1aSDavid Brownell 	return IRQ_HANDLED;
7578a56aabSPhil Blundell }
7678a56aabSPhil Blundell 
7778a56aabSPhil Blundell static int __devinit gpio_keys_probe(struct platform_device *pdev)
7878a56aabSPhil Blundell {
7978a56aabSPhil Blundell 	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
80a33466e3SDmitry Baryshkov 	struct gpio_keys_drvdata *ddata;
8178a56aabSPhil Blundell 	struct input_dev *input;
8278a56aabSPhil Blundell 	int i, error;
83e15b0213SAnti Sullin 	int wakeup = 0;
8478a56aabSPhil Blundell 
85a33466e3SDmitry Baryshkov 	ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
86a33466e3SDmitry Baryshkov 			pdata->nbuttons * sizeof(struct gpio_button_data),
87a33466e3SDmitry Baryshkov 			GFP_KERNEL);
8878a56aabSPhil Blundell 	input = input_allocate_device();
89a33466e3SDmitry Baryshkov 	if (!ddata || !input) {
90a33466e3SDmitry Baryshkov 		error = -ENOMEM;
91a33466e3SDmitry Baryshkov 		goto fail1;
92a33466e3SDmitry Baryshkov 	}
9378a56aabSPhil Blundell 
94a33466e3SDmitry Baryshkov 	platform_set_drvdata(pdev, ddata);
9578a56aabSPhil Blundell 
9678a56aabSPhil Blundell 	input->name = pdev->name;
9778a56aabSPhil Blundell 	input->phys = "gpio-keys/input0";
98469ba4dfSDmitry Torokhov 	input->dev.parent = &pdev->dev;
9978a56aabSPhil Blundell 
10078a56aabSPhil Blundell 	input->id.bustype = BUS_HOST;
10178a56aabSPhil Blundell 	input->id.vendor = 0x0001;
10278a56aabSPhil Blundell 	input->id.product = 0x0001;
10378a56aabSPhil Blundell 	input->id.version = 0x0100;
10478a56aabSPhil Blundell 
105b67b4b11SDominic Curran 	/* Enable auto repeat feature of Linux input subsystem */
106b67b4b11SDominic Curran 	if (pdata->rep)
107b67b4b11SDominic Curran 		__set_bit(EV_REP, input->evbit);
108b67b4b11SDominic Curran 
109a33466e3SDmitry Baryshkov 	ddata->input = input;
110a33466e3SDmitry Baryshkov 
11178a56aabSPhil Blundell 	for (i = 0; i < pdata->nbuttons; i++) {
11284767d00SRoman Moravcik 		struct gpio_keys_button *button = &pdata->buttons[i];
113a33466e3SDmitry Baryshkov 		struct gpio_button_data *bdata = &ddata->data[i];
1146a2e3911SHerbert Valerio Riedel 		int irq;
11584767d00SRoman Moravcik 		unsigned int type = button->type ?: EV_KEY;
11678a56aabSPhil Blundell 
117a33466e3SDmitry Baryshkov 		bdata->input = input;
11874dd4393SUwe Kleine-König 		bdata->button = button;
119ca865a77SJani Nikula 		setup_timer(&bdata->timer,
120da0d03feSJani Nikula 			    gpio_keys_timer, (unsigned long)bdata);
121da0d03feSJani Nikula 		INIT_WORK(&bdata->work, gpio_keys_report_event);
122a33466e3SDmitry Baryshkov 
1236a2e3911SHerbert Valerio Riedel 		error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
1246a2e3911SHerbert Valerio Riedel 		if (error < 0) {
1256a2e3911SHerbert Valerio Riedel 			pr_err("gpio-keys: failed to request GPIO %d,"
1266a2e3911SHerbert Valerio Riedel 				" error %d\n", button->gpio, error);
127a33466e3SDmitry Baryshkov 			goto fail2;
1286a2e3911SHerbert Valerio Riedel 		}
1296a2e3911SHerbert Valerio Riedel 
1306a2e3911SHerbert Valerio Riedel 		error = gpio_direction_input(button->gpio);
1316a2e3911SHerbert Valerio Riedel 		if (error < 0) {
1326a2e3911SHerbert Valerio Riedel 			pr_err("gpio-keys: failed to configure input"
1336a2e3911SHerbert Valerio Riedel 				" direction for GPIO %d, error %d\n",
1346a2e3911SHerbert Valerio Riedel 				button->gpio, error);
1356a2e3911SHerbert Valerio Riedel 			gpio_free(button->gpio);
136a33466e3SDmitry Baryshkov 			goto fail2;
1376a2e3911SHerbert Valerio Riedel 		}
1386a2e3911SHerbert Valerio Riedel 
1396a2e3911SHerbert Valerio Riedel 		irq = gpio_to_irq(button->gpio);
140006df302SAnti Sullin 		if (irq < 0) {
141006df302SAnti Sullin 			error = irq;
1426a2e3911SHerbert Valerio Riedel 			pr_err("gpio-keys: Unable to get irq number"
1436a2e3911SHerbert Valerio Riedel 				" for GPIO %d, error %d\n",
144006df302SAnti Sullin 				button->gpio, error);
1456a2e3911SHerbert Valerio Riedel 			gpio_free(button->gpio);
146a33466e3SDmitry Baryshkov 			goto fail2;
147006df302SAnti Sullin 		}
148006df302SAnti Sullin 
149006df302SAnti Sullin 		error = request_irq(irq, gpio_keys_isr,
150*558a5e29SDmitry Eremin-Solenikov 				    IRQF_SHARED |
15164e8563cSDmitry Torokhov 				    IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
15284767d00SRoman Moravcik 				    button->desc ? button->desc : "gpio_keys",
15357ffe9d5SUwe Kleine-König 				    bdata);
15478a56aabSPhil Blundell 		if (error) {
1556a2e3911SHerbert Valerio Riedel 			pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
1560d98f6bbSPhilipp Zabel 				irq, error);
1576a2e3911SHerbert Valerio Riedel 			gpio_free(button->gpio);
158a33466e3SDmitry Baryshkov 			goto fail2;
15978a56aabSPhil Blundell 		}
16084767d00SRoman Moravcik 
161e15b0213SAnti Sullin 		if (button->wakeup)
162e15b0213SAnti Sullin 			wakeup = 1;
163e15b0213SAnti Sullin 
16484767d00SRoman Moravcik 		input_set_capability(input, type, button->code);
16578a56aabSPhil Blundell 	}
16678a56aabSPhil Blundell 
16778a56aabSPhil Blundell 	error = input_register_device(input);
16878a56aabSPhil Blundell 	if (error) {
1696a2e3911SHerbert Valerio Riedel 		pr_err("gpio-keys: Unable to register input device, "
170006df302SAnti Sullin 			"error: %d\n", error);
171a33466e3SDmitry Baryshkov 		goto fail2;
17278a56aabSPhil Blundell 	}
17378a56aabSPhil Blundell 
174e15b0213SAnti Sullin 	device_init_wakeup(&pdev->dev, wakeup);
175e15b0213SAnti Sullin 
17678a56aabSPhil Blundell 	return 0;
17778a56aabSPhil Blundell 
178a33466e3SDmitry Baryshkov  fail2:
1796a2e3911SHerbert Valerio Riedel 	while (--i >= 0) {
18057ffe9d5SUwe Kleine-König 		free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
181ca865a77SJani Nikula 		if (pdata->buttons[i].debounce_interval)
182ca865a77SJani Nikula 			del_timer_sync(&ddata->data[i].timer);
183da0d03feSJani Nikula 		cancel_work_sync(&ddata->data[i].work);
1846a2e3911SHerbert Valerio Riedel 		gpio_free(pdata->buttons[i].gpio);
1856a2e3911SHerbert Valerio Riedel 	}
18678a56aabSPhil Blundell 
187006df302SAnti Sullin 	platform_set_drvdata(pdev, NULL);
188a33466e3SDmitry Baryshkov  fail1:
18978a56aabSPhil Blundell 	input_free_device(input);
190a33466e3SDmitry Baryshkov 	kfree(ddata);
19178a56aabSPhil Blundell 
19278a56aabSPhil Blundell 	return error;
19378a56aabSPhil Blundell }
19478a56aabSPhil Blundell 
19578a56aabSPhil Blundell static int __devexit gpio_keys_remove(struct platform_device *pdev)
19678a56aabSPhil Blundell {
19778a56aabSPhil Blundell 	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
198a33466e3SDmitry Baryshkov 	struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
199a33466e3SDmitry Baryshkov 	struct input_dev *input = ddata->input;
20078a56aabSPhil Blundell 	int i;
20178a56aabSPhil Blundell 
202e15b0213SAnti Sullin 	device_init_wakeup(&pdev->dev, 0);
203e15b0213SAnti Sullin 
20478a56aabSPhil Blundell 	for (i = 0; i < pdata->nbuttons; i++) {
2050d98f6bbSPhilipp Zabel 		int irq = gpio_to_irq(pdata->buttons[i].gpio);
20657ffe9d5SUwe Kleine-König 		free_irq(irq, &ddata->data[i]);
207ca865a77SJani Nikula 		if (pdata->buttons[i].debounce_interval)
208ca865a77SJani Nikula 			del_timer_sync(&ddata->data[i].timer);
209da0d03feSJani Nikula 		cancel_work_sync(&ddata->data[i].work);
2106a2e3911SHerbert Valerio Riedel 		gpio_free(pdata->buttons[i].gpio);
21178a56aabSPhil Blundell 	}
21278a56aabSPhil Blundell 
21378a56aabSPhil Blundell 	input_unregister_device(input);
21478a56aabSPhil Blundell 
21578a56aabSPhil Blundell 	return 0;
21678a56aabSPhil Blundell }
21778a56aabSPhil Blundell 
218e15b0213SAnti Sullin 
219e15b0213SAnti Sullin #ifdef CONFIG_PM
220ae78e0e0SMike Rapoport static int gpio_keys_suspend(struct device *dev)
221e15b0213SAnti Sullin {
222ae78e0e0SMike Rapoport 	struct platform_device *pdev = to_platform_device(dev);
223e15b0213SAnti Sullin 	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
224e15b0213SAnti Sullin 	int i;
225e15b0213SAnti Sullin 
226e15b0213SAnti Sullin 	if (device_may_wakeup(&pdev->dev)) {
227e15b0213SAnti Sullin 		for (i = 0; i < pdata->nbuttons; i++) {
228e15b0213SAnti Sullin 			struct gpio_keys_button *button = &pdata->buttons[i];
229e15b0213SAnti Sullin 			if (button->wakeup) {
230e15b0213SAnti Sullin 				int irq = gpio_to_irq(button->gpio);
231e15b0213SAnti Sullin 				enable_irq_wake(irq);
232e15b0213SAnti Sullin 			}
233e15b0213SAnti Sullin 		}
234e15b0213SAnti Sullin 	}
235e15b0213SAnti Sullin 
236e15b0213SAnti Sullin 	return 0;
237e15b0213SAnti Sullin }
238e15b0213SAnti Sullin 
239ae78e0e0SMike Rapoport static int gpio_keys_resume(struct device *dev)
240e15b0213SAnti Sullin {
241ae78e0e0SMike Rapoport 	struct platform_device *pdev = to_platform_device(dev);
242e15b0213SAnti Sullin 	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
243e15b0213SAnti Sullin 	int i;
244e15b0213SAnti Sullin 
245e15b0213SAnti Sullin 	if (device_may_wakeup(&pdev->dev)) {
246e15b0213SAnti Sullin 		for (i = 0; i < pdata->nbuttons; i++) {
247e15b0213SAnti Sullin 			struct gpio_keys_button *button = &pdata->buttons[i];
248e15b0213SAnti Sullin 			if (button->wakeup) {
249e15b0213SAnti Sullin 				int irq = gpio_to_irq(button->gpio);
250e15b0213SAnti Sullin 				disable_irq_wake(irq);
251e15b0213SAnti Sullin 			}
252e15b0213SAnti Sullin 		}
253e15b0213SAnti Sullin 	}
254e15b0213SAnti Sullin 
255e15b0213SAnti Sullin 	return 0;
256e15b0213SAnti Sullin }
257ae78e0e0SMike Rapoport 
258ae78e0e0SMike Rapoport static const struct dev_pm_ops gpio_keys_pm_ops = {
259ae78e0e0SMike Rapoport 	.suspend	= gpio_keys_suspend,
260ae78e0e0SMike Rapoport 	.resume		= gpio_keys_resume,
261ae78e0e0SMike Rapoport };
262e15b0213SAnti Sullin #endif
263e15b0213SAnti Sullin 
2649b07044cSUwe Kleine-König static struct platform_driver gpio_keys_device_driver = {
26578a56aabSPhil Blundell 	.probe		= gpio_keys_probe,
26678a56aabSPhil Blundell 	.remove		= __devexit_p(gpio_keys_remove),
26778a56aabSPhil Blundell 	.driver		= {
26878a56aabSPhil Blundell 		.name	= "gpio-keys",
269d7b5247bSKay Sievers 		.owner	= THIS_MODULE,
270ae78e0e0SMike Rapoport #ifdef CONFIG_PM
271ae78e0e0SMike Rapoport 		.pm	= &gpio_keys_pm_ops,
272ae78e0e0SMike Rapoport #endif
27378a56aabSPhil Blundell 	}
27478a56aabSPhil Blundell };
27578a56aabSPhil Blundell 
27678a56aabSPhil Blundell static int __init gpio_keys_init(void)
27778a56aabSPhil Blundell {
27878a56aabSPhil Blundell 	return platform_driver_register(&gpio_keys_device_driver);
27978a56aabSPhil Blundell }
28078a56aabSPhil Blundell 
28178a56aabSPhil Blundell static void __exit gpio_keys_exit(void)
28278a56aabSPhil Blundell {
28378a56aabSPhil Blundell 	platform_driver_unregister(&gpio_keys_device_driver);
28478a56aabSPhil Blundell }
28578a56aabSPhil Blundell 
28678a56aabSPhil Blundell module_init(gpio_keys_init);
28778a56aabSPhil Blundell module_exit(gpio_keys_exit);
28878a56aabSPhil Blundell 
28978a56aabSPhil Blundell MODULE_LICENSE("GPL");
29078a56aabSPhil Blundell MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
29178a56aabSPhil Blundell MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
292d7b5247bSKay Sievers MODULE_ALIAS("platform:gpio-keys");
293