blob: 3d6820b4465bb509a391bd4cc70f5449937eaf4c [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>
25#include <linux/irq.h>
David Brownell49015be2007-03-05 00:30:22 -080026#include <linux/gpio_keys.h>
Phil Blundell78a56aa2007-01-18 00:44:09 -050027
Philipp Zabel0d98f6b2007-02-18 01:40:46 -050028#include <asm/gpio.h>
Phil Blundell78a56aa2007-01-18 00:44:09 -050029
30static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
31{
32 int i;
33 struct platform_device *pdev = dev_id;
34 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
35 struct input_dev *input = platform_get_drvdata(pdev);
36
37 for (i = 0; i < pdata->nbuttons; i++) {
Roman Moravcik84767d02007-05-01 00:39:13 -040038 struct gpio_keys_button *button = &pdata->buttons[i];
39 int gpio = button->gpio;
Phil Blundell78a56aa2007-01-18 00:44:09 -050040
Roman Moravcik84767d02007-05-01 00:39:13 -040041 if (irq == gpio_to_irq(gpio)) {
42 unsigned int type = button->type ?: EV_KEY;
43 int state = (gpio_get_value(gpio) ? 1 : 0) ^ button->active_low;
44
45 input_event(input, type, button->code, !!state);
Phil Blundell78a56aa2007-01-18 00:44:09 -050046 input_sync(input);
47 }
48 }
49
50 return IRQ_HANDLED;
51}
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
66 input->evbit[0] = BIT(EV_KEY);
67
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];
79 int irq = gpio_to_irq(button->gpio);
80 unsigned int type = button->type ?: EV_KEY;
Phil Blundell78a56aa2007-01-18 00:44:09 -050081
Anti Sullin006df302007-09-26 00:01:03 -040082 if (irq < 0) {
83 error = irq;
84 printk(KERN_ERR
85 "gpio-keys: "
86 "Unable to get irq number for GPIO %d,"
87 "error %d\n",
88 button->gpio, error);
89 goto fail;
90 }
91
92 error = request_irq(irq, gpio_keys_isr,
93 IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
94 IRQF_TRIGGER_FALLING,
95 button->desc ? button->desc : "gpio_keys",
96 pdev);
Phil Blundell78a56aa2007-01-18 00:44:09 -050097 if (error) {
Anti Sullin006df302007-09-26 00:01:03 -040098 printk(KERN_ERR
99 "gpio-keys: Unable to claim irq %d; error %d\n",
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500100 irq, error);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500101 goto fail;
102 }
Roman Moravcik84767d02007-05-01 00:39:13 -0400103
Anti Sulline15b0212007-09-26 00:01:17 -0400104 if (button->wakeup)
105 wakeup = 1;
106
Roman Moravcik84767d02007-05-01 00:39:13 -0400107 input_set_capability(input, type, button->code);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500108 }
109
110 error = input_register_device(input);
111 if (error) {
Anti Sullin006df302007-09-26 00:01:03 -0400112 printk(KERN_ERR
113 "gpio-keys: Unable to register input device, "
114 "error: %d\n", error);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500115 goto fail;
116 }
117
Anti Sulline15b0212007-09-26 00:01:17 -0400118 device_init_wakeup(&pdev->dev, wakeup);
119
Phil Blundell78a56aa2007-01-18 00:44:09 -0500120 return 0;
121
122 fail:
Anti Sullin006df302007-09-26 00:01:03 -0400123 while (--i >= 0)
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500124 free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500125
Anti Sullin006df302007-09-26 00:01:03 -0400126 platform_set_drvdata(pdev, NULL);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500127 input_free_device(input);
128
129 return error;
130}
131
132static int __devexit gpio_keys_remove(struct platform_device *pdev)
133{
134 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
135 struct input_dev *input = platform_get_drvdata(pdev);
136 int i;
137
Anti Sulline15b0212007-09-26 00:01:17 -0400138 device_init_wakeup(&pdev->dev, 0);
139
Phil Blundell78a56aa2007-01-18 00:44:09 -0500140 for (i = 0; i < pdata->nbuttons; i++) {
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500141 int irq = gpio_to_irq(pdata->buttons[i].gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500142 free_irq(irq, pdev);
143 }
144
145 input_unregister_device(input);
146
147 return 0;
148}
149
Anti Sulline15b0212007-09-26 00:01:17 -0400150
151#ifdef CONFIG_PM
152static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
153{
154 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
155 int i;
156
157 if (device_may_wakeup(&pdev->dev)) {
158 for (i = 0; i < pdata->nbuttons; i++) {
159 struct gpio_keys_button *button = &pdata->buttons[i];
160 if (button->wakeup) {
161 int irq = gpio_to_irq(button->gpio);
162 enable_irq_wake(irq);
163 }
164 }
165 }
166
167 return 0;
168}
169
170static int gpio_keys_resume(struct platform_device *pdev)
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 disable_irq_wake(irq);
181 }
182 }
183 }
184
185 return 0;
186}
187#else
188#define gpio_keys_suspend NULL
189#define gpio_keys_resume NULL
190#endif
191
Phil Blundell78a56aa2007-01-18 00:44:09 -0500192struct platform_driver gpio_keys_device_driver = {
193 .probe = gpio_keys_probe,
194 .remove = __devexit_p(gpio_keys_remove),
Anti Sulline15b0212007-09-26 00:01:17 -0400195 .suspend = gpio_keys_suspend,
196 .resume = gpio_keys_resume,
Phil Blundell78a56aa2007-01-18 00:44:09 -0500197 .driver = {
198 .name = "gpio-keys",
199 }
200};
201
202static int __init gpio_keys_init(void)
203{
204 return platform_driver_register(&gpio_keys_device_driver);
205}
206
207static void __exit gpio_keys_exit(void)
208{
209 platform_driver_unregister(&gpio_keys_device_driver);
210}
211
212module_init(gpio_keys_init);
213module_exit(gpio_keys_exit);
214
215MODULE_LICENSE("GPL");
216MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
217MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");