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