gpio-ir-recv.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/init.h>
  6. #include <linux/module.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/slab.h>
  10. #include <linux/of.h>
  11. #include <linux/of_gpio.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/pm_qos.h>
  15. #include <linux/irq.h>
  16. #include <media/rc-core.h>
  17. #define GPIO_IR_DEVICE_NAME "gpio_ir_recv"
  18. struct gpio_rc_dev {
  19. struct rc_dev *rcdev;
  20. struct gpio_desc *gpiod;
  21. int irq;
  22. struct device *pmdev;
  23. struct pm_qos_request qos;
  24. };
  25. static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id)
  26. {
  27. int val;
  28. struct gpio_rc_dev *gpio_dev = dev_id;
  29. struct device *pmdev = gpio_dev->pmdev;
  30. /*
  31. * For some cpuidle systems, not all:
  32. * Respond to interrupt taking more latency when cpu in idle.
  33. * Invoke asynchronous pm runtime get from interrupt context,
  34. * this may introduce a millisecond delay to call resume callback,
  35. * where to disable cpuilde.
  36. *
  37. * Two issues lead to fail to decode first frame, one is latency to
  38. * respond to interrupt, another is delay introduced by async api.
  39. */
  40. if (pmdev)
  41. pm_runtime_get(pmdev);
  42. val = gpiod_get_value(gpio_dev->gpiod);
  43. if (val >= 0)
  44. ir_raw_event_store_edge(gpio_dev->rcdev, val == 1);
  45. if (pmdev) {
  46. pm_runtime_mark_last_busy(pmdev);
  47. pm_runtime_put_autosuspend(pmdev);
  48. }
  49. return IRQ_HANDLED;
  50. }
  51. static int gpio_ir_recv_probe(struct platform_device *pdev)
  52. {
  53. struct device *dev = &pdev->dev;
  54. struct device_node *np = dev->of_node;
  55. struct gpio_rc_dev *gpio_dev;
  56. struct rc_dev *rcdev;
  57. u32 period = 0;
  58. int rc;
  59. if (!np)
  60. return -ENODEV;
  61. gpio_dev = devm_kzalloc(dev, sizeof(*gpio_dev), GFP_KERNEL);
  62. if (!gpio_dev)
  63. return -ENOMEM;
  64. gpio_dev->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
  65. if (IS_ERR(gpio_dev->gpiod)) {
  66. rc = PTR_ERR(gpio_dev->gpiod);
  67. /* Just try again if this happens */
  68. if (rc != -EPROBE_DEFER)
  69. dev_err(dev, "error getting gpio (%d)\n", rc);
  70. return rc;
  71. }
  72. gpio_dev->irq = gpiod_to_irq(gpio_dev->gpiod);
  73. if (gpio_dev->irq < 0)
  74. return gpio_dev->irq;
  75. rcdev = devm_rc_allocate_device(dev, RC_DRIVER_IR_RAW);
  76. if (!rcdev)
  77. return -ENOMEM;
  78. rcdev->priv = gpio_dev;
  79. rcdev->device_name = GPIO_IR_DEVICE_NAME;
  80. rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0";
  81. rcdev->input_id.bustype = BUS_HOST;
  82. rcdev->input_id.vendor = 0x0001;
  83. rcdev->input_id.product = 0x0001;
  84. rcdev->input_id.version = 0x0100;
  85. rcdev->dev.parent = dev;
  86. rcdev->driver_name = KBUILD_MODNAME;
  87. rcdev->min_timeout = 1;
  88. rcdev->timeout = IR_DEFAULT_TIMEOUT;
  89. rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
  90. rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
  91. rcdev->map_name = of_get_property(np, "linux,rc-map-name", NULL);
  92. if (!rcdev->map_name)
  93. rcdev->map_name = RC_MAP_EMPTY;
  94. gpio_dev->rcdev = rcdev;
  95. if (of_property_read_bool(np, "wakeup-source"))
  96. device_init_wakeup(dev, true);
  97. rc = devm_rc_register_device(dev, rcdev);
  98. if (rc < 0) {
  99. dev_err(dev, "failed to register rc device (%d)\n", rc);
  100. return rc;
  101. }
  102. of_property_read_u32(np, "linux,autosuspend-period", &period);
  103. if (period) {
  104. gpio_dev->pmdev = dev;
  105. pm_runtime_set_autosuspend_delay(dev, period);
  106. pm_runtime_use_autosuspend(dev);
  107. pm_runtime_set_suspended(dev);
  108. pm_runtime_enable(dev);
  109. }
  110. platform_set_drvdata(pdev, gpio_dev);
  111. return devm_request_irq(dev, gpio_dev->irq, gpio_ir_recv_irq,
  112. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  113. "gpio-ir-recv-irq", gpio_dev);
  114. }
  115. static int gpio_ir_recv_remove(struct platform_device *pdev)
  116. {
  117. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  118. struct device *pmdev = gpio_dev->pmdev;
  119. if (pmdev) {
  120. pm_runtime_get_sync(pmdev);
  121. cpu_latency_qos_remove_request(&gpio_dev->qos);
  122. pm_runtime_disable(pmdev);
  123. pm_runtime_put_noidle(pmdev);
  124. pm_runtime_set_suspended(pmdev);
  125. }
  126. return 0;
  127. }
  128. #ifdef CONFIG_PM
  129. static int gpio_ir_recv_suspend(struct device *dev)
  130. {
  131. struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
  132. if (device_may_wakeup(dev))
  133. enable_irq_wake(gpio_dev->irq);
  134. else
  135. disable_irq(gpio_dev->irq);
  136. return 0;
  137. }
  138. static int gpio_ir_recv_resume(struct device *dev)
  139. {
  140. struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
  141. if (device_may_wakeup(dev))
  142. disable_irq_wake(gpio_dev->irq);
  143. else
  144. enable_irq(gpio_dev->irq);
  145. return 0;
  146. }
  147. static int gpio_ir_recv_runtime_suspend(struct device *dev)
  148. {
  149. struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
  150. cpu_latency_qos_remove_request(&gpio_dev->qos);
  151. return 0;
  152. }
  153. static int gpio_ir_recv_runtime_resume(struct device *dev)
  154. {
  155. struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
  156. cpu_latency_qos_add_request(&gpio_dev->qos, 0);
  157. return 0;
  158. }
  159. static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
  160. .suspend = gpio_ir_recv_suspend,
  161. .resume = gpio_ir_recv_resume,
  162. .runtime_suspend = gpio_ir_recv_runtime_suspend,
  163. .runtime_resume = gpio_ir_recv_runtime_resume,
  164. };
  165. #endif
  166. static const struct of_device_id gpio_ir_recv_of_match[] = {
  167. { .compatible = "gpio-ir-receiver", },
  168. { },
  169. };
  170. MODULE_DEVICE_TABLE(of, gpio_ir_recv_of_match);
  171. static struct platform_driver gpio_ir_recv_driver = {
  172. .probe = gpio_ir_recv_probe,
  173. .remove = gpio_ir_recv_remove,
  174. .driver = {
  175. .name = KBUILD_MODNAME,
  176. .of_match_table = of_match_ptr(gpio_ir_recv_of_match),
  177. #ifdef CONFIG_PM
  178. .pm = &gpio_ir_recv_pm_ops,
  179. #endif
  180. },
  181. };
  182. module_platform_driver(gpio_ir_recv_driver);
  183. MODULE_DESCRIPTION("GPIO IR Receiver driver");
  184. MODULE_LICENSE("GPL v2");