update module for more matrix-flags
[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>
5
ab5ed215
MG
6#define PREFIX "debounce: "
7
a4838b14 8static unsigned old_flags = 0;
bb65757a
MG
9ktime_t old_debounce_delay;
10ktime_t old_settle_time;
11ktime_t old_poll_time;
1e65c113
MG
12static struct gpio_event_matrix_info *gpio_evmi = NULL;
13
ab5ed215 14static int find_ms2_dev(struct device *dev, void *data)
d1ff9643
MG
15{
16 if (!strncmp((char*)data, dev_name(dev), strlen((char*)data))) {
ab5ed215 17 printk(KERN_INFO PREFIX "Found it\n");
d1ff9643
MG
18 return 1;
19 }
20 return 0;
21}
b422e333 22
f7cb07b2
MG
23static ssize_t show_debounce_delay(struct device *dev, struct device_attribute *attr, char *buf)
24{
25 if (!gpio_evmi)
26 return -ENODEV;
27
28 return snprintf(buf, PAGE_SIZE, "%ld\n", (gpio_evmi->debounce_delay.tv.nsec / NSEC_PER_MSEC));
29}
30
31static void set_debounce_delay(long delay)
32{
33 if (gpio_evmi->debounce_delay.tv.nsec != delay * NSEC_PER_MSEC) {
34 printk(KERN_INFO PREFIX "Changing debounce_delay\n");
35 gpio_evmi->debounce_delay.tv.nsec = delay * NSEC_PER_MSEC;
f7cb07b2
MG
36 printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
37 }
38
fc215caa 39#if 0
f7cb07b2
MG
40 if (gpio_evmi->debounce_delay.tv.nsec != 0) {
41 if (!(gpio_evmi->flags & GPIOKPF_DEBOUNCE)) {
42 printk(KERN_INFO PREFIX "Activating debounce\n");
43 gpio_evmi->flags |= GPIOKPF_DEBOUNCE;
44 }
45 } else {
46 if (gpio_evmi->flags & GPIOKPF_DEBOUNCE) {
47 printk(KERN_INFO PREFIX "Deactivating debounce\n");
48 gpio_evmi->flags &= ~GPIOKPF_DEBOUNCE;
49 }
50 }
fc215caa 51#endif
f7cb07b2
MG
52}
53
54static ssize_t store_debounce_delay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
55{
56 long int delay;
57
58 if (!gpio_evmi)
59 return -ENODEV;
60
61 sscanf(buf, "%ld", &delay);
62 set_debounce_delay(delay);
63
fc215caa 64 return count;
f7cb07b2
MG
65}
66
67static ssize_t show_settle_time(struct device *dev, struct device_attribute *attr, char *buf)
68{
69 if (!gpio_evmi)
70 return -ENODEV;
71
72 return snprintf(buf, PAGE_SIZE, "%ld\n", (gpio_evmi->settle_time.tv.nsec / NSEC_PER_USEC));
73}
74
75static ssize_t store_settle_time(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
76{
77 long int delay;
78
79 if (!gpio_evmi)
80 return -ENODEV;
81
82 sscanf(buf, "%ld", &delay);
83 gpio_evmi->settle_time.tv.nsec = delay * NSEC_PER_USEC;
84
fc215caa 85 return count;
f7cb07b2
MG
86}
87
88static ssize_t show_poll_time(struct device *dev, struct device_attribute *attr, char *buf)
89{
90 if (!gpio_evmi)
91 return -ENODEV;
92
93 return snprintf(buf, PAGE_SIZE, "%ld\n", (gpio_evmi->poll_time.tv.nsec / NSEC_PER_MSEC));
94}
95
96static ssize_t store_poll_time(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
97{
98 long int delay;
99
100 if (!gpio_evmi)
101 return -ENODEV;
102
103 sscanf(buf, "%ld", &delay);
104 gpio_evmi->poll_time.tv.nsec = delay * NSEC_PER_MSEC;
105
fc215caa 106 return count;
f7cb07b2
MG
107}
108
109static ssize_t show_flags(struct device *dev, struct device_attribute *attr, char *buf)
110{
111 if (!gpio_evmi)
112 return -ENODEV;
113
114 return snprintf(buf, PAGE_SIZE, "0x%x\n", gpio_evmi->flags);
115}
116
117static ssize_t store_flags(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
118{
119 unsigned flags;
120
121 if (!gpio_evmi)
122 return -ENODEV;
123
124 sscanf(buf, "0x%x", &flags);
fc215caa
MG
125
126 printk(KERN_INFO PREFIX "flags: 0x%x\n", flags);
127
f7cb07b2
MG
128 gpio_evmi->flags = flags;
129
fc215caa
MG
130 return count;
131}
132
133static ssize_t show_debounce_flag(struct device *dev, struct device_attribute *attr, char *buf)
134{
135 if (!gpio_evmi)
136 return -ENODEV;
137
138 return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_DEBOUNCE) ? 1 : 0);
f7cb07b2
MG
139}
140
fc215caa
MG
141static ssize_t store_debounce_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
142{
143 unsigned flag;
144
145 if (!gpio_evmi)
146 return -ENODEV;
147
148 sscanf(buf, "%u", &flag);
149
150 if (flag) {
151 gpio_evmi->flags |= GPIOKPF_DEBOUNCE;
152 } else {
153 gpio_evmi->flags &= ~GPIOKPF_DEBOUNCE;
154 }
155
156 return count;
157}
158
159static ssize_t show_remove_some_phantom_keys_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_REMOVE_SOME_PHANTOM_KEYS) ? 1 : 0);
165}
166
167static ssize_t store_remove_some_phantom_keys_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_REMOVE_SOME_PHANTOM_KEYS;
178 } else {
179 gpio_evmi->flags &= ~GPIOKPF_REMOVE_SOME_PHANTOM_KEYS;
180 }
181
182 return count;
183}
184
185static ssize_t show_print_unmapped_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_PRINT_UNMAPPED_KEYS) ? 1 : 0);
191}
192
193static ssize_t store_print_unmapped_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_PRINT_UNMAPPED_KEYS;
204 } else {
205 gpio_evmi->flags &= ~GPIOKPF_PRINT_UNMAPPED_KEYS;
206 }
207
208 return count;
209}
210
211static ssize_t show_print_mapped_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_MAPPED_KEYS) ? 1 : 0);
217}
218
219static ssize_t store_print_mapped_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_MAPPED_KEYS;
230 } else {
231 gpio_evmi->flags &= ~GPIOKPF_PRINT_MAPPED_KEYS;
232 }
233
234 return count;
235}
236
237static ssize_t show_print_phantom_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_PHANTOM_KEYS) ? 1 : 0);
243}
244
245static ssize_t store_print_phantom_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_PHANTOM_KEYS;
256 } else {
257 gpio_evmi->flags &= ~GPIOKPF_PRINT_PHANTOM_KEYS;
258 }
259
260 return count;
261}
262
a2485674
MG
263static ssize_t show_active_high_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_ACTIVE_HIGH) ? 1 : 0);
269}
270
271static ssize_t store_active_high_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_ACTIVE_HIGH;
282 } else {
283 gpio_evmi->flags &= ~GPIOKPF_ACTIVE_HIGH;
284 }
285
286 return count;
287}
288
289static ssize_t show_drive_inactive_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_DRIVE_INACTIVE) ? 1 : 0);
295}
296
297static ssize_t store_drive_inactive_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_DRIVE_INACTIVE;
308 } else {
309 gpio_evmi->flags &= ~GPIOKPF_DRIVE_INACTIVE;
310 }
311
312 return count;
313}
314
fc215caa
MG
315static DEVICE_ATTR(debounce_delay, (S_IRUGO | S_IWUGO), show_debounce_delay, store_debounce_delay);
316static DEVICE_ATTR(settle_time, (S_IRUGO | S_IWUGO), show_settle_time, store_settle_time);
317static DEVICE_ATTR(poll_time, (S_IRUGO | S_IWUGO), show_poll_time, store_poll_time);
318static DEVICE_ATTR(flags, (S_IRUGO), show_flags, store_flags);
319static DEVICE_ATTR(debounce_flag, (S_IRUGO | S_IWUGO), show_debounce_flag, store_debounce_flag);
320static DEVICE_ATTR(remove_some_phantom_keys_flag, (S_IRUGO | S_IWUGO), show_remove_some_phantom_keys_flag, store_remove_some_phantom_keys_flag);
321static DEVICE_ATTR(print_unmapped_keys_flag, (S_IRUGO | S_IWUGO), show_print_unmapped_keys_flag, store_print_unmapped_keys_flag);
322static DEVICE_ATTR(print_mapped_keys_flag, (S_IRUGO | S_IWUGO), show_print_mapped_keys_flag, store_print_mapped_keys_flag);
323static DEVICE_ATTR(print_phantom_keys_flag, (S_IRUGO | S_IWUGO), show_print_phantom_keys_flag, store_print_phantom_keys_flag);
a2485674
MG
324static DEVICE_ATTR(active_high_flag, (S_IRUGO), show_active_high_flag, store_active_high_flag);
325static DEVICE_ATTR(drive_inactive_flag, (S_IRUGO | S_IWUGO), show_drive_inactive_flag, store_drive_inactive_flag);
f7cb07b2
MG
326
327static void debounce_release(struct device *dev)
328{
329}
330
331static struct device debounce_device = {
332 .init_name = "debounce",
333 .release = debounce_release,
334};
335
b422e333
MG
336static int __init debounce_init(void)
337{
d1ff9643
MG
338 struct device *event_dev = NULL;
339 struct gpio_event_platform_data *gpio_epd;
340 struct gpio_event_info *gpio_ei;
f7cb07b2 341 int err = 0;
d1ff9643 342
ab5ed215 343 printk(KERN_INFO PREFIX "Searching for " GPIO_EVENT_DEV_NAME "...\n");
d1ff9643
MG
344
345 event_dev = device_find_child(&platform_bus, GPIO_EVENT_DEV_NAME, find_ms2_dev);
346 if (event_dev == NULL)
347 return -ENODEV;
348
349 gpio_epd = (struct gpio_event_platform_data*)event_dev->platform_data;
ab5ed215 350 printk(KERN_INFO PREFIX "And there is a %s connected...\n", gpio_epd->name);
d1ff9643
MG
351 if (strcmp(gpio_epd->name, "sholes-keypad"))
352 return -ENODEV;
353
354 gpio_ei = (struct gpio_event_info*)gpio_epd->info[0];
355 gpio_evmi = container_of(gpio_ei, struct gpio_event_matrix_info, info);
356
f7cb07b2
MG
357 err = device_register(&debounce_device);
358 if (err) {
359 return err;
360 }
361
362 err = device_create_file(&debounce_device, &dev_attr_debounce_delay);
363 err = device_create_file(&debounce_device, &dev_attr_settle_time);
364 err = device_create_file(&debounce_device, &dev_attr_poll_time);
365 err = device_create_file(&debounce_device, &dev_attr_flags);
fc215caa
MG
366 err = device_create_file(&debounce_device, &dev_attr_debounce_flag);
367 err = device_create_file(&debounce_device, &dev_attr_remove_some_phantom_keys_flag);
368 err = device_create_file(&debounce_device, &dev_attr_print_unmapped_keys_flag);
369 err = device_create_file(&debounce_device, &dev_attr_print_mapped_keys_flag);
370 err = device_create_file(&debounce_device, &dev_attr_print_phantom_keys_flag);
a2485674
MG
371 err = device_create_file(&debounce_device, &dev_attr_active_high_flag);
372 err = device_create_file(&debounce_device, &dev_attr_drive_inactive_flag);
f7cb07b2 373
ab5ed215
MG
374 printk(KERN_INFO PREFIX "settle_time: %u\n", gpio_evmi->settle_time.tv.nsec);
375 printk(KERN_INFO PREFIX "poll_time: %u\n", gpio_evmi->poll_time.tv.nsec);
376 printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
377 printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
20bf1c9e 378
bb65757a
MG
379 old_debounce_delay = gpio_evmi->debounce_delay;
380 old_settle_time = gpio_evmi->settle_time;
381 old_poll_time = gpio_evmi->poll_time;
382 old_flags = gpio_evmi->flags;
1e65c113 383
a4838b14
MG
384 printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
385
b422e333
MG
386 return 0;
387}
388
389static void __exit debounce_exit(void)
390{
1e65c113 391 if (gpio_evmi) {
bb65757a 392 if (gpio_evmi->debounce_delay.tv.nsec != old_debounce_delay.tv.nsec) {
1e65c113 393 printk(KERN_INFO PREFIX "Restoring debounce_delay\n");
bb65757a 394 gpio_evmi->debounce_delay = old_debounce_delay;
1e65c113
MG
395 printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
396 }
a4838b14
MG
397 if (gpio_evmi->flags != old_flags) {
398 printk(KERN_INFO PREFIX "Restoring flags\n");
399 gpio_evmi->flags = old_flags;
400 printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
401 }
bb65757a
MG
402 gpio_evmi->settle_time = old_settle_time;
403 gpio_evmi->poll_time = old_poll_time;
1e65c113 404 }
f7cb07b2
MG
405 device_remove_file(&debounce_device, &dev_attr_debounce_delay);
406 device_remove_file(&debounce_device, &dev_attr_settle_time);
407 device_remove_file(&debounce_device, &dev_attr_poll_time);
408 device_remove_file(&debounce_device, &dev_attr_flags);
fc215caa
MG
409 device_remove_file(&debounce_device, &dev_attr_debounce_flag);
410 device_remove_file(&debounce_device, &dev_attr_remove_some_phantom_keys_flag);
411 device_remove_file(&debounce_device, &dev_attr_print_unmapped_keys_flag);
412 device_remove_file(&debounce_device, &dev_attr_print_mapped_keys_flag);
413 device_remove_file(&debounce_device, &dev_attr_print_phantom_keys_flag);
a2485674
MG
414 device_remove_file(&debounce_device, &dev_attr_active_high_flag);
415 device_remove_file(&debounce_device, &dev_attr_drive_inactive_flag);
f7cb07b2 416 device_unregister(&debounce_device);
b422e333
MG
417}
418
419module_init(debounce_init);
420module_exit(debounce_exit);
421
422MODULE_LICENSE("GPL");
423MODULE_AUTHOR("Michael Gernoth <michael@gernoth.net>");
Impressum, Datenschutz