1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Rockchip USBDP Combo PHY with Samsung IP block driver
4 *
5 * Copyright (C) 2022 Rockchip Electronics Co., Ltd
6 */
7
8 #include <linux/bitfield.h>
9 #include <linux/clk.h>
10 #include <linux/clk-provider.h>
11 #include <linux/delay.h>
12 #include <linux/gpio.h>
13 #include <linux/io.h>
14 #include <linux/iopoll.h>
15 #include <linux/kernel.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/of.h>
20 #include <linux/of_address.h>
21 #include <linux/of_platform.h>
22 #include <linux/phy/phy.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
25 #include <linux/reset.h>
26 #include <linux/usb/ch9.h>
27 #include <linux/usb/typec_dp.h>
28 #include <linux/usb/typec_mux.h>
29
30 #include <linux/phy/phy-rockchip-usbdp.h>
31
32 #define BIT_WRITEABLE_SHIFT 16
33 #define IO_MEM __iomem
34
35 enum {
36 DP_BW_RBR,
37 DP_BW_HBR,
38 DP_BW_HBR2,
39 DP_BW_HBR3,
40 };
41
42 enum {
43 UDPHY_MODE_NONE = 0,
44 UDPHY_MODE_USB = BIT(0),
45 UDPHY_MODE_DP = BIT(1),
46 UDPHY_MODE_DP_USB = BIT(1) | BIT(0),
47 };
48
49 struct udphy_grf_reg {
50 unsigned int offset;
51 unsigned int bitend;
52 unsigned int bitstart;
53 unsigned int disable;
54 unsigned int enable;
55 };
56
57 struct udphy_grf_cfg {
58 /* u2phy-grf */
59 struct udphy_grf_reg bvalid_phy_con;
60 struct udphy_grf_reg bvalid_grf_con;
61
62 /* usb-grf */
63 struct udphy_grf_reg usb3otg0_cfg;
64 struct udphy_grf_reg usb3otg1_cfg;
65
66 /* usbdpphy-grf */
67 struct udphy_grf_reg low_pwrn;
68 struct udphy_grf_reg rx_lfps;
69 };
70
71 struct udphy_vogrf_cfg {
72 /* vo-grf */
73 struct udphy_grf_reg hpd_trigger;
74 };
75
76 struct rockchip_udphy;
77
78 struct rockchip_udphy_cfg {
79 /* resets to be requested */
80 const char *const *rst_list;
81 int num_rsts;
82
83 struct udphy_grf_cfg grfcfg;
84 struct udphy_vogrf_cfg vogrfcfg[2];
85 int (*combophy_init)(struct rockchip_udphy *udphy);
86 int (*dp_phy_set_rate)(struct rockchip_udphy *udphy, struct phy_configure_opts_dp *dp);
87 int (*dp_phy_set_voltages)(struct rockchip_udphy *udphy, struct phy_configure_opts_dp *dp);
88 int (*hpd_event_trigger)(struct rockchip_udphy *udphy, bool hpd);
89 int (*dplane_enable)(struct rockchip_udphy *udphy, int dp_lanes);
90 int (*dplane_select)(struct rockchip_udphy *udphy);
91 };
92
93 struct rockchip_udphy {
94 struct device *dev;
95 struct regmap *pma_regmap;
96 struct regmap *u2phygrf;
97 struct regmap *udphygrf;
98 struct regmap *usbgrf;
99 struct regmap *vogrf;
100 struct typec_switch *sw;
101 struct typec_mux *mux;
102 struct mutex mutex; /* mutex to protect access to individual PHYs */
103
104 /* clocks and rests */
105 int num_clks;
106 struct clk_bulk_data *clks;
107 struct clk *refclk;
108 struct reset_control **rsts;
109
110 /* PHY status management */
111 bool flip;
112 bool mode_change;
113 u8 mode;
114 u8 status;
115
116 /* utilized for USB */
117 bool hs; /* flag for high-speed */
118
119 /* utilized for DP */
120 struct gpio_desc *sbu1_dc_gpio;
121 struct gpio_desc *sbu2_dc_gpio;
122 u32 lane_mux_sel[4];
123 u32 dp_lane_sel[4];
124 u32 dp_aux_dout_sel;
125 u32 dp_aux_din_sel;
126 int id;
127
128 /* PHY const config */
129 const struct rockchip_udphy_cfg *cfgs;
130 };
131
132 static const struct reg_sequence rk3588_udphy_24m_refclk_cfg[] = {
133 {0x0090, 0x68}, {0x0094, 0x68}, {0x0128, 0x24}, {0x012c, 0x44}, {0x0130, 0x3f}, {0x0134, 0x44}, {0x015c, 0xa9},
134 {0x0160, 0x71}, {0x0164, 0x71}, {0x0168, 0xa9}, {0x0174, 0xa9}, {0x0178, 0x71}, {0x017c, 0x71}, {0x0180, 0xa9},
135 {0x018c, 0x41}, {0x0190, 0x00}, {0x0194, 0x05}, {0x01ac, 0x2a}, {0x01b0, 0x17}, {0x01b4, 0x17}, {0x01b8, 0x2a},
136 {0x01c8, 0x04}, {0x01cc, 0x08}, {0x01d0, 0x08}, {0x01d4, 0x04}, {0x01d8, 0x20}, {0x01dc, 0x01}, {0x01e0, 0x09},
137 {0x01e4, 0x03}, {0x01f0, 0x29}, {0x01f4, 0x02}, {0x01f8, 0x02}, {0x01fc, 0x29}, {0x0208, 0x2a}, {0x020c, 0x17},
138 {0x0210, 0x17}, {0x0214, 0x2a}, {0x0224, 0x20}, {0x03f0, 0x0d}, {0x03f4, 0x09}, {0x03f8, 0x09}, {0x03fc, 0x0d},
139 {0x0404, 0x0e}, {0x0408, 0x14}, {0x040c, 0x14}, {0x0410, 0x3b}, {0x0ce0, 0x68}, {0x0ce8, 0xd0}, {0x0cf0, 0x87},
140 {0x0cf8, 0x70}, {0x0d00, 0x70}, {0x0d08, 0xa9}, {0x1ce0, 0x68}, {0x1ce8, 0xd0}, {0x1cf0, 0x87}, {0x1cf8, 0x70},
141 {0x1d00, 0x70}, {0x1d08, 0xa9}, {0x0a3c, 0xd0}, {0x0a44, 0xd0}, {0x0a48, 0x01}, {0x0a4c, 0x0d}, {0x0a54, 0xe0},
142 {0x0a5c, 0xe0}, {0x0a64, 0xa8}, {0x1a3c, 0xd0}, {0x1a44, 0xd0}, {0x1a48, 0x01}, {0x1a4c, 0x0d}, {0x1a54, 0xe0},
143 {0x1a5c, 0xe0}, {0x1a64, 0xa8}};
144
145 static const struct reg_sequence rk3588_udphy_26m_refclk_cfg[] = {
146 {0x0830, 0x07}, {0x085c, 0x80}, {0x1030, 0x07}, {0x105c, 0x80}, {0x1830, 0x07}, {0x185c, 0x80}, {0x2030, 0x07},
147 {0x205c, 0x80}, {0x0228, 0x38}, {0x0104, 0x44}, {0x0248, 0x44}, {0x038C, 0x02}, {0x0878, 0x04}, {0x1878, 0x04},
148 {0x0898, 0x77}, {0x1898, 0x77}, {0x0054, 0x01}, {0x00e0, 0x38}, {0x0060, 0x24}, {0x0064, 0x77}, {0x0070, 0x76},
149 {0x0234, 0xE8}, {0x0AF4, 0x15}, {0x1AF4, 0x15}, {0x081C, 0xE5}, {0x181C, 0xE5}, {0x099C, 0x48}, {0x199C, 0x48},
150 {0x09A4, 0x07}, {0x09A8, 0x22}, {0x19A4, 0x07}, {0x19A8, 0x22}, {0x09B8, 0x3E}, {0x19B8, 0x3E}, {0x09E4, 0x02},
151 {0x19E4, 0x02}, {0x0A34, 0x1E}, {0x1A34, 0x1E}, {0x0A98, 0x2F}, {0x1A98, 0x2F}, {0x0c30, 0x0E}, {0x0C48, 0x06},
152 {0x1C30, 0x0E}, {0x1C48, 0x06}, {0x028C, 0x18}, {0x0AF0, 0x00}, {0x1AF0, 0x00}};
153
154 static const struct reg_sequence rk3588_udphy_init_sequence[] = {
155 {0x0104, 0x44}, {0x0234, 0xE8}, {0x0248, 0x44}, {0x028C, 0x18}, {0x081C, 0xE5}, {0x0878, 0x00}, {0x0994, 0x1C},
156 {0x0AF0, 0x00}, {0x181C, 0xE5}, {0x1878, 0x00}, {0x1994, 0x1C}, {0x1AF0, 0x00}, {0x0428, 0x60}, {0x0D58, 0x33},
157 {0x1D58, 0x33}, {0x0990, 0x74}, {0x0D64, 0x17}, {0x08C8, 0x13}, {0x1990, 0x74}, {0x1D64, 0x17}, {0x18C8, 0x13},
158 {0x0D90, 0x40}, {0x0DA8, 0x40}, {0x0DC0, 0x40}, {0x0DD8, 0x40}, {0x1D90, 0x40}, {0x1DA8, 0x40}, {0x1DC0, 0x40},
159 {0x1DD8, 0x40}, {0x03C0, 0x30}, {0x03C4, 0x06}, {0x0E10, 0x00}, {0x1E10, 0x00}, {0x043C, 0x0F}, {0x0D2C, 0xFF},
160 {0x1D2C, 0xFF}, {0x0D34, 0x0F}, {0x1D34, 0x0F}, {0x08FC, 0x2A}, {0x0914, 0x28}, {0x0A30, 0x03}, {0x0E38, 0x05},
161 {0x0ECC, 0x27}, {0x0ED0, 0x22}, {0x0ED4, 0x26}, {0x18FC, 0x2A}, {0x1914, 0x28}, {0x1A30, 0x03}, {0x1E38, 0x05},
162 {0x1ECC, 0x27}, {0x1ED0, 0x22}, {0x1ED4, 0x26}, {0x0048, 0x0F}, {0x0060, 0x3C}, {0x0064, 0xF7}, {0x006C, 0x20},
163 {0x0070, 0x7D}, {0x0074, 0x68}, {0x0AF4, 0x1A}, {0x1AF4, 0x1A}, {0x0440, 0x3F}, {0x10D4, 0x08}, {0x20D4, 0x08},
164 {0x00D4, 0x30}, {0x0024, 0x6e},
165 };
166
grfreg_write(struct regmap * base,const struct udphy_grf_reg * reg,bool en)167 static inline int grfreg_write(struct regmap *base, const struct udphy_grf_reg *reg, bool en)
168 {
169 u32 val, mask, tmp;
170
171 tmp = en ? reg->enable : reg->disable;
172 mask = GENMASK(reg->bitend, reg->bitstart);
173 val = (tmp << reg->bitstart) | (mask << BIT_WRITEABLE_SHIFT);
174
175 return regmap_write(base, reg->offset, val);
176 }
177
udphy_clk_init(struct rockchip_udphy * udphy,struct device * dev)178 static int udphy_clk_init(struct rockchip_udphy *udphy, struct device *dev)
179 {
180 int i;
181
182 udphy->num_clks = devm_clk_bulk_get_all(dev, &udphy->clks);
183 if (udphy->num_clks < 1) {
184 return -ENODEV;
185 }
186
187 /* used for configure phy reference clock frequency */
188 for (i = 0; i < udphy->num_clks; i++) {
189 if (!strncmp(udphy->clks[i].id, "refclk", 0x6)) {
190 udphy->refclk = udphy->clks[i].clk;
191 break;
192 }
193 }
194
195 if (!udphy->refclk) {
196 dev_warn(udphy->dev, "no refclk found\n");
197 }
198
199 return 0;
200 }
201
udphy_reset_init(struct rockchip_udphy * udphy,struct device * dev)202 static int udphy_reset_init(struct rockchip_udphy *udphy, struct device *dev)
203 {
204 const struct rockchip_udphy_cfg *cfg = udphy->cfgs;
205 int idx;
206
207 udphy->rsts = devm_kcalloc(dev, cfg->num_rsts, sizeof(*udphy->rsts), GFP_KERNEL);
208 if (!udphy->rsts) {
209 return -ENOMEM;
210 }
211
212 for (idx = 0; idx < cfg->num_rsts; idx++) {
213 struct reset_control *rst;
214 const char *name = cfg->rst_list[idx];
215
216 rst = devm_reset_control_get(dev, name);
217 if (IS_ERR(rst)) {
218 dev_err(dev, "failed to get %s reset\n", name);
219 devm_kfree(dev, (void *)udphy->rsts);
220 return PTR_ERR(rst);
221 }
222
223 udphy->rsts[idx] = rst;
224 reset_control_assert(udphy->rsts[idx]);
225 }
226
227 return 0;
228 }
229
udphy_get_rst_idx(const char * const * list,int num,char * name)230 static int udphy_get_rst_idx(const char *const *list, int num, char *name)
231 {
232 int idx;
233
234 for (idx = 0; idx < num; idx++) {
235 if (!strcmp(list[idx], name)) {
236 return idx;
237 }
238 }
239
240 return -EINVAL;
241 }
242
udphy_reset_assert(struct rockchip_udphy * udphy,char * name)243 static int udphy_reset_assert(struct rockchip_udphy *udphy, char *name)
244 {
245 const struct rockchip_udphy_cfg *cfg = udphy->cfgs;
246 int idx;
247
248 idx = udphy_get_rst_idx(cfg->rst_list, cfg->num_rsts, name);
249 if (idx < 0) {
250 return idx;
251 }
252
253 return reset_control_assert(udphy->rsts[idx]);
254 }
255
udphy_reset_deassert(struct rockchip_udphy * udphy,char * name)256 static int udphy_reset_deassert(struct rockchip_udphy *udphy, char *name)
257 {
258 const struct rockchip_udphy_cfg *cfg = udphy->cfgs;
259 int idx;
260
261 idx = udphy_get_rst_idx(cfg->rst_list, cfg->num_rsts, name);
262 if (idx < 0) {
263 return idx;
264 }
265
266 return reset_control_deassert(udphy->rsts[idx]);
267 }
268
udphy_u3_port_disable(struct rockchip_udphy * udphy,u8 disable)269 static void udphy_u3_port_disable(struct rockchip_udphy *udphy, u8 disable)
270 {
271 const struct rockchip_udphy_cfg *cfg = udphy->cfgs;
272 const struct udphy_grf_reg *preg;
273
274 preg = udphy->id ? &cfg->grfcfg.usb3otg1_cfg : &cfg->grfcfg.usb3otg0_cfg;
275 grfreg_write(udphy->usbgrf, preg, disable);
276 }
277
udphy_usb_bvalid_enable(struct rockchip_udphy * udphy,u8 enable)278 static void udphy_usb_bvalid_enable(struct rockchip_udphy *udphy, u8 enable)
279 {
280 const struct rockchip_udphy_cfg *cfg = udphy->cfgs;
281
282 grfreg_write(udphy->u2phygrf, &cfg->grfcfg.bvalid_phy_con, enable);
283 grfreg_write(udphy->u2phygrf, &cfg->grfcfg.bvalid_grf_con, enable);
284 }
285
286 /*
287 * In usb/dp combo phy driver, here are 2 ways to mapping lanes.
288 *
289 * 1 Type-C Mapping table (DP_Alt_Mode V1.0b remove ABF pin mapping)
290 * ---------------------------------------------------------------------------
291 * Type-C Pin B11-B10 A2-A3 A11-A10 B2-B3
292 * PHY Pad ln0(tx/rx) ln1(tx) ln2(tx/rx) ln3(tx)
293 * C/E(Normal) dpln3 dpln2 dpln0 dpln1
294 * C/E(Flip ) dpln0 dpln1 dpln3 dpln2
295 * D/F(Normal) usbrx usbtx dpln0 dpln1
296 * D/F(Flip ) dpln0 dpln1 usbrx usbtx
297 * A(Normal ) dpln3 dpln1 dpln2 dpln0
298 * A(Flip ) dpln2 dpln0 dpln3 dpln1
299 * B(Normal ) usbrx usbtx dpln1 dpln0
300 * B(Flip ) dpln1 dpln0 usbrx usbtx
301 * ---------------------------------------------------------------------------
302 *
303 * 2 Mapping the lanes in dtsi
304 * if all 4 lane assignment for dp function, define rockchip,dp-lane-mux = <x x x x>;
305 * sample as follow
306 * ---------------------------------------------------------------------------
307 * B11-B10 A2-A3 A11-A10 B2-B3
308 * rockchip,dp-lane-mux ln0(tx/rx) ln1(tx) ln2(tx/rx) ln3(tx)
309 * <0 1 2 3> dpln0 dpln1 dpln2 dpln3
310 * <2 3 0 1> dpln2 dpln3 dpln0 dpln1
311 * ---------------------------------------------------------------------------
312 * if 2 lane for dp function, 2 lane for usb function, define rockchip,dp-lane-mux = <x x>;
313 * sample as follow
314 * ---------------------------------------------------------------------------
315 * B11-B10 A2-A3 A11-A10 B2-B3
316 * rockchip,dp-lane-mux ln0(tx/rx) ln1(tx) ln2(tx/rx) ln3(tx)
317 * <0 1> dpln0 dpln1 usbrx usbtx
318 * <2 3> usbrx usbtx dpln0 dpln1
319 * ---------------------------------------------------------------------------
320 */
321
udphy_dplane_select(struct rockchip_udphy * udphy)322 static int udphy_dplane_select(struct rockchip_udphy *udphy)
323 {
324 const struct rockchip_udphy_cfg *cfg = udphy->cfgs;
325
326 if (cfg->dplane_select) {
327 return cfg->dplane_select(udphy);
328 }
329
330 return 0;
331 }
332
udphy_dplane_get(struct rockchip_udphy * udphy)333 static int udphy_dplane_get(struct rockchip_udphy *udphy)
334 {
335 int dp_lanes;
336
337 switch (udphy->mode) {
338 case UDPHY_MODE_DP:
339 dp_lanes = 0x04;
340 break;
341 case UDPHY_MODE_DP_USB:
342 dp_lanes = 0x02;
343 break;
344 case UDPHY_MODE_USB:
345 fallthrough;
346 default:
347 dp_lanes = 0;
348 break;
349 }
350
351 return dp_lanes;
352 }
353
udphy_dplane_enable(struct rockchip_udphy * udphy,int dp_lanes)354 static int udphy_dplane_enable(struct rockchip_udphy *udphy, int dp_lanes)
355 {
356 const struct rockchip_udphy_cfg *cfg = udphy->cfgs;
357 int ret = 0;
358
359 if (cfg->dplane_enable) {
360 ret = cfg->dplane_enable(udphy, dp_lanes);
361 }
362
363 return ret;
364 }
365
upphy_set_typec_default_mapping(struct rockchip_udphy * udphy)366 static int upphy_set_typec_default_mapping(struct rockchip_udphy *udphy)
367 {
368 if (udphy->flip) {
369 udphy->dp_lane_sel[0] = 0;
370 udphy->dp_lane_sel[1] = 1;
371 udphy->dp_lane_sel[0x02] = 0x03;
372 udphy->dp_lane_sel[0x03] = 0x02;
373 udphy->lane_mux_sel[0] = PHY_LANE_MUX_DP;
374 udphy->lane_mux_sel[1] = PHY_LANE_MUX_DP;
375 udphy->lane_mux_sel[0x02] = PHY_LANE_MUX_USB;
376 udphy->lane_mux_sel[0x03] = PHY_LANE_MUX_USB;
377 udphy->dp_aux_dout_sel = PHY_AUX_DP_DATA_POL_INVERT;
378 udphy->dp_aux_din_sel = PHY_AUX_DP_DATA_POL_INVERT;
379 gpiod_set_value_cansleep(udphy->sbu1_dc_gpio, 1);
380 gpiod_set_value_cansleep(udphy->sbu2_dc_gpio, 0);
381 } else {
382 udphy->dp_lane_sel[0] = 0x02;
383 udphy->dp_lane_sel[1] = 0x03;
384 udphy->dp_lane_sel[0x02] = 1;
385 udphy->dp_lane_sel[0x03] = 0;
386 udphy->lane_mux_sel[0] = PHY_LANE_MUX_USB;
387 udphy->lane_mux_sel[1] = PHY_LANE_MUX_USB;
388 udphy->lane_mux_sel[0x02] = PHY_LANE_MUX_DP;
389 udphy->lane_mux_sel[0x03] = PHY_LANE_MUX_DP;
390 udphy->dp_aux_dout_sel = PHY_AUX_DP_DATA_POL_NORMAL;
391 udphy->dp_aux_din_sel = PHY_AUX_DP_DATA_POL_NORMAL;
392 gpiod_set_value_cansleep(udphy->sbu1_dc_gpio, 0);
393 gpiod_set_value_cansleep(udphy->sbu2_dc_gpio, 1);
394 }
395
396 udphy->mode = UDPHY_MODE_DP_USB;
397
398 return 0;
399 }
400
udphy_orien_sw_set(struct typec_switch * sw,enum typec_orientation orien)401 static int udphy_orien_sw_set(struct typec_switch *sw, enum typec_orientation orien)
402 {
403 struct rockchip_udphy *udphy = typec_switch_get_drvdata(sw);
404
405 mutex_lock(&udphy->mutex);
406
407 if (orien == TYPEC_ORIENTATION_NONE) {
408 gpiod_set_value_cansleep(udphy->sbu1_dc_gpio, 0);
409 gpiod_set_value_cansleep(udphy->sbu2_dc_gpio, 0);
410 /* unattached */
411 udphy_usb_bvalid_enable(udphy, false);
412 goto unlock_ret;
413 }
414
415 udphy->flip = (orien == TYPEC_ORIENTATION_REVERSE) ? true : false;
416 upphy_set_typec_default_mapping(udphy);
417 udphy_usb_bvalid_enable(udphy, true);
418
419 unlock_ret:
420 mutex_unlock(&udphy->mutex);
421 return 0;
422 }
423
udphy_setup_orien_switch(struct rockchip_udphy * udphy)424 static int udphy_setup_orien_switch(struct rockchip_udphy *udphy)
425 {
426 struct typec_switch_desc sw_desc = {};
427
428 sw_desc.drvdata = udphy;
429 sw_desc.fwnode = dev_fwnode(udphy->dev);
430 sw_desc.set = udphy_orien_sw_set;
431
432 udphy->sw = typec_switch_register(udphy->dev, &sw_desc);
433 if (IS_ERR(udphy->sw)) {
434 dev_err(udphy->dev, "Error register typec orientation switch: %ld\n", PTR_ERR(udphy->sw));
435 return PTR_ERR(udphy->sw);
436 }
437
438 return 0;
439 }
440
udphy_orien_switch_unregister(void * data)441 static void udphy_orien_switch_unregister(void *data)
442 {
443 struct rockchip_udphy *udphy = data;
444
445 typec_switch_unregister(udphy->sw);
446 }
447
udphy_setup(struct rockchip_udphy * udphy)448 static int udphy_setup(struct rockchip_udphy *udphy)
449 {
450 const struct rockchip_udphy_cfg *cfg = udphy->cfgs;
451 int ret = 0;
452
453 mutex_lock(&udphy->mutex);
454
455 ret = clk_bulk_prepare_enable(udphy->num_clks, udphy->clks);
456 if (ret) {
457 dev_err(udphy->dev, "failed to enable clk\n");
458 goto unlock;
459 }
460
461 if (cfg->combophy_init) {
462 ret = cfg->combophy_init(udphy);
463 if (ret) {
464 dev_err(udphy->dev, "failed to init combophy\n");
465 goto disable_clks;
466 }
467 }
468
469 mutex_unlock(&udphy->mutex);
470 return 0;
471
472 disable_clks:
473 clk_bulk_disable_unprepare(udphy->num_clks, udphy->clks);
474 unlock:
475 mutex_unlock(&udphy->mutex);
476 return ret;
477 }
478
udphy_disable(struct rockchip_udphy * udphy)479 static int udphy_disable(struct rockchip_udphy *udphy)
480 {
481 const struct rockchip_udphy_cfg *cfg = udphy->cfgs;
482 int i;
483
484 mutex_lock(&udphy->mutex);
485
486 clk_bulk_disable_unprepare(udphy->num_clks, udphy->clks);
487
488 for (i = 0; i < cfg->num_rsts; i++) {
489 reset_control_assert(udphy->rsts[i]);
490 }
491
492 mutex_unlock(&udphy->mutex);
493 return 0;
494 }
495
udphy_parse_lane_mux_data(struct rockchip_udphy * udphy,struct device_node * np)496 static int udphy_parse_lane_mux_data(struct rockchip_udphy *udphy, struct device_node *np)
497 {
498 struct property *prop;
499 int ret, i, len, num_lanes;
500
501 prop = of_find_property(np, "rockchip,dp-lane-mux", &len);
502 if (!prop) {
503 dev_dbg(udphy->dev, "failed to find dp lane mux, following dp alt mode\n");
504 udphy->mode = UDPHY_MODE_USB;
505 return 0;
506 }
507
508 num_lanes = len / sizeof(u32);
509
510 if (num_lanes != 0x02 && num_lanes != 0x04) {
511 dev_err(udphy->dev, "invalid number of lane mux\n");
512 return -EINVAL;
513 }
514
515 ret = of_property_read_u32_array(np, "rockchip,dp-lane-mux", udphy->dp_lane_sel, num_lanes);
516 if (ret) {
517 dev_err(udphy->dev, "get dp lane mux failed\n");
518 return -EINVAL;
519 }
520
521 for (i = 0; i < num_lanes; i++) {
522 int j;
523
524 if (udphy->dp_lane_sel[i] > 0x03) {
525 dev_err(udphy->dev, "lane mux between 0 and 3, exceeding the range\n");
526 return -EINVAL;
527 }
528
529 udphy->lane_mux_sel[udphy->dp_lane_sel[i]] = PHY_LANE_MUX_DP;
530
531 for (j = i + 1; j < num_lanes; j++) {
532 if (udphy->dp_lane_sel[i] == udphy->dp_lane_sel[j]) {
533 dev_err(udphy->dev, "set repeat lane mux value\n");
534 return -EINVAL;
535 }
536 }
537 }
538
539 udphy->mode = UDPHY_MODE_DP;
540 if (num_lanes == 0x02) {
541 udphy->mode |= UDPHY_MODE_USB;
542 }
543
544 return 0;
545 }
546
udphy_parse_dt(struct rockchip_udphy * udphy,struct device * dev)547 static int udphy_parse_dt(struct rockchip_udphy *udphy, struct device *dev)
548 {
549 struct device_node *np = dev->of_node;
550 enum usb_device_speed maximum_speed;
551 int ret;
552
553 udphy->u2phygrf = syscon_regmap_lookup_by_phandle(np, "rockchip,u2phy-grf");
554 if (IS_ERR(udphy->u2phygrf)) {
555 if (PTR_ERR(udphy->u2phygrf) == -ENODEV) {
556 dev_warn(dev, "missing u2phy-grf dt node\n");
557 udphy->u2phygrf = NULL;
558 } else {
559 return PTR_ERR(udphy->u2phygrf);
560 }
561 }
562
563 udphy->udphygrf = syscon_regmap_lookup_by_phandle(np, "rockchip,usbdpphy-grf");
564 if (IS_ERR(udphy->udphygrf)) {
565 if (PTR_ERR(udphy->udphygrf) == -ENODEV) {
566 dev_warn(dev, "missing usbdpphy-grf dt node\n");
567 udphy->udphygrf = NULL;
568 } else {
569 return PTR_ERR(udphy->udphygrf);
570 }
571 }
572
573 udphy->usbgrf = syscon_regmap_lookup_by_phandle(np, "rockchip,usb-grf");
574 if (IS_ERR(udphy->usbgrf)) {
575 if (PTR_ERR(udphy->usbgrf) == -ENODEV) {
576 dev_warn(dev, "missing usb-grf dt node\n");
577 udphy->usbgrf = NULL;
578 } else {
579 return PTR_ERR(udphy->usbgrf);
580 }
581 }
582
583 udphy->vogrf = syscon_regmap_lookup_by_phandle(np, "rockchip,vo-grf");
584 if (IS_ERR(udphy->vogrf)) {
585 if (PTR_ERR(udphy->vogrf) == -ENODEV) {
586 dev_warn(dev, "missing vo-grf dt node\n");
587 udphy->vogrf = NULL;
588 } else {
589 return PTR_ERR(udphy->vogrf);
590 }
591 }
592
593 ret = udphy_parse_lane_mux_data(udphy, np);
594 if (ret) {
595 return ret;
596 }
597
598 udphy->sbu1_dc_gpio = devm_gpiod_get_optional(dev, "sbu1-dc", GPIOD_OUT_LOW);
599 if (IS_ERR(udphy->sbu1_dc_gpio)) {
600 return PTR_ERR(udphy->sbu1_dc_gpio);
601 }
602
603 udphy->sbu2_dc_gpio = devm_gpiod_get_optional(dev, "sbu2-dc", GPIOD_OUT_LOW);
604 if (IS_ERR(udphy->sbu2_dc_gpio)) {
605 return PTR_ERR(udphy->sbu2_dc_gpio);
606 }
607
608 if (device_property_present(dev, "maximum-speed")) {
609 maximum_speed = usb_get_maximum_speed(dev);
610 udphy->hs = maximum_speed <= USB_SPEED_HIGH ? true : false;
611 }
612
613 ret = udphy_clk_init(udphy, dev);
614 if (ret) {
615 return ret;
616 }
617
618 ret = udphy_reset_init(udphy, dev);
619 if (ret) {
620 return ret;
621 }
622
623 return 0;
624 }
625
udphy_power_on(struct rockchip_udphy * udphy,u8 mode)626 static int udphy_power_on(struct rockchip_udphy *udphy, u8 mode)
627 {
628 int ret;
629
630 if (!(udphy->mode & mode)) {
631 dev_info(udphy->dev, "mode 0x%02x is not support\n", mode);
632 return 0;
633 }
634
635 if (udphy->status == UDPHY_MODE_NONE) {
636 udphy->mode_change = false;
637 ret = udphy_setup(udphy);
638 if (ret) {
639 return ret;
640 }
641
642 if (udphy->mode & UDPHY_MODE_USB) {
643 udphy_u3_port_disable(udphy, false);
644 }
645 } else if (udphy->mode_change) {
646 udphy->mode_change = false;
647 udphy->status = UDPHY_MODE_NONE;
648 if (udphy->mode == UDPHY_MODE_DP) {
649 udphy_u3_port_disable(udphy, true);
650 }
651
652 ret = udphy_disable(udphy);
653 if (ret) {
654 return ret;
655 }
656 ret = udphy_setup(udphy);
657 if (ret) {
658 return ret;
659 }
660 }
661
662 udphy->status |= mode;
663
664 return 0;
665 }
666
udphy_power_off(struct rockchip_udphy * udphy,u8 mode)667 static int udphy_power_off(struct rockchip_udphy *udphy, u8 mode)
668 {
669 int ret;
670
671 if (!(udphy->mode & mode)) {
672 dev_info(udphy->dev, "mode 0x%02x is not support\n", mode);
673 return 0;
674 }
675
676 if (!udphy->status) {
677 return 0;
678 }
679
680 udphy->status &= ~mode;
681
682 if (udphy->status == UDPHY_MODE_NONE) {
683 ret = udphy_disable(udphy);
684 if (ret) {
685 return ret;
686 }
687 }
688
689 return 0;
690 }
691
rockchip_dp_phy_power_on(struct phy * phy)692 static int rockchip_dp_phy_power_on(struct phy *phy)
693 {
694 struct rockchip_udphy *udphy = phy_get_drvdata(phy);
695 int ret, dp_lanes;
696
697 dp_lanes = udphy_dplane_get(udphy);
698 phy_set_bus_width(phy, dp_lanes);
699
700 ret = udphy_power_on(udphy, UDPHY_MODE_DP);
701 if (ret) {
702 return ret;
703 }
704
705 ret = udphy_dplane_enable(udphy, dp_lanes);
706 if (ret) {
707 return ret;
708 }
709
710 return udphy_dplane_select(udphy);
711 }
712
rockchip_dp_phy_power_off(struct phy * phy)713 static int rockchip_dp_phy_power_off(struct phy *phy)
714 {
715 struct rockchip_udphy *udphy = phy_get_drvdata(phy);
716 int ret;
717
718 ret = udphy_dplane_enable(udphy, 0);
719 if (ret) {
720 return ret;
721 }
722
723 return udphy_power_off(udphy, UDPHY_MODE_DP);
724 }
725
rockchip_dp_phy_verify_config(struct rockchip_udphy * udphy,struct phy_configure_opts_dp * dp)726 static int rockchip_dp_phy_verify_config(struct rockchip_udphy *udphy, struct phy_configure_opts_dp *dp)
727 {
728 int i;
729
730 /* If changing link rate was required, verify it's supported. */
731 if (dp->set_rate) {
732 switch (dp->link_rate) {
733 case 1620:
734 case 2700:
735 case 5400:
736 case 8100:
737 /* valid bit rate */
738 break;
739 default:
740 return -EINVAL;
741 }
742 }
743
744 /* Verify lane count. */
745 switch (dp->lanes) {
746 case 1:
747 case 2:
748 case 4:
749 /* valid lane count. */
750 break;
751 default:
752 return -EINVAL;
753 }
754
755 /*
756 * If changing voltages is required, check swing and pre-emphasis
757 * levels, per-lane.
758 */
759 if (dp->set_voltages) {
760 /* Lane count verified previously. */
761 for (i = 0; i < dp->lanes; i++) {
762 if (dp->voltage[i] > 3 || dp->pre[i] > 3) {
763 return -EINVAL;
764 }
765
766 /*
767 * Sum of voltage swing and pre-emphasis levels cannot
768 * exceed 3.
769 */
770 if (dp->voltage[i] + dp->pre[i] > 3) {
771 return -EINVAL;
772 }
773 }
774 }
775
776 return 0;
777 }
778
rockchip_dp_phy_configure(struct phy * phy,union phy_configure_opts * opts)779 static int rockchip_dp_phy_configure(struct phy *phy, union phy_configure_opts *opts)
780 {
781 struct rockchip_udphy *udphy = phy_get_drvdata(phy);
782 const struct rockchip_udphy_cfg *cfg = udphy->cfgs;
783 int ret;
784
785 ret = rockchip_dp_phy_verify_config(udphy, &opts->dp);
786 if (ret) {
787 return ret;
788 }
789
790 if (opts->dp.set_rate && cfg->dp_phy_set_rate) {
791 ret = cfg->dp_phy_set_rate(udphy, &opts->dp);
792 if (ret) {
793 dev_err(udphy->dev, "rockchip_hdptx_phy_set_rate failed\n");
794 return ret;
795 }
796 }
797
798 if (opts->dp.set_voltages && cfg->dp_phy_set_voltages) {
799 ret = cfg->dp_phy_set_voltages(udphy, &opts->dp);
800 if (ret) {
801 dev_err(udphy->dev, "rockchip_dp_phy_set_voltages failed\n");
802 return ret;
803 }
804 }
805
806 return 0;
807 }
808
809 static const struct phy_ops rockchip_dp_phy_ops = {
810 .power_on = rockchip_dp_phy_power_on,
811 .power_off = rockchip_dp_phy_power_off,
812 .configure = rockchip_dp_phy_configure,
813 .owner = THIS_MODULE,
814 };
815
rockchip_u3phy_init(struct phy * phy)816 static int rockchip_u3phy_init(struct phy *phy)
817 {
818 struct rockchip_udphy *udphy = phy_get_drvdata(phy);
819
820 /* DP only or high-speed, disable U3 port */
821 if (!(udphy->mode & UDPHY_MODE_USB) || udphy->hs) {
822 udphy_u3_port_disable(udphy, true);
823 return 0;
824 }
825
826 return udphy_power_on(udphy, UDPHY_MODE_USB);
827 }
828
rockchip_u3phy_exit(struct phy * phy)829 static int rockchip_u3phy_exit(struct phy *phy)
830 {
831 struct rockchip_udphy *udphy = phy_get_drvdata(phy);
832
833 /* DP only or high-speed */
834 if (!(udphy->mode & UDPHY_MODE_USB) || udphy->hs) {
835 return 0;
836 }
837
838 return udphy_power_off(udphy, UDPHY_MODE_USB);
839 }
840
841 static const struct phy_ops rockchip_u3phy_ops = {
842 .init = rockchip_u3phy_init,
843 .exit = rockchip_u3phy_exit,
844 .owner = THIS_MODULE,
845 };
846
usbdp_typec_mux_set(struct typec_mux * mux,struct typec_mux_state * state)847 static int usbdp_typec_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
848 {
849 struct rockchip_udphy *udphy = typec_mux_get_drvdata(mux);
850 const struct rockchip_udphy_cfg *cfg = udphy->cfgs;
851 u8 mode;
852
853 switch (state->mode) {
854 case TYPEC_DP_STATE_C:
855 fallthrough;
856 case TYPEC_DP_STATE_E:
857 udphy->lane_mux_sel[0] = PHY_LANE_MUX_DP;
858 udphy->lane_mux_sel[1] = PHY_LANE_MUX_DP;
859 udphy->lane_mux_sel[0x02] = PHY_LANE_MUX_DP;
860 udphy->lane_mux_sel[0x03] = PHY_LANE_MUX_DP;
861 mode = UDPHY_MODE_DP;
862 break;
863 case TYPEC_DP_STATE_D:
864 fallthrough;
865 default:
866 if (udphy->flip) {
867 udphy->lane_mux_sel[0] = PHY_LANE_MUX_DP;
868 udphy->lane_mux_sel[1] = PHY_LANE_MUX_DP;
869 udphy->lane_mux_sel[0x02] = PHY_LANE_MUX_USB;
870 udphy->lane_mux_sel[0x03] = PHY_LANE_MUX_USB;
871 } else {
872 udphy->lane_mux_sel[0] = PHY_LANE_MUX_USB;
873 udphy->lane_mux_sel[1] = PHY_LANE_MUX_USB;
874 udphy->lane_mux_sel[0x02] = PHY_LANE_MUX_DP;
875 udphy->lane_mux_sel[0x03] = PHY_LANE_MUX_DP;
876 }
877 mode = UDPHY_MODE_DP_USB;
878 break;
879 }
880
881 if (state->alt && state->alt->svid == USB_TYPEC_DP_SID) {
882 struct typec_displayport_data *data = state->data;
883 bool hpd = !!(data && (data->status & DP_STATUS_HPD_STATE));
884
885 if (hpd && udphy->mode != mode) {
886 udphy->mode = mode;
887 udphy->mode_change = true;
888 }
889
890 if (cfg->hpd_event_trigger) {
891 cfg->hpd_event_trigger(udphy, hpd);
892 }
893 }
894
895 return 0;
896 }
897
udphy_setup_typec_mux(struct rockchip_udphy * udphy)898 static int udphy_setup_typec_mux(struct rockchip_udphy *udphy)
899 {
900 struct typec_mux_desc mux_desc = {};
901
902 mux_desc.drvdata = udphy;
903 mux_desc.fwnode = dev_fwnode(udphy->dev);
904 mux_desc.set = usbdp_typec_mux_set;
905
906 udphy->mux = typec_mux_register(udphy->dev, &mux_desc);
907 if (IS_ERR(udphy->mux)) {
908 dev_err(udphy->dev, "Error register typec mux: %ld\n", PTR_ERR(udphy->mux));
909 return PTR_ERR(udphy->mux);
910 }
911
912 return 0;
913 }
914
udphy_typec_mux_unregister(void * data)915 static void udphy_typec_mux_unregister(void *data)
916 {
917 struct rockchip_udphy *udphy = data;
918
919 typec_mux_unregister(udphy->mux);
920 }
921
922 static const struct regmap_config rockchip_udphy_pma_regmap_cfg = {
923 .reg_bits = 32,
924 .reg_stride = 4,
925 .val_bits = 32,
926 .fast_io = true,
927 .max_register = 0x20dc,
928 };
929
rockchip_udphy_probe(struct platform_device * pdev)930 static int rockchip_udphy_probe(struct platform_device *pdev)
931 {
932 struct device *dev = &pdev->dev;
933 struct device_node *np = dev->of_node;
934 struct device_node *child_np;
935 struct phy_provider *phy_provider;
936 struct resource *res;
937 struct rockchip_udphy *udphy;
938 const struct rockchip_udphy_cfg *phy_cfgs;
939 void IO_MEM *base;
940 int id, ret;
941
942 udphy = devm_kzalloc(dev, sizeof(*udphy), GFP_KERNEL);
943 if (!udphy) {
944 return -ENOMEM;
945 }
946
947 id = of_alias_get_id(dev->of_node, "usbdp");
948 if (id < 0) {
949 id = 0;
950 }
951 udphy->id = id;
952
953 phy_cfgs = of_device_get_match_data(dev);
954 if (!phy_cfgs) {
955 dev_err(dev, "no OF data can be matched with %p node\n", np);
956 return -EINVAL;
957 }
958
959 udphy->cfgs = phy_cfgs;
960
961 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
962 base = devm_ioremap_resource(dev, res);
963 if (IS_ERR(base)) {
964 return PTR_ERR(base);
965 }
966
967 udphy->pma_regmap = devm_regmap_init_mmio(dev, base + UDPHY_PMA, &rockchip_udphy_pma_regmap_cfg);
968 if (IS_ERR(udphy->pma_regmap)) {
969 return PTR_ERR(udphy->pma_regmap);
970 }
971
972 ret = udphy_parse_dt(udphy, dev);
973 if (ret) {
974 return ret;
975 }
976
977 mutex_init(&udphy->mutex);
978 udphy->dev = dev;
979 platform_set_drvdata(pdev, udphy);
980
981 if (device_property_present(dev, "orientation-switch")) {
982 ret = udphy_setup_orien_switch(udphy);
983 if (ret) {
984 return ret;
985 }
986
987 ret = devm_add_action_or_reset(dev, udphy_orien_switch_unregister, udphy);
988 if (ret) {
989 return ret;
990 }
991 }
992
993 ret = udphy_setup_typec_mux(udphy);
994 if (ret) {
995 return ret;
996 }
997
998 ret = devm_add_action_or_reset(dev, udphy_typec_mux_unregister, udphy);
999 if (ret) {
1000 return ret;
1001 }
1002
1003 for_each_available_child_of_node(np, child_np)
1004 {
1005 struct phy *phy;
1006
1007 if (of_node_name_eq(child_np, "dp-port")) {
1008 phy = devm_phy_create(dev, child_np, &rockchip_dp_phy_ops);
1009 } else if (of_node_name_eq(child_np, "u3-port")) {
1010 phy = devm_phy_create(dev, child_np, &rockchip_u3phy_ops);
1011 } else {
1012 continue;
1013 }
1014
1015 if (IS_ERR(phy)) {
1016 dev_err(dev, "failed to create phy: %pOFn\n", child_np);
1017 goto put_child;
1018 }
1019
1020 phy_set_drvdata(phy, udphy);
1021 }
1022
1023 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
1024 if (IS_ERR(phy_provider)) {
1025 dev_err(dev, "failed to register phy provider\n");
1026 goto put_child;
1027 }
1028
1029 return 0;
1030
1031 put_child:
1032 of_node_put(child_np);
1033 return ret;
1034 }
1035
rk3588_udphy_refclk_set(struct rockchip_udphy * udphy)1036 static int rk3588_udphy_refclk_set(struct rockchip_udphy *udphy)
1037 {
1038 unsigned long rate;
1039 int ret;
1040
1041 /* configure phy reference clock */
1042 rate = clk_get_rate(udphy->refclk);
1043 dev_dbg(udphy->dev, "refclk freq %ld\n", rate);
1044
1045 switch (rate) {
1046 case 0x16E3600:
1047 ret = regmap_multi_reg_write(udphy->pma_regmap, rk3588_udphy_24m_refclk_cfg,
1048 ARRAY_SIZE(rk3588_udphy_24m_refclk_cfg));
1049 if (ret) {
1050 return ret;
1051 }
1052 break;
1053 case 0x18CBA80:
1054 /* register default is 26MHz */
1055 ret = regmap_multi_reg_write(udphy->pma_regmap, rk3588_udphy_26m_refclk_cfg,
1056 ARRAY_SIZE(rk3588_udphy_26m_refclk_cfg));
1057 if (ret) {
1058 return ret;
1059 }
1060 break;
1061 default:
1062 dev_err(udphy->dev, "unsupported refclk freq %ld\n", rate);
1063 return -EINVAL;
1064 }
1065
1066 return 0;
1067 }
1068
rk3588_udphy_status_check(struct rockchip_udphy * udphy)1069 static int rk3588_udphy_status_check(struct rockchip_udphy *udphy)
1070 {
1071 unsigned int val;
1072 int ret;
1073
1074 /* LCPLL check */
1075 if (udphy->mode & UDPHY_MODE_USB) {
1076 ret = regmap_read_poll_timeout(udphy->pma_regmap, CMN_ANA_LCPLL_DONE_OFFSET, val,
1077 (val & CMN_ANA_LCPLL_AFC_DONE) && (val & CMN_ANA_LCPLL_LOCK_DONE),
1078 0xC8, 0x186A0);
1079 if (ret) {
1080 dev_err(udphy->dev, "cmn ana lcpll lock timeout\n");
1081 return ret;
1082 }
1083 }
1084
1085 if (udphy->mode & UDPHY_MODE_USB) {
1086 if (!udphy->flip) {
1087 ret = regmap_read_poll_timeout(udphy->pma_regmap, TRSV_LN0_MON_RX_CDR_DONE_OFFSET, val,
1088 val & TRSV_LN0_MON_RX_CDR_LOCK_DONE, 0xC8, 0x186A0);
1089 if (ret) {
1090 dev_err(udphy->dev, "trsv ln0 mon rx cdr lock timeout\n");
1091 }
1092 } else {
1093 ret = regmap_read_poll_timeout(udphy->pma_regmap, TRSV_LN2_MON_RX_CDR_DONE_OFFSET, val,
1094 val & TRSV_LN2_MON_RX_CDR_LOCK_DONE, 0xC8, 0x186A0);
1095 if (ret) {
1096 dev_err(udphy->dev, "trsv ln2 mon rx cdr lock timeout\n");
1097 }
1098 }
1099 }
1100
1101 return 0;
1102 }
1103
rk3588_udphy_init(struct rockchip_udphy * udphy)1104 static int rk3588_udphy_init(struct rockchip_udphy *udphy)
1105 {
1106 const struct rockchip_udphy_cfg *cfg = udphy->cfgs;
1107 int ret;
1108
1109 /* enable rx lfps for usb */
1110 if (udphy->mode & UDPHY_MODE_USB) {
1111 grfreg_write(udphy->udphygrf, &cfg->grfcfg.rx_lfps, true);
1112 }
1113
1114 /* Step 1: power on pma and deassert apb rstn */
1115 grfreg_write(udphy->udphygrf, &cfg->grfcfg.low_pwrn, true);
1116
1117 udphy_reset_deassert(udphy, "pma_apb");
1118 udphy_reset_deassert(udphy, "pcs_apb");
1119
1120 /* Step 2: set init sequence and phy refclk */
1121 ret = regmap_multi_reg_write(udphy->pma_regmap, rk3588_udphy_init_sequence, ARRAY_SIZE(rk3588_udphy_init_sequence));
1122 if (ret) {
1123 dev_err(udphy->dev, "init sequence set error %d\n", ret);
1124 goto assert_apb;
1125 }
1126
1127 ret = rk3588_udphy_refclk_set(udphy);
1128 if (ret) {
1129 dev_err(udphy->dev, "refclk set error %d\n", ret);
1130 goto assert_apb;
1131 }
1132
1133 /* Step 3: configure lane mux */
1134 regmap_update_bits(udphy->pma_regmap, CMN_LANE_MUX_AND_EN_OFFSET, CMN_DP_LANE_MUX_ALL | CMN_DP_LANE_EN_ALL,
1135 FIELD_PREP(CMN_DP_LANE_MUX_N(0x03), udphy->lane_mux_sel[0x03]) |
1136 FIELD_PREP(CMN_DP_LANE_MUX_N(0x02), udphy->lane_mux_sel[0x02]) |
1137 FIELD_PREP(CMN_DP_LANE_MUX_N(1), udphy->lane_mux_sel[1]) |
1138 FIELD_PREP(CMN_DP_LANE_MUX_N(0), udphy->lane_mux_sel[0]) |
1139 FIELD_PREP(CMN_DP_LANE_EN_ALL, 0));
1140
1141 /* Step 4: deassert init rstn and wait for 200ns from datasheet */
1142 if (udphy->mode & UDPHY_MODE_USB) {
1143 udphy_reset_deassert(udphy, "init");
1144 }
1145
1146 if (udphy->mode & UDPHY_MODE_DP) {
1147 regmap_update_bits(udphy->pma_regmap, CMN_DP_RSTN_OFFSET, CMN_DP_INIT_RSTN, FIELD_PREP(CMN_DP_INIT_RSTN, 0x1));
1148 }
1149
1150 udelay(1);
1151
1152 /* Step 5: deassert cmn/lane rstn */
1153 if (udphy->mode & UDPHY_MODE_USB) {
1154 udphy_reset_deassert(udphy, "cmn");
1155 udphy_reset_deassert(udphy, "lane");
1156 }
1157
1158 /* Step 6: wait for lock done of pll */
1159 ret = rk3588_udphy_status_check(udphy);
1160 if (ret) {
1161 goto assert_phy;
1162 }
1163
1164 return 0;
1165
1166 assert_phy:
1167 udphy_reset_assert(udphy, "init");
1168 udphy_reset_assert(udphy, "cmn");
1169 udphy_reset_assert(udphy, "lane");
1170
1171 assert_apb:
1172 udphy_reset_assert(udphy, "pma_apb");
1173 udphy_reset_assert(udphy, "pcs_apb");
1174 return ret;
1175 }
1176
rk3588_udphy_hpd_event_trigger(struct rockchip_udphy * udphy,bool hpd)1177 static int rk3588_udphy_hpd_event_trigger(struct rockchip_udphy *udphy, bool hpd)
1178 {
1179 const struct rockchip_udphy_cfg *cfg = udphy->cfgs;
1180
1181 grfreg_write(udphy->vogrf, &cfg->vogrfcfg[udphy->id].hpd_trigger, hpd);
1182
1183 return 0;
1184 }
1185
rk3588_udphy_dplane_enable(struct rockchip_udphy * udphy,int dp_lanes)1186 static int rk3588_udphy_dplane_enable(struct rockchip_udphy *udphy, int dp_lanes)
1187 {
1188 int i;
1189 u32 val = 0;
1190
1191 for (i = 0; i < dp_lanes; i++) {
1192 val |= BIT(udphy->dp_lane_sel[i]);
1193 }
1194
1195 regmap_update_bits(udphy->pma_regmap, CMN_LANE_MUX_AND_EN_OFFSET, CMN_DP_LANE_EN_ALL,
1196 FIELD_PREP(CMN_DP_LANE_EN_ALL, val));
1197
1198 if (!dp_lanes) {
1199 regmap_update_bits(udphy->pma_regmap, CMN_DP_RSTN_OFFSET, CMN_DP_CMN_RSTN, FIELD_PREP(CMN_DP_CMN_RSTN, 0x0));
1200 }
1201
1202 return 0;
1203 }
1204
rk3588_udphy_dplane_select(struct rockchip_udphy * udphy)1205 static int rk3588_udphy_dplane_select(struct rockchip_udphy *udphy)
1206 {
1207 u32 value = 0;
1208
1209 switch (udphy->mode) {
1210 case UDPHY_MODE_DP:
1211 value |= 0x02 << udphy->dp_lane_sel[0x02] * 0x02;
1212 value |= 0x03 << udphy->dp_lane_sel[0x03] * 0x02;
1213 fallthrough;
1214 case UDPHY_MODE_DP_USB:
1215 value |= 0 << udphy->dp_lane_sel[0] * 0x02;
1216 value |= 1 << udphy->dp_lane_sel[1] * 0x02;
1217 break;
1218 case UDPHY_MODE_USB:
1219 break;
1220 default:
1221 break;
1222 }
1223
1224 regmap_write(udphy->vogrf, udphy->id ? RK3588_GRF_VO0_CON2 : RK3588_GRF_VO0_CON0,
1225 ((DP_AUX_DIN_SEL | DP_AUX_DOUT_SEL | DP_LANE_SEL_ALL) << 0x10) |
1226 FIELD_PREP(DP_AUX_DIN_SEL, udphy->dp_aux_din_sel) |
1227 FIELD_PREP(DP_AUX_DOUT_SEL, udphy->dp_aux_dout_sel) | value);
1228
1229 return 0;
1230 }
1231
rk3588_dp_phy_set_rate(struct rockchip_udphy * udphy,struct phy_configure_opts_dp * dp)1232 static int rk3588_dp_phy_set_rate(struct rockchip_udphy *udphy, struct phy_configure_opts_dp *dp)
1233 {
1234 u8 bw;
1235 u32 val;
1236 int ret;
1237
1238 regmap_update_bits(udphy->pma_regmap, CMN_DP_RSTN_OFFSET, CMN_DP_CMN_RSTN, FIELD_PREP(CMN_DP_CMN_RSTN, 0x0));
1239
1240 switch (dp->link_rate) {
1241 case 0x654:
1242 bw = DP_BW_RBR;
1243 break;
1244 case 0xA8C:
1245 bw = DP_BW_HBR;
1246 break;
1247 case 0x1518:
1248 bw = DP_BW_HBR2;
1249 break;
1250 case 0x1FA4:
1251 bw = DP_BW_HBR3;
1252 break;
1253 default:
1254 return -EINVAL;
1255 }
1256
1257 regmap_update_bits(udphy->pma_regmap, CMN_DP_LINK_OFFSET, CMN_DP_TX_LINK_BW, FIELD_PREP(CMN_DP_TX_LINK_BW, bw));
1258 regmap_update_bits(udphy->pma_regmap, CMN_SSC_EN_OFFSET, CMN_ROPLL_SSC_EN, FIELD_PREP(CMN_ROPLL_SSC_EN, dp->ssc));
1259 regmap_update_bits(udphy->pma_regmap, CMN_DP_RSTN_OFFSET, CMN_DP_CMN_RSTN, FIELD_PREP(CMN_DP_CMN_RSTN, 0x1));
1260
1261 ret = regmap_read_poll_timeout(udphy->pma_regmap, CMN_ANA_ROPLL_DONE_OFFSET, val,
1262 FIELD_GET(CMN_ANA_ROPLL_LOCK_DONE, val) && FIELD_GET(CMN_ANA_ROPLL_AFC_DONE, val), 0,
1263 0x3E8);
1264 if (ret) {
1265 dev_err(udphy->dev, "ROPLL is not lock\n");
1266 return ret;
1267 }
1268
1269 return 0;
1270 }
1271
1272 static const struct {
1273 u32 trsv_reg0204;
1274 u32 trsv_reg0205;
1275 u32 trsv_reg0206;
1276 u32 trsv_reg0207;
1277 } training_table[4][4] = {
1278 /* voltage swing 0, pre-emphasis 0->3 */
1279 {{0x21, 0x10, 0x42, 0xe5}, {0x25, 0x14, 0x42, 0xe5}, {0x26, 0x17, 0x43, 0xe5}, {0x2b, 0x1c, 0x43, 0xe7}},
1280
1281 /* voltage swing 1, pre-emphasis 0->2 */
1282 {{0x26, 0x10, 0x42, 0xe7}, {0x2b, 0x15, 0x42, 0xe7}, {0x2b, 0x18, 0x43, 0xe7}},
1283
1284 /* voltage swing 2, pre-emphasis 0->1 */
1285 {{0x2a, 0x10, 0x42, 0xe7}, {0x2b, 0x15, 0x43, 0xe7}},
1286
1287 /* voltage swing 3, pre-emphasis 0 */
1288 {
1289 {0x2b, 0x10, 0x43, 0xe7},
1290 },
1291 };
1292
rk3588_dp_phy_set_voltage(struct rockchip_udphy * udphy,u32 voltage,u32 pre,u32 lane)1293 static void rk3588_dp_phy_set_voltage(struct rockchip_udphy *udphy, u32 voltage, u32 pre, u32 lane)
1294 {
1295 u32 offset = 0x800 * lane;
1296 u32 val;
1297
1298 val = training_table[voltage][pre].trsv_reg0204;
1299 regmap_write(udphy->pma_regmap, 0x0810 + offset, val);
1300
1301 val = training_table[voltage][pre].trsv_reg0205;
1302 regmap_write(udphy->pma_regmap, 0x0814 + offset, val);
1303
1304 val = training_table[voltage][pre].trsv_reg0206;
1305 regmap_write(udphy->pma_regmap, 0x0818 + offset, val);
1306
1307 val = training_table[voltage][pre].trsv_reg0207;
1308 regmap_write(udphy->pma_regmap, 0x081c + offset, val);
1309 }
1310
rk3588_dp_phy_set_voltages(struct rockchip_udphy * udphy,struct phy_configure_opts_dp * dp)1311 static int rk3588_dp_phy_set_voltages(struct rockchip_udphy *udphy, struct phy_configure_opts_dp *dp)
1312 {
1313 u32 i, lane;
1314
1315 for (i = 0; i < dp->lanes; i++) {
1316 lane = udphy->dp_lane_sel[i];
1317 switch (dp->link_rate) {
1318 case 0x654:
1319 case 0xA8C:
1320 regmap_update_bits(udphy->pma_regmap, TRSV_ANA_TX_CLK_OFFSET_N(lane), LN_ANA_TX_SER_TXCLK_INV,
1321 FIELD_PREP(LN_ANA_TX_SER_TXCLK_INV, udphy->lane_mux_sel[lane]));
1322 break;
1323 case 0x1518:
1324 case 0x1FA4:
1325 regmap_update_bits(udphy->pma_regmap, TRSV_ANA_TX_CLK_OFFSET_N(lane), LN_ANA_TX_SER_TXCLK_INV,
1326 FIELD_PREP(LN_ANA_TX_SER_TXCLK_INV, 0x0));
1327 break;
1328 }
1329
1330 rk3588_dp_phy_set_voltage(udphy, dp->voltage[i], dp->pre[i], lane);
1331 }
1332
1333 return 0;
1334 }
1335
1336 static const char *const rk3588_udphy_rst_l[] = {"init", "cmn", "lane", "pcs_apb", "pma_apb"};
1337
1338 static const struct rockchip_udphy_cfg rk3588_udphy_cfgs = {
1339 .num_rsts = ARRAY_SIZE(rk3588_udphy_rst_l),
1340 .rst_list = rk3588_udphy_rst_l,
1341 .grfcfg =
1342 {
1343 /* u2phy-grf */
1344 .bvalid_phy_con = {0x0008, 1, 0, 0x2, 0x3},
1345 .bvalid_grf_con = {0x0010, 3, 2, 0x2, 0x3},
1346
1347 /* usb-grf */
1348 .usb3otg0_cfg = {0x001c, 15, 0, 0x1100, 0x0188},
1349 .usb3otg1_cfg = {0x0034, 15, 0, 0x1100, 0x0188},
1350
1351 /* usbdpphy-grf */
1352 .low_pwrn = {0x0004, 13, 13, 0, 1},
1353 .rx_lfps = {0x0004, 14, 14, 0, 1},
1354 },
1355 .vogrfcfg =
1356 {
1357 {
1358 .hpd_trigger = {0x0000, 11, 10, 1, 3},
1359 },
1360 {
1361 .hpd_trigger = {0x0008, 11, 10, 1, 3},
1362 },
1363 },
1364 .combophy_init = rk3588_udphy_init,
1365 .dp_phy_set_rate = rk3588_dp_phy_set_rate,
1366 .dp_phy_set_voltages = rk3588_dp_phy_set_voltages,
1367 .hpd_event_trigger = rk3588_udphy_hpd_event_trigger,
1368 .dplane_enable = rk3588_udphy_dplane_enable,
1369 .dplane_select = rk3588_udphy_dplane_select,
1370 };
1371
1372 static const struct of_device_id rockchip_udphy_dt_match[] = {
1373 {.compatible = "rockchip,rk3588-usbdp-phy", .data = &rk3588_udphy_cfgs}, {}};
1374
1375 MODULE_DEVICE_TABLE(of, rockchip_udphy_dt_match);
1376
1377 static struct platform_driver rockchip_udphy_driver = {
1378 .probe = rockchip_udphy_probe,
1379 .driver =
1380 {
1381 .name = "rockchip-usbdp-phy",
1382 .of_match_table = rockchip_udphy_dt_match,
1383 },
1384 };
1385
1386 module_platform_driver(rockchip_udphy_driver);
1387
1388 MODULE_AUTHOR("Frank Wang <frank.wang@rock-chips.com>");
1389 MODULE_AUTHOR("Zhang Yubing <yubing.zhang@rock-chips.com>");
1390 MODULE_DESCRIPTION("Rockchip USBDP Combo PHY driver");
1391 MODULE_LICENSE("GPL v2");
1392