gpio_keys.c 16 KB

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