2 * Copyright © 2006-2007 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Eric Anholt <eric@anholt.net>
27 #include <linux/i2c.h>
29 #include "intel_drv.h"
33 bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
56 #define INTEL_P2_NUM 2
59 intel_range_t dot, vco, n, m, m1, m2, p, p1;
63 #define I8XX_DOT_MIN 25000
64 #define I8XX_DOT_MAX 350000
65 #define I8XX_VCO_MIN 930000
66 #define I8XX_VCO_MAX 1400000
70 #define I8XX_M_MAX 140
71 #define I8XX_M1_MIN 18
72 #define I8XX_M1_MAX 26
74 #define I8XX_M2_MAX 16
76 #define I8XX_P_MAX 128
78 #define I8XX_P1_MAX 33
79 #define I8XX_P1_LVDS_MIN 1
80 #define I8XX_P1_LVDS_MAX 6
81 #define I8XX_P2_SLOW 4
82 #define I8XX_P2_FAST 2
83 #define I8XX_P2_LVDS_SLOW 14
84 #define I8XX_P2_LVDS_FAST 14 /* No fast option */
85 #define I8XX_P2_SLOW_LIMIT 165000
87 #define I9XX_DOT_MIN 20000
88 #define I9XX_DOT_MAX 400000
89 #define I9XX_VCO_MIN 1400000
90 #define I9XX_VCO_MAX 2800000
94 #define I9XX_M_MAX 120
95 #define I9XX_M1_MIN 10
96 #define I9XX_M1_MAX 20
99 #define I9XX_P_SDVO_DAC_MIN 5
100 #define I9XX_P_SDVO_DAC_MAX 80
101 #define I9XX_P_LVDS_MIN 7
102 #define I9XX_P_LVDS_MAX 98
103 #define I9XX_P1_MIN 1
104 #define I9XX_P1_MAX 8
105 #define I9XX_P2_SDVO_DAC_SLOW 10
106 #define I9XX_P2_SDVO_DAC_FAST 5
107 #define I9XX_P2_SDVO_DAC_SLOW_LIMIT 200000
108 #define I9XX_P2_LVDS_SLOW 14
109 #define I9XX_P2_LVDS_FAST 7
110 #define I9XX_P2_LVDS_SLOW_LIMIT 112000
112 #define INTEL_LIMIT_I8XX_DVO_DAC 0
113 #define INTEL_LIMIT_I8XX_LVDS 1
114 #define INTEL_LIMIT_I9XX_SDVO_DAC 2
115 #define INTEL_LIMIT_I9XX_LVDS 3
117 static const intel_limit_t intel_limits[] = {
118 { /* INTEL_LIMIT_I8XX_DVO_DAC */
119 .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
120 .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
121 .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
122 .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
123 .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
124 .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
125 .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
126 .p1 = { .min = I8XX_P1_MIN, .max = I8XX_P1_MAX },
127 .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
128 .p2_slow = I8XX_P2_SLOW, .p2_fast = I8XX_P2_FAST },
130 { /* INTEL_LIMIT_I8XX_LVDS */
131 .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
132 .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
133 .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
134 .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
135 .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
136 .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
137 .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
138 .p1 = { .min = I8XX_P1_LVDS_MIN, .max = I8XX_P1_LVDS_MAX },
139 .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
140 .p2_slow = I8XX_P2_LVDS_SLOW, .p2_fast = I8XX_P2_LVDS_FAST },
142 { /* INTEL_LIMIT_I9XX_SDVO_DAC */
143 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
144 .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
145 .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
146 .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
147 .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
148 .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
149 .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
150 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
151 .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
152 .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
154 { /* INTEL_LIMIT_I9XX_LVDS */
155 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
156 .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
157 .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
158 .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
159 .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
160 .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
161 .p = { .min = I9XX_P_LVDS_MIN, .max = I9XX_P_LVDS_MAX },
162 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
163 /* The single-channel range is 25-112Mhz, and dual-channel
164 * is 80-224Mhz. Prefer single channel as much as possible.
166 .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
167 .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST },
171 static const intel_limit_t *intel_limit(struct drm_crtc *crtc)
173 struct drm_device *dev = crtc->dev;
174 const intel_limit_t *limit;
177 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
178 limit = &intel_limits[INTEL_LIMIT_I9XX_LVDS];
180 limit = &intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC];
182 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
183 limit = &intel_limits[INTEL_LIMIT_I8XX_LVDS];
185 limit = &intel_limits[INTEL_LIMIT_I8XX_DVO_DAC];
190 /** Derive the pixel clock for the given refclk and divisors for 8xx chips. */
192 static void i8xx_clock(int refclk, intel_clock_t *clock)
194 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
195 clock->p = clock->p1 * clock->p2;
196 clock->vco = refclk * clock->m / (clock->n + 2);
197 clock->dot = clock->vco / clock->p;
200 /** Derive the pixel clock for the given refclk and divisors for 9xx chips. */
202 static void i9xx_clock(int refclk, intel_clock_t *clock)
204 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
205 clock->p = clock->p1 * clock->p2;
206 clock->vco = refclk * clock->m / (clock->n + 2);
207 clock->dot = clock->vco / clock->p;
210 static void intel_clock(struct drm_device *dev, int refclk,
211 intel_clock_t *clock)
214 return i9xx_clock (refclk, clock);
216 return i8xx_clock (refclk, clock);
220 * Returns whether any output on the specified pipe is of the specified type
222 bool intel_pipe_has_type (struct drm_crtc *crtc, int type)
224 struct drm_device *dev = crtc->dev;
225 struct drm_mode_config *mode_config = &dev->mode_config;
226 struct drm_output *l_entry;
228 list_for_each_entry(l_entry, &mode_config->output_list, head) {
229 if (l_entry->crtc == crtc) {
230 struct intel_output *intel_output = l_entry->driver_private;
231 if (intel_output->type == type)
238 #define INTELPllInvalid(s) { /* ErrorF (s) */; return false; }
240 * Returns whether the given set of divisors are valid for a given refclk with
244 static bool intel_PLL_is_valid(struct drm_crtc *crtc, intel_clock_t *clock)
246 const intel_limit_t *limit = intel_limit (crtc);
248 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
249 INTELPllInvalid ("p1 out of range\n");
250 if (clock->p < limit->p.min || limit->p.max < clock->p)
251 INTELPllInvalid ("p out of range\n");
252 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
253 INTELPllInvalid ("m2 out of range\n");
254 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
255 INTELPllInvalid ("m1 out of range\n");
256 if (clock->m1 <= clock->m2)
257 INTELPllInvalid ("m1 <= m2\n");
258 if (clock->m < limit->m.min || limit->m.max < clock->m)
259 INTELPllInvalid ("m out of range\n");
260 if (clock->n < limit->n.min || limit->n.max < clock->n)
261 INTELPllInvalid ("n out of range\n");
262 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
263 INTELPllInvalid ("vco out of range\n");
264 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
265 * output, etc., rather than just a single range.
267 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
268 INTELPllInvalid ("dot out of range\n");
274 * Returns a set of divisors for the desired target clock with the given
275 * refclk, or FALSE. The returned values represent the clock equation:
276 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
278 static bool intel_find_best_PLL(struct drm_crtc *crtc, int target,
279 int refclk, intel_clock_t *best_clock)
281 struct drm_device *dev = crtc->dev;
282 struct drm_i915_private *dev_priv = dev->dev_private;
284 const intel_limit_t *limit = intel_limit(crtc);
287 if (IS_I9XX(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
288 (I915_READ(LVDS) & LVDS_PORT_EN) != 0) {
290 * For LVDS, if the panel is on, just rely on its current
291 * settings for dual-channel. We haven't figured out how to
292 * reliably set up different single/dual channel state, if we
295 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
297 clock.p2 = limit->p2.p2_fast;
299 clock.p2 = limit->p2.p2_slow;
301 if (target < limit->p2.dot_limit)
302 clock.p2 = limit->p2.p2_slow;
304 clock.p2 = limit->p2.p2_fast;
307 memset (best_clock, 0, sizeof (*best_clock));
309 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
310 for (clock.m2 = limit->m2.min; clock.m2 < clock.m1 &&
311 clock.m2 <= limit->m2.max; clock.m2++) {
312 for (clock.n = limit->n.min; clock.n <= limit->n.max;
314 for (clock.p1 = limit->p1.min;
315 clock.p1 <= limit->p1.max; clock.p1++) {
318 intel_clock(dev, refclk, &clock);
320 if (!intel_PLL_is_valid(crtc, &clock))
323 this_err = abs(clock.dot - target);
324 if (this_err < err) {
333 return (err != target);
337 intel_set_vblank(struct drm_device *dev)
339 struct drm_i915_private *dev_priv = dev->dev_private;
340 struct drm_crtc *crtc;
341 struct intel_crtc *intel_crtc;
344 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
345 intel_crtc = crtc->driver_private;
348 vbl_pipe |= (1<<intel_crtc->pipe);
351 dev_priv->vblank_pipe = vbl_pipe;
352 i915_enable_interrupt(dev);
355 intel_wait_for_vblank(struct drm_device *dev)
357 /* Wait for 20ms, i.e. one cycle at 50hz. */
362 intel_pipe_set_base(struct drm_crtc *crtc, int x, int y)
364 struct drm_device *dev = crtc->dev;
365 struct drm_i915_private *dev_priv = dev->dev_private;
366 struct intel_crtc *intel_crtc = crtc->driver_private;
367 int pipe = intel_crtc->pipe;
368 unsigned long Start, Offset;
369 int dspbase = (pipe == 0 ? DSPABASE : DSPBBASE);
370 int dspsurf = (pipe == 0 ? DSPASURF : DSPBSURF);
372 Start = crtc->fb->offset;
373 Offset = y * crtc->fb->pitch + x;
375 DRM_DEBUG("Writing base %08lX %08lX %d %d\n", Start, Offset, x, y);
377 I915_WRITE(dspbase, Offset);
379 I915_WRITE(dspsurf, Start);
382 I915_WRITE(dspbase, Start + Offset);
387 if (!dev_priv->sarea_priv)
392 dev_priv->sarea_priv->planeA_x = x;
393 dev_priv->sarea_priv->planeA_y = y;
396 dev_priv->sarea_priv->planeB_x = x;
397 dev_priv->sarea_priv->planeB_y = y;
400 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
406 * Sets the power management mode of the pipe and plane.
408 * This code should probably grow support for turning the cursor off and back
409 * on appropriately at the same time as we're turning the pipe off/on.
411 static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
413 struct drm_device *dev = crtc->dev;
414 struct drm_i915_private *dev_priv = dev->dev_private;
415 struct intel_crtc *intel_crtc = crtc->driver_private;
416 int pipe = intel_crtc->pipe;
417 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
418 int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
419 int dspbase_reg = (pipe == 0) ? DSPABASE : DSPBBASE;
420 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
424 /* XXX: When our outputs are all unaware of DPMS modes other than off
425 * and on, we should map those modes to DPMSModeOff in the CRTC.
429 case DPMSModeStandby:
430 case DPMSModeSuspend:
431 /* Enable the DPLL */
432 temp = I915_READ(dpll_reg);
433 if ((temp & DPLL_VCO_ENABLE) == 0) {
434 I915_WRITE(dpll_reg, temp);
436 /* Wait for the clocks to stabilize. */
438 I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
440 /* Wait for the clocks to stabilize. */
442 I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
444 /* Wait for the clocks to stabilize. */
448 /* Enable the pipe */
449 temp = I915_READ(pipeconf_reg);
450 if ((temp & PIPEACONF_ENABLE) == 0)
451 I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
453 /* Enable the plane */
454 temp = I915_READ(dspcntr_reg);
455 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
456 I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
457 /* Flush the plane changes */
458 I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
461 intel_crtc_load_lut(crtc);
463 /* Give the overlay scaler a chance to enable if it's on this pipe */
464 //intel_crtc_dpms_video(crtc, TRUE); TODO
467 /* Give the overlay scaler a chance to disable if it's on this pipe */
468 //intel_crtc_dpms_video(crtc, FALSE); TODO
470 /* Disable the VGA plane that we never use */
471 I915_WRITE(VGACNTRL, VGA_DISP_DISABLE);
473 /* Disable display plane */
474 temp = I915_READ(dspcntr_reg);
475 if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
476 I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
477 /* Flush the plane changes */
478 I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
479 I915_READ(dspbase_reg);
483 /* Wait for vblank for the disable to take effect */
484 intel_wait_for_vblank(dev);
487 /* Next, disable display pipes */
488 temp = I915_READ(pipeconf_reg);
489 if ((temp & PIPEACONF_ENABLE) != 0) {
490 I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
491 I915_READ(pipeconf_reg);
494 /* Wait for vblank for the disable to take effect. */
495 intel_wait_for_vblank(dev);
497 temp = I915_READ(dpll_reg);
498 if ((temp & DPLL_VCO_ENABLE) != 0) {
499 I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
503 /* Wait for the clocks to turn off. */
509 if (!dev_priv->sarea_priv)
512 enabled = crtc->enabled && mode != DPMSModeOff;
516 dev_priv->sarea_priv->planeA_w = enabled ? crtc->mode.hdisplay : 0;
517 dev_priv->sarea_priv->planeA_h = enabled ? crtc->mode.vdisplay : 0;
520 dev_priv->sarea_priv->planeB_w = enabled ? crtc->mode.hdisplay : 0;
521 dev_priv->sarea_priv->planeB_h = enabled ? crtc->mode.vdisplay : 0;
524 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
529 static bool intel_crtc_lock(struct drm_crtc *crtc)
531 /* Sync the engine before mode switch */
532 // i830WaitSync(crtc->scrn);
534 #if 0 // TODO def XF86DRI
535 return I830DRILock(crtc->scrn);
541 static void intel_crtc_unlock (struct drm_crtc *crtc)
543 #if 0 // TODO def XF86DRI
544 I830DRIUnlock (crtc->scrn);
548 static void intel_crtc_prepare (struct drm_crtc *crtc)
550 crtc->funcs->dpms(crtc, DPMSModeOff);
553 static void intel_crtc_commit (struct drm_crtc *crtc)
555 crtc->funcs->dpms(crtc, DPMSModeOn);
558 void intel_output_prepare (struct drm_output *output)
560 /* lvds has its own version of prepare see intel_lvds_prepare */
561 output->funcs->dpms(output, DPMSModeOff);
564 void intel_output_commit (struct drm_output *output)
566 /* lvds has its own version of commit see intel_lvds_commit */
567 output->funcs->dpms(output, DPMSModeOn);
570 static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
571 struct drm_display_mode *mode,
572 struct drm_display_mode *adjusted_mode)
578 /** Returns the core display clock speed for i830 - i945 */
579 static int intel_get_core_clock_speed(struct drm_device *dev)
582 /* Core clock values taken from the published datasheets.
583 * The 830 may go up to 166 Mhz, which we should check.
587 else if (IS_I915G(dev))
589 else if (IS_I945GM(dev) || IS_845G(dev))
591 else if (IS_I915GM(dev)) {
594 pci_read_config_word(dev->pdev, I915_GCFGC, &gcfgc);
596 if (gcfgc & I915_LOW_FREQUENCY_ENABLE)
599 switch (gcfgc & I915_DISPLAY_CLOCK_MASK) {
600 case I915_DISPLAY_CLOCK_333_MHZ:
603 case I915_DISPLAY_CLOCK_190_200_MHZ:
607 } else if (IS_I865G(dev))
609 else if (IS_I855(dev)) {
611 PCITAG bridge = pciTag(0, 0, 0); /* This is always the host bridge */
612 u16 hpllcc = pciReadWord(bridge, I855_HPLLCC);
616 /* Assume that the hardware is in the high speed state. This
617 * should be the default.
619 switch (hpllcc & I855_CLOCK_CONTROL_MASK) {
620 case I855_CLOCK_133_200:
621 case I855_CLOCK_100_200:
623 case I855_CLOCK_166_250:
625 case I855_CLOCK_100_133:
628 } else /* 852, 830 */
631 return 0; /* Silence gcc warning */
636 * Return the pipe currently connected to the panel fitter,
637 * or -1 if the panel fitter is not present or not in use
639 static int intel_panel_fitter_pipe (struct drm_device *dev)
641 struct drm_i915_private *dev_priv = dev->dev_private;
644 /* i830 doesn't have a panel fitter */
648 pfit_control = I915_READ(PFIT_CONTROL);
650 /* See if the panel fitter is in use */
651 if ((pfit_control & PFIT_ENABLE) == 0)
654 /* 965 can place panel fitter on either pipe */
656 return (pfit_control >> 29) & 0x3;
658 /* older chips can only use pipe 1 */
662 static void intel_crtc_mode_set(struct drm_crtc *crtc,
663 struct drm_display_mode *mode,
664 struct drm_display_mode *adjusted_mode,
667 struct drm_device *dev = crtc->dev;
668 struct drm_i915_private *dev_priv = dev->dev_private;
669 struct intel_crtc *intel_crtc = crtc->driver_private;
670 int pipe = intel_crtc->pipe;
671 int fp_reg = (pipe == 0) ? FPA0 : FPB0;
672 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
673 int dpll_md_reg = (intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
674 int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
675 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
676 int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
677 int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
678 int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
679 int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
680 int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
681 int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
682 int dspsize_reg = (pipe == 0) ? DSPASIZE : DSPBSIZE;
683 int dspstride_reg = (pipe == 0) ? DSPASTRIDE : DSPBSTRIDE;
684 int dsppos_reg = (pipe == 0) ? DSPAPOS : DSPBPOS;
685 int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
688 u32 dpll = 0, fp = 0, dspcntr, pipeconf;
689 bool ok, is_sdvo = false, is_dvo = false;
690 bool is_crt = false, is_lvds = false, is_tv = false;
691 struct drm_mode_config *mode_config = &dev->mode_config;
692 struct drm_output *output;
694 list_for_each_entry(output, &mode_config->output_list, head) {
695 struct intel_output *intel_output = output->driver_private;
697 if (output->crtc != crtc)
700 switch (intel_output->type) {
701 case INTEL_OUTPUT_LVDS:
704 case INTEL_OUTPUT_SDVO:
707 case INTEL_OUTPUT_DVO:
710 case INTEL_OUTPUT_TVOUT:
713 case INTEL_OUTPUT_ANALOG:
725 ok = intel_find_best_PLL(crtc, adjusted_mode->clock, refclk, &clock);
727 DRM_ERROR("Couldn't find PLL settings for mode!\n");
731 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
733 dpll = DPLL_VGA_MODE_DIS;
736 dpll |= DPLLB_MODE_LVDS;
738 dpll |= DPLLB_MODE_DAC_SERIAL;
740 dpll |= DPLL_DVO_HIGH_SPEED;
741 if (IS_I945G(dev) || IS_I945GM(dev)) {
742 int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
743 dpll |= (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
747 /* compute bitmask from p1 value */
748 dpll |= (1 << (clock.p1 - 1)) << 16;
751 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
754 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
757 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
760 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
764 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
767 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
770 dpll |= PLL_P1_DIVIDE_BY_TWO;
772 dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
774 dpll |= PLL_P2_DIVIDE_BY_4;
779 /* XXX: just matching BIOS for now */
780 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
785 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
788 dpll |= PLL_REF_INPUT_DREFCLK;
791 pipeconf = I915_READ(pipeconf_reg);
793 /* Set up the display plane register */
794 dspcntr = DISPPLANE_GAMMA_ENABLE;
796 switch (crtc->fb->bits_per_pixel) {
798 dspcntr |= DISPPLANE_8BPP;
801 if (crtc->fb->depth == 15)
802 dspcntr |= DISPPLANE_15_16BPP;
804 dspcntr |= DISPPLANE_16BPP;
807 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
810 DRM_ERROR("Unknown color depth\n");
816 dspcntr |= DISPPLANE_SEL_PIPE_A;
818 dspcntr |= DISPPLANE_SEL_PIPE_B;
820 if (pipe == 0 && !IS_I965G(dev)) {
821 /* Enable pixel doubling when the dot clock is > 90% of the (display)
824 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
827 if (mode->clock > intel_get_core_clock_speed(dev) * 9 / 10)
828 pipeconf |= PIPEACONF_DOUBLE_WIDE;
830 pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
833 dspcntr |= DISPLAY_PLANE_ENABLE;
834 pipeconf |= PIPEACONF_ENABLE;
835 dpll |= DPLL_VCO_ENABLE;
838 /* Disable the panel fitter if it was on our pipe */
839 if (intel_panel_fitter_pipe(dev) == pipe)
840 I915_WRITE(PFIT_CONTROL, 0);
842 DRM_DEBUG("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
843 drm_mode_debug_printmodeline(dev, mode);
846 if (!xf86ModesEqual(mode, adjusted_mode)) {
847 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
848 "Adjusted mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
849 xf86PrintModeline(pScrn->scrnIndex, mode);
851 i830PrintPll("chosen", &clock);
854 if (dpll & DPLL_VCO_ENABLE) {
855 I915_WRITE(fp_reg, fp);
856 I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
861 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
862 * This is an exception to the general rule that mode_set doesn't turn
866 u32 lvds = I915_READ(LVDS);
868 lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP | LVDS_PIPEB_SELECT;
869 /* Set the B0-B3 data pairs corresponding to whether we're going to
870 * set the DPLLs for dual-channel mode or not.
873 lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
875 lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
877 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
878 * appropriately here, but we need to look more thoroughly into how
879 * panels behave in the two modes.
882 I915_WRITE(LVDS, lvds);
886 I915_WRITE(fp_reg, fp);
887 I915_WRITE(dpll_reg, dpll);
889 /* Wait for the clocks to stabilize. */
893 int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
894 I915_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) |
895 ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
897 /* write it again -- the BIOS does, after all */
898 I915_WRITE(dpll_reg, dpll);
901 /* Wait for the clocks to stabilize. */
904 I915_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
905 ((adjusted_mode->crtc_htotal - 1) << 16));
906 I915_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
907 ((adjusted_mode->crtc_hblank_end - 1) << 16));
908 I915_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
909 ((adjusted_mode->crtc_hsync_end - 1) << 16));
910 I915_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
911 ((adjusted_mode->crtc_vtotal - 1) << 16));
912 I915_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
913 ((adjusted_mode->crtc_vblank_end - 1) << 16));
914 I915_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
915 ((adjusted_mode->crtc_vsync_end - 1) << 16));
916 I915_WRITE(dspstride_reg, crtc->fb->pitch);
917 /* pipesrc and dspsize control the size that is scaled from, which should
918 * always be the user's requested size.
920 I915_WRITE(dspsize_reg, ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
921 I915_WRITE(dsppos_reg, 0);
922 I915_WRITE(pipesrc_reg, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
923 I915_WRITE(pipeconf_reg, pipeconf);
924 I915_READ(pipeconf_reg);
926 intel_wait_for_vblank(dev);
928 I915_WRITE(dspcntr_reg, dspcntr);
930 /* Flush the plane changes */
931 intel_pipe_set_base(crtc, x, y);
933 intel_set_vblank(dev);
935 intel_wait_for_vblank(dev);
938 /** Loads the palette/gamma unit for the CRTC with the prepared values */
939 void intel_crtc_load_lut(struct drm_crtc *crtc)
941 struct drm_device *dev = crtc->dev;
942 struct drm_i915_private *dev_priv = dev->dev_private;
943 struct intel_crtc *intel_crtc = crtc->driver_private;
944 int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
947 /* The clocks have to be on to load the palette. */
951 for (i = 0; i < 256; i++) {
952 I915_WRITE(palreg + 4 * i,
953 (intel_crtc->lut_r[i] << 16) |
954 (intel_crtc->lut_g[i] << 8) |
955 intel_crtc->lut_b[i]);
959 /** Sets the color ramps on behalf of RandR */
960 static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
963 struct intel_crtc *intel_crtc = crtc->driver_private;
965 intel_crtc->lut_r[regno] = red >> 8;
966 intel_crtc->lut_g[regno] = green >> 8;
967 intel_crtc->lut_b[regno] = blue >> 8;
970 /* Returns the clock of the currently programmed mode of the given pipe. */
971 static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
973 struct drm_i915_private *dev_priv = dev->dev_private;
974 struct intel_crtc *intel_crtc = crtc->driver_private;
975 int pipe = intel_crtc->pipe;
976 u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
980 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
981 fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
983 fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
985 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
986 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
987 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
989 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
990 DPLL_FPA01_P1_POST_DIV_SHIFT);
992 switch (dpll & DPLL_MODE_MASK) {
993 case DPLLB_MODE_DAC_SERIAL:
994 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
997 case DPLLB_MODE_LVDS:
998 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
1002 DRM_DEBUG("Unknown DPLL mode %08x in programmed "
1003 "mode\n", (int)(dpll & DPLL_MODE_MASK));
1007 /* XXX: Handle the 100Mhz refclk */
1008 i9xx_clock(96000, &clock);
1010 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
1013 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
1014 DPLL_FPA01_P1_POST_DIV_SHIFT);
1017 if ((dpll & PLL_REF_INPUT_MASK) ==
1018 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
1019 /* XXX: might not be 66MHz */
1020 i8xx_clock(66000, &clock);
1022 i8xx_clock(48000, &clock);
1024 if (dpll & PLL_P1_DIVIDE_BY_TWO)
1027 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
1028 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
1030 if (dpll & PLL_P2_DIVIDE_BY_4)
1035 i8xx_clock(48000, &clock);
1039 /* XXX: It would be nice to validate the clocks, but we can't reuse
1040 * i830PllIsValid() because it relies on the xf86_config output
1041 * configuration being accurate, which it isn't necessarily.
1047 /** Returns the currently programmed mode of the given pipe. */
1048 struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
1049 struct drm_crtc *crtc)
1051 struct drm_i915_private *dev_priv = dev->dev_private;
1052 struct intel_crtc *intel_crtc = crtc->driver_private;
1053 int pipe = intel_crtc->pipe;
1054 struct drm_display_mode *mode;
1055 int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
1056 int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
1057 int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
1058 int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
1060 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
1064 mode->clock = intel_crtc_clock_get(dev, crtc);
1065 mode->hdisplay = (htot & 0xffff) + 1;
1066 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
1067 mode->hsync_start = (hsync & 0xffff) + 1;
1068 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
1069 mode->vdisplay = (vtot & 0xffff) + 1;
1070 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
1071 mode->vsync_start = (vsync & 0xffff) + 1;
1072 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
1074 drm_mode_set_name(mode);
1075 drm_mode_set_crtcinfo(mode, 0);
1080 static const struct drm_crtc_funcs intel_crtc_funcs = {
1081 .dpms = intel_crtc_dpms,
1082 .lock = intel_crtc_lock,
1083 .unlock = intel_crtc_unlock,
1084 .mode_fixup = intel_crtc_mode_fixup,
1085 .mode_set = intel_crtc_mode_set,
1086 .gamma_set = intel_crtc_gamma_set,
1087 .prepare = intel_crtc_prepare,
1088 .commit = intel_crtc_commit,
1092 void intel_crtc_init(struct drm_device *dev, int pipe)
1094 struct drm_crtc *crtc;
1095 struct intel_crtc *intel_crtc;
1098 crtc = drm_crtc_create(dev, &intel_crtc_funcs);
1102 intel_crtc = kzalloc(sizeof(struct intel_crtc), GFP_KERNEL);
1103 if (intel_crtc == NULL) {
1108 intel_crtc->pipe = pipe;
1109 for (i = 0; i < 256; i++) {
1110 intel_crtc->lut_r[i] = i;
1111 intel_crtc->lut_g[i] = i;
1112 intel_crtc->lut_b[i] = i;
1115 crtc->driver_private = intel_crtc;
1118 struct drm_crtc *intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
1120 struct drm_crtc *crtc = NULL;
1122 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1123 struct intel_crtc *intel_crtc = crtc->driver_private;
1124 if (intel_crtc->pipe == pipe)
1130 int intel_output_clones(struct drm_device *dev, int type_mask)
1133 struct drm_output *output;
1136 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1137 struct intel_output *intel_output = output->driver_private;
1138 if (type_mask & (1 << intel_output->type))
1139 index_mask |= (1 << entry);
1146 static void intel_setup_outputs(struct drm_device *dev)
1148 struct drm_output *output;
1150 intel_crt_init(dev);
1152 /* Set up integrated LVDS */
1153 if (IS_MOBILE(dev) && !IS_I830(dev))
1154 intel_lvds_init(dev);
1157 intel_sdvo_init(dev, SDVOB);
1158 intel_sdvo_init(dev, SDVOC);
1161 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1162 struct intel_output *intel_output = output->driver_private;
1163 int crtc_mask = 0, clone_mask = 0;
1166 switch(intel_output->type) {
1167 case INTEL_OUTPUT_DVO:
1168 case INTEL_OUTPUT_SDVO:
1169 crtc_mask = ((1 << 0)|
1171 clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
1172 (1 << INTEL_OUTPUT_DVO) |
1173 (1 << INTEL_OUTPUT_SDVO));
1175 case INTEL_OUTPUT_ANALOG:
1176 crtc_mask = ((1 << 0)|
1178 clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
1179 (1 << INTEL_OUTPUT_DVO) |
1180 (1 << INTEL_OUTPUT_SDVO));
1182 case INTEL_OUTPUT_LVDS:
1183 crtc_mask = (1 << 1);
1184 clone_mask = (1 << INTEL_OUTPUT_LVDS);
1186 case INTEL_OUTPUT_TVOUT:
1187 crtc_mask = ((1 << 0) |
1189 clone_mask = (1 << INTEL_OUTPUT_TVOUT);
1192 output->possible_crtcs = crtc_mask;
1193 output->possible_clones = intel_output_clones(dev, clone_mask);
1197 void intel_modeset_init(struct drm_device *dev)
1202 drm_mode_config_init(dev);
1204 dev->mode_config.min_width = 0;
1205 dev->mode_config.min_height = 0;
1207 dev->mode_config.max_width = 4096;
1208 dev->mode_config.max_height = 4096;
1210 /* set memory base */
1212 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
1214 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
1216 if (IS_MOBILE(dev) || IS_I9XX(dev))
1220 DRM_DEBUG("%d display pipe%s available.\n",
1221 num_pipe, num_pipe > 1 ? "s" : "");
1223 for (i = 0; i < num_pipe; i++) {
1224 intel_crtc_init(dev, i);
1227 intel_setup_outputs(dev);
1229 //drm_initial_config(dev, false);
1232 void intel_modeset_cleanup(struct drm_device *dev)
1234 drm_mode_config_cleanup(dev);