162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2016 Allwinnertech Co., Ltd.
462306a36Sopenharmony_ci * Copyright (C) 2017-2018 Bootlin
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Maxime Ripard <maxime.ripard@free-electrons.com>
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/bitops.h>
1062306a36Sopenharmony_ci#include <linux/clk.h>
1162306a36Sopenharmony_ci#include <linux/module.h>
1262306a36Sopenharmony_ci#include <linux/of_address.h>
1362306a36Sopenharmony_ci#include <linux/platform_device.h>
1462306a36Sopenharmony_ci#include <linux/regmap.h>
1562306a36Sopenharmony_ci#include <linux/reset.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include <linux/phy/phy.h>
1862306a36Sopenharmony_ci#include <linux/phy/phy-mipi-dphy.h>
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define SUN6I_DPHY_GCTL_REG		0x00
2162306a36Sopenharmony_ci#define SUN6I_DPHY_GCTL_LANE_NUM(n)		((((n) - 1) & 3) << 4)
2262306a36Sopenharmony_ci#define SUN6I_DPHY_GCTL_EN			BIT(0)
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#define SUN6I_DPHY_TX_CTL_REG		0x04
2562306a36Sopenharmony_ci#define SUN6I_DPHY_TX_CTL_HS_TX_CLK_CONT	BIT(28)
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#define SUN6I_DPHY_RX_CTL_REG		0x08
2862306a36Sopenharmony_ci#define SUN6I_DPHY_RX_CTL_EN_DBC	BIT(31)
2962306a36Sopenharmony_ci#define SUN6I_DPHY_RX_CTL_RX_CLK_FORCE	BIT(24)
3062306a36Sopenharmony_ci#define SUN6I_DPHY_RX_CTL_RX_D3_FORCE	BIT(23)
3162306a36Sopenharmony_ci#define SUN6I_DPHY_RX_CTL_RX_D2_FORCE	BIT(22)
3262306a36Sopenharmony_ci#define SUN6I_DPHY_RX_CTL_RX_D1_FORCE	BIT(21)
3362306a36Sopenharmony_ci#define SUN6I_DPHY_RX_CTL_RX_D0_FORCE	BIT(20)
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci#define SUN6I_DPHY_TX_TIME0_REG		0x10
3662306a36Sopenharmony_ci#define SUN6I_DPHY_TX_TIME0_HS_TRAIL(n)		(((n) & 0xff) << 24)
3762306a36Sopenharmony_ci#define SUN6I_DPHY_TX_TIME0_HS_PREPARE(n)	(((n) & 0xff) << 16)
3862306a36Sopenharmony_ci#define SUN6I_DPHY_TX_TIME0_LP_CLK_DIV(n)	((n) & 0xff)
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci#define SUN6I_DPHY_TX_TIME1_REG		0x14
4162306a36Sopenharmony_ci#define SUN6I_DPHY_TX_TIME1_CLK_POST(n)		(((n) & 0xff) << 24)
4262306a36Sopenharmony_ci#define SUN6I_DPHY_TX_TIME1_CLK_PRE(n)		(((n) & 0xff) << 16)
4362306a36Sopenharmony_ci#define SUN6I_DPHY_TX_TIME1_CLK_ZERO(n)		(((n) & 0xff) << 8)
4462306a36Sopenharmony_ci#define SUN6I_DPHY_TX_TIME1_CLK_PREPARE(n)	((n) & 0xff)
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#define SUN6I_DPHY_TX_TIME2_REG		0x18
4762306a36Sopenharmony_ci#define SUN6I_DPHY_TX_TIME2_CLK_TRAIL(n)	((n) & 0xff)
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci#define SUN6I_DPHY_TX_TIME3_REG		0x1c
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci#define SUN6I_DPHY_TX_TIME4_REG		0x20
5262306a36Sopenharmony_ci#define SUN6I_DPHY_TX_TIME4_HS_TX_ANA1(n)	(((n) & 0xff) << 8)
5362306a36Sopenharmony_ci#define SUN6I_DPHY_TX_TIME4_HS_TX_ANA0(n)	((n) & 0xff)
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci#define SUN6I_DPHY_RX_TIME0_REG		0x30
5662306a36Sopenharmony_ci#define SUN6I_DPHY_RX_TIME0_HS_RX_SYNC(n)	(((n) & 0xff) << 24)
5762306a36Sopenharmony_ci#define SUN6I_DPHY_RX_TIME0_HS_RX_CLK_MISS(n)	(((n) & 0xff) << 16)
5862306a36Sopenharmony_ci#define SUN6I_DPHY_RX_TIME0_LP_RX(n)		(((n) & 0xff) << 8)
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci#define SUN6I_DPHY_RX_TIME1_REG		0x34
6162306a36Sopenharmony_ci#define SUN6I_DPHY_RX_TIME1_RX_DLY(n)		(((n) & 0xfff) << 20)
6262306a36Sopenharmony_ci#define SUN6I_DPHY_RX_TIME1_LP_RX_ULPS_WP(n)	((n) & 0xfffff)
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci#define SUN6I_DPHY_RX_TIME2_REG		0x38
6562306a36Sopenharmony_ci#define SUN6I_DPHY_RX_TIME2_HS_RX_ANA1(n)	(((n) & 0xff) << 8)
6662306a36Sopenharmony_ci#define SUN6I_DPHY_RX_TIME2_HS_RX_ANA0(n)	((n) & 0xff)
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#define SUN6I_DPHY_RX_TIME3_REG		0x40
6962306a36Sopenharmony_ci#define SUN6I_DPHY_RX_TIME3_LPRST_DLY(n)	(((n) & 0xffff) << 16)
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci#define SUN6I_DPHY_ANA0_REG		0x4c
7262306a36Sopenharmony_ci#define SUN6I_DPHY_ANA0_REG_PWS			BIT(31)
7362306a36Sopenharmony_ci#define SUN6I_DPHY_ANA0_REG_PWEND		BIT(30)
7462306a36Sopenharmony_ci#define SUN6I_DPHY_ANA0_REG_PWENC		BIT(29)
7562306a36Sopenharmony_ci#define SUN6I_DPHY_ANA0_REG_DMPC		BIT(28)
7662306a36Sopenharmony_ci#define SUN6I_DPHY_ANA0_REG_DMPD(n)		(((n) & 0xf) << 24)
7762306a36Sopenharmony_ci#define SUN6I_DPHY_ANA0_REG_SRXDT(n)		(((n) & 0xf) << 20)
7862306a36Sopenharmony_ci#define SUN6I_DPHY_ANA0_REG_SRXCK(n)		(((n) & 0xf) << 16)
7962306a36Sopenharmony_ci#define SUN6I_DPHY_ANA0_REG_SDIV2		BIT(15)
8062306a36Sopenharmony_ci#define SUN6I_DPHY_ANA0_REG_SLV(n)		(((n) & 7) << 12)
8162306a36Sopenharmony_ci#define SUN6I_DPHY_ANA0_REG_DEN(n)		(((n) & 0xf) << 8)
8262306a36Sopenharmony_ci#define SUN6I_DPHY_ANA0_REG_PLR(n)		(((n) & 0xf) << 4)
8362306a36Sopenharmony_ci#define SUN6I_DPHY_ANA0_REG_SFB(n)		(((n) & 3) << 2)
8462306a36Sopenharmony_ci#define SUN6I_DPHY_ANA0_REG_RSD			BIT(1)
8562306a36Sopenharmony_ci#define SUN6I_DPHY_ANA0_REG_SELSCK		BIT(0)
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci#define SUN6I_DPHY_ANA1_REG		0x50
8862306a36Sopenharmony_ci#define SUN6I_DPHY_ANA1_REG_VTTMODE		BIT(31)
8962306a36Sopenharmony_ci#define SUN6I_DPHY_ANA1_REG_CSMPS(n)		(((n) & 3) << 28)
9062306a36Sopenharmony_ci#define SUN6I_DPHY_ANA1_REG_SVTT(n)		(((n) & 0xf) << 24)
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci#define SUN6I_DPHY_ANA2_REG		0x54
9362306a36Sopenharmony_ci#define SUN6I_DPHY_ANA2_EN_P2S_CPU(n)		(((n) & 0xf) << 24)
9462306a36Sopenharmony_ci#define SUN6I_DPHY_ANA2_EN_P2S_CPU_MASK		GENMASK(27, 24)
9562306a36Sopenharmony_ci#define SUN6I_DPHY_ANA2_EN_CK_CPU		BIT(4)
9662306a36Sopenharmony_ci#define SUN6I_DPHY_ANA2_REG_ENIB		BIT(1)
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci#define SUN6I_DPHY_ANA3_REG		0x58
9962306a36Sopenharmony_ci#define SUN6I_DPHY_ANA3_EN_VTTD(n)		(((n) & 0xf) << 28)
10062306a36Sopenharmony_ci#define SUN6I_DPHY_ANA3_EN_VTTD_MASK		GENMASK(31, 28)
10162306a36Sopenharmony_ci#define SUN6I_DPHY_ANA3_EN_VTTC			BIT(27)
10262306a36Sopenharmony_ci#define SUN6I_DPHY_ANA3_EN_DIV			BIT(26)
10362306a36Sopenharmony_ci#define SUN6I_DPHY_ANA3_EN_LDOC			BIT(25)
10462306a36Sopenharmony_ci#define SUN6I_DPHY_ANA3_EN_LDOD			BIT(24)
10562306a36Sopenharmony_ci#define SUN6I_DPHY_ANA3_EN_LDOR			BIT(18)
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci#define SUN6I_DPHY_ANA4_REG		0x5c
10862306a36Sopenharmony_ci#define SUN6I_DPHY_ANA4_REG_EN_MIPI		BIT(31)
10962306a36Sopenharmony_ci#define SUN6I_DPHY_ANA4_REG_EN_COMTEST		BIT(30)
11062306a36Sopenharmony_ci#define SUN6I_DPHY_ANA4_REG_COMTEST(n)		(((n) & 3) << 28)
11162306a36Sopenharmony_ci#define SUN6I_DPHY_ANA4_REG_IB(n)		(((n) & 3) << 25)
11262306a36Sopenharmony_ci#define SUN6I_DPHY_ANA4_REG_DMPLVC		BIT(24)
11362306a36Sopenharmony_ci#define SUN6I_DPHY_ANA4_REG_DMPLVD(n)		(((n) & 0xf) << 20)
11462306a36Sopenharmony_ci#define SUN6I_DPHY_ANA4_REG_VTT_SET(n)		(((n) & 0x7) << 17)
11562306a36Sopenharmony_ci#define SUN6I_DPHY_ANA4_REG_CKDV(n)		(((n) & 0x1f) << 12)
11662306a36Sopenharmony_ci#define SUN6I_DPHY_ANA4_REG_TMSC(n)		(((n) & 3) << 10)
11762306a36Sopenharmony_ci#define SUN6I_DPHY_ANA4_REG_TMSD(n)		(((n) & 3) << 8)
11862306a36Sopenharmony_ci#define SUN6I_DPHY_ANA4_REG_TXDNSC(n)		(((n) & 3) << 6)
11962306a36Sopenharmony_ci#define SUN6I_DPHY_ANA4_REG_TXDNSD(n)		(((n) & 3) << 4)
12062306a36Sopenharmony_ci#define SUN6I_DPHY_ANA4_REG_TXPUSC(n)		(((n) & 3) << 2)
12162306a36Sopenharmony_ci#define SUN6I_DPHY_ANA4_REG_TXPUSD(n)		((n) & 3)
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci#define SUN6I_DPHY_DBG5_REG		0xf4
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci#define SUN50I_DPHY_TX_SLEW_REG0	0xf8
12662306a36Sopenharmony_ci#define SUN50I_DPHY_TX_SLEW_REG1	0xfc
12762306a36Sopenharmony_ci#define SUN50I_DPHY_TX_SLEW_REG2	0x100
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG0		0x104
13062306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG0_CP36_EN		BIT(23)
13162306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG0_LDO_EN		BIT(22)
13262306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG0_EN_LVS		BIT(21)
13362306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG0_PLL_EN		BIT(20)
13462306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG0_P(n)		(((n) & 0xf) << 16)
13562306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG0_N(n)		(((n) & 0xff) << 8)
13662306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG0_NDET		BIT(7)
13762306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG0_TDIV		BIT(6)
13862306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG0_M0(n)		(((n) & 3) << 4)
13962306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG0_M1(n)		((n) & 0xf)
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG1		0x108
14262306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG1_UNLOCK_MDSEL(n)	(((n) & 3) << 14)
14362306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG1_LOCKMDSEL		BIT(13)
14462306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG1_LOCKDET_EN		BIT(12)
14562306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG1_VSETA(n)		(((n) & 0x7) << 9)
14662306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG1_VSETD(n)		(((n) & 0x7) << 6)
14762306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG1_LPF_SW		BIT(5)
14862306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG1_ICP_SEL(n)		(((n) & 3) << 3)
14962306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG1_ATEST_SEL(n)	(((n) & 3) << 1)
15062306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG1_TEST_EN		BIT(0)
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG2		0x10c
15362306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG2_SDM_EN		BIT(31)
15462306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG2_FF_EN		BIT(30)
15562306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG2_SS_EN		BIT(29)
15662306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG2_SS_FRAC(n)		(((n) & 0x1ff) << 20)
15762306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG2_SS_INT(n)		(((n) & 0xff) << 12)
15862306a36Sopenharmony_ci#define SUN50I_DPHY_PLL_REG2_FRAC(n)		((n) & 0xfff)
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci#define SUN50I_COMBO_PHY_REG0		0x110
16162306a36Sopenharmony_ci#define SUN50I_COMBO_PHY_REG0_EN_TEST_COMBOLDO	BIT(5)
16262306a36Sopenharmony_ci#define SUN50I_COMBO_PHY_REG0_EN_TEST_0P8	BIT(4)
16362306a36Sopenharmony_ci#define SUN50I_COMBO_PHY_REG0_EN_MIPI		BIT(3)
16462306a36Sopenharmony_ci#define SUN50I_COMBO_PHY_REG0_EN_LVDS		BIT(2)
16562306a36Sopenharmony_ci#define SUN50I_COMBO_PHY_REG0_EN_COMBOLDO	BIT(1)
16662306a36Sopenharmony_ci#define SUN50I_COMBO_PHY_REG0_EN_CP		BIT(0)
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci#define SUN50I_COMBO_PHY_REG1		0x114
16962306a36Sopenharmony_ci#define SUN50I_COMBO_PHY_REG2_REG_VREF1P6(n)	(((n) & 0x7) << 4)
17062306a36Sopenharmony_ci#define SUN50I_COMBO_PHY_REG2_REG_VREF0P8(n)	((n) & 0x7)
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci#define SUN50I_COMBO_PHY_REG2		0x118
17362306a36Sopenharmony_ci#define SUN50I_COMBO_PHY_REG2_HS_STOP_DLY(n)	((n) & 0xff)
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_cienum sun6i_dphy_direction {
17662306a36Sopenharmony_ci	SUN6I_DPHY_DIRECTION_TX,
17762306a36Sopenharmony_ci	SUN6I_DPHY_DIRECTION_RX,
17862306a36Sopenharmony_ci};
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_cistruct sun6i_dphy;
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_cistruct sun6i_dphy_variant {
18362306a36Sopenharmony_ci	void	(*tx_power_on)(struct sun6i_dphy *dphy);
18462306a36Sopenharmony_ci	bool	rx_supported;
18562306a36Sopenharmony_ci};
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_cistruct sun6i_dphy {
18862306a36Sopenharmony_ci	struct clk				*bus_clk;
18962306a36Sopenharmony_ci	struct clk				*mod_clk;
19062306a36Sopenharmony_ci	struct regmap				*regs;
19162306a36Sopenharmony_ci	struct reset_control			*reset;
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	struct phy				*phy;
19462306a36Sopenharmony_ci	struct phy_configure_opts_mipi_dphy	config;
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci	const struct sun6i_dphy_variant		*variant;
19762306a36Sopenharmony_ci	enum sun6i_dphy_direction		direction;
19862306a36Sopenharmony_ci};
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_cistatic int sun6i_dphy_init(struct phy *phy)
20162306a36Sopenharmony_ci{
20262306a36Sopenharmony_ci	struct sun6i_dphy *dphy = phy_get_drvdata(phy);
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci	reset_control_deassert(dphy->reset);
20562306a36Sopenharmony_ci	clk_prepare_enable(dphy->mod_clk);
20662306a36Sopenharmony_ci	clk_set_rate_exclusive(dphy->mod_clk, 150000000);
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci	return 0;
20962306a36Sopenharmony_ci}
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_cistatic int sun6i_dphy_configure(struct phy *phy, union phy_configure_opts *opts)
21262306a36Sopenharmony_ci{
21362306a36Sopenharmony_ci	struct sun6i_dphy *dphy = phy_get_drvdata(phy);
21462306a36Sopenharmony_ci	int ret;
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	ret = phy_mipi_dphy_config_validate(&opts->mipi_dphy);
21762306a36Sopenharmony_ci	if (ret)
21862306a36Sopenharmony_ci		return ret;
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci	memcpy(&dphy->config, opts, sizeof(dphy->config));
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci	return 0;
22362306a36Sopenharmony_ci}
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_cistatic void sun6i_a31_mipi_dphy_tx_power_on(struct sun6i_dphy *dphy)
22662306a36Sopenharmony_ci{
22762306a36Sopenharmony_ci	u8 lanes_mask = GENMASK(dphy->config.lanes - 1, 0);
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_ANA0_REG,
23062306a36Sopenharmony_ci		     SUN6I_DPHY_ANA0_REG_PWS |
23162306a36Sopenharmony_ci		     SUN6I_DPHY_ANA0_REG_DMPC |
23262306a36Sopenharmony_ci		     SUN6I_DPHY_ANA0_REG_SLV(7) |
23362306a36Sopenharmony_ci		     SUN6I_DPHY_ANA0_REG_DMPD(lanes_mask) |
23462306a36Sopenharmony_ci		     SUN6I_DPHY_ANA0_REG_DEN(lanes_mask));
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_ANA1_REG,
23762306a36Sopenharmony_ci		     SUN6I_DPHY_ANA1_REG_CSMPS(1) |
23862306a36Sopenharmony_ci		     SUN6I_DPHY_ANA1_REG_SVTT(7));
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_ANA4_REG,
24162306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_CKDV(1) |
24262306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_TMSC(1) |
24362306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_TMSD(1) |
24462306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_TXDNSC(1) |
24562306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_TXDNSD(1) |
24662306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_TXPUSC(1) |
24762306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_TXPUSD(1) |
24862306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_DMPLVC |
24962306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_DMPLVD(lanes_mask));
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_ANA2_REG,
25262306a36Sopenharmony_ci		     SUN6I_DPHY_ANA2_REG_ENIB);
25362306a36Sopenharmony_ci	udelay(5);
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_ANA3_REG,
25662306a36Sopenharmony_ci		     SUN6I_DPHY_ANA3_EN_LDOR |
25762306a36Sopenharmony_ci		     SUN6I_DPHY_ANA3_EN_LDOC |
25862306a36Sopenharmony_ci		     SUN6I_DPHY_ANA3_EN_LDOD);
25962306a36Sopenharmony_ci	udelay(1);
26062306a36Sopenharmony_ci}
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_cistatic void sun50i_a100_mipi_dphy_tx_power_on(struct sun6i_dphy *dphy)
26362306a36Sopenharmony_ci{
26462306a36Sopenharmony_ci	unsigned long mipi_symbol_rate = dphy->config.hs_clk_rate;
26562306a36Sopenharmony_ci	unsigned int div, n;
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_ANA4_REG,
26862306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_IB(2) |
26962306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_DMPLVD(4) |
27062306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_VTT_SET(3) |
27162306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_CKDV(3) |
27262306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_TMSD(1) |
27362306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_TMSC(1) |
27462306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_TXPUSD(2) |
27562306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_TXPUSC(3) |
27662306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_TXDNSD(2) |
27762306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_TXDNSC(3));
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci	regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA2_REG,
28062306a36Sopenharmony_ci			   SUN6I_DPHY_ANA2_EN_CK_CPU,
28162306a36Sopenharmony_ci			   SUN6I_DPHY_ANA2_EN_CK_CPU);
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci	regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA2_REG,
28462306a36Sopenharmony_ci			   SUN6I_DPHY_ANA2_REG_ENIB,
28562306a36Sopenharmony_ci			   SUN6I_DPHY_ANA2_REG_ENIB);
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_ANA3_REG,
28862306a36Sopenharmony_ci		     SUN6I_DPHY_ANA3_EN_LDOR |
28962306a36Sopenharmony_ci		     SUN6I_DPHY_ANA3_EN_LDOC |
29062306a36Sopenharmony_ci		     SUN6I_DPHY_ANA3_EN_LDOD);
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_ANA0_REG,
29362306a36Sopenharmony_ci		     SUN6I_DPHY_ANA0_REG_PLR(4) |
29462306a36Sopenharmony_ci		     SUN6I_DPHY_ANA0_REG_SFB(1));
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN50I_COMBO_PHY_REG0,
29762306a36Sopenharmony_ci		     SUN50I_COMBO_PHY_REG0_EN_CP);
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci	/* Choose a divider to limit the VCO frequency to around 2 GHz. */
30062306a36Sopenharmony_ci	div = 16 >> order_base_2(DIV_ROUND_UP(mipi_symbol_rate, 264000000));
30162306a36Sopenharmony_ci	n = mipi_symbol_rate * div / 24000000;
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN50I_DPHY_PLL_REG0,
30462306a36Sopenharmony_ci		     SUN50I_DPHY_PLL_REG0_CP36_EN |
30562306a36Sopenharmony_ci		     SUN50I_DPHY_PLL_REG0_LDO_EN |
30662306a36Sopenharmony_ci		     SUN50I_DPHY_PLL_REG0_EN_LVS |
30762306a36Sopenharmony_ci		     SUN50I_DPHY_PLL_REG0_PLL_EN |
30862306a36Sopenharmony_ci		     SUN50I_DPHY_PLL_REG0_NDET |
30962306a36Sopenharmony_ci		     SUN50I_DPHY_PLL_REG0_P((div - 1) % 8) |
31062306a36Sopenharmony_ci		     SUN50I_DPHY_PLL_REG0_N(n) |
31162306a36Sopenharmony_ci		     SUN50I_DPHY_PLL_REG0_M0((div - 1) / 8) |
31262306a36Sopenharmony_ci		     SUN50I_DPHY_PLL_REG0_M1(2));
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ci	/* Disable sigma-delta modulation. */
31562306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN50I_DPHY_PLL_REG2, 0);
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci	regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA4_REG,
31862306a36Sopenharmony_ci			   SUN6I_DPHY_ANA4_REG_EN_MIPI,
31962306a36Sopenharmony_ci			   SUN6I_DPHY_ANA4_REG_EN_MIPI);
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_ci	regmap_update_bits(dphy->regs, SUN50I_COMBO_PHY_REG0,
32262306a36Sopenharmony_ci			   SUN50I_COMBO_PHY_REG0_EN_MIPI |
32362306a36Sopenharmony_ci			   SUN50I_COMBO_PHY_REG0_EN_COMBOLDO,
32462306a36Sopenharmony_ci			   SUN50I_COMBO_PHY_REG0_EN_MIPI |
32562306a36Sopenharmony_ci			   SUN50I_COMBO_PHY_REG0_EN_COMBOLDO);
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN50I_COMBO_PHY_REG2,
32862306a36Sopenharmony_ci		     SUN50I_COMBO_PHY_REG2_HS_STOP_DLY(20));
32962306a36Sopenharmony_ci	udelay(1);
33062306a36Sopenharmony_ci}
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_cistatic int sun6i_dphy_tx_power_on(struct sun6i_dphy *dphy)
33362306a36Sopenharmony_ci{
33462306a36Sopenharmony_ci	u8 lanes_mask = GENMASK(dphy->config.lanes - 1, 0);
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_TX_CTL_REG,
33762306a36Sopenharmony_ci		     SUN6I_DPHY_TX_CTL_HS_TX_CLK_CONT);
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_TX_TIME0_REG,
34062306a36Sopenharmony_ci		     SUN6I_DPHY_TX_TIME0_LP_CLK_DIV(14) |
34162306a36Sopenharmony_ci		     SUN6I_DPHY_TX_TIME0_HS_PREPARE(6) |
34262306a36Sopenharmony_ci		     SUN6I_DPHY_TX_TIME0_HS_TRAIL(10));
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_TX_TIME1_REG,
34562306a36Sopenharmony_ci		     SUN6I_DPHY_TX_TIME1_CLK_PREPARE(7) |
34662306a36Sopenharmony_ci		     SUN6I_DPHY_TX_TIME1_CLK_ZERO(50) |
34762306a36Sopenharmony_ci		     SUN6I_DPHY_TX_TIME1_CLK_PRE(3) |
34862306a36Sopenharmony_ci		     SUN6I_DPHY_TX_TIME1_CLK_POST(10));
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_TX_TIME2_REG,
35162306a36Sopenharmony_ci		     SUN6I_DPHY_TX_TIME2_CLK_TRAIL(30));
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_TX_TIME3_REG, 0);
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_TX_TIME4_REG,
35662306a36Sopenharmony_ci		     SUN6I_DPHY_TX_TIME4_HS_TX_ANA0(3) |
35762306a36Sopenharmony_ci		     SUN6I_DPHY_TX_TIME4_HS_TX_ANA1(3));
35862306a36Sopenharmony_ci
35962306a36Sopenharmony_ci	dphy->variant->tx_power_on(dphy);
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_ci	regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA3_REG,
36262306a36Sopenharmony_ci			   SUN6I_DPHY_ANA3_EN_VTTC |
36362306a36Sopenharmony_ci			   SUN6I_DPHY_ANA3_EN_VTTD_MASK,
36462306a36Sopenharmony_ci			   SUN6I_DPHY_ANA3_EN_VTTC |
36562306a36Sopenharmony_ci			   SUN6I_DPHY_ANA3_EN_VTTD(lanes_mask));
36662306a36Sopenharmony_ci	udelay(1);
36762306a36Sopenharmony_ci
36862306a36Sopenharmony_ci	regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA3_REG,
36962306a36Sopenharmony_ci			   SUN6I_DPHY_ANA3_EN_DIV,
37062306a36Sopenharmony_ci			   SUN6I_DPHY_ANA3_EN_DIV);
37162306a36Sopenharmony_ci	udelay(1);
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_ci	regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA2_REG,
37462306a36Sopenharmony_ci			   SUN6I_DPHY_ANA2_EN_CK_CPU,
37562306a36Sopenharmony_ci			   SUN6I_DPHY_ANA2_EN_CK_CPU);
37662306a36Sopenharmony_ci	udelay(1);
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_ci	regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA1_REG,
37962306a36Sopenharmony_ci			   SUN6I_DPHY_ANA1_REG_VTTMODE,
38062306a36Sopenharmony_ci			   SUN6I_DPHY_ANA1_REG_VTTMODE);
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci	regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA2_REG,
38362306a36Sopenharmony_ci			   SUN6I_DPHY_ANA2_EN_P2S_CPU_MASK,
38462306a36Sopenharmony_ci			   SUN6I_DPHY_ANA2_EN_P2S_CPU(lanes_mask));
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_GCTL_REG,
38762306a36Sopenharmony_ci		     SUN6I_DPHY_GCTL_LANE_NUM(dphy->config.lanes) |
38862306a36Sopenharmony_ci		     SUN6I_DPHY_GCTL_EN);
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci	return 0;
39162306a36Sopenharmony_ci}
39262306a36Sopenharmony_ci
39362306a36Sopenharmony_cistatic int sun6i_dphy_rx_power_on(struct sun6i_dphy *dphy)
39462306a36Sopenharmony_ci{
39562306a36Sopenharmony_ci	/* Physical clock rate is actually half of symbol rate with DDR. */
39662306a36Sopenharmony_ci	unsigned long mipi_symbol_rate = dphy->config.hs_clk_rate;
39762306a36Sopenharmony_ci	unsigned long dphy_clk_rate;
39862306a36Sopenharmony_ci	unsigned int rx_dly;
39962306a36Sopenharmony_ci	unsigned int lprst_dly;
40062306a36Sopenharmony_ci	u32 value;
40162306a36Sopenharmony_ci
40262306a36Sopenharmony_ci	dphy_clk_rate = clk_get_rate(dphy->mod_clk);
40362306a36Sopenharmony_ci	if (!dphy_clk_rate)
40462306a36Sopenharmony_ci		return -EINVAL;
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_ci	/* Hardcoded timing parameters from the Allwinner BSP. */
40762306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_RX_TIME0_REG,
40862306a36Sopenharmony_ci		     SUN6I_DPHY_RX_TIME0_HS_RX_SYNC(255) |
40962306a36Sopenharmony_ci		     SUN6I_DPHY_RX_TIME0_HS_RX_CLK_MISS(255) |
41062306a36Sopenharmony_ci		     SUN6I_DPHY_RX_TIME0_LP_RX(255));
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci	/*
41362306a36Sopenharmony_ci	 * Formula from the Allwinner BSP, with hardcoded coefficients
41462306a36Sopenharmony_ci	 * (probably internal divider/multiplier).
41562306a36Sopenharmony_ci	 */
41662306a36Sopenharmony_ci	rx_dly = 8 * (unsigned int)(dphy_clk_rate / (mipi_symbol_rate / 8));
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ci	/*
41962306a36Sopenharmony_ci	 * The Allwinner BSP has an alternative formula for LP_RX_ULPS_WP:
42062306a36Sopenharmony_ci	 * lp_ulps_wp_cnt = lp_ulps_wp_ms * lp_clk / 1000
42162306a36Sopenharmony_ci	 * but does not use it and hardcodes 255 instead.
42262306a36Sopenharmony_ci	 */
42362306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_RX_TIME1_REG,
42462306a36Sopenharmony_ci		     SUN6I_DPHY_RX_TIME1_RX_DLY(rx_dly) |
42562306a36Sopenharmony_ci		     SUN6I_DPHY_RX_TIME1_LP_RX_ULPS_WP(255));
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ci	/* HS_RX_ANA0 value is hardcoded in the Allwinner BSP. */
42862306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_RX_TIME2_REG,
42962306a36Sopenharmony_ci		     SUN6I_DPHY_RX_TIME2_HS_RX_ANA0(4));
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_ci	/*
43262306a36Sopenharmony_ci	 * Formula from the Allwinner BSP, with hardcoded coefficients
43362306a36Sopenharmony_ci	 * (probably internal divider/multiplier).
43462306a36Sopenharmony_ci	 */
43562306a36Sopenharmony_ci	lprst_dly = 4 * (unsigned int)(dphy_clk_rate / (mipi_symbol_rate / 2));
43662306a36Sopenharmony_ci
43762306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_RX_TIME3_REG,
43862306a36Sopenharmony_ci		     SUN6I_DPHY_RX_TIME3_LPRST_DLY(lprst_dly));
43962306a36Sopenharmony_ci
44062306a36Sopenharmony_ci	/* Analog parameters are hardcoded in the Allwinner BSP. */
44162306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_ANA0_REG,
44262306a36Sopenharmony_ci		     SUN6I_DPHY_ANA0_REG_PWS |
44362306a36Sopenharmony_ci		     SUN6I_DPHY_ANA0_REG_SLV(7) |
44462306a36Sopenharmony_ci		     SUN6I_DPHY_ANA0_REG_SFB(2));
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_ANA1_REG,
44762306a36Sopenharmony_ci		     SUN6I_DPHY_ANA1_REG_SVTT(4));
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_ANA4_REG,
45062306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_DMPLVC |
45162306a36Sopenharmony_ci		     SUN6I_DPHY_ANA4_REG_DMPLVD(1));
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_ANA2_REG,
45462306a36Sopenharmony_ci		     SUN6I_DPHY_ANA2_REG_ENIB);
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_ANA3_REG,
45762306a36Sopenharmony_ci		     SUN6I_DPHY_ANA3_EN_LDOR |
45862306a36Sopenharmony_ci		     SUN6I_DPHY_ANA3_EN_LDOC |
45962306a36Sopenharmony_ci		     SUN6I_DPHY_ANA3_EN_LDOD);
46062306a36Sopenharmony_ci
46162306a36Sopenharmony_ci	/*
46262306a36Sopenharmony_ci	 * Delay comes from the Allwinner BSP, likely for internal regulator
46362306a36Sopenharmony_ci	 * ramp-up.
46462306a36Sopenharmony_ci	 */
46562306a36Sopenharmony_ci	udelay(3);
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci	value = SUN6I_DPHY_RX_CTL_EN_DBC | SUN6I_DPHY_RX_CTL_RX_CLK_FORCE;
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_ci	/*
47062306a36Sopenharmony_ci	 * Rx data lane force-enable bits are used as regular RX enable by the
47162306a36Sopenharmony_ci	 * Allwinner BSP.
47262306a36Sopenharmony_ci	 */
47362306a36Sopenharmony_ci	if (dphy->config.lanes >= 1)
47462306a36Sopenharmony_ci		value |= SUN6I_DPHY_RX_CTL_RX_D0_FORCE;
47562306a36Sopenharmony_ci	if (dphy->config.lanes >= 2)
47662306a36Sopenharmony_ci		value |= SUN6I_DPHY_RX_CTL_RX_D1_FORCE;
47762306a36Sopenharmony_ci	if (dphy->config.lanes >= 3)
47862306a36Sopenharmony_ci		value |= SUN6I_DPHY_RX_CTL_RX_D2_FORCE;
47962306a36Sopenharmony_ci	if (dphy->config.lanes == 4)
48062306a36Sopenharmony_ci		value |= SUN6I_DPHY_RX_CTL_RX_D3_FORCE;
48162306a36Sopenharmony_ci
48262306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_RX_CTL_REG, value);
48362306a36Sopenharmony_ci
48462306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_GCTL_REG,
48562306a36Sopenharmony_ci		     SUN6I_DPHY_GCTL_LANE_NUM(dphy->config.lanes) |
48662306a36Sopenharmony_ci		     SUN6I_DPHY_GCTL_EN);
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci	return 0;
48962306a36Sopenharmony_ci}
49062306a36Sopenharmony_ci
49162306a36Sopenharmony_cistatic int sun6i_dphy_power_on(struct phy *phy)
49262306a36Sopenharmony_ci{
49362306a36Sopenharmony_ci	struct sun6i_dphy *dphy = phy_get_drvdata(phy);
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ci	switch (dphy->direction) {
49662306a36Sopenharmony_ci	case SUN6I_DPHY_DIRECTION_TX:
49762306a36Sopenharmony_ci		return sun6i_dphy_tx_power_on(dphy);
49862306a36Sopenharmony_ci	case SUN6I_DPHY_DIRECTION_RX:
49962306a36Sopenharmony_ci		return sun6i_dphy_rx_power_on(dphy);
50062306a36Sopenharmony_ci	default:
50162306a36Sopenharmony_ci		return -EINVAL;
50262306a36Sopenharmony_ci	}
50362306a36Sopenharmony_ci}
50462306a36Sopenharmony_ci
50562306a36Sopenharmony_cistatic int sun6i_dphy_power_off(struct phy *phy)
50662306a36Sopenharmony_ci{
50762306a36Sopenharmony_ci	struct sun6i_dphy *dphy = phy_get_drvdata(phy);
50862306a36Sopenharmony_ci
50962306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_GCTL_REG, 0);
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_ANA0_REG, 0);
51262306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_ANA1_REG, 0);
51362306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_ANA2_REG, 0);
51462306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_ANA3_REG, 0);
51562306a36Sopenharmony_ci	regmap_write(dphy->regs, SUN6I_DPHY_ANA4_REG, 0);
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_ci	return 0;
51862306a36Sopenharmony_ci}
51962306a36Sopenharmony_ci
52062306a36Sopenharmony_cistatic int sun6i_dphy_exit(struct phy *phy)
52162306a36Sopenharmony_ci{
52262306a36Sopenharmony_ci	struct sun6i_dphy *dphy = phy_get_drvdata(phy);
52362306a36Sopenharmony_ci
52462306a36Sopenharmony_ci	clk_rate_exclusive_put(dphy->mod_clk);
52562306a36Sopenharmony_ci	clk_disable_unprepare(dphy->mod_clk);
52662306a36Sopenharmony_ci	reset_control_assert(dphy->reset);
52762306a36Sopenharmony_ci
52862306a36Sopenharmony_ci	return 0;
52962306a36Sopenharmony_ci}
53062306a36Sopenharmony_ci
53162306a36Sopenharmony_ci
53262306a36Sopenharmony_cistatic const struct phy_ops sun6i_dphy_ops = {
53362306a36Sopenharmony_ci	.configure	= sun6i_dphy_configure,
53462306a36Sopenharmony_ci	.power_on	= sun6i_dphy_power_on,
53562306a36Sopenharmony_ci	.power_off	= sun6i_dphy_power_off,
53662306a36Sopenharmony_ci	.init		= sun6i_dphy_init,
53762306a36Sopenharmony_ci	.exit		= sun6i_dphy_exit,
53862306a36Sopenharmony_ci};
53962306a36Sopenharmony_ci
54062306a36Sopenharmony_cistatic const struct regmap_config sun6i_dphy_regmap_config = {
54162306a36Sopenharmony_ci	.reg_bits	= 32,
54262306a36Sopenharmony_ci	.val_bits	= 32,
54362306a36Sopenharmony_ci	.reg_stride	= 4,
54462306a36Sopenharmony_ci	.max_register	= SUN50I_COMBO_PHY_REG2,
54562306a36Sopenharmony_ci	.name		= "mipi-dphy",
54662306a36Sopenharmony_ci};
54762306a36Sopenharmony_ci
54862306a36Sopenharmony_cistatic int sun6i_dphy_probe(struct platform_device *pdev)
54962306a36Sopenharmony_ci{
55062306a36Sopenharmony_ci	struct phy_provider *phy_provider;
55162306a36Sopenharmony_ci	struct sun6i_dphy *dphy;
55262306a36Sopenharmony_ci	const char *direction;
55362306a36Sopenharmony_ci	void __iomem *regs;
55462306a36Sopenharmony_ci	int ret;
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ci	dphy = devm_kzalloc(&pdev->dev, sizeof(*dphy), GFP_KERNEL);
55762306a36Sopenharmony_ci	if (!dphy)
55862306a36Sopenharmony_ci		return -ENOMEM;
55962306a36Sopenharmony_ci
56062306a36Sopenharmony_ci	dphy->variant = device_get_match_data(&pdev->dev);
56162306a36Sopenharmony_ci	if (!dphy->variant)
56262306a36Sopenharmony_ci		return -EINVAL;
56362306a36Sopenharmony_ci
56462306a36Sopenharmony_ci	regs = devm_platform_ioremap_resource(pdev, 0);
56562306a36Sopenharmony_ci	if (IS_ERR(regs)) {
56662306a36Sopenharmony_ci		dev_err(&pdev->dev, "Couldn't map the DPHY encoder registers\n");
56762306a36Sopenharmony_ci		return PTR_ERR(regs);
56862306a36Sopenharmony_ci	}
56962306a36Sopenharmony_ci
57062306a36Sopenharmony_ci	dphy->regs = devm_regmap_init_mmio_clk(&pdev->dev, "bus",
57162306a36Sopenharmony_ci					       regs, &sun6i_dphy_regmap_config);
57262306a36Sopenharmony_ci	if (IS_ERR(dphy->regs)) {
57362306a36Sopenharmony_ci		dev_err(&pdev->dev, "Couldn't create the DPHY encoder regmap\n");
57462306a36Sopenharmony_ci		return PTR_ERR(dphy->regs);
57562306a36Sopenharmony_ci	}
57662306a36Sopenharmony_ci
57762306a36Sopenharmony_ci	dphy->reset = devm_reset_control_get_shared(&pdev->dev, NULL);
57862306a36Sopenharmony_ci	if (IS_ERR(dphy->reset)) {
57962306a36Sopenharmony_ci		dev_err(&pdev->dev, "Couldn't get our reset line\n");
58062306a36Sopenharmony_ci		return PTR_ERR(dphy->reset);
58162306a36Sopenharmony_ci	}
58262306a36Sopenharmony_ci
58362306a36Sopenharmony_ci	dphy->mod_clk = devm_clk_get(&pdev->dev, "mod");
58462306a36Sopenharmony_ci	if (IS_ERR(dphy->mod_clk)) {
58562306a36Sopenharmony_ci		dev_err(&pdev->dev, "Couldn't get the DPHY mod clock\n");
58662306a36Sopenharmony_ci		return PTR_ERR(dphy->mod_clk);
58762306a36Sopenharmony_ci	}
58862306a36Sopenharmony_ci
58962306a36Sopenharmony_ci	dphy->phy = devm_phy_create(&pdev->dev, NULL, &sun6i_dphy_ops);
59062306a36Sopenharmony_ci	if (IS_ERR(dphy->phy)) {
59162306a36Sopenharmony_ci		dev_err(&pdev->dev, "failed to create PHY\n");
59262306a36Sopenharmony_ci		return PTR_ERR(dphy->phy);
59362306a36Sopenharmony_ci	}
59462306a36Sopenharmony_ci
59562306a36Sopenharmony_ci	dphy->direction = SUN6I_DPHY_DIRECTION_TX;
59662306a36Sopenharmony_ci
59762306a36Sopenharmony_ci	ret = of_property_read_string(pdev->dev.of_node, "allwinner,direction",
59862306a36Sopenharmony_ci				      &direction);
59962306a36Sopenharmony_ci
60062306a36Sopenharmony_ci	if (!ret && !strncmp(direction, "rx", 2)) {
60162306a36Sopenharmony_ci		if (!dphy->variant->rx_supported) {
60262306a36Sopenharmony_ci			dev_err(&pdev->dev, "RX not supported on this variant\n");
60362306a36Sopenharmony_ci			return -EOPNOTSUPP;
60462306a36Sopenharmony_ci		}
60562306a36Sopenharmony_ci
60662306a36Sopenharmony_ci		dphy->direction = SUN6I_DPHY_DIRECTION_RX;
60762306a36Sopenharmony_ci	}
60862306a36Sopenharmony_ci
60962306a36Sopenharmony_ci	phy_set_drvdata(dphy->phy, dphy);
61062306a36Sopenharmony_ci	phy_provider = devm_of_phy_provider_register(&pdev->dev, of_phy_simple_xlate);
61162306a36Sopenharmony_ci
61262306a36Sopenharmony_ci	return PTR_ERR_OR_ZERO(phy_provider);
61362306a36Sopenharmony_ci}
61462306a36Sopenharmony_ci
61562306a36Sopenharmony_cistatic const struct sun6i_dphy_variant sun6i_a31_mipi_dphy_variant = {
61662306a36Sopenharmony_ci	.tx_power_on	= sun6i_a31_mipi_dphy_tx_power_on,
61762306a36Sopenharmony_ci	.rx_supported	= true,
61862306a36Sopenharmony_ci};
61962306a36Sopenharmony_ci
62062306a36Sopenharmony_cistatic const struct sun6i_dphy_variant sun50i_a100_mipi_dphy_variant = {
62162306a36Sopenharmony_ci	.tx_power_on	= sun50i_a100_mipi_dphy_tx_power_on,
62262306a36Sopenharmony_ci};
62362306a36Sopenharmony_ci
62462306a36Sopenharmony_cistatic const struct of_device_id sun6i_dphy_of_table[] = {
62562306a36Sopenharmony_ci	{
62662306a36Sopenharmony_ci		.compatible	= "allwinner,sun6i-a31-mipi-dphy",
62762306a36Sopenharmony_ci		.data		= &sun6i_a31_mipi_dphy_variant,
62862306a36Sopenharmony_ci	},
62962306a36Sopenharmony_ci	{
63062306a36Sopenharmony_ci		.compatible	= "allwinner,sun50i-a100-mipi-dphy",
63162306a36Sopenharmony_ci		.data		= &sun50i_a100_mipi_dphy_variant,
63262306a36Sopenharmony_ci	},
63362306a36Sopenharmony_ci	{ }
63462306a36Sopenharmony_ci};
63562306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, sun6i_dphy_of_table);
63662306a36Sopenharmony_ci
63762306a36Sopenharmony_cistatic struct platform_driver sun6i_dphy_platform_driver = {
63862306a36Sopenharmony_ci	.probe		= sun6i_dphy_probe,
63962306a36Sopenharmony_ci	.driver		= {
64062306a36Sopenharmony_ci		.name		= "sun6i-mipi-dphy",
64162306a36Sopenharmony_ci		.of_match_table	= sun6i_dphy_of_table,
64262306a36Sopenharmony_ci	},
64362306a36Sopenharmony_ci};
64462306a36Sopenharmony_cimodule_platform_driver(sun6i_dphy_platform_driver);
64562306a36Sopenharmony_ci
64662306a36Sopenharmony_ciMODULE_AUTHOR("Maxime Ripard <maxime.ripard@bootlin>");
64762306a36Sopenharmony_ciMODULE_DESCRIPTION("Allwinner A31 MIPI D-PHY Driver");
64862306a36Sopenharmony_ciMODULE_LICENSE("GPL");
649