gpio_keys.c 22 KB

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