gpio_keys.c 25 KB

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