gpio_event.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* drivers/input/misc/gpio_event.c
  2. *
  3. * Copyright (C) 2007 Google, Inc.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/input.h>
  17. #include <linux/gpio_event.h>
  18. #include <linux/hrtimer.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. struct gpio_event {
  22. struct gpio_event_input_devs *input_devs;
  23. const struct gpio_event_platform_data *info;
  24. void *state[0];
  25. };
  26. static int gpio_input_event(
  27. struct input_dev *dev, unsigned int type, unsigned int code, int value)
  28. {
  29. int i;
  30. int devnr;
  31. int ret = 0;
  32. int tmp_ret;
  33. struct gpio_event_info **ii;
  34. struct gpio_event *ip = input_get_drvdata(dev);
  35. for (devnr = 0; devnr < ip->input_devs->count; devnr++)
  36. if (ip->input_devs->dev[devnr] == dev)
  37. break;
  38. if (devnr == ip->input_devs->count) {
  39. pr_err("gpio_input_event: unknown device %p\n", dev);
  40. return -EIO;
  41. }
  42. for (i = 0, ii = ip->info->info; i < ip->info->info_count; i++, ii++) {
  43. if ((*ii)->event) {
  44. tmp_ret = (*ii)->event(ip->input_devs, *ii,
  45. &ip->state[i],
  46. devnr, type, code, value);
  47. if (tmp_ret)
  48. ret = tmp_ret;
  49. }
  50. }
  51. return ret;
  52. }
  53. static int gpio_event_call_all_func(struct gpio_event *ip, int func)
  54. {
  55. int i;
  56. int ret;
  57. struct gpio_event_info **ii;
  58. if (func == GPIO_EVENT_FUNC_INIT || func == GPIO_EVENT_FUNC_RESUME) {
  59. ii = ip->info->info;
  60. for (i = 0; i < ip->info->info_count; i++, ii++) {
  61. if ((*ii)->func == NULL) {
  62. ret = -ENODEV;
  63. pr_err("gpio_event_probe: Incomplete pdata, "
  64. "no function\n");
  65. goto err_no_func;
  66. }
  67. if (func == GPIO_EVENT_FUNC_RESUME && (*ii)->no_suspend)
  68. continue;
  69. ret = (*ii)->func(ip->input_devs, *ii, &ip->state[i],
  70. func);
  71. if (ret) {
  72. pr_err("gpio_event_probe: function failed\n");
  73. goto err_func_failed;
  74. }
  75. }
  76. return 0;
  77. }
  78. ret = 0;
  79. i = ip->info->info_count;
  80. ii = ip->info->info + i;
  81. while (i > 0) {
  82. i--;
  83. ii--;
  84. if ((func & ~1) == GPIO_EVENT_FUNC_SUSPEND && (*ii)->no_suspend)
  85. continue;
  86. (*ii)->func(ip->input_devs, *ii, &ip->state[i], func & ~1);
  87. err_func_failed:
  88. err_no_func:
  89. ;
  90. }
  91. return ret;
  92. }
  93. static void __maybe_unused gpio_event_suspend(struct gpio_event *ip)
  94. {
  95. gpio_event_call_all_func(ip, GPIO_EVENT_FUNC_SUSPEND);
  96. if (ip->info->power)
  97. ip->info->power(ip->info, 0);
  98. }
  99. static void __maybe_unused gpio_event_resume(struct gpio_event *ip)
  100. {
  101. if (ip->info->power)
  102. ip->info->power(ip->info, 1);
  103. gpio_event_call_all_func(ip, GPIO_EVENT_FUNC_RESUME);
  104. }
  105. static int gpio_event_probe(struct platform_device *pdev)
  106. {
  107. int err;
  108. struct gpio_event *ip;
  109. struct gpio_event_platform_data *event_info;
  110. int dev_count = 1;
  111. int i;
  112. int registered = 0;
  113. event_info = pdev->dev.platform_data;
  114. if (event_info == NULL) {
  115. pr_err("gpio_event_probe: No pdata\n");
  116. return -ENODEV;
  117. }
  118. if ((!event_info->name && !event_info->names[0]) ||
  119. !event_info->info || !event_info->info_count) {
  120. pr_err("gpio_event_probe: Incomplete pdata\n");
  121. return -ENODEV;
  122. }
  123. if (!event_info->name)
  124. while (event_info->names[dev_count])
  125. dev_count++;
  126. ip = kzalloc(sizeof(*ip) +
  127. sizeof(ip->state[0]) * event_info->info_count +
  128. sizeof(*ip->input_devs) +
  129. sizeof(ip->input_devs->dev[0]) * dev_count, GFP_KERNEL);
  130. if (ip == NULL) {
  131. err = -ENOMEM;
  132. pr_err("gpio_event_probe: Failed to allocate private data\n");
  133. goto err_kp_alloc_failed;
  134. }
  135. ip->input_devs = (void*)&ip->state[event_info->info_count];
  136. platform_set_drvdata(pdev, ip);
  137. for (i = 0; i < dev_count; i++) {
  138. struct input_dev *input_dev = input_allocate_device();
  139. if (input_dev == NULL) {
  140. err = -ENOMEM;
  141. pr_err("gpio_event_probe: "
  142. "Failed to allocate input device\n");
  143. goto err_input_dev_alloc_failed;
  144. }
  145. input_set_drvdata(input_dev, ip);
  146. input_dev->name = event_info->name ?
  147. event_info->name : event_info->names[i];
  148. input_dev->event = gpio_input_event;
  149. ip->input_devs->dev[i] = input_dev;
  150. }
  151. ip->input_devs->count = dev_count;
  152. ip->info = event_info;
  153. if (event_info->power)
  154. ip->info->power(ip->info, 1);
  155. err = gpio_event_call_all_func(ip, GPIO_EVENT_FUNC_INIT);
  156. if (err)
  157. goto err_call_all_func_failed;
  158. for (i = 0; i < dev_count; i++) {
  159. err = input_register_device(ip->input_devs->dev[i]);
  160. if (err) {
  161. pr_err("gpio_event_probe: Unable to register %s "
  162. "input device\n", ip->input_devs->dev[i]->name);
  163. goto err_input_register_device_failed;
  164. }
  165. registered++;
  166. }
  167. return 0;
  168. err_input_register_device_failed:
  169. gpio_event_call_all_func(ip, GPIO_EVENT_FUNC_UNINIT);
  170. err_call_all_func_failed:
  171. if (event_info->power)
  172. ip->info->power(ip->info, 0);
  173. for (i = 0; i < registered; i++)
  174. input_unregister_device(ip->input_devs->dev[i]);
  175. for (i = dev_count - 1; i >= registered; i--) {
  176. input_free_device(ip->input_devs->dev[i]);
  177. err_input_dev_alloc_failed:
  178. ;
  179. }
  180. kfree(ip);
  181. err_kp_alloc_failed:
  182. return err;
  183. }
  184. static int gpio_event_remove(struct platform_device *pdev)
  185. {
  186. struct gpio_event *ip = platform_get_drvdata(pdev);
  187. int i;
  188. gpio_event_call_all_func(ip, GPIO_EVENT_FUNC_UNINIT);
  189. if (ip->info->power)
  190. ip->info->power(ip->info, 0);
  191. for (i = 0; i < ip->input_devs->count; i++)
  192. input_unregister_device(ip->input_devs->dev[i]);
  193. kfree(ip);
  194. return 0;
  195. }
  196. static struct platform_driver gpio_event_driver = {
  197. .probe = gpio_event_probe,
  198. .remove = gpio_event_remove,
  199. .driver = {
  200. .name = GPIO_EVENT_DEV_NAME,
  201. },
  202. };
  203. static int __devinit gpio_event_init(void)
  204. {
  205. return platform_driver_register(&gpio_event_driver);
  206. }
  207. static void __exit gpio_event_exit(void)
  208. {
  209. platform_driver_unregister(&gpio_event_driver);
  210. }
  211. module_init(gpio_event_init);
  212. module_exit(gpio_event_exit);
  213. MODULE_DESCRIPTION("GPIO Event Driver");
  214. MODULE_LICENSE("GPL");