blob: a54dc15f9005c032a1c4da4000378c55f98db54a [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>
12#include <linux/version.h>
13
14#include <linux/init.h>
15#include <linux/fs.h>
16#include <linux/interrupt.h>
17#include <linux/irq.h>
18#include <linux/sched.h>
19#include <linux/pm.h>
20#include <linux/sysctl.h>
21#include <linux/proc_fs.h>
22#include <linux/delay.h>
23#include <linux/platform_device.h>
24#include <linux/input.h>
David Brownell49015be2007-03-05 00:30:22 -080025#include <linux/gpio_keys.h>
Phil Blundell78a56aa2007-01-18 00:44:09 -050026
Philipp Zabel0d98f6b2007-02-18 01:40:46 -050027#include <asm/gpio.h>
Phil Blundell78a56aa2007-01-18 00:44:09 -050028
29static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
30{
31 int i;
32 struct platform_device *pdev = dev_id;
33 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
34 struct input_dev *input = platform_get_drvdata(pdev);
35
36 for (i = 0; i < pdata->nbuttons; i++) {
Roman Moravcik84767d02007-05-01 00:39:13 -040037 struct gpio_keys_button *button = &pdata->buttons[i];
38 int gpio = button->gpio;
Phil Blundell78a56aa2007-01-18 00:44:09 -050039
Roman Moravcik84767d02007-05-01 00:39:13 -040040 if (irq == gpio_to_irq(gpio)) {
41 unsigned int type = button->type ?: EV_KEY;
42 int state = (gpio_get_value(gpio) ? 1 : 0) ^ button->active_low;
43
44 input_event(input, type, button->code, !!state);
Phil Blundell78a56aa2007-01-18 00:44:09 -050045 input_sync(input);
David Brownell1164ec12008-04-15 01:31:13 -040046 return IRQ_HANDLED;
Phil Blundell78a56aa2007-01-18 00:44:09 -050047 }
48 }
49
David Brownell1164ec12008-04-15 01:31:13 -040050 return IRQ_NONE;
Phil Blundell78a56aa2007-01-18 00:44:09 -050051}
52
53static int __devinit gpio_keys_probe(struct platform_device *pdev)
54{
55 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
56 struct input_dev *input;
57 int i, error;
Anti Sulline15b0212007-09-26 00:01:17 -040058 int wakeup = 0;
Phil Blundell78a56aa2007-01-18 00:44:09 -050059
60 input = input_allocate_device();
61 if (!input)
62 return -ENOMEM;
63
64 platform_set_drvdata(pdev, input);
65
Jiri Slaby7b19ada2007-10-18 23:40:32 -070066 input->evbit[0] = BIT_MASK(EV_KEY);
Phil Blundell78a56aa2007-01-18 00:44:09 -050067
68 input->name = pdev->name;
69 input->phys = "gpio-keys/input0";
Dmitry Torokhov469ba4d2007-04-12 01:34:58 -040070 input->dev.parent = &pdev->dev;
Phil Blundell78a56aa2007-01-18 00:44:09 -050071
72 input->id.bustype = BUS_HOST;
73 input->id.vendor = 0x0001;
74 input->id.product = 0x0001;
75 input->id.version = 0x0100;
76
77 for (i = 0; i < pdata->nbuttons; i++) {
Roman Moravcik84767d02007-05-01 00:39:13 -040078 struct gpio_keys_button *button = &pdata->buttons[i];
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -050079 int irq;
Roman Moravcik84767d02007-05-01 00:39:13 -040080 unsigned int type = button->type ?: EV_KEY;
Phil Blundell78a56aa2007-01-18 00:44:09 -050081
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -050082 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
83 if (error < 0) {
84 pr_err("gpio-keys: failed to request GPIO %d,"
85 " error %d\n", button->gpio, error);
86 goto fail;
87 }
88
89 error = gpio_direction_input(button->gpio);
90 if (error < 0) {
91 pr_err("gpio-keys: failed to configure input"
92 " direction for GPIO %d, error %d\n",
93 button->gpio, error);
94 gpio_free(button->gpio);
95 goto fail;
96 }
97
98 irq = gpio_to_irq(button->gpio);
Anti Sullin006df302007-09-26 00:01:03 -040099 if (irq < 0) {
100 error = irq;
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500101 pr_err("gpio-keys: Unable to get irq number"
102 " for GPIO %d, error %d\n",
Anti Sullin006df302007-09-26 00:01:03 -0400103 button->gpio, error);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500104 gpio_free(button->gpio);
Anti Sullin006df302007-09-26 00:01:03 -0400105 goto fail;
106 }
107
108 error = request_irq(irq, gpio_keys_isr,
109 IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
110 IRQF_TRIGGER_FALLING,
111 button->desc ? button->desc : "gpio_keys",
112 pdev);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500113 if (error) {
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500114 pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500115 irq, error);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500116 gpio_free(button->gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500117 goto fail;
118 }
Roman Moravcik84767d02007-05-01 00:39:13 -0400119
Anti Sulline15b0212007-09-26 00:01:17 -0400120 if (button->wakeup)
121 wakeup = 1;
122
Roman Moravcik84767d02007-05-01 00:39:13 -0400123 input_set_capability(input, type, button->code);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500124 }
125
126 error = input_register_device(input);
127 if (error) {
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500128 pr_err("gpio-keys: Unable to register input device, "
Anti Sullin006df302007-09-26 00:01:03 -0400129 "error: %d\n", error);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500130 goto fail;
131 }
132
Anti Sulline15b0212007-09-26 00:01:17 -0400133 device_init_wakeup(&pdev->dev, wakeup);
134
Phil Blundell78a56aa2007-01-18 00:44:09 -0500135 return 0;
136
137 fail:
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500138 while (--i >= 0) {
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500139 free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500140 gpio_free(pdata->buttons[i].gpio);
141 }
Phil Blundell78a56aa2007-01-18 00:44:09 -0500142
Anti Sullin006df302007-09-26 00:01:03 -0400143 platform_set_drvdata(pdev, NULL);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500144 input_free_device(input);
145
146 return error;
147}
148
149static int __devexit gpio_keys_remove(struct platform_device *pdev)
150{
151 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
152 struct input_dev *input = platform_get_drvdata(pdev);
153 int i;
154
Anti Sulline15b0212007-09-26 00:01:17 -0400155 device_init_wakeup(&pdev->dev, 0);
156
Phil Blundell78a56aa2007-01-18 00:44:09 -0500157 for (i = 0; i < pdata->nbuttons; i++) {
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500158 int irq = gpio_to_irq(pdata->buttons[i].gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500159 free_irq(irq, pdev);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500160 gpio_free(pdata->buttons[i].gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500161 }
162
163 input_unregister_device(input);
164
165 return 0;
166}
167
Anti Sulline15b0212007-09-26 00:01:17 -0400168
169#ifdef CONFIG_PM
170static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
171{
172 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
173 int i;
174
175 if (device_may_wakeup(&pdev->dev)) {
176 for (i = 0; i < pdata->nbuttons; i++) {
177 struct gpio_keys_button *button = &pdata->buttons[i];
178 if (button->wakeup) {
179 int irq = gpio_to_irq(button->gpio);
180 enable_irq_wake(irq);
181 }
182 }
183 }
184
185 return 0;
186}
187
188static int gpio_keys_resume(struct platform_device *pdev)
189{
190 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
191 int i;
192
193 if (device_may_wakeup(&pdev->dev)) {
194 for (i = 0; i < pdata->nbuttons; i++) {
195 struct gpio_keys_button *button = &pdata->buttons[i];
196 if (button->wakeup) {
197 int irq = gpio_to_irq(button->gpio);
198 disable_irq_wake(irq);
199 }
200 }
201 }
202
203 return 0;
204}
205#else
206#define gpio_keys_suspend NULL
207#define gpio_keys_resume NULL
208#endif
209
Phil Blundell78a56aa2007-01-18 00:44:09 -0500210struct platform_driver gpio_keys_device_driver = {
211 .probe = gpio_keys_probe,
212 .remove = __devexit_p(gpio_keys_remove),
Anti Sulline15b0212007-09-26 00:01:17 -0400213 .suspend = gpio_keys_suspend,
214 .resume = gpio_keys_resume,
Phil Blundell78a56aa2007-01-18 00:44:09 -0500215 .driver = {
216 .name = "gpio-keys",
217 }
218};
219
220static int __init gpio_keys_init(void)
221{
222 return platform_driver_register(&gpio_keys_device_driver);
223}
224
225static void __exit gpio_keys_exit(void)
226{
227 platform_driver_unregister(&gpio_keys_device_driver);
228}
229
230module_init(gpio_keys_init);
231module_exit(gpio_keys_exit);
232
233MODULE_LICENSE("GPL");
234MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
235MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");