gpio_keys.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for keys on GPIO lines capable of generating interrupts.
  4. *
  5. * Copyright 2005 Phil Blundell
  6. * Copyright 2010, 2011 David Jander <david@protonic.nl>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/fs.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/irq.h>
  13. #include <linux/sched.h>
  14. #include <linux/pm.h>
  15. #include <linux/slab.h>
  16. #include <linux/sysctl.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/delay.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/input.h>
  21. #include <linux/gpio_keys.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/gpio.h>
  24. #include <linux/gpio/consumer.h>
  25. #include <linux/of.h>
  26. #include <linux/of_irq.h>
  27. #include <linux/spinlock.h>
  28. #include <dt-bindings/input/gpio-keys.h>
  29. struct gpio_button_data {
  30. const struct gpio_keys_button *button;
  31. struct input_dev *input;
  32. struct gpio_desc *gpiod;
  33. unsigned short *code;
  34. struct timer_list release_timer;
  35. unsigned int release_delay; /* in msecs, for IRQ-only buttons */
  36. struct delayed_work work;
  37. unsigned int software_debounce; /* in msecs, for GPIO-driven buttons */
  38. unsigned int irq;
  39. unsigned int wakeup_trigger_type;
  40. spinlock_t lock;
  41. bool disabled;
  42. bool key_pressed;
  43. bool suspended;
  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[];
  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 = bitmap_zalloc(n_events, 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. bitmap_free(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 = bitmap_zalloc(n_events, 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. bitmap_free(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. ATTRIBUTE_GROUPS(gpio_keys);
  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, *bdata->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. const struct gpio_keys_button *button = bdata->button;
  343. pm_stay_awake(bdata->input->dev.parent);
  344. if (bdata->suspended &&
  345. (button->type == 0 || button->type == EV_KEY)) {
  346. /*
  347. * Simulate wakeup key press in case the key has
  348. * already released by the time we got interrupt
  349. * handler to run.
  350. */
  351. input_report_key(bdata->input, button->code, 1);
  352. }
  353. }
  354. mod_delayed_work(system_wq,
  355. &bdata->work,
  356. msecs_to_jiffies(bdata->software_debounce));
  357. return IRQ_HANDLED;
  358. }
  359. static void gpio_keys_irq_timer(struct timer_list *t)
  360. {
  361. struct gpio_button_data *bdata = from_timer(bdata, t, release_timer);
  362. struct input_dev *input = bdata->input;
  363. unsigned long flags;
  364. spin_lock_irqsave(&bdata->lock, flags);
  365. if (bdata->key_pressed) {
  366. input_event(input, EV_KEY, *bdata->code, 0);
  367. input_sync(input);
  368. bdata->key_pressed = false;
  369. }
  370. spin_unlock_irqrestore(&bdata->lock, flags);
  371. }
  372. static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
  373. {
  374. struct gpio_button_data *bdata = dev_id;
  375. struct input_dev *input = bdata->input;
  376. unsigned long flags;
  377. BUG_ON(irq != bdata->irq);
  378. spin_lock_irqsave(&bdata->lock, flags);
  379. if (!bdata->key_pressed) {
  380. if (bdata->button->wakeup)
  381. pm_wakeup_event(bdata->input->dev.parent, 0);
  382. input_event(input, EV_KEY, *bdata->code, 1);
  383. input_sync(input);
  384. if (!bdata->release_delay) {
  385. input_event(input, EV_KEY, *bdata->code, 0);
  386. input_sync(input);
  387. goto out;
  388. }
  389. bdata->key_pressed = true;
  390. }
  391. if (bdata->release_delay)
  392. mod_timer(&bdata->release_timer,
  393. jiffies + msecs_to_jiffies(bdata->release_delay));
  394. out:
  395. spin_unlock_irqrestore(&bdata->lock, flags);
  396. return IRQ_HANDLED;
  397. }
  398. static void gpio_keys_quiesce_key(void *data)
  399. {
  400. struct gpio_button_data *bdata = data;
  401. if (bdata->gpiod)
  402. cancel_delayed_work_sync(&bdata->work);
  403. else
  404. del_timer_sync(&bdata->release_timer);
  405. }
  406. static int gpio_keys_setup_key(struct platform_device *pdev,
  407. struct input_dev *input,
  408. struct gpio_keys_drvdata *ddata,
  409. const struct gpio_keys_button *button,
  410. int idx,
  411. struct fwnode_handle *child)
  412. {
  413. const char *desc = button->desc ? button->desc : "gpio_keys";
  414. struct device *dev = &pdev->dev;
  415. struct gpio_button_data *bdata = &ddata->data[idx];
  416. irq_handler_t isr;
  417. unsigned long irqflags;
  418. int irq;
  419. int error;
  420. bdata->input = input;
  421. bdata->button = button;
  422. spin_lock_init(&bdata->lock);
  423. if (child) {
  424. bdata->gpiod = devm_fwnode_gpiod_get(dev, child,
  425. NULL, GPIOD_IN, desc);
  426. if (IS_ERR(bdata->gpiod)) {
  427. error = PTR_ERR(bdata->gpiod);
  428. if (error == -ENOENT) {
  429. /*
  430. * GPIO is optional, we may be dealing with
  431. * purely interrupt-driven setup.
  432. */
  433. bdata->gpiod = NULL;
  434. } else {
  435. if (error != -EPROBE_DEFER)
  436. dev_err(dev, "failed to get gpio: %d\n",
  437. error);
  438. return error;
  439. }
  440. }
  441. } else if (gpio_is_valid(button->gpio)) {
  442. /*
  443. * Legacy GPIO number, so request the GPIO here and
  444. * convert it to descriptor.
  445. */
  446. unsigned flags = GPIOF_IN;
  447. if (button->active_low)
  448. flags |= GPIOF_ACTIVE_LOW;
  449. error = devm_gpio_request_one(dev, button->gpio, flags, desc);
  450. if (error < 0) {
  451. dev_err(dev, "Failed to request GPIO %d, error %d\n",
  452. button->gpio, error);
  453. return error;
  454. }
  455. bdata->gpiod = gpio_to_desc(button->gpio);
  456. if (!bdata->gpiod)
  457. return -EINVAL;
  458. }
  459. if (bdata->gpiod) {
  460. bool active_low = gpiod_is_active_low(bdata->gpiod);
  461. if (button->debounce_interval) {
  462. error = gpiod_set_debounce(bdata->gpiod,
  463. button->debounce_interval * 1000);
  464. /* use timer if gpiolib doesn't provide debounce */
  465. if (error < 0)
  466. bdata->software_debounce =
  467. button->debounce_interval;
  468. }
  469. if (button->irq) {
  470. bdata->irq = button->irq;
  471. } else {
  472. irq = gpiod_to_irq(bdata->gpiod);
  473. if (irq < 0) {
  474. error = irq;
  475. dev_err(dev,
  476. "Unable to get irq number for GPIO %d, error %d\n",
  477. button->gpio, error);
  478. return error;
  479. }
  480. bdata->irq = irq;
  481. }
  482. INIT_DELAYED_WORK(&bdata->work, gpio_keys_gpio_work_func);
  483. isr = gpio_keys_gpio_isr;
  484. irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
  485. switch (button->wakeup_event_action) {
  486. case EV_ACT_ASSERTED:
  487. bdata->wakeup_trigger_type = active_low ?
  488. IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
  489. break;
  490. case EV_ACT_DEASSERTED:
  491. bdata->wakeup_trigger_type = active_low ?
  492. IRQ_TYPE_EDGE_RISING : IRQ_TYPE_EDGE_FALLING;
  493. break;
  494. case EV_ACT_ANY:
  495. default:
  496. /*
  497. * For other cases, we are OK letting suspend/resume
  498. * not reconfigure the trigger type.
  499. */
  500. break;
  501. }
  502. } else {
  503. if (!button->irq) {
  504. dev_err(dev, "Found button without gpio or irq\n");
  505. return -EINVAL;
  506. }
  507. bdata->irq = button->irq;
  508. if (button->type && button->type != EV_KEY) {
  509. dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n");
  510. return -EINVAL;
  511. }
  512. bdata->release_delay = button->debounce_interval;
  513. timer_setup(&bdata->release_timer, gpio_keys_irq_timer, 0);
  514. isr = gpio_keys_irq_isr;
  515. irqflags = 0;
  516. /*
  517. * For IRQ buttons, there is no interrupt for release.
  518. * So we don't need to reconfigure the trigger type for wakeup.
  519. */
  520. }
  521. bdata->code = &ddata->keymap[idx];
  522. *bdata->code = button->code;
  523. input_set_capability(input, button->type ?: EV_KEY, *bdata->code);
  524. /*
  525. * Install custom action to cancel release timer and
  526. * workqueue item.
  527. */
  528. error = devm_add_action(dev, gpio_keys_quiesce_key, bdata);
  529. if (error) {
  530. dev_err(dev, "failed to register quiesce action, error: %d\n",
  531. error);
  532. return error;
  533. }
  534. /*
  535. * If platform has specified that the button can be disabled,
  536. * we don't want it to share the interrupt line.
  537. */
  538. if (!button->can_disable)
  539. irqflags |= IRQF_SHARED;
  540. error = devm_request_any_context_irq(dev, bdata->irq, isr, irqflags,
  541. desc, bdata);
  542. if (error < 0) {
  543. dev_err(dev, "Unable to claim irq %d; error %d\n",
  544. bdata->irq, error);
  545. return error;
  546. }
  547. return 0;
  548. }
  549. static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
  550. {
  551. struct input_dev *input = ddata->input;
  552. int i;
  553. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  554. struct gpio_button_data *bdata = &ddata->data[i];
  555. if (bdata->gpiod)
  556. gpio_keys_gpio_report_event(bdata);
  557. }
  558. input_sync(input);
  559. }
  560. static int gpio_keys_open(struct input_dev *input)
  561. {
  562. struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
  563. const struct gpio_keys_platform_data *pdata = ddata->pdata;
  564. int error;
  565. if (pdata->enable) {
  566. error = pdata->enable(input->dev.parent);
  567. if (error)
  568. return error;
  569. }
  570. /* Report current state of buttons that are connected to GPIOs */
  571. gpio_keys_report_state(ddata);
  572. return 0;
  573. }
  574. static void gpio_keys_close(struct input_dev *input)
  575. {
  576. struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
  577. const struct gpio_keys_platform_data *pdata = ddata->pdata;
  578. if (pdata->disable)
  579. pdata->disable(input->dev.parent);
  580. }
  581. /*
  582. * Handlers for alternative sources of platform_data
  583. */
  584. /*
  585. * Translate properties into platform_data
  586. */
  587. static struct gpio_keys_platform_data *
  588. gpio_keys_get_devtree_pdata(struct device *dev)
  589. {
  590. struct gpio_keys_platform_data *pdata;
  591. struct gpio_keys_button *button;
  592. struct fwnode_handle *child;
  593. int nbuttons;
  594. nbuttons = device_get_child_node_count(dev);
  595. if (nbuttons == 0)
  596. return ERR_PTR(-ENODEV);
  597. pdata = devm_kzalloc(dev,
  598. sizeof(*pdata) + nbuttons * sizeof(*button),
  599. GFP_KERNEL);
  600. if (!pdata)
  601. return ERR_PTR(-ENOMEM);
  602. button = (struct gpio_keys_button *)(pdata + 1);
  603. pdata->buttons = button;
  604. pdata->nbuttons = nbuttons;
  605. pdata->rep = device_property_read_bool(dev, "autorepeat");
  606. device_property_read_string(dev, "label", &pdata->name);
  607. device_for_each_child_node(dev, child) {
  608. if (is_of_node(child))
  609. button->irq =
  610. irq_of_parse_and_map(to_of_node(child), 0);
  611. if (fwnode_property_read_u32(child, "linux,code",
  612. &button->code)) {
  613. dev_err(dev, "Button without keycode\n");
  614. fwnode_handle_put(child);
  615. return ERR_PTR(-EINVAL);
  616. }
  617. fwnode_property_read_string(child, "label", &button->desc);
  618. if (fwnode_property_read_u32(child, "linux,input-type",
  619. &button->type))
  620. button->type = EV_KEY;
  621. button->wakeup =
  622. fwnode_property_read_bool(child, "wakeup-source") ||
  623. /* legacy name */
  624. fwnode_property_read_bool(child, "gpio-key,wakeup");
  625. fwnode_property_read_u32(child, "wakeup-event-action",
  626. &button->wakeup_event_action);
  627. button->can_disable =
  628. fwnode_property_read_bool(child, "linux,can-disable");
  629. if (fwnode_property_read_u32(child, "debounce-interval",
  630. &button->debounce_interval))
  631. button->debounce_interval = 5;
  632. button++;
  633. }
  634. return pdata;
  635. }
  636. static const struct of_device_id gpio_keys_of_match[] = {
  637. { .compatible = "gpio-keys", },
  638. { },
  639. };
  640. MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
  641. static int gpio_keys_probe(struct platform_device *pdev)
  642. {
  643. struct device *dev = &pdev->dev;
  644. const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
  645. struct fwnode_handle *child = NULL;
  646. struct gpio_keys_drvdata *ddata;
  647. struct input_dev *input;
  648. int i, error;
  649. int wakeup = 0;
  650. if (!pdata) {
  651. pdata = gpio_keys_get_devtree_pdata(dev);
  652. if (IS_ERR(pdata))
  653. return PTR_ERR(pdata);
  654. }
  655. ddata = devm_kzalloc(dev, struct_size(ddata, data, pdata->nbuttons),
  656. GFP_KERNEL);
  657. if (!ddata) {
  658. dev_err(dev, "failed to allocate state\n");
  659. return -ENOMEM;
  660. }
  661. ddata->keymap = devm_kcalloc(dev,
  662. pdata->nbuttons, sizeof(ddata->keymap[0]),
  663. GFP_KERNEL);
  664. if (!ddata->keymap)
  665. return -ENOMEM;
  666. input = devm_input_allocate_device(dev);
  667. if (!input) {
  668. dev_err(dev, "failed to allocate input device\n");
  669. return -ENOMEM;
  670. }
  671. ddata->pdata = pdata;
  672. ddata->input = input;
  673. mutex_init(&ddata->disable_lock);
  674. platform_set_drvdata(pdev, ddata);
  675. input_set_drvdata(input, ddata);
  676. input->name = pdata->name ? : pdev->name;
  677. input->phys = "gpio-keys/input0";
  678. input->dev.parent = dev;
  679. input->open = gpio_keys_open;
  680. input->close = gpio_keys_close;
  681. input->id.bustype = BUS_HOST;
  682. input->id.vendor = 0x0001;
  683. input->id.product = 0x0001;
  684. input->id.version = 0x0100;
  685. input->keycode = ddata->keymap;
  686. input->keycodesize = sizeof(ddata->keymap[0]);
  687. input->keycodemax = pdata->nbuttons;
  688. /* Enable auto repeat feature of Linux input subsystem */
  689. if (pdata->rep)
  690. __set_bit(EV_REP, input->evbit);
  691. for (i = 0; i < pdata->nbuttons; i++) {
  692. const struct gpio_keys_button *button = &pdata->buttons[i];
  693. if (!dev_get_platdata(dev)) {
  694. child = device_get_next_child_node(dev, child);
  695. if (!child) {
  696. dev_err(dev,
  697. "missing child device node for entry %d\n",
  698. i);
  699. return -EINVAL;
  700. }
  701. }
  702. error = gpio_keys_setup_key(pdev, input, ddata,
  703. button, i, child);
  704. if (error) {
  705. fwnode_handle_put(child);
  706. return error;
  707. }
  708. if (button->wakeup)
  709. wakeup = 1;
  710. }
  711. fwnode_handle_put(child);
  712. error = input_register_device(input);
  713. if (error) {
  714. dev_err(dev, "Unable to register input device, error: %d\n",
  715. error);
  716. return error;
  717. }
  718. device_init_wakeup(dev, wakeup);
  719. return 0;
  720. }
  721. static int __maybe_unused
  722. gpio_keys_button_enable_wakeup(struct gpio_button_data *bdata)
  723. {
  724. int error;
  725. error = enable_irq_wake(bdata->irq);
  726. if (error) {
  727. dev_err(bdata->input->dev.parent,
  728. "failed to configure IRQ %d as wakeup source: %d\n",
  729. bdata->irq, error);
  730. return error;
  731. }
  732. if (bdata->wakeup_trigger_type) {
  733. error = irq_set_irq_type(bdata->irq,
  734. bdata->wakeup_trigger_type);
  735. if (error) {
  736. dev_err(bdata->input->dev.parent,
  737. "failed to set wakeup trigger %08x for IRQ %d: %d\n",
  738. bdata->wakeup_trigger_type, bdata->irq, error);
  739. disable_irq_wake(bdata->irq);
  740. return error;
  741. }
  742. }
  743. return 0;
  744. }
  745. static void __maybe_unused
  746. gpio_keys_button_disable_wakeup(struct gpio_button_data *bdata)
  747. {
  748. int error;
  749. /*
  750. * The trigger type is always both edges for gpio-based keys and we do
  751. * not support changing wakeup trigger for interrupt-based keys.
  752. */
  753. if (bdata->wakeup_trigger_type) {
  754. error = irq_set_irq_type(bdata->irq, IRQ_TYPE_EDGE_BOTH);
  755. if (error)
  756. dev_warn(bdata->input->dev.parent,
  757. "failed to restore interrupt trigger for IRQ %d: %d\n",
  758. bdata->irq, error);
  759. }
  760. error = disable_irq_wake(bdata->irq);
  761. if (error)
  762. dev_warn(bdata->input->dev.parent,
  763. "failed to disable IRQ %d as wake source: %d\n",
  764. bdata->irq, error);
  765. }
  766. static int __maybe_unused
  767. gpio_keys_enable_wakeup(struct gpio_keys_drvdata *ddata)
  768. {
  769. struct gpio_button_data *bdata;
  770. int error;
  771. int i;
  772. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  773. bdata = &ddata->data[i];
  774. if (bdata->button->wakeup) {
  775. error = gpio_keys_button_enable_wakeup(bdata);
  776. if (error)
  777. goto err_out;
  778. }
  779. bdata->suspended = true;
  780. }
  781. return 0;
  782. err_out:
  783. while (i--) {
  784. bdata = &ddata->data[i];
  785. if (bdata->button->wakeup)
  786. gpio_keys_button_disable_wakeup(bdata);
  787. bdata->suspended = false;
  788. }
  789. return error;
  790. }
  791. static void __maybe_unused
  792. gpio_keys_disable_wakeup(struct gpio_keys_drvdata *ddata)
  793. {
  794. struct gpio_button_data *bdata;
  795. int i;
  796. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  797. bdata = &ddata->data[i];
  798. bdata->suspended = false;
  799. if (irqd_is_wakeup_set(irq_get_irq_data(bdata->irq)))
  800. gpio_keys_button_disable_wakeup(bdata);
  801. }
  802. }
  803. static int __maybe_unused gpio_keys_suspend(struct device *dev)
  804. {
  805. struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
  806. struct input_dev *input = ddata->input;
  807. int error;
  808. if (device_may_wakeup(dev)) {
  809. error = gpio_keys_enable_wakeup(ddata);
  810. if (error)
  811. return error;
  812. } else {
  813. mutex_lock(&input->mutex);
  814. if (input->users)
  815. gpio_keys_close(input);
  816. mutex_unlock(&input->mutex);
  817. }
  818. return 0;
  819. }
  820. static int __maybe_unused gpio_keys_resume(struct device *dev)
  821. {
  822. struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
  823. struct input_dev *input = ddata->input;
  824. int error = 0;
  825. if (device_may_wakeup(dev)) {
  826. gpio_keys_disable_wakeup(ddata);
  827. } else {
  828. mutex_lock(&input->mutex);
  829. if (input->users)
  830. error = gpio_keys_open(input);
  831. mutex_unlock(&input->mutex);
  832. }
  833. if (error)
  834. return error;
  835. gpio_keys_report_state(ddata);
  836. return 0;
  837. }
  838. static SIMPLE_DEV_PM_OPS(gpio_keys_pm_ops, gpio_keys_suspend, gpio_keys_resume);
  839. static void gpio_keys_shutdown(struct platform_device *pdev)
  840. {
  841. int ret;
  842. ret = gpio_keys_suspend(&pdev->dev);
  843. if (ret)
  844. dev_err(&pdev->dev, "failed to shutdown\n");
  845. }
  846. static struct platform_driver gpio_keys_device_driver = {
  847. .probe = gpio_keys_probe,
  848. .shutdown = gpio_keys_shutdown,
  849. .driver = {
  850. .name = "gpio-keys",
  851. .pm = &gpio_keys_pm_ops,
  852. .of_match_table = gpio_keys_of_match,
  853. .dev_groups = gpio_keys_groups,
  854. }
  855. };
  856. static int __init gpio_keys_init(void)
  857. {
  858. return platform_driver_register(&gpio_keys_device_driver);
  859. }
  860. static void __exit gpio_keys_exit(void)
  861. {
  862. platform_driver_unregister(&gpio_keys_device_driver);
  863. }
  864. late_initcall(gpio_keys_init);
  865. module_exit(gpio_keys_exit);
  866. MODULE_LICENSE("GPL");
  867. MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
  868. MODULE_DESCRIPTION("Keyboard driver for GPIOs");
  869. MODULE_ALIAS("platform:gpio-keys");