dw-hdmi-cec.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Designware HDMI CEC driver
  3. *
  4. * Copyright (C) 2015-2017 Russell King.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/sched.h>
  15. #include <linux/slab.h>
  16. #include <drm/drm_edid.h>
  17. #include <media/cec.h>
  18. #include <media/cec-notifier.h>
  19. #include "dw-hdmi-cec.h"
  20. enum {
  21. HDMI_IH_CEC_STAT0 = 0x0106,
  22. HDMI_IH_MUTE_CEC_STAT0 = 0x0186,
  23. HDMI_CEC_CTRL = 0x7d00,
  24. CEC_CTRL_START = BIT(0),
  25. CEC_CTRL_FRAME_TYP = 3 << 1,
  26. CEC_CTRL_RETRY = 0 << 1,
  27. CEC_CTRL_NORMAL = 1 << 1,
  28. CEC_CTRL_IMMED = 2 << 1,
  29. HDMI_CEC_STAT = 0x7d01,
  30. CEC_STAT_DONE = BIT(0),
  31. CEC_STAT_EOM = BIT(1),
  32. CEC_STAT_NACK = BIT(2),
  33. CEC_STAT_ARBLOST = BIT(3),
  34. CEC_STAT_ERROR_INIT = BIT(4),
  35. CEC_STAT_ERROR_FOLL = BIT(5),
  36. CEC_STAT_WAKEUP = BIT(6),
  37. HDMI_CEC_MASK = 0x7d02,
  38. HDMI_CEC_POLARITY = 0x7d03,
  39. HDMI_CEC_INT = 0x7d04,
  40. HDMI_CEC_ADDR_L = 0x7d05,
  41. HDMI_CEC_ADDR_H = 0x7d06,
  42. HDMI_CEC_TX_CNT = 0x7d07,
  43. HDMI_CEC_RX_CNT = 0x7d08,
  44. HDMI_CEC_TX_DATA0 = 0x7d10,
  45. HDMI_CEC_RX_DATA0 = 0x7d20,
  46. HDMI_CEC_LOCK = 0x7d30,
  47. HDMI_CEC_WKUPCTRL = 0x7d31,
  48. };
  49. struct dw_hdmi_cec {
  50. struct dw_hdmi *hdmi;
  51. const struct dw_hdmi_cec_ops *ops;
  52. u32 addresses;
  53. struct cec_adapter *adap;
  54. struct cec_msg rx_msg;
  55. unsigned int tx_status;
  56. bool tx_done;
  57. bool rx_done;
  58. struct cec_notifier *notify;
  59. int irq;
  60. };
  61. static void dw_hdmi_write(struct dw_hdmi_cec *cec, u8 val, int offset)
  62. {
  63. cec->ops->write(cec->hdmi, val, offset);
  64. }
  65. static u8 dw_hdmi_read(struct dw_hdmi_cec *cec, int offset)
  66. {
  67. return cec->ops->read(cec->hdmi, offset);
  68. }
  69. static int dw_hdmi_cec_log_addr(struct cec_adapter *adap, u8 logical_addr)
  70. {
  71. struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
  72. if (logical_addr == CEC_LOG_ADDR_INVALID)
  73. cec->addresses = 0;
  74. else
  75. cec->addresses |= BIT(logical_addr) | BIT(15);
  76. dw_hdmi_write(cec, cec->addresses & 255, HDMI_CEC_ADDR_L);
  77. dw_hdmi_write(cec, cec->addresses >> 8, HDMI_CEC_ADDR_H);
  78. return 0;
  79. }
  80. static int dw_hdmi_cec_transmit(struct cec_adapter *adap, u8 attempts,
  81. u32 signal_free_time, struct cec_msg *msg)
  82. {
  83. struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
  84. unsigned int i, ctrl;
  85. switch (signal_free_time) {
  86. case CEC_SIGNAL_FREE_TIME_RETRY:
  87. ctrl = CEC_CTRL_RETRY;
  88. break;
  89. case CEC_SIGNAL_FREE_TIME_NEW_INITIATOR:
  90. default:
  91. ctrl = CEC_CTRL_NORMAL;
  92. break;
  93. case CEC_SIGNAL_FREE_TIME_NEXT_XFER:
  94. ctrl = CEC_CTRL_IMMED;
  95. break;
  96. }
  97. for (i = 0; i < msg->len; i++)
  98. dw_hdmi_write(cec, msg->msg[i], HDMI_CEC_TX_DATA0 + i);
  99. dw_hdmi_write(cec, msg->len, HDMI_CEC_TX_CNT);
  100. dw_hdmi_write(cec, ctrl | CEC_CTRL_START, HDMI_CEC_CTRL);
  101. return 0;
  102. }
  103. static irqreturn_t dw_hdmi_cec_hardirq(int irq, void *data)
  104. {
  105. struct cec_adapter *adap = data;
  106. struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
  107. unsigned int stat = dw_hdmi_read(cec, HDMI_IH_CEC_STAT0);
  108. irqreturn_t ret = IRQ_HANDLED;
  109. if (stat == 0)
  110. return IRQ_NONE;
  111. dw_hdmi_write(cec, stat, HDMI_IH_CEC_STAT0);
  112. if (stat & CEC_STAT_ERROR_INIT) {
  113. cec->tx_status = CEC_TX_STATUS_ERROR;
  114. cec->tx_done = true;
  115. ret = IRQ_WAKE_THREAD;
  116. } else if (stat & CEC_STAT_DONE) {
  117. cec->tx_status = CEC_TX_STATUS_OK;
  118. cec->tx_done = true;
  119. ret = IRQ_WAKE_THREAD;
  120. } else if (stat & CEC_STAT_NACK) {
  121. cec->tx_status = CEC_TX_STATUS_NACK;
  122. cec->tx_done = true;
  123. ret = IRQ_WAKE_THREAD;
  124. }
  125. if (stat & CEC_STAT_EOM) {
  126. unsigned int len, i;
  127. len = dw_hdmi_read(cec, HDMI_CEC_RX_CNT);
  128. if (len > sizeof(cec->rx_msg.msg))
  129. len = sizeof(cec->rx_msg.msg);
  130. for (i = 0; i < len; i++)
  131. cec->rx_msg.msg[i] =
  132. dw_hdmi_read(cec, HDMI_CEC_RX_DATA0 + i);
  133. dw_hdmi_write(cec, 0, HDMI_CEC_LOCK);
  134. cec->rx_msg.len = len;
  135. smp_wmb();
  136. cec->rx_done = true;
  137. ret = IRQ_WAKE_THREAD;
  138. }
  139. return ret;
  140. }
  141. static irqreturn_t dw_hdmi_cec_thread(int irq, void *data)
  142. {
  143. struct cec_adapter *adap = data;
  144. struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
  145. if (cec->tx_done) {
  146. cec->tx_done = false;
  147. cec_transmit_attempt_done(adap, cec->tx_status);
  148. }
  149. if (cec->rx_done) {
  150. cec->rx_done = false;
  151. smp_rmb();
  152. cec_received_msg(adap, &cec->rx_msg);
  153. }
  154. return IRQ_HANDLED;
  155. }
  156. static int dw_hdmi_cec_enable(struct cec_adapter *adap, bool enable)
  157. {
  158. struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
  159. if (!enable) {
  160. dw_hdmi_write(cec, ~0, HDMI_CEC_MASK);
  161. dw_hdmi_write(cec, ~0, HDMI_IH_MUTE_CEC_STAT0);
  162. dw_hdmi_write(cec, 0, HDMI_CEC_POLARITY);
  163. cec->ops->disable(cec->hdmi);
  164. } else {
  165. unsigned int irqs;
  166. dw_hdmi_write(cec, 0, HDMI_CEC_CTRL);
  167. dw_hdmi_write(cec, ~0, HDMI_IH_CEC_STAT0);
  168. dw_hdmi_write(cec, 0, HDMI_CEC_LOCK);
  169. dw_hdmi_cec_log_addr(cec->adap, CEC_LOG_ADDR_INVALID);
  170. cec->ops->enable(cec->hdmi);
  171. irqs = CEC_STAT_ERROR_INIT | CEC_STAT_NACK | CEC_STAT_EOM |
  172. CEC_STAT_DONE;
  173. dw_hdmi_write(cec, irqs, HDMI_CEC_POLARITY);
  174. dw_hdmi_write(cec, ~irqs, HDMI_CEC_MASK);
  175. dw_hdmi_write(cec, ~irqs, HDMI_IH_MUTE_CEC_STAT0);
  176. }
  177. return 0;
  178. }
  179. static const struct cec_adap_ops dw_hdmi_cec_ops = {
  180. .adap_enable = dw_hdmi_cec_enable,
  181. .adap_log_addr = dw_hdmi_cec_log_addr,
  182. .adap_transmit = dw_hdmi_cec_transmit,
  183. };
  184. static void dw_hdmi_cec_del(void *data)
  185. {
  186. struct dw_hdmi_cec *cec = data;
  187. cec_delete_adapter(cec->adap);
  188. }
  189. static int dw_hdmi_cec_probe(struct platform_device *pdev)
  190. {
  191. struct dw_hdmi_cec_data *data = dev_get_platdata(&pdev->dev);
  192. struct dw_hdmi_cec *cec;
  193. int ret;
  194. if (!data)
  195. return -ENXIO;
  196. /*
  197. * Our device is just a convenience - we want to link to the real
  198. * hardware device here, so that userspace can see the association
  199. * between the HDMI hardware and its associated CEC chardev.
  200. */
  201. cec = devm_kzalloc(&pdev->dev, sizeof(*cec), GFP_KERNEL);
  202. if (!cec)
  203. return -ENOMEM;
  204. cec->irq = data->irq;
  205. cec->ops = data->ops;
  206. cec->hdmi = data->hdmi;
  207. platform_set_drvdata(pdev, cec);
  208. dw_hdmi_write(cec, 0, HDMI_CEC_TX_CNT);
  209. dw_hdmi_write(cec, ~0, HDMI_CEC_MASK);
  210. dw_hdmi_write(cec, ~0, HDMI_IH_MUTE_CEC_STAT0);
  211. dw_hdmi_write(cec, 0, HDMI_CEC_POLARITY);
  212. cec->adap = cec_allocate_adapter(&dw_hdmi_cec_ops, cec, "dw_hdmi",
  213. CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT |
  214. CEC_CAP_RC | CEC_CAP_PASSTHROUGH,
  215. CEC_MAX_LOG_ADDRS);
  216. if (IS_ERR(cec->adap))
  217. return PTR_ERR(cec->adap);
  218. /* override the module pointer */
  219. cec->adap->owner = THIS_MODULE;
  220. ret = devm_add_action(&pdev->dev, dw_hdmi_cec_del, cec);
  221. if (ret) {
  222. cec_delete_adapter(cec->adap);
  223. return ret;
  224. }
  225. ret = devm_request_threaded_irq(&pdev->dev, cec->irq,
  226. dw_hdmi_cec_hardirq,
  227. dw_hdmi_cec_thread, IRQF_SHARED,
  228. "dw-hdmi-cec", cec->adap);
  229. if (ret < 0)
  230. return ret;
  231. cec->notify = cec_notifier_get(pdev->dev.parent);
  232. if (!cec->notify)
  233. return -ENOMEM;
  234. ret = cec_register_adapter(cec->adap, pdev->dev.parent);
  235. if (ret < 0) {
  236. cec_notifier_put(cec->notify);
  237. return ret;
  238. }
  239. /*
  240. * CEC documentation says we must not call cec_delete_adapter
  241. * after a successful call to cec_register_adapter().
  242. */
  243. devm_remove_action(&pdev->dev, dw_hdmi_cec_del, cec);
  244. cec_register_cec_notifier(cec->adap, cec->notify);
  245. return 0;
  246. }
  247. static int dw_hdmi_cec_remove(struct platform_device *pdev)
  248. {
  249. struct dw_hdmi_cec *cec = platform_get_drvdata(pdev);
  250. cec_unregister_adapter(cec->adap);
  251. cec_notifier_put(cec->notify);
  252. return 0;
  253. }
  254. static struct platform_driver dw_hdmi_cec_driver = {
  255. .probe = dw_hdmi_cec_probe,
  256. .remove = dw_hdmi_cec_remove,
  257. .driver = {
  258. .name = "dw-hdmi-cec",
  259. },
  260. };
  261. module_platform_driver(dw_hdmi_cec_driver);
  262. MODULE_AUTHOR("Russell King <rmk+kernel@armlinux.org.uk>");
  263. MODULE_DESCRIPTION("Synopsys Designware HDMI CEC driver for i.MX");
  264. MODULE_LICENSE("GPL");
  265. MODULE_ALIAS(PLATFORM_MODULE_PREFIX "dw-hdmi-cec");