gpio-button-hotplug.c 15 KB

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