blob: b236709a2c017d79e241c9c8eaeb1b2cc03945a7 [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
76static int __devinit gpio_keys_probe(struct platform_device *pdev)
77{
78 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040079 struct gpio_keys_drvdata *ddata;
Ben Dooksdb19fd82009-11-10 21:00:35 -080080 struct device *dev = &pdev->dev;
Phil Blundell78a56aa2007-01-18 00:44:09 -050081 struct input_dev *input;
82 int i, error;
Anti Sulline15b0212007-09-26 00:01:17 -040083 int wakeup = 0;
Phil Blundell78a56aa2007-01-18 00:44:09 -050084
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040085 ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
86 pdata->nbuttons * sizeof(struct gpio_button_data),
87 GFP_KERNEL);
Phil Blundell78a56aa2007-01-18 00:44:09 -050088 input = input_allocate_device();
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040089 if (!ddata || !input) {
Ben Dooksdb19fd82009-11-10 21:00:35 -080090 dev_err(dev, "failed to allocate state\n");
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040091 error = -ENOMEM;
92 goto fail1;
93 }
Phil Blundell78a56aa2007-01-18 00:44:09 -050094
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040095 platform_set_drvdata(pdev, ddata);
Phil Blundell78a56aa2007-01-18 00:44:09 -050096
97 input->name = pdev->name;
98 input->phys = "gpio-keys/input0";
Dmitry Torokhov469ba4d2007-04-12 01:34:58 -040099 input->dev.parent = &pdev->dev;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500100
101 input->id.bustype = BUS_HOST;
102 input->id.vendor = 0x0001;
103 input->id.product = 0x0001;
104 input->id.version = 0x0100;
105
Dominic Curranb67b4b12008-10-27 22:30:53 -0400106 /* Enable auto repeat feature of Linux input subsystem */
107 if (pdata->rep)
108 __set_bit(EV_REP, input->evbit);
109
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400110 ddata->input = input;
111
Phil Blundell78a56aa2007-01-18 00:44:09 -0500112 for (i = 0; i < pdata->nbuttons; i++) {
Roman Moravcik84767d02007-05-01 00:39:13 -0400113 struct gpio_keys_button *button = &pdata->buttons[i];
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400114 struct gpio_button_data *bdata = &ddata->data[i];
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500115 int irq;
Roman Moravcik84767d02007-05-01 00:39:13 -0400116 unsigned int type = button->type ?: EV_KEY;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500117
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400118 bdata->input = input;
Uwe Kleine-König74dd4392008-07-30 10:33:43 -0400119 bdata->button = button;
Jani Nikulaca865a72009-06-28 22:38:44 -0700120 setup_timer(&bdata->timer,
Jani Nikulada0d03f2009-06-28 22:38:56 -0700121 gpio_keys_timer, (unsigned long)bdata);
122 INIT_WORK(&bdata->work, gpio_keys_report_event);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400123
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500124 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
125 if (error < 0) {
Ben Dooksdb19fd82009-11-10 21:00:35 -0800126 dev_err(dev, "failed to request GPIO %d, error %d\n",
127 button->gpio, error);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400128 goto fail2;
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500129 }
130
131 error = gpio_direction_input(button->gpio);
132 if (error < 0) {
Ben Dooksdb19fd82009-11-10 21:00:35 -0800133 dev_err(dev, "failed to configure"
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500134 " direction for GPIO %d, error %d\n",
135 button->gpio, error);
136 gpio_free(button->gpio);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400137 goto fail2;
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500138 }
139
140 irq = gpio_to_irq(button->gpio);
Anti Sullin006df302007-09-26 00:01:03 -0400141 if (irq < 0) {
142 error = irq;
Ben Dooksdb19fd82009-11-10 21:00:35 -0800143 dev_err(dev, "Unable to get irq number "
144 "for GPIO %d, error %d\n",
Anti Sullin006df302007-09-26 00:01:03 -0400145 button->gpio, error);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500146 gpio_free(button->gpio);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400147 goto fail2;
Anti Sullin006df302007-09-26 00:01:03 -0400148 }
149
150 error = request_irq(irq, gpio_keys_isr,
Dmitry Eremin-Solenikov558a5e22009-11-02 22:04:18 -0800151 IRQF_SHARED |
Dmitry Torokhov64e85632009-04-18 18:45:17 -0700152 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
Anti Sullin006df302007-09-26 00:01:03 -0400153 button->desc ? button->desc : "gpio_keys",
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400154 bdata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500155 if (error) {
Ben Dooksdb19fd82009-11-10 21:00:35 -0800156 dev_err(dev, "Unable to claim irq %d; error %d\n",
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500157 irq, error);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500158 gpio_free(button->gpio);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400159 goto fail2;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500160 }
Roman Moravcik84767d02007-05-01 00:39:13 -0400161
Anti Sulline15b0212007-09-26 00:01:17 -0400162 if (button->wakeup)
163 wakeup = 1;
164
Roman Moravcik84767d02007-05-01 00:39:13 -0400165 input_set_capability(input, type, button->code);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500166 }
167
168 error = input_register_device(input);
169 if (error) {
Ben Dooksdb19fd82009-11-10 21:00:35 -0800170 dev_err(dev, "Unable to register input device, "
Anti Sullin006df302007-09-26 00:01:03 -0400171 "error: %d\n", error);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400172 goto fail2;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500173 }
174
Anti Sulline15b0212007-09-26 00:01:17 -0400175 device_init_wakeup(&pdev->dev, wakeup);
176
Phil Blundell78a56aa2007-01-18 00:44:09 -0500177 return 0;
178
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400179 fail2:
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500180 while (--i >= 0) {
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400181 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
Jani Nikulaca865a72009-06-28 22:38:44 -0700182 if (pdata->buttons[i].debounce_interval)
183 del_timer_sync(&ddata->data[i].timer);
Jani Nikulada0d03f2009-06-28 22:38:56 -0700184 cancel_work_sync(&ddata->data[i].work);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500185 gpio_free(pdata->buttons[i].gpio);
186 }
Phil Blundell78a56aa2007-01-18 00:44:09 -0500187
Anti Sullin006df302007-09-26 00:01:03 -0400188 platform_set_drvdata(pdev, NULL);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400189 fail1:
Phil Blundell78a56aa2007-01-18 00:44:09 -0500190 input_free_device(input);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400191 kfree(ddata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500192
193 return error;
194}
195
196static int __devexit gpio_keys_remove(struct platform_device *pdev)
197{
198 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400199 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
200 struct input_dev *input = ddata->input;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500201 int i;
202
Anti Sulline15b0212007-09-26 00:01:17 -0400203 device_init_wakeup(&pdev->dev, 0);
204
Phil Blundell78a56aa2007-01-18 00:44:09 -0500205 for (i = 0; i < pdata->nbuttons; i++) {
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500206 int irq = gpio_to_irq(pdata->buttons[i].gpio);
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400207 free_irq(irq, &ddata->data[i]);
Jani Nikulaca865a72009-06-28 22:38:44 -0700208 if (pdata->buttons[i].debounce_interval)
209 del_timer_sync(&ddata->data[i].timer);
Jani Nikulada0d03f2009-06-28 22:38:56 -0700210 cancel_work_sync(&ddata->data[i].work);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500211 gpio_free(pdata->buttons[i].gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500212 }
213
214 input_unregister_device(input);
215
216 return 0;
217}
218
Anti Sulline15b0212007-09-26 00:01:17 -0400219
220#ifdef CONFIG_PM
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700221static int gpio_keys_suspend(struct device *dev)
Anti Sulline15b0212007-09-26 00:01:17 -0400222{
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700223 struct platform_device *pdev = to_platform_device(dev);
Anti Sulline15b0212007-09-26 00:01:17 -0400224 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
225 int i;
226
227 if (device_may_wakeup(&pdev->dev)) {
228 for (i = 0; i < pdata->nbuttons; i++) {
229 struct gpio_keys_button *button = &pdata->buttons[i];
230 if (button->wakeup) {
231 int irq = gpio_to_irq(button->gpio);
232 enable_irq_wake(irq);
233 }
234 }
235 }
236
237 return 0;
238}
239
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700240static int gpio_keys_resume(struct device *dev)
Anti Sulline15b0212007-09-26 00:01:17 -0400241{
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700242 struct platform_device *pdev = to_platform_device(dev);
Anti Sulline15b0212007-09-26 00:01:17 -0400243 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
244 int i;
245
246 if (device_may_wakeup(&pdev->dev)) {
247 for (i = 0; i < pdata->nbuttons; i++) {
248 struct gpio_keys_button *button = &pdata->buttons[i];
249 if (button->wakeup) {
250 int irq = gpio_to_irq(button->gpio);
251 disable_irq_wake(irq);
252 }
253 }
254 }
255
256 return 0;
257}
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700258
259static const struct dev_pm_ops gpio_keys_pm_ops = {
260 .suspend = gpio_keys_suspend,
261 .resume = gpio_keys_resume,
262};
Anti Sulline15b0212007-09-26 00:01:17 -0400263#endif
264
Uwe Kleine-König9b070442008-07-30 10:34:02 -0400265static struct platform_driver gpio_keys_device_driver = {
Phil Blundell78a56aa2007-01-18 00:44:09 -0500266 .probe = gpio_keys_probe,
267 .remove = __devexit_p(gpio_keys_remove),
Phil Blundell78a56aa2007-01-18 00:44:09 -0500268 .driver = {
269 .name = "gpio-keys",
Kay Sieversd7b52472008-04-18 00:24:42 -0400270 .owner = THIS_MODULE,
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700271#ifdef CONFIG_PM
272 .pm = &gpio_keys_pm_ops,
273#endif
Phil Blundell78a56aa2007-01-18 00:44:09 -0500274 }
275};
276
277static int __init gpio_keys_init(void)
278{
279 return platform_driver_register(&gpio_keys_device_driver);
280}
281
282static void __exit gpio_keys_exit(void)
283{
284 platform_driver_unregister(&gpio_keys_device_driver);
285}
286
287module_init(gpio_keys_init);
288module_exit(gpio_keys_exit);
289
290MODULE_LICENSE("GPL");
291MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
292MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
Kay Sieversd7b52472008-04-18 00:24:42 -0400293MODULE_ALIAS("platform:gpio-keys");