gpio_keys.c 21 KB

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