blob: 8941a8ba89bf727a1da1391a469a5b21a57f4f08 [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>
Ben Dooks111bc592009-11-10 21:01:31 -080026#include <linux/gpio.h>
Phil Blundell78a56aa2007-01-18 00:44:09 -050027
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040028struct gpio_button_data {
29 struct gpio_keys_button *button;
30 struct input_dev *input;
Jani Nikulaca865a72009-06-28 22:38:44 -070031 struct timer_list timer;
Jani Nikulada0d03f2009-06-28 22:38:56 -070032 struct work_struct work;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040033};
34
35struct gpio_keys_drvdata {
36 struct input_dev *input;
37 struct gpio_button_data data[0];
38};
39
Jani Nikulada0d03f2009-06-28 22:38:56 -070040static void gpio_keys_report_event(struct work_struct *work)
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040041{
Jani Nikulada0d03f2009-06-28 22:38:56 -070042 struct gpio_button_data *bdata =
43 container_of(work, struct gpio_button_data, work);
Uwe Kleine-Königce25d7e2008-08-08 12:14:36 -040044 struct gpio_keys_button *button = bdata->button;
45 struct input_dev *input = bdata->input;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040046 unsigned int type = button->type ?: EV_KEY;
47 int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
48
49 input_event(input, type, button->code, !!state);
50 input_sync(input);
51}
52
Jani Nikulada0d03f2009-06-28 22:38:56 -070053static void gpio_keys_timer(unsigned long _data)
Jani Nikulaca865a72009-06-28 22:38:44 -070054{
55 struct gpio_button_data *data = (struct gpio_button_data *)_data;
56
Jani Nikulada0d03f2009-06-28 22:38:56 -070057 schedule_work(&data->work);
Jani Nikulaca865a72009-06-28 22:38:44 -070058}
59
Phil Blundell78a56aa2007-01-18 00:44:09 -050060static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
61{
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -040062 struct gpio_button_data *bdata = dev_id;
63 struct gpio_keys_button *button = bdata->button;
Phil Blundell78a56aa2007-01-18 00:44:09 -050064
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -040065 BUG_ON(irq != gpio_to_irq(button->gpio));
Phil Blundell78a56aa2007-01-18 00:44:09 -050066
Jani Nikulaca865a72009-06-28 22:38:44 -070067 if (button->debounce_interval)
68 mod_timer(&bdata->timer,
69 jiffies + msecs_to_jiffies(button->debounce_interval));
70 else
Jani Nikulada0d03f2009-06-28 22:38:56 -070071 schedule_work(&bdata->work);
Roman Moravcik84767d02007-05-01 00:39:13 -040072
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -040073 return IRQ_HANDLED;
Phil Blundell78a56aa2007-01-18 00:44:09 -050074}
75
Ben Dooksbc8f1ea2009-11-10 21:01:31 -080076static int __devinit gpio_keys_setup_key(struct device *dev,
77 struct gpio_button_data *bdata,
78 struct gpio_keys_button *button)
79{
80 char *desc = button->desc ? button->desc : "gpio_keys";
81 int irq, error;
82
83 setup_timer(&bdata->timer, gpio_keys_timer, (unsigned long)bdata);
84 INIT_WORK(&bdata->work, gpio_keys_report_event);
85
86 error = gpio_request(button->gpio, desc);
87 if (error < 0) {
88 dev_err(dev, "failed to request GPIO %d, error %d\n",
89 button->gpio, error);
90 goto fail2;
91 }
92
93 error = gpio_direction_input(button->gpio);
94 if (error < 0) {
95 dev_err(dev, "failed to configure"
96 " direction for GPIO %d, error %d\n",
97 button->gpio, error);
98 goto fail3;
99 }
100
101 irq = gpio_to_irq(button->gpio);
102 if (irq < 0) {
103 error = irq;
104 dev_err(dev, "Unable to get irq number for GPIO %d, error %d\n",
105 button->gpio, error);
106 goto fail3;
107 }
108
109 error = request_irq(irq, gpio_keys_isr,
110 IRQF_SHARED |
111 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
112 desc, bdata);
113 if (error) {
114 dev_err(dev, "Unable to claim irq %d; error %d\n",
115 irq, error);
116 goto fail3;
117 }
118
119 return 0;
120
121fail3:
122 gpio_free(button->gpio);
123fail2:
124 return error;
125}
126
Phil Blundell78a56aa2007-01-18 00:44:09 -0500127static int __devinit gpio_keys_probe(struct platform_device *pdev)
128{
129 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400130 struct gpio_keys_drvdata *ddata;
Ben Dooksdb19fd82009-11-10 21:00:35 -0800131 struct device *dev = &pdev->dev;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500132 struct input_dev *input;
133 int i, error;
Anti Sulline15b0212007-09-26 00:01:17 -0400134 int wakeup = 0;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500135
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400136 ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
137 pdata->nbuttons * sizeof(struct gpio_button_data),
138 GFP_KERNEL);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500139 input = input_allocate_device();
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400140 if (!ddata || !input) {
Ben Dooksdb19fd82009-11-10 21:00:35 -0800141 dev_err(dev, "failed to allocate state\n");
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400142 error = -ENOMEM;
143 goto fail1;
144 }
Phil Blundell78a56aa2007-01-18 00:44:09 -0500145
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400146 platform_set_drvdata(pdev, ddata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500147
148 input->name = pdev->name;
149 input->phys = "gpio-keys/input0";
Dmitry Torokhov469ba4d2007-04-12 01:34:58 -0400150 input->dev.parent = &pdev->dev;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500151
152 input->id.bustype = BUS_HOST;
153 input->id.vendor = 0x0001;
154 input->id.product = 0x0001;
155 input->id.version = 0x0100;
156
Dominic Curranb67b4b12008-10-27 22:30:53 -0400157 /* Enable auto repeat feature of Linux input subsystem */
158 if (pdata->rep)
159 __set_bit(EV_REP, input->evbit);
160
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400161 ddata->input = input;
162
Phil Blundell78a56aa2007-01-18 00:44:09 -0500163 for (i = 0; i < pdata->nbuttons; i++) {
Roman Moravcik84767d02007-05-01 00:39:13 -0400164 struct gpio_keys_button *button = &pdata->buttons[i];
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400165 struct gpio_button_data *bdata = &ddata->data[i];
Roman Moravcik84767d02007-05-01 00:39:13 -0400166 unsigned int type = button->type ?: EV_KEY;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500167
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400168 bdata->input = input;
Uwe Kleine-König74dd4392008-07-30 10:33:43 -0400169 bdata->button = button;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400170
Ben Dooksbc8f1ea2009-11-10 21:01:31 -0800171 error = gpio_keys_setup_key(dev, bdata, button);
172 if (error)
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400173 goto fail2;
Roman Moravcik84767d02007-05-01 00:39:13 -0400174
Anti Sulline15b0212007-09-26 00:01:17 -0400175 if (button->wakeup)
176 wakeup = 1;
177
Roman Moravcik84767d02007-05-01 00:39:13 -0400178 input_set_capability(input, type, button->code);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500179 }
180
181 error = input_register_device(input);
182 if (error) {
Ben Dooksdb19fd82009-11-10 21:00:35 -0800183 dev_err(dev, "Unable to register input device, "
Anti Sullin006df302007-09-26 00:01:03 -0400184 "error: %d\n", error);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400185 goto fail2;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500186 }
187
Anti Sulline15b0212007-09-26 00:01:17 -0400188 device_init_wakeup(&pdev->dev, wakeup);
189
Phil Blundell78a56aa2007-01-18 00:44:09 -0500190 return 0;
191
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400192 fail2:
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500193 while (--i >= 0) {
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400194 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
Jani Nikulaca865a72009-06-28 22:38:44 -0700195 if (pdata->buttons[i].debounce_interval)
196 del_timer_sync(&ddata->data[i].timer);
Jani Nikulada0d03f2009-06-28 22:38:56 -0700197 cancel_work_sync(&ddata->data[i].work);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500198 gpio_free(pdata->buttons[i].gpio);
199 }
Phil Blundell78a56aa2007-01-18 00:44:09 -0500200
Anti Sullin006df302007-09-26 00:01:03 -0400201 platform_set_drvdata(pdev, NULL);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400202 fail1:
Phil Blundell78a56aa2007-01-18 00:44:09 -0500203 input_free_device(input);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400204 kfree(ddata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500205
206 return error;
207}
208
209static int __devexit gpio_keys_remove(struct platform_device *pdev)
210{
211 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400212 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
213 struct input_dev *input = ddata->input;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500214 int i;
215
Anti Sulline15b0212007-09-26 00:01:17 -0400216 device_init_wakeup(&pdev->dev, 0);
217
Phil Blundell78a56aa2007-01-18 00:44:09 -0500218 for (i = 0; i < pdata->nbuttons; i++) {
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500219 int irq = gpio_to_irq(pdata->buttons[i].gpio);
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400220 free_irq(irq, &ddata->data[i]);
Jani Nikulaca865a72009-06-28 22:38:44 -0700221 if (pdata->buttons[i].debounce_interval)
222 del_timer_sync(&ddata->data[i].timer);
Jani Nikulada0d03f2009-06-28 22:38:56 -0700223 cancel_work_sync(&ddata->data[i].work);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500224 gpio_free(pdata->buttons[i].gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500225 }
226
227 input_unregister_device(input);
228
229 return 0;
230}
231
Anti Sulline15b0212007-09-26 00:01:17 -0400232
233#ifdef CONFIG_PM
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700234static int gpio_keys_suspend(struct device *dev)
Anti Sulline15b0212007-09-26 00:01:17 -0400235{
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700236 struct platform_device *pdev = to_platform_device(dev);
Anti Sulline15b0212007-09-26 00:01:17 -0400237 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
238 int i;
239
240 if (device_may_wakeup(&pdev->dev)) {
241 for (i = 0; i < pdata->nbuttons; i++) {
242 struct gpio_keys_button *button = &pdata->buttons[i];
243 if (button->wakeup) {
244 int irq = gpio_to_irq(button->gpio);
245 enable_irq_wake(irq);
246 }
247 }
248 }
249
250 return 0;
251}
252
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700253static int gpio_keys_resume(struct device *dev)
Anti Sulline15b0212007-09-26 00:01:17 -0400254{
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700255 struct platform_device *pdev = to_platform_device(dev);
Anti Sulline15b0212007-09-26 00:01:17 -0400256 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
257 int i;
258
259 if (device_may_wakeup(&pdev->dev)) {
260 for (i = 0; i < pdata->nbuttons; i++) {
261 struct gpio_keys_button *button = &pdata->buttons[i];
262 if (button->wakeup) {
263 int irq = gpio_to_irq(button->gpio);
264 disable_irq_wake(irq);
265 }
266 }
267 }
268
269 return 0;
270}
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700271
272static const struct dev_pm_ops gpio_keys_pm_ops = {
273 .suspend = gpio_keys_suspend,
274 .resume = gpio_keys_resume,
275};
Anti Sulline15b0212007-09-26 00:01:17 -0400276#endif
277
Uwe Kleine-König9b070442008-07-30 10:34:02 -0400278static struct platform_driver gpio_keys_device_driver = {
Phil Blundell78a56aa2007-01-18 00:44:09 -0500279 .probe = gpio_keys_probe,
280 .remove = __devexit_p(gpio_keys_remove),
Phil Blundell78a56aa2007-01-18 00:44:09 -0500281 .driver = {
282 .name = "gpio-keys",
Kay Sieversd7b52472008-04-18 00:24:42 -0400283 .owner = THIS_MODULE,
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700284#ifdef CONFIG_PM
285 .pm = &gpio_keys_pm_ops,
286#endif
Phil Blundell78a56aa2007-01-18 00:44:09 -0500287 }
288};
289
290static int __init gpio_keys_init(void)
291{
292 return platform_driver_register(&gpio_keys_device_driver);
293}
294
295static void __exit gpio_keys_exit(void)
296{
297 platform_driver_unregister(&gpio_keys_device_driver);
298}
299
300module_init(gpio_keys_init);
301module_exit(gpio_keys_exit);
302
303MODULE_LICENSE("GPL");
304MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
305MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
Kay Sieversd7b52472008-04-18 00:24:42 -0400306MODULE_ALIAS("platform:gpio-keys");