gpio-ir-recv.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/gpio.h>
  17. #include <linux/slab.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/irq.h>
  20. #include <linux/pm_qos.h>
  21. #include <linux/timer.h>
  22. #include <media/rc-core.h>
  23. #include <media/gpio-ir-recv.h>
  24. #define GPIO_IR_DRIVER_NAME "gpio-rc-recv"
  25. #define GPIO_IR_DEVICE_NAME "gpio_ir_recv"
  26. static int gpio_ir_timeout = 200;
  27. module_param_named(gpio_ir_timeout, gpio_ir_timeout, int, 0664);
  28. static int __init gpio_ir_timeout_setup(char *p)
  29. {
  30. gpio_ir_timeout = memparse(p, NULL);
  31. return 0;
  32. }
  33. early_param("gpio_ir_timeout", gpio_ir_timeout_setup);
  34. struct gpio_rc_dev {
  35. struct rc_dev *rcdev;
  36. struct pm_qos_request pm_qos_req;
  37. struct timer_list gpio_ir_timer;
  38. int gpio_nr;
  39. bool active_low;
  40. int can_sleep;
  41. bool can_wakeup;
  42. bool pm_qos_vote;
  43. int gpio_irq_latency;
  44. };
  45. static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id)
  46. {
  47. struct gpio_rc_dev *gpio_dev = dev_id;
  48. int gval;
  49. int rc = 0;
  50. enum raw_event_type type = IR_SPACE;
  51. if (!gpio_dev->pm_qos_vote && gpio_dev->can_wakeup) {
  52. gpio_dev->pm_qos_vote = 1;
  53. pm_qos_update_request(&gpio_dev->pm_qos_req,
  54. gpio_dev->gpio_irq_latency);
  55. }
  56. if (gpio_dev->can_sleep)
  57. gval = gpio_get_value_cansleep(gpio_dev->gpio_nr);
  58. else
  59. gval = gpio_get_value(gpio_dev->gpio_nr);
  60. if (gval < 0)
  61. goto err_get_value;
  62. if (gpio_dev->active_low)
  63. gval = !gval;
  64. if (gval == 1)
  65. type = IR_PULSE;
  66. rc = ir_raw_event_store_edge(gpio_dev->rcdev, type);
  67. if (rc < 0)
  68. goto err_get_value;
  69. ir_raw_event_handle(gpio_dev->rcdev);
  70. if (gpio_dev->can_wakeup)
  71. mod_timer(&gpio_dev->gpio_ir_timer,
  72. jiffies + msecs_to_jiffies(gpio_ir_timeout));
  73. err_get_value:
  74. return IRQ_HANDLED;
  75. }
  76. static void gpio_ir_timer(unsigned long data)
  77. {
  78. struct gpio_rc_dev *gpio_dev = (struct gpio_rc_dev *)data;
  79. pm_qos_update_request(&gpio_dev->pm_qos_req, PM_QOS_DEFAULT_VALUE);
  80. pm_qos_request_active(&gpio_dev->pm_qos_req);
  81. gpio_dev->pm_qos_vote = 0;
  82. }
  83. static int __devinit gpio_ir_recv_probe(struct platform_device *pdev)
  84. {
  85. struct gpio_rc_dev *gpio_dev;
  86. struct rc_dev *rcdev;
  87. const struct gpio_ir_recv_platform_data *pdata =
  88. pdev->dev.platform_data;
  89. int rc;
  90. if (!pdata)
  91. return -EINVAL;
  92. if (pdata->gpio_nr < 0)
  93. return -EINVAL;
  94. gpio_dev = kzalloc(sizeof(struct gpio_rc_dev), GFP_KERNEL);
  95. if (!gpio_dev)
  96. return -ENOMEM;
  97. rcdev = rc_allocate_device();
  98. if (!rcdev) {
  99. rc = -ENOMEM;
  100. goto err_allocate_device;
  101. }
  102. rcdev->driver_type = RC_DRIVER_IR_RAW;
  103. rcdev->allowed_protos = RC_TYPE_ALL;
  104. rcdev->input_name = GPIO_IR_DEVICE_NAME;
  105. rcdev->input_id.bustype = BUS_HOST;
  106. rcdev->driver_name = GPIO_IR_DRIVER_NAME;
  107. rcdev->map_name = RC_MAP_SAMSUNG_NECX;
  108. gpio_dev->rcdev = rcdev;
  109. gpio_dev->gpio_nr = pdata->gpio_nr;
  110. gpio_dev->active_low = pdata->active_low;
  111. gpio_dev->can_wakeup = pdata->can_wakeup;
  112. gpio_dev->gpio_irq_latency = pdata->swfi_latency + 1;
  113. gpio_dev->pm_qos_vote = 0;
  114. rc = gpio_request(pdata->gpio_nr, "gpio-ir-recv");
  115. if (rc < 0)
  116. goto err_gpio_request;
  117. gpio_dev->can_sleep = gpio_cansleep(pdata->gpio_nr);
  118. rc = gpio_direction_input(pdata->gpio_nr);
  119. if (rc < 0)
  120. goto err_gpio_direction_input;
  121. rc = rc_register_device(rcdev);
  122. if (rc < 0) {
  123. dev_err(&pdev->dev, "failed to register rc device\n");
  124. goto err_register_rc_device;
  125. }
  126. platform_set_drvdata(pdev, gpio_dev);
  127. rc = request_any_context_irq(gpio_to_irq(pdata->gpio_nr),
  128. gpio_ir_recv_irq,
  129. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  130. "gpio-ir-recv-irq", gpio_dev);
  131. if (rc < 0)
  132. goto err_request_irq;
  133. if (gpio_dev->can_wakeup) {
  134. pm_qos_add_request(&gpio_dev->pm_qos_req,
  135. PM_QOS_CPU_DMA_LATENCY,
  136. PM_QOS_DEFAULT_VALUE);
  137. device_init_wakeup(&pdev->dev, pdata->can_wakeup);
  138. setup_timer(&gpio_dev->gpio_ir_timer, gpio_ir_timer,
  139. (unsigned long)gpio_dev);
  140. }
  141. return 0;
  142. err_request_irq:
  143. platform_set_drvdata(pdev, NULL);
  144. rc_unregister_device(rcdev);
  145. err_register_rc_device:
  146. err_gpio_direction_input:
  147. gpio_free(pdata->gpio_nr);
  148. err_gpio_request:
  149. rc_free_device(rcdev);
  150. rcdev = NULL;
  151. err_allocate_device:
  152. kfree(gpio_dev);
  153. return rc;
  154. }
  155. static int __devexit gpio_ir_recv_remove(struct platform_device *pdev)
  156. {
  157. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  158. if (gpio_dev->can_wakeup) {
  159. del_timer_sync(&gpio_dev->gpio_ir_timer);
  160. pm_qos_remove_request(&gpio_dev->pm_qos_req);
  161. }
  162. free_irq(gpio_to_irq(gpio_dev->gpio_nr), gpio_dev);
  163. platform_set_drvdata(pdev, NULL);
  164. rc_unregister_device(gpio_dev->rcdev);
  165. gpio_free(gpio_dev->gpio_nr);
  166. rc_free_device(gpio_dev->rcdev);
  167. kfree(gpio_dev);
  168. return 0;
  169. }
  170. #ifdef CONFIG_PM
  171. static int gpio_ir_recv_suspend(struct device *dev)
  172. {
  173. struct platform_device *pdev = to_platform_device(dev);
  174. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  175. if (device_may_wakeup(dev))
  176. enable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
  177. else
  178. disable_irq(gpio_to_irq(gpio_dev->gpio_nr));
  179. return 0;
  180. }
  181. static int gpio_ir_recv_resume(struct device *dev)
  182. {
  183. struct platform_device *pdev = to_platform_device(dev);
  184. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  185. if (device_may_wakeup(dev))
  186. disable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
  187. else
  188. enable_irq(gpio_to_irq(gpio_dev->gpio_nr));
  189. return 0;
  190. }
  191. static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
  192. .suspend = gpio_ir_recv_suspend,
  193. .resume = gpio_ir_recv_resume,
  194. };
  195. #endif
  196. static struct platform_driver gpio_ir_recv_driver = {
  197. .probe = gpio_ir_recv_probe,
  198. .remove = __devexit_p(gpio_ir_recv_remove),
  199. .driver = {
  200. .name = GPIO_IR_DRIVER_NAME,
  201. .owner = THIS_MODULE,
  202. #ifdef CONFIG_PM
  203. .pm = &gpio_ir_recv_pm_ops,
  204. #endif
  205. },
  206. };
  207. static int __init gpio_ir_recv_init(void)
  208. {
  209. return platform_driver_register(&gpio_ir_recv_driver);
  210. }
  211. module_init(gpio_ir_recv_init);
  212. static void __exit gpio_ir_recv_exit(void)
  213. {
  214. platform_driver_unregister(&gpio_ir_recv_driver);
  215. }
  216. module_exit(gpio_ir_recv_exit);
  217. MODULE_DESCRIPTION("GPIO IR Receiver driver");
  218. MODULE_LICENSE("GPL v2");