gpio-button-hotplug: add support for sliding switches
[openwrt/svn-archive/archive.git] / package / kernel / gpio-button-hotplug / src / gpio-button-hotplug.c
1 /*
2 * GPIO Button Hotplug driver
3 *
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 * Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
6 *
7 * Based on the diag.c - GPIO interface driver for Broadcom boards
8 * Copyright (C) 2006 Mike Baker <mbm@openwrt.org>,
9 * Copyright (C) 2006-2007 Felix Fietkau <nbd@openwrt.org>
10 * Copyright (C) 2008 Andy Boyett <agb@openwrt.org>
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published
14 * by the Free Software Foundation.
15 */
16
17 #include <linux/module.h>
18 #include <linux/version.h>
19 #include <linux/kmod.h>
20
21 #include <linux/workqueue.h>
22 #include <linux/skbuff.h>
23 #include <linux/netlink.h>
24 #include <linux/kobject.h>
25 #include <linux/input.h>
26 #include <linux/platform_device.h>
27 #include <linux/of_gpio.h>
28 #include <linux/gpio_keys.h>
29
30 #define DRV_NAME "gpio-keys-polled"
31
32 #define BH_SKB_SIZE 2048
33
34 #define PFX DRV_NAME ": "
35
36 #undef BH_DEBUG
37
38 #ifdef BH_DEBUG
39 #define BH_DBG(fmt, args...) printk(KERN_DEBUG "%s: " fmt, DRV_NAME, ##args )
40 #else
41 #define BH_DBG(fmt, args...) do {} while (0)
42 #endif
43
44 #define BH_ERR(fmt, args...) printk(KERN_ERR "%s: " fmt, DRV_NAME, ##args )
45
46 struct bh_priv {
47 unsigned long seen;
48 };
49
50 struct bh_event {
51 const char *name;
52 unsigned int type;
53 char *action;
54 unsigned long seen;
55
56 struct sk_buff *skb;
57 struct work_struct work;
58 };
59
60 struct bh_map {
61 unsigned int code;
62 const char *name;
63 };
64
65 struct gpio_keys_button_data {
66 struct delayed_work work;
67 struct bh_priv bh;
68 int last_state;
69 int count;
70 int threshold;
71 int can_sleep;
72 };
73
74 extern u64 uevent_next_seqnum(void);
75
76 #define BH_MAP(_code, _name) \
77 { \
78 .code = (_code), \
79 .name = (_name), \
80 }
81
82 static struct bh_map button_map[] = {
83 BH_MAP(BTN_0, "BTN_0"),
84 BH_MAP(BTN_1, "BTN_1"),
85 BH_MAP(BTN_2, "BTN_2"),
86 BH_MAP(BTN_3, "BTN_3"),
87 BH_MAP(BTN_4, "BTN_4"),
88 BH_MAP(BTN_5, "BTN_5"),
89 BH_MAP(BTN_6, "BTN_6"),
90 BH_MAP(BTN_7, "BTN_7"),
91 BH_MAP(BTN_8, "BTN_8"),
92 BH_MAP(BTN_9, "BTN_9"),
93 BH_MAP(KEY_POWER, "power"),
94 BH_MAP(KEY_RESTART, "reset"),
95 BH_MAP(KEY_RFKILL, "rfkill"),
96 BH_MAP(KEY_WPS_BUTTON, "wps"),
97 };
98
99 /* -------------------------------------------------------------------------*/
100
101 static int bh_event_add_var(struct bh_event *event, int argv,
102 const char *format, ...)
103 {
104 static char buf[128];
105 char *s;
106 va_list args;
107 int len;
108
109 if (argv)
110 return 0;
111
112 va_start(args, format);
113 len = vsnprintf(buf, sizeof(buf), format, args);
114 va_end(args);
115
116 if (len >= sizeof(buf)) {
117 BH_ERR("buffer size too small\n");
118 WARN_ON(1);
119 return -ENOMEM;
120 }
121
122 s = skb_put(event->skb, len + 1);
123 strcpy(s, buf);
124
125 BH_DBG("added variable '%s'\n", s);
126
127 return 0;
128 }
129
130 static int button_hotplug_fill_event(struct bh_event *event)
131 {
132 char *s;
133 int ret;
134
135 ret = bh_event_add_var(event, 0, "HOME=%s", "/");
136 if (ret)
137 return ret;
138
139 ret = bh_event_add_var(event, 0, "PATH=%s",
140 "/sbin:/bin:/usr/sbin:/usr/bin");
141 if (ret)
142 return ret;
143
144 ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button");
145 if (ret)
146 return ret;
147
148 ret = bh_event_add_var(event, 0, "ACTION=%s", event->action);
149 if (ret)
150 return ret;
151
152 ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name);
153 if (ret)
154 return ret;
155
156 if (event->type == EV_SW) {
157 ret = bh_event_add_var(event, 0, "TYPE=%s", "switch");
158 if (ret)
159 return ret;
160 }
161
162 ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen);
163 if (ret)
164 return ret;
165
166 ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum());
167
168 return ret;
169 }
170
171 static void button_hotplug_work(struct work_struct *work)
172 {
173 struct bh_event *event = container_of(work, struct bh_event, work);
174 int ret = 0;
175
176 event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL);
177 if (!event->skb)
178 goto out_free_event;
179
180 ret = bh_event_add_var(event, 0, "%s@", event->action);
181 if (ret)
182 goto out_free_skb;
183
184 ret = button_hotplug_fill_event(event);
185 if (ret)
186 goto out_free_skb;
187
188 NETLINK_CB(event->skb).dst_group = 1;
189 broadcast_uevent(event->skb, 0, 1, GFP_KERNEL);
190
191 out_free_skb:
192 if (ret) {
193 BH_ERR("work error %d\n", ret);
194 kfree_skb(event->skb);
195 }
196 out_free_event:
197 kfree(event);
198 }
199
200 static int button_hotplug_create_event(const char *name, unsigned int type,
201 unsigned long seen, int pressed)
202 {
203 struct bh_event *event;
204
205 BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n",
206 name, seen, pressed);
207
208 event = kzalloc(sizeof(*event), GFP_KERNEL);
209 if (!event)
210 return -ENOMEM;
211
212 event->name = name;
213 event->type = type;
214 event->seen = seen;
215 event->action = pressed ? "pressed" : "released";
216
217 INIT_WORK(&event->work, (void *)(void *)button_hotplug_work);
218 schedule_work(&event->work);
219
220 return 0;
221 }
222
223 /* -------------------------------------------------------------------------*/
224
225 #ifdef CONFIG_HOTPLUG
226 static int button_get_index(unsigned int code)
227 {
228 int i;
229
230 for (i = 0; i < ARRAY_SIZE(button_map); i++)
231 if (button_map[i].code == code)
232 return i;
233
234 return -1;
235 }
236
237 static void button_hotplug_event(struct gpio_keys_button_data *data,
238 unsigned int type, unsigned int code, int value)
239 {
240 struct bh_priv *priv = &data->bh;
241 unsigned long seen = jiffies;
242 int btn;
243
244 BH_DBG("event type=%u, code=%u, value=%d\n", type, code, value);
245
246 if ((type != EV_KEY) && (type != EV_SW))
247 return;
248
249 btn = button_get_index(code);
250 if (btn < 0)
251 return;
252
253 button_hotplug_create_event(button_map[btn].name, type,
254 (seen - priv->seen) / HZ, value);
255 priv->seen = seen;
256 }
257 #else
258 static void button_hotplug_event(struct gpio_keys_button_data *data,
259 unsigned int type, unsigned int code, int value)
260 {
261 }
262 #endif /* CONFIG_HOTPLUG */
263
264 struct gpio_keys_polled_dev {
265 struct delayed_work work;
266
267 struct device *dev;
268 struct gpio_keys_platform_data *pdata;
269 struct gpio_keys_button_data data[0];
270 };
271
272 static int gpio_button_get_value(struct gpio_keys_button *button,
273 struct gpio_keys_button_data *bdata)
274 {
275 int val;
276
277 if (bdata->can_sleep)
278 val = !!gpio_get_value_cansleep(button->gpio);
279 else
280 val = !!gpio_get_value(button->gpio);
281
282 return val ^ button->active_low;
283 }
284
285 static void gpio_keys_polled_check_state(struct gpio_keys_button *button,
286 struct gpio_keys_button_data *bdata)
287 {
288 int state = gpio_button_get_value(button, bdata);
289
290 if (state != bdata->last_state) {
291 unsigned int type = button->type ?: EV_KEY;
292
293 if (bdata->count < bdata->threshold) {
294 bdata->count++;
295 return;
296 }
297
298 if ((bdata->last_state != -1) || (type == EV_SW))
299 button_hotplug_event(bdata, type, button->code, state);
300
301 bdata->last_state = state;
302 }
303
304 bdata->count = 0;
305 }
306
307 static void gpio_keys_polled_queue_work(struct gpio_keys_polled_dev *bdev)
308 {
309 struct gpio_keys_platform_data *pdata = bdev->pdata;
310 unsigned long delay = msecs_to_jiffies(pdata->poll_interval);
311
312 if (delay >= HZ)
313 delay = round_jiffies_relative(delay);
314 schedule_delayed_work(&bdev->work, delay);
315 }
316
317 static void gpio_keys_polled_poll(struct work_struct *work)
318 {
319 struct gpio_keys_polled_dev *bdev =
320 container_of(work, struct gpio_keys_polled_dev, work.work);
321 struct gpio_keys_platform_data *pdata = bdev->pdata;
322 int i;
323
324 for (i = 0; i < bdev->pdata->nbuttons; i++) {
325 struct gpio_keys_button_data *bdata = &bdev->data[i];
326 gpio_keys_polled_check_state(&pdata->buttons[i], bdata);
327 }
328 gpio_keys_polled_queue_work(bdev);
329 }
330
331 static void gpio_keys_polled_open(struct gpio_keys_polled_dev *bdev)
332 {
333 struct gpio_keys_platform_data *pdata = bdev->pdata;
334 int i;
335
336 if (pdata->enable)
337 pdata->enable(bdev->dev);
338
339 /* report initial state of the buttons */
340 for (i = 0; i < pdata->nbuttons; i++)
341 gpio_keys_polled_check_state(&pdata->buttons[i], &bdev->data[i]);
342
343 gpio_keys_polled_queue_work(bdev);
344 }
345
346 #ifdef CONFIG_OF
347 static struct gpio_keys_platform_data *
348 gpio_keys_polled_get_devtree_pdata(struct device *dev)
349 {
350 struct device_node *node, *pp;
351 struct gpio_keys_platform_data *pdata;
352 struct gpio_keys_button *button;
353 int error;
354 int nbuttons;
355 int i;
356
357 node = dev->of_node;
358 if (!node)
359 return NULL;
360
361 nbuttons = of_get_child_count(node);
362 if (nbuttons == 0)
363 return NULL;
364
365 pdata = kzalloc(sizeof(*pdata) + nbuttons * (sizeof *button),
366 GFP_KERNEL);
367 if (!pdata) {
368 error = -ENOMEM;
369 goto err_out;
370 }
371
372 pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
373 pdata->nbuttons = nbuttons;
374
375 pdata->rep = !!of_get_property(node, "autorepeat", NULL);
376 of_property_read_u32(node, "poll-interval", &pdata->poll_interval);
377
378 i = 0;
379 for_each_child_of_node(node, pp) {
380 enum of_gpio_flags flags;
381
382 if (!of_find_property(pp, "gpios", NULL)) {
383 pdata->nbuttons--;
384 dev_warn(dev, "Found button without gpios\n");
385 continue;
386 }
387
388 button = &pdata->buttons[i++];
389
390 button->gpio = of_get_gpio_flags(pp, 0, &flags);
391 button->active_low = flags & OF_GPIO_ACTIVE_LOW;
392
393 if (of_property_read_u32(pp, "linux,code", &button->code)) {
394 dev_err(dev, "Button without keycode: 0x%x\n",
395 button->gpio);
396 error = -EINVAL;
397 goto err_free_pdata;
398 }
399
400 button->desc = of_get_property(pp, "label", NULL);
401
402 if (of_property_read_u32(pp, "linux,input-type", &button->type))
403 button->type = EV_KEY;
404
405 button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
406
407 if (of_property_read_u32(pp, "debounce-interval",
408 &button->debounce_interval))
409 button->debounce_interval = 5;
410 }
411
412 if (pdata->nbuttons == 0) {
413 error = -EINVAL;
414 goto err_free_pdata;
415 }
416
417 return pdata;
418
419 err_free_pdata:
420 kfree(pdata);
421 err_out:
422 return ERR_PTR(error);
423 }
424
425 static struct of_device_id gpio_keys_polled_of_match[] = {
426 { .compatible = "gpio-keys-polled", },
427 { },
428 };
429 MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match);
430
431 #else
432
433 static inline struct gpio_keys_platform_data *
434 gpio_keys_polled_get_devtree_pdata(struct device *dev)
435 {
436 return NULL;
437 }
438 #endif
439
440 static void gpio_keys_polled_close(struct gpio_keys_polled_dev *bdev)
441 {
442 struct gpio_keys_platform_data *pdata = bdev->pdata;
443
444 cancel_delayed_work_sync(&bdev->work);
445
446 if (pdata->disable)
447 pdata->disable(bdev->dev);
448 }
449
450 static int gpio_keys_polled_probe(struct platform_device *pdev)
451 {
452 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
453 struct device *dev = &pdev->dev;
454 struct gpio_keys_polled_dev *bdev;
455 int error;
456 int i;
457
458 if (!pdata) {
459 pdata = gpio_keys_polled_get_devtree_pdata(dev);
460 if (IS_ERR(pdata))
461 return PTR_ERR(pdata);
462 if (!pdata) {
463 dev_err(dev, "missing platform data\n");
464 return -EINVAL;
465 }
466 }
467
468 if (!pdata->poll_interval) {
469 dev_err(dev, "missing poll_interval value\n");
470 error = -EINVAL;
471 goto err_free_pdata;
472 }
473
474 bdev = kzalloc(sizeof(struct gpio_keys_polled_dev) +
475 pdata->nbuttons * sizeof(struct gpio_keys_button_data),
476 GFP_KERNEL);
477 if (!bdev) {
478 dev_err(dev, "no memory for private data\n");
479 return -ENOMEM;
480 }
481
482 for (i = 0; i < pdata->nbuttons; i++) {
483 struct gpio_keys_button *button = &pdata->buttons[i];
484 struct gpio_keys_button_data *bdata = &bdev->data[i];
485 unsigned int gpio = button->gpio;
486
487 if (button->wakeup) {
488 dev_err(dev, DRV_NAME " does not support wakeup\n");
489 error = -EINVAL;
490 goto err_free_gpio;
491 }
492
493 error = gpio_request(gpio,
494 button->desc ? button->desc : DRV_NAME);
495 if (error) {
496 dev_err(dev, "unable to claim gpio %u, err=%d\n",
497 gpio, error);
498 goto err_free_gpio;
499 }
500
501 error = gpio_direction_input(gpio);
502 if (error) {
503 dev_err(dev,
504 "unable to set direction on gpio %u, err=%d\n",
505 gpio, error);
506 goto err_free_gpio;
507 }
508
509 bdata->can_sleep = gpio_cansleep(gpio);
510 bdata->last_state = -1;
511 bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
512 pdata->poll_interval);
513 }
514
515 bdev->dev = &pdev->dev;
516 bdev->pdata = pdata;
517 platform_set_drvdata(pdev, bdev);
518
519 INIT_DELAYED_WORK(&bdev->work, gpio_keys_polled_poll);
520
521 gpio_keys_polled_open(bdev);
522
523 return 0;
524
525 err_free_gpio:
526 while (--i >= 0)
527 gpio_free(pdata->buttons[i].gpio);
528
529 kfree(bdev);
530 platform_set_drvdata(pdev, NULL);
531
532 err_free_pdata:
533 /* If we have no platform_data, we allocated pdata dynamically. */
534 if (!dev_get_platdata(&pdev->dev))
535 kfree(pdata);
536
537 return error;
538 }
539
540 static int gpio_keys_polled_remove(struct platform_device *pdev)
541 {
542 struct gpio_keys_polled_dev *bdev = platform_get_drvdata(pdev);
543 struct gpio_keys_platform_data *pdata = bdev->pdata;
544 int i = pdata->nbuttons;
545
546 gpio_keys_polled_close(bdev);
547
548 while (--i >= 0)
549 gpio_free(pdata->buttons[i].gpio);
550
551 kfree(bdev);
552 platform_set_drvdata(pdev, NULL);
553
554 return 0;
555 }
556
557 static struct platform_driver gpio_keys_polled_driver = {
558 .probe = gpio_keys_polled_probe,
559 .remove = gpio_keys_polled_remove,
560 .driver = {
561 .name = DRV_NAME,
562 .owner = THIS_MODULE,
563 .of_match_table = of_match_ptr(gpio_keys_polled_of_match),
564 },
565 };
566
567 static int __init gpio_keys_polled_init(void)
568 {
569 return platform_driver_register(&gpio_keys_polled_driver);
570 }
571
572 static void __exit gpio_keys_polled_exit(void)
573 {
574 platform_driver_unregister(&gpio_keys_polled_driver);
575 }
576
577 module_init(gpio_keys_polled_init);
578 module_exit(gpio_keys_polled_exit);
579
580 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
581 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
582 MODULE_DESCRIPTION("Polled GPIO Buttons hotplug driver");
583 MODULE_LICENSE("GPL v2");
584 MODULE_ALIAS("platform:" DRV_NAME);