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