blob: 6a9ca4bdcb74acfa1dd9abc5bfec06bd2abeda62 [file] [log] [blame]
Phil Blundell78a56aa2007-01-18 00:44:09 -05001/*
2 * Driver for keys on GPIO lines capable of generating interrupts.
3 *
4 * Copyright 2005 Phil Blundell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12#include <linux/version.h>
13
14#include <linux/init.h>
15#include <linux/fs.h>
16#include <linux/interrupt.h>
17#include <linux/irq.h>
18#include <linux/sched.h>
19#include <linux/pm.h>
20#include <linux/sysctl.h>
21#include <linux/proc_fs.h>
22#include <linux/delay.h>
23#include <linux/platform_device.h>
24#include <linux/input.h>
David Brownell49015be2007-03-05 00:30:22 -080025#include <linux/gpio_keys.h>
Phil Blundell78a56aa2007-01-18 00:44:09 -050026
Philipp Zabel0d98f6b2007-02-18 01:40:46 -050027#include <asm/gpio.h>
Phil Blundell78a56aa2007-01-18 00:44:09 -050028
29static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
30{
31 int i;
32 struct platform_device *pdev = dev_id;
33 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
34 struct input_dev *input = platform_get_drvdata(pdev);
35
36 for (i = 0; i < pdata->nbuttons; i++) {
Roman Moravcik84767d02007-05-01 00:39:13 -040037 struct gpio_keys_button *button = &pdata->buttons[i];
38 int gpio = button->gpio;
Phil Blundell78a56aa2007-01-18 00:44:09 -050039
Roman Moravcik84767d02007-05-01 00:39:13 -040040 if (irq == gpio_to_irq(gpio)) {
41 unsigned int type = button->type ?: EV_KEY;
42 int state = (gpio_get_value(gpio) ? 1 : 0) ^ button->active_low;
43
44 input_event(input, type, button->code, !!state);
Phil Blundell78a56aa2007-01-18 00:44:09 -050045 input_sync(input);
46 }
47 }
48
49 return IRQ_HANDLED;
50}
51
52static int __devinit gpio_keys_probe(struct platform_device *pdev)
53{
54 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
55 struct input_dev *input;
56 int i, error;
Anti Sulline15b0212007-09-26 00:01:17 -040057 int wakeup = 0;
Phil Blundell78a56aa2007-01-18 00:44:09 -050058
59 input = input_allocate_device();
60 if (!input)
61 return -ENOMEM;
62
63 platform_set_drvdata(pdev, input);
64
Jiri Slaby7b19ada2007-10-18 23:40:32 -070065 input->evbit[0] = BIT_MASK(EV_KEY);
Phil Blundell78a56aa2007-01-18 00:44:09 -050066
67 input->name = pdev->name;
68 input->phys = "gpio-keys/input0";
Dmitry Torokhov469ba4d2007-04-12 01:34:58 -040069 input->dev.parent = &pdev->dev;
Phil Blundell78a56aa2007-01-18 00:44:09 -050070
71 input->id.bustype = BUS_HOST;
72 input->id.vendor = 0x0001;
73 input->id.product = 0x0001;
74 input->id.version = 0x0100;
75
76 for (i = 0; i < pdata->nbuttons; i++) {
Roman Moravcik84767d02007-05-01 00:39:13 -040077 struct gpio_keys_button *button = &pdata->buttons[i];
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -050078 int irq;
Roman Moravcik84767d02007-05-01 00:39:13 -040079 unsigned int type = button->type ?: EV_KEY;
Phil Blundell78a56aa2007-01-18 00:44:09 -050080
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -050081 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
82 if (error < 0) {
83 pr_err("gpio-keys: failed to request GPIO %d,"
84 " error %d\n", button->gpio, error);
85 goto fail;
86 }
87
88 error = gpio_direction_input(button->gpio);
89 if (error < 0) {
90 pr_err("gpio-keys: failed to configure input"
91 " direction for GPIO %d, error %d\n",
92 button->gpio, error);
93 gpio_free(button->gpio);
94 goto fail;
95 }
96
97 irq = gpio_to_irq(button->gpio);
Anti Sullin006df302007-09-26 00:01:03 -040098 if (irq < 0) {
99 error = irq;
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500100 pr_err("gpio-keys: Unable to get irq number"
101 " for GPIO %d, error %d\n",
Anti Sullin006df302007-09-26 00:01:03 -0400102 button->gpio, error);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500103 gpio_free(button->gpio);
Anti Sullin006df302007-09-26 00:01:03 -0400104 goto fail;
105 }
106
107 error = request_irq(irq, gpio_keys_isr,
108 IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
109 IRQF_TRIGGER_FALLING,
110 button->desc ? button->desc : "gpio_keys",
111 pdev);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500112 if (error) {
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500113 pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500114 irq, error);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500115 gpio_free(button->gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500116 goto fail;
117 }
Roman Moravcik84767d02007-05-01 00:39:13 -0400118
Anti Sulline15b0212007-09-26 00:01:17 -0400119 if (button->wakeup)
120 wakeup = 1;
121
Roman Moravcik84767d02007-05-01 00:39:13 -0400122 input_set_capability(input, type, button->code);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500123 }
124
125 error = input_register_device(input);
126 if (error) {
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500127 pr_err("gpio-keys: Unable to register input device, "
Anti Sullin006df302007-09-26 00:01:03 -0400128 "error: %d\n", error);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500129 goto fail;
130 }
131
Anti Sulline15b0212007-09-26 00:01:17 -0400132 device_init_wakeup(&pdev->dev, wakeup);
133
Phil Blundell78a56aa2007-01-18 00:44:09 -0500134 return 0;
135
136 fail:
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500137 while (--i >= 0) {
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500138 free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500139 gpio_free(pdata->buttons[i].gpio);
140 }
Phil Blundell78a56aa2007-01-18 00:44:09 -0500141
Anti Sullin006df302007-09-26 00:01:03 -0400142 platform_set_drvdata(pdev, NULL);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500143 input_free_device(input);
144
145 return error;
146}
147
148static int __devexit gpio_keys_remove(struct platform_device *pdev)
149{
150 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
151 struct input_dev *input = platform_get_drvdata(pdev);
152 int i;
153
Anti Sulline15b0212007-09-26 00:01:17 -0400154 device_init_wakeup(&pdev->dev, 0);
155
Phil Blundell78a56aa2007-01-18 00:44:09 -0500156 for (i = 0; i < pdata->nbuttons; i++) {
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500157 int irq = gpio_to_irq(pdata->buttons[i].gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500158 free_irq(irq, pdev);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500159 gpio_free(pdata->buttons[i].gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500160 }
161
162 input_unregister_device(input);
163
164 return 0;
165}
166
Anti Sulline15b0212007-09-26 00:01:17 -0400167
168#ifdef CONFIG_PM
169static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
170{
171 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
172 int i;
173
174 if (device_may_wakeup(&pdev->dev)) {
175 for (i = 0; i < pdata->nbuttons; i++) {
176 struct gpio_keys_button *button = &pdata->buttons[i];
177 if (button->wakeup) {
178 int irq = gpio_to_irq(button->gpio);
179 enable_irq_wake(irq);
180 }
181 }
182 }
183
184 return 0;
185}
186
187static int gpio_keys_resume(struct platform_device *pdev)
188{
189 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
190 int i;
191
192 if (device_may_wakeup(&pdev->dev)) {
193 for (i = 0; i < pdata->nbuttons; i++) {
194 struct gpio_keys_button *button = &pdata->buttons[i];
195 if (button->wakeup) {
196 int irq = gpio_to_irq(button->gpio);
197 disable_irq_wake(irq);
198 }
199 }
200 }
201
202 return 0;
203}
204#else
205#define gpio_keys_suspend NULL
206#define gpio_keys_resume NULL
207#endif
208
Phil Blundell78a56aa2007-01-18 00:44:09 -0500209struct platform_driver gpio_keys_device_driver = {
210 .probe = gpio_keys_probe,
211 .remove = __devexit_p(gpio_keys_remove),
Anti Sulline15b0212007-09-26 00:01:17 -0400212 .suspend = gpio_keys_suspend,
213 .resume = gpio_keys_resume,
Phil Blundell78a56aa2007-01-18 00:44:09 -0500214 .driver = {
215 .name = "gpio-keys",
216 }
217};
218
219static int __init gpio_keys_init(void)
220{
221 return platform_driver_register(&gpio_keys_device_driver);
222}
223
224static void __exit gpio_keys_exit(void)
225{
226 platform_driver_unregister(&gpio_keys_device_driver);
227}
228
229module_init(gpio_keys_init);
230module_exit(gpio_keys_exit);
231
232MODULE_LICENSE("GPL");
233MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
234MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");