gpio-ir-recv.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* Copyright (c) 2012, Code Aurora Forum. 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/of.h>
  19. #include <linux/of_gpio.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/irq.h>
  22. #include <media/rc-core.h>
  23. #include <linux/platform_data/media/gpio-ir-recv.h>
  24. #define GPIO_IR_DRIVER_NAME "gpio-rc-recv"
  25. #define GPIO_IR_DEVICE_NAME "gpio_ir_recv"
  26. struct gpio_rc_dev {
  27. struct rc_dev *rcdev;
  28. int gpio_nr;
  29. bool active_low;
  30. struct timer_list flush_timer;
  31. };
  32. #ifdef CONFIG_OF
  33. /*
  34. * Translate OpenFirmware node properties into platform_data
  35. */
  36. static int gpio_ir_recv_get_devtree_pdata(struct device *dev,
  37. struct gpio_ir_recv_platform_data *pdata)
  38. {
  39. struct device_node *np = dev->of_node;
  40. enum of_gpio_flags flags;
  41. int gpio;
  42. gpio = of_get_gpio_flags(np, 0, &flags);
  43. if (gpio < 0) {
  44. if (gpio != -EPROBE_DEFER)
  45. dev_err(dev, "Failed to get gpio flags (%d)\n", gpio);
  46. return gpio;
  47. }
  48. pdata->gpio_nr = gpio;
  49. pdata->active_low = (flags & OF_GPIO_ACTIVE_LOW);
  50. /* probe() takes care of map_name == NULL or allowed_protos == 0 */
  51. pdata->map_name = of_get_property(np, "linux,rc-map-name", NULL);
  52. pdata->allowed_protos = 0;
  53. return 0;
  54. }
  55. static const struct of_device_id gpio_ir_recv_of_match[] = {
  56. { .compatible = "gpio-ir-receiver", },
  57. { },
  58. };
  59. MODULE_DEVICE_TABLE(of, gpio_ir_recv_of_match);
  60. #else /* !CONFIG_OF */
  61. #define gpio_ir_recv_get_devtree_pdata(dev, pdata) (-ENOSYS)
  62. #endif
  63. static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id)
  64. {
  65. struct gpio_rc_dev *gpio_dev = dev_id;
  66. int gval;
  67. int rc = 0;
  68. enum raw_event_type type = IR_SPACE;
  69. gval = gpio_get_value(gpio_dev->gpio_nr);
  70. if (gval < 0)
  71. goto err_get_value;
  72. if (gpio_dev->active_low)
  73. gval = !gval;
  74. if (gval == 1)
  75. type = IR_PULSE;
  76. rc = ir_raw_event_store_edge(gpio_dev->rcdev, type);
  77. if (rc < 0)
  78. goto err_get_value;
  79. mod_timer(&gpio_dev->flush_timer,
  80. jiffies + nsecs_to_jiffies(gpio_dev->rcdev->timeout));
  81. ir_raw_event_handle(gpio_dev->rcdev);
  82. err_get_value:
  83. return IRQ_HANDLED;
  84. }
  85. static void flush_timer(unsigned long arg)
  86. {
  87. struct gpio_rc_dev *gpio_dev = (struct gpio_rc_dev *)arg;
  88. DEFINE_IR_RAW_EVENT(ev);
  89. ev.timeout = true;
  90. ev.duration = gpio_dev->rcdev->timeout;
  91. ir_raw_event_store(gpio_dev->rcdev, &ev);
  92. ir_raw_event_handle(gpio_dev->rcdev);
  93. }
  94. static int gpio_ir_recv_probe(struct platform_device *pdev)
  95. {
  96. struct gpio_rc_dev *gpio_dev;
  97. struct rc_dev *rcdev;
  98. const struct gpio_ir_recv_platform_data *pdata =
  99. pdev->dev.platform_data;
  100. int rc;
  101. if (pdev->dev.of_node) {
  102. struct gpio_ir_recv_platform_data *dtpdata =
  103. devm_kzalloc(&pdev->dev, sizeof(*dtpdata), GFP_KERNEL);
  104. if (!dtpdata)
  105. return -ENOMEM;
  106. rc = gpio_ir_recv_get_devtree_pdata(&pdev->dev, dtpdata);
  107. if (rc)
  108. return rc;
  109. pdata = dtpdata;
  110. }
  111. if (!pdata)
  112. return -EINVAL;
  113. if (pdata->gpio_nr < 0)
  114. return -EINVAL;
  115. gpio_dev = kzalloc(sizeof(struct gpio_rc_dev), GFP_KERNEL);
  116. if (!gpio_dev)
  117. return -ENOMEM;
  118. rcdev = rc_allocate_device(RC_DRIVER_IR_RAW);
  119. if (!rcdev) {
  120. rc = -ENOMEM;
  121. goto err_allocate_device;
  122. }
  123. rcdev->priv = gpio_dev;
  124. rcdev->input_name = GPIO_IR_DEVICE_NAME;
  125. rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0";
  126. rcdev->input_id.bustype = BUS_HOST;
  127. rcdev->input_id.vendor = 0x0001;
  128. rcdev->input_id.product = 0x0001;
  129. rcdev->input_id.version = 0x0100;
  130. rcdev->dev.parent = &pdev->dev;
  131. rcdev->driver_name = GPIO_IR_DRIVER_NAME;
  132. rcdev->min_timeout = 1;
  133. rcdev->timeout = IR_DEFAULT_TIMEOUT;
  134. rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
  135. if (pdata->allowed_protos)
  136. rcdev->allowed_protocols = pdata->allowed_protos;
  137. else
  138. rcdev->allowed_protocols = RC_BIT_ALL_IR_DECODER;
  139. rcdev->map_name = pdata->map_name ?: RC_MAP_EMPTY;
  140. gpio_dev->rcdev = rcdev;
  141. gpio_dev->gpio_nr = pdata->gpio_nr;
  142. gpio_dev->active_low = pdata->active_low;
  143. setup_timer(&gpio_dev->flush_timer, flush_timer,
  144. (unsigned long)gpio_dev);
  145. rc = gpio_request(pdata->gpio_nr, "gpio-ir-recv");
  146. if (rc < 0)
  147. goto err_gpio_request;
  148. rc = gpio_direction_input(pdata->gpio_nr);
  149. if (rc < 0)
  150. goto err_gpio_direction_input;
  151. rc = rc_register_device(rcdev);
  152. if (rc < 0) {
  153. dev_err(&pdev->dev, "failed to register rc device\n");
  154. goto err_register_rc_device;
  155. }
  156. platform_set_drvdata(pdev, gpio_dev);
  157. rc = request_any_context_irq(gpio_to_irq(pdata->gpio_nr),
  158. gpio_ir_recv_irq,
  159. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  160. "gpio-ir-recv-irq", gpio_dev);
  161. if (rc < 0)
  162. goto err_request_irq;
  163. return 0;
  164. err_request_irq:
  165. rc_unregister_device(rcdev);
  166. rcdev = NULL;
  167. err_register_rc_device:
  168. err_gpio_direction_input:
  169. gpio_free(pdata->gpio_nr);
  170. err_gpio_request:
  171. rc_free_device(rcdev);
  172. err_allocate_device:
  173. kfree(gpio_dev);
  174. return rc;
  175. }
  176. static int gpio_ir_recv_remove(struct platform_device *pdev)
  177. {
  178. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  179. free_irq(gpio_to_irq(gpio_dev->gpio_nr), gpio_dev);
  180. del_timer_sync(&gpio_dev->flush_timer);
  181. rc_unregister_device(gpio_dev->rcdev);
  182. gpio_free(gpio_dev->gpio_nr);
  183. kfree(gpio_dev);
  184. return 0;
  185. }
  186. #ifdef CONFIG_PM
  187. static int gpio_ir_recv_suspend(struct device *dev)
  188. {
  189. struct platform_device *pdev = to_platform_device(dev);
  190. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  191. if (device_may_wakeup(dev))
  192. enable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
  193. else
  194. disable_irq(gpio_to_irq(gpio_dev->gpio_nr));
  195. return 0;
  196. }
  197. static int gpio_ir_recv_resume(struct device *dev)
  198. {
  199. struct platform_device *pdev = to_platform_device(dev);
  200. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  201. if (device_may_wakeup(dev))
  202. disable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
  203. else
  204. enable_irq(gpio_to_irq(gpio_dev->gpio_nr));
  205. return 0;
  206. }
  207. static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
  208. .suspend = gpio_ir_recv_suspend,
  209. .resume = gpio_ir_recv_resume,
  210. };
  211. #endif
  212. static struct platform_driver gpio_ir_recv_driver = {
  213. .probe = gpio_ir_recv_probe,
  214. .remove = gpio_ir_recv_remove,
  215. .driver = {
  216. .name = GPIO_IR_DRIVER_NAME,
  217. .of_match_table = of_match_ptr(gpio_ir_recv_of_match),
  218. #ifdef CONFIG_PM
  219. .pm = &gpio_ir_recv_pm_ops,
  220. #endif
  221. },
  222. };
  223. module_platform_driver(gpio_ir_recv_driver);
  224. MODULE_DESCRIPTION("GPIO IR Receiver driver");
  225. MODULE_LICENSE("GPL v2");