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