gpio_keys.c 21 KB

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