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/iopoll.h>
14 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/reset.h>
19 #include <video/mipi_display.h>
21 #include <drm/bridge/dw_mipi_dsi.h>
22 #include <drm/drm_atomic_helper.h>
23 #include <drm/drm_bridge.h>
24 #include <drm/drm_crtc.h>
25 #include <drm/drm_mipi_dsi.h>
26 #include <drm/drm_modes.h>
27 #include <drm/drm_of.h>
28 #include <drm/drm_print.h>
29 #include <drm/drm_probe_helper.h>
31 #define HWVER_131 0x31333100 /* IP version 1.31 */
33 #define DSI_VERSION 0x00
34 #define VERSION GENMASK(31, 8)
36 #define DSI_PWR_UP 0x04
38 #define POWERUP BIT(0)
40 #define DSI_CLKMGR_CFG 0x08
41 #define TO_CLK_DIVISION(div) (((div) & 0xff) << 8)
42 #define TX_ESC_CLK_DIVISION(div) ((div) & 0xff)
44 #define DSI_DPI_VCID 0x0c
45 #define DPI_VCID(vcid) ((vcid) & 0x3)
47 #define DSI_DPI_COLOR_CODING 0x10
48 #define LOOSELY18_EN BIT(8)
49 #define DPI_COLOR_CODING_16BIT_1 0x0
50 #define DPI_COLOR_CODING_16BIT_2 0x1
51 #define DPI_COLOR_CODING_16BIT_3 0x2
52 #define DPI_COLOR_CODING_18BIT_1 0x3
53 #define DPI_COLOR_CODING_18BIT_2 0x4
54 #define DPI_COLOR_CODING_24BIT 0x5
56 #define DSI_DPI_CFG_POL 0x14
57 #define COLORM_ACTIVE_LOW BIT(4)
58 #define SHUTD_ACTIVE_LOW BIT(3)
59 #define HSYNC_ACTIVE_LOW BIT(2)
60 #define VSYNC_ACTIVE_LOW BIT(1)
61 #define DATAEN_ACTIVE_LOW BIT(0)
63 #define DSI_DPI_LP_CMD_TIM 0x18
64 #define OUTVACT_LPCMD_TIME(p) (((p) & 0xff) << 16)
65 #define INVACT_LPCMD_TIME(p) ((p) & 0xff)
67 #define DSI_DBI_VCID 0x1c
68 #define DSI_DBI_CFG 0x20
69 #define DSI_DBI_PARTITIONING_EN 0x24
70 #define DSI_DBI_CMDSIZE 0x28
72 #define DSI_PCKHDL_CFG 0x2c
73 #define CRC_RX_EN BIT(4)
74 #define ECC_RX_EN BIT(3)
76 #define EOTP_RX_EN BIT(1)
77 #define EOTP_TX_EN BIT(0)
79 #define DSI_GEN_VCID 0x30
81 #define DSI_MODE_CFG 0x34
82 #define ENABLE_VIDEO_MODE 0
83 #define ENABLE_CMD_MODE BIT(0)
85 #define DSI_VID_MODE_CFG 0x38
86 #define ENABLE_LOW_POWER (0x3f << 8)
87 #define ENABLE_LOW_POWER_MASK (0x3f << 8)
88 #define VID_MODE_TYPE_NON_BURST_SYNC_PULSES 0x0
89 #define VID_MODE_TYPE_NON_BURST_SYNC_EVENTS 0x1
90 #define VID_MODE_TYPE_BURST 0x2
91 #define VID_MODE_TYPE_MASK 0x3
93 #define DSI_VID_PKT_SIZE 0x3c
94 #define VID_PKT_SIZE(p) ((p) & 0x3fff)
96 #define DSI_VID_NUM_CHUNKS 0x40
97 #define VID_NUM_CHUNKS(c) ((c) & 0x1fff)
99 #define DSI_VID_NULL_SIZE 0x44
100 #define VID_NULL_SIZE(b) ((b) & 0x1fff)
102 #define DSI_VID_HSA_TIME 0x48
103 #define DSI_VID_HBP_TIME 0x4c
104 #define DSI_VID_HLINE_TIME 0x50
105 #define DSI_VID_VSA_LINES 0x54
106 #define DSI_VID_VBP_LINES 0x58
107 #define DSI_VID_VFP_LINES 0x5c
108 #define DSI_VID_VACTIVE_LINES 0x60
109 #define DSI_EDPI_CMD_SIZE 0x64
111 #define DSI_CMD_MODE_CFG 0x68
112 #define MAX_RD_PKT_SIZE_LP BIT(24)
113 #define DCS_LW_TX_LP BIT(19)
114 #define DCS_SR_0P_TX_LP BIT(18)
115 #define DCS_SW_1P_TX_LP BIT(17)
116 #define DCS_SW_0P_TX_LP BIT(16)
117 #define GEN_LW_TX_LP BIT(14)
118 #define GEN_SR_2P_TX_LP BIT(13)
119 #define GEN_SR_1P_TX_LP BIT(12)
120 #define GEN_SR_0P_TX_LP BIT(11)
121 #define GEN_SW_2P_TX_LP BIT(10)
122 #define GEN_SW_1P_TX_LP BIT(9)
123 #define GEN_SW_0P_TX_LP BIT(8)
124 #define ACK_RQST_EN BIT(1)
125 #define TEAR_FX_EN BIT(0)
127 #define CMD_MODE_ALL_LP (MAX_RD_PKT_SIZE_LP | \
140 #define DSI_GEN_HDR 0x6c
141 #define DSI_GEN_PLD_DATA 0x70
143 #define DSI_CMD_PKT_STATUS 0x74
144 #define GEN_RD_CMD_BUSY BIT(6)
145 #define GEN_PLD_R_FULL BIT(5)
146 #define GEN_PLD_R_EMPTY BIT(4)
147 #define GEN_PLD_W_FULL BIT(3)
148 #define GEN_PLD_W_EMPTY BIT(2)
149 #define GEN_CMD_FULL BIT(1)
150 #define GEN_CMD_EMPTY BIT(0)
152 #define DSI_TO_CNT_CFG 0x78
153 #define HSTX_TO_CNT(p) (((p) & 0xffff) << 16)
154 #define LPRX_TO_CNT(p) ((p) & 0xffff)
156 #define DSI_HS_RD_TO_CNT 0x7c
157 #define DSI_LP_RD_TO_CNT 0x80
158 #define DSI_HS_WR_TO_CNT 0x84
159 #define DSI_LP_WR_TO_CNT 0x88
160 #define DSI_BTA_TO_CNT 0x8c
162 #define DSI_LPCLK_CTRL 0x94
163 #define AUTO_CLKLANE_CTRL BIT(1)
164 #define PHY_TXREQUESTCLKHS BIT(0)
166 #define DSI_PHY_TMR_LPCLK_CFG 0x98
167 #define PHY_CLKHS2LP_TIME(lbcc) (((lbcc) & 0x3ff) << 16)
168 #define PHY_CLKLP2HS_TIME(lbcc) ((lbcc) & 0x3ff)
170 #define DSI_PHY_TMR_CFG 0x9c
171 #define PHY_HS2LP_TIME(lbcc) (((lbcc) & 0xff) << 24)
172 #define PHY_LP2HS_TIME(lbcc) (((lbcc) & 0xff) << 16)
173 #define MAX_RD_TIME(lbcc) ((lbcc) & 0x7fff)
174 #define PHY_HS2LP_TIME_V131(lbcc) (((lbcc) & 0x3ff) << 16)
175 #define PHY_LP2HS_TIME_V131(lbcc) ((lbcc) & 0x3ff)
177 #define DSI_PHY_RSTZ 0xa0
178 #define PHY_DISFORCEPLL 0
179 #define PHY_ENFORCEPLL BIT(3)
180 #define PHY_DISABLECLK 0
181 #define PHY_ENABLECLK BIT(2)
183 #define PHY_UNRSTZ BIT(1)
184 #define PHY_SHUTDOWNZ 0
185 #define PHY_UNSHUTDOWNZ BIT(0)
187 #define DSI_PHY_IF_CFG 0xa4
188 #define PHY_STOP_WAIT_TIME(cycle) (((cycle) & 0xff) << 8)
189 #define N_LANES(n) (((n) - 1) & 0x3)
191 #define DSI_PHY_ULPS_CTRL 0xa8
192 #define DSI_PHY_TX_TRIGGERS 0xac
194 #define DSI_PHY_STATUS 0xb0
195 #define PHY_STOP_STATE_CLK_LANE BIT(2)
196 #define PHY_LOCK BIT(0)
198 #define DSI_PHY_TST_CTRL0 0xb4
199 #define PHY_TESTCLK BIT(1)
200 #define PHY_UNTESTCLK 0
201 #define PHY_TESTCLR BIT(0)
202 #define PHY_UNTESTCLR 0
204 #define DSI_PHY_TST_CTRL1 0xb8
205 #define PHY_TESTEN BIT(16)
206 #define PHY_UNTESTEN 0
207 #define PHY_TESTDOUT(n) (((n) & 0xff) << 8)
208 #define PHY_TESTDIN(n) ((n) & 0xff)
210 #define DSI_INT_ST0 0xbc
211 #define DSI_INT_ST1 0xc0
212 #define DSI_INT_MSK0 0xc4
213 #define DSI_INT_MSK1 0xc8
215 #define DSI_PHY_TMR_RD_CFG 0xf4
216 #define MAX_RD_TIME_V131(lbcc) ((lbcc) & 0x7fff)
218 #define PHY_STATUS_TIMEOUT_US 10000
219 #define CMD_PKT_STATUS_TIMEOUT_US 20000
222 struct drm_bridge bridge;
223 struct mipi_dsi_host dsi_host;
224 struct drm_bridge *panel_bridge;
230 unsigned int lane_mbps; /* per lane */
234 unsigned long mode_flags;
236 struct dw_mipi_dsi *master; /* dual-dsi master ptr */
237 struct dw_mipi_dsi *slave; /* dual-dsi slave ptr */
239 const struct dw_mipi_dsi_plat_data *plat_data;
243 * Check if either a link to a master or slave is present
245 static inline bool dw_mipi_is_dual_mode(struct dw_mipi_dsi *dsi)
247 return dsi->slave || dsi->master;
251 * The controller should generate 2 frames before
252 * preparing the peripheral.
254 static void dw_mipi_dsi_wait_for_two_frames(const struct drm_display_mode *mode)
256 int refresh, two_frames;
258 refresh = drm_mode_vrefresh(mode);
259 two_frames = DIV_ROUND_UP(MSEC_PER_SEC, refresh) * 2;
263 static inline struct dw_mipi_dsi *host_to_dsi(struct mipi_dsi_host *host)
265 return container_of(host, struct dw_mipi_dsi, dsi_host);
268 static inline struct dw_mipi_dsi *bridge_to_dsi(struct drm_bridge *bridge)
270 return container_of(bridge, struct dw_mipi_dsi, bridge);
273 static inline void dsi_write(struct dw_mipi_dsi *dsi, u32 reg, u32 val)
275 writel(val, dsi->base + reg);
278 static inline u32 dsi_read(struct dw_mipi_dsi *dsi, u32 reg)
280 return readl(dsi->base + reg);
283 static int dw_mipi_dsi_host_attach(struct mipi_dsi_host *host,
284 struct mipi_dsi_device *device)
286 struct dw_mipi_dsi *dsi = host_to_dsi(host);
287 const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
288 struct drm_bridge *bridge;
289 struct drm_panel *panel;
292 if (device->lanes > dsi->plat_data->max_data_lanes) {
293 dev_err(dsi->dev, "the number of data lanes(%u) is too many\n",
298 dsi->lanes = device->lanes;
299 dsi->channel = device->channel;
300 dsi->format = device->format;
301 dsi->mode_flags = device->mode_flags;
303 ret = drm_of_find_panel_or_bridge(host->dev->of_node, 1, 0,
309 bridge = drm_panel_bridge_add(panel, DRM_MODE_CONNECTOR_DSI);
311 return PTR_ERR(bridge);
314 dsi->panel_bridge = bridge;
316 drm_bridge_add(&dsi->bridge);
318 if (pdata->host_ops && pdata->host_ops->attach) {
319 ret = pdata->host_ops->attach(pdata->priv_data, device);
327 static int dw_mipi_dsi_host_detach(struct mipi_dsi_host *host,
328 struct mipi_dsi_device *device)
330 struct dw_mipi_dsi *dsi = host_to_dsi(host);
331 const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
334 if (pdata->host_ops && pdata->host_ops->detach) {
335 ret = pdata->host_ops->detach(pdata->priv_data, device);
340 drm_of_panel_bridge_remove(host->dev->of_node, 1, 0);
342 drm_bridge_remove(&dsi->bridge);
347 static void dw_mipi_message_config(struct dw_mipi_dsi *dsi,
348 const struct mipi_dsi_msg *msg)
350 bool lpm = msg->flags & MIPI_DSI_MSG_USE_LPM;
353 if (msg->flags & MIPI_DSI_MSG_REQ_ACK)
356 val |= CMD_MODE_ALL_LP;
358 dsi_write(dsi, DSI_LPCLK_CTRL, lpm ? 0 : PHY_TXREQUESTCLKHS);
359 dsi_write(dsi, DSI_CMD_MODE_CFG, val);
362 static int dw_mipi_dsi_gen_pkt_hdr_write(struct dw_mipi_dsi *dsi, u32 hdr_val)
367 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
368 val, !(val & GEN_CMD_FULL), 1000,
369 CMD_PKT_STATUS_TIMEOUT_US);
371 dev_err(dsi->dev, "failed to get available command FIFO\n");
375 dsi_write(dsi, DSI_GEN_HDR, hdr_val);
377 mask = GEN_CMD_EMPTY | GEN_PLD_W_EMPTY;
378 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
379 val, (val & mask) == mask,
380 1000, CMD_PKT_STATUS_TIMEOUT_US);
382 dev_err(dsi->dev, "failed to write command FIFO\n");
389 static int dw_mipi_dsi_write(struct dw_mipi_dsi *dsi,
390 const struct mipi_dsi_packet *packet)
392 const u8 *tx_buf = packet->payload;
393 int len = packet->payload_length, pld_data_bytes = sizeof(u32), ret;
398 if (len < pld_data_bytes) {
400 memcpy(&word, tx_buf, len);
401 dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
404 memcpy(&word, tx_buf, pld_data_bytes);
405 dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
406 tx_buf += pld_data_bytes;
407 len -= pld_data_bytes;
410 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
411 val, !(val & GEN_PLD_W_FULL), 1000,
412 CMD_PKT_STATUS_TIMEOUT_US);
415 "failed to get available write payload FIFO\n");
421 memcpy(&word, packet->header, sizeof(packet->header));
422 return dw_mipi_dsi_gen_pkt_hdr_write(dsi, le32_to_cpu(word));
425 static int dw_mipi_dsi_read(struct dw_mipi_dsi *dsi,
426 const struct mipi_dsi_msg *msg)
428 int i, j, ret, len = msg->rx_len;
429 u8 *buf = msg->rx_buf;
432 /* Wait end of the read operation */
433 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
434 val, !(val & GEN_RD_CMD_BUSY),
435 1000, CMD_PKT_STATUS_TIMEOUT_US);
437 dev_err(dsi->dev, "Timeout during read operation\n");
441 for (i = 0; i < len; i += 4) {
442 /* Read fifo must not be empty before all bytes are read */
443 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
444 val, !(val & GEN_PLD_R_EMPTY),
445 1000, CMD_PKT_STATUS_TIMEOUT_US);
447 dev_err(dsi->dev, "Read payload FIFO is empty\n");
451 val = dsi_read(dsi, DSI_GEN_PLD_DATA);
452 for (j = 0; j < 4 && j + i < len; j++)
453 buf[i + j] = val >> (8 * j);
459 static ssize_t dw_mipi_dsi_host_transfer(struct mipi_dsi_host *host,
460 const struct mipi_dsi_msg *msg)
462 struct dw_mipi_dsi *dsi = host_to_dsi(host);
463 struct mipi_dsi_packet packet;
466 ret = mipi_dsi_create_packet(&packet, msg);
468 dev_err(dsi->dev, "failed to create packet: %d\n", ret);
472 dw_mipi_message_config(dsi, msg);
474 dw_mipi_message_config(dsi->slave, msg);
476 ret = dw_mipi_dsi_write(dsi, &packet);
480 ret = dw_mipi_dsi_write(dsi->slave, &packet);
485 if (msg->rx_buf && msg->rx_len) {
486 ret = dw_mipi_dsi_read(dsi, msg);
489 nb_bytes = msg->rx_len;
491 nb_bytes = packet.size;
497 static const struct mipi_dsi_host_ops dw_mipi_dsi_host_ops = {
498 .attach = dw_mipi_dsi_host_attach,
499 .detach = dw_mipi_dsi_host_detach,
500 .transfer = dw_mipi_dsi_host_transfer,
503 static void dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi *dsi)
508 * TODO dw drv improvements
509 * enabling low power is panel-dependent, we should use the
510 * panel configuration here...
512 val = ENABLE_LOW_POWER;
514 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
515 val |= VID_MODE_TYPE_BURST;
516 else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
517 val |= VID_MODE_TYPE_NON_BURST_SYNC_PULSES;
519 val |= VID_MODE_TYPE_NON_BURST_SYNC_EVENTS;
521 dsi_write(dsi, DSI_VID_MODE_CFG, val);
524 static void dw_mipi_dsi_set_mode(struct dw_mipi_dsi *dsi,
525 unsigned long mode_flags)
527 dsi_write(dsi, DSI_PWR_UP, RESET);
529 if (mode_flags & MIPI_DSI_MODE_VIDEO) {
530 dsi_write(dsi, DSI_MODE_CFG, ENABLE_VIDEO_MODE);
531 dw_mipi_dsi_video_mode_config(dsi);
532 dsi_write(dsi, DSI_LPCLK_CTRL, PHY_TXREQUESTCLKHS);
534 dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
537 dsi_write(dsi, DSI_PWR_UP, POWERUP);
540 static void dw_mipi_dsi_disable(struct dw_mipi_dsi *dsi)
542 dsi_write(dsi, DSI_PWR_UP, RESET);
543 dsi_write(dsi, DSI_PHY_RSTZ, PHY_RSTZ);
546 static void dw_mipi_dsi_init(struct dw_mipi_dsi *dsi)
549 * The maximum permitted escape clock is 20MHz and it is derived from
550 * lanebyteclk, which is running at "lane_mbps / 8". Thus we want:
552 * (lane_mbps >> 3) / esc_clk_division < 20
554 * (lane_mbps >> 3) / 20 > esc_clk_division
556 u32 esc_clk_division = (dsi->lane_mbps >> 3) / 20 + 1;
558 dsi_write(dsi, DSI_PWR_UP, RESET);
561 * TODO dw drv improvements
562 * timeout clock division should be computed with the
563 * high speed transmission counter timeout and byte lane...
565 dsi_write(dsi, DSI_CLKMGR_CFG, TO_CLK_DIVISION(10) |
566 TX_ESC_CLK_DIVISION(esc_clk_division));
569 static void dw_mipi_dsi_dpi_config(struct dw_mipi_dsi *dsi,
570 const struct drm_display_mode *mode)
572 u32 val = 0, color = 0;
574 switch (dsi->format) {
575 case MIPI_DSI_FMT_RGB888:
576 color = DPI_COLOR_CODING_24BIT;
578 case MIPI_DSI_FMT_RGB666:
579 color = DPI_COLOR_CODING_18BIT_2 | LOOSELY18_EN;
581 case MIPI_DSI_FMT_RGB666_PACKED:
582 color = DPI_COLOR_CODING_18BIT_1;
584 case MIPI_DSI_FMT_RGB565:
585 color = DPI_COLOR_CODING_16BIT_1;
589 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
590 val |= VSYNC_ACTIVE_LOW;
591 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
592 val |= HSYNC_ACTIVE_LOW;
594 dsi_write(dsi, DSI_DPI_VCID, DPI_VCID(dsi->channel));
595 dsi_write(dsi, DSI_DPI_COLOR_CODING, color);
596 dsi_write(dsi, DSI_DPI_CFG_POL, val);
598 * TODO dw drv improvements
599 * largest packet sizes during hfp or during vsa/vpb/vfp
600 * should be computed according to byte lane, lane number and only
601 * if sending lp cmds in high speed is enable (PHY_TXREQUESTCLKHS)
603 dsi_write(dsi, DSI_DPI_LP_CMD_TIM, OUTVACT_LPCMD_TIME(4)
604 | INVACT_LPCMD_TIME(4));
607 static void dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi *dsi)
609 dsi_write(dsi, DSI_PCKHDL_CFG, CRC_RX_EN | ECC_RX_EN | BTA_EN);
612 static void dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi *dsi,
613 const struct drm_display_mode *mode)
616 * TODO dw drv improvements
617 * only burst mode is supported here. For non-burst video modes,
618 * we should compute DSI_VID_PKT_SIZE, DSI_VCCR.NUMC &
619 * DSI_VNPCR.NPSIZE... especially because this driver supports
620 * non-burst video modes, see dw_mipi_dsi_video_mode_config()...
623 dsi_write(dsi, DSI_VID_PKT_SIZE,
624 dw_mipi_is_dual_mode(dsi) ?
625 VID_PKT_SIZE(mode->hdisplay / 2) :
626 VID_PKT_SIZE(mode->hdisplay));
629 static void dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi *dsi)
632 * TODO dw drv improvements
633 * compute high speed transmission counter timeout according
634 * to the timeout clock division (TO_CLK_DIVISION) and byte lane...
636 dsi_write(dsi, DSI_TO_CNT_CFG, HSTX_TO_CNT(1000) | LPRX_TO_CNT(1000));
638 * TODO dw drv improvements
639 * the Bus-Turn-Around Timeout Counter should be computed
640 * according to byte lane...
642 dsi_write(dsi, DSI_BTA_TO_CNT, 0xd00);
643 dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
646 /* Get lane byte clock cycles. */
647 static u32 dw_mipi_dsi_get_hcomponent_lbcc(struct dw_mipi_dsi *dsi,
648 const struct drm_display_mode *mode,
653 lbcc = hcomponent * dsi->lane_mbps * MSEC_PER_SEC / 8;
655 frac = lbcc % mode->clock;
656 lbcc = lbcc / mode->clock;
663 static void dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi *dsi,
664 const struct drm_display_mode *mode)
666 u32 htotal, hsa, hbp, lbcc;
668 htotal = mode->htotal;
669 hsa = mode->hsync_end - mode->hsync_start;
670 hbp = mode->htotal - mode->hsync_end;
673 * TODO dw drv improvements
674 * computations below may be improved...
676 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, htotal);
677 dsi_write(dsi, DSI_VID_HLINE_TIME, lbcc);
679 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hsa);
680 dsi_write(dsi, DSI_VID_HSA_TIME, lbcc);
682 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hbp);
683 dsi_write(dsi, DSI_VID_HBP_TIME, lbcc);
686 static void dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi *dsi,
687 const struct drm_display_mode *mode)
689 u32 vactive, vsa, vfp, vbp;
691 vactive = mode->vdisplay;
692 vsa = mode->vsync_end - mode->vsync_start;
693 vfp = mode->vsync_start - mode->vdisplay;
694 vbp = mode->vtotal - mode->vsync_end;
696 dsi_write(dsi, DSI_VID_VACTIVE_LINES, vactive);
697 dsi_write(dsi, DSI_VID_VSA_LINES, vsa);
698 dsi_write(dsi, DSI_VID_VFP_LINES, vfp);
699 dsi_write(dsi, DSI_VID_VBP_LINES, vbp);
702 static void dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi *dsi)
707 * TODO dw drv improvements
708 * data & clock lane timers should be computed according to panel
709 * blankings and to the automatic clock lane control mode...
710 * note: DSI_PHY_TMR_CFG.MAX_RD_TIME should be in line with
711 * DSI_CMD_MODE_CFG.MAX_RD_PKT_SIZE_LP (see CMD_MODE_ALL_LP)
714 hw_version = dsi_read(dsi, DSI_VERSION) & VERSION;
716 if (hw_version >= HWVER_131) {
717 dsi_write(dsi, DSI_PHY_TMR_CFG, PHY_HS2LP_TIME_V131(0x40) |
718 PHY_LP2HS_TIME_V131(0x40));
719 dsi_write(dsi, DSI_PHY_TMR_RD_CFG, MAX_RD_TIME_V131(10000));
721 dsi_write(dsi, DSI_PHY_TMR_CFG, PHY_HS2LP_TIME(0x40) |
722 PHY_LP2HS_TIME(0x40) | MAX_RD_TIME(10000));
725 dsi_write(dsi, DSI_PHY_TMR_LPCLK_CFG, PHY_CLKHS2LP_TIME(0x40)
726 | PHY_CLKLP2HS_TIME(0x40));
729 static void dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi *dsi)
732 * TODO dw drv improvements
733 * stop wait time should be the maximum between host dsi
734 * and panel stop wait times
736 dsi_write(dsi, DSI_PHY_IF_CFG, PHY_STOP_WAIT_TIME(0x20) |
737 N_LANES(dsi->lanes));
740 static void dw_mipi_dsi_dphy_init(struct dw_mipi_dsi *dsi)
742 /* Clear PHY state */
743 dsi_write(dsi, DSI_PHY_RSTZ, PHY_DISFORCEPLL | PHY_DISABLECLK
744 | PHY_RSTZ | PHY_SHUTDOWNZ);
745 dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
746 dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_TESTCLR);
747 dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
750 static void dw_mipi_dsi_dphy_enable(struct dw_mipi_dsi *dsi)
755 dsi_write(dsi, DSI_PHY_RSTZ, PHY_ENFORCEPLL | PHY_ENABLECLK |
756 PHY_UNRSTZ | PHY_UNSHUTDOWNZ);
758 ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS, val,
759 val & PHY_LOCK, 1000, PHY_STATUS_TIMEOUT_US);
761 DRM_DEBUG_DRIVER("failed to wait phy lock state\n");
763 ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS,
764 val, val & PHY_STOP_STATE_CLK_LANE, 1000,
765 PHY_STATUS_TIMEOUT_US);
767 DRM_DEBUG_DRIVER("failed to wait phy clk lane stop state\n");
770 static void dw_mipi_dsi_clear_err(struct dw_mipi_dsi *dsi)
772 dsi_read(dsi, DSI_INT_ST0);
773 dsi_read(dsi, DSI_INT_ST1);
774 dsi_write(dsi, DSI_INT_MSK0, 0);
775 dsi_write(dsi, DSI_INT_MSK1, 0);
778 static void dw_mipi_dsi_bridge_post_disable(struct drm_bridge *bridge)
780 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
781 const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
783 if (phy_ops->power_off)
784 phy_ops->power_off(dsi->plat_data->priv_data);
787 * Switch to command mode before panel-bridge post_disable &
789 * Note: panel-bridge disable & panel disable has been called
790 * before by the drm framework.
792 dw_mipi_dsi_set_mode(dsi, 0);
795 * TODO Only way found to call panel-bridge post_disable &
796 * panel unprepare before the dsi "final" disable...
797 * This needs to be fixed in the drm_bridge framework and the API
798 * needs to be updated to manage our own call chains...
800 dsi->panel_bridge->funcs->post_disable(dsi->panel_bridge);
803 dw_mipi_dsi_disable(dsi->slave);
804 clk_disable_unprepare(dsi->slave->pclk);
805 pm_runtime_put(dsi->slave->dev);
807 dw_mipi_dsi_disable(dsi);
809 clk_disable_unprepare(dsi->pclk);
810 pm_runtime_put(dsi->dev);
813 static unsigned int dw_mipi_dsi_get_lanes(struct dw_mipi_dsi *dsi)
815 /* this instance is the slave, so add the master's lanes */
817 return dsi->master->lanes + dsi->lanes;
819 /* this instance is the master, so add the slave's lanes */
821 return dsi->lanes + dsi->slave->lanes;
823 /* single-dsi, so no other instance to consider */
827 static void dw_mipi_dsi_mode_set(struct dw_mipi_dsi *dsi,
828 const struct drm_display_mode *adjusted_mode)
830 const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
831 void *priv_data = dsi->plat_data->priv_data;
833 u32 lanes = dw_mipi_dsi_get_lanes(dsi);
835 clk_prepare_enable(dsi->pclk);
837 ret = phy_ops->get_lane_mbps(priv_data, adjusted_mode, dsi->mode_flags,
838 lanes, dsi->format, &dsi->lane_mbps);
840 DRM_DEBUG_DRIVER("Phy get_lane_mbps() failed\n");
842 pm_runtime_get_sync(dsi->dev);
843 dw_mipi_dsi_init(dsi);
844 dw_mipi_dsi_dpi_config(dsi, adjusted_mode);
845 dw_mipi_dsi_packet_handler_config(dsi);
846 dw_mipi_dsi_video_mode_config(dsi);
847 dw_mipi_dsi_video_packet_config(dsi, adjusted_mode);
848 dw_mipi_dsi_command_mode_config(dsi);
849 dw_mipi_dsi_line_timer_config(dsi, adjusted_mode);
850 dw_mipi_dsi_vertical_timing_config(dsi, adjusted_mode);
852 dw_mipi_dsi_dphy_init(dsi);
853 dw_mipi_dsi_dphy_timing_config(dsi);
854 dw_mipi_dsi_dphy_interface_config(dsi);
856 dw_mipi_dsi_clear_err(dsi);
858 ret = phy_ops->init(priv_data);
860 DRM_DEBUG_DRIVER("Phy init() failed\n");
862 dw_mipi_dsi_dphy_enable(dsi);
864 dw_mipi_dsi_wait_for_two_frames(adjusted_mode);
866 /* Switch to cmd mode for panel-bridge pre_enable & panel prepare */
867 dw_mipi_dsi_set_mode(dsi, 0);
870 static void dw_mipi_dsi_bridge_mode_set(struct drm_bridge *bridge,
871 const struct drm_display_mode *mode,
872 const struct drm_display_mode *adjusted_mode)
874 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
876 dw_mipi_dsi_mode_set(dsi, adjusted_mode);
878 dw_mipi_dsi_mode_set(dsi->slave, adjusted_mode);
881 static void dw_mipi_dsi_bridge_enable(struct drm_bridge *bridge)
883 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
884 const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
886 /* Switch to video mode for panel-bridge enable & panel enable */
887 dw_mipi_dsi_set_mode(dsi, MIPI_DSI_MODE_VIDEO);
889 dw_mipi_dsi_set_mode(dsi->slave, MIPI_DSI_MODE_VIDEO);
891 if (phy_ops->power_on)
892 phy_ops->power_on(dsi->plat_data->priv_data);
895 static enum drm_mode_status
896 dw_mipi_dsi_bridge_mode_valid(struct drm_bridge *bridge,
897 const struct drm_display_mode *mode)
899 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
900 const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
901 enum drm_mode_status mode_status = MODE_OK;
903 if (pdata->mode_valid)
904 mode_status = pdata->mode_valid(pdata->priv_data, mode);
909 static int dw_mipi_dsi_bridge_attach(struct drm_bridge *bridge)
911 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
913 if (!bridge->encoder) {
914 DRM_ERROR("Parent encoder object not found\n");
918 /* Set the encoder type as caller does not know it */
919 bridge->encoder->encoder_type = DRM_MODE_ENCODER_DSI;
921 /* Attach the panel-bridge to the dsi bridge */
922 return drm_bridge_attach(bridge->encoder, dsi->panel_bridge, bridge);
925 static const struct drm_bridge_funcs dw_mipi_dsi_bridge_funcs = {
926 .mode_set = dw_mipi_dsi_bridge_mode_set,
927 .enable = dw_mipi_dsi_bridge_enable,
928 .post_disable = dw_mipi_dsi_bridge_post_disable,
929 .mode_valid = dw_mipi_dsi_bridge_mode_valid,
930 .attach = dw_mipi_dsi_bridge_attach,
933 static struct dw_mipi_dsi *
934 __dw_mipi_dsi_probe(struct platform_device *pdev,
935 const struct dw_mipi_dsi_plat_data *plat_data)
937 struct device *dev = &pdev->dev;
938 struct reset_control *apb_rst;
939 struct dw_mipi_dsi *dsi;
940 struct resource *res;
943 dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
945 return ERR_PTR(-ENOMEM);
948 dsi->plat_data = plat_data;
950 if (!plat_data->phy_ops->init || !plat_data->phy_ops->get_lane_mbps) {
951 DRM_ERROR("Phy not properly configured\n");
952 return ERR_PTR(-ENODEV);
955 if (!plat_data->base) {
956 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
958 return ERR_PTR(-ENODEV);
960 dsi->base = devm_ioremap_resource(dev, res);
961 if (IS_ERR(dsi->base))
962 return ERR_PTR(-ENODEV);
965 dsi->base = plat_data->base;
968 dsi->pclk = devm_clk_get(dev, "pclk");
969 if (IS_ERR(dsi->pclk)) {
970 ret = PTR_ERR(dsi->pclk);
971 dev_err(dev, "Unable to get pclk: %d\n", ret);
976 * Note that the reset was not defined in the initial device tree, so
977 * we have to be prepared for it not being found.
979 apb_rst = devm_reset_control_get_optional_exclusive(dev, "apb");
980 if (IS_ERR(apb_rst)) {
981 ret = PTR_ERR(apb_rst);
983 if (ret != -EPROBE_DEFER)
984 dev_err(dev, "Unable to get reset control: %d\n", ret);
990 ret = clk_prepare_enable(dsi->pclk);
992 dev_err(dev, "%s: Failed to enable pclk\n", __func__);
996 reset_control_assert(apb_rst);
997 usleep_range(10, 20);
998 reset_control_deassert(apb_rst);
1000 clk_disable_unprepare(dsi->pclk);
1003 pm_runtime_enable(dev);
1005 dsi->dsi_host.ops = &dw_mipi_dsi_host_ops;
1006 dsi->dsi_host.dev = dev;
1007 ret = mipi_dsi_host_register(&dsi->dsi_host);
1009 dev_err(dev, "Failed to register MIPI host: %d\n", ret);
1010 return ERR_PTR(ret);
1013 dsi->bridge.driver_private = dsi;
1014 dsi->bridge.funcs = &dw_mipi_dsi_bridge_funcs;
1016 dsi->bridge.of_node = pdev->dev.of_node;
1022 static void __dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
1024 mipi_dsi_host_unregister(&dsi->dsi_host);
1026 pm_runtime_disable(dsi->dev);
1029 void dw_mipi_dsi_set_slave(struct dw_mipi_dsi *dsi, struct dw_mipi_dsi *slave)
1031 /* introduce controllers to each other */
1033 dsi->slave->master = dsi;
1035 /* migrate settings for already attached displays */
1036 dsi->slave->lanes = dsi->lanes;
1037 dsi->slave->channel = dsi->channel;
1038 dsi->slave->format = dsi->format;
1039 dsi->slave->mode_flags = dsi->mode_flags;
1041 EXPORT_SYMBOL_GPL(dw_mipi_dsi_set_slave);
1044 * Probe/remove API, used from platforms based on the DRM bridge API.
1046 struct dw_mipi_dsi *
1047 dw_mipi_dsi_probe(struct platform_device *pdev,
1048 const struct dw_mipi_dsi_plat_data *plat_data)
1050 return __dw_mipi_dsi_probe(pdev, plat_data);
1052 EXPORT_SYMBOL_GPL(dw_mipi_dsi_probe);
1054 void dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
1056 __dw_mipi_dsi_remove(dsi);
1058 EXPORT_SYMBOL_GPL(dw_mipi_dsi_remove);
1061 * Bind/unbind API, used from platforms based on the component framework.
1063 int dw_mipi_dsi_bind(struct dw_mipi_dsi *dsi, struct drm_encoder *encoder)
1067 ret = drm_bridge_attach(encoder, &dsi->bridge, NULL);
1069 DRM_ERROR("Failed to initialize bridge with drm\n");
1075 EXPORT_SYMBOL_GPL(dw_mipi_dsi_bind);
1077 void dw_mipi_dsi_unbind(struct dw_mipi_dsi *dsi)
1080 EXPORT_SYMBOL_GPL(dw_mipi_dsi_unbind);
1082 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
1083 MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
1084 MODULE_DESCRIPTION("DW MIPI DSI host controller driver");
1085 MODULE_LICENSE("GPL");
1086 MODULE_ALIAS("platform:dw-mipi-dsi");