1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright © 2006-2011 Intel Corporation
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Authors:
6*4882a593Smuzhiyun * Eric Anholt <eric@anholt.net>
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <linux/delay.h>
10*4882a593Smuzhiyun #include <linux/i2c.h>
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <drm/drm_plane_helper.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include "framebuffer.h"
15*4882a593Smuzhiyun #include "gma_display.h"
16*4882a593Smuzhiyun #include "power.h"
17*4882a593Smuzhiyun #include "psb_drv.h"
18*4882a593Smuzhiyun #include "psb_intel_drv.h"
19*4882a593Smuzhiyun #include "psb_intel_reg.h"
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #define INTEL_LIMIT_I9XX_SDVO_DAC 0
22*4882a593Smuzhiyun #define INTEL_LIMIT_I9XX_LVDS 1
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun static const struct gma_limit_t psb_intel_limits[] = {
25*4882a593Smuzhiyun { /* INTEL_LIMIT_I9XX_SDVO_DAC */
26*4882a593Smuzhiyun .dot = {.min = 20000, .max = 400000},
27*4882a593Smuzhiyun .vco = {.min = 1400000, .max = 2800000},
28*4882a593Smuzhiyun .n = {.min = 1, .max = 6},
29*4882a593Smuzhiyun .m = {.min = 70, .max = 120},
30*4882a593Smuzhiyun .m1 = {.min = 8, .max = 18},
31*4882a593Smuzhiyun .m2 = {.min = 3, .max = 7},
32*4882a593Smuzhiyun .p = {.min = 5, .max = 80},
33*4882a593Smuzhiyun .p1 = {.min = 1, .max = 8},
34*4882a593Smuzhiyun .p2 = {.dot_limit = 200000, .p2_slow = 10, .p2_fast = 5},
35*4882a593Smuzhiyun .find_pll = gma_find_best_pll,
36*4882a593Smuzhiyun },
37*4882a593Smuzhiyun { /* INTEL_LIMIT_I9XX_LVDS */
38*4882a593Smuzhiyun .dot = {.min = 20000, .max = 400000},
39*4882a593Smuzhiyun .vco = {.min = 1400000, .max = 2800000},
40*4882a593Smuzhiyun .n = {.min = 1, .max = 6},
41*4882a593Smuzhiyun .m = {.min = 70, .max = 120},
42*4882a593Smuzhiyun .m1 = {.min = 8, .max = 18},
43*4882a593Smuzhiyun .m2 = {.min = 3, .max = 7},
44*4882a593Smuzhiyun .p = {.min = 7, .max = 98},
45*4882a593Smuzhiyun .p1 = {.min = 1, .max = 8},
46*4882a593Smuzhiyun /* The single-channel range is 25-112Mhz, and dual-channel
47*4882a593Smuzhiyun * is 80-224Mhz. Prefer single channel as much as possible.
48*4882a593Smuzhiyun */
49*4882a593Smuzhiyun .p2 = {.dot_limit = 112000, .p2_slow = 14, .p2_fast = 7},
50*4882a593Smuzhiyun .find_pll = gma_find_best_pll,
51*4882a593Smuzhiyun },
52*4882a593Smuzhiyun };
53*4882a593Smuzhiyun
psb_intel_limit(struct drm_crtc * crtc,int refclk)54*4882a593Smuzhiyun static const struct gma_limit_t *psb_intel_limit(struct drm_crtc *crtc,
55*4882a593Smuzhiyun int refclk)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun const struct gma_limit_t *limit;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun if (gma_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
60*4882a593Smuzhiyun limit = &psb_intel_limits[INTEL_LIMIT_I9XX_LVDS];
61*4882a593Smuzhiyun else
62*4882a593Smuzhiyun limit = &psb_intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC];
63*4882a593Smuzhiyun return limit;
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun
psb_intel_clock(int refclk,struct gma_clock_t * clock)66*4882a593Smuzhiyun static void psb_intel_clock(int refclk, struct gma_clock_t *clock)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
69*4882a593Smuzhiyun clock->p = clock->p1 * clock->p2;
70*4882a593Smuzhiyun clock->vco = refclk * clock->m / (clock->n + 2);
71*4882a593Smuzhiyun clock->dot = clock->vco / clock->p;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /**
75*4882a593Smuzhiyun * Return the pipe currently connected to the panel fitter,
76*4882a593Smuzhiyun * or -1 if the panel fitter is not present or not in use
77*4882a593Smuzhiyun */
psb_intel_panel_fitter_pipe(struct drm_device * dev)78*4882a593Smuzhiyun static int psb_intel_panel_fitter_pipe(struct drm_device *dev)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun u32 pfit_control;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun pfit_control = REG_READ(PFIT_CONTROL);
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun /* See if the panel fitter is in use */
85*4882a593Smuzhiyun if ((pfit_control & PFIT_ENABLE) == 0)
86*4882a593Smuzhiyun return -1;
87*4882a593Smuzhiyun /* Must be on PIPE 1 for PSB */
88*4882a593Smuzhiyun return 1;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
psb_intel_crtc_mode_set(struct drm_crtc * crtc,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode,int x,int y,struct drm_framebuffer * old_fb)91*4882a593Smuzhiyun static int psb_intel_crtc_mode_set(struct drm_crtc *crtc,
92*4882a593Smuzhiyun struct drm_display_mode *mode,
93*4882a593Smuzhiyun struct drm_display_mode *adjusted_mode,
94*4882a593Smuzhiyun int x, int y,
95*4882a593Smuzhiyun struct drm_framebuffer *old_fb)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun struct drm_device *dev = crtc->dev;
98*4882a593Smuzhiyun struct drm_psb_private *dev_priv = dev->dev_private;
99*4882a593Smuzhiyun struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
100*4882a593Smuzhiyun const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
101*4882a593Smuzhiyun int pipe = gma_crtc->pipe;
102*4882a593Smuzhiyun const struct psb_offset *map = &dev_priv->regmap[pipe];
103*4882a593Smuzhiyun int refclk;
104*4882a593Smuzhiyun struct gma_clock_t clock;
105*4882a593Smuzhiyun u32 dpll = 0, fp = 0, dspcntr, pipeconf;
106*4882a593Smuzhiyun bool ok, is_sdvo = false;
107*4882a593Smuzhiyun bool is_lvds = false, is_tv = false;
108*4882a593Smuzhiyun struct drm_mode_config *mode_config = &dev->mode_config;
109*4882a593Smuzhiyun struct drm_connector *connector;
110*4882a593Smuzhiyun const struct gma_limit_t *limit;
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun /* No scan out no play */
113*4882a593Smuzhiyun if (crtc->primary->fb == NULL) {
114*4882a593Smuzhiyun crtc_funcs->mode_set_base(crtc, x, y, old_fb);
115*4882a593Smuzhiyun return 0;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun list_for_each_entry(connector, &mode_config->connector_list, head) {
119*4882a593Smuzhiyun struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun if (!connector->encoder
122*4882a593Smuzhiyun || connector->encoder->crtc != crtc)
123*4882a593Smuzhiyun continue;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun switch (gma_encoder->type) {
126*4882a593Smuzhiyun case INTEL_OUTPUT_LVDS:
127*4882a593Smuzhiyun is_lvds = true;
128*4882a593Smuzhiyun break;
129*4882a593Smuzhiyun case INTEL_OUTPUT_SDVO:
130*4882a593Smuzhiyun is_sdvo = true;
131*4882a593Smuzhiyun break;
132*4882a593Smuzhiyun case INTEL_OUTPUT_TVOUT:
133*4882a593Smuzhiyun is_tv = true;
134*4882a593Smuzhiyun break;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun refclk = 96000;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun limit = gma_crtc->clock_funcs->limit(crtc, refclk);
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk,
143*4882a593Smuzhiyun &clock);
144*4882a593Smuzhiyun if (!ok) {
145*4882a593Smuzhiyun DRM_ERROR("Couldn't find PLL settings for mode! target: %d, actual: %d",
146*4882a593Smuzhiyun adjusted_mode->clock, clock.dot);
147*4882a593Smuzhiyun return 0;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun dpll = DPLL_VGA_MODE_DIS;
153*4882a593Smuzhiyun if (is_lvds) {
154*4882a593Smuzhiyun dpll |= DPLLB_MODE_LVDS;
155*4882a593Smuzhiyun dpll |= DPLL_DVO_HIGH_SPEED;
156*4882a593Smuzhiyun } else
157*4882a593Smuzhiyun dpll |= DPLLB_MODE_DAC_SERIAL;
158*4882a593Smuzhiyun if (is_sdvo) {
159*4882a593Smuzhiyun int sdvo_pixel_multiply =
160*4882a593Smuzhiyun adjusted_mode->clock / mode->clock;
161*4882a593Smuzhiyun dpll |= DPLL_DVO_HIGH_SPEED;
162*4882a593Smuzhiyun dpll |=
163*4882a593Smuzhiyun (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun /* compute bitmask from p1 value */
167*4882a593Smuzhiyun dpll |= (1 << (clock.p1 - 1)) << 16;
168*4882a593Smuzhiyun switch (clock.p2) {
169*4882a593Smuzhiyun case 5:
170*4882a593Smuzhiyun dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
171*4882a593Smuzhiyun break;
172*4882a593Smuzhiyun case 7:
173*4882a593Smuzhiyun dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
174*4882a593Smuzhiyun break;
175*4882a593Smuzhiyun case 10:
176*4882a593Smuzhiyun dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
177*4882a593Smuzhiyun break;
178*4882a593Smuzhiyun case 14:
179*4882a593Smuzhiyun dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
180*4882a593Smuzhiyun break;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun if (is_tv) {
184*4882a593Smuzhiyun /* XXX: just matching BIOS for now */
185*4882a593Smuzhiyun /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
186*4882a593Smuzhiyun dpll |= 3;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun dpll |= PLL_REF_INPUT_DREFCLK;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun /* setup pipeconf */
191*4882a593Smuzhiyun pipeconf = REG_READ(map->conf);
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun /* Set up the display plane register */
194*4882a593Smuzhiyun dspcntr = DISPPLANE_GAMMA_ENABLE;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun if (pipe == 0)
197*4882a593Smuzhiyun dspcntr |= DISPPLANE_SEL_PIPE_A;
198*4882a593Smuzhiyun else
199*4882a593Smuzhiyun dspcntr |= DISPPLANE_SEL_PIPE_B;
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun dspcntr |= DISPLAY_PLANE_ENABLE;
202*4882a593Smuzhiyun pipeconf |= PIPEACONF_ENABLE;
203*4882a593Smuzhiyun dpll |= DPLL_VCO_ENABLE;
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /* Disable the panel fitter if it was on our pipe */
207*4882a593Smuzhiyun if (psb_intel_panel_fitter_pipe(dev) == pipe)
208*4882a593Smuzhiyun REG_WRITE(PFIT_CONTROL, 0);
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun drm_mode_debug_printmodeline(mode);
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun if (dpll & DPLL_VCO_ENABLE) {
213*4882a593Smuzhiyun REG_WRITE(map->fp0, fp);
214*4882a593Smuzhiyun REG_WRITE(map->dpll, dpll & ~DPLL_VCO_ENABLE);
215*4882a593Smuzhiyun REG_READ(map->dpll);
216*4882a593Smuzhiyun udelay(150);
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun /* The LVDS pin pair needs to be on before the DPLLs are enabled.
220*4882a593Smuzhiyun * This is an exception to the general rule that mode_set doesn't turn
221*4882a593Smuzhiyun * things on.
222*4882a593Smuzhiyun */
223*4882a593Smuzhiyun if (is_lvds) {
224*4882a593Smuzhiyun u32 lvds = REG_READ(LVDS);
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun lvds &= ~LVDS_PIPEB_SELECT;
227*4882a593Smuzhiyun if (pipe == 1)
228*4882a593Smuzhiyun lvds |= LVDS_PIPEB_SELECT;
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
231*4882a593Smuzhiyun /* Set the B0-B3 data pairs corresponding to
232*4882a593Smuzhiyun * whether we're going to
233*4882a593Smuzhiyun * set the DPLLs for dual-channel mode or not.
234*4882a593Smuzhiyun */
235*4882a593Smuzhiyun lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
236*4882a593Smuzhiyun if (clock.p2 == 7)
237*4882a593Smuzhiyun lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
240*4882a593Smuzhiyun * appropriately here, but we need to look more
241*4882a593Smuzhiyun * thoroughly into how panels behave in the two modes.
242*4882a593Smuzhiyun */
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun REG_WRITE(LVDS, lvds);
245*4882a593Smuzhiyun REG_READ(LVDS);
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun REG_WRITE(map->fp0, fp);
249*4882a593Smuzhiyun REG_WRITE(map->dpll, dpll);
250*4882a593Smuzhiyun REG_READ(map->dpll);
251*4882a593Smuzhiyun /* Wait for the clocks to stabilize. */
252*4882a593Smuzhiyun udelay(150);
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun /* write it again -- the BIOS does, after all */
255*4882a593Smuzhiyun REG_WRITE(map->dpll, dpll);
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun REG_READ(map->dpll);
258*4882a593Smuzhiyun /* Wait for the clocks to stabilize. */
259*4882a593Smuzhiyun udelay(150);
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun REG_WRITE(map->htotal, (adjusted_mode->crtc_hdisplay - 1) |
262*4882a593Smuzhiyun ((adjusted_mode->crtc_htotal - 1) << 16));
263*4882a593Smuzhiyun REG_WRITE(map->hblank, (adjusted_mode->crtc_hblank_start - 1) |
264*4882a593Smuzhiyun ((adjusted_mode->crtc_hblank_end - 1) << 16));
265*4882a593Smuzhiyun REG_WRITE(map->hsync, (adjusted_mode->crtc_hsync_start - 1) |
266*4882a593Smuzhiyun ((adjusted_mode->crtc_hsync_end - 1) << 16));
267*4882a593Smuzhiyun REG_WRITE(map->vtotal, (adjusted_mode->crtc_vdisplay - 1) |
268*4882a593Smuzhiyun ((adjusted_mode->crtc_vtotal - 1) << 16));
269*4882a593Smuzhiyun REG_WRITE(map->vblank, (adjusted_mode->crtc_vblank_start - 1) |
270*4882a593Smuzhiyun ((adjusted_mode->crtc_vblank_end - 1) << 16));
271*4882a593Smuzhiyun REG_WRITE(map->vsync, (adjusted_mode->crtc_vsync_start - 1) |
272*4882a593Smuzhiyun ((adjusted_mode->crtc_vsync_end - 1) << 16));
273*4882a593Smuzhiyun /* pipesrc and dspsize control the size that is scaled from,
274*4882a593Smuzhiyun * which should always be the user's requested size.
275*4882a593Smuzhiyun */
276*4882a593Smuzhiyun REG_WRITE(map->size,
277*4882a593Smuzhiyun ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
278*4882a593Smuzhiyun REG_WRITE(map->pos, 0);
279*4882a593Smuzhiyun REG_WRITE(map->src,
280*4882a593Smuzhiyun ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
281*4882a593Smuzhiyun REG_WRITE(map->conf, pipeconf);
282*4882a593Smuzhiyun REG_READ(map->conf);
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun gma_wait_for_vblank(dev);
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun REG_WRITE(map->cntr, dspcntr);
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun /* Flush the plane changes */
289*4882a593Smuzhiyun crtc_funcs->mode_set_base(crtc, x, y, old_fb);
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun gma_wait_for_vblank(dev);
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun return 0;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun /* Returns the clock of the currently programmed mode of the given pipe. */
psb_intel_crtc_clock_get(struct drm_device * dev,struct drm_crtc * crtc)297*4882a593Smuzhiyun static int psb_intel_crtc_clock_get(struct drm_device *dev,
298*4882a593Smuzhiyun struct drm_crtc *crtc)
299*4882a593Smuzhiyun {
300*4882a593Smuzhiyun struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
301*4882a593Smuzhiyun struct drm_psb_private *dev_priv = dev->dev_private;
302*4882a593Smuzhiyun int pipe = gma_crtc->pipe;
303*4882a593Smuzhiyun const struct psb_offset *map = &dev_priv->regmap[pipe];
304*4882a593Smuzhiyun u32 dpll;
305*4882a593Smuzhiyun u32 fp;
306*4882a593Smuzhiyun struct gma_clock_t clock;
307*4882a593Smuzhiyun bool is_lvds;
308*4882a593Smuzhiyun struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun if (gma_power_begin(dev, false)) {
311*4882a593Smuzhiyun dpll = REG_READ(map->dpll);
312*4882a593Smuzhiyun if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
313*4882a593Smuzhiyun fp = REG_READ(map->fp0);
314*4882a593Smuzhiyun else
315*4882a593Smuzhiyun fp = REG_READ(map->fp1);
316*4882a593Smuzhiyun is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
317*4882a593Smuzhiyun gma_power_end(dev);
318*4882a593Smuzhiyun } else {
319*4882a593Smuzhiyun dpll = p->dpll;
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
322*4882a593Smuzhiyun fp = p->fp0;
323*4882a593Smuzhiyun else
324*4882a593Smuzhiyun fp = p->fp1;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun is_lvds = (pipe == 1) && (dev_priv->regs.psb.saveLVDS &
327*4882a593Smuzhiyun LVDS_PORT_EN);
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
331*4882a593Smuzhiyun clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
332*4882a593Smuzhiyun clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun if (is_lvds) {
335*4882a593Smuzhiyun clock.p1 =
336*4882a593Smuzhiyun ffs((dpll &
337*4882a593Smuzhiyun DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
338*4882a593Smuzhiyun DPLL_FPA01_P1_POST_DIV_SHIFT);
339*4882a593Smuzhiyun clock.p2 = 14;
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun if ((dpll & PLL_REF_INPUT_MASK) ==
342*4882a593Smuzhiyun PLLB_REF_INPUT_SPREADSPECTRUMIN) {
343*4882a593Smuzhiyun /* XXX: might not be 66MHz */
344*4882a593Smuzhiyun psb_intel_clock(66000, &clock);
345*4882a593Smuzhiyun } else
346*4882a593Smuzhiyun psb_intel_clock(48000, &clock);
347*4882a593Smuzhiyun } else {
348*4882a593Smuzhiyun if (dpll & PLL_P1_DIVIDE_BY_TWO)
349*4882a593Smuzhiyun clock.p1 = 2;
350*4882a593Smuzhiyun else {
351*4882a593Smuzhiyun clock.p1 =
352*4882a593Smuzhiyun ((dpll &
353*4882a593Smuzhiyun DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
354*4882a593Smuzhiyun DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun if (dpll & PLL_P2_DIVIDE_BY_4)
357*4882a593Smuzhiyun clock.p2 = 4;
358*4882a593Smuzhiyun else
359*4882a593Smuzhiyun clock.p2 = 2;
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun psb_intel_clock(48000, &clock);
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun /* XXX: It would be nice to validate the clocks, but we can't reuse
365*4882a593Smuzhiyun * i830PllIsValid() because it relies on the xf86_config connector
366*4882a593Smuzhiyun * configuration being accurate, which it isn't necessarily.
367*4882a593Smuzhiyun */
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun return clock.dot;
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun /** Returns the currently programmed mode of the given pipe. */
psb_intel_crtc_mode_get(struct drm_device * dev,struct drm_crtc * crtc)373*4882a593Smuzhiyun struct drm_display_mode *psb_intel_crtc_mode_get(struct drm_device *dev,
374*4882a593Smuzhiyun struct drm_crtc *crtc)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
377*4882a593Smuzhiyun int pipe = gma_crtc->pipe;
378*4882a593Smuzhiyun struct drm_display_mode *mode;
379*4882a593Smuzhiyun int htot;
380*4882a593Smuzhiyun int hsync;
381*4882a593Smuzhiyun int vtot;
382*4882a593Smuzhiyun int vsync;
383*4882a593Smuzhiyun struct drm_psb_private *dev_priv = dev->dev_private;
384*4882a593Smuzhiyun struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
385*4882a593Smuzhiyun const struct psb_offset *map = &dev_priv->regmap[pipe];
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun if (gma_power_begin(dev, false)) {
388*4882a593Smuzhiyun htot = REG_READ(map->htotal);
389*4882a593Smuzhiyun hsync = REG_READ(map->hsync);
390*4882a593Smuzhiyun vtot = REG_READ(map->vtotal);
391*4882a593Smuzhiyun vsync = REG_READ(map->vsync);
392*4882a593Smuzhiyun gma_power_end(dev);
393*4882a593Smuzhiyun } else {
394*4882a593Smuzhiyun htot = p->htotal;
395*4882a593Smuzhiyun hsync = p->hsync;
396*4882a593Smuzhiyun vtot = p->vtotal;
397*4882a593Smuzhiyun vsync = p->vsync;
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun mode = kzalloc(sizeof(*mode), GFP_KERNEL);
401*4882a593Smuzhiyun if (!mode)
402*4882a593Smuzhiyun return NULL;
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun mode->clock = psb_intel_crtc_clock_get(dev, crtc);
405*4882a593Smuzhiyun mode->hdisplay = (htot & 0xffff) + 1;
406*4882a593Smuzhiyun mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
407*4882a593Smuzhiyun mode->hsync_start = (hsync & 0xffff) + 1;
408*4882a593Smuzhiyun mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
409*4882a593Smuzhiyun mode->vdisplay = (vtot & 0xffff) + 1;
410*4882a593Smuzhiyun mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
411*4882a593Smuzhiyun mode->vsync_start = (vsync & 0xffff) + 1;
412*4882a593Smuzhiyun mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun drm_mode_set_name(mode);
415*4882a593Smuzhiyun drm_mode_set_crtcinfo(mode, 0);
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun return mode;
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun const struct drm_crtc_helper_funcs psb_intel_helper_funcs = {
421*4882a593Smuzhiyun .dpms = gma_crtc_dpms,
422*4882a593Smuzhiyun .mode_set = psb_intel_crtc_mode_set,
423*4882a593Smuzhiyun .mode_set_base = gma_pipe_set_base,
424*4882a593Smuzhiyun .prepare = gma_crtc_prepare,
425*4882a593Smuzhiyun .commit = gma_crtc_commit,
426*4882a593Smuzhiyun .disable = gma_crtc_disable,
427*4882a593Smuzhiyun };
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun const struct drm_crtc_funcs psb_intel_crtc_funcs = {
430*4882a593Smuzhiyun .cursor_set = gma_crtc_cursor_set,
431*4882a593Smuzhiyun .cursor_move = gma_crtc_cursor_move,
432*4882a593Smuzhiyun .gamma_set = gma_crtc_gamma_set,
433*4882a593Smuzhiyun .set_config = gma_crtc_set_config,
434*4882a593Smuzhiyun .destroy = gma_crtc_destroy,
435*4882a593Smuzhiyun .page_flip = gma_crtc_page_flip,
436*4882a593Smuzhiyun .enable_vblank = psb_enable_vblank,
437*4882a593Smuzhiyun .disable_vblank = psb_disable_vblank,
438*4882a593Smuzhiyun .get_vblank_counter = psb_get_vblank_counter,
439*4882a593Smuzhiyun };
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun const struct gma_clock_funcs psb_clock_funcs = {
442*4882a593Smuzhiyun .clock = psb_intel_clock,
443*4882a593Smuzhiyun .limit = psb_intel_limit,
444*4882a593Smuzhiyun .pll_is_valid = gma_pll_is_valid,
445*4882a593Smuzhiyun };
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun /*
448*4882a593Smuzhiyun * Set the default value of cursor control and base register
449*4882a593Smuzhiyun * to zero. This is a workaround for h/w defect on Oaktrail
450*4882a593Smuzhiyun */
psb_intel_cursor_init(struct drm_device * dev,struct gma_crtc * gma_crtc)451*4882a593Smuzhiyun static void psb_intel_cursor_init(struct drm_device *dev,
452*4882a593Smuzhiyun struct gma_crtc *gma_crtc)
453*4882a593Smuzhiyun {
454*4882a593Smuzhiyun struct drm_psb_private *dev_priv = dev->dev_private;
455*4882a593Smuzhiyun u32 control[3] = { CURACNTR, CURBCNTR, CURCCNTR };
456*4882a593Smuzhiyun u32 base[3] = { CURABASE, CURBBASE, CURCBASE };
457*4882a593Smuzhiyun struct gtt_range *cursor_gt;
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun if (dev_priv->ops->cursor_needs_phys) {
460*4882a593Smuzhiyun /* Allocate 4 pages of stolen mem for a hardware cursor. That
461*4882a593Smuzhiyun * is enough for the 64 x 64 ARGB cursors we support.
462*4882a593Smuzhiyun */
463*4882a593Smuzhiyun cursor_gt = psb_gtt_alloc_range(dev, 4 * PAGE_SIZE, "cursor", 1,
464*4882a593Smuzhiyun PAGE_SIZE);
465*4882a593Smuzhiyun if (!cursor_gt) {
466*4882a593Smuzhiyun gma_crtc->cursor_gt = NULL;
467*4882a593Smuzhiyun goto out;
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun gma_crtc->cursor_gt = cursor_gt;
470*4882a593Smuzhiyun gma_crtc->cursor_addr = dev_priv->stolen_base +
471*4882a593Smuzhiyun cursor_gt->offset;
472*4882a593Smuzhiyun } else {
473*4882a593Smuzhiyun gma_crtc->cursor_gt = NULL;
474*4882a593Smuzhiyun }
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun out:
477*4882a593Smuzhiyun REG_WRITE(control[gma_crtc->pipe], 0);
478*4882a593Smuzhiyun REG_WRITE(base[gma_crtc->pipe], 0);
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun
psb_intel_crtc_init(struct drm_device * dev,int pipe,struct psb_intel_mode_device * mode_dev)481*4882a593Smuzhiyun void psb_intel_crtc_init(struct drm_device *dev, int pipe,
482*4882a593Smuzhiyun struct psb_intel_mode_device *mode_dev)
483*4882a593Smuzhiyun {
484*4882a593Smuzhiyun struct drm_psb_private *dev_priv = dev->dev_private;
485*4882a593Smuzhiyun struct gma_crtc *gma_crtc;
486*4882a593Smuzhiyun int i;
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun /* We allocate a extra array of drm_connector pointers
489*4882a593Smuzhiyun * for fbdev after the crtc */
490*4882a593Smuzhiyun gma_crtc = kzalloc(sizeof(struct gma_crtc) +
491*4882a593Smuzhiyun (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)),
492*4882a593Smuzhiyun GFP_KERNEL);
493*4882a593Smuzhiyun if (gma_crtc == NULL)
494*4882a593Smuzhiyun return;
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun gma_crtc->crtc_state =
497*4882a593Smuzhiyun kzalloc(sizeof(struct psb_intel_crtc_state), GFP_KERNEL);
498*4882a593Smuzhiyun if (!gma_crtc->crtc_state) {
499*4882a593Smuzhiyun dev_err(dev->dev, "Crtc state error: No memory\n");
500*4882a593Smuzhiyun kfree(gma_crtc);
501*4882a593Smuzhiyun return;
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun /* Set the CRTC operations from the chip specific data */
505*4882a593Smuzhiyun drm_crtc_init(dev, &gma_crtc->base, dev_priv->ops->crtc_funcs);
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun /* Set the CRTC clock functions from chip specific data */
508*4882a593Smuzhiyun gma_crtc->clock_funcs = dev_priv->ops->clock_funcs;
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun drm_mode_crtc_set_gamma_size(&gma_crtc->base, 256);
511*4882a593Smuzhiyun gma_crtc->pipe = pipe;
512*4882a593Smuzhiyun gma_crtc->plane = pipe;
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun for (i = 0; i < 256; i++)
515*4882a593Smuzhiyun gma_crtc->lut_adj[i] = 0;
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun gma_crtc->mode_dev = mode_dev;
518*4882a593Smuzhiyun gma_crtc->cursor_addr = 0;
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun drm_crtc_helper_add(&gma_crtc->base,
521*4882a593Smuzhiyun dev_priv->ops->crtc_helper);
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun /* Setup the array of drm_connector pointer array */
524*4882a593Smuzhiyun gma_crtc->mode_set.crtc = &gma_crtc->base;
525*4882a593Smuzhiyun BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
526*4882a593Smuzhiyun dev_priv->plane_to_crtc_mapping[gma_crtc->plane] != NULL);
527*4882a593Smuzhiyun dev_priv->plane_to_crtc_mapping[gma_crtc->plane] = &gma_crtc->base;
528*4882a593Smuzhiyun dev_priv->pipe_to_crtc_mapping[gma_crtc->pipe] = &gma_crtc->base;
529*4882a593Smuzhiyun gma_crtc->mode_set.connectors = (struct drm_connector **)(gma_crtc + 1);
530*4882a593Smuzhiyun gma_crtc->mode_set.num_connectors = 0;
531*4882a593Smuzhiyun psb_intel_cursor_init(dev, gma_crtc);
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun /* Set to true so that the pipe is forced off on initial config. */
534*4882a593Smuzhiyun gma_crtc->active = true;
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun
psb_intel_get_crtc_from_pipe(struct drm_device * dev,int pipe)537*4882a593Smuzhiyun struct drm_crtc *psb_intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun struct drm_crtc *crtc;
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
542*4882a593Smuzhiyun struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun if (gma_crtc->pipe == pipe)
545*4882a593Smuzhiyun return crtc;
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun return NULL;
548*4882a593Smuzhiyun }
549*4882a593Smuzhiyun
gma_connector_clones(struct drm_device * dev,int type_mask)550*4882a593Smuzhiyun int gma_connector_clones(struct drm_device *dev, int type_mask)
551*4882a593Smuzhiyun {
552*4882a593Smuzhiyun int index_mask = 0;
553*4882a593Smuzhiyun struct drm_connector *connector;
554*4882a593Smuzhiyun int entry = 0;
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun list_for_each_entry(connector, &dev->mode_config.connector_list,
557*4882a593Smuzhiyun head) {
558*4882a593Smuzhiyun struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
559*4882a593Smuzhiyun if (type_mask & (1 << gma_encoder->type))
560*4882a593Smuzhiyun index_mask |= (1 << entry);
561*4882a593Smuzhiyun entry++;
562*4882a593Smuzhiyun }
563*4882a593Smuzhiyun return index_mask;
564*4882a593Smuzhiyun }
565