gpio_keys.c 23 KB

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