162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2018 Rockchip Electronics Co. Ltd.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Author: Wyon Bi <bivvy.bi@rock-chips.com>
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/bits.h>
962306a36Sopenharmony_ci#include <linux/kernel.h>
1062306a36Sopenharmony_ci#include <linux/clk.h>
1162306a36Sopenharmony_ci#include <linux/iopoll.h>
1262306a36Sopenharmony_ci#include <linux/clk-provider.h>
1362306a36Sopenharmony_ci#include <linux/delay.h>
1462306a36Sopenharmony_ci#include <linux/init.h>
1562306a36Sopenharmony_ci#include <linux/mfd/syscon.h>
1662306a36Sopenharmony_ci#include <linux/module.h>
1762306a36Sopenharmony_ci#include <linux/of.h>
1862306a36Sopenharmony_ci#include <linux/platform_device.h>
1962306a36Sopenharmony_ci#include <linux/pm_runtime.h>
2062306a36Sopenharmony_ci#include <linux/reset.h>
2162306a36Sopenharmony_ci#include <linux/time64.h>
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#include <linux/phy/phy.h>
2462306a36Sopenharmony_ci#include <linux/phy/phy-mipi-dphy.h>
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#define UPDATE(x, h, l)	(((x) << (l)) & GENMASK((h), (l)))
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/*
2962306a36Sopenharmony_ci * The offset address[7:0] is distributed two parts, one from the bit7 to bit5
3062306a36Sopenharmony_ci * is the first address, the other from the bit4 to bit0 is the second address.
3162306a36Sopenharmony_ci * when you configure the registers, you must set both of them. The Clock Lane
3262306a36Sopenharmony_ci * and Data Lane use the same registers with the same second address, but the
3362306a36Sopenharmony_ci * first address is different.
3462306a36Sopenharmony_ci */
3562306a36Sopenharmony_ci#define FIRST_ADDRESS(x)		(((x) & 0x7) << 5)
3662306a36Sopenharmony_ci#define SECOND_ADDRESS(x)		(((x) & 0x1f) << 0)
3762306a36Sopenharmony_ci#define PHY_REG(first, second)		(FIRST_ADDRESS(first) | \
3862306a36Sopenharmony_ci					 SECOND_ADDRESS(second))
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci/* Analog Register Part: reg00 */
4162306a36Sopenharmony_ci#define BANDGAP_POWER_MASK			BIT(7)
4262306a36Sopenharmony_ci#define BANDGAP_POWER_DOWN			BIT(7)
4362306a36Sopenharmony_ci#define BANDGAP_POWER_ON			0
4462306a36Sopenharmony_ci#define LANE_EN_MASK				GENMASK(6, 2)
4562306a36Sopenharmony_ci#define LANE_EN_CK				BIT(6)
4662306a36Sopenharmony_ci#define LANE_EN_3				BIT(5)
4762306a36Sopenharmony_ci#define LANE_EN_2				BIT(4)
4862306a36Sopenharmony_ci#define LANE_EN_1				BIT(3)
4962306a36Sopenharmony_ci#define LANE_EN_0				BIT(2)
5062306a36Sopenharmony_ci#define POWER_WORK_MASK				GENMASK(1, 0)
5162306a36Sopenharmony_ci#define POWER_WORK_ENABLE			UPDATE(1, 1, 0)
5262306a36Sopenharmony_ci#define POWER_WORK_DISABLE			UPDATE(2, 1, 0)
5362306a36Sopenharmony_ci/* Analog Register Part: reg01 */
5462306a36Sopenharmony_ci#define REG_SYNCRST_MASK			BIT(2)
5562306a36Sopenharmony_ci#define REG_SYNCRST_RESET			BIT(2)
5662306a36Sopenharmony_ci#define REG_SYNCRST_NORMAL			0
5762306a36Sopenharmony_ci#define REG_LDOPD_MASK				BIT(1)
5862306a36Sopenharmony_ci#define REG_LDOPD_POWER_DOWN			BIT(1)
5962306a36Sopenharmony_ci#define REG_LDOPD_POWER_ON			0
6062306a36Sopenharmony_ci#define REG_PLLPD_MASK				BIT(0)
6162306a36Sopenharmony_ci#define REG_PLLPD_POWER_DOWN			BIT(0)
6262306a36Sopenharmony_ci#define REG_PLLPD_POWER_ON			0
6362306a36Sopenharmony_ci/* Analog Register Part: reg03 */
6462306a36Sopenharmony_ci#define REG_FBDIV_HI_MASK			BIT(5)
6562306a36Sopenharmony_ci#define REG_FBDIV_HI(x)				UPDATE((x >> 8), 5, 5)
6662306a36Sopenharmony_ci#define REG_PREDIV_MASK				GENMASK(4, 0)
6762306a36Sopenharmony_ci#define REG_PREDIV(x)				UPDATE(x, 4, 0)
6862306a36Sopenharmony_ci/* Analog Register Part: reg04 */
6962306a36Sopenharmony_ci#define REG_FBDIV_LO_MASK			GENMASK(7, 0)
7062306a36Sopenharmony_ci#define REG_FBDIV_LO(x)				UPDATE(x, 7, 0)
7162306a36Sopenharmony_ci/* Analog Register Part: reg05 */
7262306a36Sopenharmony_ci#define SAMPLE_CLOCK_PHASE_MASK			GENMASK(6, 4)
7362306a36Sopenharmony_ci#define SAMPLE_CLOCK_PHASE(x)			UPDATE(x, 6, 4)
7462306a36Sopenharmony_ci#define CLOCK_LANE_SKEW_PHASE_MASK		GENMASK(2, 0)
7562306a36Sopenharmony_ci#define CLOCK_LANE_SKEW_PHASE(x)		UPDATE(x, 2, 0)
7662306a36Sopenharmony_ci/* Analog Register Part: reg06 */
7762306a36Sopenharmony_ci#define DATA_LANE_3_SKEW_PHASE_MASK		GENMASK(6, 4)
7862306a36Sopenharmony_ci#define DATA_LANE_3_SKEW_PHASE(x)		UPDATE(x, 6, 4)
7962306a36Sopenharmony_ci#define DATA_LANE_2_SKEW_PHASE_MASK		GENMASK(2, 0)
8062306a36Sopenharmony_ci#define DATA_LANE_2_SKEW_PHASE(x)		UPDATE(x, 2, 0)
8162306a36Sopenharmony_ci/* Analog Register Part: reg07 */
8262306a36Sopenharmony_ci#define DATA_LANE_1_SKEW_PHASE_MASK		GENMASK(6, 4)
8362306a36Sopenharmony_ci#define DATA_LANE_1_SKEW_PHASE(x)		UPDATE(x, 6, 4)
8462306a36Sopenharmony_ci#define DATA_LANE_0_SKEW_PHASE_MASK		GENMASK(2, 0)
8562306a36Sopenharmony_ci#define DATA_LANE_0_SKEW_PHASE(x)		UPDATE(x, 2, 0)
8662306a36Sopenharmony_ci/* Analog Register Part: reg08 */
8762306a36Sopenharmony_ci#define PLL_POST_DIV_ENABLE_MASK		BIT(5)
8862306a36Sopenharmony_ci#define PLL_POST_DIV_ENABLE			BIT(5)
8962306a36Sopenharmony_ci#define SAMPLE_CLOCK_DIRECTION_MASK		BIT(4)
9062306a36Sopenharmony_ci#define SAMPLE_CLOCK_DIRECTION_REVERSE		BIT(4)
9162306a36Sopenharmony_ci#define SAMPLE_CLOCK_DIRECTION_FORWARD		0
9262306a36Sopenharmony_ci#define LOWFRE_EN_MASK				BIT(5)
9362306a36Sopenharmony_ci#define PLL_OUTPUT_FREQUENCY_DIV_BY_1		0
9462306a36Sopenharmony_ci#define PLL_OUTPUT_FREQUENCY_DIV_BY_2		1
9562306a36Sopenharmony_ci/* Analog Register Part: reg0b */
9662306a36Sopenharmony_ci#define CLOCK_LANE_VOD_RANGE_SET_MASK		GENMASK(3, 0)
9762306a36Sopenharmony_ci#define CLOCK_LANE_VOD_RANGE_SET(x)		UPDATE(x, 3, 0)
9862306a36Sopenharmony_ci#define VOD_MIN_RANGE				0x1
9962306a36Sopenharmony_ci#define VOD_MID_RANGE				0x3
10062306a36Sopenharmony_ci#define VOD_BIG_RANGE				0x7
10162306a36Sopenharmony_ci#define VOD_MAX_RANGE				0xf
10262306a36Sopenharmony_ci/* Analog Register Part: reg1E */
10362306a36Sopenharmony_ci#define PLL_MODE_SEL_MASK			GENMASK(6, 5)
10462306a36Sopenharmony_ci#define PLL_MODE_SEL_LVDS_MODE			0
10562306a36Sopenharmony_ci#define PLL_MODE_SEL_MIPI_MODE			BIT(5)
10662306a36Sopenharmony_ci/* Digital Register Part: reg00 */
10762306a36Sopenharmony_ci#define REG_DIG_RSTN_MASK			BIT(0)
10862306a36Sopenharmony_ci#define REG_DIG_RSTN_NORMAL			BIT(0)
10962306a36Sopenharmony_ci#define REG_DIG_RSTN_RESET			0
11062306a36Sopenharmony_ci/* Digital Register Part: reg01 */
11162306a36Sopenharmony_ci#define INVERT_TXCLKESC_MASK			BIT(1)
11262306a36Sopenharmony_ci#define INVERT_TXCLKESC_ENABLE			BIT(1)
11362306a36Sopenharmony_ci#define INVERT_TXCLKESC_DISABLE			0
11462306a36Sopenharmony_ci#define INVERT_TXBYTECLKHS_MASK			BIT(0)
11562306a36Sopenharmony_ci#define INVERT_TXBYTECLKHS_ENABLE		BIT(0)
11662306a36Sopenharmony_ci#define INVERT_TXBYTECLKHS_DISABLE		0
11762306a36Sopenharmony_ci/* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg05 */
11862306a36Sopenharmony_ci#define T_LPX_CNT_MASK				GENMASK(5, 0)
11962306a36Sopenharmony_ci#define T_LPX_CNT(x)				UPDATE(x, 5, 0)
12062306a36Sopenharmony_ci/* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg06 */
12162306a36Sopenharmony_ci#define T_HS_ZERO_CNT_HI_MASK			BIT(7)
12262306a36Sopenharmony_ci#define T_HS_ZERO_CNT_HI(x)			UPDATE(x, 7, 7)
12362306a36Sopenharmony_ci#define T_HS_PREPARE_CNT_MASK			GENMASK(6, 0)
12462306a36Sopenharmony_ci#define T_HS_PREPARE_CNT(x)			UPDATE(x, 6, 0)
12562306a36Sopenharmony_ci/* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg07 */
12662306a36Sopenharmony_ci#define T_HS_ZERO_CNT_LO_MASK			GENMASK(5, 0)
12762306a36Sopenharmony_ci#define T_HS_ZERO_CNT_LO(x)			UPDATE(x, 5, 0)
12862306a36Sopenharmony_ci/* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg08 */
12962306a36Sopenharmony_ci#define T_HS_TRAIL_CNT_MASK			GENMASK(6, 0)
13062306a36Sopenharmony_ci#define T_HS_TRAIL_CNT(x)			UPDATE(x, 6, 0)
13162306a36Sopenharmony_ci/* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg09 */
13262306a36Sopenharmony_ci#define T_HS_EXIT_CNT_LO_MASK			GENMASK(4, 0)
13362306a36Sopenharmony_ci#define T_HS_EXIT_CNT_LO(x)			UPDATE(x, 4, 0)
13462306a36Sopenharmony_ci/* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg0a */
13562306a36Sopenharmony_ci#define T_CLK_POST_CNT_LO_MASK			GENMASK(3, 0)
13662306a36Sopenharmony_ci#define T_CLK_POST_CNT_LO(x)			UPDATE(x, 3, 0)
13762306a36Sopenharmony_ci/* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg0c */
13862306a36Sopenharmony_ci#define LPDT_TX_PPI_SYNC_MASK			BIT(2)
13962306a36Sopenharmony_ci#define LPDT_TX_PPI_SYNC_ENABLE			BIT(2)
14062306a36Sopenharmony_ci#define LPDT_TX_PPI_SYNC_DISABLE		0
14162306a36Sopenharmony_ci#define T_WAKEUP_CNT_HI_MASK			GENMASK(1, 0)
14262306a36Sopenharmony_ci#define T_WAKEUP_CNT_HI(x)			UPDATE(x, 1, 0)
14362306a36Sopenharmony_ci/* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg0d */
14462306a36Sopenharmony_ci#define T_WAKEUP_CNT_LO_MASK			GENMASK(7, 0)
14562306a36Sopenharmony_ci#define T_WAKEUP_CNT_LO(x)			UPDATE(x, 7, 0)
14662306a36Sopenharmony_ci/* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg0e */
14762306a36Sopenharmony_ci#define T_CLK_PRE_CNT_MASK			GENMASK(3, 0)
14862306a36Sopenharmony_ci#define T_CLK_PRE_CNT(x)			UPDATE(x, 3, 0)
14962306a36Sopenharmony_ci/* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg10 */
15062306a36Sopenharmony_ci#define T_CLK_POST_CNT_HI_MASK			GENMASK(7, 6)
15162306a36Sopenharmony_ci#define T_CLK_POST_CNT_HI(x)			UPDATE(x, 7, 6)
15262306a36Sopenharmony_ci#define T_TA_GO_CNT_MASK			GENMASK(5, 0)
15362306a36Sopenharmony_ci#define T_TA_GO_CNT(x)				UPDATE(x, 5, 0)
15462306a36Sopenharmony_ci/* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg11 */
15562306a36Sopenharmony_ci#define T_HS_EXIT_CNT_HI_MASK			BIT(6)
15662306a36Sopenharmony_ci#define T_HS_EXIT_CNT_HI(x)			UPDATE(x, 6, 6)
15762306a36Sopenharmony_ci#define T_TA_SURE_CNT_MASK			GENMASK(5, 0)
15862306a36Sopenharmony_ci#define T_TA_SURE_CNT(x)			UPDATE(x, 5, 0)
15962306a36Sopenharmony_ci/* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg12 */
16062306a36Sopenharmony_ci#define T_TA_WAIT_CNT_MASK			GENMASK(5, 0)
16162306a36Sopenharmony_ci#define T_TA_WAIT_CNT(x)			UPDATE(x, 5, 0)
16262306a36Sopenharmony_ci/* LVDS Register Part: reg00 */
16362306a36Sopenharmony_ci#define LVDS_DIGITAL_INTERNAL_RESET_MASK	BIT(2)
16462306a36Sopenharmony_ci#define LVDS_DIGITAL_INTERNAL_RESET_DISABLE	BIT(2)
16562306a36Sopenharmony_ci#define LVDS_DIGITAL_INTERNAL_RESET_ENABLE	0
16662306a36Sopenharmony_ci/* LVDS Register Part: reg01 */
16762306a36Sopenharmony_ci#define LVDS_DIGITAL_INTERNAL_ENABLE_MASK	BIT(7)
16862306a36Sopenharmony_ci#define LVDS_DIGITAL_INTERNAL_ENABLE		BIT(7)
16962306a36Sopenharmony_ci#define LVDS_DIGITAL_INTERNAL_DISABLE		0
17062306a36Sopenharmony_ci/* LVDS Register Part: reg03 */
17162306a36Sopenharmony_ci#define MODE_ENABLE_MASK			GENMASK(2, 0)
17262306a36Sopenharmony_ci#define TTL_MODE_ENABLE				BIT(2)
17362306a36Sopenharmony_ci#define LVDS_MODE_ENABLE			BIT(1)
17462306a36Sopenharmony_ci#define MIPI_MODE_ENABLE			BIT(0)
17562306a36Sopenharmony_ci/* LVDS Register Part: reg0b */
17662306a36Sopenharmony_ci#define LVDS_LANE_EN_MASK			GENMASK(7, 3)
17762306a36Sopenharmony_ci#define LVDS_DATA_LANE0_EN			BIT(7)
17862306a36Sopenharmony_ci#define LVDS_DATA_LANE1_EN			BIT(6)
17962306a36Sopenharmony_ci#define LVDS_DATA_LANE2_EN			BIT(5)
18062306a36Sopenharmony_ci#define LVDS_DATA_LANE3_EN			BIT(4)
18162306a36Sopenharmony_ci#define LVDS_CLK_LANE_EN			BIT(3)
18262306a36Sopenharmony_ci#define LVDS_PLL_POWER_MASK			BIT(2)
18362306a36Sopenharmony_ci#define LVDS_PLL_POWER_OFF			BIT(2)
18462306a36Sopenharmony_ci#define LVDS_PLL_POWER_ON			0
18562306a36Sopenharmony_ci#define LVDS_BANDGAP_POWER_MASK			BIT(0)
18662306a36Sopenharmony_ci#define LVDS_BANDGAP_POWER_DOWN			BIT(0)
18762306a36Sopenharmony_ci#define LVDS_BANDGAP_POWER_ON			0
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci#define DSI_PHY_RSTZ		0xa0
19062306a36Sopenharmony_ci#define PHY_ENABLECLK		BIT(2)
19162306a36Sopenharmony_ci#define DSI_PHY_STATUS		0xb0
19262306a36Sopenharmony_ci#define PHY_LOCK		BIT(0)
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_cienum phy_max_rate {
19562306a36Sopenharmony_ci	MAX_1GHZ,
19662306a36Sopenharmony_ci	MAX_2_5GHZ,
19762306a36Sopenharmony_ci};
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_cistruct inno_video_phy_plat_data {
20062306a36Sopenharmony_ci	const struct inno_mipi_dphy_timing *inno_mipi_dphy_timing_table;
20162306a36Sopenharmony_ci	const unsigned int num_timings;
20262306a36Sopenharmony_ci	enum phy_max_rate max_rate;
20362306a36Sopenharmony_ci};
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_cistruct inno_dsidphy {
20662306a36Sopenharmony_ci	struct device *dev;
20762306a36Sopenharmony_ci	struct clk *ref_clk;
20862306a36Sopenharmony_ci	struct clk *pclk_phy;
20962306a36Sopenharmony_ci	struct clk *pclk_host;
21062306a36Sopenharmony_ci	const struct inno_video_phy_plat_data *pdata;
21162306a36Sopenharmony_ci	void __iomem *phy_base;
21262306a36Sopenharmony_ci	void __iomem *host_base;
21362306a36Sopenharmony_ci	struct reset_control *rst;
21462306a36Sopenharmony_ci	enum phy_mode mode;
21562306a36Sopenharmony_ci	struct phy_configure_opts_mipi_dphy dphy_cfg;
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci	struct clk *pll_clk;
21862306a36Sopenharmony_ci	struct {
21962306a36Sopenharmony_ci		struct clk_hw hw;
22062306a36Sopenharmony_ci		u8 prediv;
22162306a36Sopenharmony_ci		u16 fbdiv;
22262306a36Sopenharmony_ci		unsigned long rate;
22362306a36Sopenharmony_ci	} pll;
22462306a36Sopenharmony_ci};
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_cienum {
22762306a36Sopenharmony_ci	REGISTER_PART_ANALOG,
22862306a36Sopenharmony_ci	REGISTER_PART_DIGITAL,
22962306a36Sopenharmony_ci	REGISTER_PART_CLOCK_LANE,
23062306a36Sopenharmony_ci	REGISTER_PART_DATA0_LANE,
23162306a36Sopenharmony_ci	REGISTER_PART_DATA1_LANE,
23262306a36Sopenharmony_ci	REGISTER_PART_DATA2_LANE,
23362306a36Sopenharmony_ci	REGISTER_PART_DATA3_LANE,
23462306a36Sopenharmony_ci	REGISTER_PART_LVDS,
23562306a36Sopenharmony_ci};
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_cistruct inno_mipi_dphy_timing {
23862306a36Sopenharmony_ci	unsigned long rate;
23962306a36Sopenharmony_ci	u8 lpx;
24062306a36Sopenharmony_ci	u8 hs_prepare;
24162306a36Sopenharmony_ci	u8 clk_lane_hs_zero;
24262306a36Sopenharmony_ci	u8 data_lane_hs_zero;
24362306a36Sopenharmony_ci	u8 hs_trail;
24462306a36Sopenharmony_ci};
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_cistatic const
24762306a36Sopenharmony_cistruct inno_mipi_dphy_timing inno_mipi_dphy_timing_table_max_1ghz[] = {
24862306a36Sopenharmony_ci	{ 110000000, 0x0, 0x20, 0x16, 0x02, 0x22},
24962306a36Sopenharmony_ci	{ 150000000, 0x0, 0x06, 0x16, 0x03, 0x45},
25062306a36Sopenharmony_ci	{ 200000000, 0x0, 0x18, 0x17, 0x04, 0x0b},
25162306a36Sopenharmony_ci	{ 250000000, 0x0, 0x05, 0x17, 0x05, 0x16},
25262306a36Sopenharmony_ci	{ 300000000, 0x0, 0x51, 0x18, 0x06, 0x2c},
25362306a36Sopenharmony_ci	{ 400000000, 0x0, 0x64, 0x19, 0x07, 0x33},
25462306a36Sopenharmony_ci	{ 500000000, 0x0, 0x20, 0x1b, 0x07, 0x4e},
25562306a36Sopenharmony_ci	{ 600000000, 0x0, 0x6a, 0x1d, 0x08, 0x3a},
25662306a36Sopenharmony_ci	{ 700000000, 0x0, 0x3e, 0x1e, 0x08, 0x6a},
25762306a36Sopenharmony_ci	{ 800000000, 0x0, 0x21, 0x1f, 0x09, 0x29},
25862306a36Sopenharmony_ci	{1000000000, 0x0, 0x09, 0x20, 0x09, 0x27},
25962306a36Sopenharmony_ci};
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_cistatic const
26262306a36Sopenharmony_cistruct inno_mipi_dphy_timing inno_mipi_dphy_timing_table_max_2_5ghz[] = {
26362306a36Sopenharmony_ci	{ 110000000, 0x02, 0x7f, 0x16, 0x02, 0x02},
26462306a36Sopenharmony_ci	{ 150000000, 0x02, 0x7f, 0x16, 0x03, 0x02},
26562306a36Sopenharmony_ci	{ 200000000, 0x02, 0x7f, 0x17, 0x04, 0x02},
26662306a36Sopenharmony_ci	{ 250000000, 0x02, 0x7f, 0x17, 0x05, 0x04},
26762306a36Sopenharmony_ci	{ 300000000, 0x02, 0x7f, 0x18, 0x06, 0x04},
26862306a36Sopenharmony_ci	{ 400000000, 0x03, 0x7e, 0x19, 0x07, 0x04},
26962306a36Sopenharmony_ci	{ 500000000, 0x03, 0x7c, 0x1b, 0x07, 0x08},
27062306a36Sopenharmony_ci	{ 600000000, 0x03, 0x70, 0x1d, 0x08, 0x10},
27162306a36Sopenharmony_ci	{ 700000000, 0x05, 0x40, 0x1e, 0x08, 0x30},
27262306a36Sopenharmony_ci	{ 800000000, 0x05, 0x02, 0x1f, 0x09, 0x30},
27362306a36Sopenharmony_ci	{1000000000, 0x05, 0x08, 0x20, 0x09, 0x30},
27462306a36Sopenharmony_ci	{1200000000, 0x06, 0x03, 0x32, 0x14, 0x0f},
27562306a36Sopenharmony_ci	{1400000000, 0x09, 0x03, 0x32, 0x14, 0x0f},
27662306a36Sopenharmony_ci	{1600000000, 0x0d, 0x42, 0x36, 0x0e, 0x0f},
27762306a36Sopenharmony_ci	{1800000000, 0x0e, 0x47, 0x7a, 0x0e, 0x0f},
27862306a36Sopenharmony_ci	{2000000000, 0x11, 0x64, 0x7a, 0x0e, 0x0b},
27962306a36Sopenharmony_ci	{2200000000, 0x13, 0x64, 0x7e, 0x15, 0x0b},
28062306a36Sopenharmony_ci	{2400000000, 0x13, 0x33, 0x7f, 0x15, 0x6a},
28162306a36Sopenharmony_ci	{2500000000, 0x15, 0x54, 0x7f, 0x15, 0x6a},
28262306a36Sopenharmony_ci};
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_cistatic void phy_update_bits(struct inno_dsidphy *inno,
28562306a36Sopenharmony_ci			    u8 first, u8 second, u8 mask, u8 val)
28662306a36Sopenharmony_ci{
28762306a36Sopenharmony_ci	u32 reg = PHY_REG(first, second) << 2;
28862306a36Sopenharmony_ci	unsigned int tmp, orig;
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci	orig = readl(inno->phy_base + reg);
29162306a36Sopenharmony_ci	tmp = orig & ~mask;
29262306a36Sopenharmony_ci	tmp |= val & mask;
29362306a36Sopenharmony_ci	writel(tmp, inno->phy_base + reg);
29462306a36Sopenharmony_ci}
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_cistatic unsigned long inno_dsidphy_pll_calc_rate(struct inno_dsidphy *inno,
29762306a36Sopenharmony_ci						unsigned long rate)
29862306a36Sopenharmony_ci{
29962306a36Sopenharmony_ci	unsigned long prate = clk_get_rate(inno->ref_clk);
30062306a36Sopenharmony_ci	unsigned long best_freq = 0;
30162306a36Sopenharmony_ci	unsigned long fref, fout;
30262306a36Sopenharmony_ci	u8 min_prediv, max_prediv;
30362306a36Sopenharmony_ci	u8 _prediv, best_prediv = 1;
30462306a36Sopenharmony_ci	u16 _fbdiv, best_fbdiv = 1;
30562306a36Sopenharmony_ci	u32 min_delta = UINT_MAX;
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci	/*
30862306a36Sopenharmony_ci	 * The PLL output frequency can be calculated using a simple formula:
30962306a36Sopenharmony_ci	 * PLL_Output_Frequency = (FREF / PREDIV * FBDIV) / 2
31062306a36Sopenharmony_ci	 * PLL_Output_Frequency: it is equal to DDR-Clock-Frequency * 2
31162306a36Sopenharmony_ci	 */
31262306a36Sopenharmony_ci	fref = prate / 2;
31362306a36Sopenharmony_ci	if (rate > 1000000000UL)
31462306a36Sopenharmony_ci		fout = 1000000000UL;
31562306a36Sopenharmony_ci	else
31662306a36Sopenharmony_ci		fout = rate;
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_ci	/* 5Mhz < Fref / prediv < 40MHz */
31962306a36Sopenharmony_ci	min_prediv = DIV_ROUND_UP(fref, 40000000);
32062306a36Sopenharmony_ci	max_prediv = fref / 5000000;
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci	for (_prediv = min_prediv; _prediv <= max_prediv; _prediv++) {
32362306a36Sopenharmony_ci		u64 tmp;
32462306a36Sopenharmony_ci		u32 delta;
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci		tmp = (u64)fout * _prediv;
32762306a36Sopenharmony_ci		do_div(tmp, fref);
32862306a36Sopenharmony_ci		_fbdiv = tmp;
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci		/*
33162306a36Sopenharmony_ci		 * The possible settings of feedback divider are
33262306a36Sopenharmony_ci		 * 12, 13, 14, 16, ~ 511
33362306a36Sopenharmony_ci		 */
33462306a36Sopenharmony_ci		if (_fbdiv == 15)
33562306a36Sopenharmony_ci			continue;
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci		if (_fbdiv < 12 || _fbdiv > 511)
33862306a36Sopenharmony_ci			continue;
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_ci		tmp = (u64)_fbdiv * fref;
34162306a36Sopenharmony_ci		do_div(tmp, _prediv);
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci		delta = abs(fout - tmp);
34462306a36Sopenharmony_ci		if (!delta) {
34562306a36Sopenharmony_ci			best_prediv = _prediv;
34662306a36Sopenharmony_ci			best_fbdiv = _fbdiv;
34762306a36Sopenharmony_ci			best_freq = tmp;
34862306a36Sopenharmony_ci			break;
34962306a36Sopenharmony_ci		} else if (delta < min_delta) {
35062306a36Sopenharmony_ci			best_prediv = _prediv;
35162306a36Sopenharmony_ci			best_fbdiv = _fbdiv;
35262306a36Sopenharmony_ci			best_freq = tmp;
35362306a36Sopenharmony_ci			min_delta = delta;
35462306a36Sopenharmony_ci		}
35562306a36Sopenharmony_ci	}
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ci	if (best_freq) {
35862306a36Sopenharmony_ci		inno->pll.prediv = best_prediv;
35962306a36Sopenharmony_ci		inno->pll.fbdiv = best_fbdiv;
36062306a36Sopenharmony_ci		inno->pll.rate = best_freq;
36162306a36Sopenharmony_ci	}
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci	return best_freq;
36462306a36Sopenharmony_ci}
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_cistatic void inno_dsidphy_mipi_mode_enable(struct inno_dsidphy *inno)
36762306a36Sopenharmony_ci{
36862306a36Sopenharmony_ci	struct phy_configure_opts_mipi_dphy *cfg = &inno->dphy_cfg;
36962306a36Sopenharmony_ci	const struct inno_mipi_dphy_timing *timings;
37062306a36Sopenharmony_ci	u32 t_txbyteclkhs, t_txclkesc;
37162306a36Sopenharmony_ci	u32 txbyteclkhs, txclkesc, esc_clk_div;
37262306a36Sopenharmony_ci	u32 hs_exit, clk_post, clk_pre, wakeup, lpx, ta_go, ta_sure, ta_wait;
37362306a36Sopenharmony_ci	u32 hs_prepare, hs_trail, hs_zero, clk_lane_hs_zero, data_lane_hs_zero;
37462306a36Sopenharmony_ci	unsigned int i;
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_ci	timings = inno->pdata->inno_mipi_dphy_timing_table;
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_ci	inno_dsidphy_pll_calc_rate(inno, cfg->hs_clk_rate);
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci	/* Select MIPI mode */
38162306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_LVDS, 0x03,
38262306a36Sopenharmony_ci			MODE_ENABLE_MASK, MIPI_MODE_ENABLE);
38362306a36Sopenharmony_ci	/* Configure PLL */
38462306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_ANALOG, 0x03,
38562306a36Sopenharmony_ci			REG_PREDIV_MASK, REG_PREDIV(inno->pll.prediv));
38662306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_ANALOG, 0x03,
38762306a36Sopenharmony_ci			REG_FBDIV_HI_MASK, REG_FBDIV_HI(inno->pll.fbdiv));
38862306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_ANALOG, 0x04,
38962306a36Sopenharmony_ci			REG_FBDIV_LO_MASK, REG_FBDIV_LO(inno->pll.fbdiv));
39062306a36Sopenharmony_ci	if (inno->pdata->max_rate == MAX_2_5GHZ) {
39162306a36Sopenharmony_ci		phy_update_bits(inno, REGISTER_PART_ANALOG, 0x08,
39262306a36Sopenharmony_ci				PLL_POST_DIV_ENABLE_MASK, PLL_POST_DIV_ENABLE);
39362306a36Sopenharmony_ci		phy_update_bits(inno, REGISTER_PART_ANALOG, 0x0b,
39462306a36Sopenharmony_ci				CLOCK_LANE_VOD_RANGE_SET_MASK,
39562306a36Sopenharmony_ci				CLOCK_LANE_VOD_RANGE_SET(VOD_MAX_RANGE));
39662306a36Sopenharmony_ci	}
39762306a36Sopenharmony_ci	/* Enable PLL and LDO */
39862306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_ANALOG, 0x01,
39962306a36Sopenharmony_ci			REG_LDOPD_MASK | REG_PLLPD_MASK,
40062306a36Sopenharmony_ci			REG_LDOPD_POWER_ON | REG_PLLPD_POWER_ON);
40162306a36Sopenharmony_ci	/* Reset analog */
40262306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_ANALOG, 0x01,
40362306a36Sopenharmony_ci			REG_SYNCRST_MASK, REG_SYNCRST_RESET);
40462306a36Sopenharmony_ci	udelay(1);
40562306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_ANALOG, 0x01,
40662306a36Sopenharmony_ci			REG_SYNCRST_MASK, REG_SYNCRST_NORMAL);
40762306a36Sopenharmony_ci	/* Reset digital */
40862306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_DIGITAL, 0x00,
40962306a36Sopenharmony_ci			REG_DIG_RSTN_MASK, REG_DIG_RSTN_RESET);
41062306a36Sopenharmony_ci	udelay(1);
41162306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_DIGITAL, 0x00,
41262306a36Sopenharmony_ci			REG_DIG_RSTN_MASK, REG_DIG_RSTN_NORMAL);
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ci	txbyteclkhs = inno->pll.rate / 8;
41562306a36Sopenharmony_ci	t_txbyteclkhs = div_u64(PSEC_PER_SEC, txbyteclkhs);
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_ci	esc_clk_div = DIV_ROUND_UP(txbyteclkhs, 20000000);
41862306a36Sopenharmony_ci	txclkesc = txbyteclkhs / esc_clk_div;
41962306a36Sopenharmony_ci	t_txclkesc = div_u64(PSEC_PER_SEC, txclkesc);
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci	/*
42262306a36Sopenharmony_ci	 * The value of counter for HS Ths-exit
42362306a36Sopenharmony_ci	 * Ths-exit = Tpin_txbyteclkhs * value
42462306a36Sopenharmony_ci	 */
42562306a36Sopenharmony_ci	hs_exit = DIV_ROUND_UP(cfg->hs_exit, t_txbyteclkhs);
42662306a36Sopenharmony_ci	/*
42762306a36Sopenharmony_ci	 * The value of counter for HS Tclk-post
42862306a36Sopenharmony_ci	 * Tclk-post = Tpin_txbyteclkhs * value
42962306a36Sopenharmony_ci	 */
43062306a36Sopenharmony_ci	clk_post = DIV_ROUND_UP(cfg->clk_post, t_txbyteclkhs);
43162306a36Sopenharmony_ci	/*
43262306a36Sopenharmony_ci	 * The value of counter for HS Tclk-pre
43362306a36Sopenharmony_ci	 * Tclk-pre = Tpin_txbyteclkhs * value
43462306a36Sopenharmony_ci	 */
43562306a36Sopenharmony_ci	clk_pre = DIV_ROUND_UP(cfg->clk_pre, BITS_PER_BYTE);
43662306a36Sopenharmony_ci
43762306a36Sopenharmony_ci	/*
43862306a36Sopenharmony_ci	 * The value of counter for HS Tta-go
43962306a36Sopenharmony_ci	 * Tta-go for turnaround
44062306a36Sopenharmony_ci	 * Tta-go = Ttxclkesc * value
44162306a36Sopenharmony_ci	 */
44262306a36Sopenharmony_ci	ta_go = DIV_ROUND_UP(cfg->ta_go, t_txclkesc);
44362306a36Sopenharmony_ci	/*
44462306a36Sopenharmony_ci	 * The value of counter for HS Tta-sure
44562306a36Sopenharmony_ci	 * Tta-sure for turnaround
44662306a36Sopenharmony_ci	 * Tta-sure = Ttxclkesc * value
44762306a36Sopenharmony_ci	 */
44862306a36Sopenharmony_ci	ta_sure = DIV_ROUND_UP(cfg->ta_sure, t_txclkesc);
44962306a36Sopenharmony_ci	/*
45062306a36Sopenharmony_ci	 * The value of counter for HS Tta-wait
45162306a36Sopenharmony_ci	 * Tta-wait for turnaround
45262306a36Sopenharmony_ci	 * Tta-wait = Ttxclkesc * value
45362306a36Sopenharmony_ci	 */
45462306a36Sopenharmony_ci	ta_wait = DIV_ROUND_UP(cfg->ta_get, t_txclkesc);
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_ci	for (i = 0; i < inno->pdata->num_timings; i++)
45762306a36Sopenharmony_ci		if (inno->pll.rate <= timings[i].rate)
45862306a36Sopenharmony_ci			break;
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci	if (i == inno->pdata->num_timings)
46162306a36Sopenharmony_ci		--i;
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ci	/*
46462306a36Sopenharmony_ci	 * The value of counter for HS Tlpx Time
46562306a36Sopenharmony_ci	 * Tlpx = Tpin_txbyteclkhs * (2 + value)
46662306a36Sopenharmony_ci	 */
46762306a36Sopenharmony_ci	if (inno->pdata->max_rate == MAX_1GHZ) {
46862306a36Sopenharmony_ci		lpx = DIV_ROUND_UP(cfg->lpx, t_txbyteclkhs);
46962306a36Sopenharmony_ci		if (lpx >= 2)
47062306a36Sopenharmony_ci			lpx -= 2;
47162306a36Sopenharmony_ci	} else
47262306a36Sopenharmony_ci		lpx = timings[i].lpx;
47362306a36Sopenharmony_ci
47462306a36Sopenharmony_ci	hs_prepare = timings[i].hs_prepare;
47562306a36Sopenharmony_ci	hs_trail = timings[i].hs_trail;
47662306a36Sopenharmony_ci	clk_lane_hs_zero = timings[i].clk_lane_hs_zero;
47762306a36Sopenharmony_ci	data_lane_hs_zero = timings[i].data_lane_hs_zero;
47862306a36Sopenharmony_ci	wakeup = 0x3ff;
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ci	for (i = REGISTER_PART_CLOCK_LANE; i <= REGISTER_PART_DATA3_LANE; i++) {
48162306a36Sopenharmony_ci		if (i == REGISTER_PART_CLOCK_LANE)
48262306a36Sopenharmony_ci			hs_zero = clk_lane_hs_zero;
48362306a36Sopenharmony_ci		else
48462306a36Sopenharmony_ci			hs_zero = data_lane_hs_zero;
48562306a36Sopenharmony_ci
48662306a36Sopenharmony_ci		phy_update_bits(inno, i, 0x05, T_LPX_CNT_MASK,
48762306a36Sopenharmony_ci				T_LPX_CNT(lpx));
48862306a36Sopenharmony_ci		phy_update_bits(inno, i, 0x06, T_HS_PREPARE_CNT_MASK,
48962306a36Sopenharmony_ci				T_HS_PREPARE_CNT(hs_prepare));
49062306a36Sopenharmony_ci		if (inno->pdata->max_rate == MAX_2_5GHZ)
49162306a36Sopenharmony_ci			phy_update_bits(inno, i, 0x06, T_HS_ZERO_CNT_HI_MASK,
49262306a36Sopenharmony_ci					T_HS_ZERO_CNT_HI(hs_zero >> 6));
49362306a36Sopenharmony_ci		phy_update_bits(inno, i, 0x07, T_HS_ZERO_CNT_LO_MASK,
49462306a36Sopenharmony_ci				T_HS_ZERO_CNT_LO(hs_zero));
49562306a36Sopenharmony_ci		phy_update_bits(inno, i, 0x08, T_HS_TRAIL_CNT_MASK,
49662306a36Sopenharmony_ci				T_HS_TRAIL_CNT(hs_trail));
49762306a36Sopenharmony_ci		if (inno->pdata->max_rate == MAX_2_5GHZ)
49862306a36Sopenharmony_ci			phy_update_bits(inno, i, 0x11, T_HS_EXIT_CNT_HI_MASK,
49962306a36Sopenharmony_ci					T_HS_EXIT_CNT_HI(hs_exit >> 5));
50062306a36Sopenharmony_ci		phy_update_bits(inno, i, 0x09, T_HS_EXIT_CNT_LO_MASK,
50162306a36Sopenharmony_ci				T_HS_EXIT_CNT_LO(hs_exit));
50262306a36Sopenharmony_ci		if (inno->pdata->max_rate == MAX_2_5GHZ)
50362306a36Sopenharmony_ci			phy_update_bits(inno, i, 0x10, T_CLK_POST_CNT_HI_MASK,
50462306a36Sopenharmony_ci					T_CLK_POST_CNT_HI(clk_post >> 4));
50562306a36Sopenharmony_ci		phy_update_bits(inno, i, 0x0a, T_CLK_POST_CNT_LO_MASK,
50662306a36Sopenharmony_ci				T_CLK_POST_CNT_LO(clk_post));
50762306a36Sopenharmony_ci		phy_update_bits(inno, i, 0x0e, T_CLK_PRE_CNT_MASK,
50862306a36Sopenharmony_ci				T_CLK_PRE_CNT(clk_pre));
50962306a36Sopenharmony_ci		phy_update_bits(inno, i, 0x0c, T_WAKEUP_CNT_HI_MASK,
51062306a36Sopenharmony_ci				T_WAKEUP_CNT_HI(wakeup >> 8));
51162306a36Sopenharmony_ci		phy_update_bits(inno, i, 0x0d, T_WAKEUP_CNT_LO_MASK,
51262306a36Sopenharmony_ci				T_WAKEUP_CNT_LO(wakeup));
51362306a36Sopenharmony_ci		phy_update_bits(inno, i, 0x10, T_TA_GO_CNT_MASK,
51462306a36Sopenharmony_ci				T_TA_GO_CNT(ta_go));
51562306a36Sopenharmony_ci		phy_update_bits(inno, i, 0x11, T_TA_SURE_CNT_MASK,
51662306a36Sopenharmony_ci				T_TA_SURE_CNT(ta_sure));
51762306a36Sopenharmony_ci		phy_update_bits(inno, i, 0x12, T_TA_WAIT_CNT_MASK,
51862306a36Sopenharmony_ci				T_TA_WAIT_CNT(ta_wait));
51962306a36Sopenharmony_ci	}
52062306a36Sopenharmony_ci
52162306a36Sopenharmony_ci	/* Enable all lanes on analog part */
52262306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_ANALOG, 0x00,
52362306a36Sopenharmony_ci			LANE_EN_MASK, LANE_EN_CK | LANE_EN_3 | LANE_EN_2 |
52462306a36Sopenharmony_ci			LANE_EN_1 | LANE_EN_0);
52562306a36Sopenharmony_ci}
52662306a36Sopenharmony_ci
52762306a36Sopenharmony_cistatic void inno_dsidphy_lvds_mode_enable(struct inno_dsidphy *inno)
52862306a36Sopenharmony_ci{
52962306a36Sopenharmony_ci	u8 prediv = 2;
53062306a36Sopenharmony_ci	u16 fbdiv = 28;
53162306a36Sopenharmony_ci
53262306a36Sopenharmony_ci	/* Sample clock reverse direction */
53362306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_ANALOG, 0x08,
53462306a36Sopenharmony_ci			SAMPLE_CLOCK_DIRECTION_MASK | LOWFRE_EN_MASK,
53562306a36Sopenharmony_ci			SAMPLE_CLOCK_DIRECTION_REVERSE |
53662306a36Sopenharmony_ci			PLL_OUTPUT_FREQUENCY_DIV_BY_1);
53762306a36Sopenharmony_ci
53862306a36Sopenharmony_ci	/* Select LVDS mode */
53962306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_LVDS, 0x03,
54062306a36Sopenharmony_ci			MODE_ENABLE_MASK, LVDS_MODE_ENABLE);
54162306a36Sopenharmony_ci	/* Configure PLL */
54262306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_ANALOG, 0x03,
54362306a36Sopenharmony_ci			REG_PREDIV_MASK, REG_PREDIV(prediv));
54462306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_ANALOG, 0x03,
54562306a36Sopenharmony_ci			REG_FBDIV_HI_MASK, REG_FBDIV_HI(fbdiv));
54662306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_ANALOG, 0x04,
54762306a36Sopenharmony_ci			REG_FBDIV_LO_MASK, REG_FBDIV_LO(fbdiv));
54862306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_LVDS, 0x08, 0xff, 0xfc);
54962306a36Sopenharmony_ci	/* Enable PLL and Bandgap */
55062306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_LVDS, 0x0b,
55162306a36Sopenharmony_ci			LVDS_PLL_POWER_MASK | LVDS_BANDGAP_POWER_MASK,
55262306a36Sopenharmony_ci			LVDS_PLL_POWER_ON | LVDS_BANDGAP_POWER_ON);
55362306a36Sopenharmony_ci
55462306a36Sopenharmony_ci	msleep(20);
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ci	/* Select PLL mode */
55762306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_ANALOG, 0x1e,
55862306a36Sopenharmony_ci			PLL_MODE_SEL_MASK, PLL_MODE_SEL_LVDS_MODE);
55962306a36Sopenharmony_ci
56062306a36Sopenharmony_ci	/* Reset LVDS digital logic */
56162306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_LVDS, 0x00,
56262306a36Sopenharmony_ci			LVDS_DIGITAL_INTERNAL_RESET_MASK,
56362306a36Sopenharmony_ci			LVDS_DIGITAL_INTERNAL_RESET_ENABLE);
56462306a36Sopenharmony_ci	udelay(1);
56562306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_LVDS, 0x00,
56662306a36Sopenharmony_ci			LVDS_DIGITAL_INTERNAL_RESET_MASK,
56762306a36Sopenharmony_ci			LVDS_DIGITAL_INTERNAL_RESET_DISABLE);
56862306a36Sopenharmony_ci	/* Enable LVDS digital logic */
56962306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_LVDS, 0x01,
57062306a36Sopenharmony_ci			LVDS_DIGITAL_INTERNAL_ENABLE_MASK,
57162306a36Sopenharmony_ci			LVDS_DIGITAL_INTERNAL_ENABLE);
57262306a36Sopenharmony_ci	/* Enable LVDS analog driver */
57362306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_LVDS, 0x0b,
57462306a36Sopenharmony_ci			LVDS_LANE_EN_MASK, LVDS_CLK_LANE_EN |
57562306a36Sopenharmony_ci			LVDS_DATA_LANE0_EN | LVDS_DATA_LANE1_EN |
57662306a36Sopenharmony_ci			LVDS_DATA_LANE2_EN | LVDS_DATA_LANE3_EN);
57762306a36Sopenharmony_ci}
57862306a36Sopenharmony_ci
57962306a36Sopenharmony_cistatic int inno_dsidphy_power_on(struct phy *phy)
58062306a36Sopenharmony_ci{
58162306a36Sopenharmony_ci	struct inno_dsidphy *inno = phy_get_drvdata(phy);
58262306a36Sopenharmony_ci
58362306a36Sopenharmony_ci	clk_prepare_enable(inno->pclk_phy);
58462306a36Sopenharmony_ci	clk_prepare_enable(inno->ref_clk);
58562306a36Sopenharmony_ci	pm_runtime_get_sync(inno->dev);
58662306a36Sopenharmony_ci
58762306a36Sopenharmony_ci	/* Bandgap power on */
58862306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_ANALOG, 0x00,
58962306a36Sopenharmony_ci			BANDGAP_POWER_MASK, BANDGAP_POWER_ON);
59062306a36Sopenharmony_ci	/* Enable power work */
59162306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_ANALOG, 0x00,
59262306a36Sopenharmony_ci			POWER_WORK_MASK, POWER_WORK_ENABLE);
59362306a36Sopenharmony_ci
59462306a36Sopenharmony_ci	switch (inno->mode) {
59562306a36Sopenharmony_ci	case PHY_MODE_MIPI_DPHY:
59662306a36Sopenharmony_ci		inno_dsidphy_mipi_mode_enable(inno);
59762306a36Sopenharmony_ci		break;
59862306a36Sopenharmony_ci	case PHY_MODE_LVDS:
59962306a36Sopenharmony_ci		inno_dsidphy_lvds_mode_enable(inno);
60062306a36Sopenharmony_ci		break;
60162306a36Sopenharmony_ci	default:
60262306a36Sopenharmony_ci		return -EINVAL;
60362306a36Sopenharmony_ci	}
60462306a36Sopenharmony_ci
60562306a36Sopenharmony_ci	return 0;
60662306a36Sopenharmony_ci}
60762306a36Sopenharmony_ci
60862306a36Sopenharmony_cistatic int inno_dsidphy_power_off(struct phy *phy)
60962306a36Sopenharmony_ci{
61062306a36Sopenharmony_ci	struct inno_dsidphy *inno = phy_get_drvdata(phy);
61162306a36Sopenharmony_ci
61262306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_ANALOG, 0x00, LANE_EN_MASK, 0);
61362306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_ANALOG, 0x01,
61462306a36Sopenharmony_ci			REG_LDOPD_MASK | REG_PLLPD_MASK,
61562306a36Sopenharmony_ci			REG_LDOPD_POWER_DOWN | REG_PLLPD_POWER_DOWN);
61662306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_ANALOG, 0x00,
61762306a36Sopenharmony_ci			POWER_WORK_MASK, POWER_WORK_DISABLE);
61862306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_ANALOG, 0x00,
61962306a36Sopenharmony_ci			BANDGAP_POWER_MASK, BANDGAP_POWER_DOWN);
62062306a36Sopenharmony_ci
62162306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_LVDS, 0x0b, LVDS_LANE_EN_MASK, 0);
62262306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_LVDS, 0x01,
62362306a36Sopenharmony_ci			LVDS_DIGITAL_INTERNAL_ENABLE_MASK,
62462306a36Sopenharmony_ci			LVDS_DIGITAL_INTERNAL_DISABLE);
62562306a36Sopenharmony_ci	phy_update_bits(inno, REGISTER_PART_LVDS, 0x0b,
62662306a36Sopenharmony_ci			LVDS_PLL_POWER_MASK | LVDS_BANDGAP_POWER_MASK,
62762306a36Sopenharmony_ci			LVDS_PLL_POWER_OFF | LVDS_BANDGAP_POWER_DOWN);
62862306a36Sopenharmony_ci
62962306a36Sopenharmony_ci	pm_runtime_put(inno->dev);
63062306a36Sopenharmony_ci	clk_disable_unprepare(inno->ref_clk);
63162306a36Sopenharmony_ci	clk_disable_unprepare(inno->pclk_phy);
63262306a36Sopenharmony_ci
63362306a36Sopenharmony_ci	return 0;
63462306a36Sopenharmony_ci}
63562306a36Sopenharmony_ci
63662306a36Sopenharmony_cistatic int inno_dsidphy_set_mode(struct phy *phy, enum phy_mode mode,
63762306a36Sopenharmony_ci				   int submode)
63862306a36Sopenharmony_ci{
63962306a36Sopenharmony_ci	struct inno_dsidphy *inno = phy_get_drvdata(phy);
64062306a36Sopenharmony_ci
64162306a36Sopenharmony_ci	switch (mode) {
64262306a36Sopenharmony_ci	case PHY_MODE_MIPI_DPHY:
64362306a36Sopenharmony_ci	case PHY_MODE_LVDS:
64462306a36Sopenharmony_ci		inno->mode = mode;
64562306a36Sopenharmony_ci		break;
64662306a36Sopenharmony_ci	default:
64762306a36Sopenharmony_ci		return -EINVAL;
64862306a36Sopenharmony_ci	}
64962306a36Sopenharmony_ci
65062306a36Sopenharmony_ci	return 0;
65162306a36Sopenharmony_ci}
65262306a36Sopenharmony_ci
65362306a36Sopenharmony_cistatic int inno_dsidphy_configure(struct phy *phy,
65462306a36Sopenharmony_ci				  union phy_configure_opts *opts)
65562306a36Sopenharmony_ci{
65662306a36Sopenharmony_ci	struct inno_dsidphy *inno = phy_get_drvdata(phy);
65762306a36Sopenharmony_ci	int ret;
65862306a36Sopenharmony_ci
65962306a36Sopenharmony_ci	if (inno->mode != PHY_MODE_MIPI_DPHY)
66062306a36Sopenharmony_ci		return -EINVAL;
66162306a36Sopenharmony_ci
66262306a36Sopenharmony_ci	ret = phy_mipi_dphy_config_validate(&opts->mipi_dphy);
66362306a36Sopenharmony_ci	if (ret)
66462306a36Sopenharmony_ci		return ret;
66562306a36Sopenharmony_ci
66662306a36Sopenharmony_ci	memcpy(&inno->dphy_cfg, &opts->mipi_dphy, sizeof(inno->dphy_cfg));
66762306a36Sopenharmony_ci
66862306a36Sopenharmony_ci	return 0;
66962306a36Sopenharmony_ci}
67062306a36Sopenharmony_ci
67162306a36Sopenharmony_cistatic const struct phy_ops inno_dsidphy_ops = {
67262306a36Sopenharmony_ci	.configure = inno_dsidphy_configure,
67362306a36Sopenharmony_ci	.set_mode = inno_dsidphy_set_mode,
67462306a36Sopenharmony_ci	.power_on = inno_dsidphy_power_on,
67562306a36Sopenharmony_ci	.power_off = inno_dsidphy_power_off,
67662306a36Sopenharmony_ci	.owner = THIS_MODULE,
67762306a36Sopenharmony_ci};
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_cistatic const struct inno_video_phy_plat_data max_1ghz_video_phy_plat_data = {
68062306a36Sopenharmony_ci	.inno_mipi_dphy_timing_table = inno_mipi_dphy_timing_table_max_1ghz,
68162306a36Sopenharmony_ci	.num_timings = ARRAY_SIZE(inno_mipi_dphy_timing_table_max_1ghz),
68262306a36Sopenharmony_ci	.max_rate = MAX_1GHZ,
68362306a36Sopenharmony_ci};
68462306a36Sopenharmony_ci
68562306a36Sopenharmony_cistatic const struct inno_video_phy_plat_data max_2_5ghz_video_phy_plat_data = {
68662306a36Sopenharmony_ci	.inno_mipi_dphy_timing_table = inno_mipi_dphy_timing_table_max_2_5ghz,
68762306a36Sopenharmony_ci	.num_timings = ARRAY_SIZE(inno_mipi_dphy_timing_table_max_2_5ghz),
68862306a36Sopenharmony_ci	.max_rate = MAX_2_5GHZ,
68962306a36Sopenharmony_ci};
69062306a36Sopenharmony_ci
69162306a36Sopenharmony_cistatic int inno_dsidphy_probe(struct platform_device *pdev)
69262306a36Sopenharmony_ci{
69362306a36Sopenharmony_ci	struct device *dev = &pdev->dev;
69462306a36Sopenharmony_ci	struct inno_dsidphy *inno;
69562306a36Sopenharmony_ci	struct phy_provider *phy_provider;
69662306a36Sopenharmony_ci	struct phy *phy;
69762306a36Sopenharmony_ci	int ret;
69862306a36Sopenharmony_ci
69962306a36Sopenharmony_ci	inno = devm_kzalloc(dev, sizeof(*inno), GFP_KERNEL);
70062306a36Sopenharmony_ci	if (!inno)
70162306a36Sopenharmony_ci		return -ENOMEM;
70262306a36Sopenharmony_ci
70362306a36Sopenharmony_ci	inno->dev = dev;
70462306a36Sopenharmony_ci	inno->pdata = of_device_get_match_data(inno->dev);
70562306a36Sopenharmony_ci	platform_set_drvdata(pdev, inno);
70662306a36Sopenharmony_ci
70762306a36Sopenharmony_ci	inno->phy_base = devm_platform_ioremap_resource(pdev, 0);
70862306a36Sopenharmony_ci	if (IS_ERR(inno->phy_base))
70962306a36Sopenharmony_ci		return PTR_ERR(inno->phy_base);
71062306a36Sopenharmony_ci
71162306a36Sopenharmony_ci	inno->ref_clk = devm_clk_get(dev, "ref");
71262306a36Sopenharmony_ci	if (IS_ERR(inno->ref_clk)) {
71362306a36Sopenharmony_ci		ret = PTR_ERR(inno->ref_clk);
71462306a36Sopenharmony_ci		dev_err(dev, "failed to get ref clock: %d\n", ret);
71562306a36Sopenharmony_ci		return ret;
71662306a36Sopenharmony_ci	}
71762306a36Sopenharmony_ci
71862306a36Sopenharmony_ci	inno->pclk_phy = devm_clk_get(dev, "pclk");
71962306a36Sopenharmony_ci	if (IS_ERR(inno->pclk_phy)) {
72062306a36Sopenharmony_ci		ret = PTR_ERR(inno->pclk_phy);
72162306a36Sopenharmony_ci		dev_err(dev, "failed to get phy pclk: %d\n", ret);
72262306a36Sopenharmony_ci		return ret;
72362306a36Sopenharmony_ci	}
72462306a36Sopenharmony_ci
72562306a36Sopenharmony_ci	inno->rst = devm_reset_control_get(dev, "apb");
72662306a36Sopenharmony_ci	if (IS_ERR(inno->rst)) {
72762306a36Sopenharmony_ci		ret = PTR_ERR(inno->rst);
72862306a36Sopenharmony_ci		dev_err(dev, "failed to get system reset control: %d\n", ret);
72962306a36Sopenharmony_ci		return ret;
73062306a36Sopenharmony_ci	}
73162306a36Sopenharmony_ci
73262306a36Sopenharmony_ci	phy = devm_phy_create(dev, NULL, &inno_dsidphy_ops);
73362306a36Sopenharmony_ci	if (IS_ERR(phy)) {
73462306a36Sopenharmony_ci		ret = PTR_ERR(phy);
73562306a36Sopenharmony_ci		dev_err(dev, "failed to create phy: %d\n", ret);
73662306a36Sopenharmony_ci		return ret;
73762306a36Sopenharmony_ci	}
73862306a36Sopenharmony_ci
73962306a36Sopenharmony_ci	phy_set_drvdata(phy, inno);
74062306a36Sopenharmony_ci
74162306a36Sopenharmony_ci	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
74262306a36Sopenharmony_ci	if (IS_ERR(phy_provider)) {
74362306a36Sopenharmony_ci		ret = PTR_ERR(phy_provider);
74462306a36Sopenharmony_ci		dev_err(dev, "failed to register phy provider: %d\n", ret);
74562306a36Sopenharmony_ci		return ret;
74662306a36Sopenharmony_ci	}
74762306a36Sopenharmony_ci
74862306a36Sopenharmony_ci	pm_runtime_enable(dev);
74962306a36Sopenharmony_ci
75062306a36Sopenharmony_ci	return 0;
75162306a36Sopenharmony_ci}
75262306a36Sopenharmony_ci
75362306a36Sopenharmony_cistatic void inno_dsidphy_remove(struct platform_device *pdev)
75462306a36Sopenharmony_ci{
75562306a36Sopenharmony_ci	struct inno_dsidphy *inno = platform_get_drvdata(pdev);
75662306a36Sopenharmony_ci
75762306a36Sopenharmony_ci	pm_runtime_disable(inno->dev);
75862306a36Sopenharmony_ci}
75962306a36Sopenharmony_ci
76062306a36Sopenharmony_cistatic const struct of_device_id inno_dsidphy_of_match[] = {
76162306a36Sopenharmony_ci	{
76262306a36Sopenharmony_ci		.compatible = "rockchip,px30-dsi-dphy",
76362306a36Sopenharmony_ci		.data = &max_1ghz_video_phy_plat_data,
76462306a36Sopenharmony_ci	}, {
76562306a36Sopenharmony_ci		.compatible = "rockchip,rk3128-dsi-dphy",
76662306a36Sopenharmony_ci		.data = &max_1ghz_video_phy_plat_data,
76762306a36Sopenharmony_ci	}, {
76862306a36Sopenharmony_ci		.compatible = "rockchip,rk3368-dsi-dphy",
76962306a36Sopenharmony_ci		.data = &max_1ghz_video_phy_plat_data,
77062306a36Sopenharmony_ci	}, {
77162306a36Sopenharmony_ci		.compatible = "rockchip,rk3568-dsi-dphy",
77262306a36Sopenharmony_ci		.data = &max_2_5ghz_video_phy_plat_data,
77362306a36Sopenharmony_ci	}, {
77462306a36Sopenharmony_ci		.compatible = "rockchip,rv1126-dsi-dphy",
77562306a36Sopenharmony_ci		.data = &max_2_5ghz_video_phy_plat_data,
77662306a36Sopenharmony_ci	},
77762306a36Sopenharmony_ci	{}
77862306a36Sopenharmony_ci};
77962306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, inno_dsidphy_of_match);
78062306a36Sopenharmony_ci
78162306a36Sopenharmony_cistatic struct platform_driver inno_dsidphy_driver = {
78262306a36Sopenharmony_ci	.driver = {
78362306a36Sopenharmony_ci		.name = "inno-dsidphy",
78462306a36Sopenharmony_ci		.of_match_table	= of_match_ptr(inno_dsidphy_of_match),
78562306a36Sopenharmony_ci	},
78662306a36Sopenharmony_ci	.probe = inno_dsidphy_probe,
78762306a36Sopenharmony_ci	.remove_new = inno_dsidphy_remove,
78862306a36Sopenharmony_ci};
78962306a36Sopenharmony_cimodule_platform_driver(inno_dsidphy_driver);
79062306a36Sopenharmony_ci
79162306a36Sopenharmony_ciMODULE_AUTHOR("Wyon Bi <bivvy.bi@rock-chips.com>");
79262306a36Sopenharmony_ciMODULE_DESCRIPTION("Innosilicon MIPI/LVDS/TTL Video Combo PHY driver");
79362306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
794