1 /* 2 * Copyright (C) 2015 Free Electrons 3 * Copyright (C) 2015 NextThing Co 4 * 5 * Maxime Ripard <maxime.ripard@free-electrons.com> 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License as 9 * published by the Free Software Foundation; either version 2 of 10 * the License, or (at your option) any later version. 11 */ 12 13 #include <drm/drmP.h> 14 #include <drm/drm_atomic_helper.h> 15 #include <drm/drm_crtc.h> 16 #include <drm/drm_crtc_helper.h> 17 #include <drm/drm_encoder.h> 18 #include <drm/drm_modes.h> 19 #include <drm/drm_of.h> 20 #include <drm/drm_panel.h> 21 22 #include <uapi/drm/drm_mode.h> 23 24 #include <linux/component.h> 25 #include <linux/ioport.h> 26 #include <linux/of_address.h> 27 #include <linux/of_device.h> 28 #include <linux/of_irq.h> 29 #include <linux/regmap.h> 30 #include <linux/reset.h> 31 32 #include "sun4i_crtc.h" 33 #include "sun4i_dotclock.h" 34 #include "sun4i_drv.h" 35 #include "sun4i_lvds.h" 36 #include "sun4i_rgb.h" 37 #include "sun4i_tcon.h" 38 #include "sun6i_mipi_dsi.h" 39 #include "sun8i_tcon_top.h" 40 #include "sunxi_engine.h" 41 42 static struct drm_connector *sun4i_tcon_get_connector(const struct drm_encoder *encoder) 43 { 44 struct drm_connector *connector; 45 struct drm_connector_list_iter iter; 46 47 drm_connector_list_iter_begin(encoder->dev, &iter); 48 drm_for_each_connector_iter(connector, &iter) 49 if (connector->encoder == encoder) { 50 drm_connector_list_iter_end(&iter); 51 return connector; 52 } 53 drm_connector_list_iter_end(&iter); 54 55 return NULL; 56 } 57 58 static int sun4i_tcon_get_pixel_depth(const struct drm_encoder *encoder) 59 { 60 struct drm_connector *connector; 61 struct drm_display_info *info; 62 63 connector = sun4i_tcon_get_connector(encoder); 64 if (!connector) 65 return -EINVAL; 66 67 info = &connector->display_info; 68 if (info->num_bus_formats != 1) 69 return -EINVAL; 70 71 switch (info->bus_formats[0]) { 72 case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG: 73 return 18; 74 75 case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA: 76 case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG: 77 return 24; 78 } 79 80 return -EINVAL; 81 } 82 83 static void sun4i_tcon_channel_set_status(struct sun4i_tcon *tcon, int channel, 84 bool enabled) 85 { 86 struct clk *clk; 87 88 switch (channel) { 89 case 0: 90 WARN_ON(!tcon->quirks->has_channel_0); 91 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 92 SUN4I_TCON0_CTL_TCON_ENABLE, 93 enabled ? SUN4I_TCON0_CTL_TCON_ENABLE : 0); 94 clk = tcon->dclk; 95 break; 96 case 1: 97 WARN_ON(!tcon->quirks->has_channel_1); 98 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 99 SUN4I_TCON1_CTL_TCON_ENABLE, 100 enabled ? SUN4I_TCON1_CTL_TCON_ENABLE : 0); 101 clk = tcon->sclk1; 102 break; 103 default: 104 DRM_WARN("Unknown channel... doing nothing\n"); 105 return; 106 } 107 108 if (enabled) { 109 clk_prepare_enable(clk); 110 clk_rate_exclusive_get(clk); 111 } else { 112 clk_rate_exclusive_put(clk); 113 clk_disable_unprepare(clk); 114 } 115 } 116 117 static void sun4i_tcon_lvds_set_status(struct sun4i_tcon *tcon, 118 const struct drm_encoder *encoder, 119 bool enabled) 120 { 121 if (enabled) { 122 u8 val; 123 124 regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, 125 SUN4I_TCON0_LVDS_IF_EN, 126 SUN4I_TCON0_LVDS_IF_EN); 127 128 /* 129 * As their name suggest, these values only apply to the A31 130 * and later SoCs. We'll have to rework this when merging 131 * support for the older SoCs. 132 */ 133 regmap_write(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG, 134 SUN6I_TCON0_LVDS_ANA0_C(2) | 135 SUN6I_TCON0_LVDS_ANA0_V(3) | 136 SUN6I_TCON0_LVDS_ANA0_PD(2) | 137 SUN6I_TCON0_LVDS_ANA0_EN_LDO); 138 udelay(2); 139 140 regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG, 141 SUN6I_TCON0_LVDS_ANA0_EN_MB, 142 SUN6I_TCON0_LVDS_ANA0_EN_MB); 143 udelay(2); 144 145 regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG, 146 SUN6I_TCON0_LVDS_ANA0_EN_DRVC, 147 SUN6I_TCON0_LVDS_ANA0_EN_DRVC); 148 149 if (sun4i_tcon_get_pixel_depth(encoder) == 18) 150 val = 7; 151 else 152 val = 0xf; 153 154 regmap_write_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG, 155 SUN6I_TCON0_LVDS_ANA0_EN_DRVD(0xf), 156 SUN6I_TCON0_LVDS_ANA0_EN_DRVD(val)); 157 } else { 158 regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, 159 SUN4I_TCON0_LVDS_IF_EN, 0); 160 } 161 } 162 163 void sun4i_tcon_set_status(struct sun4i_tcon *tcon, 164 const struct drm_encoder *encoder, 165 bool enabled) 166 { 167 bool is_lvds = false; 168 int channel; 169 170 switch (encoder->encoder_type) { 171 case DRM_MODE_ENCODER_LVDS: 172 is_lvds = true; 173 /* Fallthrough */ 174 case DRM_MODE_ENCODER_DSI: 175 case DRM_MODE_ENCODER_NONE: 176 channel = 0; 177 break; 178 case DRM_MODE_ENCODER_TMDS: 179 case DRM_MODE_ENCODER_TVDAC: 180 channel = 1; 181 break; 182 default: 183 DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n"); 184 return; 185 } 186 187 if (is_lvds && !enabled) 188 sun4i_tcon_lvds_set_status(tcon, encoder, false); 189 190 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 191 SUN4I_TCON_GCTL_TCON_ENABLE, 192 enabled ? SUN4I_TCON_GCTL_TCON_ENABLE : 0); 193 194 if (is_lvds && enabled) 195 sun4i_tcon_lvds_set_status(tcon, encoder, true); 196 197 sun4i_tcon_channel_set_status(tcon, channel, enabled); 198 } 199 200 void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable) 201 { 202 u32 mask, val = 0; 203 204 DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis"); 205 206 mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) | 207 SUN4I_TCON_GINT0_VBLANK_ENABLE(1) | 208 SUN4I_TCON_GINT0_TCON0_TRI_FINISH_ENABLE; 209 210 if (enable) 211 val = mask; 212 213 regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val); 214 } 215 EXPORT_SYMBOL(sun4i_tcon_enable_vblank); 216 217 /* 218 * This function is a helper for TCON output muxing. The TCON output 219 * muxing control register in earlier SoCs (without the TCON TOP block) 220 * are located in TCON0. This helper returns a pointer to TCON0's 221 * sun4i_tcon structure, or NULL if not found. 222 */ 223 static struct sun4i_tcon *sun4i_get_tcon0(struct drm_device *drm) 224 { 225 struct sun4i_drv *drv = drm->dev_private; 226 struct sun4i_tcon *tcon; 227 228 list_for_each_entry(tcon, &drv->tcon_list, list) 229 if (tcon->id == 0) 230 return tcon; 231 232 dev_warn(drm->dev, 233 "TCON0 not found, display output muxing may not work\n"); 234 235 return NULL; 236 } 237 238 void sun4i_tcon_set_mux(struct sun4i_tcon *tcon, int channel, 239 const struct drm_encoder *encoder) 240 { 241 int ret = -ENOTSUPP; 242 243 if (tcon->quirks->set_mux) 244 ret = tcon->quirks->set_mux(tcon, encoder); 245 246 DRM_DEBUG_DRIVER("Muxing encoder %s to CRTC %s: %d\n", 247 encoder->name, encoder->crtc->name, ret); 248 } 249 250 static int sun4i_tcon_get_clk_delay(const struct drm_display_mode *mode, 251 int channel) 252 { 253 int delay = mode->vtotal - mode->vdisplay; 254 255 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 256 delay /= 2; 257 258 if (channel == 1) 259 delay -= 2; 260 261 delay = min(delay, 30); 262 263 DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay); 264 265 return delay; 266 } 267 268 static void sun4i_tcon0_mode_set_common(struct sun4i_tcon *tcon, 269 const struct drm_display_mode *mode) 270 { 271 /* Configure the dot clock */ 272 clk_set_rate(tcon->dclk, mode->crtc_clock * 1000); 273 274 /* Set the resolution */ 275 regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG, 276 SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) | 277 SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay)); 278 } 279 280 static void sun4i_tcon0_mode_set_cpu(struct sun4i_tcon *tcon, 281 struct mipi_dsi_device *device, 282 const struct drm_display_mode *mode) 283 { 284 u8 bpp = mipi_dsi_pixel_format_to_bpp(device->format); 285 u8 lanes = device->lanes; 286 u32 block_space, start_delay; 287 u32 tcon_div; 288 289 tcon->dclk_min_div = 4; 290 tcon->dclk_max_div = 127; 291 292 sun4i_tcon0_mode_set_common(tcon, mode); 293 294 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 295 SUN4I_TCON0_CTL_IF_MASK, 296 SUN4I_TCON0_CTL_IF_8080); 297 298 regmap_write(tcon->regs, SUN4I_TCON_ECC_FIFO_REG, 299 SUN4I_TCON_ECC_FIFO_EN); 300 301 regmap_write(tcon->regs, SUN4I_TCON0_CPU_IF_REG, 302 SUN4I_TCON0_CPU_IF_MODE_DSI | 303 SUN4I_TCON0_CPU_IF_TRI_FIFO_FLUSH | 304 SUN4I_TCON0_CPU_IF_TRI_FIFO_EN | 305 SUN4I_TCON0_CPU_IF_TRI_EN); 306 307 /* 308 * This looks suspicious, but it works... 309 * 310 * The datasheet says that this should be set higher than 20 * 311 * pixel cycle, but it's not clear what a pixel cycle is. 312 */ 313 regmap_read(tcon->regs, SUN4I_TCON0_DCLK_REG, &tcon_div); 314 tcon_div &= GENMASK(6, 0); 315 block_space = mode->htotal * bpp / (tcon_div * lanes); 316 block_space -= mode->hdisplay + 40; 317 318 regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI0_REG, 319 SUN4I_TCON0_CPU_TRI0_BLOCK_SPACE(block_space) | 320 SUN4I_TCON0_CPU_TRI0_BLOCK_SIZE(mode->hdisplay)); 321 322 regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI1_REG, 323 SUN4I_TCON0_CPU_TRI1_BLOCK_NUM(mode->vdisplay)); 324 325 start_delay = (mode->crtc_vtotal - mode->crtc_vdisplay - 10 - 1); 326 start_delay = start_delay * mode->crtc_htotal * 149; 327 start_delay = start_delay / (mode->crtc_clock / 1000) / 8; 328 regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI2_REG, 329 SUN4I_TCON0_CPU_TRI2_TRANS_START_SET(10) | 330 SUN4I_TCON0_CPU_TRI2_START_DELAY(start_delay)); 331 332 /* 333 * The Allwinner BSP has a comment that the period should be 334 * the display clock * 15, but uses an hardcoded 3000... 335 */ 336 regmap_write(tcon->regs, SUN4I_TCON_SAFE_PERIOD_REG, 337 SUN4I_TCON_SAFE_PERIOD_NUM(3000) | 338 SUN4I_TCON_SAFE_PERIOD_MODE(3)); 339 340 /* Enable the output on the pins */ 341 regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 342 0xe0000000); 343 } 344 345 static void sun4i_tcon0_mode_set_lvds(struct sun4i_tcon *tcon, 346 const struct drm_encoder *encoder, 347 const struct drm_display_mode *mode) 348 { 349 unsigned int bp; 350 u8 clk_delay; 351 u32 reg, val = 0; 352 353 WARN_ON(!tcon->quirks->has_channel_0); 354 355 tcon->dclk_min_div = 7; 356 tcon->dclk_max_div = 7; 357 sun4i_tcon0_mode_set_common(tcon, mode); 358 359 /* Adjust clock delay */ 360 clk_delay = sun4i_tcon_get_clk_delay(mode, 0); 361 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 362 SUN4I_TCON0_CTL_CLK_DELAY_MASK, 363 SUN4I_TCON0_CTL_CLK_DELAY(clk_delay)); 364 365 /* 366 * This is called a backporch in the register documentation, 367 * but it really is the back porch + hsync 368 */ 369 bp = mode->crtc_htotal - mode->crtc_hsync_start; 370 DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", 371 mode->crtc_htotal, bp); 372 373 /* Set horizontal display timings */ 374 regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG, 375 SUN4I_TCON0_BASIC1_H_TOTAL(mode->htotal) | 376 SUN4I_TCON0_BASIC1_H_BACKPORCH(bp)); 377 378 /* 379 * This is called a backporch in the register documentation, 380 * but it really is the back porch + hsync 381 */ 382 bp = mode->crtc_vtotal - mode->crtc_vsync_start; 383 DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", 384 mode->crtc_vtotal, bp); 385 386 /* Set vertical display timings */ 387 regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG, 388 SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) | 389 SUN4I_TCON0_BASIC2_V_BACKPORCH(bp)); 390 391 reg = SUN4I_TCON0_LVDS_IF_CLK_SEL_TCON0 | 392 SUN4I_TCON0_LVDS_IF_DATA_POL_NORMAL | 393 SUN4I_TCON0_LVDS_IF_CLK_POL_NORMAL; 394 if (sun4i_tcon_get_pixel_depth(encoder) == 24) 395 reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_24BITS; 396 else 397 reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_18BITS; 398 399 regmap_write(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, reg); 400 401 /* Setup the polarity of the various signals */ 402 if (!(mode->flags & DRM_MODE_FLAG_PHSYNC)) 403 val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE; 404 405 if (!(mode->flags & DRM_MODE_FLAG_PVSYNC)) 406 val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE; 407 408 regmap_write(tcon->regs, SUN4I_TCON0_IO_POL_REG, val); 409 410 /* Map output pins to channel 0 */ 411 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 412 SUN4I_TCON_GCTL_IOMAP_MASK, 413 SUN4I_TCON_GCTL_IOMAP_TCON0); 414 415 /* Enable the output on the pins */ 416 regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0xe0000000); 417 } 418 419 static void sun4i_tcon0_mode_set_rgb(struct sun4i_tcon *tcon, 420 const struct drm_display_mode *mode) 421 { 422 unsigned int bp, hsync, vsync; 423 u8 clk_delay; 424 u32 val = 0; 425 426 WARN_ON(!tcon->quirks->has_channel_0); 427 428 tcon->dclk_min_div = 6; 429 tcon->dclk_max_div = 127; 430 sun4i_tcon0_mode_set_common(tcon, mode); 431 432 /* Adjust clock delay */ 433 clk_delay = sun4i_tcon_get_clk_delay(mode, 0); 434 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 435 SUN4I_TCON0_CTL_CLK_DELAY_MASK, 436 SUN4I_TCON0_CTL_CLK_DELAY(clk_delay)); 437 438 /* 439 * This is called a backporch in the register documentation, 440 * but it really is the back porch + hsync 441 */ 442 bp = mode->crtc_htotal - mode->crtc_hsync_start; 443 DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", 444 mode->crtc_htotal, bp); 445 446 /* Set horizontal display timings */ 447 regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG, 448 SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) | 449 SUN4I_TCON0_BASIC1_H_BACKPORCH(bp)); 450 451 /* 452 * This is called a backporch in the register documentation, 453 * but it really is the back porch + hsync 454 */ 455 bp = mode->crtc_vtotal - mode->crtc_vsync_start; 456 DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", 457 mode->crtc_vtotal, bp); 458 459 /* Set vertical display timings */ 460 regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG, 461 SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) | 462 SUN4I_TCON0_BASIC2_V_BACKPORCH(bp)); 463 464 /* Set Hsync and Vsync length */ 465 hsync = mode->crtc_hsync_end - mode->crtc_hsync_start; 466 vsync = mode->crtc_vsync_end - mode->crtc_vsync_start; 467 DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync); 468 regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG, 469 SUN4I_TCON0_BASIC3_V_SYNC(vsync) | 470 SUN4I_TCON0_BASIC3_H_SYNC(hsync)); 471 472 /* Setup the polarity of the various signals */ 473 if (mode->flags & DRM_MODE_FLAG_PHSYNC) 474 val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE; 475 476 if (mode->flags & DRM_MODE_FLAG_PVSYNC) 477 val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE; 478 479 /* 480 * On A20 and similar SoCs, the only way to achieve Positive Edge 481 * (Rising Edge), is setting dclk clock phase to 2/3(240°). 482 * By default TCON works in Negative Edge(Falling Edge), 483 * this is why phase is set to 0 in that case. 484 * Unfortunately there's no way to logically invert dclk through 485 * IO_POL register. 486 * The only acceptable way to work, triple checked with scope, 487 * is using clock phase set to 0° for Negative Edge and set to 240° 488 * for Positive Edge. 489 * On A33 and similar SoCs there would be a 90° phase option, 490 * but it divides also dclk by 2. 491 * Following code is a way to avoid quirks all around TCON 492 * and DOTCLOCK drivers. 493 */ 494 if (!IS_ERR(tcon->panel)) { 495 struct drm_panel *panel = tcon->panel; 496 struct drm_connector *connector = panel->connector; 497 struct drm_display_info display_info = connector->display_info; 498 499 if (display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_POSEDGE) 500 clk_set_phase(tcon->dclk, 240); 501 502 if (display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE) 503 clk_set_phase(tcon->dclk, 0); 504 } 505 506 regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG, 507 SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | SUN4I_TCON0_IO_POL_VSYNC_POSITIVE, 508 val); 509 510 /* Map output pins to channel 0 */ 511 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 512 SUN4I_TCON_GCTL_IOMAP_MASK, 513 SUN4I_TCON_GCTL_IOMAP_TCON0); 514 515 /* Enable the output on the pins */ 516 regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0); 517 } 518 519 static void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon, 520 const struct drm_display_mode *mode) 521 { 522 unsigned int bp, hsync, vsync, vtotal; 523 u8 clk_delay; 524 u32 val; 525 526 WARN_ON(!tcon->quirks->has_channel_1); 527 528 /* Configure the dot clock */ 529 clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000); 530 531 /* Adjust clock delay */ 532 clk_delay = sun4i_tcon_get_clk_delay(mode, 1); 533 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 534 SUN4I_TCON1_CTL_CLK_DELAY_MASK, 535 SUN4I_TCON1_CTL_CLK_DELAY(clk_delay)); 536 537 /* Set interlaced mode */ 538 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 539 val = SUN4I_TCON1_CTL_INTERLACE_ENABLE; 540 else 541 val = 0; 542 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 543 SUN4I_TCON1_CTL_INTERLACE_ENABLE, 544 val); 545 546 /* Set the input resolution */ 547 regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG, 548 SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) | 549 SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay)); 550 551 /* Set the upscaling resolution */ 552 regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG, 553 SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) | 554 SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay)); 555 556 /* Set the output resolution */ 557 regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG, 558 SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) | 559 SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay)); 560 561 /* Set horizontal display timings */ 562 bp = mode->crtc_htotal - mode->crtc_hsync_start; 563 DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", 564 mode->htotal, bp); 565 regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG, 566 SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) | 567 SUN4I_TCON1_BASIC3_H_BACKPORCH(bp)); 568 569 bp = mode->crtc_vtotal - mode->crtc_vsync_start; 570 DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", 571 mode->crtc_vtotal, bp); 572 573 /* 574 * The vertical resolution needs to be doubled in all 575 * cases. We could use crtc_vtotal and always multiply by two, 576 * but that leads to a rounding error in interlace when vtotal 577 * is odd. 578 * 579 * This happens with TV's PAL for example, where vtotal will 580 * be 625, crtc_vtotal 312, and thus crtc_vtotal * 2 will be 581 * 624, which apparently confuses the hardware. 582 * 583 * To work around this, we will always use vtotal, and 584 * multiply by two only if we're not in interlace. 585 */ 586 vtotal = mode->vtotal; 587 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) 588 vtotal = vtotal * 2; 589 590 /* Set vertical display timings */ 591 regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG, 592 SUN4I_TCON1_BASIC4_V_TOTAL(vtotal) | 593 SUN4I_TCON1_BASIC4_V_BACKPORCH(bp)); 594 595 /* Set Hsync and Vsync length */ 596 hsync = mode->crtc_hsync_end - mode->crtc_hsync_start; 597 vsync = mode->crtc_vsync_end - mode->crtc_vsync_start; 598 DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync); 599 regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG, 600 SUN4I_TCON1_BASIC5_V_SYNC(vsync) | 601 SUN4I_TCON1_BASIC5_H_SYNC(hsync)); 602 603 /* Map output pins to channel 1 */ 604 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 605 SUN4I_TCON_GCTL_IOMAP_MASK, 606 SUN4I_TCON_GCTL_IOMAP_TCON1); 607 } 608 609 void sun4i_tcon_mode_set(struct sun4i_tcon *tcon, 610 const struct drm_encoder *encoder, 611 const struct drm_display_mode *mode) 612 { 613 struct sun6i_dsi *dsi; 614 615 switch (encoder->encoder_type) { 616 case DRM_MODE_ENCODER_DSI: 617 /* 618 * This is not really elegant, but it's the "cleaner" 619 * way I could think of... 620 */ 621 dsi = encoder_to_sun6i_dsi(encoder); 622 sun4i_tcon0_mode_set_cpu(tcon, dsi->device, mode); 623 break; 624 case DRM_MODE_ENCODER_LVDS: 625 sun4i_tcon0_mode_set_lvds(tcon, encoder, mode); 626 break; 627 case DRM_MODE_ENCODER_NONE: 628 sun4i_tcon0_mode_set_rgb(tcon, mode); 629 sun4i_tcon_set_mux(tcon, 0, encoder); 630 break; 631 case DRM_MODE_ENCODER_TVDAC: 632 case DRM_MODE_ENCODER_TMDS: 633 sun4i_tcon1_mode_set(tcon, mode); 634 sun4i_tcon_set_mux(tcon, 1, encoder); 635 break; 636 default: 637 DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n"); 638 } 639 } 640 EXPORT_SYMBOL(sun4i_tcon_mode_set); 641 642 static void sun4i_tcon_finish_page_flip(struct drm_device *dev, 643 struct sun4i_crtc *scrtc) 644 { 645 unsigned long flags; 646 647 spin_lock_irqsave(&dev->event_lock, flags); 648 if (scrtc->event) { 649 drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event); 650 drm_crtc_vblank_put(&scrtc->crtc); 651 scrtc->event = NULL; 652 } 653 spin_unlock_irqrestore(&dev->event_lock, flags); 654 } 655 656 static irqreturn_t sun4i_tcon_handler(int irq, void *private) 657 { 658 struct sun4i_tcon *tcon = private; 659 struct drm_device *drm = tcon->drm; 660 struct sun4i_crtc *scrtc = tcon->crtc; 661 struct sunxi_engine *engine = scrtc->engine; 662 unsigned int status; 663 664 regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status); 665 666 if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) | 667 SUN4I_TCON_GINT0_VBLANK_INT(1) | 668 SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT))) 669 return IRQ_NONE; 670 671 drm_crtc_handle_vblank(&scrtc->crtc); 672 sun4i_tcon_finish_page_flip(drm, scrtc); 673 674 /* Acknowledge the interrupt */ 675 regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, 676 SUN4I_TCON_GINT0_VBLANK_INT(0) | 677 SUN4I_TCON_GINT0_VBLANK_INT(1) | 678 SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT, 679 0); 680 681 if (engine->ops->vblank_quirk) 682 engine->ops->vblank_quirk(engine); 683 684 return IRQ_HANDLED; 685 } 686 687 static int sun4i_tcon_init_clocks(struct device *dev, 688 struct sun4i_tcon *tcon) 689 { 690 tcon->clk = devm_clk_get(dev, "ahb"); 691 if (IS_ERR(tcon->clk)) { 692 dev_err(dev, "Couldn't get the TCON bus clock\n"); 693 return PTR_ERR(tcon->clk); 694 } 695 clk_prepare_enable(tcon->clk); 696 697 if (tcon->quirks->has_channel_0) { 698 tcon->sclk0 = devm_clk_get(dev, "tcon-ch0"); 699 if (IS_ERR(tcon->sclk0)) { 700 dev_err(dev, "Couldn't get the TCON channel 0 clock\n"); 701 return PTR_ERR(tcon->sclk0); 702 } 703 } 704 705 if (tcon->quirks->has_channel_1) { 706 tcon->sclk1 = devm_clk_get(dev, "tcon-ch1"); 707 if (IS_ERR(tcon->sclk1)) { 708 dev_err(dev, "Couldn't get the TCON channel 1 clock\n"); 709 return PTR_ERR(tcon->sclk1); 710 } 711 } 712 713 return 0; 714 } 715 716 static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon) 717 { 718 clk_disable_unprepare(tcon->clk); 719 } 720 721 static int sun4i_tcon_init_irq(struct device *dev, 722 struct sun4i_tcon *tcon) 723 { 724 struct platform_device *pdev = to_platform_device(dev); 725 int irq, ret; 726 727 irq = platform_get_irq(pdev, 0); 728 if (irq < 0) { 729 dev_err(dev, "Couldn't retrieve the TCON interrupt\n"); 730 return irq; 731 } 732 733 ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0, 734 dev_name(dev), tcon); 735 if (ret) { 736 dev_err(dev, "Couldn't request the IRQ\n"); 737 return ret; 738 } 739 740 return 0; 741 } 742 743 static struct regmap_config sun4i_tcon_regmap_config = { 744 .reg_bits = 32, 745 .val_bits = 32, 746 .reg_stride = 4, 747 .max_register = 0x800, 748 }; 749 750 static int sun4i_tcon_init_regmap(struct device *dev, 751 struct sun4i_tcon *tcon) 752 { 753 struct platform_device *pdev = to_platform_device(dev); 754 struct resource *res; 755 void __iomem *regs; 756 757 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 758 regs = devm_ioremap_resource(dev, res); 759 if (IS_ERR(regs)) 760 return PTR_ERR(regs); 761 762 tcon->regs = devm_regmap_init_mmio(dev, regs, 763 &sun4i_tcon_regmap_config); 764 if (IS_ERR(tcon->regs)) { 765 dev_err(dev, "Couldn't create the TCON regmap\n"); 766 return PTR_ERR(tcon->regs); 767 } 768 769 /* Make sure the TCON is disabled and all IRQs are off */ 770 regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0); 771 regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0); 772 regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0); 773 774 /* Disable IO lines and set them to tristate */ 775 regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0); 776 regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0); 777 778 return 0; 779 } 780 781 /* 782 * On SoCs with the old display pipeline design (Display Engine 1.0), 783 * the TCON is always tied to just one backend. Hence we can traverse 784 * the of_graph upwards to find the backend our tcon is connected to, 785 * and take its ID as our own. 786 * 787 * We can either identify backends from their compatible strings, which 788 * means maintaining a large list of them. Or, since the backend is 789 * registered and binded before the TCON, we can just go through the 790 * list of registered backends and compare the device node. 791 * 792 * As the structures now store engines instead of backends, here this 793 * function in fact searches the corresponding engine, and the ID is 794 * requested via the get_id function of the engine. 795 */ 796 static struct sunxi_engine * 797 sun4i_tcon_find_engine_traverse(struct sun4i_drv *drv, 798 struct device_node *node, 799 u32 port_id) 800 { 801 struct device_node *port, *ep, *remote; 802 struct sunxi_engine *engine = ERR_PTR(-EINVAL); 803 u32 reg = 0; 804 805 port = of_graph_get_port_by_id(node, port_id); 806 if (!port) 807 return ERR_PTR(-EINVAL); 808 809 /* 810 * This only works if there is only one path from the TCON 811 * to any display engine. Otherwise the probe order of the 812 * TCONs and display engines is not guaranteed. They may 813 * either bind to the wrong one, or worse, bind to the same 814 * one if additional checks are not done. 815 * 816 * Bail out if there are multiple input connections. 817 */ 818 if (of_get_available_child_count(port) != 1) 819 goto out_put_port; 820 821 /* Get the first connection without specifying an ID */ 822 ep = of_get_next_available_child(port, NULL); 823 if (!ep) 824 goto out_put_port; 825 826 remote = of_graph_get_remote_port_parent(ep); 827 if (!remote) 828 goto out_put_ep; 829 830 /* does this node match any registered engines? */ 831 list_for_each_entry(engine, &drv->engine_list, list) 832 if (remote == engine->node) 833 goto out_put_remote; 834 835 /* 836 * According to device tree binding input ports have even id 837 * number and output ports have odd id. Since component with 838 * more than one input and one output (TCON TOP) exits, correct 839 * remote input id has to be calculated by subtracting 1 from 840 * remote output id. If this for some reason can't be done, 0 841 * is used as input port id. 842 */ 843 of_node_put(port); 844 port = of_graph_get_remote_port(ep); 845 if (!of_property_read_u32(port, "reg", ®) && reg > 0) 846 reg -= 1; 847 848 /* keep looking through upstream ports */ 849 engine = sun4i_tcon_find_engine_traverse(drv, remote, reg); 850 851 out_put_remote: 852 of_node_put(remote); 853 out_put_ep: 854 of_node_put(ep); 855 out_put_port: 856 of_node_put(port); 857 858 return engine; 859 } 860 861 /* 862 * The device tree binding says that the remote endpoint ID of any 863 * connection between components, up to and including the TCON, of 864 * the display pipeline should be equal to the actual ID of the local 865 * component. Thus we can look at any one of the input connections of 866 * the TCONs, and use that connection's remote endpoint ID as our own. 867 * 868 * Since the user of this function already finds the input port, 869 * the port is passed in directly without further checks. 870 */ 871 static int sun4i_tcon_of_get_id_from_port(struct device_node *port) 872 { 873 struct device_node *ep; 874 int ret = -EINVAL; 875 876 /* try finding an upstream endpoint */ 877 for_each_available_child_of_node(port, ep) { 878 struct device_node *remote; 879 u32 reg; 880 881 remote = of_graph_get_remote_endpoint(ep); 882 if (!remote) 883 continue; 884 885 ret = of_property_read_u32(remote, "reg", ®); 886 if (ret) 887 continue; 888 889 ret = reg; 890 } 891 892 return ret; 893 } 894 895 /* 896 * Once we know the TCON's id, we can look through the list of 897 * engines to find a matching one. We assume all engines have 898 * been probed and added to the list. 899 */ 900 static struct sunxi_engine *sun4i_tcon_get_engine_by_id(struct sun4i_drv *drv, 901 int id) 902 { 903 struct sunxi_engine *engine; 904 905 list_for_each_entry(engine, &drv->engine_list, list) 906 if (engine->id == id) 907 return engine; 908 909 return ERR_PTR(-EINVAL); 910 } 911 912 static bool sun4i_tcon_connected_to_tcon_top(struct device_node *node) 913 { 914 struct device_node *remote; 915 bool ret = false; 916 917 remote = of_graph_get_remote_node(node, 0, -1); 918 if (remote) { 919 ret = !!of_match_node(sun8i_tcon_top_of_table, remote); 920 of_node_put(remote); 921 } 922 923 return ret; 924 } 925 926 static int sun4i_tcon_get_index(struct sun4i_drv *drv) 927 { 928 struct list_head *pos; 929 int size = 0; 930 931 /* 932 * Because TCON is added to the list at the end of the probe 933 * (after this function is called), index of the current TCON 934 * will be same as current TCON list size. 935 */ 936 list_for_each(pos, &drv->tcon_list) 937 ++size; 938 939 return size; 940 } 941 942 /* 943 * On SoCs with the old display pipeline design (Display Engine 1.0), 944 * we assumed the TCON was always tied to just one backend. However 945 * this proved not to be the case. On the A31, the TCON can select 946 * either backend as its source. On the A20 (and likely on the A10), 947 * the backend can choose which TCON to output to. 948 * 949 * The device tree binding says that the remote endpoint ID of any 950 * connection between components, up to and including the TCON, of 951 * the display pipeline should be equal to the actual ID of the local 952 * component. Thus we should be able to look at any one of the input 953 * connections of the TCONs, and use that connection's remote endpoint 954 * ID as our own. 955 * 956 * However the connections between the backend and TCON were assumed 957 * to be always singular, and their endpoit IDs were all incorrectly 958 * set to 0. This means for these old device trees, we cannot just look 959 * up the remote endpoint ID of a TCON input endpoint. TCON1 would be 960 * incorrectly identified as TCON0. 961 * 962 * This function first checks if the TCON node has 2 input endpoints. 963 * If so, then the device tree is a corrected version, and it will use 964 * sun4i_tcon_of_get_id() and sun4i_tcon_get_engine_by_id() from above 965 * to fetch the ID and engine directly. If not, then it is likely an 966 * old device trees, where the endpoint IDs were incorrect, but did not 967 * have endpoint connections between the backend and TCON across 968 * different display pipelines. It will fall back to the old method of 969 * traversing the of_graph to try and find a matching engine by device 970 * node. 971 * 972 * In the case of single display pipeline device trees, either method 973 * works. 974 */ 975 static struct sunxi_engine *sun4i_tcon_find_engine(struct sun4i_drv *drv, 976 struct device_node *node) 977 { 978 struct device_node *port; 979 struct sunxi_engine *engine; 980 981 port = of_graph_get_port_by_id(node, 0); 982 if (!port) 983 return ERR_PTR(-EINVAL); 984 985 /* 986 * Is this a corrected device tree with cross pipeline 987 * connections between the backend and TCON? 988 */ 989 if (of_get_child_count(port) > 1) { 990 int id; 991 992 /* 993 * When pipeline has the same number of TCONs and engines which 994 * are represented by frontends/backends (DE1) or mixers (DE2), 995 * we match them by their respective IDs. However, if pipeline 996 * contains TCON TOP, chances are that there are either more 997 * TCONs than engines (R40) or TCONs with non-consecutive ids. 998 * (H6). In that case it's easier just use TCON index in list 999 * as an id. That means that on R40, any 2 TCONs can be enabled 1000 * in DT out of 4 (there are 2 mixers). Due to the design of 1001 * TCON TOP, remaining 2 TCONs can't be connected to anything 1002 * anyway. 1003 */ 1004 if (sun4i_tcon_connected_to_tcon_top(node)) 1005 id = sun4i_tcon_get_index(drv); 1006 else 1007 id = sun4i_tcon_of_get_id_from_port(port); 1008 1009 /* Get our engine by matching our ID */ 1010 engine = sun4i_tcon_get_engine_by_id(drv, id); 1011 1012 of_node_put(port); 1013 return engine; 1014 } 1015 1016 /* Fallback to old method by traversing input endpoints */ 1017 of_node_put(port); 1018 return sun4i_tcon_find_engine_traverse(drv, node, 0); 1019 } 1020 1021 static int sun4i_tcon_bind(struct device *dev, struct device *master, 1022 void *data) 1023 { 1024 struct drm_device *drm = data; 1025 struct sun4i_drv *drv = drm->dev_private; 1026 struct sunxi_engine *engine; 1027 struct device_node *remote; 1028 struct sun4i_tcon *tcon; 1029 struct reset_control *edp_rstc; 1030 bool has_lvds_rst, has_lvds_alt, can_lvds; 1031 int ret; 1032 1033 engine = sun4i_tcon_find_engine(drv, dev->of_node); 1034 if (IS_ERR(engine)) { 1035 dev_err(dev, "Couldn't find matching engine\n"); 1036 return -EPROBE_DEFER; 1037 } 1038 1039 tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL); 1040 if (!tcon) 1041 return -ENOMEM; 1042 dev_set_drvdata(dev, tcon); 1043 tcon->drm = drm; 1044 tcon->dev = dev; 1045 tcon->id = engine->id; 1046 tcon->quirks = of_device_get_match_data(dev); 1047 1048 tcon->lcd_rst = devm_reset_control_get(dev, "lcd"); 1049 if (IS_ERR(tcon->lcd_rst)) { 1050 dev_err(dev, "Couldn't get our reset line\n"); 1051 return PTR_ERR(tcon->lcd_rst); 1052 } 1053 1054 if (tcon->quirks->needs_edp_reset) { 1055 edp_rstc = devm_reset_control_get_shared(dev, "edp"); 1056 if (IS_ERR(edp_rstc)) { 1057 dev_err(dev, "Couldn't get edp reset line\n"); 1058 return PTR_ERR(edp_rstc); 1059 } 1060 1061 ret = reset_control_deassert(edp_rstc); 1062 if (ret) { 1063 dev_err(dev, "Couldn't deassert edp reset line\n"); 1064 return ret; 1065 } 1066 } 1067 1068 /* Make sure our TCON is reset */ 1069 ret = reset_control_reset(tcon->lcd_rst); 1070 if (ret) { 1071 dev_err(dev, "Couldn't deassert our reset line\n"); 1072 return ret; 1073 } 1074 1075 if (tcon->quirks->supports_lvds) { 1076 /* 1077 * This can only be made optional since we've had DT 1078 * nodes without the LVDS reset properties. 1079 * 1080 * If the property is missing, just disable LVDS, and 1081 * print a warning. 1082 */ 1083 tcon->lvds_rst = devm_reset_control_get_optional(dev, "lvds"); 1084 if (IS_ERR(tcon->lvds_rst)) { 1085 dev_err(dev, "Couldn't get our reset line\n"); 1086 return PTR_ERR(tcon->lvds_rst); 1087 } else if (tcon->lvds_rst) { 1088 has_lvds_rst = true; 1089 reset_control_reset(tcon->lvds_rst); 1090 } else { 1091 has_lvds_rst = false; 1092 } 1093 1094 /* 1095 * This can only be made optional since we've had DT 1096 * nodes without the LVDS reset properties. 1097 * 1098 * If the property is missing, just disable LVDS, and 1099 * print a warning. 1100 */ 1101 if (tcon->quirks->has_lvds_alt) { 1102 tcon->lvds_pll = devm_clk_get(dev, "lvds-alt"); 1103 if (IS_ERR(tcon->lvds_pll)) { 1104 if (PTR_ERR(tcon->lvds_pll) == -ENOENT) { 1105 has_lvds_alt = false; 1106 } else { 1107 dev_err(dev, "Couldn't get the LVDS PLL\n"); 1108 return PTR_ERR(tcon->lvds_pll); 1109 } 1110 } else { 1111 has_lvds_alt = true; 1112 } 1113 } 1114 1115 if (!has_lvds_rst || 1116 (tcon->quirks->has_lvds_alt && !has_lvds_alt)) { 1117 dev_warn(dev, "Missing LVDS properties, Please upgrade your DT\n"); 1118 dev_warn(dev, "LVDS output disabled\n"); 1119 can_lvds = false; 1120 } else { 1121 can_lvds = true; 1122 } 1123 } else { 1124 can_lvds = false; 1125 } 1126 1127 ret = sun4i_tcon_init_clocks(dev, tcon); 1128 if (ret) { 1129 dev_err(dev, "Couldn't init our TCON clocks\n"); 1130 goto err_assert_reset; 1131 } 1132 1133 ret = sun4i_tcon_init_regmap(dev, tcon); 1134 if (ret) { 1135 dev_err(dev, "Couldn't init our TCON regmap\n"); 1136 goto err_free_clocks; 1137 } 1138 1139 if (tcon->quirks->has_channel_0) { 1140 ret = sun4i_dclk_create(dev, tcon); 1141 if (ret) { 1142 dev_err(dev, "Couldn't create our TCON dot clock\n"); 1143 goto err_free_clocks; 1144 } 1145 } 1146 1147 ret = sun4i_tcon_init_irq(dev, tcon); 1148 if (ret) { 1149 dev_err(dev, "Couldn't init our TCON interrupts\n"); 1150 goto err_free_dotclock; 1151 } 1152 1153 tcon->crtc = sun4i_crtc_init(drm, engine, tcon); 1154 if (IS_ERR(tcon->crtc)) { 1155 dev_err(dev, "Couldn't create our CRTC\n"); 1156 ret = PTR_ERR(tcon->crtc); 1157 goto err_free_dotclock; 1158 } 1159 1160 if (tcon->quirks->has_channel_0) { 1161 /* 1162 * If we have an LVDS panel connected to the TCON, we should 1163 * just probe the LVDS connector. Otherwise, just probe RGB as 1164 * we used to. 1165 */ 1166 remote = of_graph_get_remote_node(dev->of_node, 1, 0); 1167 if (of_device_is_compatible(remote, "panel-lvds")) 1168 if (can_lvds) 1169 ret = sun4i_lvds_init(drm, tcon); 1170 else 1171 ret = -EINVAL; 1172 else 1173 ret = sun4i_rgb_init(drm, tcon); 1174 of_node_put(remote); 1175 1176 if (ret < 0) 1177 goto err_free_dotclock; 1178 } 1179 1180 if (tcon->quirks->needs_de_be_mux) { 1181 /* 1182 * We assume there is no dynamic muxing of backends 1183 * and TCONs, so we select the backend with same ID. 1184 * 1185 * While dynamic selection might be interesting, since 1186 * the CRTC is tied to the TCON, while the layers are 1187 * tied to the backends, this means, we will need to 1188 * switch between groups of layers. There might not be 1189 * a way to represent this constraint in DRM. 1190 */ 1191 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 1192 SUN4I_TCON0_CTL_SRC_SEL_MASK, 1193 tcon->id); 1194 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 1195 SUN4I_TCON1_CTL_SRC_SEL_MASK, 1196 tcon->id); 1197 } 1198 1199 list_add_tail(&tcon->list, &drv->tcon_list); 1200 1201 return 0; 1202 1203 err_free_dotclock: 1204 if (tcon->quirks->has_channel_0) 1205 sun4i_dclk_free(tcon); 1206 err_free_clocks: 1207 sun4i_tcon_free_clocks(tcon); 1208 err_assert_reset: 1209 reset_control_assert(tcon->lcd_rst); 1210 return ret; 1211 } 1212 1213 static void sun4i_tcon_unbind(struct device *dev, struct device *master, 1214 void *data) 1215 { 1216 struct sun4i_tcon *tcon = dev_get_drvdata(dev); 1217 1218 list_del(&tcon->list); 1219 if (tcon->quirks->has_channel_0) 1220 sun4i_dclk_free(tcon); 1221 sun4i_tcon_free_clocks(tcon); 1222 } 1223 1224 static const struct component_ops sun4i_tcon_ops = { 1225 .bind = sun4i_tcon_bind, 1226 .unbind = sun4i_tcon_unbind, 1227 }; 1228 1229 static int sun4i_tcon_probe(struct platform_device *pdev) 1230 { 1231 struct device_node *node = pdev->dev.of_node; 1232 const struct sun4i_tcon_quirks *quirks; 1233 struct drm_bridge *bridge; 1234 struct drm_panel *panel; 1235 int ret; 1236 1237 quirks = of_device_get_match_data(&pdev->dev); 1238 1239 /* panels and bridges are present only on TCONs with channel 0 */ 1240 if (quirks->has_channel_0) { 1241 ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge); 1242 if (ret == -EPROBE_DEFER) 1243 return ret; 1244 } 1245 1246 return component_add(&pdev->dev, &sun4i_tcon_ops); 1247 } 1248 1249 static int sun4i_tcon_remove(struct platform_device *pdev) 1250 { 1251 component_del(&pdev->dev, &sun4i_tcon_ops); 1252 1253 return 0; 1254 } 1255 1256 /* platform specific TCON muxing callbacks */ 1257 static int sun4i_a10_tcon_set_mux(struct sun4i_tcon *tcon, 1258 const struct drm_encoder *encoder) 1259 { 1260 struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev); 1261 u32 shift; 1262 1263 if (!tcon0) 1264 return -EINVAL; 1265 1266 switch (encoder->encoder_type) { 1267 case DRM_MODE_ENCODER_TMDS: 1268 /* HDMI */ 1269 shift = 8; 1270 break; 1271 default: 1272 return -EINVAL; 1273 } 1274 1275 regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG, 1276 0x3 << shift, tcon->id << shift); 1277 1278 return 0; 1279 } 1280 1281 static int sun5i_a13_tcon_set_mux(struct sun4i_tcon *tcon, 1282 const struct drm_encoder *encoder) 1283 { 1284 u32 val; 1285 1286 if (encoder->encoder_type == DRM_MODE_ENCODER_TVDAC) 1287 val = 1; 1288 else 1289 val = 0; 1290 1291 /* 1292 * FIXME: Undocumented bits 1293 */ 1294 return regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, val); 1295 } 1296 1297 static int sun6i_tcon_set_mux(struct sun4i_tcon *tcon, 1298 const struct drm_encoder *encoder) 1299 { 1300 struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev); 1301 u32 shift; 1302 1303 if (!tcon0) 1304 return -EINVAL; 1305 1306 switch (encoder->encoder_type) { 1307 case DRM_MODE_ENCODER_TMDS: 1308 /* HDMI */ 1309 shift = 8; 1310 break; 1311 default: 1312 /* TODO A31 has MIPI DSI but A31s does not */ 1313 return -EINVAL; 1314 } 1315 1316 regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG, 1317 0x3 << shift, tcon->id << shift); 1318 1319 return 0; 1320 } 1321 1322 static int sun8i_r40_tcon_tv_set_mux(struct sun4i_tcon *tcon, 1323 const struct drm_encoder *encoder) 1324 { 1325 struct device_node *port, *remote; 1326 struct platform_device *pdev; 1327 int id, ret; 1328 1329 /* find TCON TOP platform device and TCON id */ 1330 1331 port = of_graph_get_port_by_id(tcon->dev->of_node, 0); 1332 if (!port) 1333 return -EINVAL; 1334 1335 id = sun4i_tcon_of_get_id_from_port(port); 1336 of_node_put(port); 1337 1338 remote = of_graph_get_remote_node(tcon->dev->of_node, 0, -1); 1339 if (!remote) 1340 return -EINVAL; 1341 1342 pdev = of_find_device_by_node(remote); 1343 of_node_put(remote); 1344 if (!pdev) 1345 return -EINVAL; 1346 1347 if (encoder->encoder_type == DRM_MODE_ENCODER_TMDS) { 1348 ret = sun8i_tcon_top_set_hdmi_src(&pdev->dev, id); 1349 if (ret) 1350 return ret; 1351 } 1352 1353 return sun8i_tcon_top_de_config(&pdev->dev, tcon->id, id); 1354 } 1355 1356 static const struct sun4i_tcon_quirks sun4i_a10_quirks = { 1357 .has_channel_0 = true, 1358 .has_channel_1 = true, 1359 .set_mux = sun4i_a10_tcon_set_mux, 1360 }; 1361 1362 static const struct sun4i_tcon_quirks sun5i_a13_quirks = { 1363 .has_channel_0 = true, 1364 .has_channel_1 = true, 1365 .set_mux = sun5i_a13_tcon_set_mux, 1366 }; 1367 1368 static const struct sun4i_tcon_quirks sun6i_a31_quirks = { 1369 .has_channel_0 = true, 1370 .has_channel_1 = true, 1371 .has_lvds_alt = true, 1372 .needs_de_be_mux = true, 1373 .set_mux = sun6i_tcon_set_mux, 1374 }; 1375 1376 static const struct sun4i_tcon_quirks sun6i_a31s_quirks = { 1377 .has_channel_0 = true, 1378 .has_channel_1 = true, 1379 .needs_de_be_mux = true, 1380 }; 1381 1382 static const struct sun4i_tcon_quirks sun7i_a20_quirks = { 1383 .has_channel_0 = true, 1384 .has_channel_1 = true, 1385 /* Same display pipeline structure as A10 */ 1386 .set_mux = sun4i_a10_tcon_set_mux, 1387 }; 1388 1389 static const struct sun4i_tcon_quirks sun8i_a33_quirks = { 1390 .has_channel_0 = true, 1391 .has_lvds_alt = true, 1392 }; 1393 1394 static const struct sun4i_tcon_quirks sun8i_a83t_lcd_quirks = { 1395 .supports_lvds = true, 1396 .has_channel_0 = true, 1397 }; 1398 1399 static const struct sun4i_tcon_quirks sun8i_a83t_tv_quirks = { 1400 .has_channel_1 = true, 1401 }; 1402 1403 static const struct sun4i_tcon_quirks sun8i_r40_tv_quirks = { 1404 .has_channel_1 = true, 1405 .set_mux = sun8i_r40_tcon_tv_set_mux, 1406 }; 1407 1408 static const struct sun4i_tcon_quirks sun8i_v3s_quirks = { 1409 .has_channel_0 = true, 1410 }; 1411 1412 static const struct sun4i_tcon_quirks sun9i_a80_tcon_lcd_quirks = { 1413 .has_channel_0 = true, 1414 .needs_edp_reset = true, 1415 }; 1416 1417 static const struct sun4i_tcon_quirks sun9i_a80_tcon_tv_quirks = { 1418 .has_channel_1 = true, 1419 .needs_edp_reset = true, 1420 }; 1421 1422 /* sun4i_drv uses this list to check if a device node is a TCON */ 1423 const struct of_device_id sun4i_tcon_of_table[] = { 1424 { .compatible = "allwinner,sun4i-a10-tcon", .data = &sun4i_a10_quirks }, 1425 { .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks }, 1426 { .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks }, 1427 { .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks }, 1428 { .compatible = "allwinner,sun7i-a20-tcon", .data = &sun7i_a20_quirks }, 1429 { .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks }, 1430 { .compatible = "allwinner,sun8i-a83t-tcon-lcd", .data = &sun8i_a83t_lcd_quirks }, 1431 { .compatible = "allwinner,sun8i-a83t-tcon-tv", .data = &sun8i_a83t_tv_quirks }, 1432 { .compatible = "allwinner,sun8i-r40-tcon-tv", .data = &sun8i_r40_tv_quirks }, 1433 { .compatible = "allwinner,sun8i-v3s-tcon", .data = &sun8i_v3s_quirks }, 1434 { .compatible = "allwinner,sun9i-a80-tcon-lcd", .data = &sun9i_a80_tcon_lcd_quirks }, 1435 { .compatible = "allwinner,sun9i-a80-tcon-tv", .data = &sun9i_a80_tcon_tv_quirks }, 1436 { } 1437 }; 1438 MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table); 1439 EXPORT_SYMBOL(sun4i_tcon_of_table); 1440 1441 static struct platform_driver sun4i_tcon_platform_driver = { 1442 .probe = sun4i_tcon_probe, 1443 .remove = sun4i_tcon_remove, 1444 .driver = { 1445 .name = "sun4i-tcon", 1446 .of_match_table = sun4i_tcon_of_table, 1447 }, 1448 }; 1449 module_platform_driver(sun4i_tcon_platform_driver); 1450 1451 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); 1452 MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver"); 1453 MODULE_LICENSE("GPL"); 1454