treewide: fix replace nbd@openwrt.org with nbd@nbd.name
[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@nbd.name>
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@nbd.name>
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 __printf(3, 4)
105 int bh_event_add_var(struct bh_event *event, int argv, 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                 WARN(1, "buffer size too small");
121                 return -ENOMEM;
122         }
123
124         s = skb_put(event->skb, len + 1);
125         strcpy(s, buf);
126
127         BH_DBG("added variable '%s'\n", s);
128
129         return 0;
130 }
131
132 static int button_hotplug_fill_event(struct bh_event *event)
133 {
134         int ret;
135
136         ret = bh_event_add_var(event, 0, "HOME=%s", "/");
137         if (ret)
138                 return ret;
139
140         ret = bh_event_add_var(event, 0, "PATH=%s",
141                                         "/sbin:/bin:/usr/sbin:/usr/bin");
142         if (ret)
143                 return ret;
144
145         ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button");
146         if (ret)
147                 return ret;
148
149         ret = bh_event_add_var(event, 0, "ACTION=%s", event->action);
150         if (ret)
151                 return ret;
152
153         ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name);
154         if (ret)
155                 return ret;
156
157         if (event->type == EV_SW) {
158                 ret = bh_event_add_var(event, 0, "TYPE=%s", "switch");
159                 if (ret)
160                         return ret;
161         }
162
163         ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen);
164         if (ret)
165                 return ret;
166
167         ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum());
168
169         return ret;
170 }
171
172 static void button_hotplug_work(struct work_struct *work)
173 {
174         struct bh_event *event = container_of(work, struct bh_event, work);
175         int ret = 0;
176
177         event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL);
178         if (!event->skb)
179                 goto out_free_event;
180
181         ret = bh_event_add_var(event, 0, "%s@", event->action);
182         if (ret)
183                 goto out_free_skb;
184
185         ret = button_hotplug_fill_event(event);
186         if (ret)
187                 goto out_free_skb;
188
189         NETLINK_CB(event->skb).dst_group = 1;
190         broadcast_uevent(event->skb, 0, 1, GFP_KERNEL);
191
192  out_free_skb:
193         if (ret) {
194                 BH_ERR("work error %d\n", ret);
195                 kfree_skb(event->skb);
196         }
197  out_free_event:
198         kfree(event);
199 }
200
201 static int button_hotplug_create_event(const char *name, unsigned int type,
202                 unsigned long seen, int pressed)
203 {
204         struct bh_event *event;
205
206         BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n",
207                 name, seen, pressed);
208
209         event = kzalloc(sizeof(*event), GFP_KERNEL);
210         if (!event)
211                 return -ENOMEM;
212
213         event->name = name;
214         event->type = type;
215         event->seen = seen;
216         event->action = pressed ? "pressed" : "released";
217
218         INIT_WORK(&event->work, (void *)(void *)button_hotplug_work);
219         schedule_work(&event->work);
220
221         return 0;
222 }
223
224 /* -------------------------------------------------------------------------*/
225
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, 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, data->b->code, value);
245
246         if ((type != EV_KEY) && (type != EV_SW))
247                 return;
248
249         btn = button_get_index(data->b->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
258 struct gpio_keys_button_dev {
259         int polled;
260         struct delayed_work work;
261
262         struct device *dev;
263         struct gpio_keys_platform_data *pdata;
264         struct gpio_keys_button_data data[0];
265 };
266
267 static int gpio_button_get_value(struct gpio_keys_button_data *bdata)
268 {
269         int val;
270
271         if (bdata->can_sleep)
272                 val = !!gpio_get_value_cansleep(bdata->b->gpio);
273         else
274                 val = !!gpio_get_value(bdata->b->gpio);
275
276         return val ^ bdata->b->active_low;
277 }
278
279 static void gpio_keys_polled_check_state(struct gpio_keys_button_data *bdata)
280 {
281         int state = gpio_button_get_value(bdata);
282
283         if (state != bdata->last_state) {
284                 unsigned int type = bdata->b->type ?: EV_KEY;
285
286                 if (bdata->count < bdata->threshold) {
287                         bdata->count++;
288                         return;
289                 }
290
291                 if ((bdata->last_state != -1) || (type == EV_SW))
292                         button_hotplug_event(bdata, type, state);
293
294                 bdata->last_state = state;
295         }
296
297         bdata->count = 0;
298 }
299
300 static void gpio_keys_polled_queue_work(struct gpio_keys_button_dev *bdev)
301 {
302         struct gpio_keys_platform_data *pdata = bdev->pdata;
303         unsigned long delay = msecs_to_jiffies(pdata->poll_interval);
304
305         if (delay >= HZ)
306                 delay = round_jiffies_relative(delay);
307         schedule_delayed_work(&bdev->work, delay);
308 }
309
310 static void gpio_keys_polled_poll(struct work_struct *work)
311 {
312         struct gpio_keys_button_dev *bdev =
313                 container_of(work, struct gpio_keys_button_dev, work.work);
314         int i;
315
316         for (i = 0; i < bdev->pdata->nbuttons; i++) {
317                 struct gpio_keys_button_data *bdata = &bdev->data[i];
318                 gpio_keys_polled_check_state(bdata);
319         }
320         gpio_keys_polled_queue_work(bdev);
321 }
322
323 static void gpio_keys_polled_close(struct gpio_keys_button_dev *bdev)
324 {
325         struct gpio_keys_platform_data *pdata = bdev->pdata;
326
327         cancel_delayed_work_sync(&bdev->work);
328
329         if (pdata->disable)
330                 pdata->disable(bdev->dev);
331 }
332
333 static irqreturn_t button_handle_irq(int irq, void *_bdata)
334 {
335         struct gpio_keys_button_data *bdata = (struct gpio_keys_button_data *) _bdata;
336
337         button_hotplug_event(bdata, bdata->b->type ?: EV_KEY, gpio_button_get_value(bdata));
338
339         return IRQ_HANDLED;
340 }
341
342 #ifdef CONFIG_OF
343 static struct gpio_keys_platform_data *
344 gpio_keys_get_devtree_pdata(struct device *dev)
345 {
346         struct device_node *node, *pp;
347         struct gpio_keys_platform_data *pdata;
348         struct gpio_keys_button *button;
349         int error;
350         int nbuttons;
351         int i = 0;
352
353         node = dev->of_node;
354         if (!node)
355                 return NULL;
356
357         nbuttons = of_get_child_count(node);
358         if (nbuttons == 0)
359                 return NULL;
360
361         pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * (sizeof *button),
362                 GFP_KERNEL);
363         if (!pdata) {
364                 error = -ENOMEM;
365                 goto err_out;
366         }
367
368         pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
369         pdata->nbuttons = nbuttons;
370
371         pdata->rep = !!of_get_property(node, "autorepeat", NULL);
372         of_property_read_u32(node, "poll-interval", &pdata->poll_interval);
373
374         for_each_child_of_node(node, pp) {
375                 enum of_gpio_flags flags;
376
377                 if (!of_find_property(pp, "gpios", NULL)) {
378                         pdata->nbuttons--;
379                         dev_warn(dev, "Found button without gpios\n");
380                         continue;
381                 }
382
383                 button = &pdata->buttons[i++];
384
385                 button->gpio = of_get_gpio_flags(pp, 0, &flags);
386                 if (button->gpio < 0) {
387                         error = button->gpio;
388                         if (error != -ENOENT) {
389                                 if (error != -EPROBE_DEFER)
390                                         dev_err(dev,
391                                                 "Failed to get gpio flags, error: %d\n",
392                                                 error);
393                                 return ERR_PTR(error);
394                         }
395                 } else {
396                         button->active_low = flags & OF_GPIO_ACTIVE_LOW;
397                 }
398
399                 if (of_property_read_u32(pp, "linux,code", &button->code)) {
400                         dev_err(dev, "Button without keycode: 0x%x\n",
401                                 button->gpio);
402                         error = -EINVAL;
403                         goto err_out;
404                 }
405
406                 button->desc = of_get_property(pp, "label", NULL);
407
408                 if (of_property_read_u32(pp, "linux,input-type", &button->type))
409                         button->type = EV_KEY;
410
411                 button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
412
413                 if (of_property_read_u32(pp, "debounce-interval",
414                                         &button->debounce_interval))
415                         button->debounce_interval = 5;
416         }
417
418         if (pdata->nbuttons == 0) {
419                 error = -EINVAL;
420                 goto err_out;
421         }
422
423         return pdata;
424
425 err_out:
426         return ERR_PTR(error);
427 }
428
429 static struct of_device_id gpio_keys_of_match[] = {
430         { .compatible = "gpio-keys", },
431         { },
432 };
433 MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
434
435 static struct of_device_id gpio_keys_polled_of_match[] = {
436         { .compatible = "gpio-keys-polled", },
437         { },
438 };
439 MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match);
440
441 #else
442
443 static inline struct gpio_keys_platform_data *
444 gpio_keys_get_devtree_pdata(struct device *dev)
445 {
446         return NULL;
447 }
448 #endif
449
450 static int gpio_keys_button_probe(struct platform_device *pdev,
451                 struct gpio_keys_button_dev **_bdev, int polled)
452 {
453         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
454         struct device *dev = &pdev->dev;
455         struct gpio_keys_button_dev *bdev;
456         struct gpio_keys_button *buttons;
457         int error;
458         int i;
459
460         if (!pdata) {
461                 pdata = gpio_keys_get_devtree_pdata(dev);
462                 if (IS_ERR(pdata))
463                         return PTR_ERR(pdata);
464                 if (!pdata) {
465                         dev_err(dev, "missing platform data\n");
466                         return -EINVAL;
467                 }
468                 pdev->dev.platform_data = pdata;
469         }
470
471         if (polled && !pdata->poll_interval) {
472                 dev_err(dev, "missing poll_interval value\n");
473                 return -EINVAL;
474         }
475
476         buttons = devm_kzalloc(dev, pdata->nbuttons * sizeof(struct gpio_keys_button),
477                        GFP_KERNEL);
478         if (!buttons) {
479                 dev_err(dev, "no memory for button data\n");
480                 return -ENOMEM;
481         }
482         memcpy(buttons, pdata->buttons, pdata->nbuttons * sizeof(struct gpio_keys_button));
483
484         bdev = devm_kzalloc(dev, sizeof(struct gpio_keys_button_dev) +
485                        pdata->nbuttons * sizeof(struct gpio_keys_button_data),
486                        GFP_KERNEL);
487         if (!bdev) {
488                 dev_err(dev, "no memory for private data\n");
489                 return -ENOMEM;
490         }
491
492         bdev->polled = polled;
493
494         for (i = 0; i < pdata->nbuttons; i++) {
495                 struct gpio_keys_button *button = &buttons[i];
496                 struct gpio_keys_button_data *bdata = &bdev->data[i];
497                 unsigned int gpio = button->gpio;
498
499                 if (button->wakeup) {
500                         dev_err(dev, DRV_NAME "does not support wakeup\n");
501                         return -EINVAL;
502                 }
503
504                 error = devm_gpio_request(dev, gpio,
505                                      button->desc ? button->desc : DRV_NAME);
506                 if (error) {
507                         dev_err(dev, "unable to claim gpio %u, err=%d\n",
508                                 gpio, error);
509                         return error;
510                 }
511
512                 error = gpio_direction_input(gpio);
513                 if (error) {
514                         dev_err(dev,
515                                 "unable to set direction on gpio %u, err=%d\n",
516                                 gpio, error);
517                         return error;
518                 }
519
520                 bdata->can_sleep = gpio_cansleep(gpio);
521                 bdata->last_state = -1;
522
523                 if (bdev->polled)
524                         bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
525                                                 pdata->poll_interval);
526                 else
527                         bdata->threshold = 1;
528
529                 bdata->b = &pdata->buttons[i];
530         }
531
532         bdev->dev = &pdev->dev;
533         bdev->pdata = pdata;
534         platform_set_drvdata(pdev, bdev);
535
536         *_bdev = bdev;
537
538         return 0;
539 }
540
541 static int gpio_keys_probe(struct platform_device *pdev)
542 {
543         struct gpio_keys_platform_data *pdata;
544         struct gpio_keys_button_dev *bdev;
545         int ret, i;
546
547
548         ret = gpio_keys_button_probe(pdev, &bdev, 0);
549
550         if (ret)
551                 return ret;
552
553         pdata = pdev->dev.platform_data;
554         for (i = 0; i < pdata->nbuttons; i++) {
555                 struct gpio_keys_button *button = &pdata->buttons[i];
556                 struct gpio_keys_button_data *bdata = &bdev->data[i];
557
558                 if (!button->irq)
559                         button->irq = gpio_to_irq(button->gpio);
560                 if (button->irq < 0) {
561                         dev_err(&pdev->dev, "failed to get irq for gpio:%d\n", button->gpio);
562                         continue;
563                 }
564
565                 ret = devm_request_threaded_irq(&pdev->dev, button->irq, NULL, button_handle_irq,
566                                                 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
567                                                 dev_name(&pdev->dev), bdata);
568                 if (ret < 0)
569                         dev_err(&pdev->dev, "failed to request irq:%d for gpio:%d\n", button->irq, button->gpio);
570                 else
571                         dev_dbg(&pdev->dev, "gpio:%d has irq:%d\n", button->gpio, button->irq);
572
573                 if (bdata->b->type == EV_SW)
574                         button_hotplug_event(bdata, EV_SW, gpio_button_get_value(bdata));
575         }
576
577         return 0;
578 }
579
580 static int gpio_keys_polled_probe(struct platform_device *pdev)
581 {
582         struct gpio_keys_platform_data *pdata;
583         struct gpio_keys_button_dev *bdev;
584         int ret;
585         int i;
586
587         ret = gpio_keys_button_probe(pdev, &bdev, 1);
588
589         if (ret)
590                 return ret;
591
592         INIT_DELAYED_WORK(&bdev->work, gpio_keys_polled_poll);
593
594         pdata = bdev->pdata;
595
596         if (pdata->enable)
597                 pdata->enable(bdev->dev);
598
599         for (i = 0; i < pdata->nbuttons; i++)
600                 gpio_keys_polled_check_state(&bdev->data[i]);
601
602         gpio_keys_polled_queue_work(bdev);
603
604         return ret;
605 }
606
607 static int gpio_keys_remove(struct platform_device *pdev)
608 {
609         struct gpio_keys_button_dev *bdev = platform_get_drvdata(pdev);
610
611         platform_set_drvdata(pdev, NULL);
612
613         if (bdev->polled)
614                 gpio_keys_polled_close(bdev);
615
616         return 0;
617 }
618
619 static struct platform_driver gpio_keys_driver = {
620         .probe  = gpio_keys_probe,
621         .remove = gpio_keys_remove,
622         .driver = {
623                 .name   = "gpio-keys",
624                 .owner  = THIS_MODULE,
625                 .of_match_table = of_match_ptr(gpio_keys_of_match),
626         },
627 };
628
629 static struct platform_driver gpio_keys_polled_driver = {
630         .probe  = gpio_keys_polled_probe,
631         .remove = gpio_keys_remove,
632         .driver = {
633                 .name   = "gpio-keys-polled",
634                 .owner  = THIS_MODULE,
635                 .of_match_table = of_match_ptr(gpio_keys_polled_of_match),
636         },
637 };
638
639 static int __init gpio_button_init(void)
640 {
641         int ret;
642
643         ret = platform_driver_register(&gpio_keys_driver);
644         if (ret)
645                 return ret;
646
647         ret = platform_driver_register(&gpio_keys_polled_driver);
648         if (ret)
649                 platform_driver_unregister(&gpio_keys_driver);
650
651         return ret;
652 }
653
654 static void __exit gpio_button_exit(void)
655 {
656         platform_driver_unregister(&gpio_keys_driver);
657         platform_driver_unregister(&gpio_keys_polled_driver);
658 }
659
660 module_init(gpio_button_init);
661 module_exit(gpio_button_exit);
662
663 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
664 MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>");
665 MODULE_DESCRIPTION("Polled GPIO Buttons hotplug driver");
666 MODULE_LICENSE("GPL v2");
667 MODULE_ALIAS("platform:" DRV_NAME);