gpio_keys.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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. mutex_lock(&ddata->disable_lock);
  213. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  214. struct gpio_button_data *bdata = &ddata->data[i];
  215. if (bdata->button->type != type)
  216. continue;
  217. if (test_bit(bdata->button->code, bits))
  218. gpio_keys_disable_button(bdata);
  219. else
  220. gpio_keys_enable_button(bdata);
  221. }
  222. mutex_unlock(&ddata->disable_lock);
  223. out:
  224. kfree(bits);
  225. return error;
  226. }
  227. #define ATTR_SHOW_FN(name, type, only_disabled) \
  228. static ssize_t gpio_keys_show_##name(struct device *dev, \
  229. struct device_attribute *attr, \
  230. char *buf) \
  231. { \
  232. struct platform_device *pdev = to_platform_device(dev); \
  233. struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
  234. \
  235. return gpio_keys_attr_show_helper(ddata, buf, \
  236. type, only_disabled); \
  237. }
  238. ATTR_SHOW_FN(keys, EV_KEY, false);
  239. ATTR_SHOW_FN(switches, EV_SW, false);
  240. ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
  241. ATTR_SHOW_FN(disabled_switches, EV_SW, true);
  242. /*
  243. * ATTRIBUTES:
  244. *
  245. * /sys/devices/platform/gpio-keys/keys [ro]
  246. * /sys/devices/platform/gpio-keys/switches [ro]
  247. */
  248. static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
  249. static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
  250. #define ATTR_STORE_FN(name, type) \
  251. static ssize_t gpio_keys_store_##name(struct device *dev, \
  252. struct device_attribute *attr, \
  253. const char *buf, \
  254. size_t count) \
  255. { \
  256. struct platform_device *pdev = to_platform_device(dev); \
  257. struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
  258. ssize_t error; \
  259. \
  260. error = gpio_keys_attr_store_helper(ddata, buf, type); \
  261. if (error) \
  262. return error; \
  263. \
  264. return count; \
  265. }
  266. ATTR_STORE_FN(disabled_keys, EV_KEY);
  267. ATTR_STORE_FN(disabled_switches, EV_SW);
  268. /*
  269. * ATTRIBUTES:
  270. *
  271. * /sys/devices/platform/gpio-keys/disabled_keys [rw]
  272. * /sys/devices/platform/gpio-keys/disables_switches [rw]
  273. */
  274. static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
  275. gpio_keys_show_disabled_keys,
  276. gpio_keys_store_disabled_keys);
  277. static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
  278. gpio_keys_show_disabled_switches,
  279. gpio_keys_store_disabled_switches);
  280. static struct attribute *gpio_keys_attrs[] = {
  281. &dev_attr_keys.attr,
  282. &dev_attr_switches.attr,
  283. &dev_attr_disabled_keys.attr,
  284. &dev_attr_disabled_switches.attr,
  285. NULL,
  286. };
  287. static struct attribute_group gpio_keys_attr_group = {
  288. .attrs = gpio_keys_attrs,
  289. };
  290. static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
  291. {
  292. const struct gpio_keys_button *button = bdata->button;
  293. struct input_dev *input = bdata->input;
  294. unsigned int type = button->type ?: EV_KEY;
  295. int state = (gpio_get_value_cansleep(button->gpio) ? 1 : 0) ^ button->active_low;
  296. if (type == EV_ABS) {
  297. if (state)
  298. input_event(input, type, button->code, button->value);
  299. } else {
  300. input_event(input, type, button->code, !!state);
  301. }
  302. input_sync(input);
  303. }
  304. static void gpio_keys_gpio_work_func(struct work_struct *work)
  305. {
  306. struct gpio_button_data *bdata =
  307. container_of(work, struct gpio_button_data, work.work);
  308. gpio_keys_gpio_report_event(bdata);
  309. if (bdata->button->wakeup)
  310. pm_relax(bdata->input->dev.parent);
  311. }
  312. static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
  313. {
  314. struct gpio_button_data *bdata = dev_id;
  315. BUG_ON(irq != bdata->irq);
  316. if (bdata->button->wakeup)
  317. pm_stay_awake(bdata->input->dev.parent);
  318. mod_delayed_work(system_wq,
  319. &bdata->work,
  320. msecs_to_jiffies(bdata->software_debounce));
  321. return IRQ_HANDLED;
  322. }
  323. static void gpio_keys_irq_timer(unsigned long _data)
  324. {
  325. struct gpio_button_data *bdata = (struct gpio_button_data *)_data;
  326. struct input_dev *input = bdata->input;
  327. unsigned long flags;
  328. spin_lock_irqsave(&bdata->lock, flags);
  329. if (bdata->key_pressed) {
  330. input_event(input, EV_KEY, bdata->button->code, 0);
  331. input_sync(input);
  332. bdata->key_pressed = false;
  333. }
  334. spin_unlock_irqrestore(&bdata->lock, flags);
  335. }
  336. static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
  337. {
  338. struct gpio_button_data *bdata = dev_id;
  339. const struct gpio_keys_button *button = bdata->button;
  340. struct input_dev *input = bdata->input;
  341. unsigned long flags;
  342. BUG_ON(irq != bdata->irq);
  343. spin_lock_irqsave(&bdata->lock, flags);
  344. if (!bdata->key_pressed) {
  345. if (bdata->button->wakeup)
  346. pm_wakeup_event(bdata->input->dev.parent, 0);
  347. input_event(input, EV_KEY, button->code, 1);
  348. input_sync(input);
  349. if (!bdata->release_delay) {
  350. input_event(input, EV_KEY, button->code, 0);
  351. input_sync(input);
  352. goto out;
  353. }
  354. bdata->key_pressed = true;
  355. }
  356. if (bdata->release_delay)
  357. mod_timer(&bdata->release_timer,
  358. jiffies + msecs_to_jiffies(bdata->release_delay));
  359. out:
  360. spin_unlock_irqrestore(&bdata->lock, flags);
  361. return IRQ_HANDLED;
  362. }
  363. static void gpio_keys_quiesce_key(void *data)
  364. {
  365. struct gpio_button_data *bdata = data;
  366. if (gpio_is_valid(bdata->button->gpio))
  367. cancel_delayed_work_sync(&bdata->work);
  368. else
  369. del_timer_sync(&bdata->release_timer);
  370. }
  371. static int gpio_keys_setup_key(struct platform_device *pdev,
  372. struct input_dev *input,
  373. struct gpio_button_data *bdata,
  374. const struct gpio_keys_button *button)
  375. {
  376. const char *desc = button->desc ? button->desc : "gpio_keys";
  377. struct device *dev = &pdev->dev;
  378. irq_handler_t isr;
  379. unsigned long irqflags;
  380. int irq;
  381. int error;
  382. bdata->input = input;
  383. bdata->button = button;
  384. spin_lock_init(&bdata->lock);
  385. if (gpio_is_valid(button->gpio)) {
  386. error = devm_gpio_request_one(&pdev->dev, button->gpio,
  387. GPIOF_IN, desc);
  388. if (error < 0) {
  389. dev_err(dev, "Failed to request GPIO %d, error %d\n",
  390. button->gpio, error);
  391. return error;
  392. }
  393. if (button->debounce_interval) {
  394. error = gpio_set_debounce(button->gpio,
  395. button->debounce_interval * 1000);
  396. /* use timer if gpiolib doesn't provide debounce */
  397. if (error < 0)
  398. bdata->software_debounce =
  399. button->debounce_interval;
  400. }
  401. if (button->irq) {
  402. bdata->irq = button->irq;
  403. } else {
  404. irq = gpio_to_irq(button->gpio);
  405. if (irq < 0) {
  406. error = irq;
  407. dev_err(dev,
  408. "Unable to get irq number for GPIO %d, error %d\n",
  409. button->gpio, error);
  410. return error;
  411. }
  412. bdata->irq = irq;
  413. }
  414. INIT_DELAYED_WORK(&bdata->work, gpio_keys_gpio_work_func);
  415. isr = gpio_keys_gpio_isr;
  416. irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
  417. } else {
  418. if (!button->irq) {
  419. dev_err(dev, "No IRQ specified\n");
  420. return -EINVAL;
  421. }
  422. bdata->irq = button->irq;
  423. if (button->type && button->type != EV_KEY) {
  424. dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n");
  425. return -EINVAL;
  426. }
  427. bdata->release_delay = button->debounce_interval;
  428. setup_timer(&bdata->release_timer,
  429. gpio_keys_irq_timer, (unsigned long)bdata);
  430. isr = gpio_keys_irq_isr;
  431. irqflags = 0;
  432. }
  433. input_set_capability(input, button->type ?: EV_KEY, button->code);
  434. /*
  435. * Install custom action to cancel release timer and
  436. * workqueue item.
  437. */
  438. error = devm_add_action(&pdev->dev, gpio_keys_quiesce_key, bdata);
  439. if (error) {
  440. dev_err(&pdev->dev,
  441. "failed to register quiesce action, error: %d\n",
  442. error);
  443. return error;
  444. }
  445. /*
  446. * If platform has specified that the button can be disabled,
  447. * we don't want it to share the interrupt line.
  448. */
  449. if (!button->can_disable)
  450. irqflags |= IRQF_SHARED;
  451. error = devm_request_any_context_irq(&pdev->dev, bdata->irq,
  452. isr, irqflags, desc, bdata);
  453. if (error < 0) {
  454. dev_err(dev, "Unable to claim irq %d; error %d\n",
  455. bdata->irq, error);
  456. return error;
  457. }
  458. return 0;
  459. }
  460. static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
  461. {
  462. struct input_dev *input = ddata->input;
  463. int i;
  464. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  465. struct gpio_button_data *bdata = &ddata->data[i];
  466. if (gpio_is_valid(bdata->button->gpio))
  467. gpio_keys_gpio_report_event(bdata);
  468. }
  469. input_sync(input);
  470. }
  471. static int gpio_keys_open(struct input_dev *input)
  472. {
  473. struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
  474. const struct gpio_keys_platform_data *pdata = ddata->pdata;
  475. int error;
  476. if (pdata->enable) {
  477. error = pdata->enable(input->dev.parent);
  478. if (error)
  479. return error;
  480. }
  481. /* Report current state of buttons that are connected to GPIOs */
  482. gpio_keys_report_state(ddata);
  483. return 0;
  484. }
  485. static void gpio_keys_close(struct input_dev *input)
  486. {
  487. struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
  488. const struct gpio_keys_platform_data *pdata = ddata->pdata;
  489. if (pdata->disable)
  490. pdata->disable(input->dev.parent);
  491. }
  492. /*
  493. * Handlers for alternative sources of platform_data
  494. */
  495. #ifdef CONFIG_OF
  496. /*
  497. * Translate OpenFirmware node properties into platform_data
  498. */
  499. static struct gpio_keys_platform_data *
  500. gpio_keys_get_devtree_pdata(struct device *dev)
  501. {
  502. struct device_node *node, *pp;
  503. struct gpio_keys_platform_data *pdata;
  504. struct gpio_keys_button *button;
  505. int error;
  506. int nbuttons;
  507. int i;
  508. node = dev->of_node;
  509. if (!node)
  510. return ERR_PTR(-ENODEV);
  511. nbuttons = of_get_child_count(node);
  512. if (nbuttons == 0)
  513. return ERR_PTR(-ENODEV);
  514. pdata = devm_kzalloc(dev,
  515. sizeof(*pdata) + nbuttons * sizeof(*button),
  516. GFP_KERNEL);
  517. if (!pdata)
  518. return ERR_PTR(-ENOMEM);
  519. pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
  520. pdata->nbuttons = nbuttons;
  521. pdata->rep = !!of_get_property(node, "autorepeat", NULL);
  522. i = 0;
  523. for_each_child_of_node(node, pp) {
  524. enum of_gpio_flags flags;
  525. button = &pdata->buttons[i++];
  526. button->gpio = of_get_gpio_flags(pp, 0, &flags);
  527. if (button->gpio < 0) {
  528. error = button->gpio;
  529. if (error != -ENOENT) {
  530. if (error != -EPROBE_DEFER)
  531. dev_err(dev,
  532. "Failed to get gpio flags, error: %d\n",
  533. error);
  534. return ERR_PTR(error);
  535. }
  536. } else {
  537. button->active_low = flags & OF_GPIO_ACTIVE_LOW;
  538. }
  539. button->irq = irq_of_parse_and_map(pp, 0);
  540. if (!gpio_is_valid(button->gpio) && !button->irq) {
  541. dev_err(dev, "Found button without gpios or irqs\n");
  542. return ERR_PTR(-EINVAL);
  543. }
  544. if (of_property_read_u32(pp, "linux,code", &button->code)) {
  545. dev_err(dev, "Button without keycode: 0x%x\n",
  546. button->gpio);
  547. return ERR_PTR(-EINVAL);
  548. }
  549. button->desc = of_get_property(pp, "label", NULL);
  550. if (of_property_read_u32(pp, "linux,input-type", &button->type))
  551. button->type = EV_KEY;
  552. button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
  553. button->can_disable = !!of_get_property(pp, "linux,can-disable", NULL);
  554. if (of_property_read_u32(pp, "debounce-interval",
  555. &button->debounce_interval))
  556. button->debounce_interval = 5;
  557. }
  558. if (pdata->nbuttons == 0)
  559. return ERR_PTR(-EINVAL);
  560. return pdata;
  561. }
  562. static const struct of_device_id gpio_keys_of_match[] = {
  563. { .compatible = "gpio-keys", },
  564. { },
  565. };
  566. MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
  567. #else
  568. static inline struct gpio_keys_platform_data *
  569. gpio_keys_get_devtree_pdata(struct device *dev)
  570. {
  571. return ERR_PTR(-ENODEV);
  572. }
  573. #endif
  574. static int gpio_keys_probe(struct platform_device *pdev)
  575. {
  576. struct device *dev = &pdev->dev;
  577. const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
  578. struct gpio_keys_drvdata *ddata;
  579. struct input_dev *input;
  580. size_t size;
  581. int i, error;
  582. int wakeup = 0;
  583. if (!pdata) {
  584. pdata = gpio_keys_get_devtree_pdata(dev);
  585. if (IS_ERR(pdata))
  586. return PTR_ERR(pdata);
  587. }
  588. size = sizeof(struct gpio_keys_drvdata) +
  589. pdata->nbuttons * sizeof(struct gpio_button_data);
  590. ddata = devm_kzalloc(dev, size, GFP_KERNEL);
  591. if (!ddata) {
  592. dev_err(dev, "failed to allocate state\n");
  593. return -ENOMEM;
  594. }
  595. input = devm_input_allocate_device(dev);
  596. if (!input) {
  597. dev_err(dev, "failed to allocate input device\n");
  598. return -ENOMEM;
  599. }
  600. ddata->pdata = pdata;
  601. ddata->input = input;
  602. mutex_init(&ddata->disable_lock);
  603. platform_set_drvdata(pdev, ddata);
  604. input_set_drvdata(input, ddata);
  605. input->name = pdata->name ? : pdev->name;
  606. input->phys = "gpio-keys/input0";
  607. input->dev.parent = &pdev->dev;
  608. input->open = gpio_keys_open;
  609. input->close = gpio_keys_close;
  610. input->id.bustype = BUS_HOST;
  611. input->id.vendor = 0x0001;
  612. input->id.product = 0x0001;
  613. input->id.version = 0x0100;
  614. /* Enable auto repeat feature of Linux input subsystem */
  615. if (pdata->rep)
  616. __set_bit(EV_REP, input->evbit);
  617. for (i = 0; i < pdata->nbuttons; i++) {
  618. const struct gpio_keys_button *button = &pdata->buttons[i];
  619. struct gpio_button_data *bdata = &ddata->data[i];
  620. error = gpio_keys_setup_key(pdev, input, bdata, button);
  621. if (error)
  622. return error;
  623. if (button->wakeup)
  624. wakeup = 1;
  625. }
  626. error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
  627. if (error) {
  628. dev_err(dev, "Unable to export keys/switches, error: %d\n",
  629. error);
  630. return error;
  631. }
  632. error = input_register_device(input);
  633. if (error) {
  634. dev_err(dev, "Unable to register input device, error: %d\n",
  635. error);
  636. goto err_remove_group;
  637. }
  638. device_init_wakeup(&pdev->dev, wakeup);
  639. return 0;
  640. err_remove_group:
  641. sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
  642. return error;
  643. }
  644. static int gpio_keys_remove(struct platform_device *pdev)
  645. {
  646. sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
  647. device_init_wakeup(&pdev->dev, 0);
  648. return 0;
  649. }
  650. #ifdef CONFIG_PM_SLEEP
  651. static int gpio_keys_suspend(struct device *dev)
  652. {
  653. struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
  654. struct input_dev *input = ddata->input;
  655. int i;
  656. if (device_may_wakeup(dev)) {
  657. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  658. struct gpio_button_data *bdata = &ddata->data[i];
  659. if (bdata->button->wakeup)
  660. enable_irq_wake(bdata->irq);
  661. }
  662. } else {
  663. mutex_lock(&input->mutex);
  664. if (input->users)
  665. gpio_keys_close(input);
  666. mutex_unlock(&input->mutex);
  667. }
  668. return 0;
  669. }
  670. static int gpio_keys_resume(struct device *dev)
  671. {
  672. struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
  673. struct input_dev *input = ddata->input;
  674. int error = 0;
  675. int i;
  676. if (device_may_wakeup(dev)) {
  677. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  678. struct gpio_button_data *bdata = &ddata->data[i];
  679. if (bdata->button->wakeup)
  680. disable_irq_wake(bdata->irq);
  681. }
  682. } else {
  683. mutex_lock(&input->mutex);
  684. if (input->users)
  685. error = gpio_keys_open(input);
  686. mutex_unlock(&input->mutex);
  687. }
  688. if (error)
  689. return error;
  690. gpio_keys_report_state(ddata);
  691. return 0;
  692. }
  693. #endif
  694. static SIMPLE_DEV_PM_OPS(gpio_keys_pm_ops, gpio_keys_suspend, gpio_keys_resume);
  695. static struct platform_driver gpio_keys_device_driver = {
  696. .probe = gpio_keys_probe,
  697. .remove = gpio_keys_remove,
  698. .driver = {
  699. .name = "gpio-keys",
  700. .pm = &gpio_keys_pm_ops,
  701. .of_match_table = of_match_ptr(gpio_keys_of_match),
  702. }
  703. };
  704. static int __init gpio_keys_init(void)
  705. {
  706. return platform_driver_register(&gpio_keys_device_driver);
  707. }
  708. static void __exit gpio_keys_exit(void)
  709. {
  710. platform_driver_unregister(&gpio_keys_device_driver);
  711. }
  712. late_initcall(gpio_keys_init);
  713. module_exit(gpio_keys_exit);
  714. MODULE_LICENSE("GPL");
  715. MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
  716. MODULE_DESCRIPTION("Keyboard driver for GPIOs");
  717. MODULE_ALIAS("platform:gpio-keys");