xref: /openbmc/linux/drivers/phy/freescale/phy-fsl-imx8mq-usb.c (revision f3c3091b98d5d52df40aaf27f11530701d02ac56)
1efe81beaSLi Jun // SPDX-License-Identifier: GPL-2.0+
2efe81beaSLi Jun /* Copyright (c) 2017 NXP. */
3efe81beaSLi Jun 
44708ee37SLi Jun #include <linux/bitfield.h>
5efe81beaSLi Jun #include <linux/clk.h>
64708ee37SLi Jun #include <linux/delay.h>
7efe81beaSLi Jun #include <linux/io.h>
8efe81beaSLi Jun #include <linux/module.h>
97559e757SRob Herring #include <linux/of.h>
10efe81beaSLi Jun #include <linux/phy/phy.h>
11efe81beaSLi Jun #include <linux/platform_device.h>
12eeda879bSLucas Stach #include <linux/regulator/consumer.h>
13efe81beaSLi Jun 
14efe81beaSLi Jun #define PHY_CTRL0			0x0
15efe81beaSLi Jun #define PHY_CTRL0_REF_SSP_EN		BIT(2)
164708ee37SLi Jun #define PHY_CTRL0_FSEL_MASK		GENMASK(10, 5)
174708ee37SLi Jun #define PHY_CTRL0_FSEL_24M		0x2a
18efe81beaSLi Jun 
19efe81beaSLi Jun #define PHY_CTRL1			0x4
20efe81beaSLi Jun #define PHY_CTRL1_RESET			BIT(0)
21efe81beaSLi Jun #define PHY_CTRL1_COMMONONN		BIT(1)
22efe81beaSLi Jun #define PHY_CTRL1_ATERESET		BIT(3)
23efe81beaSLi Jun #define PHY_CTRL1_VDATSRCENB0		BIT(19)
24efe81beaSLi Jun #define PHY_CTRL1_VDATDETENB0		BIT(20)
25efe81beaSLi Jun 
26efe81beaSLi Jun #define PHY_CTRL2			0x8
27efe81beaSLi Jun #define PHY_CTRL2_TXENABLEN0		BIT(8)
284708ee37SLi Jun #define PHY_CTRL2_OTG_DISABLE		BIT(9)
294708ee37SLi Jun 
3063c85ad0SLi Jun #define PHY_CTRL3			0xc
3163c85ad0SLi Jun #define PHY_CTRL3_COMPDISTUNE_MASK	GENMASK(2, 0)
3263c85ad0SLi Jun #define PHY_CTRL3_TXPREEMP_TUNE_MASK	GENMASK(16, 15)
3363c85ad0SLi Jun #define PHY_CTRL3_TXRISE_TUNE_MASK	GENMASK(21, 20)
3463c85ad0SLi Jun #define PHY_CTRL3_TXVREF_TUNE_MASK	GENMASK(25, 22)
3563c85ad0SLi Jun #define PHY_CTRL3_TX_VBOOST_LEVEL_MASK	GENMASK(31, 29)
3663c85ad0SLi Jun 
3763c85ad0SLi Jun #define PHY_CTRL4			0x10
3863c85ad0SLi Jun #define PHY_CTRL4_PCS_TX_DEEMPH_3P5DB_MASK	GENMASK(20, 15)
3963c85ad0SLi Jun 
4063c85ad0SLi Jun #define PHY_CTRL5			0x14
4163c85ad0SLi Jun #define PHY_CTRL5_DMPWD_OVERRIDE_SEL	BIT(23)
4263c85ad0SLi Jun #define PHY_CTRL5_DMPWD_OVERRIDE	BIT(22)
4363c85ad0SLi Jun #define PHY_CTRL5_DPPWD_OVERRIDE_SEL	BIT(21)
4463c85ad0SLi Jun #define PHY_CTRL5_DPPWD_OVERRIDE	BIT(20)
4563c85ad0SLi Jun #define PHY_CTRL5_PCS_TX_SWING_FULL_MASK	GENMASK(6, 0)
4663c85ad0SLi Jun 
474708ee37SLi Jun #define PHY_CTRL6			0x18
484708ee37SLi Jun #define PHY_CTRL6_ALT_CLK_EN		BIT(1)
494708ee37SLi Jun #define PHY_CTRL6_ALT_CLK_SEL		BIT(0)
50efe81beaSLi Jun 
5163c85ad0SLi Jun #define PHY_TUNE_DEFAULT		0xffffffff
5263c85ad0SLi Jun 
53efe81beaSLi Jun struct imx8mq_usb_phy {
54efe81beaSLi Jun 	struct phy *phy;
55efe81beaSLi Jun 	struct clk *clk;
56efe81beaSLi Jun 	void __iomem *base;
57eeda879bSLucas Stach 	struct regulator *vbus;
5863c85ad0SLi Jun 	u32 pcs_tx_swing_full;
5963c85ad0SLi Jun 	u32 pcs_tx_deemph_3p5db;
6063c85ad0SLi Jun 	u32 tx_vref_tune;
6163c85ad0SLi Jun 	u32 tx_rise_tune;
6263c85ad0SLi Jun 	u32 tx_preemp_amp_tune;
6363c85ad0SLi Jun 	u32 tx_vboost_level;
6463c85ad0SLi Jun 	u32 comp_dis_tune;
65efe81beaSLi Jun };
66efe81beaSLi Jun 
phy_tx_vref_tune_from_property(u32 percent)6763c85ad0SLi Jun static u32 phy_tx_vref_tune_from_property(u32 percent)
6863c85ad0SLi Jun {
6963c85ad0SLi Jun 	percent = clamp(percent, 94U, 124U);
7063c85ad0SLi Jun 
7163c85ad0SLi Jun 	return DIV_ROUND_CLOSEST(percent - 94U, 2);
7263c85ad0SLi Jun }
7363c85ad0SLi Jun 
phy_tx_rise_tune_from_property(u32 percent)7463c85ad0SLi Jun static u32 phy_tx_rise_tune_from_property(u32 percent)
7563c85ad0SLi Jun {
7663c85ad0SLi Jun 	switch (percent) {
7763c85ad0SLi Jun 	case 0 ... 98:
7863c85ad0SLi Jun 		return 3;
7963c85ad0SLi Jun 	case 99:
8063c85ad0SLi Jun 		return 2;
8163c85ad0SLi Jun 	case 100 ... 101:
8263c85ad0SLi Jun 		return 1;
8363c85ad0SLi Jun 	default:
8463c85ad0SLi Jun 		return 0;
8563c85ad0SLi Jun 	}
8663c85ad0SLi Jun }
8763c85ad0SLi Jun 
phy_tx_preemp_amp_tune_from_property(u32 microamp)8863c85ad0SLi Jun static u32 phy_tx_preemp_amp_tune_from_property(u32 microamp)
8963c85ad0SLi Jun {
9063c85ad0SLi Jun 	microamp = min(microamp, 1800U);
9163c85ad0SLi Jun 
9263c85ad0SLi Jun 	return microamp / 600;
9363c85ad0SLi Jun }
9463c85ad0SLi Jun 
phy_tx_vboost_level_from_property(u32 microvolt)9563c85ad0SLi Jun static u32 phy_tx_vboost_level_from_property(u32 microvolt)
9663c85ad0SLi Jun {
9763c85ad0SLi Jun 	switch (microvolt) {
9863c85ad0SLi Jun 	case 0 ... 960:
9963c85ad0SLi Jun 		return 0;
10063c85ad0SLi Jun 	case 961 ... 1160:
10163c85ad0SLi Jun 		return 2;
10263c85ad0SLi Jun 	default:
10363c85ad0SLi Jun 		return 3;
10463c85ad0SLi Jun 	}
10563c85ad0SLi Jun }
10663c85ad0SLi Jun 
phy_pcs_tx_deemph_3p5db_from_property(u32 decibel)10763c85ad0SLi Jun static u32 phy_pcs_tx_deemph_3p5db_from_property(u32 decibel)
10863c85ad0SLi Jun {
10963c85ad0SLi Jun 	return min(decibel, 36U);
11063c85ad0SLi Jun }
11163c85ad0SLi Jun 
phy_comp_dis_tune_from_property(u32 percent)11263c85ad0SLi Jun static u32 phy_comp_dis_tune_from_property(u32 percent)
11363c85ad0SLi Jun {
11463c85ad0SLi Jun 	switch (percent) {
11563c85ad0SLi Jun 	case 0 ... 92:
11663c85ad0SLi Jun 		return 0;
11763c85ad0SLi Jun 	case 93 ... 95:
11863c85ad0SLi Jun 		return 1;
11963c85ad0SLi Jun 	case 96 ... 97:
12063c85ad0SLi Jun 		return 2;
12163c85ad0SLi Jun 	case 98 ... 102:
12263c85ad0SLi Jun 		return 3;
12363c85ad0SLi Jun 	case 103 ... 105:
12463c85ad0SLi Jun 		return 4;
12563c85ad0SLi Jun 	case 106 ... 109:
12663c85ad0SLi Jun 		return 5;
12763c85ad0SLi Jun 	case 110 ... 113:
12863c85ad0SLi Jun 		return 6;
12963c85ad0SLi Jun 	default:
13063c85ad0SLi Jun 		return 7;
13163c85ad0SLi Jun 	}
13263c85ad0SLi Jun }
phy_pcs_tx_swing_full_from_property(u32 percent)13363c85ad0SLi Jun static u32 phy_pcs_tx_swing_full_from_property(u32 percent)
13463c85ad0SLi Jun {
13563c85ad0SLi Jun 	percent = min(percent, 100U);
13663c85ad0SLi Jun 
13763c85ad0SLi Jun 	return (percent * 127) / 100;
13863c85ad0SLi Jun }
13963c85ad0SLi Jun 
imx8m_get_phy_tuning_data(struct imx8mq_usb_phy * imx_phy)14063c85ad0SLi Jun static void imx8m_get_phy_tuning_data(struct imx8mq_usb_phy *imx_phy)
14163c85ad0SLi Jun {
14263c85ad0SLi Jun 	struct device *dev = imx_phy->phy->dev.parent;
14363c85ad0SLi Jun 
14463c85ad0SLi Jun 	if (device_property_read_u32(dev, "fsl,phy-tx-vref-tune-percent",
14563c85ad0SLi Jun 				     &imx_phy->tx_vref_tune))
14663c85ad0SLi Jun 		imx_phy->tx_vref_tune = PHY_TUNE_DEFAULT;
14763c85ad0SLi Jun 	else
14863c85ad0SLi Jun 		imx_phy->tx_vref_tune =
14963c85ad0SLi Jun 			phy_tx_vref_tune_from_property(imx_phy->tx_vref_tune);
15063c85ad0SLi Jun 
15163c85ad0SLi Jun 	if (device_property_read_u32(dev, "fsl,phy-tx-rise-tune-percent",
15263c85ad0SLi Jun 				     &imx_phy->tx_rise_tune))
15363c85ad0SLi Jun 		imx_phy->tx_rise_tune = PHY_TUNE_DEFAULT;
15463c85ad0SLi Jun 	else
15563c85ad0SLi Jun 		imx_phy->tx_rise_tune =
15663c85ad0SLi Jun 			phy_tx_rise_tune_from_property(imx_phy->tx_rise_tune);
15763c85ad0SLi Jun 
15863c85ad0SLi Jun 	if (device_property_read_u32(dev, "fsl,phy-tx-preemp-amp-tune-microamp",
15963c85ad0SLi Jun 				     &imx_phy->tx_preemp_amp_tune))
16063c85ad0SLi Jun 		imx_phy->tx_preemp_amp_tune = PHY_TUNE_DEFAULT;
16163c85ad0SLi Jun 	else
16263c85ad0SLi Jun 		imx_phy->tx_preemp_amp_tune =
16363c85ad0SLi Jun 			phy_tx_preemp_amp_tune_from_property(imx_phy->tx_preemp_amp_tune);
16463c85ad0SLi Jun 
16563c85ad0SLi Jun 	if (device_property_read_u32(dev, "fsl,phy-tx-vboost-level-microvolt",
16663c85ad0SLi Jun 				     &imx_phy->tx_vboost_level))
16763c85ad0SLi Jun 		imx_phy->tx_vboost_level = PHY_TUNE_DEFAULT;
16863c85ad0SLi Jun 	else
16963c85ad0SLi Jun 		imx_phy->tx_vboost_level =
17063c85ad0SLi Jun 			phy_tx_vboost_level_from_property(imx_phy->tx_vboost_level);
17163c85ad0SLi Jun 
17263c85ad0SLi Jun 	if (device_property_read_u32(dev, "fsl,phy-comp-dis-tune-percent",
17363c85ad0SLi Jun 				     &imx_phy->comp_dis_tune))
17463c85ad0SLi Jun 		imx_phy->comp_dis_tune = PHY_TUNE_DEFAULT;
17563c85ad0SLi Jun 	else
17663c85ad0SLi Jun 		imx_phy->comp_dis_tune =
17763c85ad0SLi Jun 			phy_comp_dis_tune_from_property(imx_phy->comp_dis_tune);
17863c85ad0SLi Jun 
179*954e1893SXu Yang 	if (device_property_read_u32(dev, "fsl,phy-pcs-tx-deemph-3p5db-attenuation-db",
18063c85ad0SLi Jun 				     &imx_phy->pcs_tx_deemph_3p5db))
18163c85ad0SLi Jun 		imx_phy->pcs_tx_deemph_3p5db = PHY_TUNE_DEFAULT;
18263c85ad0SLi Jun 	else
18363c85ad0SLi Jun 		imx_phy->pcs_tx_deemph_3p5db =
18463c85ad0SLi Jun 			phy_pcs_tx_deemph_3p5db_from_property(imx_phy->pcs_tx_deemph_3p5db);
18563c85ad0SLi Jun 
18663c85ad0SLi Jun 	if (device_property_read_u32(dev, "fsl,phy-pcs-tx-swing-full-percent",
18763c85ad0SLi Jun 				     &imx_phy->pcs_tx_swing_full))
18863c85ad0SLi Jun 		imx_phy->pcs_tx_swing_full = PHY_TUNE_DEFAULT;
18963c85ad0SLi Jun 	else
19063c85ad0SLi Jun 		imx_phy->pcs_tx_swing_full =
19163c85ad0SLi Jun 			phy_pcs_tx_swing_full_from_property(imx_phy->pcs_tx_swing_full);
19263c85ad0SLi Jun }
19363c85ad0SLi Jun 
imx8m_phy_tune(struct imx8mq_usb_phy * imx_phy)19463c85ad0SLi Jun static void imx8m_phy_tune(struct imx8mq_usb_phy *imx_phy)
19563c85ad0SLi Jun {
19663c85ad0SLi Jun 	u32 value;
19763c85ad0SLi Jun 
19863c85ad0SLi Jun 	/* PHY tuning */
19963c85ad0SLi Jun 	if (imx_phy->pcs_tx_deemph_3p5db != PHY_TUNE_DEFAULT) {
20063c85ad0SLi Jun 		value = readl(imx_phy->base + PHY_CTRL4);
20163c85ad0SLi Jun 		value &= ~PHY_CTRL4_PCS_TX_DEEMPH_3P5DB_MASK;
20263c85ad0SLi Jun 		value |= FIELD_PREP(PHY_CTRL4_PCS_TX_DEEMPH_3P5DB_MASK,
20363c85ad0SLi Jun 				   imx_phy->pcs_tx_deemph_3p5db);
20463c85ad0SLi Jun 		writel(value, imx_phy->base + PHY_CTRL4);
20563c85ad0SLi Jun 	}
20663c85ad0SLi Jun 
20763c85ad0SLi Jun 	if (imx_phy->pcs_tx_swing_full != PHY_TUNE_DEFAULT) {
20863c85ad0SLi Jun 		value = readl(imx_phy->base + PHY_CTRL5);
20963c85ad0SLi Jun 		value |= FIELD_PREP(PHY_CTRL5_PCS_TX_SWING_FULL_MASK,
21063c85ad0SLi Jun 				   imx_phy->pcs_tx_swing_full);
21163c85ad0SLi Jun 		writel(value, imx_phy->base + PHY_CTRL5);
21263c85ad0SLi Jun 	}
21363c85ad0SLi Jun 
21463c85ad0SLi Jun 	if ((imx_phy->tx_vref_tune & imx_phy->tx_rise_tune &
21563c85ad0SLi Jun 	     imx_phy->tx_preemp_amp_tune & imx_phy->comp_dis_tune &
21663c85ad0SLi Jun 	     imx_phy->tx_vboost_level) == PHY_TUNE_DEFAULT)
21763c85ad0SLi Jun 		/* If all are the default values, no need update. */
21863c85ad0SLi Jun 		return;
21963c85ad0SLi Jun 
22063c85ad0SLi Jun 	value = readl(imx_phy->base + PHY_CTRL3);
22163c85ad0SLi Jun 
22263c85ad0SLi Jun 	if (imx_phy->tx_vref_tune != PHY_TUNE_DEFAULT) {
22363c85ad0SLi Jun 		value &= ~PHY_CTRL3_TXVREF_TUNE_MASK;
22463c85ad0SLi Jun 		value |= FIELD_PREP(PHY_CTRL3_TXVREF_TUNE_MASK,
22563c85ad0SLi Jun 				   imx_phy->tx_vref_tune);
22663c85ad0SLi Jun 	}
22763c85ad0SLi Jun 
22863c85ad0SLi Jun 	if (imx_phy->tx_rise_tune != PHY_TUNE_DEFAULT) {
22963c85ad0SLi Jun 		value &= ~PHY_CTRL3_TXRISE_TUNE_MASK;
23063c85ad0SLi Jun 		value |= FIELD_PREP(PHY_CTRL3_TXRISE_TUNE_MASK,
23163c85ad0SLi Jun 				    imx_phy->tx_rise_tune);
23263c85ad0SLi Jun 	}
23363c85ad0SLi Jun 
23463c85ad0SLi Jun 	if (imx_phy->tx_preemp_amp_tune != PHY_TUNE_DEFAULT) {
23563c85ad0SLi Jun 		value &= ~PHY_CTRL3_TXPREEMP_TUNE_MASK;
23663c85ad0SLi Jun 		value |= FIELD_PREP(PHY_CTRL3_TXPREEMP_TUNE_MASK,
23763c85ad0SLi Jun 				imx_phy->tx_preemp_amp_tune);
23863c85ad0SLi Jun 	}
23963c85ad0SLi Jun 
24063c85ad0SLi Jun 	if (imx_phy->comp_dis_tune != PHY_TUNE_DEFAULT) {
24163c85ad0SLi Jun 		value &= ~PHY_CTRL3_COMPDISTUNE_MASK;
24263c85ad0SLi Jun 		value |= FIELD_PREP(PHY_CTRL3_COMPDISTUNE_MASK,
24363c85ad0SLi Jun 				    imx_phy->comp_dis_tune);
24463c85ad0SLi Jun 	}
24563c85ad0SLi Jun 
24663c85ad0SLi Jun 	if (imx_phy->tx_vboost_level != PHY_TUNE_DEFAULT) {
24763c85ad0SLi Jun 		value &= ~PHY_CTRL3_TX_VBOOST_LEVEL_MASK;
24863c85ad0SLi Jun 		value |= FIELD_PREP(PHY_CTRL3_TX_VBOOST_LEVEL_MASK,
24963c85ad0SLi Jun 				    imx_phy->tx_vboost_level);
25063c85ad0SLi Jun 	}
25163c85ad0SLi Jun 
25263c85ad0SLi Jun 	writel(value, imx_phy->base + PHY_CTRL3);
25363c85ad0SLi Jun }
25463c85ad0SLi Jun 
imx8mq_usb_phy_init(struct phy * phy)255efe81beaSLi Jun static int imx8mq_usb_phy_init(struct phy *phy)
256efe81beaSLi Jun {
257efe81beaSLi Jun 	struct imx8mq_usb_phy *imx_phy = phy_get_drvdata(phy);
258efe81beaSLi Jun 	u32 value;
259efe81beaSLi Jun 
260efe81beaSLi Jun 	value = readl(imx_phy->base + PHY_CTRL1);
261efe81beaSLi Jun 	value &= ~(PHY_CTRL1_VDATSRCENB0 | PHY_CTRL1_VDATDETENB0 |
262efe81beaSLi Jun 		   PHY_CTRL1_COMMONONN);
263efe81beaSLi Jun 	value |= PHY_CTRL1_RESET | PHY_CTRL1_ATERESET;
264efe81beaSLi Jun 	writel(value, imx_phy->base + PHY_CTRL1);
265efe81beaSLi Jun 
266efe81beaSLi Jun 	value = readl(imx_phy->base + PHY_CTRL0);
267efe81beaSLi Jun 	value |= PHY_CTRL0_REF_SSP_EN;
268efe81beaSLi Jun 	writel(value, imx_phy->base + PHY_CTRL0);
269efe81beaSLi Jun 
270efe81beaSLi Jun 	value = readl(imx_phy->base + PHY_CTRL2);
271efe81beaSLi Jun 	value |= PHY_CTRL2_TXENABLEN0;
272efe81beaSLi Jun 	writel(value, imx_phy->base + PHY_CTRL2);
273efe81beaSLi Jun 
274efe81beaSLi Jun 	value = readl(imx_phy->base + PHY_CTRL1);
275efe81beaSLi Jun 	value &= ~(PHY_CTRL1_RESET | PHY_CTRL1_ATERESET);
276efe81beaSLi Jun 	writel(value, imx_phy->base + PHY_CTRL1);
277efe81beaSLi Jun 
278efe81beaSLi Jun 	return 0;
279efe81beaSLi Jun }
280efe81beaSLi Jun 
imx8mp_usb_phy_init(struct phy * phy)2814708ee37SLi Jun static int imx8mp_usb_phy_init(struct phy *phy)
2824708ee37SLi Jun {
2834708ee37SLi Jun 	struct imx8mq_usb_phy *imx_phy = phy_get_drvdata(phy);
2844708ee37SLi Jun 	u32 value;
2854708ee37SLi Jun 
2864708ee37SLi Jun 	/* USB3.0 PHY signal fsel for 24M ref */
2874708ee37SLi Jun 	value = readl(imx_phy->base + PHY_CTRL0);
2884708ee37SLi Jun 	value &= ~PHY_CTRL0_FSEL_MASK;
2894708ee37SLi Jun 	value |= FIELD_PREP(PHY_CTRL0_FSEL_MASK, PHY_CTRL0_FSEL_24M);
2904708ee37SLi Jun 	writel(value, imx_phy->base + PHY_CTRL0);
2914708ee37SLi Jun 
2924708ee37SLi Jun 	/* Disable alt_clk_en and use internal MPLL clocks */
2934708ee37SLi Jun 	value = readl(imx_phy->base + PHY_CTRL6);
2944708ee37SLi Jun 	value &= ~(PHY_CTRL6_ALT_CLK_SEL | PHY_CTRL6_ALT_CLK_EN);
2954708ee37SLi Jun 	writel(value, imx_phy->base + PHY_CTRL6);
2964708ee37SLi Jun 
2974708ee37SLi Jun 	value = readl(imx_phy->base + PHY_CTRL1);
2984708ee37SLi Jun 	value &= ~(PHY_CTRL1_VDATSRCENB0 | PHY_CTRL1_VDATDETENB0);
2994708ee37SLi Jun 	value |= PHY_CTRL1_RESET | PHY_CTRL1_ATERESET;
3004708ee37SLi Jun 	writel(value, imx_phy->base + PHY_CTRL1);
3014708ee37SLi Jun 
3024708ee37SLi Jun 	value = readl(imx_phy->base + PHY_CTRL0);
3034708ee37SLi Jun 	value |= PHY_CTRL0_REF_SSP_EN;
3044708ee37SLi Jun 	writel(value, imx_phy->base + PHY_CTRL0);
3054708ee37SLi Jun 
3064708ee37SLi Jun 	value = readl(imx_phy->base + PHY_CTRL2);
3074708ee37SLi Jun 	value |= PHY_CTRL2_TXENABLEN0 | PHY_CTRL2_OTG_DISABLE;
3084708ee37SLi Jun 	writel(value, imx_phy->base + PHY_CTRL2);
3094708ee37SLi Jun 
3104708ee37SLi Jun 	udelay(10);
3114708ee37SLi Jun 
3124708ee37SLi Jun 	value = readl(imx_phy->base + PHY_CTRL1);
3134708ee37SLi Jun 	value &= ~(PHY_CTRL1_RESET | PHY_CTRL1_ATERESET);
3144708ee37SLi Jun 	writel(value, imx_phy->base + PHY_CTRL1);
3154708ee37SLi Jun 
31663c85ad0SLi Jun 	imx8m_phy_tune(imx_phy);
31763c85ad0SLi Jun 
3184708ee37SLi Jun 	return 0;
3194708ee37SLi Jun }
3204708ee37SLi Jun 
imx8mq_phy_power_on(struct phy * phy)321efe81beaSLi Jun static int imx8mq_phy_power_on(struct phy *phy)
322efe81beaSLi Jun {
323efe81beaSLi Jun 	struct imx8mq_usb_phy *imx_phy = phy_get_drvdata(phy);
324eeda879bSLucas Stach 	int ret;
325eeda879bSLucas Stach 
326eeda879bSLucas Stach 	ret = regulator_enable(imx_phy->vbus);
327eeda879bSLucas Stach 	if (ret)
328eeda879bSLucas Stach 		return ret;
329efe81beaSLi Jun 
330efe81beaSLi Jun 	return clk_prepare_enable(imx_phy->clk);
331efe81beaSLi Jun }
332efe81beaSLi Jun 
imx8mq_phy_power_off(struct phy * phy)333efe81beaSLi Jun static int imx8mq_phy_power_off(struct phy *phy)
334efe81beaSLi Jun {
335efe81beaSLi Jun 	struct imx8mq_usb_phy *imx_phy = phy_get_drvdata(phy);
336efe81beaSLi Jun 
337efe81beaSLi Jun 	clk_disable_unprepare(imx_phy->clk);
338eeda879bSLucas Stach 	regulator_disable(imx_phy->vbus);
339efe81beaSLi Jun 
340efe81beaSLi Jun 	return 0;
341efe81beaSLi Jun }
342efe81beaSLi Jun 
3432bf314d6SRikard Falkeborn static const struct phy_ops imx8mq_usb_phy_ops = {
344efe81beaSLi Jun 	.init		= imx8mq_usb_phy_init,
345efe81beaSLi Jun 	.power_on	= imx8mq_phy_power_on,
346efe81beaSLi Jun 	.power_off	= imx8mq_phy_power_off,
347efe81beaSLi Jun 	.owner		= THIS_MODULE,
348efe81beaSLi Jun };
349efe81beaSLi Jun 
350ae388532SRikard Falkeborn static const struct phy_ops imx8mp_usb_phy_ops = {
3514708ee37SLi Jun 	.init		= imx8mp_usb_phy_init,
3524708ee37SLi Jun 	.power_on	= imx8mq_phy_power_on,
3534708ee37SLi Jun 	.power_off	= imx8mq_phy_power_off,
3544708ee37SLi Jun 	.owner		= THIS_MODULE,
3554708ee37SLi Jun };
3564708ee37SLi Jun 
3574708ee37SLi Jun static const struct of_device_id imx8mq_usb_phy_of_match[] = {
3584708ee37SLi Jun 	{.compatible = "fsl,imx8mq-usb-phy",
3594708ee37SLi Jun 	 .data = &imx8mq_usb_phy_ops,},
3604708ee37SLi Jun 	{.compatible = "fsl,imx8mp-usb-phy",
3614708ee37SLi Jun 	 .data = &imx8mp_usb_phy_ops,},
3624708ee37SLi Jun 	{ }
3634708ee37SLi Jun };
3644708ee37SLi Jun MODULE_DEVICE_TABLE(of, imx8mq_usb_phy_of_match);
3654708ee37SLi Jun 
imx8mq_usb_phy_probe(struct platform_device * pdev)366efe81beaSLi Jun static int imx8mq_usb_phy_probe(struct platform_device *pdev)
367efe81beaSLi Jun {
368efe81beaSLi Jun 	struct phy_provider *phy_provider;
369efe81beaSLi Jun 	struct device *dev = &pdev->dev;
370efe81beaSLi Jun 	struct imx8mq_usb_phy *imx_phy;
3714708ee37SLi Jun 	const struct phy_ops *phy_ops;
372efe81beaSLi Jun 
373efe81beaSLi Jun 	imx_phy = devm_kzalloc(dev, sizeof(*imx_phy), GFP_KERNEL);
374efe81beaSLi Jun 	if (!imx_phy)
375efe81beaSLi Jun 		return -ENOMEM;
376efe81beaSLi Jun 
377efe81beaSLi Jun 	imx_phy->clk = devm_clk_get(dev, "phy");
378efe81beaSLi Jun 	if (IS_ERR(imx_phy->clk)) {
379efe81beaSLi Jun 		dev_err(dev, "failed to get imx8mq usb phy clock\n");
380efe81beaSLi Jun 		return PTR_ERR(imx_phy->clk);
381efe81beaSLi Jun 	}
382efe81beaSLi Jun 
3830b7c4c88SChunfeng Yun 	imx_phy->base = devm_platform_ioremap_resource(pdev, 0);
384efe81beaSLi Jun 	if (IS_ERR(imx_phy->base))
385efe81beaSLi Jun 		return PTR_ERR(imx_phy->base);
386efe81beaSLi Jun 
3874708ee37SLi Jun 	phy_ops = of_device_get_match_data(dev);
3884708ee37SLi Jun 	if (!phy_ops)
3894708ee37SLi Jun 		return -EINVAL;
3904708ee37SLi Jun 
3914708ee37SLi Jun 	imx_phy->phy = devm_phy_create(dev, NULL, phy_ops);
392efe81beaSLi Jun 	if (IS_ERR(imx_phy->phy))
393efe81beaSLi Jun 		return PTR_ERR(imx_phy->phy);
394efe81beaSLi Jun 
395eeda879bSLucas Stach 	imx_phy->vbus = devm_regulator_get(dev, "vbus");
396eeda879bSLucas Stach 	if (IS_ERR(imx_phy->vbus))
3979f266c1cSAlexander Stein 		return dev_err_probe(dev, PTR_ERR(imx_phy->vbus), "failed to get vbus\n");
398eeda879bSLucas Stach 
399efe81beaSLi Jun 	phy_set_drvdata(imx_phy->phy, imx_phy);
400efe81beaSLi Jun 
40163c85ad0SLi Jun 	imx8m_get_phy_tuning_data(imx_phy);
40263c85ad0SLi Jun 
403efe81beaSLi Jun 	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
404efe81beaSLi Jun 
405efe81beaSLi Jun 	return PTR_ERR_OR_ZERO(phy_provider);
406efe81beaSLi Jun }
407efe81beaSLi Jun 
408efe81beaSLi Jun static struct platform_driver imx8mq_usb_phy_driver = {
409efe81beaSLi Jun 	.probe	= imx8mq_usb_phy_probe,
410efe81beaSLi Jun 	.driver = {
411efe81beaSLi Jun 		.name	= "imx8mq-usb-phy",
412efe81beaSLi Jun 		.of_match_table	= imx8mq_usb_phy_of_match,
413efe81beaSLi Jun 	}
414efe81beaSLi Jun };
415efe81beaSLi Jun module_platform_driver(imx8mq_usb_phy_driver);
416efe81beaSLi Jun 
417efe81beaSLi Jun MODULE_DESCRIPTION("FSL IMX8MQ USB PHY driver");
418efe81beaSLi Jun MODULE_LICENSE("GPL");
419