xref: /openbmc/linux/drivers/input/keyboard/gpio_keys.c (revision ce25d7e90c7543f0046c3bdcdcc7594546c57dcc)
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 #include <linux/version.h>
1378a56aabSPhil Blundell 
1478a56aabSPhil Blundell #include <linux/init.h>
1578a56aabSPhil Blundell #include <linux/fs.h>
1678a56aabSPhil Blundell #include <linux/interrupt.h>
1778a56aabSPhil Blundell #include <linux/irq.h>
1878a56aabSPhil Blundell #include <linux/sched.h>
1978a56aabSPhil Blundell #include <linux/pm.h>
2078a56aabSPhil Blundell #include <linux/sysctl.h>
2178a56aabSPhil Blundell #include <linux/proc_fs.h>
2278a56aabSPhil Blundell #include <linux/delay.h>
2378a56aabSPhil Blundell #include <linux/platform_device.h>
2478a56aabSPhil Blundell #include <linux/input.h>
2549015beeSDavid Brownell #include <linux/gpio_keys.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;
32a33466e3SDmitry Baryshkov 	struct timer_list timer;
33a33466e3SDmitry Baryshkov };
34a33466e3SDmitry Baryshkov 
35a33466e3SDmitry Baryshkov struct gpio_keys_drvdata {
36a33466e3SDmitry Baryshkov 	struct input_dev *input;
37a33466e3SDmitry Baryshkov 	struct gpio_button_data data[0];
38a33466e3SDmitry Baryshkov };
39a33466e3SDmitry Baryshkov 
40*ce25d7e9SUwe Kleine-König static void gpio_keys_report_event(struct gpio_button_data *bdata)
4178a56aabSPhil Blundell {
42*ce25d7e9SUwe Kleine-König 	struct gpio_keys_button *button = bdata->button;
43*ce25d7e9SUwe Kleine-König 	struct input_dev *input = bdata->input;
4484767d00SRoman Moravcik 	unsigned int type = button->type ?: EV_KEY;
45a33466e3SDmitry Baryshkov 	int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
4684767d00SRoman Moravcik 
4784767d00SRoman Moravcik 	input_event(input, type, button->code, !!state);
4878a56aabSPhil Blundell 	input_sync(input);
49a33466e3SDmitry Baryshkov }
50a33466e3SDmitry Baryshkov 
51a33466e3SDmitry Baryshkov static void gpio_check_button(unsigned long _data)
52a33466e3SDmitry Baryshkov {
53a33466e3SDmitry Baryshkov 	struct gpio_button_data *data = (struct gpio_button_data *)_data;
54a33466e3SDmitry Baryshkov 
55*ce25d7e9SUwe Kleine-König 	gpio_keys_report_event(data);
56a33466e3SDmitry Baryshkov }
57a33466e3SDmitry Baryshkov 
58a33466e3SDmitry Baryshkov static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
59a33466e3SDmitry Baryshkov {
6057ffe9d5SUwe Kleine-König 	struct gpio_button_data *bdata = dev_id;
6157ffe9d5SUwe Kleine-König 	struct gpio_keys_button *button = bdata->button;
62a33466e3SDmitry Baryshkov 
6357ffe9d5SUwe Kleine-König 	BUG_ON(irq != gpio_to_irq(button->gpio));
64a33466e3SDmitry Baryshkov 
65a33466e3SDmitry Baryshkov 	if (button->debounce_interval)
66a33466e3SDmitry Baryshkov 		mod_timer(&bdata->timer,
6757ffe9d5SUwe Kleine-König 			jiffies + msecs_to_jiffies(button->debounce_interval));
68a33466e3SDmitry Baryshkov 	else
69*ce25d7e9SUwe Kleine-König 		gpio_keys_report_event(bdata);
70a33466e3SDmitry Baryshkov 
711164ec1aSDavid Brownell 	return IRQ_HANDLED;
7278a56aabSPhil Blundell }
7378a56aabSPhil Blundell 
7478a56aabSPhil Blundell static int __devinit gpio_keys_probe(struct platform_device *pdev)
7578a56aabSPhil Blundell {
7678a56aabSPhil Blundell 	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
77a33466e3SDmitry Baryshkov 	struct gpio_keys_drvdata *ddata;
7878a56aabSPhil Blundell 	struct input_dev *input;
7978a56aabSPhil Blundell 	int i, error;
80e15b0213SAnti Sullin 	int wakeup = 0;
8178a56aabSPhil Blundell 
82a33466e3SDmitry Baryshkov 	ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
83a33466e3SDmitry Baryshkov 			pdata->nbuttons * sizeof(struct gpio_button_data),
84a33466e3SDmitry Baryshkov 			GFP_KERNEL);
8578a56aabSPhil Blundell 	input = input_allocate_device();
86a33466e3SDmitry Baryshkov 	if (!ddata || !input) {
87a33466e3SDmitry Baryshkov 		error = -ENOMEM;
88a33466e3SDmitry Baryshkov 		goto fail1;
89a33466e3SDmitry Baryshkov 	}
9078a56aabSPhil Blundell 
91a33466e3SDmitry Baryshkov 	platform_set_drvdata(pdev, ddata);
9278a56aabSPhil Blundell 
9378a56aabSPhil Blundell 	input->name = pdev->name;
9478a56aabSPhil Blundell 	input->phys = "gpio-keys/input0";
95469ba4dfSDmitry Torokhov 	input->dev.parent = &pdev->dev;
9678a56aabSPhil Blundell 
9778a56aabSPhil Blundell 	input->id.bustype = BUS_HOST;
9878a56aabSPhil Blundell 	input->id.vendor = 0x0001;
9978a56aabSPhil Blundell 	input->id.product = 0x0001;
10078a56aabSPhil Blundell 	input->id.version = 0x0100;
10178a56aabSPhil Blundell 
102a33466e3SDmitry Baryshkov 	ddata->input = input;
103a33466e3SDmitry Baryshkov 
10478a56aabSPhil Blundell 	for (i = 0; i < pdata->nbuttons; i++) {
10584767d00SRoman Moravcik 		struct gpio_keys_button *button = &pdata->buttons[i];
106a33466e3SDmitry Baryshkov 		struct gpio_button_data *bdata = &ddata->data[i];
1076a2e3911SHerbert Valerio Riedel 		int irq;
10884767d00SRoman Moravcik 		unsigned int type = button->type ?: EV_KEY;
10978a56aabSPhil Blundell 
110a33466e3SDmitry Baryshkov 		bdata->input = input;
111a33466e3SDmitry Baryshkov 		setup_timer(&bdata->timer,
112a33466e3SDmitry Baryshkov 			    gpio_check_button, (unsigned long)bdata);
113a33466e3SDmitry Baryshkov 
1146a2e3911SHerbert Valerio Riedel 		error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
1156a2e3911SHerbert Valerio Riedel 		if (error < 0) {
1166a2e3911SHerbert Valerio Riedel 			pr_err("gpio-keys: failed to request GPIO %d,"
1176a2e3911SHerbert Valerio Riedel 				" error %d\n", button->gpio, error);
118a33466e3SDmitry Baryshkov 			goto fail2;
1196a2e3911SHerbert Valerio Riedel 		}
1206a2e3911SHerbert Valerio Riedel 
1216a2e3911SHerbert Valerio Riedel 		error = gpio_direction_input(button->gpio);
1226a2e3911SHerbert Valerio Riedel 		if (error < 0) {
1236a2e3911SHerbert Valerio Riedel 			pr_err("gpio-keys: failed to configure input"
1246a2e3911SHerbert Valerio Riedel 				" direction for GPIO %d, error %d\n",
1256a2e3911SHerbert Valerio Riedel 				button->gpio, error);
1266a2e3911SHerbert Valerio Riedel 			gpio_free(button->gpio);
127a33466e3SDmitry Baryshkov 			goto fail2;
1286a2e3911SHerbert Valerio Riedel 		}
1296a2e3911SHerbert Valerio Riedel 
1306a2e3911SHerbert Valerio Riedel 		irq = gpio_to_irq(button->gpio);
131006df302SAnti Sullin 		if (irq < 0) {
132006df302SAnti Sullin 			error = irq;
1336a2e3911SHerbert Valerio Riedel 			pr_err("gpio-keys: Unable to get irq number"
1346a2e3911SHerbert Valerio Riedel 				" for GPIO %d, error %d\n",
135006df302SAnti Sullin 				button->gpio, error);
1366a2e3911SHerbert Valerio Riedel 			gpio_free(button->gpio);
137a33466e3SDmitry Baryshkov 			goto fail2;
138006df302SAnti Sullin 		}
139006df302SAnti Sullin 
140006df302SAnti Sullin 		error = request_irq(irq, gpio_keys_isr,
141006df302SAnti Sullin 				    IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
142006df302SAnti Sullin 					IRQF_TRIGGER_FALLING,
14384767d00SRoman Moravcik 				    button->desc ? button->desc : "gpio_keys",
14457ffe9d5SUwe Kleine-König 				    bdata);
14578a56aabSPhil Blundell 		if (error) {
1466a2e3911SHerbert Valerio Riedel 			pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
1470d98f6bbSPhilipp Zabel 				irq, error);
1486a2e3911SHerbert Valerio Riedel 			gpio_free(button->gpio);
149a33466e3SDmitry Baryshkov 			goto fail2;
15078a56aabSPhil Blundell 		}
15184767d00SRoman Moravcik 
152e15b0213SAnti Sullin 		if (button->wakeup)
153e15b0213SAnti Sullin 			wakeup = 1;
154e15b0213SAnti Sullin 
15584767d00SRoman Moravcik 		input_set_capability(input, type, button->code);
15678a56aabSPhil Blundell 	}
15778a56aabSPhil Blundell 
15878a56aabSPhil Blundell 	error = input_register_device(input);
15978a56aabSPhil Blundell 	if (error) {
1606a2e3911SHerbert Valerio Riedel 		pr_err("gpio-keys: Unable to register input device, "
161006df302SAnti Sullin 			"error: %d\n", error);
162a33466e3SDmitry Baryshkov 		goto fail2;
16378a56aabSPhil Blundell 	}
16478a56aabSPhil Blundell 
165e15b0213SAnti Sullin 	device_init_wakeup(&pdev->dev, wakeup);
166e15b0213SAnti Sullin 
16778a56aabSPhil Blundell 	return 0;
16878a56aabSPhil Blundell 
169a33466e3SDmitry Baryshkov  fail2:
1706a2e3911SHerbert Valerio Riedel 	while (--i >= 0) {
17157ffe9d5SUwe Kleine-König 		free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
172a33466e3SDmitry Baryshkov 		if (pdata->buttons[i].debounce_interval)
173a33466e3SDmitry Baryshkov 			del_timer_sync(&ddata->data[i].timer);
1746a2e3911SHerbert Valerio Riedel 		gpio_free(pdata->buttons[i].gpio);
1756a2e3911SHerbert Valerio Riedel 	}
17678a56aabSPhil Blundell 
177006df302SAnti Sullin 	platform_set_drvdata(pdev, NULL);
178a33466e3SDmitry Baryshkov  fail1:
17978a56aabSPhil Blundell 	input_free_device(input);
180a33466e3SDmitry Baryshkov 	kfree(ddata);
18178a56aabSPhil Blundell 
18278a56aabSPhil Blundell 	return error;
18378a56aabSPhil Blundell }
18478a56aabSPhil Blundell 
18578a56aabSPhil Blundell static int __devexit gpio_keys_remove(struct platform_device *pdev)
18678a56aabSPhil Blundell {
18778a56aabSPhil Blundell 	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
188a33466e3SDmitry Baryshkov 	struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
189a33466e3SDmitry Baryshkov 	struct input_dev *input = ddata->input;
19078a56aabSPhil Blundell 	int i;
19178a56aabSPhil Blundell 
192e15b0213SAnti Sullin 	device_init_wakeup(&pdev->dev, 0);
193e15b0213SAnti Sullin 
19478a56aabSPhil Blundell 	for (i = 0; i < pdata->nbuttons; i++) {
1950d98f6bbSPhilipp Zabel 		int irq = gpio_to_irq(pdata->buttons[i].gpio);
19657ffe9d5SUwe Kleine-König 		free_irq(irq, &ddata->data[i]);
197a33466e3SDmitry Baryshkov 		if (pdata->buttons[i].debounce_interval)
198a33466e3SDmitry Baryshkov 			del_timer_sync(&ddata->data[i].timer);
1996a2e3911SHerbert Valerio Riedel 		gpio_free(pdata->buttons[i].gpio);
20078a56aabSPhil Blundell 	}
20178a56aabSPhil Blundell 
20278a56aabSPhil Blundell 	input_unregister_device(input);
20378a56aabSPhil Blundell 
20478a56aabSPhil Blundell 	return 0;
20578a56aabSPhil Blundell }
20678a56aabSPhil Blundell 
207e15b0213SAnti Sullin 
208e15b0213SAnti Sullin #ifdef CONFIG_PM
209e15b0213SAnti Sullin static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
210e15b0213SAnti Sullin {
211e15b0213SAnti Sullin 	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
212e15b0213SAnti Sullin 	int i;
213e15b0213SAnti Sullin 
214e15b0213SAnti Sullin 	if (device_may_wakeup(&pdev->dev)) {
215e15b0213SAnti Sullin 		for (i = 0; i < pdata->nbuttons; i++) {
216e15b0213SAnti Sullin 			struct gpio_keys_button *button = &pdata->buttons[i];
217e15b0213SAnti Sullin 			if (button->wakeup) {
218e15b0213SAnti Sullin 				int irq = gpio_to_irq(button->gpio);
219e15b0213SAnti Sullin 				enable_irq_wake(irq);
220e15b0213SAnti Sullin 			}
221e15b0213SAnti Sullin 		}
222e15b0213SAnti Sullin 	}
223e15b0213SAnti Sullin 
224e15b0213SAnti Sullin 	return 0;
225e15b0213SAnti Sullin }
226e15b0213SAnti Sullin 
227e15b0213SAnti Sullin static int gpio_keys_resume(struct platform_device *pdev)
228e15b0213SAnti Sullin {
229e15b0213SAnti Sullin 	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
230e15b0213SAnti Sullin 	int i;
231e15b0213SAnti Sullin 
232e15b0213SAnti Sullin 	if (device_may_wakeup(&pdev->dev)) {
233e15b0213SAnti Sullin 		for (i = 0; i < pdata->nbuttons; i++) {
234e15b0213SAnti Sullin 			struct gpio_keys_button *button = &pdata->buttons[i];
235e15b0213SAnti Sullin 			if (button->wakeup) {
236e15b0213SAnti Sullin 				int irq = gpio_to_irq(button->gpio);
237e15b0213SAnti Sullin 				disable_irq_wake(irq);
238e15b0213SAnti Sullin 			}
239e15b0213SAnti Sullin 		}
240e15b0213SAnti Sullin 	}
241e15b0213SAnti Sullin 
242e15b0213SAnti Sullin 	return 0;
243e15b0213SAnti Sullin }
244e15b0213SAnti Sullin #else
245e15b0213SAnti Sullin #define gpio_keys_suspend	NULL
246e15b0213SAnti Sullin #define gpio_keys_resume	NULL
247e15b0213SAnti Sullin #endif
248e15b0213SAnti Sullin 
24978a56aabSPhil Blundell struct platform_driver gpio_keys_device_driver = {
25078a56aabSPhil Blundell 	.probe		= gpio_keys_probe,
25178a56aabSPhil Blundell 	.remove		= __devexit_p(gpio_keys_remove),
252e15b0213SAnti Sullin 	.suspend	= gpio_keys_suspend,
253e15b0213SAnti Sullin 	.resume		= gpio_keys_resume,
25478a56aabSPhil Blundell 	.driver		= {
25578a56aabSPhil Blundell 		.name	= "gpio-keys",
256d7b5247bSKay Sievers 		.owner	= THIS_MODULE,
25778a56aabSPhil Blundell 	}
25878a56aabSPhil Blundell };
25978a56aabSPhil Blundell 
26078a56aabSPhil Blundell static int __init gpio_keys_init(void)
26178a56aabSPhil Blundell {
26278a56aabSPhil Blundell 	return platform_driver_register(&gpio_keys_device_driver);
26378a56aabSPhil Blundell }
26478a56aabSPhil Blundell 
26578a56aabSPhil Blundell static void __exit gpio_keys_exit(void)
26678a56aabSPhil Blundell {
26778a56aabSPhil Blundell 	platform_driver_unregister(&gpio_keys_device_driver);
26878a56aabSPhil Blundell }
26978a56aabSPhil Blundell 
27078a56aabSPhil Blundell module_init(gpio_keys_init);
27178a56aabSPhil Blundell module_exit(gpio_keys_exit);
27278a56aabSPhil Blundell 
27378a56aabSPhil Blundell MODULE_LICENSE("GPL");
27478a56aabSPhil Blundell MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
27578a56aabSPhil Blundell MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
276d7b5247bSKay Sievers MODULE_ALIAS("platform:gpio-keys");
277