hdmi4_cec.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. hdmi4_core_disable(core);
  159. return 0;
  160. }
  161. err = hdmi4_core_enable(core);
  162. if (err)
  163. return err;
  164. /* Clear TX FIFO */
  165. if (!hdmi_cec_clear_tx_fifo(adap)) {
  166. pr_err("cec-%s: could not clear TX FIFO\n", adap->name);
  167. return -EIO;
  168. }
  169. /* Clear RX FIFO */
  170. if (!hdmi_cec_clear_rx_fifo(adap)) {
  171. pr_err("cec-%s: could not clear RX FIFO\n", adap->name);
  172. return -EIO;
  173. }
  174. /* Clear CEC interrupts */
  175. hdmi_write_reg(core->base, HDMI_CEC_INT_STATUS_1,
  176. hdmi_read_reg(core->base, HDMI_CEC_INT_STATUS_1));
  177. hdmi_write_reg(core->base, HDMI_CEC_INT_STATUS_0,
  178. hdmi_read_reg(core->base, HDMI_CEC_INT_STATUS_0));
  179. /* Enable HDMI core interrupts */
  180. hdmi_wp_set_irqenable(core->wp, HDMI_IRQ_CORE);
  181. /* Unmask CEC interrupt */
  182. REG_FLD_MOD(core->base, HDMI_CORE_SYS_INTR_UNMASK4, 0x1, 3, 3);
  183. /*
  184. * Enable CEC interrupts:
  185. * Transmit Buffer Full/Empty Change event
  186. * Receiver FIFO Not Empty event
  187. */
  188. hdmi_write_reg(core->base, HDMI_CEC_INT_ENABLE_0, 0x22);
  189. /*
  190. * Enable CEC interrupts:
  191. * Frame Retransmit Count Exceeded event
  192. */
  193. hdmi_write_reg(core->base, HDMI_CEC_INT_ENABLE_1, 0x02);
  194. /* cec calibration enable (self clearing) */
  195. hdmi_write_reg(core->base, HDMI_CEC_SETUP, 0x03);
  196. msleep(20);
  197. hdmi_write_reg(core->base, HDMI_CEC_SETUP, 0x04);
  198. temp = hdmi_read_reg(core->base, HDMI_CEC_SETUP);
  199. if (FLD_GET(temp, 4, 4) != 0) {
  200. temp = FLD_MOD(temp, 0, 4, 4);
  201. hdmi_write_reg(core->base, HDMI_CEC_SETUP, temp);
  202. /*
  203. * If we enabled CEC in middle of a CEC message on the bus,
  204. * we could have start bit irregularity and/or short
  205. * pulse event. Clear them now.
  206. */
  207. temp = hdmi_read_reg(core->base, HDMI_CEC_INT_STATUS_1);
  208. temp = FLD_MOD(0x0, 0x5, 2, 0);
  209. hdmi_write_reg(core->base, HDMI_CEC_INT_STATUS_1, temp);
  210. }
  211. return 0;
  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. /*
  293. * Initialize CEC clock divider: CEC needs 2MHz clock hence
  294. * set the devider to 24 to get 48/24=2MHz clock
  295. */
  296. REG_FLD_MOD(core->wp->base, HDMI_WP_CLK, 0x18, 5, 0);
  297. ret = cec_register_adapter(core->adap, &pdev->dev);
  298. if (ret < 0) {
  299. cec_delete_adapter(core->adap);
  300. return ret;
  301. }
  302. return 0;
  303. }
  304. void hdmi4_cec_uninit(struct hdmi_core_data *core)
  305. {
  306. cec_unregister_adapter(core->adap);
  307. }