1 /* 2 * Samsung SoC MIPI DSI Master driver. 3 * 4 * Copyright (c) 2014 Samsung Electronics Co., Ltd 5 * 6 * Contacts: Tomasz Figa <t.figa@samsung.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 as 10 * published by the Free Software Foundation. 11 */ 12 13 #include <drm/drmP.h> 14 #include <drm/drm_crtc_helper.h> 15 #include <drm/drm_mipi_dsi.h> 16 #include <drm/drm_panel.h> 17 18 #include <linux/clk.h> 19 #include <linux/gpio/consumer.h> 20 #include <linux/irq.h> 21 #include <linux/of_device.h> 22 #include <linux/of_gpio.h> 23 #include <linux/phy/phy.h> 24 #include <linux/regulator/consumer.h> 25 #include <linux/component.h> 26 27 #include <video/mipi_display.h> 28 #include <video/videomode.h> 29 30 #include "exynos_drm_crtc.h" 31 #include "exynos_drm_drv.h" 32 33 /* returns true iff both arguments logically differs */ 34 #define NEQV(a, b) (!(a) ^ !(b)) 35 36 #define DSIM_STATUS_REG 0x0 /* Status register */ 37 #define DSIM_SWRST_REG 0x4 /* Software reset register */ 38 #define DSIM_CLKCTRL_REG 0x8 /* Clock control register */ 39 #define DSIM_TIMEOUT_REG 0xc /* Time out register */ 40 #define DSIM_CONFIG_REG 0x10 /* Configuration register */ 41 #define DSIM_ESCMODE_REG 0x14 /* Escape mode register */ 42 43 /* Main display image resolution register */ 44 #define DSIM_MDRESOL_REG 0x18 45 #define DSIM_MVPORCH_REG 0x1c /* Main display Vporch register */ 46 #define DSIM_MHPORCH_REG 0x20 /* Main display Hporch register */ 47 #define DSIM_MSYNC_REG 0x24 /* Main display sync area register */ 48 49 /* Sub display image resolution register */ 50 #define DSIM_SDRESOL_REG 0x28 51 #define DSIM_INTSRC_REG 0x2c /* Interrupt source register */ 52 #define DSIM_INTMSK_REG 0x30 /* Interrupt mask register */ 53 #define DSIM_PKTHDR_REG 0x34 /* Packet Header FIFO register */ 54 #define DSIM_PAYLOAD_REG 0x38 /* Payload FIFO register */ 55 #define DSIM_RXFIFO_REG 0x3c /* Read FIFO register */ 56 #define DSIM_FIFOTHLD_REG 0x40 /* FIFO threshold level register */ 57 #define DSIM_FIFOCTRL_REG 0x44 /* FIFO status and control register */ 58 59 /* FIFO memory AC characteristic register */ 60 #define DSIM_PLLCTRL_REG 0x4c /* PLL control register */ 61 #define DSIM_PHYACCHR_REG 0x54 /* D-PHY AC characteristic register */ 62 #define DSIM_PHYACCHR1_REG 0x58 /* D-PHY AC characteristic register1 */ 63 #define DSIM_PHYCTRL_REG 0x5c 64 #define DSIM_PHYTIMING_REG 0x64 65 #define DSIM_PHYTIMING1_REG 0x68 66 #define DSIM_PHYTIMING2_REG 0x6c 67 68 /* DSIM_STATUS */ 69 #define DSIM_STOP_STATE_DAT(x) (((x) & 0xf) << 0) 70 #define DSIM_STOP_STATE_CLK (1 << 8) 71 #define DSIM_TX_READY_HS_CLK (1 << 10) 72 #define DSIM_PLL_STABLE (1 << 31) 73 74 /* DSIM_SWRST */ 75 #define DSIM_FUNCRST (1 << 16) 76 #define DSIM_SWRST (1 << 0) 77 78 /* DSIM_TIMEOUT */ 79 #define DSIM_LPDR_TIMEOUT(x) ((x) << 0) 80 #define DSIM_BTA_TIMEOUT(x) ((x) << 16) 81 82 /* DSIM_CLKCTRL */ 83 #define DSIM_ESC_PRESCALER(x) (((x) & 0xffff) << 0) 84 #define DSIM_ESC_PRESCALER_MASK (0xffff << 0) 85 #define DSIM_LANE_ESC_CLK_EN_CLK (1 << 19) 86 #define DSIM_LANE_ESC_CLK_EN_DATA(x) (((x) & 0xf) << 20) 87 #define DSIM_LANE_ESC_CLK_EN_DATA_MASK (0xf << 20) 88 #define DSIM_BYTE_CLKEN (1 << 24) 89 #define DSIM_BYTE_CLK_SRC(x) (((x) & 0x3) << 25) 90 #define DSIM_BYTE_CLK_SRC_MASK (0x3 << 25) 91 #define DSIM_PLL_BYPASS (1 << 27) 92 #define DSIM_ESC_CLKEN (1 << 28) 93 #define DSIM_TX_REQUEST_HSCLK (1 << 31) 94 95 /* DSIM_CONFIG */ 96 #define DSIM_LANE_EN_CLK (1 << 0) 97 #define DSIM_LANE_EN(x) (((x) & 0xf) << 1) 98 #define DSIM_NUM_OF_DATA_LANE(x) (((x) & 0x3) << 5) 99 #define DSIM_SUB_PIX_FORMAT(x) (((x) & 0x7) << 8) 100 #define DSIM_MAIN_PIX_FORMAT_MASK (0x7 << 12) 101 #define DSIM_MAIN_PIX_FORMAT_RGB888 (0x7 << 12) 102 #define DSIM_MAIN_PIX_FORMAT_RGB666 (0x6 << 12) 103 #define DSIM_MAIN_PIX_FORMAT_RGB666_P (0x5 << 12) 104 #define DSIM_MAIN_PIX_FORMAT_RGB565 (0x4 << 12) 105 #define DSIM_SUB_VC (((x) & 0x3) << 16) 106 #define DSIM_MAIN_VC (((x) & 0x3) << 18) 107 #define DSIM_HSA_MODE (1 << 20) 108 #define DSIM_HBP_MODE (1 << 21) 109 #define DSIM_HFP_MODE (1 << 22) 110 #define DSIM_HSE_MODE (1 << 23) 111 #define DSIM_AUTO_MODE (1 << 24) 112 #define DSIM_VIDEO_MODE (1 << 25) 113 #define DSIM_BURST_MODE (1 << 26) 114 #define DSIM_SYNC_INFORM (1 << 27) 115 #define DSIM_EOT_DISABLE (1 << 28) 116 #define DSIM_MFLUSH_VS (1 << 29) 117 /* This flag is valid only for exynos3250/3472/4415/5260/5430 */ 118 #define DSIM_CLKLANE_STOP (1 << 30) 119 120 /* DSIM_ESCMODE */ 121 #define DSIM_TX_TRIGGER_RST (1 << 4) 122 #define DSIM_TX_LPDT_LP (1 << 6) 123 #define DSIM_CMD_LPDT_LP (1 << 7) 124 #define DSIM_FORCE_BTA (1 << 16) 125 #define DSIM_FORCE_STOP_STATE (1 << 20) 126 #define DSIM_STOP_STATE_CNT(x) (((x) & 0x7ff) << 21) 127 #define DSIM_STOP_STATE_CNT_MASK (0x7ff << 21) 128 129 /* DSIM_MDRESOL */ 130 #define DSIM_MAIN_STAND_BY (1 << 31) 131 #define DSIM_MAIN_VRESOL(x) (((x) & 0x7ff) << 16) 132 #define DSIM_MAIN_HRESOL(x) (((x) & 0X7ff) << 0) 133 134 /* DSIM_MVPORCH */ 135 #define DSIM_CMD_ALLOW(x) ((x) << 28) 136 #define DSIM_STABLE_VFP(x) ((x) << 16) 137 #define DSIM_MAIN_VBP(x) ((x) << 0) 138 #define DSIM_CMD_ALLOW_MASK (0xf << 28) 139 #define DSIM_STABLE_VFP_MASK (0x7ff << 16) 140 #define DSIM_MAIN_VBP_MASK (0x7ff << 0) 141 142 /* DSIM_MHPORCH */ 143 #define DSIM_MAIN_HFP(x) ((x) << 16) 144 #define DSIM_MAIN_HBP(x) ((x) << 0) 145 #define DSIM_MAIN_HFP_MASK ((0xffff) << 16) 146 #define DSIM_MAIN_HBP_MASK ((0xffff) << 0) 147 148 /* DSIM_MSYNC */ 149 #define DSIM_MAIN_VSA(x) ((x) << 22) 150 #define DSIM_MAIN_HSA(x) ((x) << 0) 151 #define DSIM_MAIN_VSA_MASK ((0x3ff) << 22) 152 #define DSIM_MAIN_HSA_MASK ((0xffff) << 0) 153 154 /* DSIM_SDRESOL */ 155 #define DSIM_SUB_STANDY(x) ((x) << 31) 156 #define DSIM_SUB_VRESOL(x) ((x) << 16) 157 #define DSIM_SUB_HRESOL(x) ((x) << 0) 158 #define DSIM_SUB_STANDY_MASK ((0x1) << 31) 159 #define DSIM_SUB_VRESOL_MASK ((0x7ff) << 16) 160 #define DSIM_SUB_HRESOL_MASK ((0x7ff) << 0) 161 162 /* DSIM_INTSRC */ 163 #define DSIM_INT_PLL_STABLE (1 << 31) 164 #define DSIM_INT_SW_RST_RELEASE (1 << 30) 165 #define DSIM_INT_SFR_FIFO_EMPTY (1 << 29) 166 #define DSIM_INT_BTA (1 << 25) 167 #define DSIM_INT_FRAME_DONE (1 << 24) 168 #define DSIM_INT_RX_TIMEOUT (1 << 21) 169 #define DSIM_INT_BTA_TIMEOUT (1 << 20) 170 #define DSIM_INT_RX_DONE (1 << 18) 171 #define DSIM_INT_RX_TE (1 << 17) 172 #define DSIM_INT_RX_ACK (1 << 16) 173 #define DSIM_INT_RX_ECC_ERR (1 << 15) 174 #define DSIM_INT_RX_CRC_ERR (1 << 14) 175 176 /* DSIM_FIFOCTRL */ 177 #define DSIM_RX_DATA_FULL (1 << 25) 178 #define DSIM_RX_DATA_EMPTY (1 << 24) 179 #define DSIM_SFR_HEADER_FULL (1 << 23) 180 #define DSIM_SFR_HEADER_EMPTY (1 << 22) 181 #define DSIM_SFR_PAYLOAD_FULL (1 << 21) 182 #define DSIM_SFR_PAYLOAD_EMPTY (1 << 20) 183 #define DSIM_I80_HEADER_FULL (1 << 19) 184 #define DSIM_I80_HEADER_EMPTY (1 << 18) 185 #define DSIM_I80_PAYLOAD_FULL (1 << 17) 186 #define DSIM_I80_PAYLOAD_EMPTY (1 << 16) 187 #define DSIM_SD_HEADER_FULL (1 << 15) 188 #define DSIM_SD_HEADER_EMPTY (1 << 14) 189 #define DSIM_SD_PAYLOAD_FULL (1 << 13) 190 #define DSIM_SD_PAYLOAD_EMPTY (1 << 12) 191 #define DSIM_MD_HEADER_FULL (1 << 11) 192 #define DSIM_MD_HEADER_EMPTY (1 << 10) 193 #define DSIM_MD_PAYLOAD_FULL (1 << 9) 194 #define DSIM_MD_PAYLOAD_EMPTY (1 << 8) 195 #define DSIM_RX_FIFO (1 << 4) 196 #define DSIM_SFR_FIFO (1 << 3) 197 #define DSIM_I80_FIFO (1 << 2) 198 #define DSIM_SD_FIFO (1 << 1) 199 #define DSIM_MD_FIFO (1 << 0) 200 201 /* DSIM_PHYACCHR */ 202 #define DSIM_AFC_EN (1 << 14) 203 #define DSIM_AFC_CTL(x) (((x) & 0x7) << 5) 204 205 /* DSIM_PLLCTRL */ 206 #define DSIM_FREQ_BAND(x) ((x) << 24) 207 #define DSIM_PLL_EN (1 << 23) 208 #define DSIM_PLL_P(x) ((x) << 13) 209 #define DSIM_PLL_M(x) ((x) << 4) 210 #define DSIM_PLL_S(x) ((x) << 1) 211 212 /* DSIM_PHYCTRL */ 213 #define DSIM_PHYCTRL_ULPS_EXIT(x) (((x) & 0x1ff) << 0) 214 215 /* DSIM_PHYTIMING */ 216 #define DSIM_PHYTIMING_LPX(x) ((x) << 8) 217 #define DSIM_PHYTIMING_HS_EXIT(x) ((x) << 0) 218 219 /* DSIM_PHYTIMING1 */ 220 #define DSIM_PHYTIMING1_CLK_PREPARE(x) ((x) << 24) 221 #define DSIM_PHYTIMING1_CLK_ZERO(x) ((x) << 16) 222 #define DSIM_PHYTIMING1_CLK_POST(x) ((x) << 8) 223 #define DSIM_PHYTIMING1_CLK_TRAIL(x) ((x) << 0) 224 225 /* DSIM_PHYTIMING2 */ 226 #define DSIM_PHYTIMING2_HS_PREPARE(x) ((x) << 16) 227 #define DSIM_PHYTIMING2_HS_ZERO(x) ((x) << 8) 228 #define DSIM_PHYTIMING2_HS_TRAIL(x) ((x) << 0) 229 230 #define DSI_MAX_BUS_WIDTH 4 231 #define DSI_NUM_VIRTUAL_CHANNELS 4 232 #define DSI_TX_FIFO_SIZE 2048 233 #define DSI_RX_FIFO_SIZE 256 234 #define DSI_XFER_TIMEOUT_MS 100 235 #define DSI_RX_FIFO_EMPTY 0x30800002 236 237 enum exynos_dsi_transfer_type { 238 EXYNOS_DSI_TX, 239 EXYNOS_DSI_RX, 240 }; 241 242 struct exynos_dsi_transfer { 243 struct list_head list; 244 struct completion completed; 245 int result; 246 u8 data_id; 247 u8 data[2]; 248 u16 flags; 249 250 const u8 *tx_payload; 251 u16 tx_len; 252 u16 tx_done; 253 254 u8 *rx_payload; 255 u16 rx_len; 256 u16 rx_done; 257 }; 258 259 #define DSIM_STATE_ENABLED BIT(0) 260 #define DSIM_STATE_INITIALIZED BIT(1) 261 #define DSIM_STATE_CMD_LPM BIT(2) 262 263 struct exynos_dsi_driver_data { 264 unsigned int plltmr_reg; 265 266 unsigned int has_freqband:1; 267 unsigned int has_clklane_stop:1; 268 }; 269 270 struct exynos_dsi { 271 struct exynos_drm_display display; 272 struct mipi_dsi_host dsi_host; 273 struct drm_connector connector; 274 struct device_node *panel_node; 275 struct drm_panel *panel; 276 struct device *dev; 277 278 void __iomem *reg_base; 279 struct phy *phy; 280 struct clk *pll_clk; 281 struct clk *bus_clk; 282 struct regulator_bulk_data supplies[2]; 283 int irq; 284 int te_gpio; 285 286 u32 pll_clk_rate; 287 u32 burst_clk_rate; 288 u32 esc_clk_rate; 289 u32 lanes; 290 u32 mode_flags; 291 u32 format; 292 struct videomode vm; 293 294 int state; 295 struct drm_property *brightness; 296 struct completion completed; 297 298 spinlock_t transfer_lock; /* protects transfer_list */ 299 struct list_head transfer_list; 300 301 struct exynos_dsi_driver_data *driver_data; 302 }; 303 304 #define host_to_dsi(host) container_of(host, struct exynos_dsi, dsi_host) 305 #define connector_to_dsi(c) container_of(c, struct exynos_dsi, connector) 306 307 static inline struct exynos_dsi *display_to_dsi(struct exynos_drm_display *d) 308 { 309 return container_of(d, struct exynos_dsi, display); 310 } 311 312 static struct exynos_dsi_driver_data exynos3_dsi_driver_data = { 313 .plltmr_reg = 0x50, 314 .has_freqband = 1, 315 .has_clklane_stop = 1, 316 }; 317 318 static struct exynos_dsi_driver_data exynos4_dsi_driver_data = { 319 .plltmr_reg = 0x50, 320 .has_freqband = 1, 321 .has_clklane_stop = 1, 322 }; 323 324 static struct exynos_dsi_driver_data exynos4415_dsi_driver_data = { 325 .plltmr_reg = 0x58, 326 .has_clklane_stop = 1, 327 }; 328 329 static struct exynos_dsi_driver_data exynos5_dsi_driver_data = { 330 .plltmr_reg = 0x58, 331 }; 332 333 static struct of_device_id exynos_dsi_of_match[] = { 334 { .compatible = "samsung,exynos3250-mipi-dsi", 335 .data = &exynos3_dsi_driver_data }, 336 { .compatible = "samsung,exynos4210-mipi-dsi", 337 .data = &exynos4_dsi_driver_data }, 338 { .compatible = "samsung,exynos4415-mipi-dsi", 339 .data = &exynos4415_dsi_driver_data }, 340 { .compatible = "samsung,exynos5410-mipi-dsi", 341 .data = &exynos5_dsi_driver_data }, 342 { } 343 }; 344 345 static inline struct exynos_dsi_driver_data *exynos_dsi_get_driver_data( 346 struct platform_device *pdev) 347 { 348 const struct of_device_id *of_id = 349 of_match_device(exynos_dsi_of_match, &pdev->dev); 350 351 return (struct exynos_dsi_driver_data *)of_id->data; 352 } 353 354 static void exynos_dsi_wait_for_reset(struct exynos_dsi *dsi) 355 { 356 if (wait_for_completion_timeout(&dsi->completed, msecs_to_jiffies(300))) 357 return; 358 359 dev_err(dsi->dev, "timeout waiting for reset\n"); 360 } 361 362 static void exynos_dsi_reset(struct exynos_dsi *dsi) 363 { 364 reinit_completion(&dsi->completed); 365 writel(DSIM_SWRST, dsi->reg_base + DSIM_SWRST_REG); 366 } 367 368 #ifndef MHZ 369 #define MHZ (1000*1000) 370 #endif 371 372 static unsigned long exynos_dsi_pll_find_pms(struct exynos_dsi *dsi, 373 unsigned long fin, unsigned long fout, u8 *p, u16 *m, u8 *s) 374 { 375 unsigned long best_freq = 0; 376 u32 min_delta = 0xffffffff; 377 u8 p_min, p_max; 378 u8 _p, uninitialized_var(best_p); 379 u16 _m, uninitialized_var(best_m); 380 u8 _s, uninitialized_var(best_s); 381 382 p_min = DIV_ROUND_UP(fin, (12 * MHZ)); 383 p_max = fin / (6 * MHZ); 384 385 for (_p = p_min; _p <= p_max; ++_p) { 386 for (_s = 0; _s <= 5; ++_s) { 387 u64 tmp; 388 u32 delta; 389 390 tmp = (u64)fout * (_p << _s); 391 do_div(tmp, fin); 392 _m = tmp; 393 if (_m < 41 || _m > 125) 394 continue; 395 396 tmp = (u64)_m * fin; 397 do_div(tmp, _p); 398 if (tmp < 500 * MHZ || tmp > 1000 * MHZ) 399 continue; 400 401 tmp = (u64)_m * fin; 402 do_div(tmp, _p << _s); 403 404 delta = abs(fout - tmp); 405 if (delta < min_delta) { 406 best_p = _p; 407 best_m = _m; 408 best_s = _s; 409 min_delta = delta; 410 best_freq = tmp; 411 } 412 } 413 } 414 415 if (best_freq) { 416 *p = best_p; 417 *m = best_m; 418 *s = best_s; 419 } 420 421 return best_freq; 422 } 423 424 static unsigned long exynos_dsi_set_pll(struct exynos_dsi *dsi, 425 unsigned long freq) 426 { 427 struct exynos_dsi_driver_data *driver_data = dsi->driver_data; 428 unsigned long fin, fout; 429 int timeout; 430 u8 p, s; 431 u16 m; 432 u32 reg; 433 434 clk_set_rate(dsi->pll_clk, dsi->pll_clk_rate); 435 436 fin = clk_get_rate(dsi->pll_clk); 437 if (!fin) { 438 dev_err(dsi->dev, "failed to get PLL clock frequency\n"); 439 return 0; 440 } 441 442 dev_dbg(dsi->dev, "PLL input frequency: %lu\n", fin); 443 444 fout = exynos_dsi_pll_find_pms(dsi, fin, freq, &p, &m, &s); 445 if (!fout) { 446 dev_err(dsi->dev, 447 "failed to find PLL PMS for requested frequency\n"); 448 return 0; 449 } 450 dev_dbg(dsi->dev, "PLL freq %lu, (p %d, m %d, s %d)\n", fout, p, m, s); 451 452 writel(500, dsi->reg_base + driver_data->plltmr_reg); 453 454 reg = DSIM_PLL_EN | DSIM_PLL_P(p) | DSIM_PLL_M(m) | DSIM_PLL_S(s); 455 456 if (driver_data->has_freqband) { 457 static const unsigned long freq_bands[] = { 458 100 * MHZ, 120 * MHZ, 160 * MHZ, 200 * MHZ, 459 270 * MHZ, 320 * MHZ, 390 * MHZ, 450 * MHZ, 460 510 * MHZ, 560 * MHZ, 640 * MHZ, 690 * MHZ, 461 770 * MHZ, 870 * MHZ, 950 * MHZ, 462 }; 463 int band; 464 465 for (band = 0; band < ARRAY_SIZE(freq_bands); ++band) 466 if (fout < freq_bands[band]) 467 break; 468 469 dev_dbg(dsi->dev, "band %d\n", band); 470 471 reg |= DSIM_FREQ_BAND(band); 472 } 473 474 writel(reg, dsi->reg_base + DSIM_PLLCTRL_REG); 475 476 timeout = 1000; 477 do { 478 if (timeout-- == 0) { 479 dev_err(dsi->dev, "PLL failed to stabilize\n"); 480 return 0; 481 } 482 reg = readl(dsi->reg_base + DSIM_STATUS_REG); 483 } while ((reg & DSIM_PLL_STABLE) == 0); 484 485 return fout; 486 } 487 488 static int exynos_dsi_enable_clock(struct exynos_dsi *dsi) 489 { 490 unsigned long hs_clk, byte_clk, esc_clk; 491 unsigned long esc_div; 492 u32 reg; 493 494 hs_clk = exynos_dsi_set_pll(dsi, dsi->burst_clk_rate); 495 if (!hs_clk) { 496 dev_err(dsi->dev, "failed to configure DSI PLL\n"); 497 return -EFAULT; 498 } 499 500 byte_clk = hs_clk / 8; 501 esc_div = DIV_ROUND_UP(byte_clk, dsi->esc_clk_rate); 502 esc_clk = byte_clk / esc_div; 503 504 if (esc_clk > 20 * MHZ) { 505 ++esc_div; 506 esc_clk = byte_clk / esc_div; 507 } 508 509 dev_dbg(dsi->dev, "hs_clk = %lu, byte_clk = %lu, esc_clk = %lu\n", 510 hs_clk, byte_clk, esc_clk); 511 512 reg = readl(dsi->reg_base + DSIM_CLKCTRL_REG); 513 reg &= ~(DSIM_ESC_PRESCALER_MASK | DSIM_LANE_ESC_CLK_EN_CLK 514 | DSIM_LANE_ESC_CLK_EN_DATA_MASK | DSIM_PLL_BYPASS 515 | DSIM_BYTE_CLK_SRC_MASK); 516 reg |= DSIM_ESC_CLKEN | DSIM_BYTE_CLKEN 517 | DSIM_ESC_PRESCALER(esc_div) 518 | DSIM_LANE_ESC_CLK_EN_CLK 519 | DSIM_LANE_ESC_CLK_EN_DATA(BIT(dsi->lanes) - 1) 520 | DSIM_BYTE_CLK_SRC(0) 521 | DSIM_TX_REQUEST_HSCLK; 522 writel(reg, dsi->reg_base + DSIM_CLKCTRL_REG); 523 524 return 0; 525 } 526 527 static void exynos_dsi_set_phy_ctrl(struct exynos_dsi *dsi) 528 { 529 struct exynos_dsi_driver_data *driver_data = dsi->driver_data; 530 u32 reg; 531 532 if (driver_data->has_freqband) 533 return; 534 535 /* B D-PHY: D-PHY Master & Slave Analog Block control */ 536 reg = DSIM_PHYCTRL_ULPS_EXIT(0x0af); 537 writel(reg, dsi->reg_base + DSIM_PHYCTRL_REG); 538 539 /* 540 * T LPX: Transmitted length of any Low-Power state period 541 * T HS-EXIT: Time that the transmitter drives LP-11 following a HS 542 * burst 543 */ 544 reg = DSIM_PHYTIMING_LPX(0x06) | DSIM_PHYTIMING_HS_EXIT(0x0b); 545 writel(reg, dsi->reg_base + DSIM_PHYTIMING_REG); 546 547 /* 548 * T CLK-PREPARE: Time that the transmitter drives the Clock Lane LP-00 549 * Line state immediately before the HS-0 Line state starting the 550 * HS transmission 551 * T CLK-ZERO: Time that the transmitter drives the HS-0 state prior to 552 * transmitting the Clock. 553 * T CLK_POST: Time that the transmitter continues to send HS clock 554 * after the last associated Data Lane has transitioned to LP Mode 555 * Interval is defined as the period from the end of T HS-TRAIL to 556 * the beginning of T CLK-TRAIL 557 * T CLK-TRAIL: Time that the transmitter drives the HS-0 state after 558 * the last payload clock bit of a HS transmission burst 559 */ 560 reg = DSIM_PHYTIMING1_CLK_PREPARE(0x07) | 561 DSIM_PHYTIMING1_CLK_ZERO(0x27) | 562 DSIM_PHYTIMING1_CLK_POST(0x0d) | 563 DSIM_PHYTIMING1_CLK_TRAIL(0x08); 564 writel(reg, dsi->reg_base + DSIM_PHYTIMING1_REG); 565 566 /* 567 * T HS-PREPARE: Time that the transmitter drives the Data Lane LP-00 568 * Line state immediately before the HS-0 Line state starting the 569 * HS transmission 570 * T HS-ZERO: Time that the transmitter drives the HS-0 state prior to 571 * transmitting the Sync sequence. 572 * T HS-TRAIL: Time that the transmitter drives the flipped differential 573 * state after last payload data bit of a HS transmission burst 574 */ 575 reg = DSIM_PHYTIMING2_HS_PREPARE(0x09) | DSIM_PHYTIMING2_HS_ZERO(0x0d) | 576 DSIM_PHYTIMING2_HS_TRAIL(0x0b); 577 writel(reg, dsi->reg_base + DSIM_PHYTIMING2_REG); 578 } 579 580 static void exynos_dsi_disable_clock(struct exynos_dsi *dsi) 581 { 582 u32 reg; 583 584 reg = readl(dsi->reg_base + DSIM_CLKCTRL_REG); 585 reg &= ~(DSIM_LANE_ESC_CLK_EN_CLK | DSIM_LANE_ESC_CLK_EN_DATA_MASK 586 | DSIM_ESC_CLKEN | DSIM_BYTE_CLKEN); 587 writel(reg, dsi->reg_base + DSIM_CLKCTRL_REG); 588 589 reg = readl(dsi->reg_base + DSIM_PLLCTRL_REG); 590 reg &= ~DSIM_PLL_EN; 591 writel(reg, dsi->reg_base + DSIM_PLLCTRL_REG); 592 } 593 594 static int exynos_dsi_init_link(struct exynos_dsi *dsi) 595 { 596 struct exynos_dsi_driver_data *driver_data = dsi->driver_data; 597 int timeout; 598 u32 reg; 599 u32 lanes_mask; 600 601 /* Initialize FIFO pointers */ 602 reg = readl(dsi->reg_base + DSIM_FIFOCTRL_REG); 603 reg &= ~0x1f; 604 writel(reg, dsi->reg_base + DSIM_FIFOCTRL_REG); 605 606 usleep_range(9000, 11000); 607 608 reg |= 0x1f; 609 writel(reg, dsi->reg_base + DSIM_FIFOCTRL_REG); 610 611 usleep_range(9000, 11000); 612 613 /* DSI configuration */ 614 reg = 0; 615 616 /* 617 * The first bit of mode_flags specifies display configuration. 618 * If this bit is set[= MIPI_DSI_MODE_VIDEO], dsi will support video 619 * mode, otherwise it will support command mode. 620 */ 621 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) { 622 reg |= DSIM_VIDEO_MODE; 623 624 /* 625 * The user manual describes that following bits are ignored in 626 * command mode. 627 */ 628 if (!(dsi->mode_flags & MIPI_DSI_MODE_VSYNC_FLUSH)) 629 reg |= DSIM_MFLUSH_VS; 630 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) 631 reg |= DSIM_SYNC_INFORM; 632 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST) 633 reg |= DSIM_BURST_MODE; 634 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_AUTO_VERT) 635 reg |= DSIM_AUTO_MODE; 636 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_HSE) 637 reg |= DSIM_HSE_MODE; 638 if (!(dsi->mode_flags & MIPI_DSI_MODE_VIDEO_HFP)) 639 reg |= DSIM_HFP_MODE; 640 if (!(dsi->mode_flags & MIPI_DSI_MODE_VIDEO_HBP)) 641 reg |= DSIM_HBP_MODE; 642 if (!(dsi->mode_flags & MIPI_DSI_MODE_VIDEO_HSA)) 643 reg |= DSIM_HSA_MODE; 644 } 645 646 if (!(dsi->mode_flags & MIPI_DSI_MODE_EOT_PACKET)) 647 reg |= DSIM_EOT_DISABLE; 648 649 switch (dsi->format) { 650 case MIPI_DSI_FMT_RGB888: 651 reg |= DSIM_MAIN_PIX_FORMAT_RGB888; 652 break; 653 case MIPI_DSI_FMT_RGB666: 654 reg |= DSIM_MAIN_PIX_FORMAT_RGB666; 655 break; 656 case MIPI_DSI_FMT_RGB666_PACKED: 657 reg |= DSIM_MAIN_PIX_FORMAT_RGB666_P; 658 break; 659 case MIPI_DSI_FMT_RGB565: 660 reg |= DSIM_MAIN_PIX_FORMAT_RGB565; 661 break; 662 default: 663 dev_err(dsi->dev, "invalid pixel format\n"); 664 return -EINVAL; 665 } 666 667 reg |= DSIM_NUM_OF_DATA_LANE(dsi->lanes - 1); 668 669 writel(reg, dsi->reg_base + DSIM_CONFIG_REG); 670 671 reg |= DSIM_LANE_EN_CLK; 672 writel(reg, dsi->reg_base + DSIM_CONFIG_REG); 673 674 lanes_mask = BIT(dsi->lanes) - 1; 675 reg |= DSIM_LANE_EN(lanes_mask); 676 writel(reg, dsi->reg_base + DSIM_CONFIG_REG); 677 678 /* 679 * Use non-continuous clock mode if the periparal wants and 680 * host controller supports 681 * 682 * In non-continous clock mode, host controller will turn off 683 * the HS clock between high-speed transmissions to reduce 684 * power consumption. 685 */ 686 if (driver_data->has_clklane_stop && 687 dsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS) { 688 reg |= DSIM_CLKLANE_STOP; 689 writel(reg, dsi->reg_base + DSIM_CONFIG_REG); 690 } 691 692 /* Check clock and data lane state are stop state */ 693 timeout = 100; 694 do { 695 if (timeout-- == 0) { 696 dev_err(dsi->dev, "waiting for bus lanes timed out\n"); 697 return -EFAULT; 698 } 699 700 reg = readl(dsi->reg_base + DSIM_STATUS_REG); 701 if ((reg & DSIM_STOP_STATE_DAT(lanes_mask)) 702 != DSIM_STOP_STATE_DAT(lanes_mask)) 703 continue; 704 } while (!(reg & (DSIM_STOP_STATE_CLK | DSIM_TX_READY_HS_CLK))); 705 706 reg = readl(dsi->reg_base + DSIM_ESCMODE_REG); 707 reg &= ~DSIM_STOP_STATE_CNT_MASK; 708 reg |= DSIM_STOP_STATE_CNT(0xf); 709 writel(reg, dsi->reg_base + DSIM_ESCMODE_REG); 710 711 reg = DSIM_BTA_TIMEOUT(0xff) | DSIM_LPDR_TIMEOUT(0xffff); 712 writel(reg, dsi->reg_base + DSIM_TIMEOUT_REG); 713 714 return 0; 715 } 716 717 static void exynos_dsi_set_display_mode(struct exynos_dsi *dsi) 718 { 719 struct videomode *vm = &dsi->vm; 720 u32 reg; 721 722 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) { 723 reg = DSIM_CMD_ALLOW(0xf) 724 | DSIM_STABLE_VFP(vm->vfront_porch) 725 | DSIM_MAIN_VBP(vm->vback_porch); 726 writel(reg, dsi->reg_base + DSIM_MVPORCH_REG); 727 728 reg = DSIM_MAIN_HFP(vm->hfront_porch) 729 | DSIM_MAIN_HBP(vm->hback_porch); 730 writel(reg, dsi->reg_base + DSIM_MHPORCH_REG); 731 732 reg = DSIM_MAIN_VSA(vm->vsync_len) 733 | DSIM_MAIN_HSA(vm->hsync_len); 734 writel(reg, dsi->reg_base + DSIM_MSYNC_REG); 735 } 736 737 reg = DSIM_MAIN_HRESOL(vm->hactive) | DSIM_MAIN_VRESOL(vm->vactive); 738 writel(reg, dsi->reg_base + DSIM_MDRESOL_REG); 739 740 dev_dbg(dsi->dev, "LCD size = %dx%d\n", vm->hactive, vm->vactive); 741 } 742 743 static void exynos_dsi_set_display_enable(struct exynos_dsi *dsi, bool enable) 744 { 745 u32 reg; 746 747 reg = readl(dsi->reg_base + DSIM_MDRESOL_REG); 748 if (enable) 749 reg |= DSIM_MAIN_STAND_BY; 750 else 751 reg &= ~DSIM_MAIN_STAND_BY; 752 writel(reg, dsi->reg_base + DSIM_MDRESOL_REG); 753 } 754 755 static int exynos_dsi_wait_for_hdr_fifo(struct exynos_dsi *dsi) 756 { 757 int timeout = 2000; 758 759 do { 760 u32 reg = readl(dsi->reg_base + DSIM_FIFOCTRL_REG); 761 762 if (!(reg & DSIM_SFR_HEADER_FULL)) 763 return 0; 764 765 if (!cond_resched()) 766 usleep_range(950, 1050); 767 } while (--timeout); 768 769 return -ETIMEDOUT; 770 } 771 772 static void exynos_dsi_set_cmd_lpm(struct exynos_dsi *dsi, bool lpm) 773 { 774 u32 v = readl(dsi->reg_base + DSIM_ESCMODE_REG); 775 776 if (lpm) 777 v |= DSIM_CMD_LPDT_LP; 778 else 779 v &= ~DSIM_CMD_LPDT_LP; 780 781 writel(v, dsi->reg_base + DSIM_ESCMODE_REG); 782 } 783 784 static void exynos_dsi_force_bta(struct exynos_dsi *dsi) 785 { 786 u32 v = readl(dsi->reg_base + DSIM_ESCMODE_REG); 787 788 v |= DSIM_FORCE_BTA; 789 writel(v, dsi->reg_base + DSIM_ESCMODE_REG); 790 } 791 792 static void exynos_dsi_send_to_fifo(struct exynos_dsi *dsi, 793 struct exynos_dsi_transfer *xfer) 794 { 795 struct device *dev = dsi->dev; 796 const u8 *payload = xfer->tx_payload + xfer->tx_done; 797 u16 length = xfer->tx_len - xfer->tx_done; 798 bool first = !xfer->tx_done; 799 u32 reg; 800 801 dev_dbg(dev, "< xfer %p: tx len %u, done %u, rx len %u, done %u\n", 802 xfer, xfer->tx_len, xfer->tx_done, xfer->rx_len, xfer->rx_done); 803 804 if (length > DSI_TX_FIFO_SIZE) 805 length = DSI_TX_FIFO_SIZE; 806 807 xfer->tx_done += length; 808 809 /* Send payload */ 810 while (length >= 4) { 811 reg = (payload[3] << 24) | (payload[2] << 16) 812 | (payload[1] << 8) | payload[0]; 813 writel(reg, dsi->reg_base + DSIM_PAYLOAD_REG); 814 payload += 4; 815 length -= 4; 816 } 817 818 reg = 0; 819 switch (length) { 820 case 3: 821 reg |= payload[2] << 16; 822 /* Fall through */ 823 case 2: 824 reg |= payload[1] << 8; 825 /* Fall through */ 826 case 1: 827 reg |= payload[0]; 828 writel(reg, dsi->reg_base + DSIM_PAYLOAD_REG); 829 break; 830 case 0: 831 /* Do nothing */ 832 break; 833 } 834 835 /* Send packet header */ 836 if (!first) 837 return; 838 839 reg = (xfer->data[1] << 16) | (xfer->data[0] << 8) | xfer->data_id; 840 if (exynos_dsi_wait_for_hdr_fifo(dsi)) { 841 dev_err(dev, "waiting for header FIFO timed out\n"); 842 return; 843 } 844 845 if (NEQV(xfer->flags & MIPI_DSI_MSG_USE_LPM, 846 dsi->state & DSIM_STATE_CMD_LPM)) { 847 exynos_dsi_set_cmd_lpm(dsi, xfer->flags & MIPI_DSI_MSG_USE_LPM); 848 dsi->state ^= DSIM_STATE_CMD_LPM; 849 } 850 851 writel(reg, dsi->reg_base + DSIM_PKTHDR_REG); 852 853 if (xfer->flags & MIPI_DSI_MSG_REQ_ACK) 854 exynos_dsi_force_bta(dsi); 855 } 856 857 static void exynos_dsi_read_from_fifo(struct exynos_dsi *dsi, 858 struct exynos_dsi_transfer *xfer) 859 { 860 u8 *payload = xfer->rx_payload + xfer->rx_done; 861 bool first = !xfer->rx_done; 862 struct device *dev = dsi->dev; 863 u16 length; 864 u32 reg; 865 866 if (first) { 867 reg = readl(dsi->reg_base + DSIM_RXFIFO_REG); 868 869 switch (reg & 0x3f) { 870 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE: 871 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE: 872 if (xfer->rx_len >= 2) { 873 payload[1] = reg >> 16; 874 ++xfer->rx_done; 875 } 876 /* Fall through */ 877 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE: 878 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE: 879 payload[0] = reg >> 8; 880 ++xfer->rx_done; 881 xfer->rx_len = xfer->rx_done; 882 xfer->result = 0; 883 goto clear_fifo; 884 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT: 885 dev_err(dev, "DSI Error Report: 0x%04x\n", 886 (reg >> 8) & 0xffff); 887 xfer->result = 0; 888 goto clear_fifo; 889 } 890 891 length = (reg >> 8) & 0xffff; 892 if (length > xfer->rx_len) { 893 dev_err(dev, 894 "response too long (%u > %u bytes), stripping\n", 895 xfer->rx_len, length); 896 length = xfer->rx_len; 897 } else if (length < xfer->rx_len) 898 xfer->rx_len = length; 899 } 900 901 length = xfer->rx_len - xfer->rx_done; 902 xfer->rx_done += length; 903 904 /* Receive payload */ 905 while (length >= 4) { 906 reg = readl(dsi->reg_base + DSIM_RXFIFO_REG); 907 payload[0] = (reg >> 0) & 0xff; 908 payload[1] = (reg >> 8) & 0xff; 909 payload[2] = (reg >> 16) & 0xff; 910 payload[3] = (reg >> 24) & 0xff; 911 payload += 4; 912 length -= 4; 913 } 914 915 if (length) { 916 reg = readl(dsi->reg_base + DSIM_RXFIFO_REG); 917 switch (length) { 918 case 3: 919 payload[2] = (reg >> 16) & 0xff; 920 /* Fall through */ 921 case 2: 922 payload[1] = (reg >> 8) & 0xff; 923 /* Fall through */ 924 case 1: 925 payload[0] = reg & 0xff; 926 } 927 } 928 929 if (xfer->rx_done == xfer->rx_len) 930 xfer->result = 0; 931 932 clear_fifo: 933 length = DSI_RX_FIFO_SIZE / 4; 934 do { 935 reg = readl(dsi->reg_base + DSIM_RXFIFO_REG); 936 if (reg == DSI_RX_FIFO_EMPTY) 937 break; 938 } while (--length); 939 } 940 941 static void exynos_dsi_transfer_start(struct exynos_dsi *dsi) 942 { 943 unsigned long flags; 944 struct exynos_dsi_transfer *xfer; 945 bool start = false; 946 947 again: 948 spin_lock_irqsave(&dsi->transfer_lock, flags); 949 950 if (list_empty(&dsi->transfer_list)) { 951 spin_unlock_irqrestore(&dsi->transfer_lock, flags); 952 return; 953 } 954 955 xfer = list_first_entry(&dsi->transfer_list, 956 struct exynos_dsi_transfer, list); 957 958 spin_unlock_irqrestore(&dsi->transfer_lock, flags); 959 960 if (xfer->tx_len && xfer->tx_done == xfer->tx_len) 961 /* waiting for RX */ 962 return; 963 964 exynos_dsi_send_to_fifo(dsi, xfer); 965 966 if (xfer->tx_len || xfer->rx_len) 967 return; 968 969 xfer->result = 0; 970 complete(&xfer->completed); 971 972 spin_lock_irqsave(&dsi->transfer_lock, flags); 973 974 list_del_init(&xfer->list); 975 start = !list_empty(&dsi->transfer_list); 976 977 spin_unlock_irqrestore(&dsi->transfer_lock, flags); 978 979 if (start) 980 goto again; 981 } 982 983 static bool exynos_dsi_transfer_finish(struct exynos_dsi *dsi) 984 { 985 struct exynos_dsi_transfer *xfer; 986 unsigned long flags; 987 bool start = true; 988 989 spin_lock_irqsave(&dsi->transfer_lock, flags); 990 991 if (list_empty(&dsi->transfer_list)) { 992 spin_unlock_irqrestore(&dsi->transfer_lock, flags); 993 return false; 994 } 995 996 xfer = list_first_entry(&dsi->transfer_list, 997 struct exynos_dsi_transfer, list); 998 999 spin_unlock_irqrestore(&dsi->transfer_lock, flags); 1000 1001 dev_dbg(dsi->dev, 1002 "> xfer %p, tx_len %u, tx_done %u, rx_len %u, rx_done %u\n", 1003 xfer, xfer->tx_len, xfer->tx_done, xfer->rx_len, xfer->rx_done); 1004 1005 if (xfer->tx_done != xfer->tx_len) 1006 return true; 1007 1008 if (xfer->rx_done != xfer->rx_len) 1009 exynos_dsi_read_from_fifo(dsi, xfer); 1010 1011 if (xfer->rx_done != xfer->rx_len) 1012 return true; 1013 1014 spin_lock_irqsave(&dsi->transfer_lock, flags); 1015 1016 list_del_init(&xfer->list); 1017 start = !list_empty(&dsi->transfer_list); 1018 1019 spin_unlock_irqrestore(&dsi->transfer_lock, flags); 1020 1021 if (!xfer->rx_len) 1022 xfer->result = 0; 1023 complete(&xfer->completed); 1024 1025 return start; 1026 } 1027 1028 static void exynos_dsi_remove_transfer(struct exynos_dsi *dsi, 1029 struct exynos_dsi_transfer *xfer) 1030 { 1031 unsigned long flags; 1032 bool start; 1033 1034 spin_lock_irqsave(&dsi->transfer_lock, flags); 1035 1036 if (!list_empty(&dsi->transfer_list) && 1037 xfer == list_first_entry(&dsi->transfer_list, 1038 struct exynos_dsi_transfer, list)) { 1039 list_del_init(&xfer->list); 1040 start = !list_empty(&dsi->transfer_list); 1041 spin_unlock_irqrestore(&dsi->transfer_lock, flags); 1042 if (start) 1043 exynos_dsi_transfer_start(dsi); 1044 return; 1045 } 1046 1047 list_del_init(&xfer->list); 1048 1049 spin_unlock_irqrestore(&dsi->transfer_lock, flags); 1050 } 1051 1052 static int exynos_dsi_transfer(struct exynos_dsi *dsi, 1053 struct exynos_dsi_transfer *xfer) 1054 { 1055 unsigned long flags; 1056 bool stopped; 1057 1058 xfer->tx_done = 0; 1059 xfer->rx_done = 0; 1060 xfer->result = -ETIMEDOUT; 1061 init_completion(&xfer->completed); 1062 1063 spin_lock_irqsave(&dsi->transfer_lock, flags); 1064 1065 stopped = list_empty(&dsi->transfer_list); 1066 list_add_tail(&xfer->list, &dsi->transfer_list); 1067 1068 spin_unlock_irqrestore(&dsi->transfer_lock, flags); 1069 1070 if (stopped) 1071 exynos_dsi_transfer_start(dsi); 1072 1073 wait_for_completion_timeout(&xfer->completed, 1074 msecs_to_jiffies(DSI_XFER_TIMEOUT_MS)); 1075 if (xfer->result == -ETIMEDOUT) { 1076 exynos_dsi_remove_transfer(dsi, xfer); 1077 dev_err(dsi->dev, "xfer timed out: %*ph %*ph\n", 2, xfer->data, 1078 xfer->tx_len, xfer->tx_payload); 1079 return -ETIMEDOUT; 1080 } 1081 1082 /* Also covers hardware timeout condition */ 1083 return xfer->result; 1084 } 1085 1086 static irqreturn_t exynos_dsi_irq(int irq, void *dev_id) 1087 { 1088 struct exynos_dsi *dsi = dev_id; 1089 u32 status; 1090 1091 status = readl(dsi->reg_base + DSIM_INTSRC_REG); 1092 if (!status) { 1093 static unsigned long int j; 1094 if (printk_timed_ratelimit(&j, 500)) 1095 dev_warn(dsi->dev, "spurious interrupt\n"); 1096 return IRQ_HANDLED; 1097 } 1098 writel(status, dsi->reg_base + DSIM_INTSRC_REG); 1099 1100 if (status & DSIM_INT_SW_RST_RELEASE) { 1101 u32 mask = ~(DSIM_INT_RX_DONE | DSIM_INT_SFR_FIFO_EMPTY); 1102 writel(mask, dsi->reg_base + DSIM_INTMSK_REG); 1103 complete(&dsi->completed); 1104 return IRQ_HANDLED; 1105 } 1106 1107 if (!(status & (DSIM_INT_RX_DONE | DSIM_INT_SFR_FIFO_EMPTY))) 1108 return IRQ_HANDLED; 1109 1110 if (exynos_dsi_transfer_finish(dsi)) 1111 exynos_dsi_transfer_start(dsi); 1112 1113 return IRQ_HANDLED; 1114 } 1115 1116 static irqreturn_t exynos_dsi_te_irq_handler(int irq, void *dev_id) 1117 { 1118 struct exynos_dsi *dsi = (struct exynos_dsi *)dev_id; 1119 struct drm_encoder *encoder = dsi->display.encoder; 1120 1121 if (dsi->state & DSIM_STATE_ENABLED) 1122 exynos_drm_crtc_te_handler(encoder->crtc); 1123 1124 return IRQ_HANDLED; 1125 } 1126 1127 static void exynos_dsi_enable_irq(struct exynos_dsi *dsi) 1128 { 1129 enable_irq(dsi->irq); 1130 1131 if (gpio_is_valid(dsi->te_gpio)) 1132 enable_irq(gpio_to_irq(dsi->te_gpio)); 1133 } 1134 1135 static void exynos_dsi_disable_irq(struct exynos_dsi *dsi) 1136 { 1137 if (gpio_is_valid(dsi->te_gpio)) 1138 disable_irq(gpio_to_irq(dsi->te_gpio)); 1139 1140 disable_irq(dsi->irq); 1141 } 1142 1143 static int exynos_dsi_init(struct exynos_dsi *dsi) 1144 { 1145 exynos_dsi_reset(dsi); 1146 exynos_dsi_enable_irq(dsi); 1147 exynos_dsi_enable_clock(dsi); 1148 exynos_dsi_wait_for_reset(dsi); 1149 exynos_dsi_set_phy_ctrl(dsi); 1150 exynos_dsi_init_link(dsi); 1151 1152 return 0; 1153 } 1154 1155 static int exynos_dsi_register_te_irq(struct exynos_dsi *dsi) 1156 { 1157 int ret; 1158 int te_gpio_irq; 1159 1160 dsi->te_gpio = of_get_named_gpio(dsi->panel_node, "te-gpios", 0); 1161 if (!gpio_is_valid(dsi->te_gpio)) { 1162 dev_err(dsi->dev, "no te-gpios specified\n"); 1163 ret = dsi->te_gpio; 1164 goto out; 1165 } 1166 1167 ret = gpio_request_one(dsi->te_gpio, GPIOF_IN, "te_gpio"); 1168 if (ret) { 1169 dev_err(dsi->dev, "gpio request failed with %d\n", ret); 1170 goto out; 1171 } 1172 1173 te_gpio_irq = gpio_to_irq(dsi->te_gpio); 1174 1175 irq_set_status_flags(te_gpio_irq, IRQ_NOAUTOEN); 1176 ret = request_threaded_irq(te_gpio_irq, exynos_dsi_te_irq_handler, NULL, 1177 IRQF_TRIGGER_RISING, "TE", dsi); 1178 if (ret) { 1179 dev_err(dsi->dev, "request interrupt failed with %d\n", ret); 1180 gpio_free(dsi->te_gpio); 1181 goto out; 1182 } 1183 1184 out: 1185 return ret; 1186 } 1187 1188 static void exynos_dsi_unregister_te_irq(struct exynos_dsi *dsi) 1189 { 1190 if (gpio_is_valid(dsi->te_gpio)) { 1191 free_irq(gpio_to_irq(dsi->te_gpio), dsi); 1192 gpio_free(dsi->te_gpio); 1193 dsi->te_gpio = -ENOENT; 1194 } 1195 } 1196 1197 static int exynos_dsi_host_attach(struct mipi_dsi_host *host, 1198 struct mipi_dsi_device *device) 1199 { 1200 struct exynos_dsi *dsi = host_to_dsi(host); 1201 1202 dsi->lanes = device->lanes; 1203 dsi->format = device->format; 1204 dsi->mode_flags = device->mode_flags; 1205 dsi->panel_node = device->dev.of_node; 1206 1207 /* 1208 * This is a temporary solution and should be made by more generic way. 1209 * 1210 * If attached panel device is for command mode one, dsi should register 1211 * TE interrupt handler. 1212 */ 1213 if (!(dsi->mode_flags & MIPI_DSI_MODE_VIDEO)) { 1214 int ret = exynos_dsi_register_te_irq(dsi); 1215 1216 if (ret) 1217 return ret; 1218 } 1219 1220 if (dsi->connector.dev) 1221 drm_helper_hpd_irq_event(dsi->connector.dev); 1222 1223 return 0; 1224 } 1225 1226 static int exynos_dsi_host_detach(struct mipi_dsi_host *host, 1227 struct mipi_dsi_device *device) 1228 { 1229 struct exynos_dsi *dsi = host_to_dsi(host); 1230 1231 exynos_dsi_unregister_te_irq(dsi); 1232 1233 dsi->panel_node = NULL; 1234 1235 if (dsi->connector.dev) 1236 drm_helper_hpd_irq_event(dsi->connector.dev); 1237 1238 return 0; 1239 } 1240 1241 /* distinguish between short and long DSI packet types */ 1242 static bool exynos_dsi_is_short_dsi_type(u8 type) 1243 { 1244 return (type & 0x0f) <= 8; 1245 } 1246 1247 static ssize_t exynos_dsi_host_transfer(struct mipi_dsi_host *host, 1248 const struct mipi_dsi_msg *msg) 1249 { 1250 struct exynos_dsi *dsi = host_to_dsi(host); 1251 struct exynos_dsi_transfer xfer; 1252 int ret; 1253 1254 if (!(dsi->state & DSIM_STATE_INITIALIZED)) { 1255 ret = exynos_dsi_init(dsi); 1256 if (ret) 1257 return ret; 1258 dsi->state |= DSIM_STATE_INITIALIZED; 1259 } 1260 1261 if (msg->tx_len == 0) 1262 return -EINVAL; 1263 1264 xfer.data_id = msg->type | (msg->channel << 6); 1265 1266 if (exynos_dsi_is_short_dsi_type(msg->type)) { 1267 const char *tx_buf = msg->tx_buf; 1268 1269 if (msg->tx_len > 2) 1270 return -EINVAL; 1271 xfer.tx_len = 0; 1272 xfer.data[0] = tx_buf[0]; 1273 xfer.data[1] = (msg->tx_len == 2) ? tx_buf[1] : 0; 1274 } else { 1275 xfer.tx_len = msg->tx_len; 1276 xfer.data[0] = msg->tx_len & 0xff; 1277 xfer.data[1] = msg->tx_len >> 8; 1278 xfer.tx_payload = msg->tx_buf; 1279 } 1280 1281 xfer.rx_len = msg->rx_len; 1282 xfer.rx_payload = msg->rx_buf; 1283 xfer.flags = msg->flags; 1284 1285 ret = exynos_dsi_transfer(dsi, &xfer); 1286 return (ret < 0) ? ret : xfer.rx_done; 1287 } 1288 1289 static const struct mipi_dsi_host_ops exynos_dsi_ops = { 1290 .attach = exynos_dsi_host_attach, 1291 .detach = exynos_dsi_host_detach, 1292 .transfer = exynos_dsi_host_transfer, 1293 }; 1294 1295 static int exynos_dsi_poweron(struct exynos_dsi *dsi) 1296 { 1297 int ret; 1298 1299 ret = regulator_bulk_enable(ARRAY_SIZE(dsi->supplies), dsi->supplies); 1300 if (ret < 0) { 1301 dev_err(dsi->dev, "cannot enable regulators %d\n", ret); 1302 return ret; 1303 } 1304 1305 ret = clk_prepare_enable(dsi->bus_clk); 1306 if (ret < 0) { 1307 dev_err(dsi->dev, "cannot enable bus clock %d\n", ret); 1308 goto err_bus_clk; 1309 } 1310 1311 ret = clk_prepare_enable(dsi->pll_clk); 1312 if (ret < 0) { 1313 dev_err(dsi->dev, "cannot enable pll clock %d\n", ret); 1314 goto err_pll_clk; 1315 } 1316 1317 ret = phy_power_on(dsi->phy); 1318 if (ret < 0) { 1319 dev_err(dsi->dev, "cannot enable phy %d\n", ret); 1320 goto err_phy; 1321 } 1322 1323 return 0; 1324 1325 err_phy: 1326 clk_disable_unprepare(dsi->pll_clk); 1327 err_pll_clk: 1328 clk_disable_unprepare(dsi->bus_clk); 1329 err_bus_clk: 1330 regulator_bulk_disable(ARRAY_SIZE(dsi->supplies), dsi->supplies); 1331 1332 return ret; 1333 } 1334 1335 static void exynos_dsi_poweroff(struct exynos_dsi *dsi) 1336 { 1337 int ret; 1338 1339 usleep_range(10000, 20000); 1340 1341 if (dsi->state & DSIM_STATE_INITIALIZED) { 1342 dsi->state &= ~DSIM_STATE_INITIALIZED; 1343 1344 exynos_dsi_disable_clock(dsi); 1345 1346 exynos_dsi_disable_irq(dsi); 1347 } 1348 1349 dsi->state &= ~DSIM_STATE_CMD_LPM; 1350 1351 phy_power_off(dsi->phy); 1352 1353 clk_disable_unprepare(dsi->pll_clk); 1354 clk_disable_unprepare(dsi->bus_clk); 1355 1356 ret = regulator_bulk_disable(ARRAY_SIZE(dsi->supplies), dsi->supplies); 1357 if (ret < 0) 1358 dev_err(dsi->dev, "cannot disable regulators %d\n", ret); 1359 } 1360 1361 static int exynos_dsi_enable(struct exynos_dsi *dsi) 1362 { 1363 int ret; 1364 1365 if (dsi->state & DSIM_STATE_ENABLED) 1366 return 0; 1367 1368 ret = exynos_dsi_poweron(dsi); 1369 if (ret < 0) 1370 return ret; 1371 1372 ret = drm_panel_prepare(dsi->panel); 1373 if (ret < 0) { 1374 exynos_dsi_poweroff(dsi); 1375 return ret; 1376 } 1377 1378 exynos_dsi_set_display_mode(dsi); 1379 exynos_dsi_set_display_enable(dsi, true); 1380 1381 dsi->state |= DSIM_STATE_ENABLED; 1382 1383 ret = drm_panel_enable(dsi->panel); 1384 if (ret < 0) { 1385 dsi->state &= ~DSIM_STATE_ENABLED; 1386 exynos_dsi_set_display_enable(dsi, false); 1387 drm_panel_unprepare(dsi->panel); 1388 exynos_dsi_poweroff(dsi); 1389 return ret; 1390 } 1391 1392 return 0; 1393 } 1394 1395 static void exynos_dsi_disable(struct exynos_dsi *dsi) 1396 { 1397 if (!(dsi->state & DSIM_STATE_ENABLED)) 1398 return; 1399 1400 drm_panel_disable(dsi->panel); 1401 exynos_dsi_set_display_enable(dsi, false); 1402 drm_panel_unprepare(dsi->panel); 1403 exynos_dsi_poweroff(dsi); 1404 1405 dsi->state &= ~DSIM_STATE_ENABLED; 1406 } 1407 1408 static void exynos_dsi_dpms(struct exynos_drm_display *display, int mode) 1409 { 1410 struct exynos_dsi *dsi = display_to_dsi(display); 1411 1412 if (dsi->panel) { 1413 switch (mode) { 1414 case DRM_MODE_DPMS_ON: 1415 exynos_dsi_enable(dsi); 1416 break; 1417 case DRM_MODE_DPMS_STANDBY: 1418 case DRM_MODE_DPMS_SUSPEND: 1419 case DRM_MODE_DPMS_OFF: 1420 exynos_dsi_disable(dsi); 1421 break; 1422 default: 1423 break; 1424 } 1425 } 1426 } 1427 1428 static enum drm_connector_status 1429 exynos_dsi_detect(struct drm_connector *connector, bool force) 1430 { 1431 struct exynos_dsi *dsi = connector_to_dsi(connector); 1432 1433 if (!dsi->panel) { 1434 dsi->panel = of_drm_find_panel(dsi->panel_node); 1435 if (dsi->panel) 1436 drm_panel_attach(dsi->panel, &dsi->connector); 1437 } else if (!dsi->panel_node) { 1438 struct exynos_drm_display *display; 1439 1440 display = platform_get_drvdata(to_platform_device(dsi->dev)); 1441 exynos_dsi_dpms(display, DRM_MODE_DPMS_OFF); 1442 drm_panel_detach(dsi->panel); 1443 dsi->panel = NULL; 1444 } 1445 1446 if (dsi->panel) 1447 return connector_status_connected; 1448 1449 return connector_status_disconnected; 1450 } 1451 1452 static void exynos_dsi_connector_destroy(struct drm_connector *connector) 1453 { 1454 drm_connector_unregister(connector); 1455 drm_connector_cleanup(connector); 1456 connector->dev = NULL; 1457 } 1458 1459 static struct drm_connector_funcs exynos_dsi_connector_funcs = { 1460 .dpms = drm_helper_connector_dpms, 1461 .detect = exynos_dsi_detect, 1462 .fill_modes = drm_helper_probe_single_connector_modes, 1463 .destroy = exynos_dsi_connector_destroy, 1464 }; 1465 1466 static int exynos_dsi_get_modes(struct drm_connector *connector) 1467 { 1468 struct exynos_dsi *dsi = connector_to_dsi(connector); 1469 1470 if (dsi->panel) 1471 return dsi->panel->funcs->get_modes(dsi->panel); 1472 1473 return 0; 1474 } 1475 1476 static int exynos_dsi_mode_valid(struct drm_connector *connector, 1477 struct drm_display_mode *mode) 1478 { 1479 return MODE_OK; 1480 } 1481 1482 static struct drm_encoder * 1483 exynos_dsi_best_encoder(struct drm_connector *connector) 1484 { 1485 struct exynos_dsi *dsi = connector_to_dsi(connector); 1486 1487 return dsi->display.encoder; 1488 } 1489 1490 static struct drm_connector_helper_funcs exynos_dsi_connector_helper_funcs = { 1491 .get_modes = exynos_dsi_get_modes, 1492 .mode_valid = exynos_dsi_mode_valid, 1493 .best_encoder = exynos_dsi_best_encoder, 1494 }; 1495 1496 static int exynos_dsi_create_connector(struct exynos_drm_display *display, 1497 struct drm_encoder *encoder) 1498 { 1499 struct exynos_dsi *dsi = display_to_dsi(display); 1500 struct drm_connector *connector = &dsi->connector; 1501 int ret; 1502 1503 connector->polled = DRM_CONNECTOR_POLL_HPD; 1504 1505 ret = drm_connector_init(encoder->dev, connector, 1506 &exynos_dsi_connector_funcs, 1507 DRM_MODE_CONNECTOR_DSI); 1508 if (ret) { 1509 DRM_ERROR("Failed to initialize connector with drm\n"); 1510 return ret; 1511 } 1512 1513 drm_connector_helper_add(connector, &exynos_dsi_connector_helper_funcs); 1514 drm_connector_register(connector); 1515 drm_mode_connector_attach_encoder(connector, encoder); 1516 1517 return 0; 1518 } 1519 1520 static void exynos_dsi_mode_set(struct exynos_drm_display *display, 1521 struct drm_display_mode *mode) 1522 { 1523 struct exynos_dsi *dsi = display_to_dsi(display); 1524 struct videomode *vm = &dsi->vm; 1525 1526 vm->hactive = mode->hdisplay; 1527 vm->vactive = mode->vdisplay; 1528 vm->vfront_porch = mode->vsync_start - mode->vdisplay; 1529 vm->vback_porch = mode->vtotal - mode->vsync_end; 1530 vm->vsync_len = mode->vsync_end - mode->vsync_start; 1531 vm->hfront_porch = mode->hsync_start - mode->hdisplay; 1532 vm->hback_porch = mode->htotal - mode->hsync_end; 1533 vm->hsync_len = mode->hsync_end - mode->hsync_start; 1534 } 1535 1536 static struct exynos_drm_display_ops exynos_dsi_display_ops = { 1537 .create_connector = exynos_dsi_create_connector, 1538 .mode_set = exynos_dsi_mode_set, 1539 .dpms = exynos_dsi_dpms 1540 }; 1541 1542 MODULE_DEVICE_TABLE(of, exynos_dsi_of_match); 1543 1544 /* of_* functions will be removed after merge of of_graph patches */ 1545 static struct device_node * 1546 of_get_child_by_name_reg(struct device_node *parent, const char *name, u32 reg) 1547 { 1548 struct device_node *np; 1549 1550 for_each_child_of_node(parent, np) { 1551 u32 r; 1552 1553 if (!np->name || of_node_cmp(np->name, name)) 1554 continue; 1555 1556 if (of_property_read_u32(np, "reg", &r) < 0) 1557 r = 0; 1558 1559 if (reg == r) 1560 break; 1561 } 1562 1563 return np; 1564 } 1565 1566 static struct device_node *of_graph_get_port_by_reg(struct device_node *parent, 1567 u32 reg) 1568 { 1569 struct device_node *ports, *port; 1570 1571 ports = of_get_child_by_name(parent, "ports"); 1572 if (ports) 1573 parent = ports; 1574 1575 port = of_get_child_by_name_reg(parent, "port", reg); 1576 1577 of_node_put(ports); 1578 1579 return port; 1580 } 1581 1582 static struct device_node * 1583 of_graph_get_endpoint_by_reg(struct device_node *port, u32 reg) 1584 { 1585 return of_get_child_by_name_reg(port, "endpoint", reg); 1586 } 1587 1588 static int exynos_dsi_of_read_u32(const struct device_node *np, 1589 const char *propname, u32 *out_value) 1590 { 1591 int ret = of_property_read_u32(np, propname, out_value); 1592 1593 if (ret < 0) 1594 pr_err("%s: failed to get '%s' property\n", np->full_name, 1595 propname); 1596 1597 return ret; 1598 } 1599 1600 enum { 1601 DSI_PORT_IN, 1602 DSI_PORT_OUT 1603 }; 1604 1605 static int exynos_dsi_parse_dt(struct exynos_dsi *dsi) 1606 { 1607 struct device *dev = dsi->dev; 1608 struct device_node *node = dev->of_node; 1609 struct device_node *port, *ep; 1610 int ret; 1611 1612 ret = exynos_dsi_of_read_u32(node, "samsung,pll-clock-frequency", 1613 &dsi->pll_clk_rate); 1614 if (ret < 0) 1615 return ret; 1616 1617 port = of_graph_get_port_by_reg(node, DSI_PORT_OUT); 1618 if (!port) { 1619 dev_err(dev, "no output port specified\n"); 1620 return -EINVAL; 1621 } 1622 1623 ep = of_graph_get_endpoint_by_reg(port, 0); 1624 of_node_put(port); 1625 if (!ep) { 1626 dev_err(dev, "no endpoint specified in output port\n"); 1627 return -EINVAL; 1628 } 1629 1630 ret = exynos_dsi_of_read_u32(ep, "samsung,burst-clock-frequency", 1631 &dsi->burst_clk_rate); 1632 if (ret < 0) 1633 goto end; 1634 1635 ret = exynos_dsi_of_read_u32(ep, "samsung,esc-clock-frequency", 1636 &dsi->esc_clk_rate); 1637 1638 end: 1639 of_node_put(ep); 1640 1641 return ret; 1642 } 1643 1644 static int exynos_dsi_bind(struct device *dev, struct device *master, 1645 void *data) 1646 { 1647 struct exynos_drm_display *display = dev_get_drvdata(dev); 1648 struct exynos_dsi *dsi = display_to_dsi(display); 1649 struct drm_device *drm_dev = data; 1650 int ret; 1651 1652 ret = exynos_drm_create_enc_conn(drm_dev, display); 1653 if (ret) { 1654 DRM_ERROR("Encoder create [%d] failed with %d\n", 1655 display->type, ret); 1656 return ret; 1657 } 1658 1659 return mipi_dsi_host_register(&dsi->dsi_host); 1660 } 1661 1662 static void exynos_dsi_unbind(struct device *dev, struct device *master, 1663 void *data) 1664 { 1665 struct exynos_drm_display *display = dev_get_drvdata(dev); 1666 struct exynos_dsi *dsi = display_to_dsi(display); 1667 1668 exynos_dsi_dpms(display, DRM_MODE_DPMS_OFF); 1669 1670 mipi_dsi_host_unregister(&dsi->dsi_host); 1671 } 1672 1673 static const struct component_ops exynos_dsi_component_ops = { 1674 .bind = exynos_dsi_bind, 1675 .unbind = exynos_dsi_unbind, 1676 }; 1677 1678 static int exynos_dsi_probe(struct platform_device *pdev) 1679 { 1680 struct device *dev = &pdev->dev; 1681 struct resource *res; 1682 struct exynos_dsi *dsi; 1683 int ret; 1684 1685 dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL); 1686 if (!dsi) 1687 return -ENOMEM; 1688 1689 dsi->display.type = EXYNOS_DISPLAY_TYPE_LCD; 1690 dsi->display.ops = &exynos_dsi_display_ops; 1691 1692 ret = exynos_drm_component_add(dev, EXYNOS_DEVICE_TYPE_CONNECTOR, 1693 dsi->display.type); 1694 if (ret) 1695 return ret; 1696 1697 /* To be checked as invalid one */ 1698 dsi->te_gpio = -ENOENT; 1699 1700 init_completion(&dsi->completed); 1701 spin_lock_init(&dsi->transfer_lock); 1702 INIT_LIST_HEAD(&dsi->transfer_list); 1703 1704 dsi->dsi_host.ops = &exynos_dsi_ops; 1705 dsi->dsi_host.dev = dev; 1706 1707 dsi->dev = dev; 1708 dsi->driver_data = exynos_dsi_get_driver_data(pdev); 1709 1710 ret = exynos_dsi_parse_dt(dsi); 1711 if (ret) 1712 goto err_del_component; 1713 1714 dsi->supplies[0].supply = "vddcore"; 1715 dsi->supplies[1].supply = "vddio"; 1716 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(dsi->supplies), 1717 dsi->supplies); 1718 if (ret) { 1719 dev_info(dev, "failed to get regulators: %d\n", ret); 1720 return -EPROBE_DEFER; 1721 } 1722 1723 dsi->pll_clk = devm_clk_get(dev, "pll_clk"); 1724 if (IS_ERR(dsi->pll_clk)) { 1725 dev_info(dev, "failed to get dsi pll input clock\n"); 1726 ret = PTR_ERR(dsi->pll_clk); 1727 goto err_del_component; 1728 } 1729 1730 dsi->bus_clk = devm_clk_get(dev, "bus_clk"); 1731 if (IS_ERR(dsi->bus_clk)) { 1732 dev_info(dev, "failed to get dsi bus clock\n"); 1733 ret = PTR_ERR(dsi->bus_clk); 1734 goto err_del_component; 1735 } 1736 1737 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1738 dsi->reg_base = devm_ioremap_resource(dev, res); 1739 if (IS_ERR(dsi->reg_base)) { 1740 dev_err(dev, "failed to remap io region\n"); 1741 ret = PTR_ERR(dsi->reg_base); 1742 goto err_del_component; 1743 } 1744 1745 dsi->phy = devm_phy_get(dev, "dsim"); 1746 if (IS_ERR(dsi->phy)) { 1747 dev_info(dev, "failed to get dsim phy\n"); 1748 ret = PTR_ERR(dsi->phy); 1749 goto err_del_component; 1750 } 1751 1752 dsi->irq = platform_get_irq(pdev, 0); 1753 if (dsi->irq < 0) { 1754 dev_err(dev, "failed to request dsi irq resource\n"); 1755 ret = dsi->irq; 1756 goto err_del_component; 1757 } 1758 1759 irq_set_status_flags(dsi->irq, IRQ_NOAUTOEN); 1760 ret = devm_request_threaded_irq(dev, dsi->irq, NULL, 1761 exynos_dsi_irq, IRQF_ONESHOT, 1762 dev_name(dev), dsi); 1763 if (ret) { 1764 dev_err(dev, "failed to request dsi irq\n"); 1765 goto err_del_component; 1766 } 1767 1768 platform_set_drvdata(pdev, &dsi->display); 1769 1770 ret = component_add(dev, &exynos_dsi_component_ops); 1771 if (ret) 1772 goto err_del_component; 1773 1774 return ret; 1775 1776 err_del_component: 1777 exynos_drm_component_del(dev, EXYNOS_DEVICE_TYPE_CONNECTOR); 1778 return ret; 1779 } 1780 1781 static int exynos_dsi_remove(struct platform_device *pdev) 1782 { 1783 component_del(&pdev->dev, &exynos_dsi_component_ops); 1784 exynos_drm_component_del(&pdev->dev, EXYNOS_DEVICE_TYPE_CONNECTOR); 1785 1786 return 0; 1787 } 1788 1789 struct platform_driver dsi_driver = { 1790 .probe = exynos_dsi_probe, 1791 .remove = exynos_dsi_remove, 1792 .driver = { 1793 .name = "exynos-dsi", 1794 .owner = THIS_MODULE, 1795 .of_match_table = exynos_dsi_of_match, 1796 }, 1797 }; 1798 1799 MODULE_AUTHOR("Tomasz Figa <t.figa@samsung.com>"); 1800 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>"); 1801 MODULE_DESCRIPTION("Samsung SoC MIPI DSI Master"); 1802 MODULE_LICENSE("GPL v2"); 1803