PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/gpu/drm/mediatek/mtk_mipi_tx.c

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