blob: 9767213b6c8f1f533619e393cc9144bbfd026658 [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>
Phil Blundell78a56aa2007-01-18 00:44:09 -050025
Philipp Zabel0d98f6b2007-02-18 01:40:46 -050026#include <asm/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;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040032};
33
34struct gpio_keys_drvdata {
35 struct input_dev *input;
36 struct gpio_button_data data[0];
37};
38
Jani Nikulaca865a72009-06-28 22:38:44 -070039static void gpio_keys_report_event(struct gpio_button_data *bdata)
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040040{
Uwe Kleine-Königce25d7e2008-08-08 12:14:36 -040041 struct gpio_keys_button *button = bdata->button;
42 struct input_dev *input = bdata->input;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040043 unsigned int type = button->type ?: EV_KEY;
44 int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
45
46 input_event(input, type, button->code, !!state);
47 input_sync(input);
48}
49
Jani Nikulaca865a72009-06-28 22:38:44 -070050static void gpio_check_button(unsigned long _data)
51{
52 struct gpio_button_data *data = (struct gpio_button_data *)_data;
53
54 gpio_keys_report_event(data);
55}
56
Phil Blundell78a56aa2007-01-18 00:44:09 -050057static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
58{
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -040059 struct gpio_button_data *bdata = dev_id;
60 struct gpio_keys_button *button = bdata->button;
Phil Blundell78a56aa2007-01-18 00:44:09 -050061
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -040062 BUG_ON(irq != gpio_to_irq(button->gpio));
Phil Blundell78a56aa2007-01-18 00:44:09 -050063
Jani Nikulaca865a72009-06-28 22:38:44 -070064 if (button->debounce_interval)
65 mod_timer(&bdata->timer,
66 jiffies + msecs_to_jiffies(button->debounce_interval));
67 else
68 gpio_keys_report_event(bdata);
Roman Moravcik84767d02007-05-01 00:39:13 -040069
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -040070 return IRQ_HANDLED;
Phil Blundell78a56aa2007-01-18 00:44:09 -050071}
72
73static int __devinit gpio_keys_probe(struct platform_device *pdev)
74{
75 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040076 struct gpio_keys_drvdata *ddata;
Phil Blundell78a56aa2007-01-18 00:44:09 -050077 struct input_dev *input;
78 int i, error;
Anti Sulline15b0212007-09-26 00:01:17 -040079 int wakeup = 0;
Phil Blundell78a56aa2007-01-18 00:44:09 -050080
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040081 ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
82 pdata->nbuttons * sizeof(struct gpio_button_data),
83 GFP_KERNEL);
Phil Blundell78a56aa2007-01-18 00:44:09 -050084 input = input_allocate_device();
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040085 if (!ddata || !input) {
86 error = -ENOMEM;
87 goto fail1;
88 }
Phil Blundell78a56aa2007-01-18 00:44:09 -050089
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040090 platform_set_drvdata(pdev, ddata);
Phil Blundell78a56aa2007-01-18 00:44:09 -050091
92 input->name = pdev->name;
93 input->phys = "gpio-keys/input0";
Dmitry Torokhov469ba4d2007-04-12 01:34:58 -040094 input->dev.parent = &pdev->dev;
Phil Blundell78a56aa2007-01-18 00:44:09 -050095
96 input->id.bustype = BUS_HOST;
97 input->id.vendor = 0x0001;
98 input->id.product = 0x0001;
99 input->id.version = 0x0100;
100
Dominic Curranb67b4b12008-10-27 22:30:53 -0400101 /* Enable auto repeat feature of Linux input subsystem */
102 if (pdata->rep)
103 __set_bit(EV_REP, input->evbit);
104
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400105 ddata->input = input;
106
Phil Blundell78a56aa2007-01-18 00:44:09 -0500107 for (i = 0; i < pdata->nbuttons; i++) {
Roman Moravcik84767d02007-05-01 00:39:13 -0400108 struct gpio_keys_button *button = &pdata->buttons[i];
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400109 struct gpio_button_data *bdata = &ddata->data[i];
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500110 int irq;
Roman Moravcik84767d02007-05-01 00:39:13 -0400111 unsigned int type = button->type ?: EV_KEY;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500112
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400113 bdata->input = input;
Uwe Kleine-König74dd4392008-07-30 10:33:43 -0400114 bdata->button = button;
Jani Nikulaca865a72009-06-28 22:38:44 -0700115 setup_timer(&bdata->timer,
116 gpio_check_button, (unsigned long)bdata);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400117
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500118 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
119 if (error < 0) {
120 pr_err("gpio-keys: failed to request GPIO %d,"
121 " error %d\n", button->gpio, error);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400122 goto fail2;
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500123 }
124
125 error = gpio_direction_input(button->gpio);
126 if (error < 0) {
127 pr_err("gpio-keys: failed to configure input"
128 " direction for GPIO %d, error %d\n",
129 button->gpio, error);
130 gpio_free(button->gpio);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400131 goto fail2;
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500132 }
133
134 irq = gpio_to_irq(button->gpio);
Anti Sullin006df302007-09-26 00:01:03 -0400135 if (irq < 0) {
136 error = irq;
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500137 pr_err("gpio-keys: Unable to get irq number"
138 " for GPIO %d, error %d\n",
Anti Sullin006df302007-09-26 00:01:03 -0400139 button->gpio, error);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500140 gpio_free(button->gpio);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400141 goto fail2;
Anti Sullin006df302007-09-26 00:01:03 -0400142 }
143
144 error = request_irq(irq, gpio_keys_isr,
Dmitry Torokhov64e85632009-04-18 18:45:17 -0700145 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
Anti Sullin006df302007-09-26 00:01:03 -0400146 button->desc ? button->desc : "gpio_keys",
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400147 bdata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500148 if (error) {
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500149 pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500150 irq, error);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500151 gpio_free(button->gpio);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400152 goto fail2;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500153 }
Roman Moravcik84767d02007-05-01 00:39:13 -0400154
Anti Sulline15b0212007-09-26 00:01:17 -0400155 if (button->wakeup)
156 wakeup = 1;
157
Roman Moravcik84767d02007-05-01 00:39:13 -0400158 input_set_capability(input, type, button->code);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500159 }
160
161 error = input_register_device(input);
162 if (error) {
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500163 pr_err("gpio-keys: Unable to register input device, "
Anti Sullin006df302007-09-26 00:01:03 -0400164 "error: %d\n", error);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400165 goto fail2;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500166 }
167
Anti Sulline15b0212007-09-26 00:01:17 -0400168 device_init_wakeup(&pdev->dev, wakeup);
169
Phil Blundell78a56aa2007-01-18 00:44:09 -0500170 return 0;
171
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400172 fail2:
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500173 while (--i >= 0) {
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400174 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
Jani Nikulaca865a72009-06-28 22:38:44 -0700175 if (pdata->buttons[i].debounce_interval)
176 del_timer_sync(&ddata->data[i].timer);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500177 gpio_free(pdata->buttons[i].gpio);
178 }
Phil Blundell78a56aa2007-01-18 00:44:09 -0500179
Anti Sullin006df302007-09-26 00:01:03 -0400180 platform_set_drvdata(pdev, NULL);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400181 fail1:
Phil Blundell78a56aa2007-01-18 00:44:09 -0500182 input_free_device(input);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400183 kfree(ddata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500184
185 return error;
186}
187
188static int __devexit gpio_keys_remove(struct platform_device *pdev)
189{
190 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400191 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
192 struct input_dev *input = ddata->input;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500193 int i;
194
Anti Sulline15b0212007-09-26 00:01:17 -0400195 device_init_wakeup(&pdev->dev, 0);
196
Phil Blundell78a56aa2007-01-18 00:44:09 -0500197 for (i = 0; i < pdata->nbuttons; i++) {
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500198 int irq = gpio_to_irq(pdata->buttons[i].gpio);
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400199 free_irq(irq, &ddata->data[i]);
Jani Nikulaca865a72009-06-28 22:38:44 -0700200 if (pdata->buttons[i].debounce_interval)
201 del_timer_sync(&ddata->data[i].timer);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500202 gpio_free(pdata->buttons[i].gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500203 }
204
205 input_unregister_device(input);
206
207 return 0;
208}
209
Anti Sulline15b0212007-09-26 00:01:17 -0400210
211#ifdef CONFIG_PM
212static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
213{
214 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
215 int i;
216
217 if (device_may_wakeup(&pdev->dev)) {
218 for (i = 0; i < pdata->nbuttons; i++) {
219 struct gpio_keys_button *button = &pdata->buttons[i];
220 if (button->wakeup) {
221 int irq = gpio_to_irq(button->gpio);
222 enable_irq_wake(irq);
223 }
224 }
225 }
226
227 return 0;
228}
229
230static int gpio_keys_resume(struct platform_device *pdev)
231{
232 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
233 int i;
234
235 if (device_may_wakeup(&pdev->dev)) {
236 for (i = 0; i < pdata->nbuttons; i++) {
237 struct gpio_keys_button *button = &pdata->buttons[i];
238 if (button->wakeup) {
239 int irq = gpio_to_irq(button->gpio);
240 disable_irq_wake(irq);
241 }
242 }
243 }
244
245 return 0;
246}
247#else
248#define gpio_keys_suspend NULL
249#define gpio_keys_resume NULL
250#endif
251
Uwe Kleine-König9b070442008-07-30 10:34:02 -0400252static struct platform_driver gpio_keys_device_driver = {
Phil Blundell78a56aa2007-01-18 00:44:09 -0500253 .probe = gpio_keys_probe,
254 .remove = __devexit_p(gpio_keys_remove),
Anti Sulline15b0212007-09-26 00:01:17 -0400255 .suspend = gpio_keys_suspend,
256 .resume = gpio_keys_resume,
Phil Blundell78a56aa2007-01-18 00:44:09 -0500257 .driver = {
258 .name = "gpio-keys",
Kay Sieversd7b52472008-04-18 00:24:42 -0400259 .owner = THIS_MODULE,
Phil Blundell78a56aa2007-01-18 00:44:09 -0500260 }
261};
262
263static int __init gpio_keys_init(void)
264{
265 return platform_driver_register(&gpio_keys_device_driver);
266}
267
268static void __exit gpio_keys_exit(void)
269{
270 platform_driver_unregister(&gpio_keys_device_driver);
271}
272
273module_init(gpio_keys_init);
274module_exit(gpio_keys_exit);
275
276MODULE_LICENSE("GPL");
277MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
278MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
Kay Sieversd7b52472008-04-18 00:24:42 -0400279MODULE_ALIAS("platform:gpio-keys");