]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/input/keyboard/gpio_keys.c
Input: gpio_keys - use <linux/gpio.h> instead of <asm/gpio.h>
[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>
da0d03fe 25#include <linux/workqueue.h>
111bc59c 26#include <linux/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;
da0d03fe 32 struct work_struct work;
a33466e3
DES
33};
34
35struct gpio_keys_drvdata {
36 struct input_dev *input;
37 struct gpio_button_data data[0];
38};
39
da0d03fe 40static void gpio_keys_report_event(struct work_struct *work)
a33466e3 41{
da0d03fe
JN
42 struct gpio_button_data *bdata =
43 container_of(work, struct gpio_button_data, work);
ce25d7e9
UKK
44 struct gpio_keys_button *button = bdata->button;
45 struct input_dev *input = bdata->input;
a33466e3
DES
46 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
da0d03fe 53static void gpio_keys_timer(unsigned long _data)
ca865a77
JN
54{
55 struct gpio_button_data *data = (struct gpio_button_data *)_data;
56
da0d03fe 57 schedule_work(&data->work);
ca865a77
JN
58}
59
78a56aab
PB
60static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
61{
57ffe9d5
UKK
62 struct gpio_button_data *bdata = dev_id;
63 struct gpio_keys_button *button = bdata->button;
78a56aab 64
57ffe9d5 65 BUG_ON(irq != gpio_to_irq(button->gpio));
84767d00 66
ca865a77
JN
67 if (button->debounce_interval)
68 mod_timer(&bdata->timer,
69 jiffies + msecs_to_jiffies(button->debounce_interval));
70 else
da0d03fe 71 schedule_work(&bdata->work);
78a56aab 72
57ffe9d5 73 return IRQ_HANDLED;
78a56aab
PB
74}
75
76static int __devinit gpio_keys_probe(struct platform_device *pdev)
77{
78 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
a33466e3 79 struct gpio_keys_drvdata *ddata;
db19fd8b 80 struct device *dev = &pdev->dev;
78a56aab
PB
81 struct input_dev *input;
82 int i, error;
e15b0213 83 int wakeup = 0;
78a56aab 84
a33466e3
DES
85 ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
86 pdata->nbuttons * sizeof(struct gpio_button_data),
87 GFP_KERNEL);
78a56aab 88 input = input_allocate_device();
a33466e3 89 if (!ddata || !input) {
db19fd8b 90 dev_err(dev, "failed to allocate state\n");
a33466e3
DES
91 error = -ENOMEM;
92 goto fail1;
93 }
78a56aab 94
a33466e3 95 platform_set_drvdata(pdev, ddata);
78a56aab
PB
96
97 input->name = pdev->name;
98 input->phys = "gpio-keys/input0";
469ba4df 99 input->dev.parent = &pdev->dev;
78a56aab
PB
100
101 input->id.bustype = BUS_HOST;
102 input->id.vendor = 0x0001;
103 input->id.product = 0x0001;
104 input->id.version = 0x0100;
105
b67b4b11
DC
106 /* Enable auto repeat feature of Linux input subsystem */
107 if (pdata->rep)
108 __set_bit(EV_REP, input->evbit);
109
a33466e3
DES
110 ddata->input = input;
111
78a56aab 112 for (i = 0; i < pdata->nbuttons; i++) {
84767d00 113 struct gpio_keys_button *button = &pdata->buttons[i];
a33466e3 114 struct gpio_button_data *bdata = &ddata->data[i];
6a2e3911 115 int irq;
84767d00 116 unsigned int type = button->type ?: EV_KEY;
78a56aab 117
a33466e3 118 bdata->input = input;
74dd4393 119 bdata->button = button;
ca865a77 120 setup_timer(&bdata->timer,
da0d03fe
JN
121 gpio_keys_timer, (unsigned long)bdata);
122 INIT_WORK(&bdata->work, gpio_keys_report_event);
a33466e3 123
6a2e3911
HVR
124 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
125 if (error < 0) {
db19fd8b
BD
126 dev_err(dev, "failed to request GPIO %d, error %d\n",
127 button->gpio, error);
a33466e3 128 goto fail2;
6a2e3911
HVR
129 }
130
131 error = gpio_direction_input(button->gpio);
132 if (error < 0) {
db19fd8b 133 dev_err(dev, "failed to configure"
6a2e3911
HVR
134 " direction for GPIO %d, error %d\n",
135 button->gpio, error);
136 gpio_free(button->gpio);
a33466e3 137 goto fail2;
6a2e3911
HVR
138 }
139
140 irq = gpio_to_irq(button->gpio);
006df302
AS
141 if (irq < 0) {
142 error = irq;
db19fd8b
BD
143 dev_err(dev, "Unable to get irq number "
144 "for GPIO %d, error %d\n",
006df302 145 button->gpio, error);
6a2e3911 146 gpio_free(button->gpio);
a33466e3 147 goto fail2;
006df302
AS
148 }
149
150 error = request_irq(irq, gpio_keys_isr,
558a5e29 151 IRQF_SHARED |
64e8563c 152 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
006df302 153 button->desc ? button->desc : "gpio_keys",
57ffe9d5 154 bdata);
78a56aab 155 if (error) {
db19fd8b 156 dev_err(dev, "Unable to claim irq %d; error %d\n",
0d98f6bb 157 irq, error);
6a2e3911 158 gpio_free(button->gpio);
a33466e3 159 goto fail2;
78a56aab 160 }
84767d00 161
e15b0213
AS
162 if (button->wakeup)
163 wakeup = 1;
164
84767d00 165 input_set_capability(input, type, button->code);
78a56aab
PB
166 }
167
168 error = input_register_device(input);
169 if (error) {
db19fd8b 170 dev_err(dev, "Unable to register input device, "
006df302 171 "error: %d\n", error);
a33466e3 172 goto fail2;
78a56aab
PB
173 }
174
e15b0213
AS
175 device_init_wakeup(&pdev->dev, wakeup);
176
78a56aab
PB
177 return 0;
178
a33466e3 179 fail2:
6a2e3911 180 while (--i >= 0) {
57ffe9d5 181 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
ca865a77
JN
182 if (pdata->buttons[i].debounce_interval)
183 del_timer_sync(&ddata->data[i].timer);
da0d03fe 184 cancel_work_sync(&ddata->data[i].work);
6a2e3911
HVR
185 gpio_free(pdata->buttons[i].gpio);
186 }
78a56aab 187
006df302 188 platform_set_drvdata(pdev, NULL);
a33466e3 189 fail1:
78a56aab 190 input_free_device(input);
a33466e3 191 kfree(ddata);
78a56aab
PB
192
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;
a33466e3
DES
199 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
200 struct input_dev *input = ddata->input;
78a56aab
PB
201 int i;
202
e15b0213
AS
203 device_init_wakeup(&pdev->dev, 0);
204
78a56aab 205 for (i = 0; i < pdata->nbuttons; i++) {
0d98f6bb 206 int irq = gpio_to_irq(pdata->buttons[i].gpio);
57ffe9d5 207 free_irq(irq, &ddata->data[i]);
ca865a77
JN
208 if (pdata->buttons[i].debounce_interval)
209 del_timer_sync(&ddata->data[i].timer);
da0d03fe 210 cancel_work_sync(&ddata->data[i].work);
6a2e3911 211 gpio_free(pdata->buttons[i].gpio);
78a56aab
PB
212 }
213
214 input_unregister_device(input);
215
216 return 0;
217}
218
e15b0213
AS
219
220#ifdef CONFIG_PM
ae78e0e0 221static int gpio_keys_suspend(struct device *dev)
e15b0213 222{
ae78e0e0 223 struct platform_device *pdev = to_platform_device(dev);
e15b0213
AS
224 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
ae78e0e0 240static int gpio_keys_resume(struct device *dev)
e15b0213 241{
ae78e0e0 242 struct platform_device *pdev = to_platform_device(dev);
e15b0213
AS
243 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}
ae78e0e0
MR
258
259static const struct dev_pm_ops gpio_keys_pm_ops = {
260 .suspend = gpio_keys_suspend,
261 .resume = gpio_keys_resume,
262};
e15b0213
AS
263#endif
264
9b07044c 265static struct platform_driver gpio_keys_device_driver = {
78a56aab
PB
266 .probe = gpio_keys_probe,
267 .remove = __devexit_p(gpio_keys_remove),
268 .driver = {
269 .name = "gpio-keys",
d7b5247b 270 .owner = THIS_MODULE,
ae78e0e0
MR
271#ifdef CONFIG_PM
272 .pm = &gpio_keys_pm_ops,
273#endif
78a56aab
PB
274 }
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");
d7b5247b 293MODULE_ALIAS("platform:gpio-keys");