blob: 6327266dd22b8bd4c962e45b0673c68261bd0b7b [file] [log] [blame]
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2013 NVIDIA Corporation
4 * Copyright (c) 2022 Svyatoslav Ryhel <clamor95@gmail.com>
5 */
6
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03007#include <dm.h>
8#include <log.h>
9#include <misc.h>
10#include <mipi_display.h>
11#include <mipi_dsi.h>
12#include <backlight.h>
13#include <panel.h>
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +020014#include <reset.h>
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030015#include <linux/delay.h>
16#include <linux/err.h>
Igor Prusovc3421ea2023-11-09 20:10:04 +030017#include <linux/time.h>
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030018#include <power/regulator.h>
19
20#include <asm/gpio.h>
21#include <asm/io.h>
22#include <asm/arch/clock.h>
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +030023#include <asm/arch-tegra/clk_rst.h>
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030024
Svyatoslav Ryhel75fec412024-01-23 19:16:18 +020025#include "tegra-dc.h"
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +020026#include "tegra-dsi.h"
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030027#include "mipi-phy.h"
28
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +020029/* List of supported DSI bridges */
30enum {
31 DSI_V0,
32 DSI_V1,
33};
34
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030035struct tegra_dsi_priv {
36 struct mipi_dsi_host host;
37 struct mipi_dsi_device device;
38 struct mipi_dphy_timing dphy_timing;
39
40 struct udevice *panel;
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +020041 struct udevice *mipi;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030042 struct display_timing timing;
43
44 struct dsi_ctlr *dsi;
45 struct udevice *avdd;
46
47 enum tegra_dsi_format format;
48
49 int dsi_clk;
50 int video_fifo_depth;
51 int host_fifo_depth;
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +020052
53 u32 version;
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +030054
55 /* for ganged-mode support */
56 struct udevice *master;
57 struct udevice *slave;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030058};
59
60static void tegra_dc_enable_controller(struct udevice *dev)
61{
62 struct tegra_dc_plat *dc_plat = dev_get_plat(dev);
63 struct dc_ctlr *dc = dc_plat->dc;
64 u32 value;
65
66 value = readl(&dc->disp.disp_win_opt);
67 value |= DSI_ENABLE;
68 writel(value, &dc->disp.disp_win_opt);
69
70 writel(GENERAL_UPDATE, &dc->cmd.state_ctrl);
71 writel(GENERAL_ACT_REQ, &dc->cmd.state_ctrl);
72}
73
74static const char * const error_report[16] = {
75 "SoT Error",
76 "SoT Sync Error",
77 "EoT Sync Error",
78 "Escape Mode Entry Command Error",
79 "Low-Power Transmit Sync Error",
80 "Peripheral Timeout Error",
81 "False Control Error",
82 "Contention Detected",
83 "ECC Error, single-bit",
84 "ECC Error, multi-bit",
85 "Checksum Error",
86 "DSI Data Type Not Recognized",
87 "DSI VC ID Invalid",
88 "Invalid Transmission Length",
89 "Reserved",
90 "DSI Protocol Violation",
91};
92
93static ssize_t tegra_dsi_read_response(struct dsi_misc_reg *misc,
94 const struct mipi_dsi_msg *msg,
95 size_t count)
96{
97 u8 *rx = msg->rx_buf;
98 unsigned int i, j, k;
99 size_t size = 0;
100 u16 errors;
101 u32 value;
102
103 /* read and parse packet header */
104 value = readl(&misc->dsi_rd_data);
105
106 switch (value & 0x3f) {
107 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
108 errors = (value >> 8) & 0xffff;
109 printf("%s: Acknowledge and error report: %04x\n",
110 __func__, errors);
111 for (i = 0; i < ARRAY_SIZE(error_report); i++)
112 if (errors & BIT(i))
113 printf("%s: %2u: %s\n", __func__, i,
114 error_report[i]);
115 break;
116
117 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
118 rx[0] = (value >> 8) & 0xff;
119 size = 1;
120 break;
121
122 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
123 rx[0] = (value >> 8) & 0xff;
124 rx[1] = (value >> 16) & 0xff;
125 size = 2;
126 break;
127
128 case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
129 size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
130 break;
131
132 case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
133 size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
134 break;
135
136 default:
137 printf("%s: unhandled response type: %02x\n",
138 __func__, value & 0x3f);
139 return -EPROTO;
140 }
141
142 size = min(size, msg->rx_len);
143
144 if (msg->rx_buf && size > 0) {
145 for (i = 0, j = 0; i < count - 1; i++, j += 4) {
146 u8 *rx = msg->rx_buf + j;
147
148 value = readl(&misc->dsi_rd_data);
149
150 for (k = 0; k < 4 && (j + k) < msg->rx_len; k++)
151 rx[j + k] = (value >> (k << 3)) & 0xff;
152 }
153 }
154
155 return size;
156}
157
158static int tegra_dsi_transmit(struct dsi_misc_reg *misc,
159 unsigned long timeout)
160{
161 writel(DSI_TRIGGER_HOST, &misc->dsi_trigger);
162
163 while (timeout--) {
164 u32 value = readl(&misc->dsi_trigger);
165
166 if ((value & DSI_TRIGGER_HOST) == 0)
167 return 0;
168
169 udelay(1000);
170 }
171
172 debug("timeout waiting for transmission to complete\n");
173 return -ETIMEDOUT;
174}
175
176static int tegra_dsi_wait_for_response(struct dsi_misc_reg *misc,
177 unsigned long timeout)
178{
179 while (timeout--) {
180 u32 value = readl(&misc->dsi_status);
181 u8 count = value & 0x1f;
182
183 if (count > 0)
184 return count;
185
186 udelay(1000);
187 }
188
189 debug("peripheral returned no data\n");
190 return -ETIMEDOUT;
191}
192
193static void tegra_dsi_writesl(struct dsi_misc_reg *misc,
194 const void *buffer, size_t size)
195{
196 const u8 *buf = buffer;
197 size_t i, j;
198 u32 value;
199
200 for (j = 0; j < size; j += 4) {
201 value = 0;
202
203 for (i = 0; i < 4 && j + i < size; i++)
204 value |= buf[j + i] << (i << 3);
205
206 writel(value, &misc->dsi_wr_data);
207 }
208}
209
210static ssize_t tegra_dsi_host_transfer(struct mipi_dsi_host *host,
211 const struct mipi_dsi_msg *msg)
212{
213 struct udevice *dev = (struct udevice *)host->dev;
214 struct tegra_dsi_priv *priv = dev_get_priv(dev);
215 struct dsi_misc_reg *misc = &priv->dsi->misc;
216 struct mipi_dsi_packet packet;
217 const u8 *header;
218 size_t count;
219 ssize_t err;
220 u32 value;
221
222 err = mipi_dsi_create_packet(&packet, msg);
223 if (err < 0)
224 return err;
225
226 header = packet.header;
227
228 /* maximum FIFO depth is 1920 words */
229 if (packet.size > priv->video_fifo_depth * 4)
230 return -ENOSPC;
231
232 /* reset underflow/overflow flags */
233 value = readl(&misc->dsi_status);
234 if (value & (DSI_STATUS_UNDERFLOW | DSI_STATUS_OVERFLOW)) {
235 value = DSI_HOST_CONTROL_FIFO_RESET;
236 writel(value, &misc->host_dsi_ctrl);
237 udelay(10);
238 }
239
240 value = readl(&misc->dsi_pwr_ctrl);
241 value |= DSI_POWER_CONTROL_ENABLE;
242 writel(value, &misc->dsi_pwr_ctrl);
243
244 mdelay(5);
245
246 value = DSI_HOST_CONTROL_CRC_RESET | DSI_HOST_CONTROL_TX_TRIG_HOST |
247 DSI_HOST_CONTROL_CS | DSI_HOST_CONTROL_ECC;
248
249 /*
250 * The host FIFO has a maximum of 64 words, so larger transmissions
251 * need to use the video FIFO.
252 */
253 if (packet.size > priv->host_fifo_depth * 4)
254 value |= DSI_HOST_CONTROL_FIFO_SEL;
255
256 writel(value, &misc->host_dsi_ctrl);
257
258 /*
259 * For reads and messages with explicitly requested ACK, generate a
260 * BTA sequence after the transmission of the packet.
261 */
262 if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
263 (msg->rx_buf && msg->rx_len > 0)) {
264 value = readl(&misc->host_dsi_ctrl);
265 value |= DSI_HOST_CONTROL_PKT_BTA;
266 writel(value, &misc->host_dsi_ctrl);
267 }
268
269 value = DSI_CONTROL_LANES(0) | DSI_CONTROL_HOST_ENABLE;
270 writel(value, &misc->dsi_ctrl);
271
272 /* write packet header, ECC is generated by hardware */
273 value = header[2] << 16 | header[1] << 8 | header[0];
274 writel(value, &misc->dsi_wr_data);
275
276 /* write payload (if any) */
277 if (packet.payload_length > 0)
278 tegra_dsi_writesl(misc, packet.payload,
279 packet.payload_length);
280
281 err = tegra_dsi_transmit(misc, 250);
282 if (err < 0)
283 return err;
284
285 if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
286 (msg->rx_buf && msg->rx_len > 0)) {
287 err = tegra_dsi_wait_for_response(misc, 250);
288 if (err < 0)
289 return err;
290
291 count = err;
292
293 value = readl(&misc->dsi_rd_data);
294 switch (value) {
295 case 0x84:
296 debug("%s: ACK\n", __func__);
297 break;
298
299 case 0x87:
300 debug("%s: ESCAPE\n", __func__);
301 break;
302
303 default:
304 printf("%s: unknown status: %08x\n", __func__, value);
305 break;
306 }
307
308 if (count > 1) {
309 err = tegra_dsi_read_response(misc, msg, count);
310 if (err < 0) {
311 printf("%s: failed to parse response: %zd\n",
312 __func__, err);
313 } else {
314 /*
315 * For read commands, return the number of
316 * bytes returned by the peripheral.
317 */
318 count = err;
319 }
320 }
321 } else {
322 /*
323 * For write commands, we have transmitted the 4-byte header
324 * plus the variable-length payload.
325 */
326 count = 4 + packet.payload_length;
327 }
328
329 return count;
330}
331
332struct mipi_dsi_host_ops tegra_dsi_bridge_host_ops = {
333 .transfer = tegra_dsi_host_transfer,
334};
335
336#define PKT_ID0(id) ((((id) & 0x3f) << 3) | (1 << 9))
337#define PKT_LEN0(len) (((len) & 0x07) << 0)
338#define PKT_ID1(id) ((((id) & 0x3f) << 13) | (1 << 19))
339#define PKT_LEN1(len) (((len) & 0x07) << 10)
340#define PKT_ID2(id) ((((id) & 0x3f) << 23) | (1 << 29))
341#define PKT_LEN2(len) (((len) & 0x07) << 20)
342
343#define PKT_LP BIT(30)
344#define NUM_PKT_SEQ 12
345
346/*
347 * non-burst mode with sync pulses
348 */
349static const u32 pkt_seq_video_non_burst_sync_pulses[NUM_PKT_SEQ] = {
350 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
351 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
352 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
353 PKT_LP,
354 [ 1] = 0,
355 [ 2] = PKT_ID0(MIPI_DSI_V_SYNC_END) | PKT_LEN0(0) |
356 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
357 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
358 PKT_LP,
359 [ 3] = 0,
360 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
361 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
362 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
363 PKT_LP,
364 [ 5] = 0,
365 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
366 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
367 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
368 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
369 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
370 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
371 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
372 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
373 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
374 PKT_LP,
375 [ 9] = 0,
376 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
377 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
378 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
379 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
380 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
381 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
382};
383
384/*
385 * non-burst mode with sync events
386 */
387static const u32 pkt_seq_video_non_burst_sync_events[NUM_PKT_SEQ] = {
388 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
389 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
390 PKT_LP,
391 [ 1] = 0,
392 [ 2] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
393 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
394 PKT_LP,
395 [ 3] = 0,
396 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
397 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
398 PKT_LP,
399 [ 5] = 0,
400 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
401 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
402 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
403 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
404 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
405 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
406 PKT_LP,
407 [ 9] = 0,
408 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
409 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
410 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
411 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
412};
413
414static const u32 pkt_seq_command_mode[NUM_PKT_SEQ] = {
415 [ 0] = 0,
416 [ 1] = 0,
417 [ 2] = 0,
418 [ 3] = 0,
419 [ 4] = 0,
420 [ 5] = 0,
421 [ 6] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(3) | PKT_LP,
422 [ 7] = 0,
423 [ 8] = 0,
424 [ 9] = 0,
425 [10] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(5) | PKT_LP,
426 [11] = 0,
427};
428
429static void tegra_dsi_get_muldiv(enum mipi_dsi_pixel_format format,
430 unsigned int *mulp, unsigned int *divp)
431{
432 switch (format) {
433 case MIPI_DSI_FMT_RGB666_PACKED:
434 case MIPI_DSI_FMT_RGB888:
435 *mulp = 3;
436 *divp = 1;
437 break;
438
439 case MIPI_DSI_FMT_RGB565:
440 *mulp = 2;
441 *divp = 1;
442 break;
443
444 case MIPI_DSI_FMT_RGB666:
445 *mulp = 9;
446 *divp = 4;
447 break;
448
449 default:
450 break;
451 }
452}
453
454static int tegra_dsi_get_format(enum mipi_dsi_pixel_format format,
455 enum tegra_dsi_format *fmt)
456{
457 switch (format) {
458 case MIPI_DSI_FMT_RGB888:
459 *fmt = TEGRA_DSI_FORMAT_24P;
460 break;
461
462 case MIPI_DSI_FMT_RGB666:
463 *fmt = TEGRA_DSI_FORMAT_18NP;
464 break;
465
466 case MIPI_DSI_FMT_RGB666_PACKED:
467 *fmt = TEGRA_DSI_FORMAT_18P;
468 break;
469
470 case MIPI_DSI_FMT_RGB565:
471 *fmt = TEGRA_DSI_FORMAT_16P;
472 break;
473
474 default:
475 return -EINVAL;
476 }
477
478 return 0;
479}
480
481static void tegra_dsi_pad_calibrate(struct dsi_pad_ctrl_reg *pad)
482{
483 u32 value;
484
485 /* start calibration */
486 value = DSI_PAD_CONTROL_PAD_LPUPADJ(0x1) |
487 DSI_PAD_CONTROL_PAD_LPDNADJ(0x1) |
488 DSI_PAD_CONTROL_PAD_PREEMP_EN(0x1) |
489 DSI_PAD_CONTROL_PAD_SLEWDNADJ(0x6) |
490 DSI_PAD_CONTROL_PAD_SLEWUPADJ(0x6) |
491 DSI_PAD_CONTROL_PAD_PDIO(0) |
492 DSI_PAD_CONTROL_PAD_PDIO_CLK(0) |
493 DSI_PAD_CONTROL_PAD_PULLDN_ENAB(0);
494 writel(value, &pad->pad_ctrl);
495
496 clock_enable(PERIPH_ID_VI);
497 clock_enable(PERIPH_ID_CSI);
498 udelay(2);
499 reset_set_enable(PERIPH_ID_VI, 0);
500 reset_set_enable(PERIPH_ID_CSI, 0);
501
502 value = MIPI_CAL_TERMOSA(0x4);
503 writel(value, TEGRA_VI_BASE + (CSI_CILA_MIPI_CAL_CONFIG_0 << 2));
504
505 value = MIPI_CAL_TERMOSB(0x4);
506 writel(value, TEGRA_VI_BASE + (CSI_CILB_MIPI_CAL_CONFIG_0 << 2));
507
508 value = MIPI_CAL_HSPUOSD(0x3) | MIPI_CAL_HSPDOSD(0x4);
509 writel(value, TEGRA_VI_BASE + (CSI_DSI_MIPI_CAL_CONFIG << 2));
510
511 value = PAD_DRIV_DN_REF(0x5) | PAD_DRIV_UP_REF(0x7);
512 writel(value, TEGRA_VI_BASE + (CSI_MIPIBIAS_PAD_CONFIG << 2));
513
514 value = PAD_CIL_PDVREG(0x0);
515 writel(value, TEGRA_VI_BASE + (CSI_CIL_PAD_CONFIG << 2));
516}
517
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200518static void tegra_dsi_mipi_calibrate(struct tegra_dsi_priv *priv)
519{
520 struct dsi_pad_ctrl_reg *pad = &priv->dsi->pad;
521 u32 value;
522 int ret;
523
524 ret = misc_set_enabled(priv->mipi, true);
525 if (ret)
526 log_debug("%s: failed to enable MIPI calibration: %d\n",
527 __func__, ret);
528
529 writel(0, &pad->pad_ctrl);
530 writel(0, &pad->pad_ctrl_1);
531 writel(0, &pad->pad_ctrl_2);
532 writel(0, &pad->pad_ctrl_3);
533 writel(0, &pad->pad_ctrl_4);
534
535 /* DSI pad enable */
536 value = DSI_PAD_CONTROL_VS1_PULLDN(0) | DSI_PAD_CONTROL_VS1_PDIO(0);
537 writel(value, &pad->pad_ctrl);
538
539 value = DSI_PAD_SLEW_UP(0x7) | DSI_PAD_SLEW_DN(0x7) |
540 DSI_PAD_LP_UP(0x1) | DSI_PAD_LP_DN(0x1) |
541 DSI_PAD_OUT_CLK(0x0);
542 writel(value, &pad->pad_ctrl_2);
543
544 value = DSI_PAD_PREEMP_PD_CLK(0x3) | DSI_PAD_PREEMP_PU_CLK(0x3) |
545 DSI_PAD_PREEMP_PD(0x03) | DSI_PAD_PREEMP_PU(0x3);
546 writel(value, &pad->pad_ctrl_3);
547
548 ret = misc_write(priv->mipi, 0, NULL, 0);
549 if (ret)
550 log_debug("%s: MIPI calibration failed %d\n", __func__, ret);
551}
552
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300553static void tegra_dsi_set_timeout(struct dsi_timeout_reg *rtimeout,
554 unsigned long bclk,
555 unsigned int vrefresh)
556{
557 unsigned int timeout;
558 u32 value;
559
560 /* one frame high-speed transmission timeout */
561 timeout = (bclk / vrefresh) / 512;
562 value = DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout);
563 writel(value, &rtimeout->dsi_timeout_0);
564
565 /* 2 ms peripheral timeout for panel */
566 timeout = 2 * bclk / 512 * 1000;
567 value = DSI_TIMEOUT_PR(timeout) | DSI_TIMEOUT_TA(0x2000);
568 writel(value, &rtimeout->dsi_timeout_1);
569
570 value = DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
571 writel(value, &rtimeout->dsi_to_tally);
572}
573
574static void tegra_dsi_set_phy_timing(struct dsi_timing_reg *ptiming,
575 unsigned long period,
576 const struct mipi_dphy_timing *dphy_timing)
577{
578 u32 value;
579
580 value = DSI_TIMING_FIELD(dphy_timing->hsexit, period, 1) << 24 |
581 DSI_TIMING_FIELD(dphy_timing->hstrail, period, 0) << 16 |
582 DSI_TIMING_FIELD(dphy_timing->hszero, period, 3) << 8 |
583 DSI_TIMING_FIELD(dphy_timing->hsprepare, period, 1);
584 writel(value, &ptiming->dsi_phy_timing_0);
585
586 value = DSI_TIMING_FIELD(dphy_timing->clktrail, period, 1) << 24 |
587 DSI_TIMING_FIELD(dphy_timing->clkpost, period, 1) << 16 |
588 DSI_TIMING_FIELD(dphy_timing->clkzero, period, 1) << 8 |
589 DSI_TIMING_FIELD(dphy_timing->lpx, period, 1);
590 writel(value, &ptiming->dsi_phy_timing_1);
591
592 value = DSI_TIMING_FIELD(dphy_timing->clkprepare, period, 1) << 16 |
593 DSI_TIMING_FIELD(dphy_timing->clkpre, period, 1) << 8 |
594 DSI_TIMING_FIELD(0xff * period, period, 0) << 0;
595 writel(value, &ptiming->dsi_phy_timing_2);
596
597 value = DSI_TIMING_FIELD(dphy_timing->taget, period, 1) << 16 |
598 DSI_TIMING_FIELD(dphy_timing->tasure, period, 1) << 8 |
599 DSI_TIMING_FIELD(dphy_timing->tago, period, 1);
600 writel(value, &ptiming->dsi_bta_timing);
601}
602
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300603static void tegra_dsi_ganged_enable(struct udevice *dev, unsigned int start,
604 unsigned int size)
605{
606 struct tegra_dsi_priv *priv = dev_get_priv(dev);
607 struct dsi_ganged_mode_reg *ganged = &priv->dsi->ganged;
608
609 writel(start, &ganged->ganged_mode_start);
610 writel(size << 16 | size, &ganged->ganged_mode_size);
611 writel(DSI_GANGED_MODE_CONTROL_ENABLE, &ganged->ganged_mode_ctrl);
612}
613
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300614static void tegra_dsi_configure(struct udevice *dev,
615 unsigned long mode_flags)
616{
617 struct tegra_dsi_priv *priv = dev_get_priv(dev);
618 struct mipi_dsi_device *device = &priv->device;
619 struct display_timing *timing = &priv->timing;
620
621 struct dsi_misc_reg *misc = &priv->dsi->misc;
622 struct dsi_pkt_seq_reg *pkt = &priv->dsi->pkt;
623 struct dsi_pkt_len_reg *len = &priv->dsi->len;
624
625 unsigned int hact, hsw, hbp, hfp, i, mul, div;
626 const u32 *pkt_seq;
627 u32 value;
628
629 tegra_dsi_get_muldiv(device->format, &mul, &div);
630
631 if (mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
632 printf("[DSI] Non-burst video mode with sync pulses\n");
633 pkt_seq = pkt_seq_video_non_burst_sync_pulses;
634 } else if (mode_flags & MIPI_DSI_MODE_VIDEO) {
635 printf("[DSI] Non-burst video mode with sync events\n");
636 pkt_seq = pkt_seq_video_non_burst_sync_events;
637 } else {
638 printf("[DSI] Command mode\n");
639 pkt_seq = pkt_seq_command_mode;
640 }
641
642 value = DSI_CONTROL_CHANNEL(0) |
643 DSI_CONTROL_FORMAT(priv->format) |
644 DSI_CONTROL_LANES(device->lanes - 1) |
645 DSI_CONTROL_SOURCE(0);
646 writel(value, &misc->dsi_ctrl);
647
648 writel(priv->video_fifo_depth, &misc->dsi_max_threshold);
649
650 value = DSI_HOST_CONTROL_HS;
651 writel(value, &misc->host_dsi_ctrl);
652
653 value = readl(&misc->dsi_ctrl);
654
655 if (mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
656 value |= DSI_CONTROL_HS_CLK_CTRL;
657
658 value &= ~DSI_CONTROL_TX_TRIG(3);
659
660 /* enable DCS commands for command mode */
661 if (mode_flags & MIPI_DSI_MODE_VIDEO)
662 value &= ~DSI_CONTROL_DCS_ENABLE;
663 else
664 value |= DSI_CONTROL_DCS_ENABLE;
665
666 value |= DSI_CONTROL_VIDEO_ENABLE;
667 value &= ~DSI_CONTROL_HOST_ENABLE;
668 writel(value, &misc->dsi_ctrl);
669
670 for (i = 0; i < NUM_PKT_SEQ; i++)
671 writel(pkt_seq[i], &pkt->dsi_pkt_seq_0_lo + i);
672
673 if (mode_flags & MIPI_DSI_MODE_VIDEO) {
674 /* horizontal active pixels */
675 hact = timing->hactive.typ * mul / div;
676
677 /* horizontal sync width */
678 hsw = timing->hsync_len.typ * mul / div;
679
680 /* horizontal back porch */
681 hbp = timing->hback_porch.typ * mul / div;
682
683 if ((mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) == 0)
684 hbp += hsw;
685
686 /* horizontal front porch */
687 hfp = timing->hfront_porch.typ * mul / div;
688
689 /* subtract packet overhead */
690 hsw -= 10;
691 hbp -= 14;
692 hfp -= 8;
693
694 writel(hsw << 16 | 0, &len->dsi_pkt_len_0_1);
695 writel(hact << 16 | hbp, &len->dsi_pkt_len_2_3);
696 writel(hfp, &len->dsi_pkt_len_4_5);
697 writel(0x0f0f << 16, &len->dsi_pkt_len_6_7);
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300698
699 /* set SOL delay (for non-burst mode only) */
700 writel(8 * mul / div, &misc->dsi_sol_delay);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300701 } else {
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300702 if (priv->master || priv->slave) {
703 /*
704 * For ganged mode, assume symmetric left-right mode.
705 */
706 value = 1 + (timing->hactive.typ / 2) * mul / div;
707 } else {
708 /* 1 byte (DCS command) + pixel data */
709 value = 1 + timing->hactive.typ * mul / div;
710 }
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300711
712 writel(0, &len->dsi_pkt_len_0_1);
713 writel(value << 16, &len->dsi_pkt_len_2_3);
714 writel(value << 16, &len->dsi_pkt_len_4_5);
715 writel(0, &len->dsi_pkt_len_6_7);
716
717 value = MIPI_DCS_WRITE_MEMORY_START << 8 |
718 MIPI_DCS_WRITE_MEMORY_CONTINUE;
719 writel(value, &len->dsi_dcs_cmds);
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300720
721 /* set SOL delay */
722 if (priv->master || priv->slave) {
723 unsigned long delay, bclk, bclk_ganged;
724 unsigned int lanes = device->lanes;
725 unsigned long htotal = timing->hactive.typ + timing->hfront_porch.typ +
726 timing->hback_porch.typ + timing->hsync_len.typ;
727
728 /* SOL to valid, valid to FIFO and FIFO write delay */
729 delay = 4 + 4 + 2;
730 delay = DIV_ROUND_UP(delay * mul, div * lanes);
731 /* FIFO read delay */
732 delay = delay + 6;
733
734 bclk = DIV_ROUND_UP(htotal * mul, div * lanes);
735 bclk_ganged = DIV_ROUND_UP(bclk * lanes / 2, lanes);
736 value = bclk - bclk_ganged + delay + 20;
737 } else {
738 /* TODO: revisit for non-ganged mode */
739 value = 8 * mul / div;
740 }
741
742 writel(value, &misc->dsi_sol_delay);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300743 }
744
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300745 if (priv->slave) {
746 /*
747 * TODO: Support modes other than symmetrical left-right
748 * split.
749 */
750 tegra_dsi_ganged_enable(dev, 0, timing->hactive.typ / 2);
751 tegra_dsi_ganged_enable(priv->slave, timing->hactive.typ / 2,
752 timing->hactive.typ / 2);
753 }
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300754}
755
756static int tegra_dsi_encoder_enable(struct udevice *dev)
757{
758 struct tegra_dsi_priv *priv = dev_get_priv(dev);
759 struct mipi_dsi_device *device = &priv->device;
760 struct display_timing *timing = &priv->timing;
761 struct dsi_misc_reg *misc = &priv->dsi->misc;
762 unsigned int mul, div;
763 unsigned long bclk, plld, period;
764 u32 value;
765 int ret;
766
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200767 /* If for some reasone DSI is enabled then it needs to
768 * be disabled in order for the panel initialization
769 * commands to be properly sent.
770 */
771 value = readl(&misc->dsi_pwr_ctrl);
772
773 if (value & DSI_POWER_CONTROL_ENABLE) {
774 value = readl(&misc->dsi_pwr_ctrl);
775 value &= ~DSI_POWER_CONTROL_ENABLE;
776 writel(value, &misc->dsi_pwr_ctrl);
777 }
778
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300779 /* Disable interrupt */
780 writel(0, &misc->int_enable);
781
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200782 if (priv->version)
783 tegra_dsi_mipi_calibrate(priv);
784 else
785 tegra_dsi_pad_calibrate(&priv->dsi->pad);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300786
787 tegra_dsi_get_muldiv(device->format, &mul, &div);
788
789 /* compute byte clock */
790 bclk = (timing->pixelclock.typ * mul) / (div * device->lanes);
791
792 tegra_dsi_set_timeout(&priv->dsi->timeout, bclk, 60);
793
794 /*
795 * Compute bit clock and round up to the next MHz.
796 */
797 plld = DIV_ROUND_UP(bclk * 8, USEC_PER_SEC) * USEC_PER_SEC;
798 period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, plld);
799
800 ret = mipi_dphy_timing_get_default(&priv->dphy_timing, period);
801 if (ret < 0) {
802 printf("%s: failed to get D-PHY timing: %d\n", __func__, ret);
803 return ret;
804 }
805
806 ret = mipi_dphy_timing_validate(&priv->dphy_timing, period);
807 if (ret < 0) {
808 printf("%s: failed to validate D-PHY timing: %d\n", __func__, ret);
809 return ret;
810 }
811
812 /*
813 * The D-PHY timing fields are expressed in byte-clock cycles, so
814 * multiply the period by 8.
815 */
816 tegra_dsi_set_phy_timing(&priv->dsi->ptiming,
817 period * 8, &priv->dphy_timing);
818
819 /* Perform panel HW setup */
820 ret = panel_enable_backlight(priv->panel);
821 if (ret)
822 return ret;
823
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300824 tegra_dsi_configure(dev, device->mode_flags);
825
826 tegra_dc_enable_controller(dev);
827
828 /* enable DSI controller */
829 value = readl(&misc->dsi_pwr_ctrl);
830 value |= DSI_POWER_CONTROL_ENABLE;
831 writel(value, &misc->dsi_pwr_ctrl);
832
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300833 if (priv->slave)
834 tegra_dsi_encoder_enable(priv->slave);
835
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300836 return 0;
837}
838
839static int tegra_dsi_bridge_set_panel(struct udevice *dev, int percent)
840{
Jonas Schwöbeldea5d962024-01-23 19:16:33 +0200841 struct tegra_dsi_priv *priv = dev_get_priv(dev);
842
843 /* Turn on/off backlight */
844 return panel_set_backlight(priv->panel, percent);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300845}
846
847static int tegra_dsi_panel_timings(struct udevice *dev,
848 struct display_timing *timing)
849{
850 struct tegra_dsi_priv *priv = dev_get_priv(dev);
851
852 memcpy(timing, &priv->timing, sizeof(*timing));
853
854 return 0;
855}
856
857static void tegra_dsi_init_clocks(struct udevice *dev)
858{
859 struct tegra_dsi_priv *priv = dev_get_priv(dev);
Svyatoslav Ryheld16c1052024-01-23 19:16:23 +0200860 struct tegra_dc_plat *dc_plat = dev_get_plat(dev);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300861 struct mipi_dsi_device *device = &priv->device;
862 unsigned int mul, div;
863 unsigned long bclk, plld;
864
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300865 if (!priv->slave) {
866 /* Change DSIB clock parent to match DSIA */
867 struct clk_rst_ctlr *clkrst =
868 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
869
870 clrbits_le32(&clkrst->plld2.pll_base, BIT(25)); /* DSIB_CLK_SRC */
871 }
872
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300873 tegra_dsi_get_muldiv(device->format, &mul, &div);
874
875 bclk = (priv->timing.pixelclock.typ * mul) /
876 (div * device->lanes);
877
878 plld = DIV_ROUND_UP(bclk * 8, USEC_PER_SEC);
879
Svyatoslav Ryheld16c1052024-01-23 19:16:23 +0200880 dc_plat->scdiv = ((plld * USEC_PER_SEC +
881 priv->timing.pixelclock.typ / 2) /
882 priv->timing.pixelclock.typ) - 2;
883
884 /*
885 * BUG: If DISP1 is a PLLD/D2 child, it cannot go over 370MHz. The
886 * cause of this is not quite clear. This can be overcomed by
887 * halving the PLLD/D2 if the target rate is > 800MHz. This way
888 * DISP1 and DSI clocks will be equal.
889 */
890 if (plld > 800)
891 plld /= 2;
892
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300893 switch (clock_get_osc_freq()) {
894 case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
895 case CLOCK_OSC_FREQ_48_0: /* OSC is 48Mhz */
896 clock_set_rate(CLOCK_ID_DISPLAY, plld, 12, 0, 8);
897 break;
898
899 case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
900 clock_set_rate(CLOCK_ID_DISPLAY, plld, 26, 0, 8);
901 break;
902
903 case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
904 case CLOCK_OSC_FREQ_16_8: /* OSC is 16.8Mhz */
905 clock_set_rate(CLOCK_ID_DISPLAY, plld, 13, 0, 8);
906 break;
907
908 case CLOCK_OSC_FREQ_19_2:
909 case CLOCK_OSC_FREQ_38_4:
910 default:
911 /*
912 * These are not supported.
913 */
914 break;
915 }
916
917 priv->dsi_clk = clock_decode_periph_id(dev);
918
919 clock_enable(priv->dsi_clk);
920 udelay(2);
921 reset_set_enable(priv->dsi_clk, 0);
922}
923
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300924static int tegra_dsi_ganged_probe(struct udevice *dev)
925{
926 struct tegra_dsi_priv *mpriv = dev_get_priv(dev);
927 struct udevice *gangster;
928
929 uclass_get_device_by_phandle(UCLASS_PANEL, dev,
930 "nvidia,ganged-mode", &gangster);
931 if (gangster) {
932 /* Ganged mode is set */
933 struct tegra_dsi_priv *spriv = dev_get_priv(gangster);
934
935 mpriv->slave = gangster;
936 spriv->master = dev;
937 }
938
939 return 0;
940}
941
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300942static int tegra_dsi_bridge_probe(struct udevice *dev)
943{
944 struct tegra_dsi_priv *priv = dev_get_priv(dev);
945 struct mipi_dsi_device *device = &priv->device;
946 struct mipi_dsi_panel_plat *mipi_plat;
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +0200947 struct reset_ctl reset_ctl;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300948 int ret;
949
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200950 priv->version = dev_get_driver_data(dev);
951
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300952 priv->dsi = (struct dsi_ctlr *)dev_read_addr_ptr(dev);
953 if (!priv->dsi) {
954 printf("%s: No display controller address\n", __func__);
955 return -EINVAL;
956 }
957
Jonas Schwöbel0cccf902024-01-23 19:16:32 +0200958 priv->video_fifo_depth = 1920;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300959 priv->host_fifo_depth = 64;
960
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300961 tegra_dsi_ganged_probe(dev);
962
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +0200963 ret = reset_get_by_name(dev, "dsi", &reset_ctl);
964 if (ret) {
965 log_debug("%s: reset_get_by_name() failed: %d\n",
966 __func__, ret);
967 return ret;
968 }
969
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300970 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
971 "avdd-dsi-csi-supply", &priv->avdd);
972 if (ret)
973 debug("%s: Cannot get avdd-dsi-csi-supply: error %d\n",
974 __func__, ret);
975
976 ret = uclass_get_device_by_phandle(UCLASS_PANEL, dev,
977 "panel", &priv->panel);
978 if (ret) {
979 printf("%s: Cannot get panel: error %d\n", __func__, ret);
980 return log_ret(ret);
981 }
982
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200983 if (priv->version) {
984 ret = uclass_get_device_by_phandle(UCLASS_MISC, dev,
985 "nvidia,mipi-calibrate",
986 &priv->mipi);
987 if (ret) {
988 log_debug("%s: cannot get MIPI: error %d\n", __func__, ret);
989 return ret;
990 }
991 }
992
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300993 panel_get_display_timing(priv->panel, &priv->timing);
994
995 mipi_plat = dev_get_plat(priv->panel);
996 mipi_plat->device = device;
997
998 priv->host.dev = (struct device *)dev;
999 priv->host.ops = &tegra_dsi_bridge_host_ops;
1000
1001 device->host = &priv->host;
1002 device->lanes = mipi_plat->lanes;
1003 device->format = mipi_plat->format;
1004 device->mode_flags = mipi_plat->mode_flags;
1005
1006 tegra_dsi_get_format(device->format, &priv->format);
1007
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +02001008 reset_assert(&reset_ctl);
1009
Svyatoslav Ryheldaab7d92023-10-03 09:25:34 +03001010 ret = regulator_set_enable_if_allowed(priv->avdd, true);
1011 if (ret && ret != -ENOSYS)
1012 return ret;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001013
1014 tegra_dsi_init_clocks(dev);
1015
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +02001016 mdelay(2);
1017 reset_deassert(&reset_ctl);
1018
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001019 return 0;
1020}
1021
1022static const struct panel_ops tegra_dsi_bridge_ops = {
1023 .enable_backlight = tegra_dsi_encoder_enable,
1024 .set_backlight = tegra_dsi_bridge_set_panel,
1025 .get_display_timing = tegra_dsi_panel_timings,
1026};
1027
1028static const struct udevice_id tegra_dsi_bridge_ids[] = {
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +02001029 { .compatible = "nvidia,tegra30-dsi", .data = DSI_V0 },
1030 { .compatible = "nvidia,tegra114-dsi", .data = DSI_V1 },
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001031 { }
1032};
1033
1034U_BOOT_DRIVER(tegra_dsi) = {
1035 .name = "tegra_dsi",
1036 .id = UCLASS_PANEL,
1037 .of_match = tegra_dsi_bridge_ids,
1038 .ops = &tegra_dsi_bridge_ops,
1039 .probe = tegra_dsi_bridge_probe,
1040 .plat_auto = sizeof(struct tegra_dc_plat),
1041 .priv_auto = sizeof(struct tegra_dsi_priv),
1042};