blob: 1aff3b76effdb0b05ee4201cba2525cce5b44769 [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>
Jani Nikulada0d03f2009-06-28 22:38:56 -070025#include <linux/workqueue.h>
Ben Dooks111bc592009-11-10 21:01:31 -080026#include <linux/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;
Jani Nikulaca865a72009-06-28 22:38:44 -070031 struct timer_list timer;
Jani Nikulada0d03f2009-06-28 22:38:56 -070032 struct work_struct work;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040033};
34
35struct gpio_keys_drvdata {
36 struct input_dev *input;
37 struct gpio_button_data data[0];
38};
39
Daniel Mack6ee88d72009-11-30 00:04:02 -080040static void gpio_keys_report_event(struct gpio_button_data *bdata)
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040041{
Uwe Kleine-Königce25d7e2008-08-08 12:14:36 -040042 struct gpio_keys_button *button = bdata->button;
43 struct input_dev *input = bdata->input;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040044 unsigned int type = button->type ?: EV_KEY;
45 int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
46
47 input_event(input, type, button->code, !!state);
48 input_sync(input);
49}
50
Daniel Mack6ee88d72009-11-30 00:04:02 -080051static void gpio_keys_work_func(struct work_struct *work)
52{
53 struct gpio_button_data *bdata =
54 container_of(work, struct gpio_button_data, work);
55
56 gpio_keys_report_event(bdata);
57}
58
Jani Nikulada0d03f2009-06-28 22:38:56 -070059static void gpio_keys_timer(unsigned long _data)
Jani Nikulaca865a72009-06-28 22:38:44 -070060{
61 struct gpio_button_data *data = (struct gpio_button_data *)_data;
62
Jani Nikulada0d03f2009-06-28 22:38:56 -070063 schedule_work(&data->work);
Jani Nikulaca865a72009-06-28 22:38:44 -070064}
65
Phil Blundell78a56aa2007-01-18 00:44:09 -050066static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
67{
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -040068 struct gpio_button_data *bdata = dev_id;
69 struct gpio_keys_button *button = bdata->button;
Phil Blundell78a56aa2007-01-18 00:44:09 -050070
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -040071 BUG_ON(irq != gpio_to_irq(button->gpio));
Phil Blundell78a56aa2007-01-18 00:44:09 -050072
Jani Nikulaca865a72009-06-28 22:38:44 -070073 if (button->debounce_interval)
74 mod_timer(&bdata->timer,
75 jiffies + msecs_to_jiffies(button->debounce_interval));
76 else
Jani Nikulada0d03f2009-06-28 22:38:56 -070077 schedule_work(&bdata->work);
Roman Moravcik84767d02007-05-01 00:39:13 -040078
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -040079 return IRQ_HANDLED;
Phil Blundell78a56aa2007-01-18 00:44:09 -050080}
81
Ben Dooksbc8f1ea2009-11-10 21:01:31 -080082static int __devinit gpio_keys_setup_key(struct device *dev,
83 struct gpio_button_data *bdata,
84 struct gpio_keys_button *button)
85{
86 char *desc = button->desc ? button->desc : "gpio_keys";
87 int irq, error;
88
89 setup_timer(&bdata->timer, gpio_keys_timer, (unsigned long)bdata);
Daniel Mack6ee88d72009-11-30 00:04:02 -080090 INIT_WORK(&bdata->work, gpio_keys_work_func);
Ben Dooksbc8f1ea2009-11-10 21:01:31 -080091
92 error = gpio_request(button->gpio, desc);
93 if (error < 0) {
94 dev_err(dev, "failed to request GPIO %d, error %d\n",
95 button->gpio, error);
96 goto fail2;
97 }
98
99 error = gpio_direction_input(button->gpio);
100 if (error < 0) {
101 dev_err(dev, "failed to configure"
102 " direction for GPIO %d, error %d\n",
103 button->gpio, error);
104 goto fail3;
105 }
106
107 irq = gpio_to_irq(button->gpio);
108 if (irq < 0) {
109 error = irq;
110 dev_err(dev, "Unable to get irq number for GPIO %d, error %d\n",
111 button->gpio, error);
112 goto fail3;
113 }
114
115 error = request_irq(irq, gpio_keys_isr,
116 IRQF_SHARED |
117 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
118 desc, bdata);
119 if (error) {
120 dev_err(dev, "Unable to claim irq %d; error %d\n",
121 irq, error);
122 goto fail3;
123 }
124
125 return 0;
126
127fail3:
128 gpio_free(button->gpio);
129fail2:
130 return error;
131}
132
Phil Blundell78a56aa2007-01-18 00:44:09 -0500133static int __devinit gpio_keys_probe(struct platform_device *pdev)
134{
135 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400136 struct gpio_keys_drvdata *ddata;
Ben Dooksdb19fd82009-11-10 21:00:35 -0800137 struct device *dev = &pdev->dev;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500138 struct input_dev *input;
139 int i, error;
Anti Sulline15b0212007-09-26 00:01:17 -0400140 int wakeup = 0;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500141
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400142 ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
143 pdata->nbuttons * sizeof(struct gpio_button_data),
144 GFP_KERNEL);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500145 input = input_allocate_device();
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400146 if (!ddata || !input) {
Ben Dooksdb19fd82009-11-10 21:00:35 -0800147 dev_err(dev, "failed to allocate state\n");
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400148 error = -ENOMEM;
149 goto fail1;
150 }
Phil Blundell78a56aa2007-01-18 00:44:09 -0500151
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400152 platform_set_drvdata(pdev, ddata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500153
154 input->name = pdev->name;
155 input->phys = "gpio-keys/input0";
Dmitry Torokhov469ba4d2007-04-12 01:34:58 -0400156 input->dev.parent = &pdev->dev;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500157
158 input->id.bustype = BUS_HOST;
159 input->id.vendor = 0x0001;
160 input->id.product = 0x0001;
161 input->id.version = 0x0100;
162
Dominic Curranb67b4b12008-10-27 22:30:53 -0400163 /* Enable auto repeat feature of Linux input subsystem */
164 if (pdata->rep)
165 __set_bit(EV_REP, input->evbit);
166
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400167 ddata->input = input;
168
Phil Blundell78a56aa2007-01-18 00:44:09 -0500169 for (i = 0; i < pdata->nbuttons; i++) {
Roman Moravcik84767d02007-05-01 00:39:13 -0400170 struct gpio_keys_button *button = &pdata->buttons[i];
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400171 struct gpio_button_data *bdata = &ddata->data[i];
Roman Moravcik84767d02007-05-01 00:39:13 -0400172 unsigned int type = button->type ?: EV_KEY;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500173
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400174 bdata->input = input;
Uwe Kleine-König74dd4392008-07-30 10:33:43 -0400175 bdata->button = button;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400176
Ben Dooksbc8f1ea2009-11-10 21:01:31 -0800177 error = gpio_keys_setup_key(dev, bdata, button);
178 if (error)
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400179 goto fail2;
Roman Moravcik84767d02007-05-01 00:39:13 -0400180
Anti Sulline15b0212007-09-26 00:01:17 -0400181 if (button->wakeup)
182 wakeup = 1;
183
Roman Moravcik84767d02007-05-01 00:39:13 -0400184 input_set_capability(input, type, button->code);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500185 }
186
187 error = input_register_device(input);
188 if (error) {
Ben Dooksdb19fd82009-11-10 21:00:35 -0800189 dev_err(dev, "Unable to register input device, "
Anti Sullin006df302007-09-26 00:01:03 -0400190 "error: %d\n", error);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400191 goto fail2;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500192 }
193
Daniel Mack6ee88d72009-11-30 00:04:02 -0800194 /* get current state of buttons */
195 for (i = 0; i < pdata->nbuttons; i++)
196 gpio_keys_report_event(&ddata->data[i]);
197 input_sync(input);
198
Anti Sulline15b0212007-09-26 00:01:17 -0400199 device_init_wakeup(&pdev->dev, wakeup);
200
Phil Blundell78a56aa2007-01-18 00:44:09 -0500201 return 0;
202
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400203 fail2:
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500204 while (--i >= 0) {
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400205 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
Jani Nikulaca865a72009-06-28 22:38:44 -0700206 if (pdata->buttons[i].debounce_interval)
207 del_timer_sync(&ddata->data[i].timer);
Jani Nikulada0d03f2009-06-28 22:38:56 -0700208 cancel_work_sync(&ddata->data[i].work);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500209 gpio_free(pdata->buttons[i].gpio);
210 }
Phil Blundell78a56aa2007-01-18 00:44:09 -0500211
Anti Sullin006df302007-09-26 00:01:03 -0400212 platform_set_drvdata(pdev, NULL);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400213 fail1:
Phil Blundell78a56aa2007-01-18 00:44:09 -0500214 input_free_device(input);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400215 kfree(ddata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500216
217 return error;
218}
219
220static int __devexit gpio_keys_remove(struct platform_device *pdev)
221{
222 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400223 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
224 struct input_dev *input = ddata->input;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500225 int i;
226
Anti Sulline15b0212007-09-26 00:01:17 -0400227 device_init_wakeup(&pdev->dev, 0);
228
Phil Blundell78a56aa2007-01-18 00:44:09 -0500229 for (i = 0; i < pdata->nbuttons; i++) {
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500230 int irq = gpio_to_irq(pdata->buttons[i].gpio);
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400231 free_irq(irq, &ddata->data[i]);
Jani Nikulaca865a72009-06-28 22:38:44 -0700232 if (pdata->buttons[i].debounce_interval)
233 del_timer_sync(&ddata->data[i].timer);
Jani Nikulada0d03f2009-06-28 22:38:56 -0700234 cancel_work_sync(&ddata->data[i].work);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500235 gpio_free(pdata->buttons[i].gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500236 }
237
238 input_unregister_device(input);
239
240 return 0;
241}
242
Anti Sulline15b0212007-09-26 00:01:17 -0400243
244#ifdef CONFIG_PM
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700245static int gpio_keys_suspend(struct device *dev)
Anti Sulline15b0212007-09-26 00:01:17 -0400246{
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700247 struct platform_device *pdev = to_platform_device(dev);
Anti Sulline15b0212007-09-26 00:01:17 -0400248 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
249 int i;
250
251 if (device_may_wakeup(&pdev->dev)) {
252 for (i = 0; i < pdata->nbuttons; i++) {
253 struct gpio_keys_button *button = &pdata->buttons[i];
254 if (button->wakeup) {
255 int irq = gpio_to_irq(button->gpio);
256 enable_irq_wake(irq);
257 }
258 }
259 }
260
261 return 0;
262}
263
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700264static int gpio_keys_resume(struct device *dev)
Anti Sulline15b0212007-09-26 00:01:17 -0400265{
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700266 struct platform_device *pdev = to_platform_device(dev);
Daniel Mack6ee88d72009-11-30 00:04:02 -0800267 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
Anti Sulline15b0212007-09-26 00:01:17 -0400268 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
269 int i;
270
Daniel Mack6ee88d72009-11-30 00:04:02 -0800271 for (i = 0; i < pdata->nbuttons; i++) {
272
273 struct gpio_keys_button *button = &pdata->buttons[i];
274 if (button->wakeup && device_may_wakeup(&pdev->dev)) {
275 int irq = gpio_to_irq(button->gpio);
276 disable_irq_wake(irq);
Anti Sulline15b0212007-09-26 00:01:17 -0400277 }
Daniel Mack6ee88d72009-11-30 00:04:02 -0800278
279 gpio_keys_report_event(&ddata->data[i]);
Anti Sulline15b0212007-09-26 00:01:17 -0400280 }
Daniel Mack6ee88d72009-11-30 00:04:02 -0800281 input_sync(ddata->input);
Anti Sulline15b0212007-09-26 00:01:17 -0400282
283 return 0;
284}
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700285
286static const struct dev_pm_ops gpio_keys_pm_ops = {
287 .suspend = gpio_keys_suspend,
288 .resume = gpio_keys_resume,
289};
Anti Sulline15b0212007-09-26 00:01:17 -0400290#endif
291
Uwe Kleine-König9b070442008-07-30 10:34:02 -0400292static struct platform_driver gpio_keys_device_driver = {
Phil Blundell78a56aa2007-01-18 00:44:09 -0500293 .probe = gpio_keys_probe,
294 .remove = __devexit_p(gpio_keys_remove),
Phil Blundell78a56aa2007-01-18 00:44:09 -0500295 .driver = {
296 .name = "gpio-keys",
Kay Sieversd7b52472008-04-18 00:24:42 -0400297 .owner = THIS_MODULE,
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700298#ifdef CONFIG_PM
299 .pm = &gpio_keys_pm_ops,
300#endif
Phil Blundell78a56aa2007-01-18 00:44:09 -0500301 }
302};
303
304static int __init gpio_keys_init(void)
305{
306 return platform_driver_register(&gpio_keys_device_driver);
307}
308
309static void __exit gpio_keys_exit(void)
310{
311 platform_driver_unregister(&gpio_keys_device_driver);
312}
313
314module_init(gpio_keys_init);
315module_exit(gpio_keys_exit);
316
317MODULE_LICENSE("GPL");
318MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
319MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
Kay Sieversd7b52472008-04-18 00:24:42 -0400320MODULE_ALIAS("platform:gpio-keys");