tegra_cec.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Tegra CEC implementation
  3. *
  4. * The original 3.10 CEC driver using a custom API:
  5. *
  6. * Copyright (c) 2012-2015, NVIDIA CORPORATION. All rights reserved.
  7. *
  8. * Conversion to the CEC framework and to the mainline kernel:
  9. *
  10. * Copyright 2016-2017 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms and conditions of the GNU General Public License,
  14. * version 2, as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  19. * more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/err.h>
  27. #include <linux/errno.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/slab.h>
  30. #include <linux/io.h>
  31. #include <linux/clk.h>
  32. #include <linux/delay.h>
  33. #include <linux/pm.h>
  34. #include <linux/of.h>
  35. #include <linux/of_platform.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/clk/tegra.h>
  38. #include <media/cec-notifier.h>
  39. #include "tegra_cec.h"
  40. #define TEGRA_CEC_NAME "tegra-cec"
  41. struct tegra_cec {
  42. struct cec_adapter *adap;
  43. struct device *dev;
  44. struct clk *clk;
  45. void __iomem *cec_base;
  46. struct cec_notifier *notifier;
  47. int tegra_cec_irq;
  48. bool rx_done;
  49. bool tx_done;
  50. int tx_status;
  51. u8 rx_buf[CEC_MAX_MSG_SIZE];
  52. u8 rx_buf_cnt;
  53. u32 tx_buf[CEC_MAX_MSG_SIZE];
  54. u8 tx_buf_cur;
  55. u8 tx_buf_cnt;
  56. };
  57. static inline u32 cec_read(struct tegra_cec *cec, u32 reg)
  58. {
  59. return readl(cec->cec_base + reg);
  60. }
  61. static inline void cec_write(struct tegra_cec *cec, u32 reg, u32 val)
  62. {
  63. writel(val, cec->cec_base + reg);
  64. }
  65. static void tegra_cec_error_recovery(struct tegra_cec *cec)
  66. {
  67. u32 hw_ctrl;
  68. hw_ctrl = cec_read(cec, TEGRA_CEC_HW_CONTROL);
  69. cec_write(cec, TEGRA_CEC_HW_CONTROL, 0);
  70. cec_write(cec, TEGRA_CEC_INT_STAT, 0xffffffff);
  71. cec_write(cec, TEGRA_CEC_HW_CONTROL, hw_ctrl);
  72. }
  73. static irqreturn_t tegra_cec_irq_thread_handler(int irq, void *data)
  74. {
  75. struct device *dev = data;
  76. struct tegra_cec *cec = dev_get_drvdata(dev);
  77. if (cec->tx_done) {
  78. cec_transmit_attempt_done(cec->adap, cec->tx_status);
  79. cec->tx_done = false;
  80. }
  81. if (cec->rx_done) {
  82. struct cec_msg msg = {};
  83. msg.len = cec->rx_buf_cnt;
  84. memcpy(msg.msg, cec->rx_buf, msg.len);
  85. cec_received_msg(cec->adap, &msg);
  86. cec->rx_done = false;
  87. cec->rx_buf_cnt = 0;
  88. }
  89. return IRQ_HANDLED;
  90. }
  91. static irqreturn_t tegra_cec_irq_handler(int irq, void *data)
  92. {
  93. struct device *dev = data;
  94. struct tegra_cec *cec = dev_get_drvdata(dev);
  95. u32 status, mask;
  96. status = cec_read(cec, TEGRA_CEC_INT_STAT);
  97. mask = cec_read(cec, TEGRA_CEC_INT_MASK);
  98. status &= mask;
  99. if (!status)
  100. return IRQ_HANDLED;
  101. if (status & TEGRA_CEC_INT_STAT_TX_REGISTER_UNDERRUN) {
  102. dev_err(dev, "TX underrun, interrupt timing issue!\n");
  103. tegra_cec_error_recovery(cec);
  104. cec_write(cec, TEGRA_CEC_INT_MASK,
  105. mask & ~TEGRA_CEC_INT_MASK_TX_REGISTER_EMPTY);
  106. cec->tx_done = true;
  107. cec->tx_status = CEC_TX_STATUS_ERROR;
  108. return IRQ_WAKE_THREAD;
  109. }
  110. if ((status & TEGRA_CEC_INT_STAT_TX_ARBITRATION_FAILED) ||
  111. (status & TEGRA_CEC_INT_STAT_TX_BUS_ANOMALY_DETECTED)) {
  112. tegra_cec_error_recovery(cec);
  113. cec_write(cec, TEGRA_CEC_INT_MASK,
  114. mask & ~TEGRA_CEC_INT_MASK_TX_REGISTER_EMPTY);
  115. cec->tx_done = true;
  116. if (status & TEGRA_CEC_INT_STAT_TX_BUS_ANOMALY_DETECTED)
  117. cec->tx_status = CEC_TX_STATUS_LOW_DRIVE;
  118. else
  119. cec->tx_status = CEC_TX_STATUS_ARB_LOST;
  120. return IRQ_WAKE_THREAD;
  121. }
  122. if (status & TEGRA_CEC_INT_STAT_TX_FRAME_TRANSMITTED) {
  123. cec_write(cec, TEGRA_CEC_INT_STAT,
  124. TEGRA_CEC_INT_STAT_TX_FRAME_TRANSMITTED);
  125. if (status & TEGRA_CEC_INT_STAT_TX_FRAME_OR_BLOCK_NAKD) {
  126. tegra_cec_error_recovery(cec);
  127. cec->tx_done = true;
  128. cec->tx_status = CEC_TX_STATUS_NACK;
  129. } else {
  130. cec->tx_done = true;
  131. cec->tx_status = CEC_TX_STATUS_OK;
  132. }
  133. return IRQ_WAKE_THREAD;
  134. }
  135. if (status & TEGRA_CEC_INT_STAT_TX_FRAME_OR_BLOCK_NAKD)
  136. dev_warn(dev, "TX NAKed on the fly!\n");
  137. if (status & TEGRA_CEC_INT_STAT_TX_REGISTER_EMPTY) {
  138. if (cec->tx_buf_cur == cec->tx_buf_cnt) {
  139. cec_write(cec, TEGRA_CEC_INT_MASK,
  140. mask & ~TEGRA_CEC_INT_MASK_TX_REGISTER_EMPTY);
  141. } else {
  142. cec_write(cec, TEGRA_CEC_TX_REGISTER,
  143. cec->tx_buf[cec->tx_buf_cur++]);
  144. cec_write(cec, TEGRA_CEC_INT_STAT,
  145. TEGRA_CEC_INT_STAT_TX_REGISTER_EMPTY);
  146. }
  147. }
  148. if (status & TEGRA_CEC_INT_STAT_RX_START_BIT_DETECTED) {
  149. cec_write(cec, TEGRA_CEC_INT_STAT,
  150. TEGRA_CEC_INT_STAT_RX_START_BIT_DETECTED);
  151. cec->rx_done = false;
  152. cec->rx_buf_cnt = 0;
  153. }
  154. if (status & TEGRA_CEC_INT_STAT_RX_REGISTER_FULL) {
  155. u32 v;
  156. cec_write(cec, TEGRA_CEC_INT_STAT,
  157. TEGRA_CEC_INT_STAT_RX_REGISTER_FULL);
  158. v = cec_read(cec, TEGRA_CEC_RX_REGISTER);
  159. if (cec->rx_buf_cnt < CEC_MAX_MSG_SIZE)
  160. cec->rx_buf[cec->rx_buf_cnt++] = v & 0xff;
  161. if (v & TEGRA_CEC_RX_REGISTER_EOM) {
  162. cec->rx_done = true;
  163. return IRQ_WAKE_THREAD;
  164. }
  165. }
  166. return IRQ_HANDLED;
  167. }
  168. static int tegra_cec_adap_enable(struct cec_adapter *adap, bool enable)
  169. {
  170. struct tegra_cec *cec = adap->priv;
  171. cec->rx_buf_cnt = 0;
  172. cec->tx_buf_cnt = 0;
  173. cec->tx_buf_cur = 0;
  174. cec_write(cec, TEGRA_CEC_HW_CONTROL, 0);
  175. cec_write(cec, TEGRA_CEC_INT_MASK, 0);
  176. cec_write(cec, TEGRA_CEC_INT_STAT, 0xffffffff);
  177. cec_write(cec, TEGRA_CEC_SW_CONTROL, 0);
  178. if (!enable)
  179. return 0;
  180. cec_write(cec, TEGRA_CEC_INPUT_FILTER, (1U << 31) | 0x20);
  181. cec_write(cec, TEGRA_CEC_RX_TIMING_0,
  182. (0x7a << TEGRA_CEC_RX_TIM0_START_BIT_MAX_LO_TIME_SHIFT) |
  183. (0x6d << TEGRA_CEC_RX_TIM0_START_BIT_MIN_LO_TIME_SHIFT) |
  184. (0x93 << TEGRA_CEC_RX_TIM0_START_BIT_MAX_DURATION_SHIFT) |
  185. (0x86 << TEGRA_CEC_RX_TIM0_START_BIT_MIN_DURATION_SHIFT));
  186. cec_write(cec, TEGRA_CEC_RX_TIMING_1,
  187. (0x35 << TEGRA_CEC_RX_TIM1_DATA_BIT_MAX_LO_TIME_SHIFT) |
  188. (0x21 << TEGRA_CEC_RX_TIM1_DATA_BIT_SAMPLE_TIME_SHIFT) |
  189. (0x56 << TEGRA_CEC_RX_TIM1_DATA_BIT_MAX_DURATION_SHIFT) |
  190. (0x40 << TEGRA_CEC_RX_TIM1_DATA_BIT_MIN_DURATION_SHIFT));
  191. cec_write(cec, TEGRA_CEC_RX_TIMING_2,
  192. (0x50 << TEGRA_CEC_RX_TIM2_END_OF_BLOCK_TIME_SHIFT));
  193. cec_write(cec, TEGRA_CEC_TX_TIMING_0,
  194. (0x74 << TEGRA_CEC_TX_TIM0_START_BIT_LO_TIME_SHIFT) |
  195. (0x8d << TEGRA_CEC_TX_TIM0_START_BIT_DURATION_SHIFT) |
  196. (0x08 << TEGRA_CEC_TX_TIM0_BUS_XITION_TIME_SHIFT) |
  197. (0x71 << TEGRA_CEC_TX_TIM0_BUS_ERROR_LO_TIME_SHIFT));
  198. cec_write(cec, TEGRA_CEC_TX_TIMING_1,
  199. (0x2f << TEGRA_CEC_TX_TIM1_LO_DATA_BIT_LO_TIME_SHIFT) |
  200. (0x13 << TEGRA_CEC_TX_TIM1_HI_DATA_BIT_LO_TIME_SHIFT) |
  201. (0x4b << TEGRA_CEC_TX_TIM1_DATA_BIT_DURATION_SHIFT) |
  202. (0x21 << TEGRA_CEC_TX_TIM1_ACK_NAK_BIT_SAMPLE_TIME_SHIFT));
  203. cec_write(cec, TEGRA_CEC_TX_TIMING_2,
  204. (0x07 << TEGRA_CEC_TX_TIM2_BUS_IDLE_TIME_ADDITIONAL_FRAME_SHIFT) |
  205. (0x05 << TEGRA_CEC_TX_TIM2_BUS_IDLE_TIME_NEW_FRAME_SHIFT) |
  206. (0x03 << TEGRA_CEC_TX_TIM2_BUS_IDLE_TIME_RETRY_FRAME_SHIFT));
  207. cec_write(cec, TEGRA_CEC_INT_MASK,
  208. TEGRA_CEC_INT_MASK_TX_REGISTER_UNDERRUN |
  209. TEGRA_CEC_INT_MASK_TX_FRAME_OR_BLOCK_NAKD |
  210. TEGRA_CEC_INT_MASK_TX_ARBITRATION_FAILED |
  211. TEGRA_CEC_INT_MASK_TX_BUS_ANOMALY_DETECTED |
  212. TEGRA_CEC_INT_MASK_TX_FRAME_TRANSMITTED |
  213. TEGRA_CEC_INT_MASK_RX_REGISTER_FULL |
  214. TEGRA_CEC_INT_MASK_RX_START_BIT_DETECTED);
  215. cec_write(cec, TEGRA_CEC_HW_CONTROL, TEGRA_CEC_HWCTRL_TX_RX_MODE);
  216. return 0;
  217. }
  218. static int tegra_cec_adap_log_addr(struct cec_adapter *adap, u8 logical_addr)
  219. {
  220. struct tegra_cec *cec = adap->priv;
  221. u32 state = cec_read(cec, TEGRA_CEC_HW_CONTROL);
  222. if (logical_addr == CEC_LOG_ADDR_INVALID)
  223. state &= ~TEGRA_CEC_HWCTRL_RX_LADDR_MASK;
  224. else
  225. state |= TEGRA_CEC_HWCTRL_RX_LADDR((1 << logical_addr));
  226. cec_write(cec, TEGRA_CEC_HW_CONTROL, state);
  227. return 0;
  228. }
  229. static int tegra_cec_adap_monitor_all_enable(struct cec_adapter *adap,
  230. bool enable)
  231. {
  232. struct tegra_cec *cec = adap->priv;
  233. u32 reg = cec_read(cec, TEGRA_CEC_HW_CONTROL);
  234. if (enable)
  235. reg |= TEGRA_CEC_HWCTRL_RX_SNOOP;
  236. else
  237. reg &= ~TEGRA_CEC_HWCTRL_RX_SNOOP;
  238. cec_write(cec, TEGRA_CEC_HW_CONTROL, reg);
  239. return 0;
  240. }
  241. static int tegra_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
  242. u32 signal_free_time_ms, struct cec_msg *msg)
  243. {
  244. bool retry_xfer = signal_free_time_ms == CEC_SIGNAL_FREE_TIME_RETRY;
  245. struct tegra_cec *cec = adap->priv;
  246. unsigned int i;
  247. u32 mode = 0;
  248. u32 mask;
  249. if (cec_msg_is_broadcast(msg))
  250. mode = TEGRA_CEC_TX_REG_BCAST;
  251. cec->tx_buf_cur = 0;
  252. cec->tx_buf_cnt = msg->len;
  253. for (i = 0; i < msg->len; i++) {
  254. cec->tx_buf[i] = mode | msg->msg[i];
  255. if (i == 0)
  256. cec->tx_buf[i] |= TEGRA_CEC_TX_REG_START_BIT;
  257. if (i == msg->len - 1)
  258. cec->tx_buf[i] |= TEGRA_CEC_TX_REG_EOM;
  259. if (i == 0 && retry_xfer)
  260. cec->tx_buf[i] |= TEGRA_CEC_TX_REG_RETRY;
  261. }
  262. mask = cec_read(cec, TEGRA_CEC_INT_MASK);
  263. cec_write(cec, TEGRA_CEC_INT_MASK,
  264. mask | TEGRA_CEC_INT_MASK_TX_REGISTER_EMPTY);
  265. return 0;
  266. }
  267. static const struct cec_adap_ops tegra_cec_ops = {
  268. .adap_enable = tegra_cec_adap_enable,
  269. .adap_log_addr = tegra_cec_adap_log_addr,
  270. .adap_transmit = tegra_cec_adap_transmit,
  271. .adap_monitor_all_enable = tegra_cec_adap_monitor_all_enable,
  272. };
  273. static int tegra_cec_probe(struct platform_device *pdev)
  274. {
  275. struct platform_device *hdmi_dev;
  276. struct device_node *np;
  277. struct tegra_cec *cec;
  278. struct resource *res;
  279. int ret = 0;
  280. np = of_parse_phandle(pdev->dev.of_node, "hdmi-phandle", 0);
  281. if (!np) {
  282. dev_err(&pdev->dev, "Failed to find hdmi node in device tree\n");
  283. return -ENODEV;
  284. }
  285. hdmi_dev = of_find_device_by_node(np);
  286. if (hdmi_dev == NULL)
  287. return -EPROBE_DEFER;
  288. cec = devm_kzalloc(&pdev->dev, sizeof(struct tegra_cec), GFP_KERNEL);
  289. if (!cec)
  290. return -ENOMEM;
  291. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  292. if (!res) {
  293. dev_err(&pdev->dev,
  294. "Unable to allocate resources for device\n");
  295. return -EBUSY;
  296. }
  297. if (!devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
  298. pdev->name)) {
  299. dev_err(&pdev->dev,
  300. "Unable to request mem region for device\n");
  301. return -EBUSY;
  302. }
  303. cec->tegra_cec_irq = platform_get_irq(pdev, 0);
  304. if (cec->tegra_cec_irq <= 0)
  305. return -EBUSY;
  306. cec->cec_base = devm_ioremap_nocache(&pdev->dev, res->start,
  307. resource_size(res));
  308. if (!cec->cec_base) {
  309. dev_err(&pdev->dev, "Unable to grab IOs for device\n");
  310. return -EBUSY;
  311. }
  312. cec->clk = devm_clk_get(&pdev->dev, "cec");
  313. if (IS_ERR_OR_NULL(cec->clk)) {
  314. dev_err(&pdev->dev, "Can't get clock for CEC\n");
  315. return -ENOENT;
  316. }
  317. clk_prepare_enable(cec->clk);
  318. /* set context info. */
  319. cec->dev = &pdev->dev;
  320. platform_set_drvdata(pdev, cec);
  321. ret = devm_request_threaded_irq(&pdev->dev, cec->tegra_cec_irq,
  322. tegra_cec_irq_handler, tegra_cec_irq_thread_handler,
  323. 0, "cec_irq", &pdev->dev);
  324. if (ret) {
  325. dev_err(&pdev->dev,
  326. "Unable to request interrupt for device\n");
  327. goto clk_error;
  328. }
  329. cec->notifier = cec_notifier_get(&hdmi_dev->dev);
  330. if (!cec->notifier) {
  331. ret = -ENOMEM;
  332. goto clk_error;
  333. }
  334. cec->adap = cec_allocate_adapter(&tegra_cec_ops, cec, TEGRA_CEC_NAME,
  335. CEC_CAP_DEFAULTS | CEC_CAP_MONITOR_ALL,
  336. CEC_MAX_LOG_ADDRS);
  337. if (IS_ERR(cec->adap)) {
  338. ret = -ENOMEM;
  339. dev_err(&pdev->dev, "Couldn't create cec adapter\n");
  340. goto cec_error;
  341. }
  342. ret = cec_register_adapter(cec->adap, &pdev->dev);
  343. if (ret) {
  344. dev_err(&pdev->dev, "Couldn't register device\n");
  345. goto cec_error;
  346. }
  347. cec_register_cec_notifier(cec->adap, cec->notifier);
  348. return 0;
  349. cec_error:
  350. if (cec->notifier)
  351. cec_notifier_put(cec->notifier);
  352. cec_delete_adapter(cec->adap);
  353. clk_error:
  354. clk_disable_unprepare(cec->clk);
  355. return ret;
  356. }
  357. static int tegra_cec_remove(struct platform_device *pdev)
  358. {
  359. struct tegra_cec *cec = platform_get_drvdata(pdev);
  360. clk_disable_unprepare(cec->clk);
  361. cec_unregister_adapter(cec->adap);
  362. cec_notifier_put(cec->notifier);
  363. return 0;
  364. }
  365. #ifdef CONFIG_PM
  366. static int tegra_cec_suspend(struct platform_device *pdev, pm_message_t state)
  367. {
  368. struct tegra_cec *cec = platform_get_drvdata(pdev);
  369. clk_disable_unprepare(cec->clk);
  370. dev_notice(&pdev->dev, "suspended\n");
  371. return 0;
  372. }
  373. static int tegra_cec_resume(struct platform_device *pdev)
  374. {
  375. struct tegra_cec *cec = platform_get_drvdata(pdev);
  376. dev_notice(&pdev->dev, "Resuming\n");
  377. clk_prepare_enable(cec->clk);
  378. return 0;
  379. }
  380. #endif
  381. static const struct of_device_id tegra_cec_of_match[] = {
  382. { .compatible = "nvidia,tegra114-cec", },
  383. { .compatible = "nvidia,tegra124-cec", },
  384. { .compatible = "nvidia,tegra210-cec", },
  385. {},
  386. };
  387. static struct platform_driver tegra_cec_driver = {
  388. .driver = {
  389. .name = TEGRA_CEC_NAME,
  390. .of_match_table = of_match_ptr(tegra_cec_of_match),
  391. },
  392. .probe = tegra_cec_probe,
  393. .remove = tegra_cec_remove,
  394. #ifdef CONFIG_PM
  395. .suspend = tegra_cec_suspend,
  396. .resume = tegra_cec_resume,
  397. #endif
  398. };
  399. module_platform_driver(tegra_cec_driver);
  400. MODULE_DESCRIPTION("Tegra HDMI CEC driver");
  401. MODULE_AUTHOR("NVIDIA CORPORATION");
  402. MODULE_AUTHOR("Cisco Systems, Inc. and/or its affiliates");
  403. MODULE_LICENSE("GPL v2");