2 * Rockchip USB2.0 PHY with Innosilicon IP block driver
4 * Copyright (C) 2016 Fuzhou Rockchip Electronics Co., Ltd
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/clk.h>
18 #include <linux/clk-provider.h>
19 #include <linux/delay.h>
20 #include <linux/extcon.h>
21 #include <linux/interrupt.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/jiffies.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/mutex.h>
29 #include <linux/of_address.h>
30 #include <linux/of_irq.h>
31 #include <linux/of_platform.h>
32 #include <linux/phy/phy.h>
33 #include <linux/platform_device.h>
34 #include <linux/power_supply.h>
35 #include <linux/regmap.h>
36 #include <linux/mfd/syscon.h>
37 #include <linux/usb/of.h>
38 #include <linux/usb/otg.h>
39 #include <linux/wakelock.h>
41 #define BIT_WRITEABLE_SHIFT 16
42 #define SCHEDULE_DELAY (60 * HZ)
43 #define OTG_SCHEDULE_DELAY (2 * HZ)
45 struct rockchip_usb2phy;
47 enum rockchip_usb2phy_port_id {
53 enum rockchip_usb2phy_host_state {
54 PHY_STATE_HS_ONLINE = 0,
55 PHY_STATE_DISCONNECT = 1,
56 PHY_STATE_CONNECT = 2,
57 PHY_STATE_FS_LS_ONLINE = 4,
61 * Different states involved in USB charger detection.
62 * USB_CHG_STATE_UNDEFINED USB charger is not connected or detection
63 * process is not yet started.
64 * USB_CHG_STATE_WAIT_FOR_DCD Waiting for Data pins contact.
65 * USB_CHG_STATE_DCD_DONE Data pin contact is detected.
66 * USB_CHG_STATE_PRIMARY_DONE Primary detection is completed (Detects
67 * between SDP and DCP/CDP).
68 * USB_CHG_STATE_SECONDARY_DONE Secondary detection is completed (Detects
69 * between DCP and CDP).
70 * USB_CHG_STATE_DETECTED USB charger type is determined.
73 USB_CHG_STATE_UNDEFINED = 0,
74 USB_CHG_STATE_WAIT_FOR_DCD,
75 USB_CHG_STATE_DCD_DONE,
76 USB_CHG_STATE_PRIMARY_DONE,
77 USB_CHG_STATE_SECONDARY_DONE,
78 USB_CHG_STATE_DETECTED,
81 static const unsigned int rockchip_usb2phy_extcon_cable[] = {
95 unsigned int bitstart;
101 * struct rockchip_chg_det_reg: usb charger detect registers
102 * @cp_det: charging port detected successfully.
103 * @dcp_det: dedicated charging port detected successfully.
104 * @dp_det: assert data pin connect successfully.
105 * @idm_sink_en: open dm sink curren.
106 * @idp_sink_en: open dp sink current.
107 * @idp_src_en: open dm source current.
108 * @rdm_pdwn_en: open dm pull down resistor.
109 * @vdm_src_en: open dm voltage source.
110 * @vdp_src_en: open dp voltage source.
111 * @opmode: utmi operational mode.
113 struct rockchip_chg_det_reg {
114 struct usb2phy_reg cp_det;
115 struct usb2phy_reg dcp_det;
116 struct usb2phy_reg dp_det;
117 struct usb2phy_reg idm_sink_en;
118 struct usb2phy_reg idp_sink_en;
119 struct usb2phy_reg idp_src_en;
120 struct usb2phy_reg rdm_pdwn_en;
121 struct usb2phy_reg vdm_src_en;
122 struct usb2phy_reg vdp_src_en;
123 struct usb2phy_reg opmode;
127 * struct rockchip_usb2phy_port_cfg: usb-phy port configuration.
128 * @phy_sus: phy suspend register.
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 * @ls_det_en: linestate detection enable register.
133 * @ls_det_st: linestate detection state register.
134 * @ls_det_clr: linestate detection clear register.
135 * @idfall_det_en: id fall detection enable register.
136 * @idfall_det_st: id fall detection state register.
137 * @idfall_det_clr: id fall detection clear register.
138 * @idrise_det_en: id rise detection enable register.
139 * @idrise_det_st: id rise detection state register.
140 * @idrise_det_clr: id rise detection clear register.
141 * @utmi_avalid: utmi vbus avalid status register.
142 * @utmi_bvalid: utmi vbus bvalid status register.
143 * @utmi_iddig: otg port id pin status register.
144 * @utmi_ls: utmi linestate state register.
145 * @utmi_hstdet: utmi host disconnect register.
146 * @vbus_det_en: vbus detect function power down register.
148 struct rockchip_usb2phy_port_cfg {
149 struct usb2phy_reg phy_sus;
150 struct usb2phy_reg bvalid_det_en;
151 struct usb2phy_reg bvalid_det_st;
152 struct usb2phy_reg bvalid_det_clr;
153 struct usb2phy_reg ls_det_en;
154 struct usb2phy_reg ls_det_st;
155 struct usb2phy_reg ls_det_clr;
156 struct usb2phy_reg idfall_det_en;
157 struct usb2phy_reg idfall_det_st;
158 struct usb2phy_reg idfall_det_clr;
159 struct usb2phy_reg idrise_det_en;
160 struct usb2phy_reg idrise_det_st;
161 struct usb2phy_reg idrise_det_clr;
162 struct usb2phy_reg utmi_avalid;
163 struct usb2phy_reg utmi_bvalid;
164 struct usb2phy_reg utmi_iddig;
165 struct usb2phy_reg utmi_ls;
166 struct usb2phy_reg utmi_hstdet;
167 struct usb2phy_reg vbus_det_en;
171 * struct rockchip_usb2phy_cfg: usb-phy configuration.
172 * @reg: the address offset of grf for usb-phy config.
173 * @num_ports: specify how many ports that the phy has.
174 * @phy_tuning: phy default parameters tunning.
175 * @clkout_ctl: keep on/turn off output clk of phy.
176 * @chg_det: charger detection registers.
178 struct rockchip_usb2phy_cfg {
180 unsigned int num_ports;
181 int (*phy_tuning)(struct rockchip_usb2phy *);
182 struct usb2phy_reg clkout_ctl;
183 const struct rockchip_usb2phy_port_cfg port_cfgs[USB2PHY_NUM_PORTS];
184 const struct rockchip_chg_det_reg chg_det;
188 * struct rockchip_usb2phy_port: usb-phy port data.
189 * @port_id: flag for otg port or host port.
190 * @perip_connected: flag for periphyeral connect status.
191 * @suspended: phy suspended flag.
192 * @utmi_avalid: utmi avalid status usage flag.
193 * true - use avalid to get vbus status
194 * flase - use bvalid to get vbus status
195 * @vbus_attached: otg device vbus status.
196 * @vbus_always_on: otg vbus is always powered on.
197 * @bvalid_irq: IRQ number assigned for vbus valid rise detection.
198 * @ls_irq: IRQ number assigned for linestate detection.
199 * @id_irq: IRQ number assigned for id fall or rise detection.
200 * @mutex: for register updating in sm_work.
201 * @chg_work: charge detect work.
202 * @otg_sm_work: OTG state machine work.
203 * @sm_work: HOST state machine work.
204 * @phy_cfg: port register configuration, assigned by driver data.
205 * @event_nb: hold event notification callback.
206 * @wakelock: wake lock struct to prevent system suspend
207 * when USB is active.
208 * @state: define OTG enumeration states before device reset.
209 * @mode: the dr_mode of the controller.
211 struct rockchip_usb2phy_port {
213 unsigned int port_id;
214 bool perip_connected;
223 struct delayed_work chg_work;
224 struct delayed_work otg_sm_work;
225 struct delayed_work sm_work;
226 const struct rockchip_usb2phy_port_cfg *port_cfg;
227 struct notifier_block event_nb;
228 struct wake_lock wakelock;
229 enum usb_otg_state state;
230 enum usb_dr_mode mode;
234 * struct rockchip_usb2phy: usb2.0 phy driver data.
235 * @grf: General Register Files regmap.
236 * @clk: clock struct of phy input clk.
237 * @clk480m: clock struct of phy output clk.
238 * @clk_hw: clock struct of phy output clk management.
239 * @chg_state: states involved in USB charger detection.
240 * @chg_type: USB charger types.
241 * @dcd_retries: The retry count used to track Data contact
243 * @edev_self: represent the source of extcon.
244 * @edev: extcon device for notification registration
245 * @phy_cfg: phy register configuration, assigned by driver data.
246 * @ports: phy port instance.
248 struct rockchip_usb2phy {
253 struct clk_hw clk480m_hw;
254 enum usb_chg_state chg_state;
255 enum power_supply_type chg_type;
259 struct extcon_dev *edev;
260 const struct rockchip_usb2phy_cfg *phy_cfg;
261 struct rockchip_usb2phy_port ports[USB2PHY_NUM_PORTS];
264 static inline int property_enable(struct rockchip_usb2phy *rphy,
265 const struct usb2phy_reg *reg, bool en)
267 unsigned int val, mask, tmp;
269 tmp = en ? reg->enable : reg->disable;
270 mask = GENMASK(reg->bitend, reg->bitstart);
271 val = (tmp << reg->bitstart) | (mask << BIT_WRITEABLE_SHIFT);
273 return regmap_write(rphy->grf, reg->offset, val);
276 static inline bool property_enabled(struct rockchip_usb2phy *rphy,
277 const struct usb2phy_reg *reg)
280 unsigned int tmp, orig;
281 unsigned int mask = GENMASK(reg->bitend, reg->bitstart);
283 ret = regmap_read(rphy->grf, reg->offset, &orig);
287 tmp = (orig & mask) >> reg->bitstart;
288 return tmp == reg->enable;
291 static int rockchip_usb2phy_clk480m_prepare(struct clk_hw *hw)
293 struct rockchip_usb2phy *rphy =
294 container_of(hw, struct rockchip_usb2phy, clk480m_hw);
297 /* turn on 480m clk output if it is off */
298 if (!property_enabled(rphy, &rphy->phy_cfg->clkout_ctl)) {
299 ret = property_enable(rphy, &rphy->phy_cfg->clkout_ctl, true);
303 /* waiting for the clk become stable */
304 usleep_range(1200, 1300);
310 static void rockchip_usb2phy_clk480m_unprepare(struct clk_hw *hw)
312 struct rockchip_usb2phy *rphy =
313 container_of(hw, struct rockchip_usb2phy, clk480m_hw);
315 /* turn off 480m clk output */
316 property_enable(rphy, &rphy->phy_cfg->clkout_ctl, false);
319 static int rockchip_usb2phy_clk480m_prepared(struct clk_hw *hw)
321 struct rockchip_usb2phy *rphy =
322 container_of(hw, struct rockchip_usb2phy, clk480m_hw);
324 return property_enabled(rphy, &rphy->phy_cfg->clkout_ctl);
328 rockchip_usb2phy_clk480m_recalc_rate(struct clk_hw *hw,
329 unsigned long parent_rate)
334 static const struct clk_ops rockchip_usb2phy_clkout_ops = {
335 .prepare = rockchip_usb2phy_clk480m_prepare,
336 .unprepare = rockchip_usb2phy_clk480m_unprepare,
337 .is_prepared = rockchip_usb2phy_clk480m_prepared,
338 .recalc_rate = rockchip_usb2phy_clk480m_recalc_rate,
341 static void rockchip_usb2phy_clk480m_unregister(void *data)
343 struct rockchip_usb2phy *rphy = data;
345 of_clk_del_provider(rphy->dev->of_node);
346 clk_unregister(rphy->clk480m);
350 rockchip_usb2phy_clk480m_register(struct rockchip_usb2phy *rphy)
352 struct device_node *node = rphy->dev->of_node;
353 struct clk_init_data init;
354 const char *clk_name;
358 init.name = "clk_usbphy_480m";
359 init.ops = &rockchip_usb2phy_clkout_ops;
361 /* optional override of the clockname */
362 of_property_read_string(node, "clock-output-names", &init.name);
365 clk_name = __clk_get_name(rphy->clk);
366 init.parent_names = &clk_name;
367 init.num_parents = 1;
369 init.parent_names = NULL;
370 init.num_parents = 0;
373 rphy->clk480m_hw.init = &init;
375 /* register the clock */
376 rphy->clk480m = clk_register(rphy->dev, &rphy->clk480m_hw);
377 if (IS_ERR(rphy->clk480m)) {
378 ret = PTR_ERR(rphy->clk480m);
382 ret = of_clk_add_provider(node, of_clk_src_simple_get, rphy->clk480m);
384 goto err_clk_provider;
386 ret = devm_add_action(rphy->dev, rockchip_usb2phy_clk480m_unregister,
389 goto err_unreg_action;
394 of_clk_del_provider(node);
396 clk_unregister(rphy->clk480m);
401 static int rockchip_usb2phy_extcon_register(struct rockchip_usb2phy *rphy)
404 struct device_node *node = rphy->dev->of_node;
405 struct extcon_dev *edev;
407 if (of_property_read_bool(node, "extcon")) {
408 edev = extcon_get_edev_by_phandle(rphy->dev, 0);
410 if (PTR_ERR(edev) != -EPROBE_DEFER)
411 dev_err(rphy->dev, "Invalid or missing extcon\n");
412 return PTR_ERR(edev);
415 /* Initialize extcon device */
416 edev = devm_extcon_dev_allocate(rphy->dev,
417 rockchip_usb2phy_extcon_cable);
422 ret = devm_extcon_dev_register(rphy->dev, edev);
424 dev_err(rphy->dev, "failed to register extcon device\n");
428 rphy->edev_self = true;
436 static int rockchip_usb2phy_init(struct phy *phy)
438 struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
439 struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
442 mutex_lock(&rport->mutex);
444 if (rport->port_id == USB2PHY_PORT_OTG) {
445 if (rport->mode != USB_DR_MODE_HOST &&
446 !rport->vbus_always_on) {
447 /* clear bvalid status and enable bvalid detect irq */
448 ret = property_enable(rphy,
455 ret = property_enable(rphy,
462 if (rphy->edev_self) {
463 ret = property_enable(rphy,
470 ret = property_enable(rphy,
477 ret = property_enable(rphy,
484 ret = property_enable(rphy,
492 schedule_delayed_work(&rport->otg_sm_work,
495 /* If OTG works in host only mode, do nothing. */
496 dev_dbg(&rport->phy->dev, "mode %d\n", rport->mode);
498 } else if (rport->port_id == USB2PHY_PORT_HOST) {
499 /* clear linestate and enable linestate detect irq */
500 ret = property_enable(rphy, &rport->port_cfg->ls_det_clr, true);
504 ret = property_enable(rphy, &rport->port_cfg->ls_det_en, true);
508 schedule_delayed_work(&rport->sm_work, SCHEDULE_DELAY);
512 mutex_unlock(&rport->mutex);
516 static int rockchip_usb2phy_power_on(struct phy *phy)
518 struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
519 struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
522 dev_dbg(&rport->phy->dev, "port power on\n");
524 if (!rport->suspended)
527 ret = clk_prepare_enable(rphy->clk480m);
531 ret = property_enable(rphy, &rport->port_cfg->phy_sus, false);
535 rport->suspended = false;
539 static int rockchip_usb2phy_power_off(struct phy *phy)
541 struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
542 struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
545 dev_dbg(&rport->phy->dev, "port power off\n");
547 if (rport->suspended)
550 ret = property_enable(rphy, &rport->port_cfg->phy_sus, true);
554 rport->suspended = true;
555 clk_disable_unprepare(rphy->clk480m);
560 static int rockchip_usb2phy_exit(struct phy *phy)
562 struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
564 if (rport->port_id == USB2PHY_PORT_OTG &&
565 rport->mode != USB_DR_MODE_HOST &&
566 !rport->vbus_always_on)
567 cancel_delayed_work_sync(&rport->chg_work);
568 else if (rport->port_id == USB2PHY_PORT_HOST)
569 cancel_delayed_work_sync(&rport->sm_work);
574 static int rockchip_usb2phy_set_mode(struct phy *phy, enum phy_mode mode)
576 struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
577 struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
581 if (rport->port_id != USB2PHY_PORT_OTG ||
582 !rport->vbus_always_on)
586 case PHY_MODE_USB_OTG:
588 * In case of using vbus to detect connect state by u2phy,
589 * enable vbus detect on otg mode.
593 case PHY_MODE_USB_DEVICE:
596 case PHY_MODE_USB_HOST:
598 case PHY_MODE_INVALID:
602 dev_info(&rport->phy->dev, "illegal mode\n");
606 ret = property_enable(rphy, &rport->port_cfg->vbus_det_en, vbus_det_en);
610 static const struct phy_ops rockchip_usb2phy_ops = {
611 .init = rockchip_usb2phy_init,
612 .exit = rockchip_usb2phy_exit,
613 .power_on = rockchip_usb2phy_power_on,
614 .power_off = rockchip_usb2phy_power_off,
615 .set_mode = rockchip_usb2phy_set_mode,
616 .owner = THIS_MODULE,
619 static void rockchip_usb2phy_otg_sm_work(struct work_struct *work)
621 struct rockchip_usb2phy_port *rport =
622 container_of(work, struct rockchip_usb2phy_port,
624 struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
625 static unsigned int cable;
629 if (rport->utmi_avalid)
630 rport->vbus_attached =
631 property_enabled(rphy, &rport->port_cfg->utmi_avalid);
633 rport->vbus_attached =
634 property_enabled(rphy, &rport->port_cfg->utmi_bvalid);
637 delay = OTG_SCHEDULE_DELAY;
639 dev_dbg(&rport->phy->dev, "%s otg sm work\n",
640 usb_otg_state_string(rport->state));
642 switch (rport->state) {
643 case OTG_STATE_UNDEFINED:
644 rport->state = OTG_STATE_B_IDLE;
645 if (!rport->vbus_attached)
646 rockchip_usb2phy_power_off(rport->phy);
648 case OTG_STATE_B_IDLE:
649 if (extcon_get_cable_state_(rphy->edev, EXTCON_USB_HOST) > 0 ||
650 extcon_get_cable_state_(rphy->edev,
651 EXTCON_USB_VBUS_EN) > 0) {
652 dev_dbg(&rport->phy->dev, "usb otg host connect\n");
653 rport->state = OTG_STATE_A_HOST;
654 rockchip_usb2phy_power_on(rport->phy);
656 } else if (rport->vbus_attached) {
657 dev_dbg(&rport->phy->dev, "vbus_attach\n");
658 switch (rphy->chg_state) {
659 case USB_CHG_STATE_UNDEFINED:
660 schedule_delayed_work(&rport->chg_work, 0);
662 case USB_CHG_STATE_DETECTED:
663 switch (rphy->chg_type) {
664 case POWER_SUPPLY_TYPE_USB:
665 dev_dbg(&rport->phy->dev,
666 "sdp cable is connecetd\n");
667 wake_lock(&rport->wakelock);
668 cable = EXTCON_CHG_USB_SDP;
669 rockchip_usb2phy_power_on(rport->phy);
670 rport->state = OTG_STATE_B_PERIPHERAL;
671 rport->perip_connected = true;
674 case POWER_SUPPLY_TYPE_USB_DCP:
675 dev_dbg(&rport->phy->dev,
676 "dcp cable is connecetd\n");
677 cable = EXTCON_CHG_USB_DCP;
678 rockchip_usb2phy_power_off(rport->phy);
681 case POWER_SUPPLY_TYPE_USB_CDP:
682 dev_dbg(&rport->phy->dev,
683 "cdp cable is connecetd\n");
684 wake_lock(&rport->wakelock);
685 cable = EXTCON_CHG_USB_CDP;
686 rockchip_usb2phy_power_on(rport->phy);
687 rport->state = OTG_STATE_B_PERIPHERAL;
688 rport->perip_connected = true;
691 case POWER_SUPPLY_TYPE_USB_FLOATING:
692 dev_dbg(&rport->phy->dev,
693 "floating cable is connecetd\n");
694 cable = EXTCON_CHG_USB_DCP;
695 rockchip_usb2phy_power_off(rport->phy);
706 rphy->chg_state = USB_CHG_STATE_UNDEFINED;
707 rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
710 case OTG_STATE_B_PERIPHERAL:
711 if (!rport->vbus_attached) {
712 dev_dbg(&rport->phy->dev, "usb disconnect\n");
713 rphy->chg_state = USB_CHG_STATE_UNDEFINED;
714 rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
715 rport->state = OTG_STATE_B_IDLE;
716 rport->perip_connected = false;
718 rockchip_usb2phy_power_off(rport->phy);
719 wake_unlock(&rport->wakelock);
724 case OTG_STATE_A_HOST:
725 if (extcon_get_cable_state_(rphy->edev, EXTCON_USB_HOST) == 0) {
726 dev_dbg(&rport->phy->dev, "usb otg host disconnect\n");
727 rport->state = OTG_STATE_B_IDLE;
728 rockchip_usb2phy_power_off(rport->phy);
735 if (extcon_get_state(rphy->edev, cable) != rport->vbus_attached)
736 extcon_set_cable_state_(rphy->edev,
737 cable, rport->vbus_attached);
739 if (rphy->edev_self &&
740 (extcon_get_state(rphy->edev, EXTCON_USB) !=
741 rport->perip_connected))
742 extcon_set_cable_state_(rphy->edev,
744 rport->perip_connected);
747 schedule_delayed_work(&rport->otg_sm_work, delay);
750 static const char *chg_to_string(enum power_supply_type chg_type)
753 case POWER_SUPPLY_TYPE_USB:
754 return "USB_SDP_CHARGER";
755 case POWER_SUPPLY_TYPE_USB_DCP:
756 return "USB_DCP_CHARGER";
757 case POWER_SUPPLY_TYPE_USB_CDP:
758 return "USB_CDP_CHARGER";
759 case POWER_SUPPLY_TYPE_USB_FLOATING:
760 return "USB_FLOATING_CHARGER";
762 return "INVALID_CHARGER";
766 static void rockchip_chg_enable_dcd(struct rockchip_usb2phy *rphy,
769 property_enable(rphy, &rphy->phy_cfg->chg_det.rdm_pdwn_en, en);
770 property_enable(rphy, &rphy->phy_cfg->chg_det.idp_src_en, en);
773 static void rockchip_chg_enable_primary_det(struct rockchip_usb2phy *rphy,
776 property_enable(rphy, &rphy->phy_cfg->chg_det.vdp_src_en, en);
777 property_enable(rphy, &rphy->phy_cfg->chg_det.idm_sink_en, en);
780 static void rockchip_chg_enable_secondary_det(struct rockchip_usb2phy *rphy,
783 property_enable(rphy, &rphy->phy_cfg->chg_det.vdm_src_en, en);
784 property_enable(rphy, &rphy->phy_cfg->chg_det.idp_sink_en, en);
787 #define CHG_DCD_POLL_TIME (100 * HZ / 1000)
788 #define CHG_DCD_MAX_RETRIES 6
789 #define CHG_PRIMARY_DET_TIME (40 * HZ / 1000)
790 #define CHG_SECONDARY_DET_TIME (40 * HZ / 1000)
791 static void rockchip_chg_detect_work(struct work_struct *work)
793 struct rockchip_usb2phy_port *rport =
794 container_of(work, struct rockchip_usb2phy_port, chg_work.work);
795 struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
796 bool is_dcd, tmout, vout;
799 dev_dbg(&rport->phy->dev, "chg detection work state = %d\n",
801 switch (rphy->chg_state) {
802 case USB_CHG_STATE_UNDEFINED:
803 if (!rport->suspended)
804 rockchip_usb2phy_power_off(rport->phy);
805 /* put the controller in non-driving mode */
806 property_enable(rphy, &rphy->phy_cfg->chg_det.opmode, false);
807 /* Start DCD processing stage 1 */
808 rockchip_chg_enable_dcd(rphy, true);
809 rphy->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
810 rphy->dcd_retries = 0;
811 rphy->primary_retries = 0;
812 delay = CHG_DCD_POLL_TIME;
814 case USB_CHG_STATE_WAIT_FOR_DCD:
815 /* get data contact detection status */
816 is_dcd = property_enabled(rphy, &rphy->phy_cfg->chg_det.dp_det);
817 tmout = ++rphy->dcd_retries == CHG_DCD_MAX_RETRIES;
819 if (is_dcd || tmout) {
821 /* Turn off DCD circuitry */
822 rockchip_chg_enable_dcd(rphy, false);
823 /* Voltage Source on DP, Probe on DM */
824 rockchip_chg_enable_primary_det(rphy, true);
825 delay = CHG_PRIMARY_DET_TIME;
826 rphy->chg_state = USB_CHG_STATE_DCD_DONE;
829 delay = CHG_DCD_POLL_TIME;
832 case USB_CHG_STATE_DCD_DONE:
833 vout = property_enabled(rphy, &rphy->phy_cfg->chg_det.cp_det);
834 rockchip_chg_enable_primary_det(rphy, false);
836 /* Voltage Source on DM, Probe on DP */
837 rockchip_chg_enable_secondary_det(rphy, true);
838 delay = CHG_SECONDARY_DET_TIME;
839 rphy->chg_state = USB_CHG_STATE_PRIMARY_DONE;
841 if (rphy->dcd_retries == CHG_DCD_MAX_RETRIES) {
842 /* floating charger found */
843 rphy->chg_type = POWER_SUPPLY_TYPE_USB_FLOATING;
844 rphy->chg_state = USB_CHG_STATE_DETECTED;
847 if (rphy->primary_retries < 2) {
848 /* Turn off DCD circuitry */
849 rockchip_chg_enable_dcd(rphy, false);
850 /* Voltage Source on DP, Probe on DM */
851 rockchip_chg_enable_primary_det(rphy,
853 delay = CHG_PRIMARY_DET_TIME;
855 USB_CHG_STATE_DCD_DONE;
856 rphy->primary_retries++;
857 /* break USB_CHG_STATE_DCD_DONE */
860 rphy->chg_type = POWER_SUPPLY_TYPE_USB;
861 rphy->chg_state = USB_CHG_STATE_DETECTED;
866 case USB_CHG_STATE_PRIMARY_DONE:
867 vout = property_enabled(rphy, &rphy->phy_cfg->chg_det.dcp_det);
868 /* Turn off voltage source */
869 rockchip_chg_enable_secondary_det(rphy, false);
871 rphy->chg_type = POWER_SUPPLY_TYPE_USB_DCP;
873 rphy->chg_type = POWER_SUPPLY_TYPE_USB_CDP;
875 case USB_CHG_STATE_SECONDARY_DONE:
876 rphy->chg_state = USB_CHG_STATE_DETECTED;
879 case USB_CHG_STATE_DETECTED:
880 /* put the controller in normal mode */
881 property_enable(rphy, &rphy->phy_cfg->chg_det.opmode, true);
882 rockchip_usb2phy_otg_sm_work(&rport->otg_sm_work.work);
883 dev_info(&rport->phy->dev, "charger = %s\n",
884 chg_to_string(rphy->chg_type));
890 schedule_delayed_work(&rport->chg_work, delay);
894 * The function manage host-phy port state and suspend/resume phy port
897 * we rely on utmi_linestate and utmi_hostdisconnect to identify whether
898 * devices is disconnect or not. Besides, we do not need care it is FS/LS
899 * disconnected or HS disconnected, actually, we just only need get the
900 * device is disconnected at last through rearm the delayed work,
901 * to suspend the phy port in _PHY_STATE_DISCONNECT_ case.
903 * NOTE: It may invoke *phy_powr_off or *phy_power_on which will invoke
904 * some clk related APIs, so do not invoke it from interrupt context directly.
906 static void rockchip_usb2phy_sm_work(struct work_struct *work)
908 struct rockchip_usb2phy_port *rport =
909 container_of(work, struct rockchip_usb2phy_port, sm_work.work);
910 struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
911 unsigned int sh = rport->port_cfg->utmi_hstdet.bitend -
912 rport->port_cfg->utmi_hstdet.bitstart + 1;
913 unsigned int ul, uhd, state;
914 unsigned int ul_mask, uhd_mask;
917 mutex_lock(&rport->mutex);
919 ret = regmap_read(rphy->grf, rport->port_cfg->utmi_ls.offset, &ul);
923 ret = regmap_read(rphy->grf, rport->port_cfg->utmi_hstdet.offset,
928 uhd_mask = GENMASK(rport->port_cfg->utmi_hstdet.bitend,
929 rport->port_cfg->utmi_hstdet.bitstart);
930 ul_mask = GENMASK(rport->port_cfg->utmi_ls.bitend,
931 rport->port_cfg->utmi_ls.bitstart);
933 /* stitch on utmi_ls and utmi_hstdet as phy state */
934 state = ((uhd & uhd_mask) >> rport->port_cfg->utmi_hstdet.bitstart) |
935 (((ul & ul_mask) >> rport->port_cfg->utmi_ls.bitstart) << sh);
938 case PHY_STATE_HS_ONLINE:
939 dev_dbg(&rport->phy->dev, "HS online\n");
941 case PHY_STATE_FS_LS_ONLINE:
943 * For FS/LS device, the online state share with connect state
944 * from utmi_ls and utmi_hstdet register, so we distinguish
945 * them via suspended flag.
947 * Plus, there are two cases, one is D- Line pull-up, and D+
948 * line pull-down, the state is 4; another is D+ line pull-up,
949 * and D- line pull-down, the state is 2.
951 if (!rport->suspended) {
952 /* D- line pull-up, D+ line pull-down */
953 dev_dbg(&rport->phy->dev, "FS/LS online\n");
957 case PHY_STATE_CONNECT:
958 if (rport->suspended) {
959 dev_dbg(&rport->phy->dev, "Connected\n");
960 rockchip_usb2phy_power_on(rport->phy);
961 rport->suspended = false;
963 /* D+ line pull-up, D- line pull-down */
964 dev_dbg(&rport->phy->dev, "FS/LS online\n");
967 case PHY_STATE_DISCONNECT:
968 if (!rport->suspended) {
969 dev_dbg(&rport->phy->dev, "Disconnected\n");
970 rockchip_usb2phy_power_off(rport->phy);
971 rport->suspended = true;
975 * activate the linestate detection to get the next device
978 property_enable(rphy, &rport->port_cfg->ls_det_clr, true);
979 property_enable(rphy, &rport->port_cfg->ls_det_en, true);
982 * we don't need to rearm the delayed work when the phy port
985 mutex_unlock(&rport->mutex);
988 dev_dbg(&rport->phy->dev, "unknown phy state\n");
993 mutex_unlock(&rport->mutex);
994 schedule_delayed_work(&rport->sm_work, SCHEDULE_DELAY);
997 static irqreturn_t rockchip_usb2phy_linestate_irq(int irq, void *data)
999 struct rockchip_usb2phy_port *rport = data;
1000 struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
1002 if (!property_enabled(rphy, &rport->port_cfg->ls_det_st))
1005 dev_dbg(&rport->phy->dev, "linestate interrupt\n");
1007 mutex_lock(&rport->mutex);
1009 /* disable linestate detect irq and clear its status */
1010 property_enable(rphy, &rport->port_cfg->ls_det_en, false);
1011 property_enable(rphy, &rport->port_cfg->ls_det_clr, true);
1013 mutex_unlock(&rport->mutex);
1016 * In this case for host phy port, a new device is plugged in,
1017 * meanwhile, if the phy port is suspended, we need rearm the work to
1018 * resume it and mange its states; otherwise, we do nothing about that.
1020 if (rport->suspended) {
1021 if (rport->port_id == USB2PHY_PORT_HOST)
1022 rockchip_usb2phy_sm_work(&rport->sm_work.work);
1024 rockchip_usb2phy_power_on(rport->phy);
1030 static irqreturn_t rockchip_usb2phy_bvalid_irq(int irq, void *data)
1032 struct rockchip_usb2phy_port *rport = data;
1033 struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
1035 if (!property_enabled(rphy, &rport->port_cfg->bvalid_det_st))
1038 mutex_lock(&rport->mutex);
1040 /* clear bvalid detect irq pending status */
1041 property_enable(rphy, &rport->port_cfg->bvalid_det_clr, true);
1043 mutex_unlock(&rport->mutex);
1045 rockchip_usb2phy_otg_sm_work(&rport->otg_sm_work.work);
1050 static irqreturn_t rockchip_usb2phy_id_irq(int irq, void *data)
1052 struct rockchip_usb2phy_port *rport = data;
1053 struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
1055 if (!property_enabled(rphy, &rport->port_cfg->idfall_det_st) &&
1056 !property_enabled(rphy, &rport->port_cfg->idrise_det_st))
1059 mutex_lock(&rport->mutex);
1061 /* clear id fall or rise detect irq pending status */
1062 if (property_enabled(rphy, &rport->port_cfg->idfall_det_st)) {
1063 property_enable(rphy, &rport->port_cfg->idfall_det_clr,
1065 extcon_set_state(rphy->edev, EXTCON_USB_HOST, true);
1066 extcon_set_state(rphy->edev, EXTCON_USB_VBUS_EN, true);
1067 } else if (property_enabled(rphy, &rport->port_cfg->idrise_det_st)) {
1068 property_enable(rphy, &rport->port_cfg->idrise_det_clr,
1070 extcon_set_state(rphy->edev, EXTCON_USB_HOST, false);
1071 extcon_set_state(rphy->edev, EXTCON_USB_VBUS_EN, false);
1074 extcon_sync(rphy->edev, EXTCON_USB_HOST);
1075 extcon_sync(rphy->edev, EXTCON_USB_VBUS_EN);
1077 mutex_unlock(&rport->mutex);
1082 static int rockchip_usb2phy_host_port_init(struct rockchip_usb2phy *rphy,
1083 struct rockchip_usb2phy_port *rport,
1084 struct device_node *child_np)
1088 rport->port_id = USB2PHY_PORT_HOST;
1089 rport->port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_HOST];
1090 rport->suspended = true;
1092 mutex_init(&rport->mutex);
1093 INIT_DELAYED_WORK(&rport->sm_work, rockchip_usb2phy_sm_work);
1095 rport->ls_irq = of_irq_get_byname(child_np, "linestate");
1096 if (rport->ls_irq < 0) {
1097 dev_err(rphy->dev, "no linestate irq provided\n");
1098 return rport->ls_irq;
1101 ret = devm_request_threaded_irq(rphy->dev, rport->ls_irq, NULL,
1102 rockchip_usb2phy_linestate_irq,
1104 "rockchip_usb2phy", rport);
1106 dev_err(rphy->dev, "failed to request linestate irq handle\n");
1113 static int rockchip_otg_event(struct notifier_block *nb,
1114 unsigned long event, void *ptr)
1116 struct rockchip_usb2phy_port *rport =
1117 container_of(nb, struct rockchip_usb2phy_port, event_nb);
1119 schedule_delayed_work(&rport->otg_sm_work, OTG_SCHEDULE_DELAY);
1124 static int rockchip_usb2phy_otg_port_init(struct rockchip_usb2phy *rphy,
1125 struct rockchip_usb2phy_port *rport,
1126 struct device_node *child_np)
1131 rport->port_id = USB2PHY_PORT_OTG;
1132 rport->port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_OTG];
1133 rport->state = OTG_STATE_UNDEFINED;
1136 * set suspended flag to true, but actually don't
1137 * put phy in suspend mode, it aims to enable usb
1138 * phy and clock in power_on() called by usb controller
1139 * driver during probe.
1141 rport->suspended = true;
1142 rport->vbus_attached = false;
1143 rport->perip_connected = false;
1145 mutex_init(&rport->mutex);
1147 rport->ls_irq = of_irq_get_byname(child_np, "linestate");
1148 if (rport->ls_irq < 0) {
1149 dev_err(rphy->dev, "no linestate irq provided\n");
1150 return rport->ls_irq;
1153 ret = devm_request_threaded_irq(rphy->dev, rport->ls_irq, NULL,
1154 rockchip_usb2phy_linestate_irq,
1156 "rockchip_usb2phy", rport);
1158 dev_err(rphy->dev, "failed to request linestate irq handle\n");
1162 rport->vbus_always_on =
1163 of_property_read_bool(child_np, "rockchip,vbus-always-on");
1165 rport->mode = of_usb_get_dr_mode_by_phy(child_np, -1);
1166 if (rport->mode == USB_DR_MODE_HOST || rport->vbus_always_on)
1169 wake_lock_init(&rport->wakelock, WAKE_LOCK_SUSPEND, "rockchip_otg");
1170 INIT_DELAYED_WORK(&rport->chg_work, rockchip_chg_detect_work);
1171 INIT_DELAYED_WORK(&rport->otg_sm_work, rockchip_usb2phy_otg_sm_work);
1173 rport->utmi_avalid =
1174 of_property_read_bool(child_np, "rockchip,utmi-avalid");
1176 rport->bvalid_irq = of_irq_get_byname(child_np, "otg-bvalid");
1177 if (rport->bvalid_irq < 0) {
1178 dev_err(rphy->dev, "no vbus valid irq provided\n");
1179 return rport->bvalid_irq;
1182 ret = devm_request_threaded_irq(rphy->dev, rport->bvalid_irq, NULL,
1183 rockchip_usb2phy_bvalid_irq,
1185 "rockchip_usb2phy_bvalid", rport);
1187 dev_err(rphy->dev, "failed to request otg-bvalid irq handle\n");
1191 if (rphy->edev_self) {
1192 rport->id_irq = of_irq_get_byname(child_np, "otg-id");
1193 if (rport->id_irq < 0) {
1194 dev_err(rphy->dev, "no otg id irq provided\n");
1195 return rport->id_irq;
1198 ret = devm_request_threaded_irq(rphy->dev, rport->id_irq, NULL,
1199 rockchip_usb2phy_id_irq,
1201 "rockchip_usb2phy_id", rport);
1203 dev_err(rphy->dev, "failed to request otg-id irq handle\n");
1207 iddig = property_enabled(rphy, &rport->port_cfg->utmi_iddig);
1209 extcon_set_state(rphy->edev, EXTCON_USB, false);
1210 extcon_set_state(rphy->edev, EXTCON_USB_HOST, true);
1211 extcon_set_state(rphy->edev, EXTCON_USB_VBUS_EN, true);
1213 extcon_set_state(rphy->edev, EXTCON_USB_HOST, false);
1214 extcon_set_state(rphy->edev, EXTCON_USB_VBUS_EN, false);
1218 if (!IS_ERR(rphy->edev)) {
1219 rport->event_nb.notifier_call = rockchip_otg_event;
1221 ret = extcon_register_notifier(rphy->edev, EXTCON_USB_HOST,
1224 dev_err(rphy->dev, "register USB HOST notifier failed\n");
1232 static int rockchip_usb2phy_probe(struct platform_device *pdev)
1234 struct device *dev = &pdev->dev;
1235 struct device_node *np = dev->of_node;
1236 struct device_node *child_np;
1237 struct phy_provider *provider;
1238 struct rockchip_usb2phy *rphy;
1239 const struct rockchip_usb2phy_cfg *phy_cfgs;
1240 const struct of_device_id *match;
1244 rphy = devm_kzalloc(dev, sizeof(*rphy), GFP_KERNEL);
1248 match = of_match_device(dev->driver->of_match_table, dev);
1249 if (!match || !match->data) {
1250 dev_err(dev, "phy configs are not assigned!\n");
1254 if (!dev->parent || !dev->parent->of_node)
1257 rphy->grf = syscon_node_to_regmap(dev->parent->of_node);
1258 if (IS_ERR(rphy->grf))
1259 return PTR_ERR(rphy->grf);
1261 if (of_property_read_u32(np, "reg", ®)) {
1262 dev_err(dev, "the reg property is not assigned in %s node\n",
1268 phy_cfgs = match->data;
1269 rphy->chg_state = USB_CHG_STATE_UNDEFINED;
1270 rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
1271 rphy->edev_self = false;
1272 platform_set_drvdata(pdev, rphy);
1274 ret = rockchip_usb2phy_extcon_register(rphy);
1278 /* find out a proper config which can be matched with dt. */
1280 while (phy_cfgs[index].reg) {
1281 if (phy_cfgs[index].reg == reg) {
1282 rphy->phy_cfg = &phy_cfgs[index];
1289 if (!rphy->phy_cfg) {
1290 dev_err(dev, "no phy-config can be matched with %s node\n",
1295 rphy->clk = of_clk_get_by_name(np, "phyclk");
1296 if (!IS_ERR(rphy->clk)) {
1297 clk_prepare_enable(rphy->clk);
1299 dev_info(&pdev->dev, "no phyclk specified\n");
1303 ret = rockchip_usb2phy_clk480m_register(rphy);
1305 dev_err(dev, "failed to register 480m output clock\n");
1309 if (rphy->phy_cfg->phy_tuning) {
1310 ret = rphy->phy_cfg->phy_tuning(rphy);
1316 for_each_available_child_of_node(np, child_np) {
1317 struct rockchip_usb2phy_port *rport = &rphy->ports[index];
1320 /* This driver aims to support both otg-port and host-port */
1321 if (of_node_cmp(child_np->name, "host-port") &&
1322 of_node_cmp(child_np->name, "otg-port"))
1325 phy = devm_phy_create(dev, child_np, &rockchip_usb2phy_ops);
1327 dev_err(dev, "failed to create phy\n");
1333 phy_set_drvdata(rport->phy, rport);
1335 /* initialize otg/host port separately */
1336 if (!of_node_cmp(child_np->name, "host-port")) {
1337 ret = rockchip_usb2phy_host_port_init(rphy, rport,
1342 ret = rockchip_usb2phy_otg_port_init(rphy, rport,
1349 /* to prevent out of boundary */
1350 if (++index >= rphy->phy_cfg->num_ports)
1354 provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
1355 return PTR_ERR_OR_ZERO(provider);
1358 of_node_put(child_np);
1361 clk_disable_unprepare(rphy->clk);
1367 static int rk3366_usb2phy_tuning(struct rockchip_usb2phy *rphy)
1369 unsigned int open_pre_emphasize = 0xffff851f;
1370 unsigned int eye_height_tuning = 0xffff68c8;
1371 unsigned int compensation_tuning = 0xffff026e;
1374 /* open HS pre-emphasize to expand HS slew rate for each port. */
1375 ret |= regmap_write(rphy->grf, 0x0780, open_pre_emphasize);
1376 ret |= regmap_write(rphy->grf, 0x079c, eye_height_tuning);
1377 ret |= regmap_write(rphy->grf, 0x07b0, open_pre_emphasize);
1378 ret |= regmap_write(rphy->grf, 0x07cc, eye_height_tuning);
1380 /* compensate default tuning reference relate to ODT and etc. */
1381 ret |= regmap_write(rphy->grf, 0x078c, compensation_tuning);
1386 static int rk3399_usb2phy_tuning(struct rockchip_usb2phy *rphy)
1388 struct device_node *node = rphy->dev->of_node;
1391 if (!of_property_read_bool(node, "rockchip,u2phy-tuning"))
1394 if (rphy->phy_cfg->reg == 0xe450) {
1396 * Set max ODT compensation voltage and
1397 * current tuning reference for PHY0.
1399 ret |= regmap_write(rphy->grf, 0x448c,
1400 GENMASK(23, 16) | 0xe3);
1402 /* Set max pre-emphasis level for PHY0 */
1403 ret |= regmap_write(rphy->grf, 0x44b0,
1404 GENMASK(18, 16) | 0x07);
1407 * Disable the pre-emphasize in eop state
1408 * and chirp state to avoid mis-trigger the
1409 * disconnect detection and also avoid hs
1410 * handshake fail for PHY0.
1412 ret |= regmap_write(rphy->grf, 0x4480,
1413 GENMASK(17, 16) | 0x0);
1414 ret |= regmap_write(rphy->grf, 0x44b4,
1415 GENMASK(17, 16) | 0x0);
1418 * Set max ODT compensation voltage and
1419 * current tuning reference for PHY1.
1421 ret |= regmap_write(rphy->grf, 0x450c,
1422 GENMASK(23, 16) | 0xe3);
1424 /* Set max pre-emphasis level for PHY1 */
1425 ret |= regmap_write(rphy->grf, 0x4530,
1426 GENMASK(18, 16) | 0x07);
1429 * Disable the pre-emphasize in eop state
1430 * and chirp state to avoid mis-trigger the
1431 * disconnect detection and also avoid hs
1432 * handshake fail for PHY1.
1434 ret |= regmap_write(rphy->grf, 0x4500,
1435 GENMASK(17, 16) | 0x0);
1436 ret |= regmap_write(rphy->grf, 0x4534,
1437 GENMASK(17, 16) | 0x0);
1443 #ifdef CONFIG_PM_SLEEP
1444 static int rockchip_usb2phy_pm_suspend(struct device *dev)
1446 struct rockchip_usb2phy *rphy = dev_get_drvdata(dev);
1447 struct rockchip_usb2phy_port *rport;
1450 for (index = 0; index < rphy->phy_cfg->num_ports; index++) {
1451 rport = &rphy->ports[index];
1455 /* activate the linestate to detect the next interrupt. */
1456 property_enable(rphy, &rport->port_cfg->ls_det_clr, true);
1457 property_enable(rphy, &rport->port_cfg->ls_det_en, true);
1463 static int rockchip_usb2phy_pm_resume(struct device *dev)
1468 static const struct dev_pm_ops rockchip_usb2phy_dev_pm_ops = {
1469 SET_SYSTEM_SLEEP_PM_OPS(rockchip_usb2phy_pm_suspend,
1470 rockchip_usb2phy_pm_resume)
1473 #define ROCKCHIP_USB2PHY_DEV_PM (&rockchip_usb2phy_dev_pm_ops)
1475 #define ROCKCHIP_USB2PHY_DEV_PM NULL
1476 #endif /* CONFIG_PM_SLEEP */
1478 static const struct rockchip_usb2phy_cfg rk3366_phy_cfgs[] = {
1482 .phy_tuning = rk3366_usb2phy_tuning,
1483 .clkout_ctl = { 0x0724, 15, 15, 1, 0 },
1485 [USB2PHY_PORT_HOST] = {
1486 .phy_sus = { 0x0728, 15, 0, 0, 0x1d1 },
1487 .ls_det_en = { 0x0680, 4, 4, 0, 1 },
1488 .ls_det_st = { 0x0690, 4, 4, 0, 1 },
1489 .ls_det_clr = { 0x06a0, 4, 4, 0, 1 },
1490 .utmi_ls = { 0x049c, 14, 13, 0, 1 },
1491 .utmi_hstdet = { 0x049c, 12, 12, 0, 1 }
1498 static const struct rockchip_usb2phy_cfg rk3399_phy_cfgs[] = {
1502 .phy_tuning = rk3399_usb2phy_tuning,
1503 .clkout_ctl = { 0xe450, 4, 4, 1, 0 },
1505 [USB2PHY_PORT_OTG] = {
1506 .phy_sus = { 0xe454, 15, 0, 0x1452, 0x15d1 },
1507 .bvalid_det_en = { 0xe3c0, 3, 3, 0, 1 },
1508 .bvalid_det_st = { 0xe3e0, 3, 3, 0, 1 },
1509 .bvalid_det_clr = { 0xe3d0, 3, 3, 0, 1 },
1510 .idfall_det_en = { 0xe3c0, 5, 5, 0, 1 },
1511 .idfall_det_st = { 0xe3e0, 5, 5, 0, 1 },
1512 .idfall_det_clr = { 0xe3d0, 5, 5, 0, 1 },
1513 .idrise_det_en = { 0xe3c0, 4, 4, 0, 1 },
1514 .idrise_det_st = { 0xe3e0, 4, 4, 0, 1 },
1515 .idrise_det_clr = { 0xe3d0, 4, 4, 0, 1 },
1516 .ls_det_en = { 0xe3c0, 2, 2, 0, 1 },
1517 .ls_det_st = { 0xe3e0, 2, 2, 0, 1 },
1518 .ls_det_clr = { 0xe3d0, 2, 2, 0, 1 },
1519 .utmi_avalid = { 0xe2ac, 7, 7, 0, 1 },
1520 .utmi_bvalid = { 0xe2ac, 12, 12, 0, 1 },
1521 .utmi_iddig = { 0xe2ac, 8, 8, 0, 1 },
1522 .utmi_ls = { 0xe2ac, 14, 13, 0, 1 },
1523 .vbus_det_en = { 0x449c, 15, 15, 1, 0 },
1525 [USB2PHY_PORT_HOST] = {
1526 .phy_sus = { 0xe458, 1, 0, 0x2, 0x1 },
1527 .ls_det_en = { 0xe3c0, 6, 6, 0, 1 },
1528 .ls_det_st = { 0xe3e0, 6, 6, 0, 1 },
1529 .ls_det_clr = { 0xe3d0, 6, 6, 0, 1 },
1530 .utmi_ls = { 0xe2ac, 22, 21, 0, 1 },
1531 .utmi_hstdet = { 0xe2ac, 23, 23, 0, 1 }
1535 .opmode = { 0xe454, 3, 0, 5, 1 },
1536 .cp_det = { 0xe2ac, 2, 2, 0, 1 },
1537 .dcp_det = { 0xe2ac, 1, 1, 0, 1 },
1538 .dp_det = { 0xe2ac, 0, 0, 0, 1 },
1539 .idm_sink_en = { 0xe450, 8, 8, 0, 1 },
1540 .idp_sink_en = { 0xe450, 7, 7, 0, 1 },
1541 .idp_src_en = { 0xe450, 9, 9, 0, 1 },
1542 .rdm_pdwn_en = { 0xe450, 10, 10, 0, 1 },
1543 .vdm_src_en = { 0xe450, 12, 12, 0, 1 },
1544 .vdp_src_en = { 0xe450, 11, 11, 0, 1 },
1550 .phy_tuning = rk3399_usb2phy_tuning,
1551 .clkout_ctl = { 0xe460, 4, 4, 1, 0 },
1553 [USB2PHY_PORT_OTG] = {
1554 .phy_sus = { 0xe464, 15, 0, 0x1452, 0x15d1 },
1555 .bvalid_det_en = { 0xe3c0, 8, 8, 0, 1 },
1556 .bvalid_det_st = { 0xe3e0, 8, 8, 0, 1 },
1557 .bvalid_det_clr = { 0xe3d0, 8, 8, 0, 1 },
1558 .idfall_det_en = { 0xe3c0, 10, 10, 0, 1 },
1559 .idfall_det_st = { 0xe3e0, 10, 10, 0, 1 },
1560 .idfall_det_clr = { 0xe3d0, 10, 10, 0, 1 },
1561 .idrise_det_en = { 0xe3c0, 9, 9, 0, 1 },
1562 .idrise_det_st = { 0xe3e0, 9, 9, 0, 1 },
1563 .idrise_det_clr = { 0xe3d0, 9, 9, 0, 1 },
1564 .ls_det_en = { 0xe3c0, 7, 7, 0, 1 },
1565 .ls_det_st = { 0xe3e0, 7, 7, 0, 1 },
1566 .ls_det_clr = { 0xe3d0, 7, 7, 0, 1 },
1567 .utmi_avalid = { 0xe2ac, 10, 10, 0, 1 },
1568 .utmi_bvalid = { 0xe2ac, 16, 16, 0, 1 },
1569 .utmi_iddig = { 0xe2ac, 11, 11, 0, 1 },
1570 .utmi_ls = { 0xe2ac, 18, 17, 0, 1 },
1571 .vbus_det_en = { 0x451c, 15, 15, 1, 0 },
1573 [USB2PHY_PORT_HOST] = {
1574 .phy_sus = { 0xe468, 1, 0, 0x2, 0x1 },
1575 .ls_det_en = { 0xe3c0, 11, 11, 0, 1 },
1576 .ls_det_st = { 0xe3e0, 11, 11, 0, 1 },
1577 .ls_det_clr = { 0xe3d0, 11, 11, 0, 1 },
1578 .utmi_ls = { 0xe2ac, 26, 25, 0, 1 },
1579 .utmi_hstdet = { 0xe2ac, 27, 27, 0, 1 }
1586 static const struct of_device_id rockchip_usb2phy_dt_match[] = {
1587 { .compatible = "rockchip,rk3366-usb2phy", .data = &rk3366_phy_cfgs },
1588 { .compatible = "rockchip,rk3399-usb2phy", .data = &rk3399_phy_cfgs },
1591 MODULE_DEVICE_TABLE(of, rockchip_usb2phy_dt_match);
1593 static struct platform_driver rockchip_usb2phy_driver = {
1594 .probe = rockchip_usb2phy_probe,
1596 .name = "rockchip-usb2phy",
1597 .pm = ROCKCHIP_USB2PHY_DEV_PM,
1598 .of_match_table = rockchip_usb2phy_dt_match,
1601 module_platform_driver(rockchip_usb2phy_driver);
1603 MODULE_AUTHOR("Frank Wang <frank.wang@rock-chips.com>");
1604 MODULE_DESCRIPTION("Rockchip USB2.0 PHY driver");
1605 MODULE_LICENSE("GPL v2");