gpio_keys.c 21 KB

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