hdmi4_cec.c 11 KB

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