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 "gma_display.h"
32 #define INTEL_LIMIT_I9XX_SDVO_DAC 0
33 #define INTEL_LIMIT_I9XX_LVDS 1
35 static const struct gma_limit_t psb_intel_limits[] = {
36 { /* INTEL_LIMIT_I9XX_SDVO_DAC */
37 .dot = {.min = 20000, .max = 400000},
38 .vco = {.min = 1400000, .max = 2800000},
39 .n = {.min = 1, .max = 6},
40 .m = {.min = 70, .max = 120},
41 .m1 = {.min = 8, .max = 18},
42 .m2 = {.min = 3, .max = 7},
43 .p = {.min = 5, .max = 80},
44 .p1 = {.min = 1, .max = 8},
45 .p2 = {.dot_limit = 200000, .p2_slow = 10, .p2_fast = 5},
46 .find_pll = gma_find_best_pll,
48 { /* INTEL_LIMIT_I9XX_LVDS */
49 .dot = {.min = 20000, .max = 400000},
50 .vco = {.min = 1400000, .max = 2800000},
51 .n = {.min = 1, .max = 6},
52 .m = {.min = 70, .max = 120},
53 .m1 = {.min = 8, .max = 18},
54 .m2 = {.min = 3, .max = 7},
55 .p = {.min = 7, .max = 98},
56 .p1 = {.min = 1, .max = 8},
57 /* The single-channel range is 25-112Mhz, and dual-channel
58 * is 80-224Mhz. Prefer single channel as much as possible.
60 .p2 = {.dot_limit = 112000, .p2_slow = 14, .p2_fast = 7},
61 .find_pll = gma_find_best_pll,
65 static const struct gma_limit_t *psb_intel_limit(struct drm_crtc *crtc,
68 const struct gma_limit_t *limit;
70 if (gma_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
71 limit = &psb_intel_limits[INTEL_LIMIT_I9XX_LVDS];
73 limit = &psb_intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC];
77 static void psb_intel_clock(int refclk, struct gma_clock_t *clock)
79 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
80 clock->p = clock->p1 * clock->p2;
81 clock->vco = refclk * clock->m / (clock->n + 2);
82 clock->dot = clock->vco / clock->p;
85 static int psb_intel_pipe_set_base(struct drm_crtc *crtc,
86 int x, int y, struct drm_framebuffer *old_fb)
88 struct drm_device *dev = crtc->dev;
89 struct drm_psb_private *dev_priv = dev->dev_private;
90 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
91 struct psb_framebuffer *psbfb = to_psb_fb(crtc->fb);
92 int pipe = psb_intel_crtc->pipe;
93 const struct psb_offset *map = &dev_priv->regmap[pipe];
94 unsigned long start, offset;
98 if (!gma_power_begin(dev, true))
103 dev_dbg(dev->dev, "No FB bound\n");
104 goto psb_intel_pipe_cleaner;
107 /* We are displaying this buffer, make sure it is actually loaded
109 ret = psb_gtt_pin(psbfb->gtt);
111 goto psb_intel_pipe_set_base_exit;
112 start = psbfb->gtt->offset;
114 offset = y * crtc->fb->pitches[0] + x * (crtc->fb->bits_per_pixel / 8);
116 REG_WRITE(map->stride, crtc->fb->pitches[0]);
118 dspcntr = REG_READ(map->cntr);
119 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
121 switch (crtc->fb->bits_per_pixel) {
123 dspcntr |= DISPPLANE_8BPP;
126 if (crtc->fb->depth == 15)
127 dspcntr |= DISPPLANE_15_16BPP;
129 dspcntr |= DISPPLANE_16BPP;
133 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
136 dev_err(dev->dev, "Unknown color depth\n");
138 psb_gtt_unpin(psbfb->gtt);
139 goto psb_intel_pipe_set_base_exit;
141 REG_WRITE(map->cntr, dspcntr);
143 REG_WRITE(map->base, start + offset);
146 psb_intel_pipe_cleaner:
147 /* If there was a previous display we can now unpin it */
149 psb_gtt_unpin(to_psb_fb(old_fb)->gtt);
151 psb_intel_pipe_set_base_exit:
157 * Sets the power management mode of the pipe and plane.
159 * This code should probably grow support for turning the cursor off and back
160 * on appropriately at the same time as we're turning the pipe off/on.
162 static void psb_intel_crtc_dpms(struct drm_crtc *crtc, int mode)
164 struct drm_device *dev = crtc->dev;
165 struct drm_psb_private *dev_priv = dev->dev_private;
166 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
167 int pipe = psb_intel_crtc->pipe;
168 const struct psb_offset *map = &dev_priv->regmap[pipe];
171 /* XXX: When our outputs are all unaware of DPMS modes other than off
172 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
175 case DRM_MODE_DPMS_ON:
176 case DRM_MODE_DPMS_STANDBY:
177 case DRM_MODE_DPMS_SUSPEND:
178 /* Enable the DPLL */
179 temp = REG_READ(map->dpll);
180 if ((temp & DPLL_VCO_ENABLE) == 0) {
181 REG_WRITE(map->dpll, temp);
183 /* Wait for the clocks to stabilize. */
185 REG_WRITE(map->dpll, temp | DPLL_VCO_ENABLE);
187 /* Wait for the clocks to stabilize. */
189 REG_WRITE(map->dpll, temp | DPLL_VCO_ENABLE);
191 /* Wait for the clocks to stabilize. */
195 /* Enable the pipe */
196 temp = REG_READ(map->conf);
197 if ((temp & PIPEACONF_ENABLE) == 0)
198 REG_WRITE(map->conf, temp | PIPEACONF_ENABLE);
200 /* Enable the plane */
201 temp = REG_READ(map->cntr);
202 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
204 temp | DISPLAY_PLANE_ENABLE);
205 /* Flush the plane changes */
206 REG_WRITE(map->base, REG_READ(map->base));
209 psb_intel_crtc_load_lut(crtc);
211 /* Give the overlay scaler a chance to enable
212 * if it's on this pipe */
213 /* psb_intel_crtc_dpms_video(crtc, true); TODO */
215 case DRM_MODE_DPMS_OFF:
216 /* Give the overlay scaler a chance to disable
217 * if it's on this pipe */
218 /* psb_intel_crtc_dpms_video(crtc, FALSE); TODO */
220 /* Disable the VGA plane that we never use */
221 REG_WRITE(VGACNTRL, VGA_DISP_DISABLE);
223 /* Disable display plane */
224 temp = REG_READ(map->cntr);
225 if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
227 temp & ~DISPLAY_PLANE_ENABLE);
228 /* Flush the plane changes */
229 REG_WRITE(map->base, REG_READ(map->base));
233 /* Next, disable display pipes */
234 temp = REG_READ(map->conf);
235 if ((temp & PIPEACONF_ENABLE) != 0) {
236 REG_WRITE(map->conf, temp & ~PIPEACONF_ENABLE);
240 /* Wait for vblank for the disable to take effect. */
241 gma_wait_for_vblank(dev);
243 temp = REG_READ(map->dpll);
244 if ((temp & DPLL_VCO_ENABLE) != 0) {
245 REG_WRITE(map->dpll, temp & ~DPLL_VCO_ENABLE);
249 /* Wait for the clocks to turn off. */
254 /*Set FIFO Watermarks*/
255 REG_WRITE(DSPARB, 0x3F3E);
258 static void psb_intel_crtc_prepare(struct drm_crtc *crtc)
260 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
261 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
264 static void psb_intel_crtc_commit(struct drm_crtc *crtc)
266 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
267 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
270 void psb_intel_encoder_prepare(struct drm_encoder *encoder)
272 struct drm_encoder_helper_funcs *encoder_funcs =
273 encoder->helper_private;
274 /* lvds has its own version of prepare see psb_intel_lvds_prepare */
275 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
278 void psb_intel_encoder_commit(struct drm_encoder *encoder)
280 struct drm_encoder_helper_funcs *encoder_funcs =
281 encoder->helper_private;
282 /* lvds has its own version of commit see psb_intel_lvds_commit */
283 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
286 void psb_intel_encoder_destroy(struct drm_encoder *encoder)
288 struct psb_intel_encoder *intel_encoder = to_psb_intel_encoder(encoder);
290 drm_encoder_cleanup(encoder);
291 kfree(intel_encoder);
294 static bool psb_intel_crtc_mode_fixup(struct drm_crtc *crtc,
295 const struct drm_display_mode *mode,
296 struct drm_display_mode *adjusted_mode)
303 * Return the pipe currently connected to the panel fitter,
304 * or -1 if the panel fitter is not present or not in use
306 static int psb_intel_panel_fitter_pipe(struct drm_device *dev)
310 pfit_control = REG_READ(PFIT_CONTROL);
312 /* See if the panel fitter is in use */
313 if ((pfit_control & PFIT_ENABLE) == 0)
315 /* Must be on PIPE 1 for PSB */
319 static int psb_intel_crtc_mode_set(struct drm_crtc *crtc,
320 struct drm_display_mode *mode,
321 struct drm_display_mode *adjusted_mode,
323 struct drm_framebuffer *old_fb)
325 struct drm_device *dev = crtc->dev;
326 struct drm_psb_private *dev_priv = dev->dev_private;
327 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
328 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
329 int pipe = psb_intel_crtc->pipe;
330 const struct psb_offset *map = &dev_priv->regmap[pipe];
332 struct gma_clock_t clock;
333 u32 dpll = 0, fp = 0, dspcntr, pipeconf;
334 bool ok, is_sdvo = false;
335 bool is_lvds = false, is_tv = false;
336 struct drm_mode_config *mode_config = &dev->mode_config;
337 struct drm_connector *connector;
338 const struct gma_limit_t *limit;
340 /* No scan out no play */
341 if (crtc->fb == NULL) {
342 crtc_funcs->mode_set_base(crtc, x, y, old_fb);
346 list_for_each_entry(connector, &mode_config->connector_list, head) {
347 struct psb_intel_encoder *psb_intel_encoder =
348 psb_intel_attached_encoder(connector);
350 if (!connector->encoder
351 || connector->encoder->crtc != crtc)
354 switch (psb_intel_encoder->type) {
355 case INTEL_OUTPUT_LVDS:
358 case INTEL_OUTPUT_SDVO:
361 case INTEL_OUTPUT_TVOUT:
369 limit = psb_intel_crtc->clock_funcs->limit(crtc, refclk);
371 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk,
374 DRM_ERROR("Couldn't find PLL settings for mode! target: %d, actual: %d",
375 adjusted_mode->clock, clock.dot);
379 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
381 dpll = DPLL_VGA_MODE_DIS;
383 dpll |= DPLLB_MODE_LVDS;
384 dpll |= DPLL_DVO_HIGH_SPEED;
386 dpll |= DPLLB_MODE_DAC_SERIAL;
388 int sdvo_pixel_multiply =
389 adjusted_mode->clock / mode->clock;
390 dpll |= DPLL_DVO_HIGH_SPEED;
392 (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
395 /* compute bitmask from p1 value */
396 dpll |= (1 << (clock.p1 - 1)) << 16;
399 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
402 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
405 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
408 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
413 /* XXX: just matching BIOS for now */
414 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
417 dpll |= PLL_REF_INPUT_DREFCLK;
420 pipeconf = REG_READ(map->conf);
422 /* Set up the display plane register */
423 dspcntr = DISPPLANE_GAMMA_ENABLE;
426 dspcntr |= DISPPLANE_SEL_PIPE_A;
428 dspcntr |= DISPPLANE_SEL_PIPE_B;
430 dspcntr |= DISPLAY_PLANE_ENABLE;
431 pipeconf |= PIPEACONF_ENABLE;
432 dpll |= DPLL_VCO_ENABLE;
435 /* Disable the panel fitter if it was on our pipe */
436 if (psb_intel_panel_fitter_pipe(dev) == pipe)
437 REG_WRITE(PFIT_CONTROL, 0);
439 drm_mode_debug_printmodeline(mode);
441 if (dpll & DPLL_VCO_ENABLE) {
442 REG_WRITE(map->fp0, fp);
443 REG_WRITE(map->dpll, dpll & ~DPLL_VCO_ENABLE);
448 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
449 * This is an exception to the general rule that mode_set doesn't turn
453 u32 lvds = REG_READ(LVDS);
455 lvds &= ~LVDS_PIPEB_SELECT;
457 lvds |= LVDS_PIPEB_SELECT;
459 lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
460 /* Set the B0-B3 data pairs corresponding to
461 * whether we're going to
462 * set the DPLLs for dual-channel mode or not.
464 lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
466 lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
468 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
469 * appropriately here, but we need to look more
470 * thoroughly into how panels behave in the two modes.
473 REG_WRITE(LVDS, lvds);
477 REG_WRITE(map->fp0, fp);
478 REG_WRITE(map->dpll, dpll);
480 /* Wait for the clocks to stabilize. */
483 /* write it again -- the BIOS does, after all */
484 REG_WRITE(map->dpll, dpll);
487 /* Wait for the clocks to stabilize. */
490 REG_WRITE(map->htotal, (adjusted_mode->crtc_hdisplay - 1) |
491 ((adjusted_mode->crtc_htotal - 1) << 16));
492 REG_WRITE(map->hblank, (adjusted_mode->crtc_hblank_start - 1) |
493 ((adjusted_mode->crtc_hblank_end - 1) << 16));
494 REG_WRITE(map->hsync, (adjusted_mode->crtc_hsync_start - 1) |
495 ((adjusted_mode->crtc_hsync_end - 1) << 16));
496 REG_WRITE(map->vtotal, (adjusted_mode->crtc_vdisplay - 1) |
497 ((adjusted_mode->crtc_vtotal - 1) << 16));
498 REG_WRITE(map->vblank, (adjusted_mode->crtc_vblank_start - 1) |
499 ((adjusted_mode->crtc_vblank_end - 1) << 16));
500 REG_WRITE(map->vsync, (adjusted_mode->crtc_vsync_start - 1) |
501 ((adjusted_mode->crtc_vsync_end - 1) << 16));
502 /* pipesrc and dspsize control the size that is scaled from,
503 * which should always be the user's requested size.
506 ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
507 REG_WRITE(map->pos, 0);
509 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
510 REG_WRITE(map->conf, pipeconf);
513 gma_wait_for_vblank(dev);
515 REG_WRITE(map->cntr, dspcntr);
517 /* Flush the plane changes */
518 crtc_funcs->mode_set_base(crtc, x, y, old_fb);
520 gma_wait_for_vblank(dev);
525 /** Loads the palette/gamma unit for the CRTC with the prepared values */
526 void psb_intel_crtc_load_lut(struct drm_crtc *crtc)
528 struct drm_device *dev = crtc->dev;
529 struct drm_psb_private *dev_priv = dev->dev_private;
530 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
531 const struct psb_offset *map = &dev_priv->regmap[psb_intel_crtc->pipe];
532 int palreg = map->palette;
535 /* The clocks have to be on to load the palette. */
539 switch (psb_intel_crtc->pipe) {
544 dev_err(dev->dev, "Illegal Pipe Number.\n");
548 if (gma_power_begin(dev, false)) {
549 for (i = 0; i < 256; i++) {
550 REG_WRITE(palreg + 4 * i,
551 ((psb_intel_crtc->lut_r[i] +
552 psb_intel_crtc->lut_adj[i]) << 16) |
553 ((psb_intel_crtc->lut_g[i] +
554 psb_intel_crtc->lut_adj[i]) << 8) |
555 (psb_intel_crtc->lut_b[i] +
556 psb_intel_crtc->lut_adj[i]));
560 for (i = 0; i < 256; i++) {
561 dev_priv->regs.pipe[0].palette[i] =
562 ((psb_intel_crtc->lut_r[i] +
563 psb_intel_crtc->lut_adj[i]) << 16) |
564 ((psb_intel_crtc->lut_g[i] +
565 psb_intel_crtc->lut_adj[i]) << 8) |
566 (psb_intel_crtc->lut_b[i] +
567 psb_intel_crtc->lut_adj[i]);
574 * Save HW states of giving crtc
576 static void psb_intel_crtc_save(struct drm_crtc *crtc)
578 struct drm_device *dev = crtc->dev;
579 struct drm_psb_private *dev_priv = dev->dev_private;
580 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
581 struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
582 const struct psb_offset *map = &dev_priv->regmap[psb_intel_crtc->pipe];
587 dev_err(dev->dev, "No CRTC state found\n");
591 crtc_state->saveDSPCNTR = REG_READ(map->cntr);
592 crtc_state->savePIPECONF = REG_READ(map->conf);
593 crtc_state->savePIPESRC = REG_READ(map->src);
594 crtc_state->saveFP0 = REG_READ(map->fp0);
595 crtc_state->saveFP1 = REG_READ(map->fp1);
596 crtc_state->saveDPLL = REG_READ(map->dpll);
597 crtc_state->saveHTOTAL = REG_READ(map->htotal);
598 crtc_state->saveHBLANK = REG_READ(map->hblank);
599 crtc_state->saveHSYNC = REG_READ(map->hsync);
600 crtc_state->saveVTOTAL = REG_READ(map->vtotal);
601 crtc_state->saveVBLANK = REG_READ(map->vblank);
602 crtc_state->saveVSYNC = REG_READ(map->vsync);
603 crtc_state->saveDSPSTRIDE = REG_READ(map->stride);
605 /*NOTE: DSPSIZE DSPPOS only for psb*/
606 crtc_state->saveDSPSIZE = REG_READ(map->size);
607 crtc_state->saveDSPPOS = REG_READ(map->pos);
609 crtc_state->saveDSPBASE = REG_READ(map->base);
611 paletteReg = map->palette;
612 for (i = 0; i < 256; ++i)
613 crtc_state->savePalette[i] = REG_READ(paletteReg + (i << 2));
617 * Restore HW states of giving crtc
619 static void psb_intel_crtc_restore(struct drm_crtc *crtc)
621 struct drm_device *dev = crtc->dev;
622 struct drm_psb_private *dev_priv = dev->dev_private;
623 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
624 struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
625 const struct psb_offset *map = &dev_priv->regmap[psb_intel_crtc->pipe];
630 dev_err(dev->dev, "No crtc state\n");
634 if (crtc_state->saveDPLL & DPLL_VCO_ENABLE) {
636 crtc_state->saveDPLL & ~DPLL_VCO_ENABLE);
641 REG_WRITE(map->fp0, crtc_state->saveFP0);
644 REG_WRITE(map->fp1, crtc_state->saveFP1);
647 REG_WRITE(map->dpll, crtc_state->saveDPLL);
651 REG_WRITE(map->htotal, crtc_state->saveHTOTAL);
652 REG_WRITE(map->hblank, crtc_state->saveHBLANK);
653 REG_WRITE(map->hsync, crtc_state->saveHSYNC);
654 REG_WRITE(map->vtotal, crtc_state->saveVTOTAL);
655 REG_WRITE(map->vblank, crtc_state->saveVBLANK);
656 REG_WRITE(map->vsync, crtc_state->saveVSYNC);
657 REG_WRITE(map->stride, crtc_state->saveDSPSTRIDE);
659 REG_WRITE(map->size, crtc_state->saveDSPSIZE);
660 REG_WRITE(map->pos, crtc_state->saveDSPPOS);
662 REG_WRITE(map->src, crtc_state->savePIPESRC);
663 REG_WRITE(map->base, crtc_state->saveDSPBASE);
664 REG_WRITE(map->conf, crtc_state->savePIPECONF);
666 gma_wait_for_vblank(dev);
668 REG_WRITE(map->cntr, crtc_state->saveDSPCNTR);
669 REG_WRITE(map->base, crtc_state->saveDSPBASE);
671 gma_wait_for_vblank(dev);
673 paletteReg = map->palette;
674 for (i = 0; i < 256; ++i)
675 REG_WRITE(paletteReg + (i << 2), crtc_state->savePalette[i]);
678 static int psb_intel_crtc_cursor_set(struct drm_crtc *crtc,
679 struct drm_file *file_priv,
681 uint32_t width, uint32_t height)
683 struct drm_device *dev = crtc->dev;
684 struct drm_psb_private *dev_priv = dev->dev_private;
685 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
686 int pipe = psb_intel_crtc->pipe;
687 uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
688 uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
691 struct gtt_range *gt;
692 struct gtt_range *cursor_gt = psb_intel_crtc->cursor_gt;
693 struct drm_gem_object *obj;
694 void *tmp_dst, *tmp_src;
695 int ret = 0, i, cursor_pages;
697 /* if we want to turn of the cursor ignore width and height */
699 /* turn off the cursor */
700 temp = CURSOR_MODE_DISABLE;
702 if (gma_power_begin(dev, false)) {
703 REG_WRITE(control, temp);
708 /* Unpin the old GEM object */
709 if (psb_intel_crtc->cursor_obj) {
710 gt = container_of(psb_intel_crtc->cursor_obj,
711 struct gtt_range, gem);
713 drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
714 psb_intel_crtc->cursor_obj = NULL;
720 /* Currently we only support 64x64 cursors */
721 if (width != 64 || height != 64) {
722 dev_dbg(dev->dev, "we currently only support 64x64 cursors\n");
726 obj = drm_gem_object_lookup(dev, file_priv, handle);
730 if (obj->size < width * height * 4) {
731 dev_dbg(dev->dev, "buffer is to small\n");
736 gt = container_of(obj, struct gtt_range, gem);
738 /* Pin the memory into the GTT */
739 ret = psb_gtt_pin(gt);
741 dev_err(dev->dev, "Can not pin down handle 0x%x\n", handle);
745 if (dev_priv->ops->cursor_needs_phys) {
746 if (cursor_gt == NULL) {
747 dev_err(dev->dev, "No hardware cursor mem available");
752 /* Prevent overflow */
756 cursor_pages = gt->npage;
758 /* Copy the cursor to cursor mem */
759 tmp_dst = dev_priv->vram_addr + cursor_gt->offset;
760 for (i = 0; i < cursor_pages; i++) {
761 tmp_src = kmap(gt->pages[i]);
762 memcpy(tmp_dst, tmp_src, PAGE_SIZE);
763 kunmap(gt->pages[i]);
764 tmp_dst += PAGE_SIZE;
767 addr = psb_intel_crtc->cursor_addr;
769 addr = gt->offset; /* Or resource.start ??? */
770 psb_intel_crtc->cursor_addr = addr;
774 /* set the pipe for the cursor */
775 temp |= (pipe << 28);
776 temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
778 if (gma_power_begin(dev, false)) {
779 REG_WRITE(control, temp);
780 REG_WRITE(base, addr);
784 /* unpin the old bo */
785 if (psb_intel_crtc->cursor_obj) {
786 gt = container_of(psb_intel_crtc->cursor_obj,
787 struct gtt_range, gem);
789 drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
792 psb_intel_crtc->cursor_obj = obj;
796 drm_gem_object_unreference(obj);
800 static int psb_intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
802 struct drm_device *dev = crtc->dev;
803 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
804 int pipe = psb_intel_crtc->pipe;
810 temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
814 temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
818 temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
819 temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
821 addr = psb_intel_crtc->cursor_addr;
823 if (gma_power_begin(dev, false)) {
824 REG_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
825 REG_WRITE((pipe == 0) ? CURABASE : CURBBASE, addr);
831 static void psb_intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red,
832 u16 *green, u16 *blue, uint32_t type, uint32_t size)
834 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
840 for (i = 0; i < 256; i++) {
841 psb_intel_crtc->lut_r[i] = red[i] >> 8;
842 psb_intel_crtc->lut_g[i] = green[i] >> 8;
843 psb_intel_crtc->lut_b[i] = blue[i] >> 8;
846 psb_intel_crtc_load_lut(crtc);
849 static int psb_crtc_set_config(struct drm_mode_set *set)
852 struct drm_device *dev = set->crtc->dev;
853 struct drm_psb_private *dev_priv = dev->dev_private;
855 if (!dev_priv->rpm_enabled)
856 return drm_crtc_helper_set_config(set);
858 pm_runtime_forbid(&dev->pdev->dev);
859 ret = drm_crtc_helper_set_config(set);
860 pm_runtime_allow(&dev->pdev->dev);
864 /* Returns the clock of the currently programmed mode of the given pipe. */
865 static int psb_intel_crtc_clock_get(struct drm_device *dev,
866 struct drm_crtc *crtc)
868 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
869 struct drm_psb_private *dev_priv = dev->dev_private;
870 int pipe = psb_intel_crtc->pipe;
871 const struct psb_offset *map = &dev_priv->regmap[pipe];
874 struct gma_clock_t clock;
876 struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
878 if (gma_power_begin(dev, false)) {
879 dpll = REG_READ(map->dpll);
880 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
881 fp = REG_READ(map->fp0);
883 fp = REG_READ(map->fp1);
884 is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
889 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
894 is_lvds = (pipe == 1) && (dev_priv->regs.psb.saveLVDS &
898 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
899 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
900 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
905 DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
906 DPLL_FPA01_P1_POST_DIV_SHIFT);
909 if ((dpll & PLL_REF_INPUT_MASK) ==
910 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
911 /* XXX: might not be 66MHz */
912 psb_intel_clock(66000, &clock);
914 psb_intel_clock(48000, &clock);
916 if (dpll & PLL_P1_DIVIDE_BY_TWO)
921 DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
922 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
924 if (dpll & PLL_P2_DIVIDE_BY_4)
929 psb_intel_clock(48000, &clock);
932 /* XXX: It would be nice to validate the clocks, but we can't reuse
933 * i830PllIsValid() because it relies on the xf86_config connector
934 * configuration being accurate, which it isn't necessarily.
940 /** Returns the currently programmed mode of the given pipe. */
941 struct drm_display_mode *psb_intel_crtc_mode_get(struct drm_device *dev,
942 struct drm_crtc *crtc)
944 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
945 int pipe = psb_intel_crtc->pipe;
946 struct drm_display_mode *mode;
951 struct drm_psb_private *dev_priv = dev->dev_private;
952 struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
953 const struct psb_offset *map = &dev_priv->regmap[pipe];
955 if (gma_power_begin(dev, false)) {
956 htot = REG_READ(map->htotal);
957 hsync = REG_READ(map->hsync);
958 vtot = REG_READ(map->vtotal);
959 vsync = REG_READ(map->vsync);
968 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
972 mode->clock = psb_intel_crtc_clock_get(dev, crtc);
973 mode->hdisplay = (htot & 0xffff) + 1;
974 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
975 mode->hsync_start = (hsync & 0xffff) + 1;
976 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
977 mode->vdisplay = (vtot & 0xffff) + 1;
978 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
979 mode->vsync_start = (vsync & 0xffff) + 1;
980 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
982 drm_mode_set_name(mode);
983 drm_mode_set_crtcinfo(mode, 0);
988 static void psb_intel_crtc_destroy(struct drm_crtc *crtc)
990 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
991 struct gtt_range *gt;
993 /* Unpin the old GEM object */
994 if (psb_intel_crtc->cursor_obj) {
995 gt = container_of(psb_intel_crtc->cursor_obj,
996 struct gtt_range, gem);
998 drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
999 psb_intel_crtc->cursor_obj = NULL;
1002 if (psb_intel_crtc->cursor_gt != NULL)
1003 psb_gtt_free_range(crtc->dev, psb_intel_crtc->cursor_gt);
1004 kfree(psb_intel_crtc->crtc_state);
1005 drm_crtc_cleanup(crtc);
1006 kfree(psb_intel_crtc);
1009 static void psb_intel_crtc_disable(struct drm_crtc *crtc)
1011 struct gtt_range *gt;
1012 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1014 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
1017 gt = to_psb_fb(crtc->fb)->gtt;
1022 const struct drm_crtc_helper_funcs psb_intel_helper_funcs = {
1023 .dpms = psb_intel_crtc_dpms,
1024 .mode_fixup = psb_intel_crtc_mode_fixup,
1025 .mode_set = psb_intel_crtc_mode_set,
1026 .mode_set_base = psb_intel_pipe_set_base,
1027 .prepare = psb_intel_crtc_prepare,
1028 .commit = psb_intel_crtc_commit,
1029 .disable = psb_intel_crtc_disable,
1032 const struct drm_crtc_funcs psb_intel_crtc_funcs = {
1033 .save = psb_intel_crtc_save,
1034 .restore = psb_intel_crtc_restore,
1035 .cursor_set = psb_intel_crtc_cursor_set,
1036 .cursor_move = psb_intel_crtc_cursor_move,
1037 .gamma_set = psb_intel_crtc_gamma_set,
1038 .set_config = psb_crtc_set_config,
1039 .destroy = psb_intel_crtc_destroy,
1042 const struct gma_clock_funcs psb_clock_funcs = {
1043 .clock = psb_intel_clock,
1044 .limit = psb_intel_limit,
1045 .pll_is_valid = gma_pll_is_valid,
1049 * Set the default value of cursor control and base register
1050 * to zero. This is a workaround for h/w defect on Oaktrail
1052 static void psb_intel_cursor_init(struct drm_device *dev,
1053 struct psb_intel_crtc *psb_intel_crtc)
1055 struct drm_psb_private *dev_priv = dev->dev_private;
1056 u32 control[3] = { CURACNTR, CURBCNTR, CURCCNTR };
1057 u32 base[3] = { CURABASE, CURBBASE, CURCBASE };
1058 struct gtt_range *cursor_gt;
1060 if (dev_priv->ops->cursor_needs_phys) {
1061 /* Allocate 4 pages of stolen mem for a hardware cursor. That
1062 * is enough for the 64 x 64 ARGB cursors we support.
1064 cursor_gt = psb_gtt_alloc_range(dev, 4 * PAGE_SIZE, "cursor", 1);
1066 psb_intel_crtc->cursor_gt = NULL;
1069 psb_intel_crtc->cursor_gt = cursor_gt;
1070 psb_intel_crtc->cursor_addr = dev_priv->stolen_base +
1073 psb_intel_crtc->cursor_gt = NULL;
1077 REG_WRITE(control[psb_intel_crtc->pipe], 0);
1078 REG_WRITE(base[psb_intel_crtc->pipe], 0);
1081 void psb_intel_crtc_init(struct drm_device *dev, int pipe,
1082 struct psb_intel_mode_device *mode_dev)
1084 struct drm_psb_private *dev_priv = dev->dev_private;
1085 struct psb_intel_crtc *psb_intel_crtc;
1087 uint16_t *r_base, *g_base, *b_base;
1089 /* We allocate a extra array of drm_connector pointers
1090 * for fbdev after the crtc */
1092 kzalloc(sizeof(struct psb_intel_crtc) +
1093 (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)),
1095 if (psb_intel_crtc == NULL)
1098 psb_intel_crtc->crtc_state =
1099 kzalloc(sizeof(struct psb_intel_crtc_state), GFP_KERNEL);
1100 if (!psb_intel_crtc->crtc_state) {
1101 dev_err(dev->dev, "Crtc state error: No memory\n");
1102 kfree(psb_intel_crtc);
1106 /* Set the CRTC operations from the chip specific data */
1107 drm_crtc_init(dev, &psb_intel_crtc->base, dev_priv->ops->crtc_funcs);
1109 /* Set the CRTC clock functions from chip specific data */
1110 psb_intel_crtc->clock_funcs = dev_priv->ops->clock_funcs;
1112 drm_mode_crtc_set_gamma_size(&psb_intel_crtc->base, 256);
1113 psb_intel_crtc->pipe = pipe;
1114 psb_intel_crtc->plane = pipe;
1116 r_base = psb_intel_crtc->base.gamma_store;
1117 g_base = r_base + 256;
1118 b_base = g_base + 256;
1119 for (i = 0; i < 256; i++) {
1120 psb_intel_crtc->lut_r[i] = i;
1121 psb_intel_crtc->lut_g[i] = i;
1122 psb_intel_crtc->lut_b[i] = i;
1127 psb_intel_crtc->lut_adj[i] = 0;
1130 psb_intel_crtc->mode_dev = mode_dev;
1131 psb_intel_crtc->cursor_addr = 0;
1133 drm_crtc_helper_add(&psb_intel_crtc->base,
1134 dev_priv->ops->crtc_helper);
1136 /* Setup the array of drm_connector pointer array */
1137 psb_intel_crtc->mode_set.crtc = &psb_intel_crtc->base;
1138 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
1139 dev_priv->plane_to_crtc_mapping[psb_intel_crtc->plane] != NULL);
1140 dev_priv->plane_to_crtc_mapping[psb_intel_crtc->plane] =
1141 &psb_intel_crtc->base;
1142 dev_priv->pipe_to_crtc_mapping[psb_intel_crtc->pipe] =
1143 &psb_intel_crtc->base;
1144 psb_intel_crtc->mode_set.connectors =
1145 (struct drm_connector **) (psb_intel_crtc + 1);
1146 psb_intel_crtc->mode_set.num_connectors = 0;
1147 psb_intel_cursor_init(dev, psb_intel_crtc);
1149 /* Set to true so that the pipe is forced off on initial config. */
1150 psb_intel_crtc->active = true;
1153 int psb_intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
1154 struct drm_file *file_priv)
1156 struct drm_psb_private *dev_priv = dev->dev_private;
1157 struct drm_psb_get_pipe_from_crtc_id_arg *pipe_from_crtc_id = data;
1158 struct drm_mode_object *drmmode_obj;
1159 struct psb_intel_crtc *crtc;
1162 dev_err(dev->dev, "called with no initialization\n");
1166 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
1167 DRM_MODE_OBJECT_CRTC);
1170 dev_err(dev->dev, "no such CRTC id\n");
1174 crtc = to_psb_intel_crtc(obj_to_crtc(drmmode_obj));
1175 pipe_from_crtc_id->pipe = crtc->pipe;
1180 struct drm_crtc *psb_intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
1182 struct drm_crtc *crtc = NULL;
1184 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1185 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1186 if (psb_intel_crtc->pipe == pipe)
1192 int psb_intel_connector_clones(struct drm_device *dev, int type_mask)
1195 struct drm_connector *connector;
1198 list_for_each_entry(connector, &dev->mode_config.connector_list,
1200 struct psb_intel_encoder *psb_intel_encoder =
1201 psb_intel_attached_encoder(connector);
1202 if (type_mask & (1 << psb_intel_encoder->type))
1203 index_mask |= (1 << entry);
1209 /* current intel driver doesn't take advantage of encoders
1210 always give back the encoder for the connector
1212 struct drm_encoder *psb_intel_best_encoder(struct drm_connector *connector)
1214 struct psb_intel_encoder *psb_intel_encoder =
1215 psb_intel_attached_encoder(connector);
1217 return &psb_intel_encoder->base;
1220 void psb_intel_connector_attach_encoder(struct psb_intel_connector *connector,
1221 struct psb_intel_encoder *encoder)
1223 connector->encoder = encoder;
1224 drm_mode_connector_attach_encoder(&connector->base,