blob: ec96b369dd7ac02ed3bb49dec3bcfe8e9c649ae1 [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;
31 struct timer_list timer;
32};
33
34struct gpio_keys_drvdata {
35 struct input_dev *input;
36 struct gpio_button_data data[0];
37};
38
39static void gpio_keys_report_event(struct gpio_keys_button *button,
40 struct input_dev *input)
41{
42 unsigned int type = button->type ?: EV_KEY;
43 int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
44
45 input_event(input, type, button->code, !!state);
46 input_sync(input);
47}
48
49static void gpio_check_button(unsigned long _data)
50{
51 struct gpio_button_data *data = (struct gpio_button_data *)_data;
52
53 gpio_keys_report_event(data->button, data->input);
54}
55
Phil Blundell78a56aa2007-01-18 00:44:09 -050056static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
57{
Phil Blundell78a56aa2007-01-18 00:44:09 -050058 struct platform_device *pdev = dev_id;
59 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040060 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
61 int i;
Phil Blundell78a56aa2007-01-18 00:44:09 -050062
63 for (i = 0; i < pdata->nbuttons; i++) {
Roman Moravcik84767d02007-05-01 00:39:13 -040064 struct gpio_keys_button *button = &pdata->buttons[i];
Phil Blundell78a56aa2007-01-18 00:44:09 -050065
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040066 if (irq == gpio_to_irq(button->gpio)) {
67 struct gpio_button_data *bdata = &ddata->data[i];
Roman Moravcik84767d02007-05-01 00:39:13 -040068
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040069 if (button->debounce_interval)
70 mod_timer(&bdata->timer,
71 jiffies +
72 msecs_to_jiffies(button->debounce_interval));
73 else
74 gpio_keys_report_event(button, bdata->input);
75
David Brownell1164ec12008-04-15 01:31:13 -040076 return IRQ_HANDLED;
Phil Blundell78a56aa2007-01-18 00:44:09 -050077 }
78 }
79
David Brownell1164ec12008-04-15 01:31:13 -040080 return IRQ_NONE;
Phil Blundell78a56aa2007-01-18 00:44:09 -050081}
82
83static int __devinit gpio_keys_probe(struct platform_device *pdev)
84{
85 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040086 struct gpio_keys_drvdata *ddata;
Phil Blundell78a56aa2007-01-18 00:44:09 -050087 struct input_dev *input;
88 int i, error;
Anti Sulline15b0212007-09-26 00:01:17 -040089 int wakeup = 0;
Phil Blundell78a56aa2007-01-18 00:44:09 -050090
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040091 ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
92 pdata->nbuttons * sizeof(struct gpio_button_data),
93 GFP_KERNEL);
Phil Blundell78a56aa2007-01-18 00:44:09 -050094 input = input_allocate_device();
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040095 if (!ddata || !input) {
96 error = -ENOMEM;
97 goto fail1;
98 }
Phil Blundell78a56aa2007-01-18 00:44:09 -050099
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400100 platform_set_drvdata(pdev, ddata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500101
102 input->name = pdev->name;
103 input->phys = "gpio-keys/input0";
Dmitry Torokhov469ba4d2007-04-12 01:34:58 -0400104 input->dev.parent = &pdev->dev;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500105
106 input->id.bustype = BUS_HOST;
107 input->id.vendor = 0x0001;
108 input->id.product = 0x0001;
109 input->id.version = 0x0100;
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;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400121 setup_timer(&bdata->timer,
122 gpio_check_button, (unsigned long)bdata);
123
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500124 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
125 if (error < 0) {
126 pr_err("gpio-keys: failed to request GPIO %d,"
127 " error %d\n", 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) {
133 pr_err("gpio-keys: failed to configure input"
134 " 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;
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500143 pr_err("gpio-keys: 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,
151 IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
152 IRQF_TRIGGER_FALLING,
153 button->desc ? button->desc : "gpio_keys",
154 pdev);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500155 if (error) {
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500156 pr_err("gpio-keys: 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) {
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500170 pr_err("gpio-keys: 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) {
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500181 free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400182 if (pdata->buttons[i].debounce_interval)
183 del_timer_sync(&ddata->data[i].timer);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500184 gpio_free(pdata->buttons[i].gpio);
185 }
Phil Blundell78a56aa2007-01-18 00:44:09 -0500186
Anti Sullin006df302007-09-26 00:01:03 -0400187 platform_set_drvdata(pdev, NULL);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400188 fail1:
Phil Blundell78a56aa2007-01-18 00:44:09 -0500189 input_free_device(input);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400190 kfree(ddata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500191
192 return error;
193}
194
195static int __devexit gpio_keys_remove(struct platform_device *pdev)
196{
197 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400198 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
199 struct input_dev *input = ddata->input;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500200 int i;
201
Anti Sulline15b0212007-09-26 00:01:17 -0400202 device_init_wakeup(&pdev->dev, 0);
203
Phil Blundell78a56aa2007-01-18 00:44:09 -0500204 for (i = 0; i < pdata->nbuttons; i++) {
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500205 int irq = gpio_to_irq(pdata->buttons[i].gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500206 free_irq(irq, pdev);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400207 if (pdata->buttons[i].debounce_interval)
208 del_timer_sync(&ddata->data[i].timer);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500209 gpio_free(pdata->buttons[i].gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500210 }
211
212 input_unregister_device(input);
213
214 return 0;
215}
216
Anti Sulline15b0212007-09-26 00:01:17 -0400217
218#ifdef CONFIG_PM
219static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
220{
221 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
222 int i;
223
224 if (device_may_wakeup(&pdev->dev)) {
225 for (i = 0; i < pdata->nbuttons; i++) {
226 struct gpio_keys_button *button = &pdata->buttons[i];
227 if (button->wakeup) {
228 int irq = gpio_to_irq(button->gpio);
229 enable_irq_wake(irq);
230 }
231 }
232 }
233
234 return 0;
235}
236
237static int gpio_keys_resume(struct platform_device *pdev)
238{
239 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
240 int i;
241
242 if (device_may_wakeup(&pdev->dev)) {
243 for (i = 0; i < pdata->nbuttons; i++) {
244 struct gpio_keys_button *button = &pdata->buttons[i];
245 if (button->wakeup) {
246 int irq = gpio_to_irq(button->gpio);
247 disable_irq_wake(irq);
248 }
249 }
250 }
251
252 return 0;
253}
254#else
255#define gpio_keys_suspend NULL
256#define gpio_keys_resume NULL
257#endif
258
Uwe Kleine-König9b070442008-07-30 10:34:02 -0400259static struct platform_driver gpio_keys_device_driver = {
Phil Blundell78a56aa2007-01-18 00:44:09 -0500260 .probe = gpio_keys_probe,
261 .remove = __devexit_p(gpio_keys_remove),
Anti Sulline15b0212007-09-26 00:01:17 -0400262 .suspend = gpio_keys_suspend,
263 .resume = gpio_keys_resume,
Phil Blundell78a56aa2007-01-18 00:44:09 -0500264 .driver = {
265 .name = "gpio-keys",
Kay Sieversd7b52472008-04-18 00:24:42 -0400266 .owner = THIS_MODULE,
Phil Blundell78a56aa2007-01-18 00:44:09 -0500267 }
268};
269
270static int __init gpio_keys_init(void)
271{
272 return platform_driver_register(&gpio_keys_device_driver);
273}
274
275static void __exit gpio_keys_exit(void)
276{
277 platform_driver_unregister(&gpio_keys_device_driver);
278}
279
280module_init(gpio_keys_init);
281module_exit(gpio_keys_exit);
282
283MODULE_LICENSE("GPL");
284MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
285MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
Kay Sieversd7b52472008-04-18 00:24:42 -0400286MODULE_ALIAS("platform:gpio-keys");