blob: 72b91ed26bfb1e5fb3f5b1e3965614914b82cd14 [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
7#include <common.h>
8#include <dm.h>
9#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>
15#include <linux/delay.h>
16#include <linux/err.h>
Igor Prusovc3421ea2023-11-09 20:10:04 +030017#include <linux/time.h>
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030018#include <power/regulator.h>
19
20#include <asm/gpio.h>
21#include <asm/io.h>
22#include <asm/arch/clock.h>
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030023#include <asm/arch-tegra30/dsi.h>
24
Svyatoslav Ryhel75fec412024-01-23 19:16:18 +020025#include "tegra-dc.h"
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030026#include "mipi-phy.h"
27
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +030028struct tegra_dsi_priv {
29 struct mipi_dsi_host host;
30 struct mipi_dsi_device device;
31 struct mipi_dphy_timing dphy_timing;
32
33 struct udevice *panel;
34 struct display_timing timing;
35
36 struct dsi_ctlr *dsi;
37 struct udevice *avdd;
38
39 enum tegra_dsi_format format;
40
41 int dsi_clk;
42 int video_fifo_depth;
43 int host_fifo_depth;
44};
45
46static void tegra_dc_enable_controller(struct udevice *dev)
47{
48 struct tegra_dc_plat *dc_plat = dev_get_plat(dev);
49 struct dc_ctlr *dc = dc_plat->dc;
50 u32 value;
51
52 value = readl(&dc->disp.disp_win_opt);
53 value |= DSI_ENABLE;
54 writel(value, &dc->disp.disp_win_opt);
55
56 writel(GENERAL_UPDATE, &dc->cmd.state_ctrl);
57 writel(GENERAL_ACT_REQ, &dc->cmd.state_ctrl);
58}
59
60static const char * const error_report[16] = {
61 "SoT Error",
62 "SoT Sync Error",
63 "EoT Sync Error",
64 "Escape Mode Entry Command Error",
65 "Low-Power Transmit Sync Error",
66 "Peripheral Timeout Error",
67 "False Control Error",
68 "Contention Detected",
69 "ECC Error, single-bit",
70 "ECC Error, multi-bit",
71 "Checksum Error",
72 "DSI Data Type Not Recognized",
73 "DSI VC ID Invalid",
74 "Invalid Transmission Length",
75 "Reserved",
76 "DSI Protocol Violation",
77};
78
79static ssize_t tegra_dsi_read_response(struct dsi_misc_reg *misc,
80 const struct mipi_dsi_msg *msg,
81 size_t count)
82{
83 u8 *rx = msg->rx_buf;
84 unsigned int i, j, k;
85 size_t size = 0;
86 u16 errors;
87 u32 value;
88
89 /* read and parse packet header */
90 value = readl(&misc->dsi_rd_data);
91
92 switch (value & 0x3f) {
93 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
94 errors = (value >> 8) & 0xffff;
95 printf("%s: Acknowledge and error report: %04x\n",
96 __func__, errors);
97 for (i = 0; i < ARRAY_SIZE(error_report); i++)
98 if (errors & BIT(i))
99 printf("%s: %2u: %s\n", __func__, i,
100 error_report[i]);
101 break;
102
103 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
104 rx[0] = (value >> 8) & 0xff;
105 size = 1;
106 break;
107
108 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
109 rx[0] = (value >> 8) & 0xff;
110 rx[1] = (value >> 16) & 0xff;
111 size = 2;
112 break;
113
114 case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
115 size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
116 break;
117
118 case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
119 size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
120 break;
121
122 default:
123 printf("%s: unhandled response type: %02x\n",
124 __func__, value & 0x3f);
125 return -EPROTO;
126 }
127
128 size = min(size, msg->rx_len);
129
130 if (msg->rx_buf && size > 0) {
131 for (i = 0, j = 0; i < count - 1; i++, j += 4) {
132 u8 *rx = msg->rx_buf + j;
133
134 value = readl(&misc->dsi_rd_data);
135
136 for (k = 0; k < 4 && (j + k) < msg->rx_len; k++)
137 rx[j + k] = (value >> (k << 3)) & 0xff;
138 }
139 }
140
141 return size;
142}
143
144static int tegra_dsi_transmit(struct dsi_misc_reg *misc,
145 unsigned long timeout)
146{
147 writel(DSI_TRIGGER_HOST, &misc->dsi_trigger);
148
149 while (timeout--) {
150 u32 value = readl(&misc->dsi_trigger);
151
152 if ((value & DSI_TRIGGER_HOST) == 0)
153 return 0;
154
155 udelay(1000);
156 }
157
158 debug("timeout waiting for transmission to complete\n");
159 return -ETIMEDOUT;
160}
161
162static int tegra_dsi_wait_for_response(struct dsi_misc_reg *misc,
163 unsigned long timeout)
164{
165 while (timeout--) {
166 u32 value = readl(&misc->dsi_status);
167 u8 count = value & 0x1f;
168
169 if (count > 0)
170 return count;
171
172 udelay(1000);
173 }
174
175 debug("peripheral returned no data\n");
176 return -ETIMEDOUT;
177}
178
179static void tegra_dsi_writesl(struct dsi_misc_reg *misc,
180 const void *buffer, size_t size)
181{
182 const u8 *buf = buffer;
183 size_t i, j;
184 u32 value;
185
186 for (j = 0; j < size; j += 4) {
187 value = 0;
188
189 for (i = 0; i < 4 && j + i < size; i++)
190 value |= buf[j + i] << (i << 3);
191
192 writel(value, &misc->dsi_wr_data);
193 }
194}
195
196static ssize_t tegra_dsi_host_transfer(struct mipi_dsi_host *host,
197 const struct mipi_dsi_msg *msg)
198{
199 struct udevice *dev = (struct udevice *)host->dev;
200 struct tegra_dsi_priv *priv = dev_get_priv(dev);
201 struct dsi_misc_reg *misc = &priv->dsi->misc;
202 struct mipi_dsi_packet packet;
203 const u8 *header;
204 size_t count;
205 ssize_t err;
206 u32 value;
207
208 err = mipi_dsi_create_packet(&packet, msg);
209 if (err < 0)
210 return err;
211
212 header = packet.header;
213
214 /* maximum FIFO depth is 1920 words */
215 if (packet.size > priv->video_fifo_depth * 4)
216 return -ENOSPC;
217
218 /* reset underflow/overflow flags */
219 value = readl(&misc->dsi_status);
220 if (value & (DSI_STATUS_UNDERFLOW | DSI_STATUS_OVERFLOW)) {
221 value = DSI_HOST_CONTROL_FIFO_RESET;
222 writel(value, &misc->host_dsi_ctrl);
223 udelay(10);
224 }
225
226 value = readl(&misc->dsi_pwr_ctrl);
227 value |= DSI_POWER_CONTROL_ENABLE;
228 writel(value, &misc->dsi_pwr_ctrl);
229
230 mdelay(5);
231
232 value = DSI_HOST_CONTROL_CRC_RESET | DSI_HOST_CONTROL_TX_TRIG_HOST |
233 DSI_HOST_CONTROL_CS | DSI_HOST_CONTROL_ECC;
234
235 /*
236 * The host FIFO has a maximum of 64 words, so larger transmissions
237 * need to use the video FIFO.
238 */
239 if (packet.size > priv->host_fifo_depth * 4)
240 value |= DSI_HOST_CONTROL_FIFO_SEL;
241
242 writel(value, &misc->host_dsi_ctrl);
243
244 /*
245 * For reads and messages with explicitly requested ACK, generate a
246 * BTA sequence after the transmission of the packet.
247 */
248 if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
249 (msg->rx_buf && msg->rx_len > 0)) {
250 value = readl(&misc->host_dsi_ctrl);
251 value |= DSI_HOST_CONTROL_PKT_BTA;
252 writel(value, &misc->host_dsi_ctrl);
253 }
254
255 value = DSI_CONTROL_LANES(0) | DSI_CONTROL_HOST_ENABLE;
256 writel(value, &misc->dsi_ctrl);
257
258 /* write packet header, ECC is generated by hardware */
259 value = header[2] << 16 | header[1] << 8 | header[0];
260 writel(value, &misc->dsi_wr_data);
261
262 /* write payload (if any) */
263 if (packet.payload_length > 0)
264 tegra_dsi_writesl(misc, packet.payload,
265 packet.payload_length);
266
267 err = tegra_dsi_transmit(misc, 250);
268 if (err < 0)
269 return err;
270
271 if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
272 (msg->rx_buf && msg->rx_len > 0)) {
273 err = tegra_dsi_wait_for_response(misc, 250);
274 if (err < 0)
275 return err;
276
277 count = err;
278
279 value = readl(&misc->dsi_rd_data);
280 switch (value) {
281 case 0x84:
282 debug("%s: ACK\n", __func__);
283 break;
284
285 case 0x87:
286 debug("%s: ESCAPE\n", __func__);
287 break;
288
289 default:
290 printf("%s: unknown status: %08x\n", __func__, value);
291 break;
292 }
293
294 if (count > 1) {
295 err = tegra_dsi_read_response(misc, msg, count);
296 if (err < 0) {
297 printf("%s: failed to parse response: %zd\n",
298 __func__, err);
299 } else {
300 /*
301 * For read commands, return the number of
302 * bytes returned by the peripheral.
303 */
304 count = err;
305 }
306 }
307 } else {
308 /*
309 * For write commands, we have transmitted the 4-byte header
310 * plus the variable-length payload.
311 */
312 count = 4 + packet.payload_length;
313 }
314
315 return count;
316}
317
318struct mipi_dsi_host_ops tegra_dsi_bridge_host_ops = {
319 .transfer = tegra_dsi_host_transfer,
320};
321
322#define PKT_ID0(id) ((((id) & 0x3f) << 3) | (1 << 9))
323#define PKT_LEN0(len) (((len) & 0x07) << 0)
324#define PKT_ID1(id) ((((id) & 0x3f) << 13) | (1 << 19))
325#define PKT_LEN1(len) (((len) & 0x07) << 10)
326#define PKT_ID2(id) ((((id) & 0x3f) << 23) | (1 << 29))
327#define PKT_LEN2(len) (((len) & 0x07) << 20)
328
329#define PKT_LP BIT(30)
330#define NUM_PKT_SEQ 12
331
332/*
333 * non-burst mode with sync pulses
334 */
335static const u32 pkt_seq_video_non_burst_sync_pulses[NUM_PKT_SEQ] = {
336 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
337 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
338 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
339 PKT_LP,
340 [ 1] = 0,
341 [ 2] = PKT_ID0(MIPI_DSI_V_SYNC_END) | PKT_LEN0(0) |
342 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
343 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
344 PKT_LP,
345 [ 3] = 0,
346 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
347 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
348 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
349 PKT_LP,
350 [ 5] = 0,
351 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
352 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
353 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
354 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
355 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
356 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
357 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
358 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
359 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
360 PKT_LP,
361 [ 9] = 0,
362 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
363 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
364 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
365 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
366 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
367 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
368};
369
370/*
371 * non-burst mode with sync events
372 */
373static const u32 pkt_seq_video_non_burst_sync_events[NUM_PKT_SEQ] = {
374 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
375 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
376 PKT_LP,
377 [ 1] = 0,
378 [ 2] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
379 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
380 PKT_LP,
381 [ 3] = 0,
382 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
383 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
384 PKT_LP,
385 [ 5] = 0,
386 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
387 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
388 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
389 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
390 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
391 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
392 PKT_LP,
393 [ 9] = 0,
394 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
395 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
396 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
397 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
398};
399
400static const u32 pkt_seq_command_mode[NUM_PKT_SEQ] = {
401 [ 0] = 0,
402 [ 1] = 0,
403 [ 2] = 0,
404 [ 3] = 0,
405 [ 4] = 0,
406 [ 5] = 0,
407 [ 6] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(3) | PKT_LP,
408 [ 7] = 0,
409 [ 8] = 0,
410 [ 9] = 0,
411 [10] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(5) | PKT_LP,
412 [11] = 0,
413};
414
415static void tegra_dsi_get_muldiv(enum mipi_dsi_pixel_format format,
416 unsigned int *mulp, unsigned int *divp)
417{
418 switch (format) {
419 case MIPI_DSI_FMT_RGB666_PACKED:
420 case MIPI_DSI_FMT_RGB888:
421 *mulp = 3;
422 *divp = 1;
423 break;
424
425 case MIPI_DSI_FMT_RGB565:
426 *mulp = 2;
427 *divp = 1;
428 break;
429
430 case MIPI_DSI_FMT_RGB666:
431 *mulp = 9;
432 *divp = 4;
433 break;
434
435 default:
436 break;
437 }
438}
439
440static int tegra_dsi_get_format(enum mipi_dsi_pixel_format format,
441 enum tegra_dsi_format *fmt)
442{
443 switch (format) {
444 case MIPI_DSI_FMT_RGB888:
445 *fmt = TEGRA_DSI_FORMAT_24P;
446 break;
447
448 case MIPI_DSI_FMT_RGB666:
449 *fmt = TEGRA_DSI_FORMAT_18NP;
450 break;
451
452 case MIPI_DSI_FMT_RGB666_PACKED:
453 *fmt = TEGRA_DSI_FORMAT_18P;
454 break;
455
456 case MIPI_DSI_FMT_RGB565:
457 *fmt = TEGRA_DSI_FORMAT_16P;
458 break;
459
460 default:
461 return -EINVAL;
462 }
463
464 return 0;
465}
466
467static void tegra_dsi_pad_calibrate(struct dsi_pad_ctrl_reg *pad)
468{
469 u32 value;
470
471 /* start calibration */
472 value = DSI_PAD_CONTROL_PAD_LPUPADJ(0x1) |
473 DSI_PAD_CONTROL_PAD_LPDNADJ(0x1) |
474 DSI_PAD_CONTROL_PAD_PREEMP_EN(0x1) |
475 DSI_PAD_CONTROL_PAD_SLEWDNADJ(0x6) |
476 DSI_PAD_CONTROL_PAD_SLEWUPADJ(0x6) |
477 DSI_PAD_CONTROL_PAD_PDIO(0) |
478 DSI_PAD_CONTROL_PAD_PDIO_CLK(0) |
479 DSI_PAD_CONTROL_PAD_PULLDN_ENAB(0);
480 writel(value, &pad->pad_ctrl);
481
482 clock_enable(PERIPH_ID_VI);
483 clock_enable(PERIPH_ID_CSI);
484 udelay(2);
485 reset_set_enable(PERIPH_ID_VI, 0);
486 reset_set_enable(PERIPH_ID_CSI, 0);
487
488 value = MIPI_CAL_TERMOSA(0x4);
489 writel(value, TEGRA_VI_BASE + (CSI_CILA_MIPI_CAL_CONFIG_0 << 2));
490
491 value = MIPI_CAL_TERMOSB(0x4);
492 writel(value, TEGRA_VI_BASE + (CSI_CILB_MIPI_CAL_CONFIG_0 << 2));
493
494 value = MIPI_CAL_HSPUOSD(0x3) | MIPI_CAL_HSPDOSD(0x4);
495 writel(value, TEGRA_VI_BASE + (CSI_DSI_MIPI_CAL_CONFIG << 2));
496
497 value = PAD_DRIV_DN_REF(0x5) | PAD_DRIV_UP_REF(0x7);
498 writel(value, TEGRA_VI_BASE + (CSI_MIPIBIAS_PAD_CONFIG << 2));
499
500 value = PAD_CIL_PDVREG(0x0);
501 writel(value, TEGRA_VI_BASE + (CSI_CIL_PAD_CONFIG << 2));
502}
503
504static void tegra_dsi_set_timeout(struct dsi_timeout_reg *rtimeout,
505 unsigned long bclk,
506 unsigned int vrefresh)
507{
508 unsigned int timeout;
509 u32 value;
510
511 /* one frame high-speed transmission timeout */
512 timeout = (bclk / vrefresh) / 512;
513 value = DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout);
514 writel(value, &rtimeout->dsi_timeout_0);
515
516 /* 2 ms peripheral timeout for panel */
517 timeout = 2 * bclk / 512 * 1000;
518 value = DSI_TIMEOUT_PR(timeout) | DSI_TIMEOUT_TA(0x2000);
519 writel(value, &rtimeout->dsi_timeout_1);
520
521 value = DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
522 writel(value, &rtimeout->dsi_to_tally);
523}
524
525static void tegra_dsi_set_phy_timing(struct dsi_timing_reg *ptiming,
526 unsigned long period,
527 const struct mipi_dphy_timing *dphy_timing)
528{
529 u32 value;
530
531 value = DSI_TIMING_FIELD(dphy_timing->hsexit, period, 1) << 24 |
532 DSI_TIMING_FIELD(dphy_timing->hstrail, period, 0) << 16 |
533 DSI_TIMING_FIELD(dphy_timing->hszero, period, 3) << 8 |
534 DSI_TIMING_FIELD(dphy_timing->hsprepare, period, 1);
535 writel(value, &ptiming->dsi_phy_timing_0);
536
537 value = DSI_TIMING_FIELD(dphy_timing->clktrail, period, 1) << 24 |
538 DSI_TIMING_FIELD(dphy_timing->clkpost, period, 1) << 16 |
539 DSI_TIMING_FIELD(dphy_timing->clkzero, period, 1) << 8 |
540 DSI_TIMING_FIELD(dphy_timing->lpx, period, 1);
541 writel(value, &ptiming->dsi_phy_timing_1);
542
543 value = DSI_TIMING_FIELD(dphy_timing->clkprepare, period, 1) << 16 |
544 DSI_TIMING_FIELD(dphy_timing->clkpre, period, 1) << 8 |
545 DSI_TIMING_FIELD(0xff * period, period, 0) << 0;
546 writel(value, &ptiming->dsi_phy_timing_2);
547
548 value = DSI_TIMING_FIELD(dphy_timing->taget, period, 1) << 16 |
549 DSI_TIMING_FIELD(dphy_timing->tasure, period, 1) << 8 |
550 DSI_TIMING_FIELD(dphy_timing->tago, period, 1);
551 writel(value, &ptiming->dsi_bta_timing);
552}
553
554static void tegra_dsi_configure(struct udevice *dev,
555 unsigned long mode_flags)
556{
557 struct tegra_dsi_priv *priv = dev_get_priv(dev);
558 struct mipi_dsi_device *device = &priv->device;
559 struct display_timing *timing = &priv->timing;
560
561 struct dsi_misc_reg *misc = &priv->dsi->misc;
562 struct dsi_pkt_seq_reg *pkt = &priv->dsi->pkt;
563 struct dsi_pkt_len_reg *len = &priv->dsi->len;
564
565 unsigned int hact, hsw, hbp, hfp, i, mul, div;
566 const u32 *pkt_seq;
567 u32 value;
568
569 tegra_dsi_get_muldiv(device->format, &mul, &div);
570
571 if (mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
572 printf("[DSI] Non-burst video mode with sync pulses\n");
573 pkt_seq = pkt_seq_video_non_burst_sync_pulses;
574 } else if (mode_flags & MIPI_DSI_MODE_VIDEO) {
575 printf("[DSI] Non-burst video mode with sync events\n");
576 pkt_seq = pkt_seq_video_non_burst_sync_events;
577 } else {
578 printf("[DSI] Command mode\n");
579 pkt_seq = pkt_seq_command_mode;
580 }
581
582 value = DSI_CONTROL_CHANNEL(0) |
583 DSI_CONTROL_FORMAT(priv->format) |
584 DSI_CONTROL_LANES(device->lanes - 1) |
585 DSI_CONTROL_SOURCE(0);
586 writel(value, &misc->dsi_ctrl);
587
588 writel(priv->video_fifo_depth, &misc->dsi_max_threshold);
589
590 value = DSI_HOST_CONTROL_HS;
591 writel(value, &misc->host_dsi_ctrl);
592
593 value = readl(&misc->dsi_ctrl);
594
595 if (mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
596 value |= DSI_CONTROL_HS_CLK_CTRL;
597
598 value &= ~DSI_CONTROL_TX_TRIG(3);
599
600 /* enable DCS commands for command mode */
601 if (mode_flags & MIPI_DSI_MODE_VIDEO)
602 value &= ~DSI_CONTROL_DCS_ENABLE;
603 else
604 value |= DSI_CONTROL_DCS_ENABLE;
605
606 value |= DSI_CONTROL_VIDEO_ENABLE;
607 value &= ~DSI_CONTROL_HOST_ENABLE;
608 writel(value, &misc->dsi_ctrl);
609
610 for (i = 0; i < NUM_PKT_SEQ; i++)
611 writel(pkt_seq[i], &pkt->dsi_pkt_seq_0_lo + i);
612
613 if (mode_flags & MIPI_DSI_MODE_VIDEO) {
614 /* horizontal active pixels */
615 hact = timing->hactive.typ * mul / div;
616
617 /* horizontal sync width */
618 hsw = timing->hsync_len.typ * mul / div;
619
620 /* horizontal back porch */
621 hbp = timing->hback_porch.typ * mul / div;
622
623 if ((mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) == 0)
624 hbp += hsw;
625
626 /* horizontal front porch */
627 hfp = timing->hfront_porch.typ * mul / div;
628
629 /* subtract packet overhead */
630 hsw -= 10;
631 hbp -= 14;
632 hfp -= 8;
633
634 writel(hsw << 16 | 0, &len->dsi_pkt_len_0_1);
635 writel(hact << 16 | hbp, &len->dsi_pkt_len_2_3);
636 writel(hfp, &len->dsi_pkt_len_4_5);
637 writel(0x0f0f << 16, &len->dsi_pkt_len_6_7);
638 } else {
639 /* 1 byte (DCS command) + pixel data */
640 value = 1 + timing->hactive.typ * mul / div;
641
642 writel(0, &len->dsi_pkt_len_0_1);
643 writel(value << 16, &len->dsi_pkt_len_2_3);
644 writel(value << 16, &len->dsi_pkt_len_4_5);
645 writel(0, &len->dsi_pkt_len_6_7);
646
647 value = MIPI_DCS_WRITE_MEMORY_START << 8 |
648 MIPI_DCS_WRITE_MEMORY_CONTINUE;
649 writel(value, &len->dsi_dcs_cmds);
650 }
651
652 /* set SOL delay (for non-burst mode only) */
653 writel(8 * mul / div, &misc->dsi_sol_delay);
654}
655
656static int tegra_dsi_encoder_enable(struct udevice *dev)
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 struct dsi_misc_reg *misc = &priv->dsi->misc;
662 unsigned int mul, div;
663 unsigned long bclk, plld, period;
664 u32 value;
665 int ret;
666
667 /* Disable interrupt */
668 writel(0, &misc->int_enable);
669
670 tegra_dsi_pad_calibrate(&priv->dsi->pad);
671
672 tegra_dsi_get_muldiv(device->format, &mul, &div);
673
674 /* compute byte clock */
675 bclk = (timing->pixelclock.typ * mul) / (div * device->lanes);
676
677 tegra_dsi_set_timeout(&priv->dsi->timeout, bclk, 60);
678
679 /*
680 * Compute bit clock and round up to the next MHz.
681 */
682 plld = DIV_ROUND_UP(bclk * 8, USEC_PER_SEC) * USEC_PER_SEC;
683 period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, plld);
684
685 ret = mipi_dphy_timing_get_default(&priv->dphy_timing, period);
686 if (ret < 0) {
687 printf("%s: failed to get D-PHY timing: %d\n", __func__, ret);
688 return ret;
689 }
690
691 ret = mipi_dphy_timing_validate(&priv->dphy_timing, period);
692 if (ret < 0) {
693 printf("%s: failed to validate D-PHY timing: %d\n", __func__, ret);
694 return ret;
695 }
696
697 /*
698 * The D-PHY timing fields are expressed in byte-clock cycles, so
699 * multiply the period by 8.
700 */
701 tegra_dsi_set_phy_timing(&priv->dsi->ptiming,
702 period * 8, &priv->dphy_timing);
703
704 /* Perform panel HW setup */
705 ret = panel_enable_backlight(priv->panel);
706 if (ret)
707 return ret;
708
709 tegra_dsi_configure(dev, 0);
710
711 ret = panel_set_backlight(priv->panel, BACKLIGHT_DEFAULT);
712 if (ret)
713 return ret;
714
715 tegra_dsi_configure(dev, device->mode_flags);
716
717 tegra_dc_enable_controller(dev);
718
719 /* enable DSI controller */
720 value = readl(&misc->dsi_pwr_ctrl);
721 value |= DSI_POWER_CONTROL_ENABLE;
722 writel(value, &misc->dsi_pwr_ctrl);
723
724 return 0;
725}
726
727static int tegra_dsi_bridge_set_panel(struct udevice *dev, int percent)
728{
729 /* Is not used in tegra dc */
730 return 0;
731}
732
733static int tegra_dsi_panel_timings(struct udevice *dev,
734 struct display_timing *timing)
735{
736 struct tegra_dsi_priv *priv = dev_get_priv(dev);
737
738 memcpy(timing, &priv->timing, sizeof(*timing));
739
740 return 0;
741}
742
743static void tegra_dsi_init_clocks(struct udevice *dev)
744{
745 struct tegra_dsi_priv *priv = dev_get_priv(dev);
746 struct mipi_dsi_device *device = &priv->device;
747 unsigned int mul, div;
748 unsigned long bclk, plld;
749
750 tegra_dsi_get_muldiv(device->format, &mul, &div);
751
752 bclk = (priv->timing.pixelclock.typ * mul) /
753 (div * device->lanes);
754
755 plld = DIV_ROUND_UP(bclk * 8, USEC_PER_SEC);
756
757 switch (clock_get_osc_freq()) {
758 case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
759 case CLOCK_OSC_FREQ_48_0: /* OSC is 48Mhz */
760 clock_set_rate(CLOCK_ID_DISPLAY, plld, 12, 0, 8);
761 break;
762
763 case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
764 clock_set_rate(CLOCK_ID_DISPLAY, plld, 26, 0, 8);
765 break;
766
767 case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
768 case CLOCK_OSC_FREQ_16_8: /* OSC is 16.8Mhz */
769 clock_set_rate(CLOCK_ID_DISPLAY, plld, 13, 0, 8);
770 break;
771
772 case CLOCK_OSC_FREQ_19_2:
773 case CLOCK_OSC_FREQ_38_4:
774 default:
775 /*
776 * These are not supported.
777 */
778 break;
779 }
780
781 priv->dsi_clk = clock_decode_periph_id(dev);
782
783 clock_enable(priv->dsi_clk);
784 udelay(2);
785 reset_set_enable(priv->dsi_clk, 0);
786}
787
788static int tegra_dsi_bridge_probe(struct udevice *dev)
789{
790 struct tegra_dsi_priv *priv = dev_get_priv(dev);
791 struct mipi_dsi_device *device = &priv->device;
792 struct mipi_dsi_panel_plat *mipi_plat;
793 int ret;
794
795 priv->dsi = (struct dsi_ctlr *)dev_read_addr_ptr(dev);
796 if (!priv->dsi) {
797 printf("%s: No display controller address\n", __func__);
798 return -EINVAL;
799 }
800
801 priv->video_fifo_depth = 480;
802 priv->host_fifo_depth = 64;
803
804 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
805 "avdd-dsi-csi-supply", &priv->avdd);
806 if (ret)
807 debug("%s: Cannot get avdd-dsi-csi-supply: error %d\n",
808 __func__, ret);
809
810 ret = uclass_get_device_by_phandle(UCLASS_PANEL, dev,
811 "panel", &priv->panel);
812 if (ret) {
813 printf("%s: Cannot get panel: error %d\n", __func__, ret);
814 return log_ret(ret);
815 }
816
817 panel_get_display_timing(priv->panel, &priv->timing);
818
819 mipi_plat = dev_get_plat(priv->panel);
820 mipi_plat->device = device;
821
822 priv->host.dev = (struct device *)dev;
823 priv->host.ops = &tegra_dsi_bridge_host_ops;
824
825 device->host = &priv->host;
826 device->lanes = mipi_plat->lanes;
827 device->format = mipi_plat->format;
828 device->mode_flags = mipi_plat->mode_flags;
829
830 tegra_dsi_get_format(device->format, &priv->format);
831
Svyatoslav Ryheldaab7d92023-10-03 09:25:34 +0300832 ret = regulator_set_enable_if_allowed(priv->avdd, true);
833 if (ret && ret != -ENOSYS)
834 return ret;
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +0300835
836 tegra_dsi_init_clocks(dev);
837
838 return 0;
839}
840
841static const struct panel_ops tegra_dsi_bridge_ops = {
842 .enable_backlight = tegra_dsi_encoder_enable,
843 .set_backlight = tegra_dsi_bridge_set_panel,
844 .get_display_timing = tegra_dsi_panel_timings,
845};
846
847static const struct udevice_id tegra_dsi_bridge_ids[] = {
848 { .compatible = "nvidia,tegra30-dsi" },
849 { }
850};
851
852U_BOOT_DRIVER(tegra_dsi) = {
853 .name = "tegra_dsi",
854 .id = UCLASS_PANEL,
855 .of_match = tegra_dsi_bridge_ids,
856 .ops = &tegra_dsi_bridge_ops,
857 .probe = tegra_dsi_bridge_probe,
858 .plat_auto = sizeof(struct tegra_dc_plat),
859 .priv_auto = sizeof(struct tegra_dsi_priv),
860};