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