blob: 358004f30f2eb274a0c6ea58640c51e35b9d6b7c [file] [log] [blame]
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2020 Texas Instruments Incorporated
4 * Copyright (C) 2022 Svyatoslav Ryhel <clamor95@gmail.com>
5 */
6
7#include <clk.h>
8#include <dm.h>
Svyatoslav Ryhela7ae9882025-02-21 13:29:43 +02009#include <dm/ofnode_graph.h>
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +020010#include <i2c.h>
11#include <log.h>
12#include <mipi_display.h>
13#include <mipi_dsi.h>
14#include <backlight.h>
15#include <panel.h>
Svyatoslav Ryhel73091cc2025-02-14 15:28:28 +020016#include <video_bridge.h>
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +020017#include <linux/delay.h>
18#include <linux/err.h>
19#include <linux/kernel.h>
20#include <linux/math64.h>
21#include <power/regulator.h>
22
23#include <asm/gpio.h>
24
25/* Global (16-bit addressable) */
26#define TC358768_CHIPID 0x0000
27#define TC358768_SYSCTL 0x0002
28#define TC358768_CONFCTL 0x0004
29#define TC358768_VSDLY 0x0006
30#define TC358768_DATAFMT 0x0008
31#define TC358768_GPIOEN 0x000E
32#define TC358768_GPIODIR 0x0010
33#define TC358768_GPIOIN 0x0012
34#define TC358768_GPIOOUT 0x0014
35#define TC358768_PLLCTL0 0x0016
36#define TC358768_PLLCTL1 0x0018
37#define TC358768_CMDBYTE 0x0022
38#define TC358768_PP_MISC 0x0032
39#define TC358768_DSITX_DT 0x0050
40#define TC358768_FIFOSTATUS 0x00F8
41
42/* Debug (16-bit addressable) */
43#define TC358768_VBUFCTRL 0x00E0
44#define TC358768_DBG_WIDTH 0x00E2
45#define TC358768_DBG_VBLANK 0x00E4
46#define TC358768_DBG_DATA 0x00E8
47
48/* TX PHY (32-bit addressable) */
49#define TC358768_CLW_DPHYCONTTX 0x0100
50#define TC358768_D0W_DPHYCONTTX 0x0104
51#define TC358768_D1W_DPHYCONTTX 0x0108
52#define TC358768_D2W_DPHYCONTTX 0x010C
53#define TC358768_D3W_DPHYCONTTX 0x0110
54#define TC358768_CLW_CNTRL 0x0140
55#define TC358768_D0W_CNTRL 0x0144
56#define TC358768_D1W_CNTRL 0x0148
57#define TC358768_D2W_CNTRL 0x014C
58#define TC358768_D3W_CNTRL 0x0150
59
60/* TX PPI (32-bit addressable) */
61#define TC358768_STARTCNTRL 0x0204
62#define TC358768_DSITXSTATUS 0x0208
63#define TC358768_LINEINITCNT 0x0210
64#define TC358768_LPTXTIMECNT 0x0214
65#define TC358768_TCLK_HEADERCNT 0x0218
66#define TC358768_TCLK_TRAILCNT 0x021C
67#define TC358768_THS_HEADERCNT 0x0220
68#define TC358768_TWAKEUP 0x0224
69#define TC358768_TCLK_POSTCNT 0x0228
70#define TC358768_THS_TRAILCNT 0x022C
71#define TC358768_HSTXVREGCNT 0x0230
72#define TC358768_HSTXVREGEN 0x0234
73#define TC358768_TXOPTIONCNTRL 0x0238
74#define TC358768_BTACNTRL1 0x023C
75
76/* TX CTRL (32-bit addressable) */
77#define TC358768_DSI_CONTROL 0x040C
78#define TC358768_DSI_STATUS 0x0410
79#define TC358768_DSI_INT 0x0414
80#define TC358768_DSI_INT_ENA 0x0418
81#define TC358768_DSICMD_RDFIFO 0x0430
82#define TC358768_DSI_ACKERR 0x0434
83#define TC358768_DSI_ACKERR_INTENA 0x0438
84#define TC358768_DSI_ACKERR_HALT 0x043c
85#define TC358768_DSI_RXERR 0x0440
86#define TC358768_DSI_RXERR_INTENA 0x0444
87#define TC358768_DSI_RXERR_HALT 0x0448
88#define TC358768_DSI_ERR 0x044C
89#define TC358768_DSI_ERR_INTENA 0x0450
90#define TC358768_DSI_ERR_HALT 0x0454
91#define TC358768_DSI_CONFW 0x0500
92#define TC358768_DSI_LPCMD 0x0500
93#define TC358768_DSI_RESET 0x0504
94#define TC358768_DSI_INT_CLR 0x050C
95#define TC358768_DSI_START 0x0518
96
97/* DSITX CTRL (16-bit addressable) */
98#define TC358768_DSICMD_TX 0x0600
99#define TC358768_DSICMD_TYPE 0x0602
100#define TC358768_DSICMD_WC 0x0604
101#define TC358768_DSICMD_WD0 0x0610
102#define TC358768_DSICMD_WD1 0x0612
103#define TC358768_DSICMD_WD2 0x0614
104#define TC358768_DSICMD_WD3 0x0616
105#define TC358768_DSI_EVENT 0x0620
106#define TC358768_DSI_VSW 0x0622
107#define TC358768_DSI_VBPR 0x0624
108#define TC358768_DSI_VACT 0x0626
109#define TC358768_DSI_HSW 0x0628
110#define TC358768_DSI_HBPR 0x062A
111#define TC358768_DSI_HACT 0x062C
112
113/* TC358768_DSI_CONTROL (0x040C) register */
114#define TC358768_DSI_CONTROL_DIS_MODE BIT(15)
115#define TC358768_DSI_CONTROL_TXMD BIT(7)
116#define TC358768_DSI_CONTROL_HSCKMD BIT(5)
117#define TC358768_DSI_CONTROL_EOTDIS BIT(0)
118
119/* TC358768_DSI_CONFW (0x0500) register */
120#define TC358768_DSI_CONFW_MODE_SET (5 << 29)
121#define TC358768_DSI_CONFW_MODE_CLR (6 << 29)
122#define TC358768_DSI_CONFW_ADDR_DSI_CONTROL (3 << 24)
123
124#define NANO 1000000000UL
125#define PICO 1000000000000ULL
126
Svyatoslav Ryhel1aee4132025-02-23 11:39:13 +0200127static const char * const tc358768_supplies[] = {
128 "vddc-supply", "vddmipi-supply", "vddio-supply"
129};
130
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200131struct tc358768_priv {
132 struct mipi_dsi_host host;
133 struct mipi_dsi_device device;
134
135 struct udevice *panel;
136 struct display_timing timing;
137
Svyatoslav Ryhel1aee4132025-02-23 11:39:13 +0200138 struct udevice *supplies[ARRAY_SIZE(tc358768_supplies)];
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200139
140 struct clk *refclk;
141
142 struct gpio_desc reset_gpio;
143
144 u32 pd_lines; /* number of Parallel Port Input Data Lines */
145 u32 dsi_lanes; /* number of DSI Lanes */
146
147 /* Parameters for PLL programming */
148 u32 fbd; /* PLL feedback divider */
149 u32 prd; /* PLL input divider */
150 u32 frs; /* PLL Freqency range for HSCK (post divider) */
151
152 u32 dsiclk; /* pll_clk / 2 */
153};
154
155static void tc358768_read(struct udevice *dev, u32 reg, u32 *val)
156{
157 int count;
158 u8 buf[4] = { 0, 0, 0, 0 };
159
160 /* 16-bit register? */
161 if (reg < 0x100 || reg >= 0x600)
162 count = 2;
163 else
164 count = 4;
165
166 dm_i2c_read(dev, reg, buf, count);
167 *val = (buf[0] << 8) | (buf[1] & 0xff) |
168 (buf[2] << 24) | (buf[3] << 16);
169
170 log_debug("%s 0x%04x >> 0x%08x\n",
171 __func__, reg, *val);
172}
173
174static void tc358768_write(struct udevice *dev, u32 reg, u32 val)
175{
176 int count;
177 u8 buf[4];
178
179 /* 16-bit register? */
180 if (reg < 0x100 || reg >= 0x600)
181 count = 2;
182 else
183 count = 4;
184
185 buf[0] = val >> 8;
186 buf[1] = val & 0xff;
187 buf[2] = val >> 24;
188 buf[3] = val >> 16;
189
190 log_debug("%s 0x%04x << 0x%08x\n",
191 __func__, reg, val);
192
193 dm_i2c_write(dev, reg, buf, count);
194}
195
196static void tc358768_update_bits(struct udevice *dev, u32 reg, u32 mask,
197 u32 val)
198{
199 u32 tmp, orig;
200
201 tc358768_read(dev, reg, &orig);
202
203 tmp = orig & ~mask;
204 tmp |= val & mask;
205 if (tmp != orig)
206 tc358768_write(dev, reg, tmp);
207}
208
209static ssize_t tc358768_dsi_host_transfer(struct mipi_dsi_host *host,
210 const struct mipi_dsi_msg *msg)
211{
212 struct udevice *dev = (struct udevice *)host->dev;
213 struct mipi_dsi_packet packet;
214 int ret;
215
216 if (msg->rx_len) {
217 log_debug("%s: MIPI rx is not supported\n", __func__);
218 return -EOPNOTSUPP;
219 }
220
221 if (msg->tx_len > 8) {
222 log_debug("%s: Maximum 8 byte MIPI tx is supported\n", __func__);
223 return -EOPNOTSUPP;
224 }
225
226 ret = mipi_dsi_create_packet(&packet, msg);
227 if (ret)
228 return ret;
229
230 if (mipi_dsi_packet_format_is_short(msg->type)) {
231 tc358768_write(dev, TC358768_DSICMD_TYPE,
232 (0x10 << 8) | (packet.header[0] & 0x3f));
233 tc358768_write(dev, TC358768_DSICMD_WC, 0);
234 tc358768_write(dev, TC358768_DSICMD_WD0,
235 (packet.header[2] << 8) | packet.header[1]);
236 } else {
237 int i;
238
239 tc358768_write(dev, TC358768_DSICMD_TYPE,
240 (0x40 << 8) | (packet.header[0] & 0x3f));
241 tc358768_write(dev, TC358768_DSICMD_WC, packet.payload_length);
242 for (i = 0; i < packet.payload_length; i += 2) {
243 u16 val = packet.payload[i];
244
245 if (i + 1 < packet.payload_length)
246 val |= packet.payload[i + 1] << 8;
247
248 tc358768_write(dev, TC358768_DSICMD_WD0 + i, val);
249 }
250 }
251
252 /* start transfer */
253 tc358768_write(dev, TC358768_DSICMD_TX, 1);
254
255 return packet.size;
256}
257
258static const struct mipi_dsi_host_ops tc358768_dsi_host_ops = {
259 .transfer = tc358768_dsi_host_transfer,
260};
261
262static void tc358768_sw_reset(struct udevice *dev)
263{
264 /* Assert Reset */
265 tc358768_write(dev, TC358768_SYSCTL, 1);
266 mdelay(5);
267
268 /* Release Reset, Exit Sleep */
269 tc358768_write(dev, TC358768_SYSCTL, 0);
270}
271
Svyatoslav Ryhel73091cc2025-02-14 15:28:28 +0200272static void tc358768_hw_enable(struct udevice *dev)
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200273{
Svyatoslav Ryhel73091cc2025-02-14 15:28:28 +0200274 struct tc358768_priv *priv = dev_get_priv(dev);
275 struct video_bridge_priv *uc_priv = dev_get_uclass_priv(dev);
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200276 int ret;
277
278 ret = clk_prepare_enable(priv->refclk);
279 if (ret)
280 log_debug("%s: error enabling refclk (%d)\n", __func__, ret);
281
Svyatoslav Ryhel1aee4132025-02-23 11:39:13 +0200282 ret = regulator_set_enable_if_allowed(priv->supplies[0], true);
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200283 if (ret)
284 log_debug("%s: error enabling vddc (%d)\n", __func__, ret);
285
Svyatoslav Ryhel1aee4132025-02-23 11:39:13 +0200286 ret = regulator_set_enable_if_allowed(priv->supplies[1], true);
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200287 if (ret)
288 log_debug("%s: error enabling vddmipi (%d)\n", __func__, ret);
289
290 mdelay(10);
291
Svyatoslav Ryhel1aee4132025-02-23 11:39:13 +0200292 ret = regulator_set_enable_if_allowed(priv->supplies[2], true);
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200293 if (ret)
294 log_debug("%s: error enabling vddio (%d)\n", __func__, ret);
295
296 mdelay(2);
297
298 /*
299 * The RESX is active low (GPIO_ACTIVE_LOW).
300 * DEASSERT (value = 0) the reset_gpio to enable the chip
301 */
Svyatoslav Ryhel73091cc2025-02-14 15:28:28 +0200302 ret = dm_gpio_set_value(&uc_priv->reset, 0);
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200303 if (ret)
304 log_debug("%s: error changing reset-gpio (%d)\n", __func__, ret);
305
306 /* wait for encoder clocks to stabilize */
307 mdelay(2);
308}
309
310static u32 tc358768_pclk_to_pll(struct tc358768_priv *priv, u32 pclk)
311{
312 return (u32)div_u64((u64)pclk * priv->pd_lines, priv->dsi_lanes);
313}
314
315static int tc358768_calc_pll(struct tc358768_priv *priv,
316 struct display_timing *dt)
317{
318 static const u32 frs_limits[] = {
319 1000000000,
320 500000000,
321 250000000,
322 125000000,
323 62500000
324 };
325 unsigned long refclk;
326 u32 prd, target_pll, i, max_pll, min_pll;
327 u32 frs, best_diff, best_pll, best_prd, best_fbd;
328
329 target_pll = tc358768_pclk_to_pll(priv, dt->pixelclock.typ);
330
331 /* pll_clk = RefClk * FBD / PRD * (1 / (2^FRS)) */
332
333 for (i = 0; i < ARRAY_SIZE(frs_limits); i++)
334 if (target_pll >= frs_limits[i])
335 break;
336
337 if (i == ARRAY_SIZE(frs_limits) || i == 0)
338 return -EINVAL;
339
340 frs = i - 1;
341 max_pll = frs_limits[i - 1];
342 min_pll = frs_limits[i];
343
344 refclk = clk_get_rate(priv->refclk);
345
346 best_diff = UINT_MAX;
347 best_pll = 0;
348 best_prd = 0;
349 best_fbd = 0;
350
351 for (prd = 1; prd <= 16; ++prd) {
352 u32 divisor = prd * (1 << frs);
353 u32 fbd;
354
355 for (fbd = 1; fbd <= 512; ++fbd) {
356 u32 pll, diff, pll_in;
357
358 pll = (u32)div_u64((u64)refclk * fbd, divisor);
359
360 if (pll >= max_pll || pll < min_pll)
361 continue;
362
363 pll_in = (u32)div_u64((u64)refclk, prd);
364 if (pll_in < 4000000)
365 continue;
366
367 diff = max(pll, target_pll) - min(pll, target_pll);
368
369 if (diff < best_diff) {
370 best_diff = diff;
371 best_pll = pll;
372 best_prd = prd;
373 best_fbd = fbd;
374
375 if (best_diff == 0)
376 goto found;
377 }
378 }
379 }
380
381 if (best_diff == UINT_MAX) {
382 log_debug("%s: could not find suitable PLL setup\n", __func__);
383 return -EINVAL;
384 }
385
386found:
387 priv->fbd = best_fbd;
388 priv->prd = best_prd;
389 priv->frs = frs;
390 priv->dsiclk = best_pll / 2;
391
392 return 0;
393}
394
395static void tc358768_setup_pll(struct udevice *dev)
396{
397 struct tc358768_priv *priv = dev_get_priv(dev);
398 u32 fbd, prd, frs;
399 int ret;
400
401 ret = tc358768_calc_pll(priv, &priv->timing);
402 if (ret)
403 log_debug("%s: PLL calculation failed: %d\n", __func__, ret);
404
405 fbd = priv->fbd;
406 prd = priv->prd;
407 frs = priv->frs;
408
409 log_debug("%s: PLL: refclk %lu, fbd %u, prd %u, frs %u\n", __func__,
410 clk_get_rate(priv->refclk), fbd, prd, frs);
411 log_debug("%s: PLL: pll_clk: %u, DSIClk %u, HSByteClk %u\n", __func__,
412 priv->dsiclk * 2, priv->dsiclk, priv->dsiclk / 4);
413
414 /* PRD[15:12] FBD[8:0] */
415 tc358768_write(dev, TC358768_PLLCTL0, ((prd - 1) << 12) | (fbd - 1));
416
417 /* FRS[11:10] LBWS[9:8] CKEN[4] RESETB[1] EN[0] */
418 tc358768_write(dev, TC358768_PLLCTL1,
419 (frs << 10) | (0x2 << 8) | BIT(1) | BIT(0));
420
421 /* wait for lock */
422 mdelay(5);
423
424 /* FRS[11:10] LBWS[9:8] CKEN[4] PLL_CKEN[4] RESETB[1] EN[0] */
425 tc358768_write(dev, TC358768_PLLCTL1,
426 (frs << 10) | (0x2 << 8) | BIT(4) | BIT(1) | BIT(0));
427}
428
429static u32 tc358768_ns_to_cnt(u32 ns, u32 period_ps)
430{
431 return DIV_ROUND_UP(ns * 1000, period_ps);
432}
433
434static u32 tc358768_ps_to_ns(u32 ps)
435{
436 return ps / 1000;
437}
438
439static u32 tc358768_dpi_to_ns(u32 val, u32 pclk)
440{
441 return (u32)div_u64((u64)val * NANO, pclk);
442}
443
444/* Convert value in DPI pixel clock units to DSI byte count */
445static u32 tc358768_dpi_to_dsi_bytes(struct tc358768_priv *priv, u32 val)
446{
447 u64 m = (u64)val * priv->dsiclk / 4 * priv->dsi_lanes;
448 u64 n = priv->timing.pixelclock.typ;
449
450 return (u32)div_u64(m + n - 1, n);
451}
452
453static u32 tc358768_dsi_bytes_to_ns(struct tc358768_priv *priv, u32 val)
454{
455 u64 m = (u64)val * NANO;
456 u64 n = priv->dsiclk / 4 * priv->dsi_lanes;
457
458 return (u32)div_u64(m, n);
459}
460
461static int tc358768_attach(struct udevice *dev)
462{
463 struct tc358768_priv *priv = dev_get_priv(dev);
464 struct mipi_dsi_device *device = &priv->device;
465 struct display_timing *dt = &priv->timing;
466 u32 val, val2, lptxcnt, hact, data_type;
467 s32 raw_val;
468 u32 hsbyteclk_ps, dsiclk_ps, ui_ps;
469 u32 dsiclk, hsbyteclk;
470 int i;
471 /* In pixelclock units */
472 u32 dpi_htot, dpi_data_start;
473 /* In byte units */
474 u32 dsi_dpi_htot, dsi_dpi_data_start;
475 u32 dsi_hsw, dsi_hbp, dsi_hact, dsi_hfp;
476 const u32 dsi_hss = 4; /* HSS is a short packet (4 bytes) */
477 /* In hsbyteclk units */
478 u32 dsi_vsdly;
479 const u32 internal_dly = 40;
480
481 if (device->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS) {
482 debug("%s: Non-continuous mode unimplemented, falling back to continuous\n", __func__);
483 device->mode_flags &= ~MIPI_DSI_CLOCK_NON_CONTINUOUS;
484 }
485
Svyatoslav Ryhel73091cc2025-02-14 15:28:28 +0200486 tc358768_hw_enable(dev);
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200487 tc358768_sw_reset(dev);
488
489 tc358768_setup_pll(dev);
490
491 dsiclk = priv->dsiclk;
492 hsbyteclk = dsiclk / 4;
493
494 /* Data Format Control Register */
495 val = BIT(2) | BIT(1) | BIT(0); /* rdswap_en | dsitx_en | txdt_en */
496 switch (device->format) {
497 case MIPI_DSI_FMT_RGB888:
498 val |= (0x3 << 4);
499 hact = dt->hactive.typ * 3;
500 data_type = MIPI_DSI_PACKED_PIXEL_STREAM_24;
501 break;
502 case MIPI_DSI_FMT_RGB666:
503 val |= (0x4 << 4);
504 hact = dt->hactive.typ * 3;
505 data_type = MIPI_DSI_PACKED_PIXEL_STREAM_18;
506 break;
507 case MIPI_DSI_FMT_RGB666_PACKED:
508 val |= (0x4 << 4) | BIT(3);
509 hact = dt->hactive.typ * 18 / 8;
510 data_type = MIPI_DSI_PIXEL_STREAM_3BYTE_18;
511 break;
512 case MIPI_DSI_FMT_RGB565:
513 val |= (0x5 << 4);
514 hact = dt->hactive.typ * 2;
515 data_type = MIPI_DSI_PACKED_PIXEL_STREAM_16;
516 break;
517 default:
518 log_debug("%s: Invalid data format (%u)\n",
519 __func__, device->format);
520 return -EINVAL;
521 }
522
523 /*
524 * There are three important things to make TC358768 work correctly,
525 * which are not trivial to manage:
526 *
527 * 1. Keep the DPI line-time and the DSI line-time as close to each
528 * other as possible.
529 * 2. TC358768 goes to LP mode after each line's active area. The DSI
530 * HFP period has to be long enough for entering and exiting LP mode.
531 * But it is not clear how to calculate this.
532 * 3. VSDly (video start delay) has to be long enough to ensure that the
533 * DSI TX does not start transmitting until we have started receiving
534 * pixel data from the DPI input. It is not clear how to calculate
535 * this either.
536 */
537
538 dpi_htot = dt->hactive.typ + dt->hfront_porch.typ +
539 dt->hsync_len.typ + dt->hback_porch.typ;
540 dpi_data_start = dt->hsync_len.typ + dt->hback_porch.typ;
541
542 log_debug("%s: dpi horiz timing (pclk): %u + %u + %u + %u = %u\n", __func__,
543 dt->hsync_len.typ, dt->hback_porch.typ, dt->hactive.typ,
544 dt->hfront_porch.typ, dpi_htot);
545
546 log_debug("%s: dpi horiz timing (ns): %u + %u + %u + %u = %u\n", __func__,
547 tc358768_dpi_to_ns(dt->hsync_len.typ, dt->pixelclock.typ),
548 tc358768_dpi_to_ns(dt->hback_porch.typ, dt->pixelclock.typ),
549 tc358768_dpi_to_ns(dt->hactive.typ, dt->pixelclock.typ),
550 tc358768_dpi_to_ns(dt->hfront_porch.typ, dt->pixelclock.typ),
551 tc358768_dpi_to_ns(dpi_htot, dt->pixelclock.typ));
552
553 log_debug("%s: dpi data start (ns): %u + %u = %u\n", __func__,
554 tc358768_dpi_to_ns(dt->hsync_len.typ, dt->pixelclock.typ),
555 tc358768_dpi_to_ns(dt->hback_porch.typ, dt->pixelclock.typ),
556 tc358768_dpi_to_ns(dpi_data_start, dt->pixelclock.typ));
557
558 dsi_dpi_htot = tc358768_dpi_to_dsi_bytes(priv, dpi_htot);
559 dsi_dpi_data_start = tc358768_dpi_to_dsi_bytes(priv, dpi_data_start);
560
561 if (device->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
562 dsi_hsw = tc358768_dpi_to_dsi_bytes(priv, dt->hsync_len.typ);
563 dsi_hbp = tc358768_dpi_to_dsi_bytes(priv, dt->hback_porch.typ);
564 } else {
565 /* HBP is included in HSW in event mode */
566 dsi_hbp = 0;
567 dsi_hsw = tc358768_dpi_to_dsi_bytes(priv,
568 dt->hsync_len.typ +
569 dt->hback_porch.typ);
570
571 /*
572 * The pixel packet includes the actual pixel data, and:
573 * DSI packet header = 4 bytes
574 * DCS code = 1 byte
575 * DSI packet footer = 2 bytes
576 */
577 dsi_hact = hact + 4 + 1 + 2;
578
579 dsi_hfp = dsi_dpi_htot - dsi_hact - dsi_hsw - dsi_hss;
580
581 /*
582 * Here we should check if HFP is long enough for entering LP
583 * and exiting LP, but it's not clear how to calculate that.
584 * Instead, this is a naive algorithm that just adjusts the HFP
585 * and HSW so that HFP is (at least) roughly 2/3 of the total
586 * blanking time.
587 */
588 if (dsi_hfp < (dsi_hfp + dsi_hsw + dsi_hss) * 2 / 3) {
589 u32 old_hfp = dsi_hfp;
590 u32 old_hsw = dsi_hsw;
591 u32 tot = dsi_hfp + dsi_hsw + dsi_hss;
592
593 dsi_hsw = tot / 3;
594
595 /*
596 * Seems like sometimes HSW has to be divisible by num-lanes, but
597 * not always...
598 */
599 dsi_hsw = roundup(dsi_hsw, priv->dsi_lanes);
600
601 dsi_hfp = dsi_dpi_htot - dsi_hact - dsi_hsw - dsi_hss;
602
603 log_debug("%s: hfp too short, adjusting dsi hfp and dsi hsw from %u, %u to %u, %u\n",
604 __func__, old_hfp, old_hsw, dsi_hfp, dsi_hsw);
605 }
606
607 log_debug("%s: dsi horiz timing (bytes): %u, %u + %u + %u + %u = %u\n", __func__,
608 dsi_hss, dsi_hsw, dsi_hbp, dsi_hact, dsi_hfp,
609 dsi_hss + dsi_hsw + dsi_hbp + dsi_hact + dsi_hfp);
610
611 log_debug("%s: dsi horiz timing (ns): %u + %u + %u + %u + %u = %u\n", __func__,
612 tc358768_dsi_bytes_to_ns(priv, dsi_hss),
613 tc358768_dsi_bytes_to_ns(priv, dsi_hsw),
614 tc358768_dsi_bytes_to_ns(priv, dsi_hbp),
615 tc358768_dsi_bytes_to_ns(priv, dsi_hact),
616 tc358768_dsi_bytes_to_ns(priv, dsi_hfp),
617 tc358768_dsi_bytes_to_ns(priv, dsi_hss + dsi_hsw +
618 dsi_hbp + dsi_hact + dsi_hfp));
619 }
620
621 /* VSDly calculation */
622
623 /* Start with the HW internal delay */
624 dsi_vsdly = internal_dly;
625
626 /* Convert to byte units as the other variables are in byte units */
627 dsi_vsdly *= priv->dsi_lanes;
628
629 /* Do we need more delay, in addition to the internal? */
630 if (dsi_dpi_data_start > dsi_vsdly + dsi_hss + dsi_hsw + dsi_hbp) {
631 dsi_vsdly = dsi_dpi_data_start - dsi_hss - dsi_hsw - dsi_hbp;
632 dsi_vsdly = roundup(dsi_vsdly, priv->dsi_lanes);
633 }
634
635 log_debug("%s: dsi data start (bytes) %u + %u + %u + %u = %u\n", __func__,
636 dsi_vsdly, dsi_hss, dsi_hsw, dsi_hbp,
637 dsi_vsdly + dsi_hss + dsi_hsw + dsi_hbp);
638
639 log_debug("%s: dsi data start (ns) %u + %u + %u + %u = %u\n", __func__,
640 tc358768_dsi_bytes_to_ns(priv, dsi_vsdly),
641 tc358768_dsi_bytes_to_ns(priv, dsi_hss),
642 tc358768_dsi_bytes_to_ns(priv, dsi_hsw),
643 tc358768_dsi_bytes_to_ns(priv, dsi_hbp),
644 tc358768_dsi_bytes_to_ns(priv, dsi_vsdly + dsi_hss + dsi_hsw + dsi_hbp));
645
646 /* Convert back to hsbyteclk */
647 dsi_vsdly /= priv->dsi_lanes;
648
649 /*
650 * The docs say that there is an internal delay of 40 cycles.
651 * However, we get underflows if we follow that rule. If we
652 * instead ignore the internal delay, things work. So either
653 * the docs are wrong or the calculations are wrong.
654 *
655 * As a temporary fix, add the internal delay here, to counter
656 * the subtraction when writing the register.
657 */
658 dsi_vsdly += internal_dly;
659
660 /* Clamp to the register max */
661 if (dsi_vsdly - internal_dly > 0x3ff) {
662 log_warning("%s: VSDly too high, underflows likely\n", __func__);
663 dsi_vsdly = 0x3ff + internal_dly;
664 }
665
666 /* VSDly[9:0] */
667 tc358768_write(dev, TC358768_VSDLY, dsi_vsdly - internal_dly);
668
669 tc358768_write(dev, TC358768_DATAFMT, val);
670 tc358768_write(dev, TC358768_DSITX_DT, data_type);
671
672 /* Enable D-PHY (HiZ->LP11) */
673 tc358768_write(dev, TC358768_CLW_CNTRL, 0x0000);
674 /* Enable lanes */
675 for (i = 0; i < device->lanes; i++)
676 tc358768_write(dev, TC358768_D0W_CNTRL + i * 4, 0x0000);
677
678 /* Set up D-PHY CONTTX */
679 tc358768_write(dev, TC358768_CLW_DPHYCONTTX, 0x0203);
680 /* Adjust lanes */
681 for (i = 0; i < device->lanes; i++)
682 tc358768_write(dev, TC358768_D0W_DPHYCONTTX + i * 4, 0x0203);
683
684 /* DSI Timings */
685 hsbyteclk_ps = (u32)div_u64(PICO, hsbyteclk);
686 dsiclk_ps = (u32)div_u64(PICO, dsiclk);
687 ui_ps = dsiclk_ps / 2;
688 log_debug("%s: dsiclk: %u ps, ui %u ps, hsbyteclk %u ps\n",
689 __func__, dsiclk_ps, ui_ps, hsbyteclk_ps);
690
691 /* LP11 > 100us for D-PHY Rx Init */
692 val = tc358768_ns_to_cnt(100 * 1000, hsbyteclk_ps) - 1;
693 log_debug("%s: LINEINITCNT: 0x%x\n", __func__, val);
694 tc358768_write(dev, TC358768_LINEINITCNT, val);
695
696 /* LPTimeCnt > 50ns */
697 val = tc358768_ns_to_cnt(50, hsbyteclk_ps) - 1;
698 lptxcnt = val;
699 log_debug("%s: LPTXTIMECNT: 0x%x\n", __func__, val);
700 tc358768_write(dev, TC358768_LPTXTIMECNT, val);
701
702 /* 38ns < TCLK_PREPARE < 95ns */
703 val = tc358768_ns_to_cnt(65, hsbyteclk_ps) - 1;
704 log_debug("%s: TCLK_PREPARECNT: 0x%x\n", __func__, val);
705 /* TCLK_PREPARE + TCLK_ZERO > 300ns */
706 val2 = tc358768_ns_to_cnt(300 - tc358768_ps_to_ns(2 * ui_ps),
707 hsbyteclk_ps) - 2;
708 log_debug("%s: TCLK_ZEROCNT: 0x%x\n", __func__, val2);
709 val |= val2 << 8;
710 tc358768_write(dev, TC358768_TCLK_HEADERCNT, val);
711
712 /* TCLK_TRAIL > 60ns AND TEOT <= 105 ns + 12*UI */
713 raw_val = tc358768_ns_to_cnt(60 + tc358768_ps_to_ns(2 * ui_ps),
714 hsbyteclk_ps) - 5;
715 val = clamp(raw_val, 0, 127);
716 log_debug("%s: TCLK_TRAILCNT: 0x%x\n", __func__, val);
717 tc358768_write(dev, TC358768_TCLK_TRAILCNT, val);
718
719 /* 40ns + 4*UI < THS_PREPARE < 85ns + 6*UI */
720 val = 50 + tc358768_ps_to_ns(4 * ui_ps);
721 val = tc358768_ns_to_cnt(val, hsbyteclk_ps) - 1;
722 log_debug("%s: THS_PREPARECNT: 0x%x\n", __func__, val);
723 /* THS_PREPARE + THS_ZERO > 145ns + 10*UI */
724 raw_val = tc358768_ns_to_cnt(145 - tc358768_ps_to_ns(3 * ui_ps),
725 hsbyteclk_ps) - 10;
726 val2 = clamp(raw_val, 0, 127);
727 log_debug("%s: THS_ZEROCNT: 0x%x\n", __func__, val2);
728 val |= val2 << 8;
729 tc358768_write(dev, TC358768_THS_HEADERCNT, val);
730
731 /* TWAKEUP > 1ms in lptxcnt steps */
732 val = tc358768_ns_to_cnt(1020000, hsbyteclk_ps);
733 val = val / (lptxcnt + 1) - 1;
734 log_debug("%s: TWAKEUP: 0x%x\n", __func__, val);
735 tc358768_write(dev, TC358768_TWAKEUP, val);
736
737 /* TCLK_POSTCNT > 60ns + 52*UI */
738 val = tc358768_ns_to_cnt(60 + tc358768_ps_to_ns(52 * ui_ps),
739 hsbyteclk_ps) - 3;
740 log_debug("%s: TCLK_POSTCNT: 0x%x\n", __func__, val);
741 tc358768_write(dev, TC358768_TCLK_POSTCNT, val);
742
743 /* max(60ns + 4*UI, 8*UI) < THS_TRAILCNT < 105ns + 12*UI */
744 raw_val = tc358768_ns_to_cnt(60 + tc358768_ps_to_ns(18 * ui_ps),
745 hsbyteclk_ps) - 4;
746 val = clamp(raw_val, 0, 15);
747 log_debug("%s: THS_TRAILCNT: 0x%x\n", __func__, val);
748 tc358768_write(dev, TC358768_THS_TRAILCNT, val);
749
750 val = BIT(0);
751 for (i = 0; i < device->lanes; i++)
752 val |= BIT(i + 1);
753 tc358768_write(dev, TC358768_HSTXVREGEN, val);
754
755 tc358768_write(dev, TC358768_TXOPTIONCNTRL,
756 (device->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS) ? 0 : BIT(0));
757
758 /* TXTAGOCNT[26:16] RXTASURECNT[10:0] */
759 val = tc358768_ps_to_ns((lptxcnt + 1) * hsbyteclk_ps * 4);
760 val = tc358768_ns_to_cnt(val, hsbyteclk_ps) / 4 - 1;
761 log_debug("%s: TXTAGOCNT: 0x%x\n", __func__, val);
762 val2 = tc358768_ns_to_cnt(tc358768_ps_to_ns((lptxcnt + 1) * hsbyteclk_ps),
763 hsbyteclk_ps) - 2;
764 log_debug("%s: RXTASURECNT: 0x%x\n", __func__, val2);
765 val = val << 16 | val2;
766 tc358768_write(dev, TC358768_BTACNTRL1, val);
767
768 /* START[0] */
769 tc358768_write(dev, TC358768_STARTCNTRL, 1);
770
771 if (device->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
772 /* Set pulse mode */
773 tc358768_write(dev, TC358768_DSI_EVENT, 0);
774
775 /* vact */
776 tc358768_write(dev, TC358768_DSI_VACT, dt->vactive.typ);
777 /* vsw */
778 tc358768_write(dev, TC358768_DSI_VSW, dt->vsync_len.typ);
779 /* vbp */
780 tc358768_write(dev, TC358768_DSI_VBPR, dt->vback_porch.typ);
781 } else {
782 /* Set event mode */
783 tc358768_write(dev, TC358768_DSI_EVENT, 1);
784
785 /* vact */
786 tc358768_write(dev, TC358768_DSI_VACT, dt->vactive.typ);
787
788 /* vsw (+ vbp) */
789 tc358768_write(dev, TC358768_DSI_VSW,
790 dt->vsync_len.typ + dt->vback_porch.typ);
791 /* vbp (not used in event mode) */
792 tc358768_write(dev, TC358768_DSI_VBPR, 0);
793 }
794
795 /* hsw (bytes) */
796 tc358768_write(dev, TC358768_DSI_HSW, dsi_hsw);
797
798 /* hbp (bytes) */
799 tc358768_write(dev, TC358768_DSI_HBPR, dsi_hbp);
800
801 /* hact (bytes) */
802 tc358768_write(dev, TC358768_DSI_HACT, hact);
803
804 /* VSYNC polarity */
805 tc358768_update_bits(dev, TC358768_CONFCTL, BIT(5),
806 (dt->flags & DISPLAY_FLAGS_VSYNC_HIGH) ? BIT(5) : 0);
807
808 /* HSYNC polarity */
809 tc358768_update_bits(dev, TC358768_PP_MISC, BIT(0),
810 (dt->flags & DISPLAY_FLAGS_HSYNC_LOW) ? BIT(0) : 0);
811
812 /* Start DSI Tx */
813 tc358768_write(dev, TC358768_DSI_START, 0x1);
814
815 /* Configure DSI_Control register */
816 val = TC358768_DSI_CONFW_MODE_CLR | TC358768_DSI_CONFW_ADDR_DSI_CONTROL;
817 val |= TC358768_DSI_CONTROL_TXMD | TC358768_DSI_CONTROL_HSCKMD |
818 0x3 << 1 | TC358768_DSI_CONTROL_EOTDIS;
819 tc358768_write(dev, TC358768_DSI_CONFW, val);
820
821 val = TC358768_DSI_CONFW_MODE_SET | TC358768_DSI_CONFW_ADDR_DSI_CONTROL;
822 val |= (device->lanes - 1) << 1;
823
824 val |= TC358768_DSI_CONTROL_TXMD;
825
826 if (!(device->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS))
827 val |= TC358768_DSI_CONTROL_HSCKMD;
828
829 /*
830 * TODO: Actually MIPI_DSI_MODE_NO_EOT_PACKET
831 *
832 * Many of the DSI flags have names opposite to their
833 * actual effects, e.g. MIPI_DSI_MODE_EOT_PACKET means
834 * that EoT packets will actually be disabled.
835 */
836 if (device->mode_flags & MIPI_DSI_MODE_EOT_PACKET)
837 val |= TC358768_DSI_CONTROL_EOTDIS;
838
839 tc358768_write(dev, TC358768_DSI_CONFW, val);
840
841 val = TC358768_DSI_CONFW_MODE_CLR |
842 TC358768_DSI_CONFW_ADDR_DSI_CONTROL |
843 TC358768_DSI_CONTROL_DIS_MODE; /* DSI mode */
844 tc358768_write(dev, TC358768_DSI_CONFW, val);
845
846 /* clear FrmStop and RstPtr */
847 tc358768_update_bits(dev, TC358768_PP_MISC, 0x3 << 14, 0);
848
849 /* set PP_en */
850 tc358768_update_bits(dev, TC358768_CONFCTL, BIT(6), BIT(6));
851
852 /* Set up panel configuration */
853 return panel_enable_backlight(priv->panel);
854}
855
856static int tc358768_set_backlight(struct udevice *dev, int percent)
857{
858 struct tc358768_priv *priv = dev_get_priv(dev);
859
860 return panel_set_backlight(priv->panel, percent);
861}
862
863static int tc358768_panel_timings(struct udevice *dev,
864 struct display_timing *timing)
865{
866 struct tc358768_priv *priv = dev_get_priv(dev);
867
868 /* Default to positive sync */
869
870 if (!(priv->timing.flags &
871 (DISPLAY_FLAGS_HSYNC_LOW | DISPLAY_FLAGS_HSYNC_HIGH)))
872 priv->timing.flags |= DISPLAY_FLAGS_HSYNC_HIGH;
873
874 if (!(priv->timing.flags &
875 (DISPLAY_FLAGS_VSYNC_LOW | DISPLAY_FLAGS_VSYNC_HIGH)))
876 priv->timing.flags |= DISPLAY_FLAGS_VSYNC_HIGH;
877
878 memcpy(timing, &priv->timing, sizeof(*timing));
879
880 return 0;
881}
882
Svyatoslav Ryhela7ae9882025-02-21 13:29:43 +0200883static int tc358768_get_panel(struct udevice *dev)
884{
885 struct tc358768_priv *priv = dev_get_priv(dev);
886 int i, ret;
887
888 u32 num = ofnode_graph_get_port_count(dev_ofnode(dev));
889
890 for (i = 0; i < num; i++) {
891 ofnode remote = ofnode_graph_get_remote_node(dev_ofnode(dev), i, -1);
892
893 ret = uclass_get_device_by_ofnode(UCLASS_PANEL, remote,
894 &priv->panel);
895 if (!ret)
896 return 0;
897 }
898
899 /* If this point is reached, no panels were found */
900 return -ENODEV;
901}
902
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200903static int tc358768_setup(struct udevice *dev)
904{
905 struct tc358768_priv *priv = dev_get_priv(dev);
Svyatoslav Ryhel73091cc2025-02-14 15:28:28 +0200906 struct video_bridge_priv *uc_priv = dev_get_uclass_priv(dev);
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200907 struct mipi_dsi_device *device = &priv->device;
908 struct mipi_dsi_panel_plat *mipi_plat;
Svyatoslav Ryhel1aee4132025-02-23 11:39:13 +0200909 int i, ret;
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200910
911 /* The bridge uses 16 bit registers */
912 ret = i2c_set_chip_offset_len(dev, 2);
913 if (ret) {
914 log_debug("%s: set_chip_offset_len failed: %d\n",
915 __func__, ret);
916 return ret;
917 }
918
Svyatoslav Ryhela7ae9882025-02-21 13:29:43 +0200919 ret = tc358768_get_panel(dev);
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200920 if (ret) {
Svyatoslav Ryhela7ae9882025-02-21 13:29:43 +0200921 log_debug("%s: panel not found, ret %d\n", __func__, ret);
922 return ret;
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200923 }
924
925 panel_get_display_timing(priv->panel, &priv->timing);
926
927 mipi_plat = dev_get_plat(priv->panel);
928 mipi_plat->device = device;
929
930 priv->host.dev = (struct device *)dev;
931 priv->host.ops = &tc358768_dsi_host_ops;
932
933 device->host = &priv->host;
934 device->lanes = mipi_plat->lanes;
935 device->format = mipi_plat->format;
936 device->mode_flags = mipi_plat->mode_flags;
937
938 priv->pd_lines = mipi_dsi_pixel_format_to_bpp(device->format);
939 priv->dsi_lanes = device->lanes;
940
941 /* get regulators */
Svyatoslav Ryhel1aee4132025-02-23 11:39:13 +0200942 for (i = 0; i < ARRAY_SIZE(tc358768_supplies); i++) {
943 ret = device_get_supply_regulator(dev, tc358768_supplies[i],
944 &priv->supplies[i]);
945 if (ret) {
946 log_debug("%s: cannot get %s %d\n", __func__,
947 tc358768_supplies[i], ret);
948 if (ret != -ENOENT)
949 return log_ret(ret);
950 }
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200951 }
952
953 /* get clk */
Svyatoslav Ryhel5cacc392025-02-23 11:41:10 +0200954 priv->refclk = devm_clk_get(dev, NULL);
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200955 if (IS_ERR(priv->refclk)) {
956 log_debug("%s: Could not get refclk: %ld\n",
957 __func__, PTR_ERR(priv->refclk));
958 return PTR_ERR(priv->refclk);
959 }
960
Svyatoslav Ryhel73091cc2025-02-14 15:28:28 +0200961 dm_gpio_set_value(&uc_priv->reset, 1);
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200962
963 return 0;
964}
965
966static int tc358768_probe(struct udevice *dev)
967{
968 if (device_get_uclass_id(dev->parent) != UCLASS_I2C)
969 return -EPROTONOSUPPORT;
970
971 return tc358768_setup(dev);
972}
973
Svyatoslav Ryhel73091cc2025-02-14 15:28:28 +0200974static const struct video_bridge_ops tc358768_ops = {
975 .attach = tc358768_attach,
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200976 .set_backlight = tc358768_set_backlight,
977 .get_display_timing = tc358768_panel_timings,
978};
979
980static const struct udevice_id tc358768_ids[] = {
981 { .compatible = "toshiba,tc358768" },
982 { .compatible = "toshiba,tc358778" },
983 { }
984};
985
986U_BOOT_DRIVER(tc358768) = {
987 .name = "tc358768",
Svyatoslav Ryhel73091cc2025-02-14 15:28:28 +0200988 .id = UCLASS_VIDEO_BRIDGE,
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200989 .of_match = tc358768_ids,
990 .ops = &tc358768_ops,
Svyatoslav Ryhela7ae9882025-02-21 13:29:43 +0200991 .bind = dm_scan_fdt_dev,
Svyatoslav Ryhelef1e26b2024-01-31 08:57:17 +0200992 .probe = tc358768_probe,
993 .priv_auto = sizeof(struct tc358768_priv),
994};