gpio-ir-recv.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. rc = devm_rc_register_device(dev, rcdev);
  96. if (rc < 0) {
  97. dev_err(dev, "failed to register rc device (%d)\n", rc);
  98. return rc;
  99. }
  100. of_property_read_u32(np, "linux,autosuspend-period", &period);
  101. if (period) {
  102. gpio_dev->pmdev = dev;
  103. pm_runtime_set_autosuspend_delay(dev, period);
  104. pm_runtime_use_autosuspend(dev);
  105. pm_runtime_set_suspended(dev);
  106. pm_runtime_enable(dev);
  107. }
  108. platform_set_drvdata(pdev, gpio_dev);
  109. return devm_request_irq(dev, gpio_dev->irq, gpio_ir_recv_irq,
  110. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  111. "gpio-ir-recv-irq", gpio_dev);
  112. }
  113. #ifdef CONFIG_PM
  114. static int gpio_ir_recv_suspend(struct device *dev)
  115. {
  116. struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
  117. if (device_may_wakeup(dev))
  118. enable_irq_wake(gpio_dev->irq);
  119. else
  120. disable_irq(gpio_dev->irq);
  121. return 0;
  122. }
  123. static int gpio_ir_recv_resume(struct device *dev)
  124. {
  125. struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
  126. if (device_may_wakeup(dev))
  127. disable_irq_wake(gpio_dev->irq);
  128. else
  129. enable_irq(gpio_dev->irq);
  130. return 0;
  131. }
  132. static int gpio_ir_recv_runtime_suspend(struct device *dev)
  133. {
  134. struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
  135. cpu_latency_qos_remove_request(&gpio_dev->qos);
  136. return 0;
  137. }
  138. static int gpio_ir_recv_runtime_resume(struct device *dev)
  139. {
  140. struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
  141. cpu_latency_qos_add_request(&gpio_dev->qos, 0);
  142. return 0;
  143. }
  144. static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
  145. .suspend = gpio_ir_recv_suspend,
  146. .resume = gpio_ir_recv_resume,
  147. .runtime_suspend = gpio_ir_recv_runtime_suspend,
  148. .runtime_resume = gpio_ir_recv_runtime_resume,
  149. };
  150. #endif
  151. static const struct of_device_id gpio_ir_recv_of_match[] = {
  152. { .compatible = "gpio-ir-receiver", },
  153. { },
  154. };
  155. MODULE_DEVICE_TABLE(of, gpio_ir_recv_of_match);
  156. static struct platform_driver gpio_ir_recv_driver = {
  157. .probe = gpio_ir_recv_probe,
  158. .driver = {
  159. .name = KBUILD_MODNAME,
  160. .of_match_table = of_match_ptr(gpio_ir_recv_of_match),
  161. #ifdef CONFIG_PM
  162. .pm = &gpio_ir_recv_pm_ops,
  163. #endif
  164. },
  165. };
  166. module_platform_driver(gpio_ir_recv_driver);
  167. MODULE_DESCRIPTION("GPIO IR Receiver driver");
  168. MODULE_LICENSE("GPL v2");