Diff markup
1 /* 1 /*
2 * Driver for keys on GPIO lines capable of ge 2 * Driver for keys on GPIO lines capable of generating interrupts.
3 * 3 *
4 * Copyright 2005 Phil Blundell 4 * Copyright 2005 Phil Blundell
5 * 5 *
6 * This program is free software; you can redi 6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Publi 7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation. 8 * published by the Free Software Foundation.
9 */ 9 */
10 10
11 #include <linux/module.h> 11 #include <linux/module.h>
12 #include <linux/version.h> 12 #include <linux/version.h>
13 13
14 #include <linux/init.h> 14 #include <linux/init.h>
15 #include <linux/fs.h> 15 #include <linux/fs.h>
16 #include <linux/interrupt.h> 16 #include <linux/interrupt.h>
17 #include <linux/irq.h> 17 #include <linux/irq.h>
18 #include <linux/sched.h> 18 #include <linux/sched.h>
19 #include <linux/pm.h> 19 #include <linux/pm.h>
20 #include <linux/sysctl.h> 20 #include <linux/sysctl.h>
21 #include <linux/proc_fs.h> 21 #include <linux/proc_fs.h>
22 #include <linux/delay.h> 22 #include <linux/delay.h>
23 #include <linux/platform_device.h> 23 #include <linux/platform_device.h>
24 #include <linux/input.h> 24 #include <linux/input.h>
25 #include <linux/gpio_keys.h> 25 #include <linux/gpio_keys.h>
26 26
27 #include <asm/gpio.h> 27 #include <asm/gpio.h>
28 28
29 static irqreturn_t gpio_keys_isr(int irq, void 29 static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
30 { 30 {
31 int i; 31 int i;
32 struct platform_device *pdev = dev_id; 32 struct platform_device *pdev = dev_id;
33 struct gpio_keys_platform_data *pdata 33 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
34 struct input_dev *input = platform_get 34 struct input_dev *input = platform_get_drvdata(pdev);
35 35
36 for (i = 0; i < pdata->nbuttons; i++) 36 for (i = 0; i < pdata->nbuttons; i++) {
37 struct gpio_keys_button *butto 37 struct gpio_keys_button *button = &pdata->buttons[i];
38 int gpio = button->gpio; 38 int gpio = button->gpio;
39 39
40 if (irq == gpio_to_irq(gpio)) 40 if (irq == gpio_to_irq(gpio)) {
41 unsigned int type = bu 41 unsigned int type = button->type ?: EV_KEY;
42 int state = (gpio_get_ 42 int state = (gpio_get_value(gpio) ? 1 : 0) ^ button->active_low;
43 43
44 input_event(input, typ 44 input_event(input, type, button->code, !!state);
45 input_sync(input); 45 input_sync(input);
46 } 46 }
47 } 47 }
48 48
49 return IRQ_HANDLED; 49 return IRQ_HANDLED;
50 } 50 }
51 51
52 static int __devinit gpio_keys_probe(struct pl 52 static int __devinit gpio_keys_probe(struct platform_device *pdev)
53 { 53 {
54 struct gpio_keys_platform_data *pdata 54 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
55 struct input_dev *input; 55 struct input_dev *input;
56 int i, error; 56 int i, error;
57 int wakeup = 0; 57 int wakeup = 0;
58 58
59 input = input_allocate_device(); 59 input = input_allocate_device();
60 if (!input) 60 if (!input)
61 return -ENOMEM; 61 return -ENOMEM;
62 62
63 platform_set_drvdata(pdev, input); 63 platform_set_drvdata(pdev, input);
64 64
65 input->evbit[0] = BIT_MASK(EV_KEY); 65 input->evbit[0] = BIT_MASK(EV_KEY);
66 66
67 input->name = pdev->name; 67 input->name = pdev->name;
68 input->phys = "gpio-keys/input0"; 68 input->phys = "gpio-keys/input0";
69 input->dev.parent = &pdev->dev; 69 input->dev.parent = &pdev->dev;
70 70
71 input->id.bustype = BUS_HOST; 71 input->id.bustype = BUS_HOST;
72 input->id.vendor = 0x0001; 72 input->id.vendor = 0x0001;
73 input->id.product = 0x0001; 73 input->id.product = 0x0001;
74 input->id.version = 0x0100; 74 input->id.version = 0x0100;
75 75
76 for (i = 0; i < pdata->nbuttons; i++) 76 for (i = 0; i < pdata->nbuttons; i++) {
77 struct gpio_keys_button *butto 77 struct gpio_keys_button *button = &pdata->buttons[i];
78 int irq; 78 int irq;
79 unsigned int type = button->ty 79 unsigned int type = button->type ?: EV_KEY;
80 80
81 error = gpio_request(button->g 81 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
82 if (error < 0) { 82 if (error < 0) {
83 pr_err("gpio-keys: fai 83 pr_err("gpio-keys: failed to request GPIO %d,"
84 " error %d\n", 84 " error %d\n", button->gpio, error);
85 goto fail; 85 goto fail;
86 } 86 }
87 87
88 error = gpio_direction_input(b 88 error = gpio_direction_input(button->gpio);
89 if (error < 0) { 89 if (error < 0) {
90 pr_err("gpio-keys: fai 90 pr_err("gpio-keys: failed to configure input"
91 " direction fo 91 " direction for GPIO %d, error %d\n",
92 button->gpio, 92 button->gpio, error);
93 gpio_free(button->gpio 93 gpio_free(button->gpio);
94 goto fail; 94 goto fail;
95 } 95 }
96 96
97 irq = gpio_to_irq(button->gpio 97 irq = gpio_to_irq(button->gpio);
98 if (irq < 0) { 98 if (irq < 0) {
99 error = irq; 99 error = irq;
100 pr_err("gpio-keys: Una 100 pr_err("gpio-keys: Unable to get irq number"
101 " for GPIO %d, 101 " for GPIO %d, error %d\n",
102 button->gpio, 102 button->gpio, error);
103 gpio_free(button->gpio 103 gpio_free(button->gpio);
104 goto fail; 104 goto fail;
105 } 105 }
106 106
107 error = request_irq(irq, gpio_ 107 error = request_irq(irq, gpio_keys_isr,
108 IRQF_SAMPL 108 IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
109 IRQF_T 109 IRQF_TRIGGER_FALLING,
110 button->de 110 button->desc ? button->desc : "gpio_keys",
111 pdev); 111 pdev);
112 if (error) { 112 if (error) {
113 pr_err("gpio-keys: Una 113 pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
114 irq, error); 114 irq, error);
115 gpio_free(button->gpio 115 gpio_free(button->gpio);
116 goto fail; 116 goto fail;
117 } 117 }
118 118
119 if (button->wakeup) 119 if (button->wakeup)
120 wakeup = 1; 120 wakeup = 1;
121 121
122 input_set_capability(input, ty 122 input_set_capability(input, type, button->code);
123 } 123 }
124 124
125 error = input_register_device(input); 125 error = input_register_device(input);
126 if (error) { 126 if (error) {
127 pr_err("gpio-keys: Unable to r 127 pr_err("gpio-keys: Unable to register input device, "
128 "error: %d\n", error); 128 "error: %d\n", error);
129 goto fail; 129 goto fail;
130 } 130 }
131 131
132 device_init_wakeup(&pdev->dev, wakeup) 132 device_init_wakeup(&pdev->dev, wakeup);
133 133
134 return 0; 134 return 0;
135 135
136 fail: 136 fail:
137 while (--i >= 0) { 137 while (--i >= 0) {
138 free_irq(gpio_to_irq(pdata->bu 138 free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev);
139 gpio_free(pdata->buttons[i].gp 139 gpio_free(pdata->buttons[i].gpio);
140 } 140 }
141 141
142 platform_set_drvdata(pdev, NULL); 142 platform_set_drvdata(pdev, NULL);
143 input_free_device(input); 143 input_free_device(input);
144 144
145 return error; 145 return error;
146 } 146 }
147 147
148 static int __devexit gpio_keys_remove(struct p 148 static int __devexit gpio_keys_remove(struct platform_device *pdev)
149 { 149 {
150 struct gpio_keys_platform_data *pdata 150 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
151 struct input_dev *input = platform_get 151 struct input_dev *input = platform_get_drvdata(pdev);
152 int i; 152 int i;
153 153
154 device_init_wakeup(&pdev->dev, 0); 154 device_init_wakeup(&pdev->dev, 0);
155 155
156 for (i = 0; i < pdata->nbuttons; i++) 156 for (i = 0; i < pdata->nbuttons; i++) {
157 int irq = gpio_to_irq(pdata->b 157 int irq = gpio_to_irq(pdata->buttons[i].gpio);
158 free_irq(irq, pdev); 158 free_irq(irq, pdev);
159 gpio_free(pdata->buttons[i].gp 159 gpio_free(pdata->buttons[i].gpio);
160 } 160 }
161 161
162 input_unregister_device(input); 162 input_unregister_device(input);
163 163
164 return 0; 164 return 0;
165 } 165 }
166 166
167 167
168 #ifdef CONFIG_PM 168 #ifdef CONFIG_PM
169 static int gpio_keys_suspend(struct platform_d 169 static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
170 { 170 {
171 struct gpio_keys_platform_data *pdata 171 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
172 int i; 172 int i;
173 173
174 if (device_may_wakeup(&pdev->dev)) { 174 if (device_may_wakeup(&pdev->dev)) {
175 for (i = 0; i < pdata->nbutton 175 for (i = 0; i < pdata->nbuttons; i++) {
176 struct gpio_keys_butto 176 struct gpio_keys_button *button = &pdata->buttons[i];
177 if (button->wakeup) { 177 if (button->wakeup) {
178 int irq = gpio 178 int irq = gpio_to_irq(button->gpio);
179 enable_irq_wak 179 enable_irq_wake(irq);
180 } 180 }
181 } 181 }
182 } 182 }
183 183
184 return 0; 184 return 0;
185 } 185 }
186 186
187 static int gpio_keys_resume(struct platform_de 187 static int gpio_keys_resume(struct platform_device *pdev)
188 { 188 {
189 struct gpio_keys_platform_data *pdata 189 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
190 int i; 190 int i;
191 191
192 if (device_may_wakeup(&pdev->dev)) { 192 if (device_may_wakeup(&pdev->dev)) {
193 for (i = 0; i < pdata->nbutton 193 for (i = 0; i < pdata->nbuttons; i++) {
194 struct gpio_keys_butto 194 struct gpio_keys_button *button = &pdata->buttons[i];
195 if (button->wakeup) { 195 if (button->wakeup) {
196 int irq = gpio 196 int irq = gpio_to_irq(button->gpio);
197 disable_irq_wa 197 disable_irq_wake(irq);
198 } 198 }
199 } 199 }
200 } 200 }
201 201
202 return 0; 202 return 0;
203 } 203 }
204 #else 204 #else
205 #define gpio_keys_suspend NULL 205 #define gpio_keys_suspend NULL
206 #define gpio_keys_resume NULL 206 #define gpio_keys_resume NULL
207 #endif 207 #endif
208 208
209 struct platform_driver gpio_keys_device_driver 209 struct platform_driver gpio_keys_device_driver = {
210 .probe = gpio_keys_probe, 210 .probe = gpio_keys_probe,
211 .remove = __devexit_p(gpio_key 211 .remove = __devexit_p(gpio_keys_remove),
212 .suspend = gpio_keys_suspend, 212 .suspend = gpio_keys_suspend,
213 .resume = gpio_keys_resume, 213 .resume = gpio_keys_resume,
214 .driver = { 214 .driver = {
215 .name = "gpio-keys", 215 .name = "gpio-keys",
216 } 216 }
217 }; 217 };
218 218
219 static int __init gpio_keys_init(void) 219 static int __init gpio_keys_init(void)
220 { 220 {
221 return platform_driver_register(&gpio_ 221 return platform_driver_register(&gpio_keys_device_driver);
222 } 222 }
223 223
224 static void __exit gpio_keys_exit(void) 224 static void __exit gpio_keys_exit(void)
225 { 225 {
226 platform_driver_unregister(&gpio_keys_ 226 platform_driver_unregister(&gpio_keys_device_driver);
227 } 227 }
228 228
229 module_init(gpio_keys_init); 229 module_init(gpio_keys_init);
230 module_exit(gpio_keys_exit); 230 module_exit(gpio_keys_exit);
231 231
232 MODULE_LICENSE("GPL"); 232 MODULE_LICENSE("GPL");
233 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org 233 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
234 MODULE_DESCRIPTION("Keyboard driver for CPU GP 234 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
235 235
|
This page was automatically generated by the
LXR engine.
|