Set memory attributes on page
[pscnv.git] / pscnv / nv04_dfp.c
blobffeae7d943ff1070efcc7188c2a80ac99da72a8e
1 /*
2 * Copyright 2003 NVIDIA, Corporation
3 * Copyright 2006 Dave Airlie
4 * Copyright 2007 Maarten Maathuis
5 * Copyright 2007-2009 Stuart Bennett
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
27 #include "nouveau_drv.h"
28 #include "drm_crtc_helper.h"
30 #include "nouveau_encoder.h"
31 #include "nouveau_connector.h"
32 #include "nouveau_crtc.h"
33 #include "nouveau_hw.h"
34 #include "nvreg.h"
36 #include "i2c/sil164.h"
38 #define FP_TG_CONTROL_ON (NV_PRAMDAC_FP_TG_CONTROL_DISPEN_POS | \
39 NV_PRAMDAC_FP_TG_CONTROL_HSYNC_POS | \
40 NV_PRAMDAC_FP_TG_CONTROL_VSYNC_POS)
41 #define FP_TG_CONTROL_OFF (NV_PRAMDAC_FP_TG_CONTROL_DISPEN_DISABLE | \
42 NV_PRAMDAC_FP_TG_CONTROL_HSYNC_DISABLE | \
43 NV_PRAMDAC_FP_TG_CONTROL_VSYNC_DISABLE)
45 static inline bool is_fpc_off(uint32_t fpc)
47 return ((fpc & (FP_TG_CONTROL_ON | FP_TG_CONTROL_OFF)) ==
48 FP_TG_CONTROL_OFF);
51 int nv04_dfp_get_bound_head(struct drm_device *dev, struct dcb_entry *dcbent)
53 /* special case of nv_read_tmds to find crtc associated with an output.
54 * this does not give a correct answer for off-chip dvi, but there's no
55 * use for such an answer anyway
57 int ramdac = (dcbent->or & OUTPUT_C) >> 2;
59 NVWriteRAMDAC(dev, ramdac, NV_PRAMDAC_FP_TMDS_CONTROL,
60 NV_PRAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE | 0x4);
61 return ((NVReadRAMDAC(dev, ramdac, NV_PRAMDAC_FP_TMDS_DATA) & 0x8) >> 3) ^ ramdac;
64 void nv04_dfp_bind_head(struct drm_device *dev, struct dcb_entry *dcbent,
65 int head, bool dl)
67 /* The BIOS scripts don't do this for us, sadly
68 * Luckily we do know the values ;-)
70 * head < 0 indicates we wish to force a setting with the overrideval
71 * (for VT restore etc.)
74 int ramdac = (dcbent->or & OUTPUT_C) >> 2;
75 uint8_t tmds04 = 0x80;
77 if (head != ramdac)
78 tmds04 = 0x88;
80 if (dcbent->type == OUTPUT_LVDS)
81 tmds04 |= 0x01;
83 nv_write_tmds(dev, dcbent->or, 0, 0x04, tmds04);
85 if (dl) /* dual link */
86 nv_write_tmds(dev, dcbent->or, 1, 0x04, tmds04 ^ 0x08);
89 void nv04_dfp_disable(struct drm_device *dev, int head)
91 struct drm_nouveau_private *dev_priv = dev->dev_private;
92 struct nv04_crtc_reg *crtcstate = dev_priv->mode_reg.crtc_reg;
94 if (NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL) &
95 FP_TG_CONTROL_ON) {
96 /* digital remnants must be cleaned before new crtc
97 * values programmed. delay is time for the vga stuff
98 * to realise it's in control again
100 NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL,
101 FP_TG_CONTROL_OFF);
102 msleep(50);
104 /* don't inadvertently turn it on when state written later */
105 crtcstate[head].fp_control = FP_TG_CONTROL_OFF;
106 crtcstate[head].CRTC[NV_CIO_CRE_LCD__INDEX] &=
107 ~NV_CIO_CRE_LCD_ROUTE_MASK;
110 void nv04_dfp_update_fp_control(struct drm_encoder *encoder, int mode)
112 struct drm_device *dev = encoder->dev;
113 struct drm_nouveau_private *dev_priv = dev->dev_private;
114 struct drm_crtc *crtc;
115 struct nouveau_crtc *nv_crtc;
116 uint32_t *fpc;
118 if (mode == DRM_MODE_DPMS_ON) {
119 nv_crtc = nouveau_crtc(encoder->crtc);
120 fpc = &dev_priv->mode_reg.crtc_reg[nv_crtc->index].fp_control;
122 if (is_fpc_off(*fpc)) {
123 /* using saved value is ok, as (is_digital && dpms_on &&
124 * fp_control==OFF) is (at present) *only* true when
125 * fpc's most recent change was by below "off" code
127 *fpc = nv_crtc->dpms_saved_fp_control;
130 nv_crtc->fp_users |= 1 << nouveau_encoder(encoder)->dcb->index;
131 NVWriteRAMDAC(dev, nv_crtc->index, NV_PRAMDAC_FP_TG_CONTROL, *fpc);
132 } else {
133 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
134 nv_crtc = nouveau_crtc(crtc);
135 fpc = &dev_priv->mode_reg.crtc_reg[nv_crtc->index].fp_control;
137 nv_crtc->fp_users &= ~(1 << nouveau_encoder(encoder)->dcb->index);
138 if (!is_fpc_off(*fpc) && !nv_crtc->fp_users) {
139 nv_crtc->dpms_saved_fp_control = *fpc;
140 /* cut the FP output */
141 *fpc &= ~FP_TG_CONTROL_ON;
142 *fpc |= FP_TG_CONTROL_OFF;
143 NVWriteRAMDAC(dev, nv_crtc->index,
144 NV_PRAMDAC_FP_TG_CONTROL, *fpc);
150 static struct drm_encoder *get_tmds_slave(struct drm_encoder *encoder)
152 struct drm_device *dev = encoder->dev;
153 struct dcb_entry *dcb = nouveau_encoder(encoder)->dcb;
154 struct drm_encoder *slave;
156 if (dcb->type != OUTPUT_TMDS || dcb->location == DCB_LOC_ON_CHIP)
157 return NULL;
159 /* Some BIOSes (e.g. the one in a Quadro FX1000) report several
160 * TMDS transmitters at the same I2C address, in the same I2C
161 * bus. This can still work because in that case one of them is
162 * always hard-wired to a reasonable configuration using straps,
163 * and the other one needs to be programmed.
165 * I don't think there's a way to know which is which, even the
166 * blob programs the one exposed via I2C for *both* heads, so
167 * let's do the same.
169 list_for_each_entry(slave, &dev->mode_config.encoder_list, head) {
170 struct dcb_entry *slave_dcb = nouveau_encoder(slave)->dcb;
172 if (slave_dcb->type == OUTPUT_TMDS && get_slave_funcs(slave) &&
173 slave_dcb->tmdsconf.slave_addr == dcb->tmdsconf.slave_addr)
174 return slave;
177 return NULL;
180 static bool nv04_dfp_mode_fixup(struct drm_encoder *encoder,
181 struct drm_display_mode *mode,
182 struct drm_display_mode *adjusted_mode)
184 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
185 struct nouveau_connector *nv_connector = nouveau_encoder_connector_get(nv_encoder);
187 if (!nv_connector->native_mode ||
188 nv_connector->scaling_mode == DRM_MODE_SCALE_NONE ||
189 mode->hdisplay > nv_connector->native_mode->hdisplay ||
190 mode->vdisplay > nv_connector->native_mode->vdisplay) {
191 nv_encoder->mode = *adjusted_mode;
193 } else {
194 nv_encoder->mode = *nv_connector->native_mode;
195 adjusted_mode->clock = nv_connector->native_mode->clock;
198 return true;
201 static void nv04_dfp_prepare_sel_clk(struct drm_device *dev,
202 struct nouveau_encoder *nv_encoder, int head)
204 struct drm_nouveau_private *dev_priv = dev->dev_private;
205 struct nv04_mode_state *state = &dev_priv->mode_reg;
206 uint32_t bits1618 = nv_encoder->dcb->or & OUTPUT_A ? 0x10000 : 0x40000;
208 if (nv_encoder->dcb->location != DCB_LOC_ON_CHIP)
209 return;
211 /* SEL_CLK is only used on the primary ramdac
212 * It toggles spread spectrum PLL output and sets the bindings of PLLs
213 * to heads on digital outputs
215 if (head)
216 state->sel_clk |= bits1618;
217 else
218 state->sel_clk &= ~bits1618;
220 /* nv30:
221 * bit 0 NVClk spread spectrum on/off
222 * bit 2 MemClk spread spectrum on/off
223 * bit 4 PixClk1 spread spectrum on/off toggle
224 * bit 6 PixClk2 spread spectrum on/off toggle
226 * nv40 (observations from bios behaviour and mmio traces):
227 * bits 4&6 as for nv30
228 * bits 5&7 head dependent as for bits 4&6, but do not appear with 4&6;
229 * maybe a different spread mode
230 * bits 8&10 seen on dual-link dvi outputs, purpose unknown (set by POST scripts)
231 * The logic behind turning spread spectrum on/off in the first place,
232 * and which bit-pair to use, is unclear on nv40 (for earlier cards, the fp table
233 * entry has the necessary info)
235 if (nv_encoder->dcb->type == OUTPUT_LVDS && dev_priv->saved_reg.sel_clk & 0xf0) {
236 int shift = (dev_priv->saved_reg.sel_clk & 0x50) ? 0 : 1;
238 state->sel_clk &= ~0xf0;
239 state->sel_clk |= (head ? 0x40 : 0x10) << shift;
243 static void nv04_dfp_prepare(struct drm_encoder *encoder)
245 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
246 struct drm_encoder_helper_funcs *helper = encoder->helper_private;
247 struct drm_device *dev = encoder->dev;
248 struct drm_nouveau_private *dev_priv = dev->dev_private;
249 int head = nouveau_crtc(encoder->crtc)->index;
250 struct nv04_crtc_reg *crtcstate = dev_priv->mode_reg.crtc_reg;
251 uint8_t *cr_lcd = &crtcstate[head].CRTC[NV_CIO_CRE_LCD__INDEX];
252 uint8_t *cr_lcd_oth = &crtcstate[head ^ 1].CRTC[NV_CIO_CRE_LCD__INDEX];
254 helper->dpms(encoder, DRM_MODE_DPMS_OFF);
256 nv04_dfp_prepare_sel_clk(dev, nv_encoder, head);
258 *cr_lcd = (*cr_lcd & ~NV_CIO_CRE_LCD_ROUTE_MASK) | 0x3;
260 if (nv_two_heads(dev)) {
261 if (nv_encoder->dcb->location == DCB_LOC_ON_CHIP)
262 *cr_lcd |= head ? 0x0 : 0x8;
263 else {
264 *cr_lcd |= (nv_encoder->dcb->or << 4) & 0x30;
265 if (nv_encoder->dcb->type == OUTPUT_LVDS)
266 *cr_lcd |= 0x30;
267 if ((*cr_lcd & 0x30) == (*cr_lcd_oth & 0x30)) {
268 /* avoid being connected to both crtcs */
269 *cr_lcd_oth &= ~0x30;
270 NVWriteVgaCrtc(dev, head ^ 1,
271 NV_CIO_CRE_LCD__INDEX,
272 *cr_lcd_oth);
279 static void nv04_dfp_mode_set(struct drm_encoder *encoder,
280 struct drm_display_mode *mode,
281 struct drm_display_mode *adjusted_mode)
283 struct drm_device *dev = encoder->dev;
284 struct drm_nouveau_private *dev_priv = dev->dev_private;
285 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
286 struct nv04_crtc_reg *regp = &dev_priv->mode_reg.crtc_reg[nv_crtc->index];
287 struct nv04_crtc_reg *savep = &dev_priv->saved_reg.crtc_reg[nv_crtc->index];
288 struct nouveau_connector *nv_connector = nouveau_crtc_connector_get(nv_crtc);
289 struct drm_connector *connector = &nv_connector->base;
290 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
291 struct drm_display_mode *output_mode = &nv_encoder->mode;
292 uint32_t mode_ratio, panel_ratio;
294 NV_DEBUG_KMS(dev, "Output mode on CRTC %d:\n", nv_crtc->index);
295 drm_mode_debug_printmodeline(output_mode);
297 /* Initialize the FP registers in this CRTC. */
298 regp->fp_horiz_regs[FP_DISPLAY_END] = output_mode->hdisplay - 1;
299 regp->fp_horiz_regs[FP_TOTAL] = output_mode->htotal - 1;
300 if (!nv_gf4_disp_arch(dev) ||
301 (output_mode->hsync_start - output_mode->hdisplay) >=
302 dev_priv->vbios.digital_min_front_porch)
303 regp->fp_horiz_regs[FP_CRTC] = output_mode->hdisplay;
304 else
305 regp->fp_horiz_regs[FP_CRTC] = output_mode->hsync_start - dev_priv->vbios.digital_min_front_porch - 1;
306 regp->fp_horiz_regs[FP_SYNC_START] = output_mode->hsync_start - 1;
307 regp->fp_horiz_regs[FP_SYNC_END] = output_mode->hsync_end - 1;
308 regp->fp_horiz_regs[FP_VALID_START] = output_mode->hskew;
309 regp->fp_horiz_regs[FP_VALID_END] = output_mode->hdisplay - 1;
311 regp->fp_vert_regs[FP_DISPLAY_END] = output_mode->vdisplay - 1;
312 regp->fp_vert_regs[FP_TOTAL] = output_mode->vtotal - 1;
313 regp->fp_vert_regs[FP_CRTC] = output_mode->vtotal - 5 - 1;
314 regp->fp_vert_regs[FP_SYNC_START] = output_mode->vsync_start - 1;
315 regp->fp_vert_regs[FP_SYNC_END] = output_mode->vsync_end - 1;
316 regp->fp_vert_regs[FP_VALID_START] = 0;
317 regp->fp_vert_regs[FP_VALID_END] = output_mode->vdisplay - 1;
319 /* bit26: a bit seen on some g7x, no as yet discernable purpose */
320 regp->fp_control = NV_PRAMDAC_FP_TG_CONTROL_DISPEN_POS |
321 (savep->fp_control & (1 << 26 | NV_PRAMDAC_FP_TG_CONTROL_READ_PROG));
322 /* Deal with vsync/hsync polarity */
323 /* LVDS screens do set this, but modes with +ve syncs are very rare */
324 if (output_mode->flags & DRM_MODE_FLAG_PVSYNC)
325 regp->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_VSYNC_POS;
326 if (output_mode->flags & DRM_MODE_FLAG_PHSYNC)
327 regp->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_HSYNC_POS;
328 /* panel scaling first, as native would get set otherwise */
329 if (nv_connector->scaling_mode == DRM_MODE_SCALE_NONE ||
330 nv_connector->scaling_mode == DRM_MODE_SCALE_CENTER) /* panel handles it */
331 regp->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_MODE_CENTER;
332 else if (adjusted_mode->hdisplay == output_mode->hdisplay &&
333 adjusted_mode->vdisplay == output_mode->vdisplay) /* native mode */
334 regp->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_MODE_NATIVE;
335 else /* gpu needs to scale */
336 regp->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_MODE_SCALE;
337 if (nvReadEXTDEV(dev, NV_PEXTDEV_BOOT_0) & NV_PEXTDEV_BOOT_0_STRAP_FP_IFACE_12BIT)
338 regp->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_WIDTH_12;
339 if (nv_encoder->dcb->location != DCB_LOC_ON_CHIP &&
340 output_mode->clock > 165000)
341 regp->fp_control |= (2 << 24);
342 if (nv_encoder->dcb->type == OUTPUT_LVDS) {
343 bool duallink, dummy;
345 nouveau_bios_parse_lvds_table(dev, nv_connector->native_mode->
346 clock, &duallink, &dummy);
347 if (duallink)
348 regp->fp_control |= (8 << 28);
349 } else
350 if (output_mode->clock > 165000)
351 regp->fp_control |= (8 << 28);
353 regp->fp_debug_0 = NV_PRAMDAC_FP_DEBUG_0_YWEIGHT_ROUND |
354 NV_PRAMDAC_FP_DEBUG_0_XWEIGHT_ROUND |
355 NV_PRAMDAC_FP_DEBUG_0_YINTERP_BILINEAR |
356 NV_PRAMDAC_FP_DEBUG_0_XINTERP_BILINEAR |
357 NV_RAMDAC_FP_DEBUG_0_TMDS_ENABLED |
358 NV_PRAMDAC_FP_DEBUG_0_YSCALE_ENABLE |
359 NV_PRAMDAC_FP_DEBUG_0_XSCALE_ENABLE;
361 /* We want automatic scaling */
362 regp->fp_debug_1 = 0;
363 /* This can override HTOTAL and VTOTAL */
364 regp->fp_debug_2 = 0;
366 /* Use 20.12 fixed point format to avoid floats */
367 mode_ratio = (1 << 12) * adjusted_mode->hdisplay / adjusted_mode->vdisplay;
368 panel_ratio = (1 << 12) * output_mode->hdisplay / output_mode->vdisplay;
369 /* if ratios are equal, SCALE_ASPECT will automatically (and correctly)
370 * get treated the same as SCALE_FULLSCREEN */
371 if (nv_connector->scaling_mode == DRM_MODE_SCALE_ASPECT &&
372 mode_ratio != panel_ratio) {
373 uint32_t diff, scale;
374 bool divide_by_2 = nv_gf4_disp_arch(dev);
376 if (mode_ratio < panel_ratio) {
377 /* vertical needs to expand to glass size (automatic)
378 * horizontal needs to be scaled at vertical scale factor
379 * to maintain aspect */
381 scale = (1 << 12) * adjusted_mode->vdisplay / output_mode->vdisplay;
382 regp->fp_debug_1 = NV_PRAMDAC_FP_DEBUG_1_XSCALE_TESTMODE_ENABLE |
383 XLATE(scale, divide_by_2, NV_PRAMDAC_FP_DEBUG_1_XSCALE_VALUE);
385 /* restrict area of screen used, horizontally */
386 diff = output_mode->hdisplay -
387 output_mode->vdisplay * mode_ratio / (1 << 12);
388 regp->fp_horiz_regs[FP_VALID_START] += diff / 2;
389 regp->fp_horiz_regs[FP_VALID_END] -= diff / 2;
392 if (mode_ratio > panel_ratio) {
393 /* horizontal needs to expand to glass size (automatic)
394 * vertical needs to be scaled at horizontal scale factor
395 * to maintain aspect */
397 scale = (1 << 12) * adjusted_mode->hdisplay / output_mode->hdisplay;
398 regp->fp_debug_1 = NV_PRAMDAC_FP_DEBUG_1_YSCALE_TESTMODE_ENABLE |
399 XLATE(scale, divide_by_2, NV_PRAMDAC_FP_DEBUG_1_YSCALE_VALUE);
401 /* restrict area of screen used, vertically */
402 diff = output_mode->vdisplay -
403 (1 << 12) * output_mode->hdisplay / mode_ratio;
404 regp->fp_vert_regs[FP_VALID_START] += diff / 2;
405 regp->fp_vert_regs[FP_VALID_END] -= diff / 2;
409 /* Output property. */
410 if ((nv_connector->dithering_mode == DITHERING_MODE_ON) ||
411 (nv_connector->dithering_mode == DITHERING_MODE_AUTO &&
412 encoder->crtc->fb->depth > connector->display_info.bpc * 3)) {
413 if (dev_priv->chipset == 0x11)
414 regp->dither = savep->dither | 0x00010000;
415 else {
416 int i;
417 regp->dither = savep->dither | 0x00000001;
418 for (i = 0; i < 3; i++) {
419 regp->dither_regs[i] = 0xe4e4e4e4;
420 regp->dither_regs[i + 3] = 0x44444444;
423 } else {
424 if (dev_priv->chipset != 0x11) {
425 /* reset them */
426 int i;
427 for (i = 0; i < 3; i++) {
428 regp->dither_regs[i] = savep->dither_regs[i];
429 regp->dither_regs[i + 3] = savep->dither_regs[i + 3];
432 regp->dither = savep->dither;
435 regp->fp_margin_color = 0;
438 static void nv04_dfp_commit(struct drm_encoder *encoder)
440 struct drm_device *dev = encoder->dev;
441 struct drm_nouveau_private *dev_priv = dev->dev_private;
442 struct drm_encoder_helper_funcs *helper = encoder->helper_private;
443 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
444 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
445 struct dcb_entry *dcbe = nv_encoder->dcb;
446 int head = nouveau_crtc(encoder->crtc)->index;
447 struct drm_encoder *slave_encoder;
449 if (dcbe->type == OUTPUT_TMDS)
450 run_tmds_table(dev, dcbe, head, nv_encoder->mode.clock);
451 else if (dcbe->type == OUTPUT_LVDS)
452 call_lvds_script(dev, dcbe, head, LVDS_RESET, nv_encoder->mode.clock);
454 /* update fp_control state for any changes made by scripts,
455 * so correct value is written at DPMS on */
456 dev_priv->mode_reg.crtc_reg[head].fp_control =
457 NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL);
459 /* This could use refinement for flatpanels, but it should work this way */
460 if (dev_priv->chipset < 0x44)
461 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + nv04_dac_output_offset(encoder), 0xf0000000);
462 else
463 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + nv04_dac_output_offset(encoder), 0x00100000);
465 /* Init external transmitters */
466 slave_encoder = get_tmds_slave(encoder);
467 if (slave_encoder)
468 get_slave_funcs(slave_encoder)->mode_set(
469 slave_encoder, &nv_encoder->mode, &nv_encoder->mode);
471 helper->dpms(encoder, DRM_MODE_DPMS_ON);
473 NV_INFO(dev, "Output %s is running on CRTC %d using output %c\n",
474 drm_get_connector_name(&nouveau_encoder_connector_get(nv_encoder)->base),
475 nv_crtc->index, '@' + ffs(nv_encoder->dcb->or));
478 static void nv04_dfp_update_backlight(struct drm_encoder *encoder, int mode)
480 #ifdef __powerpc__
481 struct drm_device *dev = encoder->dev;
483 /* BIOS scripts usually take care of the backlight, thanks
484 * Apple for your consistency.
486 if (dev->pci_device == 0x0179 || dev->pci_device == 0x0189 ||
487 dev->pci_device == 0x0329) {
488 if (mode == DRM_MODE_DPMS_ON) {
489 nv_mask(dev, NV_PBUS_DEBUG_DUALHEAD_CTL, 0, 1 << 31);
490 nv_mask(dev, NV_PCRTC_GPIO_EXT, 3, 1);
491 } else {
492 nv_mask(dev, NV_PBUS_DEBUG_DUALHEAD_CTL, 1 << 31, 0);
493 nv_mask(dev, NV_PCRTC_GPIO_EXT, 3, 0);
496 #endif
499 static inline bool is_powersaving_dpms(int mode)
501 return (mode != DRM_MODE_DPMS_ON);
504 static void nv04_lvds_dpms(struct drm_encoder *encoder, int mode)
506 struct drm_device *dev = encoder->dev;
507 struct drm_crtc *crtc = encoder->crtc;
508 struct drm_nouveau_private *dev_priv = dev->dev_private;
509 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
510 bool was_powersaving = is_powersaving_dpms(nv_encoder->last_dpms);
512 if (nv_encoder->last_dpms == mode)
513 return;
514 nv_encoder->last_dpms = mode;
516 NV_INFO(dev, "Setting dpms mode %d on lvds encoder (output %d)\n",
517 mode, nv_encoder->dcb->index);
519 if (was_powersaving && is_powersaving_dpms(mode))
520 return;
522 if (nv_encoder->dcb->lvdsconf.use_power_scripts) {
523 struct nouveau_connector *nv_connector = nouveau_encoder_connector_get(nv_encoder);
525 /* when removing an output, crtc may not be set, but PANEL_OFF
526 * must still be run
528 int head = crtc ? nouveau_crtc(crtc)->index :
529 nv04_dfp_get_bound_head(dev, nv_encoder->dcb);
531 if (mode == DRM_MODE_DPMS_ON) {
532 if (!nv_connector->native_mode) {
533 NV_ERROR(dev, "Not turning on LVDS without native mode\n");
534 return;
536 call_lvds_script(dev, nv_encoder->dcb, head,
537 LVDS_PANEL_ON, nv_connector->native_mode->clock);
538 } else
539 /* pxclk of 0 is fine for PANEL_OFF, and for a
540 * disconnected LVDS encoder there is no native_mode
542 call_lvds_script(dev, nv_encoder->dcb, head,
543 LVDS_PANEL_OFF, 0);
546 nv04_dfp_update_backlight(encoder, mode);
547 nv04_dfp_update_fp_control(encoder, mode);
549 if (mode == DRM_MODE_DPMS_ON)
550 nv04_dfp_prepare_sel_clk(dev, nv_encoder, nouveau_crtc(crtc)->index);
551 else {
552 dev_priv->mode_reg.sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK);
553 dev_priv->mode_reg.sel_clk &= ~0xf0;
555 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, dev_priv->mode_reg.sel_clk);
558 static void nv04_tmds_dpms(struct drm_encoder *encoder, int mode)
560 struct drm_device *dev = encoder->dev;
561 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
563 if (nv_encoder->last_dpms == mode)
564 return;
565 nv_encoder->last_dpms = mode;
567 NV_INFO(dev, "Setting dpms mode %d on tmds encoder (output %d)\n",
568 mode, nv_encoder->dcb->index);
570 nv04_dfp_update_backlight(encoder, mode);
571 nv04_dfp_update_fp_control(encoder, mode);
574 static void nv04_dfp_save(struct drm_encoder *encoder)
576 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
577 struct drm_device *dev = encoder->dev;
579 if (nv_two_heads(dev))
580 nv_encoder->restore.head =
581 nv04_dfp_get_bound_head(dev, nv_encoder->dcb);
584 static void nv04_dfp_restore(struct drm_encoder *encoder)
586 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
587 struct drm_device *dev = encoder->dev;
588 struct drm_nouveau_private *dev_priv = dev->dev_private;
589 int head = nv_encoder->restore.head;
591 if (nv_encoder->dcb->type == OUTPUT_LVDS) {
592 struct drm_display_mode *native_mode = nouveau_encoder_connector_get(nv_encoder)->native_mode;
593 if (native_mode)
594 call_lvds_script(dev, nv_encoder->dcb, head, LVDS_PANEL_ON,
595 native_mode->clock);
596 else
597 NV_ERROR(dev, "Not restoring LVDS without native mode\n");
599 } else if (nv_encoder->dcb->type == OUTPUT_TMDS) {
600 int clock = nouveau_hw_pllvals_to_clk
601 (&dev_priv->saved_reg.crtc_reg[head].pllvals);
603 run_tmds_table(dev, nv_encoder->dcb, head, clock);
606 nv_encoder->last_dpms = NV_DPMS_CLEARED;
609 static void nv04_dfp_destroy(struct drm_encoder *encoder)
611 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
613 NV_DEBUG_KMS(encoder->dev, "\n");
615 if (get_slave_funcs(encoder))
616 get_slave_funcs(encoder)->destroy(encoder);
618 drm_encoder_cleanup(encoder);
619 kfree(nv_encoder);
622 static void nv04_tmds_slave_init(struct drm_encoder *encoder)
624 struct drm_device *dev = encoder->dev;
625 struct dcb_entry *dcb = nouveau_encoder(encoder)->dcb;
626 struct nouveau_i2c_chan *i2c = nouveau_i2c_find(dev, 2);
627 struct i2c_board_info info[] = {
629 .type = "sil164",
630 .addr = (dcb->tmdsconf.slave_addr == 0x7 ? 0x3a : 0x38),
631 .platform_data = &(struct sil164_encoder_params) {
632 SIL164_INPUT_EDGE_RISING
637 int type;
639 if (!nv_gf4_disp_arch(dev) || !i2c ||
640 get_tmds_slave(encoder))
641 return;
643 type = nouveau_i2c_identify(dev, "TMDS transmitter", info, NULL, 2);
644 if (type < 0)
645 return;
647 drm_i2c_encoder_init(dev, to_encoder_slave(encoder),
648 &i2c->adapter, &info[type]);
651 static const struct drm_encoder_helper_funcs nv04_lvds_helper_funcs = {
652 .dpms = nv04_lvds_dpms,
653 .save = nv04_dfp_save,
654 .restore = nv04_dfp_restore,
655 .mode_fixup = nv04_dfp_mode_fixup,
656 .prepare = nv04_dfp_prepare,
657 .commit = nv04_dfp_commit,
658 .mode_set = nv04_dfp_mode_set,
659 .detect = NULL,
662 static const struct drm_encoder_helper_funcs nv04_tmds_helper_funcs = {
663 .dpms = nv04_tmds_dpms,
664 .save = nv04_dfp_save,
665 .restore = nv04_dfp_restore,
666 .mode_fixup = nv04_dfp_mode_fixup,
667 .prepare = nv04_dfp_prepare,
668 .commit = nv04_dfp_commit,
669 .mode_set = nv04_dfp_mode_set,
670 .detect = NULL,
673 static const struct drm_encoder_funcs nv04_dfp_funcs = {
674 .destroy = nv04_dfp_destroy,
678 nv04_dfp_create(struct drm_connector *connector, struct dcb_entry *entry)
680 const struct drm_encoder_helper_funcs *helper;
681 struct nouveau_encoder *nv_encoder = NULL;
682 struct drm_encoder *encoder;
683 int type;
685 switch (entry->type) {
686 case OUTPUT_TMDS:
687 type = DRM_MODE_ENCODER_TMDS;
688 helper = &nv04_tmds_helper_funcs;
689 break;
690 case OUTPUT_LVDS:
691 type = DRM_MODE_ENCODER_LVDS;
692 helper = &nv04_lvds_helper_funcs;
693 break;
694 default:
695 return -EINVAL;
698 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
699 if (!nv_encoder)
700 return -ENOMEM;
702 encoder = to_drm_encoder(nv_encoder);
704 nv_encoder->dcb = entry;
705 nv_encoder->or = ffs(entry->or) - 1;
707 drm_encoder_init(connector->dev, encoder, &nv04_dfp_funcs, type);
708 drm_encoder_helper_add(encoder, helper);
710 encoder->possible_crtcs = entry->heads;
711 encoder->possible_clones = 0;
713 if (entry->type == OUTPUT_TMDS &&
714 entry->location != DCB_LOC_ON_CHIP)
715 nv04_tmds_slave_init(encoder);
717 drm_mode_connector_attach_encoder(connector, encoder);
718 return 0;