gpio-ir-recv.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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();
  119. if (!rcdev) {
  120. rc = -ENOMEM;
  121. goto err_allocate_device;
  122. }
  123. rcdev->priv = gpio_dev;
  124. rcdev->driver_type = RC_DRIVER_IR_RAW;
  125. rcdev->input_name = GPIO_IR_DEVICE_NAME;
  126. rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0";
  127. rcdev->input_id.bustype = BUS_HOST;
  128. rcdev->input_id.vendor = 0x0001;
  129. rcdev->input_id.product = 0x0001;
  130. rcdev->input_id.version = 0x0100;
  131. rcdev->dev.parent = &pdev->dev;
  132. rcdev->driver_name = GPIO_IR_DRIVER_NAME;
  133. rcdev->min_timeout = 0;
  134. rcdev->timeout = IR_DEFAULT_TIMEOUT;
  135. rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
  136. if (pdata->allowed_protos)
  137. rcdev->allowed_protocols = pdata->allowed_protos;
  138. else
  139. rcdev->allowed_protocols = RC_BIT_ALL;
  140. rcdev->map_name = pdata->map_name ?: RC_MAP_EMPTY;
  141. gpio_dev->rcdev = rcdev;
  142. gpio_dev->gpio_nr = pdata->gpio_nr;
  143. gpio_dev->active_low = pdata->active_low;
  144. setup_timer(&gpio_dev->flush_timer, flush_timer,
  145. (unsigned long)gpio_dev);
  146. rc = gpio_request(pdata->gpio_nr, "gpio-ir-recv");
  147. if (rc < 0)
  148. goto err_gpio_request;
  149. rc = gpio_direction_input(pdata->gpio_nr);
  150. if (rc < 0)
  151. goto err_gpio_direction_input;
  152. rc = rc_register_device(rcdev);
  153. if (rc < 0) {
  154. dev_err(&pdev->dev, "failed to register rc device\n");
  155. goto err_register_rc_device;
  156. }
  157. platform_set_drvdata(pdev, gpio_dev);
  158. rc = request_any_context_irq(gpio_to_irq(pdata->gpio_nr),
  159. gpio_ir_recv_irq,
  160. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  161. "gpio-ir-recv-irq", gpio_dev);
  162. if (rc < 0)
  163. goto err_request_irq;
  164. return 0;
  165. err_request_irq:
  166. rc_unregister_device(rcdev);
  167. rcdev = NULL;
  168. err_register_rc_device:
  169. err_gpio_direction_input:
  170. gpio_free(pdata->gpio_nr);
  171. err_gpio_request:
  172. rc_free_device(rcdev);
  173. err_allocate_device:
  174. kfree(gpio_dev);
  175. return rc;
  176. }
  177. static int gpio_ir_recv_remove(struct platform_device *pdev)
  178. {
  179. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  180. free_irq(gpio_to_irq(gpio_dev->gpio_nr), gpio_dev);
  181. del_timer_sync(&gpio_dev->flush_timer);
  182. rc_unregister_device(gpio_dev->rcdev);
  183. gpio_free(gpio_dev->gpio_nr);
  184. kfree(gpio_dev);
  185. return 0;
  186. }
  187. #ifdef CONFIG_PM
  188. static int gpio_ir_recv_suspend(struct device *dev)
  189. {
  190. struct platform_device *pdev = to_platform_device(dev);
  191. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  192. if (device_may_wakeup(dev))
  193. enable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
  194. else
  195. disable_irq(gpio_to_irq(gpio_dev->gpio_nr));
  196. return 0;
  197. }
  198. static int gpio_ir_recv_resume(struct device *dev)
  199. {
  200. struct platform_device *pdev = to_platform_device(dev);
  201. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  202. if (device_may_wakeup(dev))
  203. disable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
  204. else
  205. enable_irq(gpio_to_irq(gpio_dev->gpio_nr));
  206. return 0;
  207. }
  208. static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
  209. .suspend = gpio_ir_recv_suspend,
  210. .resume = gpio_ir_recv_resume,
  211. };
  212. #endif
  213. static struct platform_driver gpio_ir_recv_driver = {
  214. .probe = gpio_ir_recv_probe,
  215. .remove = gpio_ir_recv_remove,
  216. .driver = {
  217. .name = GPIO_IR_DRIVER_NAME,
  218. .of_match_table = of_match_ptr(gpio_ir_recv_of_match),
  219. #ifdef CONFIG_PM
  220. .pm = &gpio_ir_recv_pm_ops,
  221. #endif
  222. },
  223. };
  224. module_platform_driver(gpio_ir_recv_driver);
  225. MODULE_DESCRIPTION("GPIO IR Receiver driver");
  226. MODULE_LICENSE("GPL v2");