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