1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright © 2006-2011 Intel Corporation
9 #include <linux/delay.h>
10 #include <linux/i2c.h>
12 #include <drm/drm_modeset_helper.h>
13 #include <drm/drm_modeset_helper_vtables.h>
15 #include "framebuffer.h"
17 #include "gma_display.h"
20 #include "psb_intel_drv.h"
21 #include "psb_intel_reg.h"
23 #define INTEL_LIMIT_I9XX_SDVO_DAC 0
24 #define INTEL_LIMIT_I9XX_LVDS 1
26 static const struct gma_limit_t psb_intel_limits[] = {
27 { /* INTEL_LIMIT_I9XX_SDVO_DAC */
28 .dot = {.min = 20000, .max = 400000},
29 .vco = {.min = 1400000, .max = 2800000},
30 .n = {.min = 1, .max = 6},
31 .m = {.min = 70, .max = 120},
32 .m1 = {.min = 8, .max = 18},
33 .m2 = {.min = 3, .max = 7},
34 .p = {.min = 5, .max = 80},
35 .p1 = {.min = 1, .max = 8},
36 .p2 = {.dot_limit = 200000, .p2_slow = 10, .p2_fast = 5},
37 .find_pll = gma_find_best_pll,
39 { /* INTEL_LIMIT_I9XX_LVDS */
40 .dot = {.min = 20000, .max = 400000},
41 .vco = {.min = 1400000, .max = 2800000},
42 .n = {.min = 1, .max = 6},
43 .m = {.min = 70, .max = 120},
44 .m1 = {.min = 8, .max = 18},
45 .m2 = {.min = 3, .max = 7},
46 .p = {.min = 7, .max = 98},
47 .p1 = {.min = 1, .max = 8},
48 /* The single-channel range is 25-112Mhz, and dual-channel
49 * is 80-224Mhz. Prefer single channel as much as possible.
51 .p2 = {.dot_limit = 112000, .p2_slow = 14, .p2_fast = 7},
52 .find_pll = gma_find_best_pll,
56 static const struct gma_limit_t *psb_intel_limit(struct drm_crtc *crtc,
59 const struct gma_limit_t *limit;
61 if (gma_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
62 limit = &psb_intel_limits[INTEL_LIMIT_I9XX_LVDS];
64 limit = &psb_intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC];
68 static void psb_intel_clock(int refclk, struct gma_clock_t *clock)
70 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
71 clock->p = clock->p1 * clock->p2;
72 clock->vco = refclk * clock->m / (clock->n + 2);
73 clock->dot = clock->vco / clock->p;
77 * Return the pipe currently connected to the panel fitter,
78 * or -1 if the panel fitter is not present or not in use
80 static int psb_intel_panel_fitter_pipe(struct drm_device *dev)
84 pfit_control = REG_READ(PFIT_CONTROL);
86 /* See if the panel fitter is in use */
87 if ((pfit_control & PFIT_ENABLE) == 0)
89 /* Must be on PIPE 1 for PSB */
93 static int psb_intel_crtc_mode_set(struct drm_crtc *crtc,
94 struct drm_display_mode *mode,
95 struct drm_display_mode *adjusted_mode,
97 struct drm_framebuffer *old_fb)
99 struct drm_device *dev = crtc->dev;
100 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
101 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
102 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
103 int pipe = gma_crtc->pipe;
104 const struct psb_offset *map = &dev_priv->regmap[pipe];
106 struct gma_clock_t clock;
107 u32 dpll = 0, fp = 0, dspcntr, pipeconf;
108 bool ok, is_sdvo = false;
109 bool is_lvds = false, is_tv = false;
110 struct drm_connector_list_iter conn_iter;
111 struct drm_connector *connector;
112 const struct gma_limit_t *limit;
114 /* No scan out no play */
115 if (crtc->primary->fb == NULL) {
116 crtc_funcs->mode_set_base(crtc, x, y, old_fb);
120 drm_connector_list_iter_begin(dev, &conn_iter);
121 drm_for_each_connector_iter(connector, &conn_iter) {
122 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
124 if (!connector->encoder
125 || connector->encoder->crtc != crtc)
128 switch (gma_encoder->type) {
129 case INTEL_OUTPUT_LVDS:
132 case INTEL_OUTPUT_SDVO:
135 case INTEL_OUTPUT_TVOUT:
142 drm_connector_list_iter_end(&conn_iter);
146 limit = gma_crtc->clock_funcs->limit(crtc, refclk);
148 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk,
151 DRM_ERROR("Couldn't find PLL settings for mode! target: %d, actual: %d",
152 adjusted_mode->clock, clock.dot);
156 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
158 dpll = DPLL_VGA_MODE_DIS;
160 dpll |= DPLLB_MODE_LVDS;
161 dpll |= DPLL_DVO_HIGH_SPEED;
163 dpll |= DPLLB_MODE_DAC_SERIAL;
165 int sdvo_pixel_multiply =
166 adjusted_mode->clock / mode->clock;
167 dpll |= DPLL_DVO_HIGH_SPEED;
169 (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
172 /* compute bitmask from p1 value */
173 dpll |= (1 << (clock.p1 - 1)) << 16;
176 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
179 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
182 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
185 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
190 /* XXX: just matching BIOS for now */
191 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
194 dpll |= PLL_REF_INPUT_DREFCLK;
197 pipeconf = REG_READ(map->conf);
199 /* Set up the display plane register */
200 dspcntr = DISPPLANE_GAMMA_ENABLE;
203 dspcntr |= DISPPLANE_SEL_PIPE_A;
205 dspcntr |= DISPPLANE_SEL_PIPE_B;
207 dspcntr |= DISPLAY_PLANE_ENABLE;
208 pipeconf |= PIPEACONF_ENABLE;
209 dpll |= DPLL_VCO_ENABLE;
212 /* Disable the panel fitter if it was on our pipe */
213 if (psb_intel_panel_fitter_pipe(dev) == pipe)
214 REG_WRITE(PFIT_CONTROL, 0);
216 drm_mode_debug_printmodeline(mode);
218 if (dpll & DPLL_VCO_ENABLE) {
219 REG_WRITE(map->fp0, fp);
220 REG_WRITE(map->dpll, dpll & ~DPLL_VCO_ENABLE);
225 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
226 * This is an exception to the general rule that mode_set doesn't turn
230 u32 lvds = REG_READ(LVDS);
232 lvds &= ~LVDS_PIPEB_SELECT;
234 lvds |= LVDS_PIPEB_SELECT;
236 lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
237 /* Set the B0-B3 data pairs corresponding to
238 * whether we're going to
239 * set the DPLLs for dual-channel mode or not.
241 lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
243 lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
245 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
246 * appropriately here, but we need to look more
247 * thoroughly into how panels behave in the two modes.
250 REG_WRITE(LVDS, lvds);
254 REG_WRITE(map->fp0, fp);
255 REG_WRITE(map->dpll, dpll);
257 /* Wait for the clocks to stabilize. */
260 /* write it again -- the BIOS does, after all */
261 REG_WRITE(map->dpll, dpll);
264 /* Wait for the clocks to stabilize. */
267 REG_WRITE(map->htotal, (adjusted_mode->crtc_hdisplay - 1) |
268 ((adjusted_mode->crtc_htotal - 1) << 16));
269 REG_WRITE(map->hblank, (adjusted_mode->crtc_hblank_start - 1) |
270 ((adjusted_mode->crtc_hblank_end - 1) << 16));
271 REG_WRITE(map->hsync, (adjusted_mode->crtc_hsync_start - 1) |
272 ((adjusted_mode->crtc_hsync_end - 1) << 16));
273 REG_WRITE(map->vtotal, (adjusted_mode->crtc_vdisplay - 1) |
274 ((adjusted_mode->crtc_vtotal - 1) << 16));
275 REG_WRITE(map->vblank, (adjusted_mode->crtc_vblank_start - 1) |
276 ((adjusted_mode->crtc_vblank_end - 1) << 16));
277 REG_WRITE(map->vsync, (adjusted_mode->crtc_vsync_start - 1) |
278 ((adjusted_mode->crtc_vsync_end - 1) << 16));
279 /* pipesrc and dspsize control the size that is scaled from,
280 * which should always be the user's requested size.
283 ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
284 REG_WRITE(map->pos, 0);
286 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
287 REG_WRITE(map->conf, pipeconf);
290 gma_wait_for_vblank(dev);
292 REG_WRITE(map->cntr, dspcntr);
294 /* Flush the plane changes */
295 crtc_funcs->mode_set_base(crtc, x, y, old_fb);
297 gma_wait_for_vblank(dev);
302 /* Returns the clock of the currently programmed mode of the given pipe. */
303 static int psb_intel_crtc_clock_get(struct drm_device *dev,
304 struct drm_crtc *crtc)
306 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
307 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
308 int pipe = gma_crtc->pipe;
309 const struct psb_offset *map = &dev_priv->regmap[pipe];
312 struct gma_clock_t clock;
314 struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
316 if (gma_power_begin(dev, false)) {
317 dpll = REG_READ(map->dpll);
318 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
319 fp = REG_READ(map->fp0);
321 fp = REG_READ(map->fp1);
322 is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
327 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
332 is_lvds = (pipe == 1) && (dev_priv->regs.psb.saveLVDS &
336 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
337 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
338 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
343 DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
344 DPLL_FPA01_P1_POST_DIV_SHIFT);
347 if ((dpll & PLL_REF_INPUT_MASK) ==
348 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
349 /* XXX: might not be 66MHz */
350 psb_intel_clock(66000, &clock);
352 psb_intel_clock(48000, &clock);
354 if (dpll & PLL_P1_DIVIDE_BY_TWO)
359 DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
360 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
362 if (dpll & PLL_P2_DIVIDE_BY_4)
367 psb_intel_clock(48000, &clock);
370 /* XXX: It would be nice to validate the clocks, but we can't reuse
371 * i830PllIsValid() because it relies on the xf86_config connector
372 * configuration being accurate, which it isn't necessarily.
378 /** Returns the currently programmed mode of the given pipe. */
379 struct drm_display_mode *psb_intel_crtc_mode_get(struct drm_device *dev,
380 struct drm_crtc *crtc)
382 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
383 int pipe = gma_crtc->pipe;
384 struct drm_display_mode *mode;
389 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
390 struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
391 const struct psb_offset *map = &dev_priv->regmap[pipe];
393 if (gma_power_begin(dev, false)) {
394 htot = REG_READ(map->htotal);
395 hsync = REG_READ(map->hsync);
396 vtot = REG_READ(map->vtotal);
397 vsync = REG_READ(map->vsync);
406 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
410 mode->clock = psb_intel_crtc_clock_get(dev, crtc);
411 mode->hdisplay = (htot & 0xffff) + 1;
412 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
413 mode->hsync_start = (hsync & 0xffff) + 1;
414 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
415 mode->vdisplay = (vtot & 0xffff) + 1;
416 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
417 mode->vsync_start = (vsync & 0xffff) + 1;
418 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
420 drm_mode_set_name(mode);
421 drm_mode_set_crtcinfo(mode, 0);
426 const struct drm_crtc_helper_funcs psb_intel_helper_funcs = {
427 .dpms = gma_crtc_dpms,
428 .mode_set = psb_intel_crtc_mode_set,
429 .mode_set_base = gma_pipe_set_base,
430 .prepare = gma_crtc_prepare,
431 .commit = gma_crtc_commit,
432 .disable = gma_crtc_disable,
435 const struct gma_clock_funcs psb_clock_funcs = {
436 .clock = psb_intel_clock,
437 .limit = psb_intel_limit,
438 .pll_is_valid = gma_pll_is_valid,
442 * Set the default value of cursor control and base register
443 * to zero. This is a workaround for h/w defect on Oaktrail
445 static void psb_intel_cursor_init(struct drm_device *dev,
446 struct gma_crtc *gma_crtc)
448 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
449 u32 control[3] = { CURACNTR, CURBCNTR, CURCCNTR };
450 u32 base[3] = { CURABASE, CURBBASE, CURCBASE };
451 struct psb_gem_object *cursor_pobj;
453 if (dev_priv->ops->cursor_needs_phys) {
454 /* Allocate 4 pages of stolen mem for a hardware cursor. That
455 * is enough for the 64 x 64 ARGB cursors we support.
457 cursor_pobj = psb_gem_create(dev, 4 * PAGE_SIZE, "cursor", true, PAGE_SIZE);
458 if (IS_ERR(cursor_pobj)) {
459 gma_crtc->cursor_pobj = NULL;
462 gma_crtc->cursor_pobj = cursor_pobj;
463 gma_crtc->cursor_addr = dev_priv->stolen_base + cursor_pobj->offset;
465 gma_crtc->cursor_pobj = NULL;
469 REG_WRITE(control[gma_crtc->pipe], 0);
470 REG_WRITE(base[gma_crtc->pipe], 0);
473 void psb_intel_crtc_init(struct drm_device *dev, int pipe,
474 struct psb_intel_mode_device *mode_dev)
476 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
477 struct gma_crtc *gma_crtc;
480 /* We allocate a extra array of drm_connector pointers
481 * for fbdev after the crtc */
482 gma_crtc = kzalloc(sizeof(struct gma_crtc) +
483 (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)),
485 if (gma_crtc == NULL)
488 gma_crtc->crtc_state =
489 kzalloc(sizeof(struct psb_intel_crtc_state), GFP_KERNEL);
490 if (!gma_crtc->crtc_state) {
491 dev_err(dev->dev, "Crtc state error: No memory\n");
496 drm_crtc_init(dev, &gma_crtc->base, &gma_crtc_funcs);
498 /* Set the CRTC clock functions from chip specific data */
499 gma_crtc->clock_funcs = dev_priv->ops->clock_funcs;
501 drm_mode_crtc_set_gamma_size(&gma_crtc->base, 256);
502 gma_crtc->pipe = pipe;
503 gma_crtc->plane = pipe;
505 for (i = 0; i < 256; i++)
506 gma_crtc->lut_adj[i] = 0;
508 gma_crtc->mode_dev = mode_dev;
509 gma_crtc->cursor_addr = 0;
511 drm_crtc_helper_add(&gma_crtc->base,
512 dev_priv->ops->crtc_helper);
514 /* Setup the array of drm_connector pointer array */
515 gma_crtc->mode_set.crtc = &gma_crtc->base;
516 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
517 dev_priv->plane_to_crtc_mapping[gma_crtc->plane] != NULL);
518 dev_priv->plane_to_crtc_mapping[gma_crtc->plane] = &gma_crtc->base;
519 dev_priv->pipe_to_crtc_mapping[gma_crtc->pipe] = &gma_crtc->base;
520 gma_crtc->mode_set.connectors = (struct drm_connector **)(gma_crtc + 1);
521 gma_crtc->mode_set.num_connectors = 0;
522 psb_intel_cursor_init(dev, gma_crtc);
524 /* Set to true so that the pipe is forced off on initial config. */
525 gma_crtc->active = true;
528 struct drm_crtc *psb_intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
530 struct drm_crtc *crtc;
532 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
533 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
535 if (gma_crtc->pipe == pipe)
541 int gma_connector_clones(struct drm_device *dev, int type_mask)
543 struct drm_connector_list_iter conn_iter;
544 struct drm_connector *connector;
548 drm_connector_list_iter_begin(dev, &conn_iter);
549 drm_for_each_connector_iter(connector, &conn_iter) {
550 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
551 if (type_mask & (1 << gma_encoder->type))
552 index_mask |= (1 << entry);
555 drm_connector_list_iter_end(&conn_iter);