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_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 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_one(button->gpio, GPIOF_IN, 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. if (button->debounce_interval) {
  386. error = gpio_set_debounce(button->gpio,
  387. button->debounce_interval * 1000);
  388. /* use timer if gpiolib doesn't provide debounce */
  389. if (error < 0)
  390. bdata->timer_debounce =
  391. button->debounce_interval;
  392. }
  393. irq = gpio_to_irq(button->gpio);
  394. if (irq < 0) {
  395. error = irq;
  396. dev_err(dev,
  397. "Unable to get irq number for GPIO %d, error %d\n",
  398. button->gpio, error);
  399. goto fail;
  400. }
  401. bdata->irq = irq;
  402. INIT_WORK(&bdata->work, gpio_keys_gpio_work_func);
  403. setup_timer(&bdata->timer,
  404. gpio_keys_gpio_timer, (unsigned long)bdata);
  405. isr = gpio_keys_gpio_isr;
  406. irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
  407. } else {
  408. if (!button->irq) {
  409. dev_err(dev, "No IRQ specified\n");
  410. return -EINVAL;
  411. }
  412. bdata->irq = button->irq;
  413. if (button->type && button->type != EV_KEY) {
  414. dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n");
  415. return -EINVAL;
  416. }
  417. bdata->timer_debounce = button->debounce_interval;
  418. setup_timer(&bdata->timer,
  419. gpio_keys_irq_timer, (unsigned long)bdata);
  420. isr = gpio_keys_irq_isr;
  421. irqflags = 0;
  422. }
  423. input_set_capability(input, button->type ?: EV_KEY, button->code);
  424. /*
  425. * If platform has specified that the button can be disabled,
  426. * we don't want it to share the interrupt line.
  427. */
  428. if (!button->can_disable)
  429. irqflags |= IRQF_SHARED;
  430. error = request_any_context_irq(bdata->irq, isr, irqflags, desc, bdata);
  431. if (error < 0) {
  432. dev_err(dev, "Unable to claim irq %d; error %d\n",
  433. bdata->irq, error);
  434. goto fail;
  435. }
  436. return 0;
  437. fail:
  438. if (gpio_is_valid(button->gpio))
  439. gpio_free(button->gpio);
  440. return error;
  441. }
  442. static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
  443. {
  444. struct input_dev *input = ddata->input;
  445. int i;
  446. for (i = 0; i < ddata->pdata->nbuttons; i++) {
  447. struct gpio_button_data *bdata = &ddata->data[i];
  448. if (gpio_is_valid(bdata->button->gpio))
  449. gpio_keys_gpio_report_event(bdata);
  450. }
  451. input_sync(input);
  452. }
  453. static int gpio_keys_open(struct input_dev *input)
  454. {
  455. struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
  456. const struct gpio_keys_platform_data *pdata = ddata->pdata;
  457. int error;
  458. if (pdata->enable) {
  459. error = pdata->enable(input->dev.parent);
  460. if (error)
  461. return error;
  462. }
  463. /* Report current state of buttons that are connected to GPIOs */
  464. gpio_keys_report_state(ddata);
  465. return 0;
  466. }
  467. static void gpio_keys_close(struct input_dev *input)
  468. {
  469. struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
  470. const struct gpio_keys_platform_data *pdata = ddata->pdata;
  471. if (pdata->disable)
  472. pdata->disable(input->dev.parent);
  473. }
  474. /*
  475. * Handlers for alternative sources of platform_data
  476. */
  477. #ifdef CONFIG_OF
  478. /*
  479. * Translate OpenFirmware node properties into platform_data
  480. */
  481. static struct gpio_keys_platform_data *
  482. gpio_keys_get_devtree_pdata(struct device *dev)
  483. {
  484. struct device_node *node, *pp;
  485. struct gpio_keys_platform_data *pdata;
  486. struct gpio_keys_button *button;
  487. int error;
  488. int nbuttons;
  489. int i;
  490. node = dev->of_node;
  491. if (!node) {
  492. error = -ENODEV;
  493. goto err_out;
  494. }
  495. nbuttons = of_get_child_count(node);
  496. if (nbuttons == 0) {
  497. error = -ENODEV;
  498. goto err_out;
  499. }
  500. pdata = kzalloc(sizeof(*pdata) + nbuttons * (sizeof *button),
  501. GFP_KERNEL);
  502. if (!pdata) {
  503. error = -ENOMEM;
  504. goto err_out;
  505. }
  506. pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
  507. pdata->nbuttons = nbuttons;
  508. pdata->rep = !!of_get_property(node, "autorepeat", NULL);
  509. i = 0;
  510. for_each_child_of_node(node, pp) {
  511. int gpio;
  512. enum of_gpio_flags flags;
  513. if (!of_find_property(pp, "gpios", NULL)) {
  514. pdata->nbuttons--;
  515. dev_warn(dev, "Found button without gpios\n");
  516. continue;
  517. }
  518. gpio = of_get_gpio_flags(pp, 0, &flags);
  519. if (gpio < 0) {
  520. error = gpio;
  521. if (error != -EPROBE_DEFER)
  522. dev_err(dev,
  523. "Failed to get gpio flags, error: %d\n",
  524. error);
  525. goto err_free_pdata;
  526. }
  527. button = &pdata->buttons[i++];
  528. button->gpio = gpio;
  529. button->active_low = flags & OF_GPIO_ACTIVE_LOW;
  530. if (of_property_read_u32(pp, "linux,code", &button->code)) {
  531. dev_err(dev, "Button without keycode: 0x%x\n",
  532. button->gpio);
  533. error = -EINVAL;
  534. goto err_free_pdata;
  535. }
  536. button->desc = of_get_property(pp, "label", NULL);
  537. if (of_property_read_u32(pp, "linux,input-type", &button->type))
  538. button->type = EV_KEY;
  539. button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
  540. if (of_property_read_u32(pp, "debounce-interval",
  541. &button->debounce_interval))
  542. button->debounce_interval = 5;
  543. }
  544. if (pdata->nbuttons == 0) {
  545. error = -EINVAL;
  546. goto err_free_pdata;
  547. }
  548. return pdata;
  549. err_free_pdata:
  550. kfree(pdata);
  551. err_out:
  552. return ERR_PTR(error);
  553. }
  554. static struct of_device_id gpio_keys_of_match[] = {
  555. { .compatible = "gpio-keys", },
  556. { },
  557. };
  558. MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
  559. #else
  560. static inline struct gpio_keys_platform_data *
  561. gpio_keys_get_devtree_pdata(struct device *dev)
  562. {
  563. return ERR_PTR(-ENODEV);
  564. }
  565. #endif
  566. static void gpio_remove_key(struct gpio_button_data *bdata)
  567. {
  568. free_irq(bdata->irq, bdata);
  569. if (bdata->timer_debounce)
  570. del_timer_sync(&bdata->timer);
  571. cancel_work_sync(&bdata->work);
  572. if (gpio_is_valid(bdata->button->gpio))
  573. gpio_free(bdata->button->gpio);
  574. }
  575. static int gpio_keys_probe(struct platform_device *pdev)
  576. {
  577. struct device *dev = &pdev->dev;
  578. const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
  579. struct gpio_keys_drvdata *ddata;
  580. struct input_dev *input;
  581. int i, error;
  582. int wakeup = 0;
  583. if (!pdata) {
  584. pdata = gpio_keys_get_devtree_pdata(dev);
  585. if (IS_ERR(pdata))
  586. return PTR_ERR(pdata);
  587. }
  588. ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
  589. pdata->nbuttons * sizeof(struct gpio_button_data),
  590. GFP_KERNEL);
  591. input = input_allocate_device();
  592. if (!ddata || !input) {
  593. dev_err(dev, "failed to allocate state\n");
  594. error = -ENOMEM;
  595. goto fail1;
  596. }
  597. ddata->pdata = pdata;
  598. ddata->input = input;
  599. mutex_init(&ddata->disable_lock);
  600. platform_set_drvdata(pdev, ddata);
  601. input_set_drvdata(input, ddata);
  602. input->name = pdata->name ? : pdev->name;
  603. input->phys = "gpio-keys/input0";
  604. input->dev.parent = &pdev->dev;
  605. input->open = gpio_keys_open;
  606. input->close = gpio_keys_close;
  607. input->id.bustype = BUS_HOST;
  608. input->id.vendor = 0x0001;
  609. input->id.product = 0x0001;
  610. input->id.version = 0x0100;
  611. /* Enable auto repeat feature of Linux input subsystem */
  612. if (pdata->rep)
  613. __set_bit(EV_REP, input->evbit);
  614. for (i = 0; i < pdata->nbuttons; i++) {
  615. const struct gpio_keys_button *button = &pdata->buttons[i];
  616. struct gpio_button_data *bdata = &ddata->data[i];
  617. error = gpio_keys_setup_key(pdev, input, bdata, button);
  618. if (error)
  619. goto fail2;
  620. if (button->wakeup)
  621. wakeup = 1;
  622. }
  623. error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
  624. if (error) {
  625. dev_err(dev, "Unable to export keys/switches, error: %d\n",
  626. error);
  627. goto fail2;
  628. }
  629. error = input_register_device(input);
  630. if (error) {
  631. dev_err(dev, "Unable to register input device, error: %d\n",
  632. error);
  633. goto fail3;
  634. }
  635. device_init_wakeup(&pdev->dev, wakeup);
  636. return 0;
  637. fail3:
  638. sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
  639. fail2:
  640. while (--i >= 0)
  641. gpio_remove_key(&ddata->data[i]);
  642. platform_set_drvdata(pdev, NULL);
  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");