xref: /openbmc/linux/drivers/input/keyboard/gpio_keys.c (revision b67b4b117746aef686e527c3205792db0f2c9e16)
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>
2578a56aabSPhil Blundell 
260d98f6bbSPhilipp Zabel #include <asm/gpio.h>
2778a56aabSPhil Blundell 
28a33466e3SDmitry Baryshkov struct gpio_button_data {
29a33466e3SDmitry Baryshkov 	struct gpio_keys_button *button;
30a33466e3SDmitry Baryshkov 	struct input_dev *input;
31a33466e3SDmitry Baryshkov 	struct timer_list timer;
32a33466e3SDmitry Baryshkov };
33a33466e3SDmitry Baryshkov 
34a33466e3SDmitry Baryshkov struct gpio_keys_drvdata {
35a33466e3SDmitry Baryshkov 	struct input_dev *input;
36a33466e3SDmitry Baryshkov 	struct gpio_button_data data[0];
37a33466e3SDmitry Baryshkov };
38a33466e3SDmitry Baryshkov 
39ce25d7e9SUwe Kleine-König static void gpio_keys_report_event(struct gpio_button_data *bdata)
4078a56aabSPhil Blundell {
41ce25d7e9SUwe Kleine-König 	struct gpio_keys_button *button = bdata->button;
42ce25d7e9SUwe Kleine-König 	struct input_dev *input = bdata->input;
4384767d00SRoman Moravcik 	unsigned int type = button->type ?: EV_KEY;
44a33466e3SDmitry Baryshkov 	int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
4584767d00SRoman Moravcik 
4684767d00SRoman Moravcik 	input_event(input, type, button->code, !!state);
4778a56aabSPhil Blundell 	input_sync(input);
48a33466e3SDmitry Baryshkov }
49a33466e3SDmitry Baryshkov 
50a33466e3SDmitry Baryshkov static void gpio_check_button(unsigned long _data)
51a33466e3SDmitry Baryshkov {
52a33466e3SDmitry Baryshkov 	struct gpio_button_data *data = (struct gpio_button_data *)_data;
53a33466e3SDmitry Baryshkov 
54ce25d7e9SUwe Kleine-König 	gpio_keys_report_event(data);
55a33466e3SDmitry Baryshkov }
56a33466e3SDmitry Baryshkov 
57a33466e3SDmitry Baryshkov static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
58a33466e3SDmitry Baryshkov {
5957ffe9d5SUwe Kleine-König 	struct gpio_button_data *bdata = dev_id;
6057ffe9d5SUwe Kleine-König 	struct gpio_keys_button *button = bdata->button;
61a33466e3SDmitry Baryshkov 
6257ffe9d5SUwe Kleine-König 	BUG_ON(irq != gpio_to_irq(button->gpio));
63a33466e3SDmitry Baryshkov 
64a33466e3SDmitry Baryshkov 	if (button->debounce_interval)
65a33466e3SDmitry Baryshkov 		mod_timer(&bdata->timer,
6657ffe9d5SUwe Kleine-König 			jiffies + msecs_to_jiffies(button->debounce_interval));
67a33466e3SDmitry Baryshkov 	else
68ce25d7e9SUwe Kleine-König 		gpio_keys_report_event(bdata);
69a33466e3SDmitry Baryshkov 
701164ec1aSDavid Brownell 	return IRQ_HANDLED;
7178a56aabSPhil Blundell }
7278a56aabSPhil Blundell 
7378a56aabSPhil Blundell static int __devinit gpio_keys_probe(struct platform_device *pdev)
7478a56aabSPhil Blundell {
7578a56aabSPhil Blundell 	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
76a33466e3SDmitry Baryshkov 	struct gpio_keys_drvdata *ddata;
7778a56aabSPhil Blundell 	struct input_dev *input;
7878a56aabSPhil Blundell 	int i, error;
79e15b0213SAnti Sullin 	int wakeup = 0;
8078a56aabSPhil Blundell 
81a33466e3SDmitry Baryshkov 	ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
82a33466e3SDmitry Baryshkov 			pdata->nbuttons * sizeof(struct gpio_button_data),
83a33466e3SDmitry Baryshkov 			GFP_KERNEL);
8478a56aabSPhil Blundell 	input = input_allocate_device();
85a33466e3SDmitry Baryshkov 	if (!ddata || !input) {
86a33466e3SDmitry Baryshkov 		error = -ENOMEM;
87a33466e3SDmitry Baryshkov 		goto fail1;
88a33466e3SDmitry Baryshkov 	}
8978a56aabSPhil Blundell 
90a33466e3SDmitry Baryshkov 	platform_set_drvdata(pdev, ddata);
9178a56aabSPhil Blundell 
9278a56aabSPhil Blundell 	input->name = pdev->name;
9378a56aabSPhil Blundell 	input->phys = "gpio-keys/input0";
94469ba4dfSDmitry Torokhov 	input->dev.parent = &pdev->dev;
9578a56aabSPhil Blundell 
9678a56aabSPhil Blundell 	input->id.bustype = BUS_HOST;
9778a56aabSPhil Blundell 	input->id.vendor = 0x0001;
9878a56aabSPhil Blundell 	input->id.product = 0x0001;
9978a56aabSPhil Blundell 	input->id.version = 0x0100;
10078a56aabSPhil Blundell 
101*b67b4b11SDominic Curran 	/* Enable auto repeat feature of Linux input subsystem */
102*b67b4b11SDominic Curran 	if (pdata->rep)
103*b67b4b11SDominic Curran 		__set_bit(EV_REP, input->evbit);
104*b67b4b11SDominic Curran 
105a33466e3SDmitry Baryshkov 	ddata->input = input;
106a33466e3SDmitry Baryshkov 
10778a56aabSPhil Blundell 	for (i = 0; i < pdata->nbuttons; i++) {
10884767d00SRoman Moravcik 		struct gpio_keys_button *button = &pdata->buttons[i];
109a33466e3SDmitry Baryshkov 		struct gpio_button_data *bdata = &ddata->data[i];
1106a2e3911SHerbert Valerio Riedel 		int irq;
11184767d00SRoman Moravcik 		unsigned int type = button->type ?: EV_KEY;
11278a56aabSPhil Blundell 
113a33466e3SDmitry Baryshkov 		bdata->input = input;
11474dd4393SUwe Kleine-König 		bdata->button = button;
115a33466e3SDmitry Baryshkov 		setup_timer(&bdata->timer,
116a33466e3SDmitry Baryshkov 			    gpio_check_button, (unsigned long)bdata);
117a33466e3SDmitry Baryshkov 
1186a2e3911SHerbert Valerio Riedel 		error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
1196a2e3911SHerbert Valerio Riedel 		if (error < 0) {
1206a2e3911SHerbert Valerio Riedel 			pr_err("gpio-keys: failed to request GPIO %d,"
1216a2e3911SHerbert Valerio Riedel 				" error %d\n", button->gpio, error);
122a33466e3SDmitry Baryshkov 			goto fail2;
1236a2e3911SHerbert Valerio Riedel 		}
1246a2e3911SHerbert Valerio Riedel 
1256a2e3911SHerbert Valerio Riedel 		error = gpio_direction_input(button->gpio);
1266a2e3911SHerbert Valerio Riedel 		if (error < 0) {
1276a2e3911SHerbert Valerio Riedel 			pr_err("gpio-keys: failed to configure input"
1286a2e3911SHerbert Valerio Riedel 				" direction for GPIO %d, error %d\n",
1296a2e3911SHerbert Valerio Riedel 				button->gpio, error);
1306a2e3911SHerbert Valerio Riedel 			gpio_free(button->gpio);
131a33466e3SDmitry Baryshkov 			goto fail2;
1326a2e3911SHerbert Valerio Riedel 		}
1336a2e3911SHerbert Valerio Riedel 
1346a2e3911SHerbert Valerio Riedel 		irq = gpio_to_irq(button->gpio);
135006df302SAnti Sullin 		if (irq < 0) {
136006df302SAnti Sullin 			error = irq;
1376a2e3911SHerbert Valerio Riedel 			pr_err("gpio-keys: Unable to get irq number"
1386a2e3911SHerbert Valerio Riedel 				" for GPIO %d, error %d\n",
139006df302SAnti Sullin 				button->gpio, error);
1406a2e3911SHerbert Valerio Riedel 			gpio_free(button->gpio);
141a33466e3SDmitry Baryshkov 			goto fail2;
142006df302SAnti Sullin 		}
143006df302SAnti Sullin 
144006df302SAnti Sullin 		error = request_irq(irq, gpio_keys_isr,
145006df302SAnti Sullin 				    IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
146006df302SAnti Sullin 					IRQF_TRIGGER_FALLING,
14784767d00SRoman Moravcik 				    button->desc ? button->desc : "gpio_keys",
14857ffe9d5SUwe Kleine-König 				    bdata);
14978a56aabSPhil Blundell 		if (error) {
1506a2e3911SHerbert Valerio Riedel 			pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
1510d98f6bbSPhilipp Zabel 				irq, error);
1526a2e3911SHerbert Valerio Riedel 			gpio_free(button->gpio);
153a33466e3SDmitry Baryshkov 			goto fail2;
15478a56aabSPhil Blundell 		}
15584767d00SRoman Moravcik 
156e15b0213SAnti Sullin 		if (button->wakeup)
157e15b0213SAnti Sullin 			wakeup = 1;
158e15b0213SAnti Sullin 
15984767d00SRoman Moravcik 		input_set_capability(input, type, button->code);
16078a56aabSPhil Blundell 	}
16178a56aabSPhil Blundell 
16278a56aabSPhil Blundell 	error = input_register_device(input);
16378a56aabSPhil Blundell 	if (error) {
1646a2e3911SHerbert Valerio Riedel 		pr_err("gpio-keys: Unable to register input device, "
165006df302SAnti Sullin 			"error: %d\n", error);
166a33466e3SDmitry Baryshkov 		goto fail2;
16778a56aabSPhil Blundell 	}
16878a56aabSPhil Blundell 
169e15b0213SAnti Sullin 	device_init_wakeup(&pdev->dev, wakeup);
170e15b0213SAnti Sullin 
17178a56aabSPhil Blundell 	return 0;
17278a56aabSPhil Blundell 
173a33466e3SDmitry Baryshkov  fail2:
1746a2e3911SHerbert Valerio Riedel 	while (--i >= 0) {
17557ffe9d5SUwe Kleine-König 		free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
176a33466e3SDmitry Baryshkov 		if (pdata->buttons[i].debounce_interval)
177a33466e3SDmitry Baryshkov 			del_timer_sync(&ddata->data[i].timer);
1786a2e3911SHerbert Valerio Riedel 		gpio_free(pdata->buttons[i].gpio);
1796a2e3911SHerbert Valerio Riedel 	}
18078a56aabSPhil Blundell 
181006df302SAnti Sullin 	platform_set_drvdata(pdev, NULL);
182a33466e3SDmitry Baryshkov  fail1:
18378a56aabSPhil Blundell 	input_free_device(input);
184a33466e3SDmitry Baryshkov 	kfree(ddata);
18578a56aabSPhil Blundell 
18678a56aabSPhil Blundell 	return error;
18778a56aabSPhil Blundell }
18878a56aabSPhil Blundell 
18978a56aabSPhil Blundell static int __devexit gpio_keys_remove(struct platform_device *pdev)
19078a56aabSPhil Blundell {
19178a56aabSPhil Blundell 	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
192a33466e3SDmitry Baryshkov 	struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
193a33466e3SDmitry Baryshkov 	struct input_dev *input = ddata->input;
19478a56aabSPhil Blundell 	int i;
19578a56aabSPhil Blundell 
196e15b0213SAnti Sullin 	device_init_wakeup(&pdev->dev, 0);
197e15b0213SAnti Sullin 
19878a56aabSPhil Blundell 	for (i = 0; i < pdata->nbuttons; i++) {
1990d98f6bbSPhilipp Zabel 		int irq = gpio_to_irq(pdata->buttons[i].gpio);
20057ffe9d5SUwe Kleine-König 		free_irq(irq, &ddata->data[i]);
201a33466e3SDmitry Baryshkov 		if (pdata->buttons[i].debounce_interval)
202a33466e3SDmitry Baryshkov 			del_timer_sync(&ddata->data[i].timer);
2036a2e3911SHerbert Valerio Riedel 		gpio_free(pdata->buttons[i].gpio);
20478a56aabSPhil Blundell 	}
20578a56aabSPhil Blundell 
20678a56aabSPhil Blundell 	input_unregister_device(input);
20778a56aabSPhil Blundell 
20878a56aabSPhil Blundell 	return 0;
20978a56aabSPhil Blundell }
21078a56aabSPhil Blundell 
211e15b0213SAnti Sullin 
212e15b0213SAnti Sullin #ifdef CONFIG_PM
213e15b0213SAnti Sullin static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
214e15b0213SAnti Sullin {
215e15b0213SAnti Sullin 	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
216e15b0213SAnti Sullin 	int i;
217e15b0213SAnti Sullin 
218e15b0213SAnti Sullin 	if (device_may_wakeup(&pdev->dev)) {
219e15b0213SAnti Sullin 		for (i = 0; i < pdata->nbuttons; i++) {
220e15b0213SAnti Sullin 			struct gpio_keys_button *button = &pdata->buttons[i];
221e15b0213SAnti Sullin 			if (button->wakeup) {
222e15b0213SAnti Sullin 				int irq = gpio_to_irq(button->gpio);
223e15b0213SAnti Sullin 				enable_irq_wake(irq);
224e15b0213SAnti Sullin 			}
225e15b0213SAnti Sullin 		}
226e15b0213SAnti Sullin 	}
227e15b0213SAnti Sullin 
228e15b0213SAnti Sullin 	return 0;
229e15b0213SAnti Sullin }
230e15b0213SAnti Sullin 
231e15b0213SAnti Sullin static int gpio_keys_resume(struct platform_device *pdev)
232e15b0213SAnti Sullin {
233e15b0213SAnti Sullin 	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
234e15b0213SAnti Sullin 	int i;
235e15b0213SAnti Sullin 
236e15b0213SAnti Sullin 	if (device_may_wakeup(&pdev->dev)) {
237e15b0213SAnti Sullin 		for (i = 0; i < pdata->nbuttons; i++) {
238e15b0213SAnti Sullin 			struct gpio_keys_button *button = &pdata->buttons[i];
239e15b0213SAnti Sullin 			if (button->wakeup) {
240e15b0213SAnti Sullin 				int irq = gpio_to_irq(button->gpio);
241e15b0213SAnti Sullin 				disable_irq_wake(irq);
242e15b0213SAnti Sullin 			}
243e15b0213SAnti Sullin 		}
244e15b0213SAnti Sullin 	}
245e15b0213SAnti Sullin 
246e15b0213SAnti Sullin 	return 0;
247e15b0213SAnti Sullin }
248e15b0213SAnti Sullin #else
249e15b0213SAnti Sullin #define gpio_keys_suspend	NULL
250e15b0213SAnti Sullin #define gpio_keys_resume	NULL
251e15b0213SAnti Sullin #endif
252e15b0213SAnti Sullin 
2539b07044cSUwe Kleine-König static struct platform_driver gpio_keys_device_driver = {
25478a56aabSPhil Blundell 	.probe		= gpio_keys_probe,
25578a56aabSPhil Blundell 	.remove		= __devexit_p(gpio_keys_remove),
256e15b0213SAnti Sullin 	.suspend	= gpio_keys_suspend,
257e15b0213SAnti Sullin 	.resume		= gpio_keys_resume,
25878a56aabSPhil Blundell 	.driver		= {
25978a56aabSPhil Blundell 		.name	= "gpio-keys",
260d7b5247bSKay Sievers 		.owner	= THIS_MODULE,
26178a56aabSPhil Blundell 	}
26278a56aabSPhil Blundell };
26378a56aabSPhil Blundell 
26478a56aabSPhil Blundell static int __init gpio_keys_init(void)
26578a56aabSPhil Blundell {
26678a56aabSPhil Blundell 	return platform_driver_register(&gpio_keys_device_driver);
26778a56aabSPhil Blundell }
26878a56aabSPhil Blundell 
26978a56aabSPhil Blundell static void __exit gpio_keys_exit(void)
27078a56aabSPhil Blundell {
27178a56aabSPhil Blundell 	platform_driver_unregister(&gpio_keys_device_driver);
27278a56aabSPhil Blundell }
27378a56aabSPhil Blundell 
27478a56aabSPhil Blundell module_init(gpio_keys_init);
27578a56aabSPhil Blundell module_exit(gpio_keys_exit);
27678a56aabSPhil Blundell 
27778a56aabSPhil Blundell MODULE_LICENSE("GPL");
27878a56aabSPhil Blundell MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
27978a56aabSPhil Blundell MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
280d7b5247bSKay Sievers MODULE_ALIAS("platform:gpio-keys");
281