gpio_keys.c 16 KB

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