18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Rockchip USB2.0 PHY with Innosilicon IP block driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2016 Fuzhou Rockchip Electronics Co., Ltd
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/clk.h>
98c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
108c2ecf20Sopenharmony_ci#include <linux/delay.h>
118c2ecf20Sopenharmony_ci#include <linux/extcon-provider.h>
128c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
138c2ecf20Sopenharmony_ci#include <linux/io.h>
148c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
158c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
168c2ecf20Sopenharmony_ci#include <linux/kernel.h>
178c2ecf20Sopenharmony_ci#include <linux/module.h>
188c2ecf20Sopenharmony_ci#include <linux/mutex.h>
198c2ecf20Sopenharmony_ci#include <linux/of.h>
208c2ecf20Sopenharmony_ci#include <linux/of_address.h>
218c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
228c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
238c2ecf20Sopenharmony_ci#include <linux/phy/phy.h>
248c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
258c2ecf20Sopenharmony_ci#include <linux/power_supply.h>
268c2ecf20Sopenharmony_ci#include <linux/regmap.h>
278c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
288c2ecf20Sopenharmony_ci#include <linux/usb/of.h>
298c2ecf20Sopenharmony_ci#include <linux/usb/otg.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define BIT_WRITEABLE_SHIFT	16
328c2ecf20Sopenharmony_ci#define SCHEDULE_DELAY		(60 * HZ)
338c2ecf20Sopenharmony_ci#define OTG_SCHEDULE_DELAY	(2 * HZ)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cienum rockchip_usb2phy_port_id {
368c2ecf20Sopenharmony_ci	USB2PHY_PORT_OTG,
378c2ecf20Sopenharmony_ci	USB2PHY_PORT_HOST,
388c2ecf20Sopenharmony_ci	USB2PHY_NUM_PORTS,
398c2ecf20Sopenharmony_ci};
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cienum rockchip_usb2phy_host_state {
428c2ecf20Sopenharmony_ci	PHY_STATE_HS_ONLINE	= 0,
438c2ecf20Sopenharmony_ci	PHY_STATE_DISCONNECT	= 1,
448c2ecf20Sopenharmony_ci	PHY_STATE_CONNECT	= 2,
458c2ecf20Sopenharmony_ci	PHY_STATE_FS_LS_ONLINE	= 4,
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/**
498c2ecf20Sopenharmony_ci * enum usb_chg_state - Different states involved in USB charger detection.
508c2ecf20Sopenharmony_ci * @USB_CHG_STATE_UNDEFINED:	USB charger is not connected or detection
518c2ecf20Sopenharmony_ci *				process is not yet started.
528c2ecf20Sopenharmony_ci * @USB_CHG_STATE_WAIT_FOR_DCD:	Waiting for Data pins contact.
538c2ecf20Sopenharmony_ci * @USB_CHG_STATE_DCD_DONE:	Data pin contact is detected.
548c2ecf20Sopenharmony_ci * @USB_CHG_STATE_PRIMARY_DONE:	Primary detection is completed (Detects
558c2ecf20Sopenharmony_ci *				between SDP and DCP/CDP).
568c2ecf20Sopenharmony_ci * @USB_CHG_STATE_SECONDARY_DONE: Secondary detection is completed (Detects
578c2ecf20Sopenharmony_ci *				  between DCP and CDP).
588c2ecf20Sopenharmony_ci * @USB_CHG_STATE_DETECTED:	USB charger type is determined.
598c2ecf20Sopenharmony_ci */
608c2ecf20Sopenharmony_cienum usb_chg_state {
618c2ecf20Sopenharmony_ci	USB_CHG_STATE_UNDEFINED = 0,
628c2ecf20Sopenharmony_ci	USB_CHG_STATE_WAIT_FOR_DCD,
638c2ecf20Sopenharmony_ci	USB_CHG_STATE_DCD_DONE,
648c2ecf20Sopenharmony_ci	USB_CHG_STATE_PRIMARY_DONE,
658c2ecf20Sopenharmony_ci	USB_CHG_STATE_SECONDARY_DONE,
668c2ecf20Sopenharmony_ci	USB_CHG_STATE_DETECTED,
678c2ecf20Sopenharmony_ci};
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic const unsigned int rockchip_usb2phy_extcon_cable[] = {
708c2ecf20Sopenharmony_ci	EXTCON_USB,
718c2ecf20Sopenharmony_ci	EXTCON_USB_HOST,
728c2ecf20Sopenharmony_ci	EXTCON_CHG_USB_SDP,
738c2ecf20Sopenharmony_ci	EXTCON_CHG_USB_CDP,
748c2ecf20Sopenharmony_ci	EXTCON_CHG_USB_DCP,
758c2ecf20Sopenharmony_ci	EXTCON_CHG_USB_SLOW,
768c2ecf20Sopenharmony_ci	EXTCON_NONE,
778c2ecf20Sopenharmony_ci};
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistruct usb2phy_reg {
808c2ecf20Sopenharmony_ci	unsigned int	offset;
818c2ecf20Sopenharmony_ci	unsigned int	bitend;
828c2ecf20Sopenharmony_ci	unsigned int	bitstart;
838c2ecf20Sopenharmony_ci	unsigned int	disable;
848c2ecf20Sopenharmony_ci	unsigned int	enable;
858c2ecf20Sopenharmony_ci};
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/**
888c2ecf20Sopenharmony_ci * struct rockchip_chg_det_reg - usb charger detect registers
898c2ecf20Sopenharmony_ci * @cp_det: charging port detected successfully.
908c2ecf20Sopenharmony_ci * @dcp_det: dedicated charging port detected successfully.
918c2ecf20Sopenharmony_ci * @dp_det: assert data pin connect successfully.
928c2ecf20Sopenharmony_ci * @idm_sink_en: open dm sink curren.
938c2ecf20Sopenharmony_ci * @idp_sink_en: open dp sink current.
948c2ecf20Sopenharmony_ci * @idp_src_en: open dm source current.
958c2ecf20Sopenharmony_ci * @rdm_pdwn_en: open dm pull down resistor.
968c2ecf20Sopenharmony_ci * @vdm_src_en: open dm voltage source.
978c2ecf20Sopenharmony_ci * @vdp_src_en: open dp voltage source.
988c2ecf20Sopenharmony_ci * @opmode: utmi operational mode.
998c2ecf20Sopenharmony_ci */
1008c2ecf20Sopenharmony_cistruct rockchip_chg_det_reg {
1018c2ecf20Sopenharmony_ci	struct usb2phy_reg	cp_det;
1028c2ecf20Sopenharmony_ci	struct usb2phy_reg	dcp_det;
1038c2ecf20Sopenharmony_ci	struct usb2phy_reg	dp_det;
1048c2ecf20Sopenharmony_ci	struct usb2phy_reg	idm_sink_en;
1058c2ecf20Sopenharmony_ci	struct usb2phy_reg	idp_sink_en;
1068c2ecf20Sopenharmony_ci	struct usb2phy_reg	idp_src_en;
1078c2ecf20Sopenharmony_ci	struct usb2phy_reg	rdm_pdwn_en;
1088c2ecf20Sopenharmony_ci	struct usb2phy_reg	vdm_src_en;
1098c2ecf20Sopenharmony_ci	struct usb2phy_reg	vdp_src_en;
1108c2ecf20Sopenharmony_ci	struct usb2phy_reg	opmode;
1118c2ecf20Sopenharmony_ci};
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/**
1148c2ecf20Sopenharmony_ci * struct rockchip_usb2phy_port_cfg - usb-phy port configuration.
1158c2ecf20Sopenharmony_ci * @phy_sus: phy suspend register.
1168c2ecf20Sopenharmony_ci * @bvalid_det_en: vbus valid rise detection enable register.
1178c2ecf20Sopenharmony_ci * @bvalid_det_st: vbus valid rise detection status register.
1188c2ecf20Sopenharmony_ci * @bvalid_det_clr: vbus valid rise detection clear register.
1198c2ecf20Sopenharmony_ci * @ls_det_en: linestate detection enable register.
1208c2ecf20Sopenharmony_ci * @ls_det_st: linestate detection state register.
1218c2ecf20Sopenharmony_ci * @ls_det_clr: linestate detection clear register.
1228c2ecf20Sopenharmony_ci * @utmi_avalid: utmi vbus avalid status register.
1238c2ecf20Sopenharmony_ci * @utmi_bvalid: utmi vbus bvalid status register.
1248c2ecf20Sopenharmony_ci * @utmi_ls: utmi linestate state register.
1258c2ecf20Sopenharmony_ci * @utmi_hstdet: utmi host disconnect register.
1268c2ecf20Sopenharmony_ci */
1278c2ecf20Sopenharmony_cistruct rockchip_usb2phy_port_cfg {
1288c2ecf20Sopenharmony_ci	struct usb2phy_reg	phy_sus;
1298c2ecf20Sopenharmony_ci	struct usb2phy_reg	bvalid_det_en;
1308c2ecf20Sopenharmony_ci	struct usb2phy_reg	bvalid_det_st;
1318c2ecf20Sopenharmony_ci	struct usb2phy_reg	bvalid_det_clr;
1328c2ecf20Sopenharmony_ci	struct usb2phy_reg	ls_det_en;
1338c2ecf20Sopenharmony_ci	struct usb2phy_reg	ls_det_st;
1348c2ecf20Sopenharmony_ci	struct usb2phy_reg	ls_det_clr;
1358c2ecf20Sopenharmony_ci	struct usb2phy_reg	utmi_avalid;
1368c2ecf20Sopenharmony_ci	struct usb2phy_reg	utmi_bvalid;
1378c2ecf20Sopenharmony_ci	struct usb2phy_reg	utmi_ls;
1388c2ecf20Sopenharmony_ci	struct usb2phy_reg	utmi_hstdet;
1398c2ecf20Sopenharmony_ci};
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci/**
1428c2ecf20Sopenharmony_ci * struct rockchip_usb2phy_cfg - usb-phy configuration.
1438c2ecf20Sopenharmony_ci * @reg: the address offset of grf for usb-phy config.
1448c2ecf20Sopenharmony_ci * @num_ports: specify how many ports that the phy has.
1458c2ecf20Sopenharmony_ci * @clkout_ctl: keep on/turn off output clk of phy.
1468c2ecf20Sopenharmony_ci * @port_cfgs: usb-phy port configurations.
1478c2ecf20Sopenharmony_ci * @chg_det: charger detection registers.
1488c2ecf20Sopenharmony_ci */
1498c2ecf20Sopenharmony_cistruct rockchip_usb2phy_cfg {
1508c2ecf20Sopenharmony_ci	unsigned int	reg;
1518c2ecf20Sopenharmony_ci	unsigned int	num_ports;
1528c2ecf20Sopenharmony_ci	struct usb2phy_reg	clkout_ctl;
1538c2ecf20Sopenharmony_ci	const struct rockchip_usb2phy_port_cfg	port_cfgs[USB2PHY_NUM_PORTS];
1548c2ecf20Sopenharmony_ci	const struct rockchip_chg_det_reg	chg_det;
1558c2ecf20Sopenharmony_ci};
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci/**
1588c2ecf20Sopenharmony_ci * struct rockchip_usb2phy_port - usb-phy port data.
1598c2ecf20Sopenharmony_ci * @phy: generic phy.
1608c2ecf20Sopenharmony_ci * @port_id: flag for otg port or host port.
1618c2ecf20Sopenharmony_ci * @suspended: phy suspended flag.
1628c2ecf20Sopenharmony_ci * @vbus_attached: otg device vbus status.
1638c2ecf20Sopenharmony_ci * @bvalid_irq: IRQ number assigned for vbus valid rise detection.
1648c2ecf20Sopenharmony_ci * @ls_irq: IRQ number assigned for linestate detection.
1658c2ecf20Sopenharmony_ci * @otg_mux_irq: IRQ number which multiplex otg-id/otg-bvalid/linestate
1668c2ecf20Sopenharmony_ci *		 irqs to one irq in otg-port.
1678c2ecf20Sopenharmony_ci * @mutex: for register updating in sm_work.
1688c2ecf20Sopenharmony_ci * @chg_work: charge detect work.
1698c2ecf20Sopenharmony_ci * @otg_sm_work: OTG state machine work.
1708c2ecf20Sopenharmony_ci * @sm_work: HOST state machine work.
1718c2ecf20Sopenharmony_ci * @port_cfg: port register configuration, assigned by driver data.
1728c2ecf20Sopenharmony_ci * @event_nb: hold event notification callback.
1738c2ecf20Sopenharmony_ci * @state: define OTG enumeration states before device reset.
1748c2ecf20Sopenharmony_ci * @mode: the dr_mode of the controller.
1758c2ecf20Sopenharmony_ci */
1768c2ecf20Sopenharmony_cistruct rockchip_usb2phy_port {
1778c2ecf20Sopenharmony_ci	struct phy	*phy;
1788c2ecf20Sopenharmony_ci	unsigned int	port_id;
1798c2ecf20Sopenharmony_ci	bool		suspended;
1808c2ecf20Sopenharmony_ci	bool		vbus_attached;
1818c2ecf20Sopenharmony_ci	int		bvalid_irq;
1828c2ecf20Sopenharmony_ci	int		ls_irq;
1838c2ecf20Sopenharmony_ci	int		otg_mux_irq;
1848c2ecf20Sopenharmony_ci	struct mutex	mutex;
1858c2ecf20Sopenharmony_ci	struct		delayed_work chg_work;
1868c2ecf20Sopenharmony_ci	struct		delayed_work otg_sm_work;
1878c2ecf20Sopenharmony_ci	struct		delayed_work sm_work;
1888c2ecf20Sopenharmony_ci	const struct	rockchip_usb2phy_port_cfg *port_cfg;
1898c2ecf20Sopenharmony_ci	struct notifier_block	event_nb;
1908c2ecf20Sopenharmony_ci	enum usb_otg_state	state;
1918c2ecf20Sopenharmony_ci	enum usb_dr_mode	mode;
1928c2ecf20Sopenharmony_ci};
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci/**
1958c2ecf20Sopenharmony_ci * struct rockchip_usb2phy - usb2.0 phy driver data.
1968c2ecf20Sopenharmony_ci * @dev: pointer to device.
1978c2ecf20Sopenharmony_ci * @grf: General Register Files regmap.
1988c2ecf20Sopenharmony_ci * @usbgrf: USB General Register Files regmap.
1998c2ecf20Sopenharmony_ci * @clk: clock struct of phy input clk.
2008c2ecf20Sopenharmony_ci * @clk480m: clock struct of phy output clk.
2018c2ecf20Sopenharmony_ci * @clk480m_hw: clock struct of phy output clk management.
2028c2ecf20Sopenharmony_ci * @chg_state: states involved in USB charger detection.
2038c2ecf20Sopenharmony_ci * @chg_type: USB charger types.
2048c2ecf20Sopenharmony_ci * @dcd_retries: The retry count used to track Data contact
2058c2ecf20Sopenharmony_ci *		 detection process.
2068c2ecf20Sopenharmony_ci * @edev: extcon device for notification registration
2078c2ecf20Sopenharmony_ci * @phy_cfg: phy register configuration, assigned by driver data.
2088c2ecf20Sopenharmony_ci * @ports: phy port instance.
2098c2ecf20Sopenharmony_ci */
2108c2ecf20Sopenharmony_cistruct rockchip_usb2phy {
2118c2ecf20Sopenharmony_ci	struct device	*dev;
2128c2ecf20Sopenharmony_ci	struct regmap	*grf;
2138c2ecf20Sopenharmony_ci	struct regmap	*usbgrf;
2148c2ecf20Sopenharmony_ci	struct clk	*clk;
2158c2ecf20Sopenharmony_ci	struct clk	*clk480m;
2168c2ecf20Sopenharmony_ci	struct clk_hw	clk480m_hw;
2178c2ecf20Sopenharmony_ci	enum usb_chg_state	chg_state;
2188c2ecf20Sopenharmony_ci	enum power_supply_type	chg_type;
2198c2ecf20Sopenharmony_ci	u8			dcd_retries;
2208c2ecf20Sopenharmony_ci	struct extcon_dev	*edev;
2218c2ecf20Sopenharmony_ci	const struct rockchip_usb2phy_cfg	*phy_cfg;
2228c2ecf20Sopenharmony_ci	struct rockchip_usb2phy_port	ports[USB2PHY_NUM_PORTS];
2238c2ecf20Sopenharmony_ci};
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic inline struct regmap *get_reg_base(struct rockchip_usb2phy *rphy)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	return rphy->usbgrf == NULL ? rphy->grf : rphy->usbgrf;
2288c2ecf20Sopenharmony_ci}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cistatic inline int property_enable(struct regmap *base,
2318c2ecf20Sopenharmony_ci				  const struct usb2phy_reg *reg, bool en)
2328c2ecf20Sopenharmony_ci{
2338c2ecf20Sopenharmony_ci	unsigned int val, mask, tmp;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	tmp = en ? reg->enable : reg->disable;
2368c2ecf20Sopenharmony_ci	mask = GENMASK(reg->bitend, reg->bitstart);
2378c2ecf20Sopenharmony_ci	val = (tmp << reg->bitstart) | (mask << BIT_WRITEABLE_SHIFT);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	return regmap_write(base, reg->offset, val);
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cistatic inline bool property_enabled(struct regmap *base,
2438c2ecf20Sopenharmony_ci				    const struct usb2phy_reg *reg)
2448c2ecf20Sopenharmony_ci{
2458c2ecf20Sopenharmony_ci	int ret;
2468c2ecf20Sopenharmony_ci	unsigned int tmp, orig;
2478c2ecf20Sopenharmony_ci	unsigned int mask = GENMASK(reg->bitend, reg->bitstart);
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	ret = regmap_read(base, reg->offset, &orig);
2508c2ecf20Sopenharmony_ci	if (ret)
2518c2ecf20Sopenharmony_ci		return false;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	tmp = (orig & mask) >> reg->bitstart;
2548c2ecf20Sopenharmony_ci	return tmp == reg->enable;
2558c2ecf20Sopenharmony_ci}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic int rockchip_usb2phy_clk480m_prepare(struct clk_hw *hw)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	struct rockchip_usb2phy *rphy =
2608c2ecf20Sopenharmony_ci		container_of(hw, struct rockchip_usb2phy, clk480m_hw);
2618c2ecf20Sopenharmony_ci	struct regmap *base = get_reg_base(rphy);
2628c2ecf20Sopenharmony_ci	int ret;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	/* turn on 480m clk output if it is off */
2658c2ecf20Sopenharmony_ci	if (!property_enabled(base, &rphy->phy_cfg->clkout_ctl)) {
2668c2ecf20Sopenharmony_ci		ret = property_enable(base, &rphy->phy_cfg->clkout_ctl, true);
2678c2ecf20Sopenharmony_ci		if (ret)
2688c2ecf20Sopenharmony_ci			return ret;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci		/* waiting for the clk become stable */
2718c2ecf20Sopenharmony_ci		usleep_range(1200, 1300);
2728c2ecf20Sopenharmony_ci	}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	return 0;
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cistatic void rockchip_usb2phy_clk480m_unprepare(struct clk_hw *hw)
2788c2ecf20Sopenharmony_ci{
2798c2ecf20Sopenharmony_ci	struct rockchip_usb2phy *rphy =
2808c2ecf20Sopenharmony_ci		container_of(hw, struct rockchip_usb2phy, clk480m_hw);
2818c2ecf20Sopenharmony_ci	struct regmap *base = get_reg_base(rphy);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	/* turn off 480m clk output */
2848c2ecf20Sopenharmony_ci	property_enable(base, &rphy->phy_cfg->clkout_ctl, false);
2858c2ecf20Sopenharmony_ci}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_cistatic int rockchip_usb2phy_clk480m_prepared(struct clk_hw *hw)
2888c2ecf20Sopenharmony_ci{
2898c2ecf20Sopenharmony_ci	struct rockchip_usb2phy *rphy =
2908c2ecf20Sopenharmony_ci		container_of(hw, struct rockchip_usb2phy, clk480m_hw);
2918c2ecf20Sopenharmony_ci	struct regmap *base = get_reg_base(rphy);
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	return property_enabled(base, &rphy->phy_cfg->clkout_ctl);
2948c2ecf20Sopenharmony_ci}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic unsigned long
2978c2ecf20Sopenharmony_cirockchip_usb2phy_clk480m_recalc_rate(struct clk_hw *hw,
2988c2ecf20Sopenharmony_ci				     unsigned long parent_rate)
2998c2ecf20Sopenharmony_ci{
3008c2ecf20Sopenharmony_ci	return 480000000;
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_cistatic const struct clk_ops rockchip_usb2phy_clkout_ops = {
3048c2ecf20Sopenharmony_ci	.prepare = rockchip_usb2phy_clk480m_prepare,
3058c2ecf20Sopenharmony_ci	.unprepare = rockchip_usb2phy_clk480m_unprepare,
3068c2ecf20Sopenharmony_ci	.is_prepared = rockchip_usb2phy_clk480m_prepared,
3078c2ecf20Sopenharmony_ci	.recalc_rate = rockchip_usb2phy_clk480m_recalc_rate,
3088c2ecf20Sopenharmony_ci};
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic void rockchip_usb2phy_clk480m_unregister(void *data)
3118c2ecf20Sopenharmony_ci{
3128c2ecf20Sopenharmony_ci	struct rockchip_usb2phy *rphy = data;
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	of_clk_del_provider(rphy->dev->of_node);
3158c2ecf20Sopenharmony_ci	clk_unregister(rphy->clk480m);
3168c2ecf20Sopenharmony_ci}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_cistatic int
3198c2ecf20Sopenharmony_cirockchip_usb2phy_clk480m_register(struct rockchip_usb2phy *rphy)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	struct device_node *node = rphy->dev->of_node;
3228c2ecf20Sopenharmony_ci	struct clk_init_data init;
3238c2ecf20Sopenharmony_ci	const char *clk_name;
3248c2ecf20Sopenharmony_ci	int ret;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	init.flags = 0;
3278c2ecf20Sopenharmony_ci	init.name = "clk_usbphy_480m";
3288c2ecf20Sopenharmony_ci	init.ops = &rockchip_usb2phy_clkout_ops;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	/* optional override of the clockname */
3318c2ecf20Sopenharmony_ci	of_property_read_string(node, "clock-output-names", &init.name);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	if (rphy->clk) {
3348c2ecf20Sopenharmony_ci		clk_name = __clk_get_name(rphy->clk);
3358c2ecf20Sopenharmony_ci		init.parent_names = &clk_name;
3368c2ecf20Sopenharmony_ci		init.num_parents = 1;
3378c2ecf20Sopenharmony_ci	} else {
3388c2ecf20Sopenharmony_ci		init.parent_names = NULL;
3398c2ecf20Sopenharmony_ci		init.num_parents = 0;
3408c2ecf20Sopenharmony_ci	}
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	rphy->clk480m_hw.init = &init;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	/* register the clock */
3458c2ecf20Sopenharmony_ci	rphy->clk480m = clk_register(rphy->dev, &rphy->clk480m_hw);
3468c2ecf20Sopenharmony_ci	if (IS_ERR(rphy->clk480m)) {
3478c2ecf20Sopenharmony_ci		ret = PTR_ERR(rphy->clk480m);
3488c2ecf20Sopenharmony_ci		goto err_ret;
3498c2ecf20Sopenharmony_ci	}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	ret = of_clk_add_provider(node, of_clk_src_simple_get, rphy->clk480m);
3528c2ecf20Sopenharmony_ci	if (ret < 0)
3538c2ecf20Sopenharmony_ci		goto err_clk_provider;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	ret = devm_add_action(rphy->dev, rockchip_usb2phy_clk480m_unregister,
3568c2ecf20Sopenharmony_ci			      rphy);
3578c2ecf20Sopenharmony_ci	if (ret < 0)
3588c2ecf20Sopenharmony_ci		goto err_unreg_action;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	return 0;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_cierr_unreg_action:
3638c2ecf20Sopenharmony_ci	of_clk_del_provider(node);
3648c2ecf20Sopenharmony_cierr_clk_provider:
3658c2ecf20Sopenharmony_ci	clk_unregister(rphy->clk480m);
3668c2ecf20Sopenharmony_cierr_ret:
3678c2ecf20Sopenharmony_ci	return ret;
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_cistatic int rockchip_usb2phy_extcon_register(struct rockchip_usb2phy *rphy)
3718c2ecf20Sopenharmony_ci{
3728c2ecf20Sopenharmony_ci	int ret;
3738c2ecf20Sopenharmony_ci	struct device_node *node = rphy->dev->of_node;
3748c2ecf20Sopenharmony_ci	struct extcon_dev *edev;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	if (of_property_read_bool(node, "extcon")) {
3778c2ecf20Sopenharmony_ci		edev = extcon_get_edev_by_phandle(rphy->dev, 0);
3788c2ecf20Sopenharmony_ci		if (IS_ERR(edev)) {
3798c2ecf20Sopenharmony_ci			if (PTR_ERR(edev) != -EPROBE_DEFER)
3808c2ecf20Sopenharmony_ci				dev_err(rphy->dev, "Invalid or missing extcon\n");
3818c2ecf20Sopenharmony_ci			return PTR_ERR(edev);
3828c2ecf20Sopenharmony_ci		}
3838c2ecf20Sopenharmony_ci	} else {
3848c2ecf20Sopenharmony_ci		/* Initialize extcon device */
3858c2ecf20Sopenharmony_ci		edev = devm_extcon_dev_allocate(rphy->dev,
3868c2ecf20Sopenharmony_ci						rockchip_usb2phy_extcon_cable);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci		if (IS_ERR(edev))
3898c2ecf20Sopenharmony_ci			return -ENOMEM;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci		ret = devm_extcon_dev_register(rphy->dev, edev);
3928c2ecf20Sopenharmony_ci		if (ret) {
3938c2ecf20Sopenharmony_ci			dev_err(rphy->dev, "failed to register extcon device\n");
3948c2ecf20Sopenharmony_ci			return ret;
3958c2ecf20Sopenharmony_ci		}
3968c2ecf20Sopenharmony_ci	}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	rphy->edev = edev;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	return 0;
4018c2ecf20Sopenharmony_ci}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_cistatic int rockchip_usb2phy_init(struct phy *phy)
4048c2ecf20Sopenharmony_ci{
4058c2ecf20Sopenharmony_ci	struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
4068c2ecf20Sopenharmony_ci	struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
4078c2ecf20Sopenharmony_ci	int ret = 0;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	mutex_lock(&rport->mutex);
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	if (rport->port_id == USB2PHY_PORT_OTG) {
4128c2ecf20Sopenharmony_ci		if (rport->mode != USB_DR_MODE_HOST &&
4138c2ecf20Sopenharmony_ci		    rport->mode != USB_DR_MODE_UNKNOWN) {
4148c2ecf20Sopenharmony_ci			/* clear bvalid status and enable bvalid detect irq */
4158c2ecf20Sopenharmony_ci			ret = property_enable(rphy->grf,
4168c2ecf20Sopenharmony_ci					      &rport->port_cfg->bvalid_det_clr,
4178c2ecf20Sopenharmony_ci					      true);
4188c2ecf20Sopenharmony_ci			if (ret)
4198c2ecf20Sopenharmony_ci				goto out;
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci			ret = property_enable(rphy->grf,
4228c2ecf20Sopenharmony_ci					      &rport->port_cfg->bvalid_det_en,
4238c2ecf20Sopenharmony_ci					      true);
4248c2ecf20Sopenharmony_ci			if (ret)
4258c2ecf20Sopenharmony_ci				goto out;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci			schedule_delayed_work(&rport->otg_sm_work,
4288c2ecf20Sopenharmony_ci					      OTG_SCHEDULE_DELAY * 3);
4298c2ecf20Sopenharmony_ci		} else {
4308c2ecf20Sopenharmony_ci			/* If OTG works in host only mode, do nothing. */
4318c2ecf20Sopenharmony_ci			dev_dbg(&rport->phy->dev, "mode %d\n", rport->mode);
4328c2ecf20Sopenharmony_ci		}
4338c2ecf20Sopenharmony_ci	} else if (rport->port_id == USB2PHY_PORT_HOST) {
4348c2ecf20Sopenharmony_ci		/* clear linestate and enable linestate detect irq */
4358c2ecf20Sopenharmony_ci		ret = property_enable(rphy->grf,
4368c2ecf20Sopenharmony_ci				      &rport->port_cfg->ls_det_clr, true);
4378c2ecf20Sopenharmony_ci		if (ret)
4388c2ecf20Sopenharmony_ci			goto out;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci		ret = property_enable(rphy->grf,
4418c2ecf20Sopenharmony_ci				      &rport->port_cfg->ls_det_en, true);
4428c2ecf20Sopenharmony_ci		if (ret)
4438c2ecf20Sopenharmony_ci			goto out;
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci		schedule_delayed_work(&rport->sm_work, SCHEDULE_DELAY);
4468c2ecf20Sopenharmony_ci	}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ciout:
4498c2ecf20Sopenharmony_ci	mutex_unlock(&rport->mutex);
4508c2ecf20Sopenharmony_ci	return ret;
4518c2ecf20Sopenharmony_ci}
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_cistatic int rockchip_usb2phy_power_on(struct phy *phy)
4548c2ecf20Sopenharmony_ci{
4558c2ecf20Sopenharmony_ci	struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
4568c2ecf20Sopenharmony_ci	struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
4578c2ecf20Sopenharmony_ci	struct regmap *base = get_reg_base(rphy);
4588c2ecf20Sopenharmony_ci	int ret;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	dev_dbg(&rport->phy->dev, "port power on\n");
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	if (!rport->suspended)
4638c2ecf20Sopenharmony_ci		return 0;
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(rphy->clk480m);
4668c2ecf20Sopenharmony_ci	if (ret)
4678c2ecf20Sopenharmony_ci		return ret;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	ret = property_enable(base, &rport->port_cfg->phy_sus, false);
4708c2ecf20Sopenharmony_ci	if (ret) {
4718c2ecf20Sopenharmony_ci		clk_disable_unprepare(rphy->clk480m);
4728c2ecf20Sopenharmony_ci		return ret;
4738c2ecf20Sopenharmony_ci	}
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	/* waiting for the utmi_clk to become stable */
4768c2ecf20Sopenharmony_ci	usleep_range(1500, 2000);
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	rport->suspended = false;
4798c2ecf20Sopenharmony_ci	return 0;
4808c2ecf20Sopenharmony_ci}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_cistatic int rockchip_usb2phy_power_off(struct phy *phy)
4838c2ecf20Sopenharmony_ci{
4848c2ecf20Sopenharmony_ci	struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
4858c2ecf20Sopenharmony_ci	struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
4868c2ecf20Sopenharmony_ci	struct regmap *base = get_reg_base(rphy);
4878c2ecf20Sopenharmony_ci	int ret;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	dev_dbg(&rport->phy->dev, "port power off\n");
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	if (rport->suspended)
4928c2ecf20Sopenharmony_ci		return 0;
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	ret = property_enable(base, &rport->port_cfg->phy_sus, true);
4958c2ecf20Sopenharmony_ci	if (ret)
4968c2ecf20Sopenharmony_ci		return ret;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	rport->suspended = true;
4998c2ecf20Sopenharmony_ci	clk_disable_unprepare(rphy->clk480m);
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	return 0;
5028c2ecf20Sopenharmony_ci}
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_cistatic int rockchip_usb2phy_exit(struct phy *phy)
5058c2ecf20Sopenharmony_ci{
5068c2ecf20Sopenharmony_ci	struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	if (rport->port_id == USB2PHY_PORT_OTG &&
5098c2ecf20Sopenharmony_ci	    rport->mode != USB_DR_MODE_HOST &&
5108c2ecf20Sopenharmony_ci	    rport->mode != USB_DR_MODE_UNKNOWN) {
5118c2ecf20Sopenharmony_ci		cancel_delayed_work_sync(&rport->otg_sm_work);
5128c2ecf20Sopenharmony_ci		cancel_delayed_work_sync(&rport->chg_work);
5138c2ecf20Sopenharmony_ci	} else if (rport->port_id == USB2PHY_PORT_HOST)
5148c2ecf20Sopenharmony_ci		cancel_delayed_work_sync(&rport->sm_work);
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	return 0;
5178c2ecf20Sopenharmony_ci}
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_cistatic const struct phy_ops rockchip_usb2phy_ops = {
5208c2ecf20Sopenharmony_ci	.init		= rockchip_usb2phy_init,
5218c2ecf20Sopenharmony_ci	.exit		= rockchip_usb2phy_exit,
5228c2ecf20Sopenharmony_ci	.power_on	= rockchip_usb2phy_power_on,
5238c2ecf20Sopenharmony_ci	.power_off	= rockchip_usb2phy_power_off,
5248c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
5258c2ecf20Sopenharmony_ci};
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_cistatic void rockchip_usb2phy_otg_sm_work(struct work_struct *work)
5288c2ecf20Sopenharmony_ci{
5298c2ecf20Sopenharmony_ci	struct rockchip_usb2phy_port *rport =
5308c2ecf20Sopenharmony_ci		container_of(work, struct rockchip_usb2phy_port,
5318c2ecf20Sopenharmony_ci			     otg_sm_work.work);
5328c2ecf20Sopenharmony_ci	struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
5338c2ecf20Sopenharmony_ci	static unsigned int cable;
5348c2ecf20Sopenharmony_ci	unsigned long delay;
5358c2ecf20Sopenharmony_ci	bool vbus_attach, sch_work, notify_charger;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	vbus_attach = property_enabled(rphy->grf,
5388c2ecf20Sopenharmony_ci				       &rport->port_cfg->utmi_bvalid);
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	sch_work = false;
5418c2ecf20Sopenharmony_ci	notify_charger = false;
5428c2ecf20Sopenharmony_ci	delay = OTG_SCHEDULE_DELAY;
5438c2ecf20Sopenharmony_ci	dev_dbg(&rport->phy->dev, "%s otg sm work\n",
5448c2ecf20Sopenharmony_ci		usb_otg_state_string(rport->state));
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	switch (rport->state) {
5478c2ecf20Sopenharmony_ci	case OTG_STATE_UNDEFINED:
5488c2ecf20Sopenharmony_ci		rport->state = OTG_STATE_B_IDLE;
5498c2ecf20Sopenharmony_ci		if (!vbus_attach)
5508c2ecf20Sopenharmony_ci			rockchip_usb2phy_power_off(rport->phy);
5518c2ecf20Sopenharmony_ci		fallthrough;
5528c2ecf20Sopenharmony_ci	case OTG_STATE_B_IDLE:
5538c2ecf20Sopenharmony_ci		if (extcon_get_state(rphy->edev, EXTCON_USB_HOST) > 0) {
5548c2ecf20Sopenharmony_ci			dev_dbg(&rport->phy->dev, "usb otg host connect\n");
5558c2ecf20Sopenharmony_ci			rport->state = OTG_STATE_A_HOST;
5568c2ecf20Sopenharmony_ci			rockchip_usb2phy_power_on(rport->phy);
5578c2ecf20Sopenharmony_ci			return;
5588c2ecf20Sopenharmony_ci		} else if (vbus_attach) {
5598c2ecf20Sopenharmony_ci			dev_dbg(&rport->phy->dev, "vbus_attach\n");
5608c2ecf20Sopenharmony_ci			switch (rphy->chg_state) {
5618c2ecf20Sopenharmony_ci			case USB_CHG_STATE_UNDEFINED:
5628c2ecf20Sopenharmony_ci				schedule_delayed_work(&rport->chg_work, 0);
5638c2ecf20Sopenharmony_ci				return;
5648c2ecf20Sopenharmony_ci			case USB_CHG_STATE_DETECTED:
5658c2ecf20Sopenharmony_ci				switch (rphy->chg_type) {
5668c2ecf20Sopenharmony_ci				case POWER_SUPPLY_TYPE_USB:
5678c2ecf20Sopenharmony_ci					dev_dbg(&rport->phy->dev, "sdp cable is connected\n");
5688c2ecf20Sopenharmony_ci					rockchip_usb2phy_power_on(rport->phy);
5698c2ecf20Sopenharmony_ci					rport->state = OTG_STATE_B_PERIPHERAL;
5708c2ecf20Sopenharmony_ci					notify_charger = true;
5718c2ecf20Sopenharmony_ci					sch_work = true;
5728c2ecf20Sopenharmony_ci					cable = EXTCON_CHG_USB_SDP;
5738c2ecf20Sopenharmony_ci					break;
5748c2ecf20Sopenharmony_ci				case POWER_SUPPLY_TYPE_USB_DCP:
5758c2ecf20Sopenharmony_ci					dev_dbg(&rport->phy->dev, "dcp cable is connected\n");
5768c2ecf20Sopenharmony_ci					rockchip_usb2phy_power_off(rport->phy);
5778c2ecf20Sopenharmony_ci					notify_charger = true;
5788c2ecf20Sopenharmony_ci					sch_work = true;
5798c2ecf20Sopenharmony_ci					cable = EXTCON_CHG_USB_DCP;
5808c2ecf20Sopenharmony_ci					break;
5818c2ecf20Sopenharmony_ci				case POWER_SUPPLY_TYPE_USB_CDP:
5828c2ecf20Sopenharmony_ci					dev_dbg(&rport->phy->dev, "cdp cable is connected\n");
5838c2ecf20Sopenharmony_ci					rockchip_usb2phy_power_on(rport->phy);
5848c2ecf20Sopenharmony_ci					rport->state = OTG_STATE_B_PERIPHERAL;
5858c2ecf20Sopenharmony_ci					notify_charger = true;
5868c2ecf20Sopenharmony_ci					sch_work = true;
5878c2ecf20Sopenharmony_ci					cable = EXTCON_CHG_USB_CDP;
5888c2ecf20Sopenharmony_ci					break;
5898c2ecf20Sopenharmony_ci				default:
5908c2ecf20Sopenharmony_ci					break;
5918c2ecf20Sopenharmony_ci				}
5928c2ecf20Sopenharmony_ci				break;
5938c2ecf20Sopenharmony_ci			default:
5948c2ecf20Sopenharmony_ci				break;
5958c2ecf20Sopenharmony_ci			}
5968c2ecf20Sopenharmony_ci		} else {
5978c2ecf20Sopenharmony_ci			notify_charger = true;
5988c2ecf20Sopenharmony_ci			rphy->chg_state = USB_CHG_STATE_UNDEFINED;
5998c2ecf20Sopenharmony_ci			rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
6008c2ecf20Sopenharmony_ci		}
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci		if (rport->vbus_attached != vbus_attach) {
6038c2ecf20Sopenharmony_ci			rport->vbus_attached = vbus_attach;
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci			if (notify_charger && rphy->edev) {
6068c2ecf20Sopenharmony_ci				extcon_set_state_sync(rphy->edev,
6078c2ecf20Sopenharmony_ci							cable, vbus_attach);
6088c2ecf20Sopenharmony_ci				if (cable == EXTCON_CHG_USB_SDP)
6098c2ecf20Sopenharmony_ci					extcon_set_state_sync(rphy->edev,
6108c2ecf20Sopenharmony_ci							      EXTCON_USB,
6118c2ecf20Sopenharmony_ci							      vbus_attach);
6128c2ecf20Sopenharmony_ci			}
6138c2ecf20Sopenharmony_ci		}
6148c2ecf20Sopenharmony_ci		break;
6158c2ecf20Sopenharmony_ci	case OTG_STATE_B_PERIPHERAL:
6168c2ecf20Sopenharmony_ci		if (!vbus_attach) {
6178c2ecf20Sopenharmony_ci			dev_dbg(&rport->phy->dev, "usb disconnect\n");
6188c2ecf20Sopenharmony_ci			rphy->chg_state = USB_CHG_STATE_UNDEFINED;
6198c2ecf20Sopenharmony_ci			rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
6208c2ecf20Sopenharmony_ci			rport->state = OTG_STATE_B_IDLE;
6218c2ecf20Sopenharmony_ci			delay = 0;
6228c2ecf20Sopenharmony_ci			rockchip_usb2phy_power_off(rport->phy);
6238c2ecf20Sopenharmony_ci		}
6248c2ecf20Sopenharmony_ci		sch_work = true;
6258c2ecf20Sopenharmony_ci		break;
6268c2ecf20Sopenharmony_ci	case OTG_STATE_A_HOST:
6278c2ecf20Sopenharmony_ci		if (extcon_get_state(rphy->edev, EXTCON_USB_HOST) == 0) {
6288c2ecf20Sopenharmony_ci			dev_dbg(&rport->phy->dev, "usb otg host disconnect\n");
6298c2ecf20Sopenharmony_ci			rport->state = OTG_STATE_B_IDLE;
6308c2ecf20Sopenharmony_ci			rockchip_usb2phy_power_off(rport->phy);
6318c2ecf20Sopenharmony_ci		}
6328c2ecf20Sopenharmony_ci		break;
6338c2ecf20Sopenharmony_ci	default:
6348c2ecf20Sopenharmony_ci		break;
6358c2ecf20Sopenharmony_ci	}
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	if (sch_work)
6388c2ecf20Sopenharmony_ci		schedule_delayed_work(&rport->otg_sm_work, delay);
6398c2ecf20Sopenharmony_ci}
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_cistatic const char *chg_to_string(enum power_supply_type chg_type)
6428c2ecf20Sopenharmony_ci{
6438c2ecf20Sopenharmony_ci	switch (chg_type) {
6448c2ecf20Sopenharmony_ci	case POWER_SUPPLY_TYPE_USB:
6458c2ecf20Sopenharmony_ci		return "USB_SDP_CHARGER";
6468c2ecf20Sopenharmony_ci	case POWER_SUPPLY_TYPE_USB_DCP:
6478c2ecf20Sopenharmony_ci		return "USB_DCP_CHARGER";
6488c2ecf20Sopenharmony_ci	case POWER_SUPPLY_TYPE_USB_CDP:
6498c2ecf20Sopenharmony_ci		return "USB_CDP_CHARGER";
6508c2ecf20Sopenharmony_ci	default:
6518c2ecf20Sopenharmony_ci		return "INVALID_CHARGER";
6528c2ecf20Sopenharmony_ci	}
6538c2ecf20Sopenharmony_ci}
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_cistatic void rockchip_chg_enable_dcd(struct rockchip_usb2phy *rphy,
6568c2ecf20Sopenharmony_ci				    bool en)
6578c2ecf20Sopenharmony_ci{
6588c2ecf20Sopenharmony_ci	struct regmap *base = get_reg_base(rphy);
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	property_enable(base, &rphy->phy_cfg->chg_det.rdm_pdwn_en, en);
6618c2ecf20Sopenharmony_ci	property_enable(base, &rphy->phy_cfg->chg_det.idp_src_en, en);
6628c2ecf20Sopenharmony_ci}
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_cistatic void rockchip_chg_enable_primary_det(struct rockchip_usb2phy *rphy,
6658c2ecf20Sopenharmony_ci					    bool en)
6668c2ecf20Sopenharmony_ci{
6678c2ecf20Sopenharmony_ci	struct regmap *base = get_reg_base(rphy);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	property_enable(base, &rphy->phy_cfg->chg_det.vdp_src_en, en);
6708c2ecf20Sopenharmony_ci	property_enable(base, &rphy->phy_cfg->chg_det.idm_sink_en, en);
6718c2ecf20Sopenharmony_ci}
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_cistatic void rockchip_chg_enable_secondary_det(struct rockchip_usb2phy *rphy,
6748c2ecf20Sopenharmony_ci					      bool en)
6758c2ecf20Sopenharmony_ci{
6768c2ecf20Sopenharmony_ci	struct regmap *base = get_reg_base(rphy);
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	property_enable(base, &rphy->phy_cfg->chg_det.vdm_src_en, en);
6798c2ecf20Sopenharmony_ci	property_enable(base, &rphy->phy_cfg->chg_det.idp_sink_en, en);
6808c2ecf20Sopenharmony_ci}
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci#define CHG_DCD_POLL_TIME	(100 * HZ / 1000)
6838c2ecf20Sopenharmony_ci#define CHG_DCD_MAX_RETRIES	6
6848c2ecf20Sopenharmony_ci#define CHG_PRIMARY_DET_TIME	(40 * HZ / 1000)
6858c2ecf20Sopenharmony_ci#define CHG_SECONDARY_DET_TIME	(40 * HZ / 1000)
6868c2ecf20Sopenharmony_cistatic void rockchip_chg_detect_work(struct work_struct *work)
6878c2ecf20Sopenharmony_ci{
6888c2ecf20Sopenharmony_ci	struct rockchip_usb2phy_port *rport =
6898c2ecf20Sopenharmony_ci		container_of(work, struct rockchip_usb2phy_port, chg_work.work);
6908c2ecf20Sopenharmony_ci	struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
6918c2ecf20Sopenharmony_ci	struct regmap *base = get_reg_base(rphy);
6928c2ecf20Sopenharmony_ci	bool is_dcd, tmout, vout;
6938c2ecf20Sopenharmony_ci	unsigned long delay;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	dev_dbg(&rport->phy->dev, "chg detection work state = %d\n",
6968c2ecf20Sopenharmony_ci		rphy->chg_state);
6978c2ecf20Sopenharmony_ci	switch (rphy->chg_state) {
6988c2ecf20Sopenharmony_ci	case USB_CHG_STATE_UNDEFINED:
6998c2ecf20Sopenharmony_ci		if (!rport->suspended)
7008c2ecf20Sopenharmony_ci			rockchip_usb2phy_power_off(rport->phy);
7018c2ecf20Sopenharmony_ci		/* put the controller in non-driving mode */
7028c2ecf20Sopenharmony_ci		property_enable(base, &rphy->phy_cfg->chg_det.opmode, false);
7038c2ecf20Sopenharmony_ci		/* Start DCD processing stage 1 */
7048c2ecf20Sopenharmony_ci		rockchip_chg_enable_dcd(rphy, true);
7058c2ecf20Sopenharmony_ci		rphy->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
7068c2ecf20Sopenharmony_ci		rphy->dcd_retries = 0;
7078c2ecf20Sopenharmony_ci		delay = CHG_DCD_POLL_TIME;
7088c2ecf20Sopenharmony_ci		break;
7098c2ecf20Sopenharmony_ci	case USB_CHG_STATE_WAIT_FOR_DCD:
7108c2ecf20Sopenharmony_ci		/* get data contact detection status */
7118c2ecf20Sopenharmony_ci		is_dcd = property_enabled(rphy->grf,
7128c2ecf20Sopenharmony_ci					  &rphy->phy_cfg->chg_det.dp_det);
7138c2ecf20Sopenharmony_ci		tmout = ++rphy->dcd_retries == CHG_DCD_MAX_RETRIES;
7148c2ecf20Sopenharmony_ci		/* stage 2 */
7158c2ecf20Sopenharmony_ci		if (is_dcd || tmout) {
7168c2ecf20Sopenharmony_ci			/* stage 4 */
7178c2ecf20Sopenharmony_ci			/* Turn off DCD circuitry */
7188c2ecf20Sopenharmony_ci			rockchip_chg_enable_dcd(rphy, false);
7198c2ecf20Sopenharmony_ci			/* Voltage Source on DP, Probe on DM */
7208c2ecf20Sopenharmony_ci			rockchip_chg_enable_primary_det(rphy, true);
7218c2ecf20Sopenharmony_ci			delay = CHG_PRIMARY_DET_TIME;
7228c2ecf20Sopenharmony_ci			rphy->chg_state = USB_CHG_STATE_DCD_DONE;
7238c2ecf20Sopenharmony_ci		} else {
7248c2ecf20Sopenharmony_ci			/* stage 3 */
7258c2ecf20Sopenharmony_ci			delay = CHG_DCD_POLL_TIME;
7268c2ecf20Sopenharmony_ci		}
7278c2ecf20Sopenharmony_ci		break;
7288c2ecf20Sopenharmony_ci	case USB_CHG_STATE_DCD_DONE:
7298c2ecf20Sopenharmony_ci		vout = property_enabled(rphy->grf,
7308c2ecf20Sopenharmony_ci					&rphy->phy_cfg->chg_det.cp_det);
7318c2ecf20Sopenharmony_ci		rockchip_chg_enable_primary_det(rphy, false);
7328c2ecf20Sopenharmony_ci		if (vout) {
7338c2ecf20Sopenharmony_ci			/* Voltage Source on DM, Probe on DP  */
7348c2ecf20Sopenharmony_ci			rockchip_chg_enable_secondary_det(rphy, true);
7358c2ecf20Sopenharmony_ci			delay = CHG_SECONDARY_DET_TIME;
7368c2ecf20Sopenharmony_ci			rphy->chg_state = USB_CHG_STATE_PRIMARY_DONE;
7378c2ecf20Sopenharmony_ci		} else {
7388c2ecf20Sopenharmony_ci			if (rphy->dcd_retries == CHG_DCD_MAX_RETRIES) {
7398c2ecf20Sopenharmony_ci				/* floating charger found */
7408c2ecf20Sopenharmony_ci				rphy->chg_type = POWER_SUPPLY_TYPE_USB_DCP;
7418c2ecf20Sopenharmony_ci				rphy->chg_state = USB_CHG_STATE_DETECTED;
7428c2ecf20Sopenharmony_ci				delay = 0;
7438c2ecf20Sopenharmony_ci			} else {
7448c2ecf20Sopenharmony_ci				rphy->chg_type = POWER_SUPPLY_TYPE_USB;
7458c2ecf20Sopenharmony_ci				rphy->chg_state = USB_CHG_STATE_DETECTED;
7468c2ecf20Sopenharmony_ci				delay = 0;
7478c2ecf20Sopenharmony_ci			}
7488c2ecf20Sopenharmony_ci		}
7498c2ecf20Sopenharmony_ci		break;
7508c2ecf20Sopenharmony_ci	case USB_CHG_STATE_PRIMARY_DONE:
7518c2ecf20Sopenharmony_ci		vout = property_enabled(rphy->grf,
7528c2ecf20Sopenharmony_ci					&rphy->phy_cfg->chg_det.dcp_det);
7538c2ecf20Sopenharmony_ci		/* Turn off voltage source */
7548c2ecf20Sopenharmony_ci		rockchip_chg_enable_secondary_det(rphy, false);
7558c2ecf20Sopenharmony_ci		if (vout)
7568c2ecf20Sopenharmony_ci			rphy->chg_type = POWER_SUPPLY_TYPE_USB_DCP;
7578c2ecf20Sopenharmony_ci		else
7588c2ecf20Sopenharmony_ci			rphy->chg_type = POWER_SUPPLY_TYPE_USB_CDP;
7598c2ecf20Sopenharmony_ci		fallthrough;
7608c2ecf20Sopenharmony_ci	case USB_CHG_STATE_SECONDARY_DONE:
7618c2ecf20Sopenharmony_ci		rphy->chg_state = USB_CHG_STATE_DETECTED;
7628c2ecf20Sopenharmony_ci		delay = 0;
7638c2ecf20Sopenharmony_ci		fallthrough;
7648c2ecf20Sopenharmony_ci	case USB_CHG_STATE_DETECTED:
7658c2ecf20Sopenharmony_ci		/* put the controller in normal mode */
7668c2ecf20Sopenharmony_ci		property_enable(base, &rphy->phy_cfg->chg_det.opmode, true);
7678c2ecf20Sopenharmony_ci		rockchip_usb2phy_otg_sm_work(&rport->otg_sm_work.work);
7688c2ecf20Sopenharmony_ci		dev_dbg(&rport->phy->dev, "charger = %s\n",
7698c2ecf20Sopenharmony_ci			 chg_to_string(rphy->chg_type));
7708c2ecf20Sopenharmony_ci		return;
7718c2ecf20Sopenharmony_ci	default:
7728c2ecf20Sopenharmony_ci		return;
7738c2ecf20Sopenharmony_ci	}
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	schedule_delayed_work(&rport->chg_work, delay);
7768c2ecf20Sopenharmony_ci}
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci/*
7798c2ecf20Sopenharmony_ci * The function manage host-phy port state and suspend/resume phy port
7808c2ecf20Sopenharmony_ci * to save power.
7818c2ecf20Sopenharmony_ci *
7828c2ecf20Sopenharmony_ci * we rely on utmi_linestate and utmi_hostdisconnect to identify whether
7838c2ecf20Sopenharmony_ci * devices is disconnect or not. Besides, we do not need care it is FS/LS
7848c2ecf20Sopenharmony_ci * disconnected or HS disconnected, actually, we just only need get the
7858c2ecf20Sopenharmony_ci * device is disconnected at last through rearm the delayed work,
7868c2ecf20Sopenharmony_ci * to suspend the phy port in _PHY_STATE_DISCONNECT_ case.
7878c2ecf20Sopenharmony_ci *
7888c2ecf20Sopenharmony_ci * NOTE: It may invoke *phy_powr_off or *phy_power_on which will invoke
7898c2ecf20Sopenharmony_ci * some clk related APIs, so do not invoke it from interrupt context directly.
7908c2ecf20Sopenharmony_ci */
7918c2ecf20Sopenharmony_cistatic void rockchip_usb2phy_sm_work(struct work_struct *work)
7928c2ecf20Sopenharmony_ci{
7938c2ecf20Sopenharmony_ci	struct rockchip_usb2phy_port *rport =
7948c2ecf20Sopenharmony_ci		container_of(work, struct rockchip_usb2phy_port, sm_work.work);
7958c2ecf20Sopenharmony_ci	struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
7968c2ecf20Sopenharmony_ci	unsigned int sh = rport->port_cfg->utmi_hstdet.bitend -
7978c2ecf20Sopenharmony_ci			  rport->port_cfg->utmi_hstdet.bitstart + 1;
7988c2ecf20Sopenharmony_ci	unsigned int ul, uhd, state;
7998c2ecf20Sopenharmony_ci	unsigned int ul_mask, uhd_mask;
8008c2ecf20Sopenharmony_ci	int ret;
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	mutex_lock(&rport->mutex);
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	ret = regmap_read(rphy->grf, rport->port_cfg->utmi_ls.offset, &ul);
8058c2ecf20Sopenharmony_ci	if (ret < 0)
8068c2ecf20Sopenharmony_ci		goto next_schedule;
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	ret = regmap_read(rphy->grf, rport->port_cfg->utmi_hstdet.offset, &uhd);
8098c2ecf20Sopenharmony_ci	if (ret < 0)
8108c2ecf20Sopenharmony_ci		goto next_schedule;
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	uhd_mask = GENMASK(rport->port_cfg->utmi_hstdet.bitend,
8138c2ecf20Sopenharmony_ci			   rport->port_cfg->utmi_hstdet.bitstart);
8148c2ecf20Sopenharmony_ci	ul_mask = GENMASK(rport->port_cfg->utmi_ls.bitend,
8158c2ecf20Sopenharmony_ci			  rport->port_cfg->utmi_ls.bitstart);
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	/* stitch on utmi_ls and utmi_hstdet as phy state */
8188c2ecf20Sopenharmony_ci	state = ((uhd & uhd_mask) >> rport->port_cfg->utmi_hstdet.bitstart) |
8198c2ecf20Sopenharmony_ci		(((ul & ul_mask) >> rport->port_cfg->utmi_ls.bitstart) << sh);
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	switch (state) {
8228c2ecf20Sopenharmony_ci	case PHY_STATE_HS_ONLINE:
8238c2ecf20Sopenharmony_ci		dev_dbg(&rport->phy->dev, "HS online\n");
8248c2ecf20Sopenharmony_ci		break;
8258c2ecf20Sopenharmony_ci	case PHY_STATE_FS_LS_ONLINE:
8268c2ecf20Sopenharmony_ci		/*
8278c2ecf20Sopenharmony_ci		 * For FS/LS device, the online state share with connect state
8288c2ecf20Sopenharmony_ci		 * from utmi_ls and utmi_hstdet register, so we distinguish
8298c2ecf20Sopenharmony_ci		 * them via suspended flag.
8308c2ecf20Sopenharmony_ci		 *
8318c2ecf20Sopenharmony_ci		 * Plus, there are two cases, one is D- Line pull-up, and D+
8328c2ecf20Sopenharmony_ci		 * line pull-down, the state is 4; another is D+ line pull-up,
8338c2ecf20Sopenharmony_ci		 * and D- line pull-down, the state is 2.
8348c2ecf20Sopenharmony_ci		 */
8358c2ecf20Sopenharmony_ci		if (!rport->suspended) {
8368c2ecf20Sopenharmony_ci			/* D- line pull-up, D+ line pull-down */
8378c2ecf20Sopenharmony_ci			dev_dbg(&rport->phy->dev, "FS/LS online\n");
8388c2ecf20Sopenharmony_ci			break;
8398c2ecf20Sopenharmony_ci		}
8408c2ecf20Sopenharmony_ci		fallthrough;
8418c2ecf20Sopenharmony_ci	case PHY_STATE_CONNECT:
8428c2ecf20Sopenharmony_ci		if (rport->suspended) {
8438c2ecf20Sopenharmony_ci			dev_dbg(&rport->phy->dev, "Connected\n");
8448c2ecf20Sopenharmony_ci			rockchip_usb2phy_power_on(rport->phy);
8458c2ecf20Sopenharmony_ci			rport->suspended = false;
8468c2ecf20Sopenharmony_ci		} else {
8478c2ecf20Sopenharmony_ci			/* D+ line pull-up, D- line pull-down */
8488c2ecf20Sopenharmony_ci			dev_dbg(&rport->phy->dev, "FS/LS online\n");
8498c2ecf20Sopenharmony_ci		}
8508c2ecf20Sopenharmony_ci		break;
8518c2ecf20Sopenharmony_ci	case PHY_STATE_DISCONNECT:
8528c2ecf20Sopenharmony_ci		if (!rport->suspended) {
8538c2ecf20Sopenharmony_ci			dev_dbg(&rport->phy->dev, "Disconnected\n");
8548c2ecf20Sopenharmony_ci			rockchip_usb2phy_power_off(rport->phy);
8558c2ecf20Sopenharmony_ci			rport->suspended = true;
8568c2ecf20Sopenharmony_ci		}
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci		/*
8598c2ecf20Sopenharmony_ci		 * activate the linestate detection to get the next device
8608c2ecf20Sopenharmony_ci		 * plug-in irq.
8618c2ecf20Sopenharmony_ci		 */
8628c2ecf20Sopenharmony_ci		property_enable(rphy->grf, &rport->port_cfg->ls_det_clr, true);
8638c2ecf20Sopenharmony_ci		property_enable(rphy->grf, &rport->port_cfg->ls_det_en, true);
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci		/*
8668c2ecf20Sopenharmony_ci		 * we don't need to rearm the delayed work when the phy port
8678c2ecf20Sopenharmony_ci		 * is suspended.
8688c2ecf20Sopenharmony_ci		 */
8698c2ecf20Sopenharmony_ci		mutex_unlock(&rport->mutex);
8708c2ecf20Sopenharmony_ci		return;
8718c2ecf20Sopenharmony_ci	default:
8728c2ecf20Sopenharmony_ci		dev_dbg(&rport->phy->dev, "unknown phy state\n");
8738c2ecf20Sopenharmony_ci		break;
8748c2ecf20Sopenharmony_ci	}
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_cinext_schedule:
8778c2ecf20Sopenharmony_ci	mutex_unlock(&rport->mutex);
8788c2ecf20Sopenharmony_ci	schedule_delayed_work(&rport->sm_work, SCHEDULE_DELAY);
8798c2ecf20Sopenharmony_ci}
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_cistatic irqreturn_t rockchip_usb2phy_linestate_irq(int irq, void *data)
8828c2ecf20Sopenharmony_ci{
8838c2ecf20Sopenharmony_ci	struct rockchip_usb2phy_port *rport = data;
8848c2ecf20Sopenharmony_ci	struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	if (!property_enabled(rphy->grf, &rport->port_cfg->ls_det_st))
8878c2ecf20Sopenharmony_ci		return IRQ_NONE;
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	mutex_lock(&rport->mutex);
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci	/* disable linestate detect irq and clear its status */
8928c2ecf20Sopenharmony_ci	property_enable(rphy->grf, &rport->port_cfg->ls_det_en, false);
8938c2ecf20Sopenharmony_ci	property_enable(rphy->grf, &rport->port_cfg->ls_det_clr, true);
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	mutex_unlock(&rport->mutex);
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	/*
8988c2ecf20Sopenharmony_ci	 * In this case for host phy port, a new device is plugged in,
8998c2ecf20Sopenharmony_ci	 * meanwhile, if the phy port is suspended, we need rearm the work to
9008c2ecf20Sopenharmony_ci	 * resume it and mange its states; otherwise, we do nothing about that.
9018c2ecf20Sopenharmony_ci	 */
9028c2ecf20Sopenharmony_ci	if (rport->suspended && rport->port_id == USB2PHY_PORT_HOST)
9038c2ecf20Sopenharmony_ci		rockchip_usb2phy_sm_work(&rport->sm_work.work);
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
9068c2ecf20Sopenharmony_ci}
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_cistatic irqreturn_t rockchip_usb2phy_bvalid_irq(int irq, void *data)
9098c2ecf20Sopenharmony_ci{
9108c2ecf20Sopenharmony_ci	struct rockchip_usb2phy_port *rport = data;
9118c2ecf20Sopenharmony_ci	struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci	if (!property_enabled(rphy->grf, &rport->port_cfg->bvalid_det_st))
9148c2ecf20Sopenharmony_ci		return IRQ_NONE;
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ci	mutex_lock(&rport->mutex);
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_ci	/* clear bvalid detect irq pending status */
9198c2ecf20Sopenharmony_ci	property_enable(rphy->grf, &rport->port_cfg->bvalid_det_clr, true);
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci	mutex_unlock(&rport->mutex);
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	rockchip_usb2phy_otg_sm_work(&rport->otg_sm_work.work);
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
9268c2ecf20Sopenharmony_ci}
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_cistatic irqreturn_t rockchip_usb2phy_otg_mux_irq(int irq, void *data)
9298c2ecf20Sopenharmony_ci{
9308c2ecf20Sopenharmony_ci	struct rockchip_usb2phy_port *rport = data;
9318c2ecf20Sopenharmony_ci	struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci	if (property_enabled(rphy->grf, &rport->port_cfg->bvalid_det_st))
9348c2ecf20Sopenharmony_ci		return rockchip_usb2phy_bvalid_irq(irq, data);
9358c2ecf20Sopenharmony_ci	else
9368c2ecf20Sopenharmony_ci		return IRQ_NONE;
9378c2ecf20Sopenharmony_ci}
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_cistatic int rockchip_usb2phy_host_port_init(struct rockchip_usb2phy *rphy,
9408c2ecf20Sopenharmony_ci					   struct rockchip_usb2phy_port *rport,
9418c2ecf20Sopenharmony_ci					   struct device_node *child_np)
9428c2ecf20Sopenharmony_ci{
9438c2ecf20Sopenharmony_ci	int ret;
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	rport->port_id = USB2PHY_PORT_HOST;
9468c2ecf20Sopenharmony_ci	rport->port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_HOST];
9478c2ecf20Sopenharmony_ci	rport->suspended = true;
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci	mutex_init(&rport->mutex);
9508c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&rport->sm_work, rockchip_usb2phy_sm_work);
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci	rport->ls_irq = of_irq_get_byname(child_np, "linestate");
9538c2ecf20Sopenharmony_ci	if (rport->ls_irq < 0) {
9548c2ecf20Sopenharmony_ci		dev_err(rphy->dev, "no linestate irq provided\n");
9558c2ecf20Sopenharmony_ci		return rport->ls_irq;
9568c2ecf20Sopenharmony_ci	}
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci	ret = devm_request_threaded_irq(rphy->dev, rport->ls_irq, NULL,
9598c2ecf20Sopenharmony_ci					rockchip_usb2phy_linestate_irq,
9608c2ecf20Sopenharmony_ci					IRQF_ONESHOT,
9618c2ecf20Sopenharmony_ci					"rockchip_usb2phy", rport);
9628c2ecf20Sopenharmony_ci	if (ret) {
9638c2ecf20Sopenharmony_ci		dev_err(rphy->dev, "failed to request linestate irq handle\n");
9648c2ecf20Sopenharmony_ci		return ret;
9658c2ecf20Sopenharmony_ci	}
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci	return 0;
9688c2ecf20Sopenharmony_ci}
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_cistatic int rockchip_otg_event(struct notifier_block *nb,
9718c2ecf20Sopenharmony_ci			      unsigned long event, void *ptr)
9728c2ecf20Sopenharmony_ci{
9738c2ecf20Sopenharmony_ci	struct rockchip_usb2phy_port *rport =
9748c2ecf20Sopenharmony_ci		container_of(nb, struct rockchip_usb2phy_port, event_nb);
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci	schedule_delayed_work(&rport->otg_sm_work, OTG_SCHEDULE_DELAY);
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ci	return NOTIFY_DONE;
9798c2ecf20Sopenharmony_ci}
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_cistatic int rockchip_usb2phy_otg_port_init(struct rockchip_usb2phy *rphy,
9828c2ecf20Sopenharmony_ci					  struct rockchip_usb2phy_port *rport,
9838c2ecf20Sopenharmony_ci					  struct device_node *child_np)
9848c2ecf20Sopenharmony_ci{
9858c2ecf20Sopenharmony_ci	int ret;
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	rport->port_id = USB2PHY_PORT_OTG;
9888c2ecf20Sopenharmony_ci	rport->port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_OTG];
9898c2ecf20Sopenharmony_ci	rport->state = OTG_STATE_UNDEFINED;
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci	/*
9928c2ecf20Sopenharmony_ci	 * set suspended flag to true, but actually don't
9938c2ecf20Sopenharmony_ci	 * put phy in suspend mode, it aims to enable usb
9948c2ecf20Sopenharmony_ci	 * phy and clock in power_on() called by usb controller
9958c2ecf20Sopenharmony_ci	 * driver during probe.
9968c2ecf20Sopenharmony_ci	 */
9978c2ecf20Sopenharmony_ci	rport->suspended = true;
9988c2ecf20Sopenharmony_ci	rport->vbus_attached = false;
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci	mutex_init(&rport->mutex);
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_ci	rport->mode = of_usb_get_dr_mode_by_phy(child_np, -1);
10038c2ecf20Sopenharmony_ci	if (rport->mode == USB_DR_MODE_HOST ||
10048c2ecf20Sopenharmony_ci	    rport->mode == USB_DR_MODE_UNKNOWN) {
10058c2ecf20Sopenharmony_ci		ret = 0;
10068c2ecf20Sopenharmony_ci		goto out;
10078c2ecf20Sopenharmony_ci	}
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&rport->chg_work, rockchip_chg_detect_work);
10108c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&rport->otg_sm_work, rockchip_usb2phy_otg_sm_work);
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci	/*
10138c2ecf20Sopenharmony_ci	 * Some SoCs use one interrupt with otg-id/otg-bvalid/linestate
10148c2ecf20Sopenharmony_ci	 * interrupts muxed together, so probe the otg-mux interrupt first,
10158c2ecf20Sopenharmony_ci	 * if not found, then look for the regular interrupts one by one.
10168c2ecf20Sopenharmony_ci	 */
10178c2ecf20Sopenharmony_ci	rport->otg_mux_irq = of_irq_get_byname(child_np, "otg-mux");
10188c2ecf20Sopenharmony_ci	if (rport->otg_mux_irq > 0) {
10198c2ecf20Sopenharmony_ci		ret = devm_request_threaded_irq(rphy->dev, rport->otg_mux_irq,
10208c2ecf20Sopenharmony_ci						NULL,
10218c2ecf20Sopenharmony_ci						rockchip_usb2phy_otg_mux_irq,
10228c2ecf20Sopenharmony_ci						IRQF_ONESHOT,
10238c2ecf20Sopenharmony_ci						"rockchip_usb2phy_otg",
10248c2ecf20Sopenharmony_ci						rport);
10258c2ecf20Sopenharmony_ci		if (ret) {
10268c2ecf20Sopenharmony_ci			dev_err(rphy->dev,
10278c2ecf20Sopenharmony_ci				"failed to request otg-mux irq handle\n");
10288c2ecf20Sopenharmony_ci			goto out;
10298c2ecf20Sopenharmony_ci		}
10308c2ecf20Sopenharmony_ci	} else {
10318c2ecf20Sopenharmony_ci		rport->bvalid_irq = of_irq_get_byname(child_np, "otg-bvalid");
10328c2ecf20Sopenharmony_ci		if (rport->bvalid_irq < 0) {
10338c2ecf20Sopenharmony_ci			dev_err(rphy->dev, "no vbus valid irq provided\n");
10348c2ecf20Sopenharmony_ci			ret = rport->bvalid_irq;
10358c2ecf20Sopenharmony_ci			goto out;
10368c2ecf20Sopenharmony_ci		}
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_ci		ret = devm_request_threaded_irq(rphy->dev, rport->bvalid_irq,
10398c2ecf20Sopenharmony_ci						NULL,
10408c2ecf20Sopenharmony_ci						rockchip_usb2phy_bvalid_irq,
10418c2ecf20Sopenharmony_ci						IRQF_ONESHOT,
10428c2ecf20Sopenharmony_ci						"rockchip_usb2phy_bvalid",
10438c2ecf20Sopenharmony_ci						rport);
10448c2ecf20Sopenharmony_ci		if (ret) {
10458c2ecf20Sopenharmony_ci			dev_err(rphy->dev,
10468c2ecf20Sopenharmony_ci				"failed to request otg-bvalid irq handle\n");
10478c2ecf20Sopenharmony_ci			goto out;
10488c2ecf20Sopenharmony_ci		}
10498c2ecf20Sopenharmony_ci	}
10508c2ecf20Sopenharmony_ci
10518c2ecf20Sopenharmony_ci	if (!IS_ERR(rphy->edev)) {
10528c2ecf20Sopenharmony_ci		rport->event_nb.notifier_call = rockchip_otg_event;
10538c2ecf20Sopenharmony_ci
10548c2ecf20Sopenharmony_ci		ret = devm_extcon_register_notifier(rphy->dev, rphy->edev,
10558c2ecf20Sopenharmony_ci					EXTCON_USB_HOST, &rport->event_nb);
10568c2ecf20Sopenharmony_ci		if (ret)
10578c2ecf20Sopenharmony_ci			dev_err(rphy->dev, "register USB HOST notifier failed\n");
10588c2ecf20Sopenharmony_ci	}
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_ciout:
10618c2ecf20Sopenharmony_ci	return ret;
10628c2ecf20Sopenharmony_ci}
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_cistatic int rockchip_usb2phy_probe(struct platform_device *pdev)
10658c2ecf20Sopenharmony_ci{
10668c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
10678c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
10688c2ecf20Sopenharmony_ci	struct device_node *child_np;
10698c2ecf20Sopenharmony_ci	struct phy_provider *provider;
10708c2ecf20Sopenharmony_ci	struct rockchip_usb2phy *rphy;
10718c2ecf20Sopenharmony_ci	const struct rockchip_usb2phy_cfg *phy_cfgs;
10728c2ecf20Sopenharmony_ci	const struct of_device_id *match;
10738c2ecf20Sopenharmony_ci	unsigned int reg;
10748c2ecf20Sopenharmony_ci	int index, ret;
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci	rphy = devm_kzalloc(dev, sizeof(*rphy), GFP_KERNEL);
10778c2ecf20Sopenharmony_ci	if (!rphy)
10788c2ecf20Sopenharmony_ci		return -ENOMEM;
10798c2ecf20Sopenharmony_ci
10808c2ecf20Sopenharmony_ci	match = of_match_device(dev->driver->of_match_table, dev);
10818c2ecf20Sopenharmony_ci	if (!match || !match->data) {
10828c2ecf20Sopenharmony_ci		dev_err(dev, "phy configs are not assigned!\n");
10838c2ecf20Sopenharmony_ci		return -EINVAL;
10848c2ecf20Sopenharmony_ci	}
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci	if (!dev->parent || !dev->parent->of_node)
10878c2ecf20Sopenharmony_ci		return -EINVAL;
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	rphy->grf = syscon_node_to_regmap(dev->parent->of_node);
10908c2ecf20Sopenharmony_ci	if (IS_ERR(rphy->grf))
10918c2ecf20Sopenharmony_ci		return PTR_ERR(rphy->grf);
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_ci	if (of_device_is_compatible(np, "rockchip,rv1108-usb2phy")) {
10948c2ecf20Sopenharmony_ci		rphy->usbgrf =
10958c2ecf20Sopenharmony_ci			syscon_regmap_lookup_by_phandle(dev->of_node,
10968c2ecf20Sopenharmony_ci							"rockchip,usbgrf");
10978c2ecf20Sopenharmony_ci		if (IS_ERR(rphy->usbgrf))
10988c2ecf20Sopenharmony_ci			return PTR_ERR(rphy->usbgrf);
10998c2ecf20Sopenharmony_ci	} else {
11008c2ecf20Sopenharmony_ci		rphy->usbgrf = NULL;
11018c2ecf20Sopenharmony_ci	}
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci	if (of_property_read_u32(np, "reg", &reg)) {
11048c2ecf20Sopenharmony_ci		dev_err(dev, "the reg property is not assigned in %pOFn node\n",
11058c2ecf20Sopenharmony_ci			np);
11068c2ecf20Sopenharmony_ci		return -EINVAL;
11078c2ecf20Sopenharmony_ci	}
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci	rphy->dev = dev;
11108c2ecf20Sopenharmony_ci	phy_cfgs = match->data;
11118c2ecf20Sopenharmony_ci	rphy->chg_state = USB_CHG_STATE_UNDEFINED;
11128c2ecf20Sopenharmony_ci	rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
11138c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, rphy);
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci	ret = rockchip_usb2phy_extcon_register(rphy);
11168c2ecf20Sopenharmony_ci	if (ret)
11178c2ecf20Sopenharmony_ci		return ret;
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ci	/* find out a proper config which can be matched with dt. */
11208c2ecf20Sopenharmony_ci	index = 0;
11218c2ecf20Sopenharmony_ci	while (phy_cfgs[index].reg) {
11228c2ecf20Sopenharmony_ci		if (phy_cfgs[index].reg == reg) {
11238c2ecf20Sopenharmony_ci			rphy->phy_cfg = &phy_cfgs[index];
11248c2ecf20Sopenharmony_ci			break;
11258c2ecf20Sopenharmony_ci		}
11268c2ecf20Sopenharmony_ci
11278c2ecf20Sopenharmony_ci		++index;
11288c2ecf20Sopenharmony_ci	}
11298c2ecf20Sopenharmony_ci
11308c2ecf20Sopenharmony_ci	if (!rphy->phy_cfg) {
11318c2ecf20Sopenharmony_ci		dev_err(dev, "no phy-config can be matched with %pOFn node\n",
11328c2ecf20Sopenharmony_ci			np);
11338c2ecf20Sopenharmony_ci		return -EINVAL;
11348c2ecf20Sopenharmony_ci	}
11358c2ecf20Sopenharmony_ci
11368c2ecf20Sopenharmony_ci	rphy->clk = of_clk_get_by_name(np, "phyclk");
11378c2ecf20Sopenharmony_ci	if (!IS_ERR(rphy->clk)) {
11388c2ecf20Sopenharmony_ci		clk_prepare_enable(rphy->clk);
11398c2ecf20Sopenharmony_ci	} else {
11408c2ecf20Sopenharmony_ci		dev_info(&pdev->dev, "no phyclk specified\n");
11418c2ecf20Sopenharmony_ci		rphy->clk = NULL;
11428c2ecf20Sopenharmony_ci	}
11438c2ecf20Sopenharmony_ci
11448c2ecf20Sopenharmony_ci	ret = rockchip_usb2phy_clk480m_register(rphy);
11458c2ecf20Sopenharmony_ci	if (ret) {
11468c2ecf20Sopenharmony_ci		dev_err(dev, "failed to register 480m output clock\n");
11478c2ecf20Sopenharmony_ci		goto disable_clks;
11488c2ecf20Sopenharmony_ci	}
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_ci	index = 0;
11518c2ecf20Sopenharmony_ci	for_each_available_child_of_node(np, child_np) {
11528c2ecf20Sopenharmony_ci		struct rockchip_usb2phy_port *rport = &rphy->ports[index];
11538c2ecf20Sopenharmony_ci		struct phy *phy;
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci		/* This driver aims to support both otg-port and host-port */
11568c2ecf20Sopenharmony_ci		if (!of_node_name_eq(child_np, "host-port") &&
11578c2ecf20Sopenharmony_ci		    !of_node_name_eq(child_np, "otg-port"))
11588c2ecf20Sopenharmony_ci			goto next_child;
11598c2ecf20Sopenharmony_ci
11608c2ecf20Sopenharmony_ci		phy = devm_phy_create(dev, child_np, &rockchip_usb2phy_ops);
11618c2ecf20Sopenharmony_ci		if (IS_ERR(phy)) {
11628c2ecf20Sopenharmony_ci			dev_err(dev, "failed to create phy\n");
11638c2ecf20Sopenharmony_ci			ret = PTR_ERR(phy);
11648c2ecf20Sopenharmony_ci			goto put_child;
11658c2ecf20Sopenharmony_ci		}
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_ci		rport->phy = phy;
11688c2ecf20Sopenharmony_ci		phy_set_drvdata(rport->phy, rport);
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci		/* initialize otg/host port separately */
11718c2ecf20Sopenharmony_ci		if (of_node_name_eq(child_np, "host-port")) {
11728c2ecf20Sopenharmony_ci			ret = rockchip_usb2phy_host_port_init(rphy, rport,
11738c2ecf20Sopenharmony_ci							      child_np);
11748c2ecf20Sopenharmony_ci			if (ret)
11758c2ecf20Sopenharmony_ci				goto put_child;
11768c2ecf20Sopenharmony_ci		} else {
11778c2ecf20Sopenharmony_ci			ret = rockchip_usb2phy_otg_port_init(rphy, rport,
11788c2ecf20Sopenharmony_ci							     child_np);
11798c2ecf20Sopenharmony_ci			if (ret)
11808c2ecf20Sopenharmony_ci				goto put_child;
11818c2ecf20Sopenharmony_ci		}
11828c2ecf20Sopenharmony_ci
11838c2ecf20Sopenharmony_cinext_child:
11848c2ecf20Sopenharmony_ci		/* to prevent out of boundary */
11858c2ecf20Sopenharmony_ci		if (++index >= rphy->phy_cfg->num_ports)
11868c2ecf20Sopenharmony_ci			break;
11878c2ecf20Sopenharmony_ci	}
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci	provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
11908c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(provider);
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ciput_child:
11938c2ecf20Sopenharmony_ci	of_node_put(child_np);
11948c2ecf20Sopenharmony_cidisable_clks:
11958c2ecf20Sopenharmony_ci	if (rphy->clk) {
11968c2ecf20Sopenharmony_ci		clk_disable_unprepare(rphy->clk);
11978c2ecf20Sopenharmony_ci		clk_put(rphy->clk);
11988c2ecf20Sopenharmony_ci	}
11998c2ecf20Sopenharmony_ci	return ret;
12008c2ecf20Sopenharmony_ci}
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_cistatic const struct rockchip_usb2phy_cfg rk3228_phy_cfgs[] = {
12038c2ecf20Sopenharmony_ci	{
12048c2ecf20Sopenharmony_ci		.reg = 0x760,
12058c2ecf20Sopenharmony_ci		.num_ports	= 2,
12068c2ecf20Sopenharmony_ci		.clkout_ctl	= { 0x0768, 4, 4, 1, 0 },
12078c2ecf20Sopenharmony_ci		.port_cfgs	= {
12088c2ecf20Sopenharmony_ci			[USB2PHY_PORT_OTG] = {
12098c2ecf20Sopenharmony_ci				.phy_sus	= { 0x0760, 15, 0, 0, 0x1d1 },
12108c2ecf20Sopenharmony_ci				.bvalid_det_en	= { 0x0680, 3, 3, 0, 1 },
12118c2ecf20Sopenharmony_ci				.bvalid_det_st	= { 0x0690, 3, 3, 0, 1 },
12128c2ecf20Sopenharmony_ci				.bvalid_det_clr	= { 0x06a0, 3, 3, 0, 1 },
12138c2ecf20Sopenharmony_ci				.ls_det_en	= { 0x0680, 2, 2, 0, 1 },
12148c2ecf20Sopenharmony_ci				.ls_det_st	= { 0x0690, 2, 2, 0, 1 },
12158c2ecf20Sopenharmony_ci				.ls_det_clr	= { 0x06a0, 2, 2, 0, 1 },
12168c2ecf20Sopenharmony_ci				.utmi_bvalid	= { 0x0480, 4, 4, 0, 1 },
12178c2ecf20Sopenharmony_ci				.utmi_ls	= { 0x0480, 3, 2, 0, 1 },
12188c2ecf20Sopenharmony_ci			},
12198c2ecf20Sopenharmony_ci			[USB2PHY_PORT_HOST] = {
12208c2ecf20Sopenharmony_ci				.phy_sus	= { 0x0764, 15, 0, 0, 0x1d1 },
12218c2ecf20Sopenharmony_ci				.ls_det_en	= { 0x0680, 4, 4, 0, 1 },
12228c2ecf20Sopenharmony_ci				.ls_det_st	= { 0x0690, 4, 4, 0, 1 },
12238c2ecf20Sopenharmony_ci				.ls_det_clr	= { 0x06a0, 4, 4, 0, 1 }
12248c2ecf20Sopenharmony_ci			}
12258c2ecf20Sopenharmony_ci		},
12268c2ecf20Sopenharmony_ci		.chg_det = {
12278c2ecf20Sopenharmony_ci			.opmode		= { 0x0760, 3, 0, 5, 1 },
12288c2ecf20Sopenharmony_ci			.cp_det		= { 0x0884, 4, 4, 0, 1 },
12298c2ecf20Sopenharmony_ci			.dcp_det	= { 0x0884, 3, 3, 0, 1 },
12308c2ecf20Sopenharmony_ci			.dp_det		= { 0x0884, 5, 5, 0, 1 },
12318c2ecf20Sopenharmony_ci			.idm_sink_en	= { 0x0768, 8, 8, 0, 1 },
12328c2ecf20Sopenharmony_ci			.idp_sink_en	= { 0x0768, 7, 7, 0, 1 },
12338c2ecf20Sopenharmony_ci			.idp_src_en	= { 0x0768, 9, 9, 0, 1 },
12348c2ecf20Sopenharmony_ci			.rdm_pdwn_en	= { 0x0768, 10, 10, 0, 1 },
12358c2ecf20Sopenharmony_ci			.vdm_src_en	= { 0x0768, 12, 12, 0, 1 },
12368c2ecf20Sopenharmony_ci			.vdp_src_en	= { 0x0768, 11, 11, 0, 1 },
12378c2ecf20Sopenharmony_ci		},
12388c2ecf20Sopenharmony_ci	},
12398c2ecf20Sopenharmony_ci	{
12408c2ecf20Sopenharmony_ci		.reg = 0x800,
12418c2ecf20Sopenharmony_ci		.num_ports	= 2,
12428c2ecf20Sopenharmony_ci		.clkout_ctl	= { 0x0808, 4, 4, 1, 0 },
12438c2ecf20Sopenharmony_ci		.port_cfgs	= {
12448c2ecf20Sopenharmony_ci			[USB2PHY_PORT_OTG] = {
12458c2ecf20Sopenharmony_ci				.phy_sus	= { 0x800, 15, 0, 0, 0x1d1 },
12468c2ecf20Sopenharmony_ci				.ls_det_en	= { 0x0684, 0, 0, 0, 1 },
12478c2ecf20Sopenharmony_ci				.ls_det_st	= { 0x0694, 0, 0, 0, 1 },
12488c2ecf20Sopenharmony_ci				.ls_det_clr	= { 0x06a4, 0, 0, 0, 1 }
12498c2ecf20Sopenharmony_ci			},
12508c2ecf20Sopenharmony_ci			[USB2PHY_PORT_HOST] = {
12518c2ecf20Sopenharmony_ci				.phy_sus	= { 0x804, 15, 0, 0, 0x1d1 },
12528c2ecf20Sopenharmony_ci				.ls_det_en	= { 0x0684, 1, 1, 0, 1 },
12538c2ecf20Sopenharmony_ci				.ls_det_st	= { 0x0694, 1, 1, 0, 1 },
12548c2ecf20Sopenharmony_ci				.ls_det_clr	= { 0x06a4, 1, 1, 0, 1 }
12558c2ecf20Sopenharmony_ci			}
12568c2ecf20Sopenharmony_ci		},
12578c2ecf20Sopenharmony_ci	},
12588c2ecf20Sopenharmony_ci	{ /* sentinel */ }
12598c2ecf20Sopenharmony_ci};
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_cistatic const struct rockchip_usb2phy_cfg rk3328_phy_cfgs[] = {
12628c2ecf20Sopenharmony_ci	{
12638c2ecf20Sopenharmony_ci		.reg = 0x100,
12648c2ecf20Sopenharmony_ci		.num_ports	= 2,
12658c2ecf20Sopenharmony_ci		.clkout_ctl	= { 0x108, 4, 4, 1, 0 },
12668c2ecf20Sopenharmony_ci		.port_cfgs	= {
12678c2ecf20Sopenharmony_ci			[USB2PHY_PORT_OTG] = {
12688c2ecf20Sopenharmony_ci				.phy_sus	= { 0x0100, 15, 0, 0, 0x1d1 },
12698c2ecf20Sopenharmony_ci				.bvalid_det_en	= { 0x0110, 2, 2, 0, 1 },
12708c2ecf20Sopenharmony_ci				.bvalid_det_st	= { 0x0114, 2, 2, 0, 1 },
12718c2ecf20Sopenharmony_ci				.bvalid_det_clr = { 0x0118, 2, 2, 0, 1 },
12728c2ecf20Sopenharmony_ci				.ls_det_en	= { 0x0110, 0, 0, 0, 1 },
12738c2ecf20Sopenharmony_ci				.ls_det_st	= { 0x0114, 0, 0, 0, 1 },
12748c2ecf20Sopenharmony_ci				.ls_det_clr	= { 0x0118, 0, 0, 0, 1 },
12758c2ecf20Sopenharmony_ci				.utmi_avalid	= { 0x0120, 10, 10, 0, 1 },
12768c2ecf20Sopenharmony_ci				.utmi_bvalid	= { 0x0120, 9, 9, 0, 1 },
12778c2ecf20Sopenharmony_ci				.utmi_ls	= { 0x0120, 5, 4, 0, 1 },
12788c2ecf20Sopenharmony_ci			},
12798c2ecf20Sopenharmony_ci			[USB2PHY_PORT_HOST] = {
12808c2ecf20Sopenharmony_ci				.phy_sus	= { 0x104, 15, 0, 0, 0x1d1 },
12818c2ecf20Sopenharmony_ci				.ls_det_en	= { 0x110, 1, 1, 0, 1 },
12828c2ecf20Sopenharmony_ci				.ls_det_st	= { 0x114, 1, 1, 0, 1 },
12838c2ecf20Sopenharmony_ci				.ls_det_clr	= { 0x118, 1, 1, 0, 1 },
12848c2ecf20Sopenharmony_ci				.utmi_ls	= { 0x120, 17, 16, 0, 1 },
12858c2ecf20Sopenharmony_ci				.utmi_hstdet	= { 0x120, 19, 19, 0, 1 }
12868c2ecf20Sopenharmony_ci			}
12878c2ecf20Sopenharmony_ci		},
12888c2ecf20Sopenharmony_ci		.chg_det = {
12898c2ecf20Sopenharmony_ci			.opmode		= { 0x0100, 3, 0, 5, 1 },
12908c2ecf20Sopenharmony_ci			.cp_det		= { 0x0120, 24, 24, 0, 1 },
12918c2ecf20Sopenharmony_ci			.dcp_det	= { 0x0120, 23, 23, 0, 1 },
12928c2ecf20Sopenharmony_ci			.dp_det		= { 0x0120, 25, 25, 0, 1 },
12938c2ecf20Sopenharmony_ci			.idm_sink_en	= { 0x0108, 8, 8, 0, 1 },
12948c2ecf20Sopenharmony_ci			.idp_sink_en	= { 0x0108, 7, 7, 0, 1 },
12958c2ecf20Sopenharmony_ci			.idp_src_en	= { 0x0108, 9, 9, 0, 1 },
12968c2ecf20Sopenharmony_ci			.rdm_pdwn_en	= { 0x0108, 10, 10, 0, 1 },
12978c2ecf20Sopenharmony_ci			.vdm_src_en	= { 0x0108, 12, 12, 0, 1 },
12988c2ecf20Sopenharmony_ci			.vdp_src_en	= { 0x0108, 11, 11, 0, 1 },
12998c2ecf20Sopenharmony_ci		},
13008c2ecf20Sopenharmony_ci	},
13018c2ecf20Sopenharmony_ci	{ /* sentinel */ }
13028c2ecf20Sopenharmony_ci};
13038c2ecf20Sopenharmony_ci
13048c2ecf20Sopenharmony_cistatic const struct rockchip_usb2phy_cfg rk3366_phy_cfgs[] = {
13058c2ecf20Sopenharmony_ci	{
13068c2ecf20Sopenharmony_ci		.reg = 0x700,
13078c2ecf20Sopenharmony_ci		.num_ports	= 2,
13088c2ecf20Sopenharmony_ci		.clkout_ctl	= { 0x0724, 15, 15, 1, 0 },
13098c2ecf20Sopenharmony_ci		.port_cfgs	= {
13108c2ecf20Sopenharmony_ci			[USB2PHY_PORT_HOST] = {
13118c2ecf20Sopenharmony_ci				.phy_sus	= { 0x0728, 15, 0, 0, 0x1d1 },
13128c2ecf20Sopenharmony_ci				.ls_det_en	= { 0x0680, 4, 4, 0, 1 },
13138c2ecf20Sopenharmony_ci				.ls_det_st	= { 0x0690, 4, 4, 0, 1 },
13148c2ecf20Sopenharmony_ci				.ls_det_clr	= { 0x06a0, 4, 4, 0, 1 },
13158c2ecf20Sopenharmony_ci				.utmi_ls	= { 0x049c, 14, 13, 0, 1 },
13168c2ecf20Sopenharmony_ci				.utmi_hstdet	= { 0x049c, 12, 12, 0, 1 }
13178c2ecf20Sopenharmony_ci			}
13188c2ecf20Sopenharmony_ci		},
13198c2ecf20Sopenharmony_ci	},
13208c2ecf20Sopenharmony_ci	{ /* sentinel */ }
13218c2ecf20Sopenharmony_ci};
13228c2ecf20Sopenharmony_ci
13238c2ecf20Sopenharmony_cistatic const struct rockchip_usb2phy_cfg rk3399_phy_cfgs[] = {
13248c2ecf20Sopenharmony_ci	{
13258c2ecf20Sopenharmony_ci		.reg		= 0xe450,
13268c2ecf20Sopenharmony_ci		.num_ports	= 2,
13278c2ecf20Sopenharmony_ci		.clkout_ctl	= { 0xe450, 4, 4, 1, 0 },
13288c2ecf20Sopenharmony_ci		.port_cfgs	= {
13298c2ecf20Sopenharmony_ci			[USB2PHY_PORT_OTG] = {
13308c2ecf20Sopenharmony_ci				.phy_sus	= { 0xe454, 1, 0, 2, 1 },
13318c2ecf20Sopenharmony_ci				.bvalid_det_en	= { 0xe3c0, 3, 3, 0, 1 },
13328c2ecf20Sopenharmony_ci				.bvalid_det_st	= { 0xe3e0, 3, 3, 0, 1 },
13338c2ecf20Sopenharmony_ci				.bvalid_det_clr	= { 0xe3d0, 3, 3, 0, 1 },
13348c2ecf20Sopenharmony_ci				.utmi_avalid	= { 0xe2ac, 7, 7, 0, 1 },
13358c2ecf20Sopenharmony_ci				.utmi_bvalid	= { 0xe2ac, 12, 12, 0, 1 },
13368c2ecf20Sopenharmony_ci			},
13378c2ecf20Sopenharmony_ci			[USB2PHY_PORT_HOST] = {
13388c2ecf20Sopenharmony_ci				.phy_sus	= { 0xe458, 1, 0, 0x2, 0x1 },
13398c2ecf20Sopenharmony_ci				.ls_det_en	= { 0xe3c0, 6, 6, 0, 1 },
13408c2ecf20Sopenharmony_ci				.ls_det_st	= { 0xe3e0, 6, 6, 0, 1 },
13418c2ecf20Sopenharmony_ci				.ls_det_clr	= { 0xe3d0, 6, 6, 0, 1 },
13428c2ecf20Sopenharmony_ci				.utmi_ls	= { 0xe2ac, 22, 21, 0, 1 },
13438c2ecf20Sopenharmony_ci				.utmi_hstdet	= { 0xe2ac, 23, 23, 0, 1 }
13448c2ecf20Sopenharmony_ci			}
13458c2ecf20Sopenharmony_ci		},
13468c2ecf20Sopenharmony_ci		.chg_det = {
13478c2ecf20Sopenharmony_ci			.opmode		= { 0xe454, 3, 0, 5, 1 },
13488c2ecf20Sopenharmony_ci			.cp_det		= { 0xe2ac, 2, 2, 0, 1 },
13498c2ecf20Sopenharmony_ci			.dcp_det	= { 0xe2ac, 1, 1, 0, 1 },
13508c2ecf20Sopenharmony_ci			.dp_det		= { 0xe2ac, 0, 0, 0, 1 },
13518c2ecf20Sopenharmony_ci			.idm_sink_en	= { 0xe450, 8, 8, 0, 1 },
13528c2ecf20Sopenharmony_ci			.idp_sink_en	= { 0xe450, 7, 7, 0, 1 },
13538c2ecf20Sopenharmony_ci			.idp_src_en	= { 0xe450, 9, 9, 0, 1 },
13548c2ecf20Sopenharmony_ci			.rdm_pdwn_en	= { 0xe450, 10, 10, 0, 1 },
13558c2ecf20Sopenharmony_ci			.vdm_src_en	= { 0xe450, 12, 12, 0, 1 },
13568c2ecf20Sopenharmony_ci			.vdp_src_en	= { 0xe450, 11, 11, 0, 1 },
13578c2ecf20Sopenharmony_ci		},
13588c2ecf20Sopenharmony_ci	},
13598c2ecf20Sopenharmony_ci	{
13608c2ecf20Sopenharmony_ci		.reg		= 0xe460,
13618c2ecf20Sopenharmony_ci		.num_ports	= 2,
13628c2ecf20Sopenharmony_ci		.clkout_ctl	= { 0xe460, 4, 4, 1, 0 },
13638c2ecf20Sopenharmony_ci		.port_cfgs	= {
13648c2ecf20Sopenharmony_ci			[USB2PHY_PORT_OTG] = {
13658c2ecf20Sopenharmony_ci				.phy_sus        = { 0xe464, 1, 0, 2, 1 },
13668c2ecf20Sopenharmony_ci				.bvalid_det_en  = { 0xe3c0, 8, 8, 0, 1 },
13678c2ecf20Sopenharmony_ci				.bvalid_det_st  = { 0xe3e0, 8, 8, 0, 1 },
13688c2ecf20Sopenharmony_ci				.bvalid_det_clr = { 0xe3d0, 8, 8, 0, 1 },
13698c2ecf20Sopenharmony_ci				.utmi_avalid	= { 0xe2ac, 10, 10, 0, 1 },
13708c2ecf20Sopenharmony_ci				.utmi_bvalid    = { 0xe2ac, 16, 16, 0, 1 },
13718c2ecf20Sopenharmony_ci			},
13728c2ecf20Sopenharmony_ci			[USB2PHY_PORT_HOST] = {
13738c2ecf20Sopenharmony_ci				.phy_sus	= { 0xe468, 1, 0, 0x2, 0x1 },
13748c2ecf20Sopenharmony_ci				.ls_det_en	= { 0xe3c0, 11, 11, 0, 1 },
13758c2ecf20Sopenharmony_ci				.ls_det_st	= { 0xe3e0, 11, 11, 0, 1 },
13768c2ecf20Sopenharmony_ci				.ls_det_clr	= { 0xe3d0, 11, 11, 0, 1 },
13778c2ecf20Sopenharmony_ci				.utmi_ls	= { 0xe2ac, 26, 25, 0, 1 },
13788c2ecf20Sopenharmony_ci				.utmi_hstdet	= { 0xe2ac, 27, 27, 0, 1 }
13798c2ecf20Sopenharmony_ci			}
13808c2ecf20Sopenharmony_ci		},
13818c2ecf20Sopenharmony_ci	},
13828c2ecf20Sopenharmony_ci	{ /* sentinel */ }
13838c2ecf20Sopenharmony_ci};
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_cistatic const struct rockchip_usb2phy_cfg rv1108_phy_cfgs[] = {
13868c2ecf20Sopenharmony_ci	{
13878c2ecf20Sopenharmony_ci		.reg = 0x100,
13888c2ecf20Sopenharmony_ci		.num_ports	= 2,
13898c2ecf20Sopenharmony_ci		.clkout_ctl	= { 0x108, 4, 4, 1, 0 },
13908c2ecf20Sopenharmony_ci		.port_cfgs	= {
13918c2ecf20Sopenharmony_ci			[USB2PHY_PORT_OTG] = {
13928c2ecf20Sopenharmony_ci				.phy_sus	= { 0x0100, 15, 0, 0, 0x1d1 },
13938c2ecf20Sopenharmony_ci				.bvalid_det_en	= { 0x0680, 3, 3, 0, 1 },
13948c2ecf20Sopenharmony_ci				.bvalid_det_st	= { 0x0690, 3, 3, 0, 1 },
13958c2ecf20Sopenharmony_ci				.bvalid_det_clr = { 0x06a0, 3, 3, 0, 1 },
13968c2ecf20Sopenharmony_ci				.ls_det_en	= { 0x0680, 2, 2, 0, 1 },
13978c2ecf20Sopenharmony_ci				.ls_det_st	= { 0x0690, 2, 2, 0, 1 },
13988c2ecf20Sopenharmony_ci				.ls_det_clr	= { 0x06a0, 2, 2, 0, 1 },
13998c2ecf20Sopenharmony_ci				.utmi_bvalid	= { 0x0804, 10, 10, 0, 1 },
14008c2ecf20Sopenharmony_ci				.utmi_ls	= { 0x0804, 13, 12, 0, 1 },
14018c2ecf20Sopenharmony_ci			},
14028c2ecf20Sopenharmony_ci			[USB2PHY_PORT_HOST] = {
14038c2ecf20Sopenharmony_ci				.phy_sus	= { 0x0104, 15, 0, 0, 0x1d1 },
14048c2ecf20Sopenharmony_ci				.ls_det_en	= { 0x0680, 4, 4, 0, 1 },
14058c2ecf20Sopenharmony_ci				.ls_det_st	= { 0x0690, 4, 4, 0, 1 },
14068c2ecf20Sopenharmony_ci				.ls_det_clr	= { 0x06a0, 4, 4, 0, 1 },
14078c2ecf20Sopenharmony_ci				.utmi_ls	= { 0x0804, 9, 8, 0, 1 },
14088c2ecf20Sopenharmony_ci				.utmi_hstdet	= { 0x0804, 7, 7, 0, 1 }
14098c2ecf20Sopenharmony_ci			}
14108c2ecf20Sopenharmony_ci		},
14118c2ecf20Sopenharmony_ci		.chg_det = {
14128c2ecf20Sopenharmony_ci			.opmode		= { 0x0100, 3, 0, 5, 1 },
14138c2ecf20Sopenharmony_ci			.cp_det		= { 0x0804, 1, 1, 0, 1 },
14148c2ecf20Sopenharmony_ci			.dcp_det	= { 0x0804, 0, 0, 0, 1 },
14158c2ecf20Sopenharmony_ci			.dp_det		= { 0x0804, 2, 2, 0, 1 },
14168c2ecf20Sopenharmony_ci			.idm_sink_en	= { 0x0108, 8, 8, 0, 1 },
14178c2ecf20Sopenharmony_ci			.idp_sink_en	= { 0x0108, 7, 7, 0, 1 },
14188c2ecf20Sopenharmony_ci			.idp_src_en	= { 0x0108, 9, 9, 0, 1 },
14198c2ecf20Sopenharmony_ci			.rdm_pdwn_en	= { 0x0108, 10, 10, 0, 1 },
14208c2ecf20Sopenharmony_ci			.vdm_src_en	= { 0x0108, 12, 12, 0, 1 },
14218c2ecf20Sopenharmony_ci			.vdp_src_en	= { 0x0108, 11, 11, 0, 1 },
14228c2ecf20Sopenharmony_ci		},
14238c2ecf20Sopenharmony_ci	},
14248c2ecf20Sopenharmony_ci	{ /* sentinel */ }
14258c2ecf20Sopenharmony_ci};
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_cistatic const struct of_device_id rockchip_usb2phy_dt_match[] = {
14288c2ecf20Sopenharmony_ci	{ .compatible = "rockchip,px30-usb2phy", .data = &rk3328_phy_cfgs },
14298c2ecf20Sopenharmony_ci	{ .compatible = "rockchip,rk3228-usb2phy", .data = &rk3228_phy_cfgs },
14308c2ecf20Sopenharmony_ci	{ .compatible = "rockchip,rk3328-usb2phy", .data = &rk3328_phy_cfgs },
14318c2ecf20Sopenharmony_ci	{ .compatible = "rockchip,rk3366-usb2phy", .data = &rk3366_phy_cfgs },
14328c2ecf20Sopenharmony_ci	{ .compatible = "rockchip,rk3399-usb2phy", .data = &rk3399_phy_cfgs },
14338c2ecf20Sopenharmony_ci	{ .compatible = "rockchip,rv1108-usb2phy", .data = &rv1108_phy_cfgs },
14348c2ecf20Sopenharmony_ci	{}
14358c2ecf20Sopenharmony_ci};
14368c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, rockchip_usb2phy_dt_match);
14378c2ecf20Sopenharmony_ci
14388c2ecf20Sopenharmony_cistatic struct platform_driver rockchip_usb2phy_driver = {
14398c2ecf20Sopenharmony_ci	.probe		= rockchip_usb2phy_probe,
14408c2ecf20Sopenharmony_ci	.driver		= {
14418c2ecf20Sopenharmony_ci		.name	= "rockchip-usb2phy",
14428c2ecf20Sopenharmony_ci		.of_match_table = rockchip_usb2phy_dt_match,
14438c2ecf20Sopenharmony_ci	},
14448c2ecf20Sopenharmony_ci};
14458c2ecf20Sopenharmony_cimodule_platform_driver(rockchip_usb2phy_driver);
14468c2ecf20Sopenharmony_ci
14478c2ecf20Sopenharmony_ciMODULE_AUTHOR("Frank Wang <frank.wang@rock-chips.com>");
14488c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Rockchip USB2.0 PHY driver");
14498c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
1450