1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Rockchip USB2.0 PHY with Innosilicon IP block driver
4 *
5 * Copyright (C) 2016 Fuzhou Rockchip Electronics Co., Ltd
6 */
7
8 #include <linux/clk.h>
9 #include <linux/clk-provider.h>
10 #include <linux/delay.h>
11 #include <linux/extcon-provider.h>
12 #include <linux/interrupt.h>
13 #include <linux/io.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/jiffies.h>
16 #include <linux/kernel.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_irq.h>
22 #include <linux/of_platform.h>
23 #include <linux/phy/phy.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/power_supply.h>
27 #include <linux/regmap.h>
28 #include <linux/reset.h>
29 #include <linux/rockchip/cpu.h>
30 #include <linux/mfd/syscon.h>
31 #include <linux/usb/of.h>
32 #include <linux/usb/otg.h>
33 #include <linux/usb/role.h>
34 #include <linux/usb/typec_mux.h>
35 #include <linux/wakelock.h>
36
37 #define BIT_WRITEABLE_SHIFT 16
38 #define SCHEDULE_DELAY (60 * HZ)
39 #define OTG_SCHEDULE_DELAY (1 * HZ)
40 #define BYPASS_SCHEDULE_DELAY (2 * HZ)
41 #define FILTER_COUNTER 0xF4240
42
43 struct rockchip_usb2phy;
44
45 enum rockchip_usb2phy_port_id {
46 USB2PHY_PORT_OTG,
47 USB2PHY_PORT_HOST,
48 USB2PHY_NUM_PORTS,
49 };
50
51 enum rockchip_usb2phy_host_state {
52 PHY_STATE_HS_ONLINE = 0,
53 PHY_STATE_DISCONNECT = 1,
54 PHY_STATE_CONNECT = 2,
55 PHY_STATE_FS_LS_ONLINE = 4,
56 PHY_STATE_SE1 = 6,
57 };
58
59 /**
60 * enum usb_chg_state - Different states involved in USB charger detection.
61 * @USB_CHG_STATE_UNDEFINED: USB charger is not connected or detection
62 * process is not yet started.
63 * @USB_CHG_STATE_WAIT_FOR_DCD: Waiting for Data pins contact.
64 * @USB_CHG_STATE_DCD_DONE: Data pin contact is detected.
65 * @USB_CHG_STATE_PRIMARY_DONE: Primary detection is completed (Detects
66 * between SDP and DCP/CDP).
67 * @USB_CHG_STATE_SECONDARY_DONE: Secondary detection is completed (Detects
68 * between DCP and CDP).
69 * @USB_CHG_STATE_DETECTED: USB charger type is determined.
70 */
71 enum usb_chg_state {
72 USB_CHG_STATE_UNDEFINED = 0,
73 USB_CHG_STATE_WAIT_FOR_DCD,
74 USB_CHG_STATE_DCD_DONE,
75 USB_CHG_STATE_PRIMARY_DONE,
76 USB_CHG_STATE_SECONDARY_DONE,
77 USB_CHG_STATE_DETECTED,
78 };
79
80 static const unsigned int rockchip_usb2phy_extcon_cable[] = {
81 EXTCON_USB,
82 EXTCON_USB_HOST,
83 EXTCON_USB_VBUS_EN,
84 EXTCON_CHG_USB_SDP,
85 EXTCON_CHG_USB_CDP,
86 EXTCON_CHG_USB_DCP,
87 EXTCON_CHG_USB_SLOW,
88 EXTCON_NONE,
89 };
90
91 struct usb2phy_reg {
92 unsigned int offset;
93 unsigned int bitend;
94 unsigned int bitstart;
95 unsigned int disable;
96 unsigned int enable;
97 };
98
99 /**
100 * struct rockchip_chg_det_reg - usb charger detect registers
101 * @cp_det: charging port detected successfully.
102 * @dcp_det: dedicated charging port detected successfully.
103 * @dp_det: assert data pin connect successfully.
104 * @idm_sink_en: open dm sink curren.
105 * @idp_sink_en: open dp sink current.
106 * @idp_src_en: open dm source current.
107 * @rdm_pdwn_en: open dm pull down resistor.
108 * @vdm_src_en: open dm voltage source.
109 * @vdp_src_en: open dp voltage source.
110 * @chg_mode: set phy in charge detection mode.
111 */
112 struct rockchip_chg_det_reg {
113 struct usb2phy_reg cp_det;
114 struct usb2phy_reg dcp_det;
115 struct usb2phy_reg dp_det;
116 struct usb2phy_reg idm_sink_en;
117 struct usb2phy_reg idp_sink_en;
118 struct usb2phy_reg idp_src_en;
119 struct usb2phy_reg rdm_pdwn_en;
120 struct usb2phy_reg vdm_src_en;
121 struct usb2phy_reg vdp_src_en;
122 struct usb2phy_reg chg_mode;
123 };
124
125 /**
126 * struct rockchip_usb2phy_port_cfg - usb-phy port configuration.
127 * @phy_sus: phy suspend register.
128 * @pipe_phystatus: select pipe phystatus from grf or phy.
129 * @bvalid_det_en: vbus valid rise detection enable register.
130 * @bvalid_det_st: vbus valid rise detection status register.
131 * @bvalid_det_clr: vbus valid rise detection clear register.
132 * @bvalid_grf_con: vbus valid software control.
133 * @bvalid_grf_sel: vbus valid software control select.
134 * @bvalid_phy_con: vbus valid external select and enable.
135 * @bypass_dm_en: usb bypass uart DM enable register.
136 * @bypass_sel: usb bypass uart select register.
137 * @bypass_iomux: usb bypass uart GRF iomux register.
138 * @bypass_bc: bypass battery charging module.
139 * @bypass_otg: bypass otg module.
140 * @bypass_host: bypass host module.
141 * @disfall_en: host disconnect fall edge detection enable.
142 * @disfall_st: host disconnect fall edge detection state.
143 * @disfall_clr: host disconnect fall edge detection clear.
144 * @disrise_en: host disconnect rise edge detection enable.
145 * @disrise_st: host disconnect rise edge detection state.
146 * @disrise_clr: host disconnect rise edge detection clear.
147 * @ls_det_en: linestate detection enable register.
148 * @ls_det_st: linestate detection state register.
149 * @ls_det_clr: linestate detection clear register.
150 * @iddig_output: iddig output from grf.
151 * @iddig_en: utmi iddig select between grf and phy,
152 * 0: from phy; 1: from grf
153 * @idfall_det_en: id fall detection enable register.
154 * @idfall_det_st: id fall detection state register.
155 * @idfall_det_clr: id fall detection clear register.
156 * @idrise_det_en: id rise detection enable register.
157 * @idrise_det_st: id rise detection state register.
158 * @idrise_det_clr: id rise detection clear register.
159 * @utmi_avalid: utmi vbus avalid status register.
160 * @utmi_bvalid: utmi vbus bvalid status register.
161 * @utmi_iddig: otg port id pin status register.
162 * @utmi_ls: utmi linestate state register.
163 * @utmi_hstdet: utmi host disconnect register.
164 * @vbus_det_en: vbus detect function power down register.
165 * @port_ls_filter_con: set linestate filter time for otg port or host port.
166 */
167 struct rockchip_usb2phy_port_cfg {
168 struct usb2phy_reg phy_sus;
169 struct usb2phy_reg pipe_phystatus;
170 struct usb2phy_reg bvalid_det_en;
171 struct usb2phy_reg bvalid_det_st;
172 struct usb2phy_reg bvalid_det_clr;
173 struct usb2phy_reg bvalid_grf_con;
174 struct usb2phy_reg bvalid_grf_sel;
175 struct usb2phy_reg bvalid_phy_con;
176 struct usb2phy_reg bypass_dm_en;
177 struct usb2phy_reg bypass_sel;
178 struct usb2phy_reg bypass_iomux;
179 struct usb2phy_reg bypass_bc;
180 struct usb2phy_reg bypass_otg;
181 struct usb2phy_reg bypass_host;
182 struct usb2phy_reg disfall_en;
183 struct usb2phy_reg disfall_st;
184 struct usb2phy_reg disfall_clr;
185 struct usb2phy_reg disrise_en;
186 struct usb2phy_reg disrise_st;
187 struct usb2phy_reg disrise_clr;
188 struct usb2phy_reg ls_det_en;
189 struct usb2phy_reg ls_det_st;
190 struct usb2phy_reg ls_det_clr;
191 struct usb2phy_reg iddig_output;
192 struct usb2phy_reg iddig_en;
193 struct usb2phy_reg idfall_det_en;
194 struct usb2phy_reg idfall_det_st;
195 struct usb2phy_reg idfall_det_clr;
196 struct usb2phy_reg idrise_det_en;
197 struct usb2phy_reg idrise_det_st;
198 struct usb2phy_reg idrise_det_clr;
199 struct usb2phy_reg utmi_avalid;
200 struct usb2phy_reg utmi_bvalid;
201 struct usb2phy_reg utmi_iddig;
202 struct usb2phy_reg utmi_ls;
203 struct usb2phy_reg utmi_hstdet;
204 struct usb2phy_reg vbus_det_en;
205 struct usb2phy_reg port_ls_filter_con;
206 };
207
208 /**
209 * struct rockchip_usb2phy_cfg - usb-phy configuration.
210 * @reg: the address offset of grf for usb-phy config.
211 * @num_ports: specify how many ports that the phy has.
212 * @phy_tuning: phy default parameters tuning.
213 * @vbus_detect: vbus voltage level detection function.
214 * @clkout_ctl: keep on/turn off output clk of phy via commonon bit.
215 * @clkout_ctl_phy: keep on/turn off output clk of phy via phy inner
216 * debug register.
217 * @ls_filter_con: set linestate filter time.
218 * @port_cfgs: usb-phy port configurations.
219 * @ls_filter_con: set linestate filter time.
220 * @chg_det: charger detection registers.
221 */
222 struct rockchip_usb2phy_cfg {
223 unsigned int reg;
224 unsigned int num_ports;
225 int (*phy_tuning)(struct rockchip_usb2phy *rphy);
226 int (*vbus_detect)(struct rockchip_usb2phy *rphy,
227 const struct usb2phy_reg *vbus_det_en,
228 bool en);
229 struct usb2phy_reg clkout_ctl;
230 struct usb2phy_reg clkout_ctl_phy;
231 struct usb2phy_reg ls_filter_con;
232 const struct rockchip_usb2phy_port_cfg port_cfgs[USB2PHY_NUM_PORTS];
233 const struct rockchip_chg_det_reg chg_det;
234 };
235
236 /**
237 * struct rockchip_usb2phy_port - usb-phy port data.
238 * @phy: generic phy.
239 * @port_id: flag for otg port or host port.
240 * @low_power_en: enable enter low power when suspend.
241 * @perip_connected: flag for periphyeral connect status.
242 * @prev_iddig: previous otg port id pin status.
243 * @sel_pipe_phystatus: select pipe phystatus from grf.
244 * @suspended: phy suspended flag.
245 * @typec_vbus_det: Type-C otg vbus detect.
246 * @utmi_avalid: utmi avalid status usage flag.
247 * true - use avalid to get vbus status
248 * false - use bvalid to get vbus status
249 * @vbus_attached: otg device vbus status.
250 * @vbus_always_on: otg vbus is always powered on.
251 * @vbus_enabled: vbus regulator status.
252 * @bypass_uart_en: usb bypass uart enable, passed from DT.
253 * @host_disconnect: usb host disconnect status.
254 * @dis_u2_susphy: disable usb2 phy suspend.
255 * @bvalid_irq: IRQ number assigned for vbus valid rise detection.
256 * @ls_irq: IRQ number assigned for linestate detection.
257 * @id_irq: IRQ number assigned for id fall or rise detection.
258 * @otg_mux_irq: IRQ number which multiplex otg-id/otg-bvalid/linestate
259 * irqs to one irq in otg-port.
260 * @mutex: for register updating in sm_work.
261 * @chg_work: charge detect work.
262 * @bypass_uart_work: usb bypass uart work.
263 * @otg_sm_work: OTG state machine work.
264 * @sm_work: HOST state machine work.
265 * @vbus: vbus regulator supply on few rockchip boards.
266 * @sw: orientation switch, communicate with TCPM (Type-C Port Manager).
267 * @port_cfg: port register configuration, assigned by driver data.
268 * @event_nb: hold event notification callback.
269 * @state: define OTG enumeration states before device reset.
270 * @mode: the dr_mode of the controller.
271 */
272 struct rockchip_usb2phy_port {
273 struct phy *phy;
274 unsigned int port_id;
275 bool low_power_en;
276 bool perip_connected;
277 bool prev_iddig;
278 bool sel_pipe_phystatus;
279 bool suspended;
280 bool typec_vbus_det;
281 bool utmi_avalid;
282 bool vbus_attached;
283 bool vbus_always_on;
284 bool vbus_enabled;
285 bool bypass_uart_en;
286 bool host_disconnect;
287 bool dis_u2_susphy;
288 int bvalid_irq;
289 int ls_irq;
290 int id_irq;
291 int otg_mux_irq;
292 struct mutex mutex;
293 struct delayed_work bypass_uart_work;
294 struct delayed_work chg_work;
295 struct delayed_work otg_sm_work;
296 struct delayed_work sm_work;
297 struct regulator *vbus;
298 struct typec_switch *sw;
299 const struct rockchip_usb2phy_port_cfg *port_cfg;
300 struct notifier_block event_nb;
301 struct wake_lock wakelock;
302 enum usb_otg_state state;
303 enum usb_dr_mode mode;
304 };
305
306 /**
307 * struct rockchip_usb2phy - usb2.0 phy driver data.
308 * @dev: pointer to device.
309 * @grf: General Register Files regmap.
310 * @usbgrf: USB General Register Files regmap.
311 * @usbctrl_grf: USB Controller General Register Files regmap.
312 * *phy_base: the base address of USB PHY.
313 * @phy_reset: phy reset control.
314 * @clks: array of phy input clocks.
315 * @clk480m: clock struct of phy output clk.
316 * @clk480m_hw: clock struct of phy output clk management.
317 * @num_clks: number of phy input clocks.
318 * @chg_state: states involved in USB charger detection.
319 * @chg_type: USB charger types.
320 * @dcd_retries: The retry count used to track Data contact
321 * detection process.
322 * @primary_retries: The retry count used for charger
323 * detection primary phase.
324 * @phy_sus_cfg: Store the phy current suspend configuration.
325 * @edev_self: represent the source of extcon.
326 * @irq: IRQ number assigned for phy which combined irqs of
327 * otg port and host port.
328 * @edev: extcon device for notification registration
329 * @phy_cfg: phy register configuration, assigned by driver data.
330 * @ports: phy port instance.
331 */
332 struct rockchip_usb2phy {
333 struct device *dev;
334 struct regmap *grf;
335 struct regmap *usbgrf;
336 struct regmap *usbctrl_grf;
337 void __iomem *phy_base;
338 struct reset_control *phy_reset;
339 struct clk_bulk_data *clks;
340 struct clk *clk480m;
341 struct clk_hw clk480m_hw;
342 int num_clks;
343 enum usb_chg_state chg_state;
344 enum power_supply_type chg_type;
345 u8 dcd_retries;
346 u8 primary_retries;
347 unsigned int phy_sus_cfg;
348 bool edev_self;
349 int irq;
350 struct extcon_dev *edev;
351 const struct rockchip_usb2phy_cfg *phy_cfg;
352 struct rockchip_usb2phy_port ports[USB2PHY_NUM_PORTS];
353 };
354
get_reg_base(struct rockchip_usb2phy * rphy)355 static inline struct regmap *get_reg_base(struct rockchip_usb2phy *rphy)
356 {
357 return rphy->usbgrf == NULL ? rphy->grf : rphy->usbgrf;
358 }
359
property_enable(struct regmap * base,const struct usb2phy_reg * reg,bool en)360 static inline int property_enable(struct regmap *base,
361 const struct usb2phy_reg *reg, bool en)
362 {
363 unsigned int val, mask, tmp;
364
365 tmp = en ? reg->enable : reg->disable;
366 mask = GENMASK(reg->bitend, reg->bitstart);
367 val = (tmp << reg->bitstart) | (mask << BIT_WRITEABLE_SHIFT);
368
369 return regmap_write(base, reg->offset, val);
370 }
371
property_enabled(struct regmap * base,const struct usb2phy_reg * reg)372 static inline bool property_enabled(struct regmap *base,
373 const struct usb2phy_reg *reg)
374 {
375 int ret;
376 unsigned int tmp, orig;
377 unsigned int mask = GENMASK(reg->bitend, reg->bitstart);
378
379 ret = regmap_read(base, reg->offset, &orig);
380 if (ret)
381 return false;
382
383 tmp = (orig & mask) >> reg->bitstart;
384 return tmp == reg->enable;
385 }
386
phy_property_enable(void __iomem * base,const struct usb2phy_reg * reg,bool en)387 static inline void phy_property_enable(void __iomem *base,
388 const struct usb2phy_reg *reg, bool en)
389 {
390 unsigned int val, tmp;
391
392 val = readl(base + reg->offset);
393 tmp = en ? reg->enable : reg->disable;
394 val &= ~GENMASK(reg->bitend, reg->bitstart);
395 val |= tmp << reg->bitstart;
396 writel(val, base + reg->offset);
397 }
398
phy_property_enabled(void __iomem * base,const struct usb2phy_reg * reg)399 static inline bool phy_property_enabled(void __iomem *base,
400 const struct usb2phy_reg *reg)
401 {
402 unsigned int orig, tmp;
403 unsigned int mask = GENMASK(reg->bitend, reg->bitstart);
404
405 orig = readl(base + reg->offset);
406 tmp = (orig & mask) >> reg->bitstart;
407 return tmp == reg->enable;
408 }
409
phy_clear_bits(void __iomem * reg,u32 bits)410 static inline void phy_clear_bits(void __iomem *reg, u32 bits)
411 {
412 u32 tmp = readl(reg);
413
414 tmp &= ~bits;
415 writel(tmp, reg);
416 }
417
phy_set_bits(void __iomem * reg,u32 bits)418 static inline void phy_set_bits(void __iomem *reg, u32 bits)
419 {
420 u32 tmp = readl(reg);
421
422 tmp |= bits;
423 writel(tmp, reg);
424 }
425
phy_update_bits(void __iomem * reg,u32 mask,u32 val)426 static inline void phy_update_bits(void __iomem *reg, u32 mask, u32 val)
427 {
428 u32 tmp = readl(reg);
429
430 tmp &= ~mask;
431 tmp |= val & mask;
432 writel(tmp, reg);
433 }
434
rockchip_usb2phy_reset(struct rockchip_usb2phy * rphy)435 static int rockchip_usb2phy_reset(struct rockchip_usb2phy *rphy)
436 {
437 int ret;
438
439 if (!rphy->phy_reset)
440 return 0;
441
442 ret = reset_control_assert(rphy->phy_reset);
443 if (ret)
444 return ret;
445
446 udelay(10);
447
448 ret = reset_control_deassert(rphy->phy_reset);
449 if (ret)
450 return ret;
451
452 usleep_range(100, 200);
453
454 return 0;
455 }
456
rockchip_usb2phy_clk480m_prepare(struct clk_hw * hw)457 static int rockchip_usb2phy_clk480m_prepare(struct clk_hw *hw)
458 {
459 struct rockchip_usb2phy *rphy =
460 container_of(hw, struct rockchip_usb2phy, clk480m_hw);
461 struct regmap *base = get_reg_base(rphy);
462 int ret;
463
464 /* turn on 480m clk output if it is off */
465 if (rphy->phy_cfg->clkout_ctl_phy.enable) {
466 if (!phy_property_enabled(rphy->phy_base, &rphy->phy_cfg->clkout_ctl_phy)) {
467 phy_property_enable(rphy->phy_base, &rphy->phy_cfg->clkout_ctl_phy, true);
468
469 /* waiting for the clk become stable */
470 usleep_range(1200, 1300);
471 }
472 } else if (!property_enabled(base, &rphy->phy_cfg->clkout_ctl)) {
473 ret = property_enable(base, &rphy->phy_cfg->clkout_ctl, true);
474 if (ret)
475 return ret;
476
477 /* waiting for the clk become stable */
478 usleep_range(1200, 1300);
479 }
480
481 return 0;
482 }
483
rockchip_usb2phy_clk480m_unprepare(struct clk_hw * hw)484 static void rockchip_usb2phy_clk480m_unprepare(struct clk_hw *hw)
485 {
486 struct rockchip_usb2phy *rphy =
487 container_of(hw, struct rockchip_usb2phy, clk480m_hw);
488 struct regmap *base = get_reg_base(rphy);
489
490 /* turn off 480m clk output */
491 if (rphy->phy_cfg->clkout_ctl_phy.enable)
492 phy_property_enable(rphy->phy_base, &rphy->phy_cfg->clkout_ctl_phy, false);
493 else
494 property_enable(base, &rphy->phy_cfg->clkout_ctl, false);
495 }
496
rockchip_usb2phy_clk480m_prepared(struct clk_hw * hw)497 static int rockchip_usb2phy_clk480m_prepared(struct clk_hw *hw)
498 {
499 struct rockchip_usb2phy *rphy =
500 container_of(hw, struct rockchip_usb2phy, clk480m_hw);
501 struct regmap *base = get_reg_base(rphy);
502
503 if (rphy->phy_cfg->clkout_ctl_phy.enable)
504 return phy_property_enabled(rphy->phy_base, &rphy->phy_cfg->clkout_ctl_phy);
505 else
506 return property_enabled(base, &rphy->phy_cfg->clkout_ctl);
507 }
508
509 static unsigned long
rockchip_usb2phy_clk480m_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)510 rockchip_usb2phy_clk480m_recalc_rate(struct clk_hw *hw,
511 unsigned long parent_rate)
512 {
513 return 480000000;
514 }
515
516 static const struct clk_ops rockchip_usb2phy_clkout_ops = {
517 .prepare = rockchip_usb2phy_clk480m_prepare,
518 .unprepare = rockchip_usb2phy_clk480m_unprepare,
519 .is_prepared = rockchip_usb2phy_clk480m_prepared,
520 .recalc_rate = rockchip_usb2phy_clk480m_recalc_rate,
521 };
522
rockchip_usb2phy_clk480m_unregister(void * data)523 static void rockchip_usb2phy_clk480m_unregister(void *data)
524 {
525 struct rockchip_usb2phy *rphy = data;
526
527 of_clk_del_provider(rphy->dev->of_node);
528 clk_unregister(rphy->clk480m);
529 }
530
531 static int
rockchip_usb2phy_clk480m_register(struct rockchip_usb2phy * rphy)532 rockchip_usb2phy_clk480m_register(struct rockchip_usb2phy *rphy)
533 {
534 struct device_node *node = rphy->dev->of_node;
535 struct clk_init_data init = {};
536 struct clk *refclk = of_clk_get_by_name(node, "phyclk");
537 const char *clk_name;
538 int ret;
539
540 init.flags = 0;
541 init.name = "clk_usbphy_480m";
542 init.ops = &rockchip_usb2phy_clkout_ops;
543
544 /* optional override of the clockname */
545 of_property_read_string(node, "clock-output-names", &init.name);
546
547 if (!IS_ERR(refclk)) {
548 clk_name = __clk_get_name(refclk);
549 init.parent_names = &clk_name;
550 init.num_parents = 1;
551 } else {
552 init.parent_names = NULL;
553 init.num_parents = 0;
554 }
555
556 rphy->clk480m_hw.init = &init;
557
558 /* register the clock */
559 rphy->clk480m = clk_register(rphy->dev, &rphy->clk480m_hw);
560 if (IS_ERR(rphy->clk480m)) {
561 ret = PTR_ERR(rphy->clk480m);
562 goto err_ret;
563 }
564
565 ret = of_clk_add_provider(node, of_clk_src_simple_get, rphy->clk480m);
566 if (ret < 0)
567 goto err_clk_provider;
568
569 ret = devm_add_action(rphy->dev, rockchip_usb2phy_clk480m_unregister,
570 rphy);
571 if (ret < 0)
572 goto err_unreg_action;
573
574 return 0;
575
576 err_unreg_action:
577 of_clk_del_provider(node);
578 err_clk_provider:
579 clk_unregister(rphy->clk480m);
580 err_ret:
581 return ret;
582 }
583
rockchip_usb2phy_extcon_register(struct rockchip_usb2phy * rphy)584 static int rockchip_usb2phy_extcon_register(struct rockchip_usb2phy *rphy)
585 {
586 int ret;
587 struct device_node *node = rphy->dev->of_node;
588 struct extcon_dev *edev;
589
590 if (of_property_read_bool(node, "extcon")) {
591 edev = extcon_get_edev_by_phandle(rphy->dev, 0);
592 if (IS_ERR(edev)) {
593 if (PTR_ERR(edev) != -EPROBE_DEFER)
594 dev_err(rphy->dev, "Invalid or missing extcon\n");
595 return PTR_ERR(edev);
596 }
597 } else {
598 /* Initialize extcon device */
599 edev = devm_extcon_dev_allocate(rphy->dev,
600 rockchip_usb2phy_extcon_cable);
601
602 if (IS_ERR(edev))
603 return -ENOMEM;
604
605 ret = devm_extcon_dev_register(rphy->dev, edev);
606 if (ret) {
607 dev_err(rphy->dev, "failed to register extcon device\n");
608 return ret;
609 }
610
611 rphy->edev_self = true;
612 }
613
614 rphy->edev = edev;
615
616 return 0;
617 }
618
619 /* The caller must hold rport->mutex lock */
rockchip_usb2phy_enable_id_irq(struct rockchip_usb2phy * rphy,struct rockchip_usb2phy_port * rport,bool en)620 static int rockchip_usb2phy_enable_id_irq(struct rockchip_usb2phy *rphy,
621 struct rockchip_usb2phy_port *rport,
622 bool en)
623 {
624 int ret;
625
626 ret = property_enable(rphy->grf, &rport->port_cfg->idfall_det_clr, true);
627 if (ret)
628 goto out;
629
630 ret = property_enable(rphy->grf, &rport->port_cfg->idfall_det_en, en);
631 if (ret)
632 goto out;
633
634 ret = property_enable(rphy->grf, &rport->port_cfg->idrise_det_clr, true);
635 if (ret)
636 goto out;
637
638 ret = property_enable(rphy->grf, &rport->port_cfg->idrise_det_en, en);
639 out:
640 return ret;
641 }
642
643 /* The caller must hold rport->mutex lock */
rockchip_usb2phy_enable_vbus_irq(struct rockchip_usb2phy * rphy,struct rockchip_usb2phy_port * rport,bool en)644 static int rockchip_usb2phy_enable_vbus_irq(struct rockchip_usb2phy *rphy,
645 struct rockchip_usb2phy_port *rport,
646 bool en)
647 {
648 int ret;
649
650 ret = property_enable(rphy->grf, &rport->port_cfg->bvalid_det_clr, true);
651 if (ret)
652 goto out;
653
654 ret = property_enable(rphy->grf, &rport->port_cfg->bvalid_det_en, en);
655 out:
656 return ret;
657 }
658
rockchip_usb2phy_enable_line_irq(struct rockchip_usb2phy * rphy,struct rockchip_usb2phy_port * rport,bool en)659 static int rockchip_usb2phy_enable_line_irq(struct rockchip_usb2phy *rphy,
660 struct rockchip_usb2phy_port *rport,
661 bool en)
662 {
663 int ret;
664
665 ret = property_enable(rphy->grf, &rport->port_cfg->ls_det_clr, true);
666 if (ret)
667 goto out;
668
669 ret = property_enable(rphy->grf, &rport->port_cfg->ls_det_en, en);
670 out:
671 return ret;
672 }
673
rockchip_usb2phy_enable_host_disc_irq(struct rockchip_usb2phy * rphy,struct rockchip_usb2phy_port * rport,bool en)674 static int rockchip_usb2phy_enable_host_disc_irq(struct rockchip_usb2phy *rphy,
675 struct rockchip_usb2phy_port *rport,
676 bool en)
677 {
678 int ret;
679
680 ret = property_enable(rphy->grf, &rport->port_cfg->disfall_clr, true);
681 if (ret)
682 goto out;
683
684 ret = property_enable(rphy->grf, &rport->port_cfg->disfall_en, en);
685 if (ret)
686 goto out;
687
688 ret = property_enable(rphy->grf, &rport->port_cfg->disrise_clr, true);
689 if (ret)
690 goto out;
691
692 ret = property_enable(rphy->grf, &rport->port_cfg->disrise_en, en);
693 out:
694 return ret;
695 }
696
rockchip_usb_bypass_uart(struct rockchip_usb2phy_port * rport,bool en)697 static int rockchip_usb_bypass_uart(struct rockchip_usb2phy_port *rport,
698 bool en)
699 {
700 struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
701 const struct usb2phy_reg *iomux = &rport->port_cfg->bypass_iomux;
702 struct regmap *base = get_reg_base(rphy);
703 int ret = 0;
704
705 mutex_lock(&rport->mutex);
706
707 if (en == property_enabled(base, &rport->port_cfg->bypass_sel)) {
708 dev_info(&rport->phy->dev,
709 "bypass uart %s is already set\n", en ? "on" : "off");
710 goto unlock;
711 }
712
713 dev_info(&rport->phy->dev, "bypass uart %s\n", en ? "on" : "off");
714
715 if (en) {
716 /*
717 * To use UART function:
718 * 1. Put the USB PHY in suspend mode and opmode is normal;
719 * 2. Set bypasssel to 1'b1 and bypassdmen to 1'b1;
720 *
721 * Note: Although the datasheet requires that put USB PHY
722 * in non-driving mode to disable resistance when use USB
723 * bypass UART function, but actually we find that if we
724 * set phy in non-driving mode, it will cause UART to print
725 * random codes. So just put USB PHY in normal mode.
726 */
727 ret |= property_enable(base, &rport->port_cfg->bypass_sel,
728 true);
729 ret |= property_enable(base, &rport->port_cfg->bypass_dm_en,
730 true);
731
732 /* Some platforms required to set iomux of bypass uart */
733 if (iomux->offset)
734 ret |= property_enable(rphy->grf, iomux, true);
735 } else {
736 /* just disable bypass, and resume phy in phy power_on later */
737 ret |= property_enable(base, &rport->port_cfg->bypass_sel,
738 false);
739 ret |= property_enable(base, &rport->port_cfg->bypass_dm_en,
740 false);
741
742 /* Some platforms required to set iomux of bypass uart */
743 if (iomux->offset)
744 ret |= property_enable(rphy->grf, iomux, false);
745 }
746
747 unlock:
748 mutex_unlock(&rport->mutex);
749
750 return ret;
751 }
752
rockchip_usb_bypass_uart_work(struct work_struct * work)753 static void rockchip_usb_bypass_uart_work(struct work_struct *work)
754 {
755 struct rockchip_usb2phy_port *rport =
756 container_of(work, struct rockchip_usb2phy_port,
757 bypass_uart_work.work);
758 struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
759 bool vbus, iddig;
760 int ret;
761
762 mutex_lock(&rport->mutex);
763
764 iddig = property_enabled(rphy->grf, &rport->port_cfg->utmi_iddig);
765
766 if (rport->utmi_avalid)
767 vbus = property_enabled(rphy->grf, &rport->port_cfg->utmi_avalid);
768 else
769 vbus = property_enabled(rphy->grf, &rport->port_cfg->utmi_bvalid);
770
771 mutex_unlock(&rport->mutex);
772
773 /*
774 * If the vbus is low and iddig is high, it indicates that usb
775 * otg is not working, then we can enable usb to bypass uart,
776 * otherwise schedule the work until the conditions (vbus is low
777 * and iddig is high) are matched.
778 */
779 if (!vbus && iddig) {
780 ret = rockchip_usb_bypass_uart(rport, true);
781 if (ret)
782 dev_warn(&rport->phy->dev,
783 "failed to enable bypass uart\n");
784 } else {
785 schedule_delayed_work(&rport->bypass_uart_work,
786 BYPASS_SCHEDULE_DELAY);
787 }
788 }
789
rockchip_usb2phy_init(struct phy * phy)790 static int rockchip_usb2phy_init(struct phy *phy)
791 {
792 struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
793 struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
794 int ret = 0;
795 unsigned int ul, ul_mask;
796
797 mutex_lock(&rport->mutex);
798
799 if (rport->sel_pipe_phystatus)
800 property_enable(rphy->usbctrl_grf,
801 &rport->port_cfg->pipe_phystatus, true);
802
803 if (rport->port_id == USB2PHY_PORT_OTG &&
804 (rport->mode == USB_DR_MODE_PERIPHERAL ||
805 rport->mode == USB_DR_MODE_OTG)) {
806 /* clear id status and enable id detect irq */
807 if (rport->id_irq > 0 || rport->otg_mux_irq > 0 ||
808 rphy->irq > 0) {
809 ret = rockchip_usb2phy_enable_id_irq(rphy, rport,
810 true);
811 if (ret) {
812 dev_err(rphy->dev,
813 "failed to enable id irq\n");
814 goto out;
815 }
816 }
817
818 /* clear bvalid status and enable bvalid detect irq */
819 if ((rport->bvalid_irq > 0 || rport->otg_mux_irq > 0 ||
820 rphy->irq > 0) && !rport->vbus_always_on) {
821 ret = rockchip_usb2phy_enable_vbus_irq(rphy, rport,
822 true);
823 if (ret) {
824 dev_err(rphy->dev,
825 "failed to enable bvalid irq\n");
826 goto out;
827 }
828 schedule_delayed_work(&rport->otg_sm_work,
829 rport->typec_vbus_det ? 0 : OTG_SCHEDULE_DELAY);
830 }
831 } else if (rport->port_id == USB2PHY_PORT_HOST) {
832 if (rport->port_cfg->disfall_en.offset) {
833 ret = regmap_read(rphy->grf, rport->port_cfg->utmi_ls.offset, &ul);
834 if (ret < 0)
835 goto out;
836 ul_mask = GENMASK(rport->port_cfg->utmi_ls.bitend,
837 rport->port_cfg->utmi_ls.bitstart);
838 rport->host_disconnect = (ul & ul_mask) == 0 ? true : false;
839 ret = rockchip_usb2phy_enable_host_disc_irq(rphy, rport, true);
840 if (ret) {
841 dev_err(rphy->dev, "failed to enable disconnect irq\n");
842 goto out;
843 }
844 }
845
846 /* clear linestate and enable linestate detect irq */
847 ret = rockchip_usb2phy_enable_line_irq(rphy, rport, true);
848 if (ret) {
849 dev_err(rphy->dev, "failed to enable linestate irq\n");
850 goto out;
851 }
852
853 schedule_delayed_work(&rport->sm_work, SCHEDULE_DELAY);
854 }
855
856 out:
857 mutex_unlock(&rport->mutex);
858 return ret;
859 }
860
rockchip_usb2phy_power_on(struct phy * phy)861 static int rockchip_usb2phy_power_on(struct phy *phy)
862 {
863 struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
864 struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
865 struct regmap *base = get_reg_base(rphy);
866 int ret;
867
868 dev_dbg(&rport->phy->dev, "port power on\n");
869
870 if (rport->bypass_uart_en) {
871 ret = rockchip_usb_bypass_uart(rport, false);
872 if (ret) {
873 dev_warn(&rport->phy->dev,
874 "failed to disable bypass uart\n");
875 goto exit;
876 }
877 }
878
879 mutex_lock(&rport->mutex);
880
881 if (!rport->suspended) {
882 ret = 0;
883 goto unlock;
884 }
885
886 ret = clk_prepare_enable(rphy->clk480m);
887 if (ret)
888 goto unlock;
889
890 ret = property_enable(base, &rport->port_cfg->phy_sus, false);
891 if (ret)
892 goto unlock;
893
894 /*
895 * For rk3588, it needs to reset phy when exit from
896 * suspend mode with common_on_n 1'b1(aka REFCLK_LOGIC,
897 * Bias, and PLL blocks are powered down) for lower
898 * power consumption. If you don't want to reset phy,
899 * please keep the common_on_n 1'b0 to set these blocks
900 * remain powered.
901 */
902 if (rport->port_id == USB2PHY_PORT_OTG &&
903 of_device_is_compatible(rphy->dev->of_node, "rockchip,rk3588-usb2phy")) {
904 ret = rockchip_usb2phy_reset(rphy);
905 if (ret)
906 goto unlock;
907 }
908
909 /* waiting for the utmi_clk to become stable */
910 usleep_range(1500, 2000);
911
912 rport->suspended = false;
913
914 unlock:
915 mutex_unlock(&rport->mutex);
916
917 /* Enable bypass uart in the bypass_uart_work. */
918 if (rport->bypass_uart_en)
919 schedule_delayed_work(&rport->bypass_uart_work, 0);
920
921 exit:
922 return ret;
923 }
924
rockchip_usb2phy_power_off(struct phy * phy)925 static int rockchip_usb2phy_power_off(struct phy *phy)
926 {
927 struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
928 struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
929 struct regmap *base = get_reg_base(rphy);
930 int ret;
931
932 dev_dbg(&rport->phy->dev, "port power off\n");
933
934 mutex_lock(&rport->mutex);
935
936 if (rport->suspended) {
937 ret = 0;
938 goto unlock;
939 }
940
941 ret = property_enable(base, &rport->port_cfg->phy_sus, true);
942 if (ret)
943 goto unlock;
944
945 rport->suspended = true;
946 clk_disable_unprepare(rphy->clk480m);
947
948 unlock:
949 mutex_unlock(&rport->mutex);
950
951 /* Enable bypass uart in the bypass_uart_work. */
952 if (rport->bypass_uart_en)
953 schedule_delayed_work(&rport->bypass_uart_work, 0);
954
955 return ret;
956 }
957
rockchip_usb2phy_exit(struct phy * phy)958 static int rockchip_usb2phy_exit(struct phy *phy)
959 {
960 struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
961
962 if (rport->port_id == USB2PHY_PORT_HOST)
963 cancel_delayed_work_sync(&rport->sm_work);
964 else if (rport->port_id == USB2PHY_PORT_OTG &&
965 rport->otg_sm_work.work.func)
966 flush_delayed_work(&rport->otg_sm_work);
967
968 return 0;
969 }
970
rockchip_set_vbus_power(struct rockchip_usb2phy_port * rport,bool en)971 static int rockchip_set_vbus_power(struct rockchip_usb2phy_port *rport,
972 bool en)
973 {
974 int ret = 0;
975
976 if (!rport->vbus)
977 return 0;
978
979 if (en && !rport->vbus_enabled) {
980 ret = regulator_enable(rport->vbus);
981 if (ret)
982 dev_err(&rport->phy->dev,
983 "Failed to enable VBUS supply\n");
984 } else if (!en && rport->vbus_enabled) {
985 ret = regulator_disable(rport->vbus);
986 }
987
988 if (ret == 0)
989 rport->vbus_enabled = en;
990
991 return ret;
992 }
993
rockchip_usb2phy_set_mode(struct phy * phy,enum phy_mode mode,int submode)994 static int rockchip_usb2phy_set_mode(struct phy *phy,
995 enum phy_mode mode, int submode)
996 {
997 struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
998 struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
999 bool vbus_det_en;
1000 int ret = 0;
1001
1002 if (rport->port_id != USB2PHY_PORT_OTG)
1003 return ret;
1004
1005 switch (mode) {
1006 case PHY_MODE_USB_OTG:
1007 if (rphy->edev_self && submode) {
1008 if (submode == USB_ROLE_HOST) {
1009 extcon_set_state(rphy->edev, EXTCON_USB_HOST, true);
1010 extcon_set_state(rphy->edev, EXTCON_USB, false);
1011 } else if (submode == USB_ROLE_DEVICE) {
1012 extcon_set_state(rphy->edev, EXTCON_USB_HOST, false);
1013 extcon_set_state(rphy->edev, EXTCON_USB, true);
1014 }
1015
1016 return ret;
1017 }
1018
1019 /*
1020 * In case of using vbus to detect connect state by u2phy,
1021 * enable vbus detect on otg mode.
1022 */
1023 fallthrough;
1024 case PHY_MODE_USB_DEVICE:
1025 /* Disable VBUS supply */
1026 rockchip_set_vbus_power(rport, false);
1027 extcon_set_state_sync(rphy->edev, EXTCON_USB_VBUS_EN, false);
1028 /* For vbus always on, set EXTCON_USB to true. */
1029 if (rport->vbus_always_on)
1030 extcon_set_state(rphy->edev, EXTCON_USB, true);
1031 rport->perip_connected = true;
1032 vbus_det_en = true;
1033 break;
1034 case PHY_MODE_USB_HOST:
1035 /* Enable VBUS supply */
1036 ret = rockchip_set_vbus_power(rport, true);
1037 if (ret) {
1038 dev_err(&rport->phy->dev,
1039 "Failed to set host mode\n");
1040 return ret;
1041 }
1042
1043 extcon_set_state_sync(rphy->edev, EXTCON_USB_VBUS_EN, true);
1044 /* For vbus always on, deinit EXTCON_USB to false. */
1045 if (rport->vbus_always_on)
1046 extcon_set_state(rphy->edev, EXTCON_USB, false);
1047 rport->perip_connected = false;
1048 fallthrough;
1049 case PHY_MODE_INVALID:
1050 vbus_det_en = false;
1051 break;
1052 default:
1053 dev_info(&rport->phy->dev, "illegal mode\n");
1054 return ret;
1055 }
1056
1057 if (rphy->phy_cfg->vbus_detect)
1058 rphy->phy_cfg->vbus_detect(rphy, &rport->port_cfg->vbus_det_en,
1059 vbus_det_en);
1060 else
1061 ret = property_enable(rphy->grf, &rport->port_cfg->vbus_det_en,
1062 vbus_det_en);
1063
1064 return ret;
1065 }
1066
1067 static const struct phy_ops rockchip_usb2phy_ops = {
1068 .init = rockchip_usb2phy_init,
1069 .exit = rockchip_usb2phy_exit,
1070 .power_on = rockchip_usb2phy_power_on,
1071 .power_off = rockchip_usb2phy_power_off,
1072 .set_mode = rockchip_usb2phy_set_mode,
1073 .owner = THIS_MODULE,
1074 };
1075
1076 /* Show & store the current value of otg mode for otg port */
otg_mode_show(struct device * device,struct device_attribute * attr,char * buf)1077 static ssize_t otg_mode_show(struct device *device,
1078 struct device_attribute *attr,
1079 char *buf)
1080 {
1081 struct rockchip_usb2phy *rphy = dev_get_drvdata(device);
1082 struct rockchip_usb2phy_port *rport = NULL;
1083 unsigned int index;
1084
1085 for (index = 0; index < rphy->phy_cfg->num_ports; index++) {
1086 rport = &rphy->ports[index];
1087 if (rport->port_id == USB2PHY_PORT_OTG)
1088 break;
1089 }
1090
1091 if (!rport) {
1092 dev_err(rphy->dev, "Fail to get otg port\n");
1093 return -EINVAL;
1094 } else if (rport->port_id != USB2PHY_PORT_OTG) {
1095 dev_err(rphy->dev, "No support otg\n");
1096 return -EINVAL;
1097 }
1098
1099 switch (rport->mode) {
1100 case USB_DR_MODE_HOST:
1101 return sprintf(buf, "host\n");
1102 case USB_DR_MODE_PERIPHERAL:
1103 return sprintf(buf, "peripheral\n");
1104 case USB_DR_MODE_OTG:
1105 return sprintf(buf, "otg\n");
1106 case USB_DR_MODE_UNKNOWN:
1107 return sprintf(buf, "UNKNOWN\n");
1108 }
1109
1110 return -EINVAL;
1111 }
1112
otg_mode_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count)1113 static ssize_t otg_mode_store(struct device *device,
1114 struct device_attribute *attr,
1115 const char *buf, size_t count)
1116 {
1117 struct rockchip_usb2phy *rphy = dev_get_drvdata(device);
1118 struct rockchip_usb2phy_port *rport = NULL;
1119 struct regmap *base = get_reg_base(rphy);
1120 enum usb_dr_mode new_dr_mode;
1121 unsigned int index;
1122 int rc = count;
1123
1124 for (index = 0; index < rphy->phy_cfg->num_ports; index++) {
1125 rport = &rphy->ports[index];
1126 if (rport->port_id == USB2PHY_PORT_OTG)
1127 break;
1128 }
1129
1130 if (!rport) {
1131 dev_err(rphy->dev, "Fail to get otg port\n");
1132 rc = -EINVAL;
1133 goto err0;
1134 } else if (rport->port_id != USB2PHY_PORT_OTG ||
1135 rport->mode == USB_DR_MODE_UNKNOWN) {
1136 dev_err(rphy->dev, "No support otg\n");
1137 rc = -EINVAL;
1138 goto err0;
1139 }
1140
1141 mutex_lock(&rport->mutex);
1142
1143 if (!strncmp(buf, "0", 1) || !strncmp(buf, "otg", 3)) {
1144 new_dr_mode = USB_DR_MODE_OTG;
1145 } else if (!strncmp(buf, "1", 1) || !strncmp(buf, "host", 4)) {
1146 new_dr_mode = USB_DR_MODE_HOST;
1147 } else if (!strncmp(buf, "2", 1) || !strncmp(buf, "peripheral", 10)) {
1148 new_dr_mode = USB_DR_MODE_PERIPHERAL;
1149 } else {
1150 dev_err(rphy->dev, "Error mode! Input 'otg' or 'host' or 'peripheral'\n");
1151 rc = -EINVAL;
1152 goto err1;
1153 }
1154
1155 if (rport->mode == new_dr_mode) {
1156 dev_warn(rphy->dev, "Same as current mode\n");
1157 goto err1;
1158 }
1159
1160 rport->mode = new_dr_mode;
1161
1162 switch (rport->mode) {
1163 case USB_DR_MODE_HOST:
1164 rockchip_usb2phy_set_mode(rport->phy, PHY_MODE_USB_HOST, 0);
1165 property_enable(base, &rport->port_cfg->iddig_output, false);
1166 property_enable(base, &rport->port_cfg->iddig_en, true);
1167 break;
1168 case USB_DR_MODE_PERIPHERAL:
1169 rockchip_usb2phy_set_mode(rport->phy, PHY_MODE_USB_DEVICE, 0);
1170 property_enable(base, &rport->port_cfg->iddig_output, true);
1171 property_enable(base, &rport->port_cfg->iddig_en, true);
1172 break;
1173 case USB_DR_MODE_OTG:
1174 rockchip_usb2phy_set_mode(rport->phy, PHY_MODE_USB_OTG, 0);
1175 property_enable(base, &rport->port_cfg->iddig_output, false);
1176 property_enable(base, &rport->port_cfg->iddig_en, false);
1177 break;
1178 default:
1179 break;
1180 }
1181
1182 err1:
1183 mutex_unlock(&rport->mutex);
1184
1185 err0:
1186 return rc;
1187 }
1188 static DEVICE_ATTR_RW(otg_mode);
1189
1190 /* Group all the usb2 phy attributes */
1191 static struct attribute *usb2_phy_attrs[] = {
1192 &dev_attr_otg_mode.attr,
1193 NULL,
1194 };
1195
1196 static struct attribute_group usb2_phy_attr_group = {
1197 .name = NULL, /* we want them in the same directory */
1198 .attrs = usb2_phy_attrs,
1199 };
1200
rockchip_usb2phy_otg_sm_work(struct work_struct * work)1201 static void rockchip_usb2phy_otg_sm_work(struct work_struct *work)
1202 {
1203 struct rockchip_usb2phy_port *rport =
1204 container_of(work, struct rockchip_usb2phy_port,
1205 otg_sm_work.work);
1206 struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
1207 static unsigned int cable;
1208 unsigned long delay;
1209 bool sch_work;
1210
1211 mutex_lock(&rport->mutex);
1212
1213 if (rport->port_cfg->bvalid_grf_con.enable && rport->typec_vbus_det)
1214 rport->vbus_attached =
1215 property_enabled(rphy->grf, &rport->port_cfg->bvalid_grf_con);
1216 else if (rport->utmi_avalid)
1217 rport->vbus_attached =
1218 property_enabled(rphy->grf, &rport->port_cfg->utmi_avalid);
1219 else
1220 rport->vbus_attached =
1221 property_enabled(rphy->grf, &rport->port_cfg->utmi_bvalid);
1222
1223 sch_work = false;
1224 delay = OTG_SCHEDULE_DELAY;
1225
1226 dev_dbg(&rport->phy->dev, "%s otg sm work\n",
1227 usb_otg_state_string(rport->state));
1228
1229 switch (rport->state) {
1230 case OTG_STATE_UNDEFINED:
1231 rport->state = OTG_STATE_B_IDLE;
1232 if (!rport->vbus_attached) {
1233 mutex_unlock(&rport->mutex);
1234 if (!rport->dis_u2_susphy)
1235 rockchip_usb2phy_power_off(rport->phy);
1236 mutex_lock(&rport->mutex);
1237 }
1238 fallthrough;
1239 case OTG_STATE_B_IDLE:
1240 if (extcon_get_state(rphy->edev, EXTCON_USB_HOST) > 0 ||
1241 extcon_get_state(rphy->edev, EXTCON_USB_VBUS_EN) > 0) {
1242 dev_dbg(&rport->phy->dev, "usb otg host connect\n");
1243 rport->state = OTG_STATE_A_HOST;
1244 rphy->chg_state = USB_CHG_STATE_UNDEFINED;
1245 rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
1246 mutex_unlock(&rport->mutex);
1247 rockchip_usb2phy_power_on(rport->phy);
1248 return;
1249 } else if (rport->vbus_attached) {
1250 dev_dbg(&rport->phy->dev, "vbus_attach\n");
1251 switch (rphy->chg_state) {
1252 case USB_CHG_STATE_UNDEFINED:
1253 mutex_unlock(&rport->mutex);
1254 schedule_delayed_work(&rport->chg_work, 0);
1255 return;
1256 case USB_CHG_STATE_DETECTED:
1257 switch (rphy->chg_type) {
1258 case POWER_SUPPLY_TYPE_USB:
1259 dev_dbg(&rport->phy->dev, "sdp cable is connected\n");
1260 wake_lock(&rport->wakelock);
1261 cable = EXTCON_CHG_USB_SDP;
1262 mutex_unlock(&rport->mutex);
1263 rockchip_usb2phy_power_on(rport->phy);
1264 mutex_lock(&rport->mutex);
1265 rport->state = OTG_STATE_B_PERIPHERAL;
1266 rport->perip_connected = true;
1267 sch_work = true;
1268 break;
1269 case POWER_SUPPLY_TYPE_USB_DCP:
1270 dev_dbg(&rport->phy->dev, "dcp cable is connected\n");
1271 cable = EXTCON_CHG_USB_DCP;
1272 sch_work = true;
1273 break;
1274 case POWER_SUPPLY_TYPE_USB_CDP:
1275 dev_dbg(&rport->phy->dev, "cdp cable is connected\n");
1276 wake_lock(&rport->wakelock);
1277 cable = EXTCON_CHG_USB_CDP;
1278 mutex_unlock(&rport->mutex);
1279 rockchip_usb2phy_power_on(rport->phy);
1280 mutex_lock(&rport->mutex);
1281 rport->state = OTG_STATE_B_PERIPHERAL;
1282 rport->perip_connected = true;
1283 sch_work = true;
1284 break;
1285 default:
1286 break;
1287 }
1288 break;
1289 default:
1290 break;
1291 }
1292 } else {
1293 rphy->chg_state = USB_CHG_STATE_UNDEFINED;
1294 rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
1295 rport->perip_connected = false;
1296 mutex_unlock(&rport->mutex);
1297 if (!rport->dis_u2_susphy)
1298 rockchip_usb2phy_power_off(rport->phy);
1299 mutex_lock(&rport->mutex);
1300 }
1301 break;
1302 case OTG_STATE_B_PERIPHERAL:
1303 sch_work = true;
1304
1305 if (extcon_get_state(rphy->edev, EXTCON_USB_HOST) > 0 ||
1306 extcon_get_state(rphy->edev,
1307 EXTCON_USB_VBUS_EN) > 0) {
1308 dev_dbg(&rport->phy->dev, "usb otg host connect\n");
1309 rport->state = OTG_STATE_A_HOST;
1310 rphy->chg_state = USB_CHG_STATE_UNDEFINED;
1311 rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
1312 rport->perip_connected = false;
1313 sch_work = false;
1314 wake_unlock(&rport->wakelock);
1315 } else if (!rport->vbus_attached) {
1316 dev_dbg(&rport->phy->dev, "usb disconnect\n");
1317 rport->state = OTG_STATE_B_IDLE;
1318 rport->perip_connected = false;
1319 rphy->chg_state = USB_CHG_STATE_UNDEFINED;
1320 rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
1321 delay = OTG_SCHEDULE_DELAY;
1322 wake_unlock(&rport->wakelock);
1323 }
1324 break;
1325 case OTG_STATE_A_HOST:
1326 if (extcon_get_state(rphy->edev, EXTCON_USB_HOST) == 0) {
1327 dev_dbg(&rport->phy->dev, "usb otg host disconnect\n");
1328 rport->state = OTG_STATE_B_IDLE;
1329 sch_work = true;
1330 } else {
1331 mutex_unlock(&rport->mutex);
1332 return;
1333 }
1334 break;
1335 default:
1336 mutex_unlock(&rport->mutex);
1337 return;
1338 }
1339
1340 if (extcon_get_state(rphy->edev, cable) != rport->vbus_attached) {
1341 extcon_set_state_sync(rphy->edev,
1342 cable, rport->vbus_attached);
1343
1344 if (!rport->vbus_attached)
1345 cable = EXTCON_NONE;
1346 } else if (rport->state == OTG_STATE_A_HOST &&
1347 extcon_get_state(rphy->edev, cable)) {
1348 /*
1349 * If plug in OTG host cable when the rport state is
1350 * OTG_STATE_B_PERIPHERAL, the vbus voltage will stay
1351 * in high, so the rport->vbus_attached may not be
1352 * changed. We need to set cable state here.
1353 */
1354 extcon_set_state_sync(rphy->edev, cable, false);
1355 cable = EXTCON_NONE;
1356 }
1357
1358 if (rphy->edev_self &&
1359 (extcon_get_state(rphy->edev, EXTCON_USB) !=
1360 rport->perip_connected)) {
1361 extcon_set_state_sync(rphy->edev,
1362 EXTCON_USB,
1363 rport->perip_connected);
1364 extcon_sync(rphy->edev, EXTCON_USB_HOST);
1365 }
1366 if (sch_work)
1367 schedule_delayed_work(&rport->otg_sm_work, delay);
1368
1369 mutex_unlock(&rport->mutex);
1370 }
1371
chg_to_string(enum power_supply_type chg_type)1372 static const char *chg_to_string(enum power_supply_type chg_type)
1373 {
1374 switch (chg_type) {
1375 case POWER_SUPPLY_TYPE_USB:
1376 return "USB_SDP_CHARGER";
1377 case POWER_SUPPLY_TYPE_USB_DCP:
1378 return "USB_DCP_CHARGER";
1379 case POWER_SUPPLY_TYPE_USB_CDP:
1380 return "USB_CDP_CHARGER";
1381 default:
1382 return "INVALID_CHARGER";
1383 }
1384 }
1385
rockchip_chg_enable_dcd(struct rockchip_usb2phy * rphy,bool en)1386 static void rockchip_chg_enable_dcd(struct rockchip_usb2phy *rphy,
1387 bool en)
1388 {
1389 struct regmap *base = get_reg_base(rphy);
1390
1391 property_enable(base, &rphy->phy_cfg->chg_det.rdm_pdwn_en, en);
1392 property_enable(base, &rphy->phy_cfg->chg_det.idp_src_en, en);
1393 }
1394
rockchip_chg_enable_primary_det(struct rockchip_usb2phy * rphy,bool en)1395 static void rockchip_chg_enable_primary_det(struct rockchip_usb2phy *rphy,
1396 bool en)
1397 {
1398 struct regmap *base = get_reg_base(rphy);
1399
1400 property_enable(base, &rphy->phy_cfg->chg_det.vdp_src_en, en);
1401 property_enable(base, &rphy->phy_cfg->chg_det.idm_sink_en, en);
1402 }
1403
rockchip_chg_enable_secondary_det(struct rockchip_usb2phy * rphy,bool en)1404 static void rockchip_chg_enable_secondary_det(struct rockchip_usb2phy *rphy,
1405 bool en)
1406 {
1407 struct regmap *base = get_reg_base(rphy);
1408
1409 property_enable(base, &rphy->phy_cfg->chg_det.vdm_src_en, en);
1410 property_enable(base, &rphy->phy_cfg->chg_det.idp_sink_en, en);
1411 }
1412
1413 #define CHG_DCD_POLL_TIME (100 * HZ / 1000)
1414 #define CHG_DCD_MAX_RETRIES 6
1415 #define CHG_PRIMARY_DET_TIME (40 * HZ / 1000)
1416 #define CHG_SECONDARY_DET_TIME (40 * HZ / 1000)
rockchip_chg_detect_work(struct work_struct * work)1417 static void rockchip_chg_detect_work(struct work_struct *work)
1418 {
1419 struct rockchip_usb2phy_port *rport =
1420 container_of(work, struct rockchip_usb2phy_port, chg_work.work);
1421 struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
1422 struct regmap *base = get_reg_base(rphy);
1423 const struct usb2phy_reg *phy_sus_reg;
1424 bool is_dcd, tmout, vout;
1425 unsigned long delay;
1426 unsigned int mask;
1427 int ret;
1428
1429 dev_dbg(&rport->phy->dev, "chg detection work state = %d\n",
1430 rphy->chg_state);
1431
1432 /*
1433 * The conditions for charger detection:
1434 * 1. Set the PHY in normal mode to keep the UTMI_CLK on.
1435 * 2. Set the utmi_opmode in non-driving mode.
1436 * 3. Set the utmi_xcvrselect to FS speed.
1437 * 4. Set the utmi_termselect to FS speed.
1438 * 5. Enable the DP/DM pulldown resistor.
1439 */
1440 switch (rphy->chg_state) {
1441 case USB_CHG_STATE_UNDEFINED:
1442 mutex_lock(&rport->mutex);
1443 /* Store the PHY current suspend configuration */
1444 phy_sus_reg = &rport->port_cfg->phy_sus;
1445 ret = regmap_read(base, phy_sus_reg->offset,
1446 &rphy->phy_sus_cfg);
1447 if (ret) {
1448 dev_err(&rport->phy->dev,
1449 "Fail to read phy_sus reg offset 0x%x, ret %d\n",
1450 phy_sus_reg->offset, ret);
1451 mutex_unlock(&rport->mutex);
1452 return;
1453 }
1454
1455 /* Set the PHY in charger detection mode */
1456 property_enable(base, &rphy->phy_cfg->chg_det.chg_mode, true);
1457 /* Start DCD processing stage 1 */
1458 rockchip_chg_enable_dcd(rphy, true);
1459 rphy->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1460 rphy->dcd_retries = 0;
1461 rphy->primary_retries = 0;
1462 delay = CHG_DCD_POLL_TIME;
1463 break;
1464 case USB_CHG_STATE_WAIT_FOR_DCD:
1465 /* get data contact detection status */
1466 is_dcd = property_enabled(rphy->grf,
1467 &rphy->phy_cfg->chg_det.dp_det);
1468 tmout = ++rphy->dcd_retries == CHG_DCD_MAX_RETRIES;
1469 /* stage 2 */
1470 if (is_dcd || tmout) {
1471 /* stage 4 */
1472 /* Turn off DCD circuitry */
1473 rockchip_chg_enable_dcd(rphy, false);
1474 /* Voltage Source on DP, Probe on DM */
1475 rockchip_chg_enable_primary_det(rphy, true);
1476 delay = CHG_PRIMARY_DET_TIME;
1477 rphy->chg_state = USB_CHG_STATE_DCD_DONE;
1478 } else {
1479 /* stage 3 */
1480 delay = CHG_DCD_POLL_TIME;
1481 }
1482 break;
1483 case USB_CHG_STATE_DCD_DONE:
1484 vout = property_enabled(rphy->grf,
1485 &rphy->phy_cfg->chg_det.cp_det);
1486 rockchip_chg_enable_primary_det(rphy, false);
1487 if (vout) {
1488 /* Voltage Source on DM, Probe on DP */
1489 rockchip_chg_enable_secondary_det(rphy, true);
1490 delay = CHG_SECONDARY_DET_TIME;
1491 rphy->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1492 } else {
1493 if (rphy->dcd_retries == CHG_DCD_MAX_RETRIES) {
1494 /* floating charger found */
1495 rphy->chg_type = POWER_SUPPLY_TYPE_USB_DCP;
1496 rphy->chg_state = USB_CHG_STATE_DETECTED;
1497 delay = 0;
1498 } else {
1499 if (rphy->primary_retries < 2) {
1500 /* Turn off DCD circuitry */
1501 rockchip_chg_enable_dcd(rphy, false);
1502 /* Voltage Source on DP, Probe on DM */
1503 rockchip_chg_enable_primary_det(rphy,
1504 true);
1505 delay = CHG_PRIMARY_DET_TIME;
1506 rphy->chg_state =
1507 USB_CHG_STATE_DCD_DONE;
1508 rphy->primary_retries++;
1509 /* break USB_CHG_STATE_DCD_DONE */
1510 break;
1511 }
1512 rphy->chg_type = POWER_SUPPLY_TYPE_USB;
1513 rphy->chg_state = USB_CHG_STATE_DETECTED;
1514 delay = 0;
1515 }
1516 }
1517 break;
1518 case USB_CHG_STATE_PRIMARY_DONE:
1519 vout = property_enabled(rphy->grf,
1520 &rphy->phy_cfg->chg_det.dcp_det);
1521 /* Turn off voltage source */
1522 rockchip_chg_enable_secondary_det(rphy, false);
1523 if (vout)
1524 rphy->chg_type = POWER_SUPPLY_TYPE_USB_DCP;
1525 else
1526 rphy->chg_type = POWER_SUPPLY_TYPE_USB_CDP;
1527 fallthrough;
1528 case USB_CHG_STATE_SECONDARY_DONE:
1529 rphy->chg_state = USB_CHG_STATE_DETECTED;
1530 fallthrough;
1531 case USB_CHG_STATE_DETECTED:
1532 if (rphy->phy_cfg->chg_det.chg_mode.offset !=
1533 rport->port_cfg->phy_sus.offset)
1534 property_enable(base, &rphy->phy_cfg->chg_det.chg_mode, false);
1535
1536 /* Restore the PHY suspend configuration */
1537 phy_sus_reg = &rport->port_cfg->phy_sus;
1538 mask = GENMASK(phy_sus_reg->bitend, phy_sus_reg->bitstart);
1539 ret = regmap_write(base, phy_sus_reg->offset,
1540 (rphy->phy_sus_cfg | (mask << BIT_WRITEABLE_SHIFT)));
1541 if (ret)
1542 dev_err(&rport->phy->dev,
1543 "Fail to set phy_sus reg offset 0x%x, ret %d\n",
1544 phy_sus_reg->offset, ret);
1545 mutex_unlock(&rport->mutex);
1546 rockchip_usb2phy_otg_sm_work(&rport->otg_sm_work.work);
1547 dev_dbg(&rport->phy->dev, "charger = %s\n",
1548 chg_to_string(rphy->chg_type));
1549 return;
1550 default:
1551 mutex_unlock(&rport->mutex);
1552 return;
1553 }
1554
1555 /*
1556 * Hold the mutex lock during the whole charger
1557 * detection stage, and release it after detect
1558 * the charger type.
1559 */
1560 schedule_delayed_work(&rport->chg_work, delay);
1561 }
1562
1563 /*
1564 * The function manage host-phy port state and suspend/resume phy port
1565 * to save power.
1566 *
1567 * we rely on utmi_linestate and utmi_hostdisconnect to identify whether
1568 * devices is disconnect or not. Besides, we do not need care it is FS/LS
1569 * disconnected or HS disconnected, actually, we just only need get the
1570 * device is disconnected at last through rearm the delayed work,
1571 * to suspend the phy port in _PHY_STATE_DISCONNECT_ case.
1572 *
1573 * NOTE: It may invoke *phy_powr_off or *phy_power_on which will invoke
1574 * some clk related APIs, so do not invoke it from interrupt context directly.
1575 */
rockchip_usb2phy_sm_work(struct work_struct * work)1576 static void rockchip_usb2phy_sm_work(struct work_struct *work)
1577 {
1578 struct rockchip_usb2phy_port *rport =
1579 container_of(work, struct rockchip_usb2phy_port, sm_work.work);
1580 struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
1581 unsigned int sh, ul, uhd, state;
1582 unsigned int ul_mask, uhd_mask;
1583 int ret;
1584
1585 if (!rport->port_cfg->utmi_ls.offset ||
1586 (!rport->port_cfg->utmi_hstdet.offset &&
1587 !rport->port_cfg->disfall_en.offset)) {
1588 dev_dbg(&rport->phy->dev, "some property may not be specified\n");
1589 return;
1590 }
1591
1592 mutex_lock(&rport->mutex);
1593
1594 ret = regmap_read(rphy->grf, rport->port_cfg->utmi_ls.offset, &ul);
1595 if (ret < 0)
1596 goto next_schedule;
1597
1598 ul_mask = GENMASK(rport->port_cfg->utmi_ls.bitend,
1599 rport->port_cfg->utmi_ls.bitstart);
1600
1601 if (rport->port_cfg->utmi_hstdet.offset) {
1602 ret = regmap_read(rphy->grf, rport->port_cfg->utmi_hstdet.offset, &uhd);
1603 if (ret < 0)
1604 goto next_schedule;
1605
1606 uhd_mask = GENMASK(rport->port_cfg->utmi_hstdet.bitend,
1607 rport->port_cfg->utmi_hstdet.bitstart);
1608
1609 sh = rport->port_cfg->utmi_hstdet.bitend -
1610 rport->port_cfg->utmi_hstdet.bitstart + 1;
1611 /* stitch on utmi_ls and utmi_hstdet as phy state */
1612 state = ((uhd & uhd_mask) >> rport->port_cfg->utmi_hstdet.bitstart) |
1613 (((ul & ul_mask) >> rport->port_cfg->utmi_ls.bitstart) << sh);
1614 } else {
1615 state = ((ul & ul_mask) >> rport->port_cfg->utmi_ls.bitstart) << 1 |
1616 rport->host_disconnect;
1617 }
1618
1619 switch (state) {
1620 case PHY_STATE_HS_ONLINE:
1621 dev_dbg(&rport->phy->dev, "HS online\n");
1622 break;
1623 case PHY_STATE_FS_LS_ONLINE:
1624 /*
1625 * For FS/LS device, the online state share with connect state
1626 * from utmi_ls and utmi_hstdet register, so we distinguish
1627 * them via suspended flag.
1628 *
1629 * Plus, there are two cases, one is D- Line pull-up, and D+
1630 * line pull-down, the state is 4; another is D+ line pull-up,
1631 * and D- line pull-down, the state is 2.
1632 */
1633 if (!rport->suspended) {
1634 /* D- line pull-up, D+ line pull-down */
1635 dev_dbg(&rport->phy->dev, "FS/LS online\n");
1636 break;
1637 }
1638 fallthrough;
1639 case PHY_STATE_CONNECT:
1640 if (rport->suspended) {
1641 dev_dbg(&rport->phy->dev, "Connected\n");
1642 mutex_unlock(&rport->mutex);
1643 rockchip_usb2phy_power_on(rport->phy);
1644 mutex_lock(&rport->mutex);
1645 rport->suspended = false;
1646 } else {
1647 /* D+ line pull-up, D- line pull-down */
1648 dev_dbg(&rport->phy->dev, "FS/LS online\n");
1649 }
1650 break;
1651 case PHY_STATE_SE1:
1652 if (rport->suspended) {
1653 dev_dbg(&rport->phy->dev, "linestate is SE1, power on phy\n");
1654 mutex_unlock(&rport->mutex);
1655 rockchip_usb2phy_power_on(rport->phy);
1656 mutex_lock(&rport->mutex);
1657 rport->suspended = false;
1658 }
1659 break;
1660 case PHY_STATE_DISCONNECT:
1661 if (!rport->suspended) {
1662 dev_dbg(&rport->phy->dev, "Disconnected\n");
1663 mutex_unlock(&rport->mutex);
1664 rockchip_usb2phy_power_off(rport->phy);
1665 mutex_lock(&rport->mutex);
1666 rport->suspended = true;
1667 }
1668
1669 /*
1670 * activate the linestate detection to get the next device
1671 * plug-in irq.
1672 */
1673 rockchip_usb2phy_enable_line_irq(rphy, rport, true);
1674
1675 /*
1676 * we don't need to rearm the delayed work when the phy port
1677 * is suspended.
1678 */
1679 mutex_unlock(&rport->mutex);
1680 return;
1681 default:
1682 dev_dbg(&rport->phy->dev, "unknown phy state %d\n", state);
1683 break;
1684 }
1685
1686 next_schedule:
1687 mutex_unlock(&rport->mutex);
1688 schedule_delayed_work(&rport->sm_work, SCHEDULE_DELAY);
1689 }
1690
rockchip_usb2phy_linestate_irq(int irq,void * data)1691 static irqreturn_t rockchip_usb2phy_linestate_irq(int irq, void *data)
1692 {
1693 struct rockchip_usb2phy_port *rport = data;
1694 struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
1695
1696 if (!property_enabled(rphy->grf, &rport->port_cfg->ls_det_st) ||
1697 !property_enabled(rphy->grf, &rport->port_cfg->ls_det_en))
1698 return IRQ_NONE;
1699
1700 dev_dbg(&rport->phy->dev, "linestate interrupt\n");
1701
1702 mutex_lock(&rport->mutex);
1703
1704 /* disable linestate detect irq and clear its status */
1705 rockchip_usb2phy_enable_line_irq(rphy, rport, false);
1706
1707 /*
1708 * For host port, it may miss disc irq when device is connected,
1709 * in this case, we can clear host_disconnect state depend on
1710 * the linestate irq.
1711 */
1712 if (rport->port_id == USB2PHY_PORT_HOST && rport->port_cfg->disfall_en.offset)
1713 rport->host_disconnect = false;
1714
1715 mutex_unlock(&rport->mutex);
1716
1717 /*
1718 * In this case for host phy port, a new device is plugged in,
1719 * meanwhile, if the phy port is suspended, we need rearm the work to
1720 * resume it and mange its states; otherwise, we do nothing about that.
1721 */
1722 if (rport->suspended && rport->port_id == USB2PHY_PORT_HOST)
1723 rockchip_usb2phy_sm_work(&rport->sm_work.work);
1724
1725 return IRQ_HANDLED;
1726 }
1727
rockchip_usb2phy_bvalid_irq(int irq,void * data)1728 static irqreturn_t rockchip_usb2phy_bvalid_irq(int irq, void *data)
1729 {
1730 struct rockchip_usb2phy_port *rport = data;
1731 struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
1732
1733 if (!property_enabled(rphy->grf, &rport->port_cfg->bvalid_det_st))
1734 return IRQ_NONE;
1735
1736 mutex_lock(&rport->mutex);
1737
1738 /* clear bvalid detect irq pending status */
1739 property_enable(rphy->grf, &rport->port_cfg->bvalid_det_clr, true);
1740
1741 mutex_unlock(&rport->mutex);
1742
1743 if (rport->bypass_uart_en)
1744 rockchip_usb_bypass_uart(rport, false);
1745
1746 if (rport->otg_sm_work.work.func) {
1747 cancel_delayed_work_sync(&rport->otg_sm_work);
1748 rockchip_usb2phy_otg_sm_work(&rport->otg_sm_work.work);
1749 }
1750
1751 return IRQ_HANDLED;
1752 }
1753
rockchip_usb2phy_id_irq(int irq,void * data)1754 static irqreturn_t rockchip_usb2phy_id_irq(int irq, void *data)
1755 {
1756 struct rockchip_usb2phy_port *rport = data;
1757 struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
1758 bool cable_vbus_state = false;
1759
1760 if (!property_enabled(rphy->grf, &rport->port_cfg->idfall_det_st) &&
1761 !property_enabled(rphy->grf, &rport->port_cfg->idrise_det_st))
1762 return IRQ_NONE;
1763
1764 mutex_lock(&rport->mutex);
1765
1766 /* clear id fall or rise detect irq pending status */
1767 if (property_enabled(rphy->grf, &rport->port_cfg->idfall_det_st)) {
1768 property_enable(rphy->grf, &rport->port_cfg->idfall_det_clr,
1769 true);
1770 /* switch to host if id fall det and iddig status is low */
1771 if (!property_enabled(rphy->grf, &rport->port_cfg->utmi_iddig))
1772 cable_vbus_state = true;
1773 } else if (property_enabled(rphy->grf, &rport->port_cfg->idrise_det_st)) {
1774 property_enable(rphy->grf, &rport->port_cfg->idrise_det_clr,
1775 true);
1776 cable_vbus_state = false;
1777 }
1778
1779 extcon_set_state(rphy->edev, EXTCON_USB_HOST, cable_vbus_state);
1780 extcon_set_state(rphy->edev, EXTCON_USB_VBUS_EN, cable_vbus_state);
1781
1782 extcon_sync(rphy->edev, EXTCON_USB_HOST);
1783 extcon_sync(rphy->edev, EXTCON_USB_VBUS_EN);
1784
1785 rockchip_set_vbus_power(rport, cable_vbus_state);
1786
1787 mutex_unlock(&rport->mutex);
1788
1789 return IRQ_HANDLED;
1790 }
1791
rockchip_usb2phy_host_disc_irq(int irq,void * data)1792 static irqreturn_t rockchip_usb2phy_host_disc_irq(int irq, void *data)
1793 {
1794 struct rockchip_usb2phy_port *rport = data;
1795 struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
1796
1797 if (!property_enabled(rphy->grf, &rport->port_cfg->disfall_st) &&
1798 !property_enabled(rphy->grf, &rport->port_cfg->disrise_st))
1799 return IRQ_NONE;
1800
1801 mutex_lock(&rport->mutex);
1802
1803 /* clear disconnect fall or rise detect irq pending status */
1804 if (property_enabled(rphy->grf, &rport->port_cfg->disfall_st)) {
1805 property_enable(rphy->grf, &rport->port_cfg->disfall_clr,
1806 true);
1807 rport->host_disconnect = false;
1808 } else if (property_enabled(rphy->grf, &rport->port_cfg->disrise_st)) {
1809 property_enable(rphy->grf, &rport->port_cfg->disrise_clr,
1810 true);
1811 rport->host_disconnect = true;
1812 }
1813
1814 mutex_unlock(&rport->mutex);
1815
1816 return IRQ_HANDLED;
1817 }
1818
rockchip_usb2phy_otg_mux_irq(int irq,void * data)1819 static irqreturn_t rockchip_usb2phy_otg_mux_irq(int irq, void *data)
1820 {
1821 irqreturn_t ret = IRQ_NONE;
1822
1823 ret = rockchip_usb2phy_id_irq(irq, data);
1824 ret |= rockchip_usb2phy_bvalid_irq(irq, data);
1825 ret |= rockchip_usb2phy_linestate_irq(irq, data);
1826
1827 return ret;
1828 }
1829
rockchip_usb2phy_irq(int irq,void * data)1830 static irqreturn_t rockchip_usb2phy_irq(int irq, void *data)
1831 {
1832 struct rockchip_usb2phy *rphy = data;
1833 struct rockchip_usb2phy_port *rport;
1834 irqreturn_t ret = IRQ_NONE;
1835 unsigned int index;
1836 bool force_mode;
1837
1838 for (index = 0; index < rphy->phy_cfg->num_ports; index++) {
1839 rport = &rphy->ports[index];
1840 if (!rport->phy)
1841 continue;
1842
1843 /*
1844 * Handle disc irq before linestate irq to set the disc
1845 * state for sm work scheduled in the linestate irq handler.
1846 */
1847 if (rport->port_id == USB2PHY_PORT_HOST &&
1848 rport->port_cfg->disfall_en.offset)
1849 ret |= rockchip_usb2phy_host_disc_irq(irq, rport);
1850
1851 /* Handle linestate irq for both otg port and host port */
1852 ret |= rockchip_usb2phy_linestate_irq(irq, rport);
1853
1854 /*
1855 * Handle bvalid irq and id irq for otg port which
1856 * is assigned to otg controller.
1857 */
1858 if (rport->port_id == USB2PHY_PORT_OTG &&
1859 rport->mode != USB_DR_MODE_UNKNOWN) {
1860 if (rport->mode == USB_DR_MODE_HOST) {
1861 /*
1862 * If otg port work as usb host mode and
1863 * force_mode is true, it means that the
1864 * otg port is forced to host mode by the
1865 * grf plug iddig indicator via the sys
1866 * interface "otg_mode". We need to handle
1867 * the bvalid irq and id irq in this case.
1868 */
1869 force_mode = property_enabled(rphy->grf,
1870 &rport->port_cfg->iddig_en);
1871 if (!force_mode)
1872 continue;
1873 }
1874
1875 if (!rport->vbus_always_on)
1876 ret |= rockchip_usb2phy_bvalid_irq(irq, rport);
1877
1878 ret |= rockchip_usb2phy_id_irq(irq, rport);
1879 }
1880 }
1881
1882 return ret;
1883 }
1884
rockchip_usb2phy_port_irq_init(struct rockchip_usb2phy * rphy,struct rockchip_usb2phy_port * rport,struct device_node * child_np)1885 static int rockchip_usb2phy_port_irq_init(struct rockchip_usb2phy *rphy,
1886 struct rockchip_usb2phy_port *rport,
1887 struct device_node *child_np)
1888 {
1889 int ret;
1890
1891 /*
1892 * If the usb2 phy used combined irq for otg and host port,
1893 * don't need to init otg and host port irq separately.
1894 */
1895 if (rphy->irq > 0)
1896 return 0;
1897
1898 /*
1899 * Some SoCs (e.g. RV1108) use one combined irq for all of
1900 * the irqs of otg port. So probe the otg-mux interrupt first,
1901 * if not found, then init the regular irqs one by one.
1902 */
1903 rport->otg_mux_irq = of_irq_get_byname(child_np, "otg-mux");
1904 if (rport->otg_mux_irq > 0) {
1905 ret = devm_request_threaded_irq(rphy->dev, rport->otg_mux_irq,
1906 NULL,
1907 rockchip_usb2phy_otg_mux_irq,
1908 IRQF_ONESHOT,
1909 "rockchip_usb2phy_otg",
1910 rport);
1911 if (ret)
1912 dev_err(rphy->dev,
1913 "failed to request otg-mux irq handle\n");
1914
1915 return ret;
1916 }
1917
1918 /* Init linestate irq for both otg port and host port */
1919 rport->ls_irq = of_irq_get_byname(child_np, "linestate");
1920 if (rport->ls_irq <= 0) {
1921 dev_err(rphy->dev, "no linestate irq provided\n");
1922 return -EINVAL;
1923 }
1924
1925 ret = devm_request_threaded_irq(rphy->dev, rport->ls_irq, NULL,
1926 rockchip_usb2phy_linestate_irq,
1927 IRQF_ONESHOT,
1928 "rockchip_usb2phy_ls", rport);
1929 if (ret) {
1930 dev_err(rphy->dev, "failed to request linestate irq handle\n");
1931 return ret;
1932 }
1933
1934 /*
1935 * If it's host port or it's otg port but only support
1936 * host mode, return immediately without init the bvalid
1937 * and id irqs/
1938 */
1939 if (rport->port_id == USB2PHY_PORT_HOST ||
1940 rport->mode == USB_DR_MODE_HOST ||
1941 rport->mode == USB_DR_MODE_UNKNOWN)
1942 return ret;
1943
1944 /* Init the bvalid irq for otg port */
1945 if (!rport->vbus_always_on) {
1946 rport->bvalid_irq = of_irq_get_byname(child_np,
1947 "otg-bvalid");
1948 if (rport->bvalid_irq <= 0) {
1949 dev_err(rphy->dev, "no bvalid irq provided\n");
1950 return -EINVAL;
1951 }
1952
1953 ret = devm_request_threaded_irq(rphy->dev,
1954 rport->bvalid_irq,
1955 NULL,
1956 rockchip_usb2phy_bvalid_irq,
1957 IRQF_ONESHOT,
1958 "rockchip_usb2phy_bvalid",
1959 rport);
1960 if (ret) {
1961 dev_err(rphy->dev,
1962 "failed to request otg-bvalid irq handle\n");
1963 return ret;
1964 }
1965 }
1966
1967 /* Init the id irq for otg port */
1968 if (rphy->edev_self) {
1969 rport->id_irq = of_irq_get_byname(child_np, "otg-id");
1970 if (rport->id_irq <= 0) {
1971 dev_err(rphy->dev, "no otg id irq provided\n");
1972 return -EINVAL;
1973 }
1974
1975 ret = devm_request_threaded_irq(rphy->dev,
1976 rport->id_irq, NULL,
1977 rockchip_usb2phy_id_irq,
1978 IRQF_ONESHOT,
1979 "rockchip_usb2phy_id",
1980 rport);
1981 if (ret) {
1982 dev_err(rphy->dev,
1983 "failed to request otg-id irq handle\n");
1984 return ret;
1985 }
1986 }
1987
1988 return ret;
1989 }
1990
rockchip_usb2phy_usb_bvalid_enable(struct rockchip_usb2phy_port * rport,u8 enable)1991 static void rockchip_usb2phy_usb_bvalid_enable(struct rockchip_usb2phy_port *rport,
1992 u8 enable)
1993 {
1994 struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
1995 const struct rockchip_usb2phy_port_cfg *cfg = rport->port_cfg;
1996
1997 if (cfg->bvalid_phy_con.enable)
1998 property_enable(rphy->grf, &cfg->bvalid_phy_con, enable);
1999
2000 if (cfg->bvalid_grf_con.enable)
2001 property_enable(rphy->grf, &cfg->bvalid_grf_con, enable);
2002 }
2003
rockchip_usb2phy_orien_sw_set(struct typec_switch * sw,enum typec_orientation orien)2004 static int rockchip_usb2phy_orien_sw_set(struct typec_switch *sw,
2005 enum typec_orientation orien)
2006 {
2007 struct rockchip_usb2phy_port *rport = typec_switch_get_drvdata(sw);
2008
2009 dev_dbg(&rport->phy->dev, "type-c orientation: %d\n", orien);
2010
2011 mutex_lock(&rport->mutex);
2012 rockchip_usb2phy_usb_bvalid_enable(rport, orien != TYPEC_ORIENTATION_NONE);
2013 mutex_unlock(&rport->mutex);
2014
2015 return 0;
2016 }
2017
2018 static int
rockchip_usb2phy_setup_orien_switch(struct rockchip_usb2phy * rphy,struct rockchip_usb2phy_port * rport)2019 rockchip_usb2phy_setup_orien_switch(struct rockchip_usb2phy *rphy,
2020 struct rockchip_usb2phy_port *rport)
2021 {
2022 struct typec_switch_desc sw_desc = { };
2023 struct device *dev = rphy->dev;
2024
2025 sw_desc.drvdata = rport;
2026 sw_desc.fwnode = dev_fwnode(dev);
2027 sw_desc.set = rockchip_usb2phy_orien_sw_set;
2028
2029 rport->sw = typec_switch_register(dev, &sw_desc);
2030 if (IS_ERR(rport->sw)) {
2031 dev_err(dev, "Error register typec orientation switch: %ld\n",
2032 PTR_ERR(rport->sw));
2033 return PTR_ERR(rport->sw);
2034 }
2035
2036 return 0;
2037 }
2038
rockchip_usb2phy_orien_switch_unregister(void * data)2039 static void rockchip_usb2phy_orien_switch_unregister(void *data)
2040 {
2041 struct rockchip_usb2phy_port *rport = data;
2042
2043 typec_switch_unregister(rport->sw);
2044 }
2045
rockchip_usb2phy_host_port_init(struct rockchip_usb2phy * rphy,struct rockchip_usb2phy_port * rport,struct device_node * child_np)2046 static int rockchip_usb2phy_host_port_init(struct rockchip_usb2phy *rphy,
2047 struct rockchip_usb2phy_port *rport,
2048 struct device_node *child_np)
2049 {
2050 int ret;
2051 struct regmap *base = get_reg_base(rphy);
2052
2053 rport->port_id = USB2PHY_PORT_HOST;
2054 rport->port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_HOST];
2055
2056 /* enter lower power state when suspend */
2057 rport->low_power_en =
2058 of_property_read_bool(child_np, "rockchip,low-power-mode");
2059
2060 mutex_init(&rport->mutex);
2061 INIT_DELAYED_WORK(&rport->sm_work, rockchip_usb2phy_sm_work);
2062
2063 ret = rockchip_usb2phy_port_irq_init(rphy, rport, child_np);
2064 if (ret) {
2065 dev_err(rphy->dev, "failed to init irq for host port\n");
2066 return ret;
2067 }
2068
2069 /*
2070 * Let us put phy-port into suspend mode here for saving power
2071 * consumption, and usb controller will resume it during probe
2072 * time if needed.
2073 */
2074 ret = property_enable(base, &rport->port_cfg->phy_sus, true);
2075 if (ret)
2076 return ret;
2077 rport->suspended = true;
2078
2079 return 0;
2080 }
2081
rockchip_otg_event(struct notifier_block * nb,unsigned long event,void * ptr)2082 static int rockchip_otg_event(struct notifier_block *nb,
2083 unsigned long event, void *ptr)
2084 {
2085 struct rockchip_usb2phy_port *rport =
2086 container_of(nb, struct rockchip_usb2phy_port, event_nb);
2087
2088 schedule_delayed_work(&rport->otg_sm_work, OTG_SCHEDULE_DELAY);
2089
2090 return NOTIFY_DONE;
2091 }
2092
rockchip_otg_wake_lock_destroy(void * data)2093 static void rockchip_otg_wake_lock_destroy(void *data)
2094 {
2095 wake_lock_destroy((struct wake_lock *)(data));
2096 }
2097
rockchip_usb2phy_otg_port_init(struct rockchip_usb2phy * rphy,struct rockchip_usb2phy_port * rport,struct device_node * child_np)2098 static int rockchip_usb2phy_otg_port_init(struct rockchip_usb2phy *rphy,
2099 struct rockchip_usb2phy_port *rport,
2100 struct device_node *child_np)
2101 {
2102 int ret;
2103 int iddig;
2104 struct regmap *base = get_reg_base(rphy);
2105
2106 rport->port_id = USB2PHY_PORT_OTG;
2107 rport->port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_OTG];
2108 rport->state = OTG_STATE_UNDEFINED;
2109 rport->vbus_attached = false;
2110 rport->vbus_enabled = false;
2111 rport->perip_connected = false;
2112 rport->prev_iddig = true;
2113
2114 mutex_init(&rport->mutex);
2115
2116 /* bypass uart function is only used in debug stage. */
2117 rport->bypass_uart_en =
2118 of_property_read_bool(child_np, "rockchip,bypass-uart");
2119 rport->vbus_always_on =
2120 of_property_read_bool(child_np, "rockchip,vbus-always-on");
2121 rport->utmi_avalid =
2122 of_property_read_bool(child_np, "rockchip,utmi-avalid");
2123 rport->dis_u2_susphy =
2124 of_property_read_bool(child_np, "rockchip,dis-u2-susphy");
2125
2126 /* enter lower power state when suspend */
2127 rport->low_power_en =
2128 of_property_read_bool(child_np, "rockchip,low-power-mode");
2129
2130 /* For type-c with vbus_det always pull up */
2131 rport->typec_vbus_det =
2132 of_property_read_bool(child_np, "rockchip,typec-vbus-det");
2133
2134 rport->sel_pipe_phystatus =
2135 of_property_read_bool(child_np, "rockchip,sel-pipe-phystatus");
2136
2137 if (rport->sel_pipe_phystatus) {
2138 rphy->usbctrl_grf =
2139 syscon_regmap_lookup_by_phandle(rphy->dev->of_node,
2140 "rockchip,usbctrl-grf");
2141 if (IS_ERR(rphy->usbctrl_grf)) {
2142 dev_err(rphy->dev, "Failed to map usbctrl-grf\n");
2143 return PTR_ERR(rphy->usbctrl_grf);
2144 }
2145 }
2146
2147 /* Get Vbus regulators */
2148 rport->vbus = devm_regulator_get_optional(&rport->phy->dev, "vbus");
2149 if (IS_ERR(rport->vbus)) {
2150 ret = PTR_ERR(rport->vbus);
2151 if (ret == -EPROBE_DEFER)
2152 return ret;
2153
2154 if (rport->mode == USB_DR_MODE_OTG)
2155 dev_warn(&rport->phy->dev, "No vbus specified for otg port\n");
2156 rport->vbus = NULL;
2157 }
2158
2159 rport->mode = of_usb_get_dr_mode_by_phy(child_np, -1);
2160 iddig = property_enabled(rphy->grf, &rport->port_cfg->utmi_iddig);
2161 if (rphy->edev_self && (rport->mode == USB_DR_MODE_HOST ||
2162 rport->mode == USB_DR_MODE_UNKNOWN || !iddig)) {
2163 /* Enable VBUS supply for otg port */
2164 extcon_set_state(rphy->edev, EXTCON_USB, false);
2165 extcon_set_state(rphy->edev, EXTCON_USB_HOST, true);
2166 extcon_set_state(rphy->edev, EXTCON_USB_VBUS_EN, true);
2167 ret = rockchip_set_vbus_power(rport, true);
2168 if (ret)
2169 return ret;
2170 }
2171
2172 ret = rockchip_usb2phy_port_irq_init(rphy, rport, child_np);
2173 if (ret) {
2174 dev_err(rphy->dev, "failed to init irq for otg port\n");
2175 return ret;
2176 }
2177
2178 if (IS_REACHABLE(CONFIG_TYPEC) &&
2179 device_property_present(rphy->dev, "orientation-switch")) {
2180 ret = rockchip_usb2phy_setup_orien_switch(rphy, rport);
2181 if (ret)
2182 return ret;
2183
2184 ret = devm_add_action_or_reset(rphy->dev,
2185 rockchip_usb2phy_orien_switch_unregister,
2186 rport);
2187 if (ret)
2188 return ret;
2189 }
2190
2191 /*
2192 * Set the utmi bvalid come from the usb phy or grf.
2193 * For most of Rockchip SoCs, them have VBUSDET pin
2194 * for the usb phy to detect the USB VBUS and set
2195 * the bvalid signal, so select the bvalid from the
2196 * usb phy by default. And for those SoCs which don't
2197 * have VBUSDET pin (e.g. RV1103), it needs to select
2198 * the bvaid from the grf and set bvalid to be valid
2199 * (high) by default.
2200 */
2201 if (rport->port_cfg->bvalid_grf_sel.enable != 0) {
2202 if (of_machine_is_compatible("rockchip,rv1103"))
2203 property_enable(base, &rport->port_cfg->bvalid_grf_sel, true);
2204 else
2205 property_enable(base, &rport->port_cfg->bvalid_grf_sel, false);
2206 }
2207
2208 if (rport->vbus_always_on)
2209 extcon_set_state(rphy->edev, EXTCON_USB, true);
2210
2211 if (rport->vbus_always_on || rport->mode == USB_DR_MODE_HOST ||
2212 rport->mode == USB_DR_MODE_UNKNOWN)
2213 goto out;
2214
2215 wake_lock_init(&rport->wakelock, WAKE_LOCK_SUSPEND, "rockchip_otg");
2216 ret = devm_add_action_or_reset(rphy->dev, rockchip_otg_wake_lock_destroy,
2217 &rport->wakelock);
2218 if (ret)
2219 return ret;
2220
2221 INIT_DELAYED_WORK(&rport->bypass_uart_work,
2222 rockchip_usb_bypass_uart_work);
2223 INIT_DELAYED_WORK(&rport->chg_work, rockchip_chg_detect_work);
2224 INIT_DELAYED_WORK(&rport->otg_sm_work, rockchip_usb2phy_otg_sm_work);
2225
2226 if (!IS_ERR(rphy->edev)) {
2227 rport->event_nb.notifier_call = rockchip_otg_event;
2228
2229 ret = devm_extcon_register_notifier(rphy->dev, rphy->edev,
2230 EXTCON_USB_HOST, &rport->event_nb);
2231 if (ret) {
2232 dev_err(rphy->dev, "register USB HOST notifier failed\n");
2233 return ret;
2234 }
2235 }
2236
2237 out:
2238 /*
2239 * Let us put phy-port into suspend mode here for saving power
2240 * consumption, and usb controller will resume it during probe
2241 * time if needed.
2242 */
2243 ret = property_enable(base, &rport->port_cfg->phy_sus, true);
2244 if (ret)
2245 return ret;
2246 rport->suspended = true;
2247
2248 return 0;
2249 }
2250
rockchip_usb2phy_probe(struct platform_device * pdev)2251 static int rockchip_usb2phy_probe(struct platform_device *pdev)
2252 {
2253 struct device *dev = &pdev->dev;
2254 struct device_node *np = dev->of_node;
2255 struct device_node *child_np;
2256 struct phy_provider *provider;
2257 struct rockchip_usb2phy *rphy;
2258 struct resource *res;
2259 const struct rockchip_usb2phy_cfg *phy_cfgs;
2260 const struct of_device_id *match;
2261 unsigned int reg;
2262 unsigned int index;
2263 int ret;
2264
2265 rphy = devm_kzalloc(dev, sizeof(*rphy), GFP_KERNEL);
2266 if (!rphy)
2267 return -ENOMEM;
2268
2269 match = of_match_device(dev->driver->of_match_table, dev);
2270 if (!match || !match->data) {
2271 dev_err(dev, "phy configs are not assigned!\n");
2272 return -EINVAL;
2273 }
2274
2275 if (!dev->parent || !dev->parent->of_node) {
2276 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2277 if (!res) {
2278 dev_err(dev, "missing memory resource\n");
2279 return -ENODEV;
2280 }
2281
2282 rphy->phy_base = devm_ioremap_resource(dev, res);
2283 if (IS_ERR(rphy->phy_base))
2284 return PTR_ERR(rphy->phy_base);
2285
2286 rphy->grf = syscon_regmap_lookup_by_phandle(np,
2287 "rockchip,usbgrf");
2288 if (IS_ERR(rphy->grf))
2289 return PTR_ERR(rphy->grf);
2290
2291 reg = res->start;
2292 } else {
2293 rphy->grf = syscon_node_to_regmap(dev->parent->of_node);
2294 if (IS_ERR(rphy->grf))
2295 return PTR_ERR(rphy->grf);
2296
2297 if (of_device_is_compatible(np, "rockchip,rv1108-usb2phy")) {
2298 rphy->usbgrf =
2299 syscon_regmap_lookup_by_phandle(dev->of_node,
2300 "rockchip,usbgrf");
2301 if (IS_ERR(rphy->usbgrf))
2302 return PTR_ERR(rphy->usbgrf);
2303 } else {
2304 rphy->usbgrf = NULL;
2305 }
2306
2307 if (of_property_read_u32(np, "reg", ®)) {
2308 dev_err(dev, "missing reg property in %s node\n",
2309 np->name);
2310 return -EINVAL;
2311 }
2312 }
2313
2314 rphy->dev = dev;
2315 phy_cfgs = match->data;
2316 rphy->chg_state = USB_CHG_STATE_UNDEFINED;
2317 rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
2318 rphy->edev_self = false;
2319 rphy->irq = platform_get_irq(pdev, 0);
2320 platform_set_drvdata(pdev, rphy);
2321
2322 ret = rockchip_usb2phy_extcon_register(rphy);
2323 if (ret)
2324 return ret;
2325
2326 /* find out a proper config which can be matched with dt. */
2327 index = 0;
2328 do {
2329 if (phy_cfgs[index].reg == reg) {
2330 rphy->phy_cfg = &phy_cfgs[index];
2331 break;
2332 }
2333
2334 ++index;
2335 } while (phy_cfgs[index].reg);
2336
2337 if (!rphy->phy_cfg) {
2338 dev_err(dev, "no phy-config can be matched with %pOFn node\n",
2339 np);
2340 return -EINVAL;
2341 }
2342
2343 pm_runtime_set_active(dev);
2344 pm_runtime_enable(dev);
2345 pm_runtime_get_sync(dev);
2346
2347
2348 rphy->phy_reset = devm_reset_control_get_optional(dev, "phy");
2349 if (IS_ERR(rphy->phy_reset))
2350 return PTR_ERR(rphy->phy_reset);
2351
2352 ret = devm_clk_bulk_get_all(dev, &rphy->clks);
2353 if (ret == -EPROBE_DEFER)
2354 return ret;
2355
2356 /* Clocks are optional */
2357 if (ret < 0)
2358 rphy->num_clks = 0;
2359 else
2360 rphy->num_clks = ret;
2361
2362 ret = clk_bulk_prepare_enable(rphy->num_clks, rphy->clks);
2363 if (ret)
2364 return ret;
2365
2366 if (rphy->phy_cfg->phy_tuning) {
2367 ret = rphy->phy_cfg->phy_tuning(rphy);
2368 if (ret)
2369 goto disable_clks;
2370 }
2371
2372 index = 0;
2373 for_each_available_child_of_node(np, child_np) {
2374 struct rockchip_usb2phy_port *rport = &rphy->ports[index];
2375 struct phy *phy;
2376
2377 /* This driver aims to support both otg-port and host-port */
2378 if (!of_node_name_eq(child_np, "host-port") &&
2379 !of_node_name_eq(child_np, "otg-port"))
2380 goto next_child;
2381
2382 phy = devm_phy_create(dev, child_np, &rockchip_usb2phy_ops);
2383 if (IS_ERR(phy)) {
2384 dev_err(dev, "failed to create phy\n");
2385 ret = PTR_ERR(phy);
2386 goto put_child;
2387 }
2388
2389 rport->phy = phy;
2390 phy_set_drvdata(rport->phy, rport);
2391
2392 /* initialize otg/host port separately */
2393 if (of_node_name_eq(child_np, "host-port")) {
2394 ret = rockchip_usb2phy_host_port_init(rphy, rport,
2395 child_np);
2396 if (ret)
2397 goto put_child;
2398 } else {
2399 ret = rockchip_usb2phy_otg_port_init(rphy, rport,
2400 child_np);
2401 if (ret)
2402 goto put_child;
2403 }
2404
2405 next_child:
2406 /* to prevent out of boundary */
2407 if (++index >= rphy->phy_cfg->num_ports)
2408 break;
2409 }
2410
2411 provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
2412 if (IS_ERR(provider)) {
2413 dev_err(dev, "Failed to register phy provider\n");
2414 ret = PTR_ERR(provider);
2415 goto put_child;
2416 }
2417
2418 /* Attributes */
2419 ret = sysfs_create_group(&dev->kobj, &usb2_phy_attr_group);
2420 if (ret) {
2421 dev_err(dev, "Cannot create sysfs group: %d\n", ret);
2422 goto put_child;
2423 }
2424
2425 ret = rockchip_usb2phy_clk480m_register(rphy);
2426 if (ret) {
2427 dev_err(dev, "failed to register 480m output clock\n");
2428 goto put_child;
2429 }
2430
2431 if (rphy->irq > 0) {
2432 ret = devm_request_threaded_irq(rphy->dev, rphy->irq, NULL,
2433 rockchip_usb2phy_irq,
2434 IRQF_ONESHOT,
2435 "rockchip_usb2phy",
2436 rphy);
2437 if (ret) {
2438 dev_err(rphy->dev,
2439 "failed to request usb2 phy irq handle\n");
2440 goto put_child;
2441 }
2442 }
2443
2444 if (of_property_read_bool(np, "wakeup-source"))
2445 device_init_wakeup(rphy->dev, true);
2446 else
2447 device_init_wakeup(rphy->dev, false);
2448
2449 return 0;
2450
2451 put_child:
2452 of_node_put(child_np);
2453 disable_clks:
2454 pm_runtime_put_sync(dev);
2455 pm_runtime_disable(dev);
2456 clk_bulk_disable_unprepare(rphy->num_clks, rphy->clks);
2457 return ret;
2458 }
2459
2460 static int __maybe_unused
rockchip_usb2phy_low_power_enable(struct rockchip_usb2phy * rphy,struct rockchip_usb2phy_port * rport,bool value)2461 rockchip_usb2phy_low_power_enable(struct rockchip_usb2phy *rphy,
2462 struct rockchip_usb2phy_port *rport,
2463 bool value)
2464 {
2465 int ret = 0;
2466
2467 if (!rport->low_power_en)
2468 return ret;
2469
2470 if (rport->port_id == USB2PHY_PORT_OTG) {
2471 dev_info(&rport->phy->dev, "set otg port low power state %d\n",
2472 value);
2473 ret = property_enable(rphy->grf, &rport->port_cfg->bypass_bc,
2474 value);
2475 if (ret)
2476 return ret;
2477
2478 ret = property_enable(rphy->grf, &rport->port_cfg->bypass_otg,
2479 value);
2480 if (ret)
2481 return ret;
2482
2483 ret = property_enable(rphy->grf, &rport->port_cfg->vbus_det_en,
2484 !value);
2485 } else if (rport->port_id == USB2PHY_PORT_HOST) {
2486 dev_info(&rport->phy->dev, "set host port low power state %d\n",
2487 value);
2488
2489 ret = property_enable(rphy->grf, &rport->port_cfg->bypass_host,
2490 value);
2491 }
2492
2493 return ret;
2494 }
2495
rk312x_usb2phy_tuning(struct rockchip_usb2phy * rphy)2496 static int rk312x_usb2phy_tuning(struct rockchip_usb2phy *rphy)
2497 {
2498 int ret;
2499
2500 /* Turn off differential receiver in suspend mode */
2501 ret = regmap_write(rphy->grf, 0x298, 0x00040000);
2502 if (ret)
2503 return ret;
2504
2505 return 0;
2506 }
2507
rk3228_usb2phy_tuning(struct rockchip_usb2phy * rphy)2508 static int rk3228_usb2phy_tuning(struct rockchip_usb2phy *rphy)
2509 {
2510 int ret = 0;
2511
2512 /* Open pre-emphasize in non-chirp state for PHY0 otg port */
2513 if (rphy->phy_cfg->reg == 0x760)
2514 ret = regmap_write(rphy->grf, 0x76c, 0x00070004);
2515
2516 return ret;
2517 }
2518
rk3308_usb2phy_tuning(struct rockchip_usb2phy * rphy)2519 static int rk3308_usb2phy_tuning(struct rockchip_usb2phy *rphy)
2520 {
2521 int ret;
2522
2523 if (soc_is_rk3308bs()) {
2524 /* Turn off differential receiver in suspend mode */
2525 ret = regmap_update_bits(rphy->grf, 0x30, BIT(2), 0);
2526 if (ret)
2527 return ret;
2528
2529 /* Enable otg port pre-emphasis during non-chirp phase */
2530 ret = regmap_update_bits(rphy->grf, 0, GENMASK(2, 0), BIT(2));
2531 if (ret)
2532 return ret;
2533
2534 /* Set otg port squelch trigger point configure to 100mv */
2535 ret = regmap_update_bits(rphy->grf, 0x004, GENMASK(7, 5), 0x40);
2536 if (ret)
2537 return ret;
2538
2539 ret = regmap_update_bits(rphy->grf, 0x008, BIT(0), 0x1);
2540 if (ret)
2541 return ret;
2542
2543 /* Enable host port pre-emphasis during non-chirp phase */
2544 ret = regmap_update_bits(rphy->grf, 0x400, GENMASK(2, 0), BIT(2));
2545 if (ret)
2546 return ret;
2547
2548 /* Set host port squelch trigger point configure to 100mv */
2549 ret = regmap_update_bits(rphy->grf, 0x404, GENMASK(7, 5), 0x40);
2550 if (ret)
2551 return ret;
2552
2553 ret = regmap_update_bits(rphy->grf, 0x408, BIT(0), 0x1);
2554 if (ret)
2555 return ret;
2556 } else {
2557 /* Open pre-emphasize in non-chirp state for otg port */
2558 ret = regmap_write(rphy->grf, 0x0, 0x00070004);
2559 if (ret)
2560 return ret;
2561
2562 /* Open pre-emphasize in non-chirp state for host port */
2563 ret = regmap_write(rphy->grf, 0x30, 0x00070004);
2564 if (ret)
2565 return ret;
2566
2567 /* Turn off differential receiver in suspend mode */
2568 ret = regmap_write(rphy->grf, 0x18, 0x00040000);
2569 if (ret)
2570 return ret;
2571 }
2572
2573 return 0;
2574 }
2575
rk3328_usb2phy_tuning(struct rockchip_usb2phy * rphy)2576 static int rk3328_usb2phy_tuning(struct rockchip_usb2phy *rphy)
2577 {
2578 int ret;
2579
2580 if (soc_is_px30s()) {
2581 /* Enable otg port pre-emphasis during non-chirp phase */
2582 ret = regmap_update_bits(rphy->grf, 0x8000, GENMASK(2, 0), BIT(2));
2583 if (ret)
2584 return ret;
2585
2586 /* Set otg port squelch trigger point configure to 100mv */
2587 ret = regmap_update_bits(rphy->grf, 0x8004, GENMASK(7, 5), 0x40);
2588 if (ret)
2589 return ret;
2590
2591 ret = regmap_update_bits(rphy->grf, 0x8008, BIT(0), 0x1);
2592 if (ret)
2593 return ret;
2594
2595 /* Turn off otg port differential reciver in suspend mode */
2596 ret = regmap_update_bits(rphy->grf, 0x8030, BIT(2), 0);
2597 if (ret)
2598 return ret;
2599
2600 /* Enable host port pre-emphasis during non-chirp phase */
2601 ret = regmap_update_bits(rphy->grf, 0x8400, GENMASK(2, 0), BIT(2));
2602 if (ret)
2603 return ret;
2604
2605 /* Set host port squelch trigger point configure to 100mv */
2606 ret = regmap_update_bits(rphy->grf, 0x8404, GENMASK(7, 5), 0x40);
2607 if (ret)
2608 return ret;
2609
2610 ret = regmap_update_bits(rphy->grf, 0x8408, BIT(0), 0x1);
2611 if (ret)
2612 return ret;
2613
2614 /* Turn off host port differential reciver in suspend mode */
2615 ret = regmap_update_bits(rphy->grf, 0x8430, BIT(2), 0);
2616 if (ret)
2617 return ret;
2618 } else {
2619 /* Open debug mode for tuning */
2620 ret = regmap_write(rphy->grf, 0x2c, 0xffff0400);
2621 if (ret)
2622 return ret;
2623
2624 /* Open pre-emphasize in non-chirp state for otg port */
2625 ret = regmap_write(rphy->grf, 0x0, 0x00070004);
2626 if (ret)
2627 return ret;
2628
2629 /* Open pre-emphasize in non-chirp state for host port */
2630 ret = regmap_write(rphy->grf, 0x30, 0x00070004);
2631 if (ret)
2632 return ret;
2633
2634 /* Turn off differential receiver in suspend mode */
2635 ret = regmap_write(rphy->grf, 0x18, 0x00040000);
2636 if (ret)
2637 return ret;
2638 }
2639 return 0;
2640 }
2641
rk3366_usb2phy_tuning(struct rockchip_usb2phy * rphy)2642 static int rk3366_usb2phy_tuning(struct rockchip_usb2phy *rphy)
2643 {
2644 unsigned int open_pre_emphasize = 0xffff851f;
2645 unsigned int eye_height_tuning = 0xffff68c8;
2646 unsigned int compensation_tuning = 0xffff026e;
2647 int ret = 0;
2648
2649 /* open HS pre-emphasize to expand HS slew rate for each port. */
2650 ret |= regmap_write(rphy->grf, 0x0780, open_pre_emphasize);
2651 ret |= regmap_write(rphy->grf, 0x079c, eye_height_tuning);
2652 ret |= regmap_write(rphy->grf, 0x07b0, open_pre_emphasize);
2653 ret |= regmap_write(rphy->grf, 0x07cc, eye_height_tuning);
2654
2655 /* compensate default tuning reference relate to ODT and etc. */
2656 ret |= regmap_write(rphy->grf, 0x078c, compensation_tuning);
2657
2658 return ret;
2659 }
2660
rk3399_usb2phy_tuning(struct rockchip_usb2phy * rphy)2661 static int rk3399_usb2phy_tuning(struct rockchip_usb2phy *rphy)
2662 {
2663 struct device_node *node = rphy->dev->of_node;
2664 int ret = 0;
2665
2666 if (rphy->phy_cfg->reg == 0xe450) {
2667 /*
2668 * Disable the pre-emphasize in eop state
2669 * and chirp state to avoid mis-trigger the
2670 * disconnect detection and also avoid hs
2671 * handshake fail for PHY0.
2672 */
2673 ret |= regmap_write(rphy->grf, 0x4480,
2674 GENMASK(17, 16) | 0x0);
2675 ret |= regmap_write(rphy->grf, 0x44b4,
2676 GENMASK(17, 16) | 0x0);
2677 } else {
2678 /*
2679 * Disable the pre-emphasize in eop state
2680 * and chirp state to avoid mis-trigger the
2681 * disconnect detection and also avoid hs
2682 * handshake fail for PHY1.
2683 */
2684 ret |= regmap_write(rphy->grf, 0x4500,
2685 GENMASK(17, 16) | 0x0);
2686 ret |= regmap_write(rphy->grf, 0x4534,
2687 GENMASK(17, 16) | 0x0);
2688 }
2689
2690 if (!of_property_read_bool(node, "rockchip,u2phy-tuning"))
2691 return ret;
2692
2693 if (rphy->phy_cfg->reg == 0xe450) {
2694 /*
2695 * Set max ODT compensation voltage and
2696 * current tuning reference for PHY0.
2697 */
2698 ret |= regmap_write(rphy->grf, 0x448c,
2699 GENMASK(23, 16) | 0xe3);
2700
2701 /* Set max pre-emphasis level for PHY0 */
2702 ret |= regmap_write(rphy->grf, 0x44b0,
2703 GENMASK(18, 16) | 0x07);
2704
2705 /*
2706 * Set PHY0 A port squelch trigger point to 125mv
2707 */
2708 ret |= regmap_write(rphy->grf, 0x4480,
2709 GENMASK(30, 30) | 0x4000);
2710 } else {
2711 /*
2712 * Set max ODT compensation voltage and
2713 * current tuning reference for PHY1.
2714 */
2715 ret |= regmap_write(rphy->grf, 0x450c,
2716 GENMASK(23, 16) | 0xe3);
2717
2718 /* Set max pre-emphasis level for PHY1 */
2719 ret |= regmap_write(rphy->grf, 0x4530,
2720 GENMASK(18, 16) | 0x07);
2721
2722 /*
2723 * Set PHY1 A port squelch trigger point to 125mv
2724 */
2725 ret |= regmap_write(rphy->grf, 0x4500,
2726 GENMASK(30, 30) | 0x4000);
2727 }
2728
2729 return ret;
2730 }
2731
rk3528_usb2phy_tuning(struct rockchip_usb2phy * rphy)2732 static int rk3528_usb2phy_tuning(struct rockchip_usb2phy *rphy)
2733 {
2734 int ret = 0;
2735
2736 /* Turn off otg port differential receiver in suspend mode */
2737 phy_clear_bits(rphy->phy_base + 0x30, BIT(2));
2738
2739 /* Turn off host port differential receiver in suspend mode */
2740 phy_clear_bits(rphy->phy_base + 0x430, BIT(2));
2741
2742 /* Set otg port HS eye height to 400mv(default is 450mv) */
2743 phy_update_bits(rphy->phy_base + 0x30, GENMASK(6, 4), (0x00 << 4));
2744
2745 /* Set host port HS eye height to 400mv(default is 450mv) */
2746 phy_update_bits(rphy->phy_base + 0x430, GENMASK(6, 4), (0x00 << 4));
2747
2748 /* Choose the Tx fs/ls data as linestate from TX driver for otg port */
2749 phy_update_bits(rphy->phy_base + 0x94, GENMASK(6, 3), (0x03 << 3));
2750
2751 /* Enable otg and host ports phy irq to pmu wakeup source */
2752 ret |= regmap_write(rphy->grf, 0x80004, 0x00030003);
2753
2754 return ret;
2755 }
2756
rk3562_usb2phy_tuning(struct rockchip_usb2phy * rphy)2757 static int rk3562_usb2phy_tuning(struct rockchip_usb2phy *rphy)
2758 {
2759 int ret = 0;
2760
2761 /* Turn off differential receiver by default to save power */
2762 phy_clear_bits(rphy->phy_base + 0x0030, BIT(2));
2763 phy_clear_bits(rphy->phy_base + 0x0430, BIT(2));
2764
2765 /* Enable pre-emphasis during non-chirp phase */
2766 phy_update_bits(rphy->phy_base, GENMASK(2, 0), 0x04);
2767 phy_update_bits(rphy->phy_base + 0x0400, GENMASK(2, 0), 0x04);
2768
2769 /* Set HS eye height to 425mv(default is 400mv) */
2770 phy_update_bits(rphy->phy_base + 0x0030, GENMASK(6, 4), (0x05 << 4));
2771 phy_update_bits(rphy->phy_base + 0x0430, GENMASK(6, 4), (0x05 << 4));
2772
2773 /* Set the bvalid filter time to 10ms based on the u2phy grf pclk 100MHz */
2774 ret |= regmap_write(rphy->grf, 0x0138, FILTER_COUNTER);
2775
2776 /* Set the id filter time to 10ms based on the u2phy grf pclk 100MHz */
2777 ret |= regmap_write(rphy->grf, 0x013c, FILTER_COUNTER);
2778
2779 /* Enable host port wakeup irq */
2780 ret |= regmap_write(rphy->grf, 0x010c, 0x80008000);
2781
2782 return ret;
2783 }
2784
rk3568_usb2phy_tuning(struct rockchip_usb2phy * rphy)2785 static int rk3568_usb2phy_tuning(struct rockchip_usb2phy *rphy)
2786 {
2787 int ret = 0;
2788
2789 /* Turn off differential receiver by default to save power */
2790 phy_clear_bits(rphy->phy_base + 0x30, BIT(2));
2791
2792 /* Enable otg port pre-emphasis during non-chirp phase */
2793 phy_update_bits(rphy->phy_base, GENMASK(2, 0), 0x04);
2794
2795 /* Enable host port pre-emphasis during non-chirp phase */
2796 phy_update_bits(rphy->phy_base + 0x0400, GENMASK(2, 0), 0x04);
2797
2798 if (rphy->phy_cfg->reg == 0xfe8a0000) {
2799 /* Set otg port HS eye height to 437.5mv(default is 400mv) */
2800 phy_update_bits(rphy->phy_base + 0x30, GENMASK(6, 4), (0x06 << 4));
2801
2802 /*
2803 * Set the bvalid filter time to 10ms
2804 * based on the usb2 phy grf pclk 100MHz.
2805 */
2806 ret |= regmap_write(rphy->grf, 0x0048, FILTER_COUNTER);
2807
2808 /*
2809 * Set the id filter time to 10ms based
2810 * on the usb2 phy grf pclk 100MHz.
2811 */
2812 ret |= regmap_write(rphy->grf, 0x004c, FILTER_COUNTER);
2813 }
2814
2815 /* Enable host port (usb3 host1 and usb2 host1) wakeup irq */
2816 ret |= regmap_write(rphy->grf, 0x000c, 0x80008000);
2817
2818 return ret;
2819 }
2820
rv1106_usb2phy_tuning(struct rockchip_usb2phy * rphy)2821 static int rv1106_usb2phy_tuning(struct rockchip_usb2phy *rphy)
2822 {
2823 /* Always enable pre-emphasis in SOF & EOP & chirp & non-chirp state */
2824 phy_update_bits(rphy->phy_base + 0x30, GENMASK(2, 0), 0x07);
2825
2826 if (rockchip_get_cpu_version()) {
2827 /* Set Tx HS pre_emphasize strength to 3'b001 */
2828 phy_update_bits(rphy->phy_base + 0x40, GENMASK(5, 3), (0x01 << 3));
2829 } else {
2830 /* Set Tx HS pre_emphasize strength to 3'b011 */
2831 phy_update_bits(rphy->phy_base + 0x40, GENMASK(5, 3), (0x03 << 3));
2832 }
2833
2834 /* Set RX Squelch trigger point configure to 4'b0000(112.5 mV) */
2835 phy_update_bits(rphy->phy_base + 0x64, GENMASK(6, 3), (0x00 << 3));
2836
2837 /* Turn off differential receiver by default to save power */
2838 phy_clear_bits(rphy->phy_base + 0x100, BIT(6));
2839
2840 /* Set 45ohm HS ODT value to 5'b10111 to increase driver strength */
2841 phy_update_bits(rphy->phy_base + 0x11c, GENMASK(4, 0), 0x17);
2842
2843 /* Set Tx HS eye height tuning to 3'b011(462 mV)*/
2844 phy_update_bits(rphy->phy_base + 0x124, GENMASK(4, 2), (0x03 << 2));
2845
2846 /* Bypass Squelch detector calibration */
2847 phy_update_bits(rphy->phy_base + 0x1a4, GENMASK(7, 4), (0x01 << 4));
2848 phy_update_bits(rphy->phy_base + 0x1b4, GENMASK(7, 4), (0x01 << 4));
2849
2850 /* Set HS disconnect detect mode to single ended detect mode */
2851 phy_set_bits(rphy->phy_base + 0x70, BIT(2));
2852
2853 return 0;
2854 }
2855
rockchip_usb2phy_vbus_det_control(struct rockchip_usb2phy * rphy,const struct usb2phy_reg * vbus_det_en,bool en)2856 static int rockchip_usb2phy_vbus_det_control(struct rockchip_usb2phy *rphy,
2857 const struct usb2phy_reg *vbus_det_en,
2858 bool en)
2859 {
2860 if (en) {
2861 /* Enable vbus voltage level detection function */
2862 phy_clear_bits(rphy->phy_base + vbus_det_en->offset, BIT(7));
2863 } else {
2864 /* Disable vbus voltage level detection function */
2865 phy_set_bits(rphy->phy_base + vbus_det_en->offset, BIT(7));
2866 }
2867
2868 return 0;
2869 }
2870
rk3588_usb2phy_tuning(struct rockchip_usb2phy * rphy)2871 static int rk3588_usb2phy_tuning(struct rockchip_usb2phy *rphy)
2872 {
2873 unsigned int reg;
2874 int ret = 0;
2875
2876 /* Read the SIDDQ control register */
2877 ret = regmap_read(rphy->grf, 0x0008, ®);
2878 if (ret)
2879 return ret;
2880
2881 if (reg & BIT(13)) {
2882 /* Deassert SIDDQ to power on analog block */
2883 ret = regmap_write(rphy->grf, 0x0008,
2884 GENMASK(29, 29) | 0x0000);
2885 if (ret)
2886 return ret;
2887
2888 /* Do reset after exit IDDQ mode */
2889 ret = rockchip_usb2phy_reset(rphy);
2890 if (ret)
2891 return ret;
2892 }
2893
2894 if (rphy->phy_cfg->reg == 0x0000) {
2895 /*
2896 * Set USB2 PHY0 suspend configuration for USB3_0
2897 * 1. Set utmi_termselect to 1'b1 (en FS terminations)
2898 * 2. Set utmi_xcvrselect to 2'b01 (FS transceiver)
2899 * 3. Set utmi_opmode to 2'b01 (no-driving)
2900 */
2901 ret |= regmap_write(rphy->grf, 0x000c,
2902 GENMASK(20, 16) | 0x0015);
2903
2904 /* HS DC Voltage Level Adjustment 4'b1001 : +5.89% */
2905 ret |= regmap_write(rphy->grf, 0x0004,
2906 GENMASK(27, 24) | 0x0900);
2907
2908 /* HS Transmitter Pre-Emphasis Current Control 2'b10 : 2x */
2909 ret |= regmap_write(rphy->grf, 0x0008,
2910 GENMASK(20, 19) | 0x0010);
2911
2912 /* Pullup iddig pin for USB3_0 OTG mode */
2913 ret |= regmap_write(rphy->grf, 0x0010,
2914 GENMASK(17, 16) | 0x0003);
2915 } else if (rphy->phy_cfg->reg == 0x4000) {
2916 /*
2917 * Set USB2 PHY1 suspend configuration for USB3_1
2918 * 1. Set utmi_termselect to 1'b1 (en FS terminations)
2919 * 2. Set utmi_xcvrselect to 2'b01(FS transceiver)
2920 * 3. Set utmi_opmode to 2'b01 (no-driving)
2921 */
2922 ret |= regmap_write(rphy->grf, 0x000c,
2923 GENMASK(20, 16) | 0x0015);
2924
2925 /* HS DC Voltage Level Adjustment 4'b1001 : +5.89% */
2926 ret |= regmap_write(rphy->grf, 0x0004,
2927 GENMASK(27, 24) | 0x0900);
2928
2929 /* HS Transmitter Pre-Emphasis Current Control 2'b10 : 2x */
2930 ret |= regmap_write(rphy->grf, 0x0008,
2931 GENMASK(20, 19) | 0x0010);
2932
2933 /* Pullup iddig pin for USB3_1 OTG mode */
2934 ret |= regmap_write(rphy->grf, 0x0010,
2935 GENMASK(17, 16) | 0x0003);
2936 } else if (rphy->phy_cfg->reg == 0x8000) {
2937 /*
2938 * Set USB2 PHY2 suspend configuration for USB2_0
2939 * 1. Set utmi_termselect to 1'b1 (en FS terminations)
2940 * 2. Set utmi_xcvrselect to 2'b01(FS transceiver)
2941 * 3. Set utmi_opmode to 2'b00 (normal)
2942 */
2943 ret |= regmap_write(rphy->grf, 0x000c,
2944 GENMASK(20, 16) | 0x0014);
2945
2946 /* HS DC Voltage Level Adjustment 4'b1001 : +5.89% */
2947 ret |= regmap_write(rphy->grf, 0x0004,
2948 GENMASK(27, 24) | 0x0900);
2949
2950 /* HS Transmitter Pre-Emphasis Current Control 2'b10 : 2x */
2951 ret |= regmap_write(rphy->grf, 0x0008,
2952 GENMASK(20, 19) | 0x0010);
2953 } else if (rphy->phy_cfg->reg == 0xc000) {
2954 /*
2955 * Set USB2 PHY3 suspend configuration for USB2_1
2956 * 1. Set utmi_termselect to 1'b1 (en FS terminations)
2957 * 2. Set utmi_xcvrselect to 2'b01(FS transceiver)
2958 * 3. Set utmi_opmode to 2'b00 (normal)
2959 */
2960 ret |= regmap_write(rphy->grf, 0x000c,
2961 GENMASK(20, 16) | 0x0014);
2962
2963 /* HS DC Voltage Level Adjustment 4'b1001 : +5.89% */
2964 ret |= regmap_write(rphy->grf, 0x0004,
2965 GENMASK(27, 24) | 0x0900);
2966
2967 /* HS Transmitter Pre-Emphasis Current Control 2'b10 : 2x */
2968 ret |= regmap_write(rphy->grf, 0x0008,
2969 GENMASK(20, 19) | 0x0010);
2970 }
2971
2972 return ret;
2973 }
2974
2975 #ifdef CONFIG_PM_SLEEP
rockchip_usb2phy_pm_suspend(struct device * dev)2976 static int rockchip_usb2phy_pm_suspend(struct device *dev)
2977 {
2978 struct rockchip_usb2phy *rphy = dev_get_drvdata(dev);
2979 const struct rockchip_usb2phy_cfg *phy_cfg = rphy->phy_cfg;
2980 struct rockchip_usb2phy_port *rport;
2981 unsigned int index;
2982 int ret = 0;
2983 bool wakeup_enable = false;
2984
2985 if (device_may_wakeup(rphy->dev))
2986 wakeup_enable = true;
2987
2988 /*
2989 * Set the linestate filter time to 1ms based
2990 * on the usb2 phy grf pclk 32KHz on suspend.
2991 */
2992 if (phy_cfg->ls_filter_con.enable) {
2993 ret = regmap_write(rphy->grf, phy_cfg->ls_filter_con.offset,
2994 phy_cfg->ls_filter_con.enable);
2995 if (ret)
2996 dev_err(rphy->dev, "failed to set ls filter %d\n", ret);
2997 }
2998
2999 for (index = 0; index < phy_cfg->num_ports; index++) {
3000 rport = &rphy->ports[index];
3001 if (!rport->phy)
3002 continue;
3003
3004 if (rport->port_cfg->port_ls_filter_con.enable) {
3005 ret = regmap_write(rphy->grf,
3006 rport->port_cfg->port_ls_filter_con.offset,
3007 rport->port_cfg->port_ls_filter_con.enable);
3008 if (ret)
3009 dev_err(rphy->dev, "failed to set port ls filter %d\n", ret);
3010 }
3011
3012 if (rport->port_id == USB2PHY_PORT_OTG &&
3013 (rport->id_irq > 0 || rphy->irq > 0)) {
3014 mutex_lock(&rport->mutex);
3015 rport->prev_iddig = property_enabled(rphy->grf,
3016 &rport->port_cfg->utmi_iddig);
3017 ret = rockchip_usb2phy_enable_id_irq(rphy, rport,
3018 false);
3019 mutex_unlock(&rport->mutex);
3020 if (ret) {
3021 dev_err(rphy->dev,
3022 "failed to disable id irq\n");
3023 return ret;
3024 }
3025 }
3026
3027 if (rport->port_id == USB2PHY_PORT_OTG && wakeup_enable &&
3028 rport->bvalid_irq > 0)
3029 enable_irq_wake(rport->bvalid_irq);
3030
3031 /* activate the linestate to detect the next interrupt. */
3032 mutex_lock(&rport->mutex);
3033 ret = rockchip_usb2phy_enable_line_irq(rphy, rport, true);
3034 mutex_unlock(&rport->mutex);
3035 if (ret) {
3036 dev_err(rphy->dev, "failed to enable linestate irq\n");
3037 return ret;
3038 }
3039
3040 if (wakeup_enable && rport->ls_irq > 0)
3041 enable_irq_wake(rport->ls_irq);
3042
3043 /* enter low power state */
3044 rockchip_usb2phy_low_power_enable(rphy, rport, true);
3045 }
3046
3047 if (wakeup_enable && rphy->irq > 0)
3048 enable_irq_wake(rphy->irq);
3049
3050 return ret;
3051 }
3052
rockchip_usb2phy_pm_resume(struct device * dev)3053 static int rockchip_usb2phy_pm_resume(struct device *dev)
3054 {
3055 struct rockchip_usb2phy *rphy = dev_get_drvdata(dev);
3056 const struct rockchip_usb2phy_cfg *phy_cfg = rphy->phy_cfg;
3057 struct rockchip_usb2phy_port *rport;
3058 unsigned int index;
3059 bool iddig;
3060 int ret = 0;
3061 bool wakeup_enable = false;
3062
3063 if (device_may_wakeup(rphy->dev))
3064 wakeup_enable = true;
3065
3066 /*
3067 * PHY lost power in suspend, it needs to reset
3068 * PHY to recovery clock to usb controller.
3069 */
3070 if (!wakeup_enable)
3071 rockchip_usb2phy_reset(rphy);
3072
3073 if (phy_cfg->phy_tuning)
3074 ret = phy_cfg->phy_tuning(rphy);
3075
3076 if (phy_cfg->ls_filter_con.disable) {
3077 ret = regmap_write(rphy->grf, phy_cfg->ls_filter_con.offset,
3078 phy_cfg->ls_filter_con.disable);
3079 if (ret)
3080 dev_err(rphy->dev, "failed to set ls filter %d\n", ret);
3081 }
3082
3083 for (index = 0; index < phy_cfg->num_ports; index++) {
3084 rport = &rphy->ports[index];
3085 if (!rport->phy)
3086 continue;
3087
3088 if (rport->port_cfg->port_ls_filter_con.disable) {
3089 ret = regmap_write(rphy->grf,
3090 rport->port_cfg->port_ls_filter_con.offset,
3091 rport->port_cfg->port_ls_filter_con.disable);
3092 if (ret)
3093 dev_err(rphy->dev, "failed to set port ls filter %d\n", ret);
3094 }
3095
3096 if (rport->port_id == USB2PHY_PORT_OTG &&
3097 (rport->id_irq > 0 || rphy->irq > 0)) {
3098 mutex_lock(&rport->mutex);
3099 iddig = property_enabled(rphy->grf,
3100 &rport->port_cfg->utmi_iddig);
3101 ret = rockchip_usb2phy_enable_id_irq(rphy, rport,
3102 true);
3103 mutex_unlock(&rport->mutex);
3104 if (ret) {
3105 dev_err(rphy->dev,
3106 "failed to enable id irq\n");
3107 return ret;
3108 }
3109
3110 if (iddig != rport->prev_iddig) {
3111 dev_dbg(&rport->phy->dev,
3112 "iddig changed during resume\n");
3113 rport->prev_iddig = iddig;
3114 extcon_set_state_sync(rphy->edev,
3115 EXTCON_USB_HOST,
3116 !iddig);
3117 extcon_set_state_sync(rphy->edev,
3118 EXTCON_USB_VBUS_EN,
3119 !iddig);
3120 ret = rockchip_set_vbus_power(rport, !iddig);
3121 if (ret)
3122 return ret;
3123 }
3124 }
3125
3126 /* Enable bvalid detect irq */
3127 if (rport->port_id == USB2PHY_PORT_OTG &&
3128 (rport->mode == USB_DR_MODE_PERIPHERAL ||
3129 rport->mode == USB_DR_MODE_OTG) &&
3130 (rport->bvalid_irq > 0 || rport->otg_mux_irq > 0 || rphy->irq > 0) &&
3131 !rport->vbus_always_on) {
3132 ret = rockchip_usb2phy_enable_vbus_irq(rphy, rport,
3133 true);
3134 if (ret) {
3135 dev_err(rphy->dev,
3136 "failed to enable bvalid irq\n");
3137 return ret;
3138 }
3139
3140 if (property_enabled(rphy->grf, &rport->port_cfg->utmi_bvalid))
3141 schedule_delayed_work(&rport->otg_sm_work,
3142 OTG_SCHEDULE_DELAY);
3143
3144 }
3145
3146 if (rport->port_id == USB2PHY_PORT_OTG && wakeup_enable &&
3147 rport->bvalid_irq > 0)
3148 disable_irq_wake(rport->bvalid_irq);
3149
3150 if (wakeup_enable && rport->ls_irq > 0)
3151 disable_irq_wake(rport->ls_irq);
3152
3153 /* exit low power state */
3154 rockchip_usb2phy_low_power_enable(rphy, rport, false);
3155 }
3156
3157 if (wakeup_enable && rphy->irq > 0)
3158 disable_irq_wake(rphy->irq);
3159
3160 return ret;
3161 }
3162
3163 static const struct dev_pm_ops rockchip_usb2phy_dev_pm_ops = {
3164 SET_SYSTEM_SLEEP_PM_OPS(rockchip_usb2phy_pm_suspend,
3165 rockchip_usb2phy_pm_resume)
3166 };
3167
3168 #define ROCKCHIP_USB2PHY_DEV_PM (&rockchip_usb2phy_dev_pm_ops)
3169 #else
3170 #define ROCKCHIP_USB2PHY_DEV_PM NULL
3171 #endif /* CONFIG_PM_SLEEP */
3172
3173 static const struct rockchip_usb2phy_cfg rk1808_phy_cfgs[] = {
3174 {
3175 .reg = 0x100,
3176 .num_ports = 2,
3177 .clkout_ctl = { 0x108, 4, 4, 1, 0 },
3178 .port_cfgs = {
3179 [USB2PHY_PORT_OTG] = {
3180 .phy_sus = { 0x0100, 8, 0, 0, 0x1d1 },
3181 .bvalid_det_en = { 0x0110, 2, 2, 0, 1 },
3182 .bvalid_det_st = { 0x0114, 2, 2, 0, 1 },
3183 .bvalid_det_clr = { 0x0118, 2, 2, 0, 1 },
3184 .bypass_dm_en = { 0x0108, 2, 2, 0, 1},
3185 .bypass_sel = { 0x0108, 3, 3, 0, 1},
3186 .iddig_output = { 0x0100, 10, 10, 0, 1 },
3187 .iddig_en = { 0x0100, 9, 9, 0, 1 },
3188 .idfall_det_en = { 0x0110, 5, 5, 0, 1 },
3189 .idfall_det_st = { 0x0114, 5, 5, 0, 1 },
3190 .idfall_det_clr = { 0x0118, 5, 5, 0, 1 },
3191 .idrise_det_en = { 0x0110, 4, 4, 0, 1 },
3192 .idrise_det_st = { 0x0114, 4, 4, 0, 1 },
3193 .idrise_det_clr = { 0x0118, 4, 4, 0, 1 },
3194 .ls_det_en = { 0x0110, 0, 0, 0, 1 },
3195 .ls_det_st = { 0x0114, 0, 0, 0, 1 },
3196 .ls_det_clr = { 0x0118, 0, 0, 0, 1 },
3197 .utmi_avalid = { 0x0120, 10, 10, 0, 1 },
3198 .utmi_bvalid = { 0x0120, 9, 9, 0, 1 },
3199 .utmi_iddig = { 0x0120, 6, 6, 0, 1 },
3200 .utmi_ls = { 0x0120, 5, 4, 0, 1 },
3201 .vbus_det_en = { 0x001c, 15, 15, 1, 0 },
3202 },
3203 [USB2PHY_PORT_HOST] = {
3204 .phy_sus = { 0x104, 8, 0, 0, 0x1d1 },
3205 .ls_det_en = { 0x110, 1, 1, 0, 1 },
3206 .ls_det_st = { 0x114, 1, 1, 0, 1 },
3207 .ls_det_clr = { 0x118, 1, 1, 0, 1 },
3208 .utmi_ls = { 0x120, 17, 16, 0, 1 },
3209 .utmi_hstdet = { 0x120, 19, 19, 0, 1 }
3210 }
3211 },
3212 .chg_det = {
3213 .chg_mode = { 0x0100, 8, 0, 0, 0x1d7 },
3214 .cp_det = { 0x0120, 24, 24, 0, 1 },
3215 .dcp_det = { 0x0120, 23, 23, 0, 1 },
3216 .dp_det = { 0x0120, 25, 25, 0, 1 },
3217 .idm_sink_en = { 0x0108, 8, 8, 0, 1 },
3218 .idp_sink_en = { 0x0108, 7, 7, 0, 1 },
3219 .idp_src_en = { 0x0108, 9, 9, 0, 1 },
3220 .rdm_pdwn_en = { 0x0108, 10, 10, 0, 1 },
3221 .vdm_src_en = { 0x0108, 12, 12, 0, 1 },
3222 .vdp_src_en = { 0x0108, 11, 11, 0, 1 },
3223 },
3224 },
3225 { /* sentinel */ }
3226 };
3227
3228 static const struct rockchip_usb2phy_cfg rk312x_phy_cfgs[] = {
3229 {
3230 .reg = 0x17c,
3231 .num_ports = 2,
3232 .phy_tuning = rk312x_usb2phy_tuning,
3233 .clkout_ctl = { 0x0190, 15, 15, 1, 0 },
3234 .port_cfgs = {
3235 [USB2PHY_PORT_OTG] = {
3236 .phy_sus = { 0x017c, 8, 0, 0, 0x1d1 },
3237 .bvalid_det_en = { 0x017c, 14, 14, 0, 1 },
3238 .bvalid_det_st = { 0x017c, 15, 15, 0, 1 },
3239 .bvalid_det_clr = { 0x017c, 15, 15, 0, 1 },
3240 .bypass_dm_en = { 0x0190, 12, 12, 0, 1},
3241 .bypass_sel = { 0x0190, 13, 13, 0, 1},
3242 .iddig_output = { 0x017c, 10, 10, 0, 1 },
3243 .iddig_en = { 0x017c, 9, 9, 0, 1 },
3244 .idfall_det_en = { 0x01a0, 2, 2, 0, 1 },
3245 .idfall_det_st = { 0x01a0, 3, 3, 0, 1 },
3246 .idfall_det_clr = { 0x01a0, 3, 3, 0, 1 },
3247 .idrise_det_en = { 0x01a0, 0, 0, 0, 1 },
3248 .idrise_det_st = { 0x01a0, 1, 1, 0, 1 },
3249 .idrise_det_clr = { 0x01a0, 1, 1, 0, 1 },
3250 .ls_det_en = { 0x017c, 12, 12, 0, 1 },
3251 .ls_det_st = { 0x017c, 13, 13, 0, 1 },
3252 .ls_det_clr = { 0x017c, 13, 13, 0, 1 },
3253 .utmi_bvalid = { 0x014c, 5, 5, 0, 1 },
3254 .utmi_iddig = { 0x014c, 8, 8, 0, 1 },
3255 .utmi_ls = { 0x014c, 7, 6, 0, 1 },
3256 },
3257 [USB2PHY_PORT_HOST] = {
3258 .phy_sus = { 0x0194, 8, 0, 0, 0x1d1 },
3259 .ls_det_en = { 0x0194, 14, 14, 0, 1 },
3260 .ls_det_st = { 0x0194, 15, 15, 0, 1 },
3261 .ls_det_clr = { 0x0194, 15, 15, 0, 1 }
3262 }
3263 },
3264 .chg_det = {
3265 .chg_mode = { 0x017c, 8, 0, 0, 0x1d7 },
3266 .cp_det = { 0x02c0, 6, 6, 0, 1 },
3267 .dcp_det = { 0x02c0, 5, 5, 0, 1 },
3268 .dp_det = { 0x02c0, 7, 7, 0, 1 },
3269 .idm_sink_en = { 0x0184, 8, 8, 0, 1 },
3270 .idp_sink_en = { 0x0184, 7, 7, 0, 1 },
3271 .idp_src_en = { 0x0184, 9, 9, 0, 1 },
3272 .rdm_pdwn_en = { 0x0184, 10, 10, 0, 1 },
3273 .vdm_src_en = { 0x0184, 12, 12, 0, 1 },
3274 .vdp_src_en = { 0x0184, 11, 11, 0, 1 },
3275 },
3276 },
3277 { /* sentinel */ }
3278 };
3279
3280 static const struct rockchip_usb2phy_cfg rk3228_phy_cfgs[] = {
3281 {
3282 .reg = 0x760,
3283 .num_ports = 2,
3284 .phy_tuning = rk3228_usb2phy_tuning,
3285 .clkout_ctl = { 0x0768, 4, 4, 1, 0 },
3286 .port_cfgs = {
3287 [USB2PHY_PORT_OTG] = {
3288 .phy_sus = { 0x0760, 8, 0, 0, 0x1d1 },
3289 .bvalid_det_en = { 0x0680, 3, 3, 0, 1 },
3290 .bvalid_det_st = { 0x0690, 3, 3, 0, 1 },
3291 .bvalid_det_clr = { 0x06a0, 3, 3, 0, 1 },
3292 .iddig_output = { 0x0760, 10, 10, 0, 1 },
3293 .iddig_en = { 0x0760, 9, 9, 0, 1 },
3294 .idfall_det_en = { 0x0680, 6, 6, 0, 1 },
3295 .idfall_det_st = { 0x0690, 6, 6, 0, 1 },
3296 .idfall_det_clr = { 0x06a0, 6, 6, 0, 1 },
3297 .idrise_det_en = { 0x0680, 5, 5, 0, 1 },
3298 .idrise_det_st = { 0x0690, 5, 5, 0, 1 },
3299 .idrise_det_clr = { 0x06a0, 5, 5, 0, 1 },
3300 .ls_det_en = { 0x0680, 2, 2, 0, 1 },
3301 .ls_det_st = { 0x0690, 2, 2, 0, 1 },
3302 .ls_det_clr = { 0x06a0, 2, 2, 0, 1 },
3303 .utmi_bvalid = { 0x0480, 4, 4, 0, 1 },
3304 .utmi_iddig = { 0x0480, 1, 1, 0, 1 },
3305 .utmi_ls = { 0x0480, 3, 2, 0, 1 },
3306 .vbus_det_en = { 0x0788, 15, 15, 1, 0 },
3307 },
3308 [USB2PHY_PORT_HOST] = {
3309 .phy_sus = { 0x0764, 8, 0, 0, 0x1d1 },
3310 .ls_det_en = { 0x0680, 4, 4, 0, 1 },
3311 .ls_det_st = { 0x0690, 4, 4, 0, 1 },
3312 .ls_det_clr = { 0x06a0, 4, 4, 0, 1 }
3313 }
3314 },
3315 .chg_det = {
3316 .chg_mode = { 0x0760, 8, 0, 0, 0x1d7 },
3317 .cp_det = { 0x0884, 4, 4, 0, 1 },
3318 .dcp_det = { 0x0884, 3, 3, 0, 1 },
3319 .dp_det = { 0x0884, 5, 5, 0, 1 },
3320 .idm_sink_en = { 0x0768, 8, 8, 0, 1 },
3321 .idp_sink_en = { 0x0768, 7, 7, 0, 1 },
3322 .idp_src_en = { 0x0768, 9, 9, 0, 1 },
3323 .rdm_pdwn_en = { 0x0768, 10, 10, 0, 1 },
3324 .vdm_src_en = { 0x0768, 12, 12, 0, 1 },
3325 .vdp_src_en = { 0x0768, 11, 11, 0, 1 },
3326 },
3327 },
3328 {
3329 .reg = 0x800,
3330 .num_ports = 2,
3331 .clkout_ctl = { 0x0808, 4, 4, 1, 0 },
3332 .port_cfgs = {
3333 [USB2PHY_PORT_OTG] = {
3334 .phy_sus = { 0x804, 8, 0, 0, 0x1d1 },
3335 .ls_det_en = { 0x0684, 1, 1, 0, 1 },
3336 .ls_det_st = { 0x0694, 1, 1, 0, 1 },
3337 .ls_det_clr = { 0x06a4, 1, 1, 0, 1 }
3338 },
3339 [USB2PHY_PORT_HOST] = {
3340 .phy_sus = { 0x800, 8, 0, 0, 0x1d1 },
3341 .ls_det_en = { 0x0684, 0, 0, 0, 1 },
3342 .ls_det_st = { 0x0694, 0, 0, 0, 1 },
3343 .ls_det_clr = { 0x06a4, 0, 0, 0, 1 }
3344 }
3345 },
3346 },
3347 { /* sentinel */ }
3348 };
3349
3350 static const struct rockchip_usb2phy_cfg rk3308_phy_cfgs[] = {
3351 {
3352 .reg = 0x100,
3353 .num_ports = 2,
3354 .phy_tuning = rk3308_usb2phy_tuning,
3355 .clkout_ctl = { 0x0108, 4, 4, 1, 0 },
3356 .port_cfgs = {
3357 [USB2PHY_PORT_OTG] = {
3358 .phy_sus = { 0x0100, 8, 0, 0, 0x1d1 },
3359 .bvalid_det_en = { 0x3020, 2, 2, 0, 1 },
3360 .bvalid_det_st = { 0x3024, 2, 2, 0, 1 },
3361 .bvalid_det_clr = { 0x3028, 2, 2, 0, 1 },
3362 .iddig_output = { 0x0100, 10, 10, 0, 1 },
3363 .iddig_en = { 0x0100, 9, 9, 0, 1 },
3364 .idfall_det_en = { 0x3020, 5, 5, 0, 1 },
3365 .idfall_det_st = { 0x3024, 5, 5, 0, 1 },
3366 .idfall_det_clr = { 0x3028, 5, 5, 0, 1 },
3367 .idrise_det_en = { 0x3020, 4, 4, 0, 1 },
3368 .idrise_det_st = { 0x3024, 4, 4, 0, 1 },
3369 .idrise_det_clr = { 0x3028, 4, 4, 0, 1 },
3370 .ls_det_en = { 0x3020, 0, 0, 0, 1 },
3371 .ls_det_st = { 0x3024, 0, 0, 0, 1 },
3372 .ls_det_clr = { 0x3028, 0, 0, 0, 1 },
3373 .utmi_avalid = { 0x0120, 10, 10, 0, 1 },
3374 .utmi_bvalid = { 0x0120, 9, 9, 0, 1 },
3375 .utmi_iddig = { 0x0120, 6, 6, 0, 1 },
3376 .utmi_ls = { 0x0120, 5, 4, 0, 1 },
3377 .vbus_det_en = { 0x001c, 15, 15, 1, 0 },
3378 },
3379 [USB2PHY_PORT_HOST] = {
3380 .phy_sus = { 0x0104, 8, 0, 0, 0x1d1 },
3381 .ls_det_en = { 0x3020, 1, 1, 0, 1 },
3382 .ls_det_st = { 0x3024, 1, 1, 0, 1 },
3383 .ls_det_clr = { 0x3028, 1, 1, 0, 1 },
3384 .utmi_ls = { 0x120, 17, 16, 0, 1 },
3385 .utmi_hstdet = { 0x120, 19, 19, 0, 1 }
3386 }
3387 },
3388 .chg_det = {
3389 .chg_mode = { 0x0100, 8, 0, 0, 0x1d7 },
3390 .cp_det = { 0x0120, 24, 24, 0, 1 },
3391 .dcp_det = { 0x0120, 23, 23, 0, 1 },
3392 .dp_det = { 0x0120, 25, 25, 0, 1 },
3393 .idm_sink_en = { 0x0108, 8, 8, 0, 1 },
3394 .idp_sink_en = { 0x0108, 7, 7, 0, 1 },
3395 .idp_src_en = { 0x0108, 9, 9, 0, 1 },
3396 .rdm_pdwn_en = { 0x0108, 10, 10, 0, 1 },
3397 .vdm_src_en = { 0x0108, 12, 12, 0, 1 },
3398 .vdp_src_en = { 0x0108, 11, 11, 0, 1 },
3399 },
3400 },
3401 { /* sentinel */ }
3402 };
3403
3404 static const struct rockchip_usb2phy_cfg rk3328_phy_cfgs[] = {
3405 {
3406 .reg = 0x100,
3407 .num_ports = 2,
3408 .phy_tuning = rk3328_usb2phy_tuning,
3409 .clkout_ctl = { 0x108, 4, 4, 1, 0 },
3410 .port_cfgs = {
3411 [USB2PHY_PORT_OTG] = {
3412 .phy_sus = { 0x0100, 8, 0, 0, 0x1d1 },
3413 .bvalid_det_en = { 0x0110, 2, 2, 0, 1 },
3414 .bvalid_det_st = { 0x0114, 2, 2, 0, 1 },
3415 .bvalid_det_clr = { 0x0118, 2, 2, 0, 1 },
3416 .bypass_bc = { 0x0008, 14, 14, 0, 1 },
3417 .bypass_otg = { 0x0018, 15, 15, 1, 0 },
3418 .iddig_output = { 0x0100, 10, 10, 0, 1 },
3419 .iddig_en = { 0x0100, 9, 9, 0, 1 },
3420 .idfall_det_en = { 0x0110, 5, 5, 0, 1 },
3421 .idfall_det_st = { 0x0114, 5, 5, 0, 1 },
3422 .idfall_det_clr = { 0x0118, 5, 5, 0, 1 },
3423 .idrise_det_en = { 0x0110, 4, 4, 0, 1 },
3424 .idrise_det_st = { 0x0114, 4, 4, 0, 1 },
3425 .idrise_det_clr = { 0x0118, 4, 4, 0, 1 },
3426 .ls_det_en = { 0x0110, 0, 0, 0, 1 },
3427 .ls_det_st = { 0x0114, 0, 0, 0, 1 },
3428 .ls_det_clr = { 0x0118, 0, 0, 0, 1 },
3429 .utmi_avalid = { 0x0120, 10, 10, 0, 1 },
3430 .utmi_bvalid = { 0x0120, 9, 9, 0, 1 },
3431 .utmi_iddig = { 0x0120, 6, 6, 0, 1 },
3432 .utmi_ls = { 0x0120, 5, 4, 0, 1 },
3433 .vbus_det_en = { 0x001c, 15, 15, 1, 0 },
3434 },
3435 [USB2PHY_PORT_HOST] = {
3436 .phy_sus = { 0x104, 8, 0, 0, 0x1d1 },
3437 .bypass_host = { 0x048, 15, 15, 1, 0 },
3438 .ls_det_en = { 0x110, 1, 1, 0, 1 },
3439 .ls_det_st = { 0x114, 1, 1, 0, 1 },
3440 .ls_det_clr = { 0x118, 1, 1, 0, 1 },
3441 .utmi_ls = { 0x120, 17, 16, 0, 1 },
3442 .utmi_hstdet = { 0x120, 19, 19, 0, 1 }
3443 }
3444 },
3445 .chg_det = {
3446 .chg_mode = { 0x0100, 8, 0, 0, 0x1d7 },
3447 .cp_det = { 0x0120, 24, 24, 0, 1 },
3448 .dcp_det = { 0x0120, 23, 23, 0, 1 },
3449 .dp_det = { 0x0120, 25, 25, 0, 1 },
3450 .idm_sink_en = { 0x0108, 8, 8, 0, 1 },
3451 .idp_sink_en = { 0x0108, 7, 7, 0, 1 },
3452 .idp_src_en = { 0x0108, 9, 9, 0, 1 },
3453 .rdm_pdwn_en = { 0x0108, 10, 10, 0, 1 },
3454 .vdm_src_en = { 0x0108, 12, 12, 0, 1 },
3455 .vdp_src_en = { 0x0108, 11, 11, 0, 1 },
3456 },
3457 },
3458 { /* sentinel */ }
3459 };
3460
3461 static const struct rockchip_usb2phy_cfg rk3366_phy_cfgs[] = {
3462 {
3463 .reg = 0x700,
3464 .num_ports = 2,
3465 .phy_tuning = rk3366_usb2phy_tuning,
3466 .clkout_ctl = { 0x0724, 15, 15, 1, 0 },
3467 .port_cfgs = {
3468 [USB2PHY_PORT_HOST] = {
3469 .phy_sus = { 0x0728, 8, 0, 0, 0x1d1 },
3470 .ls_det_en = { 0x0680, 4, 4, 0, 1 },
3471 .ls_det_st = { 0x0690, 4, 4, 0, 1 },
3472 .ls_det_clr = { 0x06a0, 4, 4, 0, 1 },
3473 .utmi_ls = { 0x049c, 14, 13, 0, 1 },
3474 .utmi_hstdet = { 0x049c, 12, 12, 0, 1 }
3475 }
3476 },
3477 },
3478 { /* sentinel */ }
3479 };
3480
3481 static const struct rockchip_usb2phy_cfg rk3368_phy_cfgs[] = {
3482 {
3483 .reg = 0x700,
3484 .num_ports = 2,
3485 .clkout_ctl = { 0x0724, 15, 15, 1, 0 },
3486 .port_cfgs = {
3487 [USB2PHY_PORT_OTG] = {
3488 .phy_sus = { 0x0700, 8, 0, 0, 0x1d1 },
3489 .bvalid_det_en = { 0x0680, 3, 3, 0, 1 },
3490 .bvalid_det_st = { 0x0690, 3, 3, 0, 1 },
3491 .bvalid_det_clr = { 0x06a0, 3, 3, 0, 1 },
3492 .iddig_output = { 0x0700, 10, 10, 0, 1 },
3493 .iddig_en = { 0x0700, 9, 9, 0, 1 },
3494 .idfall_det_en = { 0x0680, 6, 6, 0, 1 },
3495 .idfall_det_st = { 0x0690, 6, 6, 0, 1 },
3496 .idfall_det_clr = { 0x06a0, 6, 6, 0, 1 },
3497 .idrise_det_en = { 0x0680, 5, 5, 0, 1 },
3498 .idrise_det_st = { 0x0690, 5, 5, 0, 1 },
3499 .idrise_det_clr = { 0x06a0, 5, 5, 0, 1 },
3500 .ls_det_en = { 0x0680, 2, 2, 0, 1 },
3501 .ls_det_st = { 0x0690, 2, 2, 0, 1 },
3502 .ls_det_clr = { 0x06a0, 2, 2, 0, 1 },
3503 .utmi_bvalid = { 0x04bc, 23, 23, 0, 1 },
3504 .utmi_iddig = { 0x04bc, 26, 26, 0, 1 },
3505 .utmi_ls = { 0x04bc, 25, 24, 0, 1 },
3506 .vbus_det_en = { 0x079c, 15, 15, 1, 0 },
3507 },
3508 [USB2PHY_PORT_HOST] = {
3509 .phy_sus = { 0x0728, 15, 0, 0, 0x1d1 },
3510 .ls_det_en = { 0x0680, 4, 4, 0, 1 },
3511 .ls_det_st = { 0x0690, 4, 4, 0, 1 },
3512 .ls_det_clr = { 0x06a0, 4, 4, 0, 1 }
3513 }
3514 },
3515 .chg_det = {
3516 .chg_mode = { 0x0700, 8, 0, 0, 0x1d7 },
3517 .cp_det = { 0x04b8, 30, 30, 0, 1 },
3518 .dcp_det = { 0x04b8, 29, 29, 0, 1 },
3519 .dp_det = { 0x04b8, 31, 31, 0, 1 },
3520 .idm_sink_en = { 0x0718, 8, 8, 0, 1 },
3521 .idp_sink_en = { 0x0718, 7, 7, 0, 1 },
3522 .idp_src_en = { 0x0718, 9, 9, 0, 1 },
3523 .rdm_pdwn_en = { 0x0718, 10, 10, 0, 1 },
3524 .vdm_src_en = { 0x0718, 12, 12, 0, 1 },
3525 .vdp_src_en = { 0x0718, 11, 11, 0, 1 },
3526 },
3527 },
3528 { /* sentinel */ }
3529 };
3530
3531 static const struct rockchip_usb2phy_cfg rk3399_phy_cfgs[] = {
3532 {
3533 .reg = 0xe450,
3534 .num_ports = 2,
3535 .phy_tuning = rk3399_usb2phy_tuning,
3536 .clkout_ctl = { 0xe450, 4, 4, 1, 0 },
3537 .port_cfgs = {
3538 [USB2PHY_PORT_OTG] = {
3539 .phy_sus = { 0xe454, 8, 0, 0x052, 0x1d1 },
3540 .bvalid_det_en = { 0xe3c0, 3, 3, 0, 1 },
3541 .bvalid_det_st = { 0xe3e0, 3, 3, 0, 1 },
3542 .bvalid_det_clr = { 0xe3d0, 3, 3, 0, 1 },
3543 .bypass_dm_en = { 0xe450, 2, 2, 0, 1 },
3544 .bypass_sel = { 0xe450, 3, 3, 0, 1 },
3545 .iddig_output = { 0xe454, 10, 10, 0, 1 },
3546 .iddig_en = { 0xe454, 9, 9, 0, 1 },
3547 .idfall_det_en = { 0xe3c0, 5, 5, 0, 1 },
3548 .idfall_det_st = { 0xe3e0, 5, 5, 0, 1 },
3549 .idfall_det_clr = { 0xe3d0, 5, 5, 0, 1 },
3550 .idrise_det_en = { 0xe3c0, 4, 4, 0, 1 },
3551 .idrise_det_st = { 0xe3e0, 4, 4, 0, 1 },
3552 .idrise_det_clr = { 0xe3d0, 4, 4, 0, 1 },
3553 .ls_det_en = { 0xe3c0, 2, 2, 0, 1 },
3554 .ls_det_st = { 0xe3e0, 2, 2, 0, 1 },
3555 .ls_det_clr = { 0xe3d0, 2, 2, 0, 1 },
3556 .utmi_avalid = { 0xe2ac, 7, 7, 0, 1 },
3557 .utmi_bvalid = { 0xe2ac, 12, 12, 0, 1 },
3558 .utmi_iddig = { 0xe2ac, 8, 8, 0, 1 },
3559 .utmi_ls = { 0xe2ac, 14, 13, 0, 1 },
3560 .vbus_det_en = { 0x449c, 15, 15, 1, 0 },
3561 },
3562 [USB2PHY_PORT_HOST] = {
3563 .phy_sus = { 0xe458, 1, 0, 0x2, 0x1 },
3564 .ls_det_en = { 0xe3c0, 6, 6, 0, 1 },
3565 .ls_det_st = { 0xe3e0, 6, 6, 0, 1 },
3566 .ls_det_clr = { 0xe3d0, 6, 6, 0, 1 },
3567 .utmi_ls = { 0xe2ac, 22, 21, 0, 1 },
3568 .utmi_hstdet = { 0xe2ac, 23, 23, 0, 1 }
3569 }
3570 },
3571 .chg_det = {
3572 .chg_mode = { 0xe454, 8, 0, 0, 0x1d7 },
3573 .cp_det = { 0xe2ac, 2, 2, 0, 1 },
3574 .dcp_det = { 0xe2ac, 1, 1, 0, 1 },
3575 .dp_det = { 0xe2ac, 0, 0, 0, 1 },
3576 .idm_sink_en = { 0xe450, 8, 8, 0, 1 },
3577 .idp_sink_en = { 0xe450, 7, 7, 0, 1 },
3578 .idp_src_en = { 0xe450, 9, 9, 0, 1 },
3579 .rdm_pdwn_en = { 0xe450, 10, 10, 0, 1 },
3580 .vdm_src_en = { 0xe450, 12, 12, 0, 1 },
3581 .vdp_src_en = { 0xe450, 11, 11, 0, 1 },
3582 },
3583 },
3584 {
3585 .reg = 0xe460,
3586 .num_ports = 2,
3587 .phy_tuning = rk3399_usb2phy_tuning,
3588 .clkout_ctl = { 0xe460, 4, 4, 1, 0 },
3589 .port_cfgs = {
3590 [USB2PHY_PORT_OTG] = {
3591 .phy_sus = { 0xe464, 8, 0, 0x052, 0x1d1 },
3592 .bvalid_det_en = { 0xe3c0, 8, 8, 0, 1 },
3593 .bvalid_det_st = { 0xe3e0, 8, 8, 0, 1 },
3594 .bvalid_det_clr = { 0xe3d0, 8, 8, 0, 1 },
3595 .iddig_output = { 0xe464, 10, 10, 0, 1 },
3596 .iddig_en = { 0xe464, 9, 9, 0, 1 },
3597 .idfall_det_en = { 0xe3c0, 10, 10, 0, 1 },
3598 .idfall_det_st = { 0xe3e0, 10, 10, 0, 1 },
3599 .idfall_det_clr = { 0xe3d0, 10, 10, 0, 1 },
3600 .idrise_det_en = { 0xe3c0, 9, 9, 0, 1 },
3601 .idrise_det_st = { 0xe3e0, 9, 9, 0, 1 },
3602 .idrise_det_clr = { 0xe3d0, 9, 9, 0, 1 },
3603 .ls_det_en = { 0xe3c0, 7, 7, 0, 1 },
3604 .ls_det_st = { 0xe3e0, 7, 7, 0, 1 },
3605 .ls_det_clr = { 0xe3d0, 7, 7, 0, 1 },
3606 .utmi_avalid = { 0xe2ac, 10, 10, 0, 1 },
3607 .utmi_bvalid = { 0xe2ac, 16, 16, 0, 1 },
3608 .utmi_iddig = { 0xe2ac, 11, 11, 0, 1 },
3609 .utmi_ls = { 0xe2ac, 18, 17, 0, 1 },
3610 .vbus_det_en = { 0x451c, 15, 15, 1, 0 },
3611 },
3612 [USB2PHY_PORT_HOST] = {
3613 .phy_sus = { 0xe468, 1, 0, 0x2, 0x1 },
3614 .ls_det_en = { 0xe3c0, 11, 11, 0, 1 },
3615 .ls_det_st = { 0xe3e0, 11, 11, 0, 1 },
3616 .ls_det_clr = { 0xe3d0, 11, 11, 0, 1 },
3617 .utmi_ls = { 0xe2ac, 26, 25, 0, 1 },
3618 .utmi_hstdet = { 0xe2ac, 27, 27, 0, 1 }
3619 }
3620 },
3621 .chg_det = {
3622 .chg_mode = { 0xe464, 8, 0, 0, 0x1d7 },
3623 .cp_det = { 0xe2ac, 5, 5, 0, 1 },
3624 .dcp_det = { 0xe2ac, 4, 4, 0, 1 },
3625 .dp_det = { 0xe2ac, 3, 3, 0, 1 },
3626 .idm_sink_en = { 0xe460, 8, 8, 0, 1 },
3627 .idp_sink_en = { 0xe460, 7, 7, 0, 1 },
3628 .idp_src_en = { 0xe460, 9, 9, 0, 1 },
3629 .rdm_pdwn_en = { 0xe460, 10, 10, 0, 1 },
3630 .vdm_src_en = { 0xe460, 12, 12, 0, 1 },
3631 .vdp_src_en = { 0xe460, 11, 11, 0, 1 },
3632 },
3633 },
3634 { /* sentinel */ }
3635 };
3636
3637 static const struct rockchip_usb2phy_cfg rk3528_phy_cfgs[] = {
3638 {
3639 .reg = 0xffdf0000,
3640 .num_ports = 2,
3641 .phy_tuning = rk3528_usb2phy_tuning,
3642 .vbus_detect = rockchip_usb2phy_vbus_det_control,
3643 .clkout_ctl_phy = { 0x041c, 7, 2, 0, 0x27 },
3644 .port_cfgs = {
3645 [USB2PHY_PORT_OTG] = {
3646 .phy_sus = { 0x6004c, 8, 0, 0, 0x1d1 },
3647 .bvalid_det_en = { 0x60074, 2, 2, 0, 1 },
3648 .bvalid_det_st = { 0x60078, 2, 2, 0, 1 },
3649 .bvalid_det_clr = { 0x6007c, 2, 2, 0, 1 },
3650 .iddig_output = { 0x6004c, 10, 10, 0, 1 },
3651 .iddig_en = { 0x6004c, 9, 9, 0, 1 },
3652 .idfall_det_en = { 0x60074, 5, 5, 0, 1 },
3653 .idfall_det_st = { 0x60078, 5, 5, 0, 1 },
3654 .idfall_det_clr = { 0x6007c, 5, 5, 0, 1 },
3655 .idrise_det_en = { 0x60074, 4, 4, 0, 1 },
3656 .idrise_det_st = { 0x60078, 4, 4, 0, 1 },
3657 .idrise_det_clr = { 0x6007c, 4, 4, 0, 1 },
3658 .ls_det_en = { 0x60074, 0, 0, 0, 1 },
3659 .ls_det_st = { 0x60078, 0, 0, 0, 1 },
3660 .ls_det_clr = { 0x6007c, 0, 0, 0, 1 },
3661 .utmi_avalid = { 0x6006c, 1, 1, 0, 1 },
3662 .utmi_bvalid = { 0x6006c, 0, 0, 0, 1 },
3663 .utmi_iddig = { 0x6006c, 6, 6, 0, 1 },
3664 .utmi_ls = { 0x6006c, 5, 4, 0, 1 },
3665 .vbus_det_en = { 0x003c, 7, 7, 0, 1 },
3666 .port_ls_filter_con = { 0x60080, 19, 0, 0x30100, 0x20 },
3667 },
3668 [USB2PHY_PORT_HOST] = {
3669 .phy_sus = { 0x6005c, 8, 0, 0x1d2, 0x1d1 },
3670 .ls_det_en = { 0x60090, 0, 0, 0, 1 },
3671 .ls_det_st = { 0x60094, 0, 0, 0, 1 },
3672 .ls_det_clr = { 0x60098, 0, 0, 0, 1 },
3673 .utmi_ls = { 0x6006c, 13, 12, 0, 1 },
3674 .utmi_hstdet = { 0x6006c, 15, 15, 0, 1 },
3675 .port_ls_filter_con = { 0x6009c, 19, 0, 0x30100, 0x20 },
3676 }
3677 },
3678 .chg_det = {
3679 .chg_mode = { 0x6004c, 8, 0, 0, 0x1d7 },
3680 .cp_det = { 0x6006c, 19, 19, 0, 1 },
3681 .dcp_det = { 0x6006c, 18, 18, 0, 1 },
3682 .dp_det = { 0x6006c, 20, 20, 0, 1 },
3683 .idm_sink_en = { 0x60058, 1, 1, 0, 1 },
3684 .idp_sink_en = { 0x60058, 0, 0, 0, 1 },
3685 .idp_src_en = { 0x60058, 2, 2, 0, 1 },
3686 .rdm_pdwn_en = { 0x60058, 3, 3, 0, 1 },
3687 .vdm_src_en = { 0x60058, 5, 5, 0, 1 },
3688 .vdp_src_en = { 0x60058, 4, 4, 0, 1 },
3689 },
3690 }
3691 };
3692
3693 static const struct rockchip_usb2phy_cfg rk3562_phy_cfgs[] = {
3694 {
3695 .reg = 0xff740000,
3696 .num_ports = 2,
3697 .phy_tuning = rk3562_usb2phy_tuning,
3698 .vbus_detect = rockchip_usb2phy_vbus_det_control,
3699 .clkout_ctl = { 0x0108, 4, 4, 1, 0 },
3700 .ls_filter_con = { 0x0130, 19, 0, 0x30100, 0x00020 },
3701 .port_cfgs = {
3702 [USB2PHY_PORT_OTG] = {
3703 .phy_sus = { 0x0100, 8, 0, 0, 0x1d1 },
3704 .bvalid_det_en = { 0x0110, 2, 2, 0, 1 },
3705 .bvalid_det_st = { 0x0114, 2, 2, 0, 1 },
3706 .bvalid_det_clr = { 0x0118, 2, 2, 0, 1 },
3707 .bvalid_grf_sel = { 0x0108, 15, 14, 0, 3 },
3708 .bypass_dm_en = { 0x0108, 2, 2, 0, 1},
3709 .bypass_sel = { 0x0108, 3, 3, 0, 1},
3710 .iddig_output = { 0x0100, 10, 10, 0, 1 },
3711 .iddig_en = { 0x0100, 9, 9, 0, 1 },
3712 .idfall_det_en = { 0x0110, 5, 5, 0, 1 },
3713 .idfall_det_st = { 0x0114, 5, 5, 0, 1 },
3714 .idfall_det_clr = { 0x0118, 5, 5, 0, 1 },
3715 .idrise_det_en = { 0x0110, 4, 4, 0, 1 },
3716 .idrise_det_st = { 0x0114, 4, 4, 0, 1 },
3717 .idrise_det_clr = { 0x0118, 4, 4, 0, 1 },
3718 .ls_det_en = { 0x0110, 0, 0, 0, 1 },
3719 .ls_det_st = { 0x0114, 0, 0, 0, 1 },
3720 .ls_det_clr = { 0x0118, 0, 0, 0, 1 },
3721 .utmi_avalid = { 0x0120, 10, 10, 0, 1 },
3722 .utmi_bvalid = { 0x0120, 9, 9, 0, 1 },
3723 .utmi_iddig = { 0x0120, 6, 6, 0, 1 },
3724 .utmi_ls = { 0x0120, 5, 4, 0, 1 },
3725 .vbus_det_en = { 0x003c, 7, 7, 0, 1 },
3726 },
3727 [USB2PHY_PORT_HOST] = {
3728 .phy_sus = { 0x0104, 8, 0, 0x1d2, 0x1d1 },
3729 .ls_det_en = { 0x0110, 1, 1, 0, 1 },
3730 .ls_det_st = { 0x0114, 1, 1, 0, 1 },
3731 .ls_det_clr = { 0x0118, 1, 1, 0, 1 },
3732 .utmi_ls = { 0x0120, 17, 16, 0, 1 },
3733 .utmi_hstdet = { 0x0120, 19, 19, 0, 1 }
3734 }
3735 },
3736 .chg_det = {
3737 .chg_mode = { 0x0100, 8, 0, 0, 0x1d7 },
3738 .cp_det = { 0x0120, 24, 24, 0, 1 },
3739 .dcp_det = { 0x0120, 23, 23, 0, 1 },
3740 .dp_det = { 0x0120, 25, 25, 0, 1 },
3741 .idm_sink_en = { 0x0108, 8, 8, 0, 1 },
3742 .idp_sink_en = { 0x0108, 7, 7, 0, 1 },
3743 .idp_src_en = { 0x0108, 9, 9, 0, 1 },
3744 .rdm_pdwn_en = { 0x0108, 10, 10, 0, 1 },
3745 .vdm_src_en = { 0x0108, 12, 12, 0, 1 },
3746 .vdp_src_en = { 0x0108, 11, 11, 0, 1 },
3747 },
3748 },
3749 { /* sentinel */ }
3750 };
3751
3752 static const struct rockchip_usb2phy_cfg rk3568_phy_cfgs[] = {
3753 {
3754 .reg = 0xfe8a0000,
3755 .num_ports = 2,
3756 .phy_tuning = rk3568_usb2phy_tuning,
3757 .vbus_detect = rockchip_usb2phy_vbus_det_control,
3758 .clkout_ctl = { 0x0008, 4, 4, 1, 0 },
3759 .ls_filter_con = { 0x0040, 19, 0, 0x30100, 0x00020 },
3760 .port_cfgs = {
3761 [USB2PHY_PORT_OTG] = {
3762 .phy_sus = { 0x0000, 8, 0, 0, 0x1d1 },
3763 .bvalid_det_en = { 0x0080, 2, 2, 0, 1 },
3764 .bvalid_det_st = { 0x0084, 2, 2, 0, 1 },
3765 .bvalid_det_clr = { 0x0088, 2, 2, 0, 1 },
3766 .bvalid_grf_sel = { 0x0008, 15, 14, 0, 3 },
3767 .bypass_dm_en = { 0x0008, 2, 2, 0, 1},
3768 .bypass_sel = { 0x0008, 3, 3, 0, 1},
3769 .iddig_output = { 0x0000, 10, 10, 0, 1 },
3770 .iddig_en = { 0x0000, 9, 9, 0, 1 },
3771 .idfall_det_en = { 0x0080, 5, 5, 0, 1 },
3772 .idfall_det_st = { 0x0084, 5, 5, 0, 1 },
3773 .idfall_det_clr = { 0x0088, 5, 5, 0, 1 },
3774 .idrise_det_en = { 0x0080, 4, 4, 0, 1 },
3775 .idrise_det_st = { 0x0084, 4, 4, 0, 1 },
3776 .idrise_det_clr = { 0x0088, 4, 4, 0, 1 },
3777 .ls_det_en = { 0x0080, 0, 0, 0, 1 },
3778 .ls_det_st = { 0x0084, 0, 0, 0, 1 },
3779 .ls_det_clr = { 0x0088, 0, 0, 0, 1 },
3780 .utmi_avalid = { 0x00c0, 10, 10, 0, 1 },
3781 .utmi_bvalid = { 0x00c0, 9, 9, 0, 1 },
3782 .utmi_iddig = { 0x00c0, 6, 6, 0, 1 },
3783 .utmi_ls = { 0x00c0, 5, 4, 0, 1 },
3784 .vbus_det_en = { 0x003c, 7, 7, 0, 1 },
3785 },
3786 [USB2PHY_PORT_HOST] = {
3787 /* Select suspend control from controller */
3788 .phy_sus = { 0x0004, 8, 0, 0x1d2, 0x1d2 },
3789 .ls_det_en = { 0x0080, 1, 1, 0, 1 },
3790 .ls_det_st = { 0x0084, 1, 1, 0, 1 },
3791 .ls_det_clr = { 0x0088, 1, 1, 0, 1 },
3792 .utmi_ls = { 0x00c0, 17, 16, 0, 1 },
3793 .utmi_hstdet = { 0x00c0, 19, 19, 0, 1 }
3794 }
3795 },
3796 .chg_det = {
3797 .chg_mode = { 0x0000, 8, 0, 0, 0x1d7 },
3798 .cp_det = { 0x00c0, 24, 24, 0, 1 },
3799 .dcp_det = { 0x00c0, 23, 23, 0, 1 },
3800 .dp_det = { 0x00c0, 25, 25, 0, 1 },
3801 .idm_sink_en = { 0x0008, 8, 8, 0, 1 },
3802 .idp_sink_en = { 0x0008, 7, 7, 0, 1 },
3803 .idp_src_en = { 0x0008, 9, 9, 0, 1 },
3804 .rdm_pdwn_en = { 0x0008, 10, 10, 0, 1 },
3805 .vdm_src_en = { 0x0008, 12, 12, 0, 1 },
3806 .vdp_src_en = { 0x0008, 11, 11, 0, 1 },
3807 },
3808 },
3809 {
3810 .reg = 0xfe8b0000,
3811 .num_ports = 2,
3812 .phy_tuning = rk3568_usb2phy_tuning,
3813 .clkout_ctl = { 0x0008, 4, 4, 1, 0 },
3814 .ls_filter_con = { 0x0040, 19, 0, 0x30100, 0x00020 },
3815 .port_cfgs = {
3816 [USB2PHY_PORT_OTG] = {
3817 .phy_sus = { 0x0000, 8, 0, 0x1d2, 0x1d1 },
3818 .ls_det_en = { 0x0080, 0, 0, 0, 1 },
3819 .ls_det_st = { 0x0084, 0, 0, 0, 1 },
3820 .ls_det_clr = { 0x0088, 0, 0, 0, 1 },
3821 .utmi_ls = { 0x00c0, 5, 4, 0, 1 },
3822 .utmi_hstdet = { 0x00c0, 7, 7, 0, 1 }
3823 },
3824 [USB2PHY_PORT_HOST] = {
3825 .phy_sus = { 0x0004, 8, 0, 0x1d2, 0x1d1 },
3826 .ls_det_en = { 0x0080, 1, 1, 0, 1 },
3827 .ls_det_st = { 0x0084, 1, 1, 0, 1 },
3828 .ls_det_clr = { 0x0088, 1, 1, 0, 1 },
3829 .utmi_ls = { 0x00c0, 17, 16, 0, 1 },
3830 .utmi_hstdet = { 0x00c0, 19, 19, 0, 1 }
3831 }
3832 },
3833 },
3834 { /* sentinel */ }
3835 };
3836
3837 static const struct rockchip_usb2phy_cfg rk3588_phy_cfgs[] = {
3838 {
3839 .reg = 0x0000,
3840 .num_ports = 1,
3841 .phy_tuning = rk3588_usb2phy_tuning,
3842 .clkout_ctl = { 0x0000, 0, 0, 1, 0 },
3843 .ls_filter_con = { 0x0040, 19, 0, 0x30100, 0x00020 },
3844 .port_cfgs = {
3845 [USB2PHY_PORT_OTG] = {
3846 .phy_sus = { 0x000c, 11, 11, 0, 1 },
3847 .pipe_phystatus = { 0x001c, 3, 2, 0, 2 },
3848 .bvalid_det_en = { 0x0080, 1, 1, 0, 1 },
3849 .bvalid_det_st = { 0x0084, 1, 1, 0, 1 },
3850 .bvalid_det_clr = { 0x0088, 1, 1, 0, 1 },
3851 .bvalid_grf_sel = { 0x0010, 3, 3, 0, 1 },
3852 .bvalid_grf_con = { 0x0010, 3, 2, 2, 3 },
3853 .bvalid_phy_con = { 0x0008, 1, 0, 2, 3 },
3854 .bypass_dm_en = { 0x000c, 5, 5, 0, 1 },
3855 .bypass_sel = { 0x000c, 6, 6, 0, 1 },
3856 .iddig_output = { 0x0010, 0, 0, 0, 1 },
3857 .iddig_en = { 0x0010, 1, 1, 0, 1 },
3858 .idfall_det_en = { 0x0080, 4, 4, 0, 1 },
3859 .idfall_det_st = { 0x0084, 4, 4, 0, 1 },
3860 .idfall_det_clr = { 0x0088, 4, 4, 0, 1 },
3861 .idrise_det_en = { 0x0080, 3, 3, 0, 1 },
3862 .idrise_det_st = { 0x0084, 3, 3, 0, 1 },
3863 .idrise_det_clr = { 0x0088, 3, 3, 0, 1 },
3864 .ls_det_en = { 0x0080, 0, 0, 0, 1 },
3865 .ls_det_st = { 0x0084, 0, 0, 0, 1 },
3866 .ls_det_clr = { 0x0088, 0, 0, 0, 1 },
3867 .disfall_en = { 0x0080, 6, 6, 0, 1 },
3868 .disfall_st = { 0x0084, 6, 6, 0, 1 },
3869 .disfall_clr = { 0x0088, 6, 6, 0, 1 },
3870 .disrise_en = { 0x0080, 5, 5, 0, 1 },
3871 .disrise_st = { 0x0084, 5, 5, 0, 1 },
3872 .disrise_clr = { 0x0088, 5, 5, 0, 1 },
3873 .utmi_avalid = { 0x00c0, 7, 7, 0, 1 },
3874 .utmi_bvalid = { 0x00c0, 6, 6, 0, 1 },
3875 .utmi_iddig = { 0x00c0, 5, 5, 0, 1 },
3876 .utmi_ls = { 0x00c0, 10, 9, 0, 1 },
3877 }
3878 },
3879 .chg_det = {
3880 .chg_mode = { 0x0008, 2, 2, 0, 1 },
3881 .cp_det = { 0x00c0, 0, 0, 0, 1 },
3882 .dcp_det = { 0x00c0, 0, 0, 0, 1 },
3883 .dp_det = { 0x00c0, 1, 1, 1, 0 },
3884 .idm_sink_en = { 0x0008, 5, 5, 1, 0 },
3885 .idp_sink_en = { 0x0008, 5, 5, 0, 1 },
3886 .idp_src_en = { 0x0008, 14, 14, 0, 1 },
3887 .rdm_pdwn_en = { 0x0008, 14, 14, 0, 1 },
3888 .vdm_src_en = { 0x0008, 7, 6, 0, 3 },
3889 .vdp_src_en = { 0x0008, 7, 6, 0, 3 },
3890 },
3891 },
3892 {
3893 .reg = 0x4000,
3894 .num_ports = 1,
3895 .phy_tuning = rk3588_usb2phy_tuning,
3896 .clkout_ctl = { 0x0000, 0, 0, 1, 0 },
3897 .ls_filter_con = { 0x0040, 19, 0, 0x30100, 0x00020 },
3898 .port_cfgs = {
3899 [USB2PHY_PORT_OTG] = {
3900 .phy_sus = { 0x000c, 11, 11, 0, 1 },
3901 .pipe_phystatus = { 0x0034, 3, 2, 0, 2 },
3902 .bvalid_det_en = { 0x0080, 1, 1, 0, 1 },
3903 .bvalid_det_st = { 0x0084, 1, 1, 0, 1 },
3904 .bvalid_det_clr = { 0x0088, 1, 1, 0, 1 },
3905 .bvalid_grf_sel = { 0x0010, 3, 3, 0, 1 },
3906 .bvalid_grf_con = { 0x0010, 3, 2, 2, 3 },
3907 .bvalid_phy_con = { 0x0008, 1, 0, 2, 3 },
3908 .bypass_dm_en = { 0x000c, 5, 5, 0, 1 },
3909 .bypass_sel = { 0x000c, 6, 6, 0, 1 },
3910 .iddig_output = { 0x0010, 0, 0, 0, 1 },
3911 .iddig_en = { 0x0010, 1, 1, 0, 1 },
3912 .idfall_det_en = { 0x0080, 4, 4, 0, 1 },
3913 .idfall_det_st = { 0x0084, 4, 4, 0, 1 },
3914 .idfall_det_clr = { 0x0088, 4, 4, 0, 1 },
3915 .idrise_det_en = { 0x0080, 3, 3, 0, 1 },
3916 .idrise_det_st = { 0x0084, 3, 3, 0, 1 },
3917 .idrise_det_clr = { 0x0088, 3, 3, 0, 1 },
3918 .ls_det_en = { 0x0080, 0, 0, 0, 1 },
3919 .ls_det_st = { 0x0084, 0, 0, 0, 1 },
3920 .ls_det_clr = { 0x0088, 0, 0, 0, 1 },
3921 .disfall_en = { 0x0080, 6, 6, 0, 1 },
3922 .disfall_st = { 0x0084, 6, 6, 0, 1 },
3923 .disfall_clr = { 0x0088, 6, 6, 0, 1 },
3924 .disrise_en = { 0x0080, 5, 5, 0, 1 },
3925 .disrise_st = { 0x0084, 5, 5, 0, 1 },
3926 .disrise_clr = { 0x0088, 5, 5, 0, 1 },
3927 .utmi_avalid = { 0x00c0, 7, 7, 0, 1 },
3928 .utmi_bvalid = { 0x00c0, 6, 6, 0, 1 },
3929 .utmi_iddig = { 0x00c0, 5, 5, 0, 1 },
3930 .utmi_ls = { 0x00c0, 10, 9, 0, 1 },
3931 }
3932 },
3933 .chg_det = {
3934 .chg_mode = { 0x0008, 2, 2, 0, 1 },
3935 .cp_det = { 0x00c0, 0, 0, 0, 1 },
3936 .dcp_det = { 0x00c0, 0, 0, 0, 1 },
3937 .dp_det = { 0x00c0, 1, 1, 1, 0 },
3938 .idm_sink_en = { 0x0008, 5, 5, 1, 0 },
3939 .idp_sink_en = { 0x0008, 5, 5, 0, 1 },
3940 .idp_src_en = { 0x0008, 14, 14, 0, 1 },
3941 .rdm_pdwn_en = { 0x0008, 14, 14, 0, 1 },
3942 .vdm_src_en = { 0x0008, 7, 6, 0, 3 },
3943 .vdp_src_en = { 0x0008, 7, 6, 0, 3 },
3944 },
3945 },
3946 {
3947 .reg = 0x8000,
3948 .num_ports = 1,
3949 .phy_tuning = rk3588_usb2phy_tuning,
3950 .clkout_ctl = { 0x0000, 0, 0, 0, 0 },
3951 .ls_filter_con = { 0x0040, 19, 0, 0x30100, 0x00020 },
3952 .port_cfgs = {
3953 [USB2PHY_PORT_HOST] = {
3954 .phy_sus = { 0x0008, 2, 2, 0, 1 },
3955 .ls_det_en = { 0x0080, 0, 0, 0, 1 },
3956 .ls_det_st = { 0x0084, 0, 0, 0, 1 },
3957 .ls_det_clr = { 0x0088, 0, 0, 0, 1 },
3958 .disfall_en = { 0x0080, 6, 6, 0, 1 },
3959 .disfall_st = { 0x0084, 6, 6, 0, 1 },
3960 .disfall_clr = { 0x0088, 6, 6, 0, 1 },
3961 .disrise_en = { 0x0080, 5, 5, 0, 1 },
3962 .disrise_st = { 0x0084, 5, 5, 0, 1 },
3963 .disrise_clr = { 0x0088, 5, 5, 0, 1 },
3964 .utmi_ls = { 0x00c0, 10, 9, 0, 1 },
3965 }
3966 },
3967 },
3968 {
3969 .reg = 0xc000,
3970 .num_ports = 1,
3971 .phy_tuning = rk3588_usb2phy_tuning,
3972 .clkout_ctl = { 0x0000, 0, 0, 0, 0 },
3973 .ls_filter_con = { 0x0040, 19, 0, 0x30100, 0x00020 },
3974 .port_cfgs = {
3975 [USB2PHY_PORT_HOST] = {
3976 .phy_sus = { 0x0008, 2, 2, 0, 1 },
3977 .ls_det_en = { 0x0080, 0, 0, 0, 1 },
3978 .ls_det_st = { 0x0084, 0, 0, 0, 1 },
3979 .ls_det_clr = { 0x0088, 0, 0, 0, 1 },
3980 .disfall_en = { 0x0080, 6, 6, 0, 1 },
3981 .disfall_st = { 0x0084, 6, 6, 0, 1 },
3982 .disfall_clr = { 0x0088, 6, 6, 0, 1 },
3983 .disrise_en = { 0x0080, 5, 5, 0, 1 },
3984 .disrise_st = { 0x0084, 5, 5, 0, 1 },
3985 .disrise_clr = { 0x0088, 5, 5, 0, 1 },
3986 .utmi_ls = { 0x00c0, 10, 9, 0, 1 },
3987 }
3988 },
3989 },
3990 { /* sentinel */ }
3991 };
3992
3993 static const struct rockchip_usb2phy_cfg rv1106_phy_cfgs[] = {
3994 {
3995 .reg = 0xff3e0000,
3996 .num_ports = 1,
3997 .phy_tuning = rv1106_usb2phy_tuning,
3998 .clkout_ctl = { 0x0058, 4, 4, 1, 0 },
3999 .port_cfgs = {
4000 [USB2PHY_PORT_OTG] = {
4001 .phy_sus = { 0x0050, 8, 0, 0, 0x1d1 },
4002 .bvalid_det_en = { 0x0100, 2, 2, 0, 1 },
4003 .bvalid_det_st = { 0x0104, 2, 2, 0, 1 },
4004 .bvalid_det_clr = { 0x0108, 2, 2, 0, 1 },
4005 .bvalid_grf_sel = { 0x0058, 15, 14, 0, 3 },
4006 .iddig_output = { 0x0050, 10, 10, 0, 1 },
4007 .iddig_en = { 0x0050, 9, 9, 0, 1 },
4008 .idfall_det_en = { 0x0100, 5, 5, 0, 1 },
4009 .idfall_det_st = { 0x0104, 5, 5, 0, 1 },
4010 .idfall_det_clr = { 0x0108, 5, 5, 0, 1 },
4011 .idrise_det_en = { 0x0100, 4, 4, 0, 1 },
4012 .idrise_det_st = { 0x0104, 4, 4, 0, 1 },
4013 .idrise_det_clr = { 0x0108, 4, 4, 0, 1 },
4014 .ls_det_en = { 0x0100, 0, 0, 0, 1 },
4015 .ls_det_st = { 0x0104, 0, 0, 0, 1 },
4016 .ls_det_clr = { 0x0108, 0, 0, 0, 1 },
4017 .utmi_avalid = { 0x0060, 10, 10, 0, 1 },
4018 .utmi_bvalid = { 0x0060, 9, 9, 0, 1 },
4019 .utmi_iddig = { 0x0060, 6, 6, 0, 1 },
4020 .utmi_ls = { 0x0060, 5, 4, 0, 1 },
4021 },
4022 },
4023 .chg_det = {
4024 .chg_mode = { 0x0050, 8, 0, 0, 0x1d7 },
4025 .cp_det = { 0x0060, 13, 13, 0, 1 },
4026 .dcp_det = { 0x0060, 12, 12, 0, 1 },
4027 .dp_det = { 0x0060, 14, 14, 0, 1 },
4028 .idm_sink_en = { 0x0058, 8, 8, 0, 1 },
4029 .idp_sink_en = { 0x0058, 7, 7, 0, 1 },
4030 .idp_src_en = { 0x0058, 9, 9, 0, 1 },
4031 .rdm_pdwn_en = { 0x0058, 10, 10, 0, 1 },
4032 .vdm_src_en = { 0x0058, 12, 12, 0, 1 },
4033 .vdp_src_en = { 0x0058, 11, 11, 0, 1 },
4034 },
4035 },
4036 { /* sentinel */ }
4037 };
4038
4039 static const struct rockchip_usb2phy_cfg rv1108_phy_cfgs[] = {
4040 {
4041 .reg = 0x100,
4042 .num_ports = 2,
4043 .clkout_ctl = { 0x108, 4, 4, 1, 0 },
4044 .port_cfgs = {
4045 [USB2PHY_PORT_OTG] = {
4046 .phy_sus = { 0x0100, 15, 0, 0, 0x1d1 },
4047 .bvalid_det_en = { 0x0680, 3, 3, 0, 1 },
4048 .bvalid_det_st = { 0x0690, 3, 3, 0, 1 },
4049 .bvalid_det_clr = { 0x06a0, 3, 3, 0, 1 },
4050 .ls_det_en = { 0x0680, 2, 2, 0, 1 },
4051 .ls_det_st = { 0x0690, 2, 2, 0, 1 },
4052 .ls_det_clr = { 0x06a0, 2, 2, 0, 1 },
4053 .utmi_bvalid = { 0x0804, 10, 10, 0, 1 },
4054 .utmi_ls = { 0x0804, 13, 12, 0, 1 },
4055 },
4056 [USB2PHY_PORT_HOST] = {
4057 .phy_sus = { 0x0104, 15, 0, 0, 0x1d1 },
4058 .ls_det_en = { 0x0680, 4, 4, 0, 1 },
4059 .ls_det_st = { 0x0690, 4, 4, 0, 1 },
4060 .ls_det_clr = { 0x06a0, 4, 4, 0, 1 },
4061 .utmi_ls = { 0x0804, 9, 8, 0, 1 },
4062 .utmi_hstdet = { 0x0804, 7, 7, 0, 1 }
4063 }
4064 },
4065 .chg_det = {
4066 .chg_mode = { 0x0100, 8, 0, 0, 0x1d7 },
4067 .cp_det = { 0x0804, 1, 1, 0, 1 },
4068 .dcp_det = { 0x0804, 0, 0, 0, 1 },
4069 .dp_det = { 0x0804, 2, 2, 0, 1 },
4070 .idm_sink_en = { 0x0108, 8, 8, 0, 1 },
4071 .idp_sink_en = { 0x0108, 7, 7, 0, 1 },
4072 .idp_src_en = { 0x0108, 9, 9, 0, 1 },
4073 .rdm_pdwn_en = { 0x0108, 10, 10, 0, 1 },
4074 .vdm_src_en = { 0x0108, 12, 12, 0, 1 },
4075 .vdp_src_en = { 0x0108, 11, 11, 0, 1 },
4076 },
4077 },
4078 { /* sentinel */ }
4079 };
4080
4081 static const struct of_device_id rockchip_usb2phy_dt_match[] = {
4082 #ifdef CONFIG_CPU_PX30
4083 { .compatible = "rockchip,px30-usb2phy", .data = &rk3328_phy_cfgs },
4084 #endif
4085 #ifdef CONFIG_CPU_RK1808
4086 { .compatible = "rockchip,rk1808-usb2phy", .data = &rk1808_phy_cfgs },
4087 #endif
4088 #ifdef CONFIG_CPU_RK312X
4089 { .compatible = "rockchip,rk3128-usb2phy", .data = &rk312x_phy_cfgs },
4090 #endif
4091 #ifdef CONFIG_CPU_RK322X
4092 { .compatible = "rockchip,rk3228-usb2phy", .data = &rk3228_phy_cfgs },
4093 #endif
4094 #ifdef CONFIG_CPU_RK3308
4095 { .compatible = "rockchip,rk3308-usb2phy", .data = &rk3308_phy_cfgs },
4096 #endif
4097 #ifdef CONFIG_CPU_RK3328
4098 { .compatible = "rockchip,rk3328-usb2phy", .data = &rk3328_phy_cfgs },
4099 #endif
4100 #ifdef CONFIG_CPU_RK3366
4101 { .compatible = "rockchip,rk3366-usb2phy", .data = &rk3366_phy_cfgs },
4102 #endif
4103 #ifdef CONFIG_CPU_RK3368
4104 { .compatible = "rockchip,rk3368-usb2phy", .data = &rk3368_phy_cfgs },
4105 #endif
4106 #ifdef CONFIG_CPU_RK3399
4107 { .compatible = "rockchip,rk3399-usb2phy", .data = &rk3399_phy_cfgs },
4108 #endif
4109 #ifdef CONFIG_CPU_RK3528
4110 { .compatible = "rockchip,rk3528-usb2phy", .data = &rk3528_phy_cfgs },
4111 #endif
4112 #ifdef CONFIG_CPU_RK3562
4113 { .compatible = "rockchip,rk3562-usb2phy", .data = &rk3562_phy_cfgs },
4114 #endif
4115 #ifdef CONFIG_CPU_RK3568
4116 { .compatible = "rockchip,rk3568-usb2phy", .data = &rk3568_phy_cfgs },
4117 #endif
4118 #ifdef CONFIG_CPU_RK3588
4119 { .compatible = "rockchip,rk3588-usb2phy", .data = &rk3588_phy_cfgs },
4120 #endif
4121 #ifdef CONFIG_CPU_RV1106
4122 { .compatible = "rockchip,rv1106-usb2phy", .data = &rv1106_phy_cfgs },
4123 #endif
4124 #ifdef CONFIG_CPU_RV1108
4125 { .compatible = "rockchip,rv1108-usb2phy", .data = &rv1108_phy_cfgs },
4126 #endif
4127 {}
4128 };
4129 MODULE_DEVICE_TABLE(of, rockchip_usb2phy_dt_match);
4130
4131 static struct platform_driver rockchip_usb2phy_driver = {
4132 .probe = rockchip_usb2phy_probe,
4133 .driver = {
4134 .name = "rockchip-usb2phy",
4135 .pm = ROCKCHIP_USB2PHY_DEV_PM,
4136 .of_match_table = rockchip_usb2phy_dt_match,
4137 },
4138 };
4139 module_platform_driver(rockchip_usb2phy_driver);
4140
4141 MODULE_AUTHOR("Frank Wang <frank.wang@rock-chips.com>");
4142 MODULE_DESCRIPTION("Rockchip USB2.0 PHY driver");
4143 MODULE_LICENSE("GPL v2");
4144