blob: 079b00e0f610d489249308589cb822fd0a84133b [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>
14#include <panel.h>
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +020015#include <reset.h>
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030016#include <linux/delay.h>
17#include <linux/err.h>
Igor Prusovc3421ea2023-11-09 20:10:04 +030018#include <linux/time.h>
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030019#include <power/regulator.h>
20
21#include <asm/gpio.h>
22#include <asm/io.h>
23#include <asm/arch/clock.h>
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +030024#include <asm/arch-tegra/clk_rst.h>
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030025
Svyatoslav Ryhel75fec412024-01-23 19:16:18 +020026#include "tegra-dc.h"
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +020027#include "tegra-dsi.h"
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030028#include "mipi-phy.h"
29
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +020030/* List of supported DSI bridges */
31enum {
32 DSI_V0,
33 DSI_V1,
34};
35
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030036struct tegra_dsi_priv {
37 struct mipi_dsi_host host;
38 struct mipi_dsi_device device;
39 struct mipi_dphy_timing dphy_timing;
40
41 struct udevice *panel;
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +020042 struct udevice *mipi;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030043 struct display_timing timing;
44
45 struct dsi_ctlr *dsi;
46 struct udevice *avdd;
47
48 enum tegra_dsi_format format;
49
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +020050 struct clk *clk;
51 struct clk *clk_parent;
52
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030053 int video_fifo_depth;
54 int host_fifo_depth;
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +020055
56 u32 version;
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +030057
58 /* for ganged-mode support */
59 struct udevice *master;
60 struct udevice *slave;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030061};
62
63static void tegra_dc_enable_controller(struct udevice *dev)
64{
65 struct tegra_dc_plat *dc_plat = dev_get_plat(dev);
66 struct dc_ctlr *dc = dc_plat->dc;
67 u32 value;
68
69 value = readl(&dc->disp.disp_win_opt);
70 value |= DSI_ENABLE;
71 writel(value, &dc->disp.disp_win_opt);
72
73 writel(GENERAL_UPDATE, &dc->cmd.state_ctrl);
74 writel(GENERAL_ACT_REQ, &dc->cmd.state_ctrl);
75}
76
77static const char * const error_report[16] = {
78 "SoT Error",
79 "SoT Sync Error",
80 "EoT Sync Error",
81 "Escape Mode Entry Command Error",
82 "Low-Power Transmit Sync Error",
83 "Peripheral Timeout Error",
84 "False Control Error",
85 "Contention Detected",
86 "ECC Error, single-bit",
87 "ECC Error, multi-bit",
88 "Checksum Error",
89 "DSI Data Type Not Recognized",
90 "DSI VC ID Invalid",
91 "Invalid Transmission Length",
92 "Reserved",
93 "DSI Protocol Violation",
94};
95
96static ssize_t tegra_dsi_read_response(struct dsi_misc_reg *misc,
97 const struct mipi_dsi_msg *msg,
98 size_t count)
99{
100 u8 *rx = msg->rx_buf;
101 unsigned int i, j, k;
102 size_t size = 0;
103 u16 errors;
104 u32 value;
105
106 /* read and parse packet header */
107 value = readl(&misc->dsi_rd_data);
108
109 switch (value & 0x3f) {
110 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
111 errors = (value >> 8) & 0xffff;
112 printf("%s: Acknowledge and error report: %04x\n",
113 __func__, errors);
114 for (i = 0; i < ARRAY_SIZE(error_report); i++)
115 if (errors & BIT(i))
116 printf("%s: %2u: %s\n", __func__, i,
117 error_report[i]);
118 break;
119
120 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
121 rx[0] = (value >> 8) & 0xff;
122 size = 1;
123 break;
124
125 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
126 rx[0] = (value >> 8) & 0xff;
127 rx[1] = (value >> 16) & 0xff;
128 size = 2;
129 break;
130
131 case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
132 size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
133 break;
134
135 case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
136 size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
137 break;
138
139 default:
140 printf("%s: unhandled response type: %02x\n",
141 __func__, value & 0x3f);
142 return -EPROTO;
143 }
144
145 size = min(size, msg->rx_len);
146
147 if (msg->rx_buf && size > 0) {
148 for (i = 0, j = 0; i < count - 1; i++, j += 4) {
149 u8 *rx = msg->rx_buf + j;
150
151 value = readl(&misc->dsi_rd_data);
152
153 for (k = 0; k < 4 && (j + k) < msg->rx_len; k++)
154 rx[j + k] = (value >> (k << 3)) & 0xff;
155 }
156 }
157
158 return size;
159}
160
161static int tegra_dsi_transmit(struct dsi_misc_reg *misc,
162 unsigned long timeout)
163{
164 writel(DSI_TRIGGER_HOST, &misc->dsi_trigger);
165
166 while (timeout--) {
167 u32 value = readl(&misc->dsi_trigger);
168
169 if ((value & DSI_TRIGGER_HOST) == 0)
170 return 0;
171
172 udelay(1000);
173 }
174
175 debug("timeout waiting for transmission to complete\n");
176 return -ETIMEDOUT;
177}
178
179static int tegra_dsi_wait_for_response(struct dsi_misc_reg *misc,
180 unsigned long timeout)
181{
182 while (timeout--) {
183 u32 value = readl(&misc->dsi_status);
184 u8 count = value & 0x1f;
185
186 if (count > 0)
187 return count;
188
189 udelay(1000);
190 }
191
192 debug("peripheral returned no data\n");
193 return -ETIMEDOUT;
194}
195
196static void tegra_dsi_writesl(struct dsi_misc_reg *misc,
197 const void *buffer, size_t size)
198{
199 const u8 *buf = buffer;
200 size_t i, j;
201 u32 value;
202
203 for (j = 0; j < size; j += 4) {
204 value = 0;
205
206 for (i = 0; i < 4 && j + i < size; i++)
207 value |= buf[j + i] << (i << 3);
208
209 writel(value, &misc->dsi_wr_data);
210 }
211}
212
213static ssize_t tegra_dsi_host_transfer(struct mipi_dsi_host *host,
214 const struct mipi_dsi_msg *msg)
215{
216 struct udevice *dev = (struct udevice *)host->dev;
217 struct tegra_dsi_priv *priv = dev_get_priv(dev);
218 struct dsi_misc_reg *misc = &priv->dsi->misc;
219 struct mipi_dsi_packet packet;
220 const u8 *header;
221 size_t count;
222 ssize_t err;
223 u32 value;
224
225 err = mipi_dsi_create_packet(&packet, msg);
226 if (err < 0)
227 return err;
228
229 header = packet.header;
230
231 /* maximum FIFO depth is 1920 words */
232 if (packet.size > priv->video_fifo_depth * 4)
233 return -ENOSPC;
234
235 /* reset underflow/overflow flags */
236 value = readl(&misc->dsi_status);
237 if (value & (DSI_STATUS_UNDERFLOW | DSI_STATUS_OVERFLOW)) {
238 value = DSI_HOST_CONTROL_FIFO_RESET;
239 writel(value, &misc->host_dsi_ctrl);
240 udelay(10);
241 }
242
243 value = readl(&misc->dsi_pwr_ctrl);
244 value |= DSI_POWER_CONTROL_ENABLE;
245 writel(value, &misc->dsi_pwr_ctrl);
246
247 mdelay(5);
248
249 value = DSI_HOST_CONTROL_CRC_RESET | DSI_HOST_CONTROL_TX_TRIG_HOST |
250 DSI_HOST_CONTROL_CS | DSI_HOST_CONTROL_ECC;
251
252 /*
253 * The host FIFO has a maximum of 64 words, so larger transmissions
254 * need to use the video FIFO.
255 */
256 if (packet.size > priv->host_fifo_depth * 4)
257 value |= DSI_HOST_CONTROL_FIFO_SEL;
258
259 writel(value, &misc->host_dsi_ctrl);
260
261 /*
262 * For reads and messages with explicitly requested ACK, generate a
263 * BTA sequence after the transmission of the packet.
264 */
265 if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
266 (msg->rx_buf && msg->rx_len > 0)) {
267 value = readl(&misc->host_dsi_ctrl);
268 value |= DSI_HOST_CONTROL_PKT_BTA;
269 writel(value, &misc->host_dsi_ctrl);
270 }
271
272 value = DSI_CONTROL_LANES(0) | DSI_CONTROL_HOST_ENABLE;
273 writel(value, &misc->dsi_ctrl);
274
275 /* write packet header, ECC is generated by hardware */
276 value = header[2] << 16 | header[1] << 8 | header[0];
277 writel(value, &misc->dsi_wr_data);
278
279 /* write payload (if any) */
280 if (packet.payload_length > 0)
281 tegra_dsi_writesl(misc, packet.payload,
282 packet.payload_length);
283
284 err = tegra_dsi_transmit(misc, 250);
285 if (err < 0)
286 return err;
287
288 if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
289 (msg->rx_buf && msg->rx_len > 0)) {
290 err = tegra_dsi_wait_for_response(misc, 250);
291 if (err < 0)
292 return err;
293
294 count = err;
295
296 value = readl(&misc->dsi_rd_data);
297 switch (value) {
298 case 0x84:
299 debug("%s: ACK\n", __func__);
300 break;
301
302 case 0x87:
303 debug("%s: ESCAPE\n", __func__);
304 break;
305
306 default:
307 printf("%s: unknown status: %08x\n", __func__, value);
308 break;
309 }
310
311 if (count > 1) {
312 err = tegra_dsi_read_response(misc, msg, count);
313 if (err < 0) {
314 printf("%s: failed to parse response: %zd\n",
315 __func__, err);
316 } else {
317 /*
318 * For read commands, return the number of
319 * bytes returned by the peripheral.
320 */
321 count = err;
322 }
323 }
324 } else {
325 /*
326 * For write commands, we have transmitted the 4-byte header
327 * plus the variable-length payload.
328 */
329 count = 4 + packet.payload_length;
330 }
331
332 return count;
333}
334
335struct mipi_dsi_host_ops tegra_dsi_bridge_host_ops = {
336 .transfer = tegra_dsi_host_transfer,
337};
338
339#define PKT_ID0(id) ((((id) & 0x3f) << 3) | (1 << 9))
340#define PKT_LEN0(len) (((len) & 0x07) << 0)
341#define PKT_ID1(id) ((((id) & 0x3f) << 13) | (1 << 19))
342#define PKT_LEN1(len) (((len) & 0x07) << 10)
343#define PKT_ID2(id) ((((id) & 0x3f) << 23) | (1 << 29))
344#define PKT_LEN2(len) (((len) & 0x07) << 20)
345
346#define PKT_LP BIT(30)
347#define NUM_PKT_SEQ 12
348
349/*
350 * non-burst mode with sync pulses
351 */
352static const u32 pkt_seq_video_non_burst_sync_pulses[NUM_PKT_SEQ] = {
353 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
354 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
355 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
356 PKT_LP,
357 [ 1] = 0,
358 [ 2] = PKT_ID0(MIPI_DSI_V_SYNC_END) | 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 [ 3] = 0,
363 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | 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 [ 5] = 0,
368 [ 6] = 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 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
372 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
373 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
374 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
375 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
376 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
377 PKT_LP,
378 [ 9] = 0,
379 [10] = 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 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
383 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
384 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
385};
386
387/*
388 * non-burst mode with sync events
389 */
390static const u32 pkt_seq_video_non_burst_sync_events[NUM_PKT_SEQ] = {
391 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
392 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
393 PKT_LP,
394 [ 1] = 0,
395 [ 2] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
396 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
397 PKT_LP,
398 [ 3] = 0,
399 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
400 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
401 PKT_LP,
402 [ 5] = 0,
403 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
404 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
405 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
406 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
407 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
408 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
409 PKT_LP,
410 [ 9] = 0,
411 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
412 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
413 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
414 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
415};
416
417static const u32 pkt_seq_command_mode[NUM_PKT_SEQ] = {
418 [ 0] = 0,
419 [ 1] = 0,
420 [ 2] = 0,
421 [ 3] = 0,
422 [ 4] = 0,
423 [ 5] = 0,
424 [ 6] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(3) | PKT_LP,
425 [ 7] = 0,
426 [ 8] = 0,
427 [ 9] = 0,
428 [10] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(5) | PKT_LP,
429 [11] = 0,
430};
431
432static void tegra_dsi_get_muldiv(enum mipi_dsi_pixel_format format,
433 unsigned int *mulp, unsigned int *divp)
434{
435 switch (format) {
436 case MIPI_DSI_FMT_RGB666_PACKED:
437 case MIPI_DSI_FMT_RGB888:
438 *mulp = 3;
439 *divp = 1;
440 break;
441
442 case MIPI_DSI_FMT_RGB565:
443 *mulp = 2;
444 *divp = 1;
445 break;
446
447 case MIPI_DSI_FMT_RGB666:
448 *mulp = 9;
449 *divp = 4;
450 break;
451
452 default:
453 break;
454 }
455}
456
457static int tegra_dsi_get_format(enum mipi_dsi_pixel_format format,
458 enum tegra_dsi_format *fmt)
459{
460 switch (format) {
461 case MIPI_DSI_FMT_RGB888:
462 *fmt = TEGRA_DSI_FORMAT_24P;
463 break;
464
465 case MIPI_DSI_FMT_RGB666:
466 *fmt = TEGRA_DSI_FORMAT_18NP;
467 break;
468
469 case MIPI_DSI_FMT_RGB666_PACKED:
470 *fmt = TEGRA_DSI_FORMAT_18P;
471 break;
472
473 case MIPI_DSI_FMT_RGB565:
474 *fmt = TEGRA_DSI_FORMAT_16P;
475 break;
476
477 default:
478 return -EINVAL;
479 }
480
481 return 0;
482}
483
484static void tegra_dsi_pad_calibrate(struct dsi_pad_ctrl_reg *pad)
485{
486 u32 value;
487
488 /* start calibration */
489 value = DSI_PAD_CONTROL_PAD_LPUPADJ(0x1) |
490 DSI_PAD_CONTROL_PAD_LPDNADJ(0x1) |
491 DSI_PAD_CONTROL_PAD_PREEMP_EN(0x1) |
492 DSI_PAD_CONTROL_PAD_SLEWDNADJ(0x6) |
493 DSI_PAD_CONTROL_PAD_SLEWUPADJ(0x6) |
494 DSI_PAD_CONTROL_PAD_PDIO(0) |
495 DSI_PAD_CONTROL_PAD_PDIO_CLK(0) |
496 DSI_PAD_CONTROL_PAD_PULLDN_ENAB(0);
497 writel(value, &pad->pad_ctrl);
498
499 clock_enable(PERIPH_ID_VI);
500 clock_enable(PERIPH_ID_CSI);
501 udelay(2);
502 reset_set_enable(PERIPH_ID_VI, 0);
503 reset_set_enable(PERIPH_ID_CSI, 0);
504
505 value = MIPI_CAL_TERMOSA(0x4);
506 writel(value, TEGRA_VI_BASE + (CSI_CILA_MIPI_CAL_CONFIG_0 << 2));
507
508 value = MIPI_CAL_TERMOSB(0x4);
509 writel(value, TEGRA_VI_BASE + (CSI_CILB_MIPI_CAL_CONFIG_0 << 2));
510
511 value = MIPI_CAL_HSPUOSD(0x3) | MIPI_CAL_HSPDOSD(0x4);
512 writel(value, TEGRA_VI_BASE + (CSI_DSI_MIPI_CAL_CONFIG << 2));
513
514 value = PAD_DRIV_DN_REF(0x5) | PAD_DRIV_UP_REF(0x7);
515 writel(value, TEGRA_VI_BASE + (CSI_MIPIBIAS_PAD_CONFIG << 2));
516
517 value = PAD_CIL_PDVREG(0x0);
518 writel(value, TEGRA_VI_BASE + (CSI_CIL_PAD_CONFIG << 2));
519}
520
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200521static void tegra_dsi_mipi_calibrate(struct udevice *dev)
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200522{
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200523 struct tegra_dsi_priv *priv = dev_get_priv(dev);
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200524 struct dsi_pad_ctrl_reg *pad = &priv->dsi->pad;
525 u32 value;
526 int ret;
527
528 ret = misc_set_enabled(priv->mipi, true);
529 if (ret)
530 log_debug("%s: failed to enable MIPI calibration: %d\n",
531 __func__, ret);
532
533 writel(0, &pad->pad_ctrl);
534 writel(0, &pad->pad_ctrl_1);
535 writel(0, &pad->pad_ctrl_2);
536 writel(0, &pad->pad_ctrl_3);
537 writel(0, &pad->pad_ctrl_4);
538
539 /* DSI pad enable */
540 value = DSI_PAD_CONTROL_VS1_PULLDN(0) | DSI_PAD_CONTROL_VS1_PDIO(0);
541 writel(value, &pad->pad_ctrl);
542
543 value = DSI_PAD_SLEW_UP(0x7) | DSI_PAD_SLEW_DN(0x7) |
544 DSI_PAD_LP_UP(0x1) | DSI_PAD_LP_DN(0x1) |
545 DSI_PAD_OUT_CLK(0x0);
546 writel(value, &pad->pad_ctrl_2);
547
548 value = DSI_PAD_PREEMP_PD_CLK(0x3) | DSI_PAD_PREEMP_PU_CLK(0x3) |
549 DSI_PAD_PREEMP_PD(0x03) | DSI_PAD_PREEMP_PU(0x3);
550 writel(value, &pad->pad_ctrl_3);
551
552 ret = misc_write(priv->mipi, 0, NULL, 0);
553 if (ret)
554 log_debug("%s: MIPI calibration failed %d\n", __func__, ret);
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200555
556 if (priv->slave)
557 tegra_dsi_mipi_calibrate(priv->slave);
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200558}
559
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200560static void tegra_dsi_set_timeout(struct udevice *dev,
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300561 unsigned long bclk,
562 unsigned int vrefresh)
563{
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200564 struct tegra_dsi_priv *priv = dev_get_priv(dev);
565 struct dsi_timeout_reg *rtimeout = &priv->dsi->timeout;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300566 unsigned int timeout;
567 u32 value;
568
569 /* one frame high-speed transmission timeout */
570 timeout = (bclk / vrefresh) / 512;
571 value = DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout);
572 writel(value, &rtimeout->dsi_timeout_0);
573
574 /* 2 ms peripheral timeout for panel */
575 timeout = 2 * bclk / 512 * 1000;
576 value = DSI_TIMEOUT_PR(timeout) | DSI_TIMEOUT_TA(0x2000);
577 writel(value, &rtimeout->dsi_timeout_1);
578
579 value = DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
580 writel(value, &rtimeout->dsi_to_tally);
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200581
582 if (priv->slave)
583 tegra_dsi_set_timeout(priv->slave, bclk, vrefresh);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300584}
585
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200586static void tegra_dsi_set_phy_timing(struct udevice *dev,
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300587 unsigned long period,
588 const struct mipi_dphy_timing *dphy_timing)
589{
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200590 struct tegra_dsi_priv *priv = dev_get_priv(dev);
591 struct dsi_timing_reg *ptiming = &priv->dsi->ptiming;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300592 u32 value;
593
594 value = DSI_TIMING_FIELD(dphy_timing->hsexit, period, 1) << 24 |
595 DSI_TIMING_FIELD(dphy_timing->hstrail, period, 0) << 16 |
596 DSI_TIMING_FIELD(dphy_timing->hszero, period, 3) << 8 |
597 DSI_TIMING_FIELD(dphy_timing->hsprepare, period, 1);
598 writel(value, &ptiming->dsi_phy_timing_0);
599
600 value = DSI_TIMING_FIELD(dphy_timing->clktrail, period, 1) << 24 |
601 DSI_TIMING_FIELD(dphy_timing->clkpost, period, 1) << 16 |
602 DSI_TIMING_FIELD(dphy_timing->clkzero, period, 1) << 8 |
603 DSI_TIMING_FIELD(dphy_timing->lpx, period, 1);
604 writel(value, &ptiming->dsi_phy_timing_1);
605
606 value = DSI_TIMING_FIELD(dphy_timing->clkprepare, period, 1) << 16 |
607 DSI_TIMING_FIELD(dphy_timing->clkpre, period, 1) << 8 |
608 DSI_TIMING_FIELD(0xff * period, period, 0) << 0;
609 writel(value, &ptiming->dsi_phy_timing_2);
610
611 value = DSI_TIMING_FIELD(dphy_timing->taget, period, 1) << 16 |
612 DSI_TIMING_FIELD(dphy_timing->tasure, period, 1) << 8 |
613 DSI_TIMING_FIELD(dphy_timing->tago, period, 1);
614 writel(value, &ptiming->dsi_bta_timing);
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200615
616 if (priv->slave)
617 tegra_dsi_set_phy_timing(priv->slave, period, dphy_timing);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300618}
619
Svyatoslav Ryhelf87af5a2025-02-26 09:51:09 +0200620static u32 tegra_dsi_get_lanes(struct udevice *dev)
621{
622 struct tegra_dsi_priv *priv = dev_get_priv(dev);
623 struct mipi_dsi_device *device = &priv->device;
624
625 if (priv->master) {
626 struct tegra_dsi_priv *mpriv = dev_get_priv(priv->master);
627 struct mipi_dsi_device *mdevice = &mpriv->device;
628
629 return mdevice->lanes + device->lanes;
630 }
631
632 if (priv->slave) {
633 struct tegra_dsi_priv *spriv = dev_get_priv(priv->slave);
634 struct mipi_dsi_device *sdevice = &spriv->device;
635
636 return device->lanes + sdevice->lanes;
637 }
638
639 return device->lanes;
640}
641
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300642static void tegra_dsi_ganged_enable(struct udevice *dev, unsigned int start,
643 unsigned int size)
644{
645 struct tegra_dsi_priv *priv = dev_get_priv(dev);
646 struct dsi_ganged_mode_reg *ganged = &priv->dsi->ganged;
647
648 writel(start, &ganged->ganged_mode_start);
649 writel(size << 16 | size, &ganged->ganged_mode_size);
650 writel(DSI_GANGED_MODE_CONTROL_ENABLE, &ganged->ganged_mode_ctrl);
651}
652
Svyatoslav Ryhel6a5efea2024-12-31 09:58:01 +0200653static void tegra_dsi_configure(struct udevice *dev, unsigned int pipe,
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300654 unsigned long mode_flags)
655{
656 struct tegra_dsi_priv *priv = dev_get_priv(dev);
657 struct mipi_dsi_device *device = &priv->device;
658 struct display_timing *timing = &priv->timing;
659
660 struct dsi_misc_reg *misc = &priv->dsi->misc;
661 struct dsi_pkt_seq_reg *pkt = &priv->dsi->pkt;
662 struct dsi_pkt_len_reg *len = &priv->dsi->len;
663
664 unsigned int hact, hsw, hbp, hfp, i, mul, div;
665 const u32 *pkt_seq;
666 u32 value;
667
668 tegra_dsi_get_muldiv(device->format, &mul, &div);
669
670 if (mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
671 printf("[DSI] Non-burst video mode with sync pulses\n");
672 pkt_seq = pkt_seq_video_non_burst_sync_pulses;
673 } else if (mode_flags & MIPI_DSI_MODE_VIDEO) {
674 printf("[DSI] Non-burst video mode with sync events\n");
675 pkt_seq = pkt_seq_video_non_burst_sync_events;
676 } else {
677 printf("[DSI] Command mode\n");
678 pkt_seq = pkt_seq_command_mode;
679 }
680
681 value = DSI_CONTROL_CHANNEL(0) |
682 DSI_CONTROL_FORMAT(priv->format) |
683 DSI_CONTROL_LANES(device->lanes - 1) |
Svyatoslav Ryhel6a5efea2024-12-31 09:58:01 +0200684 DSI_CONTROL_SOURCE(pipe);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300685 writel(value, &misc->dsi_ctrl);
686
687 writel(priv->video_fifo_depth, &misc->dsi_max_threshold);
688
689 value = DSI_HOST_CONTROL_HS;
690 writel(value, &misc->host_dsi_ctrl);
691
692 value = readl(&misc->dsi_ctrl);
693
694 if (mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
695 value |= DSI_CONTROL_HS_CLK_CTRL;
696
697 value &= ~DSI_CONTROL_TX_TRIG(3);
698
699 /* enable DCS commands for command mode */
700 if (mode_flags & MIPI_DSI_MODE_VIDEO)
701 value &= ~DSI_CONTROL_DCS_ENABLE;
702 else
703 value |= DSI_CONTROL_DCS_ENABLE;
704
705 value |= DSI_CONTROL_VIDEO_ENABLE;
706 value &= ~DSI_CONTROL_HOST_ENABLE;
707 writel(value, &misc->dsi_ctrl);
708
709 for (i = 0; i < NUM_PKT_SEQ; i++)
710 writel(pkt_seq[i], &pkt->dsi_pkt_seq_0_lo + i);
711
712 if (mode_flags & MIPI_DSI_MODE_VIDEO) {
713 /* horizontal active pixels */
714 hact = timing->hactive.typ * mul / div;
715
716 /* horizontal sync width */
717 hsw = timing->hsync_len.typ * mul / div;
718
719 /* horizontal back porch */
720 hbp = timing->hback_porch.typ * mul / div;
721
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300722 /* horizontal front porch */
723 hfp = timing->hfront_porch.typ * mul / div;
724
Svyatoslav Ryhel10833fb2024-12-02 08:12:36 +0200725 if (priv->master || priv->slave) {
726 hact /= 2;
727 hsw /= 2;
728 hbp = hbp / 2 - 1;
729 hfp /= 2;
730 }
731
732 if ((mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) == 0)
733 hbp += hsw;
734
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300735 /* subtract packet overhead */
736 hsw -= 10;
737 hbp -= 14;
738 hfp -= 8;
739
740 writel(hsw << 16 | 0, &len->dsi_pkt_len_0_1);
741 writel(hact << 16 | hbp, &len->dsi_pkt_len_2_3);
742 writel(hfp, &len->dsi_pkt_len_4_5);
743 writel(0x0f0f << 16, &len->dsi_pkt_len_6_7);
744 } else {
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300745 if (priv->master || priv->slave) {
746 /*
747 * For ganged mode, assume symmetric left-right mode.
748 */
749 value = 1 + (timing->hactive.typ / 2) * mul / div;
750 } else {
751 /* 1 byte (DCS command) + pixel data */
752 value = 1 + timing->hactive.typ * mul / div;
753 }
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300754
755 writel(0, &len->dsi_pkt_len_0_1);
756 writel(value << 16, &len->dsi_pkt_len_2_3);
757 writel(value << 16, &len->dsi_pkt_len_4_5);
758 writel(0, &len->dsi_pkt_len_6_7);
759
760 value = MIPI_DCS_WRITE_MEMORY_START << 8 |
761 MIPI_DCS_WRITE_MEMORY_CONTINUE;
762 writel(value, &len->dsi_dcs_cmds);
Svyatoslav Ryhel18e4dcf2024-12-02 08:08:03 +0200763 }
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300764
Svyatoslav Ryhel18e4dcf2024-12-02 08:08:03 +0200765 /* set SOL delay */
766 if (priv->master || priv->slave) {
767 unsigned long delay, bclk, bclk_ganged;
Svyatoslav Ryhelf87af5a2025-02-26 09:51:09 +0200768 unsigned int lanes = tegra_dsi_get_lanes(dev);
Svyatoslav Ryhel18e4dcf2024-12-02 08:08:03 +0200769 unsigned long htotal = timing->hactive.typ + timing->hfront_porch.typ +
770 timing->hback_porch.typ + timing->hsync_len.typ;
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300771
Svyatoslav Ryhel18e4dcf2024-12-02 08:08:03 +0200772 /* SOL to valid, valid to FIFO and FIFO write delay */
773 delay = 4 + 4 + 2;
774 delay = DIV_ROUND_UP(delay * mul, div * lanes);
775 /* FIFO read delay */
776 delay = delay + 6;
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300777
Svyatoslav Ryhel18e4dcf2024-12-02 08:08:03 +0200778 bclk = DIV_ROUND_UP(htotal * mul, div * lanes);
779 bclk_ganged = DIV_ROUND_UP(bclk * lanes / 2, lanes);
780 value = bclk - bclk_ganged + delay + 20;
781 } else {
782 /* set SOL delay (for non-burst mode only) */
783 value = 8 * mul / div;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300784 }
785
Svyatoslav Ryhel18e4dcf2024-12-02 08:08:03 +0200786 writel(value, &misc->dsi_sol_delay);
787
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300788 if (priv->slave) {
Svyatoslav Ryhel6a5efea2024-12-31 09:58:01 +0200789 tegra_dsi_configure(priv->slave, pipe, mode_flags);
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300790 /*
791 * TODO: Support modes other than symmetrical left-right
792 * split.
793 */
794 tegra_dsi_ganged_enable(dev, 0, timing->hactive.typ / 2);
795 tegra_dsi_ganged_enable(priv->slave, timing->hactive.typ / 2,
796 timing->hactive.typ / 2);
797 }
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300798}
799
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200800static void tegra_dsi_enable(struct udevice *dev)
801{
802 struct tegra_dsi_priv *priv = dev_get_priv(dev);
803 struct dsi_misc_reg *misc = &priv->dsi->misc;
804 u32 value;
805
806 /* enable DSI controller */
807 value = readl(&misc->dsi_pwr_ctrl);
808 value |= DSI_POWER_CONTROL_ENABLE;
809 writel(value, &misc->dsi_pwr_ctrl);
810
811 if (priv->slave)
812 tegra_dsi_enable(priv->slave);
813}
814
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300815static int tegra_dsi_encoder_enable(struct udevice *dev)
816{
817 struct tegra_dsi_priv *priv = dev_get_priv(dev);
Svyatoslav Ryhel6a5efea2024-12-31 09:58:01 +0200818 struct tegra_dc_plat *dc_plat = dev_get_plat(dev);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300819 struct mipi_dsi_device *device = &priv->device;
820 struct display_timing *timing = &priv->timing;
821 struct dsi_misc_reg *misc = &priv->dsi->misc;
822 unsigned int mul, div;
823 unsigned long bclk, plld, period;
Svyatoslav Ryhelf87af5a2025-02-26 09:51:09 +0200824 u32 value, lanes;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300825 int ret;
826
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200827 /* If for some reasone DSI is enabled then it needs to
828 * be disabled in order for the panel initialization
829 * commands to be properly sent.
830 */
831 value = readl(&misc->dsi_pwr_ctrl);
832
833 if (value & DSI_POWER_CONTROL_ENABLE) {
834 value = readl(&misc->dsi_pwr_ctrl);
835 value &= ~DSI_POWER_CONTROL_ENABLE;
836 writel(value, &misc->dsi_pwr_ctrl);
837 }
838
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300839 /* Disable interrupt */
840 writel(0, &misc->int_enable);
841
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200842 if (priv->version)
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200843 tegra_dsi_mipi_calibrate(dev);
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +0200844 else
845 tegra_dsi_pad_calibrate(&priv->dsi->pad);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300846
847 tegra_dsi_get_muldiv(device->format, &mul, &div);
848
849 /* compute byte clock */
Svyatoslav Ryhelf87af5a2025-02-26 09:51:09 +0200850 lanes = tegra_dsi_get_lanes(dev);
851 bclk = (timing->pixelclock.typ * mul) / (div * lanes);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300852
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200853 tegra_dsi_set_timeout(dev, bclk, 60);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300854
855 /*
856 * Compute bit clock and round up to the next MHz.
857 */
858 plld = DIV_ROUND_UP(bclk * 8, USEC_PER_SEC) * USEC_PER_SEC;
859 period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, plld);
860
861 ret = mipi_dphy_timing_get_default(&priv->dphy_timing, period);
862 if (ret < 0) {
863 printf("%s: failed to get D-PHY timing: %d\n", __func__, ret);
864 return ret;
865 }
866
867 ret = mipi_dphy_timing_validate(&priv->dphy_timing, period);
868 if (ret < 0) {
869 printf("%s: failed to validate D-PHY timing: %d\n", __func__, ret);
870 return ret;
871 }
872
873 /*
874 * The D-PHY timing fields are expressed in byte-clock cycles, so
875 * multiply the period by 8.
876 */
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200877 tegra_dsi_set_phy_timing(dev, period * 8, &priv->dphy_timing);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300878
879 /* Perform panel HW setup */
880 ret = panel_enable_backlight(priv->panel);
881 if (ret)
882 return ret;
883
Svyatoslav Ryhel6a5efea2024-12-31 09:58:01 +0200884 tegra_dsi_configure(dev, dc_plat->pipe, device->mode_flags);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300885
886 tegra_dc_enable_controller(dev);
887
Svyatoslav Ryhel8e50b622024-12-31 09:50:03 +0200888 tegra_dsi_enable(dev);
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300889
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300890 return 0;
891}
892
893static int tegra_dsi_bridge_set_panel(struct udevice *dev, int percent)
894{
Jonas Schwöbeldea5d962024-01-23 19:16:33 +0200895 struct tegra_dsi_priv *priv = dev_get_priv(dev);
896
897 /* Turn on/off backlight */
898 return panel_set_backlight(priv->panel, percent);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300899}
900
901static int tegra_dsi_panel_timings(struct udevice *dev,
902 struct display_timing *timing)
903{
904 struct tegra_dsi_priv *priv = dev_get_priv(dev);
905
906 memcpy(timing, &priv->timing, sizeof(*timing));
907
908 return 0;
909}
910
911static void tegra_dsi_init_clocks(struct udevice *dev)
912{
913 struct tegra_dsi_priv *priv = dev_get_priv(dev);
Svyatoslav Ryheld16c1052024-01-23 19:16:23 +0200914 struct tegra_dc_plat *dc_plat = dev_get_plat(dev);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300915 struct mipi_dsi_device *device = &priv->device;
Svyatoslav Ryhelf87af5a2025-02-26 09:51:09 +0200916 unsigned int mul, div, lanes;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300917 unsigned long bclk, plld;
918
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +0200919 /* Switch parents of DSI clocks in case of not standard parent */
920 if (priv->clk->id == PERIPH_ID_DSI &&
921 priv->clk_parent->id == CLOCK_ID_DISPLAY2) {
922 /* Change DSIA clock parent to PLLD2 */
923 struct clk_rst_ctlr *clkrst =
924 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
925
926 /* DSIA_CLK_SRC */
927 setbits_le32(&clkrst->crc_pll[CLOCK_ID_DISPLAY].pll_base,
928 BIT(25));
929 }
930
931 if (priv->clk->id == PERIPH_ID_DSIB &&
932 priv->clk_parent->id == CLOCK_ID_DISPLAY) {
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300933 /* Change DSIB clock parent to match DSIA */
934 struct clk_rst_ctlr *clkrst =
935 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
936
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +0200937 /* DSIB_CLK_SRC */
938 clrbits_le32(&clkrst->plld2.pll_base, BIT(25));
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300939 }
940
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300941 tegra_dsi_get_muldiv(device->format, &mul, &div);
942
Svyatoslav Ryhelf87af5a2025-02-26 09:51:09 +0200943 lanes = tegra_dsi_get_lanes(dev);
944 bclk = (priv->timing.pixelclock.typ * mul) / (div * lanes);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300945
946 plld = DIV_ROUND_UP(bclk * 8, USEC_PER_SEC);
947
Svyatoslav Ryheld16c1052024-01-23 19:16:23 +0200948 dc_plat->scdiv = ((plld * USEC_PER_SEC +
949 priv->timing.pixelclock.typ / 2) /
950 priv->timing.pixelclock.typ) - 2;
951
952 /*
953 * BUG: If DISP1 is a PLLD/D2 child, it cannot go over 370MHz. The
954 * cause of this is not quite clear. This can be overcomed by
955 * halving the PLLD/D2 if the target rate is > 800MHz. This way
956 * DISP1 and DSI clocks will be equal.
957 */
958 if (plld > 800)
959 plld /= 2;
960
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300961 switch (clock_get_osc_freq()) {
962 case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
963 case CLOCK_OSC_FREQ_48_0: /* OSC is 48Mhz */
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +0200964 clock_set_rate(priv->clk_parent->id, plld, 12, 0, 8);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300965 break;
966
967 case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +0200968 clock_set_rate(priv->clk_parent->id, plld, 26, 0, 8);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300969 break;
970
971 case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
972 case CLOCK_OSC_FREQ_16_8: /* OSC is 16.8Mhz */
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +0200973 clock_set_rate(priv->clk_parent->id, plld, 13, 0, 8);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300974 break;
975
976 case CLOCK_OSC_FREQ_19_2:
977 case CLOCK_OSC_FREQ_38_4:
978 default:
979 /*
980 * These are not supported.
981 */
982 break;
983 }
984
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +0200985 clk_enable(priv->clk);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300986}
987
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +0300988static int tegra_dsi_ganged_probe(struct udevice *dev)
989{
990 struct tegra_dsi_priv *mpriv = dev_get_priv(dev);
991 struct udevice *gangster;
992
993 uclass_get_device_by_phandle(UCLASS_PANEL, dev,
994 "nvidia,ganged-mode", &gangster);
995 if (gangster) {
996 /* Ganged mode is set */
997 struct tegra_dsi_priv *spriv = dev_get_priv(gangster);
998
999 mpriv->slave = gangster;
1000 spriv->master = dev;
1001 }
1002
1003 return 0;
1004}
1005
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001006static int tegra_dsi_bridge_probe(struct udevice *dev)
1007{
1008 struct tegra_dsi_priv *priv = dev_get_priv(dev);
1009 struct mipi_dsi_device *device = &priv->device;
1010 struct mipi_dsi_panel_plat *mipi_plat;
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +02001011 struct reset_ctl reset_ctl;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001012 int ret;
1013
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +02001014 priv->version = dev_get_driver_data(dev);
1015
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001016 priv->dsi = (struct dsi_ctlr *)dev_read_addr_ptr(dev);
1017 if (!priv->dsi) {
1018 printf("%s: No display controller address\n", __func__);
1019 return -EINVAL;
1020 }
1021
Svyatoslav Ryhel71f2c362024-11-24 14:26:08 +02001022 priv->clk = devm_clk_get(dev, NULL);
1023 if (IS_ERR(priv->clk)) {
1024 log_debug("%s: Could not get DSI clock: %ld\n",
1025 __func__, PTR_ERR(priv->clk));
1026 return PTR_ERR(priv->clk);
1027 }
1028
1029 priv->clk_parent = devm_clk_get(dev, "parent");
1030 if (IS_ERR(priv->clk_parent)) {
1031 log_debug("%s: Could not get DSI clock parent: %ld\n",
1032 __func__, PTR_ERR(priv->clk_parent));
1033 return PTR_ERR(priv->clk_parent);
1034 }
1035
Jonas Schwöbel0cccf902024-01-23 19:16:32 +02001036 priv->video_fifo_depth = 1920;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001037 priv->host_fifo_depth = 64;
1038
Svyatoslav Ryhelab120e52024-07-30 13:35:45 +03001039 tegra_dsi_ganged_probe(dev);
1040
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +02001041 ret = reset_get_by_name(dev, "dsi", &reset_ctl);
1042 if (ret) {
1043 log_debug("%s: reset_get_by_name() failed: %d\n",
1044 __func__, ret);
1045 return ret;
1046 }
1047
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001048 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
1049 "avdd-dsi-csi-supply", &priv->avdd);
1050 if (ret)
1051 debug("%s: Cannot get avdd-dsi-csi-supply: error %d\n",
1052 __func__, ret);
1053
Svyatoslav Ryhelf6b2ab42024-11-24 09:38:03 +02001054 /* Check all DSI children */
1055 device_foreach_child(priv->panel, dev) {
1056 if (device_get_uclass_id(priv->panel) == UCLASS_PANEL)
1057 break;
1058 }
1059
1060 /* if loop exits without panel device return error */
1061 if (device_get_uclass_id(priv->panel) != UCLASS_PANEL) {
1062 log_debug("%s: panel not found, ret %d\n", __func__, ret);
1063 return -EINVAL;
1064 }
1065
1066 ret = uclass_get_device_by_ofnode(UCLASS_PANEL, dev_ofnode(priv->panel),
1067 &priv->panel);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001068 if (ret) {
Svyatoslav Ryhelf6b2ab42024-11-24 09:38:03 +02001069 log_debug("%s: Cannot get panel: error %d\n", __func__, ret);
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001070 return log_ret(ret);
1071 }
1072
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +02001073 if (priv->version) {
1074 ret = uclass_get_device_by_phandle(UCLASS_MISC, dev,
1075 "nvidia,mipi-calibrate",
1076 &priv->mipi);
1077 if (ret) {
1078 log_debug("%s: cannot get MIPI: error %d\n", __func__, ret);
1079 return ret;
1080 }
1081 }
1082
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001083 panel_get_display_timing(priv->panel, &priv->timing);
1084
1085 mipi_plat = dev_get_plat(priv->panel);
1086 mipi_plat->device = device;
1087
1088 priv->host.dev = (struct device *)dev;
1089 priv->host.ops = &tegra_dsi_bridge_host_ops;
1090
1091 device->host = &priv->host;
1092 device->lanes = mipi_plat->lanes;
1093 device->format = mipi_plat->format;
1094 device->mode_flags = mipi_plat->mode_flags;
1095
1096 tegra_dsi_get_format(device->format, &priv->format);
1097
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +02001098 reset_assert(&reset_ctl);
1099
Svyatoslav Ryheldaab7d92023-10-03 09:25:34 +03001100 ret = regulator_set_enable_if_allowed(priv->avdd, true);
1101 if (ret && ret != -ENOSYS)
1102 return ret;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001103
1104 tegra_dsi_init_clocks(dev);
1105
Svyatoslav Ryhelfe0a53a2024-01-23 19:16:30 +02001106 mdelay(2);
1107 reset_deassert(&reset_ctl);
1108
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001109 return 0;
1110}
1111
1112static const struct panel_ops tegra_dsi_bridge_ops = {
1113 .enable_backlight = tegra_dsi_encoder_enable,
1114 .set_backlight = tegra_dsi_bridge_set_panel,
1115 .get_display_timing = tegra_dsi_panel_timings,
1116};
1117
1118static const struct udevice_id tegra_dsi_bridge_ids[] = {
Svyatoslav Ryhel094d4f92024-01-23 19:16:29 +02001119 { .compatible = "nvidia,tegra30-dsi", .data = DSI_V0 },
1120 { .compatible = "nvidia,tegra114-dsi", .data = DSI_V1 },
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001121 { }
1122};
1123
1124U_BOOT_DRIVER(tegra_dsi) = {
1125 .name = "tegra_dsi",
1126 .id = UCLASS_PANEL,
1127 .of_match = tegra_dsi_bridge_ids,
1128 .ops = &tegra_dsi_bridge_ops,
Svyatoslav Ryhelf6b2ab42024-11-24 09:38:03 +02001129 .bind = dm_scan_fdt_dev,
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001130 .probe = tegra_dsi_bridge_probe,
1131 .plat_auto = sizeof(struct tegra_dc_plat),
1132 .priv_auto = sizeof(struct tegra_dsi_priv),
1133};