blob: 3ce0d33e3807a001ca826debcfa13ced53a44c5e [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
254 /*
255 * The host FIFO has a maximum of 64 words, so larger transmissions
256 * need to use the video FIFO.
257 */
258 if (packet.size > priv->host_fifo_depth * 4)
259 value |= DSI_HOST_CONTROL_FIFO_SEL;
260
261 writel(value, &misc->host_dsi_ctrl);
262
263 /*
264 * For reads and messages with explicitly requested ACK, generate a
265 * BTA sequence after the transmission of the packet.
266 */
267 if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
268 (msg->rx_buf && msg->rx_len > 0)) {
269 value = readl(&misc->host_dsi_ctrl);
270 value |= DSI_HOST_CONTROL_PKT_BTA;
271 writel(value, &misc->host_dsi_ctrl);
272 }
273
274 value = DSI_CONTROL_LANES(0) | DSI_CONTROL_HOST_ENABLE;
275 writel(value, &misc->dsi_ctrl);
276
277 /* write packet header, ECC is generated by hardware */
278 value = header[2] << 16 | header[1] << 8 | header[0];
279 writel(value, &misc->dsi_wr_data);
280
281 /* write payload (if any) */
282 if (packet.payload_length > 0)
283 tegra_dsi_writesl(misc, packet.payload,
284 packet.payload_length);
285
286 err = tegra_dsi_transmit(misc, 250);
287 if (err < 0)
288 return err;
289
290 if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
291 (msg->rx_buf && msg->rx_len > 0)) {
292 err = tegra_dsi_wait_for_response(misc, 250);
293 if (err < 0)
294 return err;
295
296 count = err;
297
298 value = readl(&misc->dsi_rd_data);
299 switch (value) {
300 case 0x84:
301 debug("%s: ACK\n", __func__);
302 break;
303
304 case 0x87:
305 debug("%s: ESCAPE\n", __func__);
306 break;
307
308 default:
309 printf("%s: unknown status: %08x\n", __func__, value);
310 break;
311 }
312
313 if (count > 1) {
314 err = tegra_dsi_read_response(misc, msg, count);
315 if (err < 0) {
316 printf("%s: failed to parse response: %zd\n",
317 __func__, err);
318 } else {
319 /*
320 * For read commands, return the number of
321 * bytes returned by the peripheral.
322 */
323 count = err;
324 }
325 }
326 } else {
327 /*
328 * For write commands, we have transmitted the 4-byte header
329 * plus the variable-length payload.
330 */
331 count = 4 + packet.payload_length;
332 }
333
334 return count;
335}
336
337struct mipi_dsi_host_ops tegra_dsi_bridge_host_ops = {
338 .transfer = tegra_dsi_host_transfer,
339};
340
341#define PKT_ID0(id) ((((id) & 0x3f) << 3) | (1 << 9))
342#define PKT_LEN0(len) (((len) & 0x07) << 0)
343#define PKT_ID1(id) ((((id) & 0x3f) << 13) | (1 << 19))
344#define PKT_LEN1(len) (((len) & 0x07) << 10)
345#define PKT_ID2(id) ((((id) & 0x3f) << 23) | (1 << 29))
346#define PKT_LEN2(len) (((len) & 0x07) << 20)
347
348#define PKT_LP BIT(30)
349#define NUM_PKT_SEQ 12
350
351/*
352 * non-burst mode with sync pulses
353 */
354static const u32 pkt_seq_video_non_burst_sync_pulses[NUM_PKT_SEQ] = {
355 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | 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 [ 1] = 0,
360 [ 2] = PKT_ID0(MIPI_DSI_V_SYNC_END) | 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 [ 3] = 0,
365 [ 4] = 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 PKT_LP,
369 [ 5] = 0,
370 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
371 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
372 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
373 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
374 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
375 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
376 [ 8] = 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 PKT_LP,
380 [ 9] = 0,
381 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
382 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
383 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
384 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
385 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
386 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
387};
388
389/*
390 * non-burst mode with sync events
391 */
392static const u32 pkt_seq_video_non_burst_sync_events[NUM_PKT_SEQ] = {
393 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
394 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
395 PKT_LP,
396 [ 1] = 0,
397 [ 2] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
398 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
399 PKT_LP,
400 [ 3] = 0,
401 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
402 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
403 PKT_LP,
404 [ 5] = 0,
405 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
406 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
407 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
408 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
409 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
410 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
411 PKT_LP,
412 [ 9] = 0,
413 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
414 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
415 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
416 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
417};
418
419static const u32 pkt_seq_command_mode[NUM_PKT_SEQ] = {
420 [ 0] = 0,
421 [ 1] = 0,
422 [ 2] = 0,
423 [ 3] = 0,
424 [ 4] = 0,
425 [ 5] = 0,
426 [ 6] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(3) | PKT_LP,
427 [ 7] = 0,
428 [ 8] = 0,
429 [ 9] = 0,
430 [10] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(5) | PKT_LP,
431 [11] = 0,
432};
433
434static void tegra_dsi_get_muldiv(enum mipi_dsi_pixel_format format,
435 unsigned int *mulp, unsigned int *divp)
436{
437 switch (format) {
438 case MIPI_DSI_FMT_RGB666_PACKED:
439 case MIPI_DSI_FMT_RGB888:
440 *mulp = 3;
441 *divp = 1;
442 break;
443
444 case MIPI_DSI_FMT_RGB565:
445 *mulp = 2;
446 *divp = 1;
447 break;
448
449 case MIPI_DSI_FMT_RGB666:
450 *mulp = 9;
451 *divp = 4;
452 break;
453
454 default:
455 break;
456 }
457}
458
459static int tegra_dsi_get_format(enum mipi_dsi_pixel_format format,
460 enum tegra_dsi_format *fmt)
461{
462 switch (format) {
463 case MIPI_DSI_FMT_RGB888:
464 *fmt = TEGRA_DSI_FORMAT_24P;
465 break;
466
467 case MIPI_DSI_FMT_RGB666:
468 *fmt = TEGRA_DSI_FORMAT_18NP;
469 break;
470
471 case MIPI_DSI_FMT_RGB666_PACKED:
472 *fmt = TEGRA_DSI_FORMAT_18P;
473 break;
474
475 case MIPI_DSI_FMT_RGB565:
476 *fmt = TEGRA_DSI_FORMAT_16P;
477 break;
478
479 default:
480 return -EINVAL;
481 }
482
483 return 0;
484}
485
486static void tegra_dsi_pad_calibrate(struct dsi_pad_ctrl_reg *pad)
487{
488 u32 value;
489
490 /* start calibration */
491 value = DSI_PAD_CONTROL_PAD_LPUPADJ(0x1) |
492 DSI_PAD_CONTROL_PAD_LPDNADJ(0x1) |
493 DSI_PAD_CONTROL_PAD_PREEMP_EN(0x1) |
494 DSI_PAD_CONTROL_PAD_SLEWDNADJ(0x6) |
495 DSI_PAD_CONTROL_PAD_SLEWUPADJ(0x6) |
496 DSI_PAD_CONTROL_PAD_PDIO(0) |
497 DSI_PAD_CONTROL_PAD_PDIO_CLK(0) |
498 DSI_PAD_CONTROL_PAD_PULLDN_ENAB(0);
499 writel(value, &pad->pad_ctrl);
500
501 clock_enable(PERIPH_ID_VI);
502 clock_enable(PERIPH_ID_CSI);
503 udelay(2);
504 reset_set_enable(PERIPH_ID_VI, 0);
505 reset_set_enable(PERIPH_ID_CSI, 0);
506
507 value = MIPI_CAL_TERMOSA(0x4);
508 writel(value, TEGRA_VI_BASE + (CSI_CILA_MIPI_CAL_CONFIG_0 << 2));
509
510 value = MIPI_CAL_TERMOSB(0x4);
511 writel(value, TEGRA_VI_BASE + (CSI_CILB_MIPI_CAL_CONFIG_0 << 2));
512
513 value = MIPI_CAL_HSPUOSD(0x3) | MIPI_CAL_HSPDOSD(0x4);
514 writel(value, TEGRA_VI_BASE + (CSI_DSI_MIPI_CAL_CONFIG << 2));
515
516 value = PAD_DRIV_DN_REF(0x5) | PAD_DRIV_UP_REF(0x7);
517 writel(value, TEGRA_VI_BASE + (CSI_MIPIBIAS_PAD_CONFIG << 2));
518
519 value = PAD_CIL_PDVREG(0x0);
520 writel(value, TEGRA_VI_BASE + (CSI_CIL_PAD_CONFIG << 2));
521}
522
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200523static void tegra_dsi_mipi_calibrate(struct udevice *dev)
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200524{
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200525 struct tegra_dsi_priv *priv = dev_get_priv(dev);
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200526 struct dsi_pad_ctrl_reg *pad = &priv->dsi->pad;
527 u32 value;
528 int ret;
529
530 ret = misc_set_enabled(priv->mipi, true);
531 if (ret)
532 log_debug("%s: failed to enable MIPI calibration: %d\n",
533 __func__, ret);
534
535 writel(0, &pad->pad_ctrl);
536 writel(0, &pad->pad_ctrl_1);
537 writel(0, &pad->pad_ctrl_2);
538 writel(0, &pad->pad_ctrl_3);
539 writel(0, &pad->pad_ctrl_4);
540
541 /* DSI pad enable */
542 value = DSI_PAD_CONTROL_VS1_PULLDN(0) | DSI_PAD_CONTROL_VS1_PDIO(0);
543 writel(value, &pad->pad_ctrl);
544
545 value = DSI_PAD_SLEW_UP(0x7) | DSI_PAD_SLEW_DN(0x7) |
546 DSI_PAD_LP_UP(0x1) | DSI_PAD_LP_DN(0x1) |
547 DSI_PAD_OUT_CLK(0x0);
548 writel(value, &pad->pad_ctrl_2);
549
550 value = DSI_PAD_PREEMP_PD_CLK(0x3) | DSI_PAD_PREEMP_PU_CLK(0x3) |
551 DSI_PAD_PREEMP_PD(0x03) | DSI_PAD_PREEMP_PU(0x3);
552 writel(value, &pad->pad_ctrl_3);
553
Svyatoslav Ryhel30cdefe2024-11-18 08:58:18 +0200554 ret = misc_write(priv->mipi, priv->calibration_pads, NULL, 0);
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200555 if (ret)
556 log_debug("%s: MIPI calibration failed %d\n", __func__, ret);
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200557
558 if (priv->slave)
559 tegra_dsi_mipi_calibrate(priv->slave);
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200560}
561
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200562static void tegra_dsi_set_timeout(struct udevice *dev,
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300563 unsigned long bclk,
564 unsigned int vrefresh)
565{
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200566 struct tegra_dsi_priv *priv = dev_get_priv(dev);
567 struct dsi_timeout_reg *rtimeout = &priv->dsi->timeout;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300568 unsigned int timeout;
569 u32 value;
570
571 /* one frame high-speed transmission timeout */
572 timeout = (bclk / vrefresh) / 512;
573 value = DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout);
574 writel(value, &rtimeout->dsi_timeout_0);
575
576 /* 2 ms peripheral timeout for panel */
577 timeout = 2 * bclk / 512 * 1000;
578 value = DSI_TIMEOUT_PR(timeout) | DSI_TIMEOUT_TA(0x2000);
579 writel(value, &rtimeout->dsi_timeout_1);
580
581 value = DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
582 writel(value, &rtimeout->dsi_to_tally);
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200583
584 if (priv->slave)
585 tegra_dsi_set_timeout(priv->slave, bclk, vrefresh);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300586}
587
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200588static void tegra_dsi_set_phy_timing(struct udevice *dev,
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300589 unsigned long period,
590 const struct mipi_dphy_timing *dphy_timing)
591{
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200592 struct tegra_dsi_priv *priv = dev_get_priv(dev);
593 struct dsi_timing_reg *ptiming = &priv->dsi->ptiming;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300594 u32 value;
595
596 value = DSI_TIMING_FIELD(dphy_timing->hsexit, period, 1) << 24 |
597 DSI_TIMING_FIELD(dphy_timing->hstrail, period, 0) << 16 |
598 DSI_TIMING_FIELD(dphy_timing->hszero, period, 3) << 8 |
599 DSI_TIMING_FIELD(dphy_timing->hsprepare, period, 1);
600 writel(value, &ptiming->dsi_phy_timing_0);
601
602 value = DSI_TIMING_FIELD(dphy_timing->clktrail, period, 1) << 24 |
603 DSI_TIMING_FIELD(dphy_timing->clkpost, period, 1) << 16 |
604 DSI_TIMING_FIELD(dphy_timing->clkzero, period, 1) << 8 |
605 DSI_TIMING_FIELD(dphy_timing->lpx, period, 1);
606 writel(value, &ptiming->dsi_phy_timing_1);
607
608 value = DSI_TIMING_FIELD(dphy_timing->clkprepare, period, 1) << 16 |
609 DSI_TIMING_FIELD(dphy_timing->clkpre, period, 1) << 8 |
610 DSI_TIMING_FIELD(0xff * period, period, 0) << 0;
611 writel(value, &ptiming->dsi_phy_timing_2);
612
613 value = DSI_TIMING_FIELD(dphy_timing->taget, period, 1) << 16 |
614 DSI_TIMING_FIELD(dphy_timing->tasure, period, 1) << 8 |
615 DSI_TIMING_FIELD(dphy_timing->tago, period, 1);
616 writel(value, &ptiming->dsi_bta_timing);
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200617
618 if (priv->slave)
619 tegra_dsi_set_phy_timing(priv->slave, period, dphy_timing);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300620}
621
Svyatoslav Ryhelf87af5a2025-02-26 09:51:09 +0200622static u32 tegra_dsi_get_lanes(struct udevice *dev)
623{
624 struct tegra_dsi_priv *priv = dev_get_priv(dev);
625 struct mipi_dsi_device *device = &priv->device;
626
627 if (priv->master) {
628 struct tegra_dsi_priv *mpriv = dev_get_priv(priv->master);
629 struct mipi_dsi_device *mdevice = &mpriv->device;
630
631 return mdevice->lanes + device->lanes;
632 }
633
634 if (priv->slave) {
635 struct tegra_dsi_priv *spriv = dev_get_priv(priv->slave);
636 struct mipi_dsi_device *sdevice = &spriv->device;
637
638 return device->lanes + sdevice->lanes;
639 }
640
641 return device->lanes;
642}
643
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300644static void tegra_dsi_ganged_enable(struct udevice *dev, unsigned int start,
645 unsigned int size)
646{
647 struct tegra_dsi_priv *priv = dev_get_priv(dev);
648 struct dsi_ganged_mode_reg *ganged = &priv->dsi->ganged;
649
650 writel(start, &ganged->ganged_mode_start);
651 writel(size << 16 | size, &ganged->ganged_mode_size);
652 writel(DSI_GANGED_MODE_CONTROL_ENABLE, &ganged->ganged_mode_ctrl);
653}
654
Svyatoslav Ryhel6a5efea2024-12-31 09:58:01 +0200655static void tegra_dsi_configure(struct udevice *dev, unsigned int pipe,
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300656 unsigned long mode_flags)
657{
658 struct tegra_dsi_priv *priv = dev_get_priv(dev);
659 struct mipi_dsi_device *device = &priv->device;
660 struct display_timing *timing = &priv->timing;
661
662 struct dsi_misc_reg *misc = &priv->dsi->misc;
663 struct dsi_pkt_seq_reg *pkt = &priv->dsi->pkt;
664 struct dsi_pkt_len_reg *len = &priv->dsi->len;
665
666 unsigned int hact, hsw, hbp, hfp, i, mul, div;
667 const u32 *pkt_seq;
668 u32 value;
669
670 tegra_dsi_get_muldiv(device->format, &mul, &div);
671
672 if (mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
673 printf("[DSI] Non-burst video mode with sync pulses\n");
674 pkt_seq = pkt_seq_video_non_burst_sync_pulses;
675 } else if (mode_flags & MIPI_DSI_MODE_VIDEO) {
676 printf("[DSI] Non-burst video mode with sync events\n");
677 pkt_seq = pkt_seq_video_non_burst_sync_events;
678 } else {
679 printf("[DSI] Command mode\n");
680 pkt_seq = pkt_seq_command_mode;
681 }
682
683 value = DSI_CONTROL_CHANNEL(0) |
684 DSI_CONTROL_FORMAT(priv->format) |
685 DSI_CONTROL_LANES(device->lanes - 1) |
Svyatoslav Ryhel6a5efea2024-12-31 09:58:01 +0200686 DSI_CONTROL_SOURCE(pipe);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300687 writel(value, &misc->dsi_ctrl);
688
689 writel(priv->video_fifo_depth, &misc->dsi_max_threshold);
690
691 value = DSI_HOST_CONTROL_HS;
692 writel(value, &misc->host_dsi_ctrl);
693
694 value = readl(&misc->dsi_ctrl);
695
696 if (mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
697 value |= DSI_CONTROL_HS_CLK_CTRL;
698
699 value &= ~DSI_CONTROL_TX_TRIG(3);
700
701 /* enable DCS commands for command mode */
702 if (mode_flags & MIPI_DSI_MODE_VIDEO)
703 value &= ~DSI_CONTROL_DCS_ENABLE;
704 else
705 value |= DSI_CONTROL_DCS_ENABLE;
706
707 value |= DSI_CONTROL_VIDEO_ENABLE;
708 value &= ~DSI_CONTROL_HOST_ENABLE;
709 writel(value, &misc->dsi_ctrl);
710
711 for (i = 0; i < NUM_PKT_SEQ; i++)
712 writel(pkt_seq[i], &pkt->dsi_pkt_seq_0_lo + i);
713
714 if (mode_flags & MIPI_DSI_MODE_VIDEO) {
715 /* horizontal active pixels */
716 hact = timing->hactive.typ * mul / div;
717
718 /* horizontal sync width */
719 hsw = timing->hsync_len.typ * mul / div;
720
721 /* horizontal back porch */
722 hbp = timing->hback_porch.typ * mul / div;
723
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300724 /* horizontal front porch */
725 hfp = timing->hfront_porch.typ * mul / div;
726
Svyatoslav Ryhel10833fb2024-12-02 08:12:36 +0200727 if (priv->master || priv->slave) {
728 hact /= 2;
729 hsw /= 2;
730 hbp = hbp / 2 - 1;
731 hfp /= 2;
732 }
733
734 if ((mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) == 0)
735 hbp += hsw;
736
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300737 /* subtract packet overhead */
738 hsw -= 10;
739 hbp -= 14;
740 hfp -= 8;
741
742 writel(hsw << 16 | 0, &len->dsi_pkt_len_0_1);
743 writel(hact << 16 | hbp, &len->dsi_pkt_len_2_3);
744 writel(hfp, &len->dsi_pkt_len_4_5);
745 writel(0x0f0f << 16, &len->dsi_pkt_len_6_7);
746 } else {
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300747 if (priv->master || priv->slave) {
748 /*
749 * For ganged mode, assume symmetric left-right mode.
750 */
751 value = 1 + (timing->hactive.typ / 2) * mul / div;
752 } else {
753 /* 1 byte (DCS command) + pixel data */
754 value = 1 + timing->hactive.typ * mul / div;
755 }
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300756
757 writel(0, &len->dsi_pkt_len_0_1);
758 writel(value << 16, &len->dsi_pkt_len_2_3);
759 writel(value << 16, &len->dsi_pkt_len_4_5);
760 writel(0, &len->dsi_pkt_len_6_7);
761
762 value = MIPI_DCS_WRITE_MEMORY_START << 8 |
763 MIPI_DCS_WRITE_MEMORY_CONTINUE;
764 writel(value, &len->dsi_dcs_cmds);
Svyatoslav Ryhel18e4dcf2024-12-02 08:08:03 +0200765 }
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300766
Svyatoslav Ryhel18e4dcf2024-12-02 08:08:03 +0200767 /* set SOL delay */
768 if (priv->master || priv->slave) {
769 unsigned long delay, bclk, bclk_ganged;
Svyatoslav Ryhelf87af5a2025-02-26 09:51:09 +0200770 unsigned int lanes = tegra_dsi_get_lanes(dev);
Svyatoslav Ryhel18e4dcf2024-12-02 08:08:03 +0200771 unsigned long htotal = timing->hactive.typ + timing->hfront_porch.typ +
772 timing->hback_porch.typ + timing->hsync_len.typ;
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300773
Svyatoslav Ryhel18e4dcf2024-12-02 08:08:03 +0200774 /* SOL to valid, valid to FIFO and FIFO write delay */
775 delay = 4 + 4 + 2;
776 delay = DIV_ROUND_UP(delay * mul, div * lanes);
777 /* FIFO read delay */
778 delay = delay + 6;
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300779
Svyatoslav Ryhel18e4dcf2024-12-02 08:08:03 +0200780 bclk = DIV_ROUND_UP(htotal * mul, div * lanes);
781 bclk_ganged = DIV_ROUND_UP(bclk * lanes / 2, lanes);
782 value = bclk - bclk_ganged + delay + 20;
783 } else {
784 /* set SOL delay (for non-burst mode only) */
785 value = 8 * mul / div;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300786 }
787
Svyatoslav Ryhel18e4dcf2024-12-02 08:08:03 +0200788 writel(value, &misc->dsi_sol_delay);
789
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300790 if (priv->slave) {
Svyatoslav Ryhel6a5efea2024-12-31 09:58:01 +0200791 tegra_dsi_configure(priv->slave, pipe, mode_flags);
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300792 /*
793 * TODO: Support modes other than symmetrical left-right
794 * split.
795 */
796 tegra_dsi_ganged_enable(dev, 0, timing->hactive.typ / 2);
797 tegra_dsi_ganged_enable(priv->slave, timing->hactive.typ / 2,
798 timing->hactive.typ / 2);
799 }
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300800}
801
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200802static void tegra_dsi_enable(struct udevice *dev)
803{
804 struct tegra_dsi_priv *priv = dev_get_priv(dev);
805 struct dsi_misc_reg *misc = &priv->dsi->misc;
806 u32 value;
807
808 /* enable DSI controller */
809 value = readl(&misc->dsi_pwr_ctrl);
810 value |= DSI_POWER_CONTROL_ENABLE;
811 writel(value, &misc->dsi_pwr_ctrl);
812
813 if (priv->slave)
814 tegra_dsi_enable(priv->slave);
815}
816
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300817static int tegra_dsi_encoder_enable(struct udevice *dev)
818{
819 struct tegra_dsi_priv *priv = dev_get_priv(dev);
Svyatoslav Ryhel6a5efea2024-12-31 09:58:01 +0200820 struct tegra_dc_plat *dc_plat = dev_get_plat(dev);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300821 struct mipi_dsi_device *device = &priv->device;
822 struct display_timing *timing = &priv->timing;
823 struct dsi_misc_reg *misc = &priv->dsi->misc;
824 unsigned int mul, div;
825 unsigned long bclk, plld, period;
Svyatoslav Ryhelf87af5a2025-02-26 09:51:09 +0200826 u32 value, lanes;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300827 int ret;
828
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200829 /* If for some reasone DSI is enabled then it needs to
830 * be disabled in order for the panel initialization
831 * commands to be properly sent.
832 */
833 value = readl(&misc->dsi_pwr_ctrl);
834
835 if (value & DSI_POWER_CONTROL_ENABLE) {
836 value = readl(&misc->dsi_pwr_ctrl);
837 value &= ~DSI_POWER_CONTROL_ENABLE;
838 writel(value, &misc->dsi_pwr_ctrl);
839 }
840
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300841 /* Disable interrupt */
842 writel(0, &misc->int_enable);
843
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200844 if (priv->version)
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200845 tegra_dsi_mipi_calibrate(dev);
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200846 else
847 tegra_dsi_pad_calibrate(&priv->dsi->pad);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300848
849 tegra_dsi_get_muldiv(device->format, &mul, &div);
850
851 /* compute byte clock */
Svyatoslav Ryhelf87af5a2025-02-26 09:51:09 +0200852 lanes = tegra_dsi_get_lanes(dev);
853 bclk = (timing->pixelclock.typ * mul) / (div * lanes);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300854
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200855 tegra_dsi_set_timeout(dev, bclk, 60);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300856
857 /*
858 * Compute bit clock and round up to the next MHz.
859 */
860 plld = DIV_ROUND_UP(bclk * 8, USEC_PER_SEC) * USEC_PER_SEC;
861 period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, plld);
862
863 ret = mipi_dphy_timing_get_default(&priv->dphy_timing, period);
864 if (ret < 0) {
865 printf("%s: failed to get D-PHY timing: %d\n", __func__, ret);
866 return ret;
867 }
868
869 ret = mipi_dphy_timing_validate(&priv->dphy_timing, period);
870 if (ret < 0) {
871 printf("%s: failed to validate D-PHY timing: %d\n", __func__, ret);
872 return ret;
873 }
874
875 /*
876 * The D-PHY timing fields are expressed in byte-clock cycles, so
877 * multiply the period by 8.
878 */
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200879 tegra_dsi_set_phy_timing(dev, period * 8, &priv->dphy_timing);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300880
881 /* Perform panel HW setup */
882 ret = panel_enable_backlight(priv->panel);
883 if (ret)
884 return ret;
885
Svyatoslav Ryhel6a5efea2024-12-31 09:58:01 +0200886 tegra_dsi_configure(dev, dc_plat->pipe, device->mode_flags);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300887
888 tegra_dc_enable_controller(dev);
889
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200890 tegra_dsi_enable(dev);
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300891
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300892 return 0;
893}
894
895static int tegra_dsi_bridge_set_panel(struct udevice *dev, int percent)
896{
Jonas Schwöbeldea5d962024-01-23 19:16:33 +0200897 struct tegra_dsi_priv *priv = dev_get_priv(dev);
898
899 /* Turn on/off backlight */
900 return panel_set_backlight(priv->panel, percent);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300901}
902
903static int tegra_dsi_panel_timings(struct udevice *dev,
904 struct display_timing *timing)
905{
906 struct tegra_dsi_priv *priv = dev_get_priv(dev);
907
908 memcpy(timing, &priv->timing, sizeof(*timing));
909
910 return 0;
911}
912
913static void tegra_dsi_init_clocks(struct udevice *dev)
914{
915 struct tegra_dsi_priv *priv = dev_get_priv(dev);
Svyatoslav Ryheld16c1052024-01-23 19:16:23 +0200916 struct tegra_dc_plat *dc_plat = dev_get_plat(dev);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300917 struct mipi_dsi_device *device = &priv->device;
Svyatoslav Ryhelf87af5a2025-02-26 09:51:09 +0200918 unsigned int mul, div, lanes;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300919 unsigned long bclk, plld;
920
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +0200921 /* Switch parents of DSI clocks in case of not standard parent */
922 if (priv->clk->id == PERIPH_ID_DSI &&
923 priv->clk_parent->id == CLOCK_ID_DISPLAY2) {
924 /* Change DSIA clock parent to PLLD2 */
925 struct clk_rst_ctlr *clkrst =
926 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
927
928 /* DSIA_CLK_SRC */
929 setbits_le32(&clkrst->crc_pll[CLOCK_ID_DISPLAY].pll_base,
930 BIT(25));
931 }
932
933 if (priv->clk->id == PERIPH_ID_DSIB &&
934 priv->clk_parent->id == CLOCK_ID_DISPLAY) {
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300935 /* Change DSIB clock parent to match DSIA */
936 struct clk_rst_ctlr *clkrst =
937 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
938
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +0200939 /* DSIB_CLK_SRC */
940 clrbits_le32(&clkrst->plld2.pll_base, BIT(25));
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300941 }
942
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300943 tegra_dsi_get_muldiv(device->format, &mul, &div);
944
Svyatoslav Ryhelf87af5a2025-02-26 09:51:09 +0200945 lanes = tegra_dsi_get_lanes(dev);
946 bclk = (priv->timing.pixelclock.typ * mul) / (div * lanes);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300947
948 plld = DIV_ROUND_UP(bclk * 8, USEC_PER_SEC);
949
Svyatoslav Ryheld16c1052024-01-23 19:16:23 +0200950 dc_plat->scdiv = ((plld * USEC_PER_SEC +
951 priv->timing.pixelclock.typ / 2) /
952 priv->timing.pixelclock.typ) - 2;
953
954 /*
955 * BUG: If DISP1 is a PLLD/D2 child, it cannot go over 370MHz. The
956 * cause of this is not quite clear. This can be overcomed by
957 * halving the PLLD/D2 if the target rate is > 800MHz. This way
958 * DISP1 and DSI clocks will be equal.
959 */
960 if (plld > 800)
961 plld /= 2;
962
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300963 switch (clock_get_osc_freq()) {
964 case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
965 case CLOCK_OSC_FREQ_48_0: /* OSC is 48Mhz */
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +0200966 clock_set_rate(priv->clk_parent->id, plld, 12, 0, 8);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300967 break;
968
969 case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +0200970 clock_set_rate(priv->clk_parent->id, plld, 26, 0, 8);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300971 break;
972
973 case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
974 case CLOCK_OSC_FREQ_16_8: /* OSC is 16.8Mhz */
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +0200975 clock_set_rate(priv->clk_parent->id, plld, 13, 0, 8);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300976 break;
977
978 case CLOCK_OSC_FREQ_19_2:
979 case CLOCK_OSC_FREQ_38_4:
980 default:
981 /*
982 * These are not supported.
983 */
984 break;
985 }
986
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +0200987 clk_enable(priv->clk);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300988}
989
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300990static int tegra_dsi_ganged_probe(struct udevice *dev)
991{
992 struct tegra_dsi_priv *mpriv = dev_get_priv(dev);
993 struct udevice *gangster;
994
Svyatoslav Ryhel90f5e6e2025-02-14 15:24:13 +0200995 uclass_get_device_by_phandle(UCLASS_VIDEO_BRIDGE, dev,
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300996 "nvidia,ganged-mode", &gangster);
997 if (gangster) {
998 /* Ganged mode is set */
999 struct tegra_dsi_priv *spriv = dev_get_priv(gangster);
1000
1001 mpriv->slave = gangster;
1002 spriv->master = dev;
1003 }
1004
1005 return 0;
1006}
1007
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001008static int tegra_dsi_bridge_probe(struct udevice *dev)
1009{
1010 struct tegra_dsi_priv *priv = dev_get_priv(dev);
1011 struct mipi_dsi_device *device = &priv->device;
1012 struct mipi_dsi_panel_plat *mipi_plat;
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +02001013 struct reset_ctl reset_ctl;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001014 int ret;
1015
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +02001016 priv->version = dev_get_driver_data(dev);
1017
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001018 priv->dsi = (struct dsi_ctlr *)dev_read_addr_ptr(dev);
1019 if (!priv->dsi) {
1020 printf("%s: No display controller address\n", __func__);
1021 return -EINVAL;
1022 }
1023
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +02001024 priv->clk = devm_clk_get(dev, NULL);
1025 if (IS_ERR(priv->clk)) {
1026 log_debug("%s: Could not get DSI clock: %ld\n",
1027 __func__, PTR_ERR(priv->clk));
1028 return PTR_ERR(priv->clk);
1029 }
1030
1031 priv->clk_parent = devm_clk_get(dev, "parent");
1032 if (IS_ERR(priv->clk_parent)) {
1033 log_debug("%s: Could not get DSI clock parent: %ld\n",
1034 __func__, PTR_ERR(priv->clk_parent));
1035 return PTR_ERR(priv->clk_parent);
1036 }
1037
Jonas Schwöbel0cccf902024-01-23 19:16:32 +02001038 priv->video_fifo_depth = 1920;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001039 priv->host_fifo_depth = 64;
1040
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +03001041 tegra_dsi_ganged_probe(dev);
1042
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +02001043 ret = reset_get_by_name(dev, "dsi", &reset_ctl);
1044 if (ret) {
1045 log_debug("%s: reset_get_by_name() failed: %d\n",
1046 __func__, ret);
1047 return ret;
1048 }
1049
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001050 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
1051 "avdd-dsi-csi-supply", &priv->avdd);
1052 if (ret)
1053 debug("%s: Cannot get avdd-dsi-csi-supply: error %d\n",
1054 __func__, ret);
1055
Svyatoslav Ryhelf6b2ab42024-11-24 09:38:03 +02001056 /* Check all DSI children */
1057 device_foreach_child(priv->panel, dev) {
1058 if (device_get_uclass_id(priv->panel) == UCLASS_PANEL)
1059 break;
1060 }
1061
1062 /* if loop exits without panel device return error */
1063 if (device_get_uclass_id(priv->panel) != UCLASS_PANEL) {
1064 log_debug("%s: panel not found, ret %d\n", __func__, ret);
1065 return -EINVAL;
1066 }
1067
1068 ret = uclass_get_device_by_ofnode(UCLASS_PANEL, dev_ofnode(priv->panel),
1069 &priv->panel);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001070 if (ret) {
Svyatoslav Ryhelf6b2ab42024-11-24 09:38:03 +02001071 log_debug("%s: Cannot get panel: error %d\n", __func__, ret);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001072 return log_ret(ret);
1073 }
1074
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +02001075 if (priv->version) {
1076 ret = uclass_get_device_by_phandle(UCLASS_MISC, dev,
1077 "nvidia,mipi-calibrate",
1078 &priv->mipi);
1079 if (ret) {
1080 log_debug("%s: cannot get MIPI: error %d\n", __func__, ret);
1081 return ret;
1082 }
Svyatoslav Ryhel30cdefe2024-11-18 08:58:18 +02001083
1084 ret = dev_read_u32_index(dev, "nvidia,mipi-calibrate", 1,
1085 &priv->calibration_pads);
1086 if (ret) {
1087 log_debug("%s: cannot get calibration pads: error %d\n",
1088 __func__, ret);
1089 return ret;
1090 }
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +02001091 }
1092
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001093 panel_get_display_timing(priv->panel, &priv->timing);
1094
1095 mipi_plat = dev_get_plat(priv->panel);
1096 mipi_plat->device = device;
1097
1098 priv->host.dev = (struct device *)dev;
1099 priv->host.ops = &tegra_dsi_bridge_host_ops;
1100
1101 device->host = &priv->host;
1102 device->lanes = mipi_plat->lanes;
1103 device->format = mipi_plat->format;
1104 device->mode_flags = mipi_plat->mode_flags;
1105
1106 tegra_dsi_get_format(device->format, &priv->format);
1107
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +02001108 reset_assert(&reset_ctl);
1109
Svyatoslav Ryheldaab7d92023-10-03 09:25:34 +03001110 ret = regulator_set_enable_if_allowed(priv->avdd, true);
1111 if (ret && ret != -ENOSYS)
1112 return ret;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001113
1114 tegra_dsi_init_clocks(dev);
1115
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +02001116 mdelay(2);
1117 reset_deassert(&reset_ctl);
1118
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001119 return 0;
1120}
1121
Svyatoslav Ryhel90f5e6e2025-02-14 15:24:13 +02001122static const struct video_bridge_ops tegra_dsi_bridge_ops = {
1123 .attach = tegra_dsi_encoder_enable,
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001124 .set_backlight = tegra_dsi_bridge_set_panel,
1125 .get_display_timing = tegra_dsi_panel_timings,
1126};
1127
1128static const struct udevice_id tegra_dsi_bridge_ids[] = {
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +02001129 { .compatible = "nvidia,tegra30-dsi", .data = DSI_V0 },
1130 { .compatible = "nvidia,tegra114-dsi", .data = DSI_V1 },
Svyatoslav Ryhel5fa06e72024-11-18 08:58:18 +02001131 { .compatible = "nvidia,tegra124-dsi", .data = DSI_V1 },
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001132 { }
1133};
1134
1135U_BOOT_DRIVER(tegra_dsi) = {
1136 .name = "tegra_dsi",
Svyatoslav Ryhel90f5e6e2025-02-14 15:24:13 +02001137 .id = UCLASS_VIDEO_BRIDGE,
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001138 .of_match = tegra_dsi_bridge_ids,
1139 .ops = &tegra_dsi_bridge_ops,
Svyatoslav Ryhelf6b2ab42024-11-24 09:38:03 +02001140 .bind = dm_scan_fdt_dev,
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001141 .probe = tegra_dsi_bridge_probe,
1142 .plat_auto = sizeof(struct tegra_dc_plat),
1143 .priv_auto = sizeof(struct tegra_dsi_priv),
1144};