cec-gpio.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2017 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/delay.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <media/cec-pin.h>
  11. struct cec_gpio {
  12. struct cec_adapter *adap;
  13. struct device *dev;
  14. struct gpio_desc *cec_gpio;
  15. int cec_irq;
  16. bool cec_is_low;
  17. bool cec_have_irq;
  18. struct gpio_desc *hpd_gpio;
  19. int hpd_irq;
  20. bool hpd_is_high;
  21. ktime_t hpd_ts;
  22. };
  23. static bool cec_gpio_read(struct cec_adapter *adap)
  24. {
  25. struct cec_gpio *cec = cec_get_drvdata(adap);
  26. if (cec->cec_is_low)
  27. return false;
  28. return gpiod_get_value(cec->cec_gpio);
  29. }
  30. static void cec_gpio_high(struct cec_adapter *adap)
  31. {
  32. struct cec_gpio *cec = cec_get_drvdata(adap);
  33. if (!cec->cec_is_low)
  34. return;
  35. cec->cec_is_low = false;
  36. gpiod_set_value(cec->cec_gpio, 1);
  37. }
  38. static void cec_gpio_low(struct cec_adapter *adap)
  39. {
  40. struct cec_gpio *cec = cec_get_drvdata(adap);
  41. if (cec->cec_is_low)
  42. return;
  43. if (WARN_ON_ONCE(cec->cec_have_irq))
  44. free_irq(cec->cec_irq, cec);
  45. cec->cec_have_irq = false;
  46. cec->cec_is_low = true;
  47. gpiod_set_value(cec->cec_gpio, 0);
  48. }
  49. static irqreturn_t cec_hpd_gpio_irq_handler_thread(int irq, void *priv)
  50. {
  51. struct cec_gpio *cec = priv;
  52. cec_queue_pin_hpd_event(cec->adap, cec->hpd_is_high, cec->hpd_ts);
  53. return IRQ_HANDLED;
  54. }
  55. static irqreturn_t cec_hpd_gpio_irq_handler(int irq, void *priv)
  56. {
  57. struct cec_gpio *cec = priv;
  58. bool is_high = gpiod_get_value(cec->hpd_gpio);
  59. if (is_high == cec->hpd_is_high)
  60. return IRQ_HANDLED;
  61. cec->hpd_ts = ktime_get();
  62. cec->hpd_is_high = is_high;
  63. return IRQ_WAKE_THREAD;
  64. }
  65. static irqreturn_t cec_gpio_irq_handler(int irq, void *priv)
  66. {
  67. struct cec_gpio *cec = priv;
  68. cec_pin_changed(cec->adap, gpiod_get_value(cec->cec_gpio));
  69. return IRQ_HANDLED;
  70. }
  71. static bool cec_gpio_enable_irq(struct cec_adapter *adap)
  72. {
  73. struct cec_gpio *cec = cec_get_drvdata(adap);
  74. if (cec->cec_have_irq)
  75. return true;
  76. if (request_irq(cec->cec_irq, cec_gpio_irq_handler,
  77. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  78. adap->name, cec))
  79. return false;
  80. cec->cec_have_irq = true;
  81. return true;
  82. }
  83. static void cec_gpio_disable_irq(struct cec_adapter *adap)
  84. {
  85. struct cec_gpio *cec = cec_get_drvdata(adap);
  86. if (cec->cec_have_irq)
  87. free_irq(cec->cec_irq, cec);
  88. cec->cec_have_irq = false;
  89. }
  90. static void cec_gpio_status(struct cec_adapter *adap, struct seq_file *file)
  91. {
  92. struct cec_gpio *cec = cec_get_drvdata(adap);
  93. seq_printf(file, "mode: %s\n", cec->cec_is_low ? "low-drive" : "read");
  94. if (cec->cec_have_irq)
  95. seq_printf(file, "using irq: %d\n", cec->cec_irq);
  96. if (cec->hpd_gpio)
  97. seq_printf(file, "hpd: %s\n",
  98. cec->hpd_is_high ? "high" : "low");
  99. }
  100. static int cec_gpio_read_hpd(struct cec_adapter *adap)
  101. {
  102. struct cec_gpio *cec = cec_get_drvdata(adap);
  103. if (!cec->hpd_gpio)
  104. return -ENOTTY;
  105. return gpiod_get_value(cec->hpd_gpio);
  106. }
  107. static void cec_gpio_free(struct cec_adapter *adap)
  108. {
  109. cec_gpio_disable_irq(adap);
  110. }
  111. static const struct cec_pin_ops cec_gpio_pin_ops = {
  112. .read = cec_gpio_read,
  113. .low = cec_gpio_low,
  114. .high = cec_gpio_high,
  115. .enable_irq = cec_gpio_enable_irq,
  116. .disable_irq = cec_gpio_disable_irq,
  117. .status = cec_gpio_status,
  118. .free = cec_gpio_free,
  119. .read_hpd = cec_gpio_read_hpd,
  120. };
  121. static int cec_gpio_probe(struct platform_device *pdev)
  122. {
  123. struct device *dev = &pdev->dev;
  124. struct cec_gpio *cec;
  125. int ret;
  126. cec = devm_kzalloc(dev, sizeof(*cec), GFP_KERNEL);
  127. if (!cec)
  128. return -ENOMEM;
  129. cec->dev = dev;
  130. cec->cec_gpio = devm_gpiod_get(dev, "cec", GPIOD_IN);
  131. if (IS_ERR(cec->cec_gpio))
  132. return PTR_ERR(cec->cec_gpio);
  133. cec->cec_irq = gpiod_to_irq(cec->cec_gpio);
  134. cec->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
  135. if (IS_ERR(cec->hpd_gpio))
  136. return PTR_ERR(cec->hpd_gpio);
  137. cec->adap = cec_pin_allocate_adapter(&cec_gpio_pin_ops,
  138. cec, pdev->name, CEC_CAP_DEFAULTS | CEC_CAP_PHYS_ADDR |
  139. CEC_CAP_MONITOR_ALL | CEC_CAP_MONITOR_PIN);
  140. if (IS_ERR(cec->adap))
  141. return PTR_ERR(cec->adap);
  142. if (cec->hpd_gpio) {
  143. cec->hpd_irq = gpiod_to_irq(cec->hpd_gpio);
  144. ret = devm_request_threaded_irq(dev, cec->hpd_irq,
  145. cec_hpd_gpio_irq_handler,
  146. cec_hpd_gpio_irq_handler_thread,
  147. IRQF_ONESHOT |
  148. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  149. "hpd-gpio", cec);
  150. if (ret)
  151. return ret;
  152. }
  153. ret = cec_register_adapter(cec->adap, &pdev->dev);
  154. if (ret) {
  155. cec_delete_adapter(cec->adap);
  156. return ret;
  157. }
  158. platform_set_drvdata(pdev, cec);
  159. return 0;
  160. }
  161. static int cec_gpio_remove(struct platform_device *pdev)
  162. {
  163. struct cec_gpio *cec = platform_get_drvdata(pdev);
  164. cec_unregister_adapter(cec->adap);
  165. return 0;
  166. }
  167. static const struct of_device_id cec_gpio_match[] = {
  168. {
  169. .compatible = "cec-gpio",
  170. },
  171. {},
  172. };
  173. MODULE_DEVICE_TABLE(of, cec_gpio_match);
  174. static struct platform_driver cec_gpio_pdrv = {
  175. .probe = cec_gpio_probe,
  176. .remove = cec_gpio_remove,
  177. .driver = {
  178. .name = "cec-gpio",
  179. .of_match_table = cec_gpio_match,
  180. },
  181. };
  182. module_platform_driver(cec_gpio_pdrv);
  183. MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
  184. MODULE_LICENSE("GPL v2");
  185. MODULE_DESCRIPTION("CEC GPIO driver");