1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright © 2006-2011 Intel Corporation
6 * Eric Anholt <eric@anholt.net>
12 #include <drm/drm_plane_helper.h>
13 #include "framebuffer.h"
15 #include "psb_intel_drv.h"
16 #include "psb_intel_reg.h"
17 #include "gma_display.h"
20 #define INTEL_LIMIT_I9XX_SDVO_DAC 0
21 #define INTEL_LIMIT_I9XX_LVDS 1
23 static const struct gma_limit_t psb_intel_limits[] = {
24 { /* INTEL_LIMIT_I9XX_SDVO_DAC */
25 .dot = {.min = 20000, .max = 400000},
26 .vco = {.min = 1400000, .max = 2800000},
27 .n = {.min = 1, .max = 6},
28 .m = {.min = 70, .max = 120},
29 .m1 = {.min = 8, .max = 18},
30 .m2 = {.min = 3, .max = 7},
31 .p = {.min = 5, .max = 80},
32 .p1 = {.min = 1, .max = 8},
33 .p2 = {.dot_limit = 200000, .p2_slow = 10, .p2_fast = 5},
34 .find_pll = gma_find_best_pll,
36 { /* INTEL_LIMIT_I9XX_LVDS */
37 .dot = {.min = 20000, .max = 400000},
38 .vco = {.min = 1400000, .max = 2800000},
39 .n = {.min = 1, .max = 6},
40 .m = {.min = 70, .max = 120},
41 .m1 = {.min = 8, .max = 18},
42 .m2 = {.min = 3, .max = 7},
43 .p = {.min = 7, .max = 98},
44 .p1 = {.min = 1, .max = 8},
45 /* The single-channel range is 25-112Mhz, and dual-channel
46 * is 80-224Mhz. Prefer single channel as much as possible.
48 .p2 = {.dot_limit = 112000, .p2_slow = 14, .p2_fast = 7},
49 .find_pll = gma_find_best_pll,
53 static const struct gma_limit_t *psb_intel_limit(struct drm_crtc *crtc,
56 const struct gma_limit_t *limit;
58 if (gma_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
59 limit = &psb_intel_limits[INTEL_LIMIT_I9XX_LVDS];
61 limit = &psb_intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC];
65 static void psb_intel_clock(int refclk, struct gma_clock_t *clock)
67 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
68 clock->p = clock->p1 * clock->p2;
69 clock->vco = refclk * clock->m / (clock->n + 2);
70 clock->dot = clock->vco / clock->p;
74 * Return the pipe currently connected to the panel fitter,
75 * or -1 if the panel fitter is not present or not in use
77 static int psb_intel_panel_fitter_pipe(struct drm_device *dev)
81 pfit_control = REG_READ(PFIT_CONTROL);
83 /* See if the panel fitter is in use */
84 if ((pfit_control & PFIT_ENABLE) == 0)
86 /* Must be on PIPE 1 for PSB */
90 static int psb_intel_crtc_mode_set(struct drm_crtc *crtc,
91 struct drm_display_mode *mode,
92 struct drm_display_mode *adjusted_mode,
94 struct drm_framebuffer *old_fb)
96 struct drm_device *dev = crtc->dev;
97 struct drm_psb_private *dev_priv = dev->dev_private;
98 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
99 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
100 int pipe = gma_crtc->pipe;
101 const struct psb_offset *map = &dev_priv->regmap[pipe];
103 struct gma_clock_t clock;
104 u32 dpll = 0, fp = 0, dspcntr, pipeconf;
105 bool ok, is_sdvo = false;
106 bool is_lvds = false, is_tv = false;
107 struct drm_mode_config *mode_config = &dev->mode_config;
108 struct drm_connector *connector;
109 const struct gma_limit_t *limit;
111 /* No scan out no play */
112 if (crtc->primary->fb == NULL) {
113 crtc_funcs->mode_set_base(crtc, x, y, old_fb);
117 list_for_each_entry(connector, &mode_config->connector_list, head) {
118 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
120 if (!connector->encoder
121 || connector->encoder->crtc != crtc)
124 switch (gma_encoder->type) {
125 case INTEL_OUTPUT_LVDS:
128 case INTEL_OUTPUT_SDVO:
131 case INTEL_OUTPUT_TVOUT:
139 limit = gma_crtc->clock_funcs->limit(crtc, refclk);
141 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk,
144 DRM_ERROR("Couldn't find PLL settings for mode! target: %d, actual: %d",
145 adjusted_mode->clock, clock.dot);
149 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
151 dpll = DPLL_VGA_MODE_DIS;
153 dpll |= DPLLB_MODE_LVDS;
154 dpll |= DPLL_DVO_HIGH_SPEED;
156 dpll |= DPLLB_MODE_DAC_SERIAL;
158 int sdvo_pixel_multiply =
159 adjusted_mode->clock / mode->clock;
160 dpll |= DPLL_DVO_HIGH_SPEED;
162 (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
165 /* compute bitmask from p1 value */
166 dpll |= (1 << (clock.p1 - 1)) << 16;
169 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
172 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
175 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
178 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
183 /* XXX: just matching BIOS for now */
184 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
187 dpll |= PLL_REF_INPUT_DREFCLK;
190 pipeconf = REG_READ(map->conf);
192 /* Set up the display plane register */
193 dspcntr = DISPPLANE_GAMMA_ENABLE;
196 dspcntr |= DISPPLANE_SEL_PIPE_A;
198 dspcntr |= DISPPLANE_SEL_PIPE_B;
200 dspcntr |= DISPLAY_PLANE_ENABLE;
201 pipeconf |= PIPEACONF_ENABLE;
202 dpll |= DPLL_VCO_ENABLE;
205 /* Disable the panel fitter if it was on our pipe */
206 if (psb_intel_panel_fitter_pipe(dev) == pipe)
207 REG_WRITE(PFIT_CONTROL, 0);
209 drm_mode_debug_printmodeline(mode);
211 if (dpll & DPLL_VCO_ENABLE) {
212 REG_WRITE(map->fp0, fp);
213 REG_WRITE(map->dpll, dpll & ~DPLL_VCO_ENABLE);
218 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
219 * This is an exception to the general rule that mode_set doesn't turn
223 u32 lvds = REG_READ(LVDS);
225 lvds &= ~LVDS_PIPEB_SELECT;
227 lvds |= LVDS_PIPEB_SELECT;
229 lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
230 /* Set the B0-B3 data pairs corresponding to
231 * whether we're going to
232 * set the DPLLs for dual-channel mode or not.
234 lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
236 lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
238 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
239 * appropriately here, but we need to look more
240 * thoroughly into how panels behave in the two modes.
243 REG_WRITE(LVDS, lvds);
247 REG_WRITE(map->fp0, fp);
248 REG_WRITE(map->dpll, dpll);
250 /* Wait for the clocks to stabilize. */
253 /* write it again -- the BIOS does, after all */
254 REG_WRITE(map->dpll, dpll);
257 /* Wait for the clocks to stabilize. */
260 REG_WRITE(map->htotal, (adjusted_mode->crtc_hdisplay - 1) |
261 ((adjusted_mode->crtc_htotal - 1) << 16));
262 REG_WRITE(map->hblank, (adjusted_mode->crtc_hblank_start - 1) |
263 ((adjusted_mode->crtc_hblank_end - 1) << 16));
264 REG_WRITE(map->hsync, (adjusted_mode->crtc_hsync_start - 1) |
265 ((adjusted_mode->crtc_hsync_end - 1) << 16));
266 REG_WRITE(map->vtotal, (adjusted_mode->crtc_vdisplay - 1) |
267 ((adjusted_mode->crtc_vtotal - 1) << 16));
268 REG_WRITE(map->vblank, (adjusted_mode->crtc_vblank_start - 1) |
269 ((adjusted_mode->crtc_vblank_end - 1) << 16));
270 REG_WRITE(map->vsync, (adjusted_mode->crtc_vsync_start - 1) |
271 ((adjusted_mode->crtc_vsync_end - 1) << 16));
272 /* pipesrc and dspsize control the size that is scaled from,
273 * which should always be the user's requested size.
276 ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
277 REG_WRITE(map->pos, 0);
279 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
280 REG_WRITE(map->conf, pipeconf);
283 gma_wait_for_vblank(dev);
285 REG_WRITE(map->cntr, dspcntr);
287 /* Flush the plane changes */
288 crtc_funcs->mode_set_base(crtc, x, y, old_fb);
290 gma_wait_for_vblank(dev);
295 /* Returns the clock of the currently programmed mode of the given pipe. */
296 static int psb_intel_crtc_clock_get(struct drm_device *dev,
297 struct drm_crtc *crtc)
299 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
300 struct drm_psb_private *dev_priv = dev->dev_private;
301 int pipe = gma_crtc->pipe;
302 const struct psb_offset *map = &dev_priv->regmap[pipe];
305 struct gma_clock_t clock;
307 struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
309 if (gma_power_begin(dev, false)) {
310 dpll = REG_READ(map->dpll);
311 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
312 fp = REG_READ(map->fp0);
314 fp = REG_READ(map->fp1);
315 is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
320 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
325 is_lvds = (pipe == 1) && (dev_priv->regs.psb.saveLVDS &
329 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
330 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
331 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
336 DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
337 DPLL_FPA01_P1_POST_DIV_SHIFT);
340 if ((dpll & PLL_REF_INPUT_MASK) ==
341 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
342 /* XXX: might not be 66MHz */
343 psb_intel_clock(66000, &clock);
345 psb_intel_clock(48000, &clock);
347 if (dpll & PLL_P1_DIVIDE_BY_TWO)
352 DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
353 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
355 if (dpll & PLL_P2_DIVIDE_BY_4)
360 psb_intel_clock(48000, &clock);
363 /* XXX: It would be nice to validate the clocks, but we can't reuse
364 * i830PllIsValid() because it relies on the xf86_config connector
365 * configuration being accurate, which it isn't necessarily.
371 /** Returns the currently programmed mode of the given pipe. */
372 struct drm_display_mode *psb_intel_crtc_mode_get(struct drm_device *dev,
373 struct drm_crtc *crtc)
375 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
376 int pipe = gma_crtc->pipe;
377 struct drm_display_mode *mode;
382 struct drm_psb_private *dev_priv = dev->dev_private;
383 struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
384 const struct psb_offset *map = &dev_priv->regmap[pipe];
386 if (gma_power_begin(dev, false)) {
387 htot = REG_READ(map->htotal);
388 hsync = REG_READ(map->hsync);
389 vtot = REG_READ(map->vtotal);
390 vsync = REG_READ(map->vsync);
399 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
403 mode->clock = psb_intel_crtc_clock_get(dev, crtc);
404 mode->hdisplay = (htot & 0xffff) + 1;
405 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
406 mode->hsync_start = (hsync & 0xffff) + 1;
407 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
408 mode->vdisplay = (vtot & 0xffff) + 1;
409 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
410 mode->vsync_start = (vsync & 0xffff) + 1;
411 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
413 drm_mode_set_name(mode);
414 drm_mode_set_crtcinfo(mode, 0);
419 const struct drm_crtc_helper_funcs psb_intel_helper_funcs = {
420 .dpms = gma_crtc_dpms,
421 .mode_set = psb_intel_crtc_mode_set,
422 .mode_set_base = gma_pipe_set_base,
423 .prepare = gma_crtc_prepare,
424 .commit = gma_crtc_commit,
425 .disable = gma_crtc_disable,
428 const struct drm_crtc_funcs psb_intel_crtc_funcs = {
429 .cursor_set = gma_crtc_cursor_set,
430 .cursor_move = gma_crtc_cursor_move,
431 .gamma_set = gma_crtc_gamma_set,
432 .set_config = gma_crtc_set_config,
433 .destroy = gma_crtc_destroy,
436 const struct gma_clock_funcs psb_clock_funcs = {
437 .clock = psb_intel_clock,
438 .limit = psb_intel_limit,
439 .pll_is_valid = gma_pll_is_valid,
443 * Set the default value of cursor control and base register
444 * to zero. This is a workaround for h/w defect on Oaktrail
446 static void psb_intel_cursor_init(struct drm_device *dev,
447 struct gma_crtc *gma_crtc)
449 struct drm_psb_private *dev_priv = dev->dev_private;
450 u32 control[3] = { CURACNTR, CURBCNTR, CURCCNTR };
451 u32 base[3] = { CURABASE, CURBBASE, CURCBASE };
452 struct gtt_range *cursor_gt;
454 if (dev_priv->ops->cursor_needs_phys) {
455 /* Allocate 4 pages of stolen mem for a hardware cursor. That
456 * is enough for the 64 x 64 ARGB cursors we support.
458 cursor_gt = psb_gtt_alloc_range(dev, 4 * PAGE_SIZE, "cursor", 1,
461 gma_crtc->cursor_gt = NULL;
464 gma_crtc->cursor_gt = cursor_gt;
465 gma_crtc->cursor_addr = dev_priv->stolen_base +
468 gma_crtc->cursor_gt = NULL;
472 REG_WRITE(control[gma_crtc->pipe], 0);
473 REG_WRITE(base[gma_crtc->pipe], 0);
476 void psb_intel_crtc_init(struct drm_device *dev, int pipe,
477 struct psb_intel_mode_device *mode_dev)
479 struct drm_psb_private *dev_priv = dev->dev_private;
480 struct gma_crtc *gma_crtc;
483 /* We allocate a extra array of drm_connector pointers
484 * for fbdev after the crtc */
485 gma_crtc = kzalloc(sizeof(struct gma_crtc) +
486 (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)),
488 if (gma_crtc == NULL)
491 gma_crtc->crtc_state =
492 kzalloc(sizeof(struct psb_intel_crtc_state), GFP_KERNEL);
493 if (!gma_crtc->crtc_state) {
494 dev_err(dev->dev, "Crtc state error: No memory\n");
499 /* Set the CRTC operations from the chip specific data */
500 drm_crtc_init(dev, &gma_crtc->base, dev_priv->ops->crtc_funcs);
502 /* Set the CRTC clock functions from chip specific data */
503 gma_crtc->clock_funcs = dev_priv->ops->clock_funcs;
505 drm_mode_crtc_set_gamma_size(&gma_crtc->base, 256);
506 gma_crtc->pipe = pipe;
507 gma_crtc->plane = pipe;
509 for (i = 0; i < 256; i++)
510 gma_crtc->lut_adj[i] = 0;
512 gma_crtc->mode_dev = mode_dev;
513 gma_crtc->cursor_addr = 0;
515 drm_crtc_helper_add(&gma_crtc->base,
516 dev_priv->ops->crtc_helper);
518 /* Setup the array of drm_connector pointer array */
519 gma_crtc->mode_set.crtc = &gma_crtc->base;
520 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
521 dev_priv->plane_to_crtc_mapping[gma_crtc->plane] != NULL);
522 dev_priv->plane_to_crtc_mapping[gma_crtc->plane] = &gma_crtc->base;
523 dev_priv->pipe_to_crtc_mapping[gma_crtc->pipe] = &gma_crtc->base;
524 gma_crtc->mode_set.connectors = (struct drm_connector **)(gma_crtc + 1);
525 gma_crtc->mode_set.num_connectors = 0;
526 psb_intel_cursor_init(dev, gma_crtc);
528 /* Set to true so that the pipe is forced off on initial config. */
529 gma_crtc->active = true;
532 struct drm_crtc *psb_intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
534 struct drm_crtc *crtc = NULL;
536 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
537 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
538 if (gma_crtc->pipe == pipe)
544 int gma_connector_clones(struct drm_device *dev, int type_mask)
547 struct drm_connector *connector;
550 list_for_each_entry(connector, &dev->mode_config.connector_list,
552 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
553 if (type_mask & (1 << gma_encoder->type))
554 index_mask |= (1 << entry);