gpio_keys.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /*
  2. * Driver for keys on GPIO lines capable of generating interrupts.
  3. *
  4. * Copyright 2005 Phil Blundell
  5. * Copyright 2010, 2011 David Jander <david@protonic.nl>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/fs.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/sched.h>
  17. #include <linux/pm.h>
  18. #include <linux/slab.h>
  19. #include <linux/sysctl.h>
  20. #include <linux/proc_fs.h>
  21. #include <linux/delay.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/input.h>
  24. #include <linux/gpio_keys.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/gpio.h>
  27. #include <linux/gpio/consumer.h>
  28. #include <linux/of.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/of_gpio.h>
  31. #include <linux/of_irq.h>
  32. #include <linux/spinlock.h>
  33. struct gpio_button_data {
  34. const struct gpio_keys_button *button;
  35. struct input_dev *input;
  36. struct gpio_desc *gpiod;
  37. struct timer_list release_timer;
  38. unsigned int release_delay; /* in msecs, for IRQ-only buttons */
  39. struct delayed_work work;
  40. unsigned int software_debounce; /* in msecs, for GPIO-driven buttons */
  41. unsigned int irq;
  42. spinlock_t lock;
  43. bool disabled;
  44. bool key_pressed;
  45. };
  46. struct gpio_keys_drvdata {
  47. const struct gpio_keys_platform_data *pdata;
  48. struct input_dev *input;
  49. struct mutex disable_lock;
  50. struct gpio_button_data data[0];
  51. };
  52. /*
  53. * SYSFS interface for enabling/disabling keys and switches:
  54. *
  55. * There are 4 attributes under /sys/devices/platform/gpio-keys/
  56. * keys [ro] - bitmap of keys (EV_KEY) which can be
  57. * disabled
  58. * switches [ro] - bitmap of switches (EV_SW) which can be
  59. * disabled
  60. * disabled_keys [rw] - bitmap of keys currently disabled
  61. * disabled_switches [rw] - bitmap of switches currently disabled
  62. *
  63. * Userland can change these values and hence disable event generation
  64. * for each key (or switch). Disabling a key means its interrupt line
  65. * is disabled.
  66. *
  67. * For example, if we have following switches set up as gpio-keys:
  68. * SW_DOCK = 5
  69. * SW_CAMERA_LENS_COVER = 9
  70. * SW_KEYPAD_SLIDE = 10
  71. * SW_FRONT_PROXIMITY = 11
  72. * This is read from switches:
  73. * 11-9,5
  74. * Next we want to disable proximity (11) and dock (5), we write:
  75. * 11,5
  76. * to file disabled_switches. Now proximity and dock IRQs are disabled.
  77. * This can be verified by reading the file disabled_switches:
  78. * 11,5
  79. * If we now want to enable proximity (11) switch we write:
  80. * 5
  81. * to disabled_switches.
  82. *
  83. * We can disable only those keys which don't allow sharing the irq.
  84. */
  85. /**
  86. * get_n_events_by_type() - returns maximum number of events per @type
  87. * @type: type of button (%EV_KEY, %EV_SW)
  88. *
  89. * Return value of this function can be used to allocate bitmap
  90. * large enough to hold all bits for given type.
  91. */
  92. static int get_n_events_by_type(int type)
  93. {
  94. BUG_ON(type != EV_SW && type != EV_KEY);
  95. return (type == EV_KEY) ? KEY_CNT : SW_CNT;
  96. }
  97. /**
  98. * get_bm_events_by_type() - returns bitmap of supported events per @type
  99. * @input: input device from which bitmap is retrieved
  100. * @type: type of button (%EV_KEY, %EV_SW)
  101. *
  102. * Return value of this function can be used to allocate bitmap
  103. * large enough to hold all bits for given type.
  104. */
  105. static const unsigned long *get_bm_events_by_type(struct input_dev *dev,
  106. int type)
  107. {
  108. BUG_ON(type != EV_SW && type != EV_KEY);
  109. return (type == EV_KEY) ? dev->keybit : dev->swbit;
  110. }
  111. /**
  112. * gpio_keys_disable_button() - disables given GPIO button
  113. * @bdata: button data for button to be disabled
  114. *
  115. * Disables button pointed by @bdata. This is done by masking
  116. * IRQ line. After this function is called, button won't generate
  117. * input events anymore. Note that one can only disable buttons
  118. * that don't share IRQs.
  119. *
  120. * Make sure that @bdata->disable_lock is locked when entering
  121. * this function to avoid races when concurrent threads are
  122. * disabling buttons at the same time.
  123. */
  124. static void gpio_keys_disable_button(struct gpio_button_data *bdata)
  125. {
  126. if (!bdata->disabled) {
  127. /*
  128. * Disable IRQ and associated timer/work structure.
  129. */
  130. disable_irq(bdata->irq);
  131. if (bdata->gpiod)
  132. cancel_delayed_work_sync(&bdata->work);
  133. else
  134. del_timer_sync(&bdata->release_timer);
  135. bdata->disabled = true;
  136. }
  137. }
  138. /**
  139. * gpio_keys_enable_button() - enables given GPIO button
  140. * @bdata: button data for button to be disabled
  141. *
  142. * Enables given button pointed by @bdata.
  143. *
  144. * Make sure that @bdata->disable_lock is locked when entering
  145. * this function to avoid races with concurrent threads trying
  146. * to enable the same button at the same time.
  147. */
  148. static void gpio_keys_enable_button(struct gpio_button_data *bdata)
  149. {
  150. if (bdata->disabled) {
  151. enable_irq(bdata->irq);
  152. bdata->disabled = false;
  153. }
  154. }
  155. /**
  156. * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons
  157. * @ddata: pointer to drvdata
  158. * @buf: buffer where stringified bitmap is written
  159. * @type: button type (%EV_KEY, %EV_SW)
  160. * @only_disabled: does caller want only those buttons that are
  161. * currently disabled or all buttons that can be
  162. * disabled
  163. *
  164. * This function writes buttons that can be disabled to @buf. If
  165. * @only_disabled is true, then @buf contains only those buttons
  166. * that are currently disabled. Returns 0 on success or negative
  167. * errno on failure.
  168. */
  169. static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
  170. char *buf, unsigned int type,
  171. bool only_disabled)
  172. {
  173. int n_events = get_n_events_by_type(type);
  174. unsigned long *bits;
  175. ssize_t ret;
  176. int i;
  177. bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
  178. if (!bits)
  179. return -ENOMEM;
  180. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  181. struct gpio_button_data *bdata = &ddata->data[i];
  182. if (bdata->button->type != type)
  183. continue;
  184. if (only_disabled && !bdata->disabled)
  185. continue;
  186. __set_bit(bdata->button->code, bits);
  187. }
  188. ret = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", n_events, bits);
  189. buf[ret++] = '\n';
  190. buf[ret] = '\0';
  191. kfree(bits);
  192. return ret;
  193. }
  194. /**
  195. * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap
  196. * @ddata: pointer to drvdata
  197. * @buf: buffer from userspace that contains stringified bitmap
  198. * @type: button type (%EV_KEY, %EV_SW)
  199. *
  200. * This function parses stringified bitmap from @buf and disables/enables
  201. * GPIO buttons accordingly. Returns 0 on success and negative error
  202. * on failure.
  203. */
  204. static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
  205. const char *buf, unsigned int type)
  206. {
  207. int n_events = get_n_events_by_type(type);
  208. const unsigned long *bitmap = get_bm_events_by_type(ddata->input, type);
  209. unsigned long *bits;
  210. ssize_t error;
  211. int i;
  212. bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
  213. if (!bits)
  214. return -ENOMEM;
  215. error = bitmap_parselist(buf, bits, n_events);
  216. if (error)
  217. goto out;
  218. /* First validate */
  219. if (!bitmap_subset(bits, bitmap, n_events)) {
  220. error = -EINVAL;
  221. goto out;
  222. }
  223. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  224. struct gpio_button_data *bdata = &ddata->data[i];
  225. if (bdata->button->type != type)
  226. continue;
  227. if (test_bit(bdata->button->code, bits) &&
  228. !bdata->button->can_disable) {
  229. error = -EINVAL;
  230. goto out;
  231. }
  232. }
  233. mutex_lock(&ddata->disable_lock);
  234. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  235. struct gpio_button_data *bdata = &ddata->data[i];
  236. if (bdata->button->type != type)
  237. continue;
  238. if (test_bit(bdata->button->code, bits))
  239. gpio_keys_disable_button(bdata);
  240. else
  241. gpio_keys_enable_button(bdata);
  242. }
  243. mutex_unlock(&ddata->disable_lock);
  244. out:
  245. kfree(bits);
  246. return error;
  247. }
  248. #define ATTR_SHOW_FN(name, type, only_disabled) \
  249. static ssize_t gpio_keys_show_##name(struct device *dev, \
  250. struct device_attribute *attr, \
  251. char *buf) \
  252. { \
  253. struct platform_device *pdev = to_platform_device(dev); \
  254. struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
  255. \
  256. return gpio_keys_attr_show_helper(ddata, buf, \
  257. type, only_disabled); \
  258. }
  259. ATTR_SHOW_FN(keys, EV_KEY, false);
  260. ATTR_SHOW_FN(switches, EV_SW, false);
  261. ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
  262. ATTR_SHOW_FN(disabled_switches, EV_SW, true);
  263. /*
  264. * ATTRIBUTES:
  265. *
  266. * /sys/devices/platform/gpio-keys/keys [ro]
  267. * /sys/devices/platform/gpio-keys/switches [ro]
  268. */
  269. static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
  270. static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
  271. #define ATTR_STORE_FN(name, type) \
  272. static ssize_t gpio_keys_store_##name(struct device *dev, \
  273. struct device_attribute *attr, \
  274. const char *buf, \
  275. size_t count) \
  276. { \
  277. struct platform_device *pdev = to_platform_device(dev); \
  278. struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
  279. ssize_t error; \
  280. \
  281. error = gpio_keys_attr_store_helper(ddata, buf, type); \
  282. if (error) \
  283. return error; \
  284. \
  285. return count; \
  286. }
  287. ATTR_STORE_FN(disabled_keys, EV_KEY);
  288. ATTR_STORE_FN(disabled_switches, EV_SW);
  289. /*
  290. * ATTRIBUTES:
  291. *
  292. * /sys/devices/platform/gpio-keys/disabled_keys [rw]
  293. * /sys/devices/platform/gpio-keys/disables_switches [rw]
  294. */
  295. static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
  296. gpio_keys_show_disabled_keys,
  297. gpio_keys_store_disabled_keys);
  298. static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
  299. gpio_keys_show_disabled_switches,
  300. gpio_keys_store_disabled_switches);
  301. static struct attribute *gpio_keys_attrs[] = {
  302. &dev_attr_keys.attr,
  303. &dev_attr_switches.attr,
  304. &dev_attr_disabled_keys.attr,
  305. &dev_attr_disabled_switches.attr,
  306. NULL,
  307. };
  308. static struct attribute_group gpio_keys_attr_group = {
  309. .attrs = gpio_keys_attrs,
  310. };
  311. static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
  312. {
  313. const struct gpio_keys_button *button = bdata->button;
  314. struct input_dev *input = bdata->input;
  315. unsigned int type = button->type ?: EV_KEY;
  316. int state;
  317. state = gpiod_get_value_cansleep(bdata->gpiod);
  318. if (state < 0) {
  319. dev_err(input->dev.parent,
  320. "failed to get gpio state: %d\n", state);
  321. return;
  322. }
  323. if (type == EV_ABS) {
  324. if (state)
  325. input_event(input, type, button->code, button->value);
  326. } else {
  327. input_event(input, type, button->code, state);
  328. }
  329. input_sync(input);
  330. }
  331. static void gpio_keys_gpio_work_func(struct work_struct *work)
  332. {
  333. struct gpio_button_data *bdata =
  334. container_of(work, struct gpio_button_data, work.work);
  335. gpio_keys_gpio_report_event(bdata);
  336. if (bdata->button->wakeup)
  337. pm_relax(bdata->input->dev.parent);
  338. }
  339. static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
  340. {
  341. struct gpio_button_data *bdata = dev_id;
  342. BUG_ON(irq != bdata->irq);
  343. if (bdata->button->wakeup)
  344. pm_stay_awake(bdata->input->dev.parent);
  345. mod_delayed_work(system_wq,
  346. &bdata->work,
  347. msecs_to_jiffies(bdata->software_debounce));
  348. return IRQ_HANDLED;
  349. }
  350. static void gpio_keys_irq_timer(unsigned long _data)
  351. {
  352. struct gpio_button_data *bdata = (struct gpio_button_data *)_data;
  353. struct input_dev *input = bdata->input;
  354. unsigned long flags;
  355. spin_lock_irqsave(&bdata->lock, flags);
  356. if (bdata->key_pressed) {
  357. input_event(input, EV_KEY, bdata->button->code, 0);
  358. input_sync(input);
  359. bdata->key_pressed = false;
  360. }
  361. spin_unlock_irqrestore(&bdata->lock, flags);
  362. }
  363. static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
  364. {
  365. struct gpio_button_data *bdata = dev_id;
  366. const struct gpio_keys_button *button = bdata->button;
  367. struct input_dev *input = bdata->input;
  368. unsigned long flags;
  369. BUG_ON(irq != bdata->irq);
  370. spin_lock_irqsave(&bdata->lock, flags);
  371. if (!bdata->key_pressed) {
  372. if (bdata->button->wakeup)
  373. pm_wakeup_event(bdata->input->dev.parent, 0);
  374. input_event(input, EV_KEY, button->code, 1);
  375. input_sync(input);
  376. if (!bdata->release_delay) {
  377. input_event(input, EV_KEY, button->code, 0);
  378. input_sync(input);
  379. goto out;
  380. }
  381. bdata->key_pressed = true;
  382. }
  383. if (bdata->release_delay)
  384. mod_timer(&bdata->release_timer,
  385. jiffies + msecs_to_jiffies(bdata->release_delay));
  386. out:
  387. spin_unlock_irqrestore(&bdata->lock, flags);
  388. return IRQ_HANDLED;
  389. }
  390. static void gpio_keys_quiesce_key(void *data)
  391. {
  392. struct gpio_button_data *bdata = data;
  393. if (bdata->gpiod)
  394. cancel_delayed_work_sync(&bdata->work);
  395. else
  396. del_timer_sync(&bdata->release_timer);
  397. }
  398. static int gpio_keys_setup_key(struct platform_device *pdev,
  399. struct input_dev *input,
  400. struct gpio_button_data *bdata,
  401. const struct gpio_keys_button *button)
  402. {
  403. const char *desc = button->desc ? button->desc : "gpio_keys";
  404. struct device *dev = &pdev->dev;
  405. irq_handler_t isr;
  406. unsigned long irqflags;
  407. int irq;
  408. int error;
  409. bdata->input = input;
  410. bdata->button = button;
  411. spin_lock_init(&bdata->lock);
  412. /*
  413. * Legacy GPIO number, so request the GPIO here and
  414. * convert it to descriptor.
  415. */
  416. if (gpio_is_valid(button->gpio)) {
  417. unsigned flags = GPIOF_IN;
  418. if (button->active_low)
  419. flags |= GPIOF_ACTIVE_LOW;
  420. error = devm_gpio_request_one(&pdev->dev, button->gpio, flags,
  421. desc);
  422. if (error < 0) {
  423. dev_err(dev, "Failed to request GPIO %d, error %d\n",
  424. button->gpio, error);
  425. return error;
  426. }
  427. bdata->gpiod = gpio_to_desc(button->gpio);
  428. if (!bdata->gpiod)
  429. return -EINVAL;
  430. if (button->debounce_interval) {
  431. error = gpiod_set_debounce(bdata->gpiod,
  432. button->debounce_interval * 1000);
  433. /* use timer if gpiolib doesn't provide debounce */
  434. if (error < 0)
  435. bdata->software_debounce =
  436. button->debounce_interval;
  437. }
  438. if (button->irq) {
  439. bdata->irq = button->irq;
  440. } else {
  441. irq = gpiod_to_irq(bdata->gpiod);
  442. if (irq < 0) {
  443. error = irq;
  444. dev_err(dev,
  445. "Unable to get irq number for GPIO %d, error %d\n",
  446. button->gpio, error);
  447. return error;
  448. }
  449. bdata->irq = irq;
  450. }
  451. INIT_DELAYED_WORK(&bdata->work, gpio_keys_gpio_work_func);
  452. isr = gpio_keys_gpio_isr;
  453. irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
  454. } else {
  455. if (!button->irq) {
  456. dev_err(dev, "No IRQ specified\n");
  457. return -EINVAL;
  458. }
  459. bdata->irq = button->irq;
  460. if (button->type && button->type != EV_KEY) {
  461. dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n");
  462. return -EINVAL;
  463. }
  464. bdata->release_delay = button->debounce_interval;
  465. setup_timer(&bdata->release_timer,
  466. gpio_keys_irq_timer, (unsigned long)bdata);
  467. isr = gpio_keys_irq_isr;
  468. irqflags = 0;
  469. }
  470. input_set_capability(input, button->type ?: EV_KEY, button->code);
  471. /*
  472. * Install custom action to cancel release timer and
  473. * workqueue item.
  474. */
  475. error = devm_add_action(&pdev->dev, gpio_keys_quiesce_key, bdata);
  476. if (error) {
  477. dev_err(&pdev->dev,
  478. "failed to register quiesce action, error: %d\n",
  479. error);
  480. return error;
  481. }
  482. /*
  483. * If platform has specified that the button can be disabled,
  484. * we don't want it to share the interrupt line.
  485. */
  486. if (!button->can_disable)
  487. irqflags |= IRQF_SHARED;
  488. error = devm_request_any_context_irq(&pdev->dev, bdata->irq,
  489. isr, irqflags, desc, bdata);
  490. if (error < 0) {
  491. dev_err(dev, "Unable to claim irq %d; error %d\n",
  492. bdata->irq, error);
  493. return error;
  494. }
  495. return 0;
  496. }
  497. static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
  498. {
  499. struct input_dev *input = ddata->input;
  500. int i;
  501. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  502. struct gpio_button_data *bdata = &ddata->data[i];
  503. if (bdata->gpiod)
  504. gpio_keys_gpio_report_event(bdata);
  505. }
  506. input_sync(input);
  507. }
  508. static int gpio_keys_open(struct input_dev *input)
  509. {
  510. struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
  511. const struct gpio_keys_platform_data *pdata = ddata->pdata;
  512. int error;
  513. if (pdata->enable) {
  514. error = pdata->enable(input->dev.parent);
  515. if (error)
  516. return error;
  517. }
  518. /* Report current state of buttons that are connected to GPIOs */
  519. gpio_keys_report_state(ddata);
  520. return 0;
  521. }
  522. static void gpio_keys_close(struct input_dev *input)
  523. {
  524. struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
  525. const struct gpio_keys_platform_data *pdata = ddata->pdata;
  526. if (pdata->disable)
  527. pdata->disable(input->dev.parent);
  528. }
  529. /*
  530. * Handlers for alternative sources of platform_data
  531. */
  532. #ifdef CONFIG_OF
  533. /*
  534. * Translate OpenFirmware node properties into platform_data
  535. */
  536. static struct gpio_keys_platform_data *
  537. gpio_keys_get_devtree_pdata(struct device *dev)
  538. {
  539. struct device_node *node, *pp;
  540. struct gpio_keys_platform_data *pdata;
  541. struct gpio_keys_button *button;
  542. int error;
  543. int nbuttons;
  544. int i;
  545. node = dev->of_node;
  546. if (!node)
  547. return ERR_PTR(-ENODEV);
  548. nbuttons = of_get_available_child_count(node);
  549. if (nbuttons == 0)
  550. return ERR_PTR(-ENODEV);
  551. pdata = devm_kzalloc(dev,
  552. sizeof(*pdata) + nbuttons * sizeof(*button),
  553. GFP_KERNEL);
  554. if (!pdata)
  555. return ERR_PTR(-ENOMEM);
  556. pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
  557. pdata->nbuttons = nbuttons;
  558. pdata->rep = !!of_get_property(node, "autorepeat", NULL);
  559. of_property_read_string(node, "label", &pdata->name);
  560. i = 0;
  561. for_each_available_child_of_node(node, pp) {
  562. enum of_gpio_flags flags;
  563. button = &pdata->buttons[i++];
  564. button->gpio = of_get_gpio_flags(pp, 0, &flags);
  565. if (button->gpio < 0) {
  566. error = button->gpio;
  567. if (error != -ENOENT) {
  568. if (error != -EPROBE_DEFER)
  569. dev_err(dev,
  570. "Failed to get gpio flags, error: %d\n",
  571. error);
  572. return ERR_PTR(error);
  573. }
  574. } else {
  575. button->active_low = flags & OF_GPIO_ACTIVE_LOW;
  576. }
  577. button->irq = irq_of_parse_and_map(pp, 0);
  578. if (!gpio_is_valid(button->gpio) && !button->irq) {
  579. dev_err(dev, "Found button without gpios or irqs\n");
  580. return ERR_PTR(-EINVAL);
  581. }
  582. if (of_property_read_u32(pp, "linux,code", &button->code)) {
  583. dev_err(dev, "Button without keycode: 0x%x\n",
  584. button->gpio);
  585. return ERR_PTR(-EINVAL);
  586. }
  587. button->desc = of_get_property(pp, "label", NULL);
  588. if (of_property_read_u32(pp, "linux,input-type", &button->type))
  589. button->type = EV_KEY;
  590. button->wakeup = of_property_read_bool(pp, "wakeup-source") ||
  591. /* legacy name */
  592. of_property_read_bool(pp, "gpio-key,wakeup");
  593. button->can_disable = !!of_get_property(pp, "linux,can-disable", NULL);
  594. if (of_property_read_u32(pp, "debounce-interval",
  595. &button->debounce_interval))
  596. button->debounce_interval = 5;
  597. }
  598. if (pdata->nbuttons == 0)
  599. return ERR_PTR(-EINVAL);
  600. return pdata;
  601. }
  602. static const struct of_device_id gpio_keys_of_match[] = {
  603. { .compatible = "gpio-keys", },
  604. { },
  605. };
  606. MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
  607. #else
  608. static inline struct gpio_keys_platform_data *
  609. gpio_keys_get_devtree_pdata(struct device *dev)
  610. {
  611. return ERR_PTR(-ENODEV);
  612. }
  613. #endif
  614. static int gpio_keys_probe(struct platform_device *pdev)
  615. {
  616. struct device *dev = &pdev->dev;
  617. const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
  618. struct gpio_keys_drvdata *ddata;
  619. struct input_dev *input;
  620. size_t size;
  621. int i, error;
  622. int wakeup = 0;
  623. if (!pdata) {
  624. pdata = gpio_keys_get_devtree_pdata(dev);
  625. if (IS_ERR(pdata))
  626. return PTR_ERR(pdata);
  627. }
  628. size = sizeof(struct gpio_keys_drvdata) +
  629. pdata->nbuttons * sizeof(struct gpio_button_data);
  630. ddata = devm_kzalloc(dev, size, GFP_KERNEL);
  631. if (!ddata) {
  632. dev_err(dev, "failed to allocate state\n");
  633. return -ENOMEM;
  634. }
  635. input = devm_input_allocate_device(dev);
  636. if (!input) {
  637. dev_err(dev, "failed to allocate input device\n");
  638. return -ENOMEM;
  639. }
  640. ddata->pdata = pdata;
  641. ddata->input = input;
  642. mutex_init(&ddata->disable_lock);
  643. platform_set_drvdata(pdev, ddata);
  644. input_set_drvdata(input, ddata);
  645. input->name = pdata->name ? : pdev->name;
  646. input->phys = "gpio-keys/input0";
  647. input->dev.parent = &pdev->dev;
  648. input->open = gpio_keys_open;
  649. input->close = gpio_keys_close;
  650. input->id.bustype = BUS_HOST;
  651. input->id.vendor = 0x0001;
  652. input->id.product = 0x0001;
  653. input->id.version = 0x0100;
  654. /* Enable auto repeat feature of Linux input subsystem */
  655. if (pdata->rep)
  656. __set_bit(EV_REP, input->evbit);
  657. for (i = 0; i < pdata->nbuttons; i++) {
  658. const struct gpio_keys_button *button = &pdata->buttons[i];
  659. struct gpio_button_data *bdata = &ddata->data[i];
  660. error = gpio_keys_setup_key(pdev, input, bdata, button);
  661. if (error)
  662. return error;
  663. if (button->wakeup)
  664. wakeup = 1;
  665. }
  666. error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
  667. if (error) {
  668. dev_err(dev, "Unable to export keys/switches, error: %d\n",
  669. error);
  670. return error;
  671. }
  672. error = input_register_device(input);
  673. if (error) {
  674. dev_err(dev, "Unable to register input device, error: %d\n",
  675. error);
  676. goto err_remove_group;
  677. }
  678. device_init_wakeup(&pdev->dev, wakeup);
  679. return 0;
  680. err_remove_group:
  681. sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
  682. return error;
  683. }
  684. static int gpio_keys_remove(struct platform_device *pdev)
  685. {
  686. sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
  687. device_init_wakeup(&pdev->dev, 0);
  688. return 0;
  689. }
  690. #ifdef CONFIG_PM_SLEEP
  691. static int gpio_keys_suspend(struct device *dev)
  692. {
  693. struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
  694. struct input_dev *input = ddata->input;
  695. int i;
  696. if (device_may_wakeup(dev)) {
  697. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  698. struct gpio_button_data *bdata = &ddata->data[i];
  699. if (bdata->button->wakeup)
  700. enable_irq_wake(bdata->irq);
  701. }
  702. } else {
  703. mutex_lock(&input->mutex);
  704. if (input->users)
  705. gpio_keys_close(input);
  706. mutex_unlock(&input->mutex);
  707. }
  708. return 0;
  709. }
  710. static int gpio_keys_resume(struct device *dev)
  711. {
  712. struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
  713. struct input_dev *input = ddata->input;
  714. int error = 0;
  715. int i;
  716. if (device_may_wakeup(dev)) {
  717. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  718. struct gpio_button_data *bdata = &ddata->data[i];
  719. if (bdata->button->wakeup)
  720. disable_irq_wake(bdata->irq);
  721. }
  722. } else {
  723. mutex_lock(&input->mutex);
  724. if (input->users)
  725. error = gpio_keys_open(input);
  726. mutex_unlock(&input->mutex);
  727. }
  728. if (error)
  729. return error;
  730. gpio_keys_report_state(ddata);
  731. return 0;
  732. }
  733. #endif
  734. static SIMPLE_DEV_PM_OPS(gpio_keys_pm_ops, gpio_keys_suspend, gpio_keys_resume);
  735. static struct platform_driver gpio_keys_device_driver = {
  736. .probe = gpio_keys_probe,
  737. .remove = gpio_keys_remove,
  738. .driver = {
  739. .name = "gpio-keys",
  740. .pm = &gpio_keys_pm_ops,
  741. .of_match_table = of_match_ptr(gpio_keys_of_match),
  742. }
  743. };
  744. static int __init gpio_keys_init(void)
  745. {
  746. return platform_driver_register(&gpio_keys_device_driver);
  747. }
  748. static void __exit gpio_keys_exit(void)
  749. {
  750. platform_driver_unregister(&gpio_keys_device_driver);
  751. }
  752. late_initcall(gpio_keys_init);
  753. module_exit(gpio_keys_exit);
  754. MODULE_LICENSE("GPL");
  755. MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
  756. MODULE_DESCRIPTION("Keyboard driver for GPIOs");
  757. MODULE_ALIAS("platform:gpio-keys");