stm32-cec.c 8.2 KB

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