blob: 9d5abacc2bff784b28fb00b2531c8aa5be643369 [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Yannick Fertré18b6ca52019-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 Glass336d4612020-02-03 07:36:16 -070024#include <dm/device_compat.h>
Patrick Delaunay754815b2019-11-12 15:39:58 +010025#include <dm/lists.h>
Simon Glasscd93d622020-05-10 11:40:13 -060026#include <linux/bitops.h>
Yannick Fertré18b6ca52019-10-07 15:29:07 +020027#include <linux/iopoll.h>
28#include <power/regulator.h>
29
30#define HWVER_130 0x31333000 /* IP version 1.30 */
31#define HWVER_131 0x31333100 /* IP version 1.31 */
32
33/* DSI digital registers & bit definitions */
34#define DSI_VERSION 0x00
35#define VERSION GENMASK(31, 8)
36
37/*
38 * DSI wrapper registers & bit definitions
39 * Note: registers are named as in the Reference Manual
40 */
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 */
44
45#define DSI_WCR 0x0404 /* Wrapper Control Reg */
46#define WCR_DSIEN BIT(3) /* DSI ENable */
47
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 */
51
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 */
55
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 */
63#define IDF_MIN 1
64#define IDF_MAX 7
65#define NDIV_MIN 10
66#define NDIV_MAX 125
67#define ODF_MIN 1
68#define ODF_MAX 8
69
70/* dsi color format coding according to the datasheet */
71enum dsi_color {
72 DSI_RGB565_CONF1,
73 DSI_RGB565_CONF2,
74 DSI_RGB565_CONF3,
75 DSI_RGB666_CONF1,
76 DSI_RGB666_CONF2,
77 DSI_RGB888,
78};
79
80#define LANE_MIN_KBPS 31250
81#define LANE_MAX_KBPS 500000
82
83/* Timeout for regulator on/off, pll lock/unlock & fifo empty */
84#define TIMEOUT_US 200000
85
86struct stm32_dsi_priv {
87 struct mipi_dsi_device device;
88 void __iomem *base;
89 struct udevice *panel;
90 u32 pllref_clk;
91 u32 hw_version;
92 int lane_min_kbps;
93 int lane_max_kbps;
94 struct udevice *vdd_reg;
95 struct udevice *dsi_host;
96};
97
98static inline void dsi_write(struct stm32_dsi_priv *dsi, u32 reg, u32 val)
99{
100 writel(val, dsi->base + reg);
101}
102
103static inline u32 dsi_read(struct stm32_dsi_priv *dsi, u32 reg)
104{
105 return readl(dsi->base + reg);
106}
107
108static inline void dsi_set(struct stm32_dsi_priv *dsi, u32 reg, u32 mask)
109{
110 dsi_write(dsi, reg, dsi_read(dsi, reg) | mask);
111}
112
113static inline void dsi_clear(struct stm32_dsi_priv *dsi, u32 reg, u32 mask)
114{
115 dsi_write(dsi, reg, dsi_read(dsi, reg) & ~mask);
116}
117
118static inline void dsi_update_bits(struct stm32_dsi_priv *dsi, u32 reg,
119 u32 mask, u32 val)
120{
121 dsi_write(dsi, reg, (dsi_read(dsi, reg) & ~mask) | val);
122}
123
124static enum dsi_color dsi_color_from_mipi(u32 fmt)
125{
126 switch (fmt) {
127 case MIPI_DSI_FMT_RGB888:
128 return DSI_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;
135 default:
136 pr_err("MIPI color invalid, so we use rgb888\n");
137 }
138 return DSI_RGB888;
139}
140
141static int dsi_pll_get_clkout_khz(int clkin_khz, int idf, int ndiv, int odf)
142{
143 int divisor = idf * odf;
144
145 /* prevent from division by 0 */
146 if (!divisor)
147 return 0;
148
149 return DIV_ROUND_CLOSEST(clkin_khz * ndiv, divisor);
150}
151
152static int dsi_pll_get_params(struct stm32_dsi_priv *dsi,
153 int clkin_khz, int clkout_khz,
154 int *idf, int *ndiv, int *odf)
155{
156 int i, o, n, n_min, n_max;
157 int fvco_min, fvco_max, delta, best_delta; /* all in khz */
158
159 /* Early checks preventing division by 0 & odd results */
160 if (clkin_khz <= 0 || clkout_khz <= 0)
161 return -EINVAL;
162
163 fvco_min = dsi->lane_min_kbps * 2 * ODF_MAX;
164 fvco_max = dsi->lane_max_kbps * 2 * ODF_MIN;
165
166 best_delta = 1000000; /* big started value (1000000khz) */
167
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);
172
173 /* No need to continue idf loop if we reach ndiv max */
174 if (n_min >= NDIV_MAX)
175 break;
176
177 /* Clamp ndiv to valid values */
178 if (n_min < NDIV_MIN)
179 n_min = NDIV_MIN;
180 if (n_max > NDIV_MAX)
181 n_max = NDIV_MAX;
182
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)
187 continue;
188 /* Check if new delta is better & saves parameters */
189 delta = dsi_pll_get_clkout_khz(clkin_khz, i, n, o) -
190 clkout_khz;
191 if (delta < 0)
192 delta = -delta;
193 if (delta < best_delta) {
194 *idf = i;
195 *ndiv = n;
196 *odf = o;
197 best_delta = delta;
198 }
199 /* fast return in case of "perfect result" */
200 if (!delta)
201 return 0;
202 }
203 }
204
205 return 0;
206}
207
208static int dsi_phy_init(void *priv_data)
209{
210 struct mipi_dsi_device *device = priv_data;
211 struct udevice *dev = device->dev;
212 struct stm32_dsi_priv *dsi = dev_get_priv(dev);
213 u32 val;
214 int ret;
215
216 debug("Initialize DSI physical layer\n");
217
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,
221 TIMEOUT_US);
222 if (ret) {
223 debug("!TIMEOUT! waiting REGU\n");
224 return ret;
225 }
226
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,
230 TIMEOUT_US);
231 if (ret) {
232 debug("!TIMEOUT! waiting PLL\n");
233 return ret;
234 }
235
236 return 0;
237}
238
239static void dsi_phy_post_set_mode(void *priv_data, unsigned long mode_flags)
240{
241 struct mipi_dsi_device *device = priv_data;
242 struct udevice *dev = device->dev;
243 struct stm32_dsi_priv *dsi = dev_get_priv(dev);
244
245 debug("Set mode %p enable %ld\n", dsi,
246 mode_flags & MIPI_DSI_MODE_VIDEO);
247
248 if (!dsi)
249 return;
250
251 /*
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.
255 */
256
257 if (mode_flags & MIPI_DSI_MODE_VIDEO)
258 dsi_set(dsi, DSI_WCR, WCR_DSIEN);
259 else
260 dsi_clear(dsi, DSI_WCR, WCR_DSIEN);
261}
262
263static int dsi_get_lane_mbps(void *priv_data, struct display_timing *timings,
264 u32 lanes, u32 format, unsigned int *lane_mbps)
265{
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;
270 int ret, bpp;
271 u32 val;
272
273 /* Update lane capabilities according to hw version */
Yannick Fertré18b6ca52019-10-07 15:29:07 +0200274 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;
Yannick Fertre7084dd82020-06-24 10:45:40 +0200356 device->lanes = mplat->lanes;
357 device->format = mplat->format;
358 device->mode_flags = mplat->mode_flags;
Yannick Fertré18b6ca52019-10-07 15:29:07 +0200359
360 ret = panel_get_display_timing(priv->panel, &timings);
361 if (ret) {
Patrick Delaunaya04616b2020-09-09 17:44:13 +0200362 ret = ofnode_decode_display_timing(dev_ofnode(priv->panel),
Yannick Fertré18b6ca52019-10-07 15:29:07 +0200363 0, &timings);
364 if (ret) {
365 dev_err(dev, "decode display timing error %d\n", ret);
366 return ret;
367 }
368 }
369
370 ret = uclass_get_device(UCLASS_DSI_HOST, 0, &priv->dsi_host);
371 if (ret) {
372 dev_err(dev, "No video dsi host detected %d\n", ret);
373 return ret;
374 }
375
376 ret = dsi_host_init(priv->dsi_host, device, &timings, 2,
377 &dsi_stm_phy_ops);
378 if (ret) {
379 dev_err(dev, "failed to initialize mipi dsi host\n");
380 return ret;
381 }
382
383 return 0;
384}
385
386static int stm32_dsi_set_backlight(struct udevice *dev, int percent)
387{
388 struct stm32_dsi_priv *priv = dev_get_priv(dev);
389 int ret;
390
391 ret = panel_enable_backlight(priv->panel);
392 if (ret) {
393 dev_err(dev, "panel %s enable backlight error %d\n",
394 priv->panel->name, ret);
395 return ret;
396 }
397
398 ret = dsi_host_enable(priv->dsi_host);
399 if (ret) {
400 dev_err(dev, "failed to enable mipi dsi host\n");
401 return ret;
402 }
403
404 return 0;
405}
406
Patrick Delaunay754815b2019-11-12 15:39:58 +0100407static int stm32_dsi_bind(struct udevice *dev)
408{
409 int ret;
410
411 ret = device_bind_driver_to_node(dev, "dw_mipi_dsi", "dsihost",
412 dev_ofnode(dev), NULL);
413 if (ret)
414 return ret;
415
416 return dm_scan_fdt_dev(dev);
417}
418
Yannick Fertré18b6ca52019-10-07 15:29:07 +0200419static int stm32_dsi_probe(struct udevice *dev)
420{
421 struct stm32_dsi_priv *priv = dev_get_priv(dev);
422 struct mipi_dsi_device *device = &priv->device;
423 struct reset_ctl rst;
424 struct clk clk;
425 int ret;
426
427 device->dev = dev;
428
429 priv->base = (void *)dev_read_addr(dev);
430 if ((fdt_addr_t)priv->base == FDT_ADDR_T_NONE) {
431 dev_err(dev, "dsi dt register address error\n");
432 return -EINVAL;
433 }
434
435 if (IS_ENABLED(CONFIG_DM_REGULATOR)) {
436 ret = device_get_supply_regulator(dev, "phy-dsi-supply",
437 &priv->vdd_reg);
438 if (ret && ret != -ENOENT) {
439 dev_err(dev, "Warning: cannot get phy dsi supply\n");
440 return -ENODEV;
441 }
442
443 if (ret != -ENOENT) {
444 ret = regulator_set_enable(priv->vdd_reg, true);
445 if (ret)
446 return ret;
447 }
448 }
449
450 ret = clk_get_by_name(device->dev, "pclk", &clk);
451 if (ret) {
452 dev_err(dev, "peripheral clock get error %d\n", ret);
453 goto err_reg;
454 }
455
456 ret = clk_enable(&clk);
457 if (ret) {
458 dev_err(dev, "peripheral clock enable error %d\n", ret);
459 goto err_reg;
460 }
461
462 ret = clk_get_by_name(dev, "ref", &clk);
463 if (ret) {
464 dev_err(dev, "pll reference clock get error %d\n", ret);
465 goto err_clk;
466 }
467
468 priv->pllref_clk = (unsigned int)clk_get_rate(&clk);
469
470 ret = reset_get_by_index(device->dev, 0, &rst);
471 if (ret) {
472 dev_err(dev, "missing dsi hardware reset\n");
473 goto err_clk;
474 }
475
476 /* Reset */
477 reset_deassert(&rst);
478
Yannick Fertre670eda32020-06-24 10:43:59 +0200479 /* check hardware version */
480 priv->hw_version = dsi_read(priv, DSI_VERSION) & VERSION;
481 if (priv->hw_version != HWVER_130 &&
482 priv->hw_version != HWVER_131) {
483 dev_err(dev, "DSI version 0x%x not supported\n", priv->hw_version);
484 ret = -ENODEV;
485 goto err_clk;
486 }
487
Yannick Fertré18b6ca52019-10-07 15:29:07 +0200488 return 0;
489err_clk:
490 clk_disable(&clk);
491err_reg:
492 if (IS_ENABLED(CONFIG_DM_REGULATOR))
493 regulator_set_enable(priv->vdd_reg, false);
494
495 return ret;
496}
497
498struct video_bridge_ops stm32_dsi_ops = {
499 .attach = stm32_dsi_attach,
500 .set_backlight = stm32_dsi_set_backlight,
501};
502
503static const struct udevice_id stm32_dsi_ids[] = {
504 { .compatible = "st,stm32-dsi"},
505 { }
506};
507
508U_BOOT_DRIVER(stm32_dsi) = {
509 .name = "stm32-display-dsi",
510 .id = UCLASS_VIDEO_BRIDGE,
511 .of_match = stm32_dsi_ids,
Patrick Delaunay754815b2019-11-12 15:39:58 +0100512 .bind = stm32_dsi_bind,
Yannick Fertré18b6ca52019-10-07 15:29:07 +0200513 .probe = stm32_dsi_probe,
514 .ops = &stm32_dsi_ops,
515 .priv_auto_alloc_size = sizeof(struct stm32_dsi_priv),
516};