add chaos_calmer branch
[15.05/openwrt.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/interrupt.h>
27 #include <linux/platform_device.h>
28 #include <linux/of_gpio.h>
29 #include <linux/gpio_keys.h>
30
31 #define DRV_NAME        "gpio-keys"
32
33 #define BH_SKB_SIZE     2048
34
35 #define PFX     DRV_NAME ": "
36
37 #undef BH_DEBUG
38
39 #ifdef BH_DEBUG
40 #define BH_DBG(fmt, args...) printk(KERN_DEBUG "%s: " fmt, DRV_NAME, ##args )
41 #else
42 #define BH_DBG(fmt, args...) do {} while (0)
43 #endif
44
45 #define BH_ERR(fmt, args...) printk(KERN_ERR "%s: " fmt, DRV_NAME, ##args )
46
47 struct bh_priv {
48         unsigned long           seen;
49 };
50
51 struct bh_event {
52         const char              *name;
53         unsigned int            type;
54         char                    *action;
55         unsigned long           seen;
56
57         struct sk_buff          *skb;
58         struct work_struct      work;
59 };
60
61 struct bh_map {
62         unsigned int    code;
63         const char      *name;
64 };
65
66 struct gpio_keys_button_data {
67         struct delayed_work work;
68         struct bh_priv bh;
69         int last_state;
70         int count;
71         int threshold;
72         int can_sleep;
73         struct gpio_keys_button *b;
74 };
75
76 extern u64 uevent_next_seqnum(void);
77
78 #define BH_MAP(_code, _name)            \
79         {                               \
80                 .code = (_code),        \
81                 .name = (_name),        \
82         }
83
84 static struct bh_map button_map[] = {
85         BH_MAP(BTN_0,           "BTN_0"),
86         BH_MAP(BTN_1,           "BTN_1"),
87         BH_MAP(BTN_2,           "BTN_2"),
88         BH_MAP(BTN_3,           "BTN_3"),
89         BH_MAP(BTN_4,           "BTN_4"),
90         BH_MAP(BTN_5,           "BTN_5"),
91         BH_MAP(BTN_6,           "BTN_6"),
92         BH_MAP(BTN_7,           "BTN_7"),
93         BH_MAP(BTN_8,           "BTN_8"),
94         BH_MAP(BTN_9,           "BTN_9"),
95         BH_MAP(KEY_POWER,       "power"),
96         BH_MAP(KEY_RESTART,     "reset"),
97         BH_MAP(KEY_RFKILL,      "rfkill"),
98         BH_MAP(KEY_WPS_BUTTON,  "wps"),
99         BH_MAP(KEY_WIMAX,       "wwan"),
100 };
101
102 /* -------------------------------------------------------------------------*/
103
104 static int bh_event_add_var(struct bh_event *event, int argv,
105                 const char *format, ...)
106 {
107         static char buf[128];
108         char *s;
109         va_list args;
110         int len;
111
112         if (argv)
113                 return 0;
114
115         va_start(args, format);
116         len = vsnprintf(buf, sizeof(buf), format, args);
117         va_end(args);
118
119         if (len >= sizeof(buf)) {
120                 BH_ERR("buffer size too small\n");
121                 WARN_ON(1);
122                 return -ENOMEM;
123         }
124
125         s = skb_put(event->skb, len + 1);
126         strcpy(s, buf);
127
128         BH_DBG("added variable '%s'\n", s);
129
130         return 0;
131 }
132
133 static int button_hotplug_fill_event(struct bh_event *event)
134 {
135         int ret;
136
137         ret = bh_event_add_var(event, 0, "HOME=%s", "/");
138         if (ret)
139                 return ret;
140
141         ret = bh_event_add_var(event, 0, "PATH=%s",
142                                         "/sbin:/bin:/usr/sbin:/usr/bin");
143         if (ret)
144                 return ret;
145
146         ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button");
147         if (ret)
148                 return ret;
149
150         ret = bh_event_add_var(event, 0, "ACTION=%s", event->action);
151         if (ret)
152                 return ret;
153
154         ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name);
155         if (ret)
156                 return ret;
157
158         if (event->type == EV_SW) {
159                 ret = bh_event_add_var(event, 0, "TYPE=%s", "switch");
160                 if (ret)
161                         return ret;
162         }
163
164         ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen);
165         if (ret)
166                 return ret;
167
168         ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum());
169
170         return ret;
171 }
172
173 static void button_hotplug_work(struct work_struct *work)
174 {
175         struct bh_event *event = container_of(work, struct bh_event, work);
176         int ret = 0;
177
178         event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL);
179         if (!event->skb)
180                 goto out_free_event;
181
182         ret = bh_event_add_var(event, 0, "%s@", event->action);
183         if (ret)
184                 goto out_free_skb;
185
186         ret = button_hotplug_fill_event(event);
187         if (ret)
188                 goto out_free_skb;
189
190         NETLINK_CB(event->skb).dst_group = 1;
191         broadcast_uevent(event->skb, 0, 1, GFP_KERNEL);
192
193  out_free_skb:
194         if (ret) {
195                 BH_ERR("work error %d\n", ret);
196                 kfree_skb(event->skb);
197         }
198  out_free_event:
199         kfree(event);
200 }
201
202 static int button_hotplug_create_event(const char *name, unsigned int type,
203                 unsigned long seen, int pressed)
204 {
205         struct bh_event *event;
206
207         BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n",
208                 name, seen, pressed);
209
210         event = kzalloc(sizeof(*event), GFP_KERNEL);
211         if (!event)
212                 return -ENOMEM;
213
214         event->name = name;
215         event->type = type;
216         event->seen = seen;
217         event->action = pressed ? "pressed" : "released";
218
219         INIT_WORK(&event->work, (void *)(void *)button_hotplug_work);
220         schedule_work(&event->work);
221
222         return 0;
223 }
224
225 /* -------------------------------------------------------------------------*/
226
227 static int button_get_index(unsigned int code)
228 {
229         int i;
230
231         for (i = 0; i < ARRAY_SIZE(button_map); i++)
232                 if (button_map[i].code == code)
233                         return i;
234
235         return -1;
236 }
237
238 static void button_hotplug_event(struct gpio_keys_button_data *data,
239                            unsigned int type, int value)
240 {
241         struct bh_priv *priv = &data->bh;
242         unsigned long seen = jiffies;
243         int btn;
244
245         BH_DBG("event type=%u, code=%u, value=%d\n", type, data->b->code, value);
246
247         if ((type != EV_KEY) && (type != EV_SW))
248                 return;
249
250         btn = button_get_index(data->b->code);
251         if (btn < 0)
252                 return;
253
254         button_hotplug_create_event(button_map[btn].name, type,
255                         (seen - priv->seen) / HZ, value);
256         priv->seen = seen;
257 }
258
259 struct gpio_keys_button_dev {
260         int polled;
261         struct delayed_work work;
262
263         struct device *dev;
264         struct gpio_keys_platform_data *pdata;
265         struct gpio_keys_button_data data[0];
266 };
267
268 static int gpio_button_get_value(struct gpio_keys_button_data *bdata)
269 {
270         int val;
271
272         if (bdata->can_sleep)
273                 val = !!gpio_get_value_cansleep(bdata->b->gpio);
274         else
275                 val = !!gpio_get_value(bdata->b->gpio);
276
277         return val ^ bdata->b->active_low;
278 }
279
280 static void gpio_keys_polled_check_state(struct gpio_keys_button_data *bdata)
281 {
282         int state = gpio_button_get_value(bdata);
283
284         if (state != bdata->last_state) {
285                 unsigned int type = bdata->b->type ?: EV_KEY;
286
287                 if (bdata->count < bdata->threshold) {
288                         bdata->count++;
289                         return;
290                 }
291
292                 if ((bdata->last_state != -1) || (type == EV_SW))
293                         button_hotplug_event(bdata, type, state);
294
295                 bdata->last_state = state;
296         }
297
298         bdata->count = 0;
299 }
300
301 static void gpio_keys_polled_queue_work(struct gpio_keys_button_dev *bdev)
302 {
303         struct gpio_keys_platform_data *pdata = bdev->pdata;
304         unsigned long delay = msecs_to_jiffies(pdata->poll_interval);
305
306         if (delay >= HZ)
307                 delay = round_jiffies_relative(delay);
308         schedule_delayed_work(&bdev->work, delay);
309 }
310
311 static void gpio_keys_polled_poll(struct work_struct *work)
312 {
313         struct gpio_keys_button_dev *bdev =
314                 container_of(work, struct gpio_keys_button_dev, work.work);
315         int i;
316
317         for (i = 0; i < bdev->pdata->nbuttons; i++) {
318                 struct gpio_keys_button_data *bdata = &bdev->data[i];
319                 gpio_keys_polled_check_state(bdata);
320         }
321         gpio_keys_polled_queue_work(bdev);
322 }
323
324 static void gpio_keys_polled_close(struct gpio_keys_button_dev *bdev)
325 {
326         struct gpio_keys_platform_data *pdata = bdev->pdata;
327
328         cancel_delayed_work_sync(&bdev->work);
329
330         if (pdata->disable)
331                 pdata->disable(bdev->dev);
332 }
333
334 static irqreturn_t button_handle_irq(int irq, void *_bdata)
335 {
336         struct gpio_keys_button_data *bdata = (struct gpio_keys_button_data *) _bdata;
337
338         button_hotplug_event(bdata, bdata->b->type ?: EV_KEY, gpio_button_get_value(bdata));
339
340         return IRQ_HANDLED;
341 }
342
343 #ifdef CONFIG_OF
344 static struct gpio_keys_platform_data *
345 gpio_keys_get_devtree_pdata(struct device *dev)
346 {
347         struct device_node *node, *pp;
348         struct gpio_keys_platform_data *pdata;
349         struct gpio_keys_button *button;
350         int error;
351         int nbuttons;
352         int i = 0;
353
354         node = dev->of_node;
355         if (!node)
356                 return NULL;
357
358         nbuttons = of_get_child_count(node);
359         if (nbuttons == 0)
360                 return NULL;
361
362         pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * (sizeof *button),
363                 GFP_KERNEL);
364         if (!pdata) {
365                 error = -ENOMEM;
366                 goto err_out;
367         }
368
369         pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
370         pdata->nbuttons = nbuttons;
371
372         pdata->rep = !!of_get_property(node, "autorepeat", NULL);
373         of_property_read_u32(node, "poll-interval", &pdata->poll_interval);
374
375         for_each_child_of_node(node, pp) {
376                 enum of_gpio_flags flags;
377
378                 if (!of_find_property(pp, "gpios", NULL)) {
379                         pdata->nbuttons--;
380                         dev_warn(dev, "Found button without gpios\n");
381                         continue;
382                 }
383
384                 button = &pdata->buttons[i++];
385
386                 button->gpio = of_get_gpio_flags(pp, 0, &flags);
387                 button->active_low = flags & OF_GPIO_ACTIVE_LOW;
388
389                 if (of_property_read_u32(pp, "linux,code", &button->code)) {
390                         dev_err(dev, "Button without keycode: 0x%x\n",
391                                 button->gpio);
392                         error = -EINVAL;
393                         goto err_out;
394                 }
395
396                 button->desc = of_get_property(pp, "label", NULL);
397
398                 if (of_property_read_u32(pp, "linux,input-type", &button->type))
399                         button->type = EV_KEY;
400
401                 button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
402
403                 if (of_property_read_u32(pp, "debounce-interval",
404                                         &button->debounce_interval))
405                         button->debounce_interval = 5;
406         }
407
408         if (pdata->nbuttons == 0) {
409                 error = -EINVAL;
410                 goto err_out;
411         }
412
413         return pdata;
414
415 err_out:
416         return ERR_PTR(error);
417 }
418
419 static struct of_device_id gpio_keys_of_match[] = {
420         { .compatible = "gpio-keys", },
421         { },
422 };
423 MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
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_get_devtree_pdata(struct device *dev)
435 {
436         return NULL;
437 }
438 #endif
439
440 static int gpio_keys_button_probe(struct platform_device *pdev,
441                 struct gpio_keys_button_dev **_bdev, int polled)
442 {
443         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
444         struct device *dev = &pdev->dev;
445         struct gpio_keys_button_dev *bdev;
446         struct gpio_keys_button *buttons;
447         int error;
448         int i;
449
450         if (!pdata) {
451                 pdata = gpio_keys_get_devtree_pdata(dev);
452                 if (IS_ERR(pdata))
453                         return PTR_ERR(pdata);
454                 if (!pdata) {
455                         dev_err(dev, "missing platform data\n");
456                         return -EINVAL;
457                 }
458                 pdev->dev.platform_data = pdata;
459         }
460
461         if (polled && !pdata->poll_interval) {
462                 dev_err(dev, "missing poll_interval value\n");
463                 return -EINVAL;
464         }
465
466         buttons = devm_kzalloc(dev, pdata->nbuttons * sizeof(struct gpio_keys_button),
467                        GFP_KERNEL);
468         if (!buttons) {
469                 dev_err(dev, "no memory for button data\n");
470                 return -ENOMEM;
471         }
472         memcpy(buttons, pdata->buttons, pdata->nbuttons * sizeof(struct gpio_keys_button));
473
474         bdev = devm_kzalloc(dev, sizeof(struct gpio_keys_button_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         bdev->polled = polled;
483
484         for (i = 0; i < pdata->nbuttons; i++) {
485                 struct gpio_keys_button *button = &buttons[i];
486                 struct gpio_keys_button_data *bdata = &bdev->data[i];
487                 unsigned int gpio = button->gpio;
488
489                 if (button->wakeup) {
490                         dev_err(dev, DRV_NAME "does not support wakeup\n");
491                         return -EINVAL;
492                 }
493
494                 error = devm_gpio_request(dev, gpio,
495                                      button->desc ? button->desc : DRV_NAME);
496                 if (error) {
497                         dev_err(dev, "unable to claim gpio %u, err=%d\n",
498                                 gpio, error);
499                         return error;
500                 }
501
502                 error = gpio_direction_input(gpio);
503                 if (error) {
504                         dev_err(dev,
505                                 "unable to set direction on gpio %u, err=%d\n",
506                                 gpio, error);
507                         return error;
508                 }
509
510                 bdata->can_sleep = gpio_cansleep(gpio);
511                 bdata->last_state = -1;
512
513                 if (bdev->polled)
514                         bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
515                                                 pdata->poll_interval);
516                 else
517                         bdata->threshold = 1;
518
519                 bdata->b = &pdata->buttons[i];
520         }
521
522         bdev->dev = &pdev->dev;
523         bdev->pdata = pdata;
524         platform_set_drvdata(pdev, bdev);
525
526         *_bdev = bdev;
527
528         return 0;
529 }
530
531 static int gpio_keys_probe(struct platform_device *pdev)
532 {
533         struct gpio_keys_platform_data *pdata;
534         struct gpio_keys_button_dev *bdev;
535         int ret, i;
536
537
538         ret = gpio_keys_button_probe(pdev, &bdev, 0);
539
540         if (ret)
541                 return ret;
542
543         pdata = pdev->dev.platform_data;
544         for (i = 0; i < pdata->nbuttons; i++) {
545                 struct gpio_keys_button *button = &pdata->buttons[i];
546                 struct gpio_keys_button_data *bdata = &bdev->data[i];
547
548                 if (bdata->can_sleep) {
549                         dev_err(&pdev->dev, "skipping gpio:%d, it can sleep\n", button->gpio);
550                         continue;
551                 }
552                 if (!button->irq)
553                         button->irq = gpio_to_irq(button->gpio);
554                 if (button->irq < 0) {
555                         dev_err(&pdev->dev, "failed to get irq for gpio:%d\n", button->gpio);
556                         continue;
557                 }
558                 ret = devm_request_irq(&pdev->dev, button->irq, button_handle_irq,
559                                         IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
560                                         dev_name(&pdev->dev), bdata);
561                 if (ret)
562                         dev_err(&pdev->dev, "failed to request irq:%d for gpio:%d\n", button->irq, button->gpio);
563                 else
564                         dev_dbg(&pdev->dev, "gpio:%d has irq:%d\n", button->gpio, button->irq);
565
566                 if (bdata->b->type == EV_SW)
567                         button_hotplug_event(bdata, EV_SW, gpio_button_get_value(bdata));
568         }
569
570         return 0;
571 }
572
573 static int gpio_keys_polled_probe(struct platform_device *pdev)
574 {
575         struct gpio_keys_platform_data *pdata;
576         struct gpio_keys_button_dev *bdev;
577         int ret;
578         int i;
579
580         ret = gpio_keys_button_probe(pdev, &bdev, 1);
581
582         if (ret)
583                 return ret;
584
585         INIT_DELAYED_WORK(&bdev->work, gpio_keys_polled_poll);
586
587         pdata = bdev->pdata;
588
589         if (pdata->enable)
590                 pdata->enable(bdev->dev);
591
592         for (i = 0; i < pdata->nbuttons; i++)
593                 gpio_keys_polled_check_state(&bdev->data[i]);
594
595         gpio_keys_polled_queue_work(bdev);
596
597         return ret;
598 }
599
600 static int gpio_keys_remove(struct platform_device *pdev)
601 {
602         struct gpio_keys_button_dev *bdev = platform_get_drvdata(pdev);
603
604         platform_set_drvdata(pdev, NULL);
605
606         if (bdev->polled)
607                 gpio_keys_polled_close(bdev);
608
609         return 0;
610 }
611
612 static struct platform_driver gpio_keys_driver = {
613         .probe  = gpio_keys_probe,
614         .remove = gpio_keys_remove,
615         .driver = {
616                 .name   = "gpio-keys",
617                 .owner  = THIS_MODULE,
618                 .of_match_table = of_match_ptr(gpio_keys_of_match),
619         },
620 };
621
622 static struct platform_driver gpio_keys_polled_driver = {
623         .probe  = gpio_keys_polled_probe,
624         .remove = gpio_keys_remove,
625         .driver = {
626                 .name   = "gpio-keys-polled",
627                 .owner  = THIS_MODULE,
628                 .of_match_table = of_match_ptr(gpio_keys_polled_of_match),
629         },
630 };
631
632 static int __init gpio_button_init(void)
633 {
634         int ret;
635
636         ret = platform_driver_register(&gpio_keys_driver);
637         if (ret)
638                 return ret;
639
640         ret = platform_driver_register(&gpio_keys_polled_driver);
641         if (ret)
642                 platform_driver_unregister(&gpio_keys_driver);
643
644         return ret;
645 }
646
647 static void __exit gpio_button_exit(void)
648 {
649         platform_driver_unregister(&gpio_keys_driver);
650         platform_driver_unregister(&gpio_keys_polled_driver);
651 }
652
653 module_init(gpio_button_init);
654 module_exit(gpio_button_exit);
655
656 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
657 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
658 MODULE_DESCRIPTION("Polled GPIO Buttons hotplug driver");
659 MODULE_LICENSE("GPL v2");
660 MODULE_ALIAS("platform:" DRV_NAME);