gpio_keys.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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. };
  45. struct gpio_keys_drvdata {
  46. const struct gpio_keys_platform_data *pdata;
  47. struct input_dev *input;
  48. struct mutex disable_lock;
  49. unsigned short *keymap;
  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->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->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->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, *bdata->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->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. struct input_dev *input = bdata->input;
  367. unsigned long flags;
  368. BUG_ON(irq != bdata->irq);
  369. spin_lock_irqsave(&bdata->lock, flags);
  370. if (!bdata->key_pressed) {
  371. if (bdata->button->wakeup)
  372. pm_wakeup_event(bdata->input->dev.parent, 0);
  373. input_event(input, EV_KEY, *bdata->code, 1);
  374. input_sync(input);
  375. if (!bdata->release_delay) {
  376. input_event(input, EV_KEY, *bdata->code, 0);
  377. input_sync(input);
  378. goto out;
  379. }
  380. bdata->key_pressed = true;
  381. }
  382. if (bdata->release_delay)
  383. mod_timer(&bdata->release_timer,
  384. jiffies + msecs_to_jiffies(bdata->release_delay));
  385. out:
  386. spin_unlock_irqrestore(&bdata->lock, flags);
  387. return IRQ_HANDLED;
  388. }
  389. static void gpio_keys_quiesce_key(void *data)
  390. {
  391. struct gpio_button_data *bdata = data;
  392. if (bdata->gpiod)
  393. cancel_delayed_work_sync(&bdata->work);
  394. else
  395. del_timer_sync(&bdata->release_timer);
  396. }
  397. static int gpio_keys_setup_key(struct platform_device *pdev,
  398. struct input_dev *input,
  399. struct gpio_keys_drvdata *ddata,
  400. const struct gpio_keys_button *button,
  401. int idx,
  402. struct fwnode_handle *child)
  403. {
  404. const char *desc = button->desc ? button->desc : "gpio_keys";
  405. struct device *dev = &pdev->dev;
  406. struct gpio_button_data *bdata = &ddata->data[idx];
  407. irq_handler_t isr;
  408. unsigned long irqflags;
  409. int irq;
  410. int error;
  411. bdata->input = input;
  412. bdata->button = button;
  413. spin_lock_init(&bdata->lock);
  414. if (child) {
  415. bdata->gpiod = devm_fwnode_get_gpiod_from_child(dev, NULL,
  416. child,
  417. GPIOD_IN,
  418. desc);
  419. if (IS_ERR(bdata->gpiod)) {
  420. error = PTR_ERR(bdata->gpiod);
  421. if (error == -ENOENT) {
  422. /*
  423. * GPIO is optional, we may be dealing with
  424. * purely interrupt-driven setup.
  425. */
  426. bdata->gpiod = NULL;
  427. } else {
  428. if (error != -EPROBE_DEFER)
  429. dev_err(dev, "failed to get gpio: %d\n",
  430. error);
  431. return error;
  432. }
  433. }
  434. } else if (gpio_is_valid(button->gpio)) {
  435. /*
  436. * Legacy GPIO number, so request the GPIO here and
  437. * convert it to descriptor.
  438. */
  439. unsigned flags = GPIOF_IN;
  440. if (button->active_low)
  441. flags |= GPIOF_ACTIVE_LOW;
  442. error = devm_gpio_request_one(dev, button->gpio, flags, desc);
  443. if (error < 0) {
  444. dev_err(dev, "Failed to request GPIO %d, error %d\n",
  445. button->gpio, error);
  446. return error;
  447. }
  448. bdata->gpiod = gpio_to_desc(button->gpio);
  449. if (!bdata->gpiod)
  450. return -EINVAL;
  451. }
  452. if (bdata->gpiod) {
  453. if (button->debounce_interval) {
  454. error = gpiod_set_debounce(bdata->gpiod,
  455. button->debounce_interval * 1000);
  456. /* use timer if gpiolib doesn't provide debounce */
  457. if (error < 0)
  458. bdata->software_debounce =
  459. button->debounce_interval;
  460. }
  461. if (button->irq) {
  462. bdata->irq = button->irq;
  463. } else {
  464. irq = gpiod_to_irq(bdata->gpiod);
  465. if (irq < 0) {
  466. error = irq;
  467. dev_err(dev,
  468. "Unable to get irq number for GPIO %d, error %d\n",
  469. button->gpio, error);
  470. return error;
  471. }
  472. bdata->irq = irq;
  473. }
  474. INIT_DELAYED_WORK(&bdata->work, gpio_keys_gpio_work_func);
  475. isr = gpio_keys_gpio_isr;
  476. irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
  477. } else {
  478. if (!button->irq) {
  479. dev_err(dev, "Found button without gpio or irq\n");
  480. return -EINVAL;
  481. }
  482. bdata->irq = button->irq;
  483. if (button->type && button->type != EV_KEY) {
  484. dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n");
  485. return -EINVAL;
  486. }
  487. bdata->release_delay = button->debounce_interval;
  488. setup_timer(&bdata->release_timer,
  489. gpio_keys_irq_timer, (unsigned long)bdata);
  490. isr = gpio_keys_irq_isr;
  491. irqflags = 0;
  492. }
  493. bdata->code = &ddata->keymap[idx];
  494. *bdata->code = button->code;
  495. input_set_capability(input, button->type ?: EV_KEY, *bdata->code);
  496. /*
  497. * Install custom action to cancel release timer and
  498. * workqueue item.
  499. */
  500. error = devm_add_action(dev, gpio_keys_quiesce_key, bdata);
  501. if (error) {
  502. dev_err(dev, "failed to register quiesce action, error: %d\n",
  503. error);
  504. return error;
  505. }
  506. /*
  507. * If platform has specified that the button can be disabled,
  508. * we don't want it to share the interrupt line.
  509. */
  510. if (!button->can_disable)
  511. irqflags |= IRQF_SHARED;
  512. error = devm_request_any_context_irq(dev, bdata->irq, isr, irqflags,
  513. desc, bdata);
  514. if (error < 0) {
  515. dev_err(dev, "Unable to claim irq %d; error %d\n",
  516. bdata->irq, error);
  517. return error;
  518. }
  519. return 0;
  520. }
  521. static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
  522. {
  523. struct input_dev *input = ddata->input;
  524. int i;
  525. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  526. struct gpio_button_data *bdata = &ddata->data[i];
  527. if (bdata->gpiod)
  528. gpio_keys_gpio_report_event(bdata);
  529. }
  530. input_sync(input);
  531. }
  532. static int gpio_keys_open(struct input_dev *input)
  533. {
  534. struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
  535. const struct gpio_keys_platform_data *pdata = ddata->pdata;
  536. int error;
  537. if (pdata->enable) {
  538. error = pdata->enable(input->dev.parent);
  539. if (error)
  540. return error;
  541. }
  542. /* Report current state of buttons that are connected to GPIOs */
  543. gpio_keys_report_state(ddata);
  544. return 0;
  545. }
  546. static void gpio_keys_close(struct input_dev *input)
  547. {
  548. struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
  549. const struct gpio_keys_platform_data *pdata = ddata->pdata;
  550. if (pdata->disable)
  551. pdata->disable(input->dev.parent);
  552. }
  553. /*
  554. * Handlers for alternative sources of platform_data
  555. */
  556. /*
  557. * Translate properties into platform_data
  558. */
  559. static struct gpio_keys_platform_data *
  560. gpio_keys_get_devtree_pdata(struct device *dev)
  561. {
  562. struct gpio_keys_platform_data *pdata;
  563. struct gpio_keys_button *button;
  564. struct fwnode_handle *child;
  565. int nbuttons;
  566. nbuttons = device_get_child_node_count(dev);
  567. if (nbuttons == 0)
  568. return ERR_PTR(-ENODEV);
  569. pdata = devm_kzalloc(dev,
  570. sizeof(*pdata) + nbuttons * sizeof(*button),
  571. GFP_KERNEL);
  572. if (!pdata)
  573. return ERR_PTR(-ENOMEM);
  574. button = (struct gpio_keys_button *)(pdata + 1);
  575. pdata->buttons = button;
  576. pdata->nbuttons = nbuttons;
  577. pdata->rep = device_property_read_bool(dev, "autorepeat");
  578. device_property_read_string(dev, "label", &pdata->name);
  579. device_for_each_child_node(dev, child) {
  580. if (is_of_node(child))
  581. button->irq =
  582. irq_of_parse_and_map(to_of_node(child), 0);
  583. if (fwnode_property_read_u32(child, "linux,code",
  584. &button->code)) {
  585. dev_err(dev, "Button without keycode\n");
  586. fwnode_handle_put(child);
  587. return ERR_PTR(-EINVAL);
  588. }
  589. fwnode_property_read_string(child, "label", &button->desc);
  590. if (fwnode_property_read_u32(child, "linux,input-type",
  591. &button->type))
  592. button->type = EV_KEY;
  593. button->wakeup =
  594. fwnode_property_read_bool(child, "wakeup-source") ||
  595. /* legacy name */
  596. fwnode_property_read_bool(child, "gpio-key,wakeup");
  597. button->can_disable =
  598. fwnode_property_read_bool(child, "linux,can-disable");
  599. if (fwnode_property_read_u32(child, "debounce-interval",
  600. &button->debounce_interval))
  601. button->debounce_interval = 5;
  602. button++;
  603. }
  604. return pdata;
  605. }
  606. static const struct of_device_id gpio_keys_of_match[] = {
  607. { .compatible = "gpio-keys", },
  608. { },
  609. };
  610. MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
  611. static int gpio_keys_probe(struct platform_device *pdev)
  612. {
  613. struct device *dev = &pdev->dev;
  614. const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
  615. struct fwnode_handle *child = NULL;
  616. struct gpio_keys_drvdata *ddata;
  617. struct input_dev *input;
  618. size_t size;
  619. int i, error;
  620. int wakeup = 0;
  621. if (!pdata) {
  622. pdata = gpio_keys_get_devtree_pdata(dev);
  623. if (IS_ERR(pdata))
  624. return PTR_ERR(pdata);
  625. }
  626. size = sizeof(struct gpio_keys_drvdata) +
  627. pdata->nbuttons * sizeof(struct gpio_button_data);
  628. ddata = devm_kzalloc(dev, size, GFP_KERNEL);
  629. if (!ddata) {
  630. dev_err(dev, "failed to allocate state\n");
  631. return -ENOMEM;
  632. }
  633. ddata->keymap = devm_kcalloc(dev,
  634. pdata->nbuttons, sizeof(ddata->keymap[0]),
  635. GFP_KERNEL);
  636. if (!ddata->keymap)
  637. return -ENOMEM;
  638. input = devm_input_allocate_device(dev);
  639. if (!input) {
  640. dev_err(dev, "failed to allocate input device\n");
  641. return -ENOMEM;
  642. }
  643. ddata->pdata = pdata;
  644. ddata->input = input;
  645. mutex_init(&ddata->disable_lock);
  646. platform_set_drvdata(pdev, ddata);
  647. input_set_drvdata(input, ddata);
  648. input->name = pdata->name ? : pdev->name;
  649. input->phys = "gpio-keys/input0";
  650. input->dev.parent = dev;
  651. input->open = gpio_keys_open;
  652. input->close = gpio_keys_close;
  653. input->id.bustype = BUS_HOST;
  654. input->id.vendor = 0x0001;
  655. input->id.product = 0x0001;
  656. input->id.version = 0x0100;
  657. input->keycode = ddata->keymap;
  658. input->keycodesize = sizeof(ddata->keymap[0]);
  659. input->keycodemax = pdata->nbuttons;
  660. /* Enable auto repeat feature of Linux input subsystem */
  661. if (pdata->rep)
  662. __set_bit(EV_REP, input->evbit);
  663. for (i = 0; i < pdata->nbuttons; i++) {
  664. const struct gpio_keys_button *button = &pdata->buttons[i];
  665. if (!dev_get_platdata(dev)) {
  666. child = device_get_next_child_node(dev, child);
  667. if (!child) {
  668. dev_err(dev,
  669. "missing child device node for entry %d\n",
  670. i);
  671. return -EINVAL;
  672. }
  673. }
  674. error = gpio_keys_setup_key(pdev, input, ddata,
  675. button, i, child);
  676. if (error) {
  677. fwnode_handle_put(child);
  678. return error;
  679. }
  680. if (button->wakeup)
  681. wakeup = 1;
  682. }
  683. fwnode_handle_put(child);
  684. error = sysfs_create_group(&dev->kobj, &gpio_keys_attr_group);
  685. if (error) {
  686. dev_err(dev, "Unable to export keys/switches, error: %d\n",
  687. error);
  688. return error;
  689. }
  690. error = input_register_device(input);
  691. if (error) {
  692. dev_err(dev, "Unable to register input device, error: %d\n",
  693. error);
  694. goto err_remove_group;
  695. }
  696. device_init_wakeup(dev, wakeup);
  697. return 0;
  698. err_remove_group:
  699. sysfs_remove_group(&dev->kobj, &gpio_keys_attr_group);
  700. return error;
  701. }
  702. static int gpio_keys_remove(struct platform_device *pdev)
  703. {
  704. sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
  705. return 0;
  706. }
  707. static int __maybe_unused gpio_keys_suspend(struct device *dev)
  708. {
  709. struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
  710. struct input_dev *input = ddata->input;
  711. int i;
  712. if (device_may_wakeup(dev)) {
  713. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  714. struct gpio_button_data *bdata = &ddata->data[i];
  715. if (bdata->button->wakeup)
  716. enable_irq_wake(bdata->irq);
  717. }
  718. } else {
  719. mutex_lock(&input->mutex);
  720. if (input->users)
  721. gpio_keys_close(input);
  722. mutex_unlock(&input->mutex);
  723. }
  724. return 0;
  725. }
  726. static int __maybe_unused gpio_keys_resume(struct device *dev)
  727. {
  728. struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
  729. struct input_dev *input = ddata->input;
  730. int error = 0;
  731. int i;
  732. if (device_may_wakeup(dev)) {
  733. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  734. struct gpio_button_data *bdata = &ddata->data[i];
  735. if (bdata->button->wakeup)
  736. disable_irq_wake(bdata->irq);
  737. }
  738. } else {
  739. mutex_lock(&input->mutex);
  740. if (input->users)
  741. error = gpio_keys_open(input);
  742. mutex_unlock(&input->mutex);
  743. }
  744. if (error)
  745. return error;
  746. gpio_keys_report_state(ddata);
  747. return 0;
  748. }
  749. static SIMPLE_DEV_PM_OPS(gpio_keys_pm_ops, gpio_keys_suspend, gpio_keys_resume);
  750. static struct platform_driver gpio_keys_device_driver = {
  751. .probe = gpio_keys_probe,
  752. .remove = gpio_keys_remove,
  753. .driver = {
  754. .name = "gpio-keys",
  755. .pm = &gpio_keys_pm_ops,
  756. .of_match_table = gpio_keys_of_match,
  757. }
  758. };
  759. static int __init gpio_keys_init(void)
  760. {
  761. return platform_driver_register(&gpio_keys_device_driver);
  762. }
  763. static void __exit gpio_keys_exit(void)
  764. {
  765. platform_driver_unregister(&gpio_keys_device_driver);
  766. }
  767. late_initcall(gpio_keys_init);
  768. module_exit(gpio_keys_exit);
  769. MODULE_LICENSE("GPL");
  770. MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
  771. MODULE_DESCRIPTION("Keyboard driver for GPIOs");
  772. MODULE_ALIAS("platform:gpio-keys");