]> cvs.zerfleddert.de Git - ms2-fixes/blame - debounce.c
only activate hw-debouncing on the inputs
[ms2-fixes] / debounce.c
CommitLineData
b422e333 1#include <linux/module.h>
d1ff9643
MG
2#include <linux/device.h>
3#include <linux/platform_device.h>
4#include <linux/gpio_event.h>
62a453fc
MG
5#include <mach/gpio.h>
6
7/* hardware debounce: (value + 1) * 31us */
6d3e3633 8#define GPIO_DEBOUNCE_TIME 0x1
d1ff9643 9
ab5ed215
MG
10#define PREFIX "debounce: "
11
a4838b14 12static unsigned old_flags = 0;
bb65757a
MG
13ktime_t old_debounce_delay;
14ktime_t old_settle_time;
15ktime_t old_poll_time;
1e65c113
MG
16static struct gpio_event_matrix_info *gpio_evmi = NULL;
17
ab5ed215 18static int find_ms2_dev(struct device *dev, void *data)
d1ff9643
MG
19{
20 if (!strncmp((char*)data, dev_name(dev), strlen((char*)data))) {
ab5ed215 21 printk(KERN_INFO PREFIX "Found it\n");
d1ff9643
MG
22 return 1;
23 }
24 return 0;
25}
b422e333 26
f7cb07b2
MG
27static ssize_t show_debounce_delay(struct device *dev, struct device_attribute *attr, char *buf)
28{
29 if (!gpio_evmi)
30 return -ENODEV;
31
32 return snprintf(buf, PAGE_SIZE, "%ld\n", (gpio_evmi->debounce_delay.tv.nsec / NSEC_PER_MSEC));
33}
34
35static void set_debounce_delay(long delay)
36{
37 if (gpio_evmi->debounce_delay.tv.nsec != delay * NSEC_PER_MSEC) {
38 printk(KERN_INFO PREFIX "Changing debounce_delay\n");
39 gpio_evmi->debounce_delay.tv.nsec = delay * NSEC_PER_MSEC;
f7cb07b2
MG
40 printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
41 }
42
fc215caa 43#if 0
f7cb07b2
MG
44 if (gpio_evmi->debounce_delay.tv.nsec != 0) {
45 if (!(gpio_evmi->flags & GPIOKPF_DEBOUNCE)) {
46 printk(KERN_INFO PREFIX "Activating debounce\n");
47 gpio_evmi->flags |= GPIOKPF_DEBOUNCE;
48 }
49 } else {
50 if (gpio_evmi->flags & GPIOKPF_DEBOUNCE) {
51 printk(KERN_INFO PREFIX "Deactivating debounce\n");
52 gpio_evmi->flags &= ~GPIOKPF_DEBOUNCE;
53 }
54 }
fc215caa 55#endif
f7cb07b2
MG
56}
57
58static ssize_t store_debounce_delay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
59{
60 long int delay;
61
62 if (!gpio_evmi)
63 return -ENODEV;
64
65 sscanf(buf, "%ld", &delay);
66 set_debounce_delay(delay);
67
fc215caa 68 return count;
f7cb07b2
MG
69}
70
71static ssize_t show_settle_time(struct device *dev, struct device_attribute *attr, char *buf)
72{
73 if (!gpio_evmi)
74 return -ENODEV;
75
76 return snprintf(buf, PAGE_SIZE, "%ld\n", (gpio_evmi->settle_time.tv.nsec / NSEC_PER_USEC));
77}
78
79static ssize_t store_settle_time(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
80{
81 long int delay;
82
83 if (!gpio_evmi)
84 return -ENODEV;
85
86 sscanf(buf, "%ld", &delay);
87 gpio_evmi->settle_time.tv.nsec = delay * NSEC_PER_USEC;
88
fc215caa 89 return count;
f7cb07b2
MG
90}
91
92static ssize_t show_poll_time(struct device *dev, struct device_attribute *attr, char *buf)
93{
94 if (!gpio_evmi)
95 return -ENODEV;
96
97 return snprintf(buf, PAGE_SIZE, "%ld\n", (gpio_evmi->poll_time.tv.nsec / NSEC_PER_MSEC));
98}
99
100static ssize_t store_poll_time(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
101{
102 long int delay;
103
104 if (!gpio_evmi)
105 return -ENODEV;
106
107 sscanf(buf, "%ld", &delay);
108 gpio_evmi->poll_time.tv.nsec = delay * NSEC_PER_MSEC;
109
fc215caa 110 return count;
f7cb07b2
MG
111}
112
113static ssize_t show_flags(struct device *dev, struct device_attribute *attr, char *buf)
114{
115 if (!gpio_evmi)
116 return -ENODEV;
117
118 return snprintf(buf, PAGE_SIZE, "0x%x\n", gpio_evmi->flags);
119}
120
121static ssize_t store_flags(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
122{
123 unsigned flags;
124
125 if (!gpio_evmi)
126 return -ENODEV;
127
128 sscanf(buf, "0x%x", &flags);
fc215caa
MG
129
130 printk(KERN_INFO PREFIX "flags: 0x%x\n", flags);
131
f7cb07b2
MG
132 gpio_evmi->flags = flags;
133
fc215caa
MG
134 return count;
135}
136
137static ssize_t show_debounce_flag(struct device *dev, struct device_attribute *attr, char *buf)
138{
139 if (!gpio_evmi)
140 return -ENODEV;
141
142 return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_DEBOUNCE) ? 1 : 0);
f7cb07b2
MG
143}
144
fc215caa
MG
145static ssize_t store_debounce_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
146{
147 unsigned flag;
148
149 if (!gpio_evmi)
150 return -ENODEV;
151
152 sscanf(buf, "%u", &flag);
153
154 if (flag) {
155 gpio_evmi->flags |= GPIOKPF_DEBOUNCE;
156 } else {
157 gpio_evmi->flags &= ~GPIOKPF_DEBOUNCE;
158 }
159
160 return count;
161}
162
163static ssize_t show_remove_some_phantom_keys_flag(struct device *dev, struct device_attribute *attr, char *buf)
164{
165 if (!gpio_evmi)
166 return -ENODEV;
167
168 return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_REMOVE_SOME_PHANTOM_KEYS) ? 1 : 0);
169}
170
171static ssize_t store_remove_some_phantom_keys_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
172{
173 unsigned flag;
174
175 if (!gpio_evmi)
176 return -ENODEV;
177
178 sscanf(buf, "%u", &flag);
179
180 if (flag) {
181 gpio_evmi->flags |= GPIOKPF_REMOVE_SOME_PHANTOM_KEYS;
182 } else {
183 gpio_evmi->flags &= ~GPIOKPF_REMOVE_SOME_PHANTOM_KEYS;
184 }
185
186 return count;
187}
188
189static ssize_t show_print_unmapped_keys_flag(struct device *dev, struct device_attribute *attr, char *buf)
190{
191 if (!gpio_evmi)
192 return -ENODEV;
193
194 return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_PRINT_UNMAPPED_KEYS) ? 1 : 0);
195}
196
197static ssize_t store_print_unmapped_keys_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
198{
199 unsigned flag;
200
201 if (!gpio_evmi)
202 return -ENODEV;
203
204 sscanf(buf, "%u", &flag);
205
206 if (flag) {
207 gpio_evmi->flags |= GPIOKPF_PRINT_UNMAPPED_KEYS;
208 } else {
209 gpio_evmi->flags &= ~GPIOKPF_PRINT_UNMAPPED_KEYS;
210 }
211
212 return count;
213}
214
215static ssize_t show_print_mapped_keys_flag(struct device *dev, struct device_attribute *attr, char *buf)
216{
217 if (!gpio_evmi)
218 return -ENODEV;
219
220 return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_PRINT_MAPPED_KEYS) ? 1 : 0);
221}
222
223static ssize_t store_print_mapped_keys_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
224{
225 unsigned flag;
226
227 if (!gpio_evmi)
228 return -ENODEV;
229
230 sscanf(buf, "%u", &flag);
231
232 if (flag) {
233 gpio_evmi->flags |= GPIOKPF_PRINT_MAPPED_KEYS;
234 } else {
235 gpio_evmi->flags &= ~GPIOKPF_PRINT_MAPPED_KEYS;
236 }
237
238 return count;
239}
240
241static ssize_t show_print_phantom_keys_flag(struct device *dev, struct device_attribute *attr, char *buf)
242{
243 if (!gpio_evmi)
244 return -ENODEV;
245
246 return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_PRINT_PHANTOM_KEYS) ? 1 : 0);
247}
248
249static ssize_t store_print_phantom_keys_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
250{
251 unsigned flag;
252
253 if (!gpio_evmi)
254 return -ENODEV;
255
256 sscanf(buf, "%u", &flag);
257
258 if (flag) {
259 gpio_evmi->flags |= GPIOKPF_PRINT_PHANTOM_KEYS;
260 } else {
261 gpio_evmi->flags &= ~GPIOKPF_PRINT_PHANTOM_KEYS;
262 }
263
264 return count;
265}
266
a2485674
MG
267static ssize_t show_active_high_flag(struct device *dev, struct device_attribute *attr, char *buf)
268{
269 if (!gpio_evmi)
270 return -ENODEV;
271
272 return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_ACTIVE_HIGH) ? 1 : 0);
273}
274
275static ssize_t store_active_high_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
276{
277 unsigned flag;
278
279 if (!gpio_evmi)
280 return -ENODEV;
281
282 sscanf(buf, "%u", &flag);
283
284 if (flag) {
285 gpio_evmi->flags |= GPIOKPF_ACTIVE_HIGH;
286 } else {
287 gpio_evmi->flags &= ~GPIOKPF_ACTIVE_HIGH;
288 }
289
290 return count;
291}
292
293static ssize_t show_drive_inactive_flag(struct device *dev, struct device_attribute *attr, char *buf)
294{
295 if (!gpio_evmi)
296 return -ENODEV;
297
298 return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_DRIVE_INACTIVE) ? 1 : 0);
299}
300
301static ssize_t store_drive_inactive_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
302{
303 unsigned flag;
304
305 if (!gpio_evmi)
306 return -ENODEV;
307
308 sscanf(buf, "%u", &flag);
309
310 if (flag) {
311 gpio_evmi->flags |= GPIOKPF_DRIVE_INACTIVE;
312 } else {
313 gpio_evmi->flags &= ~GPIOKPF_DRIVE_INACTIVE;
314 }
315
316 return count;
317}
318
fc215caa
MG
319static DEVICE_ATTR(debounce_delay, (S_IRUGO | S_IWUGO), show_debounce_delay, store_debounce_delay);
320static DEVICE_ATTR(settle_time, (S_IRUGO | S_IWUGO), show_settle_time, store_settle_time);
321static DEVICE_ATTR(poll_time, (S_IRUGO | S_IWUGO), show_poll_time, store_poll_time);
322static DEVICE_ATTR(flags, (S_IRUGO), show_flags, store_flags);
323static DEVICE_ATTR(debounce_flag, (S_IRUGO | S_IWUGO), show_debounce_flag, store_debounce_flag);
324static DEVICE_ATTR(remove_some_phantom_keys_flag, (S_IRUGO | S_IWUGO), show_remove_some_phantom_keys_flag, store_remove_some_phantom_keys_flag);
325static DEVICE_ATTR(print_unmapped_keys_flag, (S_IRUGO | S_IWUGO), show_print_unmapped_keys_flag, store_print_unmapped_keys_flag);
326static DEVICE_ATTR(print_mapped_keys_flag, (S_IRUGO | S_IWUGO), show_print_mapped_keys_flag, store_print_mapped_keys_flag);
327static DEVICE_ATTR(print_phantom_keys_flag, (S_IRUGO | S_IWUGO), show_print_phantom_keys_flag, store_print_phantom_keys_flag);
a2485674
MG
328static DEVICE_ATTR(active_high_flag, (S_IRUGO), show_active_high_flag, store_active_high_flag);
329static DEVICE_ATTR(drive_inactive_flag, (S_IRUGO | S_IWUGO), show_drive_inactive_flag, store_drive_inactive_flag);
f7cb07b2
MG
330
331static void debounce_release(struct device *dev)
332{
333}
334
335static struct device debounce_device = {
336 .init_name = "debounce",
337 .release = debounce_release,
338};
339
62a453fc
MG
340static void hw_debounce_pin(int gpio, int enable) {
341 printk(KERN_INFO PREFIX "%sabling hardware debounce for GPIO %d\n", (enable?"En":"Dis"), gpio);
342 if (enable)
343 omap_set_gpio_debounce_time(gpio, GPIO_DEBOUNCE_TIME);
344 omap_set_gpio_debounce(gpio, enable);
345}
346
347static void hw_debounce(int enable) {
348 int i;
349
a6322a6f
MG
350 if (gpio_evmi == NULL)
351 return;
62a453fc 352
a6322a6f
MG
353 for (i = 0; i < gpio_evmi->ninputs; i++) {
354 hw_debounce_pin(gpio_evmi->input_gpios[i], enable);
62a453fc
MG
355 }
356}
357
b422e333
MG
358static int __init debounce_init(void)
359{
d1ff9643
MG
360 struct device *event_dev = NULL;
361 struct gpio_event_platform_data *gpio_epd;
362 struct gpio_event_info *gpio_ei;
f7cb07b2 363 int err = 0;
d1ff9643 364
ab5ed215 365 printk(KERN_INFO PREFIX "Searching for " GPIO_EVENT_DEV_NAME "...\n");
d1ff9643
MG
366
367 event_dev = device_find_child(&platform_bus, GPIO_EVENT_DEV_NAME, find_ms2_dev);
368 if (event_dev == NULL)
369 return -ENODEV;
370
371 gpio_epd = (struct gpio_event_platform_data*)event_dev->platform_data;
ab5ed215 372 printk(KERN_INFO PREFIX "And there is a %s connected...\n", gpio_epd->name);
d1ff9643
MG
373 if (strcmp(gpio_epd->name, "sholes-keypad"))
374 return -ENODEV;
375
376 gpio_ei = (struct gpio_event_info*)gpio_epd->info[0];
377 gpio_evmi = container_of(gpio_ei, struct gpio_event_matrix_info, info);
378
f7cb07b2
MG
379 err = device_register(&debounce_device);
380 if (err) {
381 return err;
382 }
383
62a453fc
MG
384 hw_debounce(1);
385
f7cb07b2
MG
386 err = device_create_file(&debounce_device, &dev_attr_debounce_delay);
387 err = device_create_file(&debounce_device, &dev_attr_settle_time);
388 err = device_create_file(&debounce_device, &dev_attr_poll_time);
389 err = device_create_file(&debounce_device, &dev_attr_flags);
fc215caa
MG
390 err = device_create_file(&debounce_device, &dev_attr_debounce_flag);
391 err = device_create_file(&debounce_device, &dev_attr_remove_some_phantom_keys_flag);
392 err = device_create_file(&debounce_device, &dev_attr_print_unmapped_keys_flag);
393 err = device_create_file(&debounce_device, &dev_attr_print_mapped_keys_flag);
394 err = device_create_file(&debounce_device, &dev_attr_print_phantom_keys_flag);
a2485674
MG
395 err = device_create_file(&debounce_device, &dev_attr_active_high_flag);
396 err = device_create_file(&debounce_device, &dev_attr_drive_inactive_flag);
f7cb07b2 397
ab5ed215
MG
398 printk(KERN_INFO PREFIX "settle_time: %u\n", gpio_evmi->settle_time.tv.nsec);
399 printk(KERN_INFO PREFIX "poll_time: %u\n", gpio_evmi->poll_time.tv.nsec);
400 printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
401 printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
20bf1c9e 402
bb65757a
MG
403 old_debounce_delay = gpio_evmi->debounce_delay;
404 old_settle_time = gpio_evmi->settle_time;
405 old_poll_time = gpio_evmi->poll_time;
406 old_flags = gpio_evmi->flags;
1e65c113 407
a4838b14
MG
408 printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
409
b422e333
MG
410 return 0;
411}
412
413static void __exit debounce_exit(void)
414{
1e65c113 415 if (gpio_evmi) {
bb65757a 416 if (gpio_evmi->debounce_delay.tv.nsec != old_debounce_delay.tv.nsec) {
1e65c113 417 printk(KERN_INFO PREFIX "Restoring debounce_delay\n");
bb65757a 418 gpio_evmi->debounce_delay = old_debounce_delay;
1e65c113
MG
419 printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
420 }
a4838b14
MG
421 if (gpio_evmi->flags != old_flags) {
422 printk(KERN_INFO PREFIX "Restoring flags\n");
423 gpio_evmi->flags = old_flags;
424 printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
425 }
bb65757a
MG
426 gpio_evmi->settle_time = old_settle_time;
427 gpio_evmi->poll_time = old_poll_time;
1e65c113 428 }
62a453fc 429 hw_debounce(0);
f7cb07b2
MG
430 device_remove_file(&debounce_device, &dev_attr_debounce_delay);
431 device_remove_file(&debounce_device, &dev_attr_settle_time);
432 device_remove_file(&debounce_device, &dev_attr_poll_time);
433 device_remove_file(&debounce_device, &dev_attr_flags);
fc215caa
MG
434 device_remove_file(&debounce_device, &dev_attr_debounce_flag);
435 device_remove_file(&debounce_device, &dev_attr_remove_some_phantom_keys_flag);
436 device_remove_file(&debounce_device, &dev_attr_print_unmapped_keys_flag);
437 device_remove_file(&debounce_device, &dev_attr_print_mapped_keys_flag);
438 device_remove_file(&debounce_device, &dev_attr_print_phantom_keys_flag);
a2485674
MG
439 device_remove_file(&debounce_device, &dev_attr_active_high_flag);
440 device_remove_file(&debounce_device, &dev_attr_drive_inactive_flag);
f7cb07b2 441 device_unregister(&debounce_device);
b422e333
MG
442}
443
444module_init(debounce_init);
445module_exit(debounce_exit);
446
447MODULE_LICENSE("GPL");
448MODULE_AUTHOR("Michael Gernoth <michael@gernoth.net>");
Impressum, Datenschutz