2 * Copyright (C) 2013 NVIDIA Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
10 #include <linux/debugfs.h>
11 #include <linux/host1x.h>
12 #include <linux/module.h>
14 #include <linux/of_platform.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/reset.h>
19 #include <linux/regulator/consumer.h>
21 #include <drm/drm_atomic_helper.h>
22 #include <drm/drm_mipi_dsi.h>
23 #include <drm/drm_panel.h>
25 #include <video/mipi_display.h>
33 struct tegra_dsi_state
{
34 struct drm_connector_state base
;
36 struct mipi_dphy_timing timing
;
39 unsigned int vrefresh
;
44 enum tegra_dsi_format format
;
49 static inline struct tegra_dsi_state
*
50 to_dsi_state(struct drm_connector_state
*state
)
52 return container_of(state
, struct tegra_dsi_state
, base
);
56 struct host1x_client client
;
57 struct tegra_output output
;
62 struct reset_control
*rst
;
63 struct clk
*clk_parent
;
67 struct drm_info_list
*debugfs_files
;
68 struct drm_minor
*minor
;
69 struct dentry
*debugfs
;
72 enum mipi_dsi_pixel_format format
;
75 struct tegra_mipi_device
*mipi
;
76 struct mipi_dsi_host host
;
78 struct regulator
*vdd
;
80 unsigned int video_fifo_depth
;
81 unsigned int host_fifo_depth
;
83 /* for ganged-mode support */
84 struct tegra_dsi
*master
;
85 struct tegra_dsi
*slave
;
88 static inline struct tegra_dsi
*
89 host1x_client_to_dsi(struct host1x_client
*client
)
91 return container_of(client
, struct tegra_dsi
, client
);
94 static inline struct tegra_dsi
*host_to_tegra(struct mipi_dsi_host
*host
)
96 return container_of(host
, struct tegra_dsi
, host
);
99 static inline struct tegra_dsi
*to_dsi(struct tegra_output
*output
)
101 return container_of(output
, struct tegra_dsi
, output
);
104 static struct tegra_dsi_state
*tegra_dsi_get_state(struct tegra_dsi
*dsi
)
106 return to_dsi_state(dsi
->output
.connector
.state
);
109 static inline u32
tegra_dsi_readl(struct tegra_dsi
*dsi
, unsigned int offset
)
111 u32 value
= readl(dsi
->regs
+ (offset
<< 2));
113 trace_dsi_readl(dsi
->dev
, offset
, value
);
118 static inline void tegra_dsi_writel(struct tegra_dsi
*dsi
, u32 value
,
121 trace_dsi_writel(dsi
->dev
, offset
, value
);
122 writel(value
, dsi
->regs
+ (offset
<< 2));
125 static int tegra_dsi_show_regs(struct seq_file
*s
, void *data
)
127 struct drm_info_node
*node
= s
->private;
128 struct tegra_dsi
*dsi
= node
->info_ent
->data
;
129 struct drm_crtc
*crtc
= dsi
->output
.encoder
.crtc
;
130 struct drm_device
*drm
= node
->minor
->dev
;
133 drm_modeset_lock_all(drm
);
135 if (!crtc
|| !crtc
->state
->active
) {
140 #define DUMP_REG(name) \
141 seq_printf(s, "%-32s %#05x %08x\n", #name, name, \
142 tegra_dsi_readl(dsi, name))
144 DUMP_REG(DSI_INCR_SYNCPT
);
145 DUMP_REG(DSI_INCR_SYNCPT_CONTROL
);
146 DUMP_REG(DSI_INCR_SYNCPT_ERROR
);
148 DUMP_REG(DSI_RD_DATA
);
149 DUMP_REG(DSI_WR_DATA
);
150 DUMP_REG(DSI_POWER_CONTROL
);
151 DUMP_REG(DSI_INT_ENABLE
);
152 DUMP_REG(DSI_INT_STATUS
);
153 DUMP_REG(DSI_INT_MASK
);
154 DUMP_REG(DSI_HOST_CONTROL
);
155 DUMP_REG(DSI_CONTROL
);
156 DUMP_REG(DSI_SOL_DELAY
);
157 DUMP_REG(DSI_MAX_THRESHOLD
);
158 DUMP_REG(DSI_TRIGGER
);
159 DUMP_REG(DSI_TX_CRC
);
160 DUMP_REG(DSI_STATUS
);
162 DUMP_REG(DSI_INIT_SEQ_CONTROL
);
163 DUMP_REG(DSI_INIT_SEQ_DATA_0
);
164 DUMP_REG(DSI_INIT_SEQ_DATA_1
);
165 DUMP_REG(DSI_INIT_SEQ_DATA_2
);
166 DUMP_REG(DSI_INIT_SEQ_DATA_3
);
167 DUMP_REG(DSI_INIT_SEQ_DATA_4
);
168 DUMP_REG(DSI_INIT_SEQ_DATA_5
);
169 DUMP_REG(DSI_INIT_SEQ_DATA_6
);
170 DUMP_REG(DSI_INIT_SEQ_DATA_7
);
172 DUMP_REG(DSI_PKT_SEQ_0_LO
);
173 DUMP_REG(DSI_PKT_SEQ_0_HI
);
174 DUMP_REG(DSI_PKT_SEQ_1_LO
);
175 DUMP_REG(DSI_PKT_SEQ_1_HI
);
176 DUMP_REG(DSI_PKT_SEQ_2_LO
);
177 DUMP_REG(DSI_PKT_SEQ_2_HI
);
178 DUMP_REG(DSI_PKT_SEQ_3_LO
);
179 DUMP_REG(DSI_PKT_SEQ_3_HI
);
180 DUMP_REG(DSI_PKT_SEQ_4_LO
);
181 DUMP_REG(DSI_PKT_SEQ_4_HI
);
182 DUMP_REG(DSI_PKT_SEQ_5_LO
);
183 DUMP_REG(DSI_PKT_SEQ_5_HI
);
185 DUMP_REG(DSI_DCS_CMDS
);
187 DUMP_REG(DSI_PKT_LEN_0_1
);
188 DUMP_REG(DSI_PKT_LEN_2_3
);
189 DUMP_REG(DSI_PKT_LEN_4_5
);
190 DUMP_REG(DSI_PKT_LEN_6_7
);
192 DUMP_REG(DSI_PHY_TIMING_0
);
193 DUMP_REG(DSI_PHY_TIMING_1
);
194 DUMP_REG(DSI_PHY_TIMING_2
);
195 DUMP_REG(DSI_BTA_TIMING
);
197 DUMP_REG(DSI_TIMEOUT_0
);
198 DUMP_REG(DSI_TIMEOUT_1
);
199 DUMP_REG(DSI_TO_TALLY
);
201 DUMP_REG(DSI_PAD_CONTROL_0
);
202 DUMP_REG(DSI_PAD_CONTROL_CD
);
203 DUMP_REG(DSI_PAD_CD_STATUS
);
204 DUMP_REG(DSI_VIDEO_MODE_CONTROL
);
205 DUMP_REG(DSI_PAD_CONTROL_1
);
206 DUMP_REG(DSI_PAD_CONTROL_2
);
207 DUMP_REG(DSI_PAD_CONTROL_3
);
208 DUMP_REG(DSI_PAD_CONTROL_4
);
210 DUMP_REG(DSI_GANGED_MODE_CONTROL
);
211 DUMP_REG(DSI_GANGED_MODE_START
);
212 DUMP_REG(DSI_GANGED_MODE_SIZE
);
214 DUMP_REG(DSI_RAW_DATA_BYTE_COUNT
);
215 DUMP_REG(DSI_ULTRA_LOW_POWER_CONTROL
);
217 DUMP_REG(DSI_INIT_SEQ_DATA_8
);
218 DUMP_REG(DSI_INIT_SEQ_DATA_9
);
219 DUMP_REG(DSI_INIT_SEQ_DATA_10
);
220 DUMP_REG(DSI_INIT_SEQ_DATA_11
);
221 DUMP_REG(DSI_INIT_SEQ_DATA_12
);
222 DUMP_REG(DSI_INIT_SEQ_DATA_13
);
223 DUMP_REG(DSI_INIT_SEQ_DATA_14
);
224 DUMP_REG(DSI_INIT_SEQ_DATA_15
);
229 drm_modeset_unlock_all(drm
);
233 static struct drm_info_list debugfs_files
[] = {
234 { "regs", tegra_dsi_show_regs
, 0, NULL
},
237 static int tegra_dsi_debugfs_init(struct tegra_dsi
*dsi
,
238 struct drm_minor
*minor
)
240 const char *name
= dev_name(dsi
->dev
);
244 dsi
->debugfs
= debugfs_create_dir(name
, minor
->debugfs_root
);
248 dsi
->debugfs_files
= kmemdup(debugfs_files
, sizeof(debugfs_files
),
250 if (!dsi
->debugfs_files
) {
255 for (i
= 0; i
< ARRAY_SIZE(debugfs_files
); i
++)
256 dsi
->debugfs_files
[i
].data
= dsi
;
258 err
= drm_debugfs_create_files(dsi
->debugfs_files
,
259 ARRAY_SIZE(debugfs_files
),
260 dsi
->debugfs
, minor
);
269 kfree(dsi
->debugfs_files
);
270 dsi
->debugfs_files
= NULL
;
272 debugfs_remove(dsi
->debugfs
);
278 static void tegra_dsi_debugfs_exit(struct tegra_dsi
*dsi
)
280 drm_debugfs_remove_files(dsi
->debugfs_files
, ARRAY_SIZE(debugfs_files
),
284 kfree(dsi
->debugfs_files
);
285 dsi
->debugfs_files
= NULL
;
287 debugfs_remove(dsi
->debugfs
);
291 #define PKT_ID0(id) ((((id) & 0x3f) << 3) | (1 << 9))
292 #define PKT_LEN0(len) (((len) & 0x07) << 0)
293 #define PKT_ID1(id) ((((id) & 0x3f) << 13) | (1 << 19))
294 #define PKT_LEN1(len) (((len) & 0x07) << 10)
295 #define PKT_ID2(id) ((((id) & 0x3f) << 23) | (1 << 29))
296 #define PKT_LEN2(len) (((len) & 0x07) << 20)
298 #define PKT_LP (1 << 30)
299 #define NUM_PKT_SEQ 12
302 * non-burst mode with sync pulses
304 static const u32 pkt_seq_video_non_burst_sync_pulses
[NUM_PKT_SEQ
] = {
305 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START
) | PKT_LEN0(0) |
306 PKT_ID1(MIPI_DSI_BLANKING_PACKET
) | PKT_LEN1(1) |
307 PKT_ID2(MIPI_DSI_H_SYNC_END
) | PKT_LEN2(0) |
310 [ 2] = PKT_ID0(MIPI_DSI_V_SYNC_END
) | PKT_LEN0(0) |
311 PKT_ID1(MIPI_DSI_BLANKING_PACKET
) | PKT_LEN1(1) |
312 PKT_ID2(MIPI_DSI_H_SYNC_END
) | PKT_LEN2(0) |
315 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START
) | PKT_LEN0(0) |
316 PKT_ID1(MIPI_DSI_BLANKING_PACKET
) | PKT_LEN1(1) |
317 PKT_ID2(MIPI_DSI_H_SYNC_END
) | PKT_LEN2(0) |
320 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START
) | PKT_LEN0(0) |
321 PKT_ID1(MIPI_DSI_BLANKING_PACKET
) | PKT_LEN1(1) |
322 PKT_ID2(MIPI_DSI_H_SYNC_END
) | PKT_LEN2(0),
323 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET
) | PKT_LEN0(2) |
324 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24
) | PKT_LEN1(3) |
325 PKT_ID2(MIPI_DSI_BLANKING_PACKET
) | PKT_LEN2(4),
326 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START
) | PKT_LEN0(0) |
327 PKT_ID1(MIPI_DSI_BLANKING_PACKET
) | PKT_LEN1(1) |
328 PKT_ID2(MIPI_DSI_H_SYNC_END
) | PKT_LEN2(0) |
331 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START
) | PKT_LEN0(0) |
332 PKT_ID1(MIPI_DSI_BLANKING_PACKET
) | PKT_LEN1(1) |
333 PKT_ID2(MIPI_DSI_H_SYNC_END
) | PKT_LEN2(0),
334 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET
) | PKT_LEN0(2) |
335 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24
) | PKT_LEN1(3) |
336 PKT_ID2(MIPI_DSI_BLANKING_PACKET
) | PKT_LEN2(4),
340 * non-burst mode with sync events
342 static const u32 pkt_seq_video_non_burst_sync_events
[NUM_PKT_SEQ
] = {
343 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START
) | PKT_LEN0(0) |
344 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION
) | PKT_LEN1(7) |
347 [ 2] = PKT_ID0(MIPI_DSI_H_SYNC_START
) | PKT_LEN0(0) |
348 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION
) | PKT_LEN1(7) |
351 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START
) | PKT_LEN0(0) |
352 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION
) | PKT_LEN1(7) |
355 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START
) | PKT_LEN0(0) |
356 PKT_ID1(MIPI_DSI_BLANKING_PACKET
) | PKT_LEN1(2) |
357 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24
) | PKT_LEN2(3),
358 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET
) | PKT_LEN0(4),
359 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START
) | PKT_LEN0(0) |
360 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION
) | PKT_LEN1(7) |
363 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START
) | PKT_LEN0(0) |
364 PKT_ID1(MIPI_DSI_BLANKING_PACKET
) | PKT_LEN1(2) |
365 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24
) | PKT_LEN2(3),
366 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET
) | PKT_LEN0(4),
369 static const u32 pkt_seq_command_mode
[NUM_PKT_SEQ
] = {
376 [ 6] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE
) | PKT_LEN0(3) | PKT_LP
,
380 [10] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE
) | PKT_LEN0(5) | PKT_LP
,
384 static void tegra_dsi_set_phy_timing(struct tegra_dsi
*dsi
,
385 unsigned long period
,
386 const struct mipi_dphy_timing
*timing
)
390 value
= DSI_TIMING_FIELD(timing
->hsexit
, period
, 1) << 24 |
391 DSI_TIMING_FIELD(timing
->hstrail
, period
, 0) << 16 |
392 DSI_TIMING_FIELD(timing
->hszero
, period
, 3) << 8 |
393 DSI_TIMING_FIELD(timing
->hsprepare
, period
, 1);
394 tegra_dsi_writel(dsi
, value
, DSI_PHY_TIMING_0
);
396 value
= DSI_TIMING_FIELD(timing
->clktrail
, period
, 1) << 24 |
397 DSI_TIMING_FIELD(timing
->clkpost
, period
, 1) << 16 |
398 DSI_TIMING_FIELD(timing
->clkzero
, period
, 1) << 8 |
399 DSI_TIMING_FIELD(timing
->lpx
, period
, 1);
400 tegra_dsi_writel(dsi
, value
, DSI_PHY_TIMING_1
);
402 value
= DSI_TIMING_FIELD(timing
->clkprepare
, period
, 1) << 16 |
403 DSI_TIMING_FIELD(timing
->clkpre
, period
, 1) << 8 |
404 DSI_TIMING_FIELD(0xff * period
, period
, 0) << 0;
405 tegra_dsi_writel(dsi
, value
, DSI_PHY_TIMING_2
);
407 value
= DSI_TIMING_FIELD(timing
->taget
, period
, 1) << 16 |
408 DSI_TIMING_FIELD(timing
->tasure
, period
, 1) << 8 |
409 DSI_TIMING_FIELD(timing
->tago
, period
, 1);
410 tegra_dsi_writel(dsi
, value
, DSI_BTA_TIMING
);
413 tegra_dsi_set_phy_timing(dsi
->slave
, period
, timing
);
416 static int tegra_dsi_get_muldiv(enum mipi_dsi_pixel_format format
,
417 unsigned int *mulp
, unsigned int *divp
)
420 case MIPI_DSI_FMT_RGB666_PACKED
:
421 case MIPI_DSI_FMT_RGB888
:
426 case MIPI_DSI_FMT_RGB565
:
431 case MIPI_DSI_FMT_RGB666
:
443 static int tegra_dsi_get_format(enum mipi_dsi_pixel_format format
,
444 enum tegra_dsi_format
*fmt
)
447 case MIPI_DSI_FMT_RGB888
:
448 *fmt
= TEGRA_DSI_FORMAT_24P
;
451 case MIPI_DSI_FMT_RGB666
:
452 *fmt
= TEGRA_DSI_FORMAT_18NP
;
455 case MIPI_DSI_FMT_RGB666_PACKED
:
456 *fmt
= TEGRA_DSI_FORMAT_18P
;
459 case MIPI_DSI_FMT_RGB565
:
460 *fmt
= TEGRA_DSI_FORMAT_16P
;
470 static void tegra_dsi_ganged_enable(struct tegra_dsi
*dsi
, unsigned int start
,
475 tegra_dsi_writel(dsi
, start
, DSI_GANGED_MODE_START
);
476 tegra_dsi_writel(dsi
, size
<< 16 | size
, DSI_GANGED_MODE_SIZE
);
478 value
= DSI_GANGED_MODE_CONTROL_ENABLE
;
479 tegra_dsi_writel(dsi
, value
, DSI_GANGED_MODE_CONTROL
);
482 static void tegra_dsi_enable(struct tegra_dsi
*dsi
)
486 value
= tegra_dsi_readl(dsi
, DSI_POWER_CONTROL
);
487 value
|= DSI_POWER_CONTROL_ENABLE
;
488 tegra_dsi_writel(dsi
, value
, DSI_POWER_CONTROL
);
491 tegra_dsi_enable(dsi
->slave
);
494 static unsigned int tegra_dsi_get_lanes(struct tegra_dsi
*dsi
)
497 return dsi
->master
->lanes
+ dsi
->lanes
;
500 return dsi
->lanes
+ dsi
->slave
->lanes
;
505 static void tegra_dsi_configure(struct tegra_dsi
*dsi
, unsigned int pipe
,
506 const struct drm_display_mode
*mode
)
508 unsigned int hact
, hsw
, hbp
, hfp
, i
, mul
, div
;
509 struct tegra_dsi_state
*state
;
513 /* XXX: pass in state into this function? */
515 state
= tegra_dsi_get_state(dsi
->master
);
517 state
= tegra_dsi_get_state(dsi
);
522 if (dsi
->flags
& MIPI_DSI_MODE_VIDEO_SYNC_PULSE
) {
523 DRM_DEBUG_KMS("Non-burst video mode with sync pulses\n");
524 pkt_seq
= pkt_seq_video_non_burst_sync_pulses
;
525 } else if (dsi
->flags
& MIPI_DSI_MODE_VIDEO
) {
526 DRM_DEBUG_KMS("Non-burst video mode with sync events\n");
527 pkt_seq
= pkt_seq_video_non_burst_sync_events
;
529 DRM_DEBUG_KMS("Command mode\n");
530 pkt_seq
= pkt_seq_command_mode
;
533 value
= DSI_CONTROL_CHANNEL(0) |
534 DSI_CONTROL_FORMAT(state
->format
) |
535 DSI_CONTROL_LANES(dsi
->lanes
- 1) |
536 DSI_CONTROL_SOURCE(pipe
);
537 tegra_dsi_writel(dsi
, value
, DSI_CONTROL
);
539 tegra_dsi_writel(dsi
, dsi
->video_fifo_depth
, DSI_MAX_THRESHOLD
);
541 value
= DSI_HOST_CONTROL_HS
;
542 tegra_dsi_writel(dsi
, value
, DSI_HOST_CONTROL
);
544 value
= tegra_dsi_readl(dsi
, DSI_CONTROL
);
546 if (dsi
->flags
& MIPI_DSI_CLOCK_NON_CONTINUOUS
)
547 value
|= DSI_CONTROL_HS_CLK_CTRL
;
549 value
&= ~DSI_CONTROL_TX_TRIG(3);
551 /* enable DCS commands for command mode */
552 if (dsi
->flags
& MIPI_DSI_MODE_VIDEO
)
553 value
&= ~DSI_CONTROL_DCS_ENABLE
;
555 value
|= DSI_CONTROL_DCS_ENABLE
;
557 value
|= DSI_CONTROL_VIDEO_ENABLE
;
558 value
&= ~DSI_CONTROL_HOST_ENABLE
;
559 tegra_dsi_writel(dsi
, value
, DSI_CONTROL
);
561 for (i
= 0; i
< NUM_PKT_SEQ
; i
++)
562 tegra_dsi_writel(dsi
, pkt_seq
[i
], DSI_PKT_SEQ_0_LO
+ i
);
564 if (dsi
->flags
& MIPI_DSI_MODE_VIDEO
) {
565 /* horizontal active pixels */
566 hact
= mode
->hdisplay
* mul
/ div
;
568 /* horizontal sync width */
569 hsw
= (mode
->hsync_end
- mode
->hsync_start
) * mul
/ div
;
571 /* horizontal back porch */
572 hbp
= (mode
->htotal
- mode
->hsync_end
) * mul
/ div
;
574 if ((dsi
->flags
& MIPI_DSI_MODE_VIDEO_SYNC_PULSE
) == 0)
577 /* horizontal front porch */
578 hfp
= (mode
->hsync_start
- mode
->hdisplay
) * mul
/ div
;
580 /* subtract packet overhead */
585 tegra_dsi_writel(dsi
, hsw
<< 16 | 0, DSI_PKT_LEN_0_1
);
586 tegra_dsi_writel(dsi
, hact
<< 16 | hbp
, DSI_PKT_LEN_2_3
);
587 tegra_dsi_writel(dsi
, hfp
, DSI_PKT_LEN_4_5
);
588 tegra_dsi_writel(dsi
, 0x0f0f << 16, DSI_PKT_LEN_6_7
);
590 /* set SOL delay (for non-burst mode only) */
591 tegra_dsi_writel(dsi
, 8 * mul
/ div
, DSI_SOL_DELAY
);
593 /* TODO: implement ganged mode */
597 if (dsi
->master
|| dsi
->slave
) {
599 * For ganged mode, assume symmetric left-right mode.
601 bytes
= 1 + (mode
->hdisplay
/ 2) * mul
/ div
;
603 /* 1 byte (DCS command) + pixel data */
604 bytes
= 1 + mode
->hdisplay
* mul
/ div
;
607 tegra_dsi_writel(dsi
, 0, DSI_PKT_LEN_0_1
);
608 tegra_dsi_writel(dsi
, bytes
<< 16, DSI_PKT_LEN_2_3
);
609 tegra_dsi_writel(dsi
, bytes
<< 16, DSI_PKT_LEN_4_5
);
610 tegra_dsi_writel(dsi
, 0, DSI_PKT_LEN_6_7
);
612 value
= MIPI_DCS_WRITE_MEMORY_START
<< 8 |
613 MIPI_DCS_WRITE_MEMORY_CONTINUE
;
614 tegra_dsi_writel(dsi
, value
, DSI_DCS_CMDS
);
617 if (dsi
->master
|| dsi
->slave
) {
618 unsigned long delay
, bclk
, bclk_ganged
;
619 unsigned int lanes
= state
->lanes
;
621 /* SOL to valid, valid to FIFO and FIFO write delay */
623 delay
= DIV_ROUND_UP(delay
* mul
, div
* lanes
);
624 /* FIFO read delay */
627 bclk
= DIV_ROUND_UP(mode
->htotal
* mul
, div
* lanes
);
628 bclk_ganged
= DIV_ROUND_UP(bclk
* lanes
/ 2, lanes
);
629 value
= bclk
- bclk_ganged
+ delay
+ 20;
631 /* TODO: revisit for non-ganged mode */
632 value
= 8 * mul
/ div
;
635 tegra_dsi_writel(dsi
, value
, DSI_SOL_DELAY
);
639 tegra_dsi_configure(dsi
->slave
, pipe
, mode
);
642 * TODO: Support modes other than symmetrical left-right
645 tegra_dsi_ganged_enable(dsi
, 0, mode
->hdisplay
/ 2);
646 tegra_dsi_ganged_enable(dsi
->slave
, mode
->hdisplay
/ 2,
651 static int tegra_dsi_wait_idle(struct tegra_dsi
*dsi
, unsigned long timeout
)
655 timeout
= jiffies
+ msecs_to_jiffies(timeout
);
657 while (time_before(jiffies
, timeout
)) {
658 value
= tegra_dsi_readl(dsi
, DSI_STATUS
);
659 if (value
& DSI_STATUS_IDLE
)
662 usleep_range(1000, 2000);
668 static void tegra_dsi_video_disable(struct tegra_dsi
*dsi
)
672 value
= tegra_dsi_readl(dsi
, DSI_CONTROL
);
673 value
&= ~DSI_CONTROL_VIDEO_ENABLE
;
674 tegra_dsi_writel(dsi
, value
, DSI_CONTROL
);
677 tegra_dsi_video_disable(dsi
->slave
);
680 static void tegra_dsi_ganged_disable(struct tegra_dsi
*dsi
)
682 tegra_dsi_writel(dsi
, 0, DSI_GANGED_MODE_START
);
683 tegra_dsi_writel(dsi
, 0, DSI_GANGED_MODE_SIZE
);
684 tegra_dsi_writel(dsi
, 0, DSI_GANGED_MODE_CONTROL
);
687 static int tegra_dsi_pad_enable(struct tegra_dsi
*dsi
)
691 value
= DSI_PAD_CONTROL_VS1_PULLDN(0) | DSI_PAD_CONTROL_VS1_PDIO(0);
692 tegra_dsi_writel(dsi
, value
, DSI_PAD_CONTROL_0
);
697 static int tegra_dsi_pad_calibrate(struct tegra_dsi
*dsi
)
702 * XXX Is this still needed? The module reset is deasserted right
703 * before this function is called.
705 tegra_dsi_writel(dsi
, 0, DSI_PAD_CONTROL_0
);
706 tegra_dsi_writel(dsi
, 0, DSI_PAD_CONTROL_1
);
707 tegra_dsi_writel(dsi
, 0, DSI_PAD_CONTROL_2
);
708 tegra_dsi_writel(dsi
, 0, DSI_PAD_CONTROL_3
);
709 tegra_dsi_writel(dsi
, 0, DSI_PAD_CONTROL_4
);
711 /* start calibration */
712 tegra_dsi_pad_enable(dsi
);
714 value
= DSI_PAD_SLEW_UP(0x7) | DSI_PAD_SLEW_DN(0x7) |
715 DSI_PAD_LP_UP(0x1) | DSI_PAD_LP_DN(0x1) |
716 DSI_PAD_OUT_CLK(0x0);
717 tegra_dsi_writel(dsi
, value
, DSI_PAD_CONTROL_2
);
719 value
= DSI_PAD_PREEMP_PD_CLK(0x3) | DSI_PAD_PREEMP_PU_CLK(0x3) |
720 DSI_PAD_PREEMP_PD(0x03) | DSI_PAD_PREEMP_PU(0x3);
721 tegra_dsi_writel(dsi
, value
, DSI_PAD_CONTROL_3
);
723 return tegra_mipi_calibrate(dsi
->mipi
);
726 static void tegra_dsi_set_timeout(struct tegra_dsi
*dsi
, unsigned long bclk
,
727 unsigned int vrefresh
)
729 unsigned int timeout
;
732 /* one frame high-speed transmission timeout */
733 timeout
= (bclk
/ vrefresh
) / 512;
734 value
= DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout
);
735 tegra_dsi_writel(dsi
, value
, DSI_TIMEOUT_0
);
737 /* 2 ms peripheral timeout for panel */
738 timeout
= 2 * bclk
/ 512 * 1000;
739 value
= DSI_TIMEOUT_PR(timeout
) | DSI_TIMEOUT_TA(0x2000);
740 tegra_dsi_writel(dsi
, value
, DSI_TIMEOUT_1
);
742 value
= DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
743 tegra_dsi_writel(dsi
, value
, DSI_TO_TALLY
);
746 tegra_dsi_set_timeout(dsi
->slave
, bclk
, vrefresh
);
749 static void tegra_dsi_disable(struct tegra_dsi
*dsi
)
754 tegra_dsi_ganged_disable(dsi
->slave
);
755 tegra_dsi_ganged_disable(dsi
);
758 value
= tegra_dsi_readl(dsi
, DSI_POWER_CONTROL
);
759 value
&= ~DSI_POWER_CONTROL_ENABLE
;
760 tegra_dsi_writel(dsi
, value
, DSI_POWER_CONTROL
);
763 tegra_dsi_disable(dsi
->slave
);
765 usleep_range(5000, 10000);
768 static void tegra_dsi_soft_reset(struct tegra_dsi
*dsi
)
772 value
= tegra_dsi_readl(dsi
, DSI_POWER_CONTROL
);
773 value
&= ~DSI_POWER_CONTROL_ENABLE
;
774 tegra_dsi_writel(dsi
, value
, DSI_POWER_CONTROL
);
776 usleep_range(300, 1000);
778 value
= tegra_dsi_readl(dsi
, DSI_POWER_CONTROL
);
779 value
|= DSI_POWER_CONTROL_ENABLE
;
780 tegra_dsi_writel(dsi
, value
, DSI_POWER_CONTROL
);
782 usleep_range(300, 1000);
784 value
= tegra_dsi_readl(dsi
, DSI_TRIGGER
);
786 tegra_dsi_writel(dsi
, 0, DSI_TRIGGER
);
789 tegra_dsi_soft_reset(dsi
->slave
);
792 static void tegra_dsi_connector_reset(struct drm_connector
*connector
)
794 struct tegra_dsi_state
*state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
799 if (connector
->state
) {
800 __drm_atomic_helper_connector_destroy_state(connector
->state
);
801 kfree(connector
->state
);
804 __drm_atomic_helper_connector_reset(connector
, &state
->base
);
807 static struct drm_connector_state
*
808 tegra_dsi_connector_duplicate_state(struct drm_connector
*connector
)
810 struct tegra_dsi_state
*state
= to_dsi_state(connector
->state
);
811 struct tegra_dsi_state
*copy
;
813 copy
= kmemdup(state
, sizeof(*state
), GFP_KERNEL
);
817 __drm_atomic_helper_connector_duplicate_state(connector
,
823 static const struct drm_connector_funcs tegra_dsi_connector_funcs
= {
824 .reset
= tegra_dsi_connector_reset
,
825 .detect
= tegra_output_connector_detect
,
826 .fill_modes
= drm_helper_probe_single_connector_modes
,
827 .destroy
= tegra_output_connector_destroy
,
828 .atomic_duplicate_state
= tegra_dsi_connector_duplicate_state
,
829 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
832 static enum drm_mode_status
833 tegra_dsi_connector_mode_valid(struct drm_connector
*connector
,
834 struct drm_display_mode
*mode
)
839 static const struct drm_connector_helper_funcs tegra_dsi_connector_helper_funcs
= {
840 .get_modes
= tegra_output_connector_get_modes
,
841 .mode_valid
= tegra_dsi_connector_mode_valid
,
844 static const struct drm_encoder_funcs tegra_dsi_encoder_funcs
= {
845 .destroy
= tegra_output_encoder_destroy
,
848 static void tegra_dsi_unprepare(struct tegra_dsi
*dsi
)
853 tegra_dsi_unprepare(dsi
->slave
);
855 err
= tegra_mipi_disable(dsi
->mipi
);
857 dev_err(dsi
->dev
, "failed to disable MIPI calibration: %d\n",
860 pm_runtime_put(dsi
->dev
);
863 static void tegra_dsi_encoder_disable(struct drm_encoder
*encoder
)
865 struct tegra_output
*output
= encoder_to_output(encoder
);
866 struct tegra_dc
*dc
= to_tegra_dc(encoder
->crtc
);
867 struct tegra_dsi
*dsi
= to_dsi(output
);
872 drm_panel_disable(output
->panel
);
874 tegra_dsi_video_disable(dsi
);
877 * The following accesses registers of the display controller, so make
878 * sure it's only executed when the output is attached to one.
881 value
= tegra_dc_readl(dc
, DC_DISP_DISP_WIN_OPTIONS
);
882 value
&= ~DSI_ENABLE
;
883 tegra_dc_writel(dc
, value
, DC_DISP_DISP_WIN_OPTIONS
);
888 err
= tegra_dsi_wait_idle(dsi
, 100);
890 dev_dbg(dsi
->dev
, "failed to idle DSI: %d\n", err
);
892 tegra_dsi_soft_reset(dsi
);
895 drm_panel_unprepare(output
->panel
);
897 tegra_dsi_disable(dsi
);
899 tegra_dsi_unprepare(dsi
);
902 static void tegra_dsi_prepare(struct tegra_dsi
*dsi
)
906 pm_runtime_get_sync(dsi
->dev
);
908 err
= tegra_mipi_enable(dsi
->mipi
);
910 dev_err(dsi
->dev
, "failed to enable MIPI calibration: %d\n",
913 err
= tegra_dsi_pad_calibrate(dsi
);
915 dev_err(dsi
->dev
, "MIPI calibration failed: %d\n", err
);
918 tegra_dsi_prepare(dsi
->slave
);
921 static void tegra_dsi_encoder_enable(struct drm_encoder
*encoder
)
923 struct drm_display_mode
*mode
= &encoder
->crtc
->state
->adjusted_mode
;
924 struct tegra_output
*output
= encoder_to_output(encoder
);
925 struct tegra_dc
*dc
= to_tegra_dc(encoder
->crtc
);
926 struct tegra_dsi
*dsi
= to_dsi(output
);
927 struct tegra_dsi_state
*state
;
930 tegra_dsi_prepare(dsi
);
932 state
= tegra_dsi_get_state(dsi
);
934 tegra_dsi_set_timeout(dsi
, state
->bclk
, state
->vrefresh
);
937 * The D-PHY timing fields are expressed in byte-clock cycles, so
938 * multiply the period by 8.
940 tegra_dsi_set_phy_timing(dsi
, state
->period
* 8, &state
->timing
);
943 drm_panel_prepare(output
->panel
);
945 tegra_dsi_configure(dsi
, dc
->pipe
, mode
);
947 /* enable display controller */
948 value
= tegra_dc_readl(dc
, DC_DISP_DISP_WIN_OPTIONS
);
950 tegra_dc_writel(dc
, value
, DC_DISP_DISP_WIN_OPTIONS
);
954 /* enable DSI controller */
955 tegra_dsi_enable(dsi
);
958 drm_panel_enable(output
->panel
);
962 tegra_dsi_encoder_atomic_check(struct drm_encoder
*encoder
,
963 struct drm_crtc_state
*crtc_state
,
964 struct drm_connector_state
*conn_state
)
966 struct tegra_output
*output
= encoder_to_output(encoder
);
967 struct tegra_dsi_state
*state
= to_dsi_state(conn_state
);
968 struct tegra_dc
*dc
= to_tegra_dc(conn_state
->crtc
);
969 struct tegra_dsi
*dsi
= to_dsi(output
);
974 state
->pclk
= crtc_state
->mode
.clock
* 1000;
976 err
= tegra_dsi_get_muldiv(dsi
->format
, &state
->mul
, &state
->div
);
980 state
->lanes
= tegra_dsi_get_lanes(dsi
);
982 err
= tegra_dsi_get_format(dsi
->format
, &state
->format
);
986 state
->vrefresh
= drm_mode_vrefresh(&crtc_state
->mode
);
988 /* compute byte clock */
989 state
->bclk
= (state
->pclk
* state
->mul
) / (state
->div
* state
->lanes
);
991 DRM_DEBUG_KMS("mul: %u, div: %u, lanes: %u\n", state
->mul
, state
->div
,
993 DRM_DEBUG_KMS("format: %u, vrefresh: %u\n", state
->format
,
995 DRM_DEBUG_KMS("bclk: %lu\n", state
->bclk
);
998 * Compute bit clock and round up to the next MHz.
1000 plld
= DIV_ROUND_UP(state
->bclk
* 8, USEC_PER_SEC
) * USEC_PER_SEC
;
1001 state
->period
= DIV_ROUND_CLOSEST(NSEC_PER_SEC
, plld
);
1003 err
= mipi_dphy_timing_get_default(&state
->timing
, state
->period
);
1007 err
= mipi_dphy_timing_validate(&state
->timing
, state
->period
);
1009 dev_err(dsi
->dev
, "failed to validate D-PHY timing: %d\n", err
);
1014 * We divide the frequency by two here, but we make up for that by
1015 * setting the shift clock divider (further below) to half of the
1021 * Derive pixel clock from bit clock using the shift clock divider.
1022 * Note that this is only half of what we would expect, but we need
1023 * that to make up for the fact that we divided the bit clock by a
1024 * factor of two above.
1026 * It's not clear exactly why this is necessary, but the display is
1027 * not working properly otherwise. Perhaps the PLLs cannot generate
1028 * frequencies sufficiently high.
1030 scdiv
= ((8 * state
->mul
) / (state
->div
* state
->lanes
)) - 2;
1032 err
= tegra_dc_state_setup_clock(dc
, crtc_state
, dsi
->clk_parent
,
1035 dev_err(output
->dev
, "failed to setup CRTC state: %d\n", err
);
1042 static const struct drm_encoder_helper_funcs tegra_dsi_encoder_helper_funcs
= {
1043 .disable
= tegra_dsi_encoder_disable
,
1044 .enable
= tegra_dsi_encoder_enable
,
1045 .atomic_check
= tegra_dsi_encoder_atomic_check
,
1048 static int tegra_dsi_init(struct host1x_client
*client
)
1050 struct drm_device
*drm
= dev_get_drvdata(client
->parent
);
1051 struct tegra_dsi
*dsi
= host1x_client_to_dsi(client
);
1054 /* Gangsters must not register their own outputs. */
1056 dsi
->output
.dev
= client
->dev
;
1058 drm_connector_init(drm
, &dsi
->output
.connector
,
1059 &tegra_dsi_connector_funcs
,
1060 DRM_MODE_CONNECTOR_DSI
);
1061 drm_connector_helper_add(&dsi
->output
.connector
,
1062 &tegra_dsi_connector_helper_funcs
);
1063 dsi
->output
.connector
.dpms
= DRM_MODE_DPMS_OFF
;
1065 drm_encoder_init(drm
, &dsi
->output
.encoder
,
1066 &tegra_dsi_encoder_funcs
,
1067 DRM_MODE_ENCODER_DSI
, NULL
);
1068 drm_encoder_helper_add(&dsi
->output
.encoder
,
1069 &tegra_dsi_encoder_helper_funcs
);
1071 drm_mode_connector_attach_encoder(&dsi
->output
.connector
,
1072 &dsi
->output
.encoder
);
1073 drm_connector_register(&dsi
->output
.connector
);
1075 err
= tegra_output_init(drm
, &dsi
->output
);
1077 dev_err(dsi
->dev
, "failed to initialize output: %d\n",
1080 dsi
->output
.encoder
.possible_crtcs
= 0x3;
1083 if (IS_ENABLED(CONFIG_DEBUG_FS
)) {
1084 err
= tegra_dsi_debugfs_init(dsi
, drm
->primary
);
1086 dev_err(dsi
->dev
, "debugfs setup failed: %d\n", err
);
1092 static int tegra_dsi_exit(struct host1x_client
*client
)
1094 struct tegra_dsi
*dsi
= host1x_client_to_dsi(client
);
1096 tegra_output_exit(&dsi
->output
);
1098 if (IS_ENABLED(CONFIG_DEBUG_FS
))
1099 tegra_dsi_debugfs_exit(dsi
);
1101 regulator_disable(dsi
->vdd
);
1106 static const struct host1x_client_ops dsi_client_ops
= {
1107 .init
= tegra_dsi_init
,
1108 .exit
= tegra_dsi_exit
,
1111 static int tegra_dsi_setup_clocks(struct tegra_dsi
*dsi
)
1116 parent
= clk_get_parent(dsi
->clk
);
1120 err
= clk_set_parent(parent
, dsi
->clk_parent
);
1127 static const char * const error_report
[16] = {
1131 "Escape Mode Entry Command Error",
1132 "Low-Power Transmit Sync Error",
1133 "Peripheral Timeout Error",
1134 "False Control Error",
1135 "Contention Detected",
1136 "ECC Error, single-bit",
1137 "ECC Error, multi-bit",
1139 "DSI Data Type Not Recognized",
1140 "DSI VC ID Invalid",
1141 "Invalid Transmission Length",
1143 "DSI Protocol Violation",
1146 static ssize_t
tegra_dsi_read_response(struct tegra_dsi
*dsi
,
1147 const struct mipi_dsi_msg
*msg
,
1150 u8
*rx
= msg
->rx_buf
;
1151 unsigned int i
, j
, k
;
1156 /* read and parse packet header */
1157 value
= tegra_dsi_readl(dsi
, DSI_RD_DATA
);
1159 switch (value
& 0x3f) {
1160 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT
:
1161 errors
= (value
>> 8) & 0xffff;
1162 dev_dbg(dsi
->dev
, "Acknowledge and error report: %04x\n",
1164 for (i
= 0; i
< ARRAY_SIZE(error_report
); i
++)
1165 if (errors
& BIT(i
))
1166 dev_dbg(dsi
->dev
, " %2u: %s\n", i
,
1170 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE
:
1171 rx
[0] = (value
>> 8) & 0xff;
1175 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE
:
1176 rx
[0] = (value
>> 8) & 0xff;
1177 rx
[1] = (value
>> 16) & 0xff;
1181 case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE
:
1182 size
= ((value
>> 8) & 0xff00) | ((value
>> 8) & 0xff);
1185 case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE
:
1186 size
= ((value
>> 8) & 0xff00) | ((value
>> 8) & 0xff);
1190 dev_err(dsi
->dev
, "unhandled response type: %02x\n",
1195 size
= min(size
, msg
->rx_len
);
1197 if (msg
->rx_buf
&& size
> 0) {
1198 for (i
= 0, j
= 0; i
< count
- 1; i
++, j
+= 4) {
1199 u8
*rx
= msg
->rx_buf
+ j
;
1201 value
= tegra_dsi_readl(dsi
, DSI_RD_DATA
);
1203 for (k
= 0; k
< 4 && (j
+ k
) < msg
->rx_len
; k
++)
1204 rx
[j
+ k
] = (value
>> (k
<< 3)) & 0xff;
1211 static int tegra_dsi_transmit(struct tegra_dsi
*dsi
, unsigned long timeout
)
1213 tegra_dsi_writel(dsi
, DSI_TRIGGER_HOST
, DSI_TRIGGER
);
1215 timeout
= jiffies
+ msecs_to_jiffies(timeout
);
1217 while (time_before(jiffies
, timeout
)) {
1218 u32 value
= tegra_dsi_readl(dsi
, DSI_TRIGGER
);
1219 if ((value
& DSI_TRIGGER_HOST
) == 0)
1222 usleep_range(1000, 2000);
1225 DRM_DEBUG_KMS("timeout waiting for transmission to complete\n");
1229 static int tegra_dsi_wait_for_response(struct tegra_dsi
*dsi
,
1230 unsigned long timeout
)
1232 timeout
= jiffies
+ msecs_to_jiffies(250);
1234 while (time_before(jiffies
, timeout
)) {
1235 u32 value
= tegra_dsi_readl(dsi
, DSI_STATUS
);
1236 u8 count
= value
& 0x1f;
1241 usleep_range(1000, 2000);
1244 DRM_DEBUG_KMS("peripheral returned no data\n");
1248 static void tegra_dsi_writesl(struct tegra_dsi
*dsi
, unsigned long offset
,
1249 const void *buffer
, size_t size
)
1251 const u8
*buf
= buffer
;
1255 for (j
= 0; j
< size
; j
+= 4) {
1258 for (i
= 0; i
< 4 && j
+ i
< size
; i
++)
1259 value
|= buf
[j
+ i
] << (i
<< 3);
1261 tegra_dsi_writel(dsi
, value
, DSI_WR_DATA
);
1265 static ssize_t
tegra_dsi_host_transfer(struct mipi_dsi_host
*host
,
1266 const struct mipi_dsi_msg
*msg
)
1268 struct tegra_dsi
*dsi
= host_to_tegra(host
);
1269 struct mipi_dsi_packet packet
;
1275 err
= mipi_dsi_create_packet(&packet
, msg
);
1279 header
= packet
.header
;
1281 /* maximum FIFO depth is 1920 words */
1282 if (packet
.size
> dsi
->video_fifo_depth
* 4)
1285 /* reset underflow/overflow flags */
1286 value
= tegra_dsi_readl(dsi
, DSI_STATUS
);
1287 if (value
& (DSI_STATUS_UNDERFLOW
| DSI_STATUS_OVERFLOW
)) {
1288 value
= DSI_HOST_CONTROL_FIFO_RESET
;
1289 tegra_dsi_writel(dsi
, value
, DSI_HOST_CONTROL
);
1290 usleep_range(10, 20);
1293 value
= tegra_dsi_readl(dsi
, DSI_POWER_CONTROL
);
1294 value
|= DSI_POWER_CONTROL_ENABLE
;
1295 tegra_dsi_writel(dsi
, value
, DSI_POWER_CONTROL
);
1297 usleep_range(5000, 10000);
1299 value
= DSI_HOST_CONTROL_CRC_RESET
| DSI_HOST_CONTROL_TX_TRIG_HOST
|
1300 DSI_HOST_CONTROL_CS
| DSI_HOST_CONTROL_ECC
;
1302 if ((msg
->flags
& MIPI_DSI_MSG_USE_LPM
) == 0)
1303 value
|= DSI_HOST_CONTROL_HS
;
1306 * The host FIFO has a maximum of 64 words, so larger transmissions
1307 * need to use the video FIFO.
1309 if (packet
.size
> dsi
->host_fifo_depth
* 4)
1310 value
|= DSI_HOST_CONTROL_FIFO_SEL
;
1312 tegra_dsi_writel(dsi
, value
, DSI_HOST_CONTROL
);
1315 * For reads and messages with explicitly requested ACK, generate a
1316 * BTA sequence after the transmission of the packet.
1318 if ((msg
->flags
& MIPI_DSI_MSG_REQ_ACK
) ||
1319 (msg
->rx_buf
&& msg
->rx_len
> 0)) {
1320 value
= tegra_dsi_readl(dsi
, DSI_HOST_CONTROL
);
1321 value
|= DSI_HOST_CONTROL_PKT_BTA
;
1322 tegra_dsi_writel(dsi
, value
, DSI_HOST_CONTROL
);
1325 value
= DSI_CONTROL_LANES(0) | DSI_CONTROL_HOST_ENABLE
;
1326 tegra_dsi_writel(dsi
, value
, DSI_CONTROL
);
1328 /* write packet header, ECC is generated by hardware */
1329 value
= header
[2] << 16 | header
[1] << 8 | header
[0];
1330 tegra_dsi_writel(dsi
, value
, DSI_WR_DATA
);
1332 /* write payload (if any) */
1333 if (packet
.payload_length
> 0)
1334 tegra_dsi_writesl(dsi
, DSI_WR_DATA
, packet
.payload
,
1335 packet
.payload_length
);
1337 err
= tegra_dsi_transmit(dsi
, 250);
1341 if ((msg
->flags
& MIPI_DSI_MSG_REQ_ACK
) ||
1342 (msg
->rx_buf
&& msg
->rx_len
> 0)) {
1343 err
= tegra_dsi_wait_for_response(dsi
, 250);
1349 value
= tegra_dsi_readl(dsi
, DSI_RD_DATA
);
1353 dev_dbg(dsi->dev, "ACK\n");
1359 dev_dbg(dsi->dev, "ESCAPE\n");
1364 dev_err(dsi
->dev
, "unknown status: %08x\n", value
);
1369 err
= tegra_dsi_read_response(dsi
, msg
, count
);
1372 "failed to parse response: %zd\n",
1376 * For read commands, return the number of
1377 * bytes returned by the peripheral.
1384 * For write commands, we have transmitted the 4-byte header
1385 * plus the variable-length payload.
1387 count
= 4 + packet
.payload_length
;
1393 static int tegra_dsi_ganged_setup(struct tegra_dsi
*dsi
)
1398 /* make sure both DSI controllers share the same PLL */
1399 parent
= clk_get_parent(dsi
->slave
->clk
);
1403 err
= clk_set_parent(parent
, dsi
->clk_parent
);
1410 static int tegra_dsi_host_attach(struct mipi_dsi_host
*host
,
1411 struct mipi_dsi_device
*device
)
1413 struct tegra_dsi
*dsi
= host_to_tegra(host
);
1415 dsi
->flags
= device
->mode_flags
;
1416 dsi
->format
= device
->format
;
1417 dsi
->lanes
= device
->lanes
;
1422 dev_dbg(dsi
->dev
, "attaching dual-channel device %s\n",
1423 dev_name(&device
->dev
));
1425 err
= tegra_dsi_ganged_setup(dsi
);
1427 dev_err(dsi
->dev
, "failed to set up ganged mode: %d\n",
1434 * Slaves don't have a panel associated with them, so they provide
1435 * merely the second channel.
1438 struct tegra_output
*output
= &dsi
->output
;
1440 output
->panel
= of_drm_find_panel(device
->dev
.of_node
);
1441 if (output
->panel
&& output
->connector
.dev
) {
1442 drm_panel_attach(output
->panel
, &output
->connector
);
1443 drm_helper_hpd_irq_event(output
->connector
.dev
);
1450 static int tegra_dsi_host_detach(struct mipi_dsi_host
*host
,
1451 struct mipi_dsi_device
*device
)
1453 struct tegra_dsi
*dsi
= host_to_tegra(host
);
1454 struct tegra_output
*output
= &dsi
->output
;
1456 if (output
->panel
&& &device
->dev
== output
->panel
->dev
) {
1457 output
->panel
= NULL
;
1459 if (output
->connector
.dev
)
1460 drm_helper_hpd_irq_event(output
->connector
.dev
);
1466 static const struct mipi_dsi_host_ops tegra_dsi_host_ops
= {
1467 .attach
= tegra_dsi_host_attach
,
1468 .detach
= tegra_dsi_host_detach
,
1469 .transfer
= tegra_dsi_host_transfer
,
1472 static int tegra_dsi_ganged_probe(struct tegra_dsi
*dsi
)
1474 struct device_node
*np
;
1476 np
= of_parse_phandle(dsi
->dev
->of_node
, "nvidia,ganged-mode", 0);
1478 struct platform_device
*gangster
= of_find_device_by_node(np
);
1480 dsi
->slave
= platform_get_drvdata(gangster
);
1484 return -EPROBE_DEFER
;
1486 dsi
->slave
->master
= dsi
;
1492 static int tegra_dsi_probe(struct platform_device
*pdev
)
1494 struct tegra_dsi
*dsi
;
1495 struct resource
*regs
;
1498 dsi
= devm_kzalloc(&pdev
->dev
, sizeof(*dsi
), GFP_KERNEL
);
1502 dsi
->output
.dev
= dsi
->dev
= &pdev
->dev
;
1503 dsi
->video_fifo_depth
= 1920;
1504 dsi
->host_fifo_depth
= 64;
1506 err
= tegra_dsi_ganged_probe(dsi
);
1510 err
= tegra_output_probe(&dsi
->output
);
1514 dsi
->output
.connector
.polled
= DRM_CONNECTOR_POLL_HPD
;
1517 * Assume these values by default. When a DSI peripheral driver
1518 * attaches to the DSI host, the parameters will be taken from
1519 * the attached device.
1521 dsi
->flags
= MIPI_DSI_MODE_VIDEO
;
1522 dsi
->format
= MIPI_DSI_FMT_RGB888
;
1525 if (!pdev
->dev
.pm_domain
) {
1526 dsi
->rst
= devm_reset_control_get(&pdev
->dev
, "dsi");
1527 if (IS_ERR(dsi
->rst
))
1528 return PTR_ERR(dsi
->rst
);
1531 dsi
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
1532 if (IS_ERR(dsi
->clk
)) {
1533 dev_err(&pdev
->dev
, "cannot get DSI clock\n");
1534 return PTR_ERR(dsi
->clk
);
1537 dsi
->clk_lp
= devm_clk_get(&pdev
->dev
, "lp");
1538 if (IS_ERR(dsi
->clk_lp
)) {
1539 dev_err(&pdev
->dev
, "cannot get low-power clock\n");
1540 return PTR_ERR(dsi
->clk_lp
);
1543 dsi
->clk_parent
= devm_clk_get(&pdev
->dev
, "parent");
1544 if (IS_ERR(dsi
->clk_parent
)) {
1545 dev_err(&pdev
->dev
, "cannot get parent clock\n");
1546 return PTR_ERR(dsi
->clk_parent
);
1549 dsi
->vdd
= devm_regulator_get(&pdev
->dev
, "avdd-dsi-csi");
1550 if (IS_ERR(dsi
->vdd
)) {
1551 dev_err(&pdev
->dev
, "cannot get VDD supply\n");
1552 return PTR_ERR(dsi
->vdd
);
1555 err
= tegra_dsi_setup_clocks(dsi
);
1557 dev_err(&pdev
->dev
, "cannot setup clocks\n");
1561 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1562 dsi
->regs
= devm_ioremap_resource(&pdev
->dev
, regs
);
1563 if (IS_ERR(dsi
->regs
))
1564 return PTR_ERR(dsi
->regs
);
1566 dsi
->mipi
= tegra_mipi_request(&pdev
->dev
);
1567 if (IS_ERR(dsi
->mipi
))
1568 return PTR_ERR(dsi
->mipi
);
1570 dsi
->host
.ops
= &tegra_dsi_host_ops
;
1571 dsi
->host
.dev
= &pdev
->dev
;
1573 err
= mipi_dsi_host_register(&dsi
->host
);
1575 dev_err(&pdev
->dev
, "failed to register DSI host: %d\n", err
);
1579 platform_set_drvdata(pdev
, dsi
);
1580 pm_runtime_enable(&pdev
->dev
);
1582 INIT_LIST_HEAD(&dsi
->client
.list
);
1583 dsi
->client
.ops
= &dsi_client_ops
;
1584 dsi
->client
.dev
= &pdev
->dev
;
1586 err
= host1x_client_register(&dsi
->client
);
1588 dev_err(&pdev
->dev
, "failed to register host1x client: %d\n",
1596 mipi_dsi_host_unregister(&dsi
->host
);
1598 tegra_mipi_free(dsi
->mipi
);
1602 static int tegra_dsi_remove(struct platform_device
*pdev
)
1604 struct tegra_dsi
*dsi
= platform_get_drvdata(pdev
);
1607 pm_runtime_disable(&pdev
->dev
);
1609 err
= host1x_client_unregister(&dsi
->client
);
1611 dev_err(&pdev
->dev
, "failed to unregister host1x client: %d\n",
1616 tegra_output_remove(&dsi
->output
);
1618 mipi_dsi_host_unregister(&dsi
->host
);
1619 tegra_mipi_free(dsi
->mipi
);
1625 static int tegra_dsi_suspend(struct device
*dev
)
1627 struct tegra_dsi
*dsi
= dev_get_drvdata(dev
);
1631 err
= reset_control_assert(dsi
->rst
);
1633 dev_err(dev
, "failed to assert reset: %d\n", err
);
1638 usleep_range(1000, 2000);
1640 clk_disable_unprepare(dsi
->clk_lp
);
1641 clk_disable_unprepare(dsi
->clk
);
1643 regulator_disable(dsi
->vdd
);
1648 static int tegra_dsi_resume(struct device
*dev
)
1650 struct tegra_dsi
*dsi
= dev_get_drvdata(dev
);
1653 err
= regulator_enable(dsi
->vdd
);
1655 dev_err(dsi
->dev
, "failed to enable VDD supply: %d\n", err
);
1659 err
= clk_prepare_enable(dsi
->clk
);
1661 dev_err(dev
, "cannot enable DSI clock: %d\n", err
);
1665 err
= clk_prepare_enable(dsi
->clk_lp
);
1667 dev_err(dev
, "cannot enable low-power clock: %d\n", err
);
1671 usleep_range(1000, 2000);
1674 err
= reset_control_deassert(dsi
->rst
);
1676 dev_err(dev
, "cannot assert reset: %d\n", err
);
1677 goto disable_clk_lp
;
1684 clk_disable_unprepare(dsi
->clk_lp
);
1686 clk_disable_unprepare(dsi
->clk
);
1688 regulator_disable(dsi
->vdd
);
1693 static const struct dev_pm_ops tegra_dsi_pm_ops
= {
1694 SET_RUNTIME_PM_OPS(tegra_dsi_suspend
, tegra_dsi_resume
, NULL
)
1697 static const struct of_device_id tegra_dsi_of_match
[] = {
1698 { .compatible
= "nvidia,tegra210-dsi", },
1699 { .compatible
= "nvidia,tegra132-dsi", },
1700 { .compatible
= "nvidia,tegra124-dsi", },
1701 { .compatible
= "nvidia,tegra114-dsi", },
1704 MODULE_DEVICE_TABLE(of
, tegra_dsi_of_match
);
1706 struct platform_driver tegra_dsi_driver
= {
1708 .name
= "tegra-dsi",
1709 .of_match_table
= tegra_dsi_of_match
,
1710 .pm
= &tegra_dsi_pm_ops
,
1712 .probe
= tegra_dsi_probe
,
1713 .remove
= tegra_dsi_remove
,