allow tuning of more flags in /sys/devices/debounce
[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 int debounce_delay = 15;
ab5ed215 9
a4838b14 10static unsigned old_flags = 0;
bb65757a
MG
11ktime_t old_debounce_delay;
12ktime_t old_settle_time;
13ktime_t old_poll_time;
1e65c113
MG
14static struct gpio_event_matrix_info *gpio_evmi = NULL;
15
b2b792eb 16module_param(debounce_delay, int, S_IRUSR | S_IRGRP | S_IROTH);
a4838b14 17MODULE_PARM_DESC(debounce_delay, "debouncing delay (ms), default: 15");
ab5ed215
MG
18
19static int find_ms2_dev(struct device *dev, void *data)
d1ff9643
MG
20{
21 if (!strncmp((char*)data, dev_name(dev), strlen((char*)data))) {
ab5ed215 22 printk(KERN_INFO PREFIX "Found it\n");
d1ff9643
MG
23 return 1;
24 }
25 return 0;
26}
b422e333 27
f7cb07b2
MG
28static ssize_t show_debounce_delay(struct device *dev, struct device_attribute *attr, char *buf)
29{
30 if (!gpio_evmi)
31 return -ENODEV;
32
33 return snprintf(buf, PAGE_SIZE, "%ld\n", (gpio_evmi->debounce_delay.tv.nsec / NSEC_PER_MSEC));
34}
35
36static void set_debounce_delay(long delay)
37{
38 if (gpio_evmi->debounce_delay.tv.nsec != delay * NSEC_PER_MSEC) {
39 printk(KERN_INFO PREFIX "Changing debounce_delay\n");
40 gpio_evmi->debounce_delay.tv.nsec = delay * NSEC_PER_MSEC;
41 debounce_delay = delay;
42 printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
43 }
44
45 if (gpio_evmi->debounce_delay.tv.nsec != 0) {
46 if (!(gpio_evmi->flags & GPIOKPF_DEBOUNCE)) {
47 printk(KERN_INFO PREFIX "Activating debounce\n");
48 gpio_evmi->flags |= GPIOKPF_DEBOUNCE;
49 }
50 } else {
51 if (gpio_evmi->flags & GPIOKPF_DEBOUNCE) {
52 printk(KERN_INFO PREFIX "Deactivating debounce\n");
53 gpio_evmi->flags &= ~GPIOKPF_DEBOUNCE;
54 }
55 }
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
68 return strnlen(buf, PAGE_SIZE);
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
89 return strnlen(buf, PAGE_SIZE);
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
110 return strnlen(buf, PAGE_SIZE);
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);
129 gpio_evmi->flags = flags;
130
131 return strnlen(buf, PAGE_SIZE);
132}
133
134static DEVICE_ATTR(debounce_delay, (S_IRUGO | S_IWUGO), &show_debounce_delay, store_debounce_delay);
135static DEVICE_ATTR(settle_time, (S_IRUGO | S_IWUGO), &show_settle_time, store_settle_time);
136static DEVICE_ATTR(poll_time, (S_IRUGO | S_IWUGO), &show_poll_time, store_poll_time);
137static DEVICE_ATTR(flags, (S_IRUGO | S_IWUGO), &show_flags, store_flags);
138
139static void debounce_release(struct device *dev)
140{
141}
142
143static struct device debounce_device = {
144 .init_name = "debounce",
145 .release = debounce_release,
146};
147
b422e333
MG
148static int __init debounce_init(void)
149{
d1ff9643
MG
150 struct device *event_dev = NULL;
151 struct gpio_event_platform_data *gpio_epd;
152 struct gpio_event_info *gpio_ei;
f7cb07b2 153 int err = 0;
d1ff9643 154
ab5ed215 155 printk(KERN_INFO PREFIX "Searching for " GPIO_EVENT_DEV_NAME "...\n");
d1ff9643
MG
156
157 event_dev = device_find_child(&platform_bus, GPIO_EVENT_DEV_NAME, find_ms2_dev);
158 if (event_dev == NULL)
159 return -ENODEV;
160
161 gpio_epd = (struct gpio_event_platform_data*)event_dev->platform_data;
ab5ed215 162 printk(KERN_INFO PREFIX "And there is a %s connected...\n", gpio_epd->name);
d1ff9643
MG
163 if (strcmp(gpio_epd->name, "sholes-keypad"))
164 return -ENODEV;
165
166 gpio_ei = (struct gpio_event_info*)gpio_epd->info[0];
167 gpio_evmi = container_of(gpio_ei, struct gpio_event_matrix_info, info);
168
f7cb07b2
MG
169 err = device_register(&debounce_device);
170 if (err) {
171 return err;
172 }
173
174 err = device_create_file(&debounce_device, &dev_attr_debounce_delay);
175 err = device_create_file(&debounce_device, &dev_attr_settle_time);
176 err = device_create_file(&debounce_device, &dev_attr_poll_time);
177 err = device_create_file(&debounce_device, &dev_attr_flags);
178
ab5ed215
MG
179 printk(KERN_INFO PREFIX "settle_time: %u\n", gpio_evmi->settle_time.tv.nsec);
180 printk(KERN_INFO PREFIX "poll_time: %u\n", gpio_evmi->poll_time.tv.nsec);
181 printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
182 printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
20bf1c9e 183
bb65757a
MG
184 old_debounce_delay = gpio_evmi->debounce_delay;
185 old_settle_time = gpio_evmi->settle_time;
186 old_poll_time = gpio_evmi->poll_time;
187 old_flags = gpio_evmi->flags;
1e65c113 188
f7cb07b2 189 set_debounce_delay(debounce_delay);
bb65757a
MG
190#if 0
191 gpio_evmi->flags &= ~GPIOKPF_REMOVE_SOME_PHANTOM_KEYS;
192#endif
a4838b14
MG
193 printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
194
b422e333
MG
195 return 0;
196}
197
198static void __exit debounce_exit(void)
199{
1e65c113 200 if (gpio_evmi) {
bb65757a 201 if (gpio_evmi->debounce_delay.tv.nsec != old_debounce_delay.tv.nsec) {
1e65c113 202 printk(KERN_INFO PREFIX "Restoring debounce_delay\n");
bb65757a 203 gpio_evmi->debounce_delay = old_debounce_delay;
1e65c113
MG
204 printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
205 }
a4838b14
MG
206 if (gpio_evmi->flags != old_flags) {
207 printk(KERN_INFO PREFIX "Restoring flags\n");
208 gpio_evmi->flags = old_flags;
209 printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
210 }
bb65757a
MG
211 gpio_evmi->settle_time = old_settle_time;
212 gpio_evmi->poll_time = old_poll_time;
1e65c113 213 }
f7cb07b2
MG
214 device_remove_file(&debounce_device, &dev_attr_debounce_delay);
215 device_remove_file(&debounce_device, &dev_attr_settle_time);
216 device_remove_file(&debounce_device, &dev_attr_poll_time);
217 device_remove_file(&debounce_device, &dev_attr_flags);
218 device_unregister(&debounce_device);
b422e333
MG
219}
220
221module_init(debounce_init);
222module_exit(debounce_exit);
223
224MODULE_LICENSE("GPL");
225MODULE_AUTHOR("Michael Gernoth <michael@gernoth.net>");
Impressum, Datenschutz