gpio_keys.c 21 KB

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