gpio_keys.c 16 KB

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