mtk_lvds_phy.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 LVDSTX_CTL1 0x00
  22. #define LVDSTX_CTL2 0x04
  23. #define RG_LVDSTX_TVO (0xf << 0)
  24. #define RG_LVDSTX_TVCM (0xf << 4)
  25. #define RG_LVDSTX_TSTCLK_SEL (0x3 << 8)
  26. #define RG_LVDSTX_TSTCLKDIV_EN BIT(10)
  27. #define RG_LVDSTX_TSTCLK_EN BIT(11)
  28. #define RG_LVDSTX_TSTCLKDIV_SEL (3 << 12)
  29. #define RG_LVDSTX_MPX_SEL (3 << 14)
  30. #define RG_LVDSTX_BIAS_SEL (3 << 16)
  31. #define RG_LVDSTX_R_TERM (3 << 18)
  32. #define RG_LVDSTX_SEL_CKTST BIT(20)
  33. #define RG_LVDSTX_SEL_MERGE BIT(21)
  34. #define RG_LVDSTX_LDO_EN BIT(22)
  35. #define RG_LVDSTX_BIAS_EN BIT(23)
  36. #define RG_LVDSTX_SER_ABIST_EN BIT(24)
  37. #define RG_LVDSTX_SER_ABEDG_EN BIT(25)
  38. #define RG_LVDSTX_SER_BIST_TOG BIT(26)
  39. #define LVDSTX_CTL3 0x08
  40. #define RG_LVDSTX_VOUTABIST_EN (0x1f << 0)
  41. #define RG_LVDSTX_EXT_EN (0x1f << 5)
  42. #define RG_LVDSTX_DRV_EN (0x1f << 10)
  43. #define RG_LVDSTX_SER_DIN_SEL BIT(16)
  44. #define RG_LVDSTX_SER_CLKDIG_INV BIT(17)
  45. #define LVDSTX_CTL4 0x0c
  46. #define RG_LVDSTX_TSTPAD_EN BIT(20)
  47. #define RG_LVDSTX_ABIST_EN BIT(21)
  48. #define RG_LVDSTX_MPX_EN BIT(22)
  49. #define RG_LVDSTX_LDOLPF_EN BIT(23)
  50. #define RG_LVDSTX_TEST_BYPASSBUF BIT(24)
  51. #define RG_LVDSTX_BIASLPF_EN BIT(25)
  52. #define RG_LVDSTX_SER_ABMUX_SEL (7 << 26)
  53. #define RG_LVDSTX_SER_PEM_EN BIT(29)
  54. #define RG_LVDSTX_LVROD (3 << 30)
  55. #define LVDSTX_CTL5 0x10
  56. #define RG_LVDSTX_MIPICK_SEL BIT(4)
  57. #define RG_LVDSTX_INCK_SEL BIT(5)
  58. #define RG_LVDSTX_SWITCH_EN BIT(6)
  59. #define VOPLL_CTL1 0x14
  60. #define RG_VPLL_TXMUXDIV2_EN BIT(0)
  61. #define RG_VPLL_FBKSEL (3 << 6)
  62. #define RG_VPLL_FBKDIV (0x7f << 12)
  63. #define VOPLL_CTL2 0x18
  64. #define RG_VPLL_EN BIT(7)
  65. #define RG_VPLL_TXDIV1 (3 << 8)
  66. #define RG_VPLL_TXDIV2 (3 << 10)
  67. #define RG_VPLL_LVDS_EN BIT(12)
  68. #define RG_VPLL_LVDS_DPIX_DIV2 BIT(13)
  69. #define RG_VPLL_TTLDIV (3 << 16)
  70. #define RG_VPLL_TXDIV5_EN BIT(21)
  71. #define RG_VPLL_BIAS_EN BIT(24)
  72. #define RG_VPLL_BIASLPF_EN BIT(25)
  73. #define VOPLL_CTL3 0x1c
  74. #define LVDS_ISO_EN BIT(8)
  75. #define DA_LVDSTX_PWR_ON BIT(9)
  76. struct mtk_lvds_tx {
  77. struct device *dev;
  78. void __iomem *regs;
  79. void __iomem *tx1_regs;
  80. void __iomem *tx2_regs;
  81. bool dual_lvds;
  82. };
  83. static int mtk_lvds_tx_power_on_signal(struct phy *phy)
  84. {
  85. struct mtk_lvds_tx *lvds_tx = phy_get_drvdata(phy);
  86. u32 reg;
  87. dev_info(lvds_tx->dev, "%s\n", __func__);
  88. writel(DA_LVDSTX_PWR_ON, lvds_tx->tx1_regs + VOPLL_CTL3);
  89. writel(DA_LVDSTX_PWR_ON, lvds_tx->tx2_regs + VOPLL_CTL3);
  90. reg = RG_VPLL_TXMUXDIV2_EN | 1 << 6 | 0x1c << 12 | 1 << 20;
  91. if (lvds_tx->dual_lvds)
  92. writel(reg, lvds_tx->tx1_regs + VOPLL_CTL1);
  93. writel(reg, lvds_tx->tx2_regs + VOPLL_CTL1);
  94. reg = RG_VPLL_EN | 1 << 8 | (lvds_tx->dual_lvds ? 0 : 1) << 10 |
  95. RG_VPLL_LVDS_EN | RG_VPLL_LVDS_DPIX_DIV2 |
  96. (lvds_tx->dual_lvds ? 1 : 0) << 16 | RG_VPLL_TXDIV5_EN |
  97. RG_VPLL_BIAS_EN | RG_VPLL_BIASLPF_EN;
  98. writel(reg, lvds_tx->tx2_regs + VOPLL_CTL2);
  99. if (lvds_tx->dual_lvds)
  100. writel(reg, lvds_tx->tx1_regs + VOPLL_CTL2);
  101. reg = 5 | 0xb << 4 | 3 << 8 | RG_LVDSTX_TSTCLKDIV_EN |
  102. RG_LVDSTX_TSTCLK_EN | 1 << 16 | RG_LVDSTX_LDO_EN |
  103. RG_LVDSTX_BIAS_EN;
  104. writel(reg, lvds_tx->tx2_regs + LVDSTX_CTL2);
  105. if (lvds_tx->dual_lvds)
  106. writel(reg, lvds_tx->tx1_regs + LVDSTX_CTL2);
  107. reg = 0x1f << 5 | 0x1f << 10;
  108. writel(reg, lvds_tx->tx2_regs + LVDSTX_CTL3);
  109. if (lvds_tx->dual_lvds)
  110. writel(reg, lvds_tx->tx1_regs + LVDSTX_CTL3);
  111. reg = RG_LVDSTX_LDOLPF_EN | RG_LVDSTX_BIASLPF_EN;
  112. writel(reg, lvds_tx->tx2_regs + LVDSTX_CTL4);
  113. if (lvds_tx->dual_lvds)
  114. writel(reg, lvds_tx->tx1_regs + LVDSTX_CTL4);
  115. return 0;
  116. }
  117. static int mtk_lvds_tx_power_on(struct phy *phy)
  118. {
  119. mtk_lvds_tx_power_on_signal(phy);
  120. return 0;
  121. }
  122. static void mtk_lvds_tx_power_off_signal(struct phy *phy)
  123. {
  124. struct mtk_lvds_tx *lvds_tx = phy_get_drvdata(phy);
  125. u32 reg;
  126. dev_info(lvds_tx->dev, "%s\n", __func__);
  127. writel(DA_LVDSTX_PWR_ON | LVDS_ISO_EN, lvds_tx->tx1_regs + VOPLL_CTL3);
  128. writel(DA_LVDSTX_PWR_ON | LVDS_ISO_EN, lvds_tx->tx2_regs + VOPLL_CTL3);
  129. writel(0, lvds_tx->tx2_regs + VOPLL_CTL2);
  130. if (lvds_tx->dual_lvds)
  131. writel(0, lvds_tx->tx1_regs + VOPLL_CTL2);
  132. reg = readl(lvds_tx->tx2_regs + LVDSTX_CTL2) &
  133. (~(RG_LVDSTX_BIAS_EN | RG_LVDSTX_LDO_EN));
  134. writel(reg, lvds_tx->tx2_regs + LVDSTX_CTL2);
  135. if (lvds_tx->dual_lvds)
  136. writel(reg, lvds_tx->tx1_regs + LVDSTX_CTL2);
  137. writel(0, lvds_tx->tx2_regs + LVDSTX_CTL3);
  138. if (lvds_tx->dual_lvds)
  139. writel(0, lvds_tx->tx1_regs + LVDSTX_CTL3);
  140. }
  141. static int mtk_lvds_tx_power_off(struct phy *phy)
  142. {
  143. mtk_lvds_tx_power_off_signal(phy);
  144. return 0;
  145. }
  146. static const struct phy_ops mtk_lvds_tx_ops = {
  147. .power_on = mtk_lvds_tx_power_on,
  148. .power_off = mtk_lvds_tx_power_off,
  149. .owner = THIS_MODULE,
  150. };
  151. static int mtk_lvds_tx_probe(struct platform_device *pdev)
  152. {
  153. struct device *dev = &pdev->dev;
  154. struct mtk_lvds_tx *lvds_tx;
  155. struct resource *mem;
  156. struct phy *phy;
  157. struct phy_provider *phy_provider;
  158. int ret;
  159. lvds_tx = devm_kzalloc(dev, sizeof(*lvds_tx), GFP_KERNEL);
  160. if (!lvds_tx)
  161. return -ENOMEM;
  162. lvds_tx->dual_lvds = of_property_read_bool(dev->of_node,
  163. "mediatek,dual-channel");
  164. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  165. lvds_tx->tx1_regs = devm_ioremap_resource(dev, mem);
  166. if (IS_ERR(lvds_tx->tx1_regs)) {
  167. ret = PTR_ERR(lvds_tx->tx1_regs);
  168. dev_err(dev, "Failed to get lvds1 memory resource: %d\n", ret);
  169. return ret;
  170. }
  171. mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  172. lvds_tx->tx2_regs = devm_ioremap_resource(dev, mem);
  173. if (IS_ERR(lvds_tx->tx2_regs)) {
  174. ret = PTR_ERR(lvds_tx->tx2_regs);
  175. dev_err(dev, "Failed to get lvds2 memory resource: %d\n", ret);
  176. return ret;
  177. }
  178. writel(DA_LVDSTX_PWR_ON | LVDS_ISO_EN, lvds_tx->tx1_regs + VOPLL_CTL3);
  179. writel(DA_LVDSTX_PWR_ON | LVDS_ISO_EN, lvds_tx->tx2_regs + VOPLL_CTL3);
  180. phy = devm_phy_create(dev, NULL, &mtk_lvds_tx_ops);
  181. if (IS_ERR(phy)) {
  182. ret = PTR_ERR(phy);
  183. dev_err(dev, "Failed to create lvds D-PHY: %d\n", ret);
  184. return ret;
  185. }
  186. phy_set_drvdata(phy, lvds_tx);
  187. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  188. if (IS_ERR(phy)) {
  189. ret = PTR_ERR(phy_provider);
  190. dev_err(dev, "Failed to phy_provider: %d\n", ret);
  191. return ret;
  192. }
  193. lvds_tx->dev = dev;
  194. return 0;
  195. }
  196. static int mtk_lvds_tx_remove(struct platform_device *pdev)
  197. {
  198. of_clk_del_provider(pdev->dev.of_node);
  199. return 0;
  200. }
  201. static const struct of_device_id mtk_lvds_tx_match[] = {
  202. { .compatible = "mediatek,mt8173-lvds-tx" },
  203. {},
  204. };
  205. struct platform_driver mtk_lvds_tx_driver = {
  206. .probe = mtk_lvds_tx_probe,
  207. .remove = mtk_lvds_tx_remove,
  208. .driver = {
  209. .name = "mediatek-mt8173-lvds-tx",
  210. .of_match_table = mtk_lvds_tx_match,
  211. },
  212. };