gpio_keys.c 23 KB

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