cec-gpio.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. struct gpio_desc *v5_gpio;
  23. int v5_irq;
  24. bool v5_is_high;
  25. ktime_t v5_ts;
  26. };
  27. static bool cec_gpio_read(struct cec_adapter *adap)
  28. {
  29. struct cec_gpio *cec = cec_get_drvdata(adap);
  30. if (cec->cec_is_low)
  31. return false;
  32. return gpiod_get_value(cec->cec_gpio);
  33. }
  34. static void cec_gpio_high(struct cec_adapter *adap)
  35. {
  36. struct cec_gpio *cec = cec_get_drvdata(adap);
  37. if (!cec->cec_is_low)
  38. return;
  39. cec->cec_is_low = false;
  40. gpiod_set_value(cec->cec_gpio, 1);
  41. }
  42. static void cec_gpio_low(struct cec_adapter *adap)
  43. {
  44. struct cec_gpio *cec = cec_get_drvdata(adap);
  45. if (cec->cec_is_low)
  46. return;
  47. if (WARN_ON_ONCE(cec->cec_have_irq))
  48. free_irq(cec->cec_irq, cec);
  49. cec->cec_have_irq = false;
  50. cec->cec_is_low = true;
  51. gpiod_set_value(cec->cec_gpio, 0);
  52. }
  53. static irqreturn_t cec_hpd_gpio_irq_handler_thread(int irq, void *priv)
  54. {
  55. struct cec_gpio *cec = priv;
  56. cec_queue_pin_hpd_event(cec->adap, cec->hpd_is_high, cec->hpd_ts);
  57. return IRQ_HANDLED;
  58. }
  59. static irqreturn_t cec_5v_gpio_irq_handler(int irq, void *priv)
  60. {
  61. struct cec_gpio *cec = priv;
  62. bool is_high = gpiod_get_value(cec->v5_gpio);
  63. if (is_high == cec->v5_is_high)
  64. return IRQ_HANDLED;
  65. cec->v5_ts = ktime_get();
  66. cec->v5_is_high = is_high;
  67. return IRQ_WAKE_THREAD;
  68. }
  69. static irqreturn_t cec_5v_gpio_irq_handler_thread(int irq, void *priv)
  70. {
  71. struct cec_gpio *cec = priv;
  72. cec_queue_pin_5v_event(cec->adap, cec->v5_is_high, cec->v5_ts);
  73. return IRQ_HANDLED;
  74. }
  75. static irqreturn_t cec_hpd_gpio_irq_handler(int irq, void *priv)
  76. {
  77. struct cec_gpio *cec = priv;
  78. bool is_high = gpiod_get_value(cec->hpd_gpio);
  79. if (is_high == cec->hpd_is_high)
  80. return IRQ_HANDLED;
  81. cec->hpd_ts = ktime_get();
  82. cec->hpd_is_high = is_high;
  83. return IRQ_WAKE_THREAD;
  84. }
  85. static irqreturn_t cec_gpio_irq_handler(int irq, void *priv)
  86. {
  87. struct cec_gpio *cec = priv;
  88. cec_pin_changed(cec->adap, gpiod_get_value(cec->cec_gpio));
  89. return IRQ_HANDLED;
  90. }
  91. static bool cec_gpio_enable_irq(struct cec_adapter *adap)
  92. {
  93. struct cec_gpio *cec = cec_get_drvdata(adap);
  94. if (cec->cec_have_irq)
  95. return true;
  96. if (request_irq(cec->cec_irq, cec_gpio_irq_handler,
  97. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  98. adap->name, cec))
  99. return false;
  100. cec->cec_have_irq = true;
  101. return true;
  102. }
  103. static void cec_gpio_disable_irq(struct cec_adapter *adap)
  104. {
  105. struct cec_gpio *cec = cec_get_drvdata(adap);
  106. if (cec->cec_have_irq)
  107. free_irq(cec->cec_irq, cec);
  108. cec->cec_have_irq = false;
  109. }
  110. static void cec_gpio_status(struct cec_adapter *adap, struct seq_file *file)
  111. {
  112. struct cec_gpio *cec = cec_get_drvdata(adap);
  113. seq_printf(file, "mode: %s\n", cec->cec_is_low ? "low-drive" : "read");
  114. if (cec->cec_have_irq)
  115. seq_printf(file, "using irq: %d\n", cec->cec_irq);
  116. if (cec->hpd_gpio)
  117. seq_printf(file, "hpd: %s\n",
  118. cec->hpd_is_high ? "high" : "low");
  119. if (cec->v5_gpio)
  120. seq_printf(file, "5V: %s\n",
  121. cec->v5_is_high ? "high" : "low");
  122. }
  123. static int cec_gpio_read_hpd(struct cec_adapter *adap)
  124. {
  125. struct cec_gpio *cec = cec_get_drvdata(adap);
  126. if (!cec->hpd_gpio)
  127. return -ENOTTY;
  128. return gpiod_get_value(cec->hpd_gpio);
  129. }
  130. static int cec_gpio_read_5v(struct cec_adapter *adap)
  131. {
  132. struct cec_gpio *cec = cec_get_drvdata(adap);
  133. if (!cec->v5_gpio)
  134. return -ENOTTY;
  135. return gpiod_get_value(cec->v5_gpio);
  136. }
  137. static void cec_gpio_free(struct cec_adapter *adap)
  138. {
  139. cec_gpio_disable_irq(adap);
  140. }
  141. static const struct cec_pin_ops cec_gpio_pin_ops = {
  142. .read = cec_gpio_read,
  143. .low = cec_gpio_low,
  144. .high = cec_gpio_high,
  145. .enable_irq = cec_gpio_enable_irq,
  146. .disable_irq = cec_gpio_disable_irq,
  147. .status = cec_gpio_status,
  148. .free = cec_gpio_free,
  149. .read_hpd = cec_gpio_read_hpd,
  150. .read_5v = cec_gpio_read_5v,
  151. };
  152. static int cec_gpio_probe(struct platform_device *pdev)
  153. {
  154. struct device *dev = &pdev->dev;
  155. struct cec_gpio *cec;
  156. int ret;
  157. cec = devm_kzalloc(dev, sizeof(*cec), GFP_KERNEL);
  158. if (!cec)
  159. return -ENOMEM;
  160. cec->dev = dev;
  161. cec->cec_gpio = devm_gpiod_get(dev, "cec", GPIOD_OUT_HIGH_OPEN_DRAIN);
  162. if (IS_ERR(cec->cec_gpio))
  163. return PTR_ERR(cec->cec_gpio);
  164. cec->cec_irq = gpiod_to_irq(cec->cec_gpio);
  165. cec->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
  166. if (IS_ERR(cec->hpd_gpio))
  167. return PTR_ERR(cec->hpd_gpio);
  168. cec->v5_gpio = devm_gpiod_get_optional(dev, "v5", GPIOD_IN);
  169. if (IS_ERR(cec->v5_gpio))
  170. return PTR_ERR(cec->v5_gpio);
  171. cec->adap = cec_pin_allocate_adapter(&cec_gpio_pin_ops,
  172. cec, pdev->name, CEC_CAP_DEFAULTS | CEC_CAP_PHYS_ADDR |
  173. CEC_CAP_MONITOR_ALL | CEC_CAP_MONITOR_PIN);
  174. if (IS_ERR(cec->adap))
  175. return PTR_ERR(cec->adap);
  176. if (cec->hpd_gpio) {
  177. cec->hpd_irq = gpiod_to_irq(cec->hpd_gpio);
  178. ret = devm_request_threaded_irq(dev, cec->hpd_irq,
  179. cec_hpd_gpio_irq_handler,
  180. cec_hpd_gpio_irq_handler_thread,
  181. IRQF_ONESHOT |
  182. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  183. "hpd-gpio", cec);
  184. if (ret)
  185. return ret;
  186. }
  187. if (cec->v5_gpio) {
  188. cec->v5_irq = gpiod_to_irq(cec->v5_gpio);
  189. ret = devm_request_threaded_irq(dev, cec->v5_irq,
  190. cec_5v_gpio_irq_handler,
  191. cec_5v_gpio_irq_handler_thread,
  192. IRQF_ONESHOT |
  193. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  194. "v5-gpio", cec);
  195. if (ret)
  196. return ret;
  197. }
  198. ret = cec_register_adapter(cec->adap, &pdev->dev);
  199. if (ret) {
  200. cec_delete_adapter(cec->adap);
  201. return ret;
  202. }
  203. platform_set_drvdata(pdev, cec);
  204. return 0;
  205. }
  206. static int cec_gpio_remove(struct platform_device *pdev)
  207. {
  208. struct cec_gpio *cec = platform_get_drvdata(pdev);
  209. cec_unregister_adapter(cec->adap);
  210. return 0;
  211. }
  212. static const struct of_device_id cec_gpio_match[] = {
  213. {
  214. .compatible = "cec-gpio",
  215. },
  216. {},
  217. };
  218. MODULE_DEVICE_TABLE(of, cec_gpio_match);
  219. static struct platform_driver cec_gpio_pdrv = {
  220. .probe = cec_gpio_probe,
  221. .remove = cec_gpio_remove,
  222. .driver = {
  223. .name = "cec-gpio",
  224. .of_match_table = cec_gpio_match,
  225. },
  226. };
  227. module_platform_driver(cec_gpio_pdrv);
  228. MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
  229. MODULE_LICENSE("GPL v2");
  230. MODULE_DESCRIPTION("CEC GPIO driver");