a81cfe690177b8d8a667a31ccdf3395fff6dee00
[profile/ivi/libdrm.git] / linux-core / intel_display.c
1 /*
2  * Copyright © 2006-2007 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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.
22  *
23  * Authors:
24  *      Eric Anholt <eric@anholt.net>
25  */
26
27 #include <linux/i2c.h>
28 #include "drmP.h"
29 #include "intel_drv.h"
30 #include "i915_drm.h"
31 #include "i915_drv.h"
32
33 bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
34
35 typedef struct {
36     /* given values */    
37     int n;
38     int m1, m2;
39     int p1, p2;
40     /* derived values */
41     int dot;
42     int vco;
43     int m;
44     int p;
45 } intel_clock_t;
46
47 typedef struct {
48     int min, max;
49 } intel_range_t;
50
51 typedef struct {
52     int dot_limit;
53     int p2_slow, p2_fast;
54 } intel_p2_t;
55
56 #define INTEL_P2_NUM                  2
57
58 typedef struct {
59     intel_range_t   dot, vco, n, m, m1, m2, p, p1;
60     intel_p2_t      p2;
61 } intel_limit_t;
62
63 #define I8XX_DOT_MIN              25000
64 #define I8XX_DOT_MAX             350000
65 #define I8XX_VCO_MIN             930000
66 #define I8XX_VCO_MAX            1400000
67 #define I8XX_N_MIN                    3
68 #define I8XX_N_MAX                   16
69 #define I8XX_M_MIN                   96
70 #define I8XX_M_MAX                  140
71 #define I8XX_M1_MIN                  18
72 #define I8XX_M1_MAX                  26
73 #define I8XX_M2_MIN                   6
74 #define I8XX_M2_MAX                  16
75 #define I8XX_P_MIN                    4
76 #define I8XX_P_MAX                  128
77 #define I8XX_P1_MIN                   2
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
86
87 #define I9XX_DOT_MIN              20000
88 #define I9XX_DOT_MAX             400000
89 #define I9XX_VCO_MIN            1400000
90 #define I9XX_VCO_MAX            2800000
91 #define I9XX_N_MIN                    3
92 #define I9XX_N_MAX                    8
93 #define I9XX_M_MIN                   70
94 #define I9XX_M_MAX                  120
95 #define I9XX_M1_MIN                  10
96 #define I9XX_M1_MAX                  20
97 #define I9XX_M2_MIN                   5
98 #define I9XX_M2_MAX                   9
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
111
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
116
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 },
129     },
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 },
141     },
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 },
153     },
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.
165          */
166         .p2  = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
167                  .p2_slow = I9XX_P2_LVDS_SLOW,  .p2_fast = I9XX_P2_LVDS_FAST },
168     },
169 };
170
171 static const intel_limit_t *intel_limit(struct drm_crtc *crtc)
172 {
173         struct drm_device *dev = crtc->dev;
174         const intel_limit_t *limit;
175         
176         if (IS_I9XX(dev)) {
177                 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
178                         limit = &intel_limits[INTEL_LIMIT_I9XX_LVDS];
179                 else
180                         limit = &intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC];
181         } else {
182                 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
183                         limit = &intel_limits[INTEL_LIMIT_I8XX_LVDS];
184                 else
185                         limit = &intel_limits[INTEL_LIMIT_I8XX_DVO_DAC];
186         }
187         return limit;
188 }
189
190 /** Derive the pixel clock for the given refclk and divisors for 8xx chips. */
191
192 static void i8xx_clock(int refclk, intel_clock_t *clock)
193 {
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;
198 }
199
200 /** Derive the pixel clock for the given refclk and divisors for 9xx chips. */
201
202 static void i9xx_clock(int refclk, intel_clock_t *clock)
203 {
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;
208 }
209
210 static void intel_clock(struct drm_device *dev, int refclk,
211                         intel_clock_t *clock)
212 {
213         if (IS_I9XX(dev))
214                 return i9xx_clock (refclk, clock);
215         else
216                 return i8xx_clock (refclk, clock);
217 }
218
219 /**
220  * Returns whether any output on the specified pipe is of the specified type
221  */
222 bool intel_pipe_has_type (struct drm_crtc *crtc, int type)
223 {
224     struct drm_device *dev = crtc->dev;
225     struct drm_mode_config *mode_config = &dev->mode_config;
226     struct drm_output *l_entry;
227
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)
232                             return true;
233             }
234     }
235     return false;
236 }
237
238 #define INTELPllInvalid(s)   { /* ErrorF (s) */; return false; }
239 /**
240  * Returns whether the given set of divisors are valid for a given refclk with
241  * the given outputs.
242  */
243
244 static bool intel_PLL_is_valid(struct drm_crtc *crtc, intel_clock_t *clock)
245 {
246         const intel_limit_t *limit = intel_limit (crtc);
247         
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.
266          */
267         if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
268                 INTELPllInvalid ("dot out of range\n");
269         
270         return true;
271 }
272
273 /**
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.
277  */
278 static bool intel_find_best_PLL(struct drm_crtc *crtc, int target,
279                                 int refclk, intel_clock_t *best_clock)
280 {
281         struct drm_device *dev = crtc->dev;
282         struct drm_i915_private *dev_priv = dev->dev_private;
283         intel_clock_t clock;
284         const intel_limit_t *limit = intel_limit(crtc);
285         int err = target;
286
287         if (IS_I9XX(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
288             (I915_READ(LVDS) & LVDS_PORT_EN) != 0) {
289                 /*
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
293                  * even can.
294                  */
295                 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
296                     LVDS_CLKB_POWER_UP)
297                         clock.p2 = limit->p2.p2_fast;
298                 else
299                         clock.p2 = limit->p2.p2_slow;
300         } else {
301                 if (target < limit->p2.dot_limit)
302                         clock.p2 = limit->p2.p2_slow;
303                 else
304                         clock.p2 = limit->p2.p2_fast;
305         }
306         
307         memset (best_clock, 0, sizeof (*best_clock));
308         
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;
313                              clock.n++) {
314                                 for (clock.p1 = limit->p1.min;
315                                      clock.p1 <= limit->p1.max; clock.p1++) {
316                                         int this_err;
317                                         
318                                         intel_clock(dev, refclk, &clock);
319                                         
320                                         if (!intel_PLL_is_valid(crtc, &clock))
321                                                 continue;
322                                         
323                                         this_err = abs(clock.dot - target);
324                                         if (this_err < err) {
325                                                 *best_clock = clock;
326                                                 err = this_err;
327                                         }
328                                 }
329                         }
330                 }
331         }
332
333         return (err != target);
334 }
335
336 void
337 intel_set_vblank(struct drm_device *dev)
338 {
339         struct drm_i915_private *dev_priv = dev->dev_private;
340         struct drm_crtc *crtc;
341         struct intel_crtc *intel_crtc;
342         int vbl_pipe = 0;
343
344         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
345                 intel_crtc = crtc->driver_private;
346
347                 if (crtc->enabled)
348                         vbl_pipe |= (1<<intel_crtc->pipe);
349         }
350
351         dev_priv->vblank_pipe = vbl_pipe;
352         i915_enable_interrupt(dev);
353 }
354 void
355 intel_wait_for_vblank(struct drm_device *dev)
356 {
357         /* Wait for 20ms, i.e. one cycle at 50hz. */
358         udelay(20000);
359 }
360
361 void
362 intel_pipe_set_base(struct drm_crtc *crtc, int x, int y)
363 {
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);
371
372         Start = crtc->fb->offset;
373         Offset = y * crtc->fb->pitch + x;
374
375         DRM_DEBUG("Writing base %08lX %08lX %d %d\n", Start, Offset, x, y);
376         if (IS_I965G(dev)) {
377                 I915_WRITE(dspbase, Offset);
378                 I915_READ(dspbase);
379                 I915_WRITE(dspsurf, Start);
380                 I915_READ(dspsurf);
381         } else {
382                 I915_WRITE(dspbase, Start + Offset);
383                 I915_READ(dspbase);
384         }
385         
386
387         if (!dev_priv->sarea_priv) 
388                 return;
389                 
390         switch (pipe) {
391         case 0:
392                 dev_priv->sarea_priv->planeA_x = x;
393                 dev_priv->sarea_priv->planeA_y = y;
394                 break;
395         case 1:
396                 dev_priv->sarea_priv->planeB_x = x;
397                 dev_priv->sarea_priv->planeB_y = y;
398                 break;
399         default:
400                 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
401                 break;
402         }
403 }
404
405 /**
406  * Sets the power management mode of the pipe and plane.
407  *
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.
410  */
411 static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
412 {
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;
421         u32 temp;
422         bool enabled;
423
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.
426          */
427         switch (mode) {
428         case DPMSModeOn:
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);
435                         I915_READ(dpll_reg);
436                         /* Wait for the clocks to stabilize. */
437                         udelay(150);
438                         I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
439                         I915_READ(dpll_reg);
440                         /* Wait for the clocks to stabilize. */
441                         udelay(150);
442                         I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
443                         I915_READ(dpll_reg);
444                         /* Wait for the clocks to stabilize. */
445                         udelay(150);
446                 }
447                 
448                 /* Enable the pipe */
449                 temp = I915_READ(pipeconf_reg);
450                 if ((temp & PIPEACONF_ENABLE) == 0)
451                         I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
452                 
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));
459                 }
460                 
461                 intel_crtc_load_lut(crtc);
462                 
463                 /* Give the overlay scaler a chance to enable if it's on this pipe */
464                 //intel_crtc_dpms_video(crtc, TRUE); TODO
465         break;
466         case DPMSModeOff:
467                 /* Give the overlay scaler a chance to disable if it's on this pipe */
468                 //intel_crtc_dpms_video(crtc, FALSE); TODO
469                 
470                 /* Disable the VGA plane that we never use */
471                 I915_WRITE(VGACNTRL, VGA_DISP_DISABLE);
472                 
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);
480                 }
481                 
482                 if (!IS_I9XX(dev)) {
483                         /* Wait for vblank for the disable to take effect */
484                         intel_wait_for_vblank(dev);
485                 }
486                 
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);
492                 }
493                 
494                 /* Wait for vblank for the disable to take effect. */
495                 intel_wait_for_vblank(dev);
496                 
497                 temp = I915_READ(dpll_reg);
498                 if ((temp & DPLL_VCO_ENABLE) != 0) {
499                         I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
500                         I915_READ(dpll_reg);
501                 }
502                 
503                 /* Wait for the clocks to turn off. */
504                 udelay(150);
505                 break;
506         }
507         
508
509         if (!dev_priv->sarea_priv)
510                 return;
511
512         enabled = crtc->enabled && mode != DPMSModeOff;
513         
514         switch (pipe) {
515         case 0:
516                 dev_priv->sarea_priv->planeA_w = enabled ? crtc->mode.hdisplay : 0;
517                 dev_priv->sarea_priv->planeA_h = enabled ? crtc->mode.vdisplay : 0;
518                 break;
519         case 1:
520                 dev_priv->sarea_priv->planeB_w = enabled ? crtc->mode.hdisplay : 0;
521                 dev_priv->sarea_priv->planeB_h = enabled ? crtc->mode.vdisplay : 0;
522                 break;
523         default:
524                 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
525                 break;
526         }
527 }
528
529 static bool intel_crtc_lock(struct drm_crtc *crtc)
530 {
531    /* Sync the engine before mode switch */
532 //   i830WaitSync(crtc->scrn);
533
534 #if 0 // TODO def XF86DRI
535     return I830DRILock(crtc->scrn);
536 #else
537     return FALSE;
538 #endif
539 }
540
541 static void intel_crtc_unlock (struct drm_crtc *crtc)
542 {
543 #if 0 // TODO def XF86DRI
544     I830DRIUnlock (crtc->scrn);
545 #endif
546 }
547
548 static void intel_crtc_prepare (struct drm_crtc *crtc)
549 {
550         crtc->funcs->dpms(crtc, DPMSModeOff);
551 }
552
553 static void intel_crtc_commit (struct drm_crtc *crtc)
554 {
555         crtc->funcs->dpms(crtc, DPMSModeOn);
556 }
557
558 void intel_output_prepare (struct drm_output *output)
559 {
560         /* lvds has its own version of prepare see intel_lvds_prepare */
561         output->funcs->dpms(output, DPMSModeOff);
562 }
563
564 void intel_output_commit (struct drm_output *output)
565 {
566         /* lvds has its own version of commit see intel_lvds_commit */
567         output->funcs->dpms(output, DPMSModeOn);
568 }
569
570 static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
571                                   struct drm_display_mode *mode,
572                                   struct drm_display_mode *adjusted_mode)
573 {
574         return true;
575 }
576
577
578 /** Returns the core display clock speed for i830 - i945 */
579 static int intel_get_core_clock_speed(struct drm_device *dev)
580 {
581
582         /* Core clock values taken from the published datasheets.
583          * The 830 may go up to 166 Mhz, which we should check.
584          */
585         if (IS_I945G(dev))
586                 return 400000;
587         else if (IS_I915G(dev))
588                 return 333000;
589         else if (IS_I945GM(dev) || IS_845G(dev))
590                 return 200000;
591         else if (IS_I915GM(dev)) {
592                 u16 gcfgc = 0;
593
594                 pci_read_config_word(dev->pdev, I915_GCFGC, &gcfgc);
595                 
596                 if (gcfgc & I915_LOW_FREQUENCY_ENABLE)
597                         return 133000;
598                 else {
599                         switch (gcfgc & I915_DISPLAY_CLOCK_MASK) {
600                         case I915_DISPLAY_CLOCK_333_MHZ:
601                                 return 333000;
602                         default:
603                         case I915_DISPLAY_CLOCK_190_200_MHZ:
604                                 return 190000;
605                         }
606                 }
607         } else if (IS_I865G(dev))
608                 return 266000;
609         else if (IS_I855(dev)) {
610 #if 0
611                 PCITAG bridge = pciTag(0, 0, 0); /* This is always the host bridge */
612                 u16 hpllcc = pciReadWord(bridge, I855_HPLLCC);
613                 
614 #endif
615                 u16 hpllcc = 0;
616                 /* Assume that the hardware is in the high speed state.  This
617                  * should be the default.
618                  */
619                 switch (hpllcc & I855_CLOCK_CONTROL_MASK) {
620                 case I855_CLOCK_133_200:
621                 case I855_CLOCK_100_200:
622                         return 200000;
623                 case I855_CLOCK_166_250:
624                         return 250000;
625                 case I855_CLOCK_100_133:
626                         return 133000;
627                 }
628         } else /* 852, 830 */
629                 return 133000;
630         
631         return 0; /* Silence gcc warning */
632 }
633
634
635 /**
636  * Return the pipe currently connected to the panel fitter,
637  * or -1 if the panel fitter is not present or not in use
638  */
639 static int intel_panel_fitter_pipe (struct drm_device *dev)
640 {
641         struct drm_i915_private *dev_priv = dev->dev_private;
642         u32  pfit_control;
643     
644         /* i830 doesn't have a panel fitter */
645         if (IS_I830(dev))
646                 return -1;
647     
648         pfit_control = I915_READ(PFIT_CONTROL);
649     
650         /* See if the panel fitter is in use */
651         if ((pfit_control & PFIT_ENABLE) == 0)
652                 return -1;
653         
654         /* 965 can place panel fitter on either pipe */
655         if (IS_I965G(dev))
656                 return (pfit_control >> 29) & 0x3;
657         
658         /* older chips can only use pipe 1 */
659         return 1;
660 }
661
662 static void intel_crtc_mode_set(struct drm_crtc *crtc,
663                                 struct drm_display_mode *mode,
664                                 struct drm_display_mode *adjusted_mode,
665                                 int x, int y)
666 {
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;
686         int refclk;
687         intel_clock_t clock;
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;
693
694         list_for_each_entry(output, &mode_config->output_list, head) {
695                 struct intel_output *intel_output = output->driver_private;
696
697                 if (output->crtc != crtc)
698                         continue;
699
700                 switch (intel_output->type) {
701                 case INTEL_OUTPUT_LVDS:
702                         is_lvds = TRUE;
703                         break;
704                 case INTEL_OUTPUT_SDVO:
705                         is_sdvo = TRUE;
706                         break;
707                 case INTEL_OUTPUT_DVO:
708                         is_dvo = TRUE;
709                         break;
710                 case INTEL_OUTPUT_TVOUT:
711                         is_tv = TRUE;
712                         break;
713                 case INTEL_OUTPUT_ANALOG:
714                         is_crt = TRUE;
715                         break;
716                 }
717         }
718         
719         if (IS_I9XX(dev)) {
720                 refclk = 96000;
721         } else {
722                 refclk = 48000;
723         }
724
725         ok = intel_find_best_PLL(crtc, adjusted_mode->clock, refclk, &clock);
726         if (!ok) {
727                 DRM_ERROR("Couldn't find PLL settings for mode!\n");
728                 return;
729         }
730
731         fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
732         
733         dpll = DPLL_VGA_MODE_DIS;
734         if (IS_I9XX(dev)) {
735                 if (is_lvds)
736                         dpll |= DPLLB_MODE_LVDS;
737                 else
738                         dpll |= DPLLB_MODE_DAC_SERIAL;
739                 if (is_sdvo) {
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;
744                         }
745                 }
746                 
747                 /* compute bitmask from p1 value */
748                 dpll |= (1 << (clock.p1 - 1)) << 16;
749                 switch (clock.p2) {
750                 case 5:
751                         dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
752                         break;
753                 case 7:
754                         dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
755                         break;
756                 case 10:
757                         dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
758                         break;
759                 case 14:
760                         dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
761                         break;
762                 }
763                 if (IS_I965G(dev))
764                         dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
765         } else {
766                 if (is_lvds) {
767                         dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
768                 } else {
769                         if (clock.p1 == 2)
770                                 dpll |= PLL_P1_DIVIDE_BY_TWO;
771                         else
772                                 dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
773                         if (clock.p2 == 4)
774                                 dpll |= PLL_P2_DIVIDE_BY_4;
775                 }
776         }
777         
778         if (is_tv) {
779                 /* XXX: just matching BIOS for now */
780 /*      dpll |= PLL_REF_INPUT_TVCLKINBC; */
781                 dpll |= 3;
782         }
783 #if 0
784         else if (is_lvds)
785                 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
786 #endif
787         else
788                 dpll |= PLL_REF_INPUT_DREFCLK;
789         
790         /* setup pipeconf */
791         pipeconf = I915_READ(pipeconf_reg);
792
793         /* Set up the display plane register */
794         dspcntr = DISPPLANE_GAMMA_ENABLE;
795
796         switch (crtc->fb->bits_per_pixel) {
797         case 8:
798                 dspcntr |= DISPPLANE_8BPP;
799                 break;
800         case 16:
801                 if (crtc->fb->depth == 15)
802                         dspcntr |= DISPPLANE_15_16BPP;
803                 else
804                         dspcntr |= DISPPLANE_16BPP;
805                 break;
806         case 32:
807                 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
808                 break;
809         default:
810                 DRM_ERROR("Unknown color depth\n");
811                 return;
812         }
813         
814
815         if (pipe == 0)
816                 dspcntr |= DISPPLANE_SEL_PIPE_A;
817         else
818                 dspcntr |= DISPPLANE_SEL_PIPE_B;
819         
820         if (pipe == 0 && !IS_I965G(dev)) {
821                 /* Enable pixel doubling when the dot clock is > 90% of the (display)
822                  * core speed.
823                  *
824                  * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
825                  * pipe == 0 check?
826                  */
827                 if (mode->clock > intel_get_core_clock_speed(dev) * 9 / 10)
828                         pipeconf |= PIPEACONF_DOUBLE_WIDE;
829                 else
830                         pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
831         }
832
833         dspcntr |= DISPLAY_PLANE_ENABLE;
834         pipeconf |= PIPEACONF_ENABLE;
835         dpll |= DPLL_VCO_ENABLE;
836
837         
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);
841
842         DRM_DEBUG("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
843         drm_mode_debug_printmodeline(dev, mode);
844         
845 #if 0
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);
850         }
851         i830PrintPll("chosen", &clock);
852 #endif
853
854         if (dpll & DPLL_VCO_ENABLE) {
855                 I915_WRITE(fp_reg, fp);
856                 I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
857                 I915_READ(dpll_reg);
858                 udelay(150);
859         }
860         
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
863          * things on.
864          */
865         if (is_lvds) {
866                 u32 lvds = I915_READ(LVDS);
867                 
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.
871                  */
872                 if (clock.p2 == 7)
873                         lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
874                 else
875                         lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
876                 
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.
880                  */
881                 
882                 I915_WRITE(LVDS, lvds);
883                 I915_READ(LVDS);
884         }
885         
886         I915_WRITE(fp_reg, fp);
887         I915_WRITE(dpll_reg, dpll);
888         I915_READ(dpll_reg);
889         /* Wait for the clocks to stabilize. */
890         udelay(150);
891         
892         if (IS_I965G(dev)) {
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));
896         } else {
897                 /* write it again -- the BIOS does, after all */
898                 I915_WRITE(dpll_reg, dpll);
899         }
900         I915_READ(dpll_reg);
901         /* Wait for the clocks to stabilize. */
902         udelay(150);
903         
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.
919          */
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);
925         
926         intel_wait_for_vblank(dev);
927         
928         I915_WRITE(dspcntr_reg, dspcntr);
929         
930         /* Flush the plane changes */
931         intel_pipe_set_base(crtc, x, y);
932         
933         intel_set_vblank(dev);
934
935         intel_wait_for_vblank(dev);    
936 }
937
938 /** Loads the palette/gamma unit for the CRTC with the prepared values */
939 void intel_crtc_load_lut(struct drm_crtc *crtc)
940 {
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;
945         int i;
946
947         /* The clocks have to be on to load the palette. */
948         if (!crtc->enabled)
949                 return;
950
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]);
956         }
957 }
958
959 /** Sets the color ramps on behalf of RandR */
960 static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
961                                  u16 blue, int regno)
962 {
963         struct intel_crtc *intel_crtc = crtc->driver_private;
964         
965         intel_crtc->lut_r[regno] = red >> 8;
966         intel_crtc->lut_g[regno] = green >> 8;
967         intel_crtc->lut_b[regno] = blue >> 8;
968 }
969
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)
972 {
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);
977         u32 fp;
978         intel_clock_t clock;
979
980         if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
981                 fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
982         else
983                 fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
984
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;
988         if (IS_I9XX(dev)) {
989                 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
990                                DPLL_FPA01_P1_POST_DIV_SHIFT);
991
992                 switch (dpll & DPLL_MODE_MASK) {
993                 case DPLLB_MODE_DAC_SERIAL:
994                         clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
995                                 5 : 10;
996                         break;
997                 case DPLLB_MODE_LVDS:
998                         clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
999                                 7 : 14;
1000                         break;
1001                 default:
1002                         DRM_DEBUG("Unknown DPLL mode %08x in programmed "
1003                                   "mode\n", (int)(dpll & DPLL_MODE_MASK));
1004                         return 0;
1005                 }
1006
1007                 /* XXX: Handle the 100Mhz refclk */
1008                 i9xx_clock(96000, &clock);
1009         } else {
1010                 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
1011
1012                 if (is_lvds) {
1013                         clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
1014                                        DPLL_FPA01_P1_POST_DIV_SHIFT);
1015                         clock.p2 = 14;
1016
1017                         if ((dpll & PLL_REF_INPUT_MASK) ==
1018                             PLLB_REF_INPUT_SPREADSPECTRUMIN) {
1019                                 /* XXX: might not be 66MHz */
1020                                 i8xx_clock(66000, &clock);
1021                         } else
1022                                 i8xx_clock(48000, &clock);              
1023                 } else {
1024                         if (dpll & PLL_P1_DIVIDE_BY_TWO)
1025                                 clock.p1 = 2;
1026                         else {
1027                                 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
1028                                             DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
1029                         }
1030                         if (dpll & PLL_P2_DIVIDE_BY_4)
1031                                 clock.p2 = 4;
1032                         else
1033                                 clock.p2 = 2;
1034
1035                         i8xx_clock(48000, &clock);
1036                 }
1037         }
1038
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.
1042          */
1043
1044         return clock.dot;
1045 }
1046
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)
1050 {
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);
1059
1060         mode = kzalloc(sizeof(*mode), GFP_KERNEL);
1061         if (!mode)
1062                 return NULL;
1063
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;
1073
1074         drm_mode_set_name(mode);
1075         drm_mode_set_crtcinfo(mode, 0);
1076
1077         return mode;
1078 }
1079
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,
1089 };
1090
1091
1092 void intel_crtc_init(struct drm_device *dev, int pipe)
1093 {
1094         struct drm_crtc *crtc;
1095         struct intel_crtc *intel_crtc;
1096         int i;
1097
1098         crtc = drm_crtc_create(dev, &intel_crtc_funcs);
1099         if (crtc == NULL)
1100                 return;
1101
1102         intel_crtc = kzalloc(sizeof(struct intel_crtc), GFP_KERNEL);
1103         if (intel_crtc == NULL) {
1104                 kfree(crtc);
1105                 return;
1106         }
1107
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;
1113         }
1114
1115         crtc->driver_private = intel_crtc;
1116 }
1117
1118 struct drm_crtc *intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
1119 {
1120         struct drm_crtc *crtc = NULL;
1121
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)
1125                         break;
1126         }
1127         return crtc;
1128 }
1129
1130 int intel_output_clones(struct drm_device *dev, int type_mask)
1131 {
1132         int index_mask = 0;
1133         struct drm_output *output;
1134         int entry = 0;
1135
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);
1140                 entry++;
1141         }
1142         return index_mask;
1143 }
1144
1145
1146 static void intel_setup_outputs(struct drm_device *dev)
1147 {
1148         struct drm_output *output;
1149
1150         intel_crt_init(dev);
1151
1152         /* Set up integrated LVDS */
1153         if (IS_MOBILE(dev) && !IS_I830(dev))
1154                 intel_lvds_init(dev);
1155
1156         if (IS_I9XX(dev)) {
1157                 intel_sdvo_init(dev, SDVOB);
1158                 intel_sdvo_init(dev, SDVOC);
1159         }
1160
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;
1164                 
1165                 /* valid crtcs */
1166                 switch(intel_output->type) {
1167                 case INTEL_OUTPUT_DVO:
1168                 case INTEL_OUTPUT_SDVO:
1169                         crtc_mask = ((1 << 0)|
1170                                      (1 << 1));
1171                         clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
1172                                       (1 << INTEL_OUTPUT_DVO) |
1173                                       (1 << INTEL_OUTPUT_SDVO));
1174                         break;
1175                 case INTEL_OUTPUT_ANALOG:
1176                         crtc_mask = ((1 << 0)|
1177                                      (1 << 1));
1178                         clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
1179                                       (1 << INTEL_OUTPUT_DVO) |
1180                                       (1 << INTEL_OUTPUT_SDVO));
1181                         break;
1182                 case INTEL_OUTPUT_LVDS:
1183                         crtc_mask = (1 << 1);
1184                         clone_mask = (1 << INTEL_OUTPUT_LVDS);
1185                         break;
1186                 case INTEL_OUTPUT_TVOUT:
1187                         crtc_mask = ((1 << 0) |
1188                                      (1 << 1));
1189                         clone_mask = (1 << INTEL_OUTPUT_TVOUT);
1190                         break;
1191                 }
1192                 output->possible_crtcs = crtc_mask;
1193                 output->possible_clones = intel_output_clones(dev, clone_mask);
1194         }
1195 }
1196
1197 void intel_modeset_init(struct drm_device *dev)
1198 {
1199         int num_pipe;
1200         int i;
1201
1202         drm_mode_config_init(dev);
1203
1204         dev->mode_config.min_width = 0;
1205         dev->mode_config.min_height = 0;
1206
1207         dev->mode_config.max_width = 4096;
1208         dev->mode_config.max_height = 4096;
1209
1210         /* set memory base */
1211         if (IS_I9XX(dev))
1212                 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
1213         else
1214                 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
1215
1216         if (IS_MOBILE(dev) || IS_I9XX(dev))
1217                 num_pipe = 2;
1218         else
1219                 num_pipe = 1;
1220         DRM_DEBUG("%d display pipe%s available.\n",
1221                   num_pipe, num_pipe > 1 ? "s" : "");
1222
1223         for (i = 0; i < num_pipe; i++) {
1224                 intel_crtc_init(dev, i);
1225         }
1226
1227         intel_setup_outputs(dev);
1228
1229         //drm_initial_config(dev, false);
1230 }
1231
1232 void intel_modeset_cleanup(struct drm_device *dev)
1233 {
1234         drm_mode_config_cleanup(dev);
1235 }