gpio_keys.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /*
  2. * Driver for keys on GPIO lines capable of generating interrupts.
  3. *
  4. * Copyright 2005 Phil Blundell
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/fs.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irq.h>
  15. #include <linux/sched.h>
  16. #include <linux/pm.h>
  17. #include <linux/slab.h>
  18. #include <linux/sysctl.h>
  19. #include <linux/proc_fs.h>
  20. #include <linux/delay.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/input.h>
  23. #include <linux/gpio_keys.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/gpio.h>
  26. struct gpio_button_data {
  27. struct gpio_keys_button *button;
  28. struct input_dev *input;
  29. struct timer_list timer;
  30. struct work_struct work;
  31. int timer_debounce; /* in msecs */
  32. bool disabled;
  33. };
  34. struct gpio_keys_drvdata {
  35. struct input_dev *input;
  36. struct mutex disable_lock;
  37. unsigned int n_buttons;
  38. int (*enable)(struct device *dev);
  39. void (*disable)(struct device *dev);
  40. struct gpio_button_data data[0];
  41. };
  42. /*
  43. * SYSFS interface for enabling/disabling keys and switches:
  44. *
  45. * There are 4 attributes under /sys/devices/platform/gpio-keys/
  46. * keys [ro] - bitmap of keys (EV_KEY) which can be
  47. * disabled
  48. * switches [ro] - bitmap of switches (EV_SW) which can be
  49. * disabled
  50. * disabled_keys [rw] - bitmap of keys currently disabled
  51. * disabled_switches [rw] - bitmap of switches currently disabled
  52. *
  53. * Userland can change these values and hence disable event generation
  54. * for each key (or switch). Disabling a key means its interrupt line
  55. * is disabled.
  56. *
  57. * For example, if we have following switches set up as gpio-keys:
  58. * SW_DOCK = 5
  59. * SW_CAMERA_LENS_COVER = 9
  60. * SW_KEYPAD_SLIDE = 10
  61. * SW_FRONT_PROXIMITY = 11
  62. * This is read from switches:
  63. * 11-9,5
  64. * Next we want to disable proximity (11) and dock (5), we write:
  65. * 11,5
  66. * to file disabled_switches. Now proximity and dock IRQs are disabled.
  67. * This can be verified by reading the file disabled_switches:
  68. * 11,5
  69. * If we now want to enable proximity (11) switch we write:
  70. * 5
  71. * to disabled_switches.
  72. *
  73. * We can disable only those keys which don't allow sharing the irq.
  74. */
  75. /**
  76. * get_n_events_by_type() - returns maximum number of events per @type
  77. * @type: type of button (%EV_KEY, %EV_SW)
  78. *
  79. * Return value of this function can be used to allocate bitmap
  80. * large enough to hold all bits for given type.
  81. */
  82. static inline int get_n_events_by_type(int type)
  83. {
  84. BUG_ON(type != EV_SW && type != EV_KEY);
  85. return (type == EV_KEY) ? KEY_CNT : SW_CNT;
  86. }
  87. /**
  88. * gpio_keys_disable_button() - disables given GPIO button
  89. * @bdata: button data for button to be disabled
  90. *
  91. * Disables button pointed by @bdata. This is done by masking
  92. * IRQ line. After this function is called, button won't generate
  93. * input events anymore. Note that one can only disable buttons
  94. * that don't share IRQs.
  95. *
  96. * Make sure that @bdata->disable_lock is locked when entering
  97. * this function to avoid races when concurrent threads are
  98. * disabling buttons at the same time.
  99. */
  100. static void gpio_keys_disable_button(struct gpio_button_data *bdata)
  101. {
  102. if (!bdata->disabled) {
  103. /*
  104. * Disable IRQ and possible debouncing timer.
  105. */
  106. disable_irq(gpio_to_irq(bdata->button->gpio));
  107. if (bdata->timer_debounce)
  108. del_timer_sync(&bdata->timer);
  109. bdata->disabled = true;
  110. }
  111. }
  112. /**
  113. * gpio_keys_enable_button() - enables given GPIO button
  114. * @bdata: button data for button to be disabled
  115. *
  116. * Enables given button pointed by @bdata.
  117. *
  118. * Make sure that @bdata->disable_lock is locked when entering
  119. * this function to avoid races with concurrent threads trying
  120. * to enable the same button at the same time.
  121. */
  122. static void gpio_keys_enable_button(struct gpio_button_data *bdata)
  123. {
  124. if (bdata->disabled) {
  125. enable_irq(gpio_to_irq(bdata->button->gpio));
  126. bdata->disabled = false;
  127. }
  128. }
  129. /**
  130. * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons
  131. * @ddata: pointer to drvdata
  132. * @buf: buffer where stringified bitmap is written
  133. * @type: button type (%EV_KEY, %EV_SW)
  134. * @only_disabled: does caller want only those buttons that are
  135. * currently disabled or all buttons that can be
  136. * disabled
  137. *
  138. * This function writes buttons that can be disabled to @buf. If
  139. * @only_disabled is true, then @buf contains only those buttons
  140. * that are currently disabled. Returns 0 on success or negative
  141. * errno on failure.
  142. */
  143. static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
  144. char *buf, unsigned int type,
  145. bool only_disabled)
  146. {
  147. int n_events = get_n_events_by_type(type);
  148. unsigned long *bits;
  149. ssize_t ret;
  150. int i;
  151. bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
  152. if (!bits)
  153. return -ENOMEM;
  154. for (i = 0; i < ddata->n_buttons; i++) {
  155. struct gpio_button_data *bdata = &ddata->data[i];
  156. if (bdata->button->type != type)
  157. continue;
  158. if (only_disabled && !bdata->disabled)
  159. continue;
  160. __set_bit(bdata->button->code, bits);
  161. }
  162. ret = bitmap_scnlistprintf(buf, PAGE_SIZE - 2, bits, n_events);
  163. buf[ret++] = '\n';
  164. buf[ret] = '\0';
  165. kfree(bits);
  166. return ret;
  167. }
  168. /**
  169. * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap
  170. * @ddata: pointer to drvdata
  171. * @buf: buffer from userspace that contains stringified bitmap
  172. * @type: button type (%EV_KEY, %EV_SW)
  173. *
  174. * This function parses stringified bitmap from @buf and disables/enables
  175. * GPIO buttons accordinly. Returns 0 on success and negative error
  176. * on failure.
  177. */
  178. static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
  179. const char *buf, unsigned int type)
  180. {
  181. int n_events = get_n_events_by_type(type);
  182. unsigned long *bits;
  183. ssize_t error;
  184. int i;
  185. bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
  186. if (!bits)
  187. return -ENOMEM;
  188. error = bitmap_parselist(buf, bits, n_events);
  189. if (error)
  190. goto out;
  191. /* First validate */
  192. for (i = 0; i < ddata->n_buttons; i++) {
  193. struct gpio_button_data *bdata = &ddata->data[i];
  194. if (bdata->button->type != type)
  195. continue;
  196. if (test_bit(bdata->button->code, bits) &&
  197. !bdata->button->can_disable) {
  198. error = -EINVAL;
  199. goto out;
  200. }
  201. }
  202. mutex_lock(&ddata->disable_lock);
  203. for (i = 0; i < ddata->n_buttons; i++) {
  204. struct gpio_button_data *bdata = &ddata->data[i];
  205. if (bdata->button->type != type)
  206. continue;
  207. if (test_bit(bdata->button->code, bits))
  208. gpio_keys_disable_button(bdata);
  209. else
  210. gpio_keys_enable_button(bdata);
  211. }
  212. mutex_unlock(&ddata->disable_lock);
  213. out:
  214. kfree(bits);
  215. return error;
  216. }
  217. #define ATTR_SHOW_FN(name, type, only_disabled) \
  218. static ssize_t gpio_keys_show_##name(struct device *dev, \
  219. struct device_attribute *attr, \
  220. char *buf) \
  221. { \
  222. struct platform_device *pdev = to_platform_device(dev); \
  223. struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
  224. \
  225. return gpio_keys_attr_show_helper(ddata, buf, \
  226. type, only_disabled); \
  227. }
  228. ATTR_SHOW_FN(keys, EV_KEY, false);
  229. ATTR_SHOW_FN(switches, EV_SW, false);
  230. ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
  231. ATTR_SHOW_FN(disabled_switches, EV_SW, true);
  232. /*
  233. * ATTRIBUTES:
  234. *
  235. * /sys/devices/platform/gpio-keys/keys [ro]
  236. * /sys/devices/platform/gpio-keys/switches [ro]
  237. */
  238. static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
  239. static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
  240. #define ATTR_STORE_FN(name, type) \
  241. static ssize_t gpio_keys_store_##name(struct device *dev, \
  242. struct device_attribute *attr, \
  243. const char *buf, \
  244. size_t count) \
  245. { \
  246. struct platform_device *pdev = to_platform_device(dev); \
  247. struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
  248. ssize_t error; \
  249. \
  250. error = gpio_keys_attr_store_helper(ddata, buf, type); \
  251. if (error) \
  252. return error; \
  253. \
  254. return count; \
  255. }
  256. ATTR_STORE_FN(disabled_keys, EV_KEY);
  257. ATTR_STORE_FN(disabled_switches, EV_SW);
  258. /*
  259. * ATTRIBUTES:
  260. *
  261. * /sys/devices/platform/gpio-keys/disabled_keys [rw]
  262. * /sys/devices/platform/gpio-keys/disables_switches [rw]
  263. */
  264. static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
  265. gpio_keys_show_disabled_keys,
  266. gpio_keys_store_disabled_keys);
  267. static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
  268. gpio_keys_show_disabled_switches,
  269. gpio_keys_store_disabled_switches);
  270. static struct attribute *gpio_keys_attrs[] = {
  271. &dev_attr_keys.attr,
  272. &dev_attr_switches.attr,
  273. &dev_attr_disabled_keys.attr,
  274. &dev_attr_disabled_switches.attr,
  275. NULL,
  276. };
  277. static struct attribute_group gpio_keys_attr_group = {
  278. .attrs = gpio_keys_attrs,
  279. };
  280. static void gpio_keys_report_event(struct gpio_button_data *bdata)
  281. {
  282. struct gpio_keys_button *button = bdata->button;
  283. struct input_dev *input = bdata->input;
  284. unsigned int type = button->type ?: EV_KEY;
  285. int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
  286. input_event(input, type, button->code, !!state);
  287. input_sync(input);
  288. }
  289. static void gpio_keys_work_func(struct work_struct *work)
  290. {
  291. struct gpio_button_data *bdata =
  292. container_of(work, struct gpio_button_data, work);
  293. gpio_keys_report_event(bdata);
  294. }
  295. static void gpio_keys_timer(unsigned long _data)
  296. {
  297. struct gpio_button_data *data = (struct gpio_button_data *)_data;
  298. schedule_work(&data->work);
  299. }
  300. static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
  301. {
  302. struct gpio_button_data *bdata = dev_id;
  303. struct gpio_keys_button *button = bdata->button;
  304. BUG_ON(irq != gpio_to_irq(button->gpio));
  305. if (bdata->timer_debounce)
  306. mod_timer(&bdata->timer,
  307. jiffies + msecs_to_jiffies(bdata->timer_debounce));
  308. else
  309. schedule_work(&bdata->work);
  310. return IRQ_HANDLED;
  311. }
  312. static int __devinit gpio_keys_setup_key(struct platform_device *pdev,
  313. struct gpio_button_data *bdata,
  314. struct gpio_keys_button *button)
  315. {
  316. char *desc = button->desc ? button->desc : "gpio_keys";
  317. struct device *dev = &pdev->dev;
  318. unsigned long irqflags;
  319. int irq, error;
  320. setup_timer(&bdata->timer, gpio_keys_timer, (unsigned long)bdata);
  321. INIT_WORK(&bdata->work, gpio_keys_work_func);
  322. error = gpio_request(button->gpio, desc);
  323. if (error < 0) {
  324. dev_err(dev, "failed to request GPIO %d, error %d\n",
  325. button->gpio, error);
  326. goto fail2;
  327. }
  328. error = gpio_direction_input(button->gpio);
  329. if (error < 0) {
  330. dev_err(dev, "failed to configure"
  331. " direction for GPIO %d, error %d\n",
  332. button->gpio, error);
  333. goto fail3;
  334. }
  335. if (button->debounce_interval) {
  336. error = gpio_set_debounce(button->gpio,
  337. button->debounce_interval * 1000);
  338. /* use timer if gpiolib doesn't provide debounce */
  339. if (error < 0)
  340. bdata->timer_debounce = button->debounce_interval;
  341. }
  342. irq = gpio_to_irq(button->gpio);
  343. if (irq < 0) {
  344. error = irq;
  345. dev_err(dev, "Unable to get irq number for GPIO %d, error %d\n",
  346. button->gpio, error);
  347. goto fail3;
  348. }
  349. irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
  350. /*
  351. * If platform has specified that the button can be disabled,
  352. * we don't want it to share the interrupt line.
  353. */
  354. if (!button->can_disable)
  355. irqflags |= IRQF_SHARED;
  356. error = request_irq(irq, gpio_keys_isr, irqflags, desc, bdata);
  357. if (error) {
  358. dev_err(dev, "Unable to claim irq %d; error %d\n",
  359. irq, error);
  360. goto fail3;
  361. }
  362. return 0;
  363. fail3:
  364. gpio_free(button->gpio);
  365. fail2:
  366. return error;
  367. }
  368. static int gpio_keys_open(struct input_dev *input)
  369. {
  370. struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
  371. return ddata->enable ? ddata->enable(input->dev.parent) : 0;
  372. }
  373. static void gpio_keys_close(struct input_dev *input)
  374. {
  375. struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
  376. if (ddata->disable)
  377. ddata->disable(input->dev.parent);
  378. }
  379. static int __devinit gpio_keys_probe(struct platform_device *pdev)
  380. {
  381. struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  382. struct gpio_keys_drvdata *ddata;
  383. struct device *dev = &pdev->dev;
  384. struct input_dev *input;
  385. int i, error;
  386. int wakeup = 0;
  387. ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
  388. pdata->nbuttons * sizeof(struct gpio_button_data),
  389. GFP_KERNEL);
  390. input = input_allocate_device();
  391. if (!ddata || !input) {
  392. dev_err(dev, "failed to allocate state\n");
  393. error = -ENOMEM;
  394. goto fail1;
  395. }
  396. ddata->input = input;
  397. ddata->n_buttons = pdata->nbuttons;
  398. ddata->enable = pdata->enable;
  399. ddata->disable = pdata->disable;
  400. mutex_init(&ddata->disable_lock);
  401. platform_set_drvdata(pdev, ddata);
  402. input_set_drvdata(input, ddata);
  403. input->name = pdev->name;
  404. input->phys = "gpio-keys/input0";
  405. input->dev.parent = &pdev->dev;
  406. input->open = gpio_keys_open;
  407. input->close = gpio_keys_close;
  408. input->id.bustype = BUS_HOST;
  409. input->id.vendor = 0x0001;
  410. input->id.product = 0x0001;
  411. input->id.version = 0x0100;
  412. /* Enable auto repeat feature of Linux input subsystem */
  413. if (pdata->rep)
  414. __set_bit(EV_REP, input->evbit);
  415. for (i = 0; i < pdata->nbuttons; i++) {
  416. struct gpio_keys_button *button = &pdata->buttons[i];
  417. struct gpio_button_data *bdata = &ddata->data[i];
  418. unsigned int type = button->type ?: EV_KEY;
  419. bdata->input = input;
  420. bdata->button = button;
  421. error = gpio_keys_setup_key(pdev, bdata, button);
  422. if (error)
  423. goto fail2;
  424. if (button->wakeup)
  425. wakeup = 1;
  426. input_set_capability(input, type, button->code);
  427. }
  428. error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
  429. if (error) {
  430. dev_err(dev, "Unable to export keys/switches, error: %d\n",
  431. error);
  432. goto fail2;
  433. }
  434. error = input_register_device(input);
  435. if (error) {
  436. dev_err(dev, "Unable to register input device, error: %d\n",
  437. error);
  438. goto fail3;
  439. }
  440. /* get current state of buttons */
  441. for (i = 0; i < pdata->nbuttons; i++)
  442. gpio_keys_report_event(&ddata->data[i]);
  443. input_sync(input);
  444. device_init_wakeup(&pdev->dev, wakeup);
  445. return 0;
  446. fail3:
  447. sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
  448. fail2:
  449. while (--i >= 0) {
  450. free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
  451. if (ddata->data[i].timer_debounce)
  452. del_timer_sync(&ddata->data[i].timer);
  453. cancel_work_sync(&ddata->data[i].work);
  454. gpio_free(pdata->buttons[i].gpio);
  455. }
  456. platform_set_drvdata(pdev, NULL);
  457. fail1:
  458. input_free_device(input);
  459. kfree(ddata);
  460. return error;
  461. }
  462. static int __devexit gpio_keys_remove(struct platform_device *pdev)
  463. {
  464. struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  465. struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
  466. struct input_dev *input = ddata->input;
  467. int i;
  468. sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
  469. device_init_wakeup(&pdev->dev, 0);
  470. for (i = 0; i < pdata->nbuttons; i++) {
  471. int irq = gpio_to_irq(pdata->buttons[i].gpio);
  472. free_irq(irq, &ddata->data[i]);
  473. if (ddata->data[i].timer_debounce)
  474. del_timer_sync(&ddata->data[i].timer);
  475. cancel_work_sync(&ddata->data[i].work);
  476. gpio_free(pdata->buttons[i].gpio);
  477. }
  478. input_unregister_device(input);
  479. return 0;
  480. }
  481. #ifdef CONFIG_PM
  482. static int gpio_keys_suspend(struct device *dev)
  483. {
  484. struct platform_device *pdev = to_platform_device(dev);
  485. struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  486. int i;
  487. if (device_may_wakeup(&pdev->dev)) {
  488. for (i = 0; i < pdata->nbuttons; i++) {
  489. struct gpio_keys_button *button = &pdata->buttons[i];
  490. if (button->wakeup) {
  491. int irq = gpio_to_irq(button->gpio);
  492. enable_irq_wake(irq);
  493. }
  494. }
  495. }
  496. return 0;
  497. }
  498. static int gpio_keys_resume(struct device *dev)
  499. {
  500. struct platform_device *pdev = to_platform_device(dev);
  501. struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
  502. struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  503. int i;
  504. for (i = 0; i < pdata->nbuttons; i++) {
  505. struct gpio_keys_button *button = &pdata->buttons[i];
  506. if (button->wakeup && device_may_wakeup(&pdev->dev)) {
  507. int irq = gpio_to_irq(button->gpio);
  508. disable_irq_wake(irq);
  509. }
  510. gpio_keys_report_event(&ddata->data[i]);
  511. }
  512. input_sync(ddata->input);
  513. return 0;
  514. }
  515. static const struct dev_pm_ops gpio_keys_pm_ops = {
  516. .suspend = gpio_keys_suspend,
  517. .resume = gpio_keys_resume,
  518. };
  519. #endif
  520. static struct platform_driver gpio_keys_device_driver = {
  521. .probe = gpio_keys_probe,
  522. .remove = __devexit_p(gpio_keys_remove),
  523. .driver = {
  524. .name = "gpio-keys",
  525. .owner = THIS_MODULE,
  526. #ifdef CONFIG_PM
  527. .pm = &gpio_keys_pm_ops,
  528. #endif
  529. }
  530. };
  531. static int __init gpio_keys_init(void)
  532. {
  533. return platform_driver_register(&gpio_keys_device_driver);
  534. }
  535. static void __exit gpio_keys_exit(void)
  536. {
  537. platform_driver_unregister(&gpio_keys_device_driver);
  538. }
  539. module_init(gpio_keys_init);
  540. module_exit(gpio_keys_exit);
  541. MODULE_LICENSE("GPL");
  542. MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
  543. MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
  544. MODULE_ALIAS("platform:gpio-keys");