1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright: 2017 Cadence Design Systems, Inc. 4 * 5 * Author: Boris Brezillon <boris.brezillon@bootlin.com> 6 */ 7 8 #include <drm/drm_atomic_helper.h> 9 #include <drm/drm_drv.h> 10 #include <drm/drm_probe_helper.h> 11 #include <video/mipi_display.h> 12 13 #include <linux/clk.h> 14 #include <linux/interrupt.h> 15 #include <linux/iopoll.h> 16 #include <linux/module.h> 17 #include <linux/of_address.h> 18 #include <linux/of_device.h> 19 #include <linux/of_graph.h> 20 #include <linux/platform_device.h> 21 #include <linux/pm_runtime.h> 22 #include <linux/reset.h> 23 24 #include <linux/phy/phy-mipi-dphy.h> 25 26 #include "cdns-dsi-core.h" 27 #ifdef CONFIG_DRM_CDNS_DSI_J721E 28 #include "cdns-dsi-j721e.h" 29 #endif 30 31 #define IP_CONF 0x0 32 #define SP_HS_FIFO_DEPTH(x) (((x) & GENMASK(30, 26)) >> 26) 33 #define SP_LP_FIFO_DEPTH(x) (((x) & GENMASK(25, 21)) >> 21) 34 #define VRS_FIFO_DEPTH(x) (((x) & GENMASK(20, 16)) >> 16) 35 #define DIRCMD_FIFO_DEPTH(x) (((x) & GENMASK(15, 13)) >> 13) 36 #define SDI_IFACE_32 BIT(12) 37 #define INTERNAL_DATAPATH_32 (0 << 10) 38 #define INTERNAL_DATAPATH_16 (1 << 10) 39 #define INTERNAL_DATAPATH_8 (3 << 10) 40 #define INTERNAL_DATAPATH_SIZE ((x) & GENMASK(11, 10)) 41 #define NUM_IFACE(x) ((((x) & GENMASK(9, 8)) >> 8) + 1) 42 #define MAX_LANE_NB(x) (((x) & GENMASK(7, 6)) >> 6) 43 #define RX_FIFO_DEPTH(x) ((x) & GENMASK(5, 0)) 44 45 #define MCTL_MAIN_DATA_CTL 0x4 46 #define TE_MIPI_POLLING_EN BIT(25) 47 #define TE_HW_POLLING_EN BIT(24) 48 #define DISP_EOT_GEN BIT(18) 49 #define HOST_EOT_GEN BIT(17) 50 #define DISP_GEN_CHECKSUM BIT(16) 51 #define DISP_GEN_ECC BIT(15) 52 #define BTA_EN BIT(14) 53 #define READ_EN BIT(13) 54 #define REG_TE_EN BIT(12) 55 #define IF_TE_EN(x) BIT(8 + (x)) 56 #define TVG_SEL BIT(6) 57 #define VID_EN BIT(5) 58 #define IF_VID_SELECT(x) ((x) << 2) 59 #define IF_VID_SELECT_MASK GENMASK(3, 2) 60 #define IF_VID_MODE BIT(1) 61 #define LINK_EN BIT(0) 62 63 #define MCTL_MAIN_PHY_CTL 0x8 64 #define HS_INVERT_DAT(x) BIT(19 + ((x) * 2)) 65 #define SWAP_PINS_DAT(x) BIT(18 + ((x) * 2)) 66 #define HS_INVERT_CLK BIT(17) 67 #define SWAP_PINS_CLK BIT(16) 68 #define HS_SKEWCAL_EN BIT(15) 69 #define WAIT_BURST_TIME(x) ((x) << 10) 70 #define DATA_ULPM_EN(x) BIT(6 + (x)) 71 #define CLK_ULPM_EN BIT(5) 72 #define CLK_CONTINUOUS BIT(4) 73 #define DATA_LANE_EN(x) BIT((x) - 1) 74 75 #define MCTL_MAIN_EN 0xc 76 #define DATA_FORCE_STOP BIT(17) 77 #define CLK_FORCE_STOP BIT(16) 78 #define IF_EN(x) BIT(13 + (x)) 79 #define DATA_LANE_ULPM_REQ(l) BIT(9 + (l)) 80 #define CLK_LANE_ULPM_REQ BIT(8) 81 #define DATA_LANE_START(x) BIT(4 + (x)) 82 #define CLK_LANE_EN BIT(3) 83 #define PLL_START BIT(0) 84 85 #define MCTL_DPHY_CFG0 0x10 86 #define DPHY_C_RSTB BIT(20) 87 #define DPHY_D_RSTB(x) GENMASK(15 + (x), 16) 88 #define DPHY_PLL_PDN BIT(10) 89 #define DPHY_CMN_PDN BIT(9) 90 #define DPHY_C_PDN BIT(8) 91 #define DPHY_D_PDN(x) GENMASK(3 + (x), 4) 92 #define DPHY_ALL_D_PDN GENMASK(7, 4) 93 #define DPHY_PLL_PSO BIT(1) 94 #define DPHY_CMN_PSO BIT(0) 95 96 #define MCTL_DPHY_TIMEOUT1 0x14 97 #define HSTX_TIMEOUT(x) ((x) << 4) 98 #define HSTX_TIMEOUT_MAX GENMASK(17, 0) 99 #define CLK_DIV(x) (x) 100 #define CLK_DIV_MAX GENMASK(3, 0) 101 102 #define MCTL_DPHY_TIMEOUT2 0x18 103 #define LPRX_TIMEOUT(x) (x) 104 105 #define MCTL_ULPOUT_TIME 0x1c 106 #define DATA_LANE_ULPOUT_TIME(x) ((x) << 9) 107 #define CLK_LANE_ULPOUT_TIME(x) (x) 108 109 #define MCTL_3DVIDEO_CTL 0x20 110 #define VID_VSYNC_3D_EN BIT(7) 111 #define VID_VSYNC_3D_LR BIT(5) 112 #define VID_VSYNC_3D_SECOND_EN BIT(4) 113 #define VID_VSYNC_3DFORMAT_LINE (0 << 2) 114 #define VID_VSYNC_3DFORMAT_FRAME (1 << 2) 115 #define VID_VSYNC_3DFORMAT_PIXEL (2 << 2) 116 #define VID_VSYNC_3DMODE_OFF 0 117 #define VID_VSYNC_3DMODE_PORTRAIT 1 118 #define VID_VSYNC_3DMODE_LANDSCAPE 2 119 120 #define MCTL_MAIN_STS 0x24 121 #define MCTL_MAIN_STS_CTL 0x130 122 #define MCTL_MAIN_STS_CLR 0x150 123 #define MCTL_MAIN_STS_FLAG 0x170 124 #define HS_SKEWCAL_DONE BIT(11) 125 #define IF_UNTERM_PKT_ERR(x) BIT(8 + (x)) 126 #define LPRX_TIMEOUT_ERR BIT(7) 127 #define HSTX_TIMEOUT_ERR BIT(6) 128 #define DATA_LANE_RDY(l) BIT(2 + (l)) 129 #define CLK_LANE_RDY BIT(1) 130 #define PLL_LOCKED BIT(0) 131 132 #define MCTL_DPHY_ERR 0x28 133 #define MCTL_DPHY_ERR_CTL1 0x148 134 #define MCTL_DPHY_ERR_CLR 0x168 135 #define MCTL_DPHY_ERR_FLAG 0x188 136 #define ERR_CONT_LP(x, l) BIT(18 + ((x) * 4) + (l)) 137 #define ERR_CONTROL(l) BIT(14 + (l)) 138 #define ERR_SYNESC(l) BIT(10 + (l)) 139 #define ERR_ESC(l) BIT(6 + (l)) 140 141 #define MCTL_DPHY_ERR_CTL2 0x14c 142 #define ERR_CONT_LP_EDGE(x, l) BIT(12 + ((x) * 4) + (l)) 143 #define ERR_CONTROL_EDGE(l) BIT(8 + (l)) 144 #define ERR_SYN_ESC_EDGE(l) BIT(4 + (l)) 145 #define ERR_ESC_EDGE(l) BIT(0 + (l)) 146 147 #define MCTL_LANE_STS 0x2c 148 #define PPI_C_TX_READY_HS BIT(18) 149 #define DPHY_PLL_LOCK BIT(17) 150 #define PPI_D_RX_ULPS_ESC(x) (((x) & GENMASK(15, 12)) >> 12) 151 #define LANE_STATE_START 0 152 #define LANE_STATE_IDLE 1 153 #define LANE_STATE_WRITE 2 154 #define LANE_STATE_ULPM 3 155 #define LANE_STATE_READ 4 156 #define DATA_LANE_STATE(l, val) \ 157 (((val) >> (2 + 2 * (l) + ((l) ? 1 : 0))) & GENMASK((l) ? 1 : 2, 0)) 158 #define CLK_LANE_STATE_HS 2 159 #define CLK_LANE_STATE(val) ((val) & GENMASK(1, 0)) 160 161 #define DSC_MODE_CTL 0x30 162 #define DSC_MODE_EN BIT(0) 163 164 #define DSC_CMD_SEND 0x34 165 #define DSC_SEND_PPS BIT(0) 166 #define DSC_EXECUTE_QUEUE BIT(1) 167 168 #define DSC_PPS_WRDAT 0x38 169 170 #define DSC_MODE_STS 0x3c 171 #define DSC_PPS_DONE BIT(1) 172 #define DSC_EXEC_DONE BIT(2) 173 174 #define CMD_MODE_CTL 0x70 175 #define IF_LP_EN(x) BIT(9 + (x)) 176 #define IF_VCHAN_ID(x, c) ((c) << ((x) * 2)) 177 178 #define CMD_MODE_CTL2 0x74 179 #define TE_TIMEOUT(x) ((x) << 11) 180 #define FILL_VALUE(x) ((x) << 3) 181 #define ARB_IF_WITH_HIGHEST_PRIORITY(x) ((x) << 1) 182 #define ARB_ROUND_ROBIN_MODE BIT(0) 183 184 #define CMD_MODE_STS 0x78 185 #define CMD_MODE_STS_CTL 0x134 186 #define CMD_MODE_STS_CLR 0x154 187 #define CMD_MODE_STS_FLAG 0x174 188 #define ERR_IF_UNDERRUN(x) BIT(4 + (x)) 189 #define ERR_UNWANTED_READ BIT(3) 190 #define ERR_TE_MISS BIT(2) 191 #define ERR_NO_TE BIT(1) 192 #define CSM_RUNNING BIT(0) 193 194 #define DIRECT_CMD_SEND 0x80 195 196 #define DIRECT_CMD_MAIN_SETTINGS 0x84 197 #define TRIGGER_VAL(x) ((x) << 25) 198 #define CMD_LP_EN BIT(24) 199 #define CMD_SIZE(x) ((x) << 16) 200 #define CMD_VCHAN_ID(x) ((x) << 14) 201 #define CMD_DATATYPE(x) ((x) << 8) 202 #define CMD_LONG BIT(3) 203 #define WRITE_CMD 0 204 #define READ_CMD 1 205 #define TE_REQ 4 206 #define TRIGGER_REQ 5 207 #define BTA_REQ 6 208 209 #define DIRECT_CMD_STS 0x88 210 #define DIRECT_CMD_STS_CTL 0x138 211 #define DIRECT_CMD_STS_CLR 0x158 212 #define DIRECT_CMD_STS_FLAG 0x178 213 #define RCVD_ACK_VAL(val) ((val) >> 16) 214 #define RCVD_TRIGGER_VAL(val) (((val) & GENMASK(14, 11)) >> 11) 215 #define READ_COMPLETED_WITH_ERR BIT(10) 216 #define BTA_FINISHED BIT(9) 217 #define BTA_COMPLETED BIT(8) 218 #define TE_RCVD BIT(7) 219 #define TRIGGER_RCVD BIT(6) 220 #define ACK_WITH_ERR_RCVD BIT(5) 221 #define ACK_RCVD BIT(4) 222 #define READ_COMPLETED BIT(3) 223 #define TRIGGER_COMPLETED BIT(2) 224 #define WRITE_COMPLETED BIT(1) 225 #define SENDING_CMD BIT(0) 226 227 #define DIRECT_CMD_STOP_READ 0x8c 228 229 #define DIRECT_CMD_WRDATA 0x90 230 231 #define DIRECT_CMD_FIFO_RST 0x94 232 233 #define DIRECT_CMD_RDDATA 0xa0 234 235 #define DIRECT_CMD_RD_PROPS 0xa4 236 #define RD_DCS BIT(18) 237 #define RD_VCHAN_ID(val) (((val) >> 16) & GENMASK(1, 0)) 238 #define RD_SIZE(val) ((val) & GENMASK(15, 0)) 239 240 #define DIRECT_CMD_RD_STS 0xa8 241 #define DIRECT_CMD_RD_STS_CTL 0x13c 242 #define DIRECT_CMD_RD_STS_CLR 0x15c 243 #define DIRECT_CMD_RD_STS_FLAG 0x17c 244 #define ERR_EOT_WITH_ERR BIT(8) 245 #define ERR_MISSING_EOT BIT(7) 246 #define ERR_WRONG_LENGTH BIT(6) 247 #define ERR_OVERSIZE BIT(5) 248 #define ERR_RECEIVE BIT(4) 249 #define ERR_UNDECODABLE BIT(3) 250 #define ERR_CHECKSUM BIT(2) 251 #define ERR_UNCORRECTABLE BIT(1) 252 #define ERR_FIXED BIT(0) 253 254 #define VID_MAIN_CTL 0xb0 255 #define VID_IGNORE_MISS_VSYNC BIT(31) 256 #define VID_FIELD_SW BIT(28) 257 #define VID_INTERLACED_EN BIT(27) 258 #define RECOVERY_MODE(x) ((x) << 25) 259 #define RECOVERY_MODE_NEXT_HSYNC 0 260 #define RECOVERY_MODE_NEXT_STOP_POINT 2 261 #define RECOVERY_MODE_NEXT_VSYNC 3 262 #define REG_BLKEOL_MODE(x) ((x) << 23) 263 #define REG_BLKLINE_MODE(x) ((x) << 21) 264 #define REG_BLK_MODE_NULL_PKT 0 265 #define REG_BLK_MODE_BLANKING_PKT 1 266 #define REG_BLK_MODE_LP 2 267 #define SYNC_PULSE_HORIZONTAL BIT(20) 268 #define SYNC_PULSE_ACTIVE BIT(19) 269 #define BURST_MODE BIT(18) 270 #define VID_PIXEL_MODE_MASK GENMASK(17, 14) 271 #define VID_PIXEL_MODE_RGB565 (0 << 14) 272 #define VID_PIXEL_MODE_RGB666_PACKED (1 << 14) 273 #define VID_PIXEL_MODE_RGB666 (2 << 14) 274 #define VID_PIXEL_MODE_RGB888 (3 << 14) 275 #define VID_PIXEL_MODE_RGB101010 (4 << 14) 276 #define VID_PIXEL_MODE_RGB121212 (5 << 14) 277 #define VID_PIXEL_MODE_YUV420 (8 << 14) 278 #define VID_PIXEL_MODE_YUV422_PACKED (9 << 14) 279 #define VID_PIXEL_MODE_YUV422 (10 << 14) 280 #define VID_PIXEL_MODE_YUV422_24B (11 << 14) 281 #define VID_PIXEL_MODE_DSC_COMP (12 << 14) 282 #define VID_DATATYPE(x) ((x) << 8) 283 #define VID_VIRTCHAN_ID(iface, x) ((x) << (4 + (iface) * 2)) 284 #define STOP_MODE(x) ((x) << 2) 285 #define START_MODE(x) (x) 286 287 #define VID_VSIZE1 0xb4 288 #define VFP_LEN(x) ((x) << 12) 289 #define VBP_LEN(x) ((x) << 6) 290 #define VSA_LEN(x) (x) 291 292 #define VID_VSIZE2 0xb8 293 #define VACT_LEN(x) (x) 294 295 #define VID_HSIZE1 0xc0 296 #define HBP_LEN(x) ((x) << 16) 297 #define HSA_LEN(x) (x) 298 299 #define VID_HSIZE2 0xc4 300 #define HFP_LEN(x) ((x) << 16) 301 #define HACT_LEN(x) (x) 302 303 #define VID_BLKSIZE1 0xcc 304 #define BLK_EOL_PKT_LEN(x) ((x) << 15) 305 #define BLK_LINE_EVENT_PKT_LEN(x) (x) 306 307 #define VID_BLKSIZE2 0xd0 308 #define BLK_LINE_PULSE_PKT_LEN(x) (x) 309 310 #define VID_PKT_TIME 0xd8 311 #define BLK_EOL_DURATION(x) (x) 312 313 #define VID_DPHY_TIME 0xdc 314 #define REG_WAKEUP_TIME(x) ((x) << 17) 315 #define REG_LINE_DURATION(x) (x) 316 317 #define VID_ERR_COLOR1 0xe0 318 #define COL_GREEN(x) ((x) << 12) 319 #define COL_RED(x) (x) 320 321 #define VID_ERR_COLOR2 0xe4 322 #define PAD_VAL(x) ((x) << 12) 323 #define COL_BLUE(x) (x) 324 325 #define VID_VPOS 0xe8 326 #define LINE_VAL(val) (((val) & GENMASK(14, 2)) >> 2) 327 #define LINE_POS(val) ((val) & GENMASK(1, 0)) 328 329 #define VID_HPOS 0xec 330 #define HORIZ_VAL(val) (((val) & GENMASK(17, 3)) >> 3) 331 #define HORIZ_POS(val) ((val) & GENMASK(2, 0)) 332 333 #define VID_MODE_STS 0xf0 334 #define VID_MODE_STS_CTL 0x140 335 #define VID_MODE_STS_CLR 0x160 336 #define VID_MODE_STS_FLAG 0x180 337 #define VSG_RECOVERY BIT(10) 338 #define ERR_VRS_WRONG_LEN BIT(9) 339 #define ERR_LONG_READ BIT(8) 340 #define ERR_LINE_WRITE BIT(7) 341 #define ERR_BURST_WRITE BIT(6) 342 #define ERR_SMALL_HEIGHT BIT(5) 343 #define ERR_SMALL_LEN BIT(4) 344 #define ERR_MISSING_VSYNC BIT(3) 345 #define ERR_MISSING_HSYNC BIT(2) 346 #define ERR_MISSING_DATA BIT(1) 347 #define VSG_RUNNING BIT(0) 348 349 #define VID_VCA_SETTING1 0xf4 350 #define BURST_LP BIT(16) 351 #define MAX_BURST_LIMIT(x) (x) 352 353 #define VID_VCA_SETTING2 0xf8 354 #define MAX_LINE_LIMIT(x) ((x) << 16) 355 #define EXACT_BURST_LIMIT(x) (x) 356 357 #define TVG_CTL 0xfc 358 #define TVG_STRIPE_SIZE(x) ((x) << 5) 359 #define TVG_MODE_MASK GENMASK(4, 3) 360 #define TVG_MODE_SINGLE_COLOR (0 << 3) 361 #define TVG_MODE_VSTRIPES (2 << 3) 362 #define TVG_MODE_HSTRIPES (3 << 3) 363 #define TVG_STOPMODE_MASK GENMASK(2, 1) 364 #define TVG_STOPMODE_EOF (0 << 1) 365 #define TVG_STOPMODE_EOL (1 << 1) 366 #define TVG_STOPMODE_NOW (2 << 1) 367 #define TVG_RUN BIT(0) 368 369 #define TVG_IMG_SIZE 0x100 370 #define TVG_NBLINES(x) ((x) << 16) 371 #define TVG_LINE_SIZE(x) (x) 372 373 #define TVG_COLOR1 0x104 374 #define TVG_COL1_GREEN(x) ((x) << 12) 375 #define TVG_COL1_RED(x) (x) 376 377 #define TVG_COLOR1_BIS 0x108 378 #define TVG_COL1_BLUE(x) (x) 379 380 #define TVG_COLOR2 0x10c 381 #define TVG_COL2_GREEN(x) ((x) << 12) 382 #define TVG_COL2_RED(x) (x) 383 384 #define TVG_COLOR2_BIS 0x110 385 #define TVG_COL2_BLUE(x) (x) 386 387 #define TVG_STS 0x114 388 #define TVG_STS_CTL 0x144 389 #define TVG_STS_CLR 0x164 390 #define TVG_STS_FLAG 0x184 391 #define TVG_STS_RUNNING BIT(0) 392 393 #define STS_CTL_EDGE(e) ((e) << 16) 394 395 #define DPHY_LANES_MAP 0x198 396 #define DAT_REMAP_CFG(b, l) ((l) << ((b) * 8)) 397 398 #define DPI_IRQ_EN 0x1a0 399 #define DPI_IRQ_CLR 0x1a4 400 #define DPI_IRQ_STS 0x1a8 401 #define PIXEL_BUF_OVERFLOW BIT(0) 402 403 #define DPI_CFG 0x1ac 404 #define DPI_CFG_FIFO_DEPTH(x) ((x) >> 16) 405 #define DPI_CFG_FIFO_LEVEL(x) ((x) & GENMASK(15, 0)) 406 407 #define TEST_GENERIC 0x1f0 408 #define TEST_STATUS(x) ((x) >> 16) 409 #define TEST_CTRL(x) (x) 410 411 #define ID_REG 0x1fc 412 #define REV_VENDOR_ID(x) (((x) & GENMASK(31, 20)) >> 20) 413 #define REV_PRODUCT_ID(x) (((x) & GENMASK(19, 12)) >> 12) 414 #define REV_HW(x) (((x) & GENMASK(11, 8)) >> 8) 415 #define REV_MAJOR(x) (((x) & GENMASK(7, 4)) >> 4) 416 #define REV_MINOR(x) ((x) & GENMASK(3, 0)) 417 418 #define DSI_OUTPUT_PORT 0 419 #define DSI_INPUT_PORT(inputid) (1 + (inputid)) 420 421 #define DSI_HBP_FRAME_OVERHEAD 12 422 #define DSI_HSA_FRAME_OVERHEAD 14 423 #define DSI_HFP_FRAME_OVERHEAD 6 424 #define DSI_HSS_VSS_VSE_FRAME_OVERHEAD 4 425 #define DSI_BLANKING_FRAME_OVERHEAD 6 426 #define DSI_NULL_FRAME_OVERHEAD 6 427 #define DSI_EOT_PKT_SIZE 4 428 429 static inline struct cdns_dsi *input_to_dsi(struct cdns_dsi_input *input) 430 { 431 return container_of(input, struct cdns_dsi, input); 432 } 433 434 static inline struct cdns_dsi *to_cdns_dsi(struct mipi_dsi_host *host) 435 { 436 return container_of(host, struct cdns_dsi, base); 437 } 438 439 static inline struct cdns_dsi_input * 440 bridge_to_cdns_dsi_input(struct drm_bridge *bridge) 441 { 442 return container_of(bridge, struct cdns_dsi_input, bridge); 443 } 444 445 static unsigned int mode_to_dpi_hfp(const struct drm_display_mode *mode, 446 bool mode_valid_check) 447 { 448 if (mode_valid_check) 449 return mode->hsync_start - mode->hdisplay; 450 451 return mode->crtc_hsync_start - mode->crtc_hdisplay; 452 } 453 454 static unsigned int dpi_to_dsi_timing(unsigned int dpi_timing, 455 unsigned int dpi_bpp, 456 unsigned int dsi_pkt_overhead) 457 { 458 unsigned int dsi_timing = DIV_ROUND_UP(dpi_timing * dpi_bpp, 8); 459 460 if (dsi_timing < dsi_pkt_overhead) 461 dsi_timing = 0; 462 else 463 dsi_timing -= dsi_pkt_overhead; 464 465 return dsi_timing; 466 } 467 468 static int cdns_dsi_mode2cfg(struct cdns_dsi *dsi, 469 const struct drm_display_mode *mode, 470 struct cdns_dsi_cfg *dsi_cfg, 471 bool mode_valid_check) 472 { 473 struct cdns_dsi_output *output = &dsi->output; 474 unsigned int tmp; 475 bool sync_pulse = false; 476 int bpp; 477 478 memset(dsi_cfg, 0, sizeof(*dsi_cfg)); 479 480 if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) 481 sync_pulse = true; 482 483 bpp = mipi_dsi_pixel_format_to_bpp(output->dev->format); 484 485 if (mode_valid_check) 486 tmp = mode->htotal - 487 (sync_pulse ? mode->hsync_end : mode->hsync_start); 488 else 489 tmp = mode->crtc_htotal - 490 (sync_pulse ? 491 mode->crtc_hsync_end : mode->crtc_hsync_start); 492 493 dsi_cfg->hbp = dpi_to_dsi_timing(tmp, bpp, DSI_HBP_FRAME_OVERHEAD); 494 495 if (sync_pulse) { 496 if (mode_valid_check) 497 tmp = mode->hsync_end - mode->hsync_start; 498 else 499 tmp = mode->crtc_hsync_end - mode->crtc_hsync_start; 500 501 dsi_cfg->hsa = dpi_to_dsi_timing(tmp, bpp, 502 DSI_HSA_FRAME_OVERHEAD); 503 } 504 505 dsi_cfg->hact = dpi_to_dsi_timing(mode_valid_check ? 506 mode->hdisplay : mode->crtc_hdisplay, 507 bpp, 0); 508 dsi_cfg->hfp = dpi_to_dsi_timing(mode_to_dpi_hfp(mode, mode_valid_check), 509 bpp, DSI_HFP_FRAME_OVERHEAD); 510 511 return 0; 512 } 513 514 static int cdns_dsi_adjust_phy_config(struct cdns_dsi *dsi, 515 struct cdns_dsi_cfg *dsi_cfg, 516 struct phy_configure_opts_mipi_dphy *phy_cfg, 517 const struct drm_display_mode *mode, 518 bool mode_valid_check) 519 { 520 struct cdns_dsi_output *output = &dsi->output; 521 unsigned long long dlane_bps; 522 unsigned long adj_dsi_htotal; 523 unsigned long dsi_htotal; 524 unsigned long dpi_htotal; 525 unsigned long dpi_hz; 526 unsigned int dsi_hfp_ext; 527 unsigned int lanes = output->dev->lanes; 528 529 dsi_htotal = dsi_cfg->hbp + DSI_HBP_FRAME_OVERHEAD; 530 if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) 531 dsi_htotal += dsi_cfg->hsa + DSI_HSA_FRAME_OVERHEAD; 532 533 dsi_htotal += dsi_cfg->hact; 534 dsi_htotal += dsi_cfg->hfp + DSI_HFP_FRAME_OVERHEAD; 535 536 /* 537 * Make sure DSI htotal is aligned on a lane boundary when calculating 538 * the expected data rate. This is done by extending HFP in case of 539 * misalignment. 540 */ 541 adj_dsi_htotal = dsi_htotal; 542 if (dsi_htotal % lanes) 543 adj_dsi_htotal += lanes - (dsi_htotal % lanes); 544 545 dpi_hz = (mode_valid_check ? mode->clock : mode->crtc_clock) * 1000; 546 dlane_bps = (unsigned long long)dpi_hz * adj_dsi_htotal; 547 548 /* data rate in bytes/sec is not an integer, refuse the mode. */ 549 dpi_htotal = mode_valid_check ? mode->htotal : mode->crtc_htotal; 550 if (do_div(dlane_bps, lanes * dpi_htotal)) 551 return -EINVAL; 552 553 /* data rate was in bytes/sec, convert to bits/sec. */ 554 phy_cfg->hs_clk_rate = dlane_bps * 8; 555 556 dsi_hfp_ext = adj_dsi_htotal - dsi_htotal; 557 dsi_cfg->hfp += dsi_hfp_ext; 558 dsi_cfg->htotal = dsi_htotal + dsi_hfp_ext; 559 560 return 0; 561 } 562 563 static int cdns_dsi_check_conf(struct cdns_dsi *dsi, 564 const struct drm_display_mode *mode, 565 struct cdns_dsi_cfg *dsi_cfg, 566 bool mode_valid_check) 567 { 568 struct cdns_dsi_output *output = &dsi->output; 569 struct phy_configure_opts_mipi_dphy *phy_cfg = &output->phy_opts.mipi_dphy; 570 unsigned long dsi_hss_hsa_hse_hbp; 571 unsigned int nlanes = output->dev->lanes; 572 int ret; 573 574 ret = cdns_dsi_mode2cfg(dsi, mode, dsi_cfg, mode_valid_check); 575 if (ret) 576 return ret; 577 578 phy_mipi_dphy_get_default_config(mode->crtc_clock * 1000, 579 mipi_dsi_pixel_format_to_bpp(output->dev->format), 580 nlanes, phy_cfg); 581 582 ret = cdns_dsi_adjust_phy_config(dsi, dsi_cfg, phy_cfg, mode, mode_valid_check); 583 if (ret) 584 return ret; 585 586 ret = phy_validate(dsi->dphy, PHY_MODE_MIPI_DPHY, 0, &output->phy_opts); 587 if (ret) 588 return ret; 589 590 dsi_hss_hsa_hse_hbp = dsi_cfg->hbp + DSI_HBP_FRAME_OVERHEAD; 591 if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) 592 dsi_hss_hsa_hse_hbp += dsi_cfg->hsa + DSI_HSA_FRAME_OVERHEAD; 593 594 /* 595 * Make sure DPI(HFP) > DSI(HSS+HSA+HSE+HBP) to guarantee that the FIFO 596 * is empty before we start a receiving a new line on the DPI 597 * interface. 598 */ 599 if ((u64)phy_cfg->hs_clk_rate * 600 mode_to_dpi_hfp(mode, mode_valid_check) * nlanes < 601 (u64)dsi_hss_hsa_hse_hbp * 602 (mode_valid_check ? mode->clock : mode->crtc_clock) * 1000) 603 return -EINVAL; 604 605 return 0; 606 } 607 608 static int cdns_dsi_bridge_attach(struct drm_bridge *bridge, 609 enum drm_bridge_attach_flags flags) 610 { 611 struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge); 612 struct cdns_dsi *dsi = input_to_dsi(input); 613 struct cdns_dsi_output *output = &dsi->output; 614 615 if (!drm_core_check_feature(bridge->dev, DRIVER_ATOMIC)) { 616 dev_err(dsi->base.dev, 617 "cdns-dsi driver is only compatible with DRM devices supporting atomic updates"); 618 return -ENOTSUPP; 619 } 620 621 return drm_bridge_attach(bridge->encoder, output->bridge, bridge, 622 flags); 623 } 624 625 static enum drm_mode_status 626 cdns_dsi_bridge_mode_valid(struct drm_bridge *bridge, 627 const struct drm_display_info *info, 628 const struct drm_display_mode *mode) 629 { 630 struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge); 631 struct cdns_dsi *dsi = input_to_dsi(input); 632 struct cdns_dsi_output *output = &dsi->output; 633 struct cdns_dsi_cfg dsi_cfg; 634 int bpp, ret; 635 636 /* 637 * VFP_DSI should be less than VFP_DPI and VFP_DSI should be at 638 * least 1. 639 */ 640 if (mode->vtotal - mode->vsync_end < 2) 641 return MODE_V_ILLEGAL; 642 643 /* VSA_DSI = VSA_DPI and must be at least 2. */ 644 if (mode->vsync_end - mode->vsync_start < 2) 645 return MODE_V_ILLEGAL; 646 647 /* HACT must be 32-bits aligned. */ 648 bpp = mipi_dsi_pixel_format_to_bpp(output->dev->format); 649 if ((mode->hdisplay * bpp) % 32) 650 return MODE_H_ILLEGAL; 651 652 ret = cdns_dsi_check_conf(dsi, mode, &dsi_cfg, true); 653 if (ret) 654 return MODE_BAD; 655 656 return MODE_OK; 657 } 658 659 static void cdns_dsi_bridge_disable(struct drm_bridge *bridge) 660 { 661 struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge); 662 struct cdns_dsi *dsi = input_to_dsi(input); 663 u32 val; 664 665 val = readl(dsi->regs + MCTL_MAIN_DATA_CTL); 666 val &= ~(IF_VID_SELECT_MASK | IF_VID_MODE | VID_EN | HOST_EOT_GEN | 667 DISP_EOT_GEN); 668 writel(val, dsi->regs + MCTL_MAIN_DATA_CTL); 669 670 val = readl(dsi->regs + MCTL_MAIN_EN) & ~IF_EN(input->id); 671 writel(val, dsi->regs + MCTL_MAIN_EN); 672 673 if (dsi->platform_ops && dsi->platform_ops->disable) 674 dsi->platform_ops->disable(dsi); 675 676 pm_runtime_put(dsi->base.dev); 677 } 678 679 static void cdns_dsi_bridge_post_disable(struct drm_bridge *bridge) 680 { 681 struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge); 682 struct cdns_dsi *dsi = input_to_dsi(input); 683 684 pm_runtime_put(dsi->base.dev); 685 } 686 687 static void cdns_dsi_hs_init(struct cdns_dsi *dsi) 688 { 689 struct cdns_dsi_output *output = &dsi->output; 690 u32 status; 691 692 if (dsi->phy_initialized) 693 return; 694 /* 695 * Power all internal DPHY blocks down and maintain their reset line 696 * asserted before changing the DPHY config. 697 */ 698 writel(DPHY_CMN_PSO | DPHY_PLL_PSO | DPHY_ALL_D_PDN | DPHY_C_PDN | 699 DPHY_CMN_PDN | DPHY_PLL_PDN, 700 dsi->regs + MCTL_DPHY_CFG0); 701 702 phy_init(dsi->dphy); 703 phy_set_mode(dsi->dphy, PHY_MODE_MIPI_DPHY); 704 phy_configure(dsi->dphy, &output->phy_opts); 705 phy_power_on(dsi->dphy); 706 707 /* Activate the PLL and wait until it's locked. */ 708 writel(PLL_LOCKED, dsi->regs + MCTL_MAIN_STS_CLR); 709 writel(DPHY_CMN_PSO | DPHY_ALL_D_PDN | DPHY_C_PDN | DPHY_CMN_PDN, 710 dsi->regs + MCTL_DPHY_CFG0); 711 WARN_ON_ONCE(readl_poll_timeout(dsi->regs + MCTL_MAIN_STS, status, 712 status & PLL_LOCKED, 100, 100)); 713 /* De-assert data and clock reset lines. */ 714 writel(DPHY_CMN_PSO | DPHY_ALL_D_PDN | DPHY_C_PDN | DPHY_CMN_PDN | 715 DPHY_D_RSTB(output->dev->lanes) | DPHY_C_RSTB, 716 dsi->regs + MCTL_DPHY_CFG0); 717 dsi->phy_initialized = true; 718 } 719 720 static void cdns_dsi_init_link(struct cdns_dsi *dsi) 721 { 722 struct cdns_dsi_output *output = &dsi->output; 723 unsigned long sysclk_period, ulpout; 724 u32 val; 725 int i; 726 727 if (dsi->link_initialized) 728 return; 729 730 val = 0; 731 for (i = 1; i < output->dev->lanes; i++) 732 val |= DATA_LANE_EN(i); 733 734 if (!(output->dev->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)) 735 val |= CLK_CONTINUOUS; 736 737 writel(val, dsi->regs + MCTL_MAIN_PHY_CTL); 738 739 /* ULPOUT should be set to 1ms and is expressed in sysclk cycles. */ 740 sysclk_period = NSEC_PER_SEC / clk_get_rate(dsi->dsi_sys_clk); 741 ulpout = DIV_ROUND_UP(NSEC_PER_MSEC, sysclk_period); 742 writel(CLK_LANE_ULPOUT_TIME(ulpout) | DATA_LANE_ULPOUT_TIME(ulpout), 743 dsi->regs + MCTL_ULPOUT_TIME); 744 745 writel(LINK_EN, dsi->regs + MCTL_MAIN_DATA_CTL); 746 747 val = CLK_LANE_EN | PLL_START; 748 for (i = 0; i < output->dev->lanes; i++) 749 val |= DATA_LANE_START(i); 750 751 writel(val, dsi->regs + MCTL_MAIN_EN); 752 753 dsi->link_initialized = true; 754 } 755 756 static void cdns_dsi_bridge_enable(struct drm_bridge *bridge) 757 { 758 struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge); 759 struct cdns_dsi *dsi = input_to_dsi(input); 760 struct cdns_dsi_output *output = &dsi->output; 761 struct drm_display_mode *mode; 762 struct phy_configure_opts_mipi_dphy *phy_cfg = &output->phy_opts.mipi_dphy; 763 unsigned long tx_byte_period; 764 struct cdns_dsi_cfg dsi_cfg; 765 u32 tmp, reg_wakeup, div; 766 int nlanes; 767 768 if (WARN_ON(pm_runtime_get_sync(dsi->base.dev) < 0)) 769 return; 770 771 if (dsi->platform_ops && dsi->platform_ops->enable) 772 dsi->platform_ops->enable(dsi); 773 774 mode = &bridge->encoder->crtc->state->adjusted_mode; 775 nlanes = output->dev->lanes; 776 777 WARN_ON_ONCE(cdns_dsi_check_conf(dsi, mode, &dsi_cfg, false)); 778 779 cdns_dsi_hs_init(dsi); 780 cdns_dsi_init_link(dsi); 781 782 writel(HBP_LEN(dsi_cfg.hbp) | HSA_LEN(dsi_cfg.hsa), 783 dsi->regs + VID_HSIZE1); 784 writel(HFP_LEN(dsi_cfg.hfp) | HACT_LEN(dsi_cfg.hact), 785 dsi->regs + VID_HSIZE2); 786 787 writel(VBP_LEN(mode->crtc_vtotal - mode->crtc_vsync_end - 1) | 788 VFP_LEN(mode->crtc_vsync_start - mode->crtc_vdisplay) | 789 VSA_LEN(mode->crtc_vsync_end - mode->crtc_vsync_start + 1), 790 dsi->regs + VID_VSIZE1); 791 writel(mode->crtc_vdisplay, dsi->regs + VID_VSIZE2); 792 793 tmp = dsi_cfg.htotal - 794 (dsi_cfg.hsa + DSI_BLANKING_FRAME_OVERHEAD + 795 DSI_HSA_FRAME_OVERHEAD); 796 writel(BLK_LINE_PULSE_PKT_LEN(tmp), dsi->regs + VID_BLKSIZE2); 797 if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) 798 writel(MAX_LINE_LIMIT(tmp - DSI_NULL_FRAME_OVERHEAD), 799 dsi->regs + VID_VCA_SETTING2); 800 801 tmp = dsi_cfg.htotal - 802 (DSI_HSS_VSS_VSE_FRAME_OVERHEAD + DSI_BLANKING_FRAME_OVERHEAD); 803 writel(BLK_LINE_EVENT_PKT_LEN(tmp), dsi->regs + VID_BLKSIZE1); 804 if (!(output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)) 805 writel(MAX_LINE_LIMIT(tmp - DSI_NULL_FRAME_OVERHEAD), 806 dsi->regs + VID_VCA_SETTING2); 807 808 tmp = DIV_ROUND_UP(dsi_cfg.htotal, nlanes) - 809 DIV_ROUND_UP(dsi_cfg.hsa, nlanes); 810 811 if (!(output->dev->mode_flags & MIPI_DSI_MODE_NO_EOT_PACKET)) 812 tmp -= DIV_ROUND_UP(DSI_EOT_PKT_SIZE, nlanes); 813 814 tx_byte_period = DIV_ROUND_DOWN_ULL((u64)NSEC_PER_SEC * 8, 815 phy_cfg->hs_clk_rate); 816 reg_wakeup = (phy_cfg->hs_prepare + phy_cfg->hs_zero) / tx_byte_period; 817 writel(REG_WAKEUP_TIME(reg_wakeup) | REG_LINE_DURATION(tmp), 818 dsi->regs + VID_DPHY_TIME); 819 820 /* 821 * HSTX and LPRX timeouts are both expressed in TX byte clk cycles and 822 * both should be set to at least the time it takes to transmit a 823 * frame. 824 */ 825 tmp = NSEC_PER_SEC / drm_mode_vrefresh(mode); 826 tmp /= tx_byte_period; 827 828 for (div = 0; div <= CLK_DIV_MAX; div++) { 829 if (tmp <= HSTX_TIMEOUT_MAX) 830 break; 831 832 tmp >>= 1; 833 } 834 835 if (tmp > HSTX_TIMEOUT_MAX) 836 tmp = HSTX_TIMEOUT_MAX; 837 838 writel(CLK_DIV(div) | HSTX_TIMEOUT(tmp), 839 dsi->regs + MCTL_DPHY_TIMEOUT1); 840 841 writel(LPRX_TIMEOUT(tmp), dsi->regs + MCTL_DPHY_TIMEOUT2); 842 843 if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO) { 844 switch (output->dev->format) { 845 case MIPI_DSI_FMT_RGB888: 846 tmp = VID_PIXEL_MODE_RGB888 | 847 VID_DATATYPE(MIPI_DSI_PACKED_PIXEL_STREAM_24); 848 break; 849 850 case MIPI_DSI_FMT_RGB666: 851 tmp = VID_PIXEL_MODE_RGB666 | 852 VID_DATATYPE(MIPI_DSI_PIXEL_STREAM_3BYTE_18); 853 break; 854 855 case MIPI_DSI_FMT_RGB666_PACKED: 856 tmp = VID_PIXEL_MODE_RGB666_PACKED | 857 VID_DATATYPE(MIPI_DSI_PACKED_PIXEL_STREAM_18); 858 break; 859 860 case MIPI_DSI_FMT_RGB565: 861 tmp = VID_PIXEL_MODE_RGB565 | 862 VID_DATATYPE(MIPI_DSI_PACKED_PIXEL_STREAM_16); 863 break; 864 865 default: 866 dev_err(dsi->base.dev, "Unsupported DSI format\n"); 867 return; 868 } 869 870 if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) 871 tmp |= SYNC_PULSE_ACTIVE | SYNC_PULSE_HORIZONTAL; 872 873 tmp |= REG_BLKLINE_MODE(REG_BLK_MODE_BLANKING_PKT) | 874 REG_BLKEOL_MODE(REG_BLK_MODE_BLANKING_PKT) | 875 RECOVERY_MODE(RECOVERY_MODE_NEXT_HSYNC) | 876 VID_IGNORE_MISS_VSYNC; 877 878 writel(tmp, dsi->regs + VID_MAIN_CTL); 879 } 880 881 tmp = readl(dsi->regs + MCTL_MAIN_DATA_CTL); 882 tmp &= ~(IF_VID_SELECT_MASK | HOST_EOT_GEN | IF_VID_MODE); 883 884 if (!(output->dev->mode_flags & MIPI_DSI_MODE_NO_EOT_PACKET)) 885 tmp |= HOST_EOT_GEN; 886 887 if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO) 888 tmp |= IF_VID_MODE | IF_VID_SELECT(input->id) | VID_EN; 889 890 writel(tmp, dsi->regs + MCTL_MAIN_DATA_CTL); 891 892 tmp = readl(dsi->regs + MCTL_MAIN_EN) | IF_EN(input->id); 893 writel(tmp, dsi->regs + MCTL_MAIN_EN); 894 } 895 896 static void cdns_dsi_bridge_pre_enable(struct drm_bridge *bridge) 897 { 898 struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge); 899 struct cdns_dsi *dsi = input_to_dsi(input); 900 901 if (WARN_ON(pm_runtime_get_sync(dsi->base.dev) < 0)) 902 return; 903 904 cdns_dsi_init_link(dsi); 905 cdns_dsi_hs_init(dsi); 906 } 907 908 static const struct drm_bridge_funcs cdns_dsi_bridge_funcs = { 909 .attach = cdns_dsi_bridge_attach, 910 .mode_valid = cdns_dsi_bridge_mode_valid, 911 .disable = cdns_dsi_bridge_disable, 912 .pre_enable = cdns_dsi_bridge_pre_enable, 913 .enable = cdns_dsi_bridge_enable, 914 .post_disable = cdns_dsi_bridge_post_disable, 915 }; 916 917 static int cdns_dsi_attach(struct mipi_dsi_host *host, 918 struct mipi_dsi_device *dev) 919 { 920 struct cdns_dsi *dsi = to_cdns_dsi(host); 921 struct cdns_dsi_output *output = &dsi->output; 922 struct cdns_dsi_input *input = &dsi->input; 923 struct drm_bridge *bridge; 924 struct drm_panel *panel; 925 struct device_node *np; 926 int ret; 927 928 /* 929 * We currently do not support connecting several DSI devices to the 930 * same host. In order to support that we'd need the DRM bridge 931 * framework to allow dynamic reconfiguration of the bridge chain. 932 */ 933 if (output->dev) 934 return -EBUSY; 935 936 /* We do not support burst mode yet. */ 937 if (dev->mode_flags & MIPI_DSI_MODE_VIDEO_BURST) 938 return -ENOTSUPP; 939 940 /* 941 * The host <-> device link might be described using an OF-graph 942 * representation, in this case we extract the device of_node from 943 * this representation, otherwise we use dsidev->dev.of_node which 944 * should have been filled by the core. 945 */ 946 np = of_graph_get_remote_node(dsi->base.dev->of_node, DSI_OUTPUT_PORT, 947 dev->channel); 948 if (!np) 949 np = of_node_get(dev->dev.of_node); 950 951 panel = of_drm_find_panel(np); 952 if (!IS_ERR(panel)) { 953 bridge = drm_panel_bridge_add_typed(panel, 954 DRM_MODE_CONNECTOR_DSI); 955 } else { 956 bridge = of_drm_find_bridge(dev->dev.of_node); 957 if (!bridge) 958 bridge = ERR_PTR(-EINVAL); 959 } 960 961 of_node_put(np); 962 963 if (IS_ERR(bridge)) { 964 ret = PTR_ERR(bridge); 965 dev_err(host->dev, "failed to add DSI device %s (err = %d)", 966 dev->name, ret); 967 return ret; 968 } 969 970 output->dev = dev; 971 output->bridge = bridge; 972 output->panel = panel; 973 974 /* 975 * The DSI output has been properly configured, we can now safely 976 * register the input to the bridge framework so that it can take place 977 * in a display pipeline. 978 */ 979 drm_bridge_add(&input->bridge); 980 981 return 0; 982 } 983 984 static int cdns_dsi_detach(struct mipi_dsi_host *host, 985 struct mipi_dsi_device *dev) 986 { 987 struct cdns_dsi *dsi = to_cdns_dsi(host); 988 struct cdns_dsi_output *output = &dsi->output; 989 struct cdns_dsi_input *input = &dsi->input; 990 991 drm_bridge_remove(&input->bridge); 992 if (output->panel) 993 drm_panel_bridge_remove(output->bridge); 994 995 return 0; 996 } 997 998 static irqreturn_t cdns_dsi_interrupt(int irq, void *data) 999 { 1000 struct cdns_dsi *dsi = data; 1001 irqreturn_t ret = IRQ_NONE; 1002 u32 flag, ctl; 1003 1004 flag = readl(dsi->regs + DIRECT_CMD_STS_FLAG); 1005 if (flag) { 1006 ctl = readl(dsi->regs + DIRECT_CMD_STS_CTL); 1007 ctl &= ~flag; 1008 writel(ctl, dsi->regs + DIRECT_CMD_STS_CTL); 1009 complete(&dsi->direct_cmd_comp); 1010 ret = IRQ_HANDLED; 1011 } 1012 1013 return ret; 1014 } 1015 1016 static ssize_t cdns_dsi_transfer(struct mipi_dsi_host *host, 1017 const struct mipi_dsi_msg *msg) 1018 { 1019 struct cdns_dsi *dsi = to_cdns_dsi(host); 1020 u32 cmd, sts, val, wait = WRITE_COMPLETED, ctl = 0; 1021 struct mipi_dsi_packet packet; 1022 int ret, i, tx_len, rx_len; 1023 1024 ret = pm_runtime_resume_and_get(host->dev); 1025 if (ret < 0) 1026 return ret; 1027 1028 cdns_dsi_init_link(dsi); 1029 1030 ret = mipi_dsi_create_packet(&packet, msg); 1031 if (ret) 1032 goto out; 1033 1034 tx_len = msg->tx_buf ? msg->tx_len : 0; 1035 rx_len = msg->rx_buf ? msg->rx_len : 0; 1036 1037 /* For read operations, the maximum TX len is 2. */ 1038 if (rx_len && tx_len > 2) { 1039 ret = -ENOTSUPP; 1040 goto out; 1041 } 1042 1043 /* TX len is limited by the CMD FIFO depth. */ 1044 if (tx_len > dsi->direct_cmd_fifo_depth) { 1045 ret = -ENOTSUPP; 1046 goto out; 1047 } 1048 1049 /* RX len is limited by the RX FIFO depth. */ 1050 if (rx_len > dsi->rx_fifo_depth) { 1051 ret = -ENOTSUPP; 1052 goto out; 1053 } 1054 1055 cmd = CMD_SIZE(tx_len) | CMD_VCHAN_ID(msg->channel) | 1056 CMD_DATATYPE(msg->type); 1057 1058 if (msg->flags & MIPI_DSI_MSG_USE_LPM) 1059 cmd |= CMD_LP_EN; 1060 1061 if (mipi_dsi_packet_format_is_long(msg->type)) 1062 cmd |= CMD_LONG; 1063 1064 if (rx_len) { 1065 cmd |= READ_CMD; 1066 wait = READ_COMPLETED_WITH_ERR | READ_COMPLETED; 1067 ctl = READ_EN | BTA_EN; 1068 } else if (msg->flags & MIPI_DSI_MSG_REQ_ACK) { 1069 cmd |= BTA_REQ; 1070 wait = ACK_WITH_ERR_RCVD | ACK_RCVD; 1071 ctl = BTA_EN; 1072 } 1073 1074 writel(readl(dsi->regs + MCTL_MAIN_DATA_CTL) | ctl, 1075 dsi->regs + MCTL_MAIN_DATA_CTL); 1076 1077 writel(cmd, dsi->regs + DIRECT_CMD_MAIN_SETTINGS); 1078 1079 for (i = 0; i < tx_len; i += 4) { 1080 const u8 *buf = msg->tx_buf; 1081 int j; 1082 1083 val = 0; 1084 for (j = 0; j < 4 && j + i < tx_len; j++) 1085 val |= (u32)buf[i + j] << (8 * j); 1086 1087 writel(val, dsi->regs + DIRECT_CMD_WRDATA); 1088 } 1089 1090 /* Clear status flags before sending the command. */ 1091 writel(wait, dsi->regs + DIRECT_CMD_STS_CLR); 1092 writel(wait, dsi->regs + DIRECT_CMD_STS_CTL); 1093 reinit_completion(&dsi->direct_cmd_comp); 1094 writel(0, dsi->regs + DIRECT_CMD_SEND); 1095 1096 wait_for_completion_timeout(&dsi->direct_cmd_comp, 1097 msecs_to_jiffies(1000)); 1098 1099 sts = readl(dsi->regs + DIRECT_CMD_STS); 1100 writel(wait, dsi->regs + DIRECT_CMD_STS_CLR); 1101 writel(0, dsi->regs + DIRECT_CMD_STS_CTL); 1102 1103 writel(readl(dsi->regs + MCTL_MAIN_DATA_CTL) & ~ctl, 1104 dsi->regs + MCTL_MAIN_DATA_CTL); 1105 1106 /* We did not receive the events we were waiting for. */ 1107 if (!(sts & wait)) { 1108 ret = -ETIMEDOUT; 1109 goto out; 1110 } 1111 1112 /* 'READ' or 'WRITE with ACK' failed. */ 1113 if (sts & (READ_COMPLETED_WITH_ERR | ACK_WITH_ERR_RCVD)) { 1114 ret = -EIO; 1115 goto out; 1116 } 1117 1118 for (i = 0; i < rx_len; i += 4) { 1119 u8 *buf = msg->rx_buf; 1120 int j; 1121 1122 val = readl(dsi->regs + DIRECT_CMD_RDDATA); 1123 for (j = 0; j < 4 && j + i < rx_len; j++) 1124 buf[i + j] = val >> (8 * j); 1125 } 1126 1127 out: 1128 pm_runtime_put(host->dev); 1129 return ret; 1130 } 1131 1132 static const struct mipi_dsi_host_ops cdns_dsi_ops = { 1133 .attach = cdns_dsi_attach, 1134 .detach = cdns_dsi_detach, 1135 .transfer = cdns_dsi_transfer, 1136 }; 1137 1138 static int __maybe_unused cdns_dsi_resume(struct device *dev) 1139 { 1140 struct cdns_dsi *dsi = dev_get_drvdata(dev); 1141 1142 reset_control_deassert(dsi->dsi_p_rst); 1143 clk_prepare_enable(dsi->dsi_p_clk); 1144 clk_prepare_enable(dsi->dsi_sys_clk); 1145 1146 return 0; 1147 } 1148 1149 static int __maybe_unused cdns_dsi_suspend(struct device *dev) 1150 { 1151 struct cdns_dsi *dsi = dev_get_drvdata(dev); 1152 1153 clk_disable_unprepare(dsi->dsi_sys_clk); 1154 clk_disable_unprepare(dsi->dsi_p_clk); 1155 reset_control_assert(dsi->dsi_p_rst); 1156 dsi->link_initialized = false; 1157 return 0; 1158 } 1159 1160 static UNIVERSAL_DEV_PM_OPS(cdns_dsi_pm_ops, cdns_dsi_suspend, cdns_dsi_resume, 1161 NULL); 1162 1163 static int cdns_dsi_drm_probe(struct platform_device *pdev) 1164 { 1165 struct cdns_dsi *dsi; 1166 struct cdns_dsi_input *input; 1167 int ret, irq; 1168 u32 val; 1169 1170 dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL); 1171 if (!dsi) 1172 return -ENOMEM; 1173 1174 platform_set_drvdata(pdev, dsi); 1175 1176 input = &dsi->input; 1177 1178 dsi->regs = devm_platform_ioremap_resource(pdev, 0); 1179 if (IS_ERR(dsi->regs)) 1180 return PTR_ERR(dsi->regs); 1181 1182 dsi->dsi_p_clk = devm_clk_get(&pdev->dev, "dsi_p_clk"); 1183 if (IS_ERR(dsi->dsi_p_clk)) 1184 return PTR_ERR(dsi->dsi_p_clk); 1185 1186 dsi->dsi_p_rst = devm_reset_control_get_optional_exclusive(&pdev->dev, 1187 "dsi_p_rst"); 1188 if (IS_ERR(dsi->dsi_p_rst)) 1189 return PTR_ERR(dsi->dsi_p_rst); 1190 1191 dsi->dsi_sys_clk = devm_clk_get(&pdev->dev, "dsi_sys_clk"); 1192 if (IS_ERR(dsi->dsi_sys_clk)) 1193 return PTR_ERR(dsi->dsi_sys_clk); 1194 1195 irq = platform_get_irq(pdev, 0); 1196 if (irq < 0) 1197 return irq; 1198 1199 dsi->dphy = devm_phy_get(&pdev->dev, "dphy"); 1200 if (IS_ERR(dsi->dphy)) 1201 return PTR_ERR(dsi->dphy); 1202 1203 ret = clk_prepare_enable(dsi->dsi_p_clk); 1204 if (ret) 1205 return ret; 1206 1207 val = readl(dsi->regs + ID_REG); 1208 if (REV_VENDOR_ID(val) != 0xcad) { 1209 dev_err(&pdev->dev, "invalid vendor id\n"); 1210 ret = -EINVAL; 1211 goto err_disable_pclk; 1212 } 1213 1214 dsi->platform_ops = of_device_get_match_data(&pdev->dev); 1215 1216 val = readl(dsi->regs + IP_CONF); 1217 dsi->direct_cmd_fifo_depth = 1 << (DIRCMD_FIFO_DEPTH(val) + 2); 1218 dsi->rx_fifo_depth = RX_FIFO_DEPTH(val); 1219 init_completion(&dsi->direct_cmd_comp); 1220 1221 writel(0, dsi->regs + MCTL_MAIN_DATA_CTL); 1222 writel(0, dsi->regs + MCTL_MAIN_EN); 1223 writel(0, dsi->regs + MCTL_MAIN_PHY_CTL); 1224 1225 /* 1226 * We only support the DPI input, so force input->id to 1227 * CDNS_DPI_INPUT. 1228 */ 1229 input->id = CDNS_DPI_INPUT; 1230 input->bridge.funcs = &cdns_dsi_bridge_funcs; 1231 input->bridge.of_node = pdev->dev.of_node; 1232 1233 /* Mask all interrupts before registering the IRQ handler. */ 1234 writel(0, dsi->regs + MCTL_MAIN_STS_CTL); 1235 writel(0, dsi->regs + MCTL_DPHY_ERR_CTL1); 1236 writel(0, dsi->regs + CMD_MODE_STS_CTL); 1237 writel(0, dsi->regs + DIRECT_CMD_STS_CTL); 1238 writel(0, dsi->regs + DIRECT_CMD_RD_STS_CTL); 1239 writel(0, dsi->regs + VID_MODE_STS_CTL); 1240 writel(0, dsi->regs + TVG_STS_CTL); 1241 writel(0, dsi->regs + DPI_IRQ_EN); 1242 ret = devm_request_irq(&pdev->dev, irq, cdns_dsi_interrupt, 0, 1243 dev_name(&pdev->dev), dsi); 1244 if (ret) 1245 goto err_disable_pclk; 1246 1247 pm_runtime_enable(&pdev->dev); 1248 dsi->base.dev = &pdev->dev; 1249 dsi->base.ops = &cdns_dsi_ops; 1250 1251 if (dsi->platform_ops && dsi->platform_ops->init) { 1252 ret = dsi->platform_ops->init(dsi); 1253 if (ret != 0) { 1254 dev_err(&pdev->dev, "platform initialization failed: %d\n", 1255 ret); 1256 goto err_disable_runtime_pm; 1257 } 1258 } 1259 1260 ret = mipi_dsi_host_register(&dsi->base); 1261 if (ret) 1262 goto err_deinit_platform; 1263 1264 clk_disable_unprepare(dsi->dsi_p_clk); 1265 1266 return 0; 1267 1268 err_deinit_platform: 1269 if (dsi->platform_ops && dsi->platform_ops->deinit) 1270 dsi->platform_ops->deinit(dsi); 1271 1272 err_disable_runtime_pm: 1273 pm_runtime_disable(&pdev->dev); 1274 1275 err_disable_pclk: 1276 clk_disable_unprepare(dsi->dsi_p_clk); 1277 1278 return ret; 1279 } 1280 1281 static void cdns_dsi_drm_remove(struct platform_device *pdev) 1282 { 1283 struct cdns_dsi *dsi = platform_get_drvdata(pdev); 1284 1285 mipi_dsi_host_unregister(&dsi->base); 1286 1287 if (dsi->platform_ops && dsi->platform_ops->deinit) 1288 dsi->platform_ops->deinit(dsi); 1289 1290 pm_runtime_disable(&pdev->dev); 1291 } 1292 1293 static const struct of_device_id cdns_dsi_of_match[] = { 1294 { .compatible = "cdns,dsi" }, 1295 #ifdef CONFIG_DRM_CDNS_DSI_J721E 1296 { .compatible = "ti,j721e-dsi", .data = &dsi_ti_j721e_ops, }, 1297 #endif 1298 { }, 1299 }; 1300 MODULE_DEVICE_TABLE(of, cdns_dsi_of_match); 1301 1302 static struct platform_driver cdns_dsi_platform_driver = { 1303 .probe = cdns_dsi_drm_probe, 1304 .remove_new = cdns_dsi_drm_remove, 1305 .driver = { 1306 .name = "cdns-dsi", 1307 .of_match_table = cdns_dsi_of_match, 1308 .pm = &cdns_dsi_pm_ops, 1309 }, 1310 }; 1311 module_platform_driver(cdns_dsi_platform_driver); 1312 1313 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@bootlin.com>"); 1314 MODULE_DESCRIPTION("Cadence DSI driver"); 1315 MODULE_LICENSE("GPL"); 1316 MODULE_ALIAS("platform:cdns-dsi"); 1317 1318