Driver Core: Make PM operations a const pointer
[deliverable/linux.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>
78a56aab 26
0d98f6bb 27#include <asm/gpio.h>
78a56aab 28
a33466e3
DB
29struct gpio_button_data {
30 struct gpio_keys_button *button;
31 struct input_dev *input;
ca865a77 32 struct timer_list timer;
da0d03fe 33 struct work_struct work;
a33466e3
DB
34};
35
36struct gpio_keys_drvdata {
37 struct input_dev *input;
38 struct gpio_button_data data[0];
39};
40
da0d03fe 41static void gpio_keys_report_event(struct work_struct *work)
a33466e3 42{
da0d03fe
JN
43 struct gpio_button_data *bdata =
44 container_of(work, struct gpio_button_data, work);
ce25d7e9
UKK
45 struct gpio_keys_button *button = bdata->button;
46 struct input_dev *input = bdata->input;
a33466e3
DB
47 unsigned int type = button->type ?: EV_KEY;
48 int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
49
50 input_event(input, type, button->code, !!state);
51 input_sync(input);
52}
53
da0d03fe 54static void gpio_keys_timer(unsigned long _data)
ca865a77
JN
55{
56 struct gpio_button_data *data = (struct gpio_button_data *)_data;
57
da0d03fe 58 schedule_work(&data->work);
ca865a77
JN
59}
60
78a56aab
PB
61static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
62{
57ffe9d5
UKK
63 struct gpio_button_data *bdata = dev_id;
64 struct gpio_keys_button *button = bdata->button;
78a56aab 65
57ffe9d5 66 BUG_ON(irq != gpio_to_irq(button->gpio));
84767d00 67
ca865a77
JN
68 if (button->debounce_interval)
69 mod_timer(&bdata->timer,
70 jiffies + msecs_to_jiffies(button->debounce_interval));
71 else
da0d03fe 72 schedule_work(&bdata->work);
78a56aab 73
57ffe9d5 74 return IRQ_HANDLED;
78a56aab
PB
75}
76
77static int __devinit gpio_keys_probe(struct platform_device *pdev)
78{
79 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
a33466e3 80 struct gpio_keys_drvdata *ddata;
78a56aab
PB
81 struct input_dev *input;
82 int i, error;
e15b0213 83 int wakeup = 0;
78a56aab 84
a33466e3
DB
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
DB
89 if (!ddata || !input) {
90 error = -ENOMEM;
91 goto fail1;
92 }
78a56aab 93
a33466e3 94 platform_set_drvdata(pdev, ddata);
78a56aab
PB
95
96 input->name = pdev->name;
97 input->phys = "gpio-keys/input0";
469ba4df 98 input->dev.parent = &pdev->dev;
78a56aab
PB
99
100 input->id.bustype = BUS_HOST;
101 input->id.vendor = 0x0001;
102 input->id.product = 0x0001;
103 input->id.version = 0x0100;
104
b67b4b11
DC
105 /* Enable auto repeat feature of Linux input subsystem */
106 if (pdata->rep)
107 __set_bit(EV_REP, input->evbit);
108
a33466e3
DB
109 ddata->input = input;
110
78a56aab 111 for (i = 0; i < pdata->nbuttons; i++) {
84767d00 112 struct gpio_keys_button *button = &pdata->buttons[i];
a33466e3 113 struct gpio_button_data *bdata = &ddata->data[i];
6a2e3911 114 int irq;
84767d00 115 unsigned int type = button->type ?: EV_KEY;
78a56aab 116
a33466e3 117 bdata->input = input;
74dd4393 118 bdata->button = button;
ca865a77 119 setup_timer(&bdata->timer,
da0d03fe
JN
120 gpio_keys_timer, (unsigned long)bdata);
121 INIT_WORK(&bdata->work, gpio_keys_report_event);
a33466e3 122
6a2e3911
HVR
123 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
124 if (error < 0) {
125 pr_err("gpio-keys: failed to request GPIO %d,"
126 " error %d\n", button->gpio, error);
a33466e3 127 goto fail2;
6a2e3911
HVR
128 }
129
130 error = gpio_direction_input(button->gpio);
131 if (error < 0) {
132 pr_err("gpio-keys: failed to configure input"
133 " direction for GPIO %d, error %d\n",
134 button->gpio, error);
135 gpio_free(button->gpio);
a33466e3 136 goto fail2;
6a2e3911
HVR
137 }
138
139 irq = gpio_to_irq(button->gpio);
006df302
AS
140 if (irq < 0) {
141 error = irq;
6a2e3911
HVR
142 pr_err("gpio-keys: Unable to get irq number"
143 " for GPIO %d, error %d\n",
006df302 144 button->gpio, error);
6a2e3911 145 gpio_free(button->gpio);
a33466e3 146 goto fail2;
006df302
AS
147 }
148
149 error = request_irq(irq, gpio_keys_isr,
64e8563c 150 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
006df302 151 button->desc ? button->desc : "gpio_keys",
57ffe9d5 152 bdata);
78a56aab 153 if (error) {
6a2e3911 154 pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
0d98f6bb 155 irq, error);
6a2e3911 156 gpio_free(button->gpio);
a33466e3 157 goto fail2;
78a56aab 158 }
84767d00 159
e15b0213
AS
160 if (button->wakeup)
161 wakeup = 1;
162
84767d00 163 input_set_capability(input, type, button->code);
78a56aab
PB
164 }
165
166 error = input_register_device(input);
167 if (error) {
6a2e3911 168 pr_err("gpio-keys: Unable to register input device, "
006df302 169 "error: %d\n", error);
a33466e3 170 goto fail2;
78a56aab
PB
171 }
172
e15b0213
AS
173 device_init_wakeup(&pdev->dev, wakeup);
174
78a56aab
PB
175 return 0;
176
a33466e3 177 fail2:
6a2e3911 178 while (--i >= 0) {
57ffe9d5 179 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
ca865a77
JN
180 if (pdata->buttons[i].debounce_interval)
181 del_timer_sync(&ddata->data[i].timer);
da0d03fe 182 cancel_work_sync(&ddata->data[i].work);
6a2e3911
HVR
183 gpio_free(pdata->buttons[i].gpio);
184 }
78a56aab 185
006df302 186 platform_set_drvdata(pdev, NULL);
a33466e3 187 fail1:
78a56aab 188 input_free_device(input);
a33466e3 189 kfree(ddata);
78a56aab
PB
190
191 return error;
192}
193
194static int __devexit gpio_keys_remove(struct platform_device *pdev)
195{
196 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
a33466e3
DB
197 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
198 struct input_dev *input = ddata->input;
78a56aab
PB
199 int i;
200
e15b0213
AS
201 device_init_wakeup(&pdev->dev, 0);
202
78a56aab 203 for (i = 0; i < pdata->nbuttons; i++) {
0d98f6bb 204 int irq = gpio_to_irq(pdata->buttons[i].gpio);
57ffe9d5 205 free_irq(irq, &ddata->data[i]);
ca865a77
JN
206 if (pdata->buttons[i].debounce_interval)
207 del_timer_sync(&ddata->data[i].timer);
da0d03fe 208 cancel_work_sync(&ddata->data[i].work);
6a2e3911 209 gpio_free(pdata->buttons[i].gpio);
78a56aab
PB
210 }
211
212 input_unregister_device(input);
213
214 return 0;
215}
216
e15b0213
AS
217
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
9b07044c 259static struct platform_driver gpio_keys_device_driver = {
78a56aab
PB
260 .probe = gpio_keys_probe,
261 .remove = __devexit_p(gpio_keys_remove),
e15b0213
AS
262 .suspend = gpio_keys_suspend,
263 .resume = gpio_keys_resume,
78a56aab
PB
264 .driver = {
265 .name = "gpio-keys",
d7b5247b 266 .owner = THIS_MODULE,
78a56aab
PB
267 }
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");
d7b5247b 286MODULE_ALIAS("platform:gpio-keys");
This page took 0.172058 seconds and 5 git commands to generate.