1 // SPDX-License-Identifier: GPL-2.0+
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.
7 * This MIPI DSI controller driver is based on the Linux Kernel driver from
8 * drivers/gpu/drm/stm/dw_mipi_dsi-stm.c.
20 #include <video_bridge.h>
22 #include <asm/arch/gpio.h>
23 #include <dm/device-internal.h>
24 #include <dm/device_compat.h>
26 #include <linux/bitops.h>
27 #include <linux/iopoll.h>
28 #include <power/regulator.h>
30 #define HWVER_130 0x31333000 /* IP version 1.30 */
31 #define HWVER_131 0x31333100 /* IP version 1.31 */
33 /* DSI digital registers & bit definitions */
34 #define DSI_VERSION 0x00
35 #define VERSION GENMASK(31, 8)
38 * DSI wrapper registers & bit definitions
39 * Note: registers are named as in the Reference Manual
41 #define DSI_WCFGR 0x0400 /* Wrapper ConFiGuration Reg */
42 #define WCFGR_DSIM BIT(0) /* DSI Mode */
43 #define WCFGR_COLMUX GENMASK(3, 1) /* COLor MUltipleXing */
45 #define DSI_WCR 0x0404 /* Wrapper Control Reg */
46 #define WCR_DSIEN BIT(3) /* DSI ENable */
48 #define DSI_WISR 0x040C /* Wrapper Interrupt and Status Reg */
49 #define WISR_PLLLS BIT(8) /* PLL Lock Status */
50 #define WISR_RRS BIT(12) /* Regulator Ready Status */
52 #define DSI_WPCR0 0x0418 /* Wrapper Phy Conf Reg 0 */
53 #define WPCR0_UIX4 GENMASK(5, 0) /* Unit Interval X 4 */
54 #define WPCR0_TDDL BIT(16) /* Turn Disable Data Lanes */
56 #define DSI_WRPCR 0x0430 /* Wrapper Regulator & Pll Ctrl Reg */
57 #define WRPCR_PLLEN BIT(0) /* PLL ENable */
58 #define WRPCR_NDIV GENMASK(8, 2) /* pll loop DIVision Factor */
59 #define WRPCR_IDF GENMASK(14, 11) /* pll Input Division Factor */
60 #define WRPCR_ODF GENMASK(17, 16) /* pll Output Division Factor */
61 #define WRPCR_REGEN BIT(24) /* REGulator ENable */
62 #define WRPCR_BGREN BIT(28) /* BandGap Reference ENable */
70 /* dsi color format coding according to the datasheet */
80 #define LANE_MIN_KBPS 31250
81 #define LANE_MAX_KBPS 500000
83 /* Timeout for regulator on/off, pll lock/unlock & fifo empty */
84 #define TIMEOUT_US 200000
86 struct stm32_dsi_priv {
87 struct mipi_dsi_device device;
89 struct udevice *panel;
94 struct udevice *vdd_reg;
95 struct udevice *dsi_host;
98 static inline void dsi_write(struct stm32_dsi_priv *dsi, u32 reg, u32 val)
100 writel(val, dsi->base + reg);
103 static inline u32 dsi_read(struct stm32_dsi_priv *dsi, u32 reg)
105 return readl(dsi->base + reg);
108 static inline void dsi_set(struct stm32_dsi_priv *dsi, u32 reg, u32 mask)
110 dsi_write(dsi, reg, dsi_read(dsi, reg) | mask);
113 static inline void dsi_clear(struct stm32_dsi_priv *dsi, u32 reg, u32 mask)
115 dsi_write(dsi, reg, dsi_read(dsi, reg) & ~mask);
118 static inline void dsi_update_bits(struct stm32_dsi_priv *dsi, u32 reg,
121 dsi_write(dsi, reg, (dsi_read(dsi, reg) & ~mask) | val);
124 static enum dsi_color dsi_color_from_mipi(u32 fmt)
127 case MIPI_DSI_FMT_RGB888:
129 case MIPI_DSI_FMT_RGB666:
130 return DSI_RGB666_CONF2;
131 case MIPI_DSI_FMT_RGB666_PACKED:
132 return DSI_RGB666_CONF1;
133 case MIPI_DSI_FMT_RGB565:
134 return DSI_RGB565_CONF1;
136 pr_err("MIPI color invalid, so we use rgb888\n");
141 static int dsi_pll_get_clkout_khz(int clkin_khz, int idf, int ndiv, int odf)
143 int divisor = idf * odf;
145 /* prevent from division by 0 */
149 return DIV_ROUND_CLOSEST(clkin_khz * ndiv, divisor);
152 static int dsi_pll_get_params(struct stm32_dsi_priv *dsi,
153 int clkin_khz, int clkout_khz,
154 int *idf, int *ndiv, int *odf)
156 int i, o, n, n_min, n_max;
157 int fvco_min, fvco_max, delta, best_delta; /* all in khz */
159 /* Early checks preventing division by 0 & odd results */
160 if (clkin_khz <= 0 || clkout_khz <= 0)
163 fvco_min = dsi->lane_min_kbps * 2 * ODF_MAX;
164 fvco_max = dsi->lane_max_kbps * 2 * ODF_MIN;
166 best_delta = 1000000; /* big started value (1000000khz) */
168 for (i = IDF_MIN; i <= IDF_MAX; i++) {
169 /* Compute ndiv range according to Fvco */
170 n_min = ((fvco_min * i) / (2 * clkin_khz)) + 1;
171 n_max = (fvco_max * i) / (2 * clkin_khz);
173 /* No need to continue idf loop if we reach ndiv max */
174 if (n_min >= NDIV_MAX)
177 /* Clamp ndiv to valid values */
178 if (n_min < NDIV_MIN)
180 if (n_max > NDIV_MAX)
183 for (o = ODF_MIN; o <= ODF_MAX; o *= 2) {
184 n = DIV_ROUND_CLOSEST(i * o * clkout_khz, clkin_khz);
185 /* Check ndiv according to vco range */
186 if (n < n_min || n > n_max)
188 /* Check if new delta is better & saves parameters */
189 delta = dsi_pll_get_clkout_khz(clkin_khz, i, n, o) -
193 if (delta < best_delta) {
199 /* fast return in case of "perfect result" */
208 static int dsi_phy_init(void *priv_data)
210 struct mipi_dsi_device *device = priv_data;
211 struct udevice *dev = device->dev;
212 struct stm32_dsi_priv *dsi = dev_get_priv(dev);
216 debug("Initialize DSI physical layer\n");
218 /* Enable the regulator */
219 dsi_set(dsi, DSI_WRPCR, WRPCR_REGEN | WRPCR_BGREN);
220 ret = readl_poll_timeout(dsi->base + DSI_WISR, val, val & WISR_RRS,
223 debug("!TIMEOUT! waiting REGU\n");
227 /* Enable the DSI PLL & wait for its lock */
228 dsi_set(dsi, DSI_WRPCR, WRPCR_PLLEN);
229 ret = readl_poll_timeout(dsi->base + DSI_WISR, val, val & WISR_PLLLS,
232 debug("!TIMEOUT! waiting PLL\n");
239 static void dsi_phy_post_set_mode(void *priv_data, unsigned long mode_flags)
241 struct mipi_dsi_device *device = priv_data;
242 struct udevice *dev = device->dev;
243 struct stm32_dsi_priv *dsi = dev_get_priv(dev);
245 debug("Set mode %p enable %ld\n", dsi,
246 mode_flags & MIPI_DSI_MODE_VIDEO);
252 * DSI wrapper must be enabled in video mode & disabled in command mode.
253 * If wrapper is enabled in command mode, the display controller
254 * register access will hang.
257 if (mode_flags & MIPI_DSI_MODE_VIDEO)
258 dsi_set(dsi, DSI_WCR, WCR_DSIEN);
260 dsi_clear(dsi, DSI_WCR, WCR_DSIEN);
263 static int dsi_get_lane_mbps(void *priv_data, struct display_timing *timings,
264 u32 lanes, u32 format, unsigned int *lane_mbps)
266 struct mipi_dsi_device *device = priv_data;
267 struct udevice *dev = device->dev;
268 struct stm32_dsi_priv *dsi = dev_get_priv(dev);
269 int idf, ndiv, odf, pll_in_khz, pll_out_khz;
273 /* Update lane capabilities according to hw version */
274 dsi->hw_version = dsi_read(dsi, DSI_VERSION) & VERSION;
275 dsi->lane_min_kbps = LANE_MIN_KBPS;
276 dsi->lane_max_kbps = LANE_MAX_KBPS;
277 if (dsi->hw_version == HWVER_131) {
278 dsi->lane_min_kbps *= 2;
279 dsi->lane_max_kbps *= 2;
282 pll_in_khz = dsi->pllref_clk / 1000;
284 /* Compute requested pll out */
285 bpp = mipi_dsi_pixel_format_to_bpp(format);
286 pll_out_khz = (timings->pixelclock.typ / 1000) * bpp / lanes;
287 /* Add 20% to pll out to be higher than pixel bw (burst mode only) */
288 pll_out_khz = (pll_out_khz * 12) / 10;
289 if (pll_out_khz > dsi->lane_max_kbps) {
290 pll_out_khz = dsi->lane_max_kbps;
291 dev_warn(dev, "Warning max phy mbps is used\n");
293 if (pll_out_khz < dsi->lane_min_kbps) {
294 pll_out_khz = dsi->lane_min_kbps;
295 dev_warn(dev, "Warning min phy mbps is used\n");
298 /* Compute best pll parameters */
302 ret = dsi_pll_get_params(dsi, pll_in_khz, pll_out_khz,
305 dev_err(dev, "Warning dsi_pll_get_params(): bad params\n");
309 /* Get the adjusted pll out value */
310 pll_out_khz = dsi_pll_get_clkout_khz(pll_in_khz, idf, ndiv, odf);
312 /* Set the PLL division factors */
313 dsi_update_bits(dsi, DSI_WRPCR, WRPCR_NDIV | WRPCR_IDF | WRPCR_ODF,
314 (ndiv << 2) | (idf << 11) | ((ffs(odf) - 1) << 16));
316 /* Compute uix4 & set the bit period in high-speed mode */
317 val = 4000000 / pll_out_khz;
318 dsi_update_bits(dsi, DSI_WPCR0, WPCR0_UIX4, val);
320 /* Select video mode by resetting DSIM bit */
321 dsi_clear(dsi, DSI_WCFGR, WCFGR_DSIM);
323 /* Select the color coding */
324 dsi_update_bits(dsi, DSI_WCFGR, WCFGR_COLMUX,
325 dsi_color_from_mipi(format) << 1);
327 *lane_mbps = pll_out_khz / 1000;
329 debug("pll_in %ukHz pll_out %ukHz lane_mbps %uMHz\n",
330 pll_in_khz, pll_out_khz, *lane_mbps);
335 static const struct mipi_dsi_phy_ops dsi_stm_phy_ops = {
336 .init = dsi_phy_init,
337 .get_lane_mbps = dsi_get_lane_mbps,
338 .post_set_mode = dsi_phy_post_set_mode,
341 static int stm32_dsi_attach(struct udevice *dev)
343 struct stm32_dsi_priv *priv = dev_get_priv(dev);
344 struct mipi_dsi_device *device = &priv->device;
345 struct mipi_dsi_panel_plat *mplat;
346 struct display_timing timings;
349 ret = uclass_first_device(UCLASS_PANEL, &priv->panel);
351 dev_err(dev, "panel device error %d\n", ret);
355 mplat = dev_get_platdata(priv->panel);
356 mplat->device = &priv->device;
358 ret = panel_get_display_timing(priv->panel, &timings);
360 ret = fdtdec_decode_display_timing(gd->fdt_blob,
361 dev_of_offset(priv->panel),
364 dev_err(dev, "decode display timing error %d\n", ret);
369 ret = uclass_get_device(UCLASS_DSI_HOST, 0, &priv->dsi_host);
371 dev_err(dev, "No video dsi host detected %d\n", ret);
375 ret = dsi_host_init(priv->dsi_host, device, &timings, 2,
378 dev_err(dev, "failed to initialize mipi dsi host\n");
385 static int stm32_dsi_set_backlight(struct udevice *dev, int percent)
387 struct stm32_dsi_priv *priv = dev_get_priv(dev);
390 ret = panel_enable_backlight(priv->panel);
392 dev_err(dev, "panel %s enable backlight error %d\n",
393 priv->panel->name, ret);
397 ret = dsi_host_enable(priv->dsi_host);
399 dev_err(dev, "failed to enable mipi dsi host\n");
406 static int stm32_dsi_bind(struct udevice *dev)
410 ret = device_bind_driver_to_node(dev, "dw_mipi_dsi", "dsihost",
411 dev_ofnode(dev), NULL);
415 return dm_scan_fdt_dev(dev);
418 static int stm32_dsi_probe(struct udevice *dev)
420 struct stm32_dsi_priv *priv = dev_get_priv(dev);
421 struct mipi_dsi_device *device = &priv->device;
422 struct reset_ctl rst;
428 priv->base = (void *)dev_read_addr(dev);
429 if ((fdt_addr_t)priv->base == FDT_ADDR_T_NONE) {
430 dev_err(dev, "dsi dt register address error\n");
434 if (IS_ENABLED(CONFIG_DM_REGULATOR)) {
435 ret = device_get_supply_regulator(dev, "phy-dsi-supply",
437 if (ret && ret != -ENOENT) {
438 dev_err(dev, "Warning: cannot get phy dsi supply\n");
442 if (ret != -ENOENT) {
443 ret = regulator_set_enable(priv->vdd_reg, true);
449 ret = clk_get_by_name(device->dev, "pclk", &clk);
451 dev_err(dev, "peripheral clock get error %d\n", ret);
455 ret = clk_enable(&clk);
457 dev_err(dev, "peripheral clock enable error %d\n", ret);
461 ret = clk_get_by_name(dev, "ref", &clk);
463 dev_err(dev, "pll reference clock get error %d\n", ret);
467 priv->pllref_clk = (unsigned int)clk_get_rate(&clk);
469 ret = reset_get_by_index(device->dev, 0, &rst);
471 dev_err(dev, "missing dsi hardware reset\n");
476 reset_deassert(&rst);
482 if (IS_ENABLED(CONFIG_DM_REGULATOR))
483 regulator_set_enable(priv->vdd_reg, false);
488 struct video_bridge_ops stm32_dsi_ops = {
489 .attach = stm32_dsi_attach,
490 .set_backlight = stm32_dsi_set_backlight,
493 static const struct udevice_id stm32_dsi_ids[] = {
494 { .compatible = "st,stm32-dsi"},
498 U_BOOT_DRIVER(stm32_dsi) = {
499 .name = "stm32-display-dsi",
500 .id = UCLASS_VIDEO_BRIDGE,
501 .of_match = stm32_dsi_ids,
502 .bind = stm32_dsi_bind,
503 .probe = stm32_dsi_probe,
504 .ops = &stm32_dsi_ops,
505 .priv_auto_alloc_size = sizeof(struct stm32_dsi_priv),