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