blob: ded03b109c90fb911e1e96fac9ded79f79ccd36d [file] [log] [blame]
Yannick Fertré18b6ca52019-10-07 15:29:07 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 STMicroelectronics - All Rights Reserved
4 * Author(s): Philippe Cornu <philippe.cornu@st.com> for STMicroelectronics.
5 * Yannick Fertre <yannick.fertre@st.com> for STMicroelectronics.
6 *
7 * This MIPI DSI controller driver is based on the Linux Kernel driver from
8 * drivers/gpu/drm/stm/dw_mipi_dsi-stm.c.
9 */
10
11#include <common.h>
12#include <clk.h>
13#include <dm.h>
14#include <dsi_host.h>
15#include <mipi_dsi.h>
16#include <panel.h>
17#include <reset.h>
18#include <video.h>
19#include <video_bridge.h>
20#include <asm/io.h>
21#include <asm/arch/gpio.h>
22#include <dm/device-internal.h>
Simon Glass336d4612020-02-03 07:36:16 -070023#include <dm/device_compat.h>
Patrick Delaunay754815b2019-11-12 15:39:58 +010024#include <dm/lists.h>
Yannick Fertré18b6ca52019-10-07 15:29:07 +020025#include <linux/iopoll.h>
26#include <power/regulator.h>
27
28#define HWVER_130 0x31333000 /* IP version 1.30 */
29#define HWVER_131 0x31333100 /* IP version 1.31 */
30
31/* DSI digital registers & bit definitions */
32#define DSI_VERSION 0x00
33#define VERSION GENMASK(31, 8)
34
35/*
36 * DSI wrapper registers & bit definitions
37 * Note: registers are named as in the Reference Manual
38 */
39#define DSI_WCFGR 0x0400 /* Wrapper ConFiGuration Reg */
40#define WCFGR_DSIM BIT(0) /* DSI Mode */
41#define WCFGR_COLMUX GENMASK(3, 1) /* COLor MUltipleXing */
42
43#define DSI_WCR 0x0404 /* Wrapper Control Reg */
44#define WCR_DSIEN BIT(3) /* DSI ENable */
45
46#define DSI_WISR 0x040C /* Wrapper Interrupt and Status Reg */
47#define WISR_PLLLS BIT(8) /* PLL Lock Status */
48#define WISR_RRS BIT(12) /* Regulator Ready Status */
49
50#define DSI_WPCR0 0x0418 /* Wrapper Phy Conf Reg 0 */
51#define WPCR0_UIX4 GENMASK(5, 0) /* Unit Interval X 4 */
52#define WPCR0_TDDL BIT(16) /* Turn Disable Data Lanes */
53
54#define DSI_WRPCR 0x0430 /* Wrapper Regulator & Pll Ctrl Reg */
55#define WRPCR_PLLEN BIT(0) /* PLL ENable */
56#define WRPCR_NDIV GENMASK(8, 2) /* pll loop DIVision Factor */
57#define WRPCR_IDF GENMASK(14, 11) /* pll Input Division Factor */
58#define WRPCR_ODF GENMASK(17, 16) /* pll Output Division Factor */
59#define WRPCR_REGEN BIT(24) /* REGulator ENable */
60#define WRPCR_BGREN BIT(28) /* BandGap Reference ENable */
61#define IDF_MIN 1
62#define IDF_MAX 7
63#define NDIV_MIN 10
64#define NDIV_MAX 125
65#define ODF_MIN 1
66#define ODF_MAX 8
67
68/* dsi color format coding according to the datasheet */
69enum dsi_color {
70 DSI_RGB565_CONF1,
71 DSI_RGB565_CONF2,
72 DSI_RGB565_CONF3,
73 DSI_RGB666_CONF1,
74 DSI_RGB666_CONF2,
75 DSI_RGB888,
76};
77
78#define LANE_MIN_KBPS 31250
79#define LANE_MAX_KBPS 500000
80
81/* Timeout for regulator on/off, pll lock/unlock & fifo empty */
82#define TIMEOUT_US 200000
83
84struct stm32_dsi_priv {
85 struct mipi_dsi_device device;
86 void __iomem *base;
87 struct udevice *panel;
88 u32 pllref_clk;
89 u32 hw_version;
90 int lane_min_kbps;
91 int lane_max_kbps;
92 struct udevice *vdd_reg;
93 struct udevice *dsi_host;
94};
95
96static inline void dsi_write(struct stm32_dsi_priv *dsi, u32 reg, u32 val)
97{
98 writel(val, dsi->base + reg);
99}
100
101static inline u32 dsi_read(struct stm32_dsi_priv *dsi, u32 reg)
102{
103 return readl(dsi->base + reg);
104}
105
106static inline void dsi_set(struct stm32_dsi_priv *dsi, u32 reg, u32 mask)
107{
108 dsi_write(dsi, reg, dsi_read(dsi, reg) | mask);
109}
110
111static inline void dsi_clear(struct stm32_dsi_priv *dsi, u32 reg, u32 mask)
112{
113 dsi_write(dsi, reg, dsi_read(dsi, reg) & ~mask);
114}
115
116static inline void dsi_update_bits(struct stm32_dsi_priv *dsi, u32 reg,
117 u32 mask, u32 val)
118{
119 dsi_write(dsi, reg, (dsi_read(dsi, reg) & ~mask) | val);
120}
121
122static enum dsi_color dsi_color_from_mipi(u32 fmt)
123{
124 switch (fmt) {
125 case MIPI_DSI_FMT_RGB888:
126 return DSI_RGB888;
127 case MIPI_DSI_FMT_RGB666:
128 return DSI_RGB666_CONF2;
129 case MIPI_DSI_FMT_RGB666_PACKED:
130 return DSI_RGB666_CONF1;
131 case MIPI_DSI_FMT_RGB565:
132 return DSI_RGB565_CONF1;
133 default:
134 pr_err("MIPI color invalid, so we use rgb888\n");
135 }
136 return DSI_RGB888;
137}
138
139static int dsi_pll_get_clkout_khz(int clkin_khz, int idf, int ndiv, int odf)
140{
141 int divisor = idf * odf;
142
143 /* prevent from division by 0 */
144 if (!divisor)
145 return 0;
146
147 return DIV_ROUND_CLOSEST(clkin_khz * ndiv, divisor);
148}
149
150static int dsi_pll_get_params(struct stm32_dsi_priv *dsi,
151 int clkin_khz, int clkout_khz,
152 int *idf, int *ndiv, int *odf)
153{
154 int i, o, n, n_min, n_max;
155 int fvco_min, fvco_max, delta, best_delta; /* all in khz */
156
157 /* Early checks preventing division by 0 & odd results */
158 if (clkin_khz <= 0 || clkout_khz <= 0)
159 return -EINVAL;
160
161 fvco_min = dsi->lane_min_kbps * 2 * ODF_MAX;
162 fvco_max = dsi->lane_max_kbps * 2 * ODF_MIN;
163
164 best_delta = 1000000; /* big started value (1000000khz) */
165
166 for (i = IDF_MIN; i <= IDF_MAX; i++) {
167 /* Compute ndiv range according to Fvco */
168 n_min = ((fvco_min * i) / (2 * clkin_khz)) + 1;
169 n_max = (fvco_max * i) / (2 * clkin_khz);
170
171 /* No need to continue idf loop if we reach ndiv max */
172 if (n_min >= NDIV_MAX)
173 break;
174
175 /* Clamp ndiv to valid values */
176 if (n_min < NDIV_MIN)
177 n_min = NDIV_MIN;
178 if (n_max > NDIV_MAX)
179 n_max = NDIV_MAX;
180
181 for (o = ODF_MIN; o <= ODF_MAX; o *= 2) {
182 n = DIV_ROUND_CLOSEST(i * o * clkout_khz, clkin_khz);
183 /* Check ndiv according to vco range */
184 if (n < n_min || n > n_max)
185 continue;
186 /* Check if new delta is better & saves parameters */
187 delta = dsi_pll_get_clkout_khz(clkin_khz, i, n, o) -
188 clkout_khz;
189 if (delta < 0)
190 delta = -delta;
191 if (delta < best_delta) {
192 *idf = i;
193 *ndiv = n;
194 *odf = o;
195 best_delta = delta;
196 }
197 /* fast return in case of "perfect result" */
198 if (!delta)
199 return 0;
200 }
201 }
202
203 return 0;
204}
205
206static int dsi_phy_init(void *priv_data)
207{
208 struct mipi_dsi_device *device = priv_data;
209 struct udevice *dev = device->dev;
210 struct stm32_dsi_priv *dsi = dev_get_priv(dev);
211 u32 val;
212 int ret;
213
214 debug("Initialize DSI physical layer\n");
215
216 /* Enable the regulator */
217 dsi_set(dsi, DSI_WRPCR, WRPCR_REGEN | WRPCR_BGREN);
218 ret = readl_poll_timeout(dsi->base + DSI_WISR, val, val & WISR_RRS,
219 TIMEOUT_US);
220 if (ret) {
221 debug("!TIMEOUT! waiting REGU\n");
222 return ret;
223 }
224
225 /* Enable the DSI PLL & wait for its lock */
226 dsi_set(dsi, DSI_WRPCR, WRPCR_PLLEN);
227 ret = readl_poll_timeout(dsi->base + DSI_WISR, val, val & WISR_PLLLS,
228 TIMEOUT_US);
229 if (ret) {
230 debug("!TIMEOUT! waiting PLL\n");
231 return ret;
232 }
233
234 return 0;
235}
236
237static void dsi_phy_post_set_mode(void *priv_data, unsigned long mode_flags)
238{
239 struct mipi_dsi_device *device = priv_data;
240 struct udevice *dev = device->dev;
241 struct stm32_dsi_priv *dsi = dev_get_priv(dev);
242
243 debug("Set mode %p enable %ld\n", dsi,
244 mode_flags & MIPI_DSI_MODE_VIDEO);
245
246 if (!dsi)
247 return;
248
249 /*
250 * DSI wrapper must be enabled in video mode & disabled in command mode.
251 * If wrapper is enabled in command mode, the display controller
252 * register access will hang.
253 */
254
255 if (mode_flags & MIPI_DSI_MODE_VIDEO)
256 dsi_set(dsi, DSI_WCR, WCR_DSIEN);
257 else
258 dsi_clear(dsi, DSI_WCR, WCR_DSIEN);
259}
260
261static int dsi_get_lane_mbps(void *priv_data, struct display_timing *timings,
262 u32 lanes, u32 format, unsigned int *lane_mbps)
263{
264 struct mipi_dsi_device *device = priv_data;
265 struct udevice *dev = device->dev;
266 struct stm32_dsi_priv *dsi = dev_get_priv(dev);
267 int idf, ndiv, odf, pll_in_khz, pll_out_khz;
268 int ret, bpp;
269 u32 val;
270
271 /* Update lane capabilities according to hw version */
272 dsi->hw_version = dsi_read(dsi, DSI_VERSION) & VERSION;
273 dsi->lane_min_kbps = LANE_MIN_KBPS;
274 dsi->lane_max_kbps = LANE_MAX_KBPS;
275 if (dsi->hw_version == HWVER_131) {
276 dsi->lane_min_kbps *= 2;
277 dsi->lane_max_kbps *= 2;
278 }
279
280 pll_in_khz = dsi->pllref_clk / 1000;
281
282 /* Compute requested pll out */
283 bpp = mipi_dsi_pixel_format_to_bpp(format);
284 pll_out_khz = (timings->pixelclock.typ / 1000) * bpp / lanes;
285 /* Add 20% to pll out to be higher than pixel bw (burst mode only) */
286 pll_out_khz = (pll_out_khz * 12) / 10;
287 if (pll_out_khz > dsi->lane_max_kbps) {
288 pll_out_khz = dsi->lane_max_kbps;
289 dev_warn(dev, "Warning max phy mbps is used\n");
290 }
291 if (pll_out_khz < dsi->lane_min_kbps) {
292 pll_out_khz = dsi->lane_min_kbps;
293 dev_warn(dev, "Warning min phy mbps is used\n");
294 }
295
296 /* Compute best pll parameters */
297 idf = 0;
298 ndiv = 0;
299 odf = 0;
300 ret = dsi_pll_get_params(dsi, pll_in_khz, pll_out_khz,
301 &idf, &ndiv, &odf);
302 if (ret) {
303 dev_err(dev, "Warning dsi_pll_get_params(): bad params\n");
304 return ret;
305 }
306
307 /* Get the adjusted pll out value */
308 pll_out_khz = dsi_pll_get_clkout_khz(pll_in_khz, idf, ndiv, odf);
309
310 /* Set the PLL division factors */
311 dsi_update_bits(dsi, DSI_WRPCR, WRPCR_NDIV | WRPCR_IDF | WRPCR_ODF,
312 (ndiv << 2) | (idf << 11) | ((ffs(odf) - 1) << 16));
313
314 /* Compute uix4 & set the bit period in high-speed mode */
315 val = 4000000 / pll_out_khz;
316 dsi_update_bits(dsi, DSI_WPCR0, WPCR0_UIX4, val);
317
318 /* Select video mode by resetting DSIM bit */
319 dsi_clear(dsi, DSI_WCFGR, WCFGR_DSIM);
320
321 /* Select the color coding */
322 dsi_update_bits(dsi, DSI_WCFGR, WCFGR_COLMUX,
323 dsi_color_from_mipi(format) << 1);
324
325 *lane_mbps = pll_out_khz / 1000;
326
327 debug("pll_in %ukHz pll_out %ukHz lane_mbps %uMHz\n",
328 pll_in_khz, pll_out_khz, *lane_mbps);
329
330 return 0;
331}
332
333static const struct mipi_dsi_phy_ops dsi_stm_phy_ops = {
334 .init = dsi_phy_init,
335 .get_lane_mbps = dsi_get_lane_mbps,
336 .post_set_mode = dsi_phy_post_set_mode,
337};
338
339static int stm32_dsi_attach(struct udevice *dev)
340{
341 struct stm32_dsi_priv *priv = dev_get_priv(dev);
342 struct mipi_dsi_device *device = &priv->device;
343 struct mipi_dsi_panel_plat *mplat;
344 struct display_timing timings;
345 int ret;
346
347 ret = uclass_first_device(UCLASS_PANEL, &priv->panel);
348 if (ret) {
349 dev_err(dev, "panel device error %d\n", ret);
350 return ret;
351 }
352
353 mplat = dev_get_platdata(priv->panel);
354 mplat->device = &priv->device;
355
356 ret = panel_get_display_timing(priv->panel, &timings);
357 if (ret) {
358 ret = fdtdec_decode_display_timing(gd->fdt_blob,
359 dev_of_offset(priv->panel),
360 0, &timings);
361 if (ret) {
362 dev_err(dev, "decode display timing error %d\n", ret);
363 return ret;
364 }
365 }
366
367 ret = uclass_get_device(UCLASS_DSI_HOST, 0, &priv->dsi_host);
368 if (ret) {
369 dev_err(dev, "No video dsi host detected %d\n", ret);
370 return ret;
371 }
372
373 ret = dsi_host_init(priv->dsi_host, device, &timings, 2,
374 &dsi_stm_phy_ops);
375 if (ret) {
376 dev_err(dev, "failed to initialize mipi dsi host\n");
377 return ret;
378 }
379
380 return 0;
381}
382
383static int stm32_dsi_set_backlight(struct udevice *dev, int percent)
384{
385 struct stm32_dsi_priv *priv = dev_get_priv(dev);
386 int ret;
387
388 ret = panel_enable_backlight(priv->panel);
389 if (ret) {
390 dev_err(dev, "panel %s enable backlight error %d\n",
391 priv->panel->name, ret);
392 return ret;
393 }
394
395 ret = dsi_host_enable(priv->dsi_host);
396 if (ret) {
397 dev_err(dev, "failed to enable mipi dsi host\n");
398 return ret;
399 }
400
401 return 0;
402}
403
Patrick Delaunay754815b2019-11-12 15:39:58 +0100404static int stm32_dsi_bind(struct udevice *dev)
405{
406 int ret;
407
408 ret = device_bind_driver_to_node(dev, "dw_mipi_dsi", "dsihost",
409 dev_ofnode(dev), NULL);
410 if (ret)
411 return ret;
412
413 return dm_scan_fdt_dev(dev);
414}
415
Yannick Fertré18b6ca52019-10-07 15:29:07 +0200416static int stm32_dsi_probe(struct udevice *dev)
417{
418 struct stm32_dsi_priv *priv = dev_get_priv(dev);
419 struct mipi_dsi_device *device = &priv->device;
420 struct reset_ctl rst;
421 struct clk clk;
422 int ret;
423
424 device->dev = dev;
425
426 priv->base = (void *)dev_read_addr(dev);
427 if ((fdt_addr_t)priv->base == FDT_ADDR_T_NONE) {
428 dev_err(dev, "dsi dt register address error\n");
429 return -EINVAL;
430 }
431
432 if (IS_ENABLED(CONFIG_DM_REGULATOR)) {
433 ret = device_get_supply_regulator(dev, "phy-dsi-supply",
434 &priv->vdd_reg);
435 if (ret && ret != -ENOENT) {
436 dev_err(dev, "Warning: cannot get phy dsi supply\n");
437 return -ENODEV;
438 }
439
440 if (ret != -ENOENT) {
441 ret = regulator_set_enable(priv->vdd_reg, true);
442 if (ret)
443 return ret;
444 }
445 }
446
447 ret = clk_get_by_name(device->dev, "pclk", &clk);
448 if (ret) {
449 dev_err(dev, "peripheral clock get error %d\n", ret);
450 goto err_reg;
451 }
452
453 ret = clk_enable(&clk);
454 if (ret) {
455 dev_err(dev, "peripheral clock enable error %d\n", ret);
456 goto err_reg;
457 }
458
459 ret = clk_get_by_name(dev, "ref", &clk);
460 if (ret) {
461 dev_err(dev, "pll reference clock get error %d\n", ret);
462 goto err_clk;
463 }
464
465 priv->pllref_clk = (unsigned int)clk_get_rate(&clk);
466
467 ret = reset_get_by_index(device->dev, 0, &rst);
468 if (ret) {
469 dev_err(dev, "missing dsi hardware reset\n");
470 goto err_clk;
471 }
472
473 /* Reset */
474 reset_deassert(&rst);
475
476 return 0;
477err_clk:
478 clk_disable(&clk);
479err_reg:
480 if (IS_ENABLED(CONFIG_DM_REGULATOR))
481 regulator_set_enable(priv->vdd_reg, false);
482
483 return ret;
484}
485
486struct video_bridge_ops stm32_dsi_ops = {
487 .attach = stm32_dsi_attach,
488 .set_backlight = stm32_dsi_set_backlight,
489};
490
491static const struct udevice_id stm32_dsi_ids[] = {
492 { .compatible = "st,stm32-dsi"},
493 { }
494};
495
496U_BOOT_DRIVER(stm32_dsi) = {
497 .name = "stm32-display-dsi",
498 .id = UCLASS_VIDEO_BRIDGE,
499 .of_match = stm32_dsi_ids,
Patrick Delaunay754815b2019-11-12 15:39:58 +0100500 .bind = stm32_dsi_bind,
Yannick Fertré18b6ca52019-10-07 15:29:07 +0200501 .probe = stm32_dsi_probe,
502 .ops = &stm32_dsi_ops,
503 .priv_auto_alloc_size = sizeof(struct stm32_dsi_priv),
504};