gpio_keys.c 26 KB

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