phy-rockchip-typec.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /*
  2. * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
  3. * Author: Chris Zhong <zyw@rock-chips.com>
  4. * Kever Yang <kever.yang@rock-chips.com>
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * The ROCKCHIP Type-C PHY has two PLL clocks. The first PLL clock
  16. * is used for USB3, the second PLL clock is used for DP. This Type-C PHY has
  17. * 3 working modes: USB3 only mode, DP only mode, and USB3+DP mode.
  18. * At USB3 only mode, both PLL clocks need to be initialized, this allows the
  19. * PHY to switch mode between USB3 and USB3+DP, without disconnecting the USB
  20. * device.
  21. * In The DP only mode, only the DP PLL needs to be powered on, and the 4 lanes
  22. * are all used for DP.
  23. *
  24. * This driver gets extcon cable state and property, then decides which mode to
  25. * select:
  26. *
  27. * 1. USB3 only mode:
  28. * EXTCON_USB or EXTCON_USB_HOST state is true, and
  29. * EXTCON_PROP_USB_SS property is true.
  30. * EXTCON_DISP_DP state is false.
  31. *
  32. * 2. DP only mode:
  33. * EXTCON_DISP_DP state is true, and
  34. * EXTCON_PROP_USB_SS property is false.
  35. * If EXTCON_USB_HOST state is true, it is DP + USB2 mode, since the USB2 phy
  36. * is a separate phy, so this case is still DP only mode.
  37. *
  38. * 3. USB3+DP mode:
  39. * EXTCON_USB_HOST and EXTCON_DISP_DP are both true, and
  40. * EXTCON_PROP_USB_SS property is true.
  41. *
  42. * This Type-C PHY driver supports normal and flip orientation. The orientation
  43. * is reported by the EXTCON_PROP_USB_TYPEC_POLARITY property: true is flip
  44. * orientation, false is normal orientation.
  45. *
  46. */
  47. #include <linux/clk.h>
  48. #include <linux/clk-provider.h>
  49. #include <linux/delay.h>
  50. #include <linux/extcon.h>
  51. #include <linux/io.h>
  52. #include <linux/iopoll.h>
  53. #include <linux/kernel.h>
  54. #include <linux/module.h>
  55. #include <linux/mutex.h>
  56. #include <linux/of.h>
  57. #include <linux/of_address.h>
  58. #include <linux/of_platform.h>
  59. #include <linux/platform_device.h>
  60. #include <linux/regmap.h>
  61. #include <linux/reset.h>
  62. #include <linux/mfd/syscon.h>
  63. #include <linux/phy/phy.h>
  64. #define CMN_SSM_BANDGAP (0x21 << 2)
  65. #define CMN_SSM_BIAS (0x22 << 2)
  66. #define CMN_PLLSM0_PLLEN (0x29 << 2)
  67. #define CMN_PLLSM0_PLLPRE (0x2a << 2)
  68. #define CMN_PLLSM0_PLLVREF (0x2b << 2)
  69. #define CMN_PLLSM0_PLLLOCK (0x2c << 2)
  70. #define CMN_PLLSM1_PLLEN (0x31 << 2)
  71. #define CMN_PLLSM1_PLLPRE (0x32 << 2)
  72. #define CMN_PLLSM1_PLLVREF (0x33 << 2)
  73. #define CMN_PLLSM1_PLLLOCK (0x34 << 2)
  74. #define CMN_PLLSM1_USER_DEF_CTRL (0x37 << 2)
  75. #define CMN_ICAL_OVRD (0xc1 << 2)
  76. #define CMN_PLL0_VCOCAL_OVRD (0x83 << 2)
  77. #define CMN_PLL0_VCOCAL_INIT (0x84 << 2)
  78. #define CMN_PLL0_VCOCAL_ITER (0x85 << 2)
  79. #define CMN_PLL0_LOCK_REFCNT_START (0x90 << 2)
  80. #define CMN_PLL0_LOCK_PLLCNT_START (0x92 << 2)
  81. #define CMN_PLL0_LOCK_PLLCNT_THR (0x93 << 2)
  82. #define CMN_PLL0_INTDIV (0x94 << 2)
  83. #define CMN_PLL0_FRACDIV (0x95 << 2)
  84. #define CMN_PLL0_HIGH_THR (0x96 << 2)
  85. #define CMN_PLL0_DSM_DIAG (0x97 << 2)
  86. #define CMN_PLL0_SS_CTRL1 (0x98 << 2)
  87. #define CMN_PLL0_SS_CTRL2 (0x99 << 2)
  88. #define CMN_PLL1_VCOCAL_START (0xa1 << 2)
  89. #define CMN_PLL1_VCOCAL_OVRD (0xa3 << 2)
  90. #define CMN_PLL1_VCOCAL_INIT (0xa4 << 2)
  91. #define CMN_PLL1_VCOCAL_ITER (0xa5 << 2)
  92. #define CMN_PLL1_LOCK_REFCNT_START (0xb0 << 2)
  93. #define CMN_PLL1_LOCK_PLLCNT_START (0xb2 << 2)
  94. #define CMN_PLL1_LOCK_PLLCNT_THR (0xb3 << 2)
  95. #define CMN_PLL1_INTDIV (0xb4 << 2)
  96. #define CMN_PLL1_FRACDIV (0xb5 << 2)
  97. #define CMN_PLL1_HIGH_THR (0xb6 << 2)
  98. #define CMN_PLL1_DSM_DIAG (0xb7 << 2)
  99. #define CMN_PLL1_SS_CTRL1 (0xb8 << 2)
  100. #define CMN_PLL1_SS_CTRL2 (0xb9 << 2)
  101. #define CMN_RXCAL_OVRD (0xd1 << 2)
  102. #define CMN_TXPUCAL_CTRL (0xe0 << 2)
  103. #define CMN_TXPUCAL_OVRD (0xe1 << 2)
  104. #define CMN_TXPDCAL_OVRD (0xf1 << 2)
  105. #define CMN_DIAG_PLL0_FBH_OVRD (0x1c0 << 2)
  106. #define CMN_DIAG_PLL0_FBL_OVRD (0x1c1 << 2)
  107. #define CMN_DIAG_PLL0_OVRD (0x1c2 << 2)
  108. #define CMN_DIAG_PLL0_V2I_TUNE (0x1c5 << 2)
  109. #define CMN_DIAG_PLL0_CP_TUNE (0x1c6 << 2)
  110. #define CMN_DIAG_PLL0_LF_PROG (0x1c7 << 2)
  111. #define CMN_DIAG_PLL1_FBH_OVRD (0x1d0 << 2)
  112. #define CMN_DIAG_PLL1_FBL_OVRD (0x1d1 << 2)
  113. #define CMN_DIAG_PLL1_OVRD (0x1d2 << 2)
  114. #define CMN_DIAG_PLL1_V2I_TUNE (0x1d5 << 2)
  115. #define CMN_DIAG_PLL1_CP_TUNE (0x1d6 << 2)
  116. #define CMN_DIAG_PLL1_LF_PROG (0x1d7 << 2)
  117. #define CMN_DIAG_PLL1_PTATIS_TUNE1 (0x1d8 << 2)
  118. #define CMN_DIAG_PLL1_PTATIS_TUNE2 (0x1d9 << 2)
  119. #define CMN_DIAG_PLL1_INCLK_CTRL (0x1da << 2)
  120. #define CMN_DIAG_HSCLK_SEL (0x1e0 << 2)
  121. #define XCVR_PSM_RCTRL(n) ((0x4001 | ((n) << 9)) << 2)
  122. #define XCVR_PSM_CAL_TMR(n) ((0x4002 | ((n) << 9)) << 2)
  123. #define XCVR_PSM_A0IN_TMR(n) ((0x4003 | ((n) << 9)) << 2)
  124. #define TX_TXCC_CAL_SCLR_MULT(n) ((0x4047 | ((n) << 9)) << 2)
  125. #define TX_TXCC_CPOST_MULT_00(n) ((0x404c | ((n) << 9)) << 2)
  126. #define TX_TXCC_CPOST_MULT_01(n) ((0x404d | ((n) << 9)) << 2)
  127. #define TX_TXCC_CPOST_MULT_10(n) ((0x404e | ((n) << 9)) << 2)
  128. #define TX_TXCC_CPOST_MULT_11(n) ((0x404f | ((n) << 9)) << 2)
  129. #define TX_TXCC_MGNFS_MULT_000(n) ((0x4050 | ((n) << 9)) << 2)
  130. #define TX_TXCC_MGNFS_MULT_001(n) ((0x4051 | ((n) << 9)) << 2)
  131. #define TX_TXCC_MGNFS_MULT_010(n) ((0x4052 | ((n) << 9)) << 2)
  132. #define TX_TXCC_MGNFS_MULT_011(n) ((0x4053 | ((n) << 9)) << 2)
  133. #define TX_TXCC_MGNFS_MULT_100(n) ((0x4054 | ((n) << 9)) << 2)
  134. #define TX_TXCC_MGNFS_MULT_101(n) ((0x4055 | ((n) << 9)) << 2)
  135. #define TX_TXCC_MGNFS_MULT_110(n) ((0x4056 | ((n) << 9)) << 2)
  136. #define TX_TXCC_MGNFS_MULT_111(n) ((0x4057 | ((n) << 9)) << 2)
  137. #define XCVR_DIAG_PLLDRC_CTRL(n) ((0x40e0 | ((n) << 9)) << 2)
  138. #define XCVR_DIAG_BIDI_CTRL(n) ((0x40e8 | ((n) << 9)) << 2)
  139. #define XCVR_DIAG_LANE_FCM_EN_MGN(n) ((0x40f2 | ((n) << 9)) << 2)
  140. #define TX_PSC_A0(n) ((0x4100 | ((n) << 9)) << 2)
  141. #define TX_PSC_A1(n) ((0x4101 | ((n) << 9)) << 2)
  142. #define TX_PSC_A2(n) ((0x4102 | ((n) << 9)) << 2)
  143. #define TX_PSC_A3(n) ((0x4103 | ((n) << 9)) << 2)
  144. #define TX_RCVDET_CTRL(n) ((0x4120 | ((n) << 9)) << 2)
  145. #define TX_RCVDET_EN_TMR(n) ((0x4122 | ((n) << 9)) << 2)
  146. #define TX_RCVDET_ST_TMR(n) ((0x4123 | ((n) << 9)) << 2)
  147. #define TX_DIAG_TX_DRV(n) ((0x41e1 | ((n) << 9)) << 2)
  148. #define TX_DIAG_BGREF_PREDRV_DELAY (0x41e7 << 2)
  149. #define TX_ANA_CTRL_REG_1 (0x5020 << 2)
  150. #define TX_ANA_CTRL_REG_2 (0x5021 << 2)
  151. #define TXDA_COEFF_CALC_CTRL (0x5022 << 2)
  152. #define TX_DIG_CTRL_REG_2 (0x5024 << 2)
  153. #define TXDA_CYA_AUXDA_CYA (0x5025 << 2)
  154. #define TX_ANA_CTRL_REG_3 (0x5026 << 2)
  155. #define TX_ANA_CTRL_REG_4 (0x5027 << 2)
  156. #define TX_ANA_CTRL_REG_5 (0x5029 << 2)
  157. #define RX_PSC_A0(n) ((0x8000 | ((n) << 9)) << 2)
  158. #define RX_PSC_A1(n) ((0x8001 | ((n) << 9)) << 2)
  159. #define RX_PSC_A2(n) ((0x8002 | ((n) << 9)) << 2)
  160. #define RX_PSC_A3(n) ((0x8003 | ((n) << 9)) << 2)
  161. #define RX_PSC_CAL(n) ((0x8006 | ((n) << 9)) << 2)
  162. #define RX_PSC_RDY(n) ((0x8007 | ((n) << 9)) << 2)
  163. #define RX_IQPI_ILL_CAL_OVRD (0x8023 << 2)
  164. #define RX_EPI_ILL_CAL_OVRD (0x8033 << 2)
  165. #define RX_SDCAL0_OVRD (0x8041 << 2)
  166. #define RX_SDCAL1_OVRD (0x8049 << 2)
  167. #define RX_SLC_INIT (0x806d << 2)
  168. #define RX_SLC_RUN (0x806e << 2)
  169. #define RX_CDRLF_CNFG2 (0x8081 << 2)
  170. #define RX_SIGDET_HL_FILT_TMR(n) ((0x8090 | ((n) << 9)) << 2)
  171. #define RX_SLC_IOP0_OVRD (0x8101 << 2)
  172. #define RX_SLC_IOP1_OVRD (0x8105 << 2)
  173. #define RX_SLC_QOP0_OVRD (0x8109 << 2)
  174. #define RX_SLC_QOP1_OVRD (0x810d << 2)
  175. #define RX_SLC_EOP0_OVRD (0x8111 << 2)
  176. #define RX_SLC_EOP1_OVRD (0x8115 << 2)
  177. #define RX_SLC_ION0_OVRD (0x8119 << 2)
  178. #define RX_SLC_ION1_OVRD (0x811d << 2)
  179. #define RX_SLC_QON0_OVRD (0x8121 << 2)
  180. #define RX_SLC_QON1_OVRD (0x8125 << 2)
  181. #define RX_SLC_EON0_OVRD (0x8129 << 2)
  182. #define RX_SLC_EON1_OVRD (0x812d << 2)
  183. #define RX_SLC_IEP0_OVRD (0x8131 << 2)
  184. #define RX_SLC_IEP1_OVRD (0x8135 << 2)
  185. #define RX_SLC_QEP0_OVRD (0x8139 << 2)
  186. #define RX_SLC_QEP1_OVRD (0x813d << 2)
  187. #define RX_SLC_EEP0_OVRD (0x8141 << 2)
  188. #define RX_SLC_EEP1_OVRD (0x8145 << 2)
  189. #define RX_SLC_IEN0_OVRD (0x8149 << 2)
  190. #define RX_SLC_IEN1_OVRD (0x814d << 2)
  191. #define RX_SLC_QEN0_OVRD (0x8151 << 2)
  192. #define RX_SLC_QEN1_OVRD (0x8155 << 2)
  193. #define RX_SLC_EEN0_OVRD (0x8159 << 2)
  194. #define RX_SLC_EEN1_OVRD (0x815d << 2)
  195. #define RX_REE_CTRL_DATA_MASK(n) ((0x81bb | ((n) << 9)) << 2)
  196. #define RX_DIAG_SIGDET_TUNE(n) ((0x81dc | ((n) << 9)) << 2)
  197. #define RX_DIAG_SC2C_DELAY (0x81e1 << 2)
  198. #define PMA_LANE_CFG (0xc000 << 2)
  199. #define PIPE_CMN_CTRL1 (0xc001 << 2)
  200. #define PIPE_CMN_CTRL2 (0xc002 << 2)
  201. #define PIPE_COM_LOCK_CFG1 (0xc003 << 2)
  202. #define PIPE_COM_LOCK_CFG2 (0xc004 << 2)
  203. #define PIPE_RCV_DET_INH (0xc005 << 2)
  204. #define DP_MODE_CTL (0xc008 << 2)
  205. #define DP_CLK_CTL (0xc009 << 2)
  206. #define STS (0xc00F << 2)
  207. #define PHY_ISO_CMN_CTRL (0xc010 << 2)
  208. #define PHY_DP_TX_CTL (0xc408 << 2)
  209. #define PMA_CMN_CTRL1 (0xc800 << 2)
  210. #define PHY_PMA_ISO_CMN_CTRL (0xc810 << 2)
  211. #define PHY_ISOLATION_CTRL (0xc81f << 2)
  212. #define PHY_PMA_ISO_XCVR_CTRL(n) ((0xcc11 | ((n) << 6)) << 2)
  213. #define PHY_PMA_ISO_LINK_MODE(n) ((0xcc12 | ((n) << 6)) << 2)
  214. #define PHY_PMA_ISO_PWRST_CTRL(n) ((0xcc13 | ((n) << 6)) << 2)
  215. #define PHY_PMA_ISO_TX_DATA_LO(n) ((0xcc14 | ((n) << 6)) << 2)
  216. #define PHY_PMA_ISO_TX_DATA_HI(n) ((0xcc15 | ((n) << 6)) << 2)
  217. #define PHY_PMA_ISO_RX_DATA_LO(n) ((0xcc16 | ((n) << 6)) << 2)
  218. #define PHY_PMA_ISO_RX_DATA_HI(n) ((0xcc17 | ((n) << 6)) << 2)
  219. #define TX_BIST_CTRL(n) ((0x4140 | ((n) << 9)) << 2)
  220. #define TX_BIST_UDDWR(n) ((0x4141 | ((n) << 9)) << 2)
  221. /*
  222. * Selects which PLL clock will be driven on the analog high speed
  223. * clock 0: PLL 0 div 1
  224. * clock 1: PLL 1 div 2
  225. */
  226. #define CLK_PLL_CONFIG 0X30
  227. #define CLK_PLL_MASK 0x33
  228. #define CMN_READY BIT(0)
  229. #define DP_PLL_CLOCK_ENABLE BIT(2)
  230. #define DP_PLL_ENABLE BIT(0)
  231. #define DP_PLL_DATA_RATE_RBR ((2 << 12) | (4 << 8))
  232. #define DP_PLL_DATA_RATE_HBR ((2 << 12) | (4 << 8))
  233. #define DP_PLL_DATA_RATE_HBR2 ((1 << 12) | (2 << 8))
  234. #define DP_MODE_A0 BIT(4)
  235. #define DP_MODE_A2 BIT(6)
  236. #define DP_MODE_ENTER_A0 0xc101
  237. #define DP_MODE_ENTER_A2 0xc104
  238. #define PHY_MODE_SET_TIMEOUT 100000
  239. #define PIN_ASSIGN_C_E 0x51d9
  240. #define PIN_ASSIGN_D_F 0x5100
  241. #define MODE_DISCONNECT 0
  242. #define MODE_UFP_USB BIT(0)
  243. #define MODE_DFP_USB BIT(1)
  244. #define MODE_DFP_DP BIT(2)
  245. struct usb3phy_reg {
  246. u32 offset;
  247. u32 enable_bit;
  248. u32 write_enable;
  249. };
  250. struct rockchip_usb3phy_port_cfg {
  251. struct usb3phy_reg typec_conn_dir;
  252. struct usb3phy_reg usb3tousb2_en;
  253. struct usb3phy_reg external_psm;
  254. struct usb3phy_reg pipe_status;
  255. };
  256. struct rockchip_typec_phy {
  257. struct device *dev;
  258. void __iomem *base;
  259. struct extcon_dev *extcon;
  260. struct regmap *grf_regs;
  261. struct clk *clk_core;
  262. struct clk *clk_ref;
  263. struct reset_control *uphy_rst;
  264. struct reset_control *pipe_rst;
  265. struct reset_control *tcphy_rst;
  266. struct rockchip_usb3phy_port_cfg port_cfgs;
  267. /* mutex to protect access to individual PHYs */
  268. struct mutex lock;
  269. bool flip;
  270. u8 mode;
  271. };
  272. struct phy_reg {
  273. u16 value;
  274. u32 addr;
  275. };
  276. struct phy_reg usb3_pll_cfg[] = {
  277. { 0xf0, CMN_PLL0_VCOCAL_INIT },
  278. { 0x18, CMN_PLL0_VCOCAL_ITER },
  279. { 0xd0, CMN_PLL0_INTDIV },
  280. { 0x4a4a, CMN_PLL0_FRACDIV },
  281. { 0x34, CMN_PLL0_HIGH_THR },
  282. { 0x1ee, CMN_PLL0_SS_CTRL1 },
  283. { 0x7f03, CMN_PLL0_SS_CTRL2 },
  284. { 0x20, CMN_PLL0_DSM_DIAG },
  285. { 0, CMN_DIAG_PLL0_OVRD },
  286. { 0, CMN_DIAG_PLL0_FBH_OVRD },
  287. { 0, CMN_DIAG_PLL0_FBL_OVRD },
  288. { 0x7, CMN_DIAG_PLL0_V2I_TUNE },
  289. { 0x45, CMN_DIAG_PLL0_CP_TUNE },
  290. { 0x8, CMN_DIAG_PLL0_LF_PROG },
  291. };
  292. struct phy_reg dp_pll_cfg[] = {
  293. { 0xf0, CMN_PLL1_VCOCAL_INIT },
  294. { 0x18, CMN_PLL1_VCOCAL_ITER },
  295. { 0x30b9, CMN_PLL1_VCOCAL_START },
  296. { 0x21c, CMN_PLL1_INTDIV },
  297. { 0, CMN_PLL1_FRACDIV },
  298. { 0x5, CMN_PLL1_HIGH_THR },
  299. { 0x35, CMN_PLL1_SS_CTRL1 },
  300. { 0x7f1e, CMN_PLL1_SS_CTRL2 },
  301. { 0x20, CMN_PLL1_DSM_DIAG },
  302. { 0, CMN_PLLSM1_USER_DEF_CTRL },
  303. { 0, CMN_DIAG_PLL1_OVRD },
  304. { 0, CMN_DIAG_PLL1_FBH_OVRD },
  305. { 0, CMN_DIAG_PLL1_FBL_OVRD },
  306. { 0x6, CMN_DIAG_PLL1_V2I_TUNE },
  307. { 0x45, CMN_DIAG_PLL1_CP_TUNE },
  308. { 0x8, CMN_DIAG_PLL1_LF_PROG },
  309. { 0x100, CMN_DIAG_PLL1_PTATIS_TUNE1 },
  310. { 0x7, CMN_DIAG_PLL1_PTATIS_TUNE2 },
  311. { 0x4, CMN_DIAG_PLL1_INCLK_CTRL },
  312. };
  313. static void tcphy_cfg_24m(struct rockchip_typec_phy *tcphy)
  314. {
  315. u32 i, rdata;
  316. /*
  317. * cmn_ref_clk_sel = 3, select the 24Mhz for clk parent
  318. * cmn_psm_clk_dig_div = 2, set the clk division to 2
  319. */
  320. writel(0x830, tcphy->base + PMA_CMN_CTRL1);
  321. for (i = 0; i < 4; i++) {
  322. /*
  323. * The following PHY configuration assumes a 24 MHz reference
  324. * clock.
  325. */
  326. writel(0x90, tcphy->base + XCVR_DIAG_LANE_FCM_EN_MGN(i));
  327. writel(0x960, tcphy->base + TX_RCVDET_EN_TMR(i));
  328. writel(0x30, tcphy->base + TX_RCVDET_ST_TMR(i));
  329. }
  330. rdata = readl(tcphy->base + CMN_DIAG_HSCLK_SEL);
  331. rdata &= ~CLK_PLL_MASK;
  332. rdata |= CLK_PLL_CONFIG;
  333. writel(rdata, tcphy->base + CMN_DIAG_HSCLK_SEL);
  334. }
  335. static void tcphy_cfg_usb3_pll(struct rockchip_typec_phy *tcphy)
  336. {
  337. u32 i;
  338. /* load the configuration of PLL0 */
  339. for (i = 0; i < ARRAY_SIZE(usb3_pll_cfg); i++)
  340. writel(usb3_pll_cfg[i].value,
  341. tcphy->base + usb3_pll_cfg[i].addr);
  342. }
  343. static void tcphy_cfg_dp_pll(struct rockchip_typec_phy *tcphy)
  344. {
  345. u32 i;
  346. /* set the default mode to RBR */
  347. writel(DP_PLL_CLOCK_ENABLE | DP_PLL_ENABLE | DP_PLL_DATA_RATE_RBR,
  348. tcphy->base + DP_CLK_CTL);
  349. /* load the configuration of PLL1 */
  350. for (i = 0; i < ARRAY_SIZE(dp_pll_cfg); i++)
  351. writel(dp_pll_cfg[i].value, tcphy->base + dp_pll_cfg[i].addr);
  352. }
  353. static void tcphy_tx_usb3_cfg_lane(struct rockchip_typec_phy *tcphy, u32 lane)
  354. {
  355. writel(0x7799, tcphy->base + TX_PSC_A0(lane));
  356. writel(0x7798, tcphy->base + TX_PSC_A1(lane));
  357. writel(0x5098, tcphy->base + TX_PSC_A2(lane));
  358. writel(0x5098, tcphy->base + TX_PSC_A3(lane));
  359. writel(0, tcphy->base + TX_TXCC_MGNFS_MULT_000(lane));
  360. writel(0xbf, tcphy->base + XCVR_DIAG_BIDI_CTRL(lane));
  361. }
  362. static void tcphy_rx_usb3_cfg_lane(struct rockchip_typec_phy *tcphy, u32 lane)
  363. {
  364. writel(0xa6fd, tcphy->base + RX_PSC_A0(lane));
  365. writel(0xa6fd, tcphy->base + RX_PSC_A1(lane));
  366. writel(0xa410, tcphy->base + RX_PSC_A2(lane));
  367. writel(0x2410, tcphy->base + RX_PSC_A3(lane));
  368. writel(0x23ff, tcphy->base + RX_PSC_CAL(lane));
  369. writel(0x13, tcphy->base + RX_SIGDET_HL_FILT_TMR(lane));
  370. writel(0x03e7, tcphy->base + RX_REE_CTRL_DATA_MASK(lane));
  371. writel(0x1004, tcphy->base + RX_DIAG_SIGDET_TUNE(lane));
  372. writel(0x2010, tcphy->base + RX_PSC_RDY(lane));
  373. writel(0xfb, tcphy->base + XCVR_DIAG_BIDI_CTRL(lane));
  374. }
  375. static void tcphy_dp_cfg_lane(struct rockchip_typec_phy *tcphy, u32 lane)
  376. {
  377. u16 rdata;
  378. writel(0xbefc, tcphy->base + XCVR_PSM_RCTRL(lane));
  379. writel(0x6799, tcphy->base + TX_PSC_A0(lane));
  380. writel(0x6798, tcphy->base + TX_PSC_A1(lane));
  381. writel(0x98, tcphy->base + TX_PSC_A2(lane));
  382. writel(0x98, tcphy->base + TX_PSC_A3(lane));
  383. writel(0, tcphy->base + TX_TXCC_MGNFS_MULT_000(lane));
  384. writel(0, tcphy->base + TX_TXCC_MGNFS_MULT_001(lane));
  385. writel(0, tcphy->base + TX_TXCC_MGNFS_MULT_010(lane));
  386. writel(0, tcphy->base + TX_TXCC_MGNFS_MULT_011(lane));
  387. writel(0, tcphy->base + TX_TXCC_MGNFS_MULT_100(lane));
  388. writel(0, tcphy->base + TX_TXCC_MGNFS_MULT_101(lane));
  389. writel(0, tcphy->base + TX_TXCC_MGNFS_MULT_110(lane));
  390. writel(0, tcphy->base + TX_TXCC_MGNFS_MULT_111(lane));
  391. writel(0, tcphy->base + TX_TXCC_CPOST_MULT_10(lane));
  392. writel(0, tcphy->base + TX_TXCC_CPOST_MULT_01(lane));
  393. writel(0, tcphy->base + TX_TXCC_CPOST_MULT_00(lane));
  394. writel(0, tcphy->base + TX_TXCC_CPOST_MULT_11(lane));
  395. writel(0x128, tcphy->base + TX_TXCC_CAL_SCLR_MULT(lane));
  396. writel(0x400, tcphy->base + TX_DIAG_TX_DRV(lane));
  397. rdata = readl(tcphy->base + XCVR_DIAG_PLLDRC_CTRL(lane));
  398. rdata = (rdata & 0x8fff) | 0x6000;
  399. writel(rdata, tcphy->base + XCVR_DIAG_PLLDRC_CTRL(lane));
  400. }
  401. static inline int property_enable(struct rockchip_typec_phy *tcphy,
  402. const struct usb3phy_reg *reg, bool en)
  403. {
  404. u32 mask = 1 << reg->write_enable;
  405. u32 val = en << reg->enable_bit;
  406. return regmap_write(tcphy->grf_regs, reg->offset, val | mask);
  407. }
  408. static void tcphy_dp_aux_calibration(struct rockchip_typec_phy *tcphy)
  409. {
  410. u16 rdata, rdata2, val;
  411. /* disable txda_cal_latch_en for rewrite the calibration values */
  412. rdata = readl(tcphy->base + TX_ANA_CTRL_REG_1);
  413. val = rdata & 0xdfff;
  414. writel(val, tcphy->base + TX_ANA_CTRL_REG_1);
  415. /*
  416. * read a resistor calibration code from CMN_TXPUCAL_CTRL[6:0] and
  417. * write it to TX_DIG_CTRL_REG_2[6:0], and delay 1ms to make sure it
  418. * works.
  419. */
  420. rdata = readl(tcphy->base + TX_DIG_CTRL_REG_2);
  421. rdata = rdata & 0xffc0;
  422. rdata2 = readl(tcphy->base + CMN_TXPUCAL_CTRL);
  423. rdata2 = rdata2 & 0x3f;
  424. val = rdata | rdata2;
  425. writel(val, tcphy->base + TX_DIG_CTRL_REG_2);
  426. usleep_range(1000, 1050);
  427. /*
  428. * Enable signal for latch that sample and holds calibration values.
  429. * Activate this signal for 1 clock cycle to sample new calibration
  430. * values.
  431. */
  432. rdata = readl(tcphy->base + TX_ANA_CTRL_REG_1);
  433. val = rdata | 0x2000;
  434. writel(val, tcphy->base + TX_ANA_CTRL_REG_1);
  435. usleep_range(150, 200);
  436. /* set TX Voltage Level and TX Deemphasis to 0 */
  437. writel(0, tcphy->base + PHY_DP_TX_CTL);
  438. /* re-enable decap */
  439. writel(0x100, tcphy->base + TX_ANA_CTRL_REG_2);
  440. writel(0x300, tcphy->base + TX_ANA_CTRL_REG_2);
  441. writel(0x2008, tcphy->base + TX_ANA_CTRL_REG_1);
  442. writel(0x2018, tcphy->base + TX_ANA_CTRL_REG_1);
  443. writel(0, tcphy->base + TX_ANA_CTRL_REG_5);
  444. /*
  445. * Programs txda_drv_ldo_prog[15:0], Sets driver LDO
  446. * voltage 16'h1001 for DP-AUX-TX and RX
  447. */
  448. writel(0x1001, tcphy->base + TX_ANA_CTRL_REG_4);
  449. /* re-enables Bandgap reference for LDO */
  450. writel(0x2098, tcphy->base + TX_ANA_CTRL_REG_1);
  451. writel(0x2198, tcphy->base + TX_ANA_CTRL_REG_1);
  452. /*
  453. * re-enables the transmitter pre-driver, driver data selection MUX,
  454. * and receiver detect circuits.
  455. */
  456. writel(0x301, tcphy->base + TX_ANA_CTRL_REG_2);
  457. writel(0x303, tcphy->base + TX_ANA_CTRL_REG_2);
  458. /*
  459. * BIT 12: Controls auxda_polarity, which selects the polarity of the
  460. * xcvr:
  461. * 1, Reverses the polarity (If TYPEC, Pulls ups aux_p and pull
  462. * down aux_m)
  463. * 0, Normal polarity (if TYPE_C, pulls up aux_m and pulls down
  464. * aux_p)
  465. */
  466. val = 0xa078;
  467. if (!tcphy->flip)
  468. val |= BIT(12);
  469. writel(val, tcphy->base + TX_ANA_CTRL_REG_1);
  470. writel(0, tcphy->base + TX_ANA_CTRL_REG_3);
  471. writel(0, tcphy->base + TX_ANA_CTRL_REG_4);
  472. writel(0, tcphy->base + TX_ANA_CTRL_REG_5);
  473. /*
  474. * Controls low_power_swing_en, set the voltage swing of the driver
  475. * to 400mv. The values below are peak to peak (differential) values.
  476. */
  477. writel(4, tcphy->base + TXDA_COEFF_CALC_CTRL);
  478. writel(0, tcphy->base + TXDA_CYA_AUXDA_CYA);
  479. /* Controls tx_high_z_tm_en */
  480. val = readl(tcphy->base + TX_DIG_CTRL_REG_2);
  481. val |= BIT(15);
  482. writel(val, tcphy->base + TX_DIG_CTRL_REG_2);
  483. }
  484. static int tcphy_phy_init(struct rockchip_typec_phy *tcphy, u8 mode)
  485. {
  486. struct rockchip_usb3phy_port_cfg *cfg = &tcphy->port_cfgs;
  487. int ret, i;
  488. u32 val;
  489. ret = clk_prepare_enable(tcphy->clk_core);
  490. if (ret) {
  491. dev_err(tcphy->dev, "Failed to prepare_enable core clock\n");
  492. return ret;
  493. }
  494. ret = clk_prepare_enable(tcphy->clk_ref);
  495. if (ret) {
  496. dev_err(tcphy->dev, "Failed to prepare_enable ref clock\n");
  497. goto err_clk_core;
  498. }
  499. reset_control_deassert(tcphy->tcphy_rst);
  500. property_enable(tcphy, &cfg->typec_conn_dir, tcphy->flip);
  501. tcphy_cfg_24m(tcphy);
  502. if (mode == MODE_DFP_DP) {
  503. tcphy_cfg_dp_pll(tcphy);
  504. for (i = 0; i < 4; i++)
  505. tcphy_dp_cfg_lane(tcphy, i);
  506. writel(PIN_ASSIGN_C_E, tcphy->base + PMA_LANE_CFG);
  507. } else {
  508. tcphy_cfg_usb3_pll(tcphy);
  509. tcphy_cfg_dp_pll(tcphy);
  510. if (tcphy->flip) {
  511. tcphy_tx_usb3_cfg_lane(tcphy, 3);
  512. tcphy_rx_usb3_cfg_lane(tcphy, 2);
  513. tcphy_dp_cfg_lane(tcphy, 0);
  514. tcphy_dp_cfg_lane(tcphy, 1);
  515. } else {
  516. tcphy_tx_usb3_cfg_lane(tcphy, 0);
  517. tcphy_rx_usb3_cfg_lane(tcphy, 1);
  518. tcphy_dp_cfg_lane(tcphy, 2);
  519. tcphy_dp_cfg_lane(tcphy, 3);
  520. }
  521. writel(PIN_ASSIGN_D_F, tcphy->base + PMA_LANE_CFG);
  522. }
  523. writel(DP_MODE_ENTER_A2, tcphy->base + DP_MODE_CTL);
  524. reset_control_deassert(tcphy->uphy_rst);
  525. ret = readx_poll_timeout(readl, tcphy->base + PMA_CMN_CTRL1,
  526. val, val & CMN_READY, 10,
  527. PHY_MODE_SET_TIMEOUT);
  528. if (ret < 0) {
  529. dev_err(tcphy->dev, "wait pma ready timeout\n");
  530. ret = -ETIMEDOUT;
  531. goto err_wait_pma;
  532. }
  533. reset_control_deassert(tcphy->pipe_rst);
  534. return 0;
  535. err_wait_pma:
  536. reset_control_assert(tcphy->uphy_rst);
  537. reset_control_assert(tcphy->tcphy_rst);
  538. clk_disable_unprepare(tcphy->clk_ref);
  539. err_clk_core:
  540. clk_disable_unprepare(tcphy->clk_core);
  541. return ret;
  542. }
  543. static void tcphy_phy_deinit(struct rockchip_typec_phy *tcphy)
  544. {
  545. reset_control_assert(tcphy->tcphy_rst);
  546. reset_control_assert(tcphy->uphy_rst);
  547. reset_control_assert(tcphy->pipe_rst);
  548. clk_disable_unprepare(tcphy->clk_core);
  549. clk_disable_unprepare(tcphy->clk_ref);
  550. }
  551. static int tcphy_get_mode(struct rockchip_typec_phy *tcphy)
  552. {
  553. struct extcon_dev *edev = tcphy->extcon;
  554. union extcon_property_value property;
  555. unsigned int id;
  556. bool dfp, ufp, dp;
  557. u8 mode;
  558. int ret;
  559. ufp = extcon_get_state(edev, EXTCON_USB);
  560. dfp = extcon_get_state(edev, EXTCON_USB_HOST);
  561. dp = extcon_get_state(edev, EXTCON_DISP_DP);
  562. mode = MODE_DFP_USB;
  563. id = EXTCON_USB_HOST;
  564. if (ufp) {
  565. mode = MODE_UFP_USB;
  566. id = EXTCON_USB;
  567. } else if (dp) {
  568. mode = MODE_DFP_DP;
  569. id = EXTCON_DISP_DP;
  570. ret = extcon_get_property(edev, id, EXTCON_PROP_USB_SS,
  571. &property);
  572. if (ret) {
  573. dev_err(tcphy->dev, "get superspeed property failed\n");
  574. return ret;
  575. }
  576. if (property.intval)
  577. mode |= MODE_DFP_USB;
  578. }
  579. ret = extcon_get_property(edev, id, EXTCON_PROP_USB_TYPEC_POLARITY,
  580. &property);
  581. if (ret) {
  582. dev_err(tcphy->dev, "get polarity property failed\n");
  583. return ret;
  584. }
  585. tcphy->flip = property.intval ? 1 : 0;
  586. return mode;
  587. }
  588. static int rockchip_usb3_phy_power_on(struct phy *phy)
  589. {
  590. struct rockchip_typec_phy *tcphy = phy_get_drvdata(phy);
  591. struct rockchip_usb3phy_port_cfg *cfg = &tcphy->port_cfgs;
  592. const struct usb3phy_reg *reg = &cfg->pipe_status;
  593. int timeout, new_mode, ret = 0;
  594. u32 val;
  595. mutex_lock(&tcphy->lock);
  596. new_mode = tcphy_get_mode(tcphy);
  597. if (new_mode < 0) {
  598. ret = new_mode;
  599. goto unlock_ret;
  600. }
  601. /* DP-only mode; fall back to USB2 */
  602. if (!(new_mode & (MODE_DFP_USB | MODE_UFP_USB)))
  603. goto unlock_ret;
  604. if (tcphy->mode == new_mode)
  605. goto unlock_ret;
  606. if (tcphy->mode == MODE_DISCONNECT)
  607. tcphy_phy_init(tcphy, new_mode);
  608. /* wait TCPHY for pipe ready */
  609. for (timeout = 0; timeout < 100; timeout++) {
  610. regmap_read(tcphy->grf_regs, reg->offset, &val);
  611. if (!(val & BIT(reg->enable_bit))) {
  612. tcphy->mode |= new_mode & (MODE_DFP_USB | MODE_UFP_USB);
  613. goto unlock_ret;
  614. }
  615. usleep_range(10, 20);
  616. }
  617. if (tcphy->mode == MODE_DISCONNECT)
  618. tcphy_phy_deinit(tcphy);
  619. ret = -ETIMEDOUT;
  620. unlock_ret:
  621. mutex_unlock(&tcphy->lock);
  622. return ret;
  623. }
  624. static int rockchip_usb3_phy_power_off(struct phy *phy)
  625. {
  626. struct rockchip_typec_phy *tcphy = phy_get_drvdata(phy);
  627. mutex_lock(&tcphy->lock);
  628. if (tcphy->mode == MODE_DISCONNECT)
  629. goto unlock;
  630. tcphy->mode &= ~(MODE_UFP_USB | MODE_DFP_USB);
  631. if (tcphy->mode == MODE_DISCONNECT)
  632. tcphy_phy_deinit(tcphy);
  633. unlock:
  634. mutex_unlock(&tcphy->lock);
  635. return 0;
  636. }
  637. static const struct phy_ops rockchip_usb3_phy_ops = {
  638. .power_on = rockchip_usb3_phy_power_on,
  639. .power_off = rockchip_usb3_phy_power_off,
  640. .owner = THIS_MODULE,
  641. };
  642. static int rockchip_dp_phy_power_on(struct phy *phy)
  643. {
  644. struct rockchip_typec_phy *tcphy = phy_get_drvdata(phy);
  645. int new_mode, ret = 0;
  646. u32 val;
  647. mutex_lock(&tcphy->lock);
  648. new_mode = tcphy_get_mode(tcphy);
  649. if (new_mode < 0) {
  650. ret = new_mode;
  651. goto unlock_ret;
  652. }
  653. if (!(new_mode & MODE_DFP_DP)) {
  654. ret = -ENODEV;
  655. goto unlock_ret;
  656. }
  657. if (tcphy->mode == new_mode)
  658. goto unlock_ret;
  659. /*
  660. * If the PHY has been power on, but the mode is not DP only mode,
  661. * re-init the PHY for setting all of 4 lanes to DP.
  662. */
  663. if (new_mode == MODE_DFP_DP && tcphy->mode != MODE_DISCONNECT) {
  664. tcphy_phy_deinit(tcphy);
  665. tcphy_phy_init(tcphy, new_mode);
  666. } else if (tcphy->mode == MODE_DISCONNECT) {
  667. tcphy_phy_init(tcphy, new_mode);
  668. }
  669. ret = readx_poll_timeout(readl, tcphy->base + DP_MODE_CTL,
  670. val, val & DP_MODE_A2, 1000,
  671. PHY_MODE_SET_TIMEOUT);
  672. if (ret < 0) {
  673. dev_err(tcphy->dev, "failed to wait TCPHY enter A2\n");
  674. goto power_on_finish;
  675. }
  676. tcphy_dp_aux_calibration(tcphy);
  677. writel(DP_MODE_ENTER_A0, tcphy->base + DP_MODE_CTL);
  678. ret = readx_poll_timeout(readl, tcphy->base + DP_MODE_CTL,
  679. val, val & DP_MODE_A0, 1000,
  680. PHY_MODE_SET_TIMEOUT);
  681. if (ret < 0) {
  682. writel(DP_MODE_ENTER_A2, tcphy->base + DP_MODE_CTL);
  683. dev_err(tcphy->dev, "failed to wait TCPHY enter A0\n");
  684. goto power_on_finish;
  685. }
  686. tcphy->mode |= MODE_DFP_DP;
  687. power_on_finish:
  688. if (tcphy->mode == MODE_DISCONNECT)
  689. tcphy_phy_deinit(tcphy);
  690. unlock_ret:
  691. mutex_unlock(&tcphy->lock);
  692. return ret;
  693. }
  694. static int rockchip_dp_phy_power_off(struct phy *phy)
  695. {
  696. struct rockchip_typec_phy *tcphy = phy_get_drvdata(phy);
  697. mutex_lock(&tcphy->lock);
  698. if (tcphy->mode == MODE_DISCONNECT)
  699. goto unlock;
  700. tcphy->mode &= ~MODE_DFP_DP;
  701. writel(DP_MODE_ENTER_A2, tcphy->base + DP_MODE_CTL);
  702. if (tcphy->mode == MODE_DISCONNECT)
  703. tcphy_phy_deinit(tcphy);
  704. unlock:
  705. mutex_unlock(&tcphy->lock);
  706. return 0;
  707. }
  708. static const struct phy_ops rockchip_dp_phy_ops = {
  709. .power_on = rockchip_dp_phy_power_on,
  710. .power_off = rockchip_dp_phy_power_off,
  711. .owner = THIS_MODULE,
  712. };
  713. static int tcphy_get_param(struct device *dev,
  714. struct usb3phy_reg *reg,
  715. const char *name)
  716. {
  717. u32 buffer[3];
  718. int ret;
  719. ret = of_property_read_u32_array(dev->of_node, name, buffer, 3);
  720. if (ret) {
  721. dev_err(dev, "Can not parse %s\n", name);
  722. return ret;
  723. }
  724. reg->offset = buffer[0];
  725. reg->enable_bit = buffer[1];
  726. reg->write_enable = buffer[2];
  727. return 0;
  728. }
  729. static int tcphy_parse_dt(struct rockchip_typec_phy *tcphy,
  730. struct device *dev)
  731. {
  732. struct rockchip_usb3phy_port_cfg *cfg = &tcphy->port_cfgs;
  733. int ret;
  734. ret = tcphy_get_param(dev, &cfg->typec_conn_dir,
  735. "rockchip,typec-conn-dir");
  736. if (ret)
  737. return ret;
  738. ret = tcphy_get_param(dev, &cfg->usb3tousb2_en,
  739. "rockchip,usb3tousb2-en");
  740. if (ret)
  741. return ret;
  742. ret = tcphy_get_param(dev, &cfg->external_psm,
  743. "rockchip,external-psm");
  744. if (ret)
  745. return ret;
  746. ret = tcphy_get_param(dev, &cfg->pipe_status,
  747. "rockchip,pipe-status");
  748. if (ret)
  749. return ret;
  750. tcphy->grf_regs = syscon_regmap_lookup_by_phandle(dev->of_node,
  751. "rockchip,grf");
  752. if (IS_ERR(tcphy->grf_regs)) {
  753. dev_err(dev, "could not find grf dt node\n");
  754. return PTR_ERR(tcphy->grf_regs);
  755. }
  756. tcphy->clk_core = devm_clk_get(dev, "tcpdcore");
  757. if (IS_ERR(tcphy->clk_core)) {
  758. dev_err(dev, "could not get uphy core clock\n");
  759. return PTR_ERR(tcphy->clk_core);
  760. }
  761. tcphy->clk_ref = devm_clk_get(dev, "tcpdphy-ref");
  762. if (IS_ERR(tcphy->clk_ref)) {
  763. dev_err(dev, "could not get uphy ref clock\n");
  764. return PTR_ERR(tcphy->clk_ref);
  765. }
  766. tcphy->uphy_rst = devm_reset_control_get(dev, "uphy");
  767. if (IS_ERR(tcphy->uphy_rst)) {
  768. dev_err(dev, "no uphy_rst reset control found\n");
  769. return PTR_ERR(tcphy->uphy_rst);
  770. }
  771. tcphy->pipe_rst = devm_reset_control_get(dev, "uphy-pipe");
  772. if (IS_ERR(tcphy->pipe_rst)) {
  773. dev_err(dev, "no pipe_rst reset control found\n");
  774. return PTR_ERR(tcphy->pipe_rst);
  775. }
  776. tcphy->tcphy_rst = devm_reset_control_get(dev, "uphy-tcphy");
  777. if (IS_ERR(tcphy->tcphy_rst)) {
  778. dev_err(dev, "no tcphy_rst reset control found\n");
  779. return PTR_ERR(tcphy->tcphy_rst);
  780. }
  781. return 0;
  782. }
  783. static void typec_phy_pre_init(struct rockchip_typec_phy *tcphy)
  784. {
  785. struct rockchip_usb3phy_port_cfg *cfg = &tcphy->port_cfgs;
  786. reset_control_assert(tcphy->tcphy_rst);
  787. reset_control_assert(tcphy->uphy_rst);
  788. reset_control_assert(tcphy->pipe_rst);
  789. /* select external psm clock */
  790. property_enable(tcphy, &cfg->external_psm, 1);
  791. property_enable(tcphy, &cfg->usb3tousb2_en, 0);
  792. tcphy->mode = MODE_DISCONNECT;
  793. }
  794. static int rockchip_typec_phy_probe(struct platform_device *pdev)
  795. {
  796. struct device *dev = &pdev->dev;
  797. struct device_node *np = dev->of_node;
  798. struct device_node *child_np;
  799. struct rockchip_typec_phy *tcphy;
  800. struct phy_provider *phy_provider;
  801. struct resource *res;
  802. int ret;
  803. tcphy = devm_kzalloc(dev, sizeof(*tcphy), GFP_KERNEL);
  804. if (!tcphy)
  805. return -ENOMEM;
  806. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  807. tcphy->base = devm_ioremap_resource(dev, res);
  808. if (IS_ERR(tcphy->base))
  809. return PTR_ERR(tcphy->base);
  810. ret = tcphy_parse_dt(tcphy, dev);
  811. if (ret)
  812. return ret;
  813. tcphy->dev = dev;
  814. platform_set_drvdata(pdev, tcphy);
  815. mutex_init(&tcphy->lock);
  816. typec_phy_pre_init(tcphy);
  817. tcphy->extcon = extcon_get_edev_by_phandle(dev, 0);
  818. if (IS_ERR(tcphy->extcon)) {
  819. if (PTR_ERR(tcphy->extcon) != -EPROBE_DEFER)
  820. dev_err(dev, "Invalid or missing extcon\n");
  821. return PTR_ERR(tcphy->extcon);
  822. }
  823. pm_runtime_enable(dev);
  824. for_each_available_child_of_node(np, child_np) {
  825. struct phy *phy;
  826. if (!of_node_cmp(child_np->name, "dp-port"))
  827. phy = devm_phy_create(dev, child_np,
  828. &rockchip_dp_phy_ops);
  829. else if (!of_node_cmp(child_np->name, "usb3-port"))
  830. phy = devm_phy_create(dev, child_np,
  831. &rockchip_usb3_phy_ops);
  832. else
  833. continue;
  834. if (IS_ERR(phy)) {
  835. dev_err(dev, "failed to create phy: %s\n",
  836. child_np->name);
  837. return PTR_ERR(phy);
  838. }
  839. phy_set_drvdata(phy, tcphy);
  840. }
  841. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  842. if (IS_ERR(phy_provider)) {
  843. dev_err(dev, "Failed to register phy provider\n");
  844. return PTR_ERR(phy_provider);
  845. }
  846. return 0;
  847. }
  848. static int rockchip_typec_phy_remove(struct platform_device *pdev)
  849. {
  850. pm_runtime_disable(&pdev->dev);
  851. return 0;
  852. }
  853. static const struct of_device_id rockchip_typec_phy_dt_ids[] = {
  854. { .compatible = "rockchip,rk3399-typec-phy" },
  855. {}
  856. };
  857. MODULE_DEVICE_TABLE(of, rockchip_typec_phy_dt_ids);
  858. static struct platform_driver rockchip_typec_phy_driver = {
  859. .probe = rockchip_typec_phy_probe,
  860. .remove = rockchip_typec_phy_remove,
  861. .driver = {
  862. .name = "rockchip-typec-phy",
  863. .of_match_table = rockchip_typec_phy_dt_ids,
  864. },
  865. };
  866. module_platform_driver(rockchip_typec_phy_driver);
  867. MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
  868. MODULE_AUTHOR("Kever Yang <kever.yang@rock-chips.com>");
  869. MODULE_DESCRIPTION("Rockchip USB TYPE-C PHY driver");
  870. MODULE_LICENSE("GPL v2");