xref: /linux/drivers/phy/freescale/phy-fsl-imx8-mipi-dphy.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
1f4c8116eSGuido Günther // SPDX-License-Identifier: GPL-2.0+
2f4c8116eSGuido Günther /*
3f4c8116eSGuido Günther  * Copyright 2017,2018 NXP
4f4c8116eSGuido Günther  * Copyright 2019 Purism SPC
5f4c8116eSGuido Günther  */
6f4c8116eSGuido Günther 
73fbae284SLiu Ying #include <linux/bitfield.h>
8f4c8116eSGuido Günther #include <linux/clk.h>
9f4c8116eSGuido Günther #include <linux/clk-provider.h>
10f4c8116eSGuido Günther #include <linux/delay.h>
113fbae284SLiu Ying #include <linux/firmware/imx/ipc.h>
123fbae284SLiu Ying #include <linux/firmware/imx/svc/misc.h>
13f4c8116eSGuido Günther #include <linux/io.h>
14f4c8116eSGuido Günther #include <linux/kernel.h>
153fbae284SLiu Ying #include <linux/mfd/syscon.h>
16f4c8116eSGuido Günther #include <linux/module.h>
17f4c8116eSGuido Günther #include <linux/of.h>
18f4c8116eSGuido Günther #include <linux/of_platform.h>
19f4c8116eSGuido Günther #include <linux/phy/phy.h>
20f4c8116eSGuido Günther #include <linux/platform_device.h>
21f4c8116eSGuido Günther #include <linux/regmap.h>
223fbae284SLiu Ying #include <dt-bindings/firmware/imx/rsrc.h>
233fbae284SLiu Ying 
243fbae284SLiu Ying /* Control and Status Registers(CSR) */
253fbae284SLiu Ying #define PHY_CTRL			0x00
263fbae284SLiu Ying #define  CCM_MASK			GENMASK(7, 5)
273fbae284SLiu Ying #define  CCM(n)				FIELD_PREP(CCM_MASK, (n))
283fbae284SLiu Ying #define  CCM_1_2V			0x5
293fbae284SLiu Ying #define  CA_MASK			GENMASK(4, 2)
303fbae284SLiu Ying #define  CA_3_51MA			0x4
313fbae284SLiu Ying #define  CA(n)				FIELD_PREP(CA_MASK, (n))
323fbae284SLiu Ying #define  RFB				BIT(1)
333fbae284SLiu Ying #define  LVDS_EN			BIT(0)
34f4c8116eSGuido Günther 
35f4c8116eSGuido Günther /* DPHY registers */
36f4c8116eSGuido Günther #define DPHY_PD_DPHY			0x00
37f4c8116eSGuido Günther #define DPHY_M_PRG_HS_PREPARE		0x04
38f4c8116eSGuido Günther #define DPHY_MC_PRG_HS_PREPARE		0x08
39f4c8116eSGuido Günther #define DPHY_M_PRG_HS_ZERO		0x0c
40f4c8116eSGuido Günther #define DPHY_MC_PRG_HS_ZERO		0x10
41f4c8116eSGuido Günther #define DPHY_M_PRG_HS_TRAIL		0x14
42f4c8116eSGuido Günther #define DPHY_MC_PRG_HS_TRAIL		0x18
43f4c8116eSGuido Günther #define DPHY_PD_PLL			0x1c
44f4c8116eSGuido Günther #define DPHY_TST			0x20
45f4c8116eSGuido Günther #define DPHY_CN				0x24
46f4c8116eSGuido Günther #define DPHY_CM				0x28
47f4c8116eSGuido Günther #define DPHY_CO				0x2c
48f4c8116eSGuido Günther #define DPHY_LOCK			0x30
49f4c8116eSGuido Günther #define DPHY_LOCK_BYP			0x34
50f4c8116eSGuido Günther #define DPHY_REG_BYPASS_PLL		0x4C
51f4c8116eSGuido Günther 
52f4c8116eSGuido Günther #define MBPS(x) ((x) * 1000000)
53f4c8116eSGuido Günther 
54f4c8116eSGuido Günther #define DATA_RATE_MAX_SPEED MBPS(1500)
55f4c8116eSGuido Günther #define DATA_RATE_MIN_SPEED MBPS(80)
56f4c8116eSGuido Günther 
57f4c8116eSGuido Günther #define PLL_LOCK_SLEEP 10
58f4c8116eSGuido Günther #define PLL_LOCK_TIMEOUT 1000
59f4c8116eSGuido Günther 
60f4c8116eSGuido Günther #define CN_BUF	0xcb7a89c0
61f4c8116eSGuido Günther #define CO_BUF	0x63
62f4c8116eSGuido Günther #define CM(x)	(				  \
63f4c8116eSGuido Günther 		((x) <	32) ? 0xe0 | ((x) - 16) : \
64f4c8116eSGuido Günther 		((x) <	64) ? 0xc0 | ((x) - 32) : \
65f4c8116eSGuido Günther 		((x) < 128) ? 0x80 | ((x) - 64) : \
66f4c8116eSGuido Günther 		((x) - 128))
67f4c8116eSGuido Günther #define CN(x)	(((x) == 1) ? 0x1f : (((CN_BUF) >> ((x) - 1)) & 0x1f))
68f4c8116eSGuido Günther #define CO(x)	((CO_BUF) >> (8 - (x)) & 0x03)
69f4c8116eSGuido Günther 
70f4c8116eSGuido Günther /* PHY power on is active low */
71f4c8116eSGuido Günther #define PWR_ON	0
72f4c8116eSGuido Günther #define PWR_OFF	1
73f4c8116eSGuido Günther 
743fbae284SLiu Ying #define MIN_VCO_FREQ 640000000
753fbae284SLiu Ying #define MAX_VCO_FREQ 1500000000
763fbae284SLiu Ying 
773fbae284SLiu Ying #define MIN_LVDS_REFCLK_FREQ 24000000
783fbae284SLiu Ying #define MAX_LVDS_REFCLK_FREQ 150000000
793fbae284SLiu Ying 
80f4c8116eSGuido Günther enum mixel_dphy_devtype {
81f4c8116eSGuido Günther 	MIXEL_IMX8MQ,
823fbae284SLiu Ying 	MIXEL_IMX8QXP,
83f4c8116eSGuido Günther };
84f4c8116eSGuido Günther 
85f4c8116eSGuido Günther struct mixel_dphy_devdata {
86f4c8116eSGuido Günther 	u8 reg_tx_rcal;
87f4c8116eSGuido Günther 	u8 reg_auto_pd_en;
88f4c8116eSGuido Günther 	u8 reg_rxlprp;
89f4c8116eSGuido Günther 	u8 reg_rxcdrp;
90f4c8116eSGuido Günther 	u8 reg_rxhs_settle;
913fbae284SLiu Ying 	bool is_combo;	/* MIPI DPHY and LVDS PHY combo */
92f4c8116eSGuido Günther };
93f4c8116eSGuido Günther 
94f4c8116eSGuido Günther static const struct mixel_dphy_devdata mixel_dphy_devdata[] = {
95f4c8116eSGuido Günther 	[MIXEL_IMX8MQ] = {
96f4c8116eSGuido Günther 		.reg_tx_rcal = 0x38,
97f4c8116eSGuido Günther 		.reg_auto_pd_en = 0x3c,
98f4c8116eSGuido Günther 		.reg_rxlprp = 0x40,
99f4c8116eSGuido Günther 		.reg_rxcdrp = 0x44,
100f4c8116eSGuido Günther 		.reg_rxhs_settle = 0x48,
1013fbae284SLiu Ying 		.is_combo = false,
1023fbae284SLiu Ying 	},
1033fbae284SLiu Ying 	[MIXEL_IMX8QXP] = {
1043fbae284SLiu Ying 		.is_combo = true,
105f4c8116eSGuido Günther 	},
106f4c8116eSGuido Günther };
107f4c8116eSGuido Günther 
108f4c8116eSGuido Günther struct mixel_dphy_cfg {
109f4c8116eSGuido Günther 	/* DPHY PLL parameters */
110f4c8116eSGuido Günther 	u32 cm;
111f4c8116eSGuido Günther 	u32 cn;
112f4c8116eSGuido Günther 	u32 co;
113f4c8116eSGuido Günther 	/* DPHY register values */
114f4c8116eSGuido Günther 	u8 mc_prg_hs_prepare;
115f4c8116eSGuido Günther 	u8 m_prg_hs_prepare;
116f4c8116eSGuido Günther 	u8 mc_prg_hs_zero;
117f4c8116eSGuido Günther 	u8 m_prg_hs_zero;
118f4c8116eSGuido Günther 	u8 mc_prg_hs_trail;
119f4c8116eSGuido Günther 	u8 m_prg_hs_trail;
120f4c8116eSGuido Günther 	u8 rxhs_settle;
121f4c8116eSGuido Günther };
122f4c8116eSGuido Günther 
123f4c8116eSGuido Günther struct mixel_dphy_priv {
124f4c8116eSGuido Günther 	struct mixel_dphy_cfg cfg;
125f4c8116eSGuido Günther 	struct regmap *regmap;
1263fbae284SLiu Ying 	struct regmap *lvds_regmap;
127f4c8116eSGuido Günther 	struct clk *phy_ref_clk;
128f4c8116eSGuido Günther 	const struct mixel_dphy_devdata *devdata;
1293fbae284SLiu Ying 	struct imx_sc_ipc *ipc_handle;
1303fbae284SLiu Ying 	bool is_slave;
1313fbae284SLiu Ying 	int id;
132f4c8116eSGuido Günther };
133f4c8116eSGuido Günther 
134f4c8116eSGuido Günther static const struct regmap_config mixel_dphy_regmap_config = {
135f4c8116eSGuido Günther 	.reg_bits = 8,
136f4c8116eSGuido Günther 	.val_bits = 32,
137f4c8116eSGuido Günther 	.reg_stride = 4,
138f4c8116eSGuido Günther 	.max_register = DPHY_REG_BYPASS_PLL,
139f4c8116eSGuido Günther 	.name = "mipi-dphy",
140f4c8116eSGuido Günther };
141f4c8116eSGuido Günther 
phy_write(struct phy * phy,u32 value,unsigned int reg)142f4c8116eSGuido Günther static int phy_write(struct phy *phy, u32 value, unsigned int reg)
143f4c8116eSGuido Günther {
144f4c8116eSGuido Günther 	struct mixel_dphy_priv *priv = phy_get_drvdata(phy);
145f4c8116eSGuido Günther 	int ret;
146f4c8116eSGuido Günther 
147f4c8116eSGuido Günther 	ret = regmap_write(priv->regmap, reg, value);
148f4c8116eSGuido Günther 	if (ret < 0)
149f4c8116eSGuido Günther 		dev_err(&phy->dev, "Failed to write DPHY reg %d: %d\n", reg,
150f4c8116eSGuido Günther 			ret);
151f4c8116eSGuido Günther 	return ret;
152f4c8116eSGuido Günther }
153f4c8116eSGuido Günther 
154f4c8116eSGuido Günther /*
155f4c8116eSGuido Günther  * Find a ratio close to the desired one using continued fraction
156f4c8116eSGuido Günther  * approximation ending either at exact match or maximum allowed
157f4c8116eSGuido Günther  * nominator, denominator.
158f4c8116eSGuido Günther  */
get_best_ratio(u32 * pnum,u32 * pdenom,u32 max_n,u32 max_d)159f4c8116eSGuido Günther static void get_best_ratio(u32 *pnum, u32 *pdenom, u32 max_n, u32 max_d)
160f4c8116eSGuido Günther {
161f4c8116eSGuido Günther 	u32 a = *pnum;
162f4c8116eSGuido Günther 	u32 b = *pdenom;
163f4c8116eSGuido Günther 	u32 c;
164f4c8116eSGuido Günther 	u32 n[] = {0, 1};
165f4c8116eSGuido Günther 	u32 d[] = {1, 0};
166f4c8116eSGuido Günther 	u32 whole;
167f4c8116eSGuido Günther 	unsigned int i = 1;
168f4c8116eSGuido Günther 
169f4c8116eSGuido Günther 	while (b) {
170f4c8116eSGuido Günther 		i ^= 1;
171f4c8116eSGuido Günther 		whole = a / b;
172f4c8116eSGuido Günther 		n[i] += (n[i ^ 1] * whole);
173f4c8116eSGuido Günther 		d[i] += (d[i ^ 1] * whole);
174f4c8116eSGuido Günther 		if ((n[i] > max_n) || (d[i] > max_d)) {
175f4c8116eSGuido Günther 			i ^= 1;
176f4c8116eSGuido Günther 			break;
177f4c8116eSGuido Günther 		}
178f4c8116eSGuido Günther 		c = a - (b * whole);
179f4c8116eSGuido Günther 		a = b;
180f4c8116eSGuido Günther 		b = c;
181f4c8116eSGuido Günther 	}
182f4c8116eSGuido Günther 	*pnum = n[i];
183f4c8116eSGuido Günther 	*pdenom = d[i];
184f4c8116eSGuido Günther }
185f4c8116eSGuido Günther 
mixel_dphy_config_from_opts(struct phy * phy,struct phy_configure_opts_mipi_dphy * dphy_opts,struct mixel_dphy_cfg * cfg)186f4c8116eSGuido Günther static int mixel_dphy_config_from_opts(struct phy *phy,
187f4c8116eSGuido Günther 	       struct phy_configure_opts_mipi_dphy *dphy_opts,
188f4c8116eSGuido Günther 	       struct mixel_dphy_cfg *cfg)
189f4c8116eSGuido Günther {
190f4c8116eSGuido Günther 	struct mixel_dphy_priv *priv = dev_get_drvdata(phy->dev.parent);
191f4c8116eSGuido Günther 	unsigned long ref_clk = clk_get_rate(priv->phy_ref_clk);
192f4c8116eSGuido Günther 	u32 lp_t, numerator, denominator;
193f4c8116eSGuido Günther 	unsigned long long tmp;
194f4c8116eSGuido Günther 	u32 n;
195f4c8116eSGuido Günther 	int i;
196f4c8116eSGuido Günther 
197f4c8116eSGuido Günther 	if (dphy_opts->hs_clk_rate > DATA_RATE_MAX_SPEED ||
198f4c8116eSGuido Günther 	    dphy_opts->hs_clk_rate < DATA_RATE_MIN_SPEED)
199f4c8116eSGuido Günther 		return -EINVAL;
200f4c8116eSGuido Günther 
201f4c8116eSGuido Günther 	numerator = dphy_opts->hs_clk_rate;
202f4c8116eSGuido Günther 	denominator = ref_clk;
203f4c8116eSGuido Günther 	get_best_ratio(&numerator, &denominator, 255, 256);
204f4c8116eSGuido Günther 	if (!numerator || !denominator) {
205f4c8116eSGuido Günther 		dev_err(&phy->dev, "Invalid %d/%d for %ld/%ld\n",
206f4c8116eSGuido Günther 			numerator, denominator,
207f4c8116eSGuido Günther 			dphy_opts->hs_clk_rate, ref_clk);
208f4c8116eSGuido Günther 		return -EINVAL;
209f4c8116eSGuido Günther 	}
210f4c8116eSGuido Günther 
211f4c8116eSGuido Günther 	while ((numerator < 16) && (denominator <= 128)) {
212f4c8116eSGuido Günther 		numerator <<= 1;
213f4c8116eSGuido Günther 		denominator <<= 1;
214f4c8116eSGuido Günther 	}
215f4c8116eSGuido Günther 	/*
216f4c8116eSGuido Günther 	 * CM ranges between 16 and 255
217f4c8116eSGuido Günther 	 * CN ranges between 1 and 32
218f4c8116eSGuido Günther 	 * CO is power of 2: 1, 2, 4, 8
219f4c8116eSGuido Günther 	 */
220f4c8116eSGuido Günther 	i = __ffs(denominator);
221f4c8116eSGuido Günther 	if (i > 3)
222f4c8116eSGuido Günther 		i = 3;
223f4c8116eSGuido Günther 	cfg->cn = denominator >> i;
224f4c8116eSGuido Günther 	cfg->co = 1 << i;
225f4c8116eSGuido Günther 	cfg->cm = numerator;
226f4c8116eSGuido Günther 
227f4c8116eSGuido Günther 	if (cfg->cm < 16 || cfg->cm > 255 ||
228f4c8116eSGuido Günther 	    cfg->cn < 1 || cfg->cn > 32 ||
229f4c8116eSGuido Günther 	    cfg->co < 1 || cfg->co > 8) {
230f4c8116eSGuido Günther 		dev_err(&phy->dev, "Invalid CM/CN/CO values: %u/%u/%u\n",
231f4c8116eSGuido Günther 			cfg->cm, cfg->cn, cfg->co);
232f4c8116eSGuido Günther 		dev_err(&phy->dev, "for hs_clk/ref_clk=%ld/%ld ~ %d/%d\n",
233f4c8116eSGuido Günther 			dphy_opts->hs_clk_rate, ref_clk,
234f4c8116eSGuido Günther 			numerator, denominator);
235f4c8116eSGuido Günther 		return -EINVAL;
236f4c8116eSGuido Günther 	}
237f4c8116eSGuido Günther 
238f4c8116eSGuido Günther 	dev_dbg(&phy->dev, "hs_clk/ref_clk=%ld/%ld ~ %d/%d\n",
239f4c8116eSGuido Günther 		dphy_opts->hs_clk_rate, ref_clk, numerator, denominator);
240f4c8116eSGuido Günther 
241f4c8116eSGuido Günther 	/* LP clock period */
242f4c8116eSGuido Günther 	tmp = 1000000000000LL;
243f4c8116eSGuido Günther 	do_div(tmp, dphy_opts->lp_clk_rate); /* ps */
244f4c8116eSGuido Günther 	if (tmp > ULONG_MAX)
245f4c8116eSGuido Günther 		return -EINVAL;
246f4c8116eSGuido Günther 
247f4c8116eSGuido Günther 	lp_t = tmp;
248f4c8116eSGuido Günther 	dev_dbg(&phy->dev, "LP clock %lu, period: %u ps\n",
249f4c8116eSGuido Günther 		dphy_opts->lp_clk_rate, lp_t);
250f4c8116eSGuido Günther 
251f4c8116eSGuido Günther 	/* hs_prepare: in lp clock periods */
252f4c8116eSGuido Günther 	if (2 * dphy_opts->hs_prepare > 5 * lp_t) {
253f4c8116eSGuido Günther 		dev_err(&phy->dev,
254f4c8116eSGuido Günther 			"hs_prepare (%u) > 2.5 * lp clock period (%u)\n",
255f4c8116eSGuido Günther 			dphy_opts->hs_prepare, lp_t);
256f4c8116eSGuido Günther 		return -EINVAL;
257f4c8116eSGuido Günther 	}
258f4c8116eSGuido Günther 	/* 00: lp_t, 01: 1.5 * lp_t, 10: 2 * lp_t, 11: 2.5 * lp_t */
259f4c8116eSGuido Günther 	if (dphy_opts->hs_prepare < lp_t) {
260f4c8116eSGuido Günther 		n = 0;
261f4c8116eSGuido Günther 	} else {
262f4c8116eSGuido Günther 		tmp = 2 * (dphy_opts->hs_prepare - lp_t);
263f4c8116eSGuido Günther 		do_div(tmp, lp_t);
264f4c8116eSGuido Günther 		n = tmp;
265f4c8116eSGuido Günther 	}
266f4c8116eSGuido Günther 	cfg->m_prg_hs_prepare = n;
267f4c8116eSGuido Günther 
268f4c8116eSGuido Günther 	/* clk_prepare: in lp clock periods */
269f4c8116eSGuido Günther 	if (2 * dphy_opts->clk_prepare > 3 * lp_t) {
270f4c8116eSGuido Günther 		dev_err(&phy->dev,
271f4c8116eSGuido Günther 			"clk_prepare (%u) > 1.5 * lp clock period (%u)\n",
272f4c8116eSGuido Günther 			dphy_opts->clk_prepare, lp_t);
273f4c8116eSGuido Günther 		return -EINVAL;
274f4c8116eSGuido Günther 	}
275f4c8116eSGuido Günther 	/* 00: lp_t, 01: 1.5 * lp_t */
276f4c8116eSGuido Günther 	cfg->mc_prg_hs_prepare = dphy_opts->clk_prepare > lp_t ? 1 : 0;
277f4c8116eSGuido Günther 
278f4c8116eSGuido Günther 	/* hs_zero: formula from NXP BSP */
279f4c8116eSGuido Günther 	n = (144 * (dphy_opts->hs_clk_rate / 1000000) - 47500) / 10000;
280f4c8116eSGuido Günther 	cfg->m_prg_hs_zero = n < 1 ? 1 : n;
281f4c8116eSGuido Günther 
282f4c8116eSGuido Günther 	/* clk_zero: formula from NXP BSP */
283f4c8116eSGuido Günther 	n = (34 * (dphy_opts->hs_clk_rate / 1000000) - 2500) / 1000;
284f4c8116eSGuido Günther 	cfg->mc_prg_hs_zero = n < 1 ? 1 : n;
285f4c8116eSGuido Günther 
286f4c8116eSGuido Günther 	/* clk_trail, hs_trail: formula from NXP BSP */
287f4c8116eSGuido Günther 	n = (103 * (dphy_opts->hs_clk_rate / 1000000) + 10000) / 10000;
288f4c8116eSGuido Günther 	if (n > 15)
289f4c8116eSGuido Günther 		n = 15;
290f4c8116eSGuido Günther 	if (n < 1)
291f4c8116eSGuido Günther 		n = 1;
292f4c8116eSGuido Günther 	cfg->m_prg_hs_trail = n;
293f4c8116eSGuido Günther 	cfg->mc_prg_hs_trail = n;
294f4c8116eSGuido Günther 
295f4c8116eSGuido Günther 	/* rxhs_settle: formula from NXP BSP */
296f4c8116eSGuido Günther 	if (dphy_opts->hs_clk_rate < MBPS(80))
297f4c8116eSGuido Günther 		cfg->rxhs_settle = 0x0d;
298f4c8116eSGuido Günther 	else if (dphy_opts->hs_clk_rate < MBPS(90))
299f4c8116eSGuido Günther 		cfg->rxhs_settle = 0x0c;
300f4c8116eSGuido Günther 	else if (dphy_opts->hs_clk_rate < MBPS(125))
301f4c8116eSGuido Günther 		cfg->rxhs_settle = 0x0b;
302f4c8116eSGuido Günther 	else if (dphy_opts->hs_clk_rate < MBPS(150))
303f4c8116eSGuido Günther 		cfg->rxhs_settle = 0x0a;
304f4c8116eSGuido Günther 	else if (dphy_opts->hs_clk_rate < MBPS(225))
305f4c8116eSGuido Günther 		cfg->rxhs_settle = 0x09;
306f4c8116eSGuido Günther 	else if (dphy_opts->hs_clk_rate < MBPS(500))
307f4c8116eSGuido Günther 		cfg->rxhs_settle = 0x08;
308f4c8116eSGuido Günther 	else
309f4c8116eSGuido Günther 		cfg->rxhs_settle = 0x07;
310f4c8116eSGuido Günther 
311f4c8116eSGuido Günther 	dev_dbg(&phy->dev, "phy_config: %u %u %u %u %u %u %u\n",
312f4c8116eSGuido Günther 		cfg->m_prg_hs_prepare, cfg->mc_prg_hs_prepare,
313f4c8116eSGuido Günther 		cfg->m_prg_hs_zero, cfg->mc_prg_hs_zero,
314f4c8116eSGuido Günther 		cfg->m_prg_hs_trail, cfg->mc_prg_hs_trail,
315f4c8116eSGuido Günther 		cfg->rxhs_settle);
316f4c8116eSGuido Günther 
317f4c8116eSGuido Günther 	return 0;
318f4c8116eSGuido Günther }
319f4c8116eSGuido Günther 
mixel_phy_set_hs_timings(struct phy * phy)320f4c8116eSGuido Günther static void mixel_phy_set_hs_timings(struct phy *phy)
321f4c8116eSGuido Günther {
322f4c8116eSGuido Günther 	struct mixel_dphy_priv *priv = phy_get_drvdata(phy);
323f4c8116eSGuido Günther 
324f4c8116eSGuido Günther 	phy_write(phy, priv->cfg.m_prg_hs_prepare, DPHY_M_PRG_HS_PREPARE);
325f4c8116eSGuido Günther 	phy_write(phy, priv->cfg.mc_prg_hs_prepare, DPHY_MC_PRG_HS_PREPARE);
326f4c8116eSGuido Günther 	phy_write(phy, priv->cfg.m_prg_hs_zero, DPHY_M_PRG_HS_ZERO);
327f4c8116eSGuido Günther 	phy_write(phy, priv->cfg.mc_prg_hs_zero, DPHY_MC_PRG_HS_ZERO);
328f4c8116eSGuido Günther 	phy_write(phy, priv->cfg.m_prg_hs_trail, DPHY_M_PRG_HS_TRAIL);
329f4c8116eSGuido Günther 	phy_write(phy, priv->cfg.mc_prg_hs_trail, DPHY_MC_PRG_HS_TRAIL);
330f4c8116eSGuido Günther 	phy_write(phy, priv->cfg.rxhs_settle, priv->devdata->reg_rxhs_settle);
331f4c8116eSGuido Günther }
332f4c8116eSGuido Günther 
mixel_dphy_set_pll_params(struct phy * phy)333f4c8116eSGuido Günther static int mixel_dphy_set_pll_params(struct phy *phy)
334f4c8116eSGuido Günther {
335f4c8116eSGuido Günther 	struct mixel_dphy_priv *priv = dev_get_drvdata(phy->dev.parent);
336f4c8116eSGuido Günther 
337f4c8116eSGuido Günther 	if (priv->cfg.cm < 16 || priv->cfg.cm > 255 ||
338f4c8116eSGuido Günther 	    priv->cfg.cn < 1 || priv->cfg.cn > 32 ||
339f4c8116eSGuido Günther 	    priv->cfg.co < 1 || priv->cfg.co > 8) {
340f4c8116eSGuido Günther 		dev_err(&phy->dev, "Invalid CM/CN/CO values! (%u/%u/%u)\n",
341f4c8116eSGuido Günther 			priv->cfg.cm, priv->cfg.cn, priv->cfg.co);
342f4c8116eSGuido Günther 		return -EINVAL;
343f4c8116eSGuido Günther 	}
344f4c8116eSGuido Günther 	dev_dbg(&phy->dev, "Using CM:%u CN:%u CO:%u\n",
345f4c8116eSGuido Günther 		priv->cfg.cm, priv->cfg.cn, priv->cfg.co);
346f4c8116eSGuido Günther 	phy_write(phy, CM(priv->cfg.cm), DPHY_CM);
347f4c8116eSGuido Günther 	phy_write(phy, CN(priv->cfg.cn), DPHY_CN);
348f4c8116eSGuido Günther 	phy_write(phy, CO(priv->cfg.co), DPHY_CO);
349f4c8116eSGuido Günther 	return 0;
350f4c8116eSGuido Günther }
351f4c8116eSGuido Günther 
3523fbae284SLiu Ying static int
mixel_dphy_configure_mipi_dphy(struct phy * phy,union phy_configure_opts * opts)3533fbae284SLiu Ying mixel_dphy_configure_mipi_dphy(struct phy *phy, union phy_configure_opts *opts)
354f4c8116eSGuido Günther {
355f4c8116eSGuido Günther 	struct mixel_dphy_priv *priv = phy_get_drvdata(phy);
356f4c8116eSGuido Günther 	struct mixel_dphy_cfg cfg = { 0 };
357f4c8116eSGuido Günther 	int ret;
358f4c8116eSGuido Günther 
359f4c8116eSGuido Günther 	ret = mixel_dphy_config_from_opts(phy, &opts->mipi_dphy, &cfg);
360f4c8116eSGuido Günther 	if (ret)
361f4c8116eSGuido Günther 		return ret;
362f4c8116eSGuido Günther 
363f4c8116eSGuido Günther 	/* Update the configuration */
364f4c8116eSGuido Günther 	memcpy(&priv->cfg, &cfg, sizeof(struct mixel_dphy_cfg));
365f4c8116eSGuido Günther 
366f4c8116eSGuido Günther 	phy_write(phy, 0x00, DPHY_LOCK_BYP);
367f4c8116eSGuido Günther 	phy_write(phy, 0x01, priv->devdata->reg_tx_rcal);
368f4c8116eSGuido Günther 	phy_write(phy, 0x00, priv->devdata->reg_auto_pd_en);
369f4c8116eSGuido Günther 	phy_write(phy, 0x02, priv->devdata->reg_rxlprp);
370f4c8116eSGuido Günther 	phy_write(phy, 0x02, priv->devdata->reg_rxcdrp);
371f4c8116eSGuido Günther 	phy_write(phy, 0x25, DPHY_TST);
372f4c8116eSGuido Günther 
373f4c8116eSGuido Günther 	mixel_phy_set_hs_timings(phy);
374f4c8116eSGuido Günther 	ret = mixel_dphy_set_pll_params(phy);
375f4c8116eSGuido Günther 	if (ret < 0)
376f4c8116eSGuido Günther 		return ret;
377f4c8116eSGuido Günther 
378f4c8116eSGuido Günther 	return 0;
379f4c8116eSGuido Günther }
380f4c8116eSGuido Günther 
3813fbae284SLiu Ying static int
mixel_dphy_configure_lvds_phy(struct phy * phy,union phy_configure_opts * opts)3823fbae284SLiu Ying mixel_dphy_configure_lvds_phy(struct phy *phy, union phy_configure_opts *opts)
3833fbae284SLiu Ying {
3843fbae284SLiu Ying 	struct mixel_dphy_priv *priv = phy_get_drvdata(phy);
3853fbae284SLiu Ying 	struct phy_configure_opts_lvds *lvds_opts = &opts->lvds;
3863fbae284SLiu Ying 	unsigned long data_rate;
3873fbae284SLiu Ying 	unsigned long fvco;
3883fbae284SLiu Ying 	u32 rsc;
3893fbae284SLiu Ying 	u32 co;
3903fbae284SLiu Ying 	int ret;
3913fbae284SLiu Ying 
3923fbae284SLiu Ying 	priv->is_slave = lvds_opts->is_slave;
3933fbae284SLiu Ying 
3943fbae284SLiu Ying 	/* LVDS interface pins */
3953fbae284SLiu Ying 	regmap_write(priv->lvds_regmap, PHY_CTRL,
3963fbae284SLiu Ying 		     CCM(CCM_1_2V) | CA(CA_3_51MA) | RFB);
3973fbae284SLiu Ying 
3983fbae284SLiu Ying 	/* enable MODE8 only for slave LVDS PHY */
3993fbae284SLiu Ying 	rsc = priv->id ? IMX_SC_R_MIPI_1 : IMX_SC_R_MIPI_0;
4003fbae284SLiu Ying 	ret = imx_sc_misc_set_control(priv->ipc_handle, rsc, IMX_SC_C_DUAL_MODE,
4013fbae284SLiu Ying 				      lvds_opts->is_slave);
4023fbae284SLiu Ying 	if (ret) {
4033fbae284SLiu Ying 		dev_err(&phy->dev, "Failed to configure MODE8: %d\n", ret);
4043fbae284SLiu Ying 		return ret;
4053fbae284SLiu Ying 	}
4063fbae284SLiu Ying 
4073fbae284SLiu Ying 	/*
4083fbae284SLiu Ying 	 * Choose an appropriate divider ratio to meet the requirement of
4093fbae284SLiu Ying 	 * PLL VCO frequency range.
4103fbae284SLiu Ying 	 *
4113fbae284SLiu Ying 	 *  -----  640MHz ~ 1500MHz   ------------      ---------------
4123fbae284SLiu Ying 	 * | VCO | ----------------> | CO divider | -> | LVDS data rate|
4133fbae284SLiu Ying 	 *  -----       FVCO          ------------      ---------------
4143fbae284SLiu Ying 	 *                            1/2/4/8 div     7 * differential_clk_rate
4153fbae284SLiu Ying 	 */
4163fbae284SLiu Ying 	data_rate = 7 * lvds_opts->differential_clk_rate;
4173fbae284SLiu Ying 	for (co = 1; co <= 8; co *= 2) {
4183fbae284SLiu Ying 		fvco = data_rate * co;
4193fbae284SLiu Ying 
4203fbae284SLiu Ying 		if (fvco >= MIN_VCO_FREQ)
4213fbae284SLiu Ying 			break;
4223fbae284SLiu Ying 	}
4233fbae284SLiu Ying 
4243fbae284SLiu Ying 	if (fvco < MIN_VCO_FREQ || fvco > MAX_VCO_FREQ) {
4253fbae284SLiu Ying 		dev_err(&phy->dev, "VCO frequency %lu is out of range\n", fvco);
4263fbae284SLiu Ying 		return -ERANGE;
4273fbae284SLiu Ying 	}
4283fbae284SLiu Ying 
4293fbae284SLiu Ying 	/*
4303fbae284SLiu Ying 	 * CO is configurable, while CN and CM are not,
4313fbae284SLiu Ying 	 * as fixed ratios 1 and 7 are applied respectively.
4323fbae284SLiu Ying 	 */
4333fbae284SLiu Ying 	phy_write(phy, __ffs(co), DPHY_CO);
4343fbae284SLiu Ying 
4353fbae284SLiu Ying 	/* set reference clock rate */
4363fbae284SLiu Ying 	clk_set_rate(priv->phy_ref_clk, lvds_opts->differential_clk_rate);
4373fbae284SLiu Ying 
4383fbae284SLiu Ying 	return ret;
4393fbae284SLiu Ying }
4403fbae284SLiu Ying 
mixel_dphy_configure(struct phy * phy,union phy_configure_opts * opts)4413fbae284SLiu Ying static int mixel_dphy_configure(struct phy *phy, union phy_configure_opts *opts)
4423fbae284SLiu Ying {
4433fbae284SLiu Ying 	if (!opts) {
4443fbae284SLiu Ying 		dev_err(&phy->dev, "No configuration options\n");
4453fbae284SLiu Ying 		return -EINVAL;
4463fbae284SLiu Ying 	}
4473fbae284SLiu Ying 
4483fbae284SLiu Ying 	if (phy->attrs.mode == PHY_MODE_MIPI_DPHY)
4493fbae284SLiu Ying 		return mixel_dphy_configure_mipi_dphy(phy, opts);
4503fbae284SLiu Ying 	else if (phy->attrs.mode == PHY_MODE_LVDS)
4513fbae284SLiu Ying 		return mixel_dphy_configure_lvds_phy(phy, opts);
4523fbae284SLiu Ying 
4533fbae284SLiu Ying 	dev_err(&phy->dev,
4543fbae284SLiu Ying 		"Failed to configure PHY with invalid PHY mode: %d\n", phy->attrs.mode);
4553fbae284SLiu Ying 
4563fbae284SLiu Ying 	return -EINVAL;
4573fbae284SLiu Ying }
4583fbae284SLiu Ying 
4593fbae284SLiu Ying static int
mixel_dphy_validate_lvds_phy(struct phy * phy,union phy_configure_opts * opts)4603fbae284SLiu Ying mixel_dphy_validate_lvds_phy(struct phy *phy, union phy_configure_opts *opts)
4613fbae284SLiu Ying {
4623fbae284SLiu Ying 	struct phy_configure_opts_lvds *lvds_cfg = &opts->lvds;
4633fbae284SLiu Ying 
4643fbae284SLiu Ying 	if (lvds_cfg->bits_per_lane_and_dclk_cycle != 7) {
4653fbae284SLiu Ying 		dev_err(&phy->dev, "Invalid bits per LVDS data lane: %u\n",
4663fbae284SLiu Ying 			lvds_cfg->bits_per_lane_and_dclk_cycle);
4673fbae284SLiu Ying 		return -EINVAL;
4683fbae284SLiu Ying 	}
4693fbae284SLiu Ying 
4703fbae284SLiu Ying 	if (lvds_cfg->lanes != 4) {
4713fbae284SLiu Ying 		dev_err(&phy->dev, "Invalid LVDS data lanes: %u\n", lvds_cfg->lanes);
4723fbae284SLiu Ying 		return -EINVAL;
4733fbae284SLiu Ying 	}
4743fbae284SLiu Ying 
4753fbae284SLiu Ying 	if (lvds_cfg->differential_clk_rate < MIN_LVDS_REFCLK_FREQ ||
4763fbae284SLiu Ying 	    lvds_cfg->differential_clk_rate > MAX_LVDS_REFCLK_FREQ) {
4773fbae284SLiu Ying 		dev_err(&phy->dev,
4783fbae284SLiu Ying 			"Invalid LVDS differential clock rate: %lu\n",
4793fbae284SLiu Ying 			lvds_cfg->differential_clk_rate);
4803fbae284SLiu Ying 		return -EINVAL;
4813fbae284SLiu Ying 	}
4823fbae284SLiu Ying 
4833fbae284SLiu Ying 	return 0;
4843fbae284SLiu Ying }
4853fbae284SLiu Ying 
mixel_dphy_validate(struct phy * phy,enum phy_mode mode,int submode,union phy_configure_opts * opts)486f4c8116eSGuido Günther static int mixel_dphy_validate(struct phy *phy, enum phy_mode mode, int submode,
487f4c8116eSGuido Günther 			       union phy_configure_opts *opts)
488f4c8116eSGuido Günther {
4893fbae284SLiu Ying 	if (mode == PHY_MODE_MIPI_DPHY) {
4903fbae284SLiu Ying 		struct mixel_dphy_cfg mipi_dphy_cfg = { 0 };
491f4c8116eSGuido Günther 
4923fbae284SLiu Ying 		return mixel_dphy_config_from_opts(phy, &opts->mipi_dphy,
4933fbae284SLiu Ying 						   &mipi_dphy_cfg);
4943fbae284SLiu Ying 	} else if (mode == PHY_MODE_LVDS) {
4953fbae284SLiu Ying 		return mixel_dphy_validate_lvds_phy(phy, opts);
4963fbae284SLiu Ying 	}
4973fbae284SLiu Ying 
4983fbae284SLiu Ying 	dev_err(&phy->dev,
4993fbae284SLiu Ying 		"Failed to validate PHY with invalid PHY mode: %d\n", mode);
500f4c8116eSGuido Günther 	return -EINVAL;
501f4c8116eSGuido Günther }
502f4c8116eSGuido Günther 
mixel_dphy_init(struct phy * phy)503f4c8116eSGuido Günther static int mixel_dphy_init(struct phy *phy)
504f4c8116eSGuido Günther {
505f4c8116eSGuido Günther 	phy_write(phy, PWR_OFF, DPHY_PD_PLL);
506f4c8116eSGuido Günther 	phy_write(phy, PWR_OFF, DPHY_PD_DPHY);
507f4c8116eSGuido Günther 
508f4c8116eSGuido Günther 	return 0;
509f4c8116eSGuido Günther }
510f4c8116eSGuido Günther 
mixel_dphy_exit(struct phy * phy)511f4c8116eSGuido Günther static int mixel_dphy_exit(struct phy *phy)
512f4c8116eSGuido Günther {
513f4c8116eSGuido Günther 	phy_write(phy, 0, DPHY_CM);
514f4c8116eSGuido Günther 	phy_write(phy, 0, DPHY_CN);
515f4c8116eSGuido Günther 	phy_write(phy, 0, DPHY_CO);
516f4c8116eSGuido Günther 
517f4c8116eSGuido Günther 	return 0;
518f4c8116eSGuido Günther }
519f4c8116eSGuido Günther 
mixel_dphy_power_on_mipi_dphy(struct phy * phy)5203fbae284SLiu Ying static int mixel_dphy_power_on_mipi_dphy(struct phy *phy)
521f4c8116eSGuido Günther {
522f4c8116eSGuido Günther 	struct mixel_dphy_priv *priv = phy_get_drvdata(phy);
523f4c8116eSGuido Günther 	u32 locked;
524f4c8116eSGuido Günther 	int ret;
525f4c8116eSGuido Günther 
526f4c8116eSGuido Günther 	phy_write(phy, PWR_ON, DPHY_PD_PLL);
527f4c8116eSGuido Günther 	ret = regmap_read_poll_timeout(priv->regmap, DPHY_LOCK, locked,
528f4c8116eSGuido Günther 				       locked, PLL_LOCK_SLEEP,
529f4c8116eSGuido Günther 				       PLL_LOCK_TIMEOUT);
530f4c8116eSGuido Günther 	if (ret < 0) {
531f4c8116eSGuido Günther 		dev_err(&phy->dev, "Could not get DPHY lock (%d)!\n", ret);
5323fbae284SLiu Ying 		return ret;
533f4c8116eSGuido Günther 	}
534f4c8116eSGuido Günther 	phy_write(phy, PWR_ON, DPHY_PD_DPHY);
535f4c8116eSGuido Günther 
536f4c8116eSGuido Günther 	return 0;
5373fbae284SLiu Ying }
5383fbae284SLiu Ying 
mixel_dphy_power_on_lvds_phy(struct phy * phy)5393fbae284SLiu Ying static int mixel_dphy_power_on_lvds_phy(struct phy *phy)
5403fbae284SLiu Ying {
5413fbae284SLiu Ying 	struct mixel_dphy_priv *priv = phy_get_drvdata(phy);
5423fbae284SLiu Ying 	u32 locked;
5433fbae284SLiu Ying 	int ret;
5443fbae284SLiu Ying 
5453fbae284SLiu Ying 	regmap_update_bits(priv->lvds_regmap, PHY_CTRL, LVDS_EN, LVDS_EN);
5463fbae284SLiu Ying 
5473fbae284SLiu Ying 	phy_write(phy, PWR_ON, DPHY_PD_DPHY);
5483fbae284SLiu Ying 	phy_write(phy, PWR_ON, DPHY_PD_PLL);
5493fbae284SLiu Ying 
5503fbae284SLiu Ying 	/* do not wait for slave LVDS PHY being locked */
5513fbae284SLiu Ying 	if (priv->is_slave)
5523fbae284SLiu Ying 		return 0;
5533fbae284SLiu Ying 
5543fbae284SLiu Ying 	ret = regmap_read_poll_timeout(priv->regmap, DPHY_LOCK, locked,
5553fbae284SLiu Ying 				       locked, PLL_LOCK_SLEEP,
5563fbae284SLiu Ying 				       PLL_LOCK_TIMEOUT);
5573fbae284SLiu Ying 	if (ret < 0) {
5583fbae284SLiu Ying 		dev_err(&phy->dev, "Could not get LVDS PHY lock (%d)!\n", ret);
5593fbae284SLiu Ying 		return ret;
5603fbae284SLiu Ying 	}
5613fbae284SLiu Ying 
5623fbae284SLiu Ying 	return 0;
5633fbae284SLiu Ying }
5643fbae284SLiu Ying 
mixel_dphy_power_on(struct phy * phy)5653fbae284SLiu Ying static int mixel_dphy_power_on(struct phy *phy)
5663fbae284SLiu Ying {
5673fbae284SLiu Ying 	struct mixel_dphy_priv *priv = phy_get_drvdata(phy);
5683fbae284SLiu Ying 	int ret;
5693fbae284SLiu Ying 
5703fbae284SLiu Ying 	ret = clk_prepare_enable(priv->phy_ref_clk);
5713fbae284SLiu Ying 	if (ret < 0)
5723fbae284SLiu Ying 		return ret;
5733fbae284SLiu Ying 
5743fbae284SLiu Ying 	if (phy->attrs.mode == PHY_MODE_MIPI_DPHY) {
5753fbae284SLiu Ying 		ret = mixel_dphy_power_on_mipi_dphy(phy);
5763fbae284SLiu Ying 	} else if (phy->attrs.mode == PHY_MODE_LVDS) {
5773fbae284SLiu Ying 		ret = mixel_dphy_power_on_lvds_phy(phy);
5783fbae284SLiu Ying 	} else {
5793fbae284SLiu Ying 		dev_err(&phy->dev,
5803fbae284SLiu Ying 			"Failed to power on PHY with invalid PHY mode: %d\n",
5813fbae284SLiu Ying 							phy->attrs.mode);
5823fbae284SLiu Ying 		ret = -EINVAL;
5833fbae284SLiu Ying 	}
5843fbae284SLiu Ying 
5853fbae284SLiu Ying 	if (ret)
5863fbae284SLiu Ying 		goto clock_disable;
5873fbae284SLiu Ying 
5883fbae284SLiu Ying 	return 0;
589f4c8116eSGuido Günther clock_disable:
590f4c8116eSGuido Günther 	clk_disable_unprepare(priv->phy_ref_clk);
591f4c8116eSGuido Günther 	return ret;
592f4c8116eSGuido Günther }
593f4c8116eSGuido Günther 
mixel_dphy_power_off(struct phy * phy)594f4c8116eSGuido Günther static int mixel_dphy_power_off(struct phy *phy)
595f4c8116eSGuido Günther {
596f4c8116eSGuido Günther 	struct mixel_dphy_priv *priv = phy_get_drvdata(phy);
597f4c8116eSGuido Günther 
598f4c8116eSGuido Günther 	phy_write(phy, PWR_OFF, DPHY_PD_PLL);
599f4c8116eSGuido Günther 	phy_write(phy, PWR_OFF, DPHY_PD_DPHY);
600f4c8116eSGuido Günther 
6013fbae284SLiu Ying 	if (phy->attrs.mode == PHY_MODE_LVDS)
6023fbae284SLiu Ying 		regmap_update_bits(priv->lvds_regmap, PHY_CTRL, LVDS_EN, 0);
6033fbae284SLiu Ying 
604f4c8116eSGuido Günther 	clk_disable_unprepare(priv->phy_ref_clk);
605f4c8116eSGuido Günther 
606f4c8116eSGuido Günther 	return 0;
607f4c8116eSGuido Günther }
608f4c8116eSGuido Günther 
mixel_dphy_set_mode(struct phy * phy,enum phy_mode mode,int submode)6093fbae284SLiu Ying static int mixel_dphy_set_mode(struct phy *phy, enum phy_mode mode, int submode)
6103fbae284SLiu Ying {
6113fbae284SLiu Ying 	struct mixel_dphy_priv *priv = phy_get_drvdata(phy);
6123fbae284SLiu Ying 	int ret;
6133fbae284SLiu Ying 
6143fbae284SLiu Ying 	if (priv->devdata->is_combo && mode != PHY_MODE_LVDS) {
6153fbae284SLiu Ying 		dev_err(&phy->dev, "Failed to set PHY mode for combo PHY\n");
6163fbae284SLiu Ying 		return -EINVAL;
6173fbae284SLiu Ying 	}
6183fbae284SLiu Ying 
6193fbae284SLiu Ying 	if (!priv->devdata->is_combo && mode != PHY_MODE_MIPI_DPHY) {
6203fbae284SLiu Ying 		dev_err(&phy->dev, "Failed to set PHY mode to MIPI DPHY\n");
6213fbae284SLiu Ying 		return -EINVAL;
6223fbae284SLiu Ying 	}
6233fbae284SLiu Ying 
6243fbae284SLiu Ying 	if (priv->devdata->is_combo) {
6253fbae284SLiu Ying 		u32 rsc = priv->id ? IMX_SC_R_MIPI_1 : IMX_SC_R_MIPI_0;
6263fbae284SLiu Ying 
6273fbae284SLiu Ying 		ret = imx_sc_misc_set_control(priv->ipc_handle,
6283fbae284SLiu Ying 					      rsc, IMX_SC_C_MODE,
6293fbae284SLiu Ying 					      mode == PHY_MODE_LVDS);
6303fbae284SLiu Ying 		if (ret) {
6313fbae284SLiu Ying 			dev_err(&phy->dev,
6323fbae284SLiu Ying 				"Failed to set PHY mode via SCU ipc: %d\n", ret);
6333fbae284SLiu Ying 			return ret;
6343fbae284SLiu Ying 		}
6353fbae284SLiu Ying 	}
6363fbae284SLiu Ying 
6373fbae284SLiu Ying 	return 0;
6383fbae284SLiu Ying }
6393fbae284SLiu Ying 
640f4c8116eSGuido Günther static const struct phy_ops mixel_dphy_phy_ops = {
641f4c8116eSGuido Günther 	.init = mixel_dphy_init,
642f4c8116eSGuido Günther 	.exit = mixel_dphy_exit,
643f4c8116eSGuido Günther 	.power_on = mixel_dphy_power_on,
644f4c8116eSGuido Günther 	.power_off = mixel_dphy_power_off,
6453fbae284SLiu Ying 	.set_mode = mixel_dphy_set_mode,
646f4c8116eSGuido Günther 	.configure = mixel_dphy_configure,
647f4c8116eSGuido Günther 	.validate = mixel_dphy_validate,
648f4c8116eSGuido Günther 	.owner = THIS_MODULE,
649f4c8116eSGuido Günther };
650f4c8116eSGuido Günther 
651f4c8116eSGuido Günther static const struct of_device_id mixel_dphy_of_match[] = {
652f4c8116eSGuido Günther 	{ .compatible = "fsl,imx8mq-mipi-dphy",
653f4c8116eSGuido Günther 	  .data = &mixel_dphy_devdata[MIXEL_IMX8MQ] },
6543fbae284SLiu Ying 	{ .compatible = "fsl,imx8qxp-mipi-dphy",
6553fbae284SLiu Ying 	  .data = &mixel_dphy_devdata[MIXEL_IMX8QXP] },
656f4c8116eSGuido Günther 	{ /* sentinel */ },
657f4c8116eSGuido Günther };
658f4c8116eSGuido Günther MODULE_DEVICE_TABLE(of, mixel_dphy_of_match);
659f4c8116eSGuido Günther 
mixel_dphy_probe(struct platform_device * pdev)660f4c8116eSGuido Günther static int mixel_dphy_probe(struct platform_device *pdev)
661f4c8116eSGuido Günther {
662f4c8116eSGuido Günther 	struct device *dev = &pdev->dev;
663f4c8116eSGuido Günther 	struct device_node *np = dev->of_node;
664f4c8116eSGuido Günther 	struct phy_provider *phy_provider;
665f4c8116eSGuido Günther 	struct mixel_dphy_priv *priv;
666f4c8116eSGuido Günther 	struct phy *phy;
667f4c8116eSGuido Günther 	void __iomem *base;
6683fbae284SLiu Ying 	int ret;
669f4c8116eSGuido Günther 
670f4c8116eSGuido Günther 	if (!np)
671f4c8116eSGuido Günther 		return -ENODEV;
672f4c8116eSGuido Günther 
673f4c8116eSGuido Günther 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
674f4c8116eSGuido Günther 	if (!priv)
675f4c8116eSGuido Günther 		return -ENOMEM;
676f4c8116eSGuido Günther 
677f4c8116eSGuido Günther 	priv->devdata = of_device_get_match_data(&pdev->dev);
678f4c8116eSGuido Günther 	if (!priv->devdata)
679f4c8116eSGuido Günther 		return -EINVAL;
680f4c8116eSGuido Günther 
6810b7c4c88SChunfeng Yun 	base = devm_platform_ioremap_resource(pdev, 0);
682f4c8116eSGuido Günther 	if (IS_ERR(base))
683f4c8116eSGuido Günther 		return PTR_ERR(base);
684f4c8116eSGuido Günther 
685f4c8116eSGuido Günther 	priv->regmap = devm_regmap_init_mmio(&pdev->dev, base,
686f4c8116eSGuido Günther 					     &mixel_dphy_regmap_config);
687f4c8116eSGuido Günther 	if (IS_ERR(priv->regmap)) {
688f4c8116eSGuido Günther 		dev_err(dev, "Couldn't create the DPHY regmap\n");
689f4c8116eSGuido Günther 		return PTR_ERR(priv->regmap);
690f4c8116eSGuido Günther 	}
691f4c8116eSGuido Günther 
692f4c8116eSGuido Günther 	priv->phy_ref_clk = devm_clk_get(&pdev->dev, "phy_ref");
693f4c8116eSGuido Günther 	if (IS_ERR(priv->phy_ref_clk)) {
694f4c8116eSGuido Günther 		dev_err(dev, "No phy_ref clock found\n");
695f4c8116eSGuido Günther 		return PTR_ERR(priv->phy_ref_clk);
696f4c8116eSGuido Günther 	}
697f4c8116eSGuido Günther 	dev_dbg(dev, "phy_ref clock rate: %lu\n",
698f4c8116eSGuido Günther 		clk_get_rate(priv->phy_ref_clk));
699f4c8116eSGuido Günther 
7003fbae284SLiu Ying 	if (priv->devdata->is_combo) {
7013fbae284SLiu Ying 		priv->lvds_regmap =
7023fbae284SLiu Ying 			syscon_regmap_lookup_by_phandle(np, "fsl,syscon");
7033fbae284SLiu Ying 		if (IS_ERR(priv->lvds_regmap)) {
7043fbae284SLiu Ying 			ret = PTR_ERR(priv->lvds_regmap);
7053fbae284SLiu Ying 			dev_err_probe(dev, ret, "Failed to get LVDS regmap\n");
7063fbae284SLiu Ying 			return ret;
7073fbae284SLiu Ying 		}
7083fbae284SLiu Ying 
709*7936378cSAlexander Stein 		priv->id = of_alias_get_id(np, "mipi-dphy");
7103fbae284SLiu Ying 		if (priv->id < 0) {
7113fbae284SLiu Ying 			dev_err(dev, "Failed to get phy node alias id: %d\n",
7123fbae284SLiu Ying 				priv->id);
7133fbae284SLiu Ying 			return priv->id;
7143fbae284SLiu Ying 		}
7153fbae284SLiu Ying 
7163fbae284SLiu Ying 		ret = imx_scu_get_handle(&priv->ipc_handle);
7173fbae284SLiu Ying 		if (ret) {
7183fbae284SLiu Ying 			dev_err_probe(dev, ret,
7193fbae284SLiu Ying 				      "Failed to get SCU ipc handle\n");
7203fbae284SLiu Ying 			return ret;
7213fbae284SLiu Ying 		}
7223fbae284SLiu Ying 	}
7233fbae284SLiu Ying 
724f4c8116eSGuido Günther 	dev_set_drvdata(dev, priv);
725f4c8116eSGuido Günther 
726f4c8116eSGuido Günther 	phy = devm_phy_create(dev, np, &mixel_dphy_phy_ops);
727f4c8116eSGuido Günther 	if (IS_ERR(phy)) {
728f4c8116eSGuido Günther 		dev_err(dev, "Failed to create phy %ld\n", PTR_ERR(phy));
729f4c8116eSGuido Günther 		return PTR_ERR(phy);
730f4c8116eSGuido Günther 	}
731f4c8116eSGuido Günther 	phy_set_drvdata(phy, priv);
732f4c8116eSGuido Günther 
733f4c8116eSGuido Günther 	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
734f4c8116eSGuido Günther 
735f4c8116eSGuido Günther 	return PTR_ERR_OR_ZERO(phy_provider);
736f4c8116eSGuido Günther }
737f4c8116eSGuido Günther 
738f4c8116eSGuido Günther static struct platform_driver mixel_dphy_driver = {
739f4c8116eSGuido Günther 	.probe	= mixel_dphy_probe,
740f4c8116eSGuido Günther 	.driver = {
741f4c8116eSGuido Günther 		.name = "mixel-mipi-dphy",
742f4c8116eSGuido Günther 		.of_match_table	= mixel_dphy_of_match,
743f4c8116eSGuido Günther 	}
744f4c8116eSGuido Günther };
745f4c8116eSGuido Günther module_platform_driver(mixel_dphy_driver);
746f4c8116eSGuido Günther 
747f4c8116eSGuido Günther MODULE_AUTHOR("NXP Semiconductor");
748f4c8116eSGuido Günther MODULE_DESCRIPTION("Mixel MIPI-DSI PHY driver");
749f4c8116eSGuido Günther MODULE_LICENSE("GPL");
750