gpio_keys.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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/of.h>
  28. #include <linux/of_platform.h>
  29. #include <linux/of_gpio.h>
  30. #include <linux/of_irq.h>
  31. #include <linux/spinlock.h>
  32. struct gpio_button_data {
  33. const struct gpio_keys_button *button;
  34. struct input_dev *input;
  35. struct timer_list release_timer;
  36. unsigned int release_delay; /* in msecs, for IRQ-only buttons */
  37. struct delayed_work work;
  38. unsigned int software_debounce; /* in msecs, for GPIO-driven buttons */
  39. unsigned int irq;
  40. spinlock_t lock;
  41. bool disabled;
  42. bool key_pressed;
  43. };
  44. struct gpio_keys_drvdata {
  45. const struct gpio_keys_platform_data *pdata;
  46. struct input_dev *input;
  47. struct mutex disable_lock;
  48. struct gpio_button_data data[0];
  49. };
  50. /*
  51. * SYSFS interface for enabling/disabling keys and switches:
  52. *
  53. * There are 4 attributes under /sys/devices/platform/gpio-keys/
  54. * keys [ro] - bitmap of keys (EV_KEY) which can be
  55. * disabled
  56. * switches [ro] - bitmap of switches (EV_SW) which can be
  57. * disabled
  58. * disabled_keys [rw] - bitmap of keys currently disabled
  59. * disabled_switches [rw] - bitmap of switches currently disabled
  60. *
  61. * Userland can change these values and hence disable event generation
  62. * for each key (or switch). Disabling a key means its interrupt line
  63. * is disabled.
  64. *
  65. * For example, if we have following switches set up as gpio-keys:
  66. * SW_DOCK = 5
  67. * SW_CAMERA_LENS_COVER = 9
  68. * SW_KEYPAD_SLIDE = 10
  69. * SW_FRONT_PROXIMITY = 11
  70. * This is read from switches:
  71. * 11-9,5
  72. * Next we want to disable proximity (11) and dock (5), we write:
  73. * 11,5
  74. * to file disabled_switches. Now proximity and dock IRQs are disabled.
  75. * This can be verified by reading the file disabled_switches:
  76. * 11,5
  77. * If we now want to enable proximity (11) switch we write:
  78. * 5
  79. * to disabled_switches.
  80. *
  81. * We can disable only those keys which don't allow sharing the irq.
  82. */
  83. /**
  84. * get_n_events_by_type() - returns maximum number of events per @type
  85. * @type: type of button (%EV_KEY, %EV_SW)
  86. *
  87. * Return value of this function can be used to allocate bitmap
  88. * large enough to hold all bits for given type.
  89. */
  90. static inline int get_n_events_by_type(int type)
  91. {
  92. BUG_ON(type != EV_SW && type != EV_KEY);
  93. return (type == EV_KEY) ? KEY_CNT : SW_CNT;
  94. }
  95. /**
  96. * gpio_keys_disable_button() - disables given GPIO button
  97. * @bdata: button data for button to be disabled
  98. *
  99. * Disables button pointed by @bdata. This is done by masking
  100. * IRQ line. After this function is called, button won't generate
  101. * input events anymore. Note that one can only disable buttons
  102. * that don't share IRQs.
  103. *
  104. * Make sure that @bdata->disable_lock is locked when entering
  105. * this function to avoid races when concurrent threads are
  106. * disabling buttons at the same time.
  107. */
  108. static void gpio_keys_disable_button(struct gpio_button_data *bdata)
  109. {
  110. if (!bdata->disabled) {
  111. /*
  112. * Disable IRQ and associated timer/work structure.
  113. */
  114. disable_irq(bdata->irq);
  115. if (gpio_is_valid(bdata->button->gpio))
  116. cancel_delayed_work_sync(&bdata->work);
  117. else
  118. del_timer_sync(&bdata->release_timer);
  119. bdata->disabled = true;
  120. }
  121. }
  122. /**
  123. * gpio_keys_enable_button() - enables given GPIO button
  124. * @bdata: button data for button to be disabled
  125. *
  126. * Enables given button pointed by @bdata.
  127. *
  128. * Make sure that @bdata->disable_lock is locked when entering
  129. * this function to avoid races with concurrent threads trying
  130. * to enable the same button at the same time.
  131. */
  132. static void gpio_keys_enable_button(struct gpio_button_data *bdata)
  133. {
  134. if (bdata->disabled) {
  135. enable_irq(bdata->irq);
  136. bdata->disabled = false;
  137. }
  138. }
  139. /**
  140. * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons
  141. * @ddata: pointer to drvdata
  142. * @buf: buffer where stringified bitmap is written
  143. * @type: button type (%EV_KEY, %EV_SW)
  144. * @only_disabled: does caller want only those buttons that are
  145. * currently disabled or all buttons that can be
  146. * disabled
  147. *
  148. * This function writes buttons that can be disabled to @buf. If
  149. * @only_disabled is true, then @buf contains only those buttons
  150. * that are currently disabled. Returns 0 on success or negative
  151. * errno on failure.
  152. */
  153. static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
  154. char *buf, unsigned int type,
  155. bool only_disabled)
  156. {
  157. int n_events = get_n_events_by_type(type);
  158. unsigned long *bits;
  159. ssize_t ret;
  160. int i;
  161. bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
  162. if (!bits)
  163. return -ENOMEM;
  164. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  165. struct gpio_button_data *bdata = &ddata->data[i];
  166. if (bdata->button->type != type)
  167. continue;
  168. if (only_disabled && !bdata->disabled)
  169. continue;
  170. __set_bit(bdata->button->code, bits);
  171. }
  172. ret = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", n_events, bits);
  173. buf[ret++] = '\n';
  174. buf[ret] = '\0';
  175. kfree(bits);
  176. return ret;
  177. }
  178. /**
  179. * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap
  180. * @ddata: pointer to drvdata
  181. * @buf: buffer from userspace that contains stringified bitmap
  182. * @type: button type (%EV_KEY, %EV_SW)
  183. *
  184. * This function parses stringified bitmap from @buf and disables/enables
  185. * GPIO buttons accordingly. Returns 0 on success and negative error
  186. * on failure.
  187. */
  188. static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
  189. const char *buf, unsigned int type)
  190. {
  191. int n_events = get_n_events_by_type(type);
  192. unsigned long *bits;
  193. ssize_t error;
  194. int i;
  195. bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
  196. if (!bits)
  197. return -ENOMEM;
  198. error = bitmap_parselist(buf, bits, n_events);
  199. if (error)
  200. goto out;
  201. /* First validate */
  202. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  203. struct gpio_button_data *bdata = &ddata->data[i];
  204. if (bdata->button->type != type)
  205. continue;
  206. if (test_bit(bdata->button->code, bits) &&
  207. !bdata->button->can_disable) {
  208. error = -EINVAL;
  209. goto out;
  210. }
  211. }
  212. if (i == ddata->pdata->nbuttons) {
  213. error = -EINVAL;
  214. goto out;
  215. }
  216. mutex_lock(&ddata->disable_lock);
  217. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  218. struct gpio_button_data *bdata = &ddata->data[i];
  219. if (bdata->button->type != type)
  220. continue;
  221. if (test_bit(bdata->button->code, bits))
  222. gpio_keys_disable_button(bdata);
  223. else
  224. gpio_keys_enable_button(bdata);
  225. }
  226. mutex_unlock(&ddata->disable_lock);
  227. out:
  228. kfree(bits);
  229. return error;
  230. }
  231. #define ATTR_SHOW_FN(name, type, only_disabled) \
  232. static ssize_t gpio_keys_show_##name(struct device *dev, \
  233. struct device_attribute *attr, \
  234. char *buf) \
  235. { \
  236. struct platform_device *pdev = to_platform_device(dev); \
  237. struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
  238. \
  239. return gpio_keys_attr_show_helper(ddata, buf, \
  240. type, only_disabled); \
  241. }
  242. ATTR_SHOW_FN(keys, EV_KEY, false);
  243. ATTR_SHOW_FN(switches, EV_SW, false);
  244. ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
  245. ATTR_SHOW_FN(disabled_switches, EV_SW, true);
  246. /*
  247. * ATTRIBUTES:
  248. *
  249. * /sys/devices/platform/gpio-keys/keys [ro]
  250. * /sys/devices/platform/gpio-keys/switches [ro]
  251. */
  252. static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
  253. static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
  254. #define ATTR_STORE_FN(name, type) \
  255. static ssize_t gpio_keys_store_##name(struct device *dev, \
  256. struct device_attribute *attr, \
  257. const char *buf, \
  258. size_t count) \
  259. { \
  260. struct platform_device *pdev = to_platform_device(dev); \
  261. struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
  262. ssize_t error; \
  263. \
  264. error = gpio_keys_attr_store_helper(ddata, buf, type); \
  265. if (error) \
  266. return error; \
  267. \
  268. return count; \
  269. }
  270. ATTR_STORE_FN(disabled_keys, EV_KEY);
  271. ATTR_STORE_FN(disabled_switches, EV_SW);
  272. /*
  273. * ATTRIBUTES:
  274. *
  275. * /sys/devices/platform/gpio-keys/disabled_keys [rw]
  276. * /sys/devices/platform/gpio-keys/disables_switches [rw]
  277. */
  278. static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
  279. gpio_keys_show_disabled_keys,
  280. gpio_keys_store_disabled_keys);
  281. static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
  282. gpio_keys_show_disabled_switches,
  283. gpio_keys_store_disabled_switches);
  284. static struct attribute *gpio_keys_attrs[] = {
  285. &dev_attr_keys.attr,
  286. &dev_attr_switches.attr,
  287. &dev_attr_disabled_keys.attr,
  288. &dev_attr_disabled_switches.attr,
  289. NULL,
  290. };
  291. static struct attribute_group gpio_keys_attr_group = {
  292. .attrs = gpio_keys_attrs,
  293. };
  294. static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
  295. {
  296. const struct gpio_keys_button *button = bdata->button;
  297. struct input_dev *input = bdata->input;
  298. unsigned int type = button->type ?: EV_KEY;
  299. int state = (gpio_get_value_cansleep(button->gpio) ? 1 : 0) ^ button->active_low;
  300. if (type == EV_ABS) {
  301. if (state)
  302. input_event(input, type, button->code, button->value);
  303. } else {
  304. input_event(input, type, button->code, !!state);
  305. }
  306. input_sync(input);
  307. }
  308. static void gpio_keys_gpio_work_func(struct work_struct *work)
  309. {
  310. struct gpio_button_data *bdata =
  311. container_of(work, struct gpio_button_data, work.work);
  312. gpio_keys_gpio_report_event(bdata);
  313. if (bdata->button->wakeup)
  314. pm_relax(bdata->input->dev.parent);
  315. }
  316. static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
  317. {
  318. struct gpio_button_data *bdata = dev_id;
  319. BUG_ON(irq != bdata->irq);
  320. if (bdata->button->wakeup)
  321. pm_stay_awake(bdata->input->dev.parent);
  322. mod_delayed_work(system_wq,
  323. &bdata->work,
  324. msecs_to_jiffies(bdata->software_debounce));
  325. return IRQ_HANDLED;
  326. }
  327. static void gpio_keys_irq_timer(unsigned long _data)
  328. {
  329. struct gpio_button_data *bdata = (struct gpio_button_data *)_data;
  330. struct input_dev *input = bdata->input;
  331. unsigned long flags;
  332. spin_lock_irqsave(&bdata->lock, flags);
  333. if (bdata->key_pressed) {
  334. input_event(input, EV_KEY, bdata->button->code, 0);
  335. input_sync(input);
  336. bdata->key_pressed = false;
  337. }
  338. spin_unlock_irqrestore(&bdata->lock, flags);
  339. }
  340. static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
  341. {
  342. struct gpio_button_data *bdata = dev_id;
  343. const struct gpio_keys_button *button = bdata->button;
  344. struct input_dev *input = bdata->input;
  345. unsigned long flags;
  346. BUG_ON(irq != bdata->irq);
  347. spin_lock_irqsave(&bdata->lock, flags);
  348. if (!bdata->key_pressed) {
  349. if (bdata->button->wakeup)
  350. pm_wakeup_event(bdata->input->dev.parent, 0);
  351. input_event(input, EV_KEY, button->code, 1);
  352. input_sync(input);
  353. if (!bdata->release_delay) {
  354. input_event(input, EV_KEY, button->code, 0);
  355. input_sync(input);
  356. goto out;
  357. }
  358. bdata->key_pressed = true;
  359. }
  360. if (bdata->release_delay)
  361. mod_timer(&bdata->release_timer,
  362. jiffies + msecs_to_jiffies(bdata->release_delay));
  363. out:
  364. spin_unlock_irqrestore(&bdata->lock, flags);
  365. return IRQ_HANDLED;
  366. }
  367. static void gpio_keys_quiesce_key(void *data)
  368. {
  369. struct gpio_button_data *bdata = data;
  370. if (gpio_is_valid(bdata->button->gpio))
  371. cancel_delayed_work_sync(&bdata->work);
  372. else
  373. del_timer_sync(&bdata->release_timer);
  374. }
  375. static int gpio_keys_setup_key(struct platform_device *pdev,
  376. struct input_dev *input,
  377. struct gpio_button_data *bdata,
  378. const struct gpio_keys_button *button)
  379. {
  380. const char *desc = button->desc ? button->desc : "gpio_keys";
  381. struct device *dev = &pdev->dev;
  382. irq_handler_t isr;
  383. unsigned long irqflags;
  384. int irq;
  385. int error;
  386. bdata->input = input;
  387. bdata->button = button;
  388. spin_lock_init(&bdata->lock);
  389. if (gpio_is_valid(button->gpio)) {
  390. error = devm_gpio_request_one(&pdev->dev, button->gpio,
  391. GPIOF_IN, desc);
  392. if (error < 0) {
  393. dev_err(dev, "Failed to request GPIO %d, error %d\n",
  394. button->gpio, error);
  395. return error;
  396. }
  397. if (button->debounce_interval) {
  398. error = gpio_set_debounce(button->gpio,
  399. button->debounce_interval * 1000);
  400. /* use timer if gpiolib doesn't provide debounce */
  401. if (error < 0)
  402. bdata->software_debounce =
  403. button->debounce_interval;
  404. }
  405. if (button->irq) {
  406. bdata->irq = button->irq;
  407. } else {
  408. irq = gpio_to_irq(button->gpio);
  409. if (irq < 0) {
  410. error = irq;
  411. dev_err(dev,
  412. "Unable to get irq number for GPIO %d, error %d\n",
  413. button->gpio, error);
  414. return error;
  415. }
  416. bdata->irq = irq;
  417. }
  418. INIT_DELAYED_WORK(&bdata->work, gpio_keys_gpio_work_func);
  419. isr = gpio_keys_gpio_isr;
  420. irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
  421. } else {
  422. if (!button->irq) {
  423. dev_err(dev, "No IRQ specified\n");
  424. return -EINVAL;
  425. }
  426. bdata->irq = button->irq;
  427. if (button->type && button->type != EV_KEY) {
  428. dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n");
  429. return -EINVAL;
  430. }
  431. bdata->release_delay = button->debounce_interval;
  432. setup_timer(&bdata->release_timer,
  433. gpio_keys_irq_timer, (unsigned long)bdata);
  434. isr = gpio_keys_irq_isr;
  435. irqflags = 0;
  436. }
  437. input_set_capability(input, button->type ?: EV_KEY, button->code);
  438. /*
  439. * Install custom action to cancel release timer and
  440. * workqueue item.
  441. */
  442. error = devm_add_action(&pdev->dev, gpio_keys_quiesce_key, bdata);
  443. if (error) {
  444. dev_err(&pdev->dev,
  445. "failed to register quiesce action, error: %d\n",
  446. error);
  447. return error;
  448. }
  449. /*
  450. * If platform has specified that the button can be disabled,
  451. * we don't want it to share the interrupt line.
  452. */
  453. if (!button->can_disable)
  454. irqflags |= IRQF_SHARED;
  455. error = devm_request_any_context_irq(&pdev->dev, bdata->irq,
  456. isr, irqflags, desc, bdata);
  457. if (error < 0) {
  458. dev_err(dev, "Unable to claim irq %d; error %d\n",
  459. bdata->irq, error);
  460. return error;
  461. }
  462. return 0;
  463. }
  464. static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
  465. {
  466. struct input_dev *input = ddata->input;
  467. int i;
  468. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  469. struct gpio_button_data *bdata = &ddata->data[i];
  470. if (gpio_is_valid(bdata->button->gpio))
  471. gpio_keys_gpio_report_event(bdata);
  472. }
  473. input_sync(input);
  474. }
  475. static int gpio_keys_open(struct input_dev *input)
  476. {
  477. struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
  478. const struct gpio_keys_platform_data *pdata = ddata->pdata;
  479. int error;
  480. if (pdata->enable) {
  481. error = pdata->enable(input->dev.parent);
  482. if (error)
  483. return error;
  484. }
  485. /* Report current state of buttons that are connected to GPIOs */
  486. gpio_keys_report_state(ddata);
  487. return 0;
  488. }
  489. static void gpio_keys_close(struct input_dev *input)
  490. {
  491. struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
  492. const struct gpio_keys_platform_data *pdata = ddata->pdata;
  493. if (pdata->disable)
  494. pdata->disable(input->dev.parent);
  495. }
  496. /*
  497. * Handlers for alternative sources of platform_data
  498. */
  499. #ifdef CONFIG_OF
  500. /*
  501. * Translate OpenFirmware node properties into platform_data
  502. */
  503. static struct gpio_keys_platform_data *
  504. gpio_keys_get_devtree_pdata(struct device *dev)
  505. {
  506. struct device_node *node, *pp;
  507. struct gpio_keys_platform_data *pdata;
  508. struct gpio_keys_button *button;
  509. int error;
  510. int nbuttons;
  511. int i;
  512. node = dev->of_node;
  513. if (!node)
  514. return ERR_PTR(-ENODEV);
  515. nbuttons = of_get_child_count(node);
  516. if (nbuttons == 0)
  517. return ERR_PTR(-ENODEV);
  518. pdata = devm_kzalloc(dev,
  519. sizeof(*pdata) + nbuttons * sizeof(*button),
  520. GFP_KERNEL);
  521. if (!pdata)
  522. return ERR_PTR(-ENOMEM);
  523. pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
  524. pdata->nbuttons = nbuttons;
  525. pdata->rep = !!of_get_property(node, "autorepeat", NULL);
  526. i = 0;
  527. for_each_child_of_node(node, pp) {
  528. enum of_gpio_flags flags;
  529. button = &pdata->buttons[i++];
  530. button->gpio = of_get_gpio_flags(pp, 0, &flags);
  531. if (button->gpio < 0) {
  532. error = button->gpio;
  533. if (error != -ENOENT) {
  534. if (error != -EPROBE_DEFER)
  535. dev_err(dev,
  536. "Failed to get gpio flags, error: %d\n",
  537. error);
  538. return ERR_PTR(error);
  539. }
  540. } else {
  541. button->active_low = flags & OF_GPIO_ACTIVE_LOW;
  542. }
  543. button->irq = irq_of_parse_and_map(pp, 0);
  544. if (!gpio_is_valid(button->gpio) && !button->irq) {
  545. dev_err(dev, "Found button without gpios or irqs\n");
  546. return ERR_PTR(-EINVAL);
  547. }
  548. if (of_property_read_u32(pp, "linux,code", &button->code)) {
  549. dev_err(dev, "Button without keycode: 0x%x\n",
  550. button->gpio);
  551. return ERR_PTR(-EINVAL);
  552. }
  553. button->desc = of_get_property(pp, "label", NULL);
  554. if (of_property_read_u32(pp, "linux,input-type", &button->type))
  555. button->type = EV_KEY;
  556. button->wakeup = of_property_read_bool(pp, "wakeup-source") ||
  557. /* legacy name */
  558. of_property_read_bool(pp, "gpio-key,wakeup");
  559. button->can_disable = !!of_get_property(pp, "linux,can-disable", NULL);
  560. if (of_property_read_u32(pp, "debounce-interval",
  561. &button->debounce_interval))
  562. button->debounce_interval = 5;
  563. }
  564. if (pdata->nbuttons == 0)
  565. return ERR_PTR(-EINVAL);
  566. return pdata;
  567. }
  568. static const struct of_device_id gpio_keys_of_match[] = {
  569. { .compatible = "gpio-keys", },
  570. { },
  571. };
  572. MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
  573. #else
  574. static inline struct gpio_keys_platform_data *
  575. gpio_keys_get_devtree_pdata(struct device *dev)
  576. {
  577. return ERR_PTR(-ENODEV);
  578. }
  579. #endif
  580. static int gpio_keys_probe(struct platform_device *pdev)
  581. {
  582. struct device *dev = &pdev->dev;
  583. const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
  584. struct gpio_keys_drvdata *ddata;
  585. struct input_dev *input;
  586. size_t size;
  587. int i, error;
  588. int wakeup = 0;
  589. if (!pdata) {
  590. pdata = gpio_keys_get_devtree_pdata(dev);
  591. if (IS_ERR(pdata))
  592. return PTR_ERR(pdata);
  593. }
  594. size = sizeof(struct gpio_keys_drvdata) +
  595. pdata->nbuttons * sizeof(struct gpio_button_data);
  596. ddata = devm_kzalloc(dev, size, GFP_KERNEL);
  597. if (!ddata) {
  598. dev_err(dev, "failed to allocate state\n");
  599. return -ENOMEM;
  600. }
  601. input = devm_input_allocate_device(dev);
  602. if (!input) {
  603. dev_err(dev, "failed to allocate input device\n");
  604. return -ENOMEM;
  605. }
  606. ddata->pdata = pdata;
  607. ddata->input = input;
  608. mutex_init(&ddata->disable_lock);
  609. platform_set_drvdata(pdev, ddata);
  610. input_set_drvdata(input, ddata);
  611. input->name = pdata->name ? : pdev->name;
  612. input->phys = "gpio-keys/input0";
  613. input->dev.parent = &pdev->dev;
  614. input->open = gpio_keys_open;
  615. input->close = gpio_keys_close;
  616. input->id.bustype = BUS_HOST;
  617. input->id.vendor = 0x0001;
  618. input->id.product = 0x0001;
  619. input->id.version = 0x0100;
  620. /* Enable auto repeat feature of Linux input subsystem */
  621. if (pdata->rep)
  622. __set_bit(EV_REP, input->evbit);
  623. for (i = 0; i < pdata->nbuttons; i++) {
  624. const struct gpio_keys_button *button = &pdata->buttons[i];
  625. struct gpio_button_data *bdata = &ddata->data[i];
  626. error = gpio_keys_setup_key(pdev, input, bdata, button);
  627. if (error)
  628. return error;
  629. if (button->wakeup)
  630. wakeup = 1;
  631. }
  632. error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
  633. if (error) {
  634. dev_err(dev, "Unable to export keys/switches, error: %d\n",
  635. error);
  636. return error;
  637. }
  638. error = input_register_device(input);
  639. if (error) {
  640. dev_err(dev, "Unable to register input device, error: %d\n",
  641. error);
  642. goto err_remove_group;
  643. }
  644. device_init_wakeup(&pdev->dev, wakeup);
  645. return 0;
  646. err_remove_group:
  647. sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
  648. return error;
  649. }
  650. static int gpio_keys_remove(struct platform_device *pdev)
  651. {
  652. sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
  653. device_init_wakeup(&pdev->dev, 0);
  654. return 0;
  655. }
  656. #ifdef CONFIG_PM_SLEEP
  657. static int gpio_keys_suspend(struct device *dev)
  658. {
  659. struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
  660. struct input_dev *input = ddata->input;
  661. int i;
  662. if (device_may_wakeup(dev)) {
  663. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  664. struct gpio_button_data *bdata = &ddata->data[i];
  665. if (bdata->button->wakeup)
  666. enable_irq_wake(bdata->irq);
  667. }
  668. } else {
  669. mutex_lock(&input->mutex);
  670. if (input->users)
  671. gpio_keys_close(input);
  672. mutex_unlock(&input->mutex);
  673. }
  674. return 0;
  675. }
  676. static int gpio_keys_resume(struct device *dev)
  677. {
  678. struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
  679. struct input_dev *input = ddata->input;
  680. int error = 0;
  681. int i;
  682. if (device_may_wakeup(dev)) {
  683. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  684. struct gpio_button_data *bdata = &ddata->data[i];
  685. if (bdata->button->wakeup)
  686. disable_irq_wake(bdata->irq);
  687. }
  688. } else {
  689. mutex_lock(&input->mutex);
  690. if (input->users)
  691. error = gpio_keys_open(input);
  692. mutex_unlock(&input->mutex);
  693. }
  694. if (error)
  695. return error;
  696. gpio_keys_report_state(ddata);
  697. return 0;
  698. }
  699. #endif
  700. static SIMPLE_DEV_PM_OPS(gpio_keys_pm_ops, gpio_keys_suspend, gpio_keys_resume);
  701. static struct platform_driver gpio_keys_device_driver = {
  702. .probe = gpio_keys_probe,
  703. .remove = gpio_keys_remove,
  704. .driver = {
  705. .name = "gpio-keys",
  706. .pm = &gpio_keys_pm_ops,
  707. .of_match_table = of_match_ptr(gpio_keys_of_match),
  708. }
  709. };
  710. static int __init gpio_keys_init(void)
  711. {
  712. return platform_driver_register(&gpio_keys_device_driver);
  713. }
  714. static void __exit gpio_keys_exit(void)
  715. {
  716. platform_driver_unregister(&gpio_keys_device_driver);
  717. }
  718. late_initcall(gpio_keys_init);
  719. module_exit(gpio_keys_exit);
  720. MODULE_LICENSE("GPL");
  721. MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
  722. MODULE_DESCRIPTION("Keyboard driver for GPIOs");
  723. MODULE_ALIAS("platform:gpio-keys");