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