18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Meson G12A USB2 PHY driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2017 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
68c2ecf20Sopenharmony_ci * Copyright (C) 2017 Amlogic, Inc. All rights reserved
78c2ecf20Sopenharmony_ci * Copyright (C) 2019 BayLibre, SAS
88c2ecf20Sopenharmony_ci * Author: Neil Armstrong <narmstrong@baylibre.com>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/bitfield.h>
128c2ecf20Sopenharmony_ci#include <linux/bitops.h>
138c2ecf20Sopenharmony_ci#include <linux/clk.h>
148c2ecf20Sopenharmony_ci#include <linux/delay.h>
158c2ecf20Sopenharmony_ci#include <linux/io.h>
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include <linux/of_device.h>
188c2ecf20Sopenharmony_ci#include <linux/regmap.h>
198c2ecf20Sopenharmony_ci#include <linux/reset.h>
208c2ecf20Sopenharmony_ci#include <linux/phy/phy.h>
218c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define PHY_CTRL_R0						0x0
248c2ecf20Sopenharmony_ci#define PHY_CTRL_R1						0x4
258c2ecf20Sopenharmony_ci#define PHY_CTRL_R2						0x8
268c2ecf20Sopenharmony_ci#define PHY_CTRL_R3						0xc
278c2ecf20Sopenharmony_ci	#define PHY_CTRL_R3_SQUELCH_REF				GENMASK(1, 0)
288c2ecf20Sopenharmony_ci	#define PHY_CTRL_R3_HSDIC_REF				GENMASK(3, 2)
298c2ecf20Sopenharmony_ci	#define PHY_CTRL_R3_DISC_THRESH				GENMASK(7, 4)
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define PHY_CTRL_R4						0x10
328c2ecf20Sopenharmony_ci	#define PHY_CTRL_R4_CALIB_CODE_7_0			GENMASK(7, 0)
338c2ecf20Sopenharmony_ci	#define PHY_CTRL_R4_CALIB_CODE_15_8			GENMASK(15, 8)
348c2ecf20Sopenharmony_ci	#define PHY_CTRL_R4_CALIB_CODE_23_16			GENMASK(23, 16)
358c2ecf20Sopenharmony_ci	#define PHY_CTRL_R4_I_C2L_CAL_EN			BIT(24)
368c2ecf20Sopenharmony_ci	#define PHY_CTRL_R4_I_C2L_CAL_RESET_N			BIT(25)
378c2ecf20Sopenharmony_ci	#define PHY_CTRL_R4_I_C2L_CAL_DONE			BIT(26)
388c2ecf20Sopenharmony_ci	#define PHY_CTRL_R4_TEST_BYPASS_MODE_EN			BIT(27)
398c2ecf20Sopenharmony_ci	#define PHY_CTRL_R4_I_C2L_BIAS_TRIM_1_0			GENMASK(29, 28)
408c2ecf20Sopenharmony_ci	#define PHY_CTRL_R4_I_C2L_BIAS_TRIM_3_2			GENMASK(31, 30)
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define PHY_CTRL_R5						0x14
438c2ecf20Sopenharmony_ci#define PHY_CTRL_R6						0x18
448c2ecf20Sopenharmony_ci#define PHY_CTRL_R7						0x1c
458c2ecf20Sopenharmony_ci#define PHY_CTRL_R8						0x20
468c2ecf20Sopenharmony_ci#define PHY_CTRL_R9						0x24
478c2ecf20Sopenharmony_ci#define PHY_CTRL_R10						0x28
488c2ecf20Sopenharmony_ci#define PHY_CTRL_R11						0x2c
498c2ecf20Sopenharmony_ci#define PHY_CTRL_R12						0x30
508c2ecf20Sopenharmony_ci#define PHY_CTRL_R13						0x34
518c2ecf20Sopenharmony_ci	#define PHY_CTRL_R13_CUSTOM_PATTERN_19			GENMASK(7, 0)
528c2ecf20Sopenharmony_ci	#define PHY_CTRL_R13_LOAD_STAT				BIT(14)
538c2ecf20Sopenharmony_ci	#define PHY_CTRL_R13_UPDATE_PMA_SIGNALS			BIT(15)
548c2ecf20Sopenharmony_ci	#define PHY_CTRL_R13_MIN_COUNT_FOR_SYNC_DET		GENMASK(20, 16)
558c2ecf20Sopenharmony_ci	#define PHY_CTRL_R13_CLEAR_HOLD_HS_DISCONNECT		BIT(21)
568c2ecf20Sopenharmony_ci	#define PHY_CTRL_R13_BYPASS_HOST_DISCONNECT_VAL		BIT(22)
578c2ecf20Sopenharmony_ci	#define PHY_CTRL_R13_BYPASS_HOST_DISCONNECT_EN		BIT(23)
588c2ecf20Sopenharmony_ci	#define PHY_CTRL_R13_I_C2L_HS_EN			BIT(24)
598c2ecf20Sopenharmony_ci	#define PHY_CTRL_R13_I_C2L_FS_EN			BIT(25)
608c2ecf20Sopenharmony_ci	#define PHY_CTRL_R13_I_C2L_LS_EN			BIT(26)
618c2ecf20Sopenharmony_ci	#define PHY_CTRL_R13_I_C2L_HS_OE			BIT(27)
628c2ecf20Sopenharmony_ci	#define PHY_CTRL_R13_I_C2L_FS_OE			BIT(28)
638c2ecf20Sopenharmony_ci	#define PHY_CTRL_R13_I_C2L_HS_RX_EN			BIT(29)
648c2ecf20Sopenharmony_ci	#define PHY_CTRL_R13_I_C2L_FSLS_RX_EN			BIT(30)
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define PHY_CTRL_R14						0x38
678c2ecf20Sopenharmony_ci	#define PHY_CTRL_R14_I_RDP_EN				BIT(0)
688c2ecf20Sopenharmony_ci	#define PHY_CTRL_R14_I_RPU_SW1_EN			BIT(1)
698c2ecf20Sopenharmony_ci	#define PHY_CTRL_R14_I_RPU_SW2_EN			GENMASK(3, 2)
708c2ecf20Sopenharmony_ci	#define PHY_CTRL_R14_PG_RSTN				BIT(4)
718c2ecf20Sopenharmony_ci	#define PHY_CTRL_R14_I_C2L_DATA_16_8			BIT(5)
728c2ecf20Sopenharmony_ci	#define PHY_CTRL_R14_I_C2L_ASSERT_SINGLE_EN_ZERO	BIT(6)
738c2ecf20Sopenharmony_ci	#define PHY_CTRL_R14_BYPASS_CTRL_7_0			GENMASK(15, 8)
748c2ecf20Sopenharmony_ci	#define PHY_CTRL_R14_BYPASS_CTRL_15_8			GENMASK(23, 16)
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci#define PHY_CTRL_R15						0x3c
778c2ecf20Sopenharmony_ci#define PHY_CTRL_R16						0x40
788c2ecf20Sopenharmony_ci	#define PHY_CTRL_R16_MPLL_M				GENMASK(8, 0)
798c2ecf20Sopenharmony_ci	#define PHY_CTRL_R16_MPLL_N				GENMASK(14, 10)
808c2ecf20Sopenharmony_ci	#define PHY_CTRL_R16_MPLL_TDC_MODE			BIT(20)
818c2ecf20Sopenharmony_ci	#define PHY_CTRL_R16_MPLL_SDM_EN			BIT(21)
828c2ecf20Sopenharmony_ci	#define PHY_CTRL_R16_MPLL_LOAD				BIT(22)
838c2ecf20Sopenharmony_ci	#define PHY_CTRL_R16_MPLL_DCO_SDM_EN			BIT(23)
848c2ecf20Sopenharmony_ci	#define PHY_CTRL_R16_MPLL_LOCK_LONG			GENMASK(25, 24)
858c2ecf20Sopenharmony_ci	#define PHY_CTRL_R16_MPLL_LOCK_F			BIT(26)
868c2ecf20Sopenharmony_ci	#define PHY_CTRL_R16_MPLL_FAST_LOCK			BIT(27)
878c2ecf20Sopenharmony_ci	#define PHY_CTRL_R16_MPLL_EN				BIT(28)
888c2ecf20Sopenharmony_ci	#define PHY_CTRL_R16_MPLL_RESET				BIT(29)
898c2ecf20Sopenharmony_ci	#define PHY_CTRL_R16_MPLL_LOCK				BIT(30)
908c2ecf20Sopenharmony_ci	#define PHY_CTRL_R16_MPLL_LOCK_DIG			BIT(31)
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci#define PHY_CTRL_R17						0x44
938c2ecf20Sopenharmony_ci	#define PHY_CTRL_R17_MPLL_FRAC_IN			GENMASK(13, 0)
948c2ecf20Sopenharmony_ci	#define PHY_CTRL_R17_MPLL_FIX_EN			BIT(16)
958c2ecf20Sopenharmony_ci	#define PHY_CTRL_R17_MPLL_LAMBDA1			GENMASK(19, 17)
968c2ecf20Sopenharmony_ci	#define PHY_CTRL_R17_MPLL_LAMBDA0			GENMASK(22, 20)
978c2ecf20Sopenharmony_ci	#define PHY_CTRL_R17_MPLL_FILTER_MODE			BIT(23)
988c2ecf20Sopenharmony_ci	#define PHY_CTRL_R17_MPLL_FILTER_PVT2			GENMASK(27, 24)
998c2ecf20Sopenharmony_ci	#define PHY_CTRL_R17_MPLL_FILTER_PVT1			GENMASK(31, 28)
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci#define PHY_CTRL_R18						0x48
1028c2ecf20Sopenharmony_ci	#define PHY_CTRL_R18_MPLL_LKW_SEL			GENMASK(1, 0)
1038c2ecf20Sopenharmony_ci	#define PHY_CTRL_R18_MPLL_LK_W				GENMASK(5, 2)
1048c2ecf20Sopenharmony_ci	#define PHY_CTRL_R18_MPLL_LK_S				GENMASK(11, 6)
1058c2ecf20Sopenharmony_ci	#define PHY_CTRL_R18_MPLL_DCO_M_EN			BIT(12)
1068c2ecf20Sopenharmony_ci	#define PHY_CTRL_R18_MPLL_DCO_CLK_SEL			BIT(13)
1078c2ecf20Sopenharmony_ci	#define PHY_CTRL_R18_MPLL_PFD_GAIN			GENMASK(15, 14)
1088c2ecf20Sopenharmony_ci	#define PHY_CTRL_R18_MPLL_ROU				GENMASK(18, 16)
1098c2ecf20Sopenharmony_ci	#define PHY_CTRL_R18_MPLL_DATA_SEL			GENMASK(21, 19)
1108c2ecf20Sopenharmony_ci	#define PHY_CTRL_R18_MPLL_BIAS_ADJ			GENMASK(23, 22)
1118c2ecf20Sopenharmony_ci	#define PHY_CTRL_R18_MPLL_BB_MODE			GENMASK(25, 24)
1128c2ecf20Sopenharmony_ci	#define PHY_CTRL_R18_MPLL_ALPHA				GENMASK(28, 26)
1138c2ecf20Sopenharmony_ci	#define PHY_CTRL_R18_MPLL_ADJ_LDO			GENMASK(30, 29)
1148c2ecf20Sopenharmony_ci	#define PHY_CTRL_R18_MPLL_ACG_RANGE			BIT(31)
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci#define PHY_CTRL_R19						0x4c
1178c2ecf20Sopenharmony_ci#define PHY_CTRL_R20						0x50
1188c2ecf20Sopenharmony_ci	#define PHY_CTRL_R20_USB2_IDDET_EN			BIT(0)
1198c2ecf20Sopenharmony_ci	#define PHY_CTRL_R20_USB2_OTG_VBUS_TRIM_2_0		GENMASK(3, 1)
1208c2ecf20Sopenharmony_ci	#define PHY_CTRL_R20_USB2_OTG_VBUSDET_EN		BIT(4)
1218c2ecf20Sopenharmony_ci	#define PHY_CTRL_R20_USB2_AMON_EN			BIT(5)
1228c2ecf20Sopenharmony_ci	#define PHY_CTRL_R20_USB2_CAL_CODE_R5			BIT(6)
1238c2ecf20Sopenharmony_ci	#define PHY_CTRL_R20_BYPASS_OTG_DET			BIT(7)
1248c2ecf20Sopenharmony_ci	#define PHY_CTRL_R20_USB2_DMON_EN			BIT(8)
1258c2ecf20Sopenharmony_ci	#define PHY_CTRL_R20_USB2_DMON_SEL_3_0			GENMASK(12, 9)
1268c2ecf20Sopenharmony_ci	#define PHY_CTRL_R20_USB2_EDGE_DRV_EN			BIT(13)
1278c2ecf20Sopenharmony_ci	#define PHY_CTRL_R20_USB2_EDGE_DRV_TRIM_1_0		GENMASK(15, 14)
1288c2ecf20Sopenharmony_ci	#define PHY_CTRL_R20_USB2_BGR_ADJ_4_0			GENMASK(20, 16)
1298c2ecf20Sopenharmony_ci	#define PHY_CTRL_R20_USB2_BGR_START			BIT(21)
1308c2ecf20Sopenharmony_ci	#define PHY_CTRL_R20_USB2_BGR_VREF_4_0			GENMASK(28, 24)
1318c2ecf20Sopenharmony_ci	#define PHY_CTRL_R20_USB2_BGR_DBG_1_0			GENMASK(30, 29)
1328c2ecf20Sopenharmony_ci	#define PHY_CTRL_R20_BYPASS_CAL_DONE_R5			BIT(31)
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci#define PHY_CTRL_R21						0x54
1358c2ecf20Sopenharmony_ci	#define PHY_CTRL_R21_USB2_BGR_FORCE			BIT(0)
1368c2ecf20Sopenharmony_ci	#define PHY_CTRL_R21_USB2_CAL_ACK_EN			BIT(1)
1378c2ecf20Sopenharmony_ci	#define PHY_CTRL_R21_USB2_OTG_ACA_EN			BIT(2)
1388c2ecf20Sopenharmony_ci	#define PHY_CTRL_R21_USB2_TX_STRG_PD			BIT(3)
1398c2ecf20Sopenharmony_ci	#define PHY_CTRL_R21_USB2_OTG_ACA_TRIM_1_0		GENMASK(5, 4)
1408c2ecf20Sopenharmony_ci	#define PHY_CTRL_R21_BYPASS_UTMI_CNTR			GENMASK(15, 6)
1418c2ecf20Sopenharmony_ci	#define PHY_CTRL_R21_BYPASS_UTMI_REG			GENMASK(25, 20)
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci#define PHY_CTRL_R22						0x58
1448c2ecf20Sopenharmony_ci#define PHY_CTRL_R23						0x5c
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci#define RESET_COMPLETE_TIME					1000
1478c2ecf20Sopenharmony_ci#define PLL_RESET_COMPLETE_TIME					100
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cienum meson_soc_id {
1508c2ecf20Sopenharmony_ci	MESON_SOC_G12A  = 0,
1518c2ecf20Sopenharmony_ci	MESON_SOC_A1,
1528c2ecf20Sopenharmony_ci};
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistruct phy_meson_g12a_usb2_priv {
1558c2ecf20Sopenharmony_ci	struct device		*dev;
1568c2ecf20Sopenharmony_ci	struct regmap		*regmap;
1578c2ecf20Sopenharmony_ci	struct clk		*clk;
1588c2ecf20Sopenharmony_ci	struct reset_control	*reset;
1598c2ecf20Sopenharmony_ci	int                     soc_id;
1608c2ecf20Sopenharmony_ci};
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic const struct regmap_config phy_meson_g12a_usb2_regmap_conf = {
1638c2ecf20Sopenharmony_ci	.reg_bits = 8,
1648c2ecf20Sopenharmony_ci	.val_bits = 32,
1658c2ecf20Sopenharmony_ci	.reg_stride = 4,
1668c2ecf20Sopenharmony_ci	.max_register = PHY_CTRL_R23,
1678c2ecf20Sopenharmony_ci};
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic int phy_meson_g12a_usb2_init(struct phy *phy)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	struct phy_meson_g12a_usb2_priv *priv = phy_get_drvdata(phy);
1728c2ecf20Sopenharmony_ci	int ret;
1738c2ecf20Sopenharmony_ci	unsigned int value;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	ret = reset_control_reset(priv->reset);
1768c2ecf20Sopenharmony_ci	if (ret)
1778c2ecf20Sopenharmony_ci		return ret;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	udelay(RESET_COMPLETE_TIME);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	/* usb2_otg_aca_en == 0 */
1828c2ecf20Sopenharmony_ci	regmap_update_bits(priv->regmap, PHY_CTRL_R21,
1838c2ecf20Sopenharmony_ci			   PHY_CTRL_R21_USB2_OTG_ACA_EN, 0);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	/* PLL Setup : 24MHz * 20 / 1 = 480MHz */
1868c2ecf20Sopenharmony_ci	regmap_write(priv->regmap, PHY_CTRL_R16,
1878c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R16_MPLL_M, 20) |
1888c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R16_MPLL_N, 1) |
1898c2ecf20Sopenharmony_ci		     PHY_CTRL_R16_MPLL_LOAD |
1908c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R16_MPLL_LOCK_LONG, 1) |
1918c2ecf20Sopenharmony_ci		     PHY_CTRL_R16_MPLL_FAST_LOCK |
1928c2ecf20Sopenharmony_ci		     PHY_CTRL_R16_MPLL_EN |
1938c2ecf20Sopenharmony_ci		     PHY_CTRL_R16_MPLL_RESET);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	regmap_write(priv->regmap, PHY_CTRL_R17,
1968c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R17_MPLL_FRAC_IN, 0) |
1978c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R17_MPLL_LAMBDA1, 7) |
1988c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R17_MPLL_LAMBDA0, 7) |
1998c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R17_MPLL_FILTER_PVT2, 2) |
2008c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R17_MPLL_FILTER_PVT1, 9));
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	value = FIELD_PREP(PHY_CTRL_R18_MPLL_LKW_SEL, 1) |
2038c2ecf20Sopenharmony_ci		FIELD_PREP(PHY_CTRL_R18_MPLL_LK_W, 9) |
2048c2ecf20Sopenharmony_ci		FIELD_PREP(PHY_CTRL_R18_MPLL_LK_S, 0x27) |
2058c2ecf20Sopenharmony_ci		FIELD_PREP(PHY_CTRL_R18_MPLL_PFD_GAIN, 1) |
2068c2ecf20Sopenharmony_ci		FIELD_PREP(PHY_CTRL_R18_MPLL_ROU, 7) |
2078c2ecf20Sopenharmony_ci		FIELD_PREP(PHY_CTRL_R18_MPLL_DATA_SEL, 3) |
2088c2ecf20Sopenharmony_ci		FIELD_PREP(PHY_CTRL_R18_MPLL_BIAS_ADJ, 1) |
2098c2ecf20Sopenharmony_ci		FIELD_PREP(PHY_CTRL_R18_MPLL_BB_MODE, 0) |
2108c2ecf20Sopenharmony_ci		FIELD_PREP(PHY_CTRL_R18_MPLL_ALPHA, 3) |
2118c2ecf20Sopenharmony_ci		FIELD_PREP(PHY_CTRL_R18_MPLL_ADJ_LDO, 1) |
2128c2ecf20Sopenharmony_ci		PHY_CTRL_R18_MPLL_ACG_RANGE;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	if (priv->soc_id == MESON_SOC_A1)
2158c2ecf20Sopenharmony_ci		value |= PHY_CTRL_R18_MPLL_DCO_CLK_SEL;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	regmap_write(priv->regmap, PHY_CTRL_R18, value);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	udelay(PLL_RESET_COMPLETE_TIME);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	/* UnReset PLL */
2228c2ecf20Sopenharmony_ci	regmap_write(priv->regmap, PHY_CTRL_R16,
2238c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R16_MPLL_M, 20) |
2248c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R16_MPLL_N, 1) |
2258c2ecf20Sopenharmony_ci		     PHY_CTRL_R16_MPLL_LOAD |
2268c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R16_MPLL_LOCK_LONG, 1) |
2278c2ecf20Sopenharmony_ci		     PHY_CTRL_R16_MPLL_FAST_LOCK |
2288c2ecf20Sopenharmony_ci		     PHY_CTRL_R16_MPLL_EN);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	/* PHY Tuning */
2318c2ecf20Sopenharmony_ci	regmap_write(priv->regmap, PHY_CTRL_R20,
2328c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R20_USB2_OTG_VBUS_TRIM_2_0, 4) |
2338c2ecf20Sopenharmony_ci		     PHY_CTRL_R20_USB2_OTG_VBUSDET_EN |
2348c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R20_USB2_DMON_SEL_3_0, 15) |
2358c2ecf20Sopenharmony_ci		     PHY_CTRL_R20_USB2_EDGE_DRV_EN |
2368c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R20_USB2_EDGE_DRV_TRIM_1_0, 3) |
2378c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R20_USB2_BGR_ADJ_4_0, 0) |
2388c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R20_USB2_BGR_VREF_4_0, 0) |
2398c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R20_USB2_BGR_DBG_1_0, 0));
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	if (priv->soc_id == MESON_SOC_G12A)
2428c2ecf20Sopenharmony_ci		regmap_write(priv->regmap, PHY_CTRL_R4,
2438c2ecf20Sopenharmony_ci			     FIELD_PREP(PHY_CTRL_R4_CALIB_CODE_7_0, 0xf) |
2448c2ecf20Sopenharmony_ci			     FIELD_PREP(PHY_CTRL_R4_CALIB_CODE_15_8, 0xf) |
2458c2ecf20Sopenharmony_ci			     FIELD_PREP(PHY_CTRL_R4_CALIB_CODE_23_16, 0xf) |
2468c2ecf20Sopenharmony_ci			     PHY_CTRL_R4_TEST_BYPASS_MODE_EN |
2478c2ecf20Sopenharmony_ci			     FIELD_PREP(PHY_CTRL_R4_I_C2L_BIAS_TRIM_1_0, 0) |
2488c2ecf20Sopenharmony_ci			     FIELD_PREP(PHY_CTRL_R4_I_C2L_BIAS_TRIM_3_2, 0));
2498c2ecf20Sopenharmony_ci	else if (priv->soc_id == MESON_SOC_A1) {
2508c2ecf20Sopenharmony_ci		regmap_write(priv->regmap, PHY_CTRL_R21,
2518c2ecf20Sopenharmony_ci			     PHY_CTRL_R21_USB2_CAL_ACK_EN |
2528c2ecf20Sopenharmony_ci			     PHY_CTRL_R21_USB2_TX_STRG_PD |
2538c2ecf20Sopenharmony_ci			     FIELD_PREP(PHY_CTRL_R21_USB2_OTG_ACA_TRIM_1_0, 2));
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci		/* Analog Settings */
2568c2ecf20Sopenharmony_ci		regmap_write(priv->regmap, PHY_CTRL_R13,
2578c2ecf20Sopenharmony_ci			     FIELD_PREP(PHY_CTRL_R13_MIN_COUNT_FOR_SYNC_DET, 7));
2588c2ecf20Sopenharmony_ci	}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	/* Tuning Disconnect Threshold */
2618c2ecf20Sopenharmony_ci	regmap_write(priv->regmap, PHY_CTRL_R3,
2628c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R3_SQUELCH_REF, 0) |
2638c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R3_HSDIC_REF, 1) |
2648c2ecf20Sopenharmony_ci		     FIELD_PREP(PHY_CTRL_R3_DISC_THRESH, 3));
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	if (priv->soc_id == MESON_SOC_G12A) {
2678c2ecf20Sopenharmony_ci		/* Analog Settings */
2688c2ecf20Sopenharmony_ci		regmap_write(priv->regmap, PHY_CTRL_R14, 0);
2698c2ecf20Sopenharmony_ci		regmap_write(priv->regmap, PHY_CTRL_R13,
2708c2ecf20Sopenharmony_ci			     PHY_CTRL_R13_UPDATE_PMA_SIGNALS |
2718c2ecf20Sopenharmony_ci			     FIELD_PREP(PHY_CTRL_R13_MIN_COUNT_FOR_SYNC_DET, 7));
2728c2ecf20Sopenharmony_ci	}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	return 0;
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cistatic int phy_meson_g12a_usb2_exit(struct phy *phy)
2788c2ecf20Sopenharmony_ci{
2798c2ecf20Sopenharmony_ci	struct phy_meson_g12a_usb2_priv *priv = phy_get_drvdata(phy);
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	return reset_control_reset(priv->reset);
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci/* set_mode is not needed, mode setting is handled via the UTMI bus */
2858c2ecf20Sopenharmony_cistatic const struct phy_ops phy_meson_g12a_usb2_ops = {
2868c2ecf20Sopenharmony_ci	.init		= phy_meson_g12a_usb2_init,
2878c2ecf20Sopenharmony_ci	.exit		= phy_meson_g12a_usb2_exit,
2888c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
2898c2ecf20Sopenharmony_ci};
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_cistatic int phy_meson_g12a_usb2_probe(struct platform_device *pdev)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
2948c2ecf20Sopenharmony_ci	struct phy_provider *phy_provider;
2958c2ecf20Sopenharmony_ci	struct resource *res;
2968c2ecf20Sopenharmony_ci	struct phy_meson_g12a_usb2_priv *priv;
2978c2ecf20Sopenharmony_ci	struct phy *phy;
2988c2ecf20Sopenharmony_ci	void __iomem *base;
2998c2ecf20Sopenharmony_ci	int ret;
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
3028c2ecf20Sopenharmony_ci	if (!priv)
3038c2ecf20Sopenharmony_ci		return -ENOMEM;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	priv->dev = dev;
3068c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, priv);
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3098c2ecf20Sopenharmony_ci	base = devm_ioremap_resource(dev, res);
3108c2ecf20Sopenharmony_ci	if (IS_ERR(base))
3118c2ecf20Sopenharmony_ci		return PTR_ERR(base);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	priv->soc_id = (enum meson_soc_id)of_device_get_match_data(&pdev->dev);
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	priv->regmap = devm_regmap_init_mmio(dev, base,
3168c2ecf20Sopenharmony_ci					     &phy_meson_g12a_usb2_regmap_conf);
3178c2ecf20Sopenharmony_ci	if (IS_ERR(priv->regmap))
3188c2ecf20Sopenharmony_ci		return PTR_ERR(priv->regmap);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	priv->clk = devm_clk_get(dev, "xtal");
3218c2ecf20Sopenharmony_ci	if (IS_ERR(priv->clk))
3228c2ecf20Sopenharmony_ci		return PTR_ERR(priv->clk);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	priv->reset = devm_reset_control_get(dev, "phy");
3258c2ecf20Sopenharmony_ci	if (IS_ERR(priv->reset))
3268c2ecf20Sopenharmony_ci		return PTR_ERR(priv->reset);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	ret = reset_control_deassert(priv->reset);
3298c2ecf20Sopenharmony_ci	if (ret)
3308c2ecf20Sopenharmony_ci		return ret;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	phy = devm_phy_create(dev, NULL, &phy_meson_g12a_usb2_ops);
3338c2ecf20Sopenharmony_ci	if (IS_ERR(phy)) {
3348c2ecf20Sopenharmony_ci		ret = PTR_ERR(phy);
3358c2ecf20Sopenharmony_ci		if (ret != -EPROBE_DEFER)
3368c2ecf20Sopenharmony_ci			dev_err(dev, "failed to create PHY\n");
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci		return ret;
3398c2ecf20Sopenharmony_ci	}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	phy_set_bus_width(phy, 8);
3428c2ecf20Sopenharmony_ci	phy_set_drvdata(phy, priv);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(phy_provider);
3478c2ecf20Sopenharmony_ci}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_cistatic const struct of_device_id phy_meson_g12a_usb2_of_match[] = {
3508c2ecf20Sopenharmony_ci	{
3518c2ecf20Sopenharmony_ci		.compatible = "amlogic,g12a-usb2-phy",
3528c2ecf20Sopenharmony_ci		.data = (void *)MESON_SOC_G12A,
3538c2ecf20Sopenharmony_ci	},
3548c2ecf20Sopenharmony_ci	{
3558c2ecf20Sopenharmony_ci		.compatible = "amlogic,a1-usb2-phy",
3568c2ecf20Sopenharmony_ci		.data = (void *)MESON_SOC_A1,
3578c2ecf20Sopenharmony_ci	},
3588c2ecf20Sopenharmony_ci	{ /* Sentinel */ }
3598c2ecf20Sopenharmony_ci};
3608c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, phy_meson_g12a_usb2_of_match);
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_cistatic struct platform_driver phy_meson_g12a_usb2_driver = {
3638c2ecf20Sopenharmony_ci	.probe	= phy_meson_g12a_usb2_probe,
3648c2ecf20Sopenharmony_ci	.driver	= {
3658c2ecf20Sopenharmony_ci		.name		= "phy-meson-g12a-usb2",
3668c2ecf20Sopenharmony_ci		.of_match_table	= phy_meson_g12a_usb2_of_match,
3678c2ecf20Sopenharmony_ci	},
3688c2ecf20Sopenharmony_ci};
3698c2ecf20Sopenharmony_cimodule_platform_driver(phy_meson_g12a_usb2_driver);
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ciMODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
3728c2ecf20Sopenharmony_ciMODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
3738c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Meson G12A USB2 PHY driver");
3748c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
375