phy-jh7110-dphy-tx.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * DPHY TX driver for the StarFive JH7110 SoC
  4. *
  5. * Copyright (C) 2023 StarFive Technology Co., Ltd.
  6. * Author: Keith Zhao <keith.zhao@starfivetech.com>
  7. * Author: Shengyang Chen <shengyang.chen@starfivetech.com>
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/clk.h>
  11. #include <linux/io.h>
  12. #include <linux/iopoll.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/phy/phy.h>
  18. #include <linux/phy/phy-mipi-dphy.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/reset.h>
  22. #define STF_DPHY_APBIFSAIF_SYSCFG(x) (x)
  23. #define STF_DPHY_AON_POWER_READY_N_ACTIVE 0
  24. #define STF_DPHY_AON_POWER_READY_N BIT(0)
  25. #define STF_DPHY_CFG_L0_SWAP_SEL GENMASK(14, 12)
  26. #define STF_DPHY_CFG_L1_SWAP_SEL GENMASK(17, 15)
  27. #define STF_DPHY_CFG_L2_SWAP_SEL GENMASK(20, 18)
  28. #define STF_DPHY_CFG_L3_SWAP_SEL GENMASK(23, 21)
  29. #define STF_DPHY_CFG_L4_SWAP_SEL GENMASK(26, 24)
  30. #define STF_DPHY_RGS_CDTX_PLL_UNLOCK BIT(18)
  31. #define STF_DPHY_RG_CDTX_L0N_HSTX_RES GENMASK(23, 19)
  32. #define STF_DPHY_RG_CDTX_L0P_HSTX_RES GENMASK(28, 24)
  33. #define STF_DPHY_RG_CDTX_L1P_HSTX_RES GENMASK(9, 5)
  34. #define STF_DPHY_RG_CDTX_L2N_HSTX_RES GENMASK(14, 10)
  35. #define STF_DPHY_RG_CDTX_L2P_HSTX_RES GENMASK(19, 15)
  36. #define STF_DPHY_RG_CDTX_L3N_HSTX_RES GENMASK(24, 20)
  37. #define STF_DPHY_RG_CDTX_L3P_HSTX_RES GENMASK(29, 25)
  38. #define STF_DPHY_RG_CDTX_L4N_HSTX_RES GENMASK(4, 0)
  39. #define STF_DPHY_RG_CDTX_L4P_HSTX_RES GENMASK(9, 5)
  40. #define STF_DPHY_RG_CDTX_PLL_FBK_FRA GENMASK(23, 0)
  41. #define STF_DPHY_RG_CDTX_PLL_FBK_INT GENMASK(8, 0)
  42. #define STF_DPHY_RG_CDTX_PLL_FM_EN BIT(9)
  43. #define STF_DPHY_RG_CDTX_PLL_LDO_STB_X2_EN BIT(10)
  44. #define STF_DPHY_RG_CDTX_PLL_PRE_DIV GENMASK(12, 11)
  45. #define STF_DPHY_RG_CDTX_PLL_SSC_EN BIT(18)
  46. #define STF_DPHY_RG_CLANE_HS_CLK_POST_TIME GENMASK(7, 0)
  47. #define STF_DPHY_RG_CLANE_HS_CLK_PRE_TIME GENMASK(15, 8)
  48. #define STF_DPHY_RG_CLANE_HS_PRE_TIME GENMASK(23, 16)
  49. #define STF_DPHY_RG_CLANE_HS_TRAIL_TIME GENMASK(31, 24)
  50. #define STF_DPHY_RG_CLANE_HS_ZERO_TIME GENMASK(7, 0)
  51. #define STF_DPHY_RG_DLANE_HS_PRE_TIME GENMASK(15, 8)
  52. #define STF_DPHY_RG_DLANE_HS_TRAIL_TIME GENMASK(23, 16)
  53. #define STF_DPHY_RG_DLANE_HS_ZERO_TIME GENMASK(31, 24)
  54. #define STF_DPHY_RG_EXTD_CYCLE_SEL GENMASK(2, 0)
  55. #define STF_DPHY_SCFG_C_HS_PRE_ZERO_TIME GENMASK(31, 0)
  56. #define STF_DPHY_SCFG_DSI_TXREADY_ESC_SEL GENMASK(2, 1)
  57. #define STF_DPHY_SCFG_PPI_C_READY_SEL GENMASK(4, 3)
  58. #define STF_DPHY_REFCLK_IN_SEL GENMASK(28, 26)
  59. #define STF_DPHY_RESETB BIT(29)
  60. #define STF_DPHY_REFCLK_12M 1
  61. #define STF_DPHY_BITRATE_ALIGN 10000000
  62. #define STF_MAP_LANES_NUM 5
  63. #define STF_DPHY_LSHIFT_16(x) (FIELD_PREP(GENMASK(23, 16), (x)))
  64. #define STF_DPHY_LSHIFT_8(x) (FIELD_PREP(GENMASK(15, 8), (x)))
  65. #define STF_DPHY_HW_DELAY_US 200
  66. #define STF_DPHY_HW_TIMEOUT_US 5000
  67. struct stf_dphy_config {
  68. unsigned long bitrate;
  69. u32 pll_fbk_int;
  70. u32 pll_fbk_fra_val;
  71. u32 extd_cycle_sel;
  72. u32 dlane_hs_pre_time;
  73. u32 dlane_hs_zero_time;
  74. u32 dlane_hs_trail_time;
  75. u32 clane_hs_pre_time;
  76. u32 clane_hs_zero_time;
  77. u32 clane_hs_trail_time;
  78. u32 clane_hs_clk_pre_time;
  79. u32 clane_hs_clk_post_time;
  80. };
  81. static const struct stf_dphy_config reg_configs[] = {
  82. {160000000, 0x6a, 0xaa, 0x3, 0xa, 0x17, 0x11, 0x5, 0x2b, 0xd, 0x7, 0x3d},
  83. {170000000, 0x71, 0x55, 0x3, 0xb, 0x18, 0x11, 0x5, 0x2e, 0xd, 0x7, 0x3d},
  84. {180000000, 0x78, 0x0, 0x3, 0xb, 0x19, 0x12, 0x6, 0x30, 0xe, 0x7, 0x3e},
  85. {190000000, 0x7e, 0xaa, 0x3, 0xc, 0x1a, 0x12, 0x6, 0x33, 0xe, 0x7, 0x3e},
  86. {200000000, 0x85, 0x55, 0x3, 0xc, 0x1b, 0x13, 0x7, 0x35, 0xf, 0x7, 0x3f},
  87. {320000000, 0x6a, 0xaa, 0x2, 0x8, 0x14, 0xf, 0x5, 0x2b, 0xd, 0x3, 0x23},
  88. {330000000, 0x6e, 0x0, 0x2, 0x8, 0x15, 0xf, 0x5, 0x2d, 0xd, 0x3, 0x23},
  89. {340000000, 0x71, 0x55, 0x2, 0x9, 0x15, 0xf, 0x5, 0x2e, 0xd, 0x3, 0x23},
  90. {350000000, 0x74, 0xaa, 0x2, 0x9, 0x15, 0x10, 0x6, 0x2f, 0xe, 0x3, 0x24},
  91. {360000000, 0x78, 0x0, 0x2, 0x9, 0x16, 0x10, 0x6, 0x30, 0xe, 0x3, 0x24},
  92. {370000000, 0x7b, 0x55, 0x2, 0x9, 0x17, 0x10, 0x6, 0x32, 0xe, 0x3, 0x24},
  93. {380000000, 0x7e, 0xaa, 0x2, 0xa, 0x17, 0x10, 0x6, 0x33, 0xe, 0x3, 0x24},
  94. {390000000, 0x82, 0x0, 0x2, 0xa, 0x17, 0x11, 0x6, 0x35, 0xf, 0x3, 0x25},
  95. {400000000, 0x85, 0x55, 0x2, 0xa, 0x18, 0x11, 0x7, 0x35, 0xf, 0x3, 0x25},
  96. {410000000, 0x88, 0xaa, 0x2, 0xa, 0x19, 0x11, 0x7, 0x37, 0xf, 0x3, 0x25},
  97. {420000000, 0x8c, 0x0, 0x2, 0xa, 0x19, 0x12, 0x7, 0x38, 0x10, 0x3, 0x26},
  98. {430000000, 0x8f, 0x55, 0x2, 0xb, 0x19, 0x12, 0x7, 0x39, 0x10, 0x3, 0x26},
  99. {440000000, 0x92, 0xaa, 0x2, 0xb, 0x1a, 0x12, 0x7, 0x3b, 0x10, 0x3, 0x26},
  100. {450000000, 0x96, 0x0, 0x2, 0xb, 0x1b, 0x12, 0x8, 0x3c, 0x10, 0x3, 0x26},
  101. {460000000, 0x99, 0x55, 0x2, 0xb, 0x1b, 0x13, 0x8, 0x3d, 0x11, 0x3, 0x27},
  102. {470000000, 0x9c, 0xaa, 0x2, 0xc, 0x1b, 0x13, 0x8, 0x3e, 0x11, 0x3, 0x27},
  103. {480000000, 0xa0, 0x27, 0x2, 0xc, 0x1c, 0x13, 0x8, 0x40, 0x11, 0x3, 0x27},
  104. {490000000, 0xa3, 0x55, 0x2, 0xc, 0x1d, 0x14, 0x8, 0x42, 0x12, 0x3, 0x28},
  105. {500000000, 0xa6, 0xaa, 0x2, 0xc, 0x1d, 0x14, 0x9, 0x42, 0x12, 0x3, 0x28},
  106. {510000000, 0xaa, 0x0, 0x2, 0xc, 0x1e, 0x14, 0x9, 0x44, 0x12, 0x3, 0x28},
  107. {520000000, 0xad, 0x55, 0x2, 0xd, 0x1e, 0x15, 0x9, 0x45, 0x13, 0x3, 0x29},
  108. {530000000, 0xb0, 0xaa, 0x2, 0xd, 0x1e, 0x15, 0x9, 0x47, 0x13, 0x3, 0x29},
  109. {540000000, 0xb4, 0x0, 0x2, 0xd, 0x1f, 0x15, 0x9, 0x48, 0x13, 0x3, 0x29},
  110. {550000000, 0xb7, 0x55, 0x2, 0xd, 0x20, 0x16, 0x9, 0x4a, 0x14, 0x3, 0x2a},
  111. {560000000, 0xba, 0xaa, 0x2, 0xe, 0x20, 0x16, 0xa, 0x4a, 0x14, 0x3, 0x2a},
  112. {570000000, 0xbe, 0x0, 0x2, 0xe, 0x20, 0x16, 0xa, 0x4c, 0x14, 0x3, 0x2a},
  113. {580000000, 0xc1, 0x55, 0x2, 0xe, 0x21, 0x16, 0xa, 0x4d, 0x14, 0x3, 0x2a},
  114. {590000000, 0xc4, 0xaa, 0x2, 0xe, 0x22, 0x17, 0xa, 0x4f, 0x15, 0x3, 0x2b},
  115. {600000000, 0xc8, 0x0, 0x2, 0xe, 0x23, 0x17, 0xa, 0x50, 0x15, 0x3, 0x2b},
  116. {610000000, 0xcb, 0x55, 0x2, 0xf, 0x22, 0x17, 0xb, 0x50, 0x15, 0x3, 0x2b},
  117. {620000000, 0xce, 0xaa, 0x2, 0xf, 0x23, 0x18, 0xb, 0x52, 0x16, 0x3, 0x2c},
  118. {630000000, 0x69, 0x0, 0x1, 0x7, 0x12, 0xd, 0x5, 0x2a, 0xc, 0x1, 0x15},
  119. {640000000, 0x6a, 0xaa, 0x1, 0x7, 0x13, 0xe, 0x5, 0x2b, 0xd, 0x1, 0x16},
  120. {650000000, 0x6c, 0x55, 0x1, 0x7, 0x13, 0xe, 0x5, 0x2c, 0xd, 0x1, 0x16},
  121. {660000000, 0x6e, 0x0, 0x1, 0x7, 0x13, 0xe, 0x5, 0x2d, 0xd, 0x1, 0x16},
  122. {670000000, 0x6f, 0xaa, 0x1, 0x8, 0x13, 0xe, 0x5, 0x2d, 0xd, 0x1, 0x16},
  123. {680000000, 0x71, 0x55, 0x1, 0x8, 0x13, 0xe, 0x5, 0x2e, 0xd, 0x1, 0x16},
  124. {690000000, 0x73, 0x0, 0x1, 0x8, 0x14, 0xe, 0x6, 0x2e, 0xd, 0x1, 0x16},
  125. {700000000, 0x74, 0xaa, 0x1, 0x8, 0x14, 0xf, 0x6, 0x2f, 0xe, 0x1, 0x16},
  126. {710000000, 0x76, 0x55, 0x1, 0x8, 0x14, 0xf, 0x6, 0x2f, 0xe, 0x1, 0x17},
  127. {720000000, 0x78, 0x0, 0x1, 0x8, 0x15, 0xf, 0x6, 0x30, 0xe, 0x1, 0x17},
  128. {730000000, 0x79, 0xaa, 0x1, 0x8, 0x15, 0xf, 0x6, 0x31, 0xe, 0x1, 0x17},
  129. {740000000, 0x7b, 0x55, 0x1, 0x8, 0x15, 0xf, 0x6, 0x32, 0xe, 0x1, 0x17},
  130. {750000000, 0x7d, 0x0, 0x1, 0x8, 0x16, 0xf, 0x6, 0x32, 0xe, 0x1, 0x17},
  131. {760000000, 0x7e, 0xaa, 0x1, 0x9, 0x15, 0xf, 0x6, 0x33, 0xe, 0x1, 0x17},
  132. {770000000, 0x80, 0x55, 0x1, 0x9, 0x15, 0x10, 0x6, 0x34, 0xf, 0x1, 0x18},
  133. {780000000, 0x82, 0x0, 0x1, 0x9, 0x16, 0x10, 0x6, 0x35, 0xf, 0x1, 0x18,},
  134. {790000000, 0x83, 0xaa, 0x1, 0x9, 0x16, 0x10, 0x7, 0x34, 0xf, 0x1, 0x18},
  135. {800000000, 0x85, 0x55, 0x1, 0x9, 0x17, 0x10, 0x7, 0x35, 0xf, 0x1, 0x18},
  136. {810000000, 0x87, 0x0, 0x1, 0x9, 0x17, 0x10, 0x7, 0x36, 0xf, 0x1, 0x18},
  137. {820000000, 0x88, 0xaa, 0x1, 0x9, 0x17, 0x10, 0x7, 0x37, 0xf, 0x1, 0x18},
  138. {830000000, 0x8a, 0x55, 0x1, 0x9, 0x18, 0x10, 0x7, 0x37, 0xf, 0x1, 0x18},
  139. {840000000, 0x8c, 0x0, 0x1, 0x9, 0x18, 0x11, 0x7, 0x38, 0x10, 0x1, 0x19},
  140. {850000000, 0x8d, 0xaa, 0x1, 0xa, 0x17, 0x11, 0x7, 0x39, 0x10, 0x1, 0x19},
  141. {860000000, 0x8f, 0x55, 0x1, 0xa, 0x18, 0x11, 0x7, 0x39, 0x10, 0x1, 0x19},
  142. {870000000, 0x91, 0x0, 0x1, 0xa, 0x18, 0x11, 0x7, 0x3a, 0x10, 0x1, 0x19},
  143. {880000000, 0x92, 0xaa, 0x1, 0xa, 0x18, 0x11, 0x7, 0x3b, 0x10, 0x1, 0x19},
  144. {890000000, 0x94, 0x55, 0x1, 0xa, 0x19, 0x11, 0x7, 0x3c, 0x10, 0x1, 0x19},
  145. {900000000, 0x96, 0x0, 0x1, 0xa, 0x19, 0x12, 0x8, 0x3c, 0x10, 0x1, 0x19},
  146. {910000000, 0x97, 0xaa, 0x1, 0xa, 0x19, 0x12, 0x8, 0x3c, 0x11, 0x1, 0x1a},
  147. {920000000, 0x99, 0x55, 0x1, 0xa, 0x1a, 0x12, 0x8, 0x3d, 0x11, 0x1, 0x1a},
  148. {930000000, 0x9b, 0x0, 0x1, 0xa, 0x1a, 0x12, 0x8, 0x3e, 0x11, 0x1, 0x1a},
  149. {940000000, 0x9c, 0xaa, 0x1, 0xb, 0x1a, 0x12, 0x8, 0x3e, 0x11, 0x1, 0x1a},
  150. {950000000, 0x9e, 0x55, 0x1, 0xb, 0x1a, 0x12, 0x8, 0x3f, 0x11, 0x1, 0x1a},
  151. {960000000, 0xa0, 0x0, 0x1, 0xb, 0x1a, 0x12, 0x8, 0x40, 0x11, 0x1, 0x1a},
  152. {970000000, 0xa1, 0xaa, 0x1, 0xb, 0x1b, 0x13, 0x8, 0x41, 0x12, 0x1, 0x1b},
  153. {980000000, 0xa3, 0x55, 0x1, 0xb, 0x1b, 0x13, 0x8, 0x42, 0x12, 0x1, 0x1b},
  154. {990000000, 0xa5, 0x0, 0x1, 0xb, 0x1b, 0x13, 0x8, 0x42, 0x12, 0x1, 0x1b},
  155. {1000000000, 0xa6, 0xaa, 0x1, 0xb, 0x1c, 0x13, 0x9, 0x42, 0x12, 0x1, 0x1b},
  156. };
  157. struct stf_dphy_info {
  158. /**
  159. * @maps:
  160. *
  161. * Physical lanes and logic lanes mapping table.
  162. *
  163. * The default order is:
  164. * [data lane 0, data lane 1, data lane 2, date lane 3, clk lane]
  165. */
  166. u8 maps[STF_MAP_LANES_NUM];
  167. };
  168. struct stf_dphy {
  169. struct device *dev;
  170. void __iomem *topsys;
  171. struct clk *txesc_clk;
  172. struct reset_control *sys_rst;
  173. struct phy_configure_opts_mipi_dphy config;
  174. struct phy *phy;
  175. const struct stf_dphy_info *info;
  176. };
  177. static u32 stf_dphy_get_config_index(u32 bitrate)
  178. {
  179. u32 i;
  180. for (i = 0; i < ARRAY_SIZE(reg_configs); i++) {
  181. if (reg_configs[i].bitrate == bitrate)
  182. return i;
  183. }
  184. return 0;
  185. }
  186. static void stf_dphy_hw_reset(struct stf_dphy *dphy, int assert)
  187. {
  188. int rc;
  189. u32 status = 0;
  190. writel(FIELD_PREP(STF_DPHY_RESETB, assert),
  191. dphy->topsys + STF_DPHY_APBIFSAIF_SYSCFG(100));
  192. if (assert) {
  193. rc = readl_poll_timeout_atomic(dphy->topsys +
  194. STF_DPHY_APBIFSAIF_SYSCFG(8),
  195. status,
  196. !(FIELD_GET(STF_DPHY_RGS_CDTX_PLL_UNLOCK, status)),
  197. STF_DPHY_HW_DELAY_US, STF_DPHY_HW_TIMEOUT_US);
  198. if (rc)
  199. dev_err(dphy->dev, "MIPI dphy-tx # PLL Locked\n");
  200. }
  201. }
  202. static int stf_dphy_configure(struct phy *phy, union phy_configure_opts *opts)
  203. {
  204. struct stf_dphy *dphy = phy_get_drvdata(phy);
  205. const struct stf_dphy_info *info = dphy->info;
  206. const struct stf_dphy_config *p = reg_configs;
  207. unsigned long alignment = STF_DPHY_BITRATE_ALIGN;
  208. u32 bitrate = opts->mipi_dphy.hs_clk_rate;
  209. u32 tmp;
  210. u32 i;
  211. if (bitrate % alignment)
  212. bitrate += alignment - (bitrate % alignment);
  213. i = stf_dphy_get_config_index(bitrate);
  214. tmp = readl(dphy->topsys + STF_DPHY_APBIFSAIF_SYSCFG(100));
  215. tmp &= ~STF_DPHY_REFCLK_IN_SEL;
  216. tmp |= FIELD_PREP(STF_DPHY_REFCLK_IN_SEL, STF_DPHY_REFCLK_12M);
  217. writel(tmp, dphy->topsys + STF_DPHY_APBIFSAIF_SYSCFG(100));
  218. writel(FIELD_PREP(STF_DPHY_RG_CDTX_L0N_HSTX_RES, 0x10) |
  219. FIELD_PREP(STF_DPHY_RG_CDTX_L0P_HSTX_RES, 0x10),
  220. dphy->topsys + STF_DPHY_APBIFSAIF_SYSCFG(8));
  221. writel(FIELD_PREP(STF_DPHY_RG_CDTX_L0N_HSTX_RES, 0x10) |
  222. FIELD_PREP(STF_DPHY_RG_CDTX_L2N_HSTX_RES, 0x10) |
  223. FIELD_PREP(STF_DPHY_RG_CDTX_L3N_HSTX_RES, 0x10) |
  224. FIELD_PREP(STF_DPHY_RG_CDTX_L1P_HSTX_RES, 0x10) |
  225. FIELD_PREP(STF_DPHY_RG_CDTX_L2P_HSTX_RES, 0x10) |
  226. FIELD_PREP(STF_DPHY_RG_CDTX_L3P_HSTX_RES, 0x10),
  227. dphy->topsys + STF_DPHY_APBIFSAIF_SYSCFG(12));
  228. writel(FIELD_PREP(STF_DPHY_RG_CDTX_L4N_HSTX_RES, 0x10) |
  229. FIELD_PREP(STF_DPHY_RG_CDTX_L4P_HSTX_RES, 0x10),
  230. dphy->topsys + STF_DPHY_APBIFSAIF_SYSCFG(16));
  231. /* Lane setting */
  232. writel(FIELD_PREP(STF_DPHY_AON_POWER_READY_N,
  233. STF_DPHY_AON_POWER_READY_N_ACTIVE) |
  234. FIELD_PREP(STF_DPHY_CFG_L0_SWAP_SEL, info->maps[0]) |
  235. FIELD_PREP(STF_DPHY_CFG_L1_SWAP_SEL, info->maps[1]) |
  236. FIELD_PREP(STF_DPHY_CFG_L2_SWAP_SEL, info->maps[2]) |
  237. FIELD_PREP(STF_DPHY_CFG_L3_SWAP_SEL, info->maps[3]) |
  238. FIELD_PREP(STF_DPHY_CFG_L4_SWAP_SEL, info->maps[4]),
  239. dphy->topsys + STF_DPHY_APBIFSAIF_SYSCFG(0));
  240. /* PLL setting */
  241. writel(FIELD_PREP(STF_DPHY_RG_CDTX_PLL_SSC_EN, 0x0),
  242. dphy->topsys + STF_DPHY_APBIFSAIF_SYSCFG(28));
  243. writel(FIELD_PREP(STF_DPHY_RG_CDTX_PLL_LDO_STB_X2_EN, 0x1) |
  244. FIELD_PREP(STF_DPHY_RG_CDTX_PLL_FM_EN, 0x1) |
  245. FIELD_PREP(STF_DPHY_RG_CDTX_PLL_PRE_DIV, 0x0) |
  246. FIELD_PREP(STF_DPHY_RG_CDTX_PLL_FBK_INT, p[i].pll_fbk_int),
  247. dphy->topsys + STF_DPHY_APBIFSAIF_SYSCFG(24));
  248. writel(FIELD_PREP(STF_DPHY_RG_CDTX_PLL_FBK_FRA,
  249. STF_DPHY_LSHIFT_16(p[i].pll_fbk_fra_val) |
  250. STF_DPHY_LSHIFT_8(p[i].pll_fbk_fra_val) |
  251. p[i].pll_fbk_fra_val),
  252. dphy->topsys + STF_DPHY_APBIFSAIF_SYSCFG(20));
  253. writel(FIELD_PREP(STF_DPHY_RG_EXTD_CYCLE_SEL, p[i].extd_cycle_sel),
  254. dphy->topsys + STF_DPHY_APBIFSAIF_SYSCFG(40));
  255. writel(FIELD_PREP(STF_DPHY_RG_DLANE_HS_PRE_TIME, p[i].dlane_hs_pre_time) |
  256. FIELD_PREP(STF_DPHY_RG_DLANE_HS_ZERO_TIME, p[i].dlane_hs_zero_time) |
  257. FIELD_PREP(STF_DPHY_RG_DLANE_HS_TRAIL_TIME, p[i].dlane_hs_trail_time) |
  258. FIELD_PREP(STF_DPHY_RG_CLANE_HS_ZERO_TIME, p[i].clane_hs_zero_time),
  259. dphy->topsys + STF_DPHY_APBIFSAIF_SYSCFG(36));
  260. writel(FIELD_PREP(STF_DPHY_RG_CLANE_HS_PRE_TIME, p[i].clane_hs_pre_time) |
  261. FIELD_PREP(STF_DPHY_RG_CLANE_HS_TRAIL_TIME, p[i].clane_hs_trail_time) |
  262. FIELD_PREP(STF_DPHY_RG_CLANE_HS_CLK_PRE_TIME, p[i].clane_hs_clk_pre_time) |
  263. FIELD_PREP(STF_DPHY_RG_CLANE_HS_CLK_POST_TIME, p[i].clane_hs_clk_post_time),
  264. dphy->topsys + STF_DPHY_APBIFSAIF_SYSCFG(32));
  265. return 0;
  266. }
  267. static int stf_dphy_init(struct phy *phy)
  268. {
  269. struct stf_dphy *dphy = phy_get_drvdata(phy);
  270. int ret;
  271. stf_dphy_hw_reset(dphy, 1);
  272. writel(FIELD_PREP(STF_DPHY_SCFG_PPI_C_READY_SEL, 0) |
  273. FIELD_PREP(STF_DPHY_SCFG_DSI_TXREADY_ESC_SEL, 0),
  274. dphy->topsys + STF_DPHY_APBIFSAIF_SYSCFG(48));
  275. writel(FIELD_PREP(STF_DPHY_SCFG_C_HS_PRE_ZERO_TIME, 0x30),
  276. dphy->topsys + STF_DPHY_APBIFSAIF_SYSCFG(44));
  277. ret = clk_prepare_enable(dphy->txesc_clk);
  278. if (ret) {
  279. dev_err(dphy->dev, "Failed to prepare/enable txesc_clk\n");
  280. return ret;
  281. }
  282. ret = reset_control_deassert(dphy->sys_rst);
  283. if (ret) {
  284. dev_err(dphy->dev, "Failed to deassert sys_rst\n");
  285. return ret;
  286. }
  287. return 0;
  288. }
  289. static int stf_dphy_exit(struct phy *phy)
  290. {
  291. struct stf_dphy *dphy = phy_get_drvdata(phy);
  292. int ret;
  293. ret = reset_control_assert(dphy->sys_rst);
  294. if (ret) {
  295. dev_err(dphy->dev, "Failed to assert sys_rst\n");
  296. return ret;
  297. }
  298. clk_disable_unprepare(dphy->txesc_clk);
  299. stf_dphy_hw_reset(dphy, 0);
  300. return 0;
  301. }
  302. static int stf_dphy_power_on(struct phy *phy)
  303. {
  304. struct stf_dphy *dphy = phy_get_drvdata(phy);
  305. return pm_runtime_resume_and_get(dphy->dev);
  306. }
  307. static int stf_dphy_validate(struct phy *phy, enum phy_mode mode, int submode,
  308. union phy_configure_opts *opts)
  309. {
  310. if (mode != PHY_MODE_MIPI_DPHY)
  311. return -EINVAL;
  312. return 0;
  313. }
  314. static int stf_dphy_power_off(struct phy *phy)
  315. {
  316. struct stf_dphy *dphy = phy_get_drvdata(phy);
  317. return pm_runtime_put_sync(dphy->dev);
  318. }
  319. static const struct phy_ops stf_dphy_ops = {
  320. .power_on = stf_dphy_power_on,
  321. .power_off = stf_dphy_power_off,
  322. .init = stf_dphy_init,
  323. .exit = stf_dphy_exit,
  324. .configure = stf_dphy_configure,
  325. .validate = stf_dphy_validate,
  326. .owner = THIS_MODULE,
  327. };
  328. static int stf_dphy_probe(struct platform_device *pdev)
  329. {
  330. struct phy_provider *phy_provider;
  331. struct stf_dphy *dphy;
  332. dphy = devm_kzalloc(&pdev->dev, sizeof(*dphy), GFP_KERNEL);
  333. if (!dphy)
  334. return -ENOMEM;
  335. dphy->info = of_device_get_match_data(&pdev->dev);
  336. dphy->dev = &pdev->dev;
  337. dev_set_drvdata(&pdev->dev, dphy);
  338. dphy->topsys = devm_platform_ioremap_resource(pdev, 0);
  339. if (IS_ERR(dphy->topsys))
  340. return PTR_ERR(dphy->topsys);
  341. pm_runtime_enable(&pdev->dev);
  342. dphy->txesc_clk = devm_clk_get(&pdev->dev, "txesc");
  343. if (IS_ERR(dphy->txesc_clk))
  344. return dev_err_probe(&pdev->dev, PTR_ERR(dphy->txesc_clk),
  345. "Failed to get txesc clock\n");
  346. dphy->sys_rst = devm_reset_control_get_exclusive(&pdev->dev, "sys");
  347. if (IS_ERR(dphy->sys_rst))
  348. return dev_err_probe(&pdev->dev, PTR_ERR(dphy->sys_rst),
  349. "Failed to get sys reset\n");
  350. dphy->phy = devm_phy_create(&pdev->dev, NULL, &stf_dphy_ops);
  351. if (IS_ERR(dphy->phy))
  352. return dev_err_probe(&pdev->dev, PTR_ERR(dphy->phy),
  353. "Failed to create phy\n");
  354. phy_set_drvdata(dphy->phy, dphy);
  355. phy_provider = devm_of_phy_provider_register(&pdev->dev, of_phy_simple_xlate);
  356. if (IS_ERR(phy_provider))
  357. return dev_err_probe(&pdev->dev, PTR_ERR(phy_provider),
  358. "Failed to register phy\n");
  359. return 0;
  360. }
  361. static const struct stf_dphy_info starfive_dphy_info = {
  362. .maps = {0, 1, 2, 3, 4},
  363. };
  364. static const struct of_device_id stf_dphy_dt_ids[] = {
  365. {
  366. .compatible = "starfive,jh7110-dphy-tx",
  367. .data = &starfive_dphy_info,
  368. },
  369. { /* sentinel */ },
  370. };
  371. MODULE_DEVICE_TABLE(of, stf_dphy_dt_ids);
  372. static struct platform_driver stf_dphy_driver = {
  373. .driver = {
  374. .name = "starfive-dphy-tx",
  375. .of_match_table = stf_dphy_dt_ids,
  376. },
  377. .probe = stf_dphy_probe,
  378. };
  379. module_platform_driver(stf_dphy_driver);
  380. MODULE_AUTHOR("Keith Zhao <keith.zhao@starfivetech.com>");
  381. MODULE_AUTHOR("Shengyang Chen <shengyang.chen@starfivetech.com>");
  382. MODULE_DESCRIPTION("StarFive JH7110 DPHY TX driver");
  383. MODULE_LICENSE("GPL");