gpio_keys.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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/hrtimer.h>
  10. #include <linux/init.h>
  11. #include <linux/fs.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/sched.h>
  15. #include <linux/pm.h>
  16. #include <linux/slab.h>
  17. #include <linux/sysctl.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/delay.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/input.h>
  22. #include <linux/gpio_keys.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/gpio.h>
  25. #include <linux/gpio/consumer.h>
  26. #include <linux/of.h>
  27. #include <linux/of_irq.h>
  28. #include <linux/spinlock.h>
  29. #include <dt-bindings/input/gpio-keys.h>
  30. struct gpio_button_data {
  31. const struct gpio_keys_button *button;
  32. struct input_dev *input;
  33. struct gpio_desc *gpiod;
  34. unsigned short *code;
  35. struct hrtimer release_timer;
  36. unsigned int release_delay; /* in msecs, for IRQ-only buttons */
  37. struct delayed_work work;
  38. struct hrtimer debounce_timer;
  39. unsigned int software_debounce; /* in msecs, for GPIO-driven buttons */
  40. unsigned int irq;
  41. unsigned int wakeirq;
  42. unsigned int wakeup_trigger_type;
  43. spinlock_t lock;
  44. bool disabled;
  45. bool key_pressed;
  46. bool suspended;
  47. bool debounce_use_hrtimer;
  48. };
  49. struct gpio_keys_drvdata {
  50. const struct gpio_keys_platform_data *pdata;
  51. struct input_dev *input;
  52. struct mutex disable_lock;
  53. unsigned short *keymap;
  54. struct gpio_button_data data[];
  55. };
  56. /*
  57. * SYSFS interface for enabling/disabling keys and switches:
  58. *
  59. * There are 4 attributes under /sys/devices/platform/gpio-keys/
  60. * keys [ro] - bitmap of keys (EV_KEY) which can be
  61. * disabled
  62. * switches [ro] - bitmap of switches (EV_SW) which can be
  63. * disabled
  64. * disabled_keys [rw] - bitmap of keys currently disabled
  65. * disabled_switches [rw] - bitmap of switches currently disabled
  66. *
  67. * Userland can change these values and hence disable event generation
  68. * for each key (or switch). Disabling a key means its interrupt line
  69. * is disabled.
  70. *
  71. * For example, if we have following switches set up as gpio-keys:
  72. * SW_DOCK = 5
  73. * SW_CAMERA_LENS_COVER = 9
  74. * SW_KEYPAD_SLIDE = 10
  75. * SW_FRONT_PROXIMITY = 11
  76. * This is read from switches:
  77. * 11-9,5
  78. * Next we want to disable proximity (11) and dock (5), we write:
  79. * 11,5
  80. * to file disabled_switches. Now proximity and dock IRQs are disabled.
  81. * This can be verified by reading the file disabled_switches:
  82. * 11,5
  83. * If we now want to enable proximity (11) switch we write:
  84. * 5
  85. * to disabled_switches.
  86. *
  87. * We can disable only those keys which don't allow sharing the irq.
  88. */
  89. /**
  90. * get_n_events_by_type() - returns maximum number of events per @type
  91. * @type: type of button (%EV_KEY, %EV_SW)
  92. *
  93. * Return value of this function can be used to allocate bitmap
  94. * large enough to hold all bits for given type.
  95. */
  96. static int get_n_events_by_type(int type)
  97. {
  98. BUG_ON(type != EV_SW && type != EV_KEY);
  99. return (type == EV_KEY) ? KEY_CNT : SW_CNT;
  100. }
  101. /**
  102. * get_bm_events_by_type() - returns bitmap of supported events per @type
  103. * @dev: input device from which bitmap is retrieved
  104. * @type: type of button (%EV_KEY, %EV_SW)
  105. *
  106. * Return value of this function can be used to allocate bitmap
  107. * large enough to hold all bits for given type.
  108. */
  109. static const unsigned long *get_bm_events_by_type(struct input_dev *dev,
  110. int type)
  111. {
  112. BUG_ON(type != EV_SW && type != EV_KEY);
  113. return (type == EV_KEY) ? dev->keybit : dev->swbit;
  114. }
  115. static void gpio_keys_quiesce_key(void *data)
  116. {
  117. struct gpio_button_data *bdata = data;
  118. if (!bdata->gpiod)
  119. hrtimer_cancel(&bdata->release_timer);
  120. else if (bdata->debounce_use_hrtimer)
  121. hrtimer_cancel(&bdata->debounce_timer);
  122. else
  123. cancel_delayed_work_sync(&bdata->work);
  124. }
  125. /**
  126. * gpio_keys_disable_button() - disables given GPIO button
  127. * @bdata: button data for button to be disabled
  128. *
  129. * Disables button pointed by @bdata. This is done by masking
  130. * IRQ line. After this function is called, button won't generate
  131. * input events anymore. Note that one can only disable buttons
  132. * that don't share IRQs.
  133. *
  134. * Make sure that @bdata->disable_lock is locked when entering
  135. * this function to avoid races when concurrent threads are
  136. * disabling buttons at the same time.
  137. */
  138. static void gpio_keys_disable_button(struct gpio_button_data *bdata)
  139. {
  140. if (!bdata->disabled) {
  141. /*
  142. * Disable IRQ and associated timer/work structure.
  143. */
  144. disable_irq(bdata->irq);
  145. gpio_keys_quiesce_key(bdata);
  146. bdata->disabled = true;
  147. }
  148. }
  149. /**
  150. * gpio_keys_enable_button() - enables given GPIO button
  151. * @bdata: button data for button to be disabled
  152. *
  153. * Enables given button pointed by @bdata.
  154. *
  155. * Make sure that @bdata->disable_lock is locked when entering
  156. * this function to avoid races with concurrent threads trying
  157. * to enable the same button at the same time.
  158. */
  159. static void gpio_keys_enable_button(struct gpio_button_data *bdata)
  160. {
  161. if (bdata->disabled) {
  162. enable_irq(bdata->irq);
  163. bdata->disabled = false;
  164. }
  165. }
  166. /**
  167. * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons
  168. * @ddata: pointer to drvdata
  169. * @buf: buffer where stringified bitmap is written
  170. * @type: button type (%EV_KEY, %EV_SW)
  171. * @only_disabled: does caller want only those buttons that are
  172. * currently disabled or all buttons that can be
  173. * disabled
  174. *
  175. * This function writes buttons that can be disabled to @buf. If
  176. * @only_disabled is true, then @buf contains only those buttons
  177. * that are currently disabled. Returns 0 on success or negative
  178. * errno on failure.
  179. */
  180. static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
  181. char *buf, unsigned int type,
  182. bool only_disabled)
  183. {
  184. int n_events = get_n_events_by_type(type);
  185. unsigned long *bits;
  186. ssize_t ret;
  187. int i;
  188. bits = bitmap_zalloc(n_events, GFP_KERNEL);
  189. if (!bits)
  190. return -ENOMEM;
  191. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  192. struct gpio_button_data *bdata = &ddata->data[i];
  193. if (bdata->button->type != type)
  194. continue;
  195. if (only_disabled && !bdata->disabled)
  196. continue;
  197. __set_bit(*bdata->code, bits);
  198. }
  199. ret = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", n_events, bits);
  200. buf[ret++] = '\n';
  201. buf[ret] = '\0';
  202. bitmap_free(bits);
  203. return ret;
  204. }
  205. /**
  206. * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap
  207. * @ddata: pointer to drvdata
  208. * @buf: buffer from userspace that contains stringified bitmap
  209. * @type: button type (%EV_KEY, %EV_SW)
  210. *
  211. * This function parses stringified bitmap from @buf and disables/enables
  212. * GPIO buttons accordingly. Returns 0 on success and negative error
  213. * on failure.
  214. */
  215. static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
  216. const char *buf, unsigned int type)
  217. {
  218. int n_events = get_n_events_by_type(type);
  219. const unsigned long *bitmap = get_bm_events_by_type(ddata->input, type);
  220. ssize_t error;
  221. int i;
  222. unsigned long *bits __free(bitmap) = bitmap_alloc(n_events, GFP_KERNEL);
  223. if (!bits)
  224. return -ENOMEM;
  225. error = bitmap_parselist(buf, bits, n_events);
  226. if (error)
  227. return error;
  228. /* First validate */
  229. if (!bitmap_subset(bits, bitmap, n_events))
  230. return -EINVAL;
  231. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  232. struct gpio_button_data *bdata = &ddata->data[i];
  233. if (bdata->button->type != type)
  234. continue;
  235. if (test_bit(*bdata->code, bits) &&
  236. !bdata->button->can_disable) {
  237. return -EINVAL;
  238. }
  239. }
  240. guard(mutex)(&ddata->disable_lock);
  241. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  242. struct gpio_button_data *bdata = &ddata->data[i];
  243. if (bdata->button->type != type)
  244. continue;
  245. if (test_bit(*bdata->code, bits))
  246. gpio_keys_disable_button(bdata);
  247. else
  248. gpio_keys_enable_button(bdata);
  249. }
  250. return 0;
  251. }
  252. #define ATTR_SHOW_FN(name, type, only_disabled) \
  253. static ssize_t gpio_keys_show_##name(struct device *dev, \
  254. struct device_attribute *attr, \
  255. char *buf) \
  256. { \
  257. struct platform_device *pdev = to_platform_device(dev); \
  258. struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
  259. \
  260. return gpio_keys_attr_show_helper(ddata, buf, \
  261. type, only_disabled); \
  262. }
  263. ATTR_SHOW_FN(keys, EV_KEY, false);
  264. ATTR_SHOW_FN(switches, EV_SW, false);
  265. ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
  266. ATTR_SHOW_FN(disabled_switches, EV_SW, true);
  267. /*
  268. * ATTRIBUTES:
  269. *
  270. * /sys/devices/platform/gpio-keys/keys [ro]
  271. * /sys/devices/platform/gpio-keys/switches [ro]
  272. */
  273. static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
  274. static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
  275. #define ATTR_STORE_FN(name, type) \
  276. static ssize_t gpio_keys_store_##name(struct device *dev, \
  277. struct device_attribute *attr, \
  278. const char *buf, \
  279. size_t count) \
  280. { \
  281. struct platform_device *pdev = to_platform_device(dev); \
  282. struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
  283. ssize_t error; \
  284. \
  285. error = gpio_keys_attr_store_helper(ddata, buf, type); \
  286. if (error) \
  287. return error; \
  288. \
  289. return count; \
  290. }
  291. ATTR_STORE_FN(disabled_keys, EV_KEY);
  292. ATTR_STORE_FN(disabled_switches, EV_SW);
  293. /*
  294. * ATTRIBUTES:
  295. *
  296. * /sys/devices/platform/gpio-keys/disabled_keys [rw]
  297. * /sys/devices/platform/gpio-keys/disables_switches [rw]
  298. */
  299. static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
  300. gpio_keys_show_disabled_keys,
  301. gpio_keys_store_disabled_keys);
  302. static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
  303. gpio_keys_show_disabled_switches,
  304. gpio_keys_store_disabled_switches);
  305. static struct attribute *gpio_keys_attrs[] = {
  306. &dev_attr_keys.attr,
  307. &dev_attr_switches.attr,
  308. &dev_attr_disabled_keys.attr,
  309. &dev_attr_disabled_switches.attr,
  310. NULL,
  311. };
  312. ATTRIBUTE_GROUPS(gpio_keys);
  313. static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
  314. {
  315. const struct gpio_keys_button *button = bdata->button;
  316. struct input_dev *input = bdata->input;
  317. unsigned int type = button->type ?: EV_KEY;
  318. int state;
  319. state = bdata->debounce_use_hrtimer ?
  320. gpiod_get_value(bdata->gpiod) :
  321. gpiod_get_value_cansleep(bdata->gpiod);
  322. if (state < 0) {
  323. dev_err(input->dev.parent,
  324. "failed to get gpio state: %d\n", state);
  325. return;
  326. }
  327. if (type == EV_ABS) {
  328. if (state)
  329. input_event(input, type, button->code, button->value);
  330. } else {
  331. input_event(input, type, *bdata->code, state);
  332. }
  333. }
  334. static void gpio_keys_debounce_event(struct gpio_button_data *bdata)
  335. {
  336. gpio_keys_gpio_report_event(bdata);
  337. input_sync(bdata->input);
  338. if (bdata->button->wakeup)
  339. pm_relax(bdata->input->dev.parent);
  340. }
  341. static void gpio_keys_gpio_work_func(struct work_struct *work)
  342. {
  343. struct gpio_button_data *bdata =
  344. container_of(work, struct gpio_button_data, work.work);
  345. gpio_keys_debounce_event(bdata);
  346. }
  347. static enum hrtimer_restart gpio_keys_debounce_timer(struct hrtimer *t)
  348. {
  349. struct gpio_button_data *bdata =
  350. container_of(t, struct gpio_button_data, debounce_timer);
  351. gpio_keys_debounce_event(bdata);
  352. return HRTIMER_NORESTART;
  353. }
  354. static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
  355. {
  356. struct gpio_button_data *bdata = dev_id;
  357. BUG_ON(irq != bdata->irq);
  358. if (bdata->button->wakeup) {
  359. const struct gpio_keys_button *button = bdata->button;
  360. pm_stay_awake(bdata->input->dev.parent);
  361. if (bdata->suspended &&
  362. (button->type == 0 || button->type == EV_KEY)) {
  363. /*
  364. * Simulate wakeup key press in case the key has
  365. * already released by the time we got interrupt
  366. * handler to run.
  367. */
  368. input_report_key(bdata->input, button->code, 1);
  369. }
  370. }
  371. if (bdata->debounce_use_hrtimer) {
  372. hrtimer_start(&bdata->debounce_timer,
  373. ms_to_ktime(bdata->software_debounce),
  374. HRTIMER_MODE_REL);
  375. } else {
  376. mod_delayed_work(system_wq,
  377. &bdata->work,
  378. msecs_to_jiffies(bdata->software_debounce));
  379. }
  380. return IRQ_HANDLED;
  381. }
  382. static enum hrtimer_restart gpio_keys_irq_timer(struct hrtimer *t)
  383. {
  384. struct gpio_button_data *bdata = container_of(t,
  385. struct gpio_button_data,
  386. release_timer);
  387. struct input_dev *input = bdata->input;
  388. guard(spinlock_irqsave)(&bdata->lock);
  389. if (bdata->key_pressed) {
  390. input_report_key(input, *bdata->code, 0);
  391. input_sync(input);
  392. bdata->key_pressed = false;
  393. }
  394. return HRTIMER_NORESTART;
  395. }
  396. static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
  397. {
  398. struct gpio_button_data *bdata = dev_id;
  399. struct input_dev *input = bdata->input;
  400. BUG_ON(irq != bdata->irq);
  401. guard(spinlock_irqsave)(&bdata->lock);
  402. if (!bdata->key_pressed) {
  403. if (bdata->button->wakeup)
  404. pm_wakeup_event(bdata->input->dev.parent, 0);
  405. input_report_key(input, *bdata->code, 1);
  406. input_sync(input);
  407. if (!bdata->release_delay) {
  408. input_report_key(input, *bdata->code, 0);
  409. input_sync(input);
  410. goto out;
  411. }
  412. bdata->key_pressed = true;
  413. }
  414. if (bdata->release_delay)
  415. hrtimer_start(&bdata->release_timer,
  416. ms_to_ktime(bdata->release_delay),
  417. HRTIMER_MODE_REL);
  418. out:
  419. return IRQ_HANDLED;
  420. }
  421. static int gpio_keys_setup_key(struct platform_device *pdev,
  422. struct input_dev *input,
  423. struct gpio_keys_drvdata *ddata,
  424. const struct gpio_keys_button *button,
  425. int idx,
  426. struct fwnode_handle *child)
  427. {
  428. const char *desc = button->desc ? button->desc : "gpio_keys";
  429. struct device *dev = &pdev->dev;
  430. struct gpio_button_data *bdata = &ddata->data[idx];
  431. irq_handler_t isr;
  432. unsigned long irqflags;
  433. const char *wakedesc;
  434. int irq;
  435. int error;
  436. bdata->input = input;
  437. bdata->button = button;
  438. spin_lock_init(&bdata->lock);
  439. if (child) {
  440. bdata->gpiod = devm_fwnode_gpiod_get(dev, child,
  441. NULL, GPIOD_IN, desc);
  442. if (IS_ERR(bdata->gpiod)) {
  443. error = PTR_ERR(bdata->gpiod);
  444. if (error != -ENOENT)
  445. return dev_err_probe(dev, error,
  446. "failed to get gpio\n");
  447. /*
  448. * GPIO is optional, we may be dealing with
  449. * purely interrupt-driven setup.
  450. */
  451. bdata->gpiod = NULL;
  452. }
  453. } else if (gpio_is_valid(button->gpio)) {
  454. /*
  455. * Legacy GPIO number, so request the GPIO here and
  456. * convert it to descriptor.
  457. */
  458. unsigned flags = GPIOF_IN;
  459. if (button->active_low)
  460. flags |= GPIOF_ACTIVE_LOW;
  461. error = devm_gpio_request_one(dev, button->gpio, flags, desc);
  462. if (error < 0) {
  463. dev_err(dev, "Failed to request GPIO %d, error %d\n",
  464. button->gpio, error);
  465. return error;
  466. }
  467. bdata->gpiod = gpio_to_desc(button->gpio);
  468. if (!bdata->gpiod)
  469. return -EINVAL;
  470. }
  471. if (bdata->gpiod) {
  472. bool active_low = gpiod_is_active_low(bdata->gpiod);
  473. if (button->debounce_interval) {
  474. error = gpiod_set_debounce(bdata->gpiod,
  475. button->debounce_interval * 1000);
  476. /* use timer if gpiolib doesn't provide debounce */
  477. if (error < 0)
  478. bdata->software_debounce =
  479. button->debounce_interval;
  480. /*
  481. * If reading the GPIO won't sleep, we can use a
  482. * hrtimer instead of a standard timer for the software
  483. * debounce, to reduce the latency as much as possible.
  484. */
  485. bdata->debounce_use_hrtimer =
  486. !gpiod_cansleep(bdata->gpiod);
  487. }
  488. /*
  489. * If an interrupt was specified, use it instead of the gpio
  490. * interrupt and use the gpio for reading the state. A separate
  491. * interrupt may be used as the main button interrupt for
  492. * runtime PM to detect events also in deeper idle states. If a
  493. * dedicated wakeirq is used for system suspend only, see below
  494. * for bdata->wakeirq setup.
  495. */
  496. if (button->irq) {
  497. bdata->irq = button->irq;
  498. } else {
  499. irq = gpiod_to_irq(bdata->gpiod);
  500. if (irq < 0) {
  501. error = irq;
  502. dev_err_probe(dev, error,
  503. "Unable to get irq number for GPIO %d\n",
  504. button->gpio);
  505. return error;
  506. }
  507. bdata->irq = irq;
  508. }
  509. INIT_DELAYED_WORK(&bdata->work, gpio_keys_gpio_work_func);
  510. hrtimer_init(&bdata->debounce_timer,
  511. CLOCK_REALTIME, HRTIMER_MODE_REL);
  512. bdata->debounce_timer.function = gpio_keys_debounce_timer;
  513. isr = gpio_keys_gpio_isr;
  514. irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
  515. switch (button->wakeup_event_action) {
  516. case EV_ACT_ASSERTED:
  517. bdata->wakeup_trigger_type = active_low ?
  518. IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
  519. break;
  520. case EV_ACT_DEASSERTED:
  521. bdata->wakeup_trigger_type = active_low ?
  522. IRQ_TYPE_EDGE_RISING : IRQ_TYPE_EDGE_FALLING;
  523. break;
  524. case EV_ACT_ANY:
  525. default:
  526. /*
  527. * For other cases, we are OK letting suspend/resume
  528. * not reconfigure the trigger type.
  529. */
  530. break;
  531. }
  532. } else {
  533. if (!button->irq) {
  534. dev_err(dev, "Found button without gpio or irq\n");
  535. return -EINVAL;
  536. }
  537. bdata->irq = button->irq;
  538. if (button->type && button->type != EV_KEY) {
  539. dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n");
  540. return -EINVAL;
  541. }
  542. bdata->release_delay = button->debounce_interval;
  543. hrtimer_init(&bdata->release_timer,
  544. CLOCK_REALTIME, HRTIMER_MODE_REL);
  545. bdata->release_timer.function = gpio_keys_irq_timer;
  546. isr = gpio_keys_irq_isr;
  547. irqflags = 0;
  548. /*
  549. * For IRQ buttons, there is no interrupt for release.
  550. * So we don't need to reconfigure the trigger type for wakeup.
  551. */
  552. }
  553. bdata->code = &ddata->keymap[idx];
  554. *bdata->code = button->code;
  555. input_set_capability(input, button->type ?: EV_KEY, *bdata->code);
  556. /*
  557. * Install custom action to cancel release timer and
  558. * workqueue item.
  559. */
  560. error = devm_add_action(dev, gpio_keys_quiesce_key, bdata);
  561. if (error) {
  562. dev_err(dev, "failed to register quiesce action, error: %d\n",
  563. error);
  564. return error;
  565. }
  566. /*
  567. * If platform has specified that the button can be disabled,
  568. * we don't want it to share the interrupt line.
  569. */
  570. if (!button->can_disable)
  571. irqflags |= IRQF_SHARED;
  572. error = devm_request_any_context_irq(dev, bdata->irq, isr, irqflags,
  573. desc, bdata);
  574. if (error < 0) {
  575. dev_err(dev, "Unable to claim irq %d; error %d\n",
  576. bdata->irq, error);
  577. return error;
  578. }
  579. if (!button->wakeirq)
  580. return 0;
  581. /* Use :wakeup suffix like drivers/base/power/wakeirq.c does */
  582. wakedesc = devm_kasprintf(dev, GFP_KERNEL, "%s:wakeup", desc);
  583. if (!wakedesc)
  584. return -ENOMEM;
  585. bdata->wakeirq = button->wakeirq;
  586. irqflags |= IRQF_NO_SUSPEND;
  587. /*
  588. * Wakeirq shares the handler with the main interrupt, it's only
  589. * active during system suspend. See gpio_keys_button_enable_wakeup()
  590. * and gpio_keys_button_disable_wakeup().
  591. */
  592. error = devm_request_any_context_irq(dev, bdata->wakeirq, isr,
  593. irqflags, wakedesc, bdata);
  594. if (error < 0) {
  595. dev_err(dev, "Unable to claim wakeirq %d; error %d\n",
  596. bdata->irq, error);
  597. return error;
  598. }
  599. /*
  600. * Disable wakeirq until suspend. IRQF_NO_AUTOEN won't work if
  601. * IRQF_SHARED was set based on !button->can_disable.
  602. */
  603. disable_irq(bdata->wakeirq);
  604. return 0;
  605. }
  606. static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
  607. {
  608. struct input_dev *input = ddata->input;
  609. int i;
  610. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  611. struct gpio_button_data *bdata = &ddata->data[i];
  612. if (bdata->gpiod)
  613. gpio_keys_gpio_report_event(bdata);
  614. }
  615. input_sync(input);
  616. }
  617. static int gpio_keys_open(struct input_dev *input)
  618. {
  619. struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
  620. const struct gpio_keys_platform_data *pdata = ddata->pdata;
  621. int error;
  622. if (pdata->enable) {
  623. error = pdata->enable(input->dev.parent);
  624. if (error)
  625. return error;
  626. }
  627. /* Report current state of buttons that are connected to GPIOs */
  628. gpio_keys_report_state(ddata);
  629. return 0;
  630. }
  631. static void gpio_keys_close(struct input_dev *input)
  632. {
  633. struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
  634. const struct gpio_keys_platform_data *pdata = ddata->pdata;
  635. if (pdata->disable)
  636. pdata->disable(input->dev.parent);
  637. }
  638. /*
  639. * Handlers for alternative sources of platform_data
  640. */
  641. /*
  642. * Translate properties into platform_data
  643. */
  644. static struct gpio_keys_platform_data *
  645. gpio_keys_get_devtree_pdata(struct device *dev)
  646. {
  647. struct gpio_keys_platform_data *pdata;
  648. struct gpio_keys_button *button;
  649. int nbuttons, irq;
  650. nbuttons = device_get_child_node_count(dev);
  651. if (nbuttons == 0)
  652. return ERR_PTR(-ENODEV);
  653. pdata = devm_kzalloc(dev,
  654. sizeof(*pdata) + nbuttons * sizeof(*button),
  655. GFP_KERNEL);
  656. if (!pdata)
  657. return ERR_PTR(-ENOMEM);
  658. button = (struct gpio_keys_button *)(pdata + 1);
  659. pdata->buttons = button;
  660. pdata->nbuttons = nbuttons;
  661. pdata->rep = device_property_read_bool(dev, "autorepeat");
  662. device_property_read_string(dev, "label", &pdata->name);
  663. device_for_each_child_node_scoped(dev, child) {
  664. if (is_of_node(child)) {
  665. irq = of_irq_get_byname(to_of_node(child), "irq");
  666. if (irq > 0)
  667. button->irq = irq;
  668. irq = of_irq_get_byname(to_of_node(child), "wakeup");
  669. if (irq > 0)
  670. button->wakeirq = irq;
  671. if (!button->irq && !button->wakeirq)
  672. button->irq =
  673. irq_of_parse_and_map(to_of_node(child), 0);
  674. }
  675. if (fwnode_property_read_u32(child, "linux,code",
  676. &button->code)) {
  677. dev_err(dev, "Button without keycode\n");
  678. return ERR_PTR(-EINVAL);
  679. }
  680. fwnode_property_read_string(child, "label", &button->desc);
  681. if (fwnode_property_read_u32(child, "linux,input-type",
  682. &button->type))
  683. button->type = EV_KEY;
  684. fwnode_property_read_u32(child, "linux,input-value",
  685. (u32 *)&button->value);
  686. button->wakeup =
  687. fwnode_property_read_bool(child, "wakeup-source") ||
  688. /* legacy name */
  689. fwnode_property_read_bool(child, "gpio-key,wakeup");
  690. fwnode_property_read_u32(child, "wakeup-event-action",
  691. &button->wakeup_event_action);
  692. button->can_disable =
  693. fwnode_property_read_bool(child, "linux,can-disable");
  694. if (fwnode_property_read_u32(child, "debounce-interval",
  695. &button->debounce_interval))
  696. button->debounce_interval = 5;
  697. button++;
  698. }
  699. return pdata;
  700. }
  701. static const struct of_device_id gpio_keys_of_match[] = {
  702. { .compatible = "gpio-keys", },
  703. { },
  704. };
  705. MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
  706. static int gpio_keys_probe(struct platform_device *pdev)
  707. {
  708. struct device *dev = &pdev->dev;
  709. const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
  710. struct fwnode_handle *child = NULL;
  711. struct gpio_keys_drvdata *ddata;
  712. struct input_dev *input;
  713. int i, error;
  714. int wakeup = 0;
  715. if (!pdata) {
  716. pdata = gpio_keys_get_devtree_pdata(dev);
  717. if (IS_ERR(pdata))
  718. return PTR_ERR(pdata);
  719. }
  720. ddata = devm_kzalloc(dev, struct_size(ddata, data, pdata->nbuttons),
  721. GFP_KERNEL);
  722. if (!ddata) {
  723. dev_err(dev, "failed to allocate state\n");
  724. return -ENOMEM;
  725. }
  726. ddata->keymap = devm_kcalloc(dev,
  727. pdata->nbuttons, sizeof(ddata->keymap[0]),
  728. GFP_KERNEL);
  729. if (!ddata->keymap)
  730. return -ENOMEM;
  731. input = devm_input_allocate_device(dev);
  732. if (!input) {
  733. dev_err(dev, "failed to allocate input device\n");
  734. return -ENOMEM;
  735. }
  736. ddata->pdata = pdata;
  737. ddata->input = input;
  738. mutex_init(&ddata->disable_lock);
  739. platform_set_drvdata(pdev, ddata);
  740. input_set_drvdata(input, ddata);
  741. input->name = pdata->name ? : pdev->name;
  742. input->phys = "gpio-keys/input0";
  743. input->dev.parent = dev;
  744. input->open = gpio_keys_open;
  745. input->close = gpio_keys_close;
  746. input->id.bustype = BUS_HOST;
  747. input->id.vendor = 0x0001;
  748. input->id.product = 0x0001;
  749. input->id.version = 0x0100;
  750. input->keycode = ddata->keymap;
  751. input->keycodesize = sizeof(ddata->keymap[0]);
  752. input->keycodemax = pdata->nbuttons;
  753. /* Enable auto repeat feature of Linux input subsystem */
  754. if (pdata->rep)
  755. __set_bit(EV_REP, input->evbit);
  756. for (i = 0; i < pdata->nbuttons; i++) {
  757. const struct gpio_keys_button *button = &pdata->buttons[i];
  758. if (!dev_get_platdata(dev)) {
  759. child = device_get_next_child_node(dev, child);
  760. if (!child) {
  761. dev_err(dev,
  762. "missing child device node for entry %d\n",
  763. i);
  764. return -EINVAL;
  765. }
  766. }
  767. error = gpio_keys_setup_key(pdev, input, ddata,
  768. button, i, child);
  769. if (error) {
  770. fwnode_handle_put(child);
  771. return error;
  772. }
  773. if (button->wakeup)
  774. wakeup = 1;
  775. }
  776. fwnode_handle_put(child);
  777. error = input_register_device(input);
  778. if (error) {
  779. dev_err(dev, "Unable to register input device, error: %d\n",
  780. error);
  781. return error;
  782. }
  783. device_init_wakeup(dev, wakeup);
  784. return 0;
  785. }
  786. static int __maybe_unused
  787. gpio_keys_button_enable_wakeup(struct gpio_button_data *bdata)
  788. {
  789. int error;
  790. error = enable_irq_wake(bdata->irq);
  791. if (error) {
  792. dev_err(bdata->input->dev.parent,
  793. "failed to configure IRQ %d as wakeup source: %d\n",
  794. bdata->irq, error);
  795. return error;
  796. }
  797. if (bdata->wakeup_trigger_type) {
  798. error = irq_set_irq_type(bdata->irq,
  799. bdata->wakeup_trigger_type);
  800. if (error) {
  801. dev_err(bdata->input->dev.parent,
  802. "failed to set wakeup trigger %08x for IRQ %d: %d\n",
  803. bdata->wakeup_trigger_type, bdata->irq, error);
  804. disable_irq_wake(bdata->irq);
  805. return error;
  806. }
  807. }
  808. if (bdata->wakeirq) {
  809. enable_irq(bdata->wakeirq);
  810. disable_irq(bdata->irq);
  811. }
  812. return 0;
  813. }
  814. static void __maybe_unused
  815. gpio_keys_button_disable_wakeup(struct gpio_button_data *bdata)
  816. {
  817. int error;
  818. if (bdata->wakeirq) {
  819. enable_irq(bdata->irq);
  820. disable_irq(bdata->wakeirq);
  821. }
  822. /*
  823. * The trigger type is always both edges for gpio-based keys and we do
  824. * not support changing wakeup trigger for interrupt-based keys.
  825. */
  826. if (bdata->wakeup_trigger_type) {
  827. error = irq_set_irq_type(bdata->irq, IRQ_TYPE_EDGE_BOTH);
  828. if (error)
  829. dev_warn(bdata->input->dev.parent,
  830. "failed to restore interrupt trigger for IRQ %d: %d\n",
  831. bdata->irq, error);
  832. }
  833. error = disable_irq_wake(bdata->irq);
  834. if (error)
  835. dev_warn(bdata->input->dev.parent,
  836. "failed to disable IRQ %d as wake source: %d\n",
  837. bdata->irq, error);
  838. }
  839. static int __maybe_unused
  840. gpio_keys_enable_wakeup(struct gpio_keys_drvdata *ddata)
  841. {
  842. struct gpio_button_data *bdata;
  843. int error;
  844. int i;
  845. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  846. bdata = &ddata->data[i];
  847. if (bdata->button->wakeup) {
  848. error = gpio_keys_button_enable_wakeup(bdata);
  849. if (error)
  850. goto err_out;
  851. }
  852. bdata->suspended = true;
  853. }
  854. return 0;
  855. err_out:
  856. while (i--) {
  857. bdata = &ddata->data[i];
  858. if (bdata->button->wakeup)
  859. gpio_keys_button_disable_wakeup(bdata);
  860. bdata->suspended = false;
  861. }
  862. return error;
  863. }
  864. static void __maybe_unused
  865. gpio_keys_disable_wakeup(struct gpio_keys_drvdata *ddata)
  866. {
  867. struct gpio_button_data *bdata;
  868. int i;
  869. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  870. bdata = &ddata->data[i];
  871. bdata->suspended = false;
  872. if (irqd_is_wakeup_set(irq_get_irq_data(bdata->irq)))
  873. gpio_keys_button_disable_wakeup(bdata);
  874. }
  875. }
  876. static int gpio_keys_suspend(struct device *dev)
  877. {
  878. struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
  879. struct input_dev *input = ddata->input;
  880. int error;
  881. if (device_may_wakeup(dev)) {
  882. error = gpio_keys_enable_wakeup(ddata);
  883. if (error)
  884. return error;
  885. } else {
  886. guard(mutex)(&input->mutex);
  887. if (input_device_enabled(input))
  888. gpio_keys_close(input);
  889. }
  890. return 0;
  891. }
  892. static int gpio_keys_resume(struct device *dev)
  893. {
  894. struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
  895. struct input_dev *input = ddata->input;
  896. int error;
  897. if (device_may_wakeup(dev)) {
  898. gpio_keys_disable_wakeup(ddata);
  899. } else {
  900. guard(mutex)(&input->mutex);
  901. if (input_device_enabled(input)) {
  902. error = gpio_keys_open(input);
  903. if (error)
  904. return error;
  905. }
  906. }
  907. gpio_keys_report_state(ddata);
  908. return 0;
  909. }
  910. static DEFINE_SIMPLE_DEV_PM_OPS(gpio_keys_pm_ops, gpio_keys_suspend, gpio_keys_resume);
  911. static void gpio_keys_shutdown(struct platform_device *pdev)
  912. {
  913. int ret;
  914. ret = gpio_keys_suspend(&pdev->dev);
  915. if (ret)
  916. dev_err(&pdev->dev, "failed to shutdown\n");
  917. }
  918. static struct platform_driver gpio_keys_device_driver = {
  919. .probe = gpio_keys_probe,
  920. .shutdown = gpio_keys_shutdown,
  921. .driver = {
  922. .name = "gpio-keys",
  923. .pm = pm_sleep_ptr(&gpio_keys_pm_ops),
  924. .of_match_table = gpio_keys_of_match,
  925. .dev_groups = gpio_keys_groups,
  926. }
  927. };
  928. static int __init gpio_keys_init(void)
  929. {
  930. return platform_driver_register(&gpio_keys_device_driver);
  931. }
  932. static void __exit gpio_keys_exit(void)
  933. {
  934. platform_driver_unregister(&gpio_keys_device_driver);
  935. }
  936. late_initcall(gpio_keys_init);
  937. module_exit(gpio_keys_exit);
  938. MODULE_LICENSE("GPL");
  939. MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
  940. MODULE_DESCRIPTION("Keyboard driver for GPIOs");
  941. MODULE_ALIAS("platform:gpio-keys");