2 * Copyright © 2006-2011 Intel Corporation
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.
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
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.
18 * Eric Anholt <eric@anholt.net>
21 #include <linux/i2c.h>
22 #include <linux/pm_runtime.h>
25 #include "framebuffer.h"
27 #include "psb_intel_drv.h"
28 #include "psb_intel_reg.h"
29 #include "psb_intel_display.h"
31 #include "cdv_device.h"
34 struct cdv_intel_range_t {
38 struct cdv_intel_p2_t {
43 struct cdv_intel_clock_t {
55 #define INTEL_P2_NUM 2
57 struct cdv_intel_limit_t {
58 struct cdv_intel_range_t dot, vco, n, m, m1, m2, p, p1;
59 struct cdv_intel_p2_t p2;
62 #define CDV_LIMIT_SINGLE_LVDS_96 0
63 #define CDV_LIMIT_SINGLE_LVDS_100 1
64 #define CDV_LIMIT_DAC_HDMI_27 2
65 #define CDV_LIMIT_DAC_HDMI_96 3
67 static const struct cdv_intel_limit_t cdv_intel_limits[] = {
68 { /* CDV_SIGNLE_LVDS_96MHz */
69 .dot = {.min = 20000, .max = 115500},
70 .vco = {.min = 1800000, .max = 3600000},
71 .n = {.min = 2, .max = 6},
72 .m = {.min = 60, .max = 160},
73 .m1 = {.min = 0, .max = 0},
74 .m2 = {.min = 58, .max = 158},
75 .p = {.min = 28, .max = 140},
76 .p1 = {.min = 2, .max = 10},
77 .p2 = {.dot_limit = 200000,
78 .p2_slow = 14, .p2_fast = 14},
80 { /* CDV_SINGLE_LVDS_100MHz */
81 .dot = {.min = 20000, .max = 115500},
82 .vco = {.min = 1800000, .max = 3600000},
83 .n = {.min = 2, .max = 6},
84 .m = {.min = 60, .max = 160},
85 .m1 = {.min = 0, .max = 0},
86 .m2 = {.min = 58, .max = 158},
87 .p = {.min = 28, .max = 140},
88 .p1 = {.min = 2, .max = 10},
89 /* The single-channel range is 25-112Mhz, and dual-channel
90 * is 80-224Mhz. Prefer single channel as much as possible.
92 .p2 = {.dot_limit = 200000, .p2_slow = 14, .p2_fast = 14},
94 { /* CDV_DAC_HDMI_27MHz */
95 .dot = {.min = 20000, .max = 400000},
96 .vco = {.min = 1809000, .max = 3564000},
97 .n = {.min = 1, .max = 1},
98 .m = {.min = 67, .max = 132},
99 .m1 = {.min = 0, .max = 0},
100 .m2 = {.min = 65, .max = 130},
101 .p = {.min = 5, .max = 90},
102 .p1 = {.min = 1, .max = 9},
103 .p2 = {.dot_limit = 225000, .p2_slow = 10, .p2_fast = 5},
105 { /* CDV_DAC_HDMI_96MHz */
106 .dot = {.min = 20000, .max = 400000},
107 .vco = {.min = 1800000, .max = 3600000},
108 .n = {.min = 2, .max = 6},
109 .m = {.min = 60, .max = 160},
110 .m1 = {.min = 0, .max = 0},
111 .m2 = {.min = 58, .max = 158},
112 .p = {.min = 5, .max = 100},
113 .p1 = {.min = 1, .max = 10},
114 .p2 = {.dot_limit = 225000, .p2_slow = 10, .p2_fast = 5},
118 #define _wait_for(COND, MS, W) ({ \
119 unsigned long timeout__ = jiffies + msecs_to_jiffies(MS); \
122 if (time_after(jiffies, timeout__)) { \
123 ret__ = -ETIMEDOUT; \
126 if (W && !in_dbg_master()) \
132 #define wait_for(COND, MS) _wait_for(COND, MS, 1)
135 static int cdv_sb_read(struct drm_device *dev, u32 reg, u32 *val)
139 ret = wait_for((REG_READ(SB_PCKT) & SB_BUSY) == 0, 1000);
141 DRM_ERROR("timeout waiting for SB to idle before read\n");
145 REG_WRITE(SB_ADDR, reg);
147 SET_FIELD(SB_OPCODE_READ, SB_OPCODE) |
148 SET_FIELD(SB_DEST_DPLL, SB_DEST) |
149 SET_FIELD(0xf, SB_BYTE_ENABLE));
151 ret = wait_for((REG_READ(SB_PCKT) & SB_BUSY) == 0, 1000);
153 DRM_ERROR("timeout waiting for SB to idle after read\n");
157 *val = REG_READ(SB_DATA);
162 static int cdv_sb_write(struct drm_device *dev, u32 reg, u32 val)
165 static bool dpio_debug = true;
169 if (cdv_sb_read(dev, reg, &temp) == 0)
170 DRM_DEBUG_KMS("0x%08x: 0x%08x (before)\n", reg, temp);
171 DRM_DEBUG_KMS("0x%08x: 0x%08x\n", reg, val);
174 ret = wait_for((REG_READ(SB_PCKT) & SB_BUSY) == 0, 1000);
176 DRM_ERROR("timeout waiting for SB to idle before write\n");
180 REG_WRITE(SB_ADDR, reg);
181 REG_WRITE(SB_DATA, val);
183 SET_FIELD(SB_OPCODE_WRITE, SB_OPCODE) |
184 SET_FIELD(SB_DEST_DPLL, SB_DEST) |
185 SET_FIELD(0xf, SB_BYTE_ENABLE));
187 ret = wait_for((REG_READ(SB_PCKT) & SB_BUSY) == 0, 1000);
189 DRM_ERROR("timeout waiting for SB to idle after write\n");
194 if (cdv_sb_read(dev, reg, &temp) == 0)
195 DRM_DEBUG_KMS("0x%08x: 0x%08x (after)\n", reg, temp);
201 /* Reset the DPIO configuration register. The BIOS does this at every
204 static void cdv_sb_reset(struct drm_device *dev)
207 REG_WRITE(DPIO_CFG, 0);
209 REG_WRITE(DPIO_CFG, DPIO_MODE_SELECT_0 | DPIO_CMN_RESET_N);
212 /* Unlike most Intel display engines, on Cedarview the DPLL registers
213 * are behind this sideband bus. They must be programmed while the
214 * DPLL reference clock is on in the DPLL control register, but before
215 * the DPLL is enabled in the DPLL control register.
218 cdv_dpll_set_clock_cdv(struct drm_device *dev, struct drm_crtc *crtc,
219 struct cdv_intel_clock_t *clock, bool is_lvds)
221 struct psb_intel_crtc *psb_crtc =
222 to_psb_intel_crtc(crtc);
223 int pipe = psb_crtc->pipe;
226 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
227 int ref_sfr = (pipe == 0) ? SB_REF_DPLLA : SB_REF_DPLLB;
232 if ((REG_READ(dpll_reg) & DPLL_SYNCLOCK_ENABLE) == 0) {
233 DRM_ERROR("Attempting to set DPLL with refclk disabled\n");
237 /* Follow the BIOS and write the REF/SFR Register. Hardcoded value */
238 ref_value = 0x68A701;
240 cdv_sb_write(dev, SB_REF_SFR(pipe), ref_value);
242 /* We don't know what the other fields of these regs are, so
243 * leave them in place.
246 * The BIT 14:13 of 0x8010/0x8030 is used to select the ref clk
247 * for the pipe A/B. Display spec 1.06 has wrong definition.
248 * Correct definition is like below:
250 * refclka mean use clock from same PLL
252 * if DPLLA sets 01 and DPLLB sets 01, they use clock from their pll
254 * if DPLLA sets 01 and DPLLB sets 02, both use clk from DPLLA
257 ret = cdv_sb_read(dev, ref_sfr, &ref_value);
260 ref_value &= ~(REF_CLK_MASK);
262 /* use DPLL_A for pipeB on CRT/HDMI */
263 if (pipe == 1 && !is_lvds) {
264 DRM_DEBUG_KMS("use DPLLA for pipe B\n");
265 ref_value |= REF_CLK_DPLLA;
267 DRM_DEBUG_KMS("use their DPLL for pipe A/B\n");
268 ref_value |= REF_CLK_DPLL;
270 ret = cdv_sb_write(dev, ref_sfr, ref_value);
274 ret = cdv_sb_read(dev, SB_M(pipe), &m);
277 m &= ~SB_M_DIVIDER_MASK;
278 m |= ((clock->m2) << SB_M_DIVIDER_SHIFT);
279 ret = cdv_sb_write(dev, SB_M(pipe), m);
283 ret = cdv_sb_read(dev, SB_N_VCO(pipe), &n_vco);
287 /* Follow the BIOS to program the N_DIVIDER REG */
290 n_vco &= ~(SB_N_VCO_SEL_MASK |
294 n_vco |= ((clock->n) << SB_N_DIVIDER_SHIFT);
296 if (clock->vco < 2250000) {
297 n_vco |= (2 << SB_N_CB_TUNE_SHIFT);
298 n_vco |= (0 << SB_N_VCO_SEL_SHIFT);
299 } else if (clock->vco < 2750000) {
300 n_vco |= (1 << SB_N_CB_TUNE_SHIFT);
301 n_vco |= (1 << SB_N_VCO_SEL_SHIFT);
302 } else if (clock->vco < 3300000) {
303 n_vco |= (0 << SB_N_CB_TUNE_SHIFT);
304 n_vco |= (2 << SB_N_VCO_SEL_SHIFT);
306 n_vco |= (0 << SB_N_CB_TUNE_SHIFT);
307 n_vco |= (3 << SB_N_VCO_SEL_SHIFT);
310 ret = cdv_sb_write(dev, SB_N_VCO(pipe), n_vco);
314 ret = cdv_sb_read(dev, SB_P(pipe), &p);
317 p &= ~(SB_P2_DIVIDER_MASK | SB_P1_DIVIDER_MASK);
318 p |= SET_FIELD(clock->p1, SB_P1_DIVIDER);
321 p |= SET_FIELD(SB_P2_5, SB_P2_DIVIDER);
324 p |= SET_FIELD(SB_P2_10, SB_P2_DIVIDER);
327 p |= SET_FIELD(SB_P2_14, SB_P2_DIVIDER);
330 p |= SET_FIELD(SB_P2_7, SB_P2_DIVIDER);
333 DRM_ERROR("Bad P2 clock: %d\n", clock->p2);
336 ret = cdv_sb_write(dev, SB_P(pipe), p);
340 /* always Program the Lane Register for the Pipe A*/
341 /* if (pipe == 0) */ {
342 /* Program the Lane0/1 for HDMI B */
343 u32 lane_reg, lane_value;
345 lane_reg = PSB_LANE0;
346 cdv_sb_read(dev, lane_reg, &lane_value);
347 lane_value &= ~(LANE_PLL_MASK);
348 lane_value |= LANE_PLL_ENABLE;
349 cdv_sb_write(dev, lane_reg, lane_value);
351 lane_reg = PSB_LANE1;
352 cdv_sb_read(dev, lane_reg, &lane_value);
353 lane_value &= ~(LANE_PLL_MASK);
354 lane_value |= LANE_PLL_ENABLE;
355 cdv_sb_write(dev, lane_reg, lane_value);
357 /* Program the Lane2/3 for HDMI C */
358 lane_reg = PSB_LANE2;
359 cdv_sb_read(dev, lane_reg, &lane_value);
360 lane_value &= ~(LANE_PLL_MASK);
361 lane_value |= LANE_PLL_ENABLE;
362 cdv_sb_write(dev, lane_reg, lane_value);
364 lane_reg = PSB_LANE3;
365 cdv_sb_read(dev, lane_reg, &lane_value);
366 lane_value &= ~(LANE_PLL_MASK);
367 lane_value |= LANE_PLL_ENABLE;
368 cdv_sb_write(dev, lane_reg, lane_value);
375 * Returns whether any encoder on the specified pipe is of the specified type
377 static bool cdv_intel_pipe_has_type(struct drm_crtc *crtc, int type)
379 struct drm_device *dev = crtc->dev;
380 struct drm_mode_config *mode_config = &dev->mode_config;
381 struct drm_connector *l_entry;
383 list_for_each_entry(l_entry, &mode_config->connector_list, head) {
384 if (l_entry->encoder && l_entry->encoder->crtc == crtc) {
385 struct psb_intel_encoder *psb_intel_encoder =
386 psb_intel_attached_encoder(l_entry);
387 if (psb_intel_encoder->type == type)
394 static const struct cdv_intel_limit_t *cdv_intel_limit(struct drm_crtc *crtc,
397 const struct cdv_intel_limit_t *limit;
398 if (cdv_intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
400 * Now only single-channel LVDS is supported on CDV. If it is
401 * incorrect, please add the dual-channel LVDS.
404 limit = &cdv_intel_limits[CDV_LIMIT_SINGLE_LVDS_96];
406 limit = &cdv_intel_limits[CDV_LIMIT_SINGLE_LVDS_100];
409 limit = &cdv_intel_limits[CDV_LIMIT_DAC_HDMI_27];
411 limit = &cdv_intel_limits[CDV_LIMIT_DAC_HDMI_96];
416 /* m1 is reserved as 0 in CDV, n is a ring counter */
417 static void cdv_intel_clock(struct drm_device *dev,
418 int refclk, struct cdv_intel_clock_t *clock)
420 clock->m = clock->m2 + 2;
421 clock->p = clock->p1 * clock->p2;
422 clock->vco = (refclk * clock->m) / clock->n;
423 clock->dot = clock->vco / clock->p;
427 #define INTELPllInvalid(s) { /* ErrorF (s) */; return false; }
428 static bool cdv_intel_PLL_is_valid(struct drm_crtc *crtc,
429 const struct cdv_intel_limit_t *limit,
430 struct cdv_intel_clock_t *clock)
432 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
433 INTELPllInvalid("p1 out of range\n");
434 if (clock->p < limit->p.min || limit->p.max < clock->p)
435 INTELPllInvalid("p out of range\n");
436 /* unnecessary to check the range of m(m1/M2)/n again */
437 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
438 INTELPllInvalid("vco out of range\n");
439 /* XXX: We may need to be checking "Dot clock"
440 * depending on the multiplier, connector, etc.,
441 * rather than just a single range.
443 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
444 INTELPllInvalid("dot out of range\n");
449 static bool cdv_intel_find_best_PLL(struct drm_crtc *crtc, int target,
451 struct cdv_intel_clock_t *best_clock)
453 struct drm_device *dev = crtc->dev;
454 struct cdv_intel_clock_t clock;
455 const struct cdv_intel_limit_t *limit = cdv_intel_limit(crtc, refclk);
459 if (cdv_intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
460 (REG_READ(LVDS) & LVDS_PORT_EN) != 0) {
462 * For LVDS, if the panel is on, just rely on its current
463 * settings for dual-channel. We haven't figured out how to
464 * reliably set up different single/dual channel state, if we
467 if ((REG_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
469 clock.p2 = limit->p2.p2_fast;
471 clock.p2 = limit->p2.p2_slow;
473 if (target < limit->p2.dot_limit)
474 clock.p2 = limit->p2.p2_slow;
476 clock.p2 = limit->p2.p2_fast;
479 memset(best_clock, 0, sizeof(*best_clock));
481 /* m1 is reserved as 0 in CDV, n is a ring counter.
482 So skip the m1 loop */
483 for (clock.n = limit->n.min; clock.n <= limit->n.max; clock.n++) {
484 for (clock.m2 = limit->m2.min; clock.m2 <= limit->m2.max;
486 for (clock.p1 = limit->p1.min;
487 clock.p1 <= limit->p1.max;
491 cdv_intel_clock(dev, refclk, &clock);
493 if (!cdv_intel_PLL_is_valid(crtc,
497 this_err = abs(clock.dot - target);
498 if (this_err < err) {
506 return err != target;
509 static int cdv_intel_pipe_set_base(struct drm_crtc *crtc,
510 int x, int y, struct drm_framebuffer *old_fb)
512 struct drm_device *dev = crtc->dev;
513 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
514 struct psb_framebuffer *psbfb = to_psb_fb(crtc->fb);
515 int pipe = psb_intel_crtc->pipe;
516 unsigned long start, offset;
517 int dspbase = (pipe == 0 ? DSPABASE : DSPBBASE);
518 int dspsurf = (pipe == 0 ? DSPASURF : DSPBSURF);
519 int dspstride = (pipe == 0) ? DSPASTRIDE : DSPBSTRIDE;
520 int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
524 if (!gma_power_begin(dev, true))
529 dev_err(dev->dev, "No FB bound\n");
530 goto psb_intel_pipe_cleaner;
534 /* We are displaying this buffer, make sure it is actually loaded
536 ret = psb_gtt_pin(psbfb->gtt);
538 goto psb_intel_pipe_set_base_exit;
539 start = psbfb->gtt->offset;
540 offset = y * crtc->fb->pitches[0] + x * (crtc->fb->bits_per_pixel / 8);
542 REG_WRITE(dspstride, crtc->fb->pitches[0]);
544 dspcntr = REG_READ(dspcntr_reg);
545 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
547 switch (crtc->fb->bits_per_pixel) {
549 dspcntr |= DISPPLANE_8BPP;
552 if (crtc->fb->depth == 15)
553 dspcntr |= DISPPLANE_15_16BPP;
555 dspcntr |= DISPPLANE_16BPP;
559 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
562 dev_err(dev->dev, "Unknown color depth\n");
564 goto psb_intel_pipe_set_base_exit;
566 REG_WRITE(dspcntr_reg, dspcntr);
569 "Writing base %08lX %08lX %d %d\n", start, offset, x, y);
571 REG_WRITE(dspbase, offset);
573 REG_WRITE(dspsurf, start);
576 psb_intel_pipe_cleaner:
577 /* If there was a previous display we can now unpin it */
579 psb_gtt_unpin(to_psb_fb(old_fb)->gtt);
581 psb_intel_pipe_set_base_exit:
586 #define FIFO_PIPEA (1 << 0)
587 #define FIFO_PIPEB (1 << 1)
589 static bool cdv_intel_pipe_enabled(struct drm_device *dev, int pipe)
591 struct drm_crtc *crtc;
592 struct drm_psb_private *dev_priv = dev->dev_private;
593 struct psb_intel_crtc *psb_intel_crtc = NULL;
595 crtc = dev_priv->pipe_to_crtc_mapping[pipe];
596 psb_intel_crtc = to_psb_intel_crtc(crtc);
598 if (crtc->fb == NULL || !psb_intel_crtc->active)
603 static bool cdv_intel_single_pipe_active (struct drm_device *dev)
605 uint32_t pipe_enabled = 0;
607 if (cdv_intel_pipe_enabled(dev, 0))
608 pipe_enabled |= FIFO_PIPEA;
610 if (cdv_intel_pipe_enabled(dev, 1))
611 pipe_enabled |= FIFO_PIPEB;
614 DRM_DEBUG_KMS("pipe enabled %x\n", pipe_enabled);
616 if (pipe_enabled == FIFO_PIPEA || pipe_enabled == FIFO_PIPEB)
622 static bool is_pipeb_lvds(struct drm_device *dev, struct drm_crtc *crtc)
624 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
625 struct drm_mode_config *mode_config = &dev->mode_config;
626 struct drm_connector *connector;
628 if (psb_intel_crtc->pipe != 1)
631 list_for_each_entry(connector, &mode_config->connector_list, head) {
632 struct psb_intel_encoder *psb_intel_encoder =
633 psb_intel_attached_encoder(connector);
635 if (!connector->encoder
636 || connector->encoder->crtc != crtc)
639 if (psb_intel_encoder->type == INTEL_OUTPUT_LVDS)
646 static void cdv_intel_disable_self_refresh (struct drm_device *dev)
648 if (REG_READ(FW_BLC_SELF) & FW_BLC_SELF_EN) {
650 /* Disable self-refresh before adjust WM */
651 REG_WRITE(FW_BLC_SELF, (REG_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN));
652 REG_READ(FW_BLC_SELF);
654 cdv_intel_wait_for_vblank(dev);
656 /* Cedarview workaround to write ovelay plane, which force to leave
659 REG_WRITE(OV_OVADD, 0/*dev_priv->ovl_offset*/);
662 cdv_intel_wait_for_vblank(dev);
667 static void cdv_intel_update_watermark (struct drm_device *dev, struct drm_crtc *crtc)
670 if (cdv_intel_single_pipe_active(dev)) {
673 fw = REG_READ(DSPFW1);
674 fw &= ~DSP_FIFO_SR_WM_MASK;
675 fw |= (0x7e << DSP_FIFO_SR_WM_SHIFT);
676 fw &= ~CURSOR_B_FIFO_WM_MASK;
677 fw |= (0x4 << CURSOR_B_FIFO_WM_SHIFT);
678 REG_WRITE(DSPFW1, fw);
680 fw = REG_READ(DSPFW2);
681 fw &= ~CURSOR_A_FIFO_WM_MASK;
682 fw |= (0x6 << CURSOR_A_FIFO_WM_SHIFT);
683 fw &= ~DSP_PLANE_C_FIFO_WM_MASK;
684 fw |= (0x8 << DSP_PLANE_C_FIFO_WM_SHIFT);
685 REG_WRITE(DSPFW2, fw);
687 REG_WRITE(DSPFW3, 0x36000000);
691 if (is_pipeb_lvds(dev, crtc)) {
692 REG_WRITE(DSPFW5, 0x00040330);
694 fw = (3 << DSP_PLANE_B_FIFO_WM1_SHIFT) |
695 (4 << DSP_PLANE_A_FIFO_WM1_SHIFT) |
696 (3 << CURSOR_B_FIFO_WM1_SHIFT) |
697 (4 << CURSOR_FIFO_SR_WM1_SHIFT);
698 REG_WRITE(DSPFW5, fw);
701 REG_WRITE(DSPFW6, 0x10);
703 cdv_intel_wait_for_vblank(dev);
705 /* enable self-refresh for single pipe active */
706 REG_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
707 REG_READ(FW_BLC_SELF);
708 cdv_intel_wait_for_vblank(dev);
712 /* HW team suggested values... */
713 REG_WRITE(DSPFW1, 0x3f880808);
714 REG_WRITE(DSPFW2, 0x0b020202);
715 REG_WRITE(DSPFW3, 0x24000000);
716 REG_WRITE(DSPFW4, 0x08030202);
717 REG_WRITE(DSPFW5, 0x01010101);
718 REG_WRITE(DSPFW6, 0x1d0);
720 cdv_intel_wait_for_vblank(dev);
722 cdv_intel_disable_self_refresh(dev);
727 /** Loads the palette/gamma unit for the CRTC with the prepared values */
728 static void cdv_intel_crtc_load_lut(struct drm_crtc *crtc)
730 struct drm_device *dev = crtc->dev;
731 struct drm_psb_private *dev_priv =
732 (struct drm_psb_private *)dev->dev_private;
733 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
734 int palreg = PALETTE_A;
737 /* The clocks have to be on to load the palette. */
741 switch (psb_intel_crtc->pipe) {
751 dev_err(dev->dev, "Illegal Pipe Number.\n");
755 if (gma_power_begin(dev, false)) {
756 for (i = 0; i < 256; i++) {
757 REG_WRITE(palreg + 4 * i,
758 ((psb_intel_crtc->lut_r[i] +
759 psb_intel_crtc->lut_adj[i]) << 16) |
760 ((psb_intel_crtc->lut_g[i] +
761 psb_intel_crtc->lut_adj[i]) << 8) |
762 (psb_intel_crtc->lut_b[i] +
763 psb_intel_crtc->lut_adj[i]));
767 for (i = 0; i < 256; i++) {
768 dev_priv->regs.psb.save_palette_a[i] =
769 ((psb_intel_crtc->lut_r[i] +
770 psb_intel_crtc->lut_adj[i]) << 16) |
771 ((psb_intel_crtc->lut_g[i] +
772 psb_intel_crtc->lut_adj[i]) << 8) |
773 (psb_intel_crtc->lut_b[i] +
774 psb_intel_crtc->lut_adj[i]);
781 * Sets the power management mode of the pipe and plane.
783 * This code should probably grow support for turning the cursor off and back
784 * on appropriately at the same time as we're turning the pipe off/on.
786 static void cdv_intel_crtc_dpms(struct drm_crtc *crtc, int mode)
788 struct drm_device *dev = crtc->dev;
789 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
790 int pipe = psb_intel_crtc->pipe;
791 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
792 int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
793 int dspbase_reg = (pipe == 0) ? DSPABASE : DSPBBASE;
794 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
795 int pipestat_reg = (pipe == 0) ? PIPEASTAT : PIPEBSTAT;
798 /* XXX: When our outputs are all unaware of DPMS modes other than off
799 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
801 cdv_intel_disable_self_refresh(dev);
804 case DRM_MODE_DPMS_ON:
805 case DRM_MODE_DPMS_STANDBY:
806 case DRM_MODE_DPMS_SUSPEND:
807 if (psb_intel_crtc->active)
810 psb_intel_crtc->active = true;
812 /* Enable the DPLL */
813 temp = REG_READ(dpll_reg);
814 if ((temp & DPLL_VCO_ENABLE) == 0) {
815 REG_WRITE(dpll_reg, temp);
817 /* Wait for the clocks to stabilize. */
819 REG_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
821 /* Wait for the clocks to stabilize. */
823 REG_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
825 /* Wait for the clocks to stabilize. */
829 /* Jim Bish - switch plan and pipe per scott */
830 /* Enable the plane */
831 temp = REG_READ(dspcntr_reg);
832 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
833 REG_WRITE(dspcntr_reg,
834 temp | DISPLAY_PLANE_ENABLE);
835 /* Flush the plane changes */
836 REG_WRITE(dspbase_reg, REG_READ(dspbase_reg));
841 /* Enable the pipe */
842 temp = REG_READ(pipeconf_reg);
843 if ((temp & PIPEACONF_ENABLE) == 0)
844 REG_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
846 temp = REG_READ(pipestat_reg);
848 temp |= PIPE_FIFO_UNDERRUN;
849 REG_WRITE(pipestat_reg, temp);
850 REG_READ(pipestat_reg);
852 cdv_intel_update_watermark(dev, crtc);
853 cdv_intel_crtc_load_lut(crtc);
855 /* Give the overlay scaler a chance to enable
856 * if it's on this pipe */
857 /* psb_intel_crtc_dpms_video(crtc, true); TODO */
858 psb_intel_crtc->crtc_enable = true;
860 case DRM_MODE_DPMS_OFF:
861 if (!psb_intel_crtc->active)
864 psb_intel_crtc->active = false;
866 /* Give the overlay scaler a chance to disable
867 * if it's on this pipe */
868 /* psb_intel_crtc_dpms_video(crtc, FALSE); TODO */
870 /* Disable the VGA plane that we never use */
871 REG_WRITE(VGACNTRL, VGA_DISP_DISABLE);
873 /* Jim Bish - changed pipe/plane here as well. */
875 drm_vblank_off(dev, pipe);
876 /* Wait for vblank for the disable to take effect */
877 cdv_intel_wait_for_vblank(dev);
879 /* Next, disable display pipes */
880 temp = REG_READ(pipeconf_reg);
881 if ((temp & PIPEACONF_ENABLE) != 0) {
882 REG_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
883 REG_READ(pipeconf_reg);
886 /* Wait for vblank for the disable to take effect. */
887 cdv_intel_wait_for_vblank(dev);
891 /* Disable display plane */
892 temp = REG_READ(dspcntr_reg);
893 if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
894 REG_WRITE(dspcntr_reg,
895 temp & ~DISPLAY_PLANE_ENABLE);
896 /* Flush the plane changes */
897 REG_WRITE(dspbase_reg, REG_READ(dspbase_reg));
898 REG_READ(dspbase_reg);
901 temp = REG_READ(dpll_reg);
902 if ((temp & DPLL_VCO_ENABLE) != 0) {
903 REG_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
907 /* Wait for the clocks to turn off. */
909 cdv_intel_update_watermark(dev, crtc);
910 psb_intel_crtc->crtc_enable = false;
913 /*Set FIFO Watermarks*/
914 REG_WRITE(DSPARB, 0x3F3E);
917 static void cdv_intel_crtc_prepare(struct drm_crtc *crtc)
919 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
920 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
923 static void cdv_intel_crtc_commit(struct drm_crtc *crtc)
925 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
926 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
929 static bool cdv_intel_crtc_mode_fixup(struct drm_crtc *crtc,
930 struct drm_display_mode *mode,
931 struct drm_display_mode *adjusted_mode)
938 * Return the pipe currently connected to the panel fitter,
939 * or -1 if the panel fitter is not present or not in use
941 static int cdv_intel_panel_fitter_pipe(struct drm_device *dev)
945 pfit_control = REG_READ(PFIT_CONTROL);
947 /* See if the panel fitter is in use */
948 if ((pfit_control & PFIT_ENABLE) == 0)
950 return (pfit_control >> 29) & 0x3;
953 static int cdv_intel_crtc_mode_set(struct drm_crtc *crtc,
954 struct drm_display_mode *mode,
955 struct drm_display_mode *adjusted_mode,
957 struct drm_framebuffer *old_fb)
959 struct drm_device *dev = crtc->dev;
960 struct drm_psb_private *dev_priv = dev->dev_private;
961 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
962 int pipe = psb_intel_crtc->pipe;
963 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
964 int dpll_md_reg = (psb_intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
965 int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
966 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
967 int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
968 int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
969 int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
970 int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
971 int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
972 int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
973 int dspsize_reg = (pipe == 0) ? DSPASIZE : DSPBSIZE;
974 int dsppos_reg = (pipe == 0) ? DSPAPOS : DSPBPOS;
975 int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
977 struct cdv_intel_clock_t clock;
978 u32 dpll = 0, dspcntr, pipeconf;
980 bool is_crt = false, is_lvds = false, is_tv = false;
981 bool is_hdmi = false;
982 struct drm_mode_config *mode_config = &dev->mode_config;
983 struct drm_connector *connector;
985 list_for_each_entry(connector, &mode_config->connector_list, head) {
986 struct psb_intel_encoder *psb_intel_encoder =
987 psb_intel_attached_encoder(connector);
989 if (!connector->encoder
990 || connector->encoder->crtc != crtc)
993 switch (psb_intel_encoder->type) {
994 case INTEL_OUTPUT_LVDS:
997 case INTEL_OUTPUT_TVOUT:
1000 case INTEL_OUTPUT_ANALOG:
1003 case INTEL_OUTPUT_HDMI:
1009 if (dev_priv->dplla_96mhz)
1010 /* low-end sku, 96/100 mhz */
1013 /* high-end sku, 27/100 mhz */
1016 if (is_lvds && dev_priv->lvds_use_ssc) {
1017 refclk = dev_priv->lvds_ssc_freq * 1000;
1018 DRM_DEBUG_KMS("Use SSC reference clock %d Mhz\n", dev_priv->lvds_ssc_freq);
1021 drm_mode_debug_printmodeline(adjusted_mode);
1023 ok = cdv_intel_find_best_PLL(crtc, adjusted_mode->clock, refclk,
1026 dev_err(dev->dev, "Couldn't find PLL settings for mode!\n");
1030 dpll = DPLL_VGA_MODE_DIS;
1032 /* XXX: just matching BIOS for now */
1033 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
1036 /* dpll |= PLL_REF_INPUT_DREFCLK; */
1038 dpll |= DPLL_SYNCLOCK_ENABLE;
1040 dpll |= DPLLB_MODE_LVDS;
1042 dpll |= DPLLB_MODE_DAC_SERIAL; */
1043 /* dpll |= (2 << 11); */
1045 /* setup pipeconf */
1046 pipeconf = REG_READ(pipeconf_reg);
1048 /* Set up the display plane register */
1049 dspcntr = DISPPLANE_GAMMA_ENABLE;
1052 dspcntr |= DISPPLANE_SEL_PIPE_A;
1054 dspcntr |= DISPPLANE_SEL_PIPE_B;
1056 dspcntr |= DISPLAY_PLANE_ENABLE;
1057 pipeconf |= PIPEACONF_ENABLE;
1059 REG_WRITE(dpll_reg, dpll | DPLL_VGA_MODE_DIS | DPLL_SYNCLOCK_ENABLE);
1062 cdv_dpll_set_clock_cdv(dev, crtc, &clock, is_lvds);
1067 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
1068 * This is an exception to the general rule that mode_set doesn't turn
1072 u32 lvds = REG_READ(LVDS);
1075 LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP |
1077 /* Set the B0-B3 data pairs corresponding to
1078 * whether we're going to
1079 * set the DPLLs for dual-channel mode or not.
1082 lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
1084 lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
1086 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
1087 * appropriately here, but we need to look more
1088 * thoroughly into how panels behave in the two modes.
1091 REG_WRITE(LVDS, lvds);
1095 dpll |= DPLL_VCO_ENABLE;
1097 /* Disable the panel fitter if it was on our pipe */
1098 if (cdv_intel_panel_fitter_pipe(dev) == pipe)
1099 REG_WRITE(PFIT_CONTROL, 0);
1101 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
1102 drm_mode_debug_printmodeline(mode);
1105 (REG_READ(dpll_reg) & ~DPLL_LOCK) | DPLL_VCO_ENABLE);
1107 /* Wait for the clocks to stabilize. */
1108 udelay(150); /* 42 usec w/o calibration, 110 with. rounded up. */
1110 if (!(REG_READ(dpll_reg) & DPLL_LOCK)) {
1111 dev_err(dev->dev, "Failed to get DPLL lock\n");
1116 int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
1117 REG_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) | ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
1120 REG_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
1121 ((adjusted_mode->crtc_htotal - 1) << 16));
1122 REG_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
1123 ((adjusted_mode->crtc_hblank_end - 1) << 16));
1124 REG_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
1125 ((adjusted_mode->crtc_hsync_end - 1) << 16));
1126 REG_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
1127 ((adjusted_mode->crtc_vtotal - 1) << 16));
1128 REG_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
1129 ((adjusted_mode->crtc_vblank_end - 1) << 16));
1130 REG_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
1131 ((adjusted_mode->crtc_vsync_end - 1) << 16));
1132 /* pipesrc and dspsize control the size that is scaled from,
1133 * which should always be the user's requested size.
1135 REG_WRITE(dspsize_reg,
1136 ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
1137 REG_WRITE(dsppos_reg, 0);
1138 REG_WRITE(pipesrc_reg,
1139 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
1140 REG_WRITE(pipeconf_reg, pipeconf);
1141 REG_READ(pipeconf_reg);
1143 cdv_intel_wait_for_vblank(dev);
1145 REG_WRITE(dspcntr_reg, dspcntr);
1147 /* Flush the plane changes */
1149 struct drm_crtc_helper_funcs *crtc_funcs =
1150 crtc->helper_private;
1151 crtc_funcs->mode_set_base(crtc, x, y, old_fb);
1154 cdv_intel_wait_for_vblank(dev);
1161 * Save HW states of giving crtc
1163 static void cdv_intel_crtc_save(struct drm_crtc *crtc)
1165 struct drm_device *dev = crtc->dev;
1166 /* struct drm_psb_private *dev_priv =
1167 (struct drm_psb_private *)dev->dev_private; */
1168 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1169 struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
1170 int pipeA = (psb_intel_crtc->pipe == 0);
1171 uint32_t paletteReg;
1175 dev_dbg(dev->dev, "No CRTC state found\n");
1179 crtc_state->saveDSPCNTR = REG_READ(pipeA ? DSPACNTR : DSPBCNTR);
1180 crtc_state->savePIPECONF = REG_READ(pipeA ? PIPEACONF : PIPEBCONF);
1181 crtc_state->savePIPESRC = REG_READ(pipeA ? PIPEASRC : PIPEBSRC);
1182 crtc_state->saveFP0 = REG_READ(pipeA ? FPA0 : FPB0);
1183 crtc_state->saveFP1 = REG_READ(pipeA ? FPA1 : FPB1);
1184 crtc_state->saveDPLL = REG_READ(pipeA ? DPLL_A : DPLL_B);
1185 crtc_state->saveHTOTAL = REG_READ(pipeA ? HTOTAL_A : HTOTAL_B);
1186 crtc_state->saveHBLANK = REG_READ(pipeA ? HBLANK_A : HBLANK_B);
1187 crtc_state->saveHSYNC = REG_READ(pipeA ? HSYNC_A : HSYNC_B);
1188 crtc_state->saveVTOTAL = REG_READ(pipeA ? VTOTAL_A : VTOTAL_B);
1189 crtc_state->saveVBLANK = REG_READ(pipeA ? VBLANK_A : VBLANK_B);
1190 crtc_state->saveVSYNC = REG_READ(pipeA ? VSYNC_A : VSYNC_B);
1191 crtc_state->saveDSPSTRIDE = REG_READ(pipeA ? DSPASTRIDE : DSPBSTRIDE);
1193 /*NOTE: DSPSIZE DSPPOS only for psb*/
1194 crtc_state->saveDSPSIZE = REG_READ(pipeA ? DSPASIZE : DSPBSIZE);
1195 crtc_state->saveDSPPOS = REG_READ(pipeA ? DSPAPOS : DSPBPOS);
1197 crtc_state->saveDSPBASE = REG_READ(pipeA ? DSPABASE : DSPBBASE);
1199 DRM_DEBUG("(%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
1200 crtc_state->saveDSPCNTR,
1201 crtc_state->savePIPECONF,
1202 crtc_state->savePIPESRC,
1203 crtc_state->saveFP0,
1204 crtc_state->saveFP1,
1205 crtc_state->saveDPLL,
1206 crtc_state->saveHTOTAL,
1207 crtc_state->saveHBLANK,
1208 crtc_state->saveHSYNC,
1209 crtc_state->saveVTOTAL,
1210 crtc_state->saveVBLANK,
1211 crtc_state->saveVSYNC,
1212 crtc_state->saveDSPSTRIDE,
1213 crtc_state->saveDSPSIZE,
1214 crtc_state->saveDSPPOS,
1215 crtc_state->saveDSPBASE
1218 paletteReg = pipeA ? PALETTE_A : PALETTE_B;
1219 for (i = 0; i < 256; ++i)
1220 crtc_state->savePalette[i] = REG_READ(paletteReg + (i << 2));
1224 * Restore HW states of giving crtc
1226 static void cdv_intel_crtc_restore(struct drm_crtc *crtc)
1228 struct drm_device *dev = crtc->dev;
1229 /* struct drm_psb_private * dev_priv =
1230 (struct drm_psb_private *)dev->dev_private; */
1231 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1232 struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
1233 /* struct drm_crtc_helper_funcs * crtc_funcs = crtc->helper_private; */
1234 int pipeA = (psb_intel_crtc->pipe == 0);
1235 uint32_t paletteReg;
1239 dev_dbg(dev->dev, "No crtc state\n");
1244 "current:(%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
1245 REG_READ(pipeA ? DSPACNTR : DSPBCNTR),
1246 REG_READ(pipeA ? PIPEACONF : PIPEBCONF),
1247 REG_READ(pipeA ? PIPEASRC : PIPEBSRC),
1248 REG_READ(pipeA ? FPA0 : FPB0),
1249 REG_READ(pipeA ? FPA1 : FPB1),
1250 REG_READ(pipeA ? DPLL_A : DPLL_B),
1251 REG_READ(pipeA ? HTOTAL_A : HTOTAL_B),
1252 REG_READ(pipeA ? HBLANK_A : HBLANK_B),
1253 REG_READ(pipeA ? HSYNC_A : HSYNC_B),
1254 REG_READ(pipeA ? VTOTAL_A : VTOTAL_B),
1255 REG_READ(pipeA ? VBLANK_A : VBLANK_B),
1256 REG_READ(pipeA ? VSYNC_A : VSYNC_B),
1257 REG_READ(pipeA ? DSPASTRIDE : DSPBSTRIDE),
1258 REG_READ(pipeA ? DSPASIZE : DSPBSIZE),
1259 REG_READ(pipeA ? DSPAPOS : DSPBPOS),
1260 REG_READ(pipeA ? DSPABASE : DSPBBASE)
1264 "saved: (%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
1265 crtc_state->saveDSPCNTR,
1266 crtc_state->savePIPECONF,
1267 crtc_state->savePIPESRC,
1268 crtc_state->saveFP0,
1269 crtc_state->saveFP1,
1270 crtc_state->saveDPLL,
1271 crtc_state->saveHTOTAL,
1272 crtc_state->saveHBLANK,
1273 crtc_state->saveHSYNC,
1274 crtc_state->saveVTOTAL,
1275 crtc_state->saveVBLANK,
1276 crtc_state->saveVSYNC,
1277 crtc_state->saveDSPSTRIDE,
1278 crtc_state->saveDSPSIZE,
1279 crtc_state->saveDSPPOS,
1280 crtc_state->saveDSPBASE
1284 if (crtc_state->saveDPLL & DPLL_VCO_ENABLE) {
1285 REG_WRITE(pipeA ? DPLL_A : DPLL_B,
1286 crtc_state->saveDPLL & ~DPLL_VCO_ENABLE);
1287 REG_READ(pipeA ? DPLL_A : DPLL_B);
1288 DRM_DEBUG("write dpll: %x\n",
1289 REG_READ(pipeA ? DPLL_A : DPLL_B));
1293 REG_WRITE(pipeA ? FPA0 : FPB0, crtc_state->saveFP0);
1294 REG_READ(pipeA ? FPA0 : FPB0);
1296 REG_WRITE(pipeA ? FPA1 : FPB1, crtc_state->saveFP1);
1297 REG_READ(pipeA ? FPA1 : FPB1);
1299 REG_WRITE(pipeA ? DPLL_A : DPLL_B, crtc_state->saveDPLL);
1300 REG_READ(pipeA ? DPLL_A : DPLL_B);
1303 REG_WRITE(pipeA ? HTOTAL_A : HTOTAL_B, crtc_state->saveHTOTAL);
1304 REG_WRITE(pipeA ? HBLANK_A : HBLANK_B, crtc_state->saveHBLANK);
1305 REG_WRITE(pipeA ? HSYNC_A : HSYNC_B, crtc_state->saveHSYNC);
1306 REG_WRITE(pipeA ? VTOTAL_A : VTOTAL_B, crtc_state->saveVTOTAL);
1307 REG_WRITE(pipeA ? VBLANK_A : VBLANK_B, crtc_state->saveVBLANK);
1308 REG_WRITE(pipeA ? VSYNC_A : VSYNC_B, crtc_state->saveVSYNC);
1309 REG_WRITE(pipeA ? DSPASTRIDE : DSPBSTRIDE, crtc_state->saveDSPSTRIDE);
1311 REG_WRITE(pipeA ? DSPASIZE : DSPBSIZE, crtc_state->saveDSPSIZE);
1312 REG_WRITE(pipeA ? DSPAPOS : DSPBPOS, crtc_state->saveDSPPOS);
1314 REG_WRITE(pipeA ? PIPEASRC : PIPEBSRC, crtc_state->savePIPESRC);
1315 REG_WRITE(pipeA ? DSPABASE : DSPBBASE, crtc_state->saveDSPBASE);
1316 REG_WRITE(pipeA ? PIPEACONF : PIPEBCONF, crtc_state->savePIPECONF);
1318 cdv_intel_wait_for_vblank(dev);
1320 REG_WRITE(pipeA ? DSPACNTR : DSPBCNTR, crtc_state->saveDSPCNTR);
1321 REG_WRITE(pipeA ? DSPABASE : DSPBBASE, crtc_state->saveDSPBASE);
1323 cdv_intel_wait_for_vblank(dev);
1325 paletteReg = pipeA ? PALETTE_A : PALETTE_B;
1326 for (i = 0; i < 256; ++i)
1327 REG_WRITE(paletteReg + (i << 2), crtc_state->savePalette[i]);
1330 static int cdv_intel_crtc_cursor_set(struct drm_crtc *crtc,
1331 struct drm_file *file_priv,
1333 uint32_t width, uint32_t height)
1335 struct drm_device *dev = crtc->dev;
1336 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1337 int pipe = psb_intel_crtc->pipe;
1338 uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
1339 uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
1342 struct gtt_range *gt;
1343 struct drm_gem_object *obj;
1346 /* if we want to turn of the cursor ignore width and height */
1348 /* turn off the cursor */
1349 temp = CURSOR_MODE_DISABLE;
1351 if (gma_power_begin(dev, false)) {
1352 REG_WRITE(control, temp);
1357 /* unpin the old GEM object */
1358 if (psb_intel_crtc->cursor_obj) {
1359 gt = container_of(psb_intel_crtc->cursor_obj,
1360 struct gtt_range, gem);
1362 drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
1363 psb_intel_crtc->cursor_obj = NULL;
1369 /* Currently we only support 64x64 cursors */
1370 if (width != 64 || height != 64) {
1371 dev_dbg(dev->dev, "we currently only support 64x64 cursors\n");
1375 obj = drm_gem_object_lookup(dev, file_priv, handle);
1379 if (obj->size < width * height * 4) {
1380 dev_dbg(dev->dev, "buffer is to small\n");
1384 gt = container_of(obj, struct gtt_range, gem);
1386 /* Pin the memory into the GTT */
1387 ret = psb_gtt_pin(gt);
1389 dev_err(dev->dev, "Can not pin down handle 0x%x\n", handle);
1393 addr = gt->offset; /* Or resource.start ??? */
1395 psb_intel_crtc->cursor_addr = addr;
1398 /* set the pipe for the cursor */
1399 temp |= (pipe << 28);
1400 temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
1402 if (gma_power_begin(dev, false)) {
1403 REG_WRITE(control, temp);
1404 REG_WRITE(base, addr);
1408 /* unpin the old GEM object */
1409 if (psb_intel_crtc->cursor_obj) {
1410 gt = container_of(psb_intel_crtc->cursor_obj,
1411 struct gtt_range, gem);
1413 drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
1414 psb_intel_crtc->cursor_obj = obj;
1419 static int cdv_intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
1421 struct drm_device *dev = crtc->dev;
1422 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1423 int pipe = psb_intel_crtc->pipe;
1429 temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
1433 temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
1437 temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
1438 temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
1440 adder = psb_intel_crtc->cursor_addr;
1442 if (gma_power_begin(dev, false)) {
1443 REG_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
1444 REG_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
1450 static void cdv_intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red,
1451 u16 *green, u16 *blue, uint32_t start, uint32_t size)
1453 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1455 int end = (start + size > 256) ? 256 : start + size;
1457 for (i = start; i < end; i++) {
1458 psb_intel_crtc->lut_r[i] = red[i] >> 8;
1459 psb_intel_crtc->lut_g[i] = green[i] >> 8;
1460 psb_intel_crtc->lut_b[i] = blue[i] >> 8;
1463 cdv_intel_crtc_load_lut(crtc);
1466 static int cdv_crtc_set_config(struct drm_mode_set *set)
1469 struct drm_device *dev = set->crtc->dev;
1470 struct drm_psb_private *dev_priv = dev->dev_private;
1472 if (!dev_priv->rpm_enabled)
1473 return drm_crtc_helper_set_config(set);
1475 pm_runtime_forbid(&dev->pdev->dev);
1477 ret = drm_crtc_helper_set_config(set);
1479 pm_runtime_allow(&dev->pdev->dev);
1484 /** Derive the pixel clock for the given refclk and divisors for 8xx chips. */
1486 /* FIXME: why are we using this, should it be cdv_ in this tree ? */
1488 static void i8xx_clock(int refclk, struct cdv_intel_clock_t *clock)
1490 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
1491 clock->p = clock->p1 * clock->p2;
1492 clock->vco = refclk * clock->m / (clock->n + 2);
1493 clock->dot = clock->vco / clock->p;
1496 /* Returns the clock of the currently programmed mode of the given pipe. */
1497 static int cdv_intel_crtc_clock_get(struct drm_device *dev,
1498 struct drm_crtc *crtc)
1500 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1501 int pipe = psb_intel_crtc->pipe;
1504 struct cdv_intel_clock_t clock;
1506 struct drm_psb_private *dev_priv = dev->dev_private;
1508 if (gma_power_begin(dev, false)) {
1509 dpll = REG_READ((pipe == 0) ? DPLL_A : DPLL_B);
1510 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
1511 fp = REG_READ((pipe == 0) ? FPA0 : FPB0);
1513 fp = REG_READ((pipe == 0) ? FPA1 : FPB1);
1514 is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
1517 dpll = (pipe == 0) ?
1518 dev_priv->regs.psb.saveDPLL_A :
1519 dev_priv->regs.psb.saveDPLL_B;
1521 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
1523 dev_priv->regs.psb.saveFPA0 :
1524 dev_priv->regs.psb.saveFPB0;
1527 dev_priv->regs.psb.saveFPA1 :
1528 dev_priv->regs.psb.saveFPB1;
1530 is_lvds = (pipe == 1) &&
1531 (dev_priv->regs.psb.saveLVDS & LVDS_PORT_EN);
1534 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
1535 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
1536 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
1541 DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
1542 DPLL_FPA01_P1_POST_DIV_SHIFT);
1543 if (clock.p1 == 0) {
1545 dev_err(dev->dev, "PLL %d\n", dpll);
1549 if ((dpll & PLL_REF_INPUT_MASK) ==
1550 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
1551 /* XXX: might not be 66MHz */
1552 i8xx_clock(66000, &clock);
1554 i8xx_clock(48000, &clock);
1556 if (dpll & PLL_P1_DIVIDE_BY_TWO)
1561 DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
1562 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
1564 if (dpll & PLL_P2_DIVIDE_BY_4)
1569 i8xx_clock(48000, &clock);
1572 /* XXX: It would be nice to validate the clocks, but we can't reuse
1573 * i830PllIsValid() because it relies on the xf86_config connector
1574 * configuration being accurate, which it isn't necessarily.
1580 /** Returns the currently programmed mode of the given pipe. */
1581 struct drm_display_mode *cdv_intel_crtc_mode_get(struct drm_device *dev,
1582 struct drm_crtc *crtc)
1584 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1585 int pipe = psb_intel_crtc->pipe;
1586 struct drm_display_mode *mode;
1591 struct drm_psb_private *dev_priv = dev->dev_private;
1593 if (gma_power_begin(dev, false)) {
1594 htot = REG_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
1595 hsync = REG_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
1596 vtot = REG_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
1597 vsync = REG_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
1600 htot = (pipe == 0) ?
1601 dev_priv->regs.psb.saveHTOTAL_A :
1602 dev_priv->regs.psb.saveHTOTAL_B;
1603 hsync = (pipe == 0) ?
1604 dev_priv->regs.psb.saveHSYNC_A :
1605 dev_priv->regs.psb.saveHSYNC_B;
1606 vtot = (pipe == 0) ?
1607 dev_priv->regs.psb.saveVTOTAL_A :
1608 dev_priv->regs.psb.saveVTOTAL_B;
1609 vsync = (pipe == 0) ?
1610 dev_priv->regs.psb.saveVSYNC_A :
1611 dev_priv->regs.psb.saveVSYNC_B;
1614 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
1618 mode->clock = cdv_intel_crtc_clock_get(dev, crtc);
1619 mode->hdisplay = (htot & 0xffff) + 1;
1620 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
1621 mode->hsync_start = (hsync & 0xffff) + 1;
1622 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
1623 mode->vdisplay = (vtot & 0xffff) + 1;
1624 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
1625 mode->vsync_start = (vsync & 0xffff) + 1;
1626 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
1628 drm_mode_set_name(mode);
1629 drm_mode_set_crtcinfo(mode, 0);
1634 static void cdv_intel_crtc_destroy(struct drm_crtc *crtc)
1636 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1638 kfree(psb_intel_crtc->crtc_state);
1639 drm_crtc_cleanup(crtc);
1640 kfree(psb_intel_crtc);
1643 const struct drm_crtc_helper_funcs cdv_intel_helper_funcs = {
1644 .dpms = cdv_intel_crtc_dpms,
1645 .mode_fixup = cdv_intel_crtc_mode_fixup,
1646 .mode_set = cdv_intel_crtc_mode_set,
1647 .mode_set_base = cdv_intel_pipe_set_base,
1648 .prepare = cdv_intel_crtc_prepare,
1649 .commit = cdv_intel_crtc_commit,
1652 const struct drm_crtc_funcs cdv_intel_crtc_funcs = {
1653 .save = cdv_intel_crtc_save,
1654 .restore = cdv_intel_crtc_restore,
1655 .cursor_set = cdv_intel_crtc_cursor_set,
1656 .cursor_move = cdv_intel_crtc_cursor_move,
1657 .gamma_set = cdv_intel_crtc_gamma_set,
1658 .set_config = cdv_crtc_set_config,
1659 .destroy = cdv_intel_crtc_destroy,