xref: /linux/drivers/phy/amlogic/phy-meson-g12a-usb2.c (revision 014b35d9326b76942406501c465f06e096018175)
116df8bcbSNeil Armstrong // SPDX-License-Identifier: GPL-2.0
216df8bcbSNeil Armstrong /*
316df8bcbSNeil Armstrong  * Meson G12A USB2 PHY driver
416df8bcbSNeil Armstrong  *
516df8bcbSNeil Armstrong  * Copyright (C) 2017 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
616df8bcbSNeil Armstrong  * Copyright (C) 2017 Amlogic, Inc. All rights reserved
716df8bcbSNeil Armstrong  * Copyright (C) 2019 BayLibre, SAS
816df8bcbSNeil Armstrong  * Author: Neil Armstrong <narmstrong@baylibre.com>
916df8bcbSNeil Armstrong  */
1016df8bcbSNeil Armstrong 
1116df8bcbSNeil Armstrong #include <linux/bitfield.h>
1216df8bcbSNeil Armstrong #include <linux/bitops.h>
1316df8bcbSNeil Armstrong #include <linux/clk.h>
1416df8bcbSNeil Armstrong #include <linux/delay.h>
1516df8bcbSNeil Armstrong #include <linux/io.h>
1616df8bcbSNeil Armstrong #include <linux/module.h>
1716df8bcbSNeil Armstrong #include <linux/of_device.h>
1816df8bcbSNeil Armstrong #include <linux/regmap.h>
1916df8bcbSNeil Armstrong #include <linux/reset.h>
2016df8bcbSNeil Armstrong #include <linux/phy/phy.h>
2116df8bcbSNeil Armstrong #include <linux/platform_device.h>
2216df8bcbSNeil Armstrong 
2316df8bcbSNeil Armstrong #define PHY_CTRL_R0						0x0
2416df8bcbSNeil Armstrong #define PHY_CTRL_R1						0x4
2516df8bcbSNeil Armstrong #define PHY_CTRL_R2						0x8
2616df8bcbSNeil Armstrong #define PHY_CTRL_R3						0xc
2716df8bcbSNeil Armstrong 	#define PHY_CTRL_R3_SQUELCH_REF				GENMASK(1, 0)
2816df8bcbSNeil Armstrong 	#define PHY_CTRL_R3_HSDIC_REF				GENMASK(3, 2)
2916df8bcbSNeil Armstrong 	#define PHY_CTRL_R3_DISC_THRESH				GENMASK(7, 4)
3016df8bcbSNeil Armstrong 
3116df8bcbSNeil Armstrong #define PHY_CTRL_R4						0x10
3216df8bcbSNeil Armstrong 	#define PHY_CTRL_R4_CALIB_CODE_7_0			GENMASK(7, 0)
3316df8bcbSNeil Armstrong 	#define PHY_CTRL_R4_CALIB_CODE_15_8			GENMASK(15, 8)
3416df8bcbSNeil Armstrong 	#define PHY_CTRL_R4_CALIB_CODE_23_16			GENMASK(23, 16)
3516df8bcbSNeil Armstrong 	#define PHY_CTRL_R4_I_C2L_CAL_EN			BIT(24)
3616df8bcbSNeil Armstrong 	#define PHY_CTRL_R4_I_C2L_CAL_RESET_N			BIT(25)
3716df8bcbSNeil Armstrong 	#define PHY_CTRL_R4_I_C2L_CAL_DONE			BIT(26)
3816df8bcbSNeil Armstrong 	#define PHY_CTRL_R4_TEST_BYPASS_MODE_EN			BIT(27)
3916df8bcbSNeil Armstrong 	#define PHY_CTRL_R4_I_C2L_BIAS_TRIM_1_0			GENMASK(29, 28)
4016df8bcbSNeil Armstrong 	#define PHY_CTRL_R4_I_C2L_BIAS_TRIM_3_2			GENMASK(31, 30)
4116df8bcbSNeil Armstrong 
4216df8bcbSNeil Armstrong #define PHY_CTRL_R5						0x14
4316df8bcbSNeil Armstrong #define PHY_CTRL_R6						0x18
4416df8bcbSNeil Armstrong #define PHY_CTRL_R7						0x1c
4516df8bcbSNeil Armstrong #define PHY_CTRL_R8						0x20
4616df8bcbSNeil Armstrong #define PHY_CTRL_R9						0x24
4716df8bcbSNeil Armstrong #define PHY_CTRL_R10						0x28
4816df8bcbSNeil Armstrong #define PHY_CTRL_R11						0x2c
4916df8bcbSNeil Armstrong #define PHY_CTRL_R12						0x30
5016df8bcbSNeil Armstrong #define PHY_CTRL_R13						0x34
5116df8bcbSNeil Armstrong 	#define PHY_CTRL_R13_CUSTOM_PATTERN_19			GENMASK(7, 0)
5216df8bcbSNeil Armstrong 	#define PHY_CTRL_R13_LOAD_STAT				BIT(14)
5316df8bcbSNeil Armstrong 	#define PHY_CTRL_R13_UPDATE_PMA_SIGNALS			BIT(15)
5416df8bcbSNeil Armstrong 	#define PHY_CTRL_R13_MIN_COUNT_FOR_SYNC_DET		GENMASK(20, 16)
5516df8bcbSNeil Armstrong 	#define PHY_CTRL_R13_CLEAR_HOLD_HS_DISCONNECT		BIT(21)
5616df8bcbSNeil Armstrong 	#define PHY_CTRL_R13_BYPASS_HOST_DISCONNECT_VAL		BIT(22)
5716df8bcbSNeil Armstrong 	#define PHY_CTRL_R13_BYPASS_HOST_DISCONNECT_EN		BIT(23)
5816df8bcbSNeil Armstrong 	#define PHY_CTRL_R13_I_C2L_HS_EN			BIT(24)
5916df8bcbSNeil Armstrong 	#define PHY_CTRL_R13_I_C2L_FS_EN			BIT(25)
6016df8bcbSNeil Armstrong 	#define PHY_CTRL_R13_I_C2L_LS_EN			BIT(26)
6116df8bcbSNeil Armstrong 	#define PHY_CTRL_R13_I_C2L_HS_OE			BIT(27)
6216df8bcbSNeil Armstrong 	#define PHY_CTRL_R13_I_C2L_FS_OE			BIT(28)
6316df8bcbSNeil Armstrong 	#define PHY_CTRL_R13_I_C2L_HS_RX_EN			BIT(29)
6416df8bcbSNeil Armstrong 	#define PHY_CTRL_R13_I_C2L_FSLS_RX_EN			BIT(30)
6516df8bcbSNeil Armstrong 
6616df8bcbSNeil Armstrong #define PHY_CTRL_R14						0x38
6716df8bcbSNeil Armstrong 	#define PHY_CTRL_R14_I_RDP_EN				BIT(0)
6816df8bcbSNeil Armstrong 	#define PHY_CTRL_R14_I_RPU_SW1_EN			BIT(1)
69*014b35d9SJoe Perches 	#define PHY_CTRL_R14_I_RPU_SW2_EN			GENMASK(3, 2)
7016df8bcbSNeil Armstrong 	#define PHY_CTRL_R14_PG_RSTN				BIT(4)
7116df8bcbSNeil Armstrong 	#define PHY_CTRL_R14_I_C2L_DATA_16_8			BIT(5)
7216df8bcbSNeil Armstrong 	#define PHY_CTRL_R14_I_C2L_ASSERT_SINGLE_EN_ZERO	BIT(6)
7316df8bcbSNeil Armstrong 	#define PHY_CTRL_R14_BYPASS_CTRL_7_0			GENMASK(15, 8)
7416df8bcbSNeil Armstrong 	#define PHY_CTRL_R14_BYPASS_CTRL_15_8			GENMASK(23, 16)
7516df8bcbSNeil Armstrong 
7616df8bcbSNeil Armstrong #define PHY_CTRL_R15						0x3c
7716df8bcbSNeil Armstrong #define PHY_CTRL_R16						0x40
7816df8bcbSNeil Armstrong 	#define PHY_CTRL_R16_MPLL_M				GENMASK(8, 0)
7916df8bcbSNeil Armstrong 	#define PHY_CTRL_R16_MPLL_N				GENMASK(14, 10)
8016df8bcbSNeil Armstrong 	#define PHY_CTRL_R16_MPLL_TDC_MODE			BIT(20)
8116df8bcbSNeil Armstrong 	#define PHY_CTRL_R16_MPLL_SDM_EN			BIT(21)
8216df8bcbSNeil Armstrong 	#define PHY_CTRL_R16_MPLL_LOAD				BIT(22)
8316df8bcbSNeil Armstrong 	#define PHY_CTRL_R16_MPLL_DCO_SDM_EN			BIT(23)
8416df8bcbSNeil Armstrong 	#define PHY_CTRL_R16_MPLL_LOCK_LONG			GENMASK(25, 24)
8516df8bcbSNeil Armstrong 	#define PHY_CTRL_R16_MPLL_LOCK_F			BIT(26)
8616df8bcbSNeil Armstrong 	#define PHY_CTRL_R16_MPLL_FAST_LOCK			BIT(27)
8716df8bcbSNeil Armstrong 	#define PHY_CTRL_R16_MPLL_EN				BIT(28)
8816df8bcbSNeil Armstrong 	#define PHY_CTRL_R16_MPLL_RESET				BIT(29)
8916df8bcbSNeil Armstrong 	#define PHY_CTRL_R16_MPLL_LOCK				BIT(30)
9016df8bcbSNeil Armstrong 	#define PHY_CTRL_R16_MPLL_LOCK_DIG			BIT(31)
9116df8bcbSNeil Armstrong 
9216df8bcbSNeil Armstrong #define PHY_CTRL_R17						0x44
9316df8bcbSNeil Armstrong 	#define PHY_CTRL_R17_MPLL_FRAC_IN			GENMASK(13, 0)
9416df8bcbSNeil Armstrong 	#define PHY_CTRL_R17_MPLL_FIX_EN			BIT(16)
9516df8bcbSNeil Armstrong 	#define PHY_CTRL_R17_MPLL_LAMBDA1			GENMASK(19, 17)
9616df8bcbSNeil Armstrong 	#define PHY_CTRL_R17_MPLL_LAMBDA0			GENMASK(22, 20)
9716df8bcbSNeil Armstrong 	#define PHY_CTRL_R17_MPLL_FILTER_MODE			BIT(23)
9816df8bcbSNeil Armstrong 	#define PHY_CTRL_R17_MPLL_FILTER_PVT2			GENMASK(27, 24)
9916df8bcbSNeil Armstrong 	#define PHY_CTRL_R17_MPLL_FILTER_PVT1			GENMASK(31, 28)
10016df8bcbSNeil Armstrong 
10116df8bcbSNeil Armstrong #define PHY_CTRL_R18						0x48
10216df8bcbSNeil Armstrong 	#define PHY_CTRL_R18_MPLL_LKW_SEL			GENMASK(1, 0)
10316df8bcbSNeil Armstrong 	#define PHY_CTRL_R18_MPLL_LK_W				GENMASK(5, 2)
10416df8bcbSNeil Armstrong 	#define PHY_CTRL_R18_MPLL_LK_S				GENMASK(11, 6)
10516df8bcbSNeil Armstrong 	#define PHY_CTRL_R18_MPLL_DCO_M_EN			BIT(12)
10616df8bcbSNeil Armstrong 	#define PHY_CTRL_R18_MPLL_DCO_CLK_SEL			BIT(13)
10716df8bcbSNeil Armstrong 	#define PHY_CTRL_R18_MPLL_PFD_GAIN			GENMASK(15, 14)
10816df8bcbSNeil Armstrong 	#define PHY_CTRL_R18_MPLL_ROU				GENMASK(18, 16)
10916df8bcbSNeil Armstrong 	#define PHY_CTRL_R18_MPLL_DATA_SEL			GENMASK(21, 19)
11016df8bcbSNeil Armstrong 	#define PHY_CTRL_R18_MPLL_BIAS_ADJ			GENMASK(23, 22)
11116df8bcbSNeil Armstrong 	#define PHY_CTRL_R18_MPLL_BB_MODE			GENMASK(25, 24)
11216df8bcbSNeil Armstrong 	#define PHY_CTRL_R18_MPLL_ALPHA				GENMASK(28, 26)
11316df8bcbSNeil Armstrong 	#define PHY_CTRL_R18_MPLL_ADJ_LDO			GENMASK(30, 29)
11416df8bcbSNeil Armstrong 	#define PHY_CTRL_R18_MPLL_ACG_RANGE			BIT(31)
11516df8bcbSNeil Armstrong 
11616df8bcbSNeil Armstrong #define PHY_CTRL_R19						0x4c
11716df8bcbSNeil Armstrong #define PHY_CTRL_R20						0x50
11816df8bcbSNeil Armstrong 	#define PHY_CTRL_R20_USB2_IDDET_EN			BIT(0)
11916df8bcbSNeil Armstrong 	#define PHY_CTRL_R20_USB2_OTG_VBUS_TRIM_2_0		GENMASK(3, 1)
12016df8bcbSNeil Armstrong 	#define PHY_CTRL_R20_USB2_OTG_VBUSDET_EN		BIT(4)
12116df8bcbSNeil Armstrong 	#define PHY_CTRL_R20_USB2_AMON_EN			BIT(5)
12216df8bcbSNeil Armstrong 	#define PHY_CTRL_R20_USB2_CAL_CODE_R5			BIT(6)
12316df8bcbSNeil Armstrong 	#define PHY_CTRL_R20_BYPASS_OTG_DET			BIT(7)
12416df8bcbSNeil Armstrong 	#define PHY_CTRL_R20_USB2_DMON_EN			BIT(8)
12516df8bcbSNeil Armstrong 	#define PHY_CTRL_R20_USB2_DMON_SEL_3_0			GENMASK(12, 9)
12616df8bcbSNeil Armstrong 	#define PHY_CTRL_R20_USB2_EDGE_DRV_EN			BIT(13)
12716df8bcbSNeil Armstrong 	#define PHY_CTRL_R20_USB2_EDGE_DRV_TRIM_1_0		GENMASK(15, 14)
12816df8bcbSNeil Armstrong 	#define PHY_CTRL_R20_USB2_BGR_ADJ_4_0			GENMASK(20, 16)
12916df8bcbSNeil Armstrong 	#define PHY_CTRL_R20_USB2_BGR_START			BIT(21)
13016df8bcbSNeil Armstrong 	#define PHY_CTRL_R20_USB2_BGR_VREF_4_0			GENMASK(28, 24)
13116df8bcbSNeil Armstrong 	#define PHY_CTRL_R20_USB2_BGR_DBG_1_0			GENMASK(30, 29)
13216df8bcbSNeil Armstrong 	#define PHY_CTRL_R20_BYPASS_CAL_DONE_R5			BIT(31)
13316df8bcbSNeil Armstrong 
13416df8bcbSNeil Armstrong #define PHY_CTRL_R21						0x54
13516df8bcbSNeil Armstrong 	#define PHY_CTRL_R21_USB2_BGR_FORCE			BIT(0)
13616df8bcbSNeil Armstrong 	#define PHY_CTRL_R21_USB2_CAL_ACK_EN			BIT(1)
13716df8bcbSNeil Armstrong 	#define PHY_CTRL_R21_USB2_OTG_ACA_EN			BIT(2)
13816df8bcbSNeil Armstrong 	#define PHY_CTRL_R21_USB2_TX_STRG_PD			BIT(3)
13916df8bcbSNeil Armstrong 	#define PHY_CTRL_R21_USB2_OTG_ACA_TRIM_1_0		GENMASK(5, 4)
14016df8bcbSNeil Armstrong 	#define PHY_CTRL_R21_BYPASS_UTMI_CNTR			GENMASK(15, 6)
14116df8bcbSNeil Armstrong 	#define PHY_CTRL_R21_BYPASS_UTMI_REG			GENMASK(25, 20)
14216df8bcbSNeil Armstrong 
14316df8bcbSNeil Armstrong #define PHY_CTRL_R22						0x58
14416df8bcbSNeil Armstrong #define PHY_CTRL_R23						0x5c
14516df8bcbSNeil Armstrong 
14616df8bcbSNeil Armstrong #define RESET_COMPLETE_TIME					1000
14716df8bcbSNeil Armstrong #define PLL_RESET_COMPLETE_TIME					100
14816df8bcbSNeil Armstrong 
14916df8bcbSNeil Armstrong struct phy_meson_g12a_usb2_priv {
15016df8bcbSNeil Armstrong 	struct device		*dev;
15116df8bcbSNeil Armstrong 	struct regmap		*regmap;
15216df8bcbSNeil Armstrong 	struct clk		*clk;
15316df8bcbSNeil Armstrong 	struct reset_control	*reset;
15416df8bcbSNeil Armstrong };
15516df8bcbSNeil Armstrong 
15616df8bcbSNeil Armstrong static const struct regmap_config phy_meson_g12a_usb2_regmap_conf = {
15716df8bcbSNeil Armstrong 	.reg_bits = 8,
15816df8bcbSNeil Armstrong 	.val_bits = 32,
15916df8bcbSNeil Armstrong 	.reg_stride = 4,
16016df8bcbSNeil Armstrong 	.max_register = PHY_CTRL_R23,
16116df8bcbSNeil Armstrong };
16216df8bcbSNeil Armstrong 
16316df8bcbSNeil Armstrong static int phy_meson_g12a_usb2_init(struct phy *phy)
16416df8bcbSNeil Armstrong {
16516df8bcbSNeil Armstrong 	struct phy_meson_g12a_usb2_priv *priv = phy_get_drvdata(phy);
16616df8bcbSNeil Armstrong 	int ret;
16716df8bcbSNeil Armstrong 
16816df8bcbSNeil Armstrong 	ret = reset_control_reset(priv->reset);
16916df8bcbSNeil Armstrong 	if (ret)
17016df8bcbSNeil Armstrong 		return ret;
17116df8bcbSNeil Armstrong 
17216df8bcbSNeil Armstrong 	udelay(RESET_COMPLETE_TIME);
17316df8bcbSNeil Armstrong 
17416df8bcbSNeil Armstrong 	/* usb2_otg_aca_en == 0 */
17516df8bcbSNeil Armstrong 	regmap_update_bits(priv->regmap, PHY_CTRL_R21,
17616df8bcbSNeil Armstrong 			   PHY_CTRL_R21_USB2_OTG_ACA_EN, 0);
17716df8bcbSNeil Armstrong 
17816df8bcbSNeil Armstrong 	/* PLL Setup : 24MHz * 20 / 1 = 480MHz */
17916df8bcbSNeil Armstrong 	regmap_write(priv->regmap, PHY_CTRL_R16,
18016df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R16_MPLL_M, 20) |
18116df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R16_MPLL_N, 1) |
18216df8bcbSNeil Armstrong 		     PHY_CTRL_R16_MPLL_LOAD |
18316df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R16_MPLL_LOCK_LONG, 1) |
18416df8bcbSNeil Armstrong 		     PHY_CTRL_R16_MPLL_FAST_LOCK |
18516df8bcbSNeil Armstrong 		     PHY_CTRL_R16_MPLL_EN |
18616df8bcbSNeil Armstrong 		     PHY_CTRL_R16_MPLL_RESET);
18716df8bcbSNeil Armstrong 
18816df8bcbSNeil Armstrong 	regmap_write(priv->regmap, PHY_CTRL_R17,
18916df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R17_MPLL_FRAC_IN, 0) |
19016df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R17_MPLL_LAMBDA1, 7) |
19116df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R17_MPLL_LAMBDA0, 7) |
19216df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R17_MPLL_FILTER_PVT2, 2) |
19316df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R17_MPLL_FILTER_PVT1, 9));
19416df8bcbSNeil Armstrong 
19516df8bcbSNeil Armstrong 	regmap_write(priv->regmap, PHY_CTRL_R18,
19616df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R18_MPLL_LKW_SEL, 1) |
19716df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R18_MPLL_LK_W, 9) |
19816df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R18_MPLL_LK_S, 0x27) |
19916df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R18_MPLL_PFD_GAIN, 1) |
20016df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R18_MPLL_ROU, 7) |
20116df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R18_MPLL_DATA_SEL, 3) |
20216df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R18_MPLL_BIAS_ADJ, 1) |
20316df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R18_MPLL_BB_MODE, 0) |
20416df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R18_MPLL_ALPHA, 3) |
20516df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R18_MPLL_ADJ_LDO, 1) |
20616df8bcbSNeil Armstrong 		     PHY_CTRL_R18_MPLL_ACG_RANGE);
20716df8bcbSNeil Armstrong 
20816df8bcbSNeil Armstrong 	udelay(PLL_RESET_COMPLETE_TIME);
20916df8bcbSNeil Armstrong 
21016df8bcbSNeil Armstrong 	/* UnReset PLL */
21116df8bcbSNeil Armstrong 	regmap_write(priv->regmap, PHY_CTRL_R16,
21216df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R16_MPLL_M, 20) |
21316df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R16_MPLL_N, 1) |
21416df8bcbSNeil Armstrong 		     PHY_CTRL_R16_MPLL_LOAD |
21516df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R16_MPLL_LOCK_LONG, 1) |
21616df8bcbSNeil Armstrong 		     PHY_CTRL_R16_MPLL_FAST_LOCK |
21716df8bcbSNeil Armstrong 		     PHY_CTRL_R16_MPLL_EN);
21816df8bcbSNeil Armstrong 
21916df8bcbSNeil Armstrong 	/* PHY Tuning */
22016df8bcbSNeil Armstrong 	regmap_write(priv->regmap, PHY_CTRL_R20,
22116df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R20_USB2_OTG_VBUS_TRIM_2_0, 4) |
22216df8bcbSNeil Armstrong 		     PHY_CTRL_R20_USB2_OTG_VBUSDET_EN |
22316df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R20_USB2_DMON_SEL_3_0, 15) |
22416df8bcbSNeil Armstrong 		     PHY_CTRL_R20_USB2_EDGE_DRV_EN |
22516df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R20_USB2_EDGE_DRV_TRIM_1_0, 3) |
22616df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R20_USB2_BGR_ADJ_4_0, 0) |
22716df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R20_USB2_BGR_VREF_4_0, 0) |
22816df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R20_USB2_BGR_DBG_1_0, 0));
22916df8bcbSNeil Armstrong 
23016df8bcbSNeil Armstrong 	regmap_write(priv->regmap, PHY_CTRL_R4,
23116df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R4_CALIB_CODE_7_0, 0xf) |
23216df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R4_CALIB_CODE_15_8, 0xf) |
23316df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R4_CALIB_CODE_23_16, 0xf) |
23416df8bcbSNeil Armstrong 		     PHY_CTRL_R4_TEST_BYPASS_MODE_EN |
23516df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R4_I_C2L_BIAS_TRIM_1_0, 0) |
23616df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R4_I_C2L_BIAS_TRIM_3_2, 0));
23716df8bcbSNeil Armstrong 
23816df8bcbSNeil Armstrong 	/* Tuning Disconnect Threshold */
23916df8bcbSNeil Armstrong 	regmap_write(priv->regmap, PHY_CTRL_R3,
24016df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R3_SQUELCH_REF, 0) |
24116df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R3_HSDIC_REF, 1) |
24216df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R3_DISC_THRESH, 3));
24316df8bcbSNeil Armstrong 
24416df8bcbSNeil Armstrong 	/* Analog Settings */
24516df8bcbSNeil Armstrong 	regmap_write(priv->regmap, PHY_CTRL_R14, 0);
24616df8bcbSNeil Armstrong 	regmap_write(priv->regmap, PHY_CTRL_R13,
24716df8bcbSNeil Armstrong 		     PHY_CTRL_R13_UPDATE_PMA_SIGNALS |
24816df8bcbSNeil Armstrong 		     FIELD_PREP(PHY_CTRL_R13_MIN_COUNT_FOR_SYNC_DET, 7));
24916df8bcbSNeil Armstrong 
25016df8bcbSNeil Armstrong 	return 0;
25116df8bcbSNeil Armstrong }
25216df8bcbSNeil Armstrong 
25316df8bcbSNeil Armstrong static int phy_meson_g12a_usb2_exit(struct phy *phy)
25416df8bcbSNeil Armstrong {
25516df8bcbSNeil Armstrong 	struct phy_meson_g12a_usb2_priv *priv = phy_get_drvdata(phy);
25616df8bcbSNeil Armstrong 
25716df8bcbSNeil Armstrong 	return reset_control_reset(priv->reset);
25816df8bcbSNeil Armstrong }
25916df8bcbSNeil Armstrong 
26016df8bcbSNeil Armstrong /* set_mode is not needed, mode setting is handled via the UTMI bus */
26116df8bcbSNeil Armstrong static const struct phy_ops phy_meson_g12a_usb2_ops = {
26216df8bcbSNeil Armstrong 	.init		= phy_meson_g12a_usb2_init,
26316df8bcbSNeil Armstrong 	.exit		= phy_meson_g12a_usb2_exit,
26416df8bcbSNeil Armstrong 	.owner		= THIS_MODULE,
26516df8bcbSNeil Armstrong };
26616df8bcbSNeil Armstrong 
26716df8bcbSNeil Armstrong static int phy_meson_g12a_usb2_probe(struct platform_device *pdev)
26816df8bcbSNeil Armstrong {
26916df8bcbSNeil Armstrong 	struct device *dev = &pdev->dev;
27016df8bcbSNeil Armstrong 	struct phy_provider *phy_provider;
27116df8bcbSNeil Armstrong 	struct resource *res;
27216df8bcbSNeil Armstrong 	struct phy_meson_g12a_usb2_priv *priv;
27316df8bcbSNeil Armstrong 	struct phy *phy;
27416df8bcbSNeil Armstrong 	void __iomem *base;
27516df8bcbSNeil Armstrong 	int ret;
27616df8bcbSNeil Armstrong 
27716df8bcbSNeil Armstrong 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
27816df8bcbSNeil Armstrong 	if (!priv)
27916df8bcbSNeil Armstrong 		return -ENOMEM;
28016df8bcbSNeil Armstrong 
28116df8bcbSNeil Armstrong 	priv->dev = dev;
28216df8bcbSNeil Armstrong 	platform_set_drvdata(pdev, priv);
28316df8bcbSNeil Armstrong 
28416df8bcbSNeil Armstrong 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
28516df8bcbSNeil Armstrong 	base = devm_ioremap_resource(dev, res);
28616df8bcbSNeil Armstrong 	if (IS_ERR(base))
28716df8bcbSNeil Armstrong 		return PTR_ERR(base);
28816df8bcbSNeil Armstrong 
28916df8bcbSNeil Armstrong 	priv->regmap = devm_regmap_init_mmio(dev, base,
29016df8bcbSNeil Armstrong 					     &phy_meson_g12a_usb2_regmap_conf);
29116df8bcbSNeil Armstrong 	if (IS_ERR(priv->regmap))
29216df8bcbSNeil Armstrong 		return PTR_ERR(priv->regmap);
29316df8bcbSNeil Armstrong 
29416df8bcbSNeil Armstrong 	priv->clk = devm_clk_get(dev, "xtal");
29516df8bcbSNeil Armstrong 	if (IS_ERR(priv->clk))
29616df8bcbSNeil Armstrong 		return PTR_ERR(priv->clk);
29716df8bcbSNeil Armstrong 
29816df8bcbSNeil Armstrong 	priv->reset = devm_reset_control_get(dev, "phy");
29916df8bcbSNeil Armstrong 	if (IS_ERR(priv->reset))
30016df8bcbSNeil Armstrong 		return PTR_ERR(priv->reset);
30116df8bcbSNeil Armstrong 
30216df8bcbSNeil Armstrong 	ret = reset_control_deassert(priv->reset);
30316df8bcbSNeil Armstrong 	if (ret)
30416df8bcbSNeil Armstrong 		return ret;
30516df8bcbSNeil Armstrong 
30616df8bcbSNeil Armstrong 	phy = devm_phy_create(dev, NULL, &phy_meson_g12a_usb2_ops);
30716df8bcbSNeil Armstrong 	if (IS_ERR(phy)) {
30816df8bcbSNeil Armstrong 		ret = PTR_ERR(phy);
30916df8bcbSNeil Armstrong 		if (ret != -EPROBE_DEFER)
31016df8bcbSNeil Armstrong 			dev_err(dev, "failed to create PHY\n");
31116df8bcbSNeil Armstrong 
31216df8bcbSNeil Armstrong 		return ret;
31316df8bcbSNeil Armstrong 	}
31416df8bcbSNeil Armstrong 
31516df8bcbSNeil Armstrong 	phy_set_bus_width(phy, 8);
31616df8bcbSNeil Armstrong 	phy_set_drvdata(phy, priv);
31716df8bcbSNeil Armstrong 
31816df8bcbSNeil Armstrong 	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
31916df8bcbSNeil Armstrong 
32016df8bcbSNeil Armstrong 	return PTR_ERR_OR_ZERO(phy_provider);
32116df8bcbSNeil Armstrong }
32216df8bcbSNeil Armstrong 
32316df8bcbSNeil Armstrong static const struct of_device_id phy_meson_g12a_usb2_of_match[] = {
32416df8bcbSNeil Armstrong 	{ .compatible = "amlogic,g12a-usb2-phy", },
32516df8bcbSNeil Armstrong 	{ },
32616df8bcbSNeil Armstrong };
32716df8bcbSNeil Armstrong MODULE_DEVICE_TABLE(of, phy_meson_g12a_usb2_of_match);
32816df8bcbSNeil Armstrong 
32916df8bcbSNeil Armstrong static struct platform_driver phy_meson_g12a_usb2_driver = {
33016df8bcbSNeil Armstrong 	.probe	= phy_meson_g12a_usb2_probe,
33116df8bcbSNeil Armstrong 	.driver	= {
33216df8bcbSNeil Armstrong 		.name		= "phy-meson-g12a-usb2",
33316df8bcbSNeil Armstrong 		.of_match_table	= phy_meson_g12a_usb2_of_match,
33416df8bcbSNeil Armstrong 	},
33516df8bcbSNeil Armstrong };
33616df8bcbSNeil Armstrong module_platform_driver(phy_meson_g12a_usb2_driver);
33716df8bcbSNeil Armstrong 
33816df8bcbSNeil Armstrong MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
33916df8bcbSNeil Armstrong MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
34016df8bcbSNeil Armstrong MODULE_DESCRIPTION("Meson G12A USB2 PHY driver");
34116df8bcbSNeil Armstrong MODULE_LICENSE("GPL v2");
342