mtk_mipi_tx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * Copyright (c) 2015 MediaTek Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/clk-provider.h>
  15. #include <linux/delay.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/phy/phy.h>
  20. #define MIPITX_DSI_CON 0x00
  21. #define RG_DSI_LDOCORE_EN BIT(0)
  22. #define RG_DSI_CKG_LDOOUT_EN BIT(1)
  23. #define RG_DSI_BCLK_SEL (3 << 2)
  24. #define RG_DSI_LD_IDX_SEL (7 << 4)
  25. #define RG_DSI_PHYCLK_SEL (2 << 8)
  26. #define RG_DSI_DSICLK_FREQ_SEL BIT(10)
  27. #define RG_DSI_LPTX_CLMP_EN BIT(11)
  28. #define MIPITX_DSI_CLOCK_LANE 0x04
  29. #define MIPITX_DSI_DATA_LANE0 0x08
  30. #define MIPITX_DSI_DATA_LANE1 0x0c
  31. #define MIPITX_DSI_DATA_LANE2 0x10
  32. #define MIPITX_DSI_DATA_LANE3 0x14
  33. #define RG_DSI_LNTx_LDOOUT_EN BIT(0)
  34. #define RG_DSI_LNTx_CKLANE_EN BIT(1)
  35. #define RG_DSI_LNTx_LPTX_IPLUS1 BIT(2)
  36. #define RG_DSI_LNTx_LPTX_IPLUS2 BIT(3)
  37. #define RG_DSI_LNTx_LPTX_IMINUS BIT(4)
  38. #define RG_DSI_LNTx_LPCD_IPLUS BIT(5)
  39. #define RG_DSI_LNTx_LPCD_IMINUS BIT(6)
  40. #define RG_DSI_LNTx_RT_CODE (0xf << 8)
  41. #define MIPITX_DSI_TOP_CON 0x40
  42. #define RG_DSI_LNT_INTR_EN BIT(0)
  43. #define RG_DSI_LNT_HS_BIAS_EN BIT(1)
  44. #define RG_DSI_LNT_IMP_CAL_EN BIT(2)
  45. #define RG_DSI_LNT_TESTMODE_EN BIT(3)
  46. #define RG_DSI_LNT_IMP_CAL_CODE (0xf << 4)
  47. #define RG_DSI_LNT_AIO_SEL (7 << 8)
  48. #define RG_DSI_PAD_TIE_LOW_EN BIT(11)
  49. #define RG_DSI_DEBUG_INPUT_EN BIT(12)
  50. #define RG_DSI_PRESERVE (7 << 13)
  51. #define MIPITX_DSI_BG_CON 0x44
  52. #define RG_DSI_BG_CORE_EN BIT(0)
  53. #define RG_DSI_BG_CKEN BIT(1)
  54. #define RG_DSI_BG_DIV (0x3 << 2)
  55. #define RG_DSI_BG_FAST_CHARGE BIT(4)
  56. #define RG_DSI_VOUT_MSK (0x3ffff << 5)
  57. #define RG_DSI_V12_SEL (7 << 5)
  58. #define RG_DSI_V10_SEL (7 << 8)
  59. #define RG_DSI_V072_SEL (7 << 11)
  60. #define RG_DSI_V04_SEL (7 << 14)
  61. #define RG_DSI_V032_SEL (7 << 17)
  62. #define RG_DSI_V02_SEL (7 << 20)
  63. #define RG_DSI_BG_R1_TRIM (0xf << 24)
  64. #define RG_DSI_BG_R2_TRIM (0xf << 28)
  65. #define MIPITX_DSI_PLL_CON0 0x50
  66. #define RG_DSI_MPPLL_PLL_EN BIT(0)
  67. #define RG_DSI_MPPLL_DIV_MSK (0x1ff << 1)
  68. #define RG_DSI_MPPLL_PREDIV (3 << 1)
  69. #define RG_DSI_MPPLL_TXDIV0 (3 << 3)
  70. #define RG_DSI_MPPLL_TXDIV1 (3 << 5)
  71. #define RG_DSI_MPPLL_POSDIV (7 << 7)
  72. #define RG_DSI_MPPLL_MONVC_EN BIT(10)
  73. #define RG_DSI_MPPLL_MONREF_EN BIT(11)
  74. #define RG_DSI_MPPLL_VOD_EN BIT(12)
  75. #define MIPITX_DSI_PLL_CON1 0x54
  76. #define RG_DSI_MPPLL_SDM_FRA_EN BIT(0)
  77. #define RG_DSI_MPPLL_SDM_SSC_PH_INIT BIT(1)
  78. #define RG_DSI_MPPLL_SDM_SSC_EN BIT(2)
  79. #define RG_DSI_MPPLL_SDM_SSC_PRD (0xffff << 16)
  80. #define MIPITX_DSI_PLL_CON2 0x58
  81. #define MIPITX_DSI_PLL_PWR 0x68
  82. #define RG_DSI_MPPLL_SDM_PWR_ON BIT(0)
  83. #define RG_DSI_MPPLL_SDM_ISO_EN BIT(1)
  84. #define RG_DSI_MPPLL_SDM_PWR_ACK BIT(8)
  85. #define MIPITX_DSI_SW_CTRL 0x80
  86. #define SW_CTRL_EN BIT(0)
  87. #define MIPITX_DSI_SW_CTRL_CON0 0x84
  88. #define SW_LNTC_LPTX_PRE_OE BIT(0)
  89. #define SW_LNTC_LPTX_OE BIT(1)
  90. #define SW_LNTC_LPTX_P BIT(2)
  91. #define SW_LNTC_LPTX_N BIT(3)
  92. #define SW_LNTC_HSTX_PRE_OE BIT(4)
  93. #define SW_LNTC_HSTX_OE BIT(5)
  94. #define SW_LNTC_HSTX_ZEROCLK BIT(6)
  95. #define SW_LNT0_LPTX_PRE_OE BIT(7)
  96. #define SW_LNT0_LPTX_OE BIT(8)
  97. #define SW_LNT0_LPTX_P BIT(9)
  98. #define SW_LNT0_LPTX_N BIT(10)
  99. #define SW_LNT0_HSTX_PRE_OE BIT(11)
  100. #define SW_LNT0_HSTX_OE BIT(12)
  101. #define SW_LNT0_LPRX_EN BIT(13)
  102. #define SW_LNT1_LPTX_PRE_OE BIT(14)
  103. #define SW_LNT1_LPTX_OE BIT(15)
  104. #define SW_LNT1_LPTX_P BIT(16)
  105. #define SW_LNT1_LPTX_N BIT(17)
  106. #define SW_LNT1_HSTX_PRE_OE BIT(18)
  107. #define SW_LNT1_HSTX_OE BIT(19)
  108. #define SW_LNT2_LPTX_PRE_OE BIT(20)
  109. #define SW_LNT2_LPTX_OE BIT(21)
  110. #define SW_LNT2_LPTX_P BIT(22)
  111. #define SW_LNT2_LPTX_N BIT(23)
  112. #define SW_LNT2_HSTX_PRE_OE BIT(24)
  113. #define SW_LNT2_HSTX_OE BIT(25)
  114. struct mtk_mipi_tx {
  115. struct device *dev;
  116. void __iomem *regs;
  117. unsigned int data_rate;
  118. struct clk_hw pll_hw;
  119. struct clk *pll;
  120. };
  121. static inline struct mtk_mipi_tx *mtk_mipi_tx_from_clk_hw(struct clk_hw *hw)
  122. {
  123. return container_of(hw, struct mtk_mipi_tx, pll_hw);
  124. }
  125. static void mtk_mipi_tx_clear_bits(struct mtk_mipi_tx *mipi_tx, u32 offset,
  126. u32 bits)
  127. {
  128. u32 temp = readl(mipi_tx->regs + offset);
  129. writel(temp & ~bits, mipi_tx->regs + offset);
  130. }
  131. static void mtk_mipi_tx_set_bits(struct mtk_mipi_tx *mipi_tx, u32 offset,
  132. u32 bits)
  133. {
  134. u32 temp = readl(mipi_tx->regs + offset);
  135. writel(temp | bits, mipi_tx->regs + offset);
  136. }
  137. static void mtk_mipi_tx_update_bits(struct mtk_mipi_tx *mipi_tx, u32 offset,
  138. u32 mask, u32 data)
  139. {
  140. u32 temp = readl(mipi_tx->regs + offset);
  141. writel((temp & ~mask) | (data & mask), mipi_tx->regs + offset);
  142. }
  143. static int mtk_mipi_tx_pll_prepare(struct clk_hw *hw)
  144. {
  145. struct mtk_mipi_tx *mipi_tx = mtk_mipi_tx_from_clk_hw(hw);
  146. unsigned int txdiv, txdiv0, txdiv1;
  147. u64 pcw;
  148. dev_dbg(mipi_tx->dev, "prepare: %u Hz\n", mipi_tx->data_rate);
  149. if (mipi_tx->data_rate >= 500000000) {
  150. txdiv = 1;
  151. txdiv0 = 0;
  152. txdiv1 = 0;
  153. } else if (mipi_tx->data_rate >= 250000000) {
  154. txdiv = 2;
  155. txdiv0 = 1;
  156. txdiv1 = 0;
  157. } else if (mipi_tx->data_rate >= 125000000) {
  158. txdiv = 4;
  159. txdiv0 = 2;
  160. txdiv1 = 0;
  161. } else if (mipi_tx->data_rate > 62000000) {
  162. txdiv = 8;
  163. txdiv0 = 2;
  164. txdiv1 = 1;
  165. } else if (mipi_tx->data_rate >= 50000000) {
  166. txdiv = 16;
  167. txdiv0 = 2;
  168. txdiv1 = 2;
  169. } else {
  170. return -EINVAL;
  171. }
  172. mtk_mipi_tx_update_bits(mipi_tx, MIPITX_DSI_BG_CON,
  173. RG_DSI_VOUT_MSK |
  174. RG_DSI_BG_CKEN | RG_DSI_BG_CORE_EN,
  175. (4 << 20) | (4 << 17) | (4 << 14) |
  176. (4 << 11) | (4 << 8) | (4 << 5) |
  177. RG_DSI_BG_CKEN | RG_DSI_BG_CORE_EN);
  178. usleep_range(30, 100);
  179. mtk_mipi_tx_update_bits(mipi_tx, MIPITX_DSI_TOP_CON,
  180. RG_DSI_LNT_IMP_CAL_CODE | RG_DSI_LNT_HS_BIAS_EN,
  181. (8 << 4) | RG_DSI_LNT_HS_BIAS_EN);
  182. mtk_mipi_tx_set_bits(mipi_tx, MIPITX_DSI_CON,
  183. RG_DSI_CKG_LDOOUT_EN | RG_DSI_LDOCORE_EN);
  184. mtk_mipi_tx_update_bits(mipi_tx, MIPITX_DSI_PLL_PWR,
  185. RG_DSI_MPPLL_SDM_PWR_ON |
  186. RG_DSI_MPPLL_SDM_ISO_EN,
  187. RG_DSI_MPPLL_SDM_PWR_ON);
  188. mtk_mipi_tx_clear_bits(mipi_tx, MIPITX_DSI_PLL_CON0,
  189. RG_DSI_MPPLL_PLL_EN);
  190. mtk_mipi_tx_update_bits(mipi_tx, MIPITX_DSI_PLL_CON0,
  191. RG_DSI_MPPLL_TXDIV0 | RG_DSI_MPPLL_TXDIV1 |
  192. RG_DSI_MPPLL_PREDIV,
  193. (txdiv0 << 3) | (txdiv1 << 5));
  194. /*
  195. * PLL PCW config
  196. * PCW bit 24~30 = integer part of pcw
  197. * PCW bit 0~23 = fractional part of pcw
  198. * pcw = data_Rate*4*txdiv/(Ref_clk*2);
  199. * Post DIV =4, so need data_Rate*4
  200. * Ref_clk is 26MHz
  201. */
  202. pcw = div_u64(((u64)mipi_tx->data_rate * 2 * txdiv) << 24,
  203. 26000000);
  204. writel(pcw, mipi_tx->regs + MIPITX_DSI_PLL_CON2);
  205. mtk_mipi_tx_set_bits(mipi_tx, MIPITX_DSI_PLL_CON1,
  206. RG_DSI_MPPLL_SDM_FRA_EN);
  207. mtk_mipi_tx_set_bits(mipi_tx, MIPITX_DSI_PLL_CON0, RG_DSI_MPPLL_PLL_EN);
  208. usleep_range(20, 100);
  209. mtk_mipi_tx_clear_bits(mipi_tx, MIPITX_DSI_PLL_CON1,
  210. RG_DSI_MPPLL_SDM_SSC_EN);
  211. return 0;
  212. }
  213. static void mtk_mipi_tx_pll_unprepare(struct clk_hw *hw)
  214. {
  215. struct mtk_mipi_tx *mipi_tx = mtk_mipi_tx_from_clk_hw(hw);
  216. dev_dbg(mipi_tx->dev, "unprepare\n");
  217. mtk_mipi_tx_clear_bits(mipi_tx, MIPITX_DSI_PLL_CON0,
  218. RG_DSI_MPPLL_PLL_EN);
  219. mtk_mipi_tx_update_bits(mipi_tx, MIPITX_DSI_PLL_PWR,
  220. RG_DSI_MPPLL_SDM_ISO_EN |
  221. RG_DSI_MPPLL_SDM_PWR_ON,
  222. RG_DSI_MPPLL_SDM_ISO_EN);
  223. mtk_mipi_tx_clear_bits(mipi_tx, MIPITX_DSI_TOP_CON,
  224. RG_DSI_LNT_HS_BIAS_EN);
  225. mtk_mipi_tx_clear_bits(mipi_tx, MIPITX_DSI_CON,
  226. RG_DSI_CKG_LDOOUT_EN | RG_DSI_LDOCORE_EN);
  227. mtk_mipi_tx_clear_bits(mipi_tx, MIPITX_DSI_BG_CON,
  228. RG_DSI_BG_CKEN | RG_DSI_BG_CORE_EN);
  229. mtk_mipi_tx_clear_bits(mipi_tx, MIPITX_DSI_PLL_CON0,
  230. RG_DSI_MPPLL_DIV_MSK);
  231. }
  232. static long mtk_mipi_tx_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  233. unsigned long *prate)
  234. {
  235. return clamp_val(rate, 50000000, 1250000000);
  236. }
  237. static int mtk_mipi_tx_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  238. unsigned long parent_rate)
  239. {
  240. struct mtk_mipi_tx *mipi_tx = mtk_mipi_tx_from_clk_hw(hw);
  241. dev_dbg(mipi_tx->dev, "set rate: %lu Hz\n", rate);
  242. mipi_tx->data_rate = rate;
  243. return 0;
  244. }
  245. static unsigned long mtk_mipi_tx_pll_recalc_rate(struct clk_hw *hw,
  246. unsigned long parent_rate)
  247. {
  248. struct mtk_mipi_tx *mipi_tx = mtk_mipi_tx_from_clk_hw(hw);
  249. return mipi_tx->data_rate;
  250. }
  251. static const struct clk_ops mtk_mipi_tx_pll_ops = {
  252. .prepare = mtk_mipi_tx_pll_prepare,
  253. .unprepare = mtk_mipi_tx_pll_unprepare,
  254. .round_rate = mtk_mipi_tx_pll_round_rate,
  255. .set_rate = mtk_mipi_tx_pll_set_rate,
  256. .recalc_rate = mtk_mipi_tx_pll_recalc_rate,
  257. };
  258. static int mtk_mipi_tx_power_on_signal(struct phy *phy)
  259. {
  260. struct mtk_mipi_tx *mipi_tx = phy_get_drvdata(phy);
  261. unsigned int reg;
  262. for (reg = MIPITX_DSI_CLOCK_LANE;
  263. reg <= MIPITX_DSI_DATA_LANE3; reg += 4)
  264. mtk_mipi_tx_set_bits(mipi_tx, reg, RG_DSI_LNTx_LDOOUT_EN);
  265. mtk_mipi_tx_clear_bits(mipi_tx, MIPITX_DSI_TOP_CON,
  266. RG_DSI_PAD_TIE_LOW_EN);
  267. return 0;
  268. }
  269. static int mtk_mipi_tx_power_on(struct phy *phy)
  270. {
  271. struct mtk_mipi_tx *mipi_tx = phy_get_drvdata(phy);
  272. int ret;
  273. /* Power up core and enable PLL */
  274. ret = clk_prepare_enable(mipi_tx->pll);
  275. if (ret < 0)
  276. return ret;
  277. /* Enable DSI Lane LDO outputs, disable pad tie low */
  278. mtk_mipi_tx_power_on_signal(phy);
  279. return 0;
  280. }
  281. static void mtk_mipi_tx_power_off_signal(struct phy *phy)
  282. {
  283. struct mtk_mipi_tx *mipi_tx = phy_get_drvdata(phy);
  284. unsigned int reg;
  285. mtk_mipi_tx_set_bits(mipi_tx, MIPITX_DSI_TOP_CON,
  286. RG_DSI_PAD_TIE_LOW_EN);
  287. for (reg = MIPITX_DSI_CLOCK_LANE;
  288. reg <= MIPITX_DSI_DATA_LANE3; reg += 4)
  289. mtk_mipi_tx_clear_bits(mipi_tx, reg, RG_DSI_LNTx_LDOOUT_EN);
  290. }
  291. static int mtk_mipi_tx_power_off(struct phy *phy)
  292. {
  293. struct mtk_mipi_tx *mipi_tx = phy_get_drvdata(phy);
  294. /* Enable pad tie low, disable DSI Lane LDO outputs */
  295. mtk_mipi_tx_power_off_signal(phy);
  296. /* Disable PLL and power down core */
  297. clk_disable_unprepare(mipi_tx->pll);
  298. return 0;
  299. }
  300. static const struct phy_ops mtk_mipi_tx_ops = {
  301. .power_on = mtk_mipi_tx_power_on,
  302. .power_off = mtk_mipi_tx_power_off,
  303. .owner = THIS_MODULE,
  304. };
  305. static int mtk_mipi_tx_probe(struct platform_device *pdev)
  306. {
  307. struct device *dev = &pdev->dev;
  308. struct mtk_mipi_tx *mipi_tx;
  309. struct resource *mem;
  310. struct clk *ref_clk;
  311. const char *ref_clk_name;
  312. struct clk_init_data clk_init = {
  313. .ops = &mtk_mipi_tx_pll_ops,
  314. .num_parents = 1,
  315. .parent_names = (const char * const *)&ref_clk_name,
  316. .flags = CLK_SET_RATE_GATE,
  317. };
  318. struct phy *phy;
  319. struct phy_provider *phy_provider;
  320. int ret;
  321. mipi_tx = devm_kzalloc(dev, sizeof(*mipi_tx), GFP_KERNEL);
  322. if (!mipi_tx)
  323. return -ENOMEM;
  324. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  325. mipi_tx->regs = devm_ioremap_resource(dev, mem);
  326. if (IS_ERR(mipi_tx->regs)) {
  327. ret = PTR_ERR(mipi_tx->regs);
  328. dev_err(dev, "Failed to get memory resource: %d\n", ret);
  329. return ret;
  330. }
  331. ref_clk = devm_clk_get(dev, NULL);
  332. if (IS_ERR(ref_clk)) {
  333. ret = PTR_ERR(ref_clk);
  334. dev_err(dev, "Failed to get reference clock: %d\n", ret);
  335. return ret;
  336. }
  337. ref_clk_name = __clk_get_name(ref_clk);
  338. ret = of_property_read_string(dev->of_node, "clock-output-names",
  339. &clk_init.name);
  340. if (ret < 0) {
  341. dev_err(dev, "Failed to read clock-output-names: %d\n", ret);
  342. return ret;
  343. }
  344. mipi_tx->pll_hw.init = &clk_init;
  345. mipi_tx->pll = devm_clk_register(dev, &mipi_tx->pll_hw);
  346. if (IS_ERR(mipi_tx->pll)) {
  347. ret = PTR_ERR(mipi_tx->pll);
  348. dev_err(dev, "Failed to register PLL: %d\n", ret);
  349. return ret;
  350. }
  351. phy = devm_phy_create(dev, NULL, &mtk_mipi_tx_ops);
  352. if (IS_ERR(phy)) {
  353. ret = PTR_ERR(phy);
  354. dev_err(dev, "Failed to create MIPI D-PHY: %d\n", ret);
  355. return ret;
  356. }
  357. phy_set_drvdata(phy, mipi_tx);
  358. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  359. if (IS_ERR(phy_provider)) {
  360. ret = PTR_ERR(phy_provider);
  361. return ret;
  362. }
  363. mipi_tx->dev = dev;
  364. return of_clk_add_provider(dev->of_node, of_clk_src_simple_get,
  365. mipi_tx->pll);
  366. }
  367. static int mtk_mipi_tx_remove(struct platform_device *pdev)
  368. {
  369. of_clk_del_provider(pdev->dev.of_node);
  370. return 0;
  371. }
  372. static const struct of_device_id mtk_mipi_tx_match[] = {
  373. { .compatible = "mediatek,mt8173-mipi-tx", },
  374. {},
  375. };
  376. struct platform_driver mtk_mipi_tx_driver = {
  377. .probe = mtk_mipi_tx_probe,
  378. .remove = mtk_mipi_tx_remove,
  379. .driver = {
  380. .name = "mediatek-mipi-tx",
  381. .of_match_table = mtk_mipi_tx_match,
  382. },
  383. };