1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright © 2006-2011 Intel Corporation 4 * 5 * Authors: 6 * Eric Anholt <eric@anholt.net> 7 */ 8 9 #include <linux/delay.h> 10 #include <linux/i2c.h> 11 12 #include <drm/drm_fourcc.h> 13 #include <drm/drm_modeset_helper.h> 14 #include <drm/drm_modeset_helper_vtables.h> 15 #include <drm/drm_plane.h> 16 #include <drm/drm_plane_helper.h> 17 #include <drm/drm_print.h> 18 19 #include "framebuffer.h" 20 #include "gem.h" 21 #include "gma_display.h" 22 #include "power.h" 23 #include "psb_drv.h" 24 #include "psb_intel_drv.h" 25 #include "psb_intel_reg.h" 26 27 #define INTEL_LIMIT_I9XX_SDVO_DAC 0 28 #define INTEL_LIMIT_I9XX_LVDS 1 29 30 static const struct gma_limit_t psb_intel_limits[] = { 31 { /* INTEL_LIMIT_I9XX_SDVO_DAC */ 32 .dot = {.min = 20000, .max = 400000}, 33 .vco = {.min = 1400000, .max = 2800000}, 34 .n = {.min = 1, .max = 6}, 35 .m = {.min = 70, .max = 120}, 36 .m1 = {.min = 8, .max = 18}, 37 .m2 = {.min = 3, .max = 7}, 38 .p = {.min = 5, .max = 80}, 39 .p1 = {.min = 1, .max = 8}, 40 .p2 = {.dot_limit = 200000, .p2_slow = 10, .p2_fast = 5}, 41 .find_pll = gma_find_best_pll, 42 }, 43 { /* INTEL_LIMIT_I9XX_LVDS */ 44 .dot = {.min = 20000, .max = 400000}, 45 .vco = {.min = 1400000, .max = 2800000}, 46 .n = {.min = 1, .max = 6}, 47 .m = {.min = 70, .max = 120}, 48 .m1 = {.min = 8, .max = 18}, 49 .m2 = {.min = 3, .max = 7}, 50 .p = {.min = 7, .max = 98}, 51 .p1 = {.min = 1, .max = 8}, 52 /* The single-channel range is 25-112Mhz, and dual-channel 53 * is 80-224Mhz. Prefer single channel as much as possible. 54 */ 55 .p2 = {.dot_limit = 112000, .p2_slow = 14, .p2_fast = 7}, 56 .find_pll = gma_find_best_pll, 57 }, 58 }; 59 60 static const struct gma_limit_t *psb_intel_limit(struct drm_crtc *crtc, 61 int refclk) 62 { 63 const struct gma_limit_t *limit; 64 65 if (gma_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) 66 limit = &psb_intel_limits[INTEL_LIMIT_I9XX_LVDS]; 67 else 68 limit = &psb_intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC]; 69 return limit; 70 } 71 72 static void psb_intel_clock(int refclk, struct gma_clock_t *clock) 73 { 74 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2); 75 clock->p = clock->p1 * clock->p2; 76 clock->vco = refclk * clock->m / (clock->n + 2); 77 clock->dot = clock->vco / clock->p; 78 } 79 80 /* 81 * Return the pipe currently connected to the panel fitter, 82 * or -1 if the panel fitter is not present or not in use 83 */ 84 static int psb_intel_panel_fitter_pipe(struct drm_device *dev) 85 { 86 u32 pfit_control; 87 88 pfit_control = REG_READ(PFIT_CONTROL); 89 90 /* See if the panel fitter is in use */ 91 if ((pfit_control & PFIT_ENABLE) == 0) 92 return -1; 93 /* Must be on PIPE 1 for PSB */ 94 return 1; 95 } 96 97 static int psb_intel_crtc_mode_set(struct drm_crtc *crtc, 98 struct drm_display_mode *mode, 99 struct drm_display_mode *adjusted_mode, 100 int x, int y, 101 struct drm_framebuffer *old_fb) 102 { 103 struct drm_device *dev = crtc->dev; 104 struct drm_psb_private *dev_priv = to_drm_psb_private(dev); 105 struct gma_crtc *gma_crtc = to_gma_crtc(crtc); 106 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 107 int pipe = gma_crtc->pipe; 108 const struct psb_offset *map = &dev_priv->regmap[pipe]; 109 int refclk; 110 struct gma_clock_t clock; 111 u32 dpll = 0, fp = 0, dspcntr, pipeconf; 112 bool ok, is_sdvo = false; 113 bool is_lvds = false, is_tv = false; 114 struct drm_connector_list_iter conn_iter; 115 struct drm_connector *connector; 116 const struct gma_limit_t *limit; 117 118 /* No scan out no play */ 119 if (crtc->primary->fb == NULL) { 120 crtc_funcs->mode_set_base(crtc, x, y, old_fb); 121 return 0; 122 } 123 124 drm_connector_list_iter_begin(dev, &conn_iter); 125 drm_for_each_connector_iter(connector, &conn_iter) { 126 struct gma_encoder *gma_encoder = gma_attached_encoder(connector); 127 128 if (!connector->encoder 129 || connector->encoder->crtc != crtc) 130 continue; 131 132 switch (gma_encoder->type) { 133 case INTEL_OUTPUT_LVDS: 134 is_lvds = true; 135 break; 136 case INTEL_OUTPUT_SDVO: 137 is_sdvo = true; 138 break; 139 case INTEL_OUTPUT_TVOUT: 140 is_tv = true; 141 break; 142 } 143 144 break; 145 } 146 drm_connector_list_iter_end(&conn_iter); 147 148 refclk = 96000; 149 150 limit = gma_crtc->clock_funcs->limit(crtc, refclk); 151 152 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, 153 &clock); 154 if (!ok) { 155 DRM_ERROR("Couldn't find PLL settings for mode! target: %d, actual: %d", 156 adjusted_mode->clock, clock.dot); 157 return 0; 158 } 159 160 fp = clock.n << 16 | clock.m1 << 8 | clock.m2; 161 162 dpll = DPLL_VGA_MODE_DIS; 163 if (is_lvds) { 164 dpll |= DPLLB_MODE_LVDS; 165 dpll |= DPLL_DVO_HIGH_SPEED; 166 } else 167 dpll |= DPLLB_MODE_DAC_SERIAL; 168 if (is_sdvo) { 169 int sdvo_pixel_multiply = 170 adjusted_mode->clock / mode->clock; 171 dpll |= DPLL_DVO_HIGH_SPEED; 172 dpll |= 173 (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES; 174 } 175 176 /* compute bitmask from p1 value */ 177 dpll |= (1 << (clock.p1 - 1)) << 16; 178 switch (clock.p2) { 179 case 5: 180 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5; 181 break; 182 case 7: 183 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7; 184 break; 185 case 10: 186 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10; 187 break; 188 case 14: 189 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14; 190 break; 191 } 192 193 if (is_tv) { 194 /* XXX: just matching BIOS for now */ 195 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */ 196 dpll |= 3; 197 } 198 dpll |= PLL_REF_INPUT_DREFCLK; 199 200 /* setup pipeconf */ 201 pipeconf = REG_READ(map->conf); 202 203 /* Set up the display plane register */ 204 dspcntr = DISPPLANE_GAMMA_ENABLE; 205 206 if (pipe == 0) 207 dspcntr |= DISPPLANE_SEL_PIPE_A; 208 else 209 dspcntr |= DISPPLANE_SEL_PIPE_B; 210 211 dspcntr |= DISPLAY_PLANE_ENABLE; 212 pipeconf |= PIPEACONF_ENABLE; 213 dpll |= DPLL_VCO_ENABLE; 214 215 216 /* Disable the panel fitter if it was on our pipe */ 217 if (psb_intel_panel_fitter_pipe(dev) == pipe) 218 REG_WRITE(PFIT_CONTROL, 0); 219 220 drm_mode_debug_printmodeline(mode); 221 222 if (dpll & DPLL_VCO_ENABLE) { 223 REG_WRITE(map->fp0, fp); 224 REG_WRITE(map->dpll, dpll & ~DPLL_VCO_ENABLE); 225 REG_READ(map->dpll); 226 udelay(150); 227 } 228 229 /* The LVDS pin pair needs to be on before the DPLLs are enabled. 230 * This is an exception to the general rule that mode_set doesn't turn 231 * things on. 232 */ 233 if (is_lvds) { 234 u32 lvds = REG_READ(LVDS); 235 236 lvds &= ~LVDS_PIPEB_SELECT; 237 if (pipe == 1) 238 lvds |= LVDS_PIPEB_SELECT; 239 240 lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP; 241 /* Set the B0-B3 data pairs corresponding to 242 * whether we're going to 243 * set the DPLLs for dual-channel mode or not. 244 */ 245 lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP); 246 if (clock.p2 == 7) 247 lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP; 248 249 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP) 250 * appropriately here, but we need to look more 251 * thoroughly into how panels behave in the two modes. 252 */ 253 254 REG_WRITE(LVDS, lvds); 255 REG_READ(LVDS); 256 } 257 258 REG_WRITE(map->fp0, fp); 259 REG_WRITE(map->dpll, dpll); 260 REG_READ(map->dpll); 261 /* Wait for the clocks to stabilize. */ 262 udelay(150); 263 264 /* write it again -- the BIOS does, after all */ 265 REG_WRITE(map->dpll, dpll); 266 267 REG_READ(map->dpll); 268 /* Wait for the clocks to stabilize. */ 269 udelay(150); 270 271 REG_WRITE(map->htotal, (adjusted_mode->crtc_hdisplay - 1) | 272 ((adjusted_mode->crtc_htotal - 1) << 16)); 273 REG_WRITE(map->hblank, (adjusted_mode->crtc_hblank_start - 1) | 274 ((adjusted_mode->crtc_hblank_end - 1) << 16)); 275 REG_WRITE(map->hsync, (adjusted_mode->crtc_hsync_start - 1) | 276 ((adjusted_mode->crtc_hsync_end - 1) << 16)); 277 REG_WRITE(map->vtotal, (adjusted_mode->crtc_vdisplay - 1) | 278 ((adjusted_mode->crtc_vtotal - 1) << 16)); 279 REG_WRITE(map->vblank, (adjusted_mode->crtc_vblank_start - 1) | 280 ((adjusted_mode->crtc_vblank_end - 1) << 16)); 281 REG_WRITE(map->vsync, (adjusted_mode->crtc_vsync_start - 1) | 282 ((adjusted_mode->crtc_vsync_end - 1) << 16)); 283 /* pipesrc and dspsize control the size that is scaled from, 284 * which should always be the user's requested size. 285 */ 286 REG_WRITE(map->size, 287 ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1)); 288 REG_WRITE(map->pos, 0); 289 REG_WRITE(map->src, 290 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1)); 291 REG_WRITE(map->conf, pipeconf); 292 REG_READ(map->conf); 293 294 gma_wait_for_vblank(dev); 295 296 REG_WRITE(map->cntr, dspcntr); 297 298 /* Flush the plane changes */ 299 crtc_funcs->mode_set_base(crtc, x, y, old_fb); 300 301 gma_wait_for_vblank(dev); 302 303 return 0; 304 } 305 306 /* Returns the clock of the currently programmed mode of the given pipe. */ 307 static int psb_intel_crtc_clock_get(struct drm_device *dev, 308 struct drm_crtc *crtc) 309 { 310 struct gma_crtc *gma_crtc = to_gma_crtc(crtc); 311 struct drm_psb_private *dev_priv = to_drm_psb_private(dev); 312 int pipe = gma_crtc->pipe; 313 const struct psb_offset *map = &dev_priv->regmap[pipe]; 314 u32 dpll; 315 u32 fp; 316 struct gma_clock_t clock; 317 bool is_lvds; 318 struct psb_pipe *p = &dev_priv->regs.pipe[pipe]; 319 320 if (gma_power_begin(dev, false)) { 321 dpll = REG_READ(map->dpll); 322 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0) 323 fp = REG_READ(map->fp0); 324 else 325 fp = REG_READ(map->fp1); 326 is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN); 327 gma_power_end(dev); 328 } else { 329 dpll = p->dpll; 330 331 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0) 332 fp = p->fp0; 333 else 334 fp = p->fp1; 335 336 is_lvds = (pipe == 1) && (dev_priv->regs.psb.saveLVDS & 337 LVDS_PORT_EN); 338 } 339 340 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT; 341 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT; 342 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT; 343 344 if (is_lvds) { 345 clock.p1 = 346 ffs((dpll & 347 DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >> 348 DPLL_FPA01_P1_POST_DIV_SHIFT); 349 clock.p2 = 14; 350 351 if ((dpll & PLL_REF_INPUT_MASK) == 352 PLLB_REF_INPUT_SPREADSPECTRUMIN) { 353 /* XXX: might not be 66MHz */ 354 psb_intel_clock(66000, &clock); 355 } else 356 psb_intel_clock(48000, &clock); 357 } else { 358 if (dpll & PLL_P1_DIVIDE_BY_TWO) 359 clock.p1 = 2; 360 else { 361 clock.p1 = 362 ((dpll & 363 DPLL_FPA01_P1_POST_DIV_MASK_I830) >> 364 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2; 365 } 366 if (dpll & PLL_P2_DIVIDE_BY_4) 367 clock.p2 = 4; 368 else 369 clock.p2 = 2; 370 371 psb_intel_clock(48000, &clock); 372 } 373 374 /* XXX: It would be nice to validate the clocks, but we can't reuse 375 * i830PllIsValid() because it relies on the xf86_config connector 376 * configuration being accurate, which it isn't necessarily. 377 */ 378 379 return clock.dot; 380 } 381 382 /** Returns the currently programmed mode of the given pipe. */ 383 struct drm_display_mode *psb_intel_crtc_mode_get(struct drm_device *dev, 384 struct drm_crtc *crtc) 385 { 386 struct gma_crtc *gma_crtc = to_gma_crtc(crtc); 387 int pipe = gma_crtc->pipe; 388 struct drm_display_mode *mode; 389 int htot; 390 int hsync; 391 int vtot; 392 int vsync; 393 struct drm_psb_private *dev_priv = to_drm_psb_private(dev); 394 struct psb_pipe *p = &dev_priv->regs.pipe[pipe]; 395 const struct psb_offset *map = &dev_priv->regmap[pipe]; 396 397 if (gma_power_begin(dev, false)) { 398 htot = REG_READ(map->htotal); 399 hsync = REG_READ(map->hsync); 400 vtot = REG_READ(map->vtotal); 401 vsync = REG_READ(map->vsync); 402 gma_power_end(dev); 403 } else { 404 htot = p->htotal; 405 hsync = p->hsync; 406 vtot = p->vtotal; 407 vsync = p->vsync; 408 } 409 410 mode = kzalloc_obj(*mode); 411 if (!mode) 412 return NULL; 413 414 mode->clock = psb_intel_crtc_clock_get(dev, crtc); 415 mode->hdisplay = (htot & 0xffff) + 1; 416 mode->htotal = ((htot & 0xffff0000) >> 16) + 1; 417 mode->hsync_start = (hsync & 0xffff) + 1; 418 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1; 419 mode->vdisplay = (vtot & 0xffff) + 1; 420 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1; 421 mode->vsync_start = (vsync & 0xffff) + 1; 422 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1; 423 424 drm_mode_set_name(mode); 425 drm_mode_set_crtcinfo(mode, 0); 426 427 return mode; 428 } 429 430 const struct drm_crtc_helper_funcs psb_intel_helper_funcs = { 431 .dpms = gma_crtc_dpms, 432 .mode_set = psb_intel_crtc_mode_set, 433 .mode_set_base = gma_pipe_set_base, 434 .prepare = gma_crtc_prepare, 435 .commit = gma_crtc_commit, 436 .disable = gma_crtc_disable, 437 }; 438 439 const struct gma_clock_funcs psb_clock_funcs = { 440 .clock = psb_intel_clock, 441 .limit = psb_intel_limit, 442 .pll_is_valid = gma_pll_is_valid, 443 }; 444 445 /* 446 * Set the default value of cursor control and base register 447 * to zero. This is a workaround for h/w defect on Oaktrail 448 */ 449 static void psb_intel_cursor_init(struct drm_device *dev, 450 struct gma_crtc *gma_crtc) 451 { 452 struct drm_psb_private *dev_priv = to_drm_psb_private(dev); 453 u32 control[3] = { CURACNTR, CURBCNTR, CURCCNTR }; 454 u32 base[3] = { CURABASE, CURBBASE, CURCBASE }; 455 struct psb_gem_object *cursor_pobj; 456 457 if (dev_priv->ops->cursor_needs_phys) { 458 /* Allocate 4 pages of stolen mem for a hardware cursor. That 459 * is enough for the 64 x 64 ARGB cursors we support. 460 */ 461 cursor_pobj = psb_gem_create(dev, 4 * PAGE_SIZE, "cursor", true, PAGE_SIZE); 462 if (IS_ERR(cursor_pobj)) { 463 gma_crtc->cursor_pobj = NULL; 464 goto out; 465 } 466 gma_crtc->cursor_pobj = cursor_pobj; 467 gma_crtc->cursor_addr = dev_priv->stolen_base + cursor_pobj->offset; 468 } else { 469 gma_crtc->cursor_pobj = NULL; 470 } 471 472 out: 473 REG_WRITE(control[gma_crtc->pipe], 0); 474 REG_WRITE(base[gma_crtc->pipe], 0); 475 } 476 477 /* 478 * The display engine programs the primary plane with 479 * DISPPLANE_32BPP_NO_ALPHA, so no formats with alpha. 480 */ 481 static const uint32_t gma_primary_formats[] = { 482 DRM_FORMAT_XRGB8888, 483 }; 484 485 static const struct drm_plane_funcs gma_primary_plane_funcs = { 486 .update_plane = drm_plane_helper_update_primary, 487 .disable_plane = drm_plane_helper_disable_primary, 488 }; 489 490 void psb_intel_crtc_init(struct drm_device *dev, int pipe, 491 struct psb_intel_mode_device *mode_dev) 492 { 493 struct drm_psb_private *dev_priv = to_drm_psb_private(dev); 494 struct gma_crtc *gma_crtc; 495 struct drm_plane *primary; 496 int i; 497 498 /* We allocate a extra array of drm_connector pointers 499 * for fbdev after the crtc */ 500 gma_crtc = kzalloc(sizeof(struct gma_crtc) + 501 (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), 502 GFP_KERNEL); 503 if (gma_crtc == NULL) 504 return; 505 506 gma_crtc->crtc_state = 507 kzalloc_obj(struct psb_intel_crtc_state); 508 if (!gma_crtc->crtc_state) { 509 dev_err(dev->dev, "Crtc state error: No memory\n"); 510 kfree(gma_crtc); 511 return; 512 } 513 514 primary = drmm_universal_plane_alloc(dev, struct drm_plane, dev, 0, 515 &gma_primary_plane_funcs, 516 gma_primary_formats, 517 ARRAY_SIZE(gma_primary_formats), 518 NULL, DRM_PLANE_TYPE_PRIMARY, NULL); 519 if (IS_ERR(primary)) 520 goto err_free_crtc; 521 522 if (drm_crtc_init_with_planes(dev, &gma_crtc->base, primary, NULL, 523 &gma_crtc_funcs, NULL)) 524 goto err_free_crtc; 525 526 /* Set the CRTC clock functions from chip specific data */ 527 gma_crtc->clock_funcs = dev_priv->ops->clock_funcs; 528 529 drm_mode_crtc_set_gamma_size(&gma_crtc->base, 256); 530 gma_crtc->pipe = pipe; 531 gma_crtc->plane = pipe; 532 533 for (i = 0; i < 256; i++) 534 gma_crtc->lut_adj[i] = 0; 535 536 gma_crtc->mode_dev = mode_dev; 537 gma_crtc->cursor_addr = 0; 538 539 drm_crtc_helper_add(&gma_crtc->base, 540 dev_priv->ops->crtc_helper); 541 542 /* Setup the array of drm_connector pointer array */ 543 gma_crtc->mode_set.crtc = &gma_crtc->base; 544 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) || 545 dev_priv->plane_to_crtc_mapping[gma_crtc->plane] != NULL); 546 dev_priv->plane_to_crtc_mapping[gma_crtc->plane] = &gma_crtc->base; 547 dev_priv->pipe_to_crtc_mapping[gma_crtc->pipe] = &gma_crtc->base; 548 gma_crtc->mode_set.connectors = (struct drm_connector **)(gma_crtc + 1); 549 gma_crtc->mode_set.num_connectors = 0; 550 psb_intel_cursor_init(dev, gma_crtc); 551 552 /* Set to true so that the pipe is forced off on initial config. */ 553 gma_crtc->active = true; 554 return; 555 556 err_free_crtc: 557 kfree(gma_crtc->crtc_state); 558 kfree(gma_crtc); 559 } 560 561 struct drm_crtc *psb_intel_get_crtc_from_pipe(struct drm_device *dev, int pipe) 562 { 563 struct drm_crtc *crtc; 564 565 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 566 struct gma_crtc *gma_crtc = to_gma_crtc(crtc); 567 568 if (gma_crtc->pipe == pipe) 569 return crtc; 570 } 571 return NULL; 572 } 573 574 int gma_connector_clones(struct drm_device *dev, int type_mask) 575 { 576 struct drm_connector_list_iter conn_iter; 577 struct drm_connector *connector; 578 int index_mask = 0; 579 int entry = 0; 580 581 drm_connector_list_iter_begin(dev, &conn_iter); 582 drm_for_each_connector_iter(connector, &conn_iter) { 583 struct gma_encoder *gma_encoder = gma_attached_encoder(connector); 584 if (type_mask & (1 << gma_encoder->type)) 585 index_mask |= (1 << entry); 586 entry++; 587 } 588 drm_connector_list_iter_end(&conn_iter); 589 590 return index_mask; 591 } 592