blob: 5c8e2113cb349c10c8bdebbdafad7bb07b26be35 [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>
Phil Blundell78a56aa2007-01-18 00:44:09 -050012
13#include <linux/init.h>
14#include <linux/fs.h>
15#include <linux/interrupt.h>
16#include <linux/irq.h>
17#include <linux/sched.h>
18#include <linux/pm.h>
19#include <linux/sysctl.h>
20#include <linux/proc_fs.h>
21#include <linux/delay.h>
22#include <linux/platform_device.h>
23#include <linux/input.h>
David Brownell49015be2007-03-05 00:30:22 -080024#include <linux/gpio_keys.h>
Jani Nikulada0d03f2009-06-28 22:38:56 -070025#include <linux/workqueue.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;
Jani Nikulaca865a72009-06-28 22:38:44 -070032 struct timer_list timer;
Jani Nikulada0d03f2009-06-28 22:38:56 -070033 struct work_struct work;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040034};
35
36struct gpio_keys_drvdata {
37 struct input_dev *input;
38 struct gpio_button_data data[0];
39};
40
Jani Nikulada0d03f2009-06-28 22:38:56 -070041static void gpio_keys_report_event(struct work_struct *work)
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040042{
Jani Nikulada0d03f2009-06-28 22:38:56 -070043 struct gpio_button_data *bdata =
44 container_of(work, struct gpio_button_data, work);
Uwe Kleine-Königce25d7e2008-08-08 12:14:36 -040045 struct gpio_keys_button *button = bdata->button;
46 struct input_dev *input = bdata->input;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040047 unsigned int type = button->type ?: EV_KEY;
48 int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
49
50 input_event(input, type, button->code, !!state);
51 input_sync(input);
52}
53
Jani Nikulada0d03f2009-06-28 22:38:56 -070054static void gpio_keys_timer(unsigned long _data)
Jani Nikulaca865a72009-06-28 22:38:44 -070055{
56 struct gpio_button_data *data = (struct gpio_button_data *)_data;
57
Jani Nikulada0d03f2009-06-28 22:38:56 -070058 schedule_work(&data->work);
Jani Nikulaca865a72009-06-28 22:38:44 -070059}
60
Phil Blundell78a56aa2007-01-18 00:44:09 -050061static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
62{
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -040063 struct gpio_button_data *bdata = dev_id;
64 struct gpio_keys_button *button = bdata->button;
Phil Blundell78a56aa2007-01-18 00:44:09 -050065
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -040066 BUG_ON(irq != gpio_to_irq(button->gpio));
Phil Blundell78a56aa2007-01-18 00:44:09 -050067
Jani Nikulaca865a72009-06-28 22:38:44 -070068 if (button->debounce_interval)
69 mod_timer(&bdata->timer,
70 jiffies + msecs_to_jiffies(button->debounce_interval));
71 else
Jani Nikulada0d03f2009-06-28 22:38:56 -070072 schedule_work(&bdata->work);
Roman Moravcik84767d02007-05-01 00:39:13 -040073
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -040074 return IRQ_HANDLED;
Phil Blundell78a56aa2007-01-18 00:44:09 -050075}
76
77static int __devinit gpio_keys_probe(struct platform_device *pdev)
78{
79 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040080 struct gpio_keys_drvdata *ddata;
Ben Dooksdb19fd82009-11-10 21:00:35 -080081 struct device *dev = &pdev->dev;
Phil Blundell78a56aa2007-01-18 00:44:09 -050082 struct input_dev *input;
83 int i, error;
Anti Sulline15b0212007-09-26 00:01:17 -040084 int wakeup = 0;
Phil Blundell78a56aa2007-01-18 00:44:09 -050085
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040086 ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
87 pdata->nbuttons * sizeof(struct gpio_button_data),
88 GFP_KERNEL);
Phil Blundell78a56aa2007-01-18 00:44:09 -050089 input = input_allocate_device();
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040090 if (!ddata || !input) {
Ben Dooksdb19fd82009-11-10 21:00:35 -080091 dev_err(dev, "failed to allocate state\n");
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040092 error = -ENOMEM;
93 goto fail1;
94 }
Phil Blundell78a56aa2007-01-18 00:44:09 -050095
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040096 platform_set_drvdata(pdev, ddata);
Phil Blundell78a56aa2007-01-18 00:44:09 -050097
98 input->name = pdev->name;
99 input->phys = "gpio-keys/input0";
Dmitry Torokhov469ba4d2007-04-12 01:34:58 -0400100 input->dev.parent = &pdev->dev;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500101
102 input->id.bustype = BUS_HOST;
103 input->id.vendor = 0x0001;
104 input->id.product = 0x0001;
105 input->id.version = 0x0100;
106
Dominic Curranb67b4b12008-10-27 22:30:53 -0400107 /* Enable auto repeat feature of Linux input subsystem */
108 if (pdata->rep)
109 __set_bit(EV_REP, input->evbit);
110
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400111 ddata->input = input;
112
Phil Blundell78a56aa2007-01-18 00:44:09 -0500113 for (i = 0; i < pdata->nbuttons; i++) {
Roman Moravcik84767d02007-05-01 00:39:13 -0400114 struct gpio_keys_button *button = &pdata->buttons[i];
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400115 struct gpio_button_data *bdata = &ddata->data[i];
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500116 int irq;
Roman Moravcik84767d02007-05-01 00:39:13 -0400117 unsigned int type = button->type ?: EV_KEY;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500118
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400119 bdata->input = input;
Uwe Kleine-König74dd4392008-07-30 10:33:43 -0400120 bdata->button = button;
Jani Nikulaca865a72009-06-28 22:38:44 -0700121 setup_timer(&bdata->timer,
Jani Nikulada0d03f2009-06-28 22:38:56 -0700122 gpio_keys_timer, (unsigned long)bdata);
123 INIT_WORK(&bdata->work, gpio_keys_report_event);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400124
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500125 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
126 if (error < 0) {
Ben Dooksdb19fd82009-11-10 21:00:35 -0800127 dev_err(dev, "failed to request GPIO %d, error %d\n",
128 button->gpio, error);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400129 goto fail2;
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500130 }
131
132 error = gpio_direction_input(button->gpio);
133 if (error < 0) {
Ben Dooksdb19fd82009-11-10 21:00:35 -0800134 dev_err(dev, "failed to configure"
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500135 " direction for GPIO %d, error %d\n",
136 button->gpio, error);
137 gpio_free(button->gpio);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400138 goto fail2;
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500139 }
140
141 irq = gpio_to_irq(button->gpio);
Anti Sullin006df302007-09-26 00:01:03 -0400142 if (irq < 0) {
143 error = irq;
Ben Dooksdb19fd82009-11-10 21:00:35 -0800144 dev_err(dev, "Unable to get irq number "
145 "for GPIO %d, error %d\n",
Anti Sullin006df302007-09-26 00:01:03 -0400146 button->gpio, error);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500147 gpio_free(button->gpio);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400148 goto fail2;
Anti Sullin006df302007-09-26 00:01:03 -0400149 }
150
151 error = request_irq(irq, gpio_keys_isr,
Dmitry Eremin-Solenikov558a5e22009-11-02 22:04:18 -0800152 IRQF_SHARED |
Dmitry Torokhov64e85632009-04-18 18:45:17 -0700153 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
Anti Sullin006df302007-09-26 00:01:03 -0400154 button->desc ? button->desc : "gpio_keys",
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400155 bdata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500156 if (error) {
Ben Dooksdb19fd82009-11-10 21:00:35 -0800157 dev_err(dev, "Unable to claim irq %d; error %d\n",
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500158 irq, error);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500159 gpio_free(button->gpio);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400160 goto fail2;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500161 }
Roman Moravcik84767d02007-05-01 00:39:13 -0400162
Anti Sulline15b0212007-09-26 00:01:17 -0400163 if (button->wakeup)
164 wakeup = 1;
165
Roman Moravcik84767d02007-05-01 00:39:13 -0400166 input_set_capability(input, type, button->code);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500167 }
168
169 error = input_register_device(input);
170 if (error) {
Ben Dooksdb19fd82009-11-10 21:00:35 -0800171 dev_err(dev, "Unable to register input device, "
Anti Sullin006df302007-09-26 00:01:03 -0400172 "error: %d\n", error);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400173 goto fail2;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500174 }
175
Anti Sulline15b0212007-09-26 00:01:17 -0400176 device_init_wakeup(&pdev->dev, wakeup);
177
Phil Blundell78a56aa2007-01-18 00:44:09 -0500178 return 0;
179
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400180 fail2:
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500181 while (--i >= 0) {
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400182 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
Jani Nikulaca865a72009-06-28 22:38:44 -0700183 if (pdata->buttons[i].debounce_interval)
184 del_timer_sync(&ddata->data[i].timer);
Jani Nikulada0d03f2009-06-28 22:38:56 -0700185 cancel_work_sync(&ddata->data[i].work);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500186 gpio_free(pdata->buttons[i].gpio);
187 }
Phil Blundell78a56aa2007-01-18 00:44:09 -0500188
Anti Sullin006df302007-09-26 00:01:03 -0400189 platform_set_drvdata(pdev, NULL);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400190 fail1:
Phil Blundell78a56aa2007-01-18 00:44:09 -0500191 input_free_device(input);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400192 kfree(ddata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500193
194 return error;
195}
196
197static int __devexit gpio_keys_remove(struct platform_device *pdev)
198{
199 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400200 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
201 struct input_dev *input = ddata->input;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500202 int i;
203
Anti Sulline15b0212007-09-26 00:01:17 -0400204 device_init_wakeup(&pdev->dev, 0);
205
Phil Blundell78a56aa2007-01-18 00:44:09 -0500206 for (i = 0; i < pdata->nbuttons; i++) {
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500207 int irq = gpio_to_irq(pdata->buttons[i].gpio);
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400208 free_irq(irq, &ddata->data[i]);
Jani Nikulaca865a72009-06-28 22:38:44 -0700209 if (pdata->buttons[i].debounce_interval)
210 del_timer_sync(&ddata->data[i].timer);
Jani Nikulada0d03f2009-06-28 22:38:56 -0700211 cancel_work_sync(&ddata->data[i].work);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500212 gpio_free(pdata->buttons[i].gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500213 }
214
215 input_unregister_device(input);
216
217 return 0;
218}
219
Anti Sulline15b0212007-09-26 00:01:17 -0400220
221#ifdef CONFIG_PM
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700222static int gpio_keys_suspend(struct device *dev)
Anti Sulline15b0212007-09-26 00:01:17 -0400223{
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700224 struct platform_device *pdev = to_platform_device(dev);
Anti Sulline15b0212007-09-26 00:01:17 -0400225 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
226 int i;
227
228 if (device_may_wakeup(&pdev->dev)) {
229 for (i = 0; i < pdata->nbuttons; i++) {
230 struct gpio_keys_button *button = &pdata->buttons[i];
231 if (button->wakeup) {
232 int irq = gpio_to_irq(button->gpio);
233 enable_irq_wake(irq);
234 }
235 }
236 }
237
238 return 0;
239}
240
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700241static int gpio_keys_resume(struct device *dev)
Anti Sulline15b0212007-09-26 00:01:17 -0400242{
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700243 struct platform_device *pdev = to_platform_device(dev);
Anti Sulline15b0212007-09-26 00:01:17 -0400244 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
245 int i;
246
247 if (device_may_wakeup(&pdev->dev)) {
248 for (i = 0; i < pdata->nbuttons; i++) {
249 struct gpio_keys_button *button = &pdata->buttons[i];
250 if (button->wakeup) {
251 int irq = gpio_to_irq(button->gpio);
252 disable_irq_wake(irq);
253 }
254 }
255 }
256
257 return 0;
258}
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700259
260static const struct dev_pm_ops gpio_keys_pm_ops = {
261 .suspend = gpio_keys_suspend,
262 .resume = gpio_keys_resume,
263};
Anti Sulline15b0212007-09-26 00:01:17 -0400264#endif
265
Uwe Kleine-König9b070442008-07-30 10:34:02 -0400266static struct platform_driver gpio_keys_device_driver = {
Phil Blundell78a56aa2007-01-18 00:44:09 -0500267 .probe = gpio_keys_probe,
268 .remove = __devexit_p(gpio_keys_remove),
Phil Blundell78a56aa2007-01-18 00:44:09 -0500269 .driver = {
270 .name = "gpio-keys",
Kay Sieversd7b52472008-04-18 00:24:42 -0400271 .owner = THIS_MODULE,
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700272#ifdef CONFIG_PM
273 .pm = &gpio_keys_pm_ops,
274#endif
Phil Blundell78a56aa2007-01-18 00:44:09 -0500275 }
276};
277
278static int __init gpio_keys_init(void)
279{
280 return platform_driver_register(&gpio_keys_device_driver);
281}
282
283static void __exit gpio_keys_exit(void)
284{
285 platform_driver_unregister(&gpio_keys_device_driver);
286}
287
288module_init(gpio_keys_init);
289module_exit(gpio_keys_exit);
290
291MODULE_LICENSE("GPL");
292MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
293MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
Kay Sieversd7b52472008-04-18 00:24:42 -0400294MODULE_ALIAS("platform:gpio-keys");