blob: a2a22fa0fe2de4ad8566603e0a4a76a60c4705f3 [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>
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +02008#include <clk.h>
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03009#include <log.h>
10#include <misc.h>
11#include <mipi_display.h>
12#include <mipi_dsi.h>
13#include <backlight.h>
Svyatoslav Ryhel90f5e6e2025-02-14 15:24:13 +020014#include <video_bridge.h>
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030015#include <panel.h>
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +020016#include <reset.h>
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030017#include <linux/delay.h>
18#include <linux/err.h>
Igor Prusovc3421ea2023-11-09 20:10:04 +030019#include <linux/time.h>
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030020#include <power/regulator.h>
21
22#include <asm/gpio.h>
23#include <asm/io.h>
24#include <asm/arch/clock.h>
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +030025#include <asm/arch-tegra/clk_rst.h>
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030026
Svyatoslav Ryhel75fec412024-01-23 19:16:18 +020027#include "tegra-dc.h"
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +020028#include "tegra-dsi.h"
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030029#include "mipi-phy.h"
30
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +020031/* List of supported DSI bridges */
32enum {
33 DSI_V0,
34 DSI_V1,
35};
36
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030037struct tegra_dsi_priv {
38 struct mipi_dsi_host host;
39 struct mipi_dsi_device device;
40 struct mipi_dphy_timing dphy_timing;
41
42 struct udevice *panel;
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +020043 struct udevice *mipi;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030044 struct display_timing timing;
45
46 struct dsi_ctlr *dsi;
47 struct udevice *avdd;
48
49 enum tegra_dsi_format format;
50
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +020051 struct clk *clk;
52 struct clk *clk_parent;
53
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030054 int video_fifo_depth;
55 int host_fifo_depth;
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +020056
Svyatoslav Ryhel30cdefe2024-11-18 08:58:18 +020057 u32 calibration_pads;
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +020058 u32 version;
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +030059
60 /* for ganged-mode support */
61 struct udevice *master;
62 struct udevice *slave;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030063};
64
65static void tegra_dc_enable_controller(struct udevice *dev)
66{
67 struct tegra_dc_plat *dc_plat = dev_get_plat(dev);
68 struct dc_ctlr *dc = dc_plat->dc;
69 u32 value;
70
71 value = readl(&dc->disp.disp_win_opt);
72 value |= DSI_ENABLE;
73 writel(value, &dc->disp.disp_win_opt);
74
75 writel(GENERAL_UPDATE, &dc->cmd.state_ctrl);
76 writel(GENERAL_ACT_REQ, &dc->cmd.state_ctrl);
77}
78
79static const char * const error_report[16] = {
80 "SoT Error",
81 "SoT Sync Error",
82 "EoT Sync Error",
83 "Escape Mode Entry Command Error",
84 "Low-Power Transmit Sync Error",
85 "Peripheral Timeout Error",
86 "False Control Error",
87 "Contention Detected",
88 "ECC Error, single-bit",
89 "ECC Error, multi-bit",
90 "Checksum Error",
91 "DSI Data Type Not Recognized",
92 "DSI VC ID Invalid",
93 "Invalid Transmission Length",
94 "Reserved",
95 "DSI Protocol Violation",
96};
97
98static ssize_t tegra_dsi_read_response(struct dsi_misc_reg *misc,
99 const struct mipi_dsi_msg *msg,
100 size_t count)
101{
102 u8 *rx = msg->rx_buf;
103 unsigned int i, j, k;
104 size_t size = 0;
105 u16 errors;
106 u32 value;
107
108 /* read and parse packet header */
109 value = readl(&misc->dsi_rd_data);
110
111 switch (value & 0x3f) {
112 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
113 errors = (value >> 8) & 0xffff;
114 printf("%s: Acknowledge and error report: %04x\n",
115 __func__, errors);
116 for (i = 0; i < ARRAY_SIZE(error_report); i++)
117 if (errors & BIT(i))
118 printf("%s: %2u: %s\n", __func__, i,
119 error_report[i]);
120 break;
121
122 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
123 rx[0] = (value >> 8) & 0xff;
124 size = 1;
125 break;
126
127 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
128 rx[0] = (value >> 8) & 0xff;
129 rx[1] = (value >> 16) & 0xff;
130 size = 2;
131 break;
132
133 case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
134 size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
135 break;
136
137 case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
138 size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
139 break;
140
141 default:
142 printf("%s: unhandled response type: %02x\n",
143 __func__, value & 0x3f);
144 return -EPROTO;
145 }
146
147 size = min(size, msg->rx_len);
148
149 if (msg->rx_buf && size > 0) {
150 for (i = 0, j = 0; i < count - 1; i++, j += 4) {
151 u8 *rx = msg->rx_buf + j;
152
153 value = readl(&misc->dsi_rd_data);
154
155 for (k = 0; k < 4 && (j + k) < msg->rx_len; k++)
156 rx[j + k] = (value >> (k << 3)) & 0xff;
157 }
158 }
159
160 return size;
161}
162
163static int tegra_dsi_transmit(struct dsi_misc_reg *misc,
164 unsigned long timeout)
165{
166 writel(DSI_TRIGGER_HOST, &misc->dsi_trigger);
167
168 while (timeout--) {
169 u32 value = readl(&misc->dsi_trigger);
170
171 if ((value & DSI_TRIGGER_HOST) == 0)
172 return 0;
173
174 udelay(1000);
175 }
176
177 debug("timeout waiting for transmission to complete\n");
178 return -ETIMEDOUT;
179}
180
181static int tegra_dsi_wait_for_response(struct dsi_misc_reg *misc,
182 unsigned long timeout)
183{
184 while (timeout--) {
185 u32 value = readl(&misc->dsi_status);
186 u8 count = value & 0x1f;
187
188 if (count > 0)
189 return count;
190
191 udelay(1000);
192 }
193
194 debug("peripheral returned no data\n");
195 return -ETIMEDOUT;
196}
197
198static void tegra_dsi_writesl(struct dsi_misc_reg *misc,
199 const void *buffer, size_t size)
200{
201 const u8 *buf = buffer;
202 size_t i, j;
203 u32 value;
204
205 for (j = 0; j < size; j += 4) {
206 value = 0;
207
208 for (i = 0; i < 4 && j + i < size; i++)
209 value |= buf[j + i] << (i << 3);
210
211 writel(value, &misc->dsi_wr_data);
212 }
213}
214
215static ssize_t tegra_dsi_host_transfer(struct mipi_dsi_host *host,
216 const struct mipi_dsi_msg *msg)
217{
218 struct udevice *dev = (struct udevice *)host->dev;
219 struct tegra_dsi_priv *priv = dev_get_priv(dev);
220 struct dsi_misc_reg *misc = &priv->dsi->misc;
221 struct mipi_dsi_packet packet;
222 const u8 *header;
223 size_t count;
224 ssize_t err;
225 u32 value;
226
227 err = mipi_dsi_create_packet(&packet, msg);
228 if (err < 0)
229 return err;
230
231 header = packet.header;
232
233 /* maximum FIFO depth is 1920 words */
234 if (packet.size > priv->video_fifo_depth * 4)
235 return -ENOSPC;
236
237 /* reset underflow/overflow flags */
238 value = readl(&misc->dsi_status);
239 if (value & (DSI_STATUS_UNDERFLOW | DSI_STATUS_OVERFLOW)) {
240 value = DSI_HOST_CONTROL_FIFO_RESET;
241 writel(value, &misc->host_dsi_ctrl);
242 udelay(10);
243 }
244
245 value = readl(&misc->dsi_pwr_ctrl);
246 value |= DSI_POWER_CONTROL_ENABLE;
247 writel(value, &misc->dsi_pwr_ctrl);
248
249 mdelay(5);
250
251 value = DSI_HOST_CONTROL_CRC_RESET | DSI_HOST_CONTROL_TX_TRIG_HOST |
252 DSI_HOST_CONTROL_CS | DSI_HOST_CONTROL_ECC;
253
Svyatoslav Ryheld288b8a2025-02-21 14:13:08 +0200254 if ((msg->flags & MIPI_DSI_MSG_USE_LPM) == 0)
255 value |= DSI_HOST_CONTROL_HS;
256
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300257 /*
258 * The host FIFO has a maximum of 64 words, so larger transmissions
259 * need to use the video FIFO.
260 */
261 if (packet.size > priv->host_fifo_depth * 4)
262 value |= DSI_HOST_CONTROL_FIFO_SEL;
263
264 writel(value, &misc->host_dsi_ctrl);
265
266 /*
267 * For reads and messages with explicitly requested ACK, generate a
268 * BTA sequence after the transmission of the packet.
269 */
270 if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
271 (msg->rx_buf && msg->rx_len > 0)) {
272 value = readl(&misc->host_dsi_ctrl);
273 value |= DSI_HOST_CONTROL_PKT_BTA;
274 writel(value, &misc->host_dsi_ctrl);
275 }
276
277 value = DSI_CONTROL_LANES(0) | DSI_CONTROL_HOST_ENABLE;
278 writel(value, &misc->dsi_ctrl);
279
280 /* write packet header, ECC is generated by hardware */
281 value = header[2] << 16 | header[1] << 8 | header[0];
282 writel(value, &misc->dsi_wr_data);
283
284 /* write payload (if any) */
285 if (packet.payload_length > 0)
286 tegra_dsi_writesl(misc, packet.payload,
287 packet.payload_length);
288
289 err = tegra_dsi_transmit(misc, 250);
290 if (err < 0)
291 return err;
292
293 if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
294 (msg->rx_buf && msg->rx_len > 0)) {
295 err = tegra_dsi_wait_for_response(misc, 250);
296 if (err < 0)
297 return err;
298
299 count = err;
300
301 value = readl(&misc->dsi_rd_data);
302 switch (value) {
303 case 0x84:
304 debug("%s: ACK\n", __func__);
305 break;
306
307 case 0x87:
308 debug("%s: ESCAPE\n", __func__);
309 break;
310
311 default:
312 printf("%s: unknown status: %08x\n", __func__, value);
313 break;
314 }
315
316 if (count > 1) {
317 err = tegra_dsi_read_response(misc, msg, count);
318 if (err < 0) {
319 printf("%s: failed to parse response: %zd\n",
320 __func__, err);
321 } else {
322 /*
323 * For read commands, return the number of
324 * bytes returned by the peripheral.
325 */
326 count = err;
327 }
328 }
329 } else {
330 /*
331 * For write commands, we have transmitted the 4-byte header
332 * plus the variable-length payload.
333 */
334 count = 4 + packet.payload_length;
335 }
336
337 return count;
338}
339
340struct mipi_dsi_host_ops tegra_dsi_bridge_host_ops = {
341 .transfer = tegra_dsi_host_transfer,
342};
343
344#define PKT_ID0(id) ((((id) & 0x3f) << 3) | (1 << 9))
345#define PKT_LEN0(len) (((len) & 0x07) << 0)
346#define PKT_ID1(id) ((((id) & 0x3f) << 13) | (1 << 19))
347#define PKT_LEN1(len) (((len) & 0x07) << 10)
348#define PKT_ID2(id) ((((id) & 0x3f) << 23) | (1 << 29))
349#define PKT_LEN2(len) (((len) & 0x07) << 20)
350
351#define PKT_LP BIT(30)
352#define NUM_PKT_SEQ 12
353
354/*
355 * non-burst mode with sync pulses
356 */
357static const u32 pkt_seq_video_non_burst_sync_pulses[NUM_PKT_SEQ] = {
358 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
359 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
360 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
361 PKT_LP,
362 [ 1] = 0,
363 [ 2] = PKT_ID0(MIPI_DSI_V_SYNC_END) | PKT_LEN0(0) |
364 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
365 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
366 PKT_LP,
367 [ 3] = 0,
368 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
369 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
370 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
371 PKT_LP,
372 [ 5] = 0,
373 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
374 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
375 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
376 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
377 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
378 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
379 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
380 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
381 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
382 PKT_LP,
383 [ 9] = 0,
384 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
385 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
386 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
387 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
388 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
389 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
390};
391
392/*
393 * non-burst mode with sync events
394 */
395static const u32 pkt_seq_video_non_burst_sync_events[NUM_PKT_SEQ] = {
396 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
397 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
398 PKT_LP,
399 [ 1] = 0,
400 [ 2] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
401 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
402 PKT_LP,
403 [ 3] = 0,
404 [ 4] = 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 [ 5] = 0,
408 [ 6] = 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 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
412 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
413 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
414 PKT_LP,
415 [ 9] = 0,
416 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
417 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
418 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
419 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
420};
421
422static const u32 pkt_seq_command_mode[NUM_PKT_SEQ] = {
423 [ 0] = 0,
424 [ 1] = 0,
425 [ 2] = 0,
426 [ 3] = 0,
427 [ 4] = 0,
428 [ 5] = 0,
429 [ 6] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(3) | PKT_LP,
430 [ 7] = 0,
431 [ 8] = 0,
432 [ 9] = 0,
433 [10] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(5) | PKT_LP,
434 [11] = 0,
435};
436
437static void tegra_dsi_get_muldiv(enum mipi_dsi_pixel_format format,
438 unsigned int *mulp, unsigned int *divp)
439{
440 switch (format) {
441 case MIPI_DSI_FMT_RGB666_PACKED:
442 case MIPI_DSI_FMT_RGB888:
443 *mulp = 3;
444 *divp = 1;
445 break;
446
447 case MIPI_DSI_FMT_RGB565:
448 *mulp = 2;
449 *divp = 1;
450 break;
451
452 case MIPI_DSI_FMT_RGB666:
453 *mulp = 9;
454 *divp = 4;
455 break;
456
457 default:
458 break;
459 }
460}
461
462static int tegra_dsi_get_format(enum mipi_dsi_pixel_format format,
463 enum tegra_dsi_format *fmt)
464{
465 switch (format) {
466 case MIPI_DSI_FMT_RGB888:
467 *fmt = TEGRA_DSI_FORMAT_24P;
468 break;
469
470 case MIPI_DSI_FMT_RGB666:
471 *fmt = TEGRA_DSI_FORMAT_18NP;
472 break;
473
474 case MIPI_DSI_FMT_RGB666_PACKED:
475 *fmt = TEGRA_DSI_FORMAT_18P;
476 break;
477
478 case MIPI_DSI_FMT_RGB565:
479 *fmt = TEGRA_DSI_FORMAT_16P;
480 break;
481
482 default:
483 return -EINVAL;
484 }
485
486 return 0;
487}
488
489static void tegra_dsi_pad_calibrate(struct dsi_pad_ctrl_reg *pad)
490{
491 u32 value;
492
493 /* start calibration */
494 value = DSI_PAD_CONTROL_PAD_LPUPADJ(0x1) |
495 DSI_PAD_CONTROL_PAD_LPDNADJ(0x1) |
496 DSI_PAD_CONTROL_PAD_PREEMP_EN(0x1) |
497 DSI_PAD_CONTROL_PAD_SLEWDNADJ(0x6) |
498 DSI_PAD_CONTROL_PAD_SLEWUPADJ(0x6) |
499 DSI_PAD_CONTROL_PAD_PDIO(0) |
500 DSI_PAD_CONTROL_PAD_PDIO_CLK(0) |
501 DSI_PAD_CONTROL_PAD_PULLDN_ENAB(0);
502 writel(value, &pad->pad_ctrl);
503
504 clock_enable(PERIPH_ID_VI);
505 clock_enable(PERIPH_ID_CSI);
506 udelay(2);
507 reset_set_enable(PERIPH_ID_VI, 0);
508 reset_set_enable(PERIPH_ID_CSI, 0);
509
510 value = MIPI_CAL_TERMOSA(0x4);
511 writel(value, TEGRA_VI_BASE + (CSI_CILA_MIPI_CAL_CONFIG_0 << 2));
512
513 value = MIPI_CAL_TERMOSB(0x4);
514 writel(value, TEGRA_VI_BASE + (CSI_CILB_MIPI_CAL_CONFIG_0 << 2));
515
516 value = MIPI_CAL_HSPUOSD(0x3) | MIPI_CAL_HSPDOSD(0x4);
517 writel(value, TEGRA_VI_BASE + (CSI_DSI_MIPI_CAL_CONFIG << 2));
518
519 value = PAD_DRIV_DN_REF(0x5) | PAD_DRIV_UP_REF(0x7);
520 writel(value, TEGRA_VI_BASE + (CSI_MIPIBIAS_PAD_CONFIG << 2));
521
522 value = PAD_CIL_PDVREG(0x0);
523 writel(value, TEGRA_VI_BASE + (CSI_CIL_PAD_CONFIG << 2));
524}
525
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200526static void tegra_dsi_mipi_calibrate(struct udevice *dev)
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200527{
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200528 struct tegra_dsi_priv *priv = dev_get_priv(dev);
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200529 struct dsi_pad_ctrl_reg *pad = &priv->dsi->pad;
530 u32 value;
531 int ret;
532
533 ret = misc_set_enabled(priv->mipi, true);
534 if (ret)
535 log_debug("%s: failed to enable MIPI calibration: %d\n",
536 __func__, ret);
537
538 writel(0, &pad->pad_ctrl);
539 writel(0, &pad->pad_ctrl_1);
540 writel(0, &pad->pad_ctrl_2);
541 writel(0, &pad->pad_ctrl_3);
542 writel(0, &pad->pad_ctrl_4);
543
544 /* DSI pad enable */
545 value = DSI_PAD_CONTROL_VS1_PULLDN(0) | DSI_PAD_CONTROL_VS1_PDIO(0);
546 writel(value, &pad->pad_ctrl);
547
548 value = DSI_PAD_SLEW_UP(0x7) | DSI_PAD_SLEW_DN(0x7) |
549 DSI_PAD_LP_UP(0x1) | DSI_PAD_LP_DN(0x1) |
550 DSI_PAD_OUT_CLK(0x0);
551 writel(value, &pad->pad_ctrl_2);
552
553 value = DSI_PAD_PREEMP_PD_CLK(0x3) | DSI_PAD_PREEMP_PU_CLK(0x3) |
554 DSI_PAD_PREEMP_PD(0x03) | DSI_PAD_PREEMP_PU(0x3);
555 writel(value, &pad->pad_ctrl_3);
556
Svyatoslav Ryhel30cdefe2024-11-18 08:58:18 +0200557 ret = misc_write(priv->mipi, priv->calibration_pads, NULL, 0);
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200558 if (ret)
559 log_debug("%s: MIPI calibration failed %d\n", __func__, ret);
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200560
561 if (priv->slave)
562 tegra_dsi_mipi_calibrate(priv->slave);
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200563}
564
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200565static void tegra_dsi_set_timeout(struct udevice *dev,
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300566 unsigned long bclk,
567 unsigned int vrefresh)
568{
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200569 struct tegra_dsi_priv *priv = dev_get_priv(dev);
570 struct dsi_timeout_reg *rtimeout = &priv->dsi->timeout;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300571 unsigned int timeout;
572 u32 value;
573
574 /* one frame high-speed transmission timeout */
575 timeout = (bclk / vrefresh) / 512;
576 value = DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout);
577 writel(value, &rtimeout->dsi_timeout_0);
578
579 /* 2 ms peripheral timeout for panel */
580 timeout = 2 * bclk / 512 * 1000;
581 value = DSI_TIMEOUT_PR(timeout) | DSI_TIMEOUT_TA(0x2000);
582 writel(value, &rtimeout->dsi_timeout_1);
583
584 value = DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
585 writel(value, &rtimeout->dsi_to_tally);
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200586
587 if (priv->slave)
588 tegra_dsi_set_timeout(priv->slave, bclk, vrefresh);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300589}
590
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200591static void tegra_dsi_set_phy_timing(struct udevice *dev,
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300592 unsigned long period,
593 const struct mipi_dphy_timing *dphy_timing)
594{
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200595 struct tegra_dsi_priv *priv = dev_get_priv(dev);
596 struct dsi_timing_reg *ptiming = &priv->dsi->ptiming;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300597 u32 value;
598
599 value = DSI_TIMING_FIELD(dphy_timing->hsexit, period, 1) << 24 |
600 DSI_TIMING_FIELD(dphy_timing->hstrail, period, 0) << 16 |
601 DSI_TIMING_FIELD(dphy_timing->hszero, period, 3) << 8 |
602 DSI_TIMING_FIELD(dphy_timing->hsprepare, period, 1);
603 writel(value, &ptiming->dsi_phy_timing_0);
604
605 value = DSI_TIMING_FIELD(dphy_timing->clktrail, period, 1) << 24 |
606 DSI_TIMING_FIELD(dphy_timing->clkpost, period, 1) << 16 |
607 DSI_TIMING_FIELD(dphy_timing->clkzero, period, 1) << 8 |
608 DSI_TIMING_FIELD(dphy_timing->lpx, period, 1);
609 writel(value, &ptiming->dsi_phy_timing_1);
610
611 value = DSI_TIMING_FIELD(dphy_timing->clkprepare, period, 1) << 16 |
612 DSI_TIMING_FIELD(dphy_timing->clkpre, period, 1) << 8 |
613 DSI_TIMING_FIELD(0xff * period, period, 0) << 0;
614 writel(value, &ptiming->dsi_phy_timing_2);
615
616 value = DSI_TIMING_FIELD(dphy_timing->taget, period, 1) << 16 |
617 DSI_TIMING_FIELD(dphy_timing->tasure, period, 1) << 8 |
618 DSI_TIMING_FIELD(dphy_timing->tago, period, 1);
619 writel(value, &ptiming->dsi_bta_timing);
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200620
621 if (priv->slave)
622 tegra_dsi_set_phy_timing(priv->slave, period, dphy_timing);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300623}
624
Svyatoslav Ryhelf87af5a2025-02-26 09:51:09 +0200625static u32 tegra_dsi_get_lanes(struct udevice *dev)
626{
627 struct tegra_dsi_priv *priv = dev_get_priv(dev);
628 struct mipi_dsi_device *device = &priv->device;
629
630 if (priv->master) {
631 struct tegra_dsi_priv *mpriv = dev_get_priv(priv->master);
632 struct mipi_dsi_device *mdevice = &mpriv->device;
633
634 return mdevice->lanes + device->lanes;
635 }
636
637 if (priv->slave) {
638 struct tegra_dsi_priv *spriv = dev_get_priv(priv->slave);
639 struct mipi_dsi_device *sdevice = &spriv->device;
640
641 return device->lanes + sdevice->lanes;
642 }
643
644 return device->lanes;
645}
646
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300647static void tegra_dsi_ganged_enable(struct udevice *dev, unsigned int start,
648 unsigned int size)
649{
650 struct tegra_dsi_priv *priv = dev_get_priv(dev);
651 struct dsi_ganged_mode_reg *ganged = &priv->dsi->ganged;
652
653 writel(start, &ganged->ganged_mode_start);
654 writel(size << 16 | size, &ganged->ganged_mode_size);
655 writel(DSI_GANGED_MODE_CONTROL_ENABLE, &ganged->ganged_mode_ctrl);
656}
657
Svyatoslav Ryhel6a5efea2024-12-31 09:58:01 +0200658static void tegra_dsi_configure(struct udevice *dev, unsigned int pipe,
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300659 unsigned long mode_flags)
660{
661 struct tegra_dsi_priv *priv = dev_get_priv(dev);
662 struct mipi_dsi_device *device = &priv->device;
663 struct display_timing *timing = &priv->timing;
664
665 struct dsi_misc_reg *misc = &priv->dsi->misc;
666 struct dsi_pkt_seq_reg *pkt = &priv->dsi->pkt;
667 struct dsi_pkt_len_reg *len = &priv->dsi->len;
668
669 unsigned int hact, hsw, hbp, hfp, i, mul, div;
670 const u32 *pkt_seq;
671 u32 value;
672
673 tegra_dsi_get_muldiv(device->format, &mul, &div);
674
675 if (mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
676 printf("[DSI] Non-burst video mode with sync pulses\n");
677 pkt_seq = pkt_seq_video_non_burst_sync_pulses;
678 } else if (mode_flags & MIPI_DSI_MODE_VIDEO) {
679 printf("[DSI] Non-burst video mode with sync events\n");
680 pkt_seq = pkt_seq_video_non_burst_sync_events;
681 } else {
682 printf("[DSI] Command mode\n");
683 pkt_seq = pkt_seq_command_mode;
684 }
685
686 value = DSI_CONTROL_CHANNEL(0) |
687 DSI_CONTROL_FORMAT(priv->format) |
688 DSI_CONTROL_LANES(device->lanes - 1) |
Svyatoslav Ryhel6a5efea2024-12-31 09:58:01 +0200689 DSI_CONTROL_SOURCE(pipe);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300690 writel(value, &misc->dsi_ctrl);
691
692 writel(priv->video_fifo_depth, &misc->dsi_max_threshold);
693
694 value = DSI_HOST_CONTROL_HS;
695 writel(value, &misc->host_dsi_ctrl);
696
697 value = readl(&misc->dsi_ctrl);
698
699 if (mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
700 value |= DSI_CONTROL_HS_CLK_CTRL;
701
702 value &= ~DSI_CONTROL_TX_TRIG(3);
703
704 /* enable DCS commands for command mode */
705 if (mode_flags & MIPI_DSI_MODE_VIDEO)
706 value &= ~DSI_CONTROL_DCS_ENABLE;
707 else
708 value |= DSI_CONTROL_DCS_ENABLE;
709
710 value |= DSI_CONTROL_VIDEO_ENABLE;
711 value &= ~DSI_CONTROL_HOST_ENABLE;
712 writel(value, &misc->dsi_ctrl);
713
714 for (i = 0; i < NUM_PKT_SEQ; i++)
715 writel(pkt_seq[i], &pkt->dsi_pkt_seq_0_lo + i);
716
717 if (mode_flags & MIPI_DSI_MODE_VIDEO) {
718 /* horizontal active pixels */
719 hact = timing->hactive.typ * mul / div;
720
721 /* horizontal sync width */
722 hsw = timing->hsync_len.typ * mul / div;
723
724 /* horizontal back porch */
725 hbp = timing->hback_porch.typ * mul / div;
726
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300727 /* horizontal front porch */
728 hfp = timing->hfront_porch.typ * mul / div;
729
Svyatoslav Ryhel10833fb2024-12-02 08:12:36 +0200730 if (priv->master || priv->slave) {
731 hact /= 2;
732 hsw /= 2;
733 hbp = hbp / 2 - 1;
734 hfp /= 2;
735 }
736
737 if ((mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) == 0)
738 hbp += hsw;
739
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300740 /* subtract packet overhead */
741 hsw -= 10;
742 hbp -= 14;
743 hfp -= 8;
744
745 writel(hsw << 16 | 0, &len->dsi_pkt_len_0_1);
746 writel(hact << 16 | hbp, &len->dsi_pkt_len_2_3);
747 writel(hfp, &len->dsi_pkt_len_4_5);
748 writel(0x0f0f << 16, &len->dsi_pkt_len_6_7);
749 } else {
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300750 if (priv->master || priv->slave) {
751 /*
752 * For ganged mode, assume symmetric left-right mode.
753 */
754 value = 1 + (timing->hactive.typ / 2) * mul / div;
755 } else {
756 /* 1 byte (DCS command) + pixel data */
757 value = 1 + timing->hactive.typ * mul / div;
758 }
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300759
760 writel(0, &len->dsi_pkt_len_0_1);
761 writel(value << 16, &len->dsi_pkt_len_2_3);
762 writel(value << 16, &len->dsi_pkt_len_4_5);
763 writel(0, &len->dsi_pkt_len_6_7);
764
765 value = MIPI_DCS_WRITE_MEMORY_START << 8 |
766 MIPI_DCS_WRITE_MEMORY_CONTINUE;
767 writel(value, &len->dsi_dcs_cmds);
Svyatoslav Ryhel18e4dcf2024-12-02 08:08:03 +0200768 }
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300769
Svyatoslav Ryhel18e4dcf2024-12-02 08:08:03 +0200770 /* set SOL delay */
771 if (priv->master || priv->slave) {
772 unsigned long delay, bclk, bclk_ganged;
Svyatoslav Ryhelf87af5a2025-02-26 09:51:09 +0200773 unsigned int lanes = tegra_dsi_get_lanes(dev);
Svyatoslav Ryhel18e4dcf2024-12-02 08:08:03 +0200774 unsigned long htotal = timing->hactive.typ + timing->hfront_porch.typ +
775 timing->hback_porch.typ + timing->hsync_len.typ;
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300776
Svyatoslav Ryhel18e4dcf2024-12-02 08:08:03 +0200777 /* SOL to valid, valid to FIFO and FIFO write delay */
778 delay = 4 + 4 + 2;
779 delay = DIV_ROUND_UP(delay * mul, div * lanes);
780 /* FIFO read delay */
781 delay = delay + 6;
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300782
Svyatoslav Ryhel18e4dcf2024-12-02 08:08:03 +0200783 bclk = DIV_ROUND_UP(htotal * mul, div * lanes);
784 bclk_ganged = DIV_ROUND_UP(bclk * lanes / 2, lanes);
785 value = bclk - bclk_ganged + delay + 20;
786 } else {
787 /* set SOL delay (for non-burst mode only) */
788 value = 8 * mul / div;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300789 }
790
Svyatoslav Ryhel18e4dcf2024-12-02 08:08:03 +0200791 writel(value, &misc->dsi_sol_delay);
792
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300793 if (priv->slave) {
Svyatoslav Ryhel6a5efea2024-12-31 09:58:01 +0200794 tegra_dsi_configure(priv->slave, pipe, mode_flags);
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300795 /*
796 * TODO: Support modes other than symmetrical left-right
797 * split.
798 */
799 tegra_dsi_ganged_enable(dev, 0, timing->hactive.typ / 2);
800 tegra_dsi_ganged_enable(priv->slave, timing->hactive.typ / 2,
801 timing->hactive.typ / 2);
802 }
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300803}
804
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200805static void tegra_dsi_enable(struct udevice *dev)
806{
807 struct tegra_dsi_priv *priv = dev_get_priv(dev);
808 struct dsi_misc_reg *misc = &priv->dsi->misc;
809 u32 value;
810
811 /* enable DSI controller */
812 value = readl(&misc->dsi_pwr_ctrl);
813 value |= DSI_POWER_CONTROL_ENABLE;
814 writel(value, &misc->dsi_pwr_ctrl);
815
816 if (priv->slave)
817 tegra_dsi_enable(priv->slave);
818}
819
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300820static int tegra_dsi_encoder_enable(struct udevice *dev)
821{
822 struct tegra_dsi_priv *priv = dev_get_priv(dev);
Svyatoslav Ryhel6a5efea2024-12-31 09:58:01 +0200823 struct tegra_dc_plat *dc_plat = dev_get_plat(dev);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300824 struct mipi_dsi_device *device = &priv->device;
825 struct display_timing *timing = &priv->timing;
826 struct dsi_misc_reg *misc = &priv->dsi->misc;
827 unsigned int mul, div;
828 unsigned long bclk, plld, period;
Svyatoslav Ryhelf87af5a2025-02-26 09:51:09 +0200829 u32 value, lanes;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300830 int ret;
831
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200832 /* If for some reasone DSI is enabled then it needs to
833 * be disabled in order for the panel initialization
834 * commands to be properly sent.
835 */
836 value = readl(&misc->dsi_pwr_ctrl);
837
838 if (value & DSI_POWER_CONTROL_ENABLE) {
839 value = readl(&misc->dsi_pwr_ctrl);
840 value &= ~DSI_POWER_CONTROL_ENABLE;
841 writel(value, &misc->dsi_pwr_ctrl);
842 }
843
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300844 /* Disable interrupt */
845 writel(0, &misc->int_enable);
846
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200847 if (priv->version)
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200848 tegra_dsi_mipi_calibrate(dev);
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200849 else
850 tegra_dsi_pad_calibrate(&priv->dsi->pad);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300851
852 tegra_dsi_get_muldiv(device->format, &mul, &div);
853
854 /* compute byte clock */
Svyatoslav Ryhelf87af5a2025-02-26 09:51:09 +0200855 lanes = tegra_dsi_get_lanes(dev);
856 bclk = (timing->pixelclock.typ * mul) / (div * lanes);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300857
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200858 tegra_dsi_set_timeout(dev, bclk, 60);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300859
860 /*
861 * Compute bit clock and round up to the next MHz.
862 */
863 plld = DIV_ROUND_UP(bclk * 8, USEC_PER_SEC) * USEC_PER_SEC;
864 period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, plld);
865
866 ret = mipi_dphy_timing_get_default(&priv->dphy_timing, period);
867 if (ret < 0) {
868 printf("%s: failed to get D-PHY timing: %d\n", __func__, ret);
869 return ret;
870 }
871
872 ret = mipi_dphy_timing_validate(&priv->dphy_timing, period);
873 if (ret < 0) {
874 printf("%s: failed to validate D-PHY timing: %d\n", __func__, ret);
875 return ret;
876 }
877
878 /*
879 * The D-PHY timing fields are expressed in byte-clock cycles, so
880 * multiply the period by 8.
881 */
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200882 tegra_dsi_set_phy_timing(dev, period * 8, &priv->dphy_timing);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300883
884 /* Perform panel HW setup */
885 ret = panel_enable_backlight(priv->panel);
886 if (ret)
887 return ret;
888
Svyatoslav Ryhel6a5efea2024-12-31 09:58:01 +0200889 tegra_dsi_configure(dev, dc_plat->pipe, device->mode_flags);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300890
891 tegra_dc_enable_controller(dev);
892
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200893 tegra_dsi_enable(dev);
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300894
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300895 return 0;
896}
897
898static int tegra_dsi_bridge_set_panel(struct udevice *dev, int percent)
899{
Jonas Schwöbeldea5d962024-01-23 19:16:33 +0200900 struct tegra_dsi_priv *priv = dev_get_priv(dev);
901
902 /* Turn on/off backlight */
903 return panel_set_backlight(priv->panel, percent);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300904}
905
906static int tegra_dsi_panel_timings(struct udevice *dev,
907 struct display_timing *timing)
908{
909 struct tegra_dsi_priv *priv = dev_get_priv(dev);
910
911 memcpy(timing, &priv->timing, sizeof(*timing));
912
913 return 0;
914}
915
916static void tegra_dsi_init_clocks(struct udevice *dev)
917{
918 struct tegra_dsi_priv *priv = dev_get_priv(dev);
Svyatoslav Ryheld16c1052024-01-23 19:16:23 +0200919 struct tegra_dc_plat *dc_plat = dev_get_plat(dev);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300920 struct mipi_dsi_device *device = &priv->device;
Svyatoslav Ryhelf87af5a2025-02-26 09:51:09 +0200921 unsigned int mul, div, lanes;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300922 unsigned long bclk, plld;
923
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +0200924 /* Switch parents of DSI clocks in case of not standard parent */
925 if (priv->clk->id == PERIPH_ID_DSI &&
926 priv->clk_parent->id == CLOCK_ID_DISPLAY2) {
927 /* Change DSIA clock parent to PLLD2 */
928 struct clk_rst_ctlr *clkrst =
929 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
930
931 /* DSIA_CLK_SRC */
932 setbits_le32(&clkrst->crc_pll[CLOCK_ID_DISPLAY].pll_base,
933 BIT(25));
934 }
935
936 if (priv->clk->id == PERIPH_ID_DSIB &&
937 priv->clk_parent->id == CLOCK_ID_DISPLAY) {
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300938 /* Change DSIB clock parent to match DSIA */
939 struct clk_rst_ctlr *clkrst =
940 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
941
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +0200942 /* DSIB_CLK_SRC */
943 clrbits_le32(&clkrst->plld2.pll_base, BIT(25));
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300944 }
945
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300946 tegra_dsi_get_muldiv(device->format, &mul, &div);
947
Svyatoslav Ryhelf87af5a2025-02-26 09:51:09 +0200948 lanes = tegra_dsi_get_lanes(dev);
949 bclk = (priv->timing.pixelclock.typ * mul) / (div * lanes);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300950
951 plld = DIV_ROUND_UP(bclk * 8, USEC_PER_SEC);
952
Svyatoslav Ryheld16c1052024-01-23 19:16:23 +0200953 dc_plat->scdiv = ((plld * USEC_PER_SEC +
954 priv->timing.pixelclock.typ / 2) /
955 priv->timing.pixelclock.typ) - 2;
956
957 /*
958 * BUG: If DISP1 is a PLLD/D2 child, it cannot go over 370MHz. The
959 * cause of this is not quite clear. This can be overcomed by
960 * halving the PLLD/D2 if the target rate is > 800MHz. This way
961 * DISP1 and DSI clocks will be equal.
962 */
963 if (plld > 800)
964 plld /= 2;
965
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300966 switch (clock_get_osc_freq()) {
967 case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
968 case CLOCK_OSC_FREQ_48_0: /* OSC is 48Mhz */
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +0200969 clock_set_rate(priv->clk_parent->id, plld, 12, 0, 8);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300970 break;
971
972 case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +0200973 clock_set_rate(priv->clk_parent->id, plld, 26, 0, 8);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300974 break;
975
976 case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
977 case CLOCK_OSC_FREQ_16_8: /* OSC is 16.8Mhz */
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +0200978 clock_set_rate(priv->clk_parent->id, plld, 13, 0, 8);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300979 break;
980
981 case CLOCK_OSC_FREQ_19_2:
982 case CLOCK_OSC_FREQ_38_4:
983 default:
984 /*
985 * These are not supported.
986 */
987 break;
988 }
989
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +0200990 clk_enable(priv->clk);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300991}
992
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300993static int tegra_dsi_ganged_probe(struct udevice *dev)
994{
995 struct tegra_dsi_priv *mpriv = dev_get_priv(dev);
996 struct udevice *gangster;
997
Svyatoslav Ryhel90f5e6e2025-02-14 15:24:13 +0200998 uclass_get_device_by_phandle(UCLASS_VIDEO_BRIDGE, dev,
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300999 "nvidia,ganged-mode", &gangster);
1000 if (gangster) {
1001 /* Ganged mode is set */
1002 struct tegra_dsi_priv *spriv = dev_get_priv(gangster);
1003
1004 mpriv->slave = gangster;
1005 spriv->master = dev;
1006 }
1007
1008 return 0;
1009}
1010
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001011static int tegra_dsi_bridge_probe(struct udevice *dev)
1012{
1013 struct tegra_dsi_priv *priv = dev_get_priv(dev);
1014 struct mipi_dsi_device *device = &priv->device;
1015 struct mipi_dsi_panel_plat *mipi_plat;
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +02001016 struct reset_ctl reset_ctl;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001017 int ret;
1018
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +02001019 priv->version = dev_get_driver_data(dev);
1020
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001021 priv->dsi = (struct dsi_ctlr *)dev_read_addr_ptr(dev);
1022 if (!priv->dsi) {
1023 printf("%s: No display controller address\n", __func__);
1024 return -EINVAL;
1025 }
1026
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +02001027 priv->clk = devm_clk_get(dev, NULL);
1028 if (IS_ERR(priv->clk)) {
1029 log_debug("%s: Could not get DSI clock: %ld\n",
1030 __func__, PTR_ERR(priv->clk));
1031 return PTR_ERR(priv->clk);
1032 }
1033
1034 priv->clk_parent = devm_clk_get(dev, "parent");
1035 if (IS_ERR(priv->clk_parent)) {
1036 log_debug("%s: Could not get DSI clock parent: %ld\n",
1037 __func__, PTR_ERR(priv->clk_parent));
1038 return PTR_ERR(priv->clk_parent);
1039 }
1040
Jonas Schwöbel0cccf902024-01-23 19:16:32 +02001041 priv->video_fifo_depth = 1920;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001042 priv->host_fifo_depth = 64;
1043
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +03001044 tegra_dsi_ganged_probe(dev);
1045
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +02001046 ret = reset_get_by_name(dev, "dsi", &reset_ctl);
1047 if (ret) {
1048 log_debug("%s: reset_get_by_name() failed: %d\n",
1049 __func__, ret);
1050 return ret;
1051 }
1052
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001053 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
1054 "avdd-dsi-csi-supply", &priv->avdd);
1055 if (ret)
1056 debug("%s: Cannot get avdd-dsi-csi-supply: error %d\n",
1057 __func__, ret);
1058
Svyatoslav Ryhelf6b2ab42024-11-24 09:38:03 +02001059 /* Check all DSI children */
1060 device_foreach_child(priv->panel, dev) {
1061 if (device_get_uclass_id(priv->panel) == UCLASS_PANEL)
1062 break;
1063 }
1064
1065 /* if loop exits without panel device return error */
1066 if (device_get_uclass_id(priv->panel) != UCLASS_PANEL) {
1067 log_debug("%s: panel not found, ret %d\n", __func__, ret);
1068 return -EINVAL;
1069 }
1070
1071 ret = uclass_get_device_by_ofnode(UCLASS_PANEL, dev_ofnode(priv->panel),
1072 &priv->panel);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001073 if (ret) {
Svyatoslav Ryhelf6b2ab42024-11-24 09:38:03 +02001074 log_debug("%s: Cannot get panel: error %d\n", __func__, ret);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001075 return log_ret(ret);
1076 }
1077
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +02001078 if (priv->version) {
1079 ret = uclass_get_device_by_phandle(UCLASS_MISC, dev,
1080 "nvidia,mipi-calibrate",
1081 &priv->mipi);
1082 if (ret) {
1083 log_debug("%s: cannot get MIPI: error %d\n", __func__, ret);
1084 return ret;
1085 }
Svyatoslav Ryhel30cdefe2024-11-18 08:58:18 +02001086
1087 ret = dev_read_u32_index(dev, "nvidia,mipi-calibrate", 1,
1088 &priv->calibration_pads);
1089 if (ret) {
1090 log_debug("%s: cannot get calibration pads: error %d\n",
1091 __func__, ret);
1092 return ret;
1093 }
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +02001094 }
1095
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001096 panel_get_display_timing(priv->panel, &priv->timing);
1097
1098 mipi_plat = dev_get_plat(priv->panel);
1099 mipi_plat->device = device;
1100
1101 priv->host.dev = (struct device *)dev;
1102 priv->host.ops = &tegra_dsi_bridge_host_ops;
1103
1104 device->host = &priv->host;
1105 device->lanes = mipi_plat->lanes;
1106 device->format = mipi_plat->format;
1107 device->mode_flags = mipi_plat->mode_flags;
1108
1109 tegra_dsi_get_format(device->format, &priv->format);
1110
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +02001111 reset_assert(&reset_ctl);
1112
Svyatoslav Ryheldaab7d92023-10-03 09:25:34 +03001113 ret = regulator_set_enable_if_allowed(priv->avdd, true);
1114 if (ret && ret != -ENOSYS)
1115 return ret;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001116
1117 tegra_dsi_init_clocks(dev);
1118
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +02001119 mdelay(2);
1120 reset_deassert(&reset_ctl);
1121
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001122 return 0;
1123}
1124
Svyatoslav Ryhel90f5e6e2025-02-14 15:24:13 +02001125static const struct video_bridge_ops tegra_dsi_bridge_ops = {
1126 .attach = tegra_dsi_encoder_enable,
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001127 .set_backlight = tegra_dsi_bridge_set_panel,
1128 .get_display_timing = tegra_dsi_panel_timings,
1129};
1130
1131static const struct udevice_id tegra_dsi_bridge_ids[] = {
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +02001132 { .compatible = "nvidia,tegra30-dsi", .data = DSI_V0 },
1133 { .compatible = "nvidia,tegra114-dsi", .data = DSI_V1 },
Svyatoslav Ryhel5fa06e72024-11-18 08:58:18 +02001134 { .compatible = "nvidia,tegra124-dsi", .data = DSI_V1 },
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001135 { }
1136};
1137
1138U_BOOT_DRIVER(tegra_dsi) = {
1139 .name = "tegra_dsi",
Svyatoslav Ryhel90f5e6e2025-02-14 15:24:13 +02001140 .id = UCLASS_VIDEO_BRIDGE,
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001141 .of_match = tegra_dsi_bridge_ids,
1142 .ops = &tegra_dsi_bridge_ops,
Svyatoslav Ryhelf6b2ab42024-11-24 09:38:03 +02001143 .bind = dm_scan_fdt_dev,
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001144 .probe = tegra_dsi_bridge_probe,
1145 .plat_auto = sizeof(struct tegra_dc_plat),
1146 .priv_auto = sizeof(struct tegra_dsi_priv),
1147};