hdmi4_cec.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HDMI CEC
  4. *
  5. * Based on the CEC code from hdmi_ti_4xxx_ip.c from Android.
  6. *
  7. * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com/
  8. * Authors: Yong Zhi
  9. * Mythri pk <mythripk@ti.com>
  10. *
  11. * Heavily modified to use the linux CEC framework:
  12. *
  13. * Copyright 2016-2017 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/err.h>
  17. #include <linux/io.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include "dss.h"
  21. #include "hdmi.h"
  22. #include "hdmi4_core.h"
  23. #include "hdmi4_cec.h"
  24. /* HDMI CEC */
  25. #define HDMI_CEC_DEV_ID 0x900
  26. #define HDMI_CEC_SPEC 0x904
  27. /* Not really a debug register, more a low-level control register */
  28. #define HDMI_CEC_DBG_3 0x91C
  29. #define HDMI_CEC_TX_INIT 0x920
  30. #define HDMI_CEC_TX_DEST 0x924
  31. #define HDMI_CEC_SETUP 0x938
  32. #define HDMI_CEC_TX_COMMAND 0x93C
  33. #define HDMI_CEC_TX_OPERAND 0x940
  34. #define HDMI_CEC_TRANSMIT_DATA 0x97C
  35. #define HDMI_CEC_CA_7_0 0x988
  36. #define HDMI_CEC_CA_15_8 0x98C
  37. #define HDMI_CEC_INT_STATUS_0 0x998
  38. #define HDMI_CEC_INT_STATUS_1 0x99C
  39. #define HDMI_CEC_INT_ENABLE_0 0x990
  40. #define HDMI_CEC_INT_ENABLE_1 0x994
  41. #define HDMI_CEC_RX_CONTROL 0x9B0
  42. #define HDMI_CEC_RX_COUNT 0x9B4
  43. #define HDMI_CEC_RX_CMD_HEADER 0x9B8
  44. #define HDMI_CEC_RX_COMMAND 0x9BC
  45. #define HDMI_CEC_RX_OPERAND 0x9C0
  46. #define HDMI_CEC_TX_FIFO_INT_MASK 0x64
  47. #define HDMI_CEC_RETRANSMIT_CNT_INT_MASK 0x2
  48. #define HDMI_CORE_CEC_RETRY 200
  49. static void hdmi_cec_received_msg(struct hdmi_core_data *core)
  50. {
  51. u32 cnt = hdmi_read_reg(core->base, HDMI_CEC_RX_COUNT) & 0xff;
  52. /* While there are CEC frames in the FIFO */
  53. while (cnt & 0x70) {
  54. /* and the frame doesn't have an error */
  55. if (!(cnt & 0x80)) {
  56. struct cec_msg msg = {};
  57. unsigned int i;
  58. /* then read the message */
  59. msg.len = cnt & 0xf;
  60. if (msg.len > CEC_MAX_MSG_SIZE - 2)
  61. msg.len = CEC_MAX_MSG_SIZE - 2;
  62. msg.msg[0] = hdmi_read_reg(core->base,
  63. HDMI_CEC_RX_CMD_HEADER);
  64. msg.msg[1] = hdmi_read_reg(core->base,
  65. HDMI_CEC_RX_COMMAND);
  66. for (i = 0; i < msg.len; i++) {
  67. unsigned int reg = HDMI_CEC_RX_OPERAND + i * 4;
  68. msg.msg[2 + i] =
  69. hdmi_read_reg(core->base, reg);
  70. }
  71. msg.len += 2;
  72. cec_received_msg(core->adap, &msg);
  73. }
  74. /* Clear the current frame from the FIFO */
  75. hdmi_write_reg(core->base, HDMI_CEC_RX_CONTROL, 1);
  76. /* Wait until the current frame is cleared */
  77. while (hdmi_read_reg(core->base, HDMI_CEC_RX_CONTROL) & 1)
  78. udelay(1);
  79. /*
  80. * Re-read the count register and loop to see if there are
  81. * more messages in the FIFO.
  82. */
  83. cnt = hdmi_read_reg(core->base, HDMI_CEC_RX_COUNT) & 0xff;
  84. }
  85. }
  86. void hdmi4_cec_irq(struct hdmi_core_data *core)
  87. {
  88. u32 stat0 = hdmi_read_reg(core->base, HDMI_CEC_INT_STATUS_0);
  89. u32 stat1 = hdmi_read_reg(core->base, HDMI_CEC_INT_STATUS_1);
  90. hdmi_write_reg(core->base, HDMI_CEC_INT_STATUS_0, stat0);
  91. hdmi_write_reg(core->base, HDMI_CEC_INT_STATUS_1, stat1);
  92. if (stat0 & 0x20) {
  93. cec_transmit_done(core->adap, CEC_TX_STATUS_OK,
  94. 0, 0, 0, 0);
  95. REG_FLD_MOD(core->base, HDMI_CEC_DBG_3, 0x1, 7, 7);
  96. } else if (stat1 & 0x02) {
  97. u32 dbg3 = hdmi_read_reg(core->base, HDMI_CEC_DBG_3);
  98. cec_transmit_done(core->adap,
  99. CEC_TX_STATUS_NACK |
  100. CEC_TX_STATUS_MAX_RETRIES,
  101. 0, (dbg3 >> 4) & 7, 0, 0);
  102. REG_FLD_MOD(core->base, HDMI_CEC_DBG_3, 0x1, 7, 7);
  103. }
  104. if (stat0 & 0x02)
  105. hdmi_cec_received_msg(core);
  106. }
  107. static bool hdmi_cec_clear_tx_fifo(struct cec_adapter *adap)
  108. {
  109. struct hdmi_core_data *core = cec_get_drvdata(adap);
  110. int retry = HDMI_CORE_CEC_RETRY;
  111. int temp;
  112. REG_FLD_MOD(core->base, HDMI_CEC_DBG_3, 0x1, 7, 7);
  113. while (retry) {
  114. temp = hdmi_read_reg(core->base, HDMI_CEC_DBG_3);
  115. if (FLD_GET(temp, 7, 7) == 0)
  116. break;
  117. retry--;
  118. }
  119. return retry != 0;
  120. }
  121. static bool hdmi_cec_clear_rx_fifo(struct cec_adapter *adap)
  122. {
  123. struct hdmi_core_data *core = cec_get_drvdata(adap);
  124. int retry = HDMI_CORE_CEC_RETRY;
  125. int temp;
  126. hdmi_write_reg(core->base, HDMI_CEC_RX_CONTROL, 0x3);
  127. retry = HDMI_CORE_CEC_RETRY;
  128. while (retry) {
  129. temp = hdmi_read_reg(core->base, HDMI_CEC_RX_CONTROL);
  130. if (FLD_GET(temp, 1, 0) == 0)
  131. break;
  132. retry--;
  133. }
  134. return retry != 0;
  135. }
  136. static int hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
  137. {
  138. struct hdmi_core_data *core = cec_get_drvdata(adap);
  139. int temp, err;
  140. if (!enable) {
  141. hdmi_write_reg(core->base, HDMI_CEC_INT_ENABLE_0, 0);
  142. hdmi_write_reg(core->base, HDMI_CEC_INT_ENABLE_1, 0);
  143. REG_FLD_MOD(core->base, HDMI_CORE_SYS_INTR_UNMASK4, 0, 3, 3);
  144. hdmi_wp_clear_irqenable(core->wp, HDMI_IRQ_CORE);
  145. hdmi_wp_set_irqstatus(core->wp, HDMI_IRQ_CORE);
  146. REG_FLD_MOD(core->wp->base, HDMI_WP_CLK, 0, 5, 0);
  147. hdmi4_core_disable(core);
  148. return 0;
  149. }
  150. err = hdmi4_core_enable(core);
  151. if (err)
  152. return err;
  153. /*
  154. * Initialize CEC clock divider: CEC needs 2MHz clock hence
  155. * set the divider to 24 to get 48/24=2MHz clock
  156. */
  157. REG_FLD_MOD(core->wp->base, HDMI_WP_CLK, 0x18, 5, 0);
  158. /* Clear TX FIFO */
  159. if (!hdmi_cec_clear_tx_fifo(adap)) {
  160. pr_err("cec-%s: could not clear TX FIFO\n", adap->name);
  161. err = -EIO;
  162. goto err_disable_clk;
  163. }
  164. /* Clear RX FIFO */
  165. if (!hdmi_cec_clear_rx_fifo(adap)) {
  166. pr_err("cec-%s: could not clear RX FIFO\n", adap->name);
  167. err = -EIO;
  168. goto err_disable_clk;
  169. }
  170. /* Clear CEC interrupts */
  171. hdmi_write_reg(core->base, HDMI_CEC_INT_STATUS_1,
  172. hdmi_read_reg(core->base, HDMI_CEC_INT_STATUS_1));
  173. hdmi_write_reg(core->base, HDMI_CEC_INT_STATUS_0,
  174. hdmi_read_reg(core->base, HDMI_CEC_INT_STATUS_0));
  175. /* Enable HDMI core interrupts */
  176. hdmi_wp_set_irqenable(core->wp, HDMI_IRQ_CORE);
  177. /* Unmask CEC interrupt */
  178. REG_FLD_MOD(core->base, HDMI_CORE_SYS_INTR_UNMASK4, 0x1, 3, 3);
  179. /*
  180. * Enable CEC interrupts:
  181. * Transmit Buffer Full/Empty Change event
  182. * Receiver FIFO Not Empty event
  183. */
  184. hdmi_write_reg(core->base, HDMI_CEC_INT_ENABLE_0, 0x22);
  185. /*
  186. * Enable CEC interrupts:
  187. * Frame Retransmit Count Exceeded event
  188. */
  189. hdmi_write_reg(core->base, HDMI_CEC_INT_ENABLE_1, 0x02);
  190. /* cec calibration enable (self clearing) */
  191. hdmi_write_reg(core->base, HDMI_CEC_SETUP, 0x03);
  192. msleep(20);
  193. hdmi_write_reg(core->base, HDMI_CEC_SETUP, 0x04);
  194. temp = hdmi_read_reg(core->base, HDMI_CEC_SETUP);
  195. if (FLD_GET(temp, 4, 4) != 0) {
  196. temp = FLD_MOD(temp, 0, 4, 4);
  197. hdmi_write_reg(core->base, HDMI_CEC_SETUP, temp);
  198. /*
  199. * If we enabled CEC in middle of a CEC message on the bus,
  200. * we could have start bit irregularity and/or short
  201. * pulse event. Clear them now.
  202. */
  203. temp = hdmi_read_reg(core->base, HDMI_CEC_INT_STATUS_1);
  204. temp = FLD_MOD(0x0, 0x5, 2, 0);
  205. hdmi_write_reg(core->base, HDMI_CEC_INT_STATUS_1, temp);
  206. }
  207. return 0;
  208. err_disable_clk:
  209. REG_FLD_MOD(core->wp->base, HDMI_WP_CLK, 0, 5, 0);
  210. hdmi4_core_disable(core);
  211. return err;
  212. }
  213. static int hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
  214. {
  215. struct hdmi_core_data *core = cec_get_drvdata(adap);
  216. u32 v;
  217. if (log_addr == CEC_LOG_ADDR_INVALID) {
  218. hdmi_write_reg(core->base, HDMI_CEC_CA_7_0, 0);
  219. hdmi_write_reg(core->base, HDMI_CEC_CA_15_8, 0);
  220. return 0;
  221. }
  222. if (log_addr <= 7) {
  223. v = hdmi_read_reg(core->base, HDMI_CEC_CA_7_0);
  224. v |= 1 << log_addr;
  225. hdmi_write_reg(core->base, HDMI_CEC_CA_7_0, v);
  226. } else {
  227. v = hdmi_read_reg(core->base, HDMI_CEC_CA_15_8);
  228. v |= 1 << (log_addr - 8);
  229. hdmi_write_reg(core->base, HDMI_CEC_CA_15_8, v);
  230. }
  231. return 0;
  232. }
  233. static int hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
  234. u32 signal_free_time, struct cec_msg *msg)
  235. {
  236. struct hdmi_core_data *core = cec_get_drvdata(adap);
  237. int temp;
  238. u32 i;
  239. /* Clear TX FIFO */
  240. if (!hdmi_cec_clear_tx_fifo(adap)) {
  241. pr_err("cec-%s: could not clear TX FIFO for transmit\n",
  242. adap->name);
  243. return -EIO;
  244. }
  245. /* Clear TX interrupts */
  246. hdmi_write_reg(core->base, HDMI_CEC_INT_STATUS_0,
  247. HDMI_CEC_TX_FIFO_INT_MASK);
  248. hdmi_write_reg(core->base, HDMI_CEC_INT_STATUS_1,
  249. HDMI_CEC_RETRANSMIT_CNT_INT_MASK);
  250. /* Set the retry count */
  251. REG_FLD_MOD(core->base, HDMI_CEC_DBG_3, attempts - 1, 6, 4);
  252. /* Set the initiator addresses */
  253. hdmi_write_reg(core->base, HDMI_CEC_TX_INIT, cec_msg_initiator(msg));
  254. /* Set destination id */
  255. temp = cec_msg_destination(msg);
  256. if (msg->len == 1)
  257. temp |= 0x80;
  258. hdmi_write_reg(core->base, HDMI_CEC_TX_DEST, temp);
  259. if (msg->len == 1)
  260. return 0;
  261. /* Setup command and arguments for the command */
  262. hdmi_write_reg(core->base, HDMI_CEC_TX_COMMAND, msg->msg[1]);
  263. for (i = 0; i < msg->len - 2; i++)
  264. hdmi_write_reg(core->base, HDMI_CEC_TX_OPERAND + i * 4,
  265. msg->msg[2 + i]);
  266. /* Operand count */
  267. hdmi_write_reg(core->base, HDMI_CEC_TRANSMIT_DATA,
  268. (msg->len - 2) | 0x10);
  269. return 0;
  270. }
  271. static const struct cec_adap_ops hdmi_cec_adap_ops = {
  272. .adap_enable = hdmi_cec_adap_enable,
  273. .adap_log_addr = hdmi_cec_adap_log_addr,
  274. .adap_transmit = hdmi_cec_adap_transmit,
  275. };
  276. void hdmi4_cec_set_phys_addr(struct hdmi_core_data *core, u16 pa)
  277. {
  278. cec_s_phys_addr(core->adap, pa, false);
  279. }
  280. int hdmi4_cec_init(struct platform_device *pdev, struct hdmi_core_data *core,
  281. struct hdmi_wp_data *wp)
  282. {
  283. const u32 caps = CEC_CAP_TRANSMIT | CEC_CAP_LOG_ADDRS |
  284. CEC_CAP_PASSTHROUGH | CEC_CAP_RC;
  285. int ret;
  286. core->adap = cec_allocate_adapter(&hdmi_cec_adap_ops, core,
  287. "omap4", caps, CEC_MAX_LOG_ADDRS);
  288. ret = PTR_ERR_OR_ZERO(core->adap);
  289. if (ret < 0)
  290. return ret;
  291. core->wp = wp;
  292. /* Disable clock initially, hdmi_cec_adap_enable() manages it */
  293. REG_FLD_MOD(core->wp->base, HDMI_WP_CLK, 0, 5, 0);
  294. ret = cec_register_adapter(core->adap, &pdev->dev);
  295. if (ret < 0) {
  296. cec_delete_adapter(core->adap);
  297. return ret;
  298. }
  299. return 0;
  300. }
  301. void hdmi4_cec_uninit(struct hdmi_core_data *core)
  302. {
  303. cec_unregister_adapter(core->adap);
  304. }