mtk_hdmi_ddc.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Copyright (c) 2014 MediaTek Inc.
  3. * Author: Jie Qiu <jie.qiu@mediatek.com>
  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 version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/i2c.h>
  17. #include <linux/time.h>
  18. #include <linux/delay.h>
  19. #include <linux/errno.h>
  20. #include <linux/err.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/clk.h>
  23. #include <linux/slab.h>
  24. #include <linux/io.h>
  25. #include <linux/iopoll.h>
  26. #include <linux/of_address.h>
  27. #include <linux/of_irq.h>
  28. #include <linux/of_platform.h>
  29. #define SIF1_CLOK (288)
  30. #define DDC_DDCMCTL0 (0x0)
  31. #define DDCM_ODRAIN BIT(31)
  32. #define DDCM_CLK_DIV_OFFSET (16)
  33. #define DDCM_CLK_DIV_MASK (0xfff << 16)
  34. #define DDCM_CS_STATUS BIT(4)
  35. #define DDCM_SCL_STATE BIT(3)
  36. #define DDCM_SDA_STATE BIT(2)
  37. #define DDCM_SM0EN BIT(1)
  38. #define DDCM_SCL_STRECH BIT(0)
  39. #define DDC_DDCMCTL1 (0x4)
  40. #define DDCM_ACK_OFFSET (16)
  41. #define DDCM_ACK_MASK (0xff << 16)
  42. #define DDCM_PGLEN_OFFSET (8)
  43. #define DDCM_PGLEN_MASK (0x7 << 8)
  44. #define DDCM_SIF_MODE_OFFSET (4)
  45. #define DDCM_SIF_MODE_MASK (0x7 << 4)
  46. #define DDCM_START (0x1)
  47. #define DDCM_WRITE_DATA (0x2)
  48. #define DDCM_STOP (0x3)
  49. #define DDCM_READ_DATA_NO_ACK (0x4)
  50. #define DDCM_READ_DATA_ACK (0x5)
  51. #define DDCM_TRI BIT(0)
  52. #define DDC_DDCMD0 (0x8)
  53. #define DDCM_DATA3 (0xff << 24)
  54. #define DDCM_DATA2 (0xff << 16)
  55. #define DDCM_DATA1 (0xff << 8)
  56. #define DDCM_DATA0 (0xff << 0)
  57. #define DDC_DDCMD1 (0xc)
  58. #define DDCM_DATA7 (0xff << 24)
  59. #define DDCM_DATA6 (0xff << 16)
  60. #define DDCM_DATA5 (0xff << 8)
  61. #define DDCM_DATA4 (0xff << 0)
  62. struct mtk_hdmi_ddc {
  63. struct i2c_adapter adap;
  64. struct clk *clk;
  65. void __iomem *regs;
  66. };
  67. static inline void sif_set_bit(struct mtk_hdmi_ddc *ddc, unsigned int offset,
  68. unsigned int val)
  69. {
  70. writel(readl(ddc->regs + offset) | val, ddc->regs + offset);
  71. }
  72. static inline void sif_clr_bit(struct mtk_hdmi_ddc *ddc, unsigned int offset,
  73. unsigned int val)
  74. {
  75. writel(readl(ddc->regs + offset) & ~val, ddc->regs + offset);
  76. }
  77. static inline bool sif_bit_is_set(struct mtk_hdmi_ddc *ddc, unsigned int offset,
  78. unsigned int val)
  79. {
  80. return (readl(ddc->regs + offset) & val) == val;
  81. }
  82. static inline void sif_write_mask(struct mtk_hdmi_ddc *ddc, unsigned int offset,
  83. unsigned int mask, unsigned int shift,
  84. unsigned int val)
  85. {
  86. unsigned int tmp;
  87. tmp = readl(ddc->regs + offset);
  88. tmp &= ~mask;
  89. tmp |= (val << shift) & mask;
  90. writel(tmp, ddc->regs + offset);
  91. }
  92. static inline unsigned int sif_read_mask(struct mtk_hdmi_ddc *ddc,
  93. unsigned int offset, unsigned int mask,
  94. unsigned int shift)
  95. {
  96. return (readl(ddc->regs + offset) & mask) >> shift;
  97. }
  98. static void ddcm_trigger_mode(struct mtk_hdmi_ddc *ddc, int mode)
  99. {
  100. u32 val;
  101. sif_write_mask(ddc, DDC_DDCMCTL1, DDCM_SIF_MODE_MASK,
  102. DDCM_SIF_MODE_OFFSET, mode);
  103. sif_set_bit(ddc, DDC_DDCMCTL1, DDCM_TRI);
  104. readl_poll_timeout(ddc->regs + DDC_DDCMCTL1, val,
  105. (val & DDCM_TRI) != DDCM_TRI, 4, 20000);
  106. }
  107. static int mtk_hdmi_ddc_read_msg(struct mtk_hdmi_ddc *ddc, struct i2c_msg *msg)
  108. {
  109. struct device *dev = ddc->adap.dev.parent;
  110. u32 remain_count, ack_count, ack_final, read_count, temp_count;
  111. u32 index = 0;
  112. u32 ack;
  113. int i;
  114. ddcm_trigger_mode(ddc, DDCM_START);
  115. sif_write_mask(ddc, DDC_DDCMD0, 0xff, 0, (msg->addr << 1) | 0x01);
  116. sif_write_mask(ddc, DDC_DDCMCTL1, DDCM_PGLEN_MASK, DDCM_PGLEN_OFFSET,
  117. 0x00);
  118. ddcm_trigger_mode(ddc, DDCM_WRITE_DATA);
  119. ack = sif_read_mask(ddc, DDC_DDCMCTL1, DDCM_ACK_MASK, DDCM_ACK_OFFSET);
  120. dev_dbg(dev, "ack = 0x%x\n", ack);
  121. if (ack != 0x01) {
  122. dev_err(dev, "i2c ack err!\n");
  123. return -ENXIO;
  124. }
  125. remain_count = msg->len;
  126. ack_count = (msg->len - 1) / 8;
  127. ack_final = 0;
  128. while (remain_count > 0) {
  129. if (ack_count > 0) {
  130. read_count = 8;
  131. ack_final = 0;
  132. ack_count--;
  133. } else {
  134. read_count = remain_count;
  135. ack_final = 1;
  136. }
  137. sif_write_mask(ddc, DDC_DDCMCTL1, DDCM_PGLEN_MASK,
  138. DDCM_PGLEN_OFFSET, read_count - 1);
  139. ddcm_trigger_mode(ddc, (ack_final == 1) ?
  140. DDCM_READ_DATA_NO_ACK :
  141. DDCM_READ_DATA_ACK);
  142. ack = sif_read_mask(ddc, DDC_DDCMCTL1, DDCM_ACK_MASK,
  143. DDCM_ACK_OFFSET);
  144. temp_count = 0;
  145. while (((ack & (1 << temp_count)) != 0) && (temp_count < 8))
  146. temp_count++;
  147. if (((ack_final == 1) && (temp_count != (read_count - 1))) ||
  148. ((ack_final == 0) && (temp_count != read_count))) {
  149. dev_err(dev, "Address NACK! ACK(0x%x)\n", ack);
  150. break;
  151. }
  152. for (i = read_count; i >= 1; i--) {
  153. int shift;
  154. int offset;
  155. if (i > 4) {
  156. offset = DDC_DDCMD1;
  157. shift = (i - 5) * 8;
  158. } else {
  159. offset = DDC_DDCMD0;
  160. shift = (i - 1) * 8;
  161. }
  162. msg->buf[index + i - 1] = sif_read_mask(ddc, offset,
  163. 0xff << shift,
  164. shift);
  165. }
  166. remain_count -= read_count;
  167. index += read_count;
  168. }
  169. return 0;
  170. }
  171. static int mtk_hdmi_ddc_write_msg(struct mtk_hdmi_ddc *ddc, struct i2c_msg *msg)
  172. {
  173. struct device *dev = ddc->adap.dev.parent;
  174. u32 ack;
  175. ddcm_trigger_mode(ddc, DDCM_START);
  176. sif_write_mask(ddc, DDC_DDCMD0, DDCM_DATA0, 0, msg->addr << 1);
  177. sif_write_mask(ddc, DDC_DDCMD0, DDCM_DATA1, 8, msg->buf[0]);
  178. sif_write_mask(ddc, DDC_DDCMCTL1, DDCM_PGLEN_MASK, DDCM_PGLEN_OFFSET,
  179. 0x1);
  180. ddcm_trigger_mode(ddc, DDCM_WRITE_DATA);
  181. ack = sif_read_mask(ddc, DDC_DDCMCTL1, DDCM_ACK_MASK, DDCM_ACK_OFFSET);
  182. dev_dbg(dev, "ack = %d\n", ack);
  183. if (ack != 0x03) {
  184. dev_err(dev, "i2c ack err!\n");
  185. return -EIO;
  186. }
  187. return 0;
  188. }
  189. static int mtk_hdmi_ddc_xfer(struct i2c_adapter *adapter,
  190. struct i2c_msg *msgs, int num)
  191. {
  192. struct mtk_hdmi_ddc *ddc = adapter->algo_data;
  193. struct device *dev = adapter->dev.parent;
  194. int ret;
  195. int i;
  196. if (!ddc) {
  197. dev_err(dev, "invalid arguments\n");
  198. return -EINVAL;
  199. }
  200. sif_set_bit(ddc, DDC_DDCMCTL0, DDCM_SCL_STRECH);
  201. sif_set_bit(ddc, DDC_DDCMCTL0, DDCM_SM0EN);
  202. sif_clr_bit(ddc, DDC_DDCMCTL0, DDCM_ODRAIN);
  203. if (sif_bit_is_set(ddc, DDC_DDCMCTL1, DDCM_TRI)) {
  204. dev_err(dev, "ddc line is busy!\n");
  205. return -EBUSY;
  206. }
  207. sif_write_mask(ddc, DDC_DDCMCTL0, DDCM_CLK_DIV_MASK,
  208. DDCM_CLK_DIV_OFFSET, SIF1_CLOK);
  209. for (i = 0; i < num; i++) {
  210. struct i2c_msg *msg = &msgs[i];
  211. dev_dbg(dev, "i2c msg, adr:0x%x, flags:%d, len :0x%x\n",
  212. msg->addr, msg->flags, msg->len);
  213. if (msg->flags & I2C_M_RD)
  214. ret = mtk_hdmi_ddc_read_msg(ddc, msg);
  215. else
  216. ret = mtk_hdmi_ddc_write_msg(ddc, msg);
  217. if (ret < 0)
  218. goto xfer_end;
  219. }
  220. ddcm_trigger_mode(ddc, DDCM_STOP);
  221. return i;
  222. xfer_end:
  223. ddcm_trigger_mode(ddc, DDCM_STOP);
  224. dev_err(dev, "ddc failed!\n");
  225. return ret;
  226. }
  227. static u32 mtk_hdmi_ddc_func(struct i2c_adapter *adapter)
  228. {
  229. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  230. }
  231. static const struct i2c_algorithm mtk_hdmi_ddc_algorithm = {
  232. .master_xfer = mtk_hdmi_ddc_xfer,
  233. .functionality = mtk_hdmi_ddc_func,
  234. };
  235. static int mtk_hdmi_ddc_probe(struct platform_device *pdev)
  236. {
  237. struct device *dev = &pdev->dev;
  238. struct mtk_hdmi_ddc *ddc;
  239. struct resource *mem;
  240. int ret;
  241. ddc = devm_kzalloc(dev, sizeof(struct mtk_hdmi_ddc), GFP_KERNEL);
  242. if (!ddc)
  243. return -ENOMEM;
  244. ddc->clk = devm_clk_get(dev, "ddc-i2c");
  245. if (IS_ERR(ddc->clk)) {
  246. dev_err(dev, "get ddc_clk failed: %p ,\n", ddc->clk);
  247. return PTR_ERR(ddc->clk);
  248. }
  249. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  250. ddc->regs = devm_ioremap_resource(&pdev->dev, mem);
  251. if (IS_ERR(ddc->regs))
  252. return PTR_ERR(ddc->regs);
  253. ret = clk_prepare_enable(ddc->clk);
  254. if (ret) {
  255. dev_err(dev, "enable ddc clk failed!\n");
  256. return ret;
  257. }
  258. strlcpy(ddc->adap.name, "mediatek-hdmi-ddc", sizeof(ddc->adap.name));
  259. ddc->adap.owner = THIS_MODULE;
  260. ddc->adap.class = I2C_CLASS_DDC;
  261. ddc->adap.algo = &mtk_hdmi_ddc_algorithm;
  262. ddc->adap.retries = 3;
  263. ddc->adap.dev.of_node = dev->of_node;
  264. ddc->adap.algo_data = ddc;
  265. ddc->adap.dev.parent = &pdev->dev;
  266. ret = i2c_add_adapter(&ddc->adap);
  267. if (ret < 0) {
  268. dev_err(dev, "failed to add bus to i2c core\n");
  269. goto err_clk_disable;
  270. }
  271. platform_set_drvdata(pdev, ddc);
  272. dev_dbg(dev, "ddc->adap: %p\n", &ddc->adap);
  273. dev_dbg(dev, "ddc->clk: %p\n", ddc->clk);
  274. dev_dbg(dev, "physical adr: %pa, end: %pa\n", &mem->start,
  275. &mem->end);
  276. return 0;
  277. err_clk_disable:
  278. clk_disable_unprepare(ddc->clk);
  279. return ret;
  280. }
  281. static int mtk_hdmi_ddc_remove(struct platform_device *pdev)
  282. {
  283. struct mtk_hdmi_ddc *ddc = platform_get_drvdata(pdev);
  284. i2c_del_adapter(&ddc->adap);
  285. clk_disable_unprepare(ddc->clk);
  286. return 0;
  287. }
  288. static const struct of_device_id mtk_hdmi_ddc_match[] = {
  289. { .compatible = "mediatek,mt8173-hdmi-ddc", },
  290. {},
  291. };
  292. struct platform_driver mtk_hdmi_ddc_driver = {
  293. .probe = mtk_hdmi_ddc_probe,
  294. .remove = mtk_hdmi_ddc_remove,
  295. .driver = {
  296. .name = "mediatek-hdmi-ddc",
  297. .of_match_table = mtk_hdmi_ddc_match,
  298. },
  299. };
  300. MODULE_AUTHOR("Jie Qiu <jie.qiu@mediatek.com>");
  301. MODULE_DESCRIPTION("MediaTek HDMI DDC Driver");
  302. MODULE_LICENSE("GPL v2");