]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/input/keyboard/gpio_keys.c
Input: gpio-keys - revert 'change timer to workqueue'
[mirror_ubuntu-zesty-kernel.git] / drivers / input / keyboard / gpio_keys.c
CommitLineData
78a56aab
PB
1/*
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>
78a56aab
PB
12
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>
49015bee 24#include <linux/gpio_keys.h>
78a56aab 25
0d98f6bb 26#include <asm/gpio.h>
78a56aab 27
a33466e3
DES
28struct gpio_button_data {
29 struct gpio_keys_button *button;
30 struct input_dev *input;
ca865a77 31 struct timer_list timer;
a33466e3
DES
32};
33
34struct gpio_keys_drvdata {
35 struct input_dev *input;
36 struct gpio_button_data data[0];
37};
38
ca865a77 39static void gpio_keys_report_event(struct gpio_button_data *bdata)
a33466e3 40{
ce25d7e9
UKK
41 struct gpio_keys_button *button = bdata->button;
42 struct input_dev *input = bdata->input;
a33466e3
DES
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
ca865a77
JN
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);
55}
56
78a56aab
PB
57static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
58{
57ffe9d5
UKK
59 struct gpio_button_data *bdata = dev_id;
60 struct gpio_keys_button *button = bdata->button;
78a56aab 61
57ffe9d5 62 BUG_ON(irq != gpio_to_irq(button->gpio));
84767d00 63
ca865a77
JN
64 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);
78a56aab 69
57ffe9d5 70 return IRQ_HANDLED;
78a56aab
PB
71}
72
73static int __devinit gpio_keys_probe(struct platform_device *pdev)
74{
75 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
a33466e3 76 struct gpio_keys_drvdata *ddata;
78a56aab
PB
77 struct input_dev *input;
78 int i, error;
e15b0213 79 int wakeup = 0;
78a56aab 80
a33466e3
DES
81 ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
82 pdata->nbuttons * sizeof(struct gpio_button_data),
83 GFP_KERNEL);
78a56aab 84 input = input_allocate_device();
a33466e3
DES
85 if (!ddata || !input) {
86 error = -ENOMEM;
87 goto fail1;
88 }
78a56aab 89
a33466e3 90 platform_set_drvdata(pdev, ddata);
78a56aab
PB
91
92 input->name = pdev->name;
93 input->phys = "gpio-keys/input0";
469ba4df 94 input->dev.parent = &pdev->dev;
78a56aab
PB
95
96 input->id.bustype = BUS_HOST;
97 input->id.vendor = 0x0001;
98 input->id.product = 0x0001;
99 input->id.version = 0x0100;
100
b67b4b11
DC
101 /* Enable auto repeat feature of Linux input subsystem */
102 if (pdata->rep)
103 __set_bit(EV_REP, input->evbit);
104
a33466e3
DES
105 ddata->input = input;
106
78a56aab 107 for (i = 0; i < pdata->nbuttons; i++) {
84767d00 108 struct gpio_keys_button *button = &pdata->buttons[i];
a33466e3 109 struct gpio_button_data *bdata = &ddata->data[i];
6a2e3911 110 int irq;
84767d00 111 unsigned int type = button->type ?: EV_KEY;
78a56aab 112
a33466e3 113 bdata->input = input;
74dd4393 114 bdata->button = button;
ca865a77
JN
115 setup_timer(&bdata->timer,
116 gpio_check_button, (unsigned long)bdata);
a33466e3 117
6a2e3911
HVR
118 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);
a33466e3 122 goto fail2;
6a2e3911
HVR
123 }
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);
a33466e3 131 goto fail2;
6a2e3911
HVR
132 }
133
134 irq = gpio_to_irq(button->gpio);
006df302
AS
135 if (irq < 0) {
136 error = irq;
6a2e3911
HVR
137 pr_err("gpio-keys: Unable to get irq number"
138 " for GPIO %d, error %d\n",
006df302 139 button->gpio, error);
6a2e3911 140 gpio_free(button->gpio);
a33466e3 141 goto fail2;
006df302
AS
142 }
143
144 error = request_irq(irq, gpio_keys_isr,
64e8563c 145 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
006df302 146 button->desc ? button->desc : "gpio_keys",
57ffe9d5 147 bdata);
78a56aab 148 if (error) {
6a2e3911 149 pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
0d98f6bb 150 irq, error);
6a2e3911 151 gpio_free(button->gpio);
a33466e3 152 goto fail2;
78a56aab 153 }
84767d00 154
e15b0213
AS
155 if (button->wakeup)
156 wakeup = 1;
157
84767d00 158 input_set_capability(input, type, button->code);
78a56aab
PB
159 }
160
161 error = input_register_device(input);
162 if (error) {
6a2e3911 163 pr_err("gpio-keys: Unable to register input device, "
006df302 164 "error: %d\n", error);
a33466e3 165 goto fail2;
78a56aab
PB
166 }
167
e15b0213
AS
168 device_init_wakeup(&pdev->dev, wakeup);
169
78a56aab
PB
170 return 0;
171
a33466e3 172 fail2:
6a2e3911 173 while (--i >= 0) {
57ffe9d5 174 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
ca865a77
JN
175 if (pdata->buttons[i].debounce_interval)
176 del_timer_sync(&ddata->data[i].timer);
6a2e3911
HVR
177 gpio_free(pdata->buttons[i].gpio);
178 }
78a56aab 179
006df302 180 platform_set_drvdata(pdev, NULL);
a33466e3 181 fail1:
78a56aab 182 input_free_device(input);
a33466e3 183 kfree(ddata);
78a56aab
PB
184
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;
a33466e3
DES
191 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
192 struct input_dev *input = ddata->input;
78a56aab
PB
193 int i;
194
e15b0213
AS
195 device_init_wakeup(&pdev->dev, 0);
196
78a56aab 197 for (i = 0; i < pdata->nbuttons; i++) {
0d98f6bb 198 int irq = gpio_to_irq(pdata->buttons[i].gpio);
57ffe9d5 199 free_irq(irq, &ddata->data[i]);
ca865a77
JN
200 if (pdata->buttons[i].debounce_interval)
201 del_timer_sync(&ddata->data[i].timer);
6a2e3911 202 gpio_free(pdata->buttons[i].gpio);
78a56aab
PB
203 }
204
205 input_unregister_device(input);
206
207 return 0;
208}
209
e15b0213
AS
210
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
9b07044c 252static struct platform_driver gpio_keys_device_driver = {
78a56aab
PB
253 .probe = gpio_keys_probe,
254 .remove = __devexit_p(gpio_keys_remove),
e15b0213
AS
255 .suspend = gpio_keys_suspend,
256 .resume = gpio_keys_resume,
78a56aab
PB
257 .driver = {
258 .name = "gpio-keys",
d7b5247b 259 .owner = THIS_MODULE,
78a56aab
PB
260 }
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");
d7b5247b 279MODULE_ALIAS("platform:gpio-keys");