blob: b9c03ea03864b6ba278e5e8a98d8cb8c8eac2e68 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jernej Skrabec8d91b462017-03-27 19:22:32 +02002/*
3 * Allwinner DW HDMI bridge
4 *
5 * (C) Copyright 2017 Jernej Skrabec <jernej.skrabec@siol.net>
Jernej Skrabec8d91b462017-03-27 19:22:32 +02006 */
7
Samuel Holland65bd46b2022-11-28 01:02:27 -06008#include <clk.h>
Jernej Skrabec8d91b462017-03-27 19:22:32 +02009#include <display.h>
10#include <dm.h>
11#include <dw_hdmi.h>
12#include <edid.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Samuel Holland65bd46b2022-11-28 01:02:27 -060014#include <reset.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070015#include <time.h>
Jernej Skrabec8d91b462017-03-27 19:22:32 +020016#include <asm/io.h>
17#include <asm/arch/clock.h>
18#include <asm/arch/lcdc.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060019#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060020#include <linux/delay.h>
Samuel Holland56a730b2022-11-28 01:02:28 -060021#include <power/regulator.h>
Jernej Skrabec8d91b462017-03-27 19:22:32 +020022
23struct sunxi_dw_hdmi_priv {
24 struct dw_hdmi hdmi;
Samuel Holland65bd46b2022-11-28 01:02:27 -060025 struct reset_ctl_bulk resets;
26 struct clk_bulk clocks;
Samuel Holland56a730b2022-11-28 01:02:28 -060027 struct udevice *hvcc;
Jernej Skrabec8d91b462017-03-27 19:22:32 +020028};
29
30struct sunxi_hdmi_phy {
31 u32 pol;
32 u32 res1[3];
33 u32 read_en;
34 u32 unscramble;
35 u32 res2[2];
36 u32 ctrl;
37 u32 unk1;
38 u32 unk2;
39 u32 pll;
40 u32 clk;
41 u32 unk3;
42 u32 status;
43};
44
45#define HDMI_PHY_OFFS 0x10000
46
47static int sunxi_dw_hdmi_get_divider(uint clock)
48{
49 /*
50 * Due to missing documentaion of HDMI PHY, we know correct
51 * settings only for following four PHY dividers. Select one
52 * based on clock speed.
53 */
54 if (clock <= 27000000)
55 return 11;
56 else if (clock <= 74250000)
57 return 4;
58 else if (clock <= 148500000)
59 return 2;
60 else
61 return 1;
62}
63
Jernej Skrabecd04dbf72022-11-28 01:02:26 -060064static void sunxi_dw_hdmi_phy_init(struct dw_hdmi *hdmi)
Jernej Skrabec8d91b462017-03-27 19:22:32 +020065{
66 struct sunxi_hdmi_phy * const phy =
Jernej Skrabecd04dbf72022-11-28 01:02:26 -060067 (struct sunxi_hdmi_phy *)(hdmi->ioaddr + HDMI_PHY_OFFS);
Jernej Skrabec8d91b462017-03-27 19:22:32 +020068 unsigned long tmo;
69 u32 tmp;
70
71 /*
72 * HDMI PHY settings are taken as-is from Allwinner BSP code.
73 * There is no documentation.
74 */
75 writel(0, &phy->ctrl);
76 setbits_le32(&phy->ctrl, BIT(0));
77 udelay(5);
78 setbits_le32(&phy->ctrl, BIT(16));
79 setbits_le32(&phy->ctrl, BIT(1));
80 udelay(10);
81 setbits_le32(&phy->ctrl, BIT(2));
82 udelay(5);
83 setbits_le32(&phy->ctrl, BIT(3));
84 udelay(40);
85 setbits_le32(&phy->ctrl, BIT(19));
86 udelay(100);
87 setbits_le32(&phy->ctrl, BIT(18));
88 setbits_le32(&phy->ctrl, 7 << 4);
89
90 /* Note that Allwinner code doesn't fail in case of timeout */
91 tmo = timer_get_us() + 2000;
92 while ((readl(&phy->status) & 0x80) == 0) {
93 if (timer_get_us() > tmo) {
94 printf("Warning: HDMI PHY init timeout!\n");
95 break;
96 }
97 }
98
99 setbits_le32(&phy->ctrl, 0xf << 8);
100 setbits_le32(&phy->ctrl, BIT(7));
101
102 writel(0x39dc5040, &phy->pll);
103 writel(0x80084343, &phy->clk);
104 udelay(10000);
105 writel(1, &phy->unk3);
106 setbits_le32(&phy->pll, BIT(25));
107 udelay(100000);
108 tmp = (readl(&phy->status) & 0x1f800) >> 11;
109 setbits_le32(&phy->pll, BIT(31) | BIT(30));
110 setbits_le32(&phy->pll, tmp);
111 writel(0x01FF0F7F, &phy->ctrl);
112 writel(0x80639000, &phy->unk1);
113 writel(0x0F81C405, &phy->unk2);
114
115 /* enable read access to HDMI controller */
116 writel(0x54524545, &phy->read_en);
117 /* descramble register offsets */
118 writel(0x42494E47, &phy->unscramble);
119}
120
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600121static void sunxi_dw_hdmi_phy_set(struct dw_hdmi *hdmi, uint clock, int phy_div)
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200122{
123 struct sunxi_hdmi_phy * const phy =
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600124 (struct sunxi_hdmi_phy *)(hdmi->ioaddr + HDMI_PHY_OFFS);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200125 int div = sunxi_dw_hdmi_get_divider(clock);
126 u32 tmp;
127
128 /*
129 * Unfortunately, we don't know much about those magic
130 * numbers. They are taken from Allwinner BSP driver.
131 */
132 switch (div) {
133 case 1:
134 writel(0x30dc5fc0, &phy->pll);
Jernej Skrabec89d18022019-03-24 19:26:40 +0100135 writel(0x800863C0 | (phy_div - 1), &phy->clk);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200136 mdelay(10);
137 writel(0x00000001, &phy->unk3);
138 setbits_le32(&phy->pll, BIT(25));
139 mdelay(200);
140 tmp = (readl(&phy->status) & 0x1f800) >> 11;
141 setbits_le32(&phy->pll, BIT(31) | BIT(30));
142 if (tmp < 0x3d)
143 setbits_le32(&phy->pll, tmp + 2);
144 else
145 setbits_le32(&phy->pll, 0x3f);
146 mdelay(100);
147 writel(0x01FFFF7F, &phy->ctrl);
148 writel(0x8063b000, &phy->unk1);
149 writel(0x0F8246B5, &phy->unk2);
150 break;
151 case 2:
152 writel(0x39dc5040, &phy->pll);
Jernej Skrabec89d18022019-03-24 19:26:40 +0100153 writel(0x80084380 | (phy_div - 1), &phy->clk);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200154 mdelay(10);
155 writel(0x00000001, &phy->unk3);
156 setbits_le32(&phy->pll, BIT(25));
157 mdelay(100);
158 tmp = (readl(&phy->status) & 0x1f800) >> 11;
159 setbits_le32(&phy->pll, BIT(31) | BIT(30));
160 setbits_le32(&phy->pll, tmp);
161 writel(0x01FFFF7F, &phy->ctrl);
162 writel(0x8063a800, &phy->unk1);
163 writel(0x0F81C485, &phy->unk2);
164 break;
165 case 4:
166 writel(0x39dc5040, &phy->pll);
Jernej Skrabec89d18022019-03-24 19:26:40 +0100167 writel(0x80084340 | (phy_div - 1), &phy->clk);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200168 mdelay(10);
169 writel(0x00000001, &phy->unk3);
170 setbits_le32(&phy->pll, BIT(25));
171 mdelay(100);
172 tmp = (readl(&phy->status) & 0x1f800) >> 11;
173 setbits_le32(&phy->pll, BIT(31) | BIT(30));
174 setbits_le32(&phy->pll, tmp);
175 writel(0x01FFFF7F, &phy->ctrl);
176 writel(0x8063b000, &phy->unk1);
177 writel(0x0F81C405, &phy->unk2);
178 break;
179 case 11:
180 writel(0x39dc5040, &phy->pll);
Jernej Skrabec89d18022019-03-24 19:26:40 +0100181 writel(0x80084300 | (phy_div - 1), &phy->clk);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200182 mdelay(10);
183 writel(0x00000001, &phy->unk3);
184 setbits_le32(&phy->pll, BIT(25));
185 mdelay(100);
186 tmp = (readl(&phy->status) & 0x1f800) >> 11;
187 setbits_le32(&phy->pll, BIT(31) | BIT(30));
188 setbits_le32(&phy->pll, tmp);
189 writel(0x01FFFF7F, &phy->ctrl);
190 writel(0x8063b000, &phy->unk1);
191 writel(0x0F81C405, &phy->unk2);
192 break;
193 }
194}
195
Jernej Skrabec89d18022019-03-24 19:26:40 +0100196static void sunxi_dw_hdmi_pll_set(uint clk_khz, int *phy_div)
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200197{
Jernej Skrabec89d18022019-03-24 19:26:40 +0100198 int value, n, m, div, diff;
199 int best_n = 0, best_m = 0, best_div = 0, best_diff = 0x0FFFFFFF;
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200200
201 /*
202 * Find the lowest divider resulting in a matching clock. If there
203 * is no match, pick the closest lower clock, as monitors tend to
204 * not sync to higher frequencies.
205 */
Jernej Skrabec89d18022019-03-24 19:26:40 +0100206 for (div = 1; div <= 16; div++) {
207 int target = clk_khz * div;
208
209 if (target < 192000)
210 continue;
211 if (target > 912000)
212 continue;
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200213
Jernej Skrabec89d18022019-03-24 19:26:40 +0100214 for (m = 1; m <= 16; m++) {
215 n = (m * target) / 24000;
216
217 if (n >= 1 && n <= 128) {
218 value = (24000 * n) / m / div;
219 diff = clk_khz - value;
220 if (diff < best_diff) {
221 best_diff = diff;
222 best_m = m;
223 best_n = n;
224 best_div = div;
225 }
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200226 }
227 }
228 }
229
Jernej Skrabec89d18022019-03-24 19:26:40 +0100230 *phy_div = best_div;
231
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200232 clock_set_pll3_factors(best_m, best_n);
233 debug("dotclock: %dkHz = %dkHz: (24MHz * %d) / %d / %d\n",
Jernej Skrabec89d18022019-03-24 19:26:40 +0100234 clk_khz, (clock_get_pll3() / 1000) / best_div,
235 best_n, best_m, best_div);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200236}
237
238static void sunxi_dw_hdmi_lcdc_init(int mux, const struct display_timing *edid,
239 int bpp)
240{
241 struct sunxi_ccm_reg * const ccm =
242 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Mark Kettenis898c6092019-08-09 22:30:26 +0200243 int div = DIV_ROUND_UP(clock_get_pll3(), edid->pixelclock.typ);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200244 struct sunxi_lcdc_reg *lcdc;
245
246 if (mux == 0) {
247 lcdc = (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
248
249 /* Reset off */
250 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_LCD0);
251
252 /* Clock on */
253 setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_LCD0);
254 writel(CCM_LCD0_CTRL_GATE | CCM_LCD0_CTRL_M(div),
255 &ccm->lcd0_clk_cfg);
256 } else {
257 lcdc = (struct sunxi_lcdc_reg *)SUNXI_LCD1_BASE;
258
259 /* Reset off */
260 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_LCD1);
261
262 /* Clock on */
263 setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_LCD1);
264 writel(CCM_LCD1_CTRL_GATE | CCM_LCD1_CTRL_M(div),
265 &ccm->lcd1_clk_cfg);
266 }
267
268 lcdc_init(lcdc);
269 lcdc_tcon1_mode_set(lcdc, edid, false, false);
270 lcdc_enable(lcdc, bpp);
271}
272
273static int sunxi_dw_hdmi_phy_cfg(struct dw_hdmi *hdmi, uint mpixelclock)
274{
Jernej Skrabec89d18022019-03-24 19:26:40 +0100275 int phy_div;
276
277 sunxi_dw_hdmi_pll_set(mpixelclock / 1000, &phy_div);
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600278 sunxi_dw_hdmi_phy_set(hdmi, mpixelclock, phy_div);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200279
280 return 0;
281}
282
283static int sunxi_dw_hdmi_read_edid(struct udevice *dev, u8 *buf, int buf_size)
284{
285 struct sunxi_dw_hdmi_priv *priv = dev_get_priv(dev);
286
287 return dw_hdmi_read_edid(&priv->hdmi, buf, buf_size);
288}
289
Jernej Skrabec5b2b0a72021-04-22 01:14:26 +0100290static bool sunxi_dw_hdmi_mode_valid(struct udevice *dev,
291 const struct display_timing *timing)
292{
293 return timing->pixelclock.typ <= 297000000;
294}
295
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200296static int sunxi_dw_hdmi_enable(struct udevice *dev, int panel_bpp,
297 const struct display_timing *edid)
298{
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600299 struct sunxi_dw_hdmi_priv *priv = dev_get_priv(dev);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200300 struct sunxi_hdmi_phy * const phy =
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600301 (struct sunxi_hdmi_phy *)(priv->hdmi.ioaddr + HDMI_PHY_OFFS);
Jernej Skrabec5dd59e52021-04-22 01:14:33 +0100302 struct display_plat *uc_plat = dev_get_uclass_plat(dev);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200303 int ret;
304
305 ret = dw_hdmi_enable(&priv->hdmi, edid);
306 if (ret)
307 return ret;
308
Jernej Skrabec5dd59e52021-04-22 01:14:33 +0100309 sunxi_dw_hdmi_lcdc_init(uc_plat->source_id, edid, panel_bpp);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200310
Vasily Khoruzhickefce4122018-05-14 13:49:52 -0700311 if (edid->flags & DISPLAY_FLAGS_VSYNC_LOW)
Vasily Khoruzhick97d19672017-11-28 22:33:27 -0800312 setbits_le32(&phy->pol, 0x200);
313
Vasily Khoruzhickefce4122018-05-14 13:49:52 -0700314 if (edid->flags & DISPLAY_FLAGS_HSYNC_LOW)
Vasily Khoruzhick97d19672017-11-28 22:33:27 -0800315 setbits_le32(&phy->pol, 0x100);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200316
317 setbits_le32(&phy->ctrl, 0xf << 12);
318
319 /*
320 * This is last hdmi access before boot, so scramble addresses
321 * again or othwerwise BSP driver won't work. Dummy read is
322 * needed or otherwise last write doesn't get written correctly.
323 */
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600324 (void)readb(priv->hdmi.ioaddr);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200325 writel(0, &phy->unscramble);
326
327 return 0;
328}
329
330static int sunxi_dw_hdmi_probe(struct udevice *dev)
331{
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200332 struct sunxi_dw_hdmi_priv *priv = dev_get_priv(dev);
333 struct sunxi_ccm_reg * const ccm =
334 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
335 int ret;
336
Samuel Holland56a730b2022-11-28 01:02:28 -0600337 if (priv->hvcc)
338 regulator_set_enable(priv->hvcc, true);
339
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200340 /* Set pll3 to 297 MHz */
341 clock_set_pll3(297000000);
342
343 /* Set hdmi parent to pll3 */
344 clrsetbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_PLL_MASK,
345 CCM_HDMI_CTRL_PLL3);
346
Samuel Holland65bd46b2022-11-28 01:02:27 -0600347 /* This reset is referenced from the PHY devicetree node. */
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200348 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_HDMI2);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200349
Samuel Holland65bd46b2022-11-28 01:02:27 -0600350 ret = reset_deassert_bulk(&priv->resets);
351 if (ret)
352 return ret;
353
354 ret = clk_enable_bulk(&priv->clocks);
355 if (ret)
356 return ret;
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200357
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600358 sunxi_dw_hdmi_phy_init(&priv->hdmi);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200359
Jagan Teki17d0f552024-01-17 13:21:40 +0530360 ret = dw_hdmi_detect_hpd(&priv->hdmi);
361 if (ret < 0)
362 return ret;
Jernej Skrabec1cf66192021-04-22 01:14:30 +0100363
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200364 dw_hdmi_init(&priv->hdmi);
365
366 return 0;
367}
368
Jagan Tekib3c66b62024-01-17 13:21:39 +0530369static const struct dw_hdmi_phy_ops dw_hdmi_sunxi_phy_ops = {
370 .phy_set = sunxi_dw_hdmi_phy_cfg,
371};
372
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600373static int sunxi_dw_hdmi_of_to_plat(struct udevice *dev)
374{
375 struct sunxi_dw_hdmi_priv *priv = dev_get_priv(dev);
376 struct dw_hdmi *hdmi = &priv->hdmi;
Samuel Holland65bd46b2022-11-28 01:02:27 -0600377 int ret;
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600378
379 hdmi->ioaddr = (ulong)dev_read_addr(dev);
380 hdmi->i2c_clk_high = 0xd8;
381 hdmi->i2c_clk_low = 0xfe;
382 hdmi->reg_io_width = 1;
Jagan Tekib3c66b62024-01-17 13:21:39 +0530383 hdmi->ops = &dw_hdmi_sunxi_phy_ops;
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600384
Samuel Holland65bd46b2022-11-28 01:02:27 -0600385 ret = reset_get_bulk(dev, &priv->resets);
386 if (ret)
387 return ret;
388
389 ret = clk_get_bulk(dev, &priv->clocks);
390 if (ret)
391 return ret;
392
Samuel Holland56a730b2022-11-28 01:02:28 -0600393 ret = device_get_supply_regulator(dev, "hvcc-supply", &priv->hvcc);
394 if (ret)
395 priv->hvcc = NULL;
396
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600397 return 0;
398}
399
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200400static const struct dm_display_ops sunxi_dw_hdmi_ops = {
401 .read_edid = sunxi_dw_hdmi_read_edid,
402 .enable = sunxi_dw_hdmi_enable,
Jernej Skrabec5b2b0a72021-04-22 01:14:26 +0100403 .mode_valid = sunxi_dw_hdmi_mode_valid,
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200404};
405
Jernej Skrabec97d10d42022-11-28 01:02:25 -0600406static const struct udevice_id sunxi_dw_hdmi_ids[] = {
407 { .compatible = "allwinner,sun8i-a83t-dw-hdmi" },
408 { }
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200409};
410
Jernej Skrabec97d10d42022-11-28 01:02:25 -0600411U_BOOT_DRIVER(sunxi_dw_hdmi) = {
412 .name = "sunxi_dw_hdmi",
413 .id = UCLASS_DISPLAY,
414 .of_match = sunxi_dw_hdmi_ids,
415 .probe = sunxi_dw_hdmi_probe,
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600416 .of_to_plat = sunxi_dw_hdmi_of_to_plat,
Jernej Skrabec97d10d42022-11-28 01:02:25 -0600417 .priv_auto = sizeof(struct sunxi_dw_hdmi_priv),
418 .ops = &sunxi_dw_hdmi_ops,
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200419};