hdmi4_cec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. msg.msg[0] = hdmi_read_reg(core->base,
  73. HDMI_CEC_RX_CMD_HEADER);
  74. msg.msg[1] = hdmi_read_reg(core->base,
  75. HDMI_CEC_RX_COMMAND);
  76. for (i = 0; i < msg.len; i++) {
  77. unsigned int reg = HDMI_CEC_RX_OPERAND + i * 4;
  78. msg.msg[2 + i] =
  79. hdmi_read_reg(core->base, reg);
  80. }
  81. msg.len += 2;
  82. cec_received_msg(core->adap, &msg);
  83. }
  84. /* Clear the current frame from the FIFO */
  85. hdmi_write_reg(core->base, HDMI_CEC_RX_CONTROL, 1);
  86. /* Wait until the current frame is cleared */
  87. while (hdmi_read_reg(core->base, HDMI_CEC_RX_CONTROL) & 1)
  88. udelay(1);
  89. /*
  90. * Re-read the count register and loop to see if there are
  91. * more messages in the FIFO.
  92. */
  93. cnt = hdmi_read_reg(core->base, HDMI_CEC_RX_COUNT) & 0xff;
  94. }
  95. }
  96. static void hdmi_cec_transmit_fifo_empty(struct hdmi_core_data *core, u32 stat1)
  97. {
  98. if (stat1 & 2) {
  99. u32 dbg3 = hdmi_read_reg(core->base, HDMI_CEC_DBG_3);
  100. cec_transmit_done(core->adap,
  101. CEC_TX_STATUS_NACK |
  102. CEC_TX_STATUS_MAX_RETRIES,
  103. 0, (dbg3 >> 4) & 7, 0, 0);
  104. } else if (stat1 & 1) {
  105. cec_transmit_done(core->adap,
  106. CEC_TX_STATUS_ARB_LOST |
  107. CEC_TX_STATUS_MAX_RETRIES,
  108. 0, 0, 0, 0);
  109. } else if (stat1 == 0) {
  110. cec_transmit_done(core->adap, CEC_TX_STATUS_OK,
  111. 0, 0, 0, 0);
  112. }
  113. }
  114. void hdmi4_cec_irq(struct hdmi_core_data *core)
  115. {
  116. u32 stat0 = hdmi_read_reg(core->base, HDMI_CEC_INT_STATUS_0);
  117. u32 stat1 = hdmi_read_reg(core->base, HDMI_CEC_INT_STATUS_1);
  118. hdmi_write_reg(core->base, HDMI_CEC_INT_STATUS_0, stat0);
  119. hdmi_write_reg(core->base, HDMI_CEC_INT_STATUS_1, stat1);
  120. if (stat0 & 0x40)
  121. REG_FLD_MOD(core->base, HDMI_CEC_DBG_3, 0x1, 7, 7);
  122. else if (stat0 & 0x24)
  123. hdmi_cec_transmit_fifo_empty(core, stat1);
  124. if (stat1 & 2) {
  125. u32 dbg3 = hdmi_read_reg(core->base, HDMI_CEC_DBG_3);
  126. cec_transmit_done(core->adap,
  127. CEC_TX_STATUS_NACK |
  128. CEC_TX_STATUS_MAX_RETRIES,
  129. 0, (dbg3 >> 4) & 7, 0, 0);
  130. } else if (stat1 & 1) {
  131. cec_transmit_done(core->adap,
  132. CEC_TX_STATUS_ARB_LOST |
  133. CEC_TX_STATUS_MAX_RETRIES,
  134. 0, 0, 0, 0);
  135. }
  136. if (stat0 & 0x02)
  137. hdmi_cec_received_msg(core);
  138. if (stat1 & 0x3)
  139. REG_FLD_MOD(core->base, HDMI_CEC_DBG_3, 0x1, 7, 7);
  140. }
  141. static bool hdmi_cec_clear_tx_fifo(struct cec_adapter *adap)
  142. {
  143. struct hdmi_core_data *core = cec_get_drvdata(adap);
  144. int retry = HDMI_CORE_CEC_RETRY;
  145. int temp;
  146. REG_FLD_MOD(core->base, HDMI_CEC_DBG_3, 0x1, 7, 7);
  147. while (retry) {
  148. temp = hdmi_read_reg(core->base, HDMI_CEC_DBG_3);
  149. if (FLD_GET(temp, 7, 7) == 0)
  150. break;
  151. retry--;
  152. }
  153. return retry != 0;
  154. }
  155. static bool hdmi_cec_clear_rx_fifo(struct cec_adapter *adap)
  156. {
  157. struct hdmi_core_data *core = cec_get_drvdata(adap);
  158. int retry = HDMI_CORE_CEC_RETRY;
  159. int temp;
  160. hdmi_write_reg(core->base, HDMI_CEC_RX_CONTROL, 0x3);
  161. retry = HDMI_CORE_CEC_RETRY;
  162. while (retry) {
  163. temp = hdmi_read_reg(core->base, HDMI_CEC_RX_CONTROL);
  164. if (FLD_GET(temp, 1, 0) == 0)
  165. break;
  166. retry--;
  167. }
  168. return retry != 0;
  169. }
  170. static int hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
  171. {
  172. struct hdmi_core_data *core = cec_get_drvdata(adap);
  173. int temp, err;
  174. if (!enable) {
  175. hdmi_write_reg(core->base, HDMI_CEC_INT_ENABLE_0, 0);
  176. hdmi_write_reg(core->base, HDMI_CEC_INT_ENABLE_1, 0);
  177. REG_FLD_MOD(core->base, HDMI_CORE_SYS_INTR_UNMASK4, 0, 3, 3);
  178. hdmi_wp_clear_irqenable(core->wp, HDMI_IRQ_CORE);
  179. hdmi_wp_set_irqstatus(core->wp, HDMI_IRQ_CORE);
  180. hdmi4_core_disable(NULL);
  181. return 0;
  182. }
  183. err = hdmi4_core_enable(NULL);
  184. if (err)
  185. return err;
  186. /* Clear TX FIFO */
  187. if (!hdmi_cec_clear_tx_fifo(adap)) {
  188. pr_err("cec-%s: could not clear TX FIFO\n", adap->name);
  189. return -EIO;
  190. }
  191. /* Clear RX FIFO */
  192. if (!hdmi_cec_clear_rx_fifo(adap)) {
  193. pr_err("cec-%s: could not clear RX FIFO\n", adap->name);
  194. return -EIO;
  195. }
  196. /* Clear CEC interrupts */
  197. hdmi_write_reg(core->base, HDMI_CEC_INT_STATUS_1,
  198. hdmi_read_reg(core->base, HDMI_CEC_INT_STATUS_1));
  199. hdmi_write_reg(core->base, HDMI_CEC_INT_STATUS_0,
  200. hdmi_read_reg(core->base, HDMI_CEC_INT_STATUS_0));
  201. /* Enable HDMI core interrupts */
  202. hdmi_wp_set_irqenable(core->wp, HDMI_IRQ_CORE);
  203. /* Unmask CEC interrupt */
  204. REG_FLD_MOD(core->base, HDMI_CORE_SYS_INTR_UNMASK4, 0x1, 3, 3);
  205. /*
  206. * Enable CEC interrupts:
  207. * Transmit Buffer Full/Empty Change event
  208. * Transmitter FIFO Empty event
  209. * Receiver FIFO Not Empty event
  210. */
  211. hdmi_write_reg(core->base, HDMI_CEC_INT_ENABLE_0, 0x26);
  212. /*
  213. * Enable CEC interrupts:
  214. * RX FIFO Overrun Error event
  215. * Short Pulse Detected event
  216. * Frame Retransmit Count Exceeded event
  217. * Start Bit Irregularity event
  218. */
  219. hdmi_write_reg(core->base, HDMI_CEC_INT_ENABLE_1, 0x0f);
  220. /* cec calibration enable (self clearing) */
  221. hdmi_write_reg(core->base, HDMI_CEC_SETUP, 0x03);
  222. msleep(20);
  223. hdmi_write_reg(core->base, HDMI_CEC_SETUP, 0x04);
  224. temp = hdmi_read_reg(core->base, HDMI_CEC_SETUP);
  225. if (FLD_GET(temp, 4, 4) != 0) {
  226. temp = FLD_MOD(temp, 0, 4, 4);
  227. hdmi_write_reg(core->base, HDMI_CEC_SETUP, temp);
  228. /*
  229. * If we enabled CEC in middle of a CEC message on the bus,
  230. * we could have start bit irregularity and/or short
  231. * pulse event. Clear them now.
  232. */
  233. temp = hdmi_read_reg(core->base, HDMI_CEC_INT_STATUS_1);
  234. temp = FLD_MOD(0x0, 0x5, 2, 0);
  235. hdmi_write_reg(core->base, HDMI_CEC_INT_STATUS_1, temp);
  236. }
  237. return 0;
  238. }
  239. static int hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
  240. {
  241. struct hdmi_core_data *core = cec_get_drvdata(adap);
  242. u32 v;
  243. if (log_addr == CEC_LOG_ADDR_INVALID) {
  244. hdmi_write_reg(core->base, HDMI_CEC_CA_7_0, 0);
  245. hdmi_write_reg(core->base, HDMI_CEC_CA_15_8, 0);
  246. return 0;
  247. }
  248. if (log_addr <= 7) {
  249. v = hdmi_read_reg(core->base, HDMI_CEC_CA_7_0);
  250. v |= 1 << log_addr;
  251. hdmi_write_reg(core->base, HDMI_CEC_CA_7_0, v);
  252. } else {
  253. v = hdmi_read_reg(core->base, HDMI_CEC_CA_15_8);
  254. v |= 1 << (log_addr - 8);
  255. hdmi_write_reg(core->base, HDMI_CEC_CA_15_8, v);
  256. }
  257. return 0;
  258. }
  259. static int hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
  260. u32 signal_free_time, struct cec_msg *msg)
  261. {
  262. struct hdmi_core_data *core = cec_get_drvdata(adap);
  263. int temp;
  264. u32 i;
  265. /* Clear TX FIFO */
  266. if (!hdmi_cec_clear_tx_fifo(adap)) {
  267. pr_err("cec-%s: could not clear TX FIFO for transmit\n",
  268. adap->name);
  269. return -EIO;
  270. }
  271. /* Clear TX interrupts */
  272. hdmi_write_reg(core->base, HDMI_CEC_INT_STATUS_0,
  273. HDMI_CEC_TX_FIFO_INT_MASK);
  274. hdmi_write_reg(core->base, HDMI_CEC_INT_STATUS_1,
  275. HDMI_CEC_RETRANSMIT_CNT_INT_MASK);
  276. /* Set the retry count */
  277. REG_FLD_MOD(core->base, HDMI_CEC_DBG_3, attempts - 1, 6, 4);
  278. /* Set the initiator addresses */
  279. hdmi_write_reg(core->base, HDMI_CEC_TX_INIT, cec_msg_initiator(msg));
  280. /* Set destination id */
  281. temp = cec_msg_destination(msg);
  282. if (msg->len == 1)
  283. temp |= 0x80;
  284. hdmi_write_reg(core->base, HDMI_CEC_TX_DEST, temp);
  285. if (msg->len == 1)
  286. return 0;
  287. /* Setup command and arguments for the command */
  288. hdmi_write_reg(core->base, HDMI_CEC_TX_COMMAND, msg->msg[1]);
  289. for (i = 0; i < msg->len - 2; i++)
  290. hdmi_write_reg(core->base, HDMI_CEC_TX_OPERAND + i * 4,
  291. msg->msg[2 + i]);
  292. /* Operand count */
  293. hdmi_write_reg(core->base, HDMI_CEC_TRANSMIT_DATA,
  294. (msg->len - 2) | 0x10);
  295. return 0;
  296. }
  297. static const struct cec_adap_ops hdmi_cec_adap_ops = {
  298. .adap_enable = hdmi_cec_adap_enable,
  299. .adap_log_addr = hdmi_cec_adap_log_addr,
  300. .adap_transmit = hdmi_cec_adap_transmit,
  301. };
  302. void hdmi4_cec_set_phys_addr(struct hdmi_core_data *core, u16 pa)
  303. {
  304. cec_s_phys_addr(core->adap, pa, false);
  305. }
  306. int hdmi4_cec_init(struct platform_device *pdev, struct hdmi_core_data *core,
  307. struct hdmi_wp_data *wp)
  308. {
  309. const u32 caps = CEC_CAP_TRANSMIT | CEC_CAP_LOG_ADDRS |
  310. CEC_CAP_PASSTHROUGH | CEC_CAP_RC;
  311. int ret;
  312. core->adap = cec_allocate_adapter(&hdmi_cec_adap_ops, core,
  313. "omap4", caps, CEC_MAX_LOG_ADDRS);
  314. ret = PTR_ERR_OR_ZERO(core->adap);
  315. if (ret < 0)
  316. return ret;
  317. core->wp = wp;
  318. /*
  319. * Initialize CEC clock divider: CEC needs 2MHz clock hence
  320. * set the devider to 24 to get 48/24=2MHz clock
  321. */
  322. REG_FLD_MOD(core->wp->base, HDMI_WP_CLK, 0x18, 5, 0);
  323. ret = cec_register_adapter(core->adap, &pdev->dev);
  324. if (ret < 0) {
  325. cec_delete_adapter(core->adap);
  326. return ret;
  327. }
  328. return 0;
  329. }
  330. void hdmi4_cec_uninit(struct hdmi_core_data *core)
  331. {
  332. cec_unregister_adapter(core->adap);
  333. }