1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd 4 * Copyright (C) STMicroelectronics SA 2017 5 * 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. 9 */ 10 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> 19 20 #include <video/mipi_display.h> 21 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> 31 32 #define HWVER_131 0x31333100 /* IP version 1.31 */ 33 34 #define DSI_VERSION 0x00 35 #define VERSION GENMASK(31, 8) 36 37 #define DSI_PWR_UP 0x04 38 #define RESET 0 39 #define POWERUP BIT(0) 40 41 #define DSI_CLKMGR_CFG 0x08 42 #define TO_CLK_DIVISION(div) (((div) & 0xff) << 8) 43 #define TX_ESC_CLK_DIVISION(div) ((div) & 0xff) 44 45 #define DSI_DPI_VCID 0x0c 46 #define DPI_VCID(vcid) ((vcid) & 0x3) 47 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 56 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) 63 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) 67 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 72 73 #define DSI_PCKHDL_CFG 0x2c 74 #define CRC_RX_EN BIT(4) 75 #define ECC_RX_EN BIT(3) 76 #define BTA_EN BIT(2) 77 #define EOTP_RX_EN BIT(1) 78 #define EOTP_TX_EN BIT(0) 79 80 #define DSI_GEN_VCID 0x30 81 82 #define DSI_MODE_CFG 0x34 83 #define ENABLE_VIDEO_MODE 0 84 #define ENABLE_CMD_MODE BIT(0) 85 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) 95 96 #define DSI_VID_PKT_SIZE 0x3c 97 #define VID_PKT_SIZE(p) ((p) & 0x3fff) 98 99 #define DSI_VID_NUM_CHUNKS 0x40 100 #define VID_NUM_CHUNKS(c) ((c) & 0x1fff) 101 102 #define DSI_VID_NULL_SIZE 0x44 103 #define VID_NULL_SIZE(b) ((b) & 0x1fff) 104 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 113 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) 129 130 #define CMD_MODE_ALL_LP (MAX_RD_PKT_SIZE_LP | \ 131 DCS_LW_TX_LP | \ 132 DCS_SR_0P_TX_LP | \ 133 DCS_SW_1P_TX_LP | \ 134 DCS_SW_0P_TX_LP | \ 135 GEN_LW_TX_LP | \ 136 GEN_SR_2P_TX_LP | \ 137 GEN_SR_1P_TX_LP | \ 138 GEN_SR_0P_TX_LP | \ 139 GEN_SW_2P_TX_LP | \ 140 GEN_SW_1P_TX_LP | \ 141 GEN_SW_0P_TX_LP) 142 143 #define DSI_GEN_HDR 0x6c 144 #define DSI_GEN_PLD_DATA 0x70 145 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) 154 155 #define DSI_TO_CNT_CFG 0x78 156 #define HSTX_TO_CNT(p) (((p) & 0xffff) << 16) 157 #define LPRX_TO_CNT(p) ((p) & 0xffff) 158 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 164 165 #define DSI_LPCLK_CTRL 0x94 166 #define AUTO_CLKLANE_CTRL BIT(1) 167 #define PHY_TXREQUESTCLKHS BIT(0) 168 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) 172 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) 179 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) 185 #define PHY_RSTZ 0 186 #define PHY_UNRSTZ BIT(1) 187 #define PHY_SHUTDOWNZ 0 188 #define PHY_UNSHUTDOWNZ BIT(0) 189 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) 193 194 #define DSI_PHY_ULPS_CTRL 0xa8 195 #define DSI_PHY_TX_TRIGGERS 0xac 196 197 #define DSI_PHY_STATUS 0xb0 198 #define PHY_STOP_STATE_CLK_LANE BIT(2) 199 #define PHY_LOCK BIT(0) 200 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 206 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) 212 213 #define DSI_INT_ST0 0xbc 214 #define DSI_INT_ST1 0xc0 215 #define DSI_INT_MSK0 0xc4 216 #define DSI_INT_MSK1 0xc8 217 218 #define DSI_PHY_TMR_RD_CFG 0xf4 219 #define MAX_RD_TIME_V131(lbcc) ((lbcc) & 0x7fff) 220 221 #define PHY_STATUS_TIMEOUT_US 10000 222 #define CMD_PKT_STATUS_TIMEOUT_US 20000 223 224 struct dw_mipi_dsi { 225 struct drm_bridge bridge; 226 struct mipi_dsi_host dsi_host; 227 struct drm_bridge *panel_bridge; 228 struct device *dev; 229 void __iomem *base; 230 231 struct clk *pclk; 232 233 unsigned int lane_mbps; /* per lane */ 234 u32 channel; 235 u32 lanes; 236 u32 format; 237 unsigned long mode_flags; 238 239 #ifdef CONFIG_DEBUG_FS 240 struct dentry *debugfs; 241 242 bool vpg; 243 bool vpg_horizontal; 244 #endif /* CONFIG_DEBUG_FS */ 245 246 struct dw_mipi_dsi *master; /* dual-dsi master ptr */ 247 struct dw_mipi_dsi *slave; /* dual-dsi slave ptr */ 248 249 const struct dw_mipi_dsi_plat_data *plat_data; 250 }; 251 252 /* 253 * Check if either a link to a master or slave is present 254 */ 255 static inline bool dw_mipi_is_dual_mode(struct dw_mipi_dsi *dsi) 256 { 257 return dsi->slave || dsi->master; 258 } 259 260 /* 261 * The controller should generate 2 frames before 262 * preparing the peripheral. 263 */ 264 static void dw_mipi_dsi_wait_for_two_frames(const struct drm_display_mode *mode) 265 { 266 int refresh, two_frames; 267 268 refresh = drm_mode_vrefresh(mode); 269 two_frames = DIV_ROUND_UP(MSEC_PER_SEC, refresh) * 2; 270 msleep(two_frames); 271 } 272 273 static inline struct dw_mipi_dsi *host_to_dsi(struct mipi_dsi_host *host) 274 { 275 return container_of(host, struct dw_mipi_dsi, dsi_host); 276 } 277 278 static inline struct dw_mipi_dsi *bridge_to_dsi(struct drm_bridge *bridge) 279 { 280 return container_of(bridge, struct dw_mipi_dsi, bridge); 281 } 282 283 static inline void dsi_write(struct dw_mipi_dsi *dsi, u32 reg, u32 val) 284 { 285 writel(val, dsi->base + reg); 286 } 287 288 static inline u32 dsi_read(struct dw_mipi_dsi *dsi, u32 reg) 289 { 290 return readl(dsi->base + reg); 291 } 292 293 static int dw_mipi_dsi_host_attach(struct mipi_dsi_host *host, 294 struct mipi_dsi_device *device) 295 { 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; 300 int ret; 301 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", 304 device->lanes); 305 return -EINVAL; 306 } 307 308 dsi->lanes = device->lanes; 309 dsi->channel = device->channel; 310 dsi->format = device->format; 311 dsi->mode_flags = device->mode_flags; 312 313 ret = drm_of_find_panel_or_bridge(host->dev->of_node, 1, 0, 314 &panel, &bridge); 315 if (ret) 316 return ret; 317 318 if (panel) { 319 bridge = drm_panel_bridge_add_typed(panel, 320 DRM_MODE_CONNECTOR_DSI); 321 if (IS_ERR(bridge)) 322 return PTR_ERR(bridge); 323 } 324 325 dsi->panel_bridge = bridge; 326 327 drm_bridge_add(&dsi->bridge); 328 329 if (pdata->host_ops && pdata->host_ops->attach) { 330 ret = pdata->host_ops->attach(pdata->priv_data, device); 331 if (ret < 0) 332 return ret; 333 } 334 335 return 0; 336 } 337 338 static int dw_mipi_dsi_host_detach(struct mipi_dsi_host *host, 339 struct mipi_dsi_device *device) 340 { 341 struct dw_mipi_dsi *dsi = host_to_dsi(host); 342 const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data; 343 int ret; 344 345 if (pdata->host_ops && pdata->host_ops->detach) { 346 ret = pdata->host_ops->detach(pdata->priv_data, device); 347 if (ret < 0) 348 return ret; 349 } 350 351 drm_of_panel_bridge_remove(host->dev->of_node, 1, 0); 352 353 drm_bridge_remove(&dsi->bridge); 354 355 return 0; 356 } 357 358 static void dw_mipi_message_config(struct dw_mipi_dsi *dsi, 359 const struct mipi_dsi_msg *msg) 360 { 361 bool lpm = msg->flags & MIPI_DSI_MSG_USE_LPM; 362 u32 val = 0; 363 364 if (msg->flags & MIPI_DSI_MSG_REQ_ACK) 365 val |= ACK_RQST_EN; 366 if (lpm) 367 val |= CMD_MODE_ALL_LP; 368 369 dsi_write(dsi, DSI_LPCLK_CTRL, lpm ? 0 : PHY_TXREQUESTCLKHS); 370 dsi_write(dsi, DSI_CMD_MODE_CFG, val); 371 } 372 373 static int dw_mipi_dsi_gen_pkt_hdr_write(struct dw_mipi_dsi *dsi, u32 hdr_val) 374 { 375 int ret; 376 u32 val, mask; 377 378 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS, 379 val, !(val & GEN_CMD_FULL), 1000, 380 CMD_PKT_STATUS_TIMEOUT_US); 381 if (ret) { 382 dev_err(dsi->dev, "failed to get available command FIFO\n"); 383 return ret; 384 } 385 386 dsi_write(dsi, DSI_GEN_HDR, hdr_val); 387 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); 392 if (ret) { 393 dev_err(dsi->dev, "failed to write command FIFO\n"); 394 return ret; 395 } 396 397 return 0; 398 } 399 400 static int dw_mipi_dsi_write(struct dw_mipi_dsi *dsi, 401 const struct mipi_dsi_packet *packet) 402 { 403 const u8 *tx_buf = packet->payload; 404 int len = packet->payload_length, pld_data_bytes = sizeof(u32), ret; 405 __le32 word; 406 u32 val; 407 408 while (len) { 409 if (len < pld_data_bytes) { 410 word = 0; 411 memcpy(&word, tx_buf, len); 412 dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word)); 413 len = 0; 414 } else { 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; 419 } 420 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); 424 if (ret) { 425 dev_err(dsi->dev, 426 "failed to get available write payload FIFO\n"); 427 return ret; 428 } 429 } 430 431 word = 0; 432 memcpy(&word, packet->header, sizeof(packet->header)); 433 return dw_mipi_dsi_gen_pkt_hdr_write(dsi, le32_to_cpu(word)); 434 } 435 436 static int dw_mipi_dsi_read(struct dw_mipi_dsi *dsi, 437 const struct mipi_dsi_msg *msg) 438 { 439 int i, j, ret, len = msg->rx_len; 440 u8 *buf = msg->rx_buf; 441 u32 val; 442 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); 447 if (ret) { 448 dev_err(dsi->dev, "Timeout during read operation\n"); 449 return ret; 450 } 451 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); 457 if (ret) { 458 dev_err(dsi->dev, "Read payload FIFO is empty\n"); 459 return ret; 460 } 461 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); 465 } 466 467 return ret; 468 } 469 470 static ssize_t dw_mipi_dsi_host_transfer(struct mipi_dsi_host *host, 471 const struct mipi_dsi_msg *msg) 472 { 473 struct dw_mipi_dsi *dsi = host_to_dsi(host); 474 struct mipi_dsi_packet packet; 475 int ret, nb_bytes; 476 477 ret = mipi_dsi_create_packet(&packet, msg); 478 if (ret) { 479 dev_err(dsi->dev, "failed to create packet: %d\n", ret); 480 return ret; 481 } 482 483 dw_mipi_message_config(dsi, msg); 484 if (dsi->slave) 485 dw_mipi_message_config(dsi->slave, msg); 486 487 ret = dw_mipi_dsi_write(dsi, &packet); 488 if (ret) 489 return ret; 490 if (dsi->slave) { 491 ret = dw_mipi_dsi_write(dsi->slave, &packet); 492 if (ret) 493 return ret; 494 } 495 496 if (msg->rx_buf && msg->rx_len) { 497 ret = dw_mipi_dsi_read(dsi, msg); 498 if (ret) 499 return ret; 500 nb_bytes = msg->rx_len; 501 } else { 502 nb_bytes = packet.size; 503 } 504 505 return nb_bytes; 506 } 507 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, 512 }; 513 514 static void dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi *dsi) 515 { 516 u32 val; 517 518 /* 519 * TODO dw drv improvements 520 * enabling low power is panel-dependent, we should use the 521 * panel configuration here... 522 */ 523 val = ENABLE_LOW_POWER; 524 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; 529 else 530 val |= VID_MODE_TYPE_NON_BURST_SYNC_EVENTS; 531 532 #ifdef CONFIG_DEBUG_FS 533 if (dsi->vpg) { 534 val |= VID_MODE_VPG_ENABLE; 535 val |= dsi->vpg_horizontal ? VID_MODE_VPG_HORIZONTAL : 0; 536 } 537 #endif /* CONFIG_DEBUG_FS */ 538 539 dsi_write(dsi, DSI_VID_MODE_CFG, val); 540 } 541 542 static void dw_mipi_dsi_set_mode(struct dw_mipi_dsi *dsi, 543 unsigned long mode_flags) 544 { 545 dsi_write(dsi, DSI_PWR_UP, RESET); 546 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); 551 } else { 552 dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE); 553 } 554 555 dsi_write(dsi, DSI_PWR_UP, POWERUP); 556 } 557 558 static void dw_mipi_dsi_disable(struct dw_mipi_dsi *dsi) 559 { 560 dsi_write(dsi, DSI_PWR_UP, RESET); 561 dsi_write(dsi, DSI_PHY_RSTZ, PHY_RSTZ); 562 } 563 564 static void dw_mipi_dsi_init(struct dw_mipi_dsi *dsi) 565 { 566 /* 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: 569 * 570 * (lane_mbps >> 3) / esc_clk_division < 20 571 * which is: 572 * (lane_mbps >> 3) / 20 > esc_clk_division 573 */ 574 u32 esc_clk_division = (dsi->lane_mbps >> 3) / 20 + 1; 575 576 dsi_write(dsi, DSI_PWR_UP, RESET); 577 578 /* 579 * TODO dw drv improvements 580 * timeout clock division should be computed with the 581 * high speed transmission counter timeout and byte lane... 582 */ 583 dsi_write(dsi, DSI_CLKMGR_CFG, TO_CLK_DIVISION(10) | 584 TX_ESC_CLK_DIVISION(esc_clk_division)); 585 } 586 587 static void dw_mipi_dsi_dpi_config(struct dw_mipi_dsi *dsi, 588 const struct drm_display_mode *mode) 589 { 590 u32 val = 0, color = 0; 591 592 switch (dsi->format) { 593 case MIPI_DSI_FMT_RGB888: 594 color = DPI_COLOR_CODING_24BIT; 595 break; 596 case MIPI_DSI_FMT_RGB666: 597 color = DPI_COLOR_CODING_18BIT_2 | LOOSELY18_EN; 598 break; 599 case MIPI_DSI_FMT_RGB666_PACKED: 600 color = DPI_COLOR_CODING_18BIT_1; 601 break; 602 case MIPI_DSI_FMT_RGB565: 603 color = DPI_COLOR_CODING_16BIT_1; 604 break; 605 } 606 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; 611 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); 615 /* 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) 620 */ 621 dsi_write(dsi, DSI_DPI_LP_CMD_TIM, OUTVACT_LPCMD_TIME(4) 622 | INVACT_LPCMD_TIME(4)); 623 } 624 625 static void dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi *dsi) 626 { 627 dsi_write(dsi, DSI_PCKHDL_CFG, CRC_RX_EN | ECC_RX_EN | BTA_EN); 628 } 629 630 static void dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi *dsi, 631 const struct drm_display_mode *mode) 632 { 633 /* 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()... 639 */ 640 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)); 645 } 646 647 static void dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi *dsi) 648 { 649 /* 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... 653 */ 654 dsi_write(dsi, DSI_TO_CNT_CFG, HSTX_TO_CNT(1000) | LPRX_TO_CNT(1000)); 655 /* 656 * TODO dw drv improvements 657 * the Bus-Turn-Around Timeout Counter should be computed 658 * according to byte lane... 659 */ 660 dsi_write(dsi, DSI_BTA_TO_CNT, 0xd00); 661 dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE); 662 } 663 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, 667 u32 hcomponent) 668 { 669 u32 frac, lbcc; 670 671 lbcc = hcomponent * dsi->lane_mbps * MSEC_PER_SEC / 8; 672 673 frac = lbcc % mode->clock; 674 lbcc = lbcc / mode->clock; 675 if (frac) 676 lbcc++; 677 678 return lbcc; 679 } 680 681 static void dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi *dsi, 682 const struct drm_display_mode *mode) 683 { 684 u32 htotal, hsa, hbp, lbcc; 685 686 htotal = mode->htotal; 687 hsa = mode->hsync_end - mode->hsync_start; 688 hbp = mode->htotal - mode->hsync_end; 689 690 /* 691 * TODO dw drv improvements 692 * computations below may be improved... 693 */ 694 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, htotal); 695 dsi_write(dsi, DSI_VID_HLINE_TIME, lbcc); 696 697 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hsa); 698 dsi_write(dsi, DSI_VID_HSA_TIME, lbcc); 699 700 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hbp); 701 dsi_write(dsi, DSI_VID_HBP_TIME, lbcc); 702 } 703 704 static void dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi *dsi, 705 const struct drm_display_mode *mode) 706 { 707 u32 vactive, vsa, vfp, vbp; 708 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; 713 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); 718 } 719 720 static void dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi *dsi) 721 { 722 const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops; 723 struct dw_mipi_dsi_dphy_timing timing; 724 u32 hw_version; 725 int ret; 726 727 ret = phy_ops->get_timing(dsi->plat_data->priv_data, 728 dsi->lane_mbps, &timing); 729 if (ret) 730 DRM_DEV_ERROR(dsi->dev, "Retrieving phy timings failed\n"); 731 732 /* 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) 738 */ 739 740 hw_version = dsi_read(dsi, DSI_VERSION) & VERSION; 741 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)); 747 } else { 748 dsi_write(dsi, DSI_PHY_TMR_CFG, 749 PHY_HS2LP_TIME(timing.data_hs2lp) | 750 PHY_LP2HS_TIME(timing.data_lp2hs) | 751 MAX_RD_TIME(10000)); 752 } 753 754 dsi_write(dsi, DSI_PHY_TMR_LPCLK_CFG, 755 PHY_CLKHS2LP_TIME(timing.clk_hs2lp) | 756 PHY_CLKLP2HS_TIME(timing.clk_lp2hs)); 757 } 758 759 static void dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi *dsi) 760 { 761 /* 762 * TODO dw drv improvements 763 * stop wait time should be the maximum between host dsi 764 * and panel stop wait times 765 */ 766 dsi_write(dsi, DSI_PHY_IF_CFG, PHY_STOP_WAIT_TIME(0x20) | 767 N_LANES(dsi->lanes)); 768 } 769 770 static void dw_mipi_dsi_dphy_init(struct dw_mipi_dsi *dsi) 771 { 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); 778 } 779 780 static void dw_mipi_dsi_dphy_enable(struct dw_mipi_dsi *dsi) 781 { 782 u32 val; 783 int ret; 784 785 dsi_write(dsi, DSI_PHY_RSTZ, PHY_ENFORCEPLL | PHY_ENABLECLK | 786 PHY_UNRSTZ | PHY_UNSHUTDOWNZ); 787 788 ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS, val, 789 val & PHY_LOCK, 1000, PHY_STATUS_TIMEOUT_US); 790 if (ret) 791 DRM_DEBUG_DRIVER("failed to wait phy lock state\n"); 792 793 ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS, 794 val, val & PHY_STOP_STATE_CLK_LANE, 1000, 795 PHY_STATUS_TIMEOUT_US); 796 if (ret) 797 DRM_DEBUG_DRIVER("failed to wait phy clk lane stop state\n"); 798 } 799 800 static void dw_mipi_dsi_clear_err(struct dw_mipi_dsi *dsi) 801 { 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); 806 } 807 808 static void dw_mipi_dsi_bridge_post_disable(struct drm_bridge *bridge) 809 { 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; 812 813 /* 814 * Switch to command mode before panel-bridge post_disable & 815 * panel unprepare. 816 * Note: panel-bridge disable & panel disable has been called 817 * before by the drm framework. 818 */ 819 dw_mipi_dsi_set_mode(dsi, 0); 820 821 /* 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... 826 */ 827 if (dsi->panel_bridge->funcs->post_disable) 828 dsi->panel_bridge->funcs->post_disable(dsi->panel_bridge); 829 830 if (phy_ops->power_off) 831 phy_ops->power_off(dsi->plat_data->priv_data); 832 833 if (dsi->slave) { 834 dw_mipi_dsi_disable(dsi->slave); 835 clk_disable_unprepare(dsi->slave->pclk); 836 pm_runtime_put(dsi->slave->dev); 837 } 838 dw_mipi_dsi_disable(dsi); 839 840 clk_disable_unprepare(dsi->pclk); 841 pm_runtime_put(dsi->dev); 842 } 843 844 static unsigned int dw_mipi_dsi_get_lanes(struct dw_mipi_dsi *dsi) 845 { 846 /* this instance is the slave, so add the master's lanes */ 847 if (dsi->master) 848 return dsi->master->lanes + dsi->lanes; 849 850 /* this instance is the master, so add the slave's lanes */ 851 if (dsi->slave) 852 return dsi->lanes + dsi->slave->lanes; 853 854 /* single-dsi, so no other instance to consider */ 855 return dsi->lanes; 856 } 857 858 static void dw_mipi_dsi_mode_set(struct dw_mipi_dsi *dsi, 859 const struct drm_display_mode *adjusted_mode) 860 { 861 const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops; 862 void *priv_data = dsi->plat_data->priv_data; 863 int ret; 864 u32 lanes = dw_mipi_dsi_get_lanes(dsi); 865 866 clk_prepare_enable(dsi->pclk); 867 868 ret = phy_ops->get_lane_mbps(priv_data, adjusted_mode, dsi->mode_flags, 869 lanes, dsi->format, &dsi->lane_mbps); 870 if (ret) 871 DRM_DEBUG_DRIVER("Phy get_lane_mbps() failed\n"); 872 873 pm_runtime_get_sync(dsi->dev); 874 dw_mipi_dsi_init(dsi); 875 dw_mipi_dsi_dpi_config(dsi, adjusted_mode); 876 dw_mipi_dsi_packet_handler_config(dsi); 877 dw_mipi_dsi_video_mode_config(dsi); 878 dw_mipi_dsi_video_packet_config(dsi, adjusted_mode); 879 dw_mipi_dsi_command_mode_config(dsi); 880 dw_mipi_dsi_line_timer_config(dsi, adjusted_mode); 881 dw_mipi_dsi_vertical_timing_config(dsi, adjusted_mode); 882 883 dw_mipi_dsi_dphy_init(dsi); 884 dw_mipi_dsi_dphy_timing_config(dsi); 885 dw_mipi_dsi_dphy_interface_config(dsi); 886 887 dw_mipi_dsi_clear_err(dsi); 888 889 ret = phy_ops->init(priv_data); 890 if (ret) 891 DRM_DEBUG_DRIVER("Phy init() failed\n"); 892 893 dw_mipi_dsi_dphy_enable(dsi); 894 895 dw_mipi_dsi_wait_for_two_frames(adjusted_mode); 896 897 /* Switch to cmd mode for panel-bridge pre_enable & panel prepare */ 898 dw_mipi_dsi_set_mode(dsi, 0); 899 900 if (phy_ops->power_on) 901 phy_ops->power_on(dsi->plat_data->priv_data); 902 } 903 904 static void dw_mipi_dsi_bridge_mode_set(struct drm_bridge *bridge, 905 const struct drm_display_mode *mode, 906 const struct drm_display_mode *adjusted_mode) 907 { 908 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge); 909 910 dw_mipi_dsi_mode_set(dsi, adjusted_mode); 911 if (dsi->slave) 912 dw_mipi_dsi_mode_set(dsi->slave, adjusted_mode); 913 } 914 915 static void dw_mipi_dsi_bridge_enable(struct drm_bridge *bridge) 916 { 917 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge); 918 919 /* Switch to video mode for panel-bridge enable & panel enable */ 920 dw_mipi_dsi_set_mode(dsi, MIPI_DSI_MODE_VIDEO); 921 if (dsi->slave) 922 dw_mipi_dsi_set_mode(dsi->slave, MIPI_DSI_MODE_VIDEO); 923 } 924 925 static enum drm_mode_status 926 dw_mipi_dsi_bridge_mode_valid(struct drm_bridge *bridge, 927 const struct drm_display_mode *mode) 928 { 929 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge); 930 const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data; 931 enum drm_mode_status mode_status = MODE_OK; 932 933 if (pdata->mode_valid) 934 mode_status = pdata->mode_valid(pdata->priv_data, mode); 935 936 return mode_status; 937 } 938 939 static int dw_mipi_dsi_bridge_attach(struct drm_bridge *bridge, 940 enum drm_bridge_attach_flags flags) 941 { 942 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge); 943 944 if (!bridge->encoder) { 945 DRM_ERROR("Parent encoder object not found\n"); 946 return -ENODEV; 947 } 948 949 /* Set the encoder type as caller does not know it */ 950 bridge->encoder->encoder_type = DRM_MODE_ENCODER_DSI; 951 952 /* Attach the panel-bridge to the dsi bridge */ 953 return drm_bridge_attach(bridge->encoder, dsi->panel_bridge, bridge, 954 flags); 955 } 956 957 static const struct drm_bridge_funcs dw_mipi_dsi_bridge_funcs = { 958 .mode_set = dw_mipi_dsi_bridge_mode_set, 959 .enable = dw_mipi_dsi_bridge_enable, 960 .post_disable = dw_mipi_dsi_bridge_post_disable, 961 .mode_valid = dw_mipi_dsi_bridge_mode_valid, 962 .attach = dw_mipi_dsi_bridge_attach, 963 }; 964 965 #ifdef CONFIG_DEBUG_FS 966 967 static void dw_mipi_dsi_debugfs_init(struct dw_mipi_dsi *dsi) 968 { 969 dsi->debugfs = debugfs_create_dir(dev_name(dsi->dev), NULL); 970 if (IS_ERR(dsi->debugfs)) { 971 dev_err(dsi->dev, "failed to create debugfs root\n"); 972 return; 973 } 974 975 debugfs_create_bool("vpg", 0660, dsi->debugfs, &dsi->vpg); 976 debugfs_create_bool("vpg_horizontal", 0660, dsi->debugfs, 977 &dsi->vpg_horizontal); 978 } 979 980 static void dw_mipi_dsi_debugfs_remove(struct dw_mipi_dsi *dsi) 981 { 982 debugfs_remove_recursive(dsi->debugfs); 983 } 984 985 #else 986 987 static void dw_mipi_dsi_debugfs_init(struct dw_mipi_dsi *dsi) { } 988 static void dw_mipi_dsi_debugfs_remove(struct dw_mipi_dsi *dsi) { } 989 990 #endif /* CONFIG_DEBUG_FS */ 991 992 static struct dw_mipi_dsi * 993 __dw_mipi_dsi_probe(struct platform_device *pdev, 994 const struct dw_mipi_dsi_plat_data *plat_data) 995 { 996 struct device *dev = &pdev->dev; 997 struct reset_control *apb_rst; 998 struct dw_mipi_dsi *dsi; 999 int ret; 1000 1001 dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL); 1002 if (!dsi) 1003 return ERR_PTR(-ENOMEM); 1004 1005 dsi->dev = dev; 1006 dsi->plat_data = plat_data; 1007 1008 if (!plat_data->phy_ops->init || !plat_data->phy_ops->get_lane_mbps || 1009 !plat_data->phy_ops->get_timing) { 1010 DRM_ERROR("Phy not properly configured\n"); 1011 return ERR_PTR(-ENODEV); 1012 } 1013 1014 if (!plat_data->base) { 1015 dsi->base = devm_platform_ioremap_resource(pdev, 0); 1016 if (IS_ERR(dsi->base)) 1017 return ERR_PTR(-ENODEV); 1018 1019 } else { 1020 dsi->base = plat_data->base; 1021 } 1022 1023 dsi->pclk = devm_clk_get(dev, "pclk"); 1024 if (IS_ERR(dsi->pclk)) { 1025 ret = PTR_ERR(dsi->pclk); 1026 dev_err(dev, "Unable to get pclk: %d\n", ret); 1027 return ERR_PTR(ret); 1028 } 1029 1030 /* 1031 * Note that the reset was not defined in the initial device tree, so 1032 * we have to be prepared for it not being found. 1033 */ 1034 apb_rst = devm_reset_control_get_optional_exclusive(dev, "apb"); 1035 if (IS_ERR(apb_rst)) { 1036 ret = PTR_ERR(apb_rst); 1037 1038 if (ret != -EPROBE_DEFER) 1039 dev_err(dev, "Unable to get reset control: %d\n", ret); 1040 1041 return ERR_PTR(ret); 1042 } 1043 1044 if (apb_rst) { 1045 ret = clk_prepare_enable(dsi->pclk); 1046 if (ret) { 1047 dev_err(dev, "%s: Failed to enable pclk\n", __func__); 1048 return ERR_PTR(ret); 1049 } 1050 1051 reset_control_assert(apb_rst); 1052 usleep_range(10, 20); 1053 reset_control_deassert(apb_rst); 1054 1055 clk_disable_unprepare(dsi->pclk); 1056 } 1057 1058 dw_mipi_dsi_debugfs_init(dsi); 1059 pm_runtime_enable(dev); 1060 1061 dsi->dsi_host.ops = &dw_mipi_dsi_host_ops; 1062 dsi->dsi_host.dev = dev; 1063 ret = mipi_dsi_host_register(&dsi->dsi_host); 1064 if (ret) { 1065 dev_err(dev, "Failed to register MIPI host: %d\n", ret); 1066 dw_mipi_dsi_debugfs_remove(dsi); 1067 return ERR_PTR(ret); 1068 } 1069 1070 dsi->bridge.driver_private = dsi; 1071 dsi->bridge.funcs = &dw_mipi_dsi_bridge_funcs; 1072 #ifdef CONFIG_OF 1073 dsi->bridge.of_node = pdev->dev.of_node; 1074 #endif 1075 1076 return dsi; 1077 } 1078 1079 static void __dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi) 1080 { 1081 mipi_dsi_host_unregister(&dsi->dsi_host); 1082 1083 pm_runtime_disable(dsi->dev); 1084 dw_mipi_dsi_debugfs_remove(dsi); 1085 } 1086 1087 void dw_mipi_dsi_set_slave(struct dw_mipi_dsi *dsi, struct dw_mipi_dsi *slave) 1088 { 1089 /* introduce controllers to each other */ 1090 dsi->slave = slave; 1091 dsi->slave->master = dsi; 1092 1093 /* migrate settings for already attached displays */ 1094 dsi->slave->lanes = dsi->lanes; 1095 dsi->slave->channel = dsi->channel; 1096 dsi->slave->format = dsi->format; 1097 dsi->slave->mode_flags = dsi->mode_flags; 1098 } 1099 EXPORT_SYMBOL_GPL(dw_mipi_dsi_set_slave); 1100 1101 /* 1102 * Probe/remove API, used from platforms based on the DRM bridge API. 1103 */ 1104 struct dw_mipi_dsi * 1105 dw_mipi_dsi_probe(struct platform_device *pdev, 1106 const struct dw_mipi_dsi_plat_data *plat_data) 1107 { 1108 return __dw_mipi_dsi_probe(pdev, plat_data); 1109 } 1110 EXPORT_SYMBOL_GPL(dw_mipi_dsi_probe); 1111 1112 void dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi) 1113 { 1114 __dw_mipi_dsi_remove(dsi); 1115 } 1116 EXPORT_SYMBOL_GPL(dw_mipi_dsi_remove); 1117 1118 /* 1119 * Bind/unbind API, used from platforms based on the component framework. 1120 */ 1121 int dw_mipi_dsi_bind(struct dw_mipi_dsi *dsi, struct drm_encoder *encoder) 1122 { 1123 int ret; 1124 1125 ret = drm_bridge_attach(encoder, &dsi->bridge, NULL, 0); 1126 if (ret) { 1127 DRM_ERROR("Failed to initialize bridge with drm\n"); 1128 return ret; 1129 } 1130 1131 return ret; 1132 } 1133 EXPORT_SYMBOL_GPL(dw_mipi_dsi_bind); 1134 1135 void dw_mipi_dsi_unbind(struct dw_mipi_dsi *dsi) 1136 { 1137 } 1138 EXPORT_SYMBOL_GPL(dw_mipi_dsi_unbind); 1139 1140 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>"); 1141 MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>"); 1142 MODULE_DESCRIPTION("DW MIPI DSI host controller driver"); 1143 MODULE_LICENSE("GPL"); 1144 MODULE_ALIAS("platform:dw-mipi-dsi"); 1145