Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Copyright (c) 2018 Jernej Skrabec <jernej.skrabec@siol.net>
  4 */
  5
  6#include <linux/delay.h>
  7#include <linux/of.h>
  8#include <linux/of_platform.h>
  9#include <linux/platform_device.h>
 10
 11#include "sun8i_dw_hdmi.h"
 12
 13/*
 14 * Address can be actually any value. Here is set to same value as
 15 * it is set in BSP driver.
 16 */
 17#define I2C_ADDR	0x69
 18
 19static const struct dw_hdmi_mpll_config sun50i_h6_mpll_cfg[] = {
 20	{
 21		30666000, {
 22			{ 0x00b3, 0x0000 },
 23			{ 0x2153, 0x0000 },
 24			{ 0x40f3, 0x0000 },
 25		},
 26	},  {
 27		36800000, {
 28			{ 0x00b3, 0x0000 },
 29			{ 0x2153, 0x0000 },
 30			{ 0x40a2, 0x0001 },
 31		},
 32	},  {
 33		46000000, {
 34			{ 0x00b3, 0x0000 },
 35			{ 0x2142, 0x0001 },
 36			{ 0x40a2, 0x0001 },
 37		},
 38	},  {
 39		61333000, {
 40			{ 0x0072, 0x0001 },
 41			{ 0x2142, 0x0001 },
 42			{ 0x40a2, 0x0001 },
 43		},
 44	},  {
 45		73600000, {
 46			{ 0x0072, 0x0001 },
 47			{ 0x2142, 0x0001 },
 48			{ 0x4061, 0x0002 },
 49		},
 50	},  {
 51		92000000, {
 52			{ 0x0072, 0x0001 },
 53			{ 0x2145, 0x0002 },
 54			{ 0x4061, 0x0002 },
 55		},
 56	},  {
 57		122666000, {
 58			{ 0x0051, 0x0002 },
 59			{ 0x2145, 0x0002 },
 60			{ 0x4061, 0x0002 },
 61		},
 62	},  {
 63		147200000, {
 64			{ 0x0051, 0x0002 },
 65			{ 0x2145, 0x0002 },
 66			{ 0x4064, 0x0003 },
 67		},
 68	},  {
 69		184000000, {
 70			{ 0x0051, 0x0002 },
 71			{ 0x214c, 0x0003 },
 72			{ 0x4064, 0x0003 },
 73		},
 74	},  {
 75		226666000, {
 76			{ 0x0040, 0x0003 },
 77			{ 0x214c, 0x0003 },
 78			{ 0x4064, 0x0003 },
 79		},
 80	},  {
 81		272000000, {
 82			{ 0x0040, 0x0003 },
 83			{ 0x214c, 0x0003 },
 84			{ 0x5a64, 0x0003 },
 85		},
 86	},  {
 87		340000000, {
 88			{ 0x0040, 0x0003 },
 89			{ 0x3b4c, 0x0003 },
 90			{ 0x5a64, 0x0003 },
 91		},
 92	},  {
 93		594000000, {
 94			{ 0x1a40, 0x0003 },
 95			{ 0x3b4c, 0x0003 },
 96			{ 0x5a64, 0x0003 },
 97		},
 98	}, {
 99		~0UL, {
100			{ 0x0000, 0x0000 },
101			{ 0x0000, 0x0000 },
102			{ 0x0000, 0x0000 },
103		},
104	}
105};
106
107static const struct dw_hdmi_curr_ctrl sun50i_h6_cur_ctr[] = {
108	/* pixelclk    bpp8    bpp10   bpp12 */
 
109	{ 27000000,  { 0x0012, 0x0000, 0x0000 }, },
110	{ 74250000,  { 0x0013, 0x001a, 0x001b }, },
111	{ 148500000, { 0x0019, 0x0033, 0x0034 }, },
112	{ 297000000, { 0x0019, 0x001b, 0x001b }, },
113	{ 594000000, { 0x0010, 0x001b, 0x001b }, },
 
 
 
 
 
 
 
 
 
114	{ ~0UL,      { 0x0000, 0x0000, 0x0000 }, }
115};
116
117static const struct dw_hdmi_phy_config sun50i_h6_phy_config[] = {
118	/*pixelclk   symbol   term   vlev*/
119	{ 27000000,  0x8009, 0x0007, 0x02b0 },
120	{ 74250000,  0x8009, 0x0006, 0x022d },
121	{ 148500000, 0x8029, 0x0006, 0x0270 },
122	{ 297000000, 0x8039, 0x0005, 0x01ab },
123	{ 594000000, 0x8029, 0x0000, 0x008a },
124	{ ~0UL,	     0x0000, 0x0000, 0x0000}
125};
126
127static void sun8i_hdmi_phy_set_polarity(struct sun8i_hdmi_phy *phy,
128					const struct drm_display_mode *mode)
 
