blob: e2809d29d99d9f9275579d215419ced74b8da0f9 [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
40static void gpio_keys_report_event(struct gpio_keys_button *button,
41 struct input_dev *input)
42{
43 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
50static 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->button, data->input);
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
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -040064 if (button->debounce_interval)
65 mod_timer(&bdata->timer,
66 jiffies + msecs_to_jiffies(button->debounce_interval));
67 else
68 gpio_keys_report_event(button, bdata->input);
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
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400101 ddata->input = input;
102
Phil Blundell78a56aa2007-01-18 00:44:09 -0500103 for (i = 0; i < pdata->nbuttons; i++) {
Roman Moravcik84767d02007-05-01 00:39:13 -0400104 struct gpio_keys_button *button = &pdata->buttons[i];
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400105 struct gpio_button_data *bdata = &ddata->data[i];
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500106 int irq;
Roman Moravcik84767d02007-05-01 00:39:13 -0400107 unsigned int type = button->type ?: EV_KEY;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500108
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400109 bdata->input = input;
110 setup_timer(&bdata->timer,
111 gpio_check_button, (unsigned long)bdata);
112
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500113 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
114 if (error < 0) {
115 pr_err("gpio-keys: failed to request GPIO %d,"
116 " error %d\n", button->gpio, error);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400117 goto fail2;
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500118 }
119
120 error = gpio_direction_input(button->gpio);
121 if (error < 0) {
122 pr_err("gpio-keys: failed to configure input"
123 " direction for GPIO %d, error %d\n",
124 button->gpio, error);
125 gpio_free(button->gpio);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400126 goto fail2;
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500127 }
128
129 irq = gpio_to_irq(button->gpio);
Anti Sullin006df302007-09-26 00:01:03 -0400130 if (irq < 0) {
131 error = irq;
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500132 pr_err("gpio-keys: Unable to get irq number"
133 " for GPIO %d, error %d\n",
Anti Sullin006df302007-09-26 00:01:03 -0400134 button->gpio, error);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500135 gpio_free(button->gpio);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400136 goto fail2;
Anti Sullin006df302007-09-26 00:01:03 -0400137 }
138
139 error = request_irq(irq, gpio_keys_isr,
140 IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
141 IRQF_TRIGGER_FALLING,
142 button->desc ? button->desc : "gpio_keys",
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400143 bdata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500144 if (error) {
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500145 pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500146 irq, error);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500147 gpio_free(button->gpio);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400148 goto fail2;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500149 }
Roman Moravcik84767d02007-05-01 00:39:13 -0400150
Anti Sulline15b0212007-09-26 00:01:17 -0400151 if (button->wakeup)
152 wakeup = 1;
153
Roman Moravcik84767d02007-05-01 00:39:13 -0400154 input_set_capability(input, type, button->code);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500155 }
156
157 error = input_register_device(input);
158 if (error) {
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500159 pr_err("gpio-keys: Unable to register input device, "
Anti Sullin006df302007-09-26 00:01:03 -0400160 "error: %d\n", error);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400161 goto fail2;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500162 }
163
Anti Sulline15b0212007-09-26 00:01:17 -0400164 device_init_wakeup(&pdev->dev, wakeup);
165
Phil Blundell78a56aa2007-01-18 00:44:09 -0500166 return 0;
167
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400168 fail2:
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500169 while (--i >= 0) {
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400170 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400171 if (pdata->buttons[i].debounce_interval)
172 del_timer_sync(&ddata->data[i].timer);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500173 gpio_free(pdata->buttons[i].gpio);
174 }
Phil Blundell78a56aa2007-01-18 00:44:09 -0500175
Anti Sullin006df302007-09-26 00:01:03 -0400176 platform_set_drvdata(pdev, NULL);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400177 fail1:
Phil Blundell78a56aa2007-01-18 00:44:09 -0500178 input_free_device(input);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400179 kfree(ddata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500180
181 return error;
182}
183
184static int __devexit gpio_keys_remove(struct platform_device *pdev)
185{
186 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400187 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
188 struct input_dev *input = ddata->input;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500189 int i;
190
Anti Sulline15b0212007-09-26 00:01:17 -0400191 device_init_wakeup(&pdev->dev, 0);
192
Phil Blundell78a56aa2007-01-18 00:44:09 -0500193 for (i = 0; i < pdata->nbuttons; i++) {
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500194 int irq = gpio_to_irq(pdata->buttons[i].gpio);
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400195 free_irq(irq, &ddata->data[i]);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400196 if (pdata->buttons[i].debounce_interval)
197 del_timer_sync(&ddata->data[i].timer);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500198 gpio_free(pdata->buttons[i].gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500199 }
200
201 input_unregister_device(input);
202
203 return 0;
204}
205
Anti Sulline15b0212007-09-26 00:01:17 -0400206
207#ifdef CONFIG_PM
208static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
209{
210 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
211 int i;
212
213 if (device_may_wakeup(&pdev->dev)) {
214 for (i = 0; i < pdata->nbuttons; i++) {
215 struct gpio_keys_button *button = &pdata->buttons[i];
216 if (button->wakeup) {
217 int irq = gpio_to_irq(button->gpio);
218 enable_irq_wake(irq);
219 }
220 }
221 }
222
223 return 0;
224}
225
226static int gpio_keys_resume(struct platform_device *pdev)
227{
228 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
229 int i;
230
231 if (device_may_wakeup(&pdev->dev)) {
232 for (i = 0; i < pdata->nbuttons; i++) {
233 struct gpio_keys_button *button = &pdata->buttons[i];
234 if (button->wakeup) {
235 int irq = gpio_to_irq(button->gpio);
236 disable_irq_wake(irq);
237 }
238 }
239 }
240
241 return 0;
242}
243#else
244#define gpio_keys_suspend NULL
245#define gpio_keys_resume NULL
246#endif
247
Phil Blundell78a56aa2007-01-18 00:44:09 -0500248struct platform_driver gpio_keys_device_driver = {
249 .probe = gpio_keys_probe,
250 .remove = __devexit_p(gpio_keys_remove),
Anti Sulline15b0212007-09-26 00:01:17 -0400251 .suspend = gpio_keys_suspend,
252 .resume = gpio_keys_resume,
Phil Blundell78a56aa2007-01-18 00:44:09 -0500253 .driver = {
254 .name = "gpio-keys",
Kay Sieversd7b52472008-04-18 00:24:42 -0400255 .owner = THIS_MODULE,
Phil Blundell78a56aa2007-01-18 00:44:09 -0500256 }
257};
258
259static int __init gpio_keys_init(void)
260{
261 return platform_driver_register(&gpio_keys_device_driver);
262}
263
264static void __exit gpio_keys_exit(void)
265{
266 platform_driver_unregister(&gpio_keys_device_driver);
267}
268
269module_init(gpio_keys_init);
270module_exit(gpio_keys_exit);
271
272MODULE_LICENSE("GPL");
273MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
274MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
Kay Sieversd7b52472008-04-18 00:24:42 -0400275MODULE_ALIAS("platform:gpio-keys");