stm32-cec.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * STM32 CEC driver
  3. * Copyright (C) STMicroelectronics SA 2017
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regmap.h>
  18. #include <media/cec.h>
  19. #define CEC_NAME "stm32-cec"
  20. /* CEC registers */
  21. #define CEC_CR 0x0000 /* Control Register */
  22. #define CEC_CFGR 0x0004 /* ConFiGuration Register */
  23. #define CEC_TXDR 0x0008 /* Rx data Register */
  24. #define CEC_RXDR 0x000C /* Rx data Register */
  25. #define CEC_ISR 0x0010 /* Interrupt and status Register */
  26. #define CEC_IER 0x0014 /* Interrupt enable Register */
  27. #define TXEOM BIT(2)
  28. #define TXSOM BIT(1)
  29. #define CECEN BIT(0)
  30. #define LSTN BIT(31)
  31. #define OAR GENMASK(30, 16)
  32. #define SFTOP BIT(8)
  33. #define BRDNOGEN BIT(7)
  34. #define LBPEGEN BIT(6)
  35. #define BREGEN BIT(5)
  36. #define BRESTP BIT(4)
  37. #define RXTOL BIT(3)
  38. #define SFT GENMASK(2, 0)
  39. #define FULL_CFG (LSTN | SFTOP | BRDNOGEN | LBPEGEN | BREGEN | BRESTP \
  40. | RXTOL)
  41. #define TXACKE BIT(12)
  42. #define TXERR BIT(11)
  43. #define TXUDR BIT(10)
  44. #define TXEND BIT(9)
  45. #define TXBR BIT(8)
  46. #define ARBLST BIT(7)
  47. #define RXACKE BIT(6)
  48. #define RXOVR BIT(2)
  49. #define RXEND BIT(1)
  50. #define RXBR BIT(0)
  51. #define ALL_TX_IT (TXEND | TXBR | TXACKE | TXERR | TXUDR | ARBLST)
  52. #define ALL_RX_IT (RXEND | RXBR | RXACKE | RXOVR)
  53. struct stm32_cec {
  54. struct cec_adapter *adap;
  55. struct device *dev;
  56. struct clk *clk_cec;
  57. struct clk *clk_hdmi_cec;
  58. struct reset_control *rstc;
  59. struct regmap *regmap;
  60. int irq;
  61. u32 irq_status;
  62. struct cec_msg rx_msg;
  63. struct cec_msg tx_msg;
  64. int tx_cnt;
  65. };
  66. static void cec_hw_init(struct stm32_cec *cec)
  67. {
  68. regmap_update_bits(cec->regmap, CEC_CR, TXEOM | TXSOM | CECEN, 0);
  69. regmap_update_bits(cec->regmap, CEC_IER, ALL_TX_IT | ALL_RX_IT,
  70. ALL_TX_IT | ALL_RX_IT);
  71. regmap_update_bits(cec->regmap, CEC_CFGR, FULL_CFG, FULL_CFG);
  72. }
  73. static void stm32_tx_done(struct stm32_cec *cec, u32 status)
  74. {
  75. if (status & (TXERR | TXUDR)) {
  76. cec_transmit_done(cec->adap, CEC_TX_STATUS_ERROR,
  77. 0, 0, 0, 1);
  78. return;
  79. }
  80. if (status & ARBLST) {
  81. cec_transmit_done(cec->adap, CEC_TX_STATUS_ARB_LOST,
  82. 1, 0, 0, 0);
  83. return;
  84. }
  85. if (status & TXACKE) {
  86. cec_transmit_done(cec->adap, CEC_TX_STATUS_NACK,
  87. 0, 1, 0, 0);
  88. return;
  89. }
  90. if (cec->irq_status & TXBR) {
  91. /* send next byte */
  92. if (cec->tx_cnt < cec->tx_msg.len)
  93. regmap_write(cec->regmap, CEC_TXDR,
  94. cec->tx_msg.msg[cec->tx_cnt++]);
  95. /* TXEOM is set to command transmission of the last byte */
  96. if (cec->tx_cnt == cec->tx_msg.len)
  97. regmap_update_bits(cec->regmap, CEC_CR, TXEOM, TXEOM);
  98. }
  99. if (cec->irq_status & TXEND)
  100. cec_transmit_done(cec->adap, CEC_TX_STATUS_OK, 0, 0, 0, 0);
  101. }
  102. static void stm32_rx_done(struct stm32_cec *cec, u32 status)
  103. {
  104. if (cec->irq_status & (RXACKE | RXOVR)) {
  105. cec->rx_msg.len = 0;
  106. return;
  107. }
  108. if (cec->irq_status & RXBR) {
  109. u32 val;
  110. regmap_read(cec->regmap, CEC_RXDR, &val);
  111. cec->rx_msg.msg[cec->rx_msg.len++] = val & 0xFF;
  112. }
  113. if (cec->irq_status & RXEND) {
  114. cec_received_msg(cec->adap, &cec->rx_msg);
  115. cec->rx_msg.len = 0;
  116. }
  117. }
  118. static irqreturn_t stm32_cec_irq_thread(int irq, void *arg)
  119. {
  120. struct stm32_cec *cec = arg;
  121. if (cec->irq_status & ALL_TX_IT)
  122. stm32_tx_done(cec, cec->irq_status);
  123. if (cec->irq_status & ALL_RX_IT)
  124. stm32_rx_done(cec, cec->irq_status);
  125. cec->irq_status = 0;
  126. return IRQ_HANDLED;
  127. }
  128. static irqreturn_t stm32_cec_irq_handler(int irq, void *arg)
  129. {
  130. struct stm32_cec *cec = arg;
  131. regmap_read(cec->regmap, CEC_ISR, &cec->irq_status);
  132. regmap_update_bits(cec->regmap, CEC_ISR,
  133. ALL_TX_IT | ALL_RX_IT,
  134. ALL_TX_IT | ALL_RX_IT);
  135. return IRQ_WAKE_THREAD;
  136. }
  137. static int stm32_cec_adap_enable(struct cec_adapter *adap, bool enable)
  138. {
  139. struct stm32_cec *cec = adap->priv;
  140. int ret = 0;
  141. if (enable) {
  142. ret = clk_enable(cec->clk_cec);
  143. if (ret)
  144. dev_err(cec->dev, "fail to enable cec clock\n");
  145. clk_enable(cec->clk_hdmi_cec);
  146. regmap_update_bits(cec->regmap, CEC_CR, CECEN, CECEN);
  147. } else {
  148. clk_disable(cec->clk_cec);
  149. clk_disable(cec->clk_hdmi_cec);
  150. regmap_update_bits(cec->regmap, CEC_CR, CECEN, 0);
  151. }
  152. return ret;
  153. }
  154. static int stm32_cec_adap_log_addr(struct cec_adapter *adap, u8 logical_addr)
  155. {
  156. struct stm32_cec *cec = adap->priv;
  157. u32 oar = (1 << logical_addr) << 16;
  158. regmap_update_bits(cec->regmap, CEC_CR, CECEN, 0);
  159. if (logical_addr == CEC_LOG_ADDR_INVALID)
  160. regmap_update_bits(cec->regmap, CEC_CFGR, OAR, 0);
  161. else
  162. regmap_update_bits(cec->regmap, CEC_CFGR, oar, oar);
  163. regmap_update_bits(cec->regmap, CEC_CR, CECEN, CECEN);
  164. return 0;
  165. }
  166. static int stm32_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
  167. u32 signal_free_time, struct cec_msg *msg)
  168. {
  169. struct stm32_cec *cec = adap->priv;
  170. /* Copy message */
  171. cec->tx_msg = *msg;
  172. cec->tx_cnt = 0;
  173. /*
  174. * If the CEC message consists of only one byte,
  175. * TXEOM must be set before of TXSOM.
  176. */
  177. if (cec->tx_msg.len == 1)
  178. regmap_update_bits(cec->regmap, CEC_CR, TXEOM, TXEOM);
  179. /* TXSOM is set to command transmission of the first byte */
  180. regmap_update_bits(cec->regmap, CEC_CR, TXSOM, TXSOM);
  181. /* Write the header (first byte of message) */
  182. regmap_write(cec->regmap, CEC_TXDR, cec->tx_msg.msg[0]);
  183. cec->tx_cnt++;
  184. return 0;
  185. }
  186. static const struct cec_adap_ops stm32_cec_adap_ops = {
  187. .adap_enable = stm32_cec_adap_enable,
  188. .adap_log_addr = stm32_cec_adap_log_addr,
  189. .adap_transmit = stm32_cec_adap_transmit,
  190. };
  191. static const struct regmap_config stm32_cec_regmap_cfg = {
  192. .reg_bits = 32,
  193. .val_bits = 32,
  194. .reg_stride = sizeof(u32),
  195. .max_register = 0x14,
  196. .fast_io = true,
  197. };
  198. static int stm32_cec_probe(struct platform_device *pdev)
  199. {
  200. u32 caps = CEC_CAP_DEFAULTS | CEC_CAP_PHYS_ADDR | CEC_MODE_MONITOR_ALL;
  201. struct resource *res;
  202. struct stm32_cec *cec;
  203. void __iomem *mmio;
  204. int ret;
  205. cec = devm_kzalloc(&pdev->dev, sizeof(*cec), GFP_KERNEL);
  206. if (!cec)
  207. return -ENOMEM;
  208. cec->dev = &pdev->dev;
  209. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  210. mmio = devm_ioremap_resource(&pdev->dev, res);
  211. if (IS_ERR(mmio))
  212. return PTR_ERR(mmio);
  213. cec->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "cec", mmio,
  214. &stm32_cec_regmap_cfg);
  215. if (IS_ERR(cec->regmap))
  216. return PTR_ERR(cec->regmap);
  217. cec->irq = platform_get_irq(pdev, 0);
  218. if (cec->irq < 0)
  219. return cec->irq;
  220. ret = devm_request_threaded_irq(&pdev->dev, cec->irq,
  221. stm32_cec_irq_handler,
  222. stm32_cec_irq_thread,
  223. 0,
  224. pdev->name, cec);
  225. if (ret)
  226. return ret;
  227. cec->clk_cec = devm_clk_get(&pdev->dev, "cec");
  228. if (IS_ERR(cec->clk_cec)) {
  229. dev_err(&pdev->dev, "Cannot get cec clock\n");
  230. return PTR_ERR(cec->clk_cec);
  231. }
  232. ret = clk_prepare(cec->clk_cec);
  233. if (ret) {
  234. dev_err(&pdev->dev, "Unable to prepare cec clock\n");
  235. return ret;
  236. }
  237. cec->clk_hdmi_cec = devm_clk_get(&pdev->dev, "hdmi-cec");
  238. if (!IS_ERR(cec->clk_hdmi_cec)) {
  239. ret = clk_prepare(cec->clk_hdmi_cec);
  240. if (ret) {
  241. dev_err(&pdev->dev, "Unable to prepare hdmi-cec clock\n");
  242. return ret;
  243. }
  244. }
  245. /*
  246. * CEC_CAP_PHYS_ADDR caps should be removed when a cec notifier is
  247. * available for example when a drm driver can provide edid
  248. */
  249. cec->adap = cec_allocate_adapter(&stm32_cec_adap_ops, cec,
  250. CEC_NAME, caps, CEC_MAX_LOG_ADDRS);
  251. ret = PTR_ERR_OR_ZERO(cec->adap);
  252. if (ret)
  253. return ret;
  254. ret = cec_register_adapter(cec->adap, &pdev->dev);
  255. if (ret) {
  256. cec_delete_adapter(cec->adap);
  257. return ret;
  258. }
  259. cec_hw_init(cec);
  260. platform_set_drvdata(pdev, cec);
  261. return 0;
  262. }
  263. static int stm32_cec_remove(struct platform_device *pdev)
  264. {
  265. struct stm32_cec *cec = platform_get_drvdata(pdev);
  266. clk_unprepare(cec->clk_cec);
  267. clk_unprepare(cec->clk_hdmi_cec);
  268. cec_unregister_adapter(cec->adap);
  269. return 0;
  270. }
  271. static const struct of_device_id stm32_cec_of_match[] = {
  272. { .compatible = "st,stm32-cec" },
  273. { /* end node */ }
  274. };
  275. MODULE_DEVICE_TABLE(of, stm32_cec_of_match);
  276. static struct platform_driver stm32_cec_driver = {
  277. .probe = stm32_cec_probe,
  278. .remove = stm32_cec_remove,
  279. .driver = {
  280. .name = CEC_NAME,
  281. .of_match_table = stm32_cec_of_match,
  282. },
  283. };
  284. module_platform_driver(stm32_cec_driver);
  285. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  286. MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
  287. MODULE_DESCRIPTION("STMicroelectronics STM32 Consumer Electronics Control");
  288. MODULE_LICENSE("GPL v2");