gpio_keys.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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_dfl_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. error = devm_gpio_request_one(dev, button->gpio, GPIOF_IN, desc);
  459. if (error < 0) {
  460. dev_err(dev, "Failed to request GPIO %d, error %d\n",
  461. button->gpio, error);
  462. return error;
  463. }
  464. bdata->gpiod = gpio_to_desc(button->gpio);
  465. if (!bdata->gpiod)
  466. return -EINVAL;
  467. if (button->active_low ^ gpiod_is_active_low(bdata->gpiod))
  468. gpiod_toggle_active_low(bdata->gpiod);
  469. }
  470. if (bdata->gpiod) {
  471. bool active_low = gpiod_is_active_low(bdata->gpiod);
  472. if (button->debounce_interval) {
  473. error = gpiod_set_debounce(bdata->gpiod,
  474. button->debounce_interval * 1000);
  475. /* use timer if gpiolib doesn't provide debounce */
  476. if (error < 0)
  477. bdata->software_debounce =
  478. button->debounce_interval;
  479. /*
  480. * If reading the GPIO won't sleep, we can use a
  481. * hrtimer instead of a standard timer for the software
  482. * debounce, to reduce the latency as much as possible.
  483. */
  484. bdata->debounce_use_hrtimer =
  485. !gpiod_cansleep(bdata->gpiod);
  486. }
  487. /*
  488. * If an interrupt was specified, use it instead of the gpio
  489. * interrupt and use the gpio for reading the state. A separate
  490. * interrupt may be used as the main button interrupt for
  491. * runtime PM to detect events also in deeper idle states. If a
  492. * dedicated wakeirq is used for system suspend only, see below
  493. * for bdata->wakeirq setup.
  494. */
  495. if (button->irq) {
  496. bdata->irq = button->irq;
  497. } else {
  498. irq = gpiod_to_irq(bdata->gpiod);
  499. if (irq < 0) {
  500. error = irq;
  501. dev_err_probe(dev, error,
  502. "Unable to get irq number for GPIO %d\n",
  503. button->gpio);
  504. return error;
  505. }
  506. bdata->irq = irq;
  507. }
  508. INIT_DELAYED_WORK(&bdata->work, gpio_keys_gpio_work_func);
  509. hrtimer_setup(&bdata->debounce_timer, gpio_keys_debounce_timer,
  510. CLOCK_REALTIME, HRTIMER_MODE_REL);
  511. isr = gpio_keys_gpio_isr;
  512. irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
  513. switch (button->wakeup_event_action) {
  514. case EV_ACT_ASSERTED:
  515. bdata->wakeup_trigger_type = active_low ?
  516. IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
  517. break;
  518. case EV_ACT_DEASSERTED:
  519. bdata->wakeup_trigger_type = active_low ?
  520. IRQ_TYPE_EDGE_RISING : IRQ_TYPE_EDGE_FALLING;
  521. break;
  522. case EV_ACT_ANY:
  523. default:
  524. /*
  525. * For other cases, we are OK letting suspend/resume
  526. * not reconfigure the trigger type.
  527. */
  528. break;
  529. }
  530. } else {
  531. if (button->irq) {
  532. bdata->irq = button->irq;
  533. } else {
  534. irq = platform_get_irq_optional(pdev, idx);
  535. if (irq < 0) {
  536. error = irq;
  537. return dev_err_probe(dev, error,
  538. "Unable to determine IRQ# for button #%d",
  539. idx);
  540. }
  541. bdata->irq = irq;
  542. }
  543. if (button->type && button->type != EV_KEY) {
  544. dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n");
  545. return -EINVAL;
  546. }
  547. bdata->release_delay = button->debounce_interval;
  548. hrtimer_setup(&bdata->release_timer, gpio_keys_irq_timer,
  549. CLOCK_REALTIME, HRTIMER_MODE_REL);
  550. isr = gpio_keys_irq_isr;
  551. irqflags = 0;
  552. /*
  553. * For IRQ buttons, there is no interrupt for release.
  554. * So we don't need to reconfigure the trigger type for wakeup.
  555. */
  556. }
  557. bdata->code = &ddata->keymap[idx];
  558. *bdata->code = button->code;
  559. input_set_capability(input, button->type ?: EV_KEY, *bdata->code);
  560. /*
  561. * Install custom action to cancel release timer and
  562. * workqueue item.
  563. */
  564. error = devm_add_action(dev, gpio_keys_quiesce_key, bdata);
  565. if (error) {
  566. dev_err(dev, "failed to register quiesce action, error: %d\n",
  567. error);
  568. return error;
  569. }
  570. /*
  571. * If platform has specified that the button can be disabled,
  572. * we don't want it to share the interrupt line.
  573. */
  574. if (!button->can_disable)
  575. irqflags |= IRQF_SHARED;
  576. error = devm_request_any_context_irq(dev, bdata->irq, isr, irqflags,
  577. desc, bdata);
  578. if (error < 0) {
  579. dev_err(dev, "Unable to claim irq %d; error %d\n",
  580. bdata->irq, error);
  581. return error;
  582. }
  583. if (!button->wakeirq)
  584. return 0;
  585. /* Use :wakeup suffix like drivers/base/power/wakeirq.c does */
  586. wakedesc = devm_kasprintf(dev, GFP_KERNEL, "%s:wakeup", desc);
  587. if (!wakedesc)
  588. return -ENOMEM;
  589. bdata->wakeirq = button->wakeirq;
  590. irqflags |= IRQF_NO_SUSPEND;
  591. /*
  592. * Wakeirq shares the handler with the main interrupt, it's only
  593. * active during system suspend. See gpio_keys_button_enable_wakeup()
  594. * and gpio_keys_button_disable_wakeup().
  595. */
  596. error = devm_request_any_context_irq(dev, bdata->wakeirq, isr,
  597. irqflags, wakedesc, bdata);
  598. if (error < 0) {
  599. dev_err(dev, "Unable to claim wakeirq %d; error %d\n",
  600. bdata->irq, error);
  601. return error;
  602. }
  603. /*
  604. * Disable wakeirq until suspend. IRQF_NO_AUTOEN won't work if
  605. * IRQF_SHARED was set based on !button->can_disable.
  606. */
  607. disable_irq(bdata->wakeirq);
  608. return 0;
  609. }
  610. static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
  611. {
  612. struct input_dev *input = ddata->input;
  613. int i;
  614. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  615. struct gpio_button_data *bdata = &ddata->data[i];
  616. if (bdata->gpiod)
  617. gpio_keys_gpio_report_event(bdata);
  618. }
  619. input_sync(input);
  620. }
  621. static int gpio_keys_open(struct input_dev *input)
  622. {
  623. struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
  624. const struct gpio_keys_platform_data *pdata = ddata->pdata;
  625. int error;
  626. if (pdata->enable) {
  627. error = pdata->enable(input->dev.parent);
  628. if (error)
  629. return error;
  630. }
  631. /* Report current state of buttons that are connected to GPIOs */
  632. gpio_keys_report_state(ddata);
  633. return 0;
  634. }
  635. static void gpio_keys_close(struct input_dev *input)
  636. {
  637. struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
  638. const struct gpio_keys_platform_data *pdata = ddata->pdata;
  639. if (pdata->disable)
  640. pdata->disable(input->dev.parent);
  641. }
  642. /*
  643. * Handlers for alternative sources of platform_data
  644. */
  645. /*
  646. * Translate properties into platform_data
  647. */
  648. static struct gpio_keys_platform_data *
  649. gpio_keys_get_devtree_pdata(struct device *dev)
  650. {
  651. struct gpio_keys_platform_data *pdata;
  652. struct gpio_keys_button *button;
  653. int nbuttons, irq;
  654. nbuttons = device_get_child_node_count(dev);
  655. if (nbuttons == 0)
  656. return ERR_PTR(-ENODEV);
  657. pdata = devm_kzalloc(dev,
  658. sizeof(*pdata) + nbuttons * sizeof(*button),
  659. GFP_KERNEL);
  660. if (!pdata)
  661. return ERR_PTR(-ENOMEM);
  662. button = (struct gpio_keys_button *)(pdata + 1);
  663. pdata->buttons = button;
  664. pdata->nbuttons = nbuttons;
  665. pdata->rep = device_property_read_bool(dev, "autorepeat");
  666. device_property_read_string(dev, "label", &pdata->name);
  667. device_for_each_child_node_scoped(dev, child) {
  668. if (is_of_node(child)) {
  669. irq = of_irq_get_byname(to_of_node(child), "irq");
  670. if (irq > 0)
  671. button->irq = irq;
  672. irq = of_irq_get_byname(to_of_node(child), "wakeup");
  673. if (irq > 0)
  674. button->wakeirq = irq;
  675. if (!button->irq && !button->wakeirq)
  676. button->irq =
  677. irq_of_parse_and_map(to_of_node(child), 0);
  678. }
  679. if (fwnode_property_read_u32(child, "linux,code",
  680. &button->code)) {
  681. dev_err(dev, "Button without keycode\n");
  682. return ERR_PTR(-EINVAL);
  683. }
  684. fwnode_property_read_string(child, "label", &button->desc);
  685. if (fwnode_property_read_u32(child, "linux,input-type",
  686. &button->type))
  687. button->type = EV_KEY;
  688. fwnode_property_read_u32(child, "linux,input-value",
  689. (u32 *)&button->value);
  690. button->wakeup =
  691. fwnode_property_read_bool(child, "wakeup-source") ||
  692. /* legacy name */
  693. fwnode_property_read_bool(child, "gpio-key,wakeup");
  694. fwnode_property_read_u32(child, "wakeup-event-action",
  695. &button->wakeup_event_action);
  696. button->can_disable =
  697. fwnode_property_read_bool(child, "linux,can-disable");
  698. if (fwnode_property_read_u32(child, "debounce-interval",
  699. &button->debounce_interval))
  700. button->debounce_interval = 5;
  701. button++;
  702. }
  703. return pdata;
  704. }
  705. static const struct of_device_id gpio_keys_of_match[] = {
  706. { .compatible = "gpio-keys", },
  707. { },
  708. };
  709. MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
  710. static int gpio_keys_probe(struct platform_device *pdev)
  711. {
  712. struct device *dev = &pdev->dev;
  713. const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
  714. struct fwnode_handle *child = NULL;
  715. struct gpio_keys_drvdata *ddata;
  716. struct input_dev *input;
  717. int i, error;
  718. int wakeup = 0;
  719. if (!pdata) {
  720. pdata = gpio_keys_get_devtree_pdata(dev);
  721. if (IS_ERR(pdata))
  722. return PTR_ERR(pdata);
  723. }
  724. ddata = devm_kzalloc(dev, struct_size(ddata, data, pdata->nbuttons),
  725. GFP_KERNEL);
  726. if (!ddata) {
  727. dev_err(dev, "failed to allocate state\n");
  728. return -ENOMEM;
  729. }
  730. ddata->keymap = devm_kcalloc(dev,
  731. pdata->nbuttons, sizeof(ddata->keymap[0]),
  732. GFP_KERNEL);
  733. if (!ddata->keymap)
  734. return -ENOMEM;
  735. input = devm_input_allocate_device(dev);
  736. if (!input) {
  737. dev_err(dev, "failed to allocate input device\n");
  738. return -ENOMEM;
  739. }
  740. ddata->pdata = pdata;
  741. ddata->input = input;
  742. mutex_init(&ddata->disable_lock);
  743. platform_set_drvdata(pdev, ddata);
  744. input_set_drvdata(input, ddata);
  745. input->name = pdata->name ? : pdev->name;
  746. input->phys = "gpio-keys/input0";
  747. input->dev.parent = dev;
  748. input->open = gpio_keys_open;
  749. input->close = gpio_keys_close;
  750. input->id.bustype = BUS_HOST;
  751. input->id.vendor = 0x0001;
  752. input->id.product = 0x0001;
  753. input->id.version = 0x0100;
  754. input->keycode = ddata->keymap;
  755. input->keycodesize = sizeof(ddata->keymap[0]);
  756. input->keycodemax = pdata->nbuttons;
  757. /* Enable auto repeat feature of Linux input subsystem */
  758. if (pdata->rep)
  759. __set_bit(EV_REP, input->evbit);
  760. for (i = 0; i < pdata->nbuttons; i++) {
  761. const struct gpio_keys_button *button = &pdata->buttons[i];
  762. if (!dev_get_platdata(dev)) {
  763. child = device_get_next_child_node(dev, child);
  764. if (!child) {
  765. dev_err(dev,
  766. "missing child device node for entry %d\n",
  767. i);
  768. return -EINVAL;
  769. }
  770. }
  771. error = gpio_keys_setup_key(pdev, input, ddata,
  772. button, i, child);
  773. if (error) {
  774. fwnode_handle_put(child);
  775. return error;
  776. }
  777. if (button->wakeup)
  778. wakeup = 1;
  779. }
  780. fwnode_handle_put(child);
  781. error = input_register_device(input);
  782. if (error) {
  783. dev_err(dev, "Unable to register input device, error: %d\n",
  784. error);
  785. return error;
  786. }
  787. device_init_wakeup(dev, wakeup);
  788. return 0;
  789. }
  790. static int __maybe_unused
  791. gpio_keys_button_enable_wakeup(struct gpio_button_data *bdata)
  792. {
  793. int error;
  794. error = enable_irq_wake(bdata->irq);
  795. if (error) {
  796. dev_err(bdata->input->dev.parent,
  797. "failed to configure IRQ %d as wakeup source: %d\n",
  798. bdata->irq, error);
  799. return error;
  800. }
  801. if (bdata->wakeup_trigger_type) {
  802. error = irq_set_irq_type(bdata->irq,
  803. bdata->wakeup_trigger_type);
  804. if (error) {
  805. dev_err(bdata->input->dev.parent,
  806. "failed to set wakeup trigger %08x for IRQ %d: %d\n",
  807. bdata->wakeup_trigger_type, bdata->irq, error);
  808. disable_irq_wake(bdata->irq);
  809. return error;
  810. }
  811. }
  812. if (bdata->wakeirq) {
  813. enable_irq(bdata->wakeirq);
  814. disable_irq(bdata->irq);
  815. }
  816. return 0;
  817. }
  818. static void __maybe_unused
  819. gpio_keys_button_disable_wakeup(struct gpio_button_data *bdata)
  820. {
  821. int error;
  822. if (bdata->wakeirq) {
  823. enable_irq(bdata->irq);
  824. disable_irq(bdata->wakeirq);
  825. }
  826. /*
  827. * The trigger type is always both edges for gpio-based keys and we do
  828. * not support changing wakeup trigger for interrupt-based keys.
  829. */
  830. if (bdata->wakeup_trigger_type) {
  831. error = irq_set_irq_type(bdata->irq, IRQ_TYPE_EDGE_BOTH);
  832. if (error)
  833. dev_warn(bdata->input->dev.parent,
  834. "failed to restore interrupt trigger for IRQ %d: %d\n",
  835. bdata->irq, error);
  836. }
  837. error = disable_irq_wake(bdata->irq);
  838. if (error)
  839. dev_warn(bdata->input->dev.parent,
  840. "failed to disable IRQ %d as wake source: %d\n",
  841. bdata->irq, error);
  842. }
  843. static int __maybe_unused
  844. gpio_keys_enable_wakeup(struct gpio_keys_drvdata *ddata)
  845. {
  846. struct gpio_button_data *bdata;
  847. int error;
  848. int i;
  849. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  850. bdata = &ddata->data[i];
  851. if (bdata->button->wakeup) {
  852. error = gpio_keys_button_enable_wakeup(bdata);
  853. if (error)
  854. goto err_out;
  855. }
  856. bdata->suspended = true;
  857. }
  858. return 0;
  859. err_out:
  860. while (i--) {
  861. bdata = &ddata->data[i];
  862. if (bdata->button->wakeup)
  863. gpio_keys_button_disable_wakeup(bdata);
  864. bdata->suspended = false;
  865. }
  866. return error;
  867. }
  868. static void __maybe_unused
  869. gpio_keys_disable_wakeup(struct gpio_keys_drvdata *ddata)
  870. {
  871. struct gpio_button_data *bdata;
  872. int i;
  873. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  874. bdata = &ddata->data[i];
  875. bdata->suspended = false;
  876. if (irqd_is_wakeup_set(irq_get_irq_data(bdata->irq)))
  877. gpio_keys_button_disable_wakeup(bdata);
  878. }
  879. }
  880. static int gpio_keys_suspend(struct device *dev)
  881. {
  882. struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
  883. struct input_dev *input = ddata->input;
  884. int error;
  885. if (device_may_wakeup(dev)) {
  886. error = gpio_keys_enable_wakeup(ddata);
  887. if (error)
  888. return error;
  889. } else {
  890. guard(mutex)(&input->mutex);
  891. if (input_device_enabled(input))
  892. gpio_keys_close(input);
  893. }
  894. return 0;
  895. }
  896. static int gpio_keys_resume(struct device *dev)
  897. {
  898. struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
  899. struct input_dev *input = ddata->input;
  900. int error;
  901. if (device_may_wakeup(dev)) {
  902. gpio_keys_disable_wakeup(ddata);
  903. } else {
  904. guard(mutex)(&input->mutex);
  905. if (input_device_enabled(input)) {
  906. error = gpio_keys_open(input);
  907. if (error)
  908. return error;
  909. }
  910. }
  911. gpio_keys_report_state(ddata);
  912. return 0;
  913. }
  914. static DEFINE_SIMPLE_DEV_PM_OPS(gpio_keys_pm_ops, gpio_keys_suspend, gpio_keys_resume);
  915. static void gpio_keys_shutdown(struct platform_device *pdev)
  916. {
  917. int ret;
  918. ret = gpio_keys_suspend(&pdev->dev);
  919. if (ret)
  920. dev_err(&pdev->dev, "failed to shutdown\n");
  921. }
  922. static struct platform_driver gpio_keys_device_driver = {
  923. .probe = gpio_keys_probe,
  924. .shutdown = gpio_keys_shutdown,
  925. .driver = {
  926. .name = "gpio-keys",
  927. .pm = pm_sleep_ptr(&gpio_keys_pm_ops),
  928. .of_match_table = gpio_keys_of_match,
  929. .dev_groups = gpio_keys_groups,
  930. }
  931. };
  932. static int __init gpio_keys_init(void)
  933. {
  934. return platform_driver_register(&gpio_keys_device_driver);
  935. }
  936. static void __exit gpio_keys_exit(void)
  937. {
  938. platform_driver_unregister(&gpio_keys_device_driver);
  939. }
  940. late_initcall(gpio_keys_init);
  941. module_exit(gpio_keys_exit);
  942. MODULE_LICENSE("GPL");
  943. MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
  944. MODULE_DESCRIPTION("Keyboard driver for GPIOs");
  945. MODULE_ALIAS("platform:gpio-keys");