gpio-button-hotplug: add wwan button
[openwrt/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 #ifdef CONFIG_HOTPLUG
228 static int button_get_index(unsigned int code)
229 {
230 int i;
231
232 for (i = 0; i < ARRAY_SIZE(button_map); i++)
233 if (button_map[i].code == code)
234 return i;
235
236 return -1;
237 }
238
239 static void button_hotplug_event(struct gpio_keys_button_data *data,
240 unsigned int type, int value)
241 {
242 struct bh_priv *priv = &data->bh;
243 unsigned long seen = jiffies;
244 int btn;
245
246 BH_DBG("event type=%u, code=%u, value=%d\n", type, data->b->code, value);
247
248 if ((type != EV_KEY) && (type != EV_SW))
249 return;
250
251 btn = button_get_index(data->b->code);
252 if (btn < 0)
253 return;
254
255 button_hotplug_create_event(button_map[btn].name, type,
256 (seen - priv->seen) / HZ, value);
257 priv->seen = seen;
258 }
259 #else
260 static void button_hotplug_event(struct gpio_keys_button_data *data,
261 unsigned int type, int value)
262 {
263 }
264 #endif /* CONFIG_HOTPLUG */
265
266 struct gpio_keys_button_dev {
267 int polled;
268 struct delayed_work work;
269
270 struct device *dev;
271 struct gpio_keys_platform_data *pdata;
272 struct gpio_keys_button_data data[0];
273 };
274
275 static int gpio_button_get_value(struct gpio_keys_button_data *bdata)
276 {
277 int val;
278
279 if (bdata->can_sleep)
280 val = !!gpio_get_value_cansleep(bdata->b->gpio);
281 else
282 val = !!gpio_get_value(bdata->b->gpio);
283
284 return val ^ bdata->b->active_low;
285 }
286
287 static void gpio_keys_polled_check_state(struct gpio_keys_button_data *bdata)
288 {
289 int state = gpio_button_get_value(bdata);
290
291 if (state != bdata->last_state) {
292 unsigned int type = bdata->b->type ?: EV_KEY;
293
294 if (bdata->count < bdata->threshold) {
295 bdata->count++;
296 return;
297 }
298
299 if ((bdata->last_state != -1) || (type == EV_SW))
300 button_hotplug_event(bdata, type, state);
301
302 bdata->last_state = state;
303 }
304
305 bdata->count = 0;
306 }
307
308 static void gpio_keys_polled_queue_work(struct gpio_keys_button_dev *bdev)
309 {
310 struct gpio_keys_platform_data *pdata = bdev->pdata;
311 unsigned long delay = msecs_to_jiffies(pdata->poll_interval);
312
313 if (delay >= HZ)
314 delay = round_jiffies_relative(delay);
315 schedule_delayed_work(&bdev->work, delay);
316 }
317
318 static void gpio_keys_polled_poll(struct work_struct *work)
319 {
320 struct gpio_keys_button_dev *bdev =
321 container_of(work, struct gpio_keys_button_dev, work.work);
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(bdata);
327 }
328 gpio_keys_polled_queue_work(bdev);
329 }
330
331 static void gpio_keys_polled_close(struct gpio_keys_button_dev *bdev)
332 {
333 struct gpio_keys_platform_data *pdata = bdev->pdata;
334
335 cancel_delayed_work_sync(&bdev->work);
336
337 if (pdata->disable)
338 pdata->disable(bdev->dev);
339 }
340
341 static irqreturn_t button_handle_irq(int irq, void *_bdata)
342 {
343 struct gpio_keys_button_data *bdata = (struct gpio_keys_button_data *) _bdata;
344
345 button_hotplug_event(bdata, bdata->b->type ?: EV_KEY, gpio_button_get_value(bdata));
346
347 return IRQ_HANDLED;
348 }
349
350 #ifdef CONFIG_OF
351 static struct gpio_keys_platform_data *
352 gpio_keys_get_devtree_pdata(struct device *dev)
353 {
354 struct device_node *node, *pp;
355 struct gpio_keys_platform_data *pdata;
356 struct gpio_keys_button *button;
357 int error;
358 int nbuttons;
359 int i = 0;
360
361 node = dev->of_node;
362 if (!node)
363 return NULL;
364
365 nbuttons = of_get_child_count(node);
366 if (nbuttons == 0)
367 return NULL;
368
369 pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * (sizeof *button),
370 GFP_KERNEL);
371 if (!pdata) {
372 error = -ENOMEM;
373 goto err_out;
374 }
375
376 pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
377 pdata->nbuttons = nbuttons;
378
379 pdata->rep = !!of_get_property(node, "autorepeat", NULL);
380 of_property_read_u32(node, "poll-interval", &pdata->poll_interval);
381
382 for_each_child_of_node(node, pp) {
383 enum of_gpio_flags flags;
384
385 if (!of_find_property(pp, "gpios", NULL)) {
386 pdata->nbuttons--;
387 dev_warn(dev, "Found button without gpios\n");
388 continue;
389 }
390
391 button = &pdata->buttons[i++];
392
393 button->gpio = of_get_gpio_flags(pp, 0, &flags);
394 button->active_low = flags & OF_GPIO_ACTIVE_LOW;
395
396 if (of_property_read_u32(pp, "linux,code", &button->code)) {
397 dev_err(dev, "Button without keycode: 0x%x\n",
398 button->gpio);
399 error = -EINVAL;
400 goto err_out;
401 }
402
403 button->desc = of_get_property(pp, "label", NULL);
404
405 if (of_property_read_u32(pp, "linux,input-type", &button->type))
406 button->type = EV_KEY;
407
408 button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
409
410 if (of_property_read_u32(pp, "debounce-interval",
411 &button->debounce_interval))
412 button->debounce_interval = 5;
413 }
414
415 if (pdata->nbuttons == 0) {
416 error = -EINVAL;
417 goto err_out;
418 }
419
420 return pdata;
421
422 err_out:
423 return ERR_PTR(error);
424 }
425
426 static struct of_device_id gpio_keys_of_match[] = {
427 { .compatible = "gpio-keys", },
428 { },
429 };
430 MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
431
432 static struct of_device_id gpio_keys_polled_of_match[] = {
433 { .compatible = "gpio-keys-polled", },
434 { },
435 };
436 MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match);
437
438 #else
439
440 static inline struct gpio_keys_platform_data *
441 gpio_keys_get_devtree_pdata(struct device *dev)
442 {
443 return NULL;
444 }
445 #endif
446
447 static int gpio_keys_button_probe(struct platform_device *pdev,
448 struct gpio_keys_button_dev **_bdev, int polled)
449 {
450 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
451 struct device *dev = &pdev->dev;
452 struct gpio_keys_button_dev *bdev;
453 struct gpio_keys_button *buttons;
454 int error;
455 int i;
456
457 if (!pdata) {
458 pdata = gpio_keys_get_devtree_pdata(dev);
459 if (IS_ERR(pdata))
460 return PTR_ERR(pdata);
461 if (!pdata) {
462 dev_err(dev, "missing platform data\n");
463 return -EINVAL;
464 }
465 pdev->dev.platform_data = pdata;
466 }
467
468 if (polled && !pdata->poll_interval) {
469 dev_err(dev, "missing poll_interval value\n");
470 return -EINVAL;
471 }
472
473 buttons = devm_kzalloc(dev, pdata->nbuttons * sizeof(struct gpio_keys_button),
474 GFP_KERNEL);
475 if (!buttons) {
476 dev_err(dev, "no memory for button data\n");
477 return -ENOMEM;
478 }
479 memcpy(buttons, pdata->buttons, pdata->nbuttons * sizeof(struct gpio_keys_button));
480
481 bdev = devm_kzalloc(dev, sizeof(struct gpio_keys_button_dev) +
482 pdata->nbuttons * sizeof(struct gpio_keys_button_data),
483 GFP_KERNEL);
484 if (!bdev) {
485 dev_err(dev, "no memory for private data\n");
486 return -ENOMEM;
487 }
488
489 bdev->polled = polled;
490
491 for (i = 0; i < pdata->nbuttons; i++) {
492 struct gpio_keys_button *button = &buttons[i];
493 struct gpio_keys_button_data *bdata = &bdev->data[i];
494 unsigned int gpio = button->gpio;
495
496 if (button->wakeup) {
497 dev_err(dev, DRV_NAME "does not support wakeup\n");
498 return -EINVAL;
499 }
500
501 error = devm_gpio_request(dev, gpio,
502 button->desc ? button->desc : DRV_NAME);
503 if (error) {
504 dev_err(dev, "unable to claim gpio %u, err=%d\n",
505 gpio, error);
506 return error;
507 }
508
509 error = gpio_direction_input(gpio);
510 if (error) {
511 dev_err(dev,
512 "unable to set direction on gpio %u, err=%d\n",
513 gpio, error);
514 return error;
515 }
516
517 bdata->can_sleep = gpio_cansleep(gpio);
518 bdata->last_state = -1;
519
520 if (bdev->polled)
521 bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
522 pdata->poll_interval);
523 else
524 bdata->threshold = 1;
525
526 bdata->b = &pdata->buttons[i];
527 }
528
529 bdev->dev = &pdev->dev;
530 bdev->pdata = pdata;
531 platform_set_drvdata(pdev, bdev);
532
533 *_bdev = bdev;
534
535 return 0;
536 }
537
538 static int gpio_keys_probe(struct platform_device *pdev)
539 {
540 struct gpio_keys_platform_data *pdata;
541 struct gpio_keys_button_dev *bdev;
542 int ret, i;
543
544
545 ret = gpio_keys_button_probe(pdev, &bdev, 0);
546
547 if (ret)
548 return ret;
549
550 pdata = pdev->dev.platform_data;
551 for (i = 0; i < pdata->nbuttons; i++) {
552 struct gpio_keys_button *button = &pdata->buttons[i];
553 struct gpio_keys_button_data *bdata = &bdev->data[i];
554
555 if (bdata->can_sleep) {
556 dev_err(&pdev->dev, "skipping gpio:%d, it can sleep\n", button->gpio);
557 continue;
558 }
559 if (!button->irq)
560 button->irq = gpio_to_irq(button->gpio);
561 if (button->irq < 0) {
562 dev_err(&pdev->dev, "failed to get irq for gpio:%d\n", button->gpio);
563 continue;
564 }
565 ret = devm_request_irq(&pdev->dev, button->irq, button_handle_irq,
566 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
567 dev_name(&pdev->dev), bdata);
568 if (ret)
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@openwrt.org>");
665 MODULE_DESCRIPTION("Polled GPIO Buttons hotplug driver");
666 MODULE_LICENSE("GPL v2");
667 MODULE_ALIAS("platform:" DRV_NAME);