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