xref: /linux/drivers/gpu/drm/mgag200/mgag200_mode.c (revision 760285e7e7ab282c25b5e90816f7c47000557f4f)
1414c4531SDave Airlie /*
2414c4531SDave Airlie  * Copyright 2010 Matt Turner.
3414c4531SDave Airlie  * Copyright 2012 Red Hat
4414c4531SDave Airlie  *
5414c4531SDave Airlie  * This file is subject to the terms and conditions of the GNU General
6414c4531SDave Airlie  * Public License version 2. See the file COPYING in the main
7414c4531SDave Airlie  * directory of this archive for more details.
8414c4531SDave Airlie  *
9414c4531SDave Airlie  * Authors: Matthew Garrett
10414c4531SDave Airlie  *	    Matt Turner
11414c4531SDave Airlie  *	    Dave Airlie
12414c4531SDave Airlie  */
13414c4531SDave Airlie 
14414c4531SDave Airlie #include <linux/delay.h>
15414c4531SDave Airlie 
16*760285e7SDavid Howells #include <drm/drmP.h>
17*760285e7SDavid Howells #include <drm/drm_crtc_helper.h>
18414c4531SDave Airlie 
19414c4531SDave Airlie #include "mgag200_drv.h"
20414c4531SDave Airlie 
21414c4531SDave Airlie #define MGAG200_LUT_SIZE 256
22414c4531SDave Airlie 
23414c4531SDave Airlie /*
24414c4531SDave Airlie  * This file contains setup code for the CRTC.
25414c4531SDave Airlie  */
26414c4531SDave Airlie 
27414c4531SDave Airlie static void mga_crtc_load_lut(struct drm_crtc *crtc)
28414c4531SDave Airlie {
29414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
30414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
31414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
32414c4531SDave Airlie 	int i;
33414c4531SDave Airlie 
34414c4531SDave Airlie 	if (!crtc->enabled)
35414c4531SDave Airlie 		return;
36414c4531SDave Airlie 
37414c4531SDave Airlie 	WREG8(DAC_INDEX + MGA1064_INDEX, 0);
38414c4531SDave Airlie 
39414c4531SDave Airlie 	for (i = 0; i < MGAG200_LUT_SIZE; i++) {
40414c4531SDave Airlie 		/* VGA registers */
41414c4531SDave Airlie 		WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_r[i]);
42414c4531SDave Airlie 		WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_g[i]);
43414c4531SDave Airlie 		WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_b[i]);
44414c4531SDave Airlie 	}
45414c4531SDave Airlie }
46414c4531SDave Airlie 
47414c4531SDave Airlie static inline void mga_wait_vsync(struct mga_device *mdev)
48414c4531SDave Airlie {
49414c4531SDave Airlie 	unsigned int count = 0;
50414c4531SDave Airlie 	unsigned int status = 0;
51414c4531SDave Airlie 
52414c4531SDave Airlie 	do {
53414c4531SDave Airlie 		status = RREG32(MGAREG_Status);
54414c4531SDave Airlie 		count++;
55414c4531SDave Airlie 	} while ((status & 0x08) && (count < 250000));
56414c4531SDave Airlie 	count = 0;
57414c4531SDave Airlie 	status = 0;
58414c4531SDave Airlie 	do {
59414c4531SDave Airlie 		status = RREG32(MGAREG_Status);
60414c4531SDave Airlie 		count++;
61414c4531SDave Airlie 	} while (!(status & 0x08) && (count < 250000));
62414c4531SDave Airlie }
63414c4531SDave Airlie 
64414c4531SDave Airlie static inline void mga_wait_busy(struct mga_device *mdev)
65414c4531SDave Airlie {
66414c4531SDave Airlie 	unsigned int count = 0;
67414c4531SDave Airlie 	unsigned int status = 0;
68414c4531SDave Airlie 	do {
69414c4531SDave Airlie 		status = RREG8(MGAREG_Status + 2);
70414c4531SDave Airlie 		count++;
71414c4531SDave Airlie 	} while ((status & 0x01) && (count < 500000));
72414c4531SDave Airlie }
73414c4531SDave Airlie 
74414c4531SDave Airlie /*
75414c4531SDave Airlie  * The core passes the desired mode to the CRTC code to see whether any
76414c4531SDave Airlie  * CRTC-specific modifications need to be made to it. We're in a position
77414c4531SDave Airlie  * to just pass that straight through, so this does nothing
78414c4531SDave Airlie  */
79414c4531SDave Airlie static bool mga_crtc_mode_fixup(struct drm_crtc *crtc,
80e811f5aeSLaurent Pinchart 				const struct drm_display_mode *mode,
81414c4531SDave Airlie 				struct drm_display_mode *adjusted_mode)
82414c4531SDave Airlie {
83414c4531SDave Airlie 	return true;
84414c4531SDave Airlie }
85414c4531SDave Airlie 
86414c4531SDave Airlie static int mga_g200se_set_plls(struct mga_device *mdev, long clock)
87414c4531SDave Airlie {
88414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
89414c4531SDave Airlie 	unsigned int delta, tmpdelta, permitteddelta;
90414c4531SDave Airlie 	unsigned int testp, testm, testn;
91414c4531SDave Airlie 	unsigned int p, m, n;
92414c4531SDave Airlie 	unsigned int computed;
93414c4531SDave Airlie 
94414c4531SDave Airlie 	m = n = p = 0;
95414c4531SDave Airlie 	vcomax = 320000;
96414c4531SDave Airlie 	vcomin = 160000;
97414c4531SDave Airlie 	pllreffreq = 25000;
98414c4531SDave Airlie 
99414c4531SDave Airlie 	delta = 0xffffffff;
100414c4531SDave Airlie 	permitteddelta = clock * 5 / 1000;
101414c4531SDave Airlie 
102414c4531SDave Airlie 	for (testp = 8; testp > 0; testp /= 2) {
103414c4531SDave Airlie 		if (clock * testp > vcomax)
104414c4531SDave Airlie 			continue;
105414c4531SDave Airlie 		if (clock * testp < vcomin)
106414c4531SDave Airlie 			continue;
107414c4531SDave Airlie 
108414c4531SDave Airlie 		for (testn = 17; testn < 256; testn++) {
109414c4531SDave Airlie 			for (testm = 1; testm < 32; testm++) {
110414c4531SDave Airlie 				computed = (pllreffreq * testn) /
111414c4531SDave Airlie 					(testm * testp);
112414c4531SDave Airlie 				if (computed > clock)
113414c4531SDave Airlie 					tmpdelta = computed - clock;
114414c4531SDave Airlie 				else
115414c4531SDave Airlie 					tmpdelta = clock - computed;
116414c4531SDave Airlie 				if (tmpdelta < delta) {
117414c4531SDave Airlie 					delta = tmpdelta;
118414c4531SDave Airlie 					m = testm - 1;
119414c4531SDave Airlie 					n = testn - 1;
120414c4531SDave Airlie 					p = testp - 1;
121414c4531SDave Airlie 				}
122414c4531SDave Airlie 			}
123414c4531SDave Airlie 		}
124414c4531SDave Airlie 	}
125414c4531SDave Airlie 
126414c4531SDave Airlie 	if (delta > permitteddelta) {
127414c4531SDave Airlie 		printk(KERN_WARNING "PLL delta too large\n");
128414c4531SDave Airlie 		return 1;
129414c4531SDave Airlie 	}
130414c4531SDave Airlie 
131414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLLC_M, m);
132414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLLC_N, n);
133414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLLC_P, p);
134414c4531SDave Airlie 	return 0;
135414c4531SDave Airlie }
136414c4531SDave Airlie 
137414c4531SDave Airlie static int mga_g200wb_set_plls(struct mga_device *mdev, long clock)
138414c4531SDave Airlie {
139414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
140414c4531SDave Airlie 	unsigned int delta, tmpdelta, permitteddelta;
141414c4531SDave Airlie 	unsigned int testp, testm, testn;
142414c4531SDave Airlie 	unsigned int p, m, n;
143414c4531SDave Airlie 	unsigned int computed;
144414c4531SDave Airlie 	int i, j, tmpcount, vcount;
145414c4531SDave Airlie 	bool pll_locked = false;
146414c4531SDave Airlie 	u8 tmp;
147414c4531SDave Airlie 
148414c4531SDave Airlie 	m = n = p = 0;
149414c4531SDave Airlie 	vcomax = 550000;
150414c4531SDave Airlie 	vcomin = 150000;
151414c4531SDave Airlie 	pllreffreq = 48000;
152414c4531SDave Airlie 
153414c4531SDave Airlie 	delta = 0xffffffff;
154414c4531SDave Airlie 	permitteddelta = clock * 5 / 1000;
155414c4531SDave Airlie 
156414c4531SDave Airlie 	for (testp = 1; testp < 9; testp++) {
157414c4531SDave Airlie 		if (clock * testp > vcomax)
158414c4531SDave Airlie 			continue;
159414c4531SDave Airlie 		if (clock * testp < vcomin)
160414c4531SDave Airlie 			continue;
161414c4531SDave Airlie 
162414c4531SDave Airlie 		for (testm = 1; testm < 17; testm++) {
163414c4531SDave Airlie 			for (testn = 1; testn < 151; testn++) {
164414c4531SDave Airlie 				computed = (pllreffreq * testn) /
165414c4531SDave Airlie 					(testm * testp);
166414c4531SDave Airlie 				if (computed > clock)
167414c4531SDave Airlie 					tmpdelta = computed - clock;
168414c4531SDave Airlie 				else
169414c4531SDave Airlie 					tmpdelta = clock - computed;
170414c4531SDave Airlie 				if (tmpdelta < delta) {
171414c4531SDave Airlie 					delta = tmpdelta;
172414c4531SDave Airlie 					n = testn - 1;
173414c4531SDave Airlie 					m = (testm - 1) | ((n >> 1) & 0x80);
174414c4531SDave Airlie 					p = testp - 1;
175414c4531SDave Airlie 				}
176414c4531SDave Airlie 			}
177414c4531SDave Airlie 		}
178414c4531SDave Airlie 	}
179414c4531SDave Airlie 
180414c4531SDave Airlie 	for (i = 0; i <= 32 && pll_locked == false; i++) {
181414c4531SDave Airlie 		if (i > 0) {
182414c4531SDave Airlie 			WREG8(MGAREG_CRTC_INDEX, 0x1e);
183414c4531SDave Airlie 			tmp = RREG8(MGAREG_CRTC_DATA);
184414c4531SDave Airlie 			if (tmp < 0xff)
185414c4531SDave Airlie 				WREG8(MGAREG_CRTC_DATA, tmp+1);
186414c4531SDave Airlie 		}
187414c4531SDave Airlie 
188414c4531SDave Airlie 		/* set pixclkdis to 1 */
189414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
190414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
191414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
192414c4531SDave Airlie 		WREG_DAC(MGA1064_PIX_CLK_CTL_CLK_DIS, tmp);
193414c4531SDave Airlie 
194414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
195414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
196414c4531SDave Airlie 		tmp |= MGA1064_REMHEADCTL_CLKDIS;
197414c4531SDave Airlie 		WREG_DAC(MGA1064_REMHEADCTL, tmp);
198414c4531SDave Airlie 
199414c4531SDave Airlie 		/* select PLL Set C */
200414c4531SDave Airlie 		tmp = RREG8(MGAREG_MEM_MISC_READ);
201414c4531SDave Airlie 		tmp |= 0x3 << 2;
202414c4531SDave Airlie 		WREG8(MGAREG_MEM_MISC_WRITE, tmp);
203414c4531SDave Airlie 
204414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
205414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
206414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN | 0x80;
207414c4531SDave Airlie 		WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
208414c4531SDave Airlie 
209414c4531SDave Airlie 		udelay(500);
210414c4531SDave Airlie 
211414c4531SDave Airlie 		/* reset the PLL */
212414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_VREF_CTL);
213414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
214414c4531SDave Airlie 		tmp &= ~0x04;
215414c4531SDave Airlie 		WREG_DAC(MGA1064_VREF_CTL, tmp);
216414c4531SDave Airlie 
217414c4531SDave Airlie 		udelay(50);
218414c4531SDave Airlie 
219414c4531SDave Airlie 		/* program pixel pll register */
220414c4531SDave Airlie 		WREG_DAC(MGA1064_WB_PIX_PLLC_N, n);
221414c4531SDave Airlie 		WREG_DAC(MGA1064_WB_PIX_PLLC_M, m);
222414c4531SDave Airlie 		WREG_DAC(MGA1064_WB_PIX_PLLC_P, p);
223414c4531SDave Airlie 
224414c4531SDave Airlie 		udelay(50);
225414c4531SDave Airlie 
226414c4531SDave Airlie 		/* turn pll on */
227414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_VREF_CTL);
228414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
229414c4531SDave Airlie 		tmp |= 0x04;
230414c4531SDave Airlie 		WREG_DAC(MGA1064_VREF_CTL, tmp);
231414c4531SDave Airlie 
232414c4531SDave Airlie 		udelay(500);
233414c4531SDave Airlie 
234414c4531SDave Airlie 		/* select the pixel pll */
235414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
236414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
237414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
238414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
239414c4531SDave Airlie 		WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
240414c4531SDave Airlie 
241414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
242414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
243414c4531SDave Airlie 		tmp &= ~MGA1064_REMHEADCTL_CLKSL_MSK;
244414c4531SDave Airlie 		tmp |= MGA1064_REMHEADCTL_CLKSL_PLL;
245414c4531SDave Airlie 		WREG_DAC(MGA1064_REMHEADCTL, tmp);
246414c4531SDave Airlie 
247414c4531SDave Airlie 		/* reset dotclock rate bit */
248414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 1);
249414c4531SDave Airlie 		tmp = RREG8(MGAREG_SEQ_DATA);
250414c4531SDave Airlie 		tmp &= ~0x8;
251414c4531SDave Airlie 		WREG8(MGAREG_SEQ_DATA, tmp);
252414c4531SDave Airlie 
253414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
254414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
255414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
256414c4531SDave Airlie 		WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
257414c4531SDave Airlie 
258414c4531SDave Airlie 		vcount = RREG8(MGAREG_VCOUNT);
259414c4531SDave Airlie 
260414c4531SDave Airlie 		for (j = 0; j < 30 && pll_locked == false; j++) {
261414c4531SDave Airlie 			tmpcount = RREG8(MGAREG_VCOUNT);
262414c4531SDave Airlie 			if (tmpcount < vcount)
263414c4531SDave Airlie 				vcount = 0;
264414c4531SDave Airlie 			if ((tmpcount - vcount) > 2)
265414c4531SDave Airlie 				pll_locked = true;
266414c4531SDave Airlie 			else
267414c4531SDave Airlie 				udelay(5);
268414c4531SDave Airlie 		}
269414c4531SDave Airlie 	}
270414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
271414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
272414c4531SDave Airlie 	tmp &= ~MGA1064_REMHEADCTL_CLKDIS;
273414c4531SDave Airlie 	WREG_DAC(MGA1064_REMHEADCTL, tmp);
274414c4531SDave Airlie 	return 0;
275414c4531SDave Airlie }
276414c4531SDave Airlie 
277414c4531SDave Airlie static int mga_g200ev_set_plls(struct mga_device *mdev, long clock)
278414c4531SDave Airlie {
279414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
280414c4531SDave Airlie 	unsigned int delta, tmpdelta, permitteddelta;
281414c4531SDave Airlie 	unsigned int testp, testm, testn;
282414c4531SDave Airlie 	unsigned int p, m, n;
283414c4531SDave Airlie 	unsigned int computed;
284414c4531SDave Airlie 	u8 tmp;
285414c4531SDave Airlie 
286414c4531SDave Airlie 	m = n = p = 0;
287414c4531SDave Airlie 	vcomax = 550000;
288414c4531SDave Airlie 	vcomin = 150000;
289414c4531SDave Airlie 	pllreffreq = 50000;
290414c4531SDave Airlie 
291414c4531SDave Airlie 	delta = 0xffffffff;
292414c4531SDave Airlie 	permitteddelta = clock * 5 / 1000;
293414c4531SDave Airlie 
294414c4531SDave Airlie 	for (testp = 16; testp > 0; testp--) {
295414c4531SDave Airlie 		if (clock * testp > vcomax)
296414c4531SDave Airlie 			continue;
297414c4531SDave Airlie 		if (clock * testp < vcomin)
298414c4531SDave Airlie 			continue;
299414c4531SDave Airlie 
300414c4531SDave Airlie 		for (testn = 1; testn < 257; testn++) {
301414c4531SDave Airlie 			for (testm = 1; testm < 17; testm++) {
302414c4531SDave Airlie 				computed = (pllreffreq * testn) /
303414c4531SDave Airlie 					(testm * testp);
304414c4531SDave Airlie 				if (computed > clock)
305414c4531SDave Airlie 					tmpdelta = computed - clock;
306414c4531SDave Airlie 				else
307414c4531SDave Airlie 					tmpdelta = clock - computed;
308414c4531SDave Airlie 				if (tmpdelta < delta) {
309414c4531SDave Airlie 					delta = tmpdelta;
310414c4531SDave Airlie 					n = testn - 1;
311414c4531SDave Airlie 					m = testm - 1;
312414c4531SDave Airlie 					p = testp - 1;
313414c4531SDave Airlie 				}
314414c4531SDave Airlie 			}
315414c4531SDave Airlie 		}
316414c4531SDave Airlie 	}
317414c4531SDave Airlie 
318414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
319414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
320414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
321414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_CLK_CTL_CLK_DIS, tmp);
322414c4531SDave Airlie 
323414c4531SDave Airlie 	tmp = RREG8(MGAREG_MEM_MISC_READ);
324414c4531SDave Airlie 	tmp |= 0x3 << 2;
325414c4531SDave Airlie 	WREG8(MGAREG_MEM_MISC_WRITE, tmp);
326414c4531SDave Airlie 
327414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
328414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
329414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLL_STAT, tmp & ~0x40);
330414c4531SDave Airlie 
331414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
332414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
333414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
334414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
335414c4531SDave Airlie 
336414c4531SDave Airlie 	WREG_DAC(MGA1064_EV_PIX_PLLC_M, m);
337414c4531SDave Airlie 	WREG_DAC(MGA1064_EV_PIX_PLLC_N, n);
338414c4531SDave Airlie 	WREG_DAC(MGA1064_EV_PIX_PLLC_P, p);
339414c4531SDave Airlie 
340414c4531SDave Airlie 	udelay(50);
341414c4531SDave Airlie 
342414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
343414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
344414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
345414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
346414c4531SDave Airlie 
347414c4531SDave Airlie 	udelay(500);
348414c4531SDave Airlie 
349414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
350414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
351414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
352414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
353414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
354414c4531SDave Airlie 
355414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
356414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
357414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLL_STAT, tmp | 0x40);
358414c4531SDave Airlie 
359414c4531SDave Airlie 	tmp = RREG8(MGAREG_MEM_MISC_READ);
360414c4531SDave Airlie 	tmp |= (0x3 << 2);
361414c4531SDave Airlie 	WREG8(MGAREG_MEM_MISC_WRITE, tmp);
362414c4531SDave Airlie 
363414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
364414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
365414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
366414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
367414c4531SDave Airlie 
368414c4531SDave Airlie 	return 0;
369414c4531SDave Airlie }
370414c4531SDave Airlie 
371414c4531SDave Airlie static int mga_g200eh_set_plls(struct mga_device *mdev, long clock)
372414c4531SDave Airlie {
373414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
374414c4531SDave Airlie 	unsigned int delta, tmpdelta, permitteddelta;
375414c4531SDave Airlie 	unsigned int testp, testm, testn;
376414c4531SDave Airlie 	unsigned int p, m, n;
377414c4531SDave Airlie 	unsigned int computed;
378414c4531SDave Airlie 	int i, j, tmpcount, vcount;
379414c4531SDave Airlie 	u8 tmp;
380414c4531SDave Airlie 	bool pll_locked = false;
381414c4531SDave Airlie 
382414c4531SDave Airlie 	m = n = p = 0;
383414c4531SDave Airlie 	vcomax = 800000;
384414c4531SDave Airlie 	vcomin = 400000;
385414c4531SDave Airlie 	pllreffreq = 3333;
386414c4531SDave Airlie 
387414c4531SDave Airlie 	delta = 0xffffffff;
388414c4531SDave Airlie 	permitteddelta = clock * 5 / 1000;
389414c4531SDave Airlie 
390414c4531SDave Airlie 	for (testp = 16; testp > 0; testp--) {
391414c4531SDave Airlie 		if (clock * testp > vcomax)
392414c4531SDave Airlie 			continue;
393414c4531SDave Airlie 		if (clock * testp < vcomin)
394414c4531SDave Airlie 			continue;
395414c4531SDave Airlie 
396414c4531SDave Airlie 		for (testm = 1; testm < 33; testm++) {
397414c4531SDave Airlie 			for (testn = 1; testn < 257; testn++) {
398414c4531SDave Airlie 				computed = (pllreffreq * testn) /
399414c4531SDave Airlie 					(testm * testp);
400414c4531SDave Airlie 				if (computed > clock)
401414c4531SDave Airlie 					tmpdelta = computed - clock;
402414c4531SDave Airlie 				else
403414c4531SDave Airlie 					tmpdelta = clock - computed;
404414c4531SDave Airlie 				if (tmpdelta < delta) {
405414c4531SDave Airlie 					delta = tmpdelta;
406414c4531SDave Airlie 					n = testn - 1;
407414c4531SDave Airlie 					m = (testm - 1) | ((n >> 1) & 0x80);
408414c4531SDave Airlie 					p = testp - 1;
409414c4531SDave Airlie 				}
410414c4531SDave Airlie 				if ((clock * testp) >= 600000)
411414c4531SDave Airlie 					p |= 80;
412414c4531SDave Airlie 			}
413414c4531SDave Airlie 		}
414414c4531SDave Airlie 	}
415414c4531SDave Airlie 	for (i = 0; i <= 32 && pll_locked == false; i++) {
416414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
417414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
418414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
419414c4531SDave Airlie 		WREG_DAC(MGA1064_PIX_CLK_CTL_CLK_DIS, tmp);
420414c4531SDave Airlie 
421414c4531SDave Airlie 		tmp = RREG8(MGAREG_MEM_MISC_READ);
422414c4531SDave Airlie 		tmp |= 0x3 << 2;
423414c4531SDave Airlie 		WREG8(MGAREG_MEM_MISC_WRITE, tmp);
424414c4531SDave Airlie 
425414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
426414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
427414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
428414c4531SDave Airlie 		WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
429414c4531SDave Airlie 
430414c4531SDave Airlie 		udelay(500);
431414c4531SDave Airlie 
432414c4531SDave Airlie 		WREG_DAC(MGA1064_EH_PIX_PLLC_M, m);
433414c4531SDave Airlie 		WREG_DAC(MGA1064_EH_PIX_PLLC_N, n);
434414c4531SDave Airlie 		WREG_DAC(MGA1064_EH_PIX_PLLC_P, p);
435414c4531SDave Airlie 
436414c4531SDave Airlie 		udelay(500);
437414c4531SDave Airlie 
438414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
439414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
440414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
441414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
442414c4531SDave Airlie 		WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
443414c4531SDave Airlie 
444414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
445414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
446414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
447414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
448414c4531SDave Airlie 		WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
449414c4531SDave Airlie 
450414c4531SDave Airlie 		vcount = RREG8(MGAREG_VCOUNT);
451414c4531SDave Airlie 
452414c4531SDave Airlie 		for (j = 0; j < 30 && pll_locked == false; j++) {
453414c4531SDave Airlie 			tmpcount = RREG8(MGAREG_VCOUNT);
454414c4531SDave Airlie 			if (tmpcount < vcount)
455414c4531SDave Airlie 				vcount = 0;
456414c4531SDave Airlie 			if ((tmpcount - vcount) > 2)
457414c4531SDave Airlie 				pll_locked = true;
458414c4531SDave Airlie 			else
459414c4531SDave Airlie 				udelay(5);
460414c4531SDave Airlie 		}
461414c4531SDave Airlie 	}
462414c4531SDave Airlie 
463414c4531SDave Airlie 	return 0;
464414c4531SDave Airlie }
465414c4531SDave Airlie 
466414c4531SDave Airlie static int mga_g200er_set_plls(struct mga_device *mdev, long clock)
467414c4531SDave Airlie {
468414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
469414c4531SDave Airlie 	unsigned int delta, tmpdelta;
4709830605dSDave Airlie 	int testr, testn, testm, testo;
471414c4531SDave Airlie 	unsigned int p, m, n;
4729830605dSDave Airlie 	unsigned int computed, vco;
473414c4531SDave Airlie 	int tmp;
4749830605dSDave Airlie 	const unsigned int m_div_val[] = { 1, 2, 4, 8 };
475414c4531SDave Airlie 
476414c4531SDave Airlie 	m = n = p = 0;
477414c4531SDave Airlie 	vcomax = 1488000;
478414c4531SDave Airlie 	vcomin = 1056000;
479414c4531SDave Airlie 	pllreffreq = 48000;
480414c4531SDave Airlie 
481414c4531SDave Airlie 	delta = 0xffffffff;
482414c4531SDave Airlie 
483414c4531SDave Airlie 	for (testr = 0; testr < 4; testr++) {
484414c4531SDave Airlie 		if (delta == 0)
485414c4531SDave Airlie 			break;
486414c4531SDave Airlie 		for (testn = 5; testn < 129; testn++) {
487414c4531SDave Airlie 			if (delta == 0)
488414c4531SDave Airlie 				break;
489414c4531SDave Airlie 			for (testm = 3; testm >= 0; testm--) {
490414c4531SDave Airlie 				if (delta == 0)
491414c4531SDave Airlie 					break;
492414c4531SDave Airlie 				for (testo = 5; testo < 33; testo++) {
4939830605dSDave Airlie 					vco = pllreffreq * (testn + 1) /
494414c4531SDave Airlie 						(testr + 1);
4959830605dSDave Airlie 					if (vco < vcomin)
496414c4531SDave Airlie 						continue;
4979830605dSDave Airlie 					if (vco > vcomax)
498414c4531SDave Airlie 						continue;
4999830605dSDave Airlie 					computed = vco / (m_div_val[testm] * (testo + 1));
500414c4531SDave Airlie 					if (computed > clock)
501414c4531SDave Airlie 						tmpdelta = computed - clock;
502414c4531SDave Airlie 					else
503414c4531SDave Airlie 						tmpdelta = clock - computed;
504414c4531SDave Airlie 					if (tmpdelta < delta) {
505414c4531SDave Airlie 						delta = tmpdelta;
506414c4531SDave Airlie 						m = testm | (testo << 3);
507414c4531SDave Airlie 						n = testn;
508414c4531SDave Airlie 						p = testr | (testr << 3);
509414c4531SDave Airlie 					}
510414c4531SDave Airlie 				}
511414c4531SDave Airlie 			}
512414c4531SDave Airlie 		}
513414c4531SDave Airlie 	}
514414c4531SDave Airlie 
515414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
516414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
517414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
518414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_CLK_CTL_CLK_DIS, tmp);
519414c4531SDave Airlie 
520414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
521414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
522414c4531SDave Airlie 	tmp |= MGA1064_REMHEADCTL_CLKDIS;
523414c4531SDave Airlie 	WREG_DAC(MGA1064_REMHEADCTL, tmp);
524414c4531SDave Airlie 
525414c4531SDave Airlie 	tmp = RREG8(MGAREG_MEM_MISC_READ);
526414c4531SDave Airlie 	tmp |= (0x3<<2) | 0xc0;
527414c4531SDave Airlie 	WREG8(MGAREG_MEM_MISC_WRITE, tmp);
528414c4531SDave Airlie 
529414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
530414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
531414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
532414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
533414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
534414c4531SDave Airlie 
535414c4531SDave Airlie 	udelay(500);
536414c4531SDave Airlie 
537414c4531SDave Airlie 	WREG_DAC(MGA1064_ER_PIX_PLLC_N, n);
538414c4531SDave Airlie 	WREG_DAC(MGA1064_ER_PIX_PLLC_M, m);
539414c4531SDave Airlie 	WREG_DAC(MGA1064_ER_PIX_PLLC_P, p);
540414c4531SDave Airlie 
541414c4531SDave Airlie 	udelay(50);
542414c4531SDave Airlie 
543414c4531SDave Airlie 	return 0;
544414c4531SDave Airlie }
545414c4531SDave Airlie 
546414c4531SDave Airlie static int mga_crtc_set_plls(struct mga_device *mdev, long clock)
547414c4531SDave Airlie {
548414c4531SDave Airlie 	switch(mdev->type) {
549414c4531SDave Airlie 	case G200_SE_A:
550414c4531SDave Airlie 	case G200_SE_B:
551414c4531SDave Airlie 		return mga_g200se_set_plls(mdev, clock);
552414c4531SDave Airlie 		break;
553414c4531SDave Airlie 	case G200_WB:
554414c4531SDave Airlie 		return mga_g200wb_set_plls(mdev, clock);
555414c4531SDave Airlie 		break;
556414c4531SDave Airlie 	case G200_EV:
557414c4531SDave Airlie 		return mga_g200ev_set_plls(mdev, clock);
558414c4531SDave Airlie 		break;
559414c4531SDave Airlie 	case G200_EH:
560414c4531SDave Airlie 		return mga_g200eh_set_plls(mdev, clock);
561414c4531SDave Airlie 		break;
562414c4531SDave Airlie 	case G200_ER:
563414c4531SDave Airlie 		return mga_g200er_set_plls(mdev, clock);
564414c4531SDave Airlie 		break;
565414c4531SDave Airlie 	}
566414c4531SDave Airlie 	return 0;
567414c4531SDave Airlie }
568414c4531SDave Airlie 
569414c4531SDave Airlie static void mga_g200wb_prepare(struct drm_crtc *crtc)
570414c4531SDave Airlie {
571414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
572414c4531SDave Airlie 	u8 tmp;
573414c4531SDave Airlie 	int iter_max;
574414c4531SDave Airlie 
575414c4531SDave Airlie 	/* 1- The first step is to warn the BMC of an upcoming mode change.
576414c4531SDave Airlie 	 * We are putting the misc<0> to output.*/
577414c4531SDave Airlie 
578414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_GEN_IO_CTL);
579414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
580414c4531SDave Airlie 	tmp |= 0x10;
581414c4531SDave Airlie 	WREG_DAC(MGA1064_GEN_IO_CTL, tmp);
582414c4531SDave Airlie 
583414c4531SDave Airlie 	/* we are putting a 1 on the misc<0> line */
584414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
585414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
586414c4531SDave Airlie 	tmp |= 0x10;
587414c4531SDave Airlie 	WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
588414c4531SDave Airlie 
589414c4531SDave Airlie 	/* 2- Second step to mask and further scan request
590414c4531SDave Airlie 	 * This will be done by asserting the remfreqmsk bit (XSPAREREG<7>)
591414c4531SDave Airlie 	 */
592414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_SPAREREG);
593414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
594414c4531SDave Airlie 	tmp |= 0x80;
595414c4531SDave Airlie 	WREG_DAC(MGA1064_SPAREREG, tmp);
596414c4531SDave Airlie 
597414c4531SDave Airlie 	/* 3a- the third step is to verifu if there is an active scan
598414c4531SDave Airlie 	 * We are searching for a 0 on remhsyncsts <XSPAREREG<0>)
599414c4531SDave Airlie 	 */
600414c4531SDave Airlie 	iter_max = 300;
601414c4531SDave Airlie 	while (!(tmp & 0x1) && iter_max) {
602414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_SPAREREG);
603414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
604414c4531SDave Airlie 		udelay(1000);
605414c4531SDave Airlie 		iter_max--;
606414c4531SDave Airlie 	}
607414c4531SDave Airlie 
608414c4531SDave Airlie 	/* 3b- this step occurs only if the remove is actually scanning
609414c4531SDave Airlie 	 * we are waiting for the end of the frame which is a 1 on
610414c4531SDave Airlie 	 * remvsyncsts (XSPAREREG<1>)
611414c4531SDave Airlie 	 */
612414c4531SDave Airlie 	if (iter_max) {
613414c4531SDave Airlie 		iter_max = 300;
614414c4531SDave Airlie 		while ((tmp & 0x2) && iter_max) {
615414c4531SDave Airlie 			WREG8(DAC_INDEX, MGA1064_SPAREREG);
616414c4531SDave Airlie 			tmp = RREG8(DAC_DATA);
617414c4531SDave Airlie 			udelay(1000);
618414c4531SDave Airlie 			iter_max--;
619414c4531SDave Airlie 		}
620414c4531SDave Airlie 	}
621414c4531SDave Airlie }
622414c4531SDave Airlie 
623414c4531SDave Airlie static void mga_g200wb_commit(struct drm_crtc *crtc)
624414c4531SDave Airlie {
625414c4531SDave Airlie 	u8 tmp;
626414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
627414c4531SDave Airlie 
628414c4531SDave Airlie 	/* 1- The first step is to ensure that the vrsten and hrsten are set */
629414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_INDEX, 1);
630414c4531SDave Airlie 	tmp = RREG8(MGAREG_CRTCEXT_DATA);
631414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_DATA, tmp | 0x88);
632414c4531SDave Airlie 
633414c4531SDave Airlie 	/* 2- second step is to assert the rstlvl2 */
634414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
635414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
636414c4531SDave Airlie 	tmp |= 0x8;
637414c4531SDave Airlie 	WREG8(DAC_DATA, tmp);
638414c4531SDave Airlie 
639414c4531SDave Airlie 	/* wait 10 us */
640414c4531SDave Airlie 	udelay(10);
641414c4531SDave Airlie 
642414c4531SDave Airlie 	/* 3- deassert rstlvl2 */
643414c4531SDave Airlie 	tmp &= ~0x08;
644414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
645414c4531SDave Airlie 	WREG8(DAC_DATA, tmp);
646414c4531SDave Airlie 
647414c4531SDave Airlie 	/* 4- remove mask of scan request */
648414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_SPAREREG);
649414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
650414c4531SDave Airlie 	tmp &= ~0x80;
651414c4531SDave Airlie 	WREG8(DAC_DATA, tmp);
652414c4531SDave Airlie 
653414c4531SDave Airlie 	/* 5- put back a 0 on the misc<0> line */
654414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
655414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
656414c4531SDave Airlie 	tmp &= ~0x10;
657414c4531SDave Airlie 	WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
658414c4531SDave Airlie }
659414c4531SDave Airlie 
660414c4531SDave Airlie 
661414c4531SDave Airlie void mga_set_start_address(struct drm_crtc *crtc, unsigned offset)
662414c4531SDave Airlie {
663414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
664414c4531SDave Airlie 	u32 addr;
665414c4531SDave Airlie 	int count;
666414c4531SDave Airlie 
667414c4531SDave Airlie 	while (RREG8(0x1fda) & 0x08);
668414c4531SDave Airlie 	while (!(RREG8(0x1fda) & 0x08));
669414c4531SDave Airlie 
670414c4531SDave Airlie 	count = RREG8(MGAREG_VCOUNT) + 2;
671414c4531SDave Airlie 	while (RREG8(MGAREG_VCOUNT) < count);
672414c4531SDave Airlie 
673414c4531SDave Airlie 	addr = offset >> 2;
674414c4531SDave Airlie 	WREG_CRT(0x0d, (u8)(addr & 0xff));
675414c4531SDave Airlie 	WREG_CRT(0x0c, (u8)(addr >> 8) & 0xff);
676414c4531SDave Airlie 	WREG_CRT(0xaf, (u8)(addr >> 16) & 0xf);
677414c4531SDave Airlie }
678414c4531SDave Airlie 
679414c4531SDave Airlie 
680414c4531SDave Airlie /* ast is different - we will force move buffers out of VRAM */
681414c4531SDave Airlie static int mga_crtc_do_set_base(struct drm_crtc *crtc,
682414c4531SDave Airlie 				struct drm_framebuffer *fb,
683414c4531SDave Airlie 				int x, int y, int atomic)
684414c4531SDave Airlie {
685414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
686414c4531SDave Airlie 	struct drm_gem_object *obj;
687414c4531SDave Airlie 	struct mga_framebuffer *mga_fb;
688414c4531SDave Airlie 	struct mgag200_bo *bo;
689414c4531SDave Airlie 	int ret;
690414c4531SDave Airlie 	u64 gpu_addr;
691414c4531SDave Airlie 
692414c4531SDave Airlie 	/* push the previous fb to system ram */
693414c4531SDave Airlie 	if (!atomic && fb) {
694414c4531SDave Airlie 		mga_fb = to_mga_framebuffer(fb);
695414c4531SDave Airlie 		obj = mga_fb->obj;
696414c4531SDave Airlie 		bo = gem_to_mga_bo(obj);
697414c4531SDave Airlie 		ret = mgag200_bo_reserve(bo, false);
698414c4531SDave Airlie 		if (ret)
699414c4531SDave Airlie 			return ret;
700414c4531SDave Airlie 		mgag200_bo_push_sysram(bo);
701414c4531SDave Airlie 		mgag200_bo_unreserve(bo);
702414c4531SDave Airlie 	}
703414c4531SDave Airlie 
704414c4531SDave Airlie 	mga_fb = to_mga_framebuffer(crtc->fb);
705414c4531SDave Airlie 	obj = mga_fb->obj;
706414c4531SDave Airlie 	bo = gem_to_mga_bo(obj);
707414c4531SDave Airlie 
708414c4531SDave Airlie 	ret = mgag200_bo_reserve(bo, false);
709414c4531SDave Airlie 	if (ret)
710414c4531SDave Airlie 		return ret;
711414c4531SDave Airlie 
712414c4531SDave Airlie 	ret = mgag200_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
713414c4531SDave Airlie 	if (ret) {
714414c4531SDave Airlie 		mgag200_bo_unreserve(bo);
715414c4531SDave Airlie 		return ret;
716414c4531SDave Airlie 	}
717414c4531SDave Airlie 
718414c4531SDave Airlie 	if (&mdev->mfbdev->mfb == mga_fb) {
719414c4531SDave Airlie 		/* if pushing console in kmap it */
720414c4531SDave Airlie 		ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
721414c4531SDave Airlie 		if (ret)
722414c4531SDave Airlie 			DRM_ERROR("failed to kmap fbcon\n");
723414c4531SDave Airlie 
724414c4531SDave Airlie 	}
725414c4531SDave Airlie 	mgag200_bo_unreserve(bo);
726414c4531SDave Airlie 
727414c4531SDave Airlie 	DRM_INFO("mga base %llx\n", gpu_addr);
728414c4531SDave Airlie 
729414c4531SDave Airlie 	mga_set_start_address(crtc, (u32)gpu_addr);
730414c4531SDave Airlie 
731414c4531SDave Airlie 	return 0;
732414c4531SDave Airlie }
733414c4531SDave Airlie 
734414c4531SDave Airlie static int mga_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
735414c4531SDave Airlie 				  struct drm_framebuffer *old_fb)
736414c4531SDave Airlie {
737414c4531SDave Airlie 	return mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
738414c4531SDave Airlie }
739414c4531SDave Airlie 
740414c4531SDave Airlie static int mga_crtc_mode_set(struct drm_crtc *crtc,
741414c4531SDave Airlie 				struct drm_display_mode *mode,
742414c4531SDave Airlie 				struct drm_display_mode *adjusted_mode,
743414c4531SDave Airlie 				int x, int y, struct drm_framebuffer *old_fb)
744414c4531SDave Airlie {
745414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
746414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
747414c4531SDave Airlie 	int hdisplay, hsyncstart, hsyncend, htotal;
748414c4531SDave Airlie 	int vdisplay, vsyncstart, vsyncend, vtotal;
749414c4531SDave Airlie 	int pitch;
750414c4531SDave Airlie 	int option = 0, option2 = 0;
751414c4531SDave Airlie 	int i;
752414c4531SDave Airlie 	unsigned char misc = 0;
753414c4531SDave Airlie 	unsigned char ext_vga[6];
754414c4531SDave Airlie 	unsigned char ext_vga_index24;
755414c4531SDave Airlie 	unsigned char dac_index90 = 0;
756414c4531SDave Airlie 	u8 bppshift;
757414c4531SDave Airlie 
758414c4531SDave Airlie 	static unsigned char dacvalue[] = {
759414c4531SDave Airlie 		/* 0x00: */        0,    0,    0,    0,    0,    0, 0x00,    0,
760414c4531SDave Airlie 		/* 0x08: */        0,    0,    0,    0,    0,    0,    0,    0,
761414c4531SDave Airlie 		/* 0x10: */        0,    0,    0,    0,    0,    0,    0,    0,
762414c4531SDave Airlie 		/* 0x18: */     0x00,    0, 0xC9, 0xFF, 0xBF, 0x20, 0x1F, 0x20,
763414c4531SDave Airlie 		/* 0x20: */     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
764414c4531SDave Airlie 		/* 0x28: */     0x00, 0x00, 0x00, 0x00,    0,    0,    0, 0x40,
765414c4531SDave Airlie 		/* 0x30: */     0x00, 0xB0, 0x00, 0xC2, 0x34, 0x14, 0x02, 0x83,
766414c4531SDave Airlie 		/* 0x38: */     0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3A,
767414c4531SDave Airlie 		/* 0x40: */        0,    0,    0,    0,    0,    0,    0,    0,
768414c4531SDave Airlie 		/* 0x48: */        0,    0,    0,    0,    0,    0,    0,    0
769414c4531SDave Airlie 	};
770414c4531SDave Airlie 
771414c4531SDave Airlie 	bppshift = mdev->bpp_shifts[(crtc->fb->bits_per_pixel >> 3) - 1];
772414c4531SDave Airlie 
773414c4531SDave Airlie 	switch (mdev->type) {
774414c4531SDave Airlie 	case G200_SE_A:
775414c4531SDave Airlie 	case G200_SE_B:
776414c4531SDave Airlie 		dacvalue[MGA1064_VREF_CTL] = 0x03;
777414c4531SDave Airlie 		dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
778414c4531SDave Airlie 		dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_DAC_EN |
779414c4531SDave Airlie 					     MGA1064_MISC_CTL_VGA8 |
780414c4531SDave Airlie 					     MGA1064_MISC_CTL_DAC_RAM_CS;
781414c4531SDave Airlie 		if (mdev->has_sdram)
782414c4531SDave Airlie 			option = 0x40049120;
783414c4531SDave Airlie 		else
784414c4531SDave Airlie 			option = 0x4004d120;
785414c4531SDave Airlie 		option2 = 0x00008000;
786414c4531SDave Airlie 		break;
787414c4531SDave Airlie 	case G200_WB:
788414c4531SDave Airlie 		dacvalue[MGA1064_VREF_CTL] = 0x07;
789414c4531SDave Airlie 		option = 0x41049120;
790414c4531SDave Airlie 		option2 = 0x0000b000;
791414c4531SDave Airlie 		break;
792414c4531SDave Airlie 	case G200_EV:
793414c4531SDave Airlie 		dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
794414c4531SDave Airlie 		dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
795414c4531SDave Airlie 					     MGA1064_MISC_CTL_DAC_RAM_CS;
796414c4531SDave Airlie 		option = 0x00000120;
797414c4531SDave Airlie 		option2 = 0x0000b000;
798414c4531SDave Airlie 		break;
799414c4531SDave Airlie 	case G200_EH:
800414c4531SDave Airlie 		dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
801414c4531SDave Airlie 					     MGA1064_MISC_CTL_DAC_RAM_CS;
802414c4531SDave Airlie 		option = 0x00000120;
803414c4531SDave Airlie 		option2 = 0x0000b000;
804414c4531SDave Airlie 		break;
805414c4531SDave Airlie 	case G200_ER:
806414c4531SDave Airlie 		dac_index90 = 0;
807414c4531SDave Airlie 		break;
808414c4531SDave Airlie 	}
809414c4531SDave Airlie 
810414c4531SDave Airlie 	switch (crtc->fb->bits_per_pixel) {
811414c4531SDave Airlie 	case 8:
812414c4531SDave Airlie 		dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_8bits;
813414c4531SDave Airlie 		break;
814414c4531SDave Airlie 	case 16:
815414c4531SDave Airlie 		if (crtc->fb->depth == 15)
816414c4531SDave Airlie 			dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_15bits;
817414c4531SDave Airlie 		else
818414c4531SDave Airlie 			dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_16bits;
819414c4531SDave Airlie 		break;
820414c4531SDave Airlie 	case 24:
821414c4531SDave Airlie 		dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_24bits;
822414c4531SDave Airlie 		break;
823414c4531SDave Airlie 	case 32:
824414c4531SDave Airlie 		dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_32_24bits;
825414c4531SDave Airlie 		break;
826414c4531SDave Airlie 	}
827414c4531SDave Airlie 
828414c4531SDave Airlie 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
829414c4531SDave Airlie 		misc |= 0x40;
830414c4531SDave Airlie 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
831414c4531SDave Airlie 		misc |= 0x80;
832414c4531SDave Airlie 
833414c4531SDave Airlie 
834414c4531SDave Airlie 	for (i = 0; i < sizeof(dacvalue); i++) {
835414c4531SDave Airlie 		if ((i <= 0x03) ||
836414c4531SDave Airlie 		    (i == 0x07) ||
837414c4531SDave Airlie 		    (i == 0x0b) ||
838414c4531SDave Airlie 		    (i == 0x0f) ||
839414c4531SDave Airlie 		    ((i >= 0x13) && (i <= 0x17)) ||
840414c4531SDave Airlie 		    (i == 0x1b) ||
841414c4531SDave Airlie 		    (i == 0x1c) ||
842414c4531SDave Airlie 		    ((i >= 0x1f) && (i <= 0x29)) ||
843414c4531SDave Airlie 		    ((i >= 0x30) && (i <= 0x37)))
844414c4531SDave Airlie 			continue;
845414c4531SDave Airlie 		if (IS_G200_SE(mdev) &&
846414c4531SDave Airlie 		    ((i == 0x2c) || (i == 0x2d) || (i == 0x2e)))
847414c4531SDave Airlie 			continue;
848414c4531SDave Airlie 		if ((mdev->type == G200_EV || mdev->type == G200_WB || mdev->type == G200_EH) &&
849414c4531SDave Airlie 		    (i >= 0x44) && (i <= 0x4e))
850414c4531SDave Airlie 			continue;
851414c4531SDave Airlie 
852414c4531SDave Airlie 		WREG_DAC(i, dacvalue[i]);
853414c4531SDave Airlie 	}
854414c4531SDave Airlie 
855414c4531SDave Airlie 	if (mdev->type == G200_ER) {
856414c4531SDave Airlie 		WREG_DAC(0x90, dac_index90);
857414c4531SDave Airlie 	}
858414c4531SDave Airlie 
859414c4531SDave Airlie 
860414c4531SDave Airlie 	if (option)
861414c4531SDave Airlie 		pci_write_config_dword(dev->pdev, PCI_MGA_OPTION, option);
862414c4531SDave Airlie 	if (option2)
863414c4531SDave Airlie 		pci_write_config_dword(dev->pdev, PCI_MGA_OPTION2, option2);
864414c4531SDave Airlie 
865414c4531SDave Airlie 	WREG_SEQ(2, 0xf);
866414c4531SDave Airlie 	WREG_SEQ(3, 0);
867414c4531SDave Airlie 	WREG_SEQ(4, 0xe);
868414c4531SDave Airlie 
869414c4531SDave Airlie 	pitch = crtc->fb->pitches[0] / (crtc->fb->bits_per_pixel / 8);
870414c4531SDave Airlie 	if (crtc->fb->bits_per_pixel == 24)
871414c4531SDave Airlie 		pitch = pitch >> (4 - bppshift);
872414c4531SDave Airlie 	else
873414c4531SDave Airlie 		pitch = pitch >> (4 - bppshift);
874414c4531SDave Airlie 
875414c4531SDave Airlie 	hdisplay = mode->hdisplay / 8 - 1;
876414c4531SDave Airlie 	hsyncstart = mode->hsync_start / 8 - 1;
877414c4531SDave Airlie 	hsyncend = mode->hsync_end / 8 - 1;
878414c4531SDave Airlie 	htotal = mode->htotal / 8 - 1;
879414c4531SDave Airlie 
880414c4531SDave Airlie 	/* Work around hardware quirk */
881414c4531SDave Airlie 	if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
882414c4531SDave Airlie 		htotal++;
883414c4531SDave Airlie 
884414c4531SDave Airlie 	vdisplay = mode->vdisplay - 1;
885414c4531SDave Airlie 	vsyncstart = mode->vsync_start - 1;
886414c4531SDave Airlie 	vsyncend = mode->vsync_end - 1;
887414c4531SDave Airlie 	vtotal = mode->vtotal - 2;
888414c4531SDave Airlie 
889414c4531SDave Airlie 	WREG_GFX(0, 0);
890414c4531SDave Airlie 	WREG_GFX(1, 0);
891414c4531SDave Airlie 	WREG_GFX(2, 0);
892414c4531SDave Airlie 	WREG_GFX(3, 0);
893414c4531SDave Airlie 	WREG_GFX(4, 0);
894414c4531SDave Airlie 	WREG_GFX(5, 0x40);
895414c4531SDave Airlie 	WREG_GFX(6, 0x5);
896414c4531SDave Airlie 	WREG_GFX(7, 0xf);
897414c4531SDave Airlie 	WREG_GFX(8, 0xf);
898414c4531SDave Airlie 
899414c4531SDave Airlie 	WREG_CRT(0, htotal - 4);
900414c4531SDave Airlie 	WREG_CRT(1, hdisplay);
901414c4531SDave Airlie 	WREG_CRT(2, hdisplay);
902414c4531SDave Airlie 	WREG_CRT(3, (htotal & 0x1F) | 0x80);
903414c4531SDave Airlie 	WREG_CRT(4, hsyncstart);
904414c4531SDave Airlie 	WREG_CRT(5, ((htotal & 0x20) << 2) | (hsyncend & 0x1F));
905414c4531SDave Airlie 	WREG_CRT(6, vtotal & 0xFF);
906414c4531SDave Airlie 	WREG_CRT(7, ((vtotal & 0x100) >> 8) |
907414c4531SDave Airlie 		 ((vdisplay & 0x100) >> 7) |
908414c4531SDave Airlie 		 ((vsyncstart & 0x100) >> 6) |
909414c4531SDave Airlie 		 ((vdisplay & 0x100) >> 5) |
910414c4531SDave Airlie 		 ((vdisplay & 0x100) >> 4) | /* linecomp */
911414c4531SDave Airlie 		 ((vtotal & 0x200) >> 4)|
912414c4531SDave Airlie 		 ((vdisplay & 0x200) >> 3) |
913414c4531SDave Airlie 		 ((vsyncstart & 0x200) >> 2));
914414c4531SDave Airlie 	WREG_CRT(9, ((vdisplay & 0x200) >> 4) |
915414c4531SDave Airlie 		 ((vdisplay & 0x200) >> 3));
916414c4531SDave Airlie 	WREG_CRT(10, 0);
917414c4531SDave Airlie 	WREG_CRT(11, 0);
918414c4531SDave Airlie 	WREG_CRT(12, 0);
919414c4531SDave Airlie 	WREG_CRT(13, 0);
920414c4531SDave Airlie 	WREG_CRT(14, 0);
921414c4531SDave Airlie 	WREG_CRT(15, 0);
922414c4531SDave Airlie 	WREG_CRT(16, vsyncstart & 0xFF);
923414c4531SDave Airlie 	WREG_CRT(17, (vsyncend & 0x0F) | 0x20);
924414c4531SDave Airlie 	WREG_CRT(18, vdisplay & 0xFF);
925414c4531SDave Airlie 	WREG_CRT(19, pitch & 0xFF);
926414c4531SDave Airlie 	WREG_CRT(20, 0);
927414c4531SDave Airlie 	WREG_CRT(21, vdisplay & 0xFF);
928414c4531SDave Airlie 	WREG_CRT(22, (vtotal + 1) & 0xFF);
929414c4531SDave Airlie 	WREG_CRT(23, 0xc3);
930414c4531SDave Airlie 	WREG_CRT(24, vdisplay & 0xFF);
931414c4531SDave Airlie 
932414c4531SDave Airlie 	ext_vga[0] = 0;
933414c4531SDave Airlie 	ext_vga[5] = 0;
934414c4531SDave Airlie 
935414c4531SDave Airlie 	/* TODO interlace */
936414c4531SDave Airlie 
937414c4531SDave Airlie 	ext_vga[0] |= (pitch & 0x300) >> 4;
938414c4531SDave Airlie 	ext_vga[1] = (((htotal - 4) & 0x100) >> 8) |
939414c4531SDave Airlie 		((hdisplay & 0x100) >> 7) |
940414c4531SDave Airlie 		((hsyncstart & 0x100) >> 6) |
941414c4531SDave Airlie 		(htotal & 0x40);
942414c4531SDave Airlie 	ext_vga[2] = ((vtotal & 0xc00) >> 10) |
943414c4531SDave Airlie 		((vdisplay & 0x400) >> 8) |
944414c4531SDave Airlie 		((vdisplay & 0xc00) >> 7) |
945414c4531SDave Airlie 		((vsyncstart & 0xc00) >> 5) |
946414c4531SDave Airlie 		((vdisplay & 0x400) >> 3);
947414c4531SDave Airlie 	if (crtc->fb->bits_per_pixel == 24)
948414c4531SDave Airlie 		ext_vga[3] = (((1 << bppshift) * 3) - 1) | 0x80;
949414c4531SDave Airlie 	else
950414c4531SDave Airlie 		ext_vga[3] = ((1 << bppshift) - 1) | 0x80;
951414c4531SDave Airlie 	ext_vga[4] = 0;
952414c4531SDave Airlie 	if (mdev->type == G200_WB)
953414c4531SDave Airlie 		ext_vga[1] |= 0x88;
954414c4531SDave Airlie 
955414c4531SDave Airlie 	ext_vga_index24 = 0x05;
956414c4531SDave Airlie 
957414c4531SDave Airlie 	/* Set pixel clocks */
958414c4531SDave Airlie 	misc = 0x2d;
959414c4531SDave Airlie 	WREG8(MGA_MISC_OUT, misc);
960414c4531SDave Airlie 
961414c4531SDave Airlie 	mga_crtc_set_plls(mdev, mode->clock);
962414c4531SDave Airlie 
963414c4531SDave Airlie 	for (i = 0; i < 6; i++) {
964414c4531SDave Airlie 		WREG_ECRT(i, ext_vga[i]);
965414c4531SDave Airlie 	}
966414c4531SDave Airlie 
967414c4531SDave Airlie 	if (mdev->type == G200_ER)
968414c4531SDave Airlie 		WREG_ECRT(24, ext_vga_index24);
969414c4531SDave Airlie 
970414c4531SDave Airlie 	if (mdev->type == G200_EV) {
971414c4531SDave Airlie 		WREG_ECRT(6, 0);
972414c4531SDave Airlie 	}
973414c4531SDave Airlie 
974414c4531SDave Airlie 	WREG_ECRT(0, ext_vga[0]);
975414c4531SDave Airlie 	/* Enable mga pixel clock */
976414c4531SDave Airlie 	misc = 0x2d;
977414c4531SDave Airlie 
978414c4531SDave Airlie 	WREG8(MGA_MISC_OUT, misc);
979414c4531SDave Airlie 
980414c4531SDave Airlie 	if (adjusted_mode)
981414c4531SDave Airlie 		memcpy(&mdev->mode, mode, sizeof(struct drm_display_mode));
982414c4531SDave Airlie 
983414c4531SDave Airlie 	mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
984414c4531SDave Airlie 
985414c4531SDave Airlie 	/* reset tagfifo */
986414c4531SDave Airlie 	if (mdev->type == G200_ER) {
987414c4531SDave Airlie 		u32 mem_ctl = RREG32(MGAREG_MEMCTL);
988414c4531SDave Airlie 		u8 seq1;
989414c4531SDave Airlie 
990414c4531SDave Airlie 		/* screen off */
991414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 0x01);
992414c4531SDave Airlie 		seq1 = RREG8(MGAREG_SEQ_DATA) | 0x20;
993414c4531SDave Airlie 		WREG8(MGAREG_SEQ_DATA, seq1);
994414c4531SDave Airlie 
995414c4531SDave Airlie 		WREG32(MGAREG_MEMCTL, mem_ctl | 0x00200000);
996414c4531SDave Airlie 		udelay(1000);
997414c4531SDave Airlie 		WREG32(MGAREG_MEMCTL, mem_ctl & ~0x00200000);
998414c4531SDave Airlie 
999414c4531SDave Airlie 		WREG8(MGAREG_SEQ_DATA, seq1 & ~0x20);
1000414c4531SDave Airlie 	}
1001414c4531SDave Airlie 
1002414c4531SDave Airlie 
1003414c4531SDave Airlie 	if (IS_G200_SE(mdev)) {
1004414c4531SDave Airlie 		if (mdev->reg_1e24 >= 0x02) {
1005414c4531SDave Airlie 			u8 hi_pri_lvl;
1006414c4531SDave Airlie 			u32 bpp;
1007414c4531SDave Airlie 			u32 mb;
1008414c4531SDave Airlie 
1009414c4531SDave Airlie 			if (crtc->fb->bits_per_pixel > 16)
1010414c4531SDave Airlie 				bpp = 32;
1011414c4531SDave Airlie 			else if (crtc->fb->bits_per_pixel > 8)
1012414c4531SDave Airlie 				bpp = 16;
1013414c4531SDave Airlie 			else
1014414c4531SDave Airlie 				bpp = 8;
1015414c4531SDave Airlie 
1016414c4531SDave Airlie 			mb = (mode->clock * bpp) / 1000;
1017414c4531SDave Airlie 			if (mb > 3100)
1018414c4531SDave Airlie 				hi_pri_lvl = 0;
1019414c4531SDave Airlie 			else if (mb > 2600)
1020414c4531SDave Airlie 				hi_pri_lvl = 1;
1021414c4531SDave Airlie 			else if (mb > 1900)
1022414c4531SDave Airlie 				hi_pri_lvl = 2;
1023414c4531SDave Airlie 			else if (mb > 1160)
1024414c4531SDave Airlie 				hi_pri_lvl = 3;
1025414c4531SDave Airlie 			else if (mb > 440)
1026414c4531SDave Airlie 				hi_pri_lvl = 4;
1027414c4531SDave Airlie 			else
1028414c4531SDave Airlie 				hi_pri_lvl = 5;
1029414c4531SDave Airlie 
1030414c4531SDave Airlie 			WREG8(0x1fde, 0x06);
1031414c4531SDave Airlie 			WREG8(0x1fdf, hi_pri_lvl);
1032414c4531SDave Airlie 		} else {
1033414c4531SDave Airlie 			if (mdev->reg_1e24 >= 0x01)
1034414c4531SDave Airlie 				WREG8(0x1fdf, 0x03);
1035414c4531SDave Airlie 			else
1036414c4531SDave Airlie 				WREG8(0x1fdf, 0x04);
1037414c4531SDave Airlie 		}
1038414c4531SDave Airlie 	}
1039414c4531SDave Airlie 	return 0;
1040414c4531SDave Airlie }
1041414c4531SDave Airlie 
1042414c4531SDave Airlie #if 0 /* code from mjg to attempt D3 on crtc dpms off - revisit later */
1043414c4531SDave Airlie static int mga_suspend(struct drm_crtc *crtc)
1044414c4531SDave Airlie {
1045414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1046414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1047414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1048414c4531SDave Airlie 	struct pci_dev *pdev = dev->pdev;
1049414c4531SDave Airlie 	int option;
1050414c4531SDave Airlie 
1051414c4531SDave Airlie 	if (mdev->suspended)
1052414c4531SDave Airlie 		return 0;
1053414c4531SDave Airlie 
1054414c4531SDave Airlie 	WREG_SEQ(1, 0x20);
1055414c4531SDave Airlie 	WREG_ECRT(1, 0x30);
1056414c4531SDave Airlie 	/* Disable the pixel clock */
1057414c4531SDave Airlie 	WREG_DAC(0x1a, 0x05);
1058414c4531SDave Airlie 	/* Power down the DAC */
1059414c4531SDave Airlie 	WREG_DAC(0x1e, 0x18);
1060414c4531SDave Airlie 	/* Power down the pixel PLL */
1061414c4531SDave Airlie 	WREG_DAC(0x1a, 0x0d);
1062414c4531SDave Airlie 
1063414c4531SDave Airlie 	/* Disable PLLs and clocks */
1064414c4531SDave Airlie 	pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1065414c4531SDave Airlie 	option &= ~(0x1F8024);
1066414c4531SDave Airlie 	pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1067414c4531SDave Airlie 	pci_set_power_state(pdev, PCI_D3hot);
1068414c4531SDave Airlie 	pci_disable_device(pdev);
1069414c4531SDave Airlie 
1070414c4531SDave Airlie 	mdev->suspended = true;
1071414c4531SDave Airlie 
1072414c4531SDave Airlie 	return 0;
1073414c4531SDave Airlie }
1074414c4531SDave Airlie 
1075414c4531SDave Airlie static int mga_resume(struct drm_crtc *crtc)
1076414c4531SDave Airlie {
1077414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1078414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1079414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1080414c4531SDave Airlie 	struct pci_dev *pdev = dev->pdev;
1081414c4531SDave Airlie 	int option;
1082414c4531SDave Airlie 
1083414c4531SDave Airlie 	if (!mdev->suspended)
1084414c4531SDave Airlie 		return 0;
1085414c4531SDave Airlie 
1086414c4531SDave Airlie 	pci_set_power_state(pdev, PCI_D0);
1087414c4531SDave Airlie 	pci_enable_device(pdev);
1088414c4531SDave Airlie 
1089414c4531SDave Airlie 	/* Disable sysclk */
1090414c4531SDave Airlie 	pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1091414c4531SDave Airlie 	option &= ~(0x4);
1092414c4531SDave Airlie 	pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1093414c4531SDave Airlie 
1094414c4531SDave Airlie 	mdev->suspended = false;
1095414c4531SDave Airlie 
1096414c4531SDave Airlie 	return 0;
1097414c4531SDave Airlie }
1098414c4531SDave Airlie 
1099414c4531SDave Airlie #endif
1100414c4531SDave Airlie 
1101414c4531SDave Airlie static void mga_crtc_dpms(struct drm_crtc *crtc, int mode)
1102414c4531SDave Airlie {
1103414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1104414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1105414c4531SDave Airlie 	u8 seq1 = 0, crtcext1 = 0;
1106414c4531SDave Airlie 
1107414c4531SDave Airlie 	switch (mode) {
1108414c4531SDave Airlie 	case DRM_MODE_DPMS_ON:
1109414c4531SDave Airlie 		seq1 = 0;
1110414c4531SDave Airlie 		crtcext1 = 0;
1111414c4531SDave Airlie 		mga_crtc_load_lut(crtc);
1112414c4531SDave Airlie 		break;
1113414c4531SDave Airlie 	case DRM_MODE_DPMS_STANDBY:
1114414c4531SDave Airlie 		seq1 = 0x20;
1115414c4531SDave Airlie 		crtcext1 = 0x10;
1116414c4531SDave Airlie 		break;
1117414c4531SDave Airlie 	case DRM_MODE_DPMS_SUSPEND:
1118414c4531SDave Airlie 		seq1 = 0x20;
1119414c4531SDave Airlie 		crtcext1 = 0x20;
1120414c4531SDave Airlie 		break;
1121414c4531SDave Airlie 	case DRM_MODE_DPMS_OFF:
1122414c4531SDave Airlie 		seq1 = 0x20;
1123414c4531SDave Airlie 		crtcext1 = 0x30;
1124414c4531SDave Airlie 		break;
1125414c4531SDave Airlie 	}
1126414c4531SDave Airlie 
1127414c4531SDave Airlie #if 0
1128414c4531SDave Airlie 	if (mode == DRM_MODE_DPMS_OFF) {
1129414c4531SDave Airlie 		mga_suspend(crtc);
1130414c4531SDave Airlie 	}
1131414c4531SDave Airlie #endif
1132414c4531SDave Airlie 	WREG8(MGAREG_SEQ_INDEX, 0x01);
1133414c4531SDave Airlie 	seq1 |= RREG8(MGAREG_SEQ_DATA) & ~0x20;
1134414c4531SDave Airlie 	mga_wait_vsync(mdev);
1135414c4531SDave Airlie 	mga_wait_busy(mdev);
1136414c4531SDave Airlie 	WREG8(MGAREG_SEQ_DATA, seq1);
1137414c4531SDave Airlie 	msleep(20);
1138414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_INDEX, 0x01);
1139414c4531SDave Airlie 	crtcext1 |= RREG8(MGAREG_CRTCEXT_DATA) & ~0x30;
1140414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_DATA, crtcext1);
1141414c4531SDave Airlie 
1142414c4531SDave Airlie #if 0
1143414c4531SDave Airlie 	if (mode == DRM_MODE_DPMS_ON && mdev->suspended == true) {
1144414c4531SDave Airlie 		mga_resume(crtc);
1145414c4531SDave Airlie 		drm_helper_resume_force_mode(dev);
1146414c4531SDave Airlie 	}
1147414c4531SDave Airlie #endif
1148414c4531SDave Airlie }
1149414c4531SDave Airlie 
1150414c4531SDave Airlie /*
1151414c4531SDave Airlie  * This is called before a mode is programmed. A typical use might be to
1152414c4531SDave Airlie  * enable DPMS during the programming to avoid seeing intermediate stages,
1153414c4531SDave Airlie  * but that's not relevant to us
1154414c4531SDave Airlie  */
1155414c4531SDave Airlie static void mga_crtc_prepare(struct drm_crtc *crtc)
1156414c4531SDave Airlie {
1157414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1158414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1159414c4531SDave Airlie 	u8 tmp;
1160414c4531SDave Airlie 
1161414c4531SDave Airlie 	/*	mga_resume(crtc);*/
1162414c4531SDave Airlie 
1163414c4531SDave Airlie 	WREG8(MGAREG_CRTC_INDEX, 0x11);
1164414c4531SDave Airlie 	tmp = RREG8(MGAREG_CRTC_DATA);
1165414c4531SDave Airlie 	WREG_CRT(0x11, tmp | 0x80);
1166414c4531SDave Airlie 
1167414c4531SDave Airlie 	if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1168414c4531SDave Airlie 		WREG_SEQ(0, 1);
1169414c4531SDave Airlie 		msleep(50);
1170414c4531SDave Airlie 		WREG_SEQ(1, 0x20);
1171414c4531SDave Airlie 		msleep(20);
1172414c4531SDave Airlie 	} else {
1173414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 0x1);
1174414c4531SDave Airlie 		tmp = RREG8(MGAREG_SEQ_DATA);
1175414c4531SDave Airlie 
1176414c4531SDave Airlie 		/* start sync reset */
1177414c4531SDave Airlie 		WREG_SEQ(0, 1);
1178414c4531SDave Airlie 		WREG_SEQ(1, tmp | 0x20);
1179414c4531SDave Airlie 	}
1180414c4531SDave Airlie 
1181414c4531SDave Airlie 	if (mdev->type == G200_WB)
1182414c4531SDave Airlie 		mga_g200wb_prepare(crtc);
1183414c4531SDave Airlie 
1184414c4531SDave Airlie 	WREG_CRT(17, 0);
1185414c4531SDave Airlie }
1186414c4531SDave Airlie 
1187414c4531SDave Airlie /*
1188414c4531SDave Airlie  * This is called after a mode is programmed. It should reverse anything done
1189414c4531SDave Airlie  * by the prepare function
1190414c4531SDave Airlie  */
1191414c4531SDave Airlie static void mga_crtc_commit(struct drm_crtc *crtc)
1192414c4531SDave Airlie {
1193414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1194414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1195414c4531SDave Airlie 	struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1196414c4531SDave Airlie 	u8 tmp;
1197414c4531SDave Airlie 
1198414c4531SDave Airlie 	if (mdev->type == G200_WB)
1199414c4531SDave Airlie 		mga_g200wb_commit(crtc);
1200414c4531SDave Airlie 
1201414c4531SDave Airlie 	if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1202414c4531SDave Airlie 		msleep(50);
1203414c4531SDave Airlie 		WREG_SEQ(1, 0x0);
1204414c4531SDave Airlie 		msleep(20);
1205414c4531SDave Airlie 		WREG_SEQ(0, 0x3);
1206414c4531SDave Airlie 	} else {
1207414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 0x1);
1208414c4531SDave Airlie 		tmp = RREG8(MGAREG_SEQ_DATA);
1209414c4531SDave Airlie 
1210414c4531SDave Airlie 		tmp &= ~0x20;
1211414c4531SDave Airlie 		WREG_SEQ(0x1, tmp);
1212414c4531SDave Airlie 		WREG_SEQ(0, 3);
1213414c4531SDave Airlie 	}
1214414c4531SDave Airlie 	crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
1215414c4531SDave Airlie }
1216414c4531SDave Airlie 
1217414c4531SDave Airlie /*
1218414c4531SDave Airlie  * The core can pass us a set of gamma values to program. We actually only
1219414c4531SDave Airlie  * use this for 8-bit mode so can't perform smooth fades on deeper modes,
1220414c4531SDave Airlie  * but it's a requirement that we provide the function
1221414c4531SDave Airlie  */
1222414c4531SDave Airlie static void mga_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
1223414c4531SDave Airlie 				  u16 *blue, uint32_t start, uint32_t size)
1224414c4531SDave Airlie {
1225414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1226414c4531SDave Airlie 	int end = (start + size > MGAG200_LUT_SIZE) ? MGAG200_LUT_SIZE : start + size;
1227414c4531SDave Airlie 	int i;
1228414c4531SDave Airlie 
1229414c4531SDave Airlie 	for (i = start; i < end; i++) {
1230414c4531SDave Airlie 		mga_crtc->lut_r[i] = red[i] >> 8;
1231414c4531SDave Airlie 		mga_crtc->lut_g[i] = green[i] >> 8;
1232414c4531SDave Airlie 		mga_crtc->lut_b[i] = blue[i] >> 8;
1233414c4531SDave Airlie 	}
1234414c4531SDave Airlie 	mga_crtc_load_lut(crtc);
1235414c4531SDave Airlie }
1236414c4531SDave Airlie 
1237414c4531SDave Airlie /* Simple cleanup function */
1238414c4531SDave Airlie static void mga_crtc_destroy(struct drm_crtc *crtc)
1239414c4531SDave Airlie {
1240414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1241414c4531SDave Airlie 
1242414c4531SDave Airlie 	drm_crtc_cleanup(crtc);
1243414c4531SDave Airlie 	kfree(mga_crtc);
1244414c4531SDave Airlie }
1245414c4531SDave Airlie 
1246414c4531SDave Airlie /* These provide the minimum set of functions required to handle a CRTC */
1247414c4531SDave Airlie static const struct drm_crtc_funcs mga_crtc_funcs = {
1248414c4531SDave Airlie 	.gamma_set = mga_crtc_gamma_set,
1249414c4531SDave Airlie 	.set_config = drm_crtc_helper_set_config,
1250414c4531SDave Airlie 	.destroy = mga_crtc_destroy,
1251414c4531SDave Airlie };
1252414c4531SDave Airlie 
1253414c4531SDave Airlie static const struct drm_crtc_helper_funcs mga_helper_funcs = {
1254414c4531SDave Airlie 	.dpms = mga_crtc_dpms,
1255414c4531SDave Airlie 	.mode_fixup = mga_crtc_mode_fixup,
1256414c4531SDave Airlie 	.mode_set = mga_crtc_mode_set,
1257414c4531SDave Airlie 	.mode_set_base = mga_crtc_mode_set_base,
1258414c4531SDave Airlie 	.prepare = mga_crtc_prepare,
1259414c4531SDave Airlie 	.commit = mga_crtc_commit,
1260414c4531SDave Airlie 	.load_lut = mga_crtc_load_lut,
1261414c4531SDave Airlie };
1262414c4531SDave Airlie 
1263414c4531SDave Airlie /* CRTC setup */
1264414c4531SDave Airlie static void mga_crtc_init(struct drm_device *dev)
1265414c4531SDave Airlie {
1266414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1267414c4531SDave Airlie 	struct mga_crtc *mga_crtc;
1268414c4531SDave Airlie 	int i;
1269414c4531SDave Airlie 
1270414c4531SDave Airlie 	mga_crtc = kzalloc(sizeof(struct mga_crtc) +
1271414c4531SDave Airlie 			      (MGAG200FB_CONN_LIMIT * sizeof(struct drm_connector *)),
1272414c4531SDave Airlie 			      GFP_KERNEL);
1273414c4531SDave Airlie 
1274414c4531SDave Airlie 	if (mga_crtc == NULL)
1275414c4531SDave Airlie 		return;
1276414c4531SDave Airlie 
1277414c4531SDave Airlie 	drm_crtc_init(dev, &mga_crtc->base, &mga_crtc_funcs);
1278414c4531SDave Airlie 
1279414c4531SDave Airlie 	drm_mode_crtc_set_gamma_size(&mga_crtc->base, MGAG200_LUT_SIZE);
1280414c4531SDave Airlie 	mdev->mode_info.crtc = mga_crtc;
1281414c4531SDave Airlie 
1282414c4531SDave Airlie 	for (i = 0; i < MGAG200_LUT_SIZE; i++) {
1283414c4531SDave Airlie 		mga_crtc->lut_r[i] = i;
1284414c4531SDave Airlie 		mga_crtc->lut_g[i] = i;
1285414c4531SDave Airlie 		mga_crtc->lut_b[i] = i;
1286414c4531SDave Airlie 	}
1287414c4531SDave Airlie 
1288414c4531SDave Airlie 	drm_crtc_helper_add(&mga_crtc->base, &mga_helper_funcs);
1289414c4531SDave Airlie }
1290414c4531SDave Airlie 
1291414c4531SDave Airlie /** Sets the color ramps on behalf of fbcon */
1292414c4531SDave Airlie void mga_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
1293414c4531SDave Airlie 			      u16 blue, int regno)
1294414c4531SDave Airlie {
1295414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1296414c4531SDave Airlie 
1297414c4531SDave Airlie 	mga_crtc->lut_r[regno] = red >> 8;
1298414c4531SDave Airlie 	mga_crtc->lut_g[regno] = green >> 8;
1299414c4531SDave Airlie 	mga_crtc->lut_b[regno] = blue >> 8;
1300414c4531SDave Airlie }
1301414c4531SDave Airlie 
1302414c4531SDave Airlie /** Gets the color ramps on behalf of fbcon */
1303414c4531SDave Airlie void mga_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
1304414c4531SDave Airlie 			      u16 *blue, int regno)
1305414c4531SDave Airlie {
1306414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1307414c4531SDave Airlie 
1308414c4531SDave Airlie 	*red = (u16)mga_crtc->lut_r[regno] << 8;
1309414c4531SDave Airlie 	*green = (u16)mga_crtc->lut_g[regno] << 8;
1310414c4531SDave Airlie 	*blue = (u16)mga_crtc->lut_b[regno] << 8;
1311414c4531SDave Airlie }
1312414c4531SDave Airlie 
1313414c4531SDave Airlie /*
1314414c4531SDave Airlie  * The encoder comes after the CRTC in the output pipeline, but before
1315414c4531SDave Airlie  * the connector. It's responsible for ensuring that the digital
1316414c4531SDave Airlie  * stream is appropriately converted into the output format. Setup is
1317414c4531SDave Airlie  * very simple in this case - all we have to do is inform qemu of the
1318414c4531SDave Airlie  * colour depth in order to ensure that it displays appropriately
1319414c4531SDave Airlie  */
1320414c4531SDave Airlie 
1321414c4531SDave Airlie /*
1322414c4531SDave Airlie  * These functions are analagous to those in the CRTC code, but are intended
1323414c4531SDave Airlie  * to handle any encoder-specific limitations
1324414c4531SDave Airlie  */
1325414c4531SDave Airlie static bool mga_encoder_mode_fixup(struct drm_encoder *encoder,
1326e811f5aeSLaurent Pinchart 				   const struct drm_display_mode *mode,
1327414c4531SDave Airlie 				   struct drm_display_mode *adjusted_mode)
1328414c4531SDave Airlie {
1329414c4531SDave Airlie 	return true;
1330414c4531SDave Airlie }
1331414c4531SDave Airlie 
1332414c4531SDave Airlie static void mga_encoder_mode_set(struct drm_encoder *encoder,
1333414c4531SDave Airlie 				struct drm_display_mode *mode,
1334414c4531SDave Airlie 				struct drm_display_mode *adjusted_mode)
1335414c4531SDave Airlie {
1336414c4531SDave Airlie 
1337414c4531SDave Airlie }
1338414c4531SDave Airlie 
1339414c4531SDave Airlie static void mga_encoder_dpms(struct drm_encoder *encoder, int state)
1340414c4531SDave Airlie {
1341414c4531SDave Airlie 	return;
1342414c4531SDave Airlie }
1343414c4531SDave Airlie 
1344414c4531SDave Airlie static void mga_encoder_prepare(struct drm_encoder *encoder)
1345414c4531SDave Airlie {
1346414c4531SDave Airlie }
1347414c4531SDave Airlie 
1348414c4531SDave Airlie static void mga_encoder_commit(struct drm_encoder *encoder)
1349414c4531SDave Airlie {
1350414c4531SDave Airlie }
1351414c4531SDave Airlie 
1352414c4531SDave Airlie void mga_encoder_destroy(struct drm_encoder *encoder)
1353414c4531SDave Airlie {
1354414c4531SDave Airlie 	struct mga_encoder *mga_encoder = to_mga_encoder(encoder);
1355414c4531SDave Airlie 	drm_encoder_cleanup(encoder);
1356414c4531SDave Airlie 	kfree(mga_encoder);
1357414c4531SDave Airlie }
1358414c4531SDave Airlie 
1359414c4531SDave Airlie static const struct drm_encoder_helper_funcs mga_encoder_helper_funcs = {
1360414c4531SDave Airlie 	.dpms = mga_encoder_dpms,
1361414c4531SDave Airlie 	.mode_fixup = mga_encoder_mode_fixup,
1362414c4531SDave Airlie 	.mode_set = mga_encoder_mode_set,
1363414c4531SDave Airlie 	.prepare = mga_encoder_prepare,
1364414c4531SDave Airlie 	.commit = mga_encoder_commit,
1365414c4531SDave Airlie };
1366414c4531SDave Airlie 
1367414c4531SDave Airlie static const struct drm_encoder_funcs mga_encoder_encoder_funcs = {
1368414c4531SDave Airlie 	.destroy = mga_encoder_destroy,
1369414c4531SDave Airlie };
1370414c4531SDave Airlie 
1371414c4531SDave Airlie static struct drm_encoder *mga_encoder_init(struct drm_device *dev)
1372414c4531SDave Airlie {
1373414c4531SDave Airlie 	struct drm_encoder *encoder;
1374414c4531SDave Airlie 	struct mga_encoder *mga_encoder;
1375414c4531SDave Airlie 
1376414c4531SDave Airlie 	mga_encoder = kzalloc(sizeof(struct mga_encoder), GFP_KERNEL);
1377414c4531SDave Airlie 	if (!mga_encoder)
1378414c4531SDave Airlie 		return NULL;
1379414c4531SDave Airlie 
1380414c4531SDave Airlie 	encoder = &mga_encoder->base;
1381414c4531SDave Airlie 	encoder->possible_crtcs = 0x1;
1382414c4531SDave Airlie 
1383414c4531SDave Airlie 	drm_encoder_init(dev, encoder, &mga_encoder_encoder_funcs,
1384414c4531SDave Airlie 			 DRM_MODE_ENCODER_DAC);
1385414c4531SDave Airlie 	drm_encoder_helper_add(encoder, &mga_encoder_helper_funcs);
1386414c4531SDave Airlie 
1387414c4531SDave Airlie 	return encoder;
1388414c4531SDave Airlie }
1389414c4531SDave Airlie 
1390414c4531SDave Airlie 
1391414c4531SDave Airlie static int mga_vga_get_modes(struct drm_connector *connector)
1392414c4531SDave Airlie {
1393414c4531SDave Airlie 	struct mga_connector *mga_connector = to_mga_connector(connector);
1394414c4531SDave Airlie 	struct edid *edid;
1395414c4531SDave Airlie 	int ret = 0;
1396414c4531SDave Airlie 
1397414c4531SDave Airlie 	edid = drm_get_edid(connector, &mga_connector->i2c->adapter);
1398414c4531SDave Airlie 	if (edid) {
1399414c4531SDave Airlie 		drm_mode_connector_update_edid_property(connector, edid);
1400414c4531SDave Airlie 		ret = drm_add_edid_modes(connector, edid);
1401414c4531SDave Airlie 		connector->display_info.raw_edid = NULL;
1402414c4531SDave Airlie 		kfree(edid);
1403414c4531SDave Airlie 	}
1404414c4531SDave Airlie 	return ret;
1405414c4531SDave Airlie }
1406414c4531SDave Airlie 
1407414c4531SDave Airlie static int mga_vga_mode_valid(struct drm_connector *connector,
1408414c4531SDave Airlie 				 struct drm_display_mode *mode)
1409414c4531SDave Airlie {
1410414c4531SDave Airlie 	/* FIXME: Add bandwidth and g200se limitations */
1411414c4531SDave Airlie 
1412414c4531SDave Airlie 	if (mode->crtc_hdisplay > 2048 || mode->crtc_hsync_start > 4096 ||
1413414c4531SDave Airlie 	    mode->crtc_hsync_end > 4096 || mode->crtc_htotal > 4096 ||
1414414c4531SDave Airlie 	    mode->crtc_vdisplay > 2048 || mode->crtc_vsync_start > 4096 ||
1415414c4531SDave Airlie 	    mode->crtc_vsync_end > 4096 || mode->crtc_vtotal > 4096) {
1416414c4531SDave Airlie 		return MODE_BAD;
1417414c4531SDave Airlie 	}
1418414c4531SDave Airlie 
1419414c4531SDave Airlie 	return MODE_OK;
1420414c4531SDave Airlie }
1421414c4531SDave Airlie 
1422414c4531SDave Airlie struct drm_encoder *mga_connector_best_encoder(struct drm_connector
1423414c4531SDave Airlie 						  *connector)
1424414c4531SDave Airlie {
1425414c4531SDave Airlie 	int enc_id = connector->encoder_ids[0];
1426414c4531SDave Airlie 	struct drm_mode_object *obj;
1427414c4531SDave Airlie 	struct drm_encoder *encoder;
1428414c4531SDave Airlie 
1429414c4531SDave Airlie 	/* pick the encoder ids */
1430414c4531SDave Airlie 	if (enc_id) {
1431414c4531SDave Airlie 		obj =
1432414c4531SDave Airlie 		    drm_mode_object_find(connector->dev, enc_id,
1433414c4531SDave Airlie 					 DRM_MODE_OBJECT_ENCODER);
1434414c4531SDave Airlie 		if (!obj)
1435414c4531SDave Airlie 			return NULL;
1436414c4531SDave Airlie 		encoder = obj_to_encoder(obj);
1437414c4531SDave Airlie 		return encoder;
1438414c4531SDave Airlie 	}
1439414c4531SDave Airlie 	return NULL;
1440414c4531SDave Airlie }
1441414c4531SDave Airlie 
1442414c4531SDave Airlie static enum drm_connector_status mga_vga_detect(struct drm_connector
1443414c4531SDave Airlie 						   *connector, bool force)
1444414c4531SDave Airlie {
1445414c4531SDave Airlie 	return connector_status_connected;
1446414c4531SDave Airlie }
1447414c4531SDave Airlie 
1448414c4531SDave Airlie static void mga_connector_destroy(struct drm_connector *connector)
1449414c4531SDave Airlie {
1450414c4531SDave Airlie 	struct mga_connector *mga_connector = to_mga_connector(connector);
1451414c4531SDave Airlie 	mgag200_i2c_destroy(mga_connector->i2c);
1452414c4531SDave Airlie 	drm_connector_cleanup(connector);
1453414c4531SDave Airlie 	kfree(connector);
1454414c4531SDave Airlie }
1455414c4531SDave Airlie 
1456414c4531SDave Airlie struct drm_connector_helper_funcs mga_vga_connector_helper_funcs = {
1457414c4531SDave Airlie 	.get_modes = mga_vga_get_modes,
1458414c4531SDave Airlie 	.mode_valid = mga_vga_mode_valid,
1459414c4531SDave Airlie 	.best_encoder = mga_connector_best_encoder,
1460414c4531SDave Airlie };
1461414c4531SDave Airlie 
1462414c4531SDave Airlie struct drm_connector_funcs mga_vga_connector_funcs = {
1463414c4531SDave Airlie 	.dpms = drm_helper_connector_dpms,
1464414c4531SDave Airlie 	.detect = mga_vga_detect,
1465414c4531SDave Airlie 	.fill_modes = drm_helper_probe_single_connector_modes,
1466414c4531SDave Airlie 	.destroy = mga_connector_destroy,
1467414c4531SDave Airlie };
1468414c4531SDave Airlie 
1469414c4531SDave Airlie static struct drm_connector *mga_vga_init(struct drm_device *dev)
1470414c4531SDave Airlie {
1471414c4531SDave Airlie 	struct drm_connector *connector;
1472414c4531SDave Airlie 	struct mga_connector *mga_connector;
1473414c4531SDave Airlie 
1474414c4531SDave Airlie 	mga_connector = kzalloc(sizeof(struct mga_connector), GFP_KERNEL);
1475414c4531SDave Airlie 	if (!mga_connector)
1476414c4531SDave Airlie 		return NULL;
1477414c4531SDave Airlie 
1478414c4531SDave Airlie 	connector = &mga_connector->base;
1479414c4531SDave Airlie 
1480414c4531SDave Airlie 	drm_connector_init(dev, connector,
1481414c4531SDave Airlie 			   &mga_vga_connector_funcs, DRM_MODE_CONNECTOR_VGA);
1482414c4531SDave Airlie 
1483414c4531SDave Airlie 	drm_connector_helper_add(connector, &mga_vga_connector_helper_funcs);
1484414c4531SDave Airlie 
1485414c4531SDave Airlie 	mga_connector->i2c = mgag200_i2c_create(dev);
1486414c4531SDave Airlie 	if (!mga_connector->i2c)
1487414c4531SDave Airlie 		DRM_ERROR("failed to add ddc bus\n");
1488414c4531SDave Airlie 
1489414c4531SDave Airlie 	return connector;
1490414c4531SDave Airlie }
1491414c4531SDave Airlie 
1492414c4531SDave Airlie 
1493414c4531SDave Airlie int mgag200_modeset_init(struct mga_device *mdev)
1494414c4531SDave Airlie {
1495414c4531SDave Airlie 	struct drm_encoder *encoder;
1496414c4531SDave Airlie 	struct drm_connector *connector;
1497414c4531SDave Airlie 	int ret;
1498414c4531SDave Airlie 
1499414c4531SDave Airlie 	mdev->mode_info.mode_config_initialized = true;
1500414c4531SDave Airlie 
1501414c4531SDave Airlie 	mdev->dev->mode_config.max_width = MGAG200_MAX_FB_WIDTH;
1502414c4531SDave Airlie 	mdev->dev->mode_config.max_height = MGAG200_MAX_FB_HEIGHT;
1503414c4531SDave Airlie 
1504414c4531SDave Airlie 	mdev->dev->mode_config.fb_base = mdev->mc.vram_base;
1505414c4531SDave Airlie 
1506414c4531SDave Airlie 	mga_crtc_init(mdev->dev);
1507414c4531SDave Airlie 
1508414c4531SDave Airlie 	encoder = mga_encoder_init(mdev->dev);
1509414c4531SDave Airlie 	if (!encoder) {
1510414c4531SDave Airlie 		DRM_ERROR("mga_encoder_init failed\n");
1511414c4531SDave Airlie 		return -1;
1512414c4531SDave Airlie 	}
1513414c4531SDave Airlie 
1514414c4531SDave Airlie 	connector = mga_vga_init(mdev->dev);
1515414c4531SDave Airlie 	if (!connector) {
1516414c4531SDave Airlie 		DRM_ERROR("mga_vga_init failed\n");
1517414c4531SDave Airlie 		return -1;
1518414c4531SDave Airlie 	}
1519414c4531SDave Airlie 
1520414c4531SDave Airlie 	drm_mode_connector_attach_encoder(connector, encoder);
1521414c4531SDave Airlie 
1522414c4531SDave Airlie 	ret = mgag200_fbdev_init(mdev);
1523414c4531SDave Airlie 	if (ret) {
1524414c4531SDave Airlie 		DRM_ERROR("mga_fbdev_init failed\n");
1525414c4531SDave Airlie 		return ret;
1526414c4531SDave Airlie 	}
1527414c4531SDave Airlie 
1528414c4531SDave Airlie 	return 0;
1529414c4531SDave Airlie }
1530414c4531SDave Airlie 
1531414c4531SDave Airlie void mgag200_modeset_fini(struct mga_device *mdev)
1532414c4531SDave Airlie {
1533414c4531SDave Airlie 
1534414c4531SDave Airlie }
1535