Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4
[firefly-linux-kernel-4.4.55.git] / drivers / staging / gma500 / psb_intel_display.c
1 /*
2  * Copyright Â© 2006-2007 Intel Corporation
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16  *
17  * Authors:
18  *      Eric Anholt <eric@anholt.net>
19  */
20
21 #include <linux/i2c.h>
22 #include <linux/pm_runtime.h>
23
24 #include <drm/drmP.h>
25 #include "psb_fb.h"
26 #include "psb_drv.h"
27 #include "psb_intel_drv.h"
28 #include "psb_intel_reg.h"
29 #include "psb_intel_display.h"
30 #include "psb_powermgmt.h"
31
32
33 struct psb_intel_clock_t {
34         /* given values */
35         int n;
36         int m1, m2;
37         int p1, p2;
38         /* derived values */
39         int dot;
40         int vco;
41         int m;
42         int p;
43 };
44
45 struct psb_intel_range_t {
46         int min, max;
47 };
48
49 struct psb_intel_p2_t {
50         int dot_limit;
51         int p2_slow, p2_fast;
52 };
53
54 #define INTEL_P2_NUM                  2
55
56 struct psb_intel_limit_t {
57         struct psb_intel_range_t dot, vco, n, m, m1, m2, p, p1;
58         struct psb_intel_p2_t p2;
59 };
60
61 #define I8XX_DOT_MIN              25000
62 #define I8XX_DOT_MAX             350000
63 #define I8XX_VCO_MIN             930000
64 #define I8XX_VCO_MAX            1400000
65 #define I8XX_N_MIN                    3
66 #define I8XX_N_MAX                   16
67 #define I8XX_M_MIN                   96
68 #define I8XX_M_MAX                  140
69 #define I8XX_M1_MIN                  18
70 #define I8XX_M1_MAX                  26
71 #define I8XX_M2_MIN                   6
72 #define I8XX_M2_MAX                  16
73 #define I8XX_P_MIN                    4
74 #define I8XX_P_MAX                  128
75 #define I8XX_P1_MIN                   2
76 #define I8XX_P1_MAX                  33
77 #define I8XX_P1_LVDS_MIN              1
78 #define I8XX_P1_LVDS_MAX              6
79 #define I8XX_P2_SLOW                  4
80 #define I8XX_P2_FAST                  2
81 #define I8XX_P2_LVDS_SLOW             14
82 #define I8XX_P2_LVDS_FAST             14        /* No fast option */
83 #define I8XX_P2_SLOW_LIMIT       165000
84
85 #define I9XX_DOT_MIN              20000
86 #define I9XX_DOT_MAX             400000
87 #define I9XX_VCO_MIN            1400000
88 #define I9XX_VCO_MAX            2800000
89 #define I9XX_N_MIN                    3
90 #define I9XX_N_MAX                    8
91 #define I9XX_M_MIN                   70
92 #define I9XX_M_MAX                  120
93 #define I9XX_M1_MIN                  10
94 #define I9XX_M1_MAX                  20
95 #define I9XX_M2_MIN                   5
96 #define I9XX_M2_MAX                   9
97 #define I9XX_P_SDVO_DAC_MIN           5
98 #define I9XX_P_SDVO_DAC_MAX          80
99 #define I9XX_P_LVDS_MIN               7
100 #define I9XX_P_LVDS_MAX              98
101 #define I9XX_P1_MIN                   1
102 #define I9XX_P1_MAX                   8
103 #define I9XX_P2_SDVO_DAC_SLOW                10
104 #define I9XX_P2_SDVO_DAC_FAST                 5
105 #define I9XX_P2_SDVO_DAC_SLOW_LIMIT      200000
106 #define I9XX_P2_LVDS_SLOW                    14
107 #define I9XX_P2_LVDS_FAST                     7
108 #define I9XX_P2_LVDS_SLOW_LIMIT          112000
109
110 #define INTEL_LIMIT_I8XX_DVO_DAC    0
111 #define INTEL_LIMIT_I8XX_LVDS       1
112 #define INTEL_LIMIT_I9XX_SDVO_DAC   2
113 #define INTEL_LIMIT_I9XX_LVDS       3
114
115 static const struct psb_intel_limit_t psb_intel_limits[] = {
116         {                       /* INTEL_LIMIT_I8XX_DVO_DAC */
117          .dot = {.min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX},
118          .vco = {.min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX},
119          .n = {.min = I8XX_N_MIN, .max = I8XX_N_MAX},
120          .m = {.min = I8XX_M_MIN, .max = I8XX_M_MAX},
121          .m1 = {.min = I8XX_M1_MIN, .max = I8XX_M1_MAX},
122          .m2 = {.min = I8XX_M2_MIN, .max = I8XX_M2_MAX},
123          .p = {.min = I8XX_P_MIN, .max = I8XX_P_MAX},
124          .p1 = {.min = I8XX_P1_MIN, .max = I8XX_P1_MAX},
125          .p2 = {.dot_limit = I8XX_P2_SLOW_LIMIT,
126                 .p2_slow = I8XX_P2_SLOW, .p2_fast = I8XX_P2_FAST},
127          },
128         {                       /* INTEL_LIMIT_I8XX_LVDS */
129          .dot = {.min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX},
130          .vco = {.min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX},
131          .n = {.min = I8XX_N_MIN, .max = I8XX_N_MAX},
132          .m = {.min = I8XX_M_MIN, .max = I8XX_M_MAX},
133          .m1 = {.min = I8XX_M1_MIN, .max = I8XX_M1_MAX},
134          .m2 = {.min = I8XX_M2_MIN, .max = I8XX_M2_MAX},
135          .p = {.min = I8XX_P_MIN, .max = I8XX_P_MAX},
136          .p1 = {.min = I8XX_P1_LVDS_MIN, .max = I8XX_P1_LVDS_MAX},
137          .p2 = {.dot_limit = I8XX_P2_SLOW_LIMIT,
138                 .p2_slow = I8XX_P2_LVDS_SLOW, .p2_fast = I8XX_P2_LVDS_FAST},
139          },
140         {                       /* INTEL_LIMIT_I9XX_SDVO_DAC */
141          .dot = {.min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX},
142          .vco = {.min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX},
143          .n = {.min = I9XX_N_MIN, .max = I9XX_N_MAX},
144          .m = {.min = I9XX_M_MIN, .max = I9XX_M_MAX},
145          .m1 = {.min = I9XX_M1_MIN, .max = I9XX_M1_MAX},
146          .m2 = {.min = I9XX_M2_MIN, .max = I9XX_M2_MAX},
147          .p = {.min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX},
148          .p1 = {.min = I9XX_P1_MIN, .max = I9XX_P1_MAX},
149          .p2 = {.dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
150                 .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast =
151                 I9XX_P2_SDVO_DAC_FAST},
152          },
153         {                       /* INTEL_LIMIT_I9XX_LVDS */
154          .dot = {.min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX},
155          .vco = {.min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX},
156          .n = {.min = I9XX_N_MIN, .max = I9XX_N_MAX},
157          .m = {.min = I9XX_M_MIN, .max = I9XX_M_MAX},
158          .m1 = {.min = I9XX_M1_MIN, .max = I9XX_M1_MAX},
159          .m2 = {.min = I9XX_M2_MIN, .max = I9XX_M2_MAX},
160          .p = {.min = I9XX_P_LVDS_MIN, .max = I9XX_P_LVDS_MAX},
161          .p1 = {.min = I9XX_P1_MIN, .max = I9XX_P1_MAX},
162          /* The single-channel range is 25-112Mhz, and dual-channel
163           * is 80-224Mhz.  Prefer single channel as much as possible.
164           */
165          .p2 = {.dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
166                 .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST},
167          },
168 };
169
170 static const struct psb_intel_limit_t *psb_intel_limit(struct drm_crtc *crtc)
171 {
172         const struct psb_intel_limit_t *limit;
173
174         if (psb_intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
175                 limit = &psb_intel_limits[INTEL_LIMIT_I9XX_LVDS];
176         else
177                 limit = &psb_intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC];
178         return limit;
179 }
180
181 /** Derive the pixel clock for the given refclk and divisors for 8xx chips. */
182
183 static void i8xx_clock(int refclk, struct psb_intel_clock_t *clock)
184 {
185         clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
186         clock->p = clock->p1 * clock->p2;
187         clock->vco = refclk * clock->m / (clock->n + 2);
188         clock->dot = clock->vco / clock->p;
189 }
190
191 /** Derive the pixel clock for the given refclk and divisors for 9xx chips. */
192
193 static void i9xx_clock(int refclk, struct psb_intel_clock_t *clock)
194 {
195         clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
196         clock->p = clock->p1 * clock->p2;
197         clock->vco = refclk * clock->m / (clock->n + 2);
198         clock->dot = clock->vco / clock->p;
199 }
200
201 static void psb_intel_clock(struct drm_device *dev, int refclk,
202                         struct psb_intel_clock_t *clock)
203 {
204         return i9xx_clock(refclk, clock);
205 }
206
207 /**
208  * Returns whether any output on the specified pipe is of the specified type
209  */
210 bool psb_intel_pipe_has_type(struct drm_crtc *crtc, int type)
211 {
212         struct drm_device *dev = crtc->dev;
213         struct drm_mode_config *mode_config = &dev->mode_config;
214         struct drm_connector *l_entry;
215
216         list_for_each_entry(l_entry, &mode_config->connector_list, head) {
217                 if (l_entry->encoder && l_entry->encoder->crtc == crtc) {
218                         struct psb_intel_output *psb_intel_output =
219                             to_psb_intel_output(l_entry);
220                         if (psb_intel_output->type == type)
221                                 return true;
222                 }
223         }
224         return false;
225 }
226
227 #define INTELPllInvalid(s)   { /* ErrorF (s) */; return false; }
228 /**
229  * Returns whether the given set of divisors are valid for a given refclk with
230  * the given connectors.
231  */
232
233 static bool psb_intel_PLL_is_valid(struct drm_crtc *crtc,
234                                struct psb_intel_clock_t *clock)
235 {
236         const struct psb_intel_limit_t *limit = psb_intel_limit(crtc);
237
238         if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
239                 INTELPllInvalid("p1 out of range\n");
240         if (clock->p < limit->p.min || limit->p.max < clock->p)
241                 INTELPllInvalid("p out of range\n");
242         if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
243                 INTELPllInvalid("m2 out of range\n");
244         if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
245                 INTELPllInvalid("m1 out of range\n");
246         if (clock->m1 <= clock->m2)
247                 INTELPllInvalid("m1 <= m2\n");
248         if (clock->m < limit->m.min || limit->m.max < clock->m)
249                 INTELPllInvalid("m out of range\n");
250         if (clock->n < limit->n.min || limit->n.max < clock->n)
251                 INTELPllInvalid("n out of range\n");
252         if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
253                 INTELPllInvalid("vco out of range\n");
254         /* XXX: We may need to be checking "Dot clock"
255          * depending on the multiplier, connector, etc.,
256          * rather than just a single range.
257          */
258         if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
259                 INTELPllInvalid("dot out of range\n");
260
261         return true;
262 }
263
264 /**
265  * Returns a set of divisors for the desired target clock with the given
266  * refclk, or FALSE.  The returned values represent the clock equation:
267  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
268  */
269 static bool psb_intel_find_best_PLL(struct drm_crtc *crtc, int target,
270                                 int refclk,
271                                 struct psb_intel_clock_t *best_clock)
272 {
273         struct drm_device *dev = crtc->dev;
274         struct psb_intel_clock_t clock;
275         const struct psb_intel_limit_t *limit = psb_intel_limit(crtc);
276         int err = target;
277
278         if (psb_intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
279             (REG_READ(LVDS) & LVDS_PORT_EN) != 0) {
280                 /*
281                  * For LVDS, if the panel is on, just rely on its current
282                  * settings for dual-channel.  We haven't figured out how to
283                  * reliably set up different single/dual channel state, if we
284                  * even can.
285                  */
286                 if ((REG_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
287                     LVDS_CLKB_POWER_UP)
288                         clock.p2 = limit->p2.p2_fast;
289                 else
290                         clock.p2 = limit->p2.p2_slow;
291         } else {
292                 if (target < limit->p2.dot_limit)
293                         clock.p2 = limit->p2.p2_slow;
294                 else
295                         clock.p2 = limit->p2.p2_fast;
296         }
297
298         memset(best_clock, 0, sizeof(*best_clock));
299
300         for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
301              clock.m1++) {
302                 for (clock.m2 = limit->m2.min;
303                      clock.m2 < clock.m1 && clock.m2 <= limit->m2.max;
304                      clock.m2++) {
305                         for (clock.n = limit->n.min;
306                              clock.n <= limit->n.max; clock.n++) {
307                                 for (clock.p1 = limit->p1.min;
308                                      clock.p1 <= limit->p1.max;
309                                      clock.p1++) {
310                                         int this_err;
311
312                                         psb_intel_clock(dev, refclk, &clock);
313
314                                         if (!psb_intel_PLL_is_valid
315                                             (crtc, &clock))
316                                                 continue;
317
318                                         this_err = abs(clock.dot - target);
319                                         if (this_err < err) {
320                                                 *best_clock = clock;
321                                                 err = this_err;
322                                         }
323                                 }
324                         }
325                 }
326         }
327
328         return err != target;
329 }
330
331 void psb_intel_wait_for_vblank(struct drm_device *dev)
332 {
333         /* Wait for 20ms, i.e. one cycle at 50hz. */
334         udelay(20000);
335 }
336
337 int psb_intel_pipe_set_base(struct drm_crtc *crtc,
338                             int x, int y, struct drm_framebuffer *old_fb)
339 {
340         struct drm_device *dev = crtc->dev;
341         /* struct drm_i915_master_private *master_priv; */
342         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
343         struct psb_framebuffer *psbfb = to_psb_fb(crtc->fb);
344         int pipe = psb_intel_crtc->pipe;
345         unsigned long start, offset;
346         int dspbase = (pipe == 0 ? DSPABASE : DSPBBASE);
347         int dspsurf = (pipe == 0 ? DSPASURF : DSPBSURF);
348         int dspstride = (pipe == 0) ? DSPASTRIDE : DSPBSTRIDE;
349         int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
350         u32 dspcntr;
351         int ret = 0;
352
353         PSB_DEBUG_ENTRY("\n");
354
355         /* no fb bound */
356         if (!crtc->fb) {
357                 DRM_DEBUG("No FB bound\n");
358                 return 0;
359         }
360
361         if (!gma_power_begin(dev, true))
362                 return 0;
363
364         /* We are displaying this buffer, make sure it is actually loaded
365            into the GTT */
366         ret = psb_gtt_pin(psbfb->gtt);
367         if (ret < 0)
368                 goto psb_intel_pipe_set_base_exit;
369         start = psbfb->gtt->offset;
370
371         offset = y * crtc->fb->pitch + x * (crtc->fb->bits_per_pixel / 8);
372
373         REG_WRITE(dspstride, crtc->fb->pitch);
374
375         dspcntr = REG_READ(dspcntr_reg);
376         dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
377
378         switch (crtc->fb->bits_per_pixel) {
379         case 8:
380                 dspcntr |= DISPPLANE_8BPP;
381                 break;
382         case 16:
383                 if (crtc->fb->depth == 15)
384                         dspcntr |= DISPPLANE_15_16BPP;
385                 else
386                         dspcntr |= DISPPLANE_16BPP;
387                 break;
388         case 24:
389         case 32:
390                 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
391                 break;
392         default:
393                 DRM_ERROR("Unknown color depth\n");
394                 ret = -EINVAL;
395                 psb_gtt_unpin(psbfb->gtt);
396                 goto psb_intel_pipe_set_base_exit;
397         }
398         REG_WRITE(dspcntr_reg, dspcntr);
399
400
401         DRM_DEBUG("Writing base %08lX %08lX %d %d\n", start, offset, x, y);
402         if (0 /* FIXMEAC - check what PSB needs */) {
403                 REG_WRITE(dspbase, offset);
404                 REG_READ(dspbase);
405                 REG_WRITE(dspsurf, start);
406                 REG_READ(dspsurf);
407         } else {
408                 REG_WRITE(dspbase, start + offset);
409                 REG_READ(dspbase);
410         }
411
412         /* If there was a previous display we can now unpin it */
413         if (old_fb)
414                 psb_gtt_unpin(to_psb_fb(old_fb)->gtt);
415
416 psb_intel_pipe_set_base_exit:
417         gma_power_end(dev);
418         return ret;
419 }
420
421 /**
422  * Sets the power management mode of the pipe and plane.
423  *
424  * This code should probably grow support for turning the cursor off and back
425  * on appropriately at the same time as we're turning the pipe off/on.
426  */
427 static void psb_intel_crtc_dpms(struct drm_crtc *crtc, int mode)
428 {
429         struct drm_device *dev = crtc->dev;
430         /* struct drm_i915_master_private *master_priv; */
431         /* struct drm_i915_private *dev_priv = dev->dev_private; */
432         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
433         int pipe = psb_intel_crtc->pipe;
434         int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
435         int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
436         int dspbase_reg = (pipe == 0) ? DSPABASE : DSPBBASE;
437         int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
438         u32 temp;
439         bool enabled;
440
441         /* XXX: When our outputs are all unaware of DPMS modes other than off
442          * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
443          */
444         switch (mode) {
445         case DRM_MODE_DPMS_ON:
446         case DRM_MODE_DPMS_STANDBY:
447         case DRM_MODE_DPMS_SUSPEND:
448                 /* Enable the DPLL */
449                 temp = REG_READ(dpll_reg);
450                 if ((temp & DPLL_VCO_ENABLE) == 0) {
451                         REG_WRITE(dpll_reg, temp);
452                         REG_READ(dpll_reg);
453                         /* Wait for the clocks to stabilize. */
454                         udelay(150);
455                         REG_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
456                         REG_READ(dpll_reg);
457                         /* Wait for the clocks to stabilize. */
458                         udelay(150);
459                         REG_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
460                         REG_READ(dpll_reg);
461                         /* Wait for the clocks to stabilize. */
462                         udelay(150);
463                 }
464
465                 /* Enable the pipe */
466                 temp = REG_READ(pipeconf_reg);
467                 if ((temp & PIPEACONF_ENABLE) == 0)
468                         REG_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
469
470                 /* Enable the plane */
471                 temp = REG_READ(dspcntr_reg);
472                 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
473                         REG_WRITE(dspcntr_reg,
474                                   temp | DISPLAY_PLANE_ENABLE);
475                         /* Flush the plane changes */
476                         REG_WRITE(dspbase_reg, REG_READ(dspbase_reg));
477                 }
478
479                 psb_intel_crtc_load_lut(crtc);
480
481                 /* Give the overlay scaler a chance to enable
482                  * if it's on this pipe */
483                 /* psb_intel_crtc_dpms_video(crtc, true); TODO */
484                 break;
485         case DRM_MODE_DPMS_OFF:
486                 /* Give the overlay scaler a chance to disable
487                  * if it's on this pipe */
488                 /* psb_intel_crtc_dpms_video(crtc, FALSE); TODO */
489
490                 /* Disable the VGA plane that we never use */
491                 REG_WRITE(VGACNTRL, VGA_DISP_DISABLE);
492
493                 /* Disable display plane */
494                 temp = REG_READ(dspcntr_reg);
495                 if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
496                         REG_WRITE(dspcntr_reg,
497                                   temp & ~DISPLAY_PLANE_ENABLE);
498                         /* Flush the plane changes */
499                         REG_WRITE(dspbase_reg, REG_READ(dspbase_reg));
500                         REG_READ(dspbase_reg);
501                 }
502
503                 /* Next, disable display pipes */
504                 temp = REG_READ(pipeconf_reg);
505                 if ((temp & PIPEACONF_ENABLE) != 0) {
506                         REG_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
507                         REG_READ(pipeconf_reg);
508                 }
509
510                 /* Wait for vblank for the disable to take effect. */
511                 psb_intel_wait_for_vblank(dev);
512
513                 temp = REG_READ(dpll_reg);
514                 if ((temp & DPLL_VCO_ENABLE) != 0) {
515                         REG_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
516                         REG_READ(dpll_reg);
517                 }
518
519                 /* Wait for the clocks to turn off. */
520                 udelay(150);
521                 break;
522         }
523
524         enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
525
526         /*Set FIFO Watermarks*/
527         REG_WRITE(DSPARB, 0x3F3E);
528 }
529
530 static void psb_intel_crtc_prepare(struct drm_crtc *crtc)
531 {
532         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
533         crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
534 }
535
536 static void psb_intel_crtc_commit(struct drm_crtc *crtc)
537 {
538         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
539         crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
540 }
541
542 void psb_intel_encoder_prepare(struct drm_encoder *encoder)
543 {
544         struct drm_encoder_helper_funcs *encoder_funcs =
545             encoder->helper_private;
546         /* lvds has its own version of prepare see psb_intel_lvds_prepare */
547         encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
548 }
549
550 void psb_intel_encoder_commit(struct drm_encoder *encoder)
551 {
552         struct drm_encoder_helper_funcs *encoder_funcs =
553             encoder->helper_private;
554         /* lvds has its own version of commit see psb_intel_lvds_commit */
555         encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
556 }
557
558 static bool psb_intel_crtc_mode_fixup(struct drm_crtc *crtc,
559                                   struct drm_display_mode *mode,
560                                   struct drm_display_mode *adjusted_mode)
561 {
562         return true;
563 }
564
565
566 /**
567  * Return the pipe currently connected to the panel fitter,
568  * or -1 if the panel fitter is not present or not in use
569  */
570 static int psb_intel_panel_fitter_pipe(struct drm_device *dev)
571 {
572         u32 pfit_control;
573
574         pfit_control = REG_READ(PFIT_CONTROL);
575
576         /* See if the panel fitter is in use */
577         if ((pfit_control & PFIT_ENABLE) == 0)
578                 return -1;
579         /* Must be on PIPE 1 for PSB */
580         return 1;
581 }
582
583 static int psb_intel_crtc_mode_set(struct drm_crtc *crtc,
584                                struct drm_display_mode *mode,
585                                struct drm_display_mode *adjusted_mode,
586                                int x, int y,
587                                struct drm_framebuffer *old_fb)
588 {
589         struct drm_device *dev = crtc->dev;
590         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
591         int pipe = psb_intel_crtc->pipe;
592         int fp_reg = (pipe == 0) ? FPA0 : FPB0;
593         int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
594         int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
595         int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
596         int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
597         int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
598         int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
599         int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
600         int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
601         int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
602         int dspsize_reg = (pipe == 0) ? DSPASIZE : DSPBSIZE;
603         int dsppos_reg = (pipe == 0) ? DSPAPOS : DSPBPOS;
604         int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
605         int refclk;
606         struct psb_intel_clock_t clock;
607         u32 dpll = 0, fp = 0, dspcntr, pipeconf;
608         bool ok, is_sdvo = false, is_dvo = false;
609         bool is_crt = false, is_lvds = false, is_tv = false;
610         struct drm_mode_config *mode_config = &dev->mode_config;
611         struct drm_connector *connector;
612
613         list_for_each_entry(connector, &mode_config->connector_list, head) {
614                 struct psb_intel_output *psb_intel_output =
615                     to_psb_intel_output(connector);
616
617                 if (!connector->encoder
618                     || connector->encoder->crtc != crtc)
619                         continue;
620
621                 switch (psb_intel_output->type) {
622                 case INTEL_OUTPUT_LVDS:
623                         is_lvds = true;
624                         break;
625                 case INTEL_OUTPUT_SDVO:
626                         is_sdvo = true;
627                         break;
628                 case INTEL_OUTPUT_DVO:
629                         is_dvo = true;
630                         break;
631                 case INTEL_OUTPUT_TVOUT:
632                         is_tv = true;
633                         break;
634                 case INTEL_OUTPUT_ANALOG:
635                         is_crt = true;
636                         break;
637                 }
638         }
639
640         refclk = 96000;
641
642         ok = psb_intel_find_best_PLL(crtc, adjusted_mode->clock, refclk,
643                                  &clock);
644         if (!ok) {
645                 DRM_ERROR("Couldn't find PLL settings for mode!\n");
646                 return 0;
647         }
648
649         fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
650
651         dpll = DPLL_VGA_MODE_DIS;
652         if (is_lvds) {
653                 dpll |= DPLLB_MODE_LVDS;
654                 dpll |= DPLL_DVO_HIGH_SPEED;
655         } else
656                 dpll |= DPLLB_MODE_DAC_SERIAL;
657         if (is_sdvo) {
658                 int sdvo_pixel_multiply =
659                             adjusted_mode->clock / mode->clock;
660                 dpll |= DPLL_DVO_HIGH_SPEED;
661                 dpll |=
662                     (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
663         }
664
665         /* compute bitmask from p1 value */
666         dpll |= (1 << (clock.p1 - 1)) << 16;
667         switch (clock.p2) {
668         case 5:
669                 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
670                 break;
671         case 7:
672                 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
673                 break;
674         case 10:
675                 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
676                 break;
677         case 14:
678                 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
679                 break;
680         }
681
682         if (is_tv) {
683                 /* XXX: just matching BIOS for now */
684 /*      dpll |= PLL_REF_INPUT_TVCLKINBC; */
685                 dpll |= 3;
686         }
687         dpll |= PLL_REF_INPUT_DREFCLK;
688
689         /* setup pipeconf */
690         pipeconf = REG_READ(pipeconf_reg);
691
692         /* Set up the display plane register */
693         dspcntr = DISPPLANE_GAMMA_ENABLE;
694
695         if (pipe == 0)
696                 dspcntr |= DISPPLANE_SEL_PIPE_A;
697         else
698                 dspcntr |= DISPPLANE_SEL_PIPE_B;
699
700         dspcntr |= DISPLAY_PLANE_ENABLE;
701         pipeconf |= PIPEACONF_ENABLE;
702         dpll |= DPLL_VCO_ENABLE;
703
704
705         /* Disable the panel fitter if it was on our pipe */
706         if (psb_intel_panel_fitter_pipe(dev) == pipe)
707                 REG_WRITE(PFIT_CONTROL, 0);
708
709         DRM_DEBUG("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
710         drm_mode_debug_printmodeline(mode);
711
712         if (dpll & DPLL_VCO_ENABLE) {
713                 REG_WRITE(fp_reg, fp);
714                 REG_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
715                 REG_READ(dpll_reg);
716                 udelay(150);
717         }
718
719         /* The LVDS pin pair needs to be on before the DPLLs are enabled.
720          * This is an exception to the general rule that mode_set doesn't turn
721          * things on.
722          */
723         if (is_lvds) {
724                 u32 lvds = REG_READ(LVDS);
725
726                 lvds |=
727                     LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP |
728                     LVDS_PIPEB_SELECT;
729                 /* Set the B0-B3 data pairs corresponding to
730                  * whether we're going to
731                  * set the DPLLs for dual-channel mode or not.
732                  */
733                 if (clock.p2 == 7)
734                         lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
735                 else
736                         lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
737
738                 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
739                  * appropriately here, but we need to look more
740                  * thoroughly into how panels behave in the two modes.
741                  */
742
743                 REG_WRITE(LVDS, lvds);
744                 REG_READ(LVDS);
745         }
746
747         REG_WRITE(fp_reg, fp);
748         REG_WRITE(dpll_reg, dpll);
749         REG_READ(dpll_reg);
750         /* Wait for the clocks to stabilize. */
751         udelay(150);
752
753         /* write it again -- the BIOS does, after all */
754         REG_WRITE(dpll_reg, dpll);
755
756         REG_READ(dpll_reg);
757         /* Wait for the clocks to stabilize. */
758         udelay(150);
759
760         REG_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
761                   ((adjusted_mode->crtc_htotal - 1) << 16));
762         REG_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
763                   ((adjusted_mode->crtc_hblank_end - 1) << 16));
764         REG_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
765                   ((adjusted_mode->crtc_hsync_end - 1) << 16));
766         REG_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
767                   ((adjusted_mode->crtc_vtotal - 1) << 16));
768         REG_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
769                   ((adjusted_mode->crtc_vblank_end - 1) << 16));
770         REG_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
771                   ((adjusted_mode->crtc_vsync_end - 1) << 16));
772         /* pipesrc and dspsize control the size that is scaled from,
773          * which should always be the user's requested size.
774          */
775         REG_WRITE(dspsize_reg,
776                   ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
777         REG_WRITE(dsppos_reg, 0);
778         REG_WRITE(pipesrc_reg,
779                   ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
780         REG_WRITE(pipeconf_reg, pipeconf);
781         REG_READ(pipeconf_reg);
782
783         psb_intel_wait_for_vblank(dev);
784
785         REG_WRITE(dspcntr_reg, dspcntr);
786
787         /* Flush the plane changes */
788         {
789                 struct drm_crtc_helper_funcs *crtc_funcs =
790                     crtc->helper_private;
791                 crtc_funcs->mode_set_base(crtc, x, y, old_fb);
792         }
793
794         psb_intel_wait_for_vblank(dev);
795
796         return 0;
797 }
798
799 /** Loads the palette/gamma unit for the CRTC with the prepared values */
800 void psb_intel_crtc_load_lut(struct drm_crtc *crtc)
801 {
802         struct drm_device *dev = crtc->dev;
803         struct drm_psb_private *dev_priv =
804                                 (struct drm_psb_private *)dev->dev_private;
805         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
806         int palreg = PALETTE_A;
807         int i;
808
809         /* The clocks have to be on to load the palette. */
810         if (!crtc->enabled)
811                 return;
812
813         switch (psb_intel_crtc->pipe) {
814         case 0:
815                 break;
816         case 1:
817                 palreg = PALETTE_B;
818                 break;
819         case 2:
820                 palreg = PALETTE_C;
821                 break;
822         default:
823                 DRM_ERROR("Illegal Pipe Number.\n");
824                 return;
825         }
826
827         if (gma_power_begin(dev, false)) {
828                 for (i = 0; i < 256; i++) {
829                         REG_WRITE(palreg + 4 * i,
830                                   ((psb_intel_crtc->lut_r[i] +
831                                   psb_intel_crtc->lut_adj[i]) << 16) |
832                                   ((psb_intel_crtc->lut_g[i] +
833                                   psb_intel_crtc->lut_adj[i]) << 8) |
834                                   (psb_intel_crtc->lut_b[i] +
835                                   psb_intel_crtc->lut_adj[i]));
836                 }
837                 gma_power_end(dev);
838         } else {
839                 for (i = 0; i < 256; i++) {
840                         dev_priv->save_palette_a[i] =
841                                   ((psb_intel_crtc->lut_r[i] +
842                                   psb_intel_crtc->lut_adj[i]) << 16) |
843                                   ((psb_intel_crtc->lut_g[i] +
844                                   psb_intel_crtc->lut_adj[i]) << 8) |
845                                   (psb_intel_crtc->lut_b[i] +
846                                   psb_intel_crtc->lut_adj[i]);
847                 }
848
849         }
850 }
851
852 /**
853  * Save HW states of giving crtc
854  */
855 static void psb_intel_crtc_save(struct drm_crtc *crtc)
856 {
857         struct drm_device *dev = crtc->dev;
858         /* struct drm_psb_private *dev_priv =
859                         (struct drm_psb_private *)dev->dev_private; */
860         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
861         struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
862         int pipeA = (psb_intel_crtc->pipe == 0);
863         uint32_t paletteReg;
864         int i;
865
866         DRM_DEBUG("\n");
867
868         if (!crtc_state) {
869                 DRM_DEBUG("No CRTC state found\n");
870                 return;
871         }
872
873         crtc_state->saveDSPCNTR = REG_READ(pipeA ? DSPACNTR : DSPBCNTR);
874         crtc_state->savePIPECONF = REG_READ(pipeA ? PIPEACONF : PIPEBCONF);
875         crtc_state->savePIPESRC = REG_READ(pipeA ? PIPEASRC : PIPEBSRC);
876         crtc_state->saveFP0 = REG_READ(pipeA ? FPA0 : FPB0);
877         crtc_state->saveFP1 = REG_READ(pipeA ? FPA1 : FPB1);
878         crtc_state->saveDPLL = REG_READ(pipeA ? DPLL_A : DPLL_B);
879         crtc_state->saveHTOTAL = REG_READ(pipeA ? HTOTAL_A : HTOTAL_B);
880         crtc_state->saveHBLANK = REG_READ(pipeA ? HBLANK_A : HBLANK_B);
881         crtc_state->saveHSYNC = REG_READ(pipeA ? HSYNC_A : HSYNC_B);
882         crtc_state->saveVTOTAL = REG_READ(pipeA ? VTOTAL_A : VTOTAL_B);
883         crtc_state->saveVBLANK = REG_READ(pipeA ? VBLANK_A : VBLANK_B);
884         crtc_state->saveVSYNC = REG_READ(pipeA ? VSYNC_A : VSYNC_B);
885         crtc_state->saveDSPSTRIDE = REG_READ(pipeA ? DSPASTRIDE : DSPBSTRIDE);
886
887         /*NOTE: DSPSIZE DSPPOS only for psb*/
888         crtc_state->saveDSPSIZE = REG_READ(pipeA ? DSPASIZE : DSPBSIZE);
889         crtc_state->saveDSPPOS = REG_READ(pipeA ? DSPAPOS : DSPBPOS);
890
891         crtc_state->saveDSPBASE = REG_READ(pipeA ? DSPABASE : DSPBBASE);
892
893         DRM_DEBUG("(%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
894                         crtc_state->saveDSPCNTR,
895                         crtc_state->savePIPECONF,
896                         crtc_state->savePIPESRC,
897                         crtc_state->saveFP0,
898                         crtc_state->saveFP1,
899                         crtc_state->saveDPLL,
900                         crtc_state->saveHTOTAL,
901                         crtc_state->saveHBLANK,
902                         crtc_state->saveHSYNC,
903                         crtc_state->saveVTOTAL,
904                         crtc_state->saveVBLANK,
905                         crtc_state->saveVSYNC,
906                         crtc_state->saveDSPSTRIDE,
907                         crtc_state->saveDSPSIZE,
908                         crtc_state->saveDSPPOS,
909                         crtc_state->saveDSPBASE
910                 );
911
912         paletteReg = pipeA ? PALETTE_A : PALETTE_B;
913         for (i = 0; i < 256; ++i)
914                 crtc_state->savePalette[i] = REG_READ(paletteReg + (i << 2));
915 }
916
917 /**
918  * Restore HW states of giving crtc
919  */
920 static void psb_intel_crtc_restore(struct drm_crtc *crtc)
921 {
922         struct drm_device *dev = crtc->dev;
923         /* struct drm_psb_private * dev_priv =
924                                 (struct drm_psb_private *)dev->dev_private; */
925         struct psb_intel_crtc *psb_intel_crtc =  to_psb_intel_crtc(crtc);
926         struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
927         /* struct drm_crtc_helper_funcs * crtc_funcs = crtc->helper_private; */
928         int pipeA = (psb_intel_crtc->pipe == 0);
929         uint32_t paletteReg;
930         int i;
931
932         DRM_DEBUG("\n");
933
934         if (!crtc_state) {
935                 DRM_DEBUG("No crtc state\n");
936                 return;
937         }
938
939         DRM_DEBUG(
940                 "current:(%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
941                 REG_READ(pipeA ? DSPACNTR : DSPBCNTR),
942                 REG_READ(pipeA ? PIPEACONF : PIPEBCONF),
943                 REG_READ(pipeA ? PIPEASRC : PIPEBSRC),
944                 REG_READ(pipeA ? FPA0 : FPB0),
945                 REG_READ(pipeA ? FPA1 : FPB1),
946                 REG_READ(pipeA ? DPLL_A : DPLL_B),
947                 REG_READ(pipeA ? HTOTAL_A : HTOTAL_B),
948                 REG_READ(pipeA ? HBLANK_A : HBLANK_B),
949                 REG_READ(pipeA ? HSYNC_A : HSYNC_B),
950                 REG_READ(pipeA ? VTOTAL_A : VTOTAL_B),
951                 REG_READ(pipeA ? VBLANK_A : VBLANK_B),
952                 REG_READ(pipeA ? VSYNC_A : VSYNC_B),
953                 REG_READ(pipeA ? DSPASTRIDE : DSPBSTRIDE),
954                 REG_READ(pipeA ? DSPASIZE : DSPBSIZE),
955                 REG_READ(pipeA ? DSPAPOS : DSPBPOS),
956                 REG_READ(pipeA ? DSPABASE : DSPBBASE)
957                 );
958
959         DRM_DEBUG(
960                 "saved: (%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
961                 crtc_state->saveDSPCNTR,
962                 crtc_state->savePIPECONF,
963                 crtc_state->savePIPESRC,
964                 crtc_state->saveFP0,
965                 crtc_state->saveFP1,
966                 crtc_state->saveDPLL,
967                 crtc_state->saveHTOTAL,
968                 crtc_state->saveHBLANK,
969                 crtc_state->saveHSYNC,
970                 crtc_state->saveVTOTAL,
971                 crtc_state->saveVBLANK,
972                 crtc_state->saveVSYNC,
973                 crtc_state->saveDSPSTRIDE,
974                 crtc_state->saveDSPSIZE,
975                 crtc_state->saveDSPPOS,
976                 crtc_state->saveDSPBASE
977                 );
978
979
980         if (crtc_state->saveDPLL & DPLL_VCO_ENABLE) {
981                 REG_WRITE(pipeA ? DPLL_A : DPLL_B,
982                         crtc_state->saveDPLL & ~DPLL_VCO_ENABLE);
983                 REG_READ(pipeA ? DPLL_A : DPLL_B);
984                 DRM_DEBUG("write dpll: %x\n",
985                                 REG_READ(pipeA ? DPLL_A : DPLL_B));
986                 udelay(150);
987         }
988
989         REG_WRITE(pipeA ? FPA0 : FPB0, crtc_state->saveFP0);
990         REG_READ(pipeA ? FPA0 : FPB0);
991
992         REG_WRITE(pipeA ? FPA1 : FPB1, crtc_state->saveFP1);
993         REG_READ(pipeA ? FPA1 : FPB1);
994
995         REG_WRITE(pipeA ? DPLL_A : DPLL_B, crtc_state->saveDPLL);
996         REG_READ(pipeA ? DPLL_A : DPLL_B);
997         udelay(150);
998
999         REG_WRITE(pipeA ? HTOTAL_A : HTOTAL_B, crtc_state->saveHTOTAL);
1000         REG_WRITE(pipeA ? HBLANK_A : HBLANK_B, crtc_state->saveHBLANK);
1001         REG_WRITE(pipeA ? HSYNC_A : HSYNC_B, crtc_state->saveHSYNC);
1002         REG_WRITE(pipeA ? VTOTAL_A : VTOTAL_B, crtc_state->saveVTOTAL);
1003         REG_WRITE(pipeA ? VBLANK_A : VBLANK_B, crtc_state->saveVBLANK);
1004         REG_WRITE(pipeA ? VSYNC_A : VSYNC_B, crtc_state->saveVSYNC);
1005         REG_WRITE(pipeA ? DSPASTRIDE : DSPBSTRIDE, crtc_state->saveDSPSTRIDE);
1006
1007         REG_WRITE(pipeA ? DSPASIZE : DSPBSIZE, crtc_state->saveDSPSIZE);
1008         REG_WRITE(pipeA ? DSPAPOS : DSPBPOS, crtc_state->saveDSPPOS);
1009
1010         REG_WRITE(pipeA ? PIPEASRC : PIPEBSRC, crtc_state->savePIPESRC);
1011         REG_WRITE(pipeA ? DSPABASE : DSPBBASE, crtc_state->saveDSPBASE);
1012         REG_WRITE(pipeA ? PIPEACONF : PIPEBCONF, crtc_state->savePIPECONF);
1013
1014         psb_intel_wait_for_vblank(dev);
1015
1016         REG_WRITE(pipeA ? DSPACNTR : DSPBCNTR, crtc_state->saveDSPCNTR);
1017         REG_WRITE(pipeA ? DSPABASE : DSPBBASE, crtc_state->saveDSPBASE);
1018
1019         psb_intel_wait_for_vblank(dev);
1020
1021         paletteReg = pipeA ? PALETTE_A : PALETTE_B;
1022         for (i = 0; i < 256; ++i)
1023                 REG_WRITE(paletteReg + (i << 2), crtc_state->savePalette[i]);
1024 }
1025
1026 static int psb_intel_crtc_cursor_set(struct drm_crtc *crtc,
1027                                  struct drm_file *file_priv,
1028                                  uint32_t handle,
1029                                  uint32_t width, uint32_t height)
1030 {
1031         struct drm_device *dev = crtc->dev;
1032         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1033         int pipe = psb_intel_crtc->pipe;
1034         uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
1035         uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
1036         uint32_t temp;
1037         size_t addr = 0;
1038         struct gtt_range *gt;
1039         struct drm_gem_object *obj;
1040         int ret;
1041
1042         DRM_DEBUG("\n");
1043
1044         /* if we want to turn of the cursor ignore width and height */
1045         if (!handle) {
1046                 DRM_DEBUG("cursor off\n");
1047                 /* turn off the cursor */
1048                 temp = CURSOR_MODE_DISABLE;
1049
1050                 if (gma_power_begin(dev, false)) {
1051                         REG_WRITE(control, temp);
1052                         REG_WRITE(base, 0);
1053                         gma_power_end(dev);
1054                 }
1055
1056                 /* Unpin the old GEM object */
1057                 if (psb_intel_crtc->cursor_obj) {
1058                         gt = container_of(psb_intel_crtc->cursor_obj,
1059                                                         struct gtt_range, gem);
1060                         psb_gtt_unpin(gt);
1061                         drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
1062                         psb_intel_crtc->cursor_obj = NULL;
1063                 }
1064
1065                 return 0;
1066         }
1067
1068         /* Currently we only support 64x64 cursors */
1069         if (width != 64 || height != 64) {
1070                 DRM_ERROR("we currently only support 64x64 cursors\n");
1071                 return -EINVAL;
1072         }
1073
1074         obj = drm_gem_object_lookup(dev, file_priv, handle);
1075         if (!obj)
1076                 return -ENOENT;
1077
1078         if (obj->size < width * height * 4) {
1079                 DRM_ERROR("buffer is to small\n");
1080                 return -ENOMEM;
1081         }
1082
1083         gt = container_of(obj, struct gtt_range, gem);
1084
1085         /* Pin the memory into the GTT */
1086         ret = psb_gtt_pin(gt);
1087         if (ret) {
1088                 DRM_ERROR("Can not pin down handle 0x%x\n", handle);
1089                 return ret;
1090         }
1091
1092
1093         addr = gt->offset;      /* Or resource.start ??? */
1094
1095         psb_intel_crtc->cursor_addr = addr;
1096
1097         temp = 0;
1098         /* set the pipe for the cursor */
1099         temp |= (pipe << 28);
1100         temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
1101
1102         if (gma_power_begin(dev, false)) {
1103                 REG_WRITE(control, temp);
1104                 REG_WRITE(base, addr);
1105                 gma_power_end(dev);
1106         }
1107
1108         /* unpin the old bo */
1109         if (psb_intel_crtc->cursor_obj && psb_intel_crtc->cursor_obj != obj) {
1110                 gt = container_of(psb_intel_crtc->cursor_obj,
1111                                                         struct gtt_range, gem);
1112                 psb_gtt_unpin(gt);
1113                 drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
1114                 psb_intel_crtc->cursor_obj = obj;
1115         }
1116
1117         return 0;
1118 }
1119
1120 static int psb_intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
1121 {
1122         struct drm_device *dev = crtc->dev;
1123         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1124         int pipe = psb_intel_crtc->pipe;
1125         uint32_t temp = 0;
1126         uint32_t addr;
1127
1128
1129         if (x < 0) {
1130                 temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
1131                 x = -x;
1132         }
1133         if (y < 0) {
1134                 temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
1135                 y = -y;
1136         }
1137
1138         temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
1139         temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
1140
1141         addr = psb_intel_crtc->cursor_addr;
1142
1143         if (gma_power_begin(dev, false)) {
1144                 REG_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
1145                 REG_WRITE((pipe == 0) ? CURABASE : CURBBASE, addr);
1146                 gma_power_end(dev);
1147         }
1148         return 0;
1149 }
1150
1151 static void psb_intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red,
1152                          u16 *green, u16 *blue, uint32_t type, uint32_t size)
1153 {
1154         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1155         int i;
1156
1157         if (size != 256)
1158                 return;
1159
1160         for (i = 0; i < 256; i++) {
1161                 psb_intel_crtc->lut_r[i] = red[i] >> 8;
1162                 psb_intel_crtc->lut_g[i] = green[i] >> 8;
1163                 psb_intel_crtc->lut_b[i] = blue[i] >> 8;
1164         }
1165
1166         psb_intel_crtc_load_lut(crtc);
1167 }
1168
1169 static int psb_crtc_set_config(struct drm_mode_set *set)
1170 {
1171         int ret;
1172         struct drm_device *dev = set->crtc->dev;
1173         struct drm_psb_private *dev_priv = dev->dev_private;
1174
1175         if (!dev_priv->rpm_enabled)
1176                 return drm_crtc_helper_set_config(set);
1177
1178         pm_runtime_forbid(&dev->pdev->dev);
1179         ret = drm_crtc_helper_set_config(set);
1180         pm_runtime_allow(&dev->pdev->dev);
1181         return ret;
1182 }
1183
1184 /* Returns the clock of the currently programmed mode of the given pipe. */
1185 static int psb_intel_crtc_clock_get(struct drm_device *dev,
1186                                 struct drm_crtc *crtc)
1187 {
1188         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1189         int pipe = psb_intel_crtc->pipe;
1190         u32 dpll;
1191         u32 fp;
1192         struct psb_intel_clock_t clock;
1193         bool is_lvds;
1194         struct drm_psb_private *dev_priv = dev->dev_private;
1195
1196         if (gma_power_begin(dev, false)) {
1197                 dpll = REG_READ((pipe == 0) ? DPLL_A : DPLL_B);
1198                 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
1199                         fp = REG_READ((pipe == 0) ? FPA0 : FPB0);
1200                 else
1201                         fp = REG_READ((pipe == 0) ? FPA1 : FPB1);
1202                 is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
1203                 gma_power_end(dev);
1204         } else {
1205                 dpll = (pipe == 0) ?
1206                         dev_priv->saveDPLL_A : dev_priv->saveDPLL_B;
1207
1208                 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
1209                         fp = (pipe == 0) ?
1210                                 dev_priv->saveFPA0 :
1211                                 dev_priv->saveFPB0;
1212                 else
1213                         fp = (pipe == 0) ?
1214                                 dev_priv->saveFPA1 :
1215                                 dev_priv->saveFPB1;
1216
1217                 is_lvds = (pipe == 1) && (dev_priv->saveLVDS & LVDS_PORT_EN);
1218         }
1219
1220         clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
1221         clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
1222         clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
1223
1224         if (is_lvds) {
1225                 clock.p1 =
1226                     ffs((dpll &
1227                          DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
1228                         DPLL_FPA01_P1_POST_DIV_SHIFT);
1229                 clock.p2 = 14;
1230
1231                 if ((dpll & PLL_REF_INPUT_MASK) ==
1232                     PLLB_REF_INPUT_SPREADSPECTRUMIN) {
1233                         /* XXX: might not be 66MHz */
1234                         i8xx_clock(66000, &clock);
1235                 } else
1236                         i8xx_clock(48000, &clock);
1237         } else {
1238                 if (dpll & PLL_P1_DIVIDE_BY_TWO)
1239                         clock.p1 = 2;
1240                 else {
1241                         clock.p1 =
1242                             ((dpll &
1243                               DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
1244                              DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
1245                 }
1246                 if (dpll & PLL_P2_DIVIDE_BY_4)
1247                         clock.p2 = 4;
1248                 else
1249                         clock.p2 = 2;
1250
1251                 i8xx_clock(48000, &clock);
1252         }
1253
1254         /* XXX: It would be nice to validate the clocks, but we can't reuse
1255          * i830PllIsValid() because it relies on the xf86_config connector
1256          * configuration being accurate, which it isn't necessarily.
1257          */
1258
1259         return clock.dot;
1260 }
1261
1262 /** Returns the currently programmed mode of the given pipe. */
1263 struct drm_display_mode *psb_intel_crtc_mode_get(struct drm_device *dev,
1264                                              struct drm_crtc *crtc)
1265 {
1266         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1267         int pipe = psb_intel_crtc->pipe;
1268         struct drm_display_mode *mode;
1269         int htot;
1270         int hsync;
1271         int vtot;
1272         int vsync;
1273         struct drm_psb_private *dev_priv = dev->dev_private;
1274
1275         if (gma_power_begin(dev, false)) {
1276                 htot = REG_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
1277                 hsync = REG_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
1278                 vtot = REG_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
1279                 vsync = REG_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
1280                 gma_power_end(dev);
1281         } else {
1282                 htot = (pipe == 0) ?
1283                         dev_priv->saveHTOTAL_A : dev_priv->saveHTOTAL_B;
1284                 hsync = (pipe == 0) ?
1285                         dev_priv->saveHSYNC_A : dev_priv->saveHSYNC_B;
1286                 vtot = (pipe == 0) ?
1287                         dev_priv->saveVTOTAL_A : dev_priv->saveVTOTAL_B;
1288                 vsync = (pipe == 0) ?
1289                         dev_priv->saveVSYNC_A : dev_priv->saveVSYNC_B;
1290         }
1291
1292         mode = kzalloc(sizeof(*mode), GFP_KERNEL);
1293         if (!mode)
1294                 return NULL;
1295
1296         mode->clock = psb_intel_crtc_clock_get(dev, crtc);
1297         mode->hdisplay = (htot & 0xffff) + 1;
1298         mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
1299         mode->hsync_start = (hsync & 0xffff) + 1;
1300         mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
1301         mode->vdisplay = (vtot & 0xffff) + 1;
1302         mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
1303         mode->vsync_start = (vsync & 0xffff) + 1;
1304         mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
1305
1306         drm_mode_set_name(mode);
1307         drm_mode_set_crtcinfo(mode, 0);
1308
1309         return mode;
1310 }
1311
1312 static void psb_intel_crtc_destroy(struct drm_crtc *crtc)
1313 {
1314         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1315         struct gtt_range *gt;
1316
1317         /* Unpin the old GEM object */
1318         if (psb_intel_crtc->cursor_obj) {
1319                 gt = container_of(psb_intel_crtc->cursor_obj,
1320                                                 struct gtt_range, gem);
1321                 psb_gtt_unpin(gt);
1322                 drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
1323                 psb_intel_crtc->cursor_obj = NULL;
1324         }
1325         kfree(psb_intel_crtc->crtc_state);
1326         drm_crtc_cleanup(crtc);
1327         kfree(psb_intel_crtc);
1328 }
1329
1330 static const struct drm_crtc_helper_funcs psb_intel_helper_funcs = {
1331         .dpms = psb_intel_crtc_dpms,
1332         .mode_fixup = psb_intel_crtc_mode_fixup,
1333         .mode_set = psb_intel_crtc_mode_set,
1334         .mode_set_base = psb_intel_pipe_set_base,
1335         .prepare = psb_intel_crtc_prepare,
1336         .commit = psb_intel_crtc_commit,
1337 };
1338
1339 const struct drm_crtc_funcs psb_intel_crtc_funcs = {
1340         .save = psb_intel_crtc_save,
1341         .restore = psb_intel_crtc_restore,
1342         .cursor_set = psb_intel_crtc_cursor_set,
1343         .cursor_move = psb_intel_crtc_cursor_move,
1344         .gamma_set = psb_intel_crtc_gamma_set,
1345         .set_config = psb_crtc_set_config,
1346         .destroy = psb_intel_crtc_destroy,
1347 };
1348
1349 void psb_intel_crtc_init(struct drm_device *dev, int pipe,
1350                      struct psb_intel_mode_device *mode_dev)
1351 {
1352         struct drm_psb_private *dev_priv = dev->dev_private;
1353         struct psb_intel_crtc *psb_intel_crtc;
1354         int i;
1355         uint16_t *r_base, *g_base, *b_base;
1356
1357         PSB_DEBUG_ENTRY("\n");
1358
1359         /* We allocate a extra array of drm_connector pointers
1360          * for fbdev after the crtc */
1361         psb_intel_crtc =
1362             kzalloc(sizeof(struct psb_intel_crtc) +
1363                     (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)),
1364                     GFP_KERNEL);
1365         if (psb_intel_crtc == NULL)
1366                 return;
1367
1368         psb_intel_crtc->crtc_state =
1369                 kzalloc(sizeof(struct psb_intel_crtc_state), GFP_KERNEL);
1370         if (!psb_intel_crtc->crtc_state) {
1371                 DRM_INFO("Crtc state error: No memory\n");
1372                 kfree(psb_intel_crtc);
1373                 return;
1374         }
1375
1376         drm_crtc_init(dev, &psb_intel_crtc->base, &psb_intel_crtc_funcs);
1377
1378         drm_mode_crtc_set_gamma_size(&psb_intel_crtc->base, 256);
1379         psb_intel_crtc->pipe = pipe;
1380         psb_intel_crtc->plane = pipe;
1381
1382         r_base = psb_intel_crtc->base.gamma_store;
1383         g_base = r_base + 256;
1384         b_base = g_base + 256;
1385         for (i = 0; i < 256; i++) {
1386                 psb_intel_crtc->lut_r[i] = i;
1387                 psb_intel_crtc->lut_g[i] = i;
1388                 psb_intel_crtc->lut_b[i] = i;
1389                 r_base[i] = i << 8;
1390                 g_base[i] = i << 8;
1391                 b_base[i] = i << 8;
1392
1393                 psb_intel_crtc->lut_adj[i] = 0;
1394         }
1395
1396         psb_intel_crtc->mode_dev = mode_dev;
1397         psb_intel_crtc->cursor_addr = 0;
1398
1399         if (IS_MRST(dev))
1400                 drm_crtc_helper_add(&psb_intel_crtc->base,
1401                                     &mrst_helper_funcs);
1402         else
1403                 drm_crtc_helper_add(&psb_intel_crtc->base,
1404                                     &psb_intel_helper_funcs);
1405
1406         /* Setup the array of drm_connector pointer array */
1407         psb_intel_crtc->mode_set.crtc = &psb_intel_crtc->base;
1408         BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
1409                dev_priv->plane_to_crtc_mapping[psb_intel_crtc->plane] != NULL);
1410         dev_priv->plane_to_crtc_mapping[psb_intel_crtc->plane] =
1411                                                         &psb_intel_crtc->base;
1412         dev_priv->pipe_to_crtc_mapping[psb_intel_crtc->pipe] =
1413                                                         &psb_intel_crtc->base;
1414         psb_intel_crtc->mode_set.connectors =
1415             (struct drm_connector **) (psb_intel_crtc + 1);
1416         psb_intel_crtc->mode_set.num_connectors = 0;
1417 }
1418
1419 int psb_intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
1420                                 struct drm_file *file_priv)
1421 {
1422         struct drm_psb_private *dev_priv = dev->dev_private;
1423         struct drm_psb_get_pipe_from_crtc_id_arg *pipe_from_crtc_id = data;
1424         struct drm_mode_object *drmmode_obj;
1425         struct psb_intel_crtc *crtc;
1426
1427         if (!dev_priv) {
1428                 DRM_ERROR("called with no initialization\n");
1429                 return -EINVAL;
1430         }
1431
1432         drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
1433                         DRM_MODE_OBJECT_CRTC);
1434
1435         if (!drmmode_obj) {
1436                 DRM_ERROR("no such CRTC id\n");
1437                 return -EINVAL;
1438         }
1439
1440         crtc = to_psb_intel_crtc(obj_to_crtc(drmmode_obj));
1441         pipe_from_crtc_id->pipe = crtc->pipe;
1442
1443         return 0;
1444 }
1445
1446 struct drm_crtc *psb_intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
1447 {
1448         struct drm_crtc *crtc = NULL;
1449
1450         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1451                 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1452                 if (psb_intel_crtc->pipe == pipe)
1453                         break;
1454         }
1455         return crtc;
1456 }
1457
1458 int psb_intel_connector_clones(struct drm_device *dev, int type_mask)
1459 {
1460         int index_mask = 0;
1461         struct drm_connector *connector;
1462         int entry = 0;
1463
1464         list_for_each_entry(connector, &dev->mode_config.connector_list,
1465                             head) {
1466                 struct psb_intel_output *psb_intel_output =
1467                     to_psb_intel_output(connector);
1468                 if (type_mask & (1 << psb_intel_output->type))
1469                         index_mask |= (1 << entry);
1470                 entry++;
1471         }
1472         return index_mask;
1473 }
1474
1475
1476 void psb_intel_modeset_cleanup(struct drm_device *dev)
1477 {
1478         drm_mode_config_cleanup(dev);
1479 }
1480
1481
1482 /* current intel driver doesn't take advantage of encoders
1483    always give back the encoder for the connector
1484 */
1485 struct drm_encoder *psb_intel_best_encoder(struct drm_connector *connector)
1486 {
1487         struct psb_intel_output *psb_intel_output =
1488                                         to_psb_intel_output(connector);
1489
1490         return &psb_intel_output->enc;
1491 }
1492