blob: fe22ca34d57640ea4998c4f1ce7806b2ccd41bac [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
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040029struct gpio_button_data {
30 struct gpio_keys_button *button;
31 struct input_dev *input;
32 struct timer_list timer;
33};
34
35struct gpio_keys_drvdata {
36 struct input_dev *input;
37 struct gpio_button_data data[0];
38};
39
Uwe Kleine-Königce25d7e2008-08-08 12:14:36 -040040static void gpio_keys_report_event(struct gpio_button_data *bdata)
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040041{
Uwe Kleine-Königce25d7e2008-08-08 12:14:36 -040042 struct gpio_keys_button *button = bdata->button;
43 struct input_dev *input = bdata->input;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040044 unsigned int type = button->type ?: EV_KEY;
45 int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
46
47 input_event(input, type, button->code, !!state);
48 input_sync(input);
49}
50
51static void gpio_check_button(unsigned long _data)
52{
53 struct gpio_button_data *data = (struct gpio_button_data *)_data;
54
Uwe Kleine-Königce25d7e2008-08-08 12:14:36 -040055 gpio_keys_report_event(data);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040056}
57
Phil Blundell78a56aa2007-01-18 00:44:09 -050058static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
59{
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -040060 struct gpio_button_data *bdata = dev_id;
61 struct gpio_keys_button *button = bdata->button;
Phil Blundell78a56aa2007-01-18 00:44:09 -050062
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -040063 BUG_ON(irq != gpio_to_irq(button->gpio));
Phil Blundell78a56aa2007-01-18 00:44:09 -050064
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -040065 if (button->debounce_interval)
66 mod_timer(&bdata->timer,
67 jiffies + msecs_to_jiffies(button->debounce_interval));
68 else
Uwe Kleine-Königce25d7e2008-08-08 12:14:36 -040069 gpio_keys_report_event(bdata);
Roman Moravcik84767d02007-05-01 00:39:13 -040070
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -040071 return IRQ_HANDLED;
Phil Blundell78a56aa2007-01-18 00:44:09 -050072}
73
74static int __devinit gpio_keys_probe(struct platform_device *pdev)
75{
76 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040077 struct gpio_keys_drvdata *ddata;
Phil Blundell78a56aa2007-01-18 00:44:09 -050078 struct input_dev *input;
79 int i, error;
Anti Sulline15b0212007-09-26 00:01:17 -040080 int wakeup = 0;
Phil Blundell78a56aa2007-01-18 00:44:09 -050081
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040082 ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
83 pdata->nbuttons * sizeof(struct gpio_button_data),
84 GFP_KERNEL);
Phil Blundell78a56aa2007-01-18 00:44:09 -050085 input = input_allocate_device();
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040086 if (!ddata || !input) {
87 error = -ENOMEM;
88 goto fail1;
89 }
Phil Blundell78a56aa2007-01-18 00:44:09 -050090
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040091 platform_set_drvdata(pdev, ddata);
Phil Blundell78a56aa2007-01-18 00:44:09 -050092
93 input->name = pdev->name;
94 input->phys = "gpio-keys/input0";
Dmitry Torokhov469ba4d2007-04-12 01:34:58 -040095 input->dev.parent = &pdev->dev;
Phil Blundell78a56aa2007-01-18 00:44:09 -050096
97 input->id.bustype = BUS_HOST;
98 input->id.vendor = 0x0001;
99 input->id.product = 0x0001;
100 input->id.version = 0x0100;
101
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400102 ddata->input = input;
103
Phil Blundell78a56aa2007-01-18 00:44:09 -0500104 for (i = 0; i < pdata->nbuttons; i++) {
Roman Moravcik84767d02007-05-01 00:39:13 -0400105 struct gpio_keys_button *button = &pdata->buttons[i];
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400106 struct gpio_button_data *bdata = &ddata->data[i];
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500107 int irq;
Roman Moravcik84767d02007-05-01 00:39:13 -0400108 unsigned int type = button->type ?: EV_KEY;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500109
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400110 bdata->input = input;
111 setup_timer(&bdata->timer,
112 gpio_check_button, (unsigned long)bdata);
113
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500114 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
115 if (error < 0) {
116 pr_err("gpio-keys: failed to request GPIO %d,"
117 " error %d\n", button->gpio, error);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400118 goto fail2;
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500119 }
120
121 error = gpio_direction_input(button->gpio);
122 if (error < 0) {
123 pr_err("gpio-keys: failed to configure input"
124 " direction for GPIO %d, error %d\n",
125 button->gpio, error);
126 gpio_free(button->gpio);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400127 goto fail2;
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500128 }
129
130 irq = gpio_to_irq(button->gpio);
Anti Sullin006df302007-09-26 00:01:03 -0400131 if (irq < 0) {
132 error = irq;
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500133 pr_err("gpio-keys: Unable to get irq number"
134 " for GPIO %d, error %d\n",
Anti Sullin006df302007-09-26 00:01:03 -0400135 button->gpio, error);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500136 gpio_free(button->gpio);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400137 goto fail2;
Anti Sullin006df302007-09-26 00:01:03 -0400138 }
139
140 error = request_irq(irq, gpio_keys_isr,
141 IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
142 IRQF_TRIGGER_FALLING,
143 button->desc ? button->desc : "gpio_keys",
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400144 bdata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500145 if (error) {
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500146 pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500147 irq, error);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500148 gpio_free(button->gpio);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400149 goto fail2;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500150 }
Roman Moravcik84767d02007-05-01 00:39:13 -0400151
Anti Sulline15b0212007-09-26 00:01:17 -0400152 if (button->wakeup)
153 wakeup = 1;
154
Roman Moravcik84767d02007-05-01 00:39:13 -0400155 input_set_capability(input, type, button->code);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500156 }
157
158 error = input_register_device(input);
159 if (error) {
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500160 pr_err("gpio-keys: Unable to register input device, "
Anti Sullin006df302007-09-26 00:01:03 -0400161 "error: %d\n", error);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400162 goto fail2;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500163 }
164
Anti Sulline15b0212007-09-26 00:01:17 -0400165 device_init_wakeup(&pdev->dev, wakeup);
166
Phil Blundell78a56aa2007-01-18 00:44:09 -0500167 return 0;
168
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400169 fail2:
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500170 while (--i >= 0) {
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400171 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400172 if (pdata->buttons[i].debounce_interval)
173 del_timer_sync(&ddata->data[i].timer);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500174 gpio_free(pdata->buttons[i].gpio);
175 }
Phil Blundell78a56aa2007-01-18 00:44:09 -0500176
Anti Sullin006df302007-09-26 00:01:03 -0400177 platform_set_drvdata(pdev, NULL);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400178 fail1:
Phil Blundell78a56aa2007-01-18 00:44:09 -0500179 input_free_device(input);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400180 kfree(ddata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500181
182 return error;
183}
184
185static int __devexit gpio_keys_remove(struct platform_device *pdev)
186{
187 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400188 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
189 struct input_dev *input = ddata->input;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500190 int i;
191
Anti Sulline15b0212007-09-26 00:01:17 -0400192 device_init_wakeup(&pdev->dev, 0);
193
Phil Blundell78a56aa2007-01-18 00:44:09 -0500194 for (i = 0; i < pdata->nbuttons; i++) {
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500195 int irq = gpio_to_irq(pdata->buttons[i].gpio);
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400196 free_irq(irq, &ddata->data[i]);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400197 if (pdata->buttons[i].debounce_interval)
198 del_timer_sync(&ddata->data[i].timer);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500199 gpio_free(pdata->buttons[i].gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500200 }
201
202 input_unregister_device(input);
203
204 return 0;
205}
206
Anti Sulline15b0212007-09-26 00:01:17 -0400207
208#ifdef CONFIG_PM
209static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
210{
211 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
212 int i;
213
214 if (device_may_wakeup(&pdev->dev)) {
215 for (i = 0; i < pdata->nbuttons; i++) {
216 struct gpio_keys_button *button = &pdata->buttons[i];
217 if (button->wakeup) {
218 int irq = gpio_to_irq(button->gpio);
219 enable_irq_wake(irq);
220 }
221 }
222 }
223
224 return 0;
225}
226
227static int gpio_keys_resume(struct platform_device *pdev)
228{
229 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
230 int i;
231
232 if (device_may_wakeup(&pdev->dev)) {
233 for (i = 0; i < pdata->nbuttons; i++) {
234 struct gpio_keys_button *button = &pdata->buttons[i];
235 if (button->wakeup) {
236 int irq = gpio_to_irq(button->gpio);
237 disable_irq_wake(irq);
238 }
239 }
240 }
241
242 return 0;
243}
244#else
245#define gpio_keys_suspend NULL
246#define gpio_keys_resume NULL
247#endif
248
Phil Blundell78a56aa2007-01-18 00:44:09 -0500249struct platform_driver gpio_keys_device_driver = {
250 .probe = gpio_keys_probe,
251 .remove = __devexit_p(gpio_keys_remove),
Anti Sulline15b0212007-09-26 00:01:17 -0400252 .suspend = gpio_keys_suspend,
253 .resume = gpio_keys_resume,
Phil Blundell78a56aa2007-01-18 00:44:09 -0500254 .driver = {
255 .name = "gpio-keys",
Kay Sieversd7b52472008-04-18 00:24:42 -0400256 .owner = THIS_MODULE,
Phil Blundell78a56aa2007-01-18 00:44:09 -0500257 }
258};
259
260static int __init gpio_keys_init(void)
261{
262 return platform_driver_register(&gpio_keys_device_driver);
263}
264
265static void __exit gpio_keys_exit(void)
266{
267 platform_driver_unregister(&gpio_keys_device_driver);
268}
269
270module_init(gpio_keys_init);
271module_exit(gpio_keys_exit);
272
273MODULE_LICENSE("GPL");
274MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
275MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
Kay Sieversd7b52472008-04-18 00:24:42 -0400276MODULE_ALIAS("platform:gpio-keys");