1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd
4 * Copyright (C) STMicroelectronics SA 2017
6 * Modified by Philippe Cornu <philippe.cornu@st.com>
7 * This generic Synopsys DesignWare MIPI DSI host driver is based on the
8 * Rockchip version from rockchip/dw-mipi-dsi.c with phy & bridge APIs.
11 #include <linux/clk.h>
12 #include <linux/component.h>
13 #include <linux/debugfs.h>
14 #include <linux/iopoll.h>
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/reset.h>
20 #include <video/mipi_display.h>
22 #include <drm/bridge/dw_mipi_dsi.h>
23 #include <drm/drm_atomic_helper.h>
24 #include <drm/drm_bridge.h>
25 #include <drm/drm_crtc.h>
26 #include <drm/drm_mipi_dsi.h>
27 #include <drm/drm_modes.h>
28 #include <drm/drm_of.h>
29 #include <drm/drm_print.h>
30 #include <drm/drm_probe_helper.h>
32 #define HWVER_131 0x31333100 /* IP version 1.31 */
34 #define DSI_VERSION 0x00
35 #define VERSION GENMASK(31, 8)
37 #define DSI_PWR_UP 0x04
39 #define POWERUP BIT(0)
41 #define DSI_CLKMGR_CFG 0x08
42 #define TO_CLK_DIVISION(div) (((div) & 0xff) << 8)
43 #define TX_ESC_CLK_DIVISION(div) ((div) & 0xff)
45 #define DSI_DPI_VCID 0x0c
46 #define DPI_VCID(vcid) ((vcid) & 0x3)
48 #define DSI_DPI_COLOR_CODING 0x10
49 #define LOOSELY18_EN BIT(8)
50 #define DPI_COLOR_CODING_16BIT_1 0x0
51 #define DPI_COLOR_CODING_16BIT_2 0x1
52 #define DPI_COLOR_CODING_16BIT_3 0x2
53 #define DPI_COLOR_CODING_18BIT_1 0x3
54 #define DPI_COLOR_CODING_18BIT_2 0x4
55 #define DPI_COLOR_CODING_24BIT 0x5
57 #define DSI_DPI_CFG_POL 0x14
58 #define COLORM_ACTIVE_LOW BIT(4)
59 #define SHUTD_ACTIVE_LOW BIT(3)
60 #define HSYNC_ACTIVE_LOW BIT(2)
61 #define VSYNC_ACTIVE_LOW BIT(1)
62 #define DATAEN_ACTIVE_LOW BIT(0)
64 #define DSI_DPI_LP_CMD_TIM 0x18
65 #define OUTVACT_LPCMD_TIME(p) (((p) & 0xff) << 16)
66 #define INVACT_LPCMD_TIME(p) ((p) & 0xff)
68 #define DSI_DBI_VCID 0x1c
69 #define DSI_DBI_CFG 0x20
70 #define DSI_DBI_PARTITIONING_EN 0x24
71 #define DSI_DBI_CMDSIZE 0x28
73 #define DSI_PCKHDL_CFG 0x2c
74 #define CRC_RX_EN BIT(4)
75 #define ECC_RX_EN BIT(3)
77 #define EOTP_RX_EN BIT(1)
78 #define EOTP_TX_EN BIT(0)
80 #define DSI_GEN_VCID 0x30
82 #define DSI_MODE_CFG 0x34
83 #define ENABLE_VIDEO_MODE 0
84 #define ENABLE_CMD_MODE BIT(0)
86 #define DSI_VID_MODE_CFG 0x38
87 #define ENABLE_LOW_POWER (0x3f << 8)
88 #define ENABLE_LOW_POWER_MASK (0x3f << 8)
89 #define VID_MODE_TYPE_NON_BURST_SYNC_PULSES 0x0
90 #define VID_MODE_TYPE_NON_BURST_SYNC_EVENTS 0x1
91 #define VID_MODE_TYPE_BURST 0x2
92 #define VID_MODE_TYPE_MASK 0x3
93 #define VID_MODE_VPG_ENABLE BIT(16)
94 #define VID_MODE_VPG_HORIZONTAL BIT(24)
96 #define DSI_VID_PKT_SIZE 0x3c
97 #define VID_PKT_SIZE(p) ((p) & 0x3fff)
99 #define DSI_VID_NUM_CHUNKS 0x40
100 #define VID_NUM_CHUNKS(c) ((c) & 0x1fff)
102 #define DSI_VID_NULL_SIZE 0x44
103 #define VID_NULL_SIZE(b) ((b) & 0x1fff)
105 #define DSI_VID_HSA_TIME 0x48
106 #define DSI_VID_HBP_TIME 0x4c
107 #define DSI_VID_HLINE_TIME 0x50
108 #define DSI_VID_VSA_LINES 0x54
109 #define DSI_VID_VBP_LINES 0x58
110 #define DSI_VID_VFP_LINES 0x5c
111 #define DSI_VID_VACTIVE_LINES 0x60
112 #define DSI_EDPI_CMD_SIZE 0x64
114 #define DSI_CMD_MODE_CFG 0x68
115 #define MAX_RD_PKT_SIZE_LP BIT(24)
116 #define DCS_LW_TX_LP BIT(19)
117 #define DCS_SR_0P_TX_LP BIT(18)
118 #define DCS_SW_1P_TX_LP BIT(17)
119 #define DCS_SW_0P_TX_LP BIT(16)
120 #define GEN_LW_TX_LP BIT(14)
121 #define GEN_SR_2P_TX_LP BIT(13)
122 #define GEN_SR_1P_TX_LP BIT(12)
123 #define GEN_SR_0P_TX_LP BIT(11)
124 #define GEN_SW_2P_TX_LP BIT(10)
125 #define GEN_SW_1P_TX_LP BIT(9)
126 #define GEN_SW_0P_TX_LP BIT(8)
127 #define ACK_RQST_EN BIT(1)
128 #define TEAR_FX_EN BIT(0)
130 #define CMD_MODE_ALL_LP (MAX_RD_PKT_SIZE_LP | \
143 #define DSI_GEN_HDR 0x6c
144 #define DSI_GEN_PLD_DATA 0x70
146 #define DSI_CMD_PKT_STATUS 0x74
147 #define GEN_RD_CMD_BUSY BIT(6)
148 #define GEN_PLD_R_FULL BIT(5)
149 #define GEN_PLD_R_EMPTY BIT(4)
150 #define GEN_PLD_W_FULL BIT(3)
151 #define GEN_PLD_W_EMPTY BIT(2)
152 #define GEN_CMD_FULL BIT(1)
153 #define GEN_CMD_EMPTY BIT(0)
155 #define DSI_TO_CNT_CFG 0x78
156 #define HSTX_TO_CNT(p) (((p) & 0xffff) << 16)
157 #define LPRX_TO_CNT(p) ((p) & 0xffff)
159 #define DSI_HS_RD_TO_CNT 0x7c
160 #define DSI_LP_RD_TO_CNT 0x80
161 #define DSI_HS_WR_TO_CNT 0x84
162 #define DSI_LP_WR_TO_CNT 0x88
163 #define DSI_BTA_TO_CNT 0x8c
165 #define DSI_LPCLK_CTRL 0x94
166 #define AUTO_CLKLANE_CTRL BIT(1)
167 #define PHY_TXREQUESTCLKHS BIT(0)
169 #define DSI_PHY_TMR_LPCLK_CFG 0x98
170 #define PHY_CLKHS2LP_TIME(lbcc) (((lbcc) & 0x3ff) << 16)
171 #define PHY_CLKLP2HS_TIME(lbcc) ((lbcc) & 0x3ff)
173 #define DSI_PHY_TMR_CFG 0x9c
174 #define PHY_HS2LP_TIME(lbcc) (((lbcc) & 0xff) << 24)
175 #define PHY_LP2HS_TIME(lbcc) (((lbcc) & 0xff) << 16)
176 #define MAX_RD_TIME(lbcc) ((lbcc) & 0x7fff)
177 #define PHY_HS2LP_TIME_V131(lbcc) (((lbcc) & 0x3ff) << 16)
178 #define PHY_LP2HS_TIME_V131(lbcc) ((lbcc) & 0x3ff)
180 #define DSI_PHY_RSTZ 0xa0
181 #define PHY_DISFORCEPLL 0
182 #define PHY_ENFORCEPLL BIT(3)
183 #define PHY_DISABLECLK 0
184 #define PHY_ENABLECLK BIT(2)
186 #define PHY_UNRSTZ BIT(1)
187 #define PHY_SHUTDOWNZ 0
188 #define PHY_UNSHUTDOWNZ BIT(0)
190 #define DSI_PHY_IF_CFG 0xa4
191 #define PHY_STOP_WAIT_TIME(cycle) (((cycle) & 0xff) << 8)
192 #define N_LANES(n) (((n) - 1) & 0x3)
194 #define DSI_PHY_ULPS_CTRL 0xa8
195 #define DSI_PHY_TX_TRIGGERS 0xac
197 #define DSI_PHY_STATUS 0xb0
198 #define PHY_STOP_STATE_CLK_LANE BIT(2)
199 #define PHY_LOCK BIT(0)
201 #define DSI_PHY_TST_CTRL0 0xb4
202 #define PHY_TESTCLK BIT(1)
203 #define PHY_UNTESTCLK 0
204 #define PHY_TESTCLR BIT(0)
205 #define PHY_UNTESTCLR 0
207 #define DSI_PHY_TST_CTRL1 0xb8
208 #define PHY_TESTEN BIT(16)
209 #define PHY_UNTESTEN 0
210 #define PHY_TESTDOUT(n) (((n) & 0xff) << 8)
211 #define PHY_TESTDIN(n) ((n) & 0xff)
213 #define DSI_INT_ST0 0xbc
214 #define DSI_INT_ST1 0xc0
215 #define DSI_INT_MSK0 0xc4
216 #define DSI_INT_MSK1 0xc8
218 #define DSI_PHY_TMR_RD_CFG 0xf4
219 #define MAX_RD_TIME_V131(lbcc) ((lbcc) & 0x7fff)
221 #define PHY_STATUS_TIMEOUT_US 10000
222 #define CMD_PKT_STATUS_TIMEOUT_US 20000
225 struct drm_bridge bridge
;
226 struct mipi_dsi_host dsi_host
;
227 struct drm_bridge
*panel_bridge
;
233 unsigned int lane_mbps
; /* per lane */
237 unsigned long mode_flags
;
239 #ifdef CONFIG_DEBUG_FS
240 struct dentry
*debugfs
;
244 #endif /* CONFIG_DEBUG_FS */
246 struct dw_mipi_dsi
*master
; /* dual-dsi master ptr */
247 struct dw_mipi_dsi
*slave
; /* dual-dsi slave ptr */
249 const struct dw_mipi_dsi_plat_data
*plat_data
;
253 * Check if either a link to a master or slave is present
255 static inline bool dw_mipi_is_dual_mode(struct dw_mipi_dsi
*dsi
)
257 return dsi
->slave
|| dsi
->master
;
261 * The controller should generate 2 frames before
262 * preparing the peripheral.
264 static void dw_mipi_dsi_wait_for_two_frames(const struct drm_display_mode
*mode
)
266 int refresh
, two_frames
;
268 refresh
= drm_mode_vrefresh(mode
);
269 two_frames
= DIV_ROUND_UP(MSEC_PER_SEC
, refresh
) * 2;
273 static inline struct dw_mipi_dsi
*host_to_dsi(struct mipi_dsi_host
*host
)
275 return container_of(host
, struct dw_mipi_dsi
, dsi_host
);
278 static inline struct dw_mipi_dsi
*bridge_to_dsi(struct drm_bridge
*bridge
)
280 return container_of(bridge
, struct dw_mipi_dsi
, bridge
);
283 static inline void dsi_write(struct dw_mipi_dsi
*dsi
, u32 reg
, u32 val
)
285 writel(val
, dsi
->base
+ reg
);
288 static inline u32
dsi_read(struct dw_mipi_dsi
*dsi
, u32 reg
)
290 return readl(dsi
->base
+ reg
);
293 static int dw_mipi_dsi_host_attach(struct mipi_dsi_host
*host
,
294 struct mipi_dsi_device
*device
)
296 struct dw_mipi_dsi
*dsi
= host_to_dsi(host
);
297 const struct dw_mipi_dsi_plat_data
*pdata
= dsi
->plat_data
;
298 struct drm_bridge
*bridge
;
299 struct drm_panel
*panel
;
302 if (device
->lanes
> dsi
->plat_data
->max_data_lanes
) {
303 dev_err(dsi
->dev
, "the number of data lanes(%u) is too many\n",
308 dsi
->lanes
= device
->lanes
;
309 dsi
->channel
= device
->channel
;
310 dsi
->format
= device
->format
;
311 dsi
->mode_flags
= device
->mode_flags
;
313 ret
= drm_of_find_panel_or_bridge(host
->dev
->of_node
, 1, 0,
319 bridge
= drm_panel_bridge_add_typed(panel
,
320 DRM_MODE_CONNECTOR_DSI
);
322 return PTR_ERR(bridge
);
325 dsi
->panel_bridge
= bridge
;
327 drm_bridge_add(&dsi
->bridge
);
329 if (pdata
->host_ops
&& pdata
->host_ops
->attach
) {
330 ret
= pdata
->host_ops
->attach(pdata
->priv_data
, device
);
338 static int dw_mipi_dsi_host_detach(struct mipi_dsi_host
*host
,
339 struct mipi_dsi_device
*device
)
341 struct dw_mipi_dsi
*dsi
= host_to_dsi(host
);
342 const struct dw_mipi_dsi_plat_data
*pdata
= dsi
->plat_data
;
345 if (pdata
->host_ops
&& pdata
->host_ops
->detach
) {
346 ret
= pdata
->host_ops
->detach(pdata
->priv_data
, device
);
351 drm_of_panel_bridge_remove(host
->dev
->of_node
, 1, 0);
353 drm_bridge_remove(&dsi
->bridge
);
358 static void dw_mipi_message_config(struct dw_mipi_dsi
*dsi
,
359 const struct mipi_dsi_msg
*msg
)
361 bool lpm
= msg
->flags
& MIPI_DSI_MSG_USE_LPM
;
364 if (msg
->flags
& MIPI_DSI_MSG_REQ_ACK
)
367 val
|= CMD_MODE_ALL_LP
;
369 dsi_write(dsi
, DSI_LPCLK_CTRL
, lpm
? 0 : PHY_TXREQUESTCLKHS
);
370 dsi_write(dsi
, DSI_CMD_MODE_CFG
, val
);
373 static int dw_mipi_dsi_gen_pkt_hdr_write(struct dw_mipi_dsi
*dsi
, u32 hdr_val
)
378 ret
= readl_poll_timeout(dsi
->base
+ DSI_CMD_PKT_STATUS
,
379 val
, !(val
& GEN_CMD_FULL
), 1000,
380 CMD_PKT_STATUS_TIMEOUT_US
);
382 dev_err(dsi
->dev
, "failed to get available command FIFO\n");
386 dsi_write(dsi
, DSI_GEN_HDR
, hdr_val
);
388 mask
= GEN_CMD_EMPTY
| GEN_PLD_W_EMPTY
;
389 ret
= readl_poll_timeout(dsi
->base
+ DSI_CMD_PKT_STATUS
,
390 val
, (val
& mask
) == mask
,
391 1000, CMD_PKT_STATUS_TIMEOUT_US
);
393 dev_err(dsi
->dev
, "failed to write command FIFO\n");
400 static int dw_mipi_dsi_write(struct dw_mipi_dsi
*dsi
,
401 const struct mipi_dsi_packet
*packet
)
403 const u8
*tx_buf
= packet
->payload
;
404 int len
= packet
->payload_length
, pld_data_bytes
= sizeof(u32
), ret
;
409 if (len
< pld_data_bytes
) {
411 memcpy(&word
, tx_buf
, len
);
412 dsi_write(dsi
, DSI_GEN_PLD_DATA
, le32_to_cpu(word
));
415 memcpy(&word
, tx_buf
, pld_data_bytes
);
416 dsi_write(dsi
, DSI_GEN_PLD_DATA
, le32_to_cpu(word
));
417 tx_buf
+= pld_data_bytes
;
418 len
-= pld_data_bytes
;
421 ret
= readl_poll_timeout(dsi
->base
+ DSI_CMD_PKT_STATUS
,
422 val
, !(val
& GEN_PLD_W_FULL
), 1000,
423 CMD_PKT_STATUS_TIMEOUT_US
);
426 "failed to get available write payload FIFO\n");
432 memcpy(&word
, packet
->header
, sizeof(packet
->header
));
433 return dw_mipi_dsi_gen_pkt_hdr_write(dsi
, le32_to_cpu(word
));
436 static int dw_mipi_dsi_read(struct dw_mipi_dsi
*dsi
,
437 const struct mipi_dsi_msg
*msg
)
439 int i
, j
, ret
, len
= msg
->rx_len
;
440 u8
*buf
= msg
->rx_buf
;
443 /* Wait end of the read operation */
444 ret
= readl_poll_timeout(dsi
->base
+ DSI_CMD_PKT_STATUS
,
445 val
, !(val
& GEN_RD_CMD_BUSY
),
446 1000, CMD_PKT_STATUS_TIMEOUT_US
);
448 dev_err(dsi
->dev
, "Timeout during read operation\n");
452 for (i
= 0; i
< len
; i
+= 4) {
453 /* Read fifo must not be empty before all bytes are read */
454 ret
= readl_poll_timeout(dsi
->base
+ DSI_CMD_PKT_STATUS
,
455 val
, !(val
& GEN_PLD_R_EMPTY
),
456 1000, CMD_PKT_STATUS_TIMEOUT_US
);
458 dev_err(dsi
->dev
, "Read payload FIFO is empty\n");
462 val
= dsi_read(dsi
, DSI_GEN_PLD_DATA
);
463 for (j
= 0; j
< 4 && j
+ i
< len
; j
++)
464 buf
[i
+ j
] = val
>> (8 * j
);
470 static ssize_t
dw_mipi_dsi_host_transfer(struct mipi_dsi_host
*host
,
471 const struct mipi_dsi_msg
*msg
)
473 struct dw_mipi_dsi
*dsi
= host_to_dsi(host
);
474 struct mipi_dsi_packet packet
;
477 ret
= mipi_dsi_create_packet(&packet
, msg
);
479 dev_err(dsi
->dev
, "failed to create packet: %d\n", ret
);
483 dw_mipi_message_config(dsi
, msg
);
485 dw_mipi_message_config(dsi
->slave
, msg
);
487 ret
= dw_mipi_dsi_write(dsi
, &packet
);
491 ret
= dw_mipi_dsi_write(dsi
->slave
, &packet
);
496 if (msg
->rx_buf
&& msg
->rx_len
) {
497 ret
= dw_mipi_dsi_read(dsi
, msg
);
500 nb_bytes
= msg
->rx_len
;
502 nb_bytes
= packet
.size
;
508 static const struct mipi_dsi_host_ops dw_mipi_dsi_host_ops
= {
509 .attach
= dw_mipi_dsi_host_attach
,
510 .detach
= dw_mipi_dsi_host_detach
,
511 .transfer
= dw_mipi_dsi_host_transfer
,
514 static void dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi
*dsi
)
519 * TODO dw drv improvements
520 * enabling low power is panel-dependent, we should use the
521 * panel configuration here...
523 val
= ENABLE_LOW_POWER
;
525 if (dsi
->mode_flags
& MIPI_DSI_MODE_VIDEO_BURST
)
526 val
|= VID_MODE_TYPE_BURST
;
527 else if (dsi
->mode_flags
& MIPI_DSI_MODE_VIDEO_SYNC_PULSE
)
528 val
|= VID_MODE_TYPE_NON_BURST_SYNC_PULSES
;
530 val
|= VID_MODE_TYPE_NON_BURST_SYNC_EVENTS
;
532 #ifdef CONFIG_DEBUG_FS
534 val
|= VID_MODE_VPG_ENABLE
;
535 val
|= dsi
->vpg_horizontal
? VID_MODE_VPG_HORIZONTAL
: 0;
537 #endif /* CONFIG_DEBUG_FS */
539 dsi_write(dsi
, DSI_VID_MODE_CFG
, val
);
542 static void dw_mipi_dsi_set_mode(struct dw_mipi_dsi
*dsi
,
543 unsigned long mode_flags
)
545 dsi_write(dsi
, DSI_PWR_UP
, RESET
);
547 if (mode_flags
& MIPI_DSI_MODE_VIDEO
) {
548 dsi_write(dsi
, DSI_MODE_CFG
, ENABLE_VIDEO_MODE
);
549 dw_mipi_dsi_video_mode_config(dsi
);
550 dsi_write(dsi
, DSI_LPCLK_CTRL
, PHY_TXREQUESTCLKHS
);
552 dsi_write(dsi
, DSI_MODE_CFG
, ENABLE_CMD_MODE
);
555 dsi_write(dsi
, DSI_PWR_UP
, POWERUP
);
558 static void dw_mipi_dsi_disable(struct dw_mipi_dsi
*dsi
)
560 dsi_write(dsi
, DSI_PWR_UP
, RESET
);
561 dsi_write(dsi
, DSI_PHY_RSTZ
, PHY_RSTZ
);
564 static void dw_mipi_dsi_init(struct dw_mipi_dsi
*dsi
)
567 * The maximum permitted escape clock is 20MHz and it is derived from
568 * lanebyteclk, which is running at "lane_mbps / 8". Thus we want:
570 * (lane_mbps >> 3) / esc_clk_division < 20
572 * (lane_mbps >> 3) / 20 > esc_clk_division
574 u32 esc_clk_division
= (dsi
->lane_mbps
>> 3) / 20 + 1;
576 dsi_write(dsi
, DSI_PWR_UP
, RESET
);
579 * TODO dw drv improvements
580 * timeout clock division should be computed with the
581 * high speed transmission counter timeout and byte lane...
583 dsi_write(dsi
, DSI_CLKMGR_CFG
, TO_CLK_DIVISION(10) |
584 TX_ESC_CLK_DIVISION(esc_clk_division
));
587 static void dw_mipi_dsi_dpi_config(struct dw_mipi_dsi
*dsi
,
588 const struct drm_display_mode
*mode
)
590 u32 val
= 0, color
= 0;
592 switch (dsi
->format
) {
593 case MIPI_DSI_FMT_RGB888
:
594 color
= DPI_COLOR_CODING_24BIT
;
596 case MIPI_DSI_FMT_RGB666
:
597 color
= DPI_COLOR_CODING_18BIT_2
| LOOSELY18_EN
;
599 case MIPI_DSI_FMT_RGB666_PACKED
:
600 color
= DPI_COLOR_CODING_18BIT_1
;
602 case MIPI_DSI_FMT_RGB565
:
603 color
= DPI_COLOR_CODING_16BIT_1
;
607 if (mode
->flags
& DRM_MODE_FLAG_NVSYNC
)
608 val
|= VSYNC_ACTIVE_LOW
;
609 if (mode
->flags
& DRM_MODE_FLAG_NHSYNC
)
610 val
|= HSYNC_ACTIVE_LOW
;
612 dsi_write(dsi
, DSI_DPI_VCID
, DPI_VCID(dsi
->channel
));
613 dsi_write(dsi
, DSI_DPI_COLOR_CODING
, color
);
614 dsi_write(dsi
, DSI_DPI_CFG_POL
, val
);
616 * TODO dw drv improvements
617 * largest packet sizes during hfp or during vsa/vpb/vfp
618 * should be computed according to byte lane, lane number and only
619 * if sending lp cmds in high speed is enable (PHY_TXREQUESTCLKHS)
621 dsi_write(dsi
, DSI_DPI_LP_CMD_TIM
, OUTVACT_LPCMD_TIME(4)
622 | INVACT_LPCMD_TIME(4));
625 static void dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi
*dsi
)
627 dsi_write(dsi
, DSI_PCKHDL_CFG
, CRC_RX_EN
| ECC_RX_EN
| BTA_EN
);
630 static void dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi
*dsi
,
631 const struct drm_display_mode
*mode
)
634 * TODO dw drv improvements
635 * only burst mode is supported here. For non-burst video modes,
636 * we should compute DSI_VID_PKT_SIZE, DSI_VCCR.NUMC &
637 * DSI_VNPCR.NPSIZE... especially because this driver supports
638 * non-burst video modes, see dw_mipi_dsi_video_mode_config()...
641 dsi_write(dsi
, DSI_VID_PKT_SIZE
,
642 dw_mipi_is_dual_mode(dsi
) ?
643 VID_PKT_SIZE(mode
->hdisplay
/ 2) :
644 VID_PKT_SIZE(mode
->hdisplay
));
647 static void dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi
*dsi
)
650 * TODO dw drv improvements
651 * compute high speed transmission counter timeout according
652 * to the timeout clock division (TO_CLK_DIVISION) and byte lane...
654 dsi_write(dsi
, DSI_TO_CNT_CFG
, HSTX_TO_CNT(1000) | LPRX_TO_CNT(1000));
656 * TODO dw drv improvements
657 * the Bus-Turn-Around Timeout Counter should be computed
658 * according to byte lane...
660 dsi_write(dsi
, DSI_BTA_TO_CNT
, 0xd00);
661 dsi_write(dsi
, DSI_MODE_CFG
, ENABLE_CMD_MODE
);
664 /* Get lane byte clock cycles. */
665 static u32
dw_mipi_dsi_get_hcomponent_lbcc(struct dw_mipi_dsi
*dsi
,
666 const struct drm_display_mode
*mode
,
671 lbcc
= hcomponent
* dsi
->lane_mbps
* MSEC_PER_SEC
/ 8;
673 frac
= lbcc
% mode
->clock
;
674 lbcc
= lbcc
/ mode
->clock
;
681 static void dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi
*dsi
,
682 const struct drm_display_mode
*mode
)
684 u32 htotal
, hsa
, hbp
, lbcc
;
686 htotal
= mode
->htotal
;
687 hsa
= mode
->hsync_end
- mode
->hsync_start
;
688 hbp
= mode
->htotal
- mode
->hsync_end
;
691 * TODO dw drv improvements
692 * computations below may be improved...
694 lbcc
= dw_mipi_dsi_get_hcomponent_lbcc(dsi
, mode
, htotal
);
695 dsi_write(dsi
, DSI_VID_HLINE_TIME
, lbcc
);
697 lbcc
= dw_mipi_dsi_get_hcomponent_lbcc(dsi
, mode
, hsa
);
698 dsi_write(dsi
, DSI_VID_HSA_TIME
, lbcc
);
700 lbcc
= dw_mipi_dsi_get_hcomponent_lbcc(dsi
, mode
, hbp
);
701 dsi_write(dsi
, DSI_VID_HBP_TIME
, lbcc
);
704 static void dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi
*dsi
,
705 const struct drm_display_mode
*mode
)
707 u32 vactive
, vsa
, vfp
, vbp
;
709 vactive
= mode
->vdisplay
;
710 vsa
= mode
->vsync_end
- mode
->vsync_start
;
711 vfp
= mode
->vsync_start
- mode
->vdisplay
;
712 vbp
= mode
->vtotal
- mode
->vsync_end
;
714 dsi_write(dsi
, DSI_VID_VACTIVE_LINES
, vactive
);
715 dsi_write(dsi
, DSI_VID_VSA_LINES
, vsa
);
716 dsi_write(dsi
, DSI_VID_VFP_LINES
, vfp
);
717 dsi_write(dsi
, DSI_VID_VBP_LINES
, vbp
);
720 static void dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi
*dsi
)
722 const struct dw_mipi_dsi_phy_ops
*phy_ops
= dsi
->plat_data
->phy_ops
;
723 struct dw_mipi_dsi_dphy_timing timing
;
727 ret
= phy_ops
->get_timing(dsi
->plat_data
->priv_data
,
728 dsi
->lane_mbps
, &timing
);
730 DRM_DEV_ERROR(dsi
->dev
, "Retrieving phy timings failed\n");
733 * TODO dw drv improvements
734 * data & clock lane timers should be computed according to panel
735 * blankings and to the automatic clock lane control mode...
736 * note: DSI_PHY_TMR_CFG.MAX_RD_TIME should be in line with
737 * DSI_CMD_MODE_CFG.MAX_RD_PKT_SIZE_LP (see CMD_MODE_ALL_LP)
740 hw_version
= dsi_read(dsi
, DSI_VERSION
) & VERSION
;
742 if (hw_version
>= HWVER_131
) {
743 dsi_write(dsi
, DSI_PHY_TMR_CFG
,
744 PHY_HS2LP_TIME_V131(timing
.data_hs2lp
) |
745 PHY_LP2HS_TIME_V131(timing
.data_lp2hs
));
746 dsi_write(dsi
, DSI_PHY_TMR_RD_CFG
, MAX_RD_TIME_V131(10000));
748 dsi_write(dsi
, DSI_PHY_TMR_CFG
,
749 PHY_HS2LP_TIME(timing
.data_hs2lp
) |
750 PHY_LP2HS_TIME(timing
.data_lp2hs
) |
754 dsi_write(dsi
, DSI_PHY_TMR_LPCLK_CFG
,
755 PHY_CLKHS2LP_TIME(timing
.clk_hs2lp
) |
756 PHY_CLKLP2HS_TIME(timing
.clk_lp2hs
));
759 static void dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi
*dsi
)
762 * TODO dw drv improvements
763 * stop wait time should be the maximum between host dsi
764 * and panel stop wait times
766 dsi_write(dsi
, DSI_PHY_IF_CFG
, PHY_STOP_WAIT_TIME(0x20) |
767 N_LANES(dsi
->lanes
));
770 static void dw_mipi_dsi_dphy_init(struct dw_mipi_dsi
*dsi
)
772 /* Clear PHY state */
773 dsi_write(dsi
, DSI_PHY_RSTZ
, PHY_DISFORCEPLL
| PHY_DISABLECLK
774 | PHY_RSTZ
| PHY_SHUTDOWNZ
);
775 dsi_write(dsi
, DSI_PHY_TST_CTRL0
, PHY_UNTESTCLR
);
776 dsi_write(dsi
, DSI_PHY_TST_CTRL0
, PHY_TESTCLR
);
777 dsi_write(dsi
, DSI_PHY_TST_CTRL0
, PHY_UNTESTCLR
);
780 static void dw_mipi_dsi_dphy_enable(struct dw_mipi_dsi
*dsi
)
785 dsi_write(dsi
, DSI_PHY_RSTZ
, PHY_ENFORCEPLL
| PHY_ENABLECLK
|
786 PHY_UNRSTZ
| PHY_UNSHUTDOWNZ
);
788 ret
= readl_poll_timeout(dsi
->base
+ DSI_PHY_STATUS
, val
,
789 val
& PHY_LOCK
, 1000, PHY_STATUS_TIMEOUT_US
);
791 DRM_DEBUG_DRIVER("failed to wait phy lock state\n");
793 ret
= readl_poll_timeout(dsi
->base
+ DSI_PHY_STATUS
,
794 val
, val
& PHY_STOP_STATE_CLK_LANE
, 1000,
795 PHY_STATUS_TIMEOUT_US
);
797 DRM_DEBUG_DRIVER("failed to wait phy clk lane stop state\n");
800 static void dw_mipi_dsi_clear_err(struct dw_mipi_dsi
*dsi
)
802 dsi_read(dsi
, DSI_INT_ST0
);
803 dsi_read(dsi
, DSI_INT_ST1
);
804 dsi_write(dsi
, DSI_INT_MSK0
, 0);
805 dsi_write(dsi
, DSI_INT_MSK1
, 0);
808 static void dw_mipi_dsi_bridge_post_disable(struct drm_bridge
*bridge
)
810 struct dw_mipi_dsi
*dsi
= bridge_to_dsi(bridge
);
811 const struct dw_mipi_dsi_phy_ops
*phy_ops
= dsi
->plat_data
->phy_ops
;
814 * Switch to command mode before panel-bridge post_disable &
816 * Note: panel-bridge disable & panel disable has been called
817 * before by the drm framework.
819 dw_mipi_dsi_set_mode(dsi
, 0);
822 * TODO Only way found to call panel-bridge post_disable &
823 * panel unprepare before the dsi "final" disable...
824 * This needs to be fixed in the drm_bridge framework and the API
825 * needs to be updated to manage our own call chains...
827 dsi
->panel_bridge
->funcs
->post_disable(dsi
->panel_bridge
);
829 if (phy_ops
->power_off
)
830 phy_ops
->power_off(dsi
->plat_data
->priv_data
);
833 dw_mipi_dsi_disable(dsi
->slave
);
834 clk_disable_unprepare(dsi
->slave
->pclk
);
835 pm_runtime_put(dsi
->slave
->dev
);
837 dw_mipi_dsi_disable(dsi
);
839 clk_disable_unprepare(dsi
->pclk
);
840 pm_runtime_put(dsi
->dev
);
843 static unsigned int dw_mipi_dsi_get_lanes(struct dw_mipi_dsi
*dsi
)
845 /* this instance is the slave, so add the master's lanes */
847 return dsi
->master
->lanes
+ dsi
->lanes
;
849 /* this instance is the master, so add the slave's lanes */
851 return dsi
->lanes
+ dsi
->slave
->lanes
;
853 /* single-dsi, so no other instance to consider */
857 static void dw_mipi_dsi_mode_set(struct dw_mipi_dsi
*dsi
,
858 const struct drm_display_mode
*adjusted_mode
)
860 const struct dw_mipi_dsi_phy_ops
*phy_ops
= dsi
->plat_data
->phy_ops
;
861 void *priv_data
= dsi
->plat_data
->priv_data
;
863 u32 lanes
= dw_mipi_dsi_get_lanes(dsi
);
865 clk_prepare_enable(dsi
->pclk
);
867 ret
= phy_ops
->get_lane_mbps(priv_data
, adjusted_mode
, dsi
->mode_flags
,
868 lanes
, dsi
->format
, &dsi
->lane_mbps
);
870 DRM_DEBUG_DRIVER("Phy get_lane_mbps() failed\n");
872 pm_runtime_get_sync(dsi
->dev
);
873 dw_mipi_dsi_init(dsi
);
874 dw_mipi_dsi_dpi_config(dsi
, adjusted_mode
);
875 dw_mipi_dsi_packet_handler_config(dsi
);
876 dw_mipi_dsi_video_mode_config(dsi
);
877 dw_mipi_dsi_video_packet_config(dsi
, adjusted_mode
);
878 dw_mipi_dsi_command_mode_config(dsi
);
879 dw_mipi_dsi_line_timer_config(dsi
, adjusted_mode
);
880 dw_mipi_dsi_vertical_timing_config(dsi
, adjusted_mode
);
882 dw_mipi_dsi_dphy_init(dsi
);
883 dw_mipi_dsi_dphy_timing_config(dsi
);
884 dw_mipi_dsi_dphy_interface_config(dsi
);
886 dw_mipi_dsi_clear_err(dsi
);
888 ret
= phy_ops
->init(priv_data
);
890 DRM_DEBUG_DRIVER("Phy init() failed\n");
892 dw_mipi_dsi_dphy_enable(dsi
);
894 dw_mipi_dsi_wait_for_two_frames(adjusted_mode
);
896 /* Switch to cmd mode for panel-bridge pre_enable & panel prepare */
897 dw_mipi_dsi_set_mode(dsi
, 0);
899 if (phy_ops
->power_on
)
900 phy_ops
->power_on(dsi
->plat_data
->priv_data
);
903 static void dw_mipi_dsi_bridge_mode_set(struct drm_bridge
*bridge
,
904 const struct drm_display_mode
*mode
,
905 const struct drm_display_mode
*adjusted_mode
)
907 struct dw_mipi_dsi
*dsi
= bridge_to_dsi(bridge
);
909 dw_mipi_dsi_mode_set(dsi
, adjusted_mode
);
911 dw_mipi_dsi_mode_set(dsi
->slave
, adjusted_mode
);
914 static void dw_mipi_dsi_bridge_enable(struct drm_bridge
*bridge
)
916 struct dw_mipi_dsi
*dsi
= bridge_to_dsi(bridge
);
918 /* Switch to video mode for panel-bridge enable & panel enable */
919 dw_mipi_dsi_set_mode(dsi
, MIPI_DSI_MODE_VIDEO
);
921 dw_mipi_dsi_set_mode(dsi
->slave
, MIPI_DSI_MODE_VIDEO
);
924 static enum drm_mode_status
925 dw_mipi_dsi_bridge_mode_valid(struct drm_bridge
*bridge
,
926 const struct drm_display_mode
*mode
)
928 struct dw_mipi_dsi
*dsi
= bridge_to_dsi(bridge
);
929 const struct dw_mipi_dsi_plat_data
*pdata
= dsi
->plat_data
;
930 enum drm_mode_status mode_status
= MODE_OK
;
932 if (pdata
->mode_valid
)
933 mode_status
= pdata
->mode_valid(pdata
->priv_data
, mode
);
938 static int dw_mipi_dsi_bridge_attach(struct drm_bridge
*bridge
)
940 struct dw_mipi_dsi
*dsi
= bridge_to_dsi(bridge
);
942 if (!bridge
->encoder
) {
943 DRM_ERROR("Parent encoder object not found\n");
947 /* Set the encoder type as caller does not know it */
948 bridge
->encoder
->encoder_type
= DRM_MODE_ENCODER_DSI
;
950 /* Attach the panel-bridge to the dsi bridge */
951 return drm_bridge_attach(bridge
->encoder
, dsi
->panel_bridge
, bridge
);
954 static const struct drm_bridge_funcs dw_mipi_dsi_bridge_funcs
= {
955 .mode_set
= dw_mipi_dsi_bridge_mode_set
,
956 .enable
= dw_mipi_dsi_bridge_enable
,
957 .post_disable
= dw_mipi_dsi_bridge_post_disable
,
958 .mode_valid
= dw_mipi_dsi_bridge_mode_valid
,
959 .attach
= dw_mipi_dsi_bridge_attach
,
962 #ifdef CONFIG_DEBUG_FS
964 static void dw_mipi_dsi_debugfs_init(struct dw_mipi_dsi
*dsi
)
966 dsi
->debugfs
= debugfs_create_dir(dev_name(dsi
->dev
), NULL
);
967 if (IS_ERR(dsi
->debugfs
)) {
968 dev_err(dsi
->dev
, "failed to create debugfs root\n");
972 debugfs_create_bool("vpg", 0660, dsi
->debugfs
, &dsi
->vpg
);
973 debugfs_create_bool("vpg_horizontal", 0660, dsi
->debugfs
,
974 &dsi
->vpg_horizontal
);
977 static void dw_mipi_dsi_debugfs_remove(struct dw_mipi_dsi
*dsi
)
979 debugfs_remove_recursive(dsi
->debugfs
);
984 static void dw_mipi_dsi_debugfs_init(struct dw_mipi_dsi
*dsi
) { }
985 static void dw_mipi_dsi_debugfs_remove(struct dw_mipi_dsi
*dsi
) { }
987 #endif /* CONFIG_DEBUG_FS */
989 static struct dw_mipi_dsi
*
990 __dw_mipi_dsi_probe(struct platform_device
*pdev
,
991 const struct dw_mipi_dsi_plat_data
*plat_data
)
993 struct device
*dev
= &pdev
->dev
;
994 struct reset_control
*apb_rst
;
995 struct dw_mipi_dsi
*dsi
;
998 dsi
= devm_kzalloc(dev
, sizeof(*dsi
), GFP_KERNEL
);
1000 return ERR_PTR(-ENOMEM
);
1003 dsi
->plat_data
= plat_data
;
1005 if (!plat_data
->phy_ops
->init
|| !plat_data
->phy_ops
->get_lane_mbps
||
1006 !plat_data
->phy_ops
->get_timing
) {
1007 DRM_ERROR("Phy not properly configured\n");
1008 return ERR_PTR(-ENODEV
);
1011 if (!plat_data
->base
) {
1012 dsi
->base
= devm_platform_ioremap_resource(pdev
, 0);
1013 if (IS_ERR(dsi
->base
))
1014 return ERR_PTR(-ENODEV
);
1017 dsi
->base
= plat_data
->base
;
1020 dsi
->pclk
= devm_clk_get(dev
, "pclk");
1021 if (IS_ERR(dsi
->pclk
)) {
1022 ret
= PTR_ERR(dsi
->pclk
);
1023 dev_err(dev
, "Unable to get pclk: %d\n", ret
);
1024 return ERR_PTR(ret
);
1028 * Note that the reset was not defined in the initial device tree, so
1029 * we have to be prepared for it not being found.
1031 apb_rst
= devm_reset_control_get_optional_exclusive(dev
, "apb");
1032 if (IS_ERR(apb_rst
)) {
1033 ret
= PTR_ERR(apb_rst
);
1035 if (ret
!= -EPROBE_DEFER
)
1036 dev_err(dev
, "Unable to get reset control: %d\n", ret
);
1038 return ERR_PTR(ret
);
1042 ret
= clk_prepare_enable(dsi
->pclk
);
1044 dev_err(dev
, "%s: Failed to enable pclk\n", __func__
);
1045 return ERR_PTR(ret
);
1048 reset_control_assert(apb_rst
);
1049 usleep_range(10, 20);
1050 reset_control_deassert(apb_rst
);
1052 clk_disable_unprepare(dsi
->pclk
);
1055 dw_mipi_dsi_debugfs_init(dsi
);
1056 pm_runtime_enable(dev
);
1058 dsi
->dsi_host
.ops
= &dw_mipi_dsi_host_ops
;
1059 dsi
->dsi_host
.dev
= dev
;
1060 ret
= mipi_dsi_host_register(&dsi
->dsi_host
);
1062 dev_err(dev
, "Failed to register MIPI host: %d\n", ret
);
1063 dw_mipi_dsi_debugfs_remove(dsi
);
1064 return ERR_PTR(ret
);
1067 dsi
->bridge
.driver_private
= dsi
;
1068 dsi
->bridge
.funcs
= &dw_mipi_dsi_bridge_funcs
;
1070 dsi
->bridge
.of_node
= pdev
->dev
.of_node
;
1076 static void __dw_mipi_dsi_remove(struct dw_mipi_dsi
*dsi
)
1078 mipi_dsi_host_unregister(&dsi
->dsi_host
);
1080 pm_runtime_disable(dsi
->dev
);
1081 dw_mipi_dsi_debugfs_remove(dsi
);
1084 void dw_mipi_dsi_set_slave(struct dw_mipi_dsi
*dsi
, struct dw_mipi_dsi
*slave
)
1086 /* introduce controllers to each other */
1088 dsi
->slave
->master
= dsi
;
1090 /* migrate settings for already attached displays */
1091 dsi
->slave
->lanes
= dsi
->lanes
;
1092 dsi
->slave
->channel
= dsi
->channel
;
1093 dsi
->slave
->format
= dsi
->format
;
1094 dsi
->slave
->mode_flags
= dsi
->mode_flags
;
1096 EXPORT_SYMBOL_GPL(dw_mipi_dsi_set_slave
);
1099 * Probe/remove API, used from platforms based on the DRM bridge API.
1101 struct dw_mipi_dsi
*
1102 dw_mipi_dsi_probe(struct platform_device
*pdev
,
1103 const struct dw_mipi_dsi_plat_data
*plat_data
)
1105 return __dw_mipi_dsi_probe(pdev
, plat_data
);
1107 EXPORT_SYMBOL_GPL(dw_mipi_dsi_probe
);
1109 void dw_mipi_dsi_remove(struct dw_mipi_dsi
*dsi
)
1111 __dw_mipi_dsi_remove(dsi
);
1113 EXPORT_SYMBOL_GPL(dw_mipi_dsi_remove
);
1116 * Bind/unbind API, used from platforms based on the component framework.
1118 int dw_mipi_dsi_bind(struct dw_mipi_dsi
*dsi
, struct drm_encoder
*encoder
)
1122 ret
= drm_bridge_attach(encoder
, &dsi
->bridge
, NULL
);
1124 DRM_ERROR("Failed to initialize bridge with drm\n");
1130 EXPORT_SYMBOL_GPL(dw_mipi_dsi_bind
);
1132 void dw_mipi_dsi_unbind(struct dw_mipi_dsi
*dsi
)
1135 EXPORT_SYMBOL_GPL(dw_mipi_dsi_unbind
);
1137 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
1138 MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
1139 MODULE_DESCRIPTION("DW MIPI DSI host controller driver");
1140 MODULE_LICENSE("GPL");
1141 MODULE_ALIAS("platform:dw-mipi-dsi");