gpio_keys.c 19 KB

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