129{
130	u32 val = 0;
131
132	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
133		val |= SUN8I_HDMI_PHY_DBG_CTRL_POL_NHSYNC;
134
135	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
136		val |= SUN8I_HDMI_PHY_DBG_CTRL_POL_NVSYNC;
137
138	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_DBG_CTRL_REG,
139			   SUN8I_HDMI_PHY_DBG_CTRL_POL_MASK, val);
140};
141
142static int sun8i_a83t_hdmi_phy_config(struct dw_hdmi *hdmi, void *data,
143				      const struct drm_display_info *display,
144				      const struct drm_display_mode *mode)
145{
146	unsigned int clk_rate = mode->crtc_clock * 1000;
147	struct sun8i_hdmi_phy *phy = data;
148
149	sun8i_hdmi_phy_set_polarity(phy, mode);
150
151	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_REXT_CTRL_REG,
152			   SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN,
153			   SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN);
154
155	/* power down */
156	dw_hdmi_phy_gen2_txpwron(hdmi, 0);
157	dw_hdmi_phy_gen2_pddq(hdmi, 1);
158
159	dw_hdmi_phy_gen2_reset(hdmi);
160
161	dw_hdmi_phy_gen2_pddq(hdmi, 0);
162
163	dw_hdmi_phy_i2c_set_addr(hdmi, I2C_ADDR);
164
165	/*
166	 * Values are taken from BSP HDMI driver. Although AW didn't
167	 * release any documentation, explanation of this values can
168	 * be found in i.MX 6Dual/6Quad Reference Manual.
169	 */
170	if (clk_rate <= 27000000) {
171		dw_hdmi_phy_i2c_write(hdmi, 0x01e0, 0x06);
172		dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x15);
173		dw_hdmi_phy_i2c_write(hdmi, 0x08da, 0x10);
174		dw_hdmi_phy_i2c_write(hdmi, 0x0007, 0x19);
175		dw_hdmi_phy_i2c_write(hdmi, 0x0318, 0x0e);
176		dw_hdmi_phy_i2c_write(hdmi, 0x8009, 0x09);
177	} else if (clk_rate <= 74250000) {
178		dw_hdmi_phy_i2c_write(hdmi, 0x0540, 0x06);
179		dw_hdmi_phy_i2c_write(hdmi, 0x0005, 0x15);
180		dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x10);
181		dw_hdmi_phy_i2c_write(hdmi, 0x0007, 0x19);
182		dw_hdmi_phy_i2c_write(hdmi, 0x02b5, 0x0e);
183		dw_hdmi_phy_i2c_write(hdmi, 0x8009, 0x09);
184	} else if (clk_rate <= 148500000) {
185		dw_hdmi_phy_i2c_write(hdmi, 0x04a0, 0x06);
186		dw_hdmi_phy_i2c_write(hdmi, 0x000a, 0x15);
187		dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x10);
188		dw_hdmi_phy_i2c_write(hdmi, 0x0002, 0x19);
189		dw_hdmi_phy_i2c_write(hdmi, 0x0021, 0x0e);
190		dw_hdmi_phy_i2c_write(hdmi, 0x8029, 0x09);
191	} else {
192		dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x06);
193		dw_hdmi_phy_i2c_write(hdmi, 0x000f, 0x15);
194		dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x10);
195		dw_hdmi_phy_i2c_write(hdmi, 0x0002, 0x19);
196		dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x0e);
197		dw_hdmi_phy_i2c_write(hdmi, 0x802b, 0x09);
198	}
199
200	dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x1e);
201	dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x13);
202	dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x17);
203
204	dw_hdmi_phy_gen2_txpwron(hdmi, 1);
205
206	return 0;
207}
208
209static void sun8i_a83t_hdmi_phy_disable(struct dw_hdmi *hdmi, void *data)
210{
211	struct sun8i_hdmi_phy *phy = data;
212
213	dw_hdmi_phy_gen2_txpwron(hdmi, 0);
214	dw_hdmi_phy_gen2_pddq(hdmi, 1);
215
216	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_REXT_CTRL_REG,
217			   SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN, 0);
218}
219
220static const struct dw_hdmi_phy_ops sun8i_a83t_hdmi_phy_ops = {
221	.init		= sun8i_a83t_hdmi_phy_config,
222	.disable	= sun8i_a83t_hdmi_phy_disable,
223	.read_hpd	= dw_hdmi_phy_read_hpd,
224	.update_hpd	= dw_hdmi_phy_update_hpd,
225	.setup_hpd	= dw_hdmi_phy_setup_hpd,
226};
227
228static int sun8i_h3_hdmi_phy_config(struct dw_hdmi *hdmi, void *data,
229				    const struct drm_display_info *display,
230				    const struct drm_display_mode *mode)
231{
232	unsigned int clk_rate = mode->crtc_clock * 1000;
233	struct sun8i_hdmi_phy *phy = data;
234	u32 pll_cfg1_init;
235	u32 pll_cfg2_init;
236	u32 ana_cfg1_end;
237	u32 ana_cfg2_init;
238	u32 ana_cfg3_init;
239	u32 b_offset = 0;
240	u32 val;
241
242	if (phy->variant->has_phy_clk)
243		clk_set_rate(phy->clk_phy, clk_rate);
244
245	sun8i_hdmi_phy_set_polarity(phy, mode);
246
247	/* bandwidth / frequency independent settings */
248
249	pll_cfg1_init = SUN8I_HDMI_PHY_PLL_CFG1_LDO2_EN |
250			SUN8I_HDMI_PHY_PLL_CFG1_LDO1_EN |
251			SUN8I_HDMI_PHY_PLL_CFG1_LDO_VSET(7) |
252			SUN8I_HDMI_PHY_PLL_CFG1_UNKNOWN(1) |
253			SUN8I_HDMI_PHY_PLL_CFG1_PLLDBEN |
254			SUN8I_HDMI_PHY_PLL_CFG1_CS |
255			SUN8I_HDMI_PHY_PLL_CFG1_CP_S(2) |
256			SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(63) |
257			SUN8I_HDMI_PHY_PLL_CFG1_BWS;
258
259	pll_cfg2_init = SUN8I_HDMI_PHY_PLL_CFG2_SV_H |
260			SUN8I_HDMI_PHY_PLL_CFG2_VCOGAIN_EN |
261			SUN8I_HDMI_PHY_PLL_CFG2_SDIV2;
262
263	ana_cfg1_end = SUN8I_HDMI_PHY_ANA_CFG1_REG_SVBH(1) |
264		       SUN8I_HDMI_PHY_ANA_CFG1_AMP_OPT |
265		       SUN8I_HDMI_PHY_ANA_CFG1_EMP_OPT |
266		       SUN8I_HDMI_PHY_ANA_CFG1_AMPCK_OPT |
267		       SUN8I_HDMI_PHY_ANA_CFG1_EMPCK_OPT |
268		       SUN8I_HDMI_PHY_ANA_CFG1_ENRCAL |
269		       SUN8I_HDMI_PHY_ANA_CFG1_ENCALOG |
270		       SUN8I_HDMI_PHY_ANA_CFG1_REG_SCKTMDS |
271		       SUN8I_HDMI_PHY_ANA_CFG1_TMDSCLK_EN |
272		       SUN8I_HDMI_PHY_ANA_CFG1_TXEN_MASK |
273		       SUN8I_HDMI_PHY_ANA_CFG1_TXEN_ALL |
274		       SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDSCLK |
275		       SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS2 |
276		       SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS1 |
277		       SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS0 |
278		       SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS2 |
279		       SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS1 |
280		       SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS0 |
281		       SUN8I_HDMI_PHY_ANA_CFG1_CKEN |
282		       SUN8I_HDMI_PHY_ANA_CFG1_LDOEN |
283		       SUN8I_HDMI_PHY_ANA_CFG1_ENVBS |
284		       SUN8I_HDMI_PHY_ANA_CFG1_ENBI;
285
286	ana_cfg2_init = SUN8I_HDMI_PHY_ANA_CFG2_M_EN |
287			SUN8I_HDMI_PHY_ANA_CFG2_REG_DENCK |
288			SUN8I_HDMI_PHY_ANA_CFG2_REG_DEN |
289			SUN8I_HDMI_PHY_ANA_CFG2_REG_CKSS(1) |
290			SUN8I_HDMI_PHY_ANA_CFG2_REG_CSMPS(1);
291
292	ana_cfg3_init = SUN8I_HDMI_PHY_ANA_CFG3_REG_WIRE(0x3e0) |
293			SUN8I_HDMI_PHY_ANA_CFG3_SDAEN |
294			SUN8I_HDMI_PHY_ANA_CFG3_SCLEN;
295
296	/* bandwidth / frequency dependent settings */
297	if (clk_rate <= 27000000) {
298		pll_cfg1_init |= SUN8I_HDMI_PHY_PLL_CFG1_HV_IS_33 |
299				 SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(32);
300		pll_cfg2_init |= SUN8I_HDMI_PHY_PLL_CFG2_VCO_S(4) |
301				 SUN8I_HDMI_PHY_PLL_CFG2_S(4);
302		ana_cfg1_end |= SUN8I_HDMI_PHY_ANA_CFG1_REG_CALSW;
303		ana_cfg2_init |= SUN8I_HDMI_PHY_ANA_CFG2_REG_SLV(4) |
304				 SUN8I_HDMI_PHY_ANA_CFG2_REG_RESDI(phy->rcal);
305		ana_cfg3_init |= SUN8I_HDMI_PHY_ANA_CFG3_REG_AMPCK(3) |
306				 SUN8I_HDMI_PHY_ANA_CFG3_REG_AMP(5);
307	} else if (clk_rate <= 74250000) {
308		pll_cfg1_init |= SUN8I_HDMI_PHY_PLL_CFG1_HV_IS_33 |
309				 SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(32);
310		pll_cfg2_init |= SUN8I_HDMI_PHY_PLL_CFG2_VCO_S(4) |
311				 SUN8I_HDMI_PHY_PLL_CFG2_S(5);
312		ana_cfg1_end |= SUN8I_HDMI_PHY_ANA_CFG1_REG_CALSW;
313		ana_cfg2_init |= SUN8I_HDMI_PHY_ANA_CFG2_REG_SLV(4) |
314				 SUN8I_HDMI_PHY_ANA_CFG2_REG_RESDI(phy->rcal);
315		ana_cfg3_init |= SUN8I_HDMI_PHY_ANA_CFG3_REG_AMPCK(5) |
316				 SUN8I_HDMI_PHY_ANA_CFG3_REG_AMP(7);
317	} else if (clk_rate <= 148500000) {
318		pll_cfg1_init |= SUN8I_HDMI_PHY_PLL_CFG1_HV_IS_33 |
319				 SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(32);
320		pll_cfg2_init |= SUN8I_HDMI_PHY_PLL_CFG2_VCO_S(4) |
321				 SUN8I_HDMI_PHY_PLL_CFG2_S(6);
322		ana_cfg2_init |= SUN8I_HDMI_PHY_ANA_CFG2_REG_BIGSWCK |
323				 SUN8I_HDMI_PHY_ANA_CFG2_REG_BIGSW |
324				 SUN8I_HDMI_PHY_ANA_CFG2_REG_SLV(2);
325		ana_cfg3_init |= SUN8I_HDMI_PHY_ANA_CFG3_REG_AMPCK(7) |
326				 SUN8I_HDMI_PHY_ANA_CFG3_REG_AMP(9);
327	} else {
328		b_offset = 2;
329		pll_cfg1_init |= SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(63);
330		pll_cfg2_init |= SUN8I_HDMI_PHY_PLL_CFG2_VCO_S(6) |
331				 SUN8I_HDMI_PHY_PLL_CFG2_S(7);
332		ana_cfg2_init |= SUN8I_HDMI_PHY_ANA_CFG2_REG_BIGSWCK |
333				 SUN8I_HDMI_PHY_ANA_CFG2_REG_BIGSW |
334				 SUN8I_HDMI_PHY_ANA_CFG2_REG_SLV(4);
335		ana_cfg3_init |= SUN8I_HDMI_PHY_ANA_CFG3_REG_AMPCK(9) |
336				 SUN8I_HDMI_PHY_ANA_CFG3_REG_AMP(13) |
337				 SUN8I_HDMI_PHY_ANA_CFG3_REG_EMP(3);
338	}
339
340	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
341			   SUN8I_HDMI_PHY_ANA_CFG1_TXEN_MASK, 0);
342
343	/*
344	 * NOTE: We have to be careful not to overwrite PHY parent
345	 * clock selection bit and clock divider.
346	 */
347	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
348			   (u32)~SUN8I_HDMI_PHY_PLL_CFG1_CKIN_SEL_MSK,
349			   pll_cfg1_init);
350	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG2_REG,
351			   (u32)~SUN8I_HDMI_PHY_PLL_CFG2_PREDIV_MSK,
352			   pll_cfg2_init);
353	usleep_range(10000, 15000);
354	regmap_write(phy->regs, SUN8I_HDMI_PHY_PLL_CFG3_REG,
355		     SUN8I_HDMI_PHY_PLL_CFG3_SOUT_DIV2);
356	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
357			   SUN8I_HDMI_PHY_PLL_CFG1_PLLEN,
358			   SUN8I_HDMI_PHY_PLL_CFG1_PLLEN);
359	msleep(100);
360
361	/* get B value */
362	regmap_read(phy->regs, SUN8I_HDMI_PHY_ANA_STS_REG, &val);
363	val = (val & SUN8I_HDMI_PHY_ANA_STS_B_OUT_MSK) >>
364		SUN8I_HDMI_PHY_ANA_STS_B_OUT_SHIFT;
365	val = min(val + b_offset, (u32)0x3f);
366
367	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
368			   SUN8I_HDMI_PHY_PLL_CFG1_REG_OD1 |
369			   SUN8I_HDMI_PHY_PLL_CFG1_REG_OD,
370			   SUN8I_HDMI_PHY_PLL_CFG1_REG_OD1 |
371			   SUN8I_HDMI_PHY_PLL_CFG1_REG_OD);
372	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
373			   SUN8I_HDMI_PHY_PLL_CFG1_B_IN_MSK,
374			   val << SUN8I_HDMI_PHY_PLL_CFG1_B_IN_SHIFT);
375	msleep(100);
376	regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG, ana_cfg1_end);
377	regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG2_REG, ana_cfg2_init);
378	regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG3_REG, ana_cfg3_init);
379
380	return 0;
381}
382
383static void sun8i_h3_hdmi_phy_disable(struct dw_hdmi *hdmi, void *data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
384{
385	struct sun8i_hdmi_phy *phy = data;
 
386
 
 
 
 
 
 
 
387	regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
388		     SUN8I_HDMI_PHY_ANA_CFG1_LDOEN |
389		     SUN8I_HDMI_PHY_ANA_CFG1_ENVBS |
390		     SUN8I_HDMI_PHY_ANA_CFG1_ENBI);
391	regmap_write(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG, 0);
392}
393
394static const struct dw_hdmi_phy_ops sun8i_h3_hdmi_phy_ops = {
395	.init		= sun8i_h3_hdmi_phy_config,
396	.disable	= sun8i_h3_hdmi_phy_disable,
397	.read_hpd	= dw_hdmi_phy_read_hpd,
398	.update_hpd	= dw_hdmi_phy_update_hpd,
399	.setup_hpd	= dw_hdmi_phy_setup_hpd,
 
 
 
 
 
 
 
400};
401
402static void sun8i_hdmi_phy_unlock(struct sun8i_hdmi_phy *phy)
403{
404	/* enable read access to HDMI controller */
405	regmap_write(phy->regs, SUN8I_HDMI_PHY_READ_EN_REG,
406		     SUN8I_HDMI_PHY_READ_EN_MAGIC);
407
408	/* unscramble register offsets */
409	regmap_write(phy->regs, SUN8I_HDMI_PHY_UNSCRAMBLE_REG,
410		     SUN8I_HDMI_PHY_UNSCRAMBLE_MAGIC);
411}
412
413static void sun50i_hdmi_phy_init_h6(struct sun8i_hdmi_phy *phy)
414{
415	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_REXT_CTRL_REG,
416			   SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN,
417			   SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN);
418
419	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_REXT_CTRL_REG,
420			   0xffff0000, 0x80c00000);
421}
422
423static void sun8i_hdmi_phy_init_a83t(struct sun8i_hdmi_phy *phy)
424{
425	sun8i_hdmi_phy_unlock(phy);
426
427	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_DBG_CTRL_REG,
428			   SUN8I_HDMI_PHY_DBG_CTRL_PX_LOCK,
429			   SUN8I_HDMI_PHY_DBG_CTRL_PX_LOCK);
430
431	/*
432	 * Set PHY I2C address. It must match to the address set by
433	 * dw_hdmi_phy_set_slave_addr().
434	 */
435	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_DBG_CTRL_REG,
436			   SUN8I_HDMI_PHY_DBG_CTRL_ADDR_MASK,
437			   SUN8I_HDMI_PHY_DBG_CTRL_ADDR(I2C_ADDR));
438}
439
440static void sun8i_hdmi_phy_init_h3(struct sun8i_hdmi_phy *phy)
441{
442	unsigned int val;
443
444	sun8i_hdmi_phy_unlock(phy);
445
446	regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG, 0);
447	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
448			   SUN8I_HDMI_PHY_ANA_CFG1_ENBI,
449			   SUN8I_HDMI_PHY_ANA_CFG1_ENBI);
450	udelay(5);
451	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
452			   SUN8I_HDMI_PHY_ANA_CFG1_TMDSCLK_EN,
453			   SUN8I_HDMI_PHY_ANA_CFG1_TMDSCLK_EN);
454	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
455			   SUN8I_HDMI_PHY_ANA_CFG1_ENVBS,
456			   SUN8I_HDMI_PHY_ANA_CFG1_ENVBS);
457	usleep_range(10, 20);
458	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
459			   SUN8I_HDMI_PHY_ANA_CFG1_LDOEN,
460			   SUN8I_HDMI_PHY_ANA_CFG1_LDOEN);
461	udelay(5);
462	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
463			   SUN8I_HDMI_PHY_ANA_CFG1_CKEN,
464			   SUN8I_HDMI_PHY_ANA_CFG1_CKEN);
465	usleep_range(40, 100);
466	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
467			   SUN8I_HDMI_PHY_ANA_CFG1_ENRCAL,
468			   SUN8I_HDMI_PHY_ANA_CFG1_ENRCAL);
469	usleep_range(100, 200);
470	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
471			   SUN8I_HDMI_PHY_ANA_CFG1_ENCALOG,
472			   SUN8I_HDMI_PHY_ANA_CFG1_ENCALOG);
473	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
474			   SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS0 |
475			   SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS1 |
476			   SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS2,
477			   SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS0 |
478			   SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS1 |
479			   SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS2);
480
481	/* wait for calibration to finish */
482	regmap_read_poll_timeout(phy->regs, SUN8I_HDMI_PHY_ANA_STS_REG, val,
483				 (val & SUN8I_HDMI_PHY_ANA_STS_RCALEND2D),
484				 100, 2000);
485
486	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
487			   SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDSCLK,
488			   SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDSCLK);
489	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
490			   SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS0 |
491			   SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS1 |
492			   SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS2 |
493			   SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDSCLK,
494			   SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS0 |
495			   SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS1 |
496			   SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS2 |
497			   SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDSCLK);
498
499	/* enable DDC communication */
500	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG3_REG,
501			   SUN8I_HDMI_PHY_ANA_CFG3_SCLEN |
502			   SUN8I_HDMI_PHY_ANA_CFG3_SDAEN,
503			   SUN8I_HDMI_PHY_ANA_CFG3_SCLEN |
504			   SUN8I_HDMI_PHY_ANA_CFG3_SDAEN);
505
506	/* reset PHY PLL clock parent */
507	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
508			   SUN8I_HDMI_PHY_PLL_CFG1_CKIN_SEL_MSK, 0);
509
510	/* set HW control of CEC pins */
511	regmap_write(phy->regs, SUN8I_HDMI_PHY_CEC_REG, 0);
512
513	/* read calibration data */
514	regmap_read(phy->regs, SUN8I_HDMI_PHY_ANA_STS_REG, &val);
515	phy->rcal = (val & SUN8I_HDMI_PHY_ANA_STS_RCAL_MASK) >> 2;
516}
517
518int sun8i_hdmi_phy_init(struct sun8i_hdmi_phy *phy)
519{
520	int ret;
521
522	ret = reset_control_deassert(phy->rst_phy);
523	if (ret) {
524		dev_err(phy->dev, "Cannot deassert phy reset control: %d\n", ret);
525		return ret;
526	}
527
528	ret = clk_prepare_enable(phy->clk_bus);
529	if (ret) {
530		dev_err(phy->dev, "Cannot enable bus clock: %d\n", ret);
531		goto err_assert_rst_phy;
532	}
533
534	ret = clk_prepare_enable(phy->clk_mod);
535	if (ret) {
536		dev_err(phy->dev, "Cannot enable mod clock: %d\n", ret);
537		goto err_disable_clk_bus;
538	}
539
540	if (phy->variant->has_phy_clk) {
541		ret = sun8i_phy_clk_create(phy, phy->dev,
542					   phy->variant->has_second_pll);
543		if (ret) {
544			dev_err(phy->dev, "Couldn't create the PHY clock\n");
545			goto err_disable_clk_mod;
546		}
547
548		clk_prepare_enable(phy->clk_phy);
549	}
550
551	phy->variant->phy_init(phy);
552
553	return 0;
554
555err_disable_clk_mod:
556	clk_disable_unprepare(phy->clk_mod);
557err_disable_clk_bus:
558	clk_disable_unprepare(phy->clk_bus);
559err_assert_rst_phy:
560	reset_control_assert(phy->rst_phy);
561
562	return ret;
563}
564
565void sun8i_hdmi_phy_deinit(struct sun8i_hdmi_phy *phy)
566{
567	clk_disable_unprepare(phy->clk_mod);
568	clk_disable_unprepare(phy->clk_bus);
569	clk_disable_unprepare(phy->clk_phy);
570
571	reset_control_assert(phy->rst_phy);
572}
573
574void sun8i_hdmi_phy_set_ops(struct sun8i_hdmi_phy *phy,
575			    struct dw_hdmi_plat_data *plat_data)
576{
577	const struct sun8i_hdmi_phy_variant *variant = phy->variant;
578
579	if (variant->phy_ops) {
580		plat_data->phy_ops = variant->phy_ops;
581		plat_data->phy_name = "sun8i_dw_hdmi_phy";
582		plat_data->phy_data = phy;
583	} else {
584		plat_data->mpll_cfg = variant->mpll_cfg;
585		plat_data->cur_ctr = variant->cur_ctr;
586		plat_data->phy_config = variant->phy_cfg;
587	}
588}
589
590static const struct regmap_config sun8i_hdmi_phy_regmap_config = {
591	.reg_bits	= 32,
592	.val_bits	= 32,
593	.reg_stride	= 4,
594	.max_register	= SUN8I_HDMI_PHY_CEC_REG,
595	.name		= "phy"
596};
597
598static const struct sun8i_hdmi_phy_variant sun8i_a83t_hdmi_phy = {
599	.phy_ops = &sun8i_a83t_hdmi_phy_ops,
600	.phy_init = &sun8i_hdmi_phy_init_a83t,
 
 
601};
602
603static const struct sun8i_hdmi_phy_variant sun8i_h3_hdmi_phy = {
604	.has_phy_clk = true,
605	.phy_ops = &sun8i_h3_hdmi_phy_ops,
606	.phy_init = &sun8i_hdmi_phy_init_h3,
 
 
607};
608
609static const struct sun8i_hdmi_phy_variant sun8i_r40_hdmi_phy = {
610	.has_phy_clk = true,
611	.has_second_pll = true,
612	.phy_ops = &sun8i_h3_hdmi_phy_ops,
613	.phy_init = &sun8i_hdmi_phy_init_h3,
 
 
614};
615
616static const struct sun8i_hdmi_phy_variant sun50i_a64_hdmi_phy = {
617	.has_phy_clk = true,
618	.phy_ops = &sun8i_h3_hdmi_phy_ops,
619	.phy_init = &sun8i_hdmi_phy_init_h3,
 
 
620};
621
622static const struct sun8i_hdmi_phy_variant sun50i_h6_hdmi_phy = {
623	.cur_ctr  = sun50i_h6_cur_ctr,
624	.mpll_cfg = sun50i_h6_mpll_cfg,
625	.phy_cfg  = sun50i_h6_phy_config,
626	.phy_init = &sun50i_hdmi_phy_init_h6,
627};
628
629static const struct of_device_id sun8i_hdmi_phy_of_table[] = {
630	{
631		.compatible = "allwinner,sun8i-a83t-hdmi-phy",
632		.data = &sun8i_a83t_hdmi_phy,
633	},
634	{
635		.compatible = "allwinner,sun8i-h3-hdmi-phy",
636		.data = &sun8i_h3_hdmi_phy,
637	},
638	{
639		.compatible = "allwinner,sun8i-r40-hdmi-phy",
640		.data = &sun8i_r40_hdmi_phy,
641	},
642	{
643		.compatible = "allwinner,sun50i-a64-hdmi-phy",
644		.data = &sun50i_a64_hdmi_phy,
645	},
646	{
647		.compatible = "allwinner,sun50i-h6-hdmi-phy",
648		.data = &sun50i_h6_hdmi_phy,
649	},
650	{ /* sentinel */ }
651};
652
653int sun8i_hdmi_phy_get(struct sun8i_dw_hdmi *hdmi, struct device_node *node)
654{
655	struct platform_device *pdev = of_find_device_by_node(node);
 
656	struct sun8i_hdmi_phy *phy;
 
 
 
657
658	if (!pdev)
659		return -EPROBE_DEFER;
660
661	phy = platform_get_drvdata(pdev);
662	if (!phy) {
663		put_device(&pdev->dev);
664		return -EPROBE_DEFER;
665	}
666
667	hdmi->phy = phy;
668
669	put_device(&pdev->dev);
670
671	return 0;
672}
673
674static int sun8i_hdmi_phy_probe(struct platform_device *pdev)
675{
676	struct device *dev = &pdev->dev;
677	struct sun8i_hdmi_phy *phy;
678	void __iomem *regs;
679
680	phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
681	if (!phy)
682		return -ENOMEM;
683
684	phy->variant = of_device_get_match_data(dev);
685	phy->dev = dev;
686
687	regs = devm_platform_ioremap_resource(pdev, 0);
688	if (IS_ERR(regs))
689		return dev_err_probe(dev, PTR_ERR(regs),
690				     "Couldn't map the HDMI PHY registers\n");
 
 
 
 
 
 
 
691
692	phy->regs = devm_regmap_init_mmio(dev, regs,
693					  &sun8i_hdmi_phy_regmap_config);
694	if (IS_ERR(phy->regs))
695		return dev_err_probe(dev, PTR_ERR(phy->regs),
696				     "Couldn't create the HDMI PHY regmap\n");
697
698	phy->clk_bus = devm_clk_get(dev, "bus");
699	if (IS_ERR(phy->clk_bus))
700		return dev_err_probe(dev, PTR_ERR(phy->clk_bus),
701				     "Could not get bus clock\n");
702
703	phy->clk_mod = devm_clk_get(dev, "mod");
704	if (IS_ERR(phy->clk_mod))
705		return dev_err_probe(dev, PTR_ERR(phy->clk_mod),
706				     "Could not get mod clock\n");
 
 
 
 
707
708	if (phy->variant->has_phy_clk) {
709		phy->clk_pll0 = devm_clk_get(dev, "pll-0");
710		if (IS_ERR(phy->clk_pll0))
711			return dev_err_probe(dev, PTR_ERR(phy->clk_pll0),
712					     "Could not get pll-0 clock\n");
 
 
713
714		if (phy->variant->has_second_pll) {
715			phy->clk_pll1 = devm_clk_get(dev, "pll-1");
716			if (IS_ERR(phy->clk_pll1))
717				return dev_err_probe(dev, PTR_ERR(phy->clk_pll1),
718						     "Could not get pll-1 clock\n");
 
 
719		}
720	}
721
722	phy->rst_phy = devm_reset_control_get_shared(dev, "phy");
723	if (IS_ERR(phy->rst_phy))
724		return dev_err_probe(dev, PTR_ERR(phy->rst_phy),
725				     "Could not get phy reset control\n");
 
 
726
727	platform_set_drvdata(pdev, phy);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
728
729	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
730}
731
732struct platform_driver sun8i_hdmi_phy_driver = {
733	.probe  = sun8i_hdmi_phy_probe,
734	.driver = {
735		.name = "sun8i-hdmi-phy",
736		.of_match_table = sun8i_hdmi_phy_of_table,
737	},
738};
 
 
 
 
 
 
 
 
 
 
v5.4
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Copyright (c) 2018 Jernej Skrabec <jernej.skrabec@siol.net>
  4 */
  5
  6#include <linux/delay.h>
  7#include <linux/of_address.h>
 
 
  8
  9#include "sun8i_dw_hdmi.h"
 10
 11/*
 12 * Address can be actually any value. Here is set to same value as
 13 * it is set in BSP driver.
 14 */
 15#define I2C_ADDR	0x69
 16
 17static const struct dw_hdmi_mpll_config sun50i_h6_mpll_cfg[] = {
 18	{
 19		30666000, {
 20			{ 0x00b3, 0x0000 },
 21			{ 0x2153, 0x0000 },
 22			{ 0x40f3, 0x0000 },
 23		},
 24	},  {
 25		36800000, {
 26			{ 0x00b3, 0x0000 },
 27			{ 0x2153, 0x0000 },
 28			{ 0x40a2, 0x0001 },
 29		},
 30	},  {
 31		46000000, {
 32			{ 0x00b3, 0x0000 },
 33			{ 0x2142, 0x0001 },
 34			{ 0x40a2, 0x0001 },
 35		},
 36	},  {
 37		61333000, {
 38			{ 0x0072, 0x0001 },
 39			{ 0x2142, 0x0001 },
 40			{ 0x40a2, 0x0001 },
 41		},
 42	},  {
 43		73600000, {
 44			{ 0x0072, 0x0001 },
 45			{ 0x2142, 0x0001 },
 46			{ 0x4061, 0x0002 },
 47		},
 48	},  {
 49		92000000, {
 50			{ 0x0072, 0x0001 },
 51			{ 0x2145, 0x0002 },
 52			{ 0x4061, 0x0002 },
 53		},
 54	},  {
 55		122666000, {
 56			{ 0x0051, 0x0002 },
 57			{ 0x2145, 0x0002 },
 58			{ 0x4061, 0x0002 },
 59		},
 60	},  {
 61		147200000, {
 62			{ 0x0051, 0x0002 },
 63			{ 0x2145, 0x0002 },
 64			{ 0x4064, 0x0003 },
 65		},
 66	},  {
 67		184000000, {
 68			{ 0x0051, 0x0002 },
 69			{ 0x214c, 0x0003 },
 70			{ 0x4064, 0x0003 },
 71		},
 72	},  {
 73		226666000, {
 74			{ 0x0040, 0x0003 },
 75			{ 0x214c, 0x0003 },
 76			{ 0x4064, 0x0003 },
 77		},
 78	},  {
 79		272000000, {
 80			{ 0x0040, 0x0003 },
 81			{ 0x214c, 0x0003 },
 82			{ 0x5a64, 0x0003 },
 83		},
 84	},  {
 85		340000000, {
 86			{ 0x0040, 0x0003 },
 87			{ 0x3b4c, 0x0003 },
 88			{ 0x5a64, 0x0003 },
 89		},
 90	},  {
 91		594000000, {
 92			{ 0x1a40, 0x0003 },
 93			{ 0x3b4c, 0x0003 },
 94			{ 0x5a64, 0x0003 },
 95		},
 96	}, {
 97		~0UL, {
 98			{ 0x0000, 0x0000 },
 99			{ 0x0000, 0x0000 },
100			{ 0x0000, 0x0000 },
101		},
102	}
103};
104
105static const struct dw_hdmi_curr_ctrl sun50i_h6_cur_ctr[] = {
106	/* pixelclk    bpp8    bpp10   bpp12 */
107	{ 25175000,  { 0x0000, 0x0000, 0x0000 }, },
108	{ 27000000,  { 0x0012, 0x0000, 0x0000 }, },
109	{ 59400000,  { 0x0008, 0x0008, 0x0008 }, },
110	{ 72000000,  { 0x0008, 0x0008, 0x001b }, },
111	{ 74250000,  { 0x0013, 0x0013, 0x0013 }, },
112	{ 90000000,  { 0x0008, 0x001a, 0x001b }, },
113	{ 118800000, { 0x001b, 0x001a, 0x001b }, },
114	{ 144000000, { 0x001b, 0x001a, 0x0034 }, },
115	{ 180000000, { 0x001b, 0x0033, 0x0034 }, },
116	{ 216000000, { 0x0036, 0x0033, 0x0034 }, },
117	{ 237600000, { 0x0036, 0x0033, 0x001b }, },
118	{ 288000000, { 0x0036, 0x001b, 0x001b }, },
119	{ 297000000, { 0x0019, 0x001b, 0x0019 }, },
120	{ 330000000, { 0x0036, 0x001b, 0x001b }, },
121	{ 594000000, { 0x003f, 0x001b, 0x001b }, },
122	{ ~0UL,      { 0x0000, 0x0000, 0x0000 }, }
123};
124
125static const struct dw_hdmi_phy_config sun50i_h6_phy_config[] = {
126	/*pixelclk   symbol   term   vlev*/
127	{ 74250000,  0x8009, 0x0004, 0x0232},
128	{ 148500000, 0x8029, 0x0004, 0x0273},
129	{ 594000000, 0x8039, 0x0004, 0x014a},
 
 
130	{ ~0UL,	     0x0000, 0x0000, 0x0000}
131};
132
133static int sun8i_hdmi_phy_config_a83t(struct dw_hdmi *hdmi,
134				      struct sun8i_hdmi_phy *phy,
135				      unsigned int clk_rate)
136{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_REXT_CTRL_REG,
138			   SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN,
139			   SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN);
140
141	/* power down */
142	dw_hdmi_phy_gen2_txpwron(hdmi, 0);
143	dw_hdmi_phy_gen2_pddq(hdmi, 1);
144
145	dw_hdmi_phy_reset(hdmi);
146
147	dw_hdmi_phy_gen2_pddq(hdmi, 0);
148
149	dw_hdmi_phy_i2c_set_addr(hdmi, I2C_ADDR);
150
151	/*
152	 * Values are taken from BSP HDMI driver. Although AW didn't
153	 * release any documentation, explanation of this values can
154	 * be found in i.MX 6Dual/6Quad Reference Manual.
155	 */
156	if (clk_rate <= 27000000) {
157		dw_hdmi_phy_i2c_write(hdmi, 0x01e0, 0x06);
158		dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x15);
159		dw_hdmi_phy_i2c_write(hdmi, 0x08da, 0x10);
160		dw_hdmi_phy_i2c_write(hdmi, 0x0007, 0x19);
161		dw_hdmi_phy_i2c_write(hdmi, 0x0318, 0x0e);
162		dw_hdmi_phy_i2c_write(hdmi, 0x8009, 0x09);
163	} else if (clk_rate <= 74250000) {
164		dw_hdmi_phy_i2c_write(hdmi, 0x0540, 0x06);
165		dw_hdmi_phy_i2c_write(hdmi, 0x0005, 0x15);
166		dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x10);
167		dw_hdmi_phy_i2c_write(hdmi, 0x0007, 0x19);
168		dw_hdmi_phy_i2c_write(hdmi, 0x02b5, 0x0e);
169		dw_hdmi_phy_i2c_write(hdmi, 0x8009, 0x09);
170	} else if (clk_rate <= 148500000) {
171		dw_hdmi_phy_i2c_write(hdmi, 0x04a0, 0x06);
172		dw_hdmi_phy_i2c_write(hdmi, 0x000a, 0x15);
173		dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x10);
174		dw_hdmi_phy_i2c_write(hdmi, 0x0002, 0x19);
175		dw_hdmi_phy_i2c_write(hdmi, 0x0021, 0x0e);
176		dw_hdmi_phy_i2c_write(hdmi, 0x8029, 0x09);
177	} else {
178		dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x06);
179		dw_hdmi_phy_i2c_write(hdmi, 0x000f, 0x15);
180		dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x10);
181		dw_hdmi_phy_i2c_write(hdmi, 0x0002, 0x19);
182		dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x0e);
183		dw_hdmi_phy_i2c_write(hdmi, 0x802b, 0x09);
184	}
185
186	dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x1e);
187	dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x13);
188	dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x17);
189
190	dw_hdmi_phy_gen2_txpwron(hdmi, 1);
191
192	return 0;
193}
194
195static int sun8i_hdmi_phy_config_h3(struct dw_hdmi *hdmi,
196				    struct sun8i_hdmi_phy *phy,
197				    unsigned int clk_rate)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198{
 
 
199	u32 pll_cfg1_init;
200	u32 pll_cfg2_init;
201	u32 ana_cfg1_end;
202	u32 ana_cfg2_init;
203	u32 ana_cfg3_init;
204	u32 b_offset = 0;
205	u32 val;
206
 
 
 
 
 
207	/* bandwidth / frequency independent settings */
208
209	pll_cfg1_init = SUN8I_HDMI_PHY_PLL_CFG1_LDO2_EN |
210			SUN8I_HDMI_PHY_PLL_CFG1_LDO1_EN |
211			SUN8I_HDMI_PHY_PLL_CFG1_LDO_VSET(7) |
212			SUN8I_HDMI_PHY_PLL_CFG1_UNKNOWN(1) |
213			SUN8I_HDMI_PHY_PLL_CFG1_PLLDBEN |
214			SUN8I_HDMI_PHY_PLL_CFG1_CS |
215			SUN8I_HDMI_PHY_PLL_CFG1_CP_S(2) |
216			SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(63) |
217			SUN8I_HDMI_PHY_PLL_CFG1_BWS;
218
219	pll_cfg2_init = SUN8I_HDMI_PHY_PLL_CFG2_SV_H |
220			SUN8I_HDMI_PHY_PLL_CFG2_VCOGAIN_EN |
221			SUN8I_HDMI_PHY_PLL_CFG2_SDIV2;
222
223	ana_cfg1_end = SUN8I_HDMI_PHY_ANA_CFG1_REG_SVBH(1) |
224		       SUN8I_HDMI_PHY_ANA_CFG1_AMP_OPT |
225		       SUN8I_HDMI_PHY_ANA_CFG1_EMP_OPT |
226		       SUN8I_HDMI_PHY_ANA_CFG1_AMPCK_OPT |
227		       SUN8I_HDMI_PHY_ANA_CFG1_EMPCK_OPT |
228		       SUN8I_HDMI_PHY_ANA_CFG1_ENRCAL |
229		       SUN8I_HDMI_PHY_ANA_CFG1_ENCALOG |
230		       SUN8I_HDMI_PHY_ANA_CFG1_REG_SCKTMDS |
231		       SUN8I_HDMI_PHY_ANA_CFG1_TMDSCLK_EN |
232		       SUN8I_HDMI_PHY_ANA_CFG1_TXEN_MASK |
233		       SUN8I_HDMI_PHY_ANA_CFG1_TXEN_ALL |
234		       SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDSCLK |
235		       SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS2 |
236		       SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS1 |
237		       SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS0 |
238		       SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS2 |
239		       SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS1 |
240		       SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS0 |
241		       SUN8I_HDMI_PHY_ANA_CFG1_CKEN |
242		       SUN8I_HDMI_PHY_ANA_CFG1_LDOEN |
243		       SUN8I_HDMI_PHY_ANA_CFG1_ENVBS |
244		       SUN8I_HDMI_PHY_ANA_CFG1_ENBI;
245
246	ana_cfg2_init = SUN8I_HDMI_PHY_ANA_CFG2_M_EN |
247			SUN8I_HDMI_PHY_ANA_CFG2_REG_DENCK |
248			SUN8I_HDMI_PHY_ANA_CFG2_REG_DEN |
249			SUN8I_HDMI_PHY_ANA_CFG2_REG_CKSS(1) |
250			SUN8I_HDMI_PHY_ANA_CFG2_REG_CSMPS(1);
251
252	ana_cfg3_init = SUN8I_HDMI_PHY_ANA_CFG3_REG_WIRE(0x3e0) |
253			SUN8I_HDMI_PHY_ANA_CFG3_SDAEN |
254			SUN8I_HDMI_PHY_ANA_CFG3_SCLEN;
255
256	/* bandwidth / frequency dependent settings */
257	if (clk_rate <= 27000000) {
258		pll_cfg1_init |= SUN8I_HDMI_PHY_PLL_CFG1_HV_IS_33 |
259				 SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(32);
260		pll_cfg2_init |= SUN8I_HDMI_PHY_PLL_CFG2_VCO_S(4) |
261				 SUN8I_HDMI_PHY_PLL_CFG2_S(4);
262		ana_cfg1_end |= SUN8I_HDMI_PHY_ANA_CFG1_REG_CALSW;
263		ana_cfg2_init |= SUN8I_HDMI_PHY_ANA_CFG2_REG_SLV(4) |
264				 SUN8I_HDMI_PHY_ANA_CFG2_REG_RESDI(phy->rcal);
265		ana_cfg3_init |= SUN8I_HDMI_PHY_ANA_CFG3_REG_AMPCK(3) |
266				 SUN8I_HDMI_PHY_ANA_CFG3_REG_AMP(5);
267	} else if (clk_rate <= 74250000) {
268		pll_cfg1_init |= SUN8I_HDMI_PHY_PLL_CFG1_HV_IS_33 |
269				 SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(32);
270		pll_cfg2_init |= SUN8I_HDMI_PHY_PLL_CFG2_VCO_S(4) |
271				 SUN8I_HDMI_PHY_PLL_CFG2_S(5);
272		ana_cfg1_end |= SUN8I_HDMI_PHY_ANA_CFG1_REG_CALSW;
273		ana_cfg2_init |= SUN8I_HDMI_PHY_ANA_CFG2_REG_SLV(4) |
274				 SUN8I_HDMI_PHY_ANA_CFG2_REG_RESDI(phy->rcal);
275		ana_cfg3_init |= SUN8I_HDMI_PHY_ANA_CFG3_REG_AMPCK(5) |
276				 SUN8I_HDMI_PHY_ANA_CFG3_REG_AMP(7);
277	} else if (clk_rate <= 148500000) {
278		pll_cfg1_init |= SUN8I_HDMI_PHY_PLL_CFG1_HV_IS_33 |
279				 SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(32);
280		pll_cfg2_init |= SUN8I_HDMI_PHY_PLL_CFG2_VCO_S(4) |
281				 SUN8I_HDMI_PHY_PLL_CFG2_S(6);
282		ana_cfg2_init |= SUN8I_HDMI_PHY_ANA_CFG2_REG_BIGSWCK |
283				 SUN8I_HDMI_PHY_ANA_CFG2_REG_BIGSW |
284				 SUN8I_HDMI_PHY_ANA_CFG2_REG_SLV(2);
285		ana_cfg3_init |= SUN8I_HDMI_PHY_ANA_CFG3_REG_AMPCK(7) |
286				 SUN8I_HDMI_PHY_ANA_CFG3_REG_AMP(9);
287	} else {
288		b_offset = 2;
289		pll_cfg1_init |= SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(63);
290		pll_cfg2_init |= SUN8I_HDMI_PHY_PLL_CFG2_VCO_S(6) |
291				 SUN8I_HDMI_PHY_PLL_CFG2_S(7);
292		ana_cfg2_init |= SUN8I_HDMI_PHY_ANA_CFG2_REG_BIGSWCK |
293				 SUN8I_HDMI_PHY_ANA_CFG2_REG_BIGSW |
294				 SUN8I_HDMI_PHY_ANA_CFG2_REG_SLV(4);
295		ana_cfg3_init |= SUN8I_HDMI_PHY_ANA_CFG3_REG_AMPCK(9) |
296				 SUN8I_HDMI_PHY_ANA_CFG3_REG_AMP(13) |
297				 SUN8I_HDMI_PHY_ANA_CFG3_REG_EMP(3);
298	}
299
300	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
301			   SUN8I_HDMI_PHY_ANA_CFG1_TXEN_MASK, 0);
302
303	/*
304	 * NOTE: We have to be careful not to overwrite PHY parent
305	 * clock selection bit and clock divider.
306	 */
307	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
308			   (u32)~SUN8I_HDMI_PHY_PLL_CFG1_CKIN_SEL_MSK,
309			   pll_cfg1_init);
310	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG2_REG,
311			   (u32)~SUN8I_HDMI_PHY_PLL_CFG2_PREDIV_MSK,
312			   pll_cfg2_init);
313	usleep_range(10000, 15000);
314	regmap_write(phy->regs, SUN8I_HDMI_PHY_PLL_CFG3_REG,
315		     SUN8I_HDMI_PHY_PLL_CFG3_SOUT_DIV2);
316	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
317			   SUN8I_HDMI_PHY_PLL_CFG1_PLLEN,
318			   SUN8I_HDMI_PHY_PLL_CFG1_PLLEN);
319	msleep(100);
320
321	/* get B value */
322	regmap_read(phy->regs, SUN8I_HDMI_PHY_ANA_STS_REG, &val);
323	val = (val & SUN8I_HDMI_PHY_ANA_STS_B_OUT_MSK) >>
324		SUN8I_HDMI_PHY_ANA_STS_B_OUT_SHIFT;
325	val = min(val + b_offset, (u32)0x3f);
326
327	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
328			   SUN8I_HDMI_PHY_PLL_CFG1_REG_OD1 |
329			   SUN8I_HDMI_PHY_PLL_CFG1_REG_OD,
330			   SUN8I_HDMI_PHY_PLL_CFG1_REG_OD1 |
331			   SUN8I_HDMI_PHY_PLL_CFG1_REG_OD);
332	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
333			   SUN8I_HDMI_PHY_PLL_CFG1_B_IN_MSK,
334			   val << SUN8I_HDMI_PHY_PLL_CFG1_B_IN_SHIFT);
335	msleep(100);
336	regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG, ana_cfg1_end);
337	regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG2_REG, ana_cfg2_init);
338	regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG3_REG, ana_cfg3_init);
339
340	return 0;
341}
342
343static int sun8i_hdmi_phy_config(struct dw_hdmi *hdmi, void *data,
344				 struct drm_display_mode *mode)
345{
346	struct sun8i_hdmi_phy *phy = (struct sun8i_hdmi_phy *)data;
347	u32 val = 0;
348
349	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
350		val |= SUN8I_HDMI_PHY_DBG_CTRL_POL_NHSYNC;
351
352	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
353		val |= SUN8I_HDMI_PHY_DBG_CTRL_POL_NVSYNC;
354
355	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_DBG_CTRL_REG,
356			   SUN8I_HDMI_PHY_DBG_CTRL_POL_MASK, val);
357
358	if (phy->variant->has_phy_clk)
359		clk_set_rate(phy->clk_phy, mode->crtc_clock * 1000);
360
361	return phy->variant->phy_config(hdmi, phy, mode->crtc_clock * 1000);
362};
363
364static void sun8i_hdmi_phy_disable_a83t(struct dw_hdmi *hdmi,
365					struct sun8i_hdmi_phy *phy)
366{
367	dw_hdmi_phy_gen2_txpwron(hdmi, 0);
368	dw_hdmi_phy_gen2_pddq(hdmi, 1);
369
370	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_REXT_CTRL_REG,
371			   SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN, 0);
372}
373
374static void sun8i_hdmi_phy_disable_h3(struct dw_hdmi *hdmi,
375				      struct sun8i_hdmi_phy *phy)
376{
377	regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
378		     SUN8I_HDMI_PHY_ANA_CFG1_LDOEN |
379		     SUN8I_HDMI_PHY_ANA_CFG1_ENVBS |
380		     SUN8I_HDMI_PHY_ANA_CFG1_ENBI);
381	regmap_write(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG, 0);
382}
383
384static void sun8i_hdmi_phy_disable(struct dw_hdmi *hdmi, void *data)
385{
386	struct sun8i_hdmi_phy *phy = (struct sun8i_hdmi_phy *)data;
387
388	phy->variant->phy_disable(hdmi, phy);
389}
390
391static const struct dw_hdmi_phy_ops sun8i_hdmi_phy_ops = {
392	.init = &sun8i_hdmi_phy_config,
393	.disable = &sun8i_hdmi_phy_disable,
394	.read_hpd = &dw_hdmi_phy_read_hpd,
395	.update_hpd = &dw_hdmi_phy_update_hpd,
396	.setup_hpd = &dw_hdmi_phy_setup_hpd,
397};
398
399static void sun8i_hdmi_phy_unlock(struct sun8i_hdmi_phy *phy)
400{
401	/* enable read access to HDMI controller */
402	regmap_write(phy->regs, SUN8I_HDMI_PHY_READ_EN_REG,
403		     SUN8I_HDMI_PHY_READ_EN_MAGIC);
404
405	/* unscramble register offsets */
406	regmap_write(phy->regs, SUN8I_HDMI_PHY_UNSCRAMBLE_REG,
407		     SUN8I_HDMI_PHY_UNSCRAMBLE_MAGIC);
408}
409
410static void sun50i_hdmi_phy_init_h6(struct sun8i_hdmi_phy *phy)
411{
412	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_REXT_CTRL_REG,
413			   SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN,
414			   SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN);
415
416	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_REXT_CTRL_REG,
417			   0xffff0000, 0x80c00000);
418}
419
420static void sun8i_hdmi_phy_init_a83t(struct sun8i_hdmi_phy *phy)
421{
422	sun8i_hdmi_phy_unlock(phy);
423
424	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_DBG_CTRL_REG,
425			   SUN8I_HDMI_PHY_DBG_CTRL_PX_LOCK,
426			   SUN8I_HDMI_PHY_DBG_CTRL_PX_LOCK);
427
428	/*
429	 * Set PHY I2C address. It must match to the address set by
430	 * dw_hdmi_phy_set_slave_addr().
431	 */
432	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_DBG_CTRL_REG,
433			   SUN8I_HDMI_PHY_DBG_CTRL_ADDR_MASK,
434			   SUN8I_HDMI_PHY_DBG_CTRL_ADDR(I2C_ADDR));
435}
436
437static void sun8i_hdmi_phy_init_h3(struct sun8i_hdmi_phy *phy)
438{
439	unsigned int val;
440
441	sun8i_hdmi_phy_unlock(phy);
442
443	regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG, 0);
444	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
445			   SUN8I_HDMI_PHY_ANA_CFG1_ENBI,
446			   SUN8I_HDMI_PHY_ANA_CFG1_ENBI);
447	udelay(5);
448	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
449			   SUN8I_HDMI_PHY_ANA_CFG1_TMDSCLK_EN,
450			   SUN8I_HDMI_PHY_ANA_CFG1_TMDSCLK_EN);
451	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
452			   SUN8I_HDMI_PHY_ANA_CFG1_ENVBS,
453			   SUN8I_HDMI_PHY_ANA_CFG1_ENVBS);
454	usleep_range(10, 20);
455	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
456			   SUN8I_HDMI_PHY_ANA_CFG1_LDOEN,
457			   SUN8I_HDMI_PHY_ANA_CFG1_LDOEN);
458	udelay(5);
459	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
460			   SUN8I_HDMI_PHY_ANA_CFG1_CKEN,
461			   SUN8I_HDMI_PHY_ANA_CFG1_CKEN);
462	usleep_range(40, 100);
463	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
464			   SUN8I_HDMI_PHY_ANA_CFG1_ENRCAL,
465			   SUN8I_HDMI_PHY_ANA_CFG1_ENRCAL);
466	usleep_range(100, 200);
467	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
468			   SUN8I_HDMI_PHY_ANA_CFG1_ENCALOG,
469			   SUN8I_HDMI_PHY_ANA_CFG1_ENCALOG);
470	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
471			   SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS0 |
472			   SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS1 |
473			   SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS2,
474			   SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS0 |
475			   SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS1 |
476			   SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS2);
477
478	/* wait for calibration to finish */
479	regmap_read_poll_timeout(phy->regs, SUN8I_HDMI_PHY_ANA_STS_REG, val,
480				 (val & SUN8I_HDMI_PHY_ANA_STS_RCALEND2D),
481				 100, 2000);
482
483	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
484			   SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDSCLK,
485			   SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDSCLK);
486	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
487			   SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS0 |
488			   SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS1 |
489			   SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS2 |
490			   SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDSCLK,
491			   SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS0 |
492			   SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS1 |
493			   SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS2 |
494			   SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDSCLK);
495
496	/* enable DDC communication */
497	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG3_REG,
498			   SUN8I_HDMI_PHY_ANA_CFG3_SCLEN |
499			   SUN8I_HDMI_PHY_ANA_CFG3_SDAEN,
500			   SUN8I_HDMI_PHY_ANA_CFG3_SCLEN |
501			   SUN8I_HDMI_PHY_ANA_CFG3_SDAEN);
502
503	/* reset PHY PLL clock parent */
504	regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
505			   SUN8I_HDMI_PHY_PLL_CFG1_CKIN_SEL_MSK, 0);
506
507	/* set HW control of CEC pins */
508	regmap_write(phy->regs, SUN8I_HDMI_PHY_CEC_REG, 0);
509
510	/* read calibration data */
511	regmap_read(phy->regs, SUN8I_HDMI_PHY_ANA_STS_REG, &val);
512	phy->rcal = (val & SUN8I_HDMI_PHY_ANA_STS_RCAL_MASK) >> 2;
513}
514
515void sun8i_hdmi_phy_init(struct sun8i_hdmi_phy *phy)
516{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
517	phy->variant->phy_init(phy);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
518}
519
520void sun8i_hdmi_phy_set_ops(struct sun8i_hdmi_phy *phy,
521			    struct dw_hdmi_plat_data *plat_data)
522{
523	struct sun8i_hdmi_phy_variant *variant = phy->variant;
524
525	if (variant->is_custom_phy) {
526		plat_data->phy_ops = &sun8i_hdmi_phy_ops;
527		plat_data->phy_name = "sun8i_dw_hdmi_phy";
528		plat_data->phy_data = phy;
529	} else {
530		plat_data->mpll_cfg = variant->mpll_cfg;
531		plat_data->cur_ctr = variant->cur_ctr;
532		plat_data->phy_config = variant->phy_cfg;
533	}
534}
535
536static struct regmap_config sun8i_hdmi_phy_regmap_config = {
537	.reg_bits	= 32,
538	.val_bits	= 32,
539	.reg_stride	= 4,
540	.max_register	= SUN8I_HDMI_PHY_CEC_REG,
541	.name		= "phy"
542};
543
544static const struct sun8i_hdmi_phy_variant sun8i_a83t_hdmi_phy = {
545	.is_custom_phy = true,
546	.phy_init = &sun8i_hdmi_phy_init_a83t,
547	.phy_disable = &sun8i_hdmi_phy_disable_a83t,
548	.phy_config = &sun8i_hdmi_phy_config_a83t,
549};
550
551static const struct sun8i_hdmi_phy_variant sun8i_h3_hdmi_phy = {
552	.has_phy_clk = true,
553	.is_custom_phy = true,
554	.phy_init = &sun8i_hdmi_phy_init_h3,
555	.phy_disable = &sun8i_hdmi_phy_disable_h3,
556	.phy_config = &sun8i_hdmi_phy_config_h3,
557};
558
559static const struct sun8i_hdmi_phy_variant sun8i_r40_hdmi_phy = {
560	.has_phy_clk = true,
561	.has_second_pll = true,
562	.is_custom_phy = true,
563	.phy_init = &sun8i_hdmi_phy_init_h3,
564	.phy_disable = &sun8i_hdmi_phy_disable_h3,
565	.phy_config = &sun8i_hdmi_phy_config_h3,
566};
567
568static const struct sun8i_hdmi_phy_variant sun50i_a64_hdmi_phy = {
569	.has_phy_clk = true,
570	.is_custom_phy = true,
571	.phy_init = &sun8i_hdmi_phy_init_h3,
572	.phy_disable = &sun8i_hdmi_phy_disable_h3,
573	.phy_config = &sun8i_hdmi_phy_config_h3,
574};
575
576static const struct sun8i_hdmi_phy_variant sun50i_h6_hdmi_phy = {
577	.cur_ctr  = sun50i_h6_cur_ctr,
578	.mpll_cfg = sun50i_h6_mpll_cfg,
579	.phy_cfg  = sun50i_h6_phy_config,
580	.phy_init = &sun50i_hdmi_phy_init_h6,
581};
582
583static const struct of_device_id sun8i_hdmi_phy_of_table[] = {
584	{
585		.compatible = "allwinner,sun8i-a83t-hdmi-phy",
586		.data = &sun8i_a83t_hdmi_phy,
587	},
588	{
589		.compatible = "allwinner,sun8i-h3-hdmi-phy",
590		.data = &sun8i_h3_hdmi_phy,
591	},
592	{
593		.compatible = "allwinner,sun8i-r40-hdmi-phy",
594		.data = &sun8i_r40_hdmi_phy,
595	},
596	{
597		.compatible = "allwinner,sun50i-a64-hdmi-phy",
598		.data = &sun50i_a64_hdmi_phy,
599	},
600	{
601		.compatible = "allwinner,sun50i-h6-hdmi-phy",
602		.data = &sun50i_h6_hdmi_phy,
603	},
604	{ /* sentinel */ }
605};
606
607int sun8i_hdmi_phy_probe(struct sun8i_dw_hdmi *hdmi, struct device_node *node)
608{
609	const struct of_device_id *match;
610	struct device *dev = hdmi->dev;
611	struct sun8i_hdmi_phy *phy;
612	struct resource res;
613	void __iomem *regs;
614	int ret;
615
616	match = of_match_node(sun8i_hdmi_phy_of_table, node);
617	if (!match) {
618		dev_err(dev, "Incompatible HDMI PHY\n");
619		return -EINVAL;
 
 
 
620	}
621
 
 
 
 
 
 
 
 
 
 
 
 
 
622	phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
623	if (!phy)
624		return -ENOMEM;
625
626	phy->variant = (struct sun8i_hdmi_phy_variant *)match->data;
 
627
628	ret = of_address_to_resource(node, 0, &res);
629	if (ret) {
630		dev_err(dev, "phy: Couldn't get our resources\n");
631		return ret;
632	}
633
634	regs = devm_ioremap_resource(dev, &res);
635	if (IS_ERR(regs)) {
636		dev_err(dev, "Couldn't map the HDMI PHY registers\n");
637		return PTR_ERR(regs);
638	}
639
640	phy->regs = devm_regmap_init_mmio(dev, regs,
641					  &sun8i_hdmi_phy_regmap_config);
642	if (IS_ERR(phy->regs)) {
643		dev_err(dev, "Couldn't create the HDMI PHY regmap\n");
644		return PTR_ERR(phy->regs);
645	}
646
647	phy->clk_bus = of_clk_get_by_name(node, "bus");
648	if (IS_ERR(phy->clk_bus)) {
649		dev_err(dev, "Could not get bus clock\n");
650		return PTR_ERR(phy->clk_bus);
651	}
652
653	phy->clk_mod = of_clk_get_by_name(node, "mod");
654	if (IS_ERR(phy->clk_mod)) {
655		dev_err(dev, "Could not get mod clock\n");
656		ret = PTR_ERR(phy->clk_mod);
657		goto err_put_clk_bus;
658	}
659
660	if (phy->variant->has_phy_clk) {
661		phy->clk_pll0 = of_clk_get_by_name(node, "pll-0");
662		if (IS_ERR(phy->clk_pll0)) {
663			dev_err(dev, "Could not get pll-0 clock\n");
664			ret = PTR_ERR(phy->clk_pll0);
665			goto err_put_clk_mod;
666		}
667
668		if (phy->variant->has_second_pll) {
669			phy->clk_pll1 = of_clk_get_by_name(node, "pll-1");
670			if (IS_ERR(phy->clk_pll1)) {
671				dev_err(dev, "Could not get pll-1 clock\n");
672				ret = PTR_ERR(phy->clk_pll1);
673				goto err_put_clk_pll0;
674			}
675		}
676	}
677
678	phy->rst_phy = of_reset_control_get_shared(node, "phy");
679	if (IS_ERR(phy->rst_phy)) {
680		dev_err(dev, "Could not get phy reset control\n");
681		ret = PTR_ERR(phy->rst_phy);
682		goto err_put_clk_pll1;
683	}
684
685	ret = reset_control_deassert(phy->rst_phy);
686	if (ret) {
687		dev_err(dev, "Cannot deassert phy reset control: %d\n", ret);
688		goto err_put_rst_phy;
689	}
690
691	ret = clk_prepare_enable(phy->clk_bus);
692	if (ret) {
693		dev_err(dev, "Cannot enable bus clock: %d\n", ret);
694		goto err_deassert_rst_phy;
695	}
696
697	ret = clk_prepare_enable(phy->clk_mod);
698	if (ret) {
699		dev_err(dev, "Cannot enable mod clock: %d\n", ret);
700		goto err_disable_clk_bus;
701	}
702
703	if (phy->variant->has_phy_clk) {
704		ret = sun8i_phy_clk_create(phy, dev,
705					   phy->variant->has_second_pll);
706		if (ret) {
707			dev_err(dev, "Couldn't create the PHY clock\n");
708			goto err_disable_clk_mod;
709		}
710
711		clk_prepare_enable(phy->clk_phy);
712	}
713
714	hdmi->phy = phy;
715
716	return 0;
717
718err_disable_clk_mod:
719	clk_disable_unprepare(phy->clk_mod);
720err_disable_clk_bus:
721	clk_disable_unprepare(phy->clk_bus);
722err_deassert_rst_phy:
723	reset_control_assert(phy->rst_phy);
724err_put_rst_phy:
725	reset_control_put(phy->rst_phy);
726err_put_clk_pll1:
727	clk_put(phy->clk_pll1);
728err_put_clk_pll0:
729	clk_put(phy->clk_pll0);
730err_put_clk_mod:
731	clk_put(phy->clk_mod);
732err_put_clk_bus:
733	clk_put(phy->clk_bus);
734
735	return ret;
736}
737
738void sun8i_hdmi_phy_remove(struct sun8i_dw_hdmi *hdmi)
739{
740	struct sun8i_hdmi_phy *phy = hdmi->phy;
741
742	clk_disable_unprepare(phy->clk_mod);
743	clk_disable_unprepare(phy->clk_bus);
744	clk_disable_unprepare(phy->clk_phy);
745
746	reset_control_assert(phy->rst_phy);
747
748	reset_control_put(phy->rst_phy);
749
750	clk_put(phy->clk_pll0);
751	clk_put(phy->clk_pll1);
752	clk_put(phy->clk_mod);
753	clk_put(phy->clk_bus);
754}