1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Geode LX framebuffer driver.
4  *
5  * Copyright (C) 2007 Advanced Micro Devices, Inc.
6  * Built from gxfb (which is Copyright (C) 2006 Arcom Control Systems Ltd.)
7  */
8 
9 #include <linux/aperture.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/string.h>
14 #include <linux/console.h>
15 #include <linux/mm.h>
16 #include <linux/suspend.h>
17 #include <linux/delay.h>
18 #include <linux/fb.h>
19 #include <linux/init.h>
20 #include <linux/pci.h>
21 #include <linux/uaccess.h>
22 
23 #include <asm/olpc.h>
24 
25 #include "lxfb.h"
26 
27 static char *mode_option;
28 static int noclear, nopanel, nocrt;
29 static int vram;
30 static int vt_switch;
31 
32 /* Most of these modes are sorted in ascending order, but
33  * since the first entry in this table is the "default" mode,
34  * we try to make it something sane - 640x480-60 is sane
35  */
36 
37 static struct fb_videomode geode_modedb[] = {
38 	/* 640x480-60 */
39 	{ NULL, 60, 640, 480, 39682, 48, 8, 25, 2, 88, 2,
40 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
41 	  FB_VMODE_NONINTERLACED, 0 },
42 	/* 640x400-70 */
43 	{ NULL, 70, 640, 400, 39770, 40, 8, 28, 5, 96, 2,
44 	  FB_SYNC_HOR_HIGH_ACT,
45 	  FB_VMODE_NONINTERLACED, 0 },
46 	/* 640x480-70 */
47 	{ NULL, 70, 640, 480, 35014, 88, 24, 15, 2, 64, 3,
48 	  0, FB_VMODE_NONINTERLACED, 0 },
49 	/* 640x480-72 */
50 	{ NULL, 72, 640, 480, 32102, 120, 16, 20, 1, 40, 3,
51 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
52 	  FB_VMODE_NONINTERLACED, 0 },
53 	/* 640x480-75 */
54 	{ NULL, 75, 640, 480, 31746, 120, 16, 16, 1, 64, 3,
55 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
56 	  FB_VMODE_NONINTERLACED, 0 },
57 	/* 640x480-85 */
58 	{ NULL, 85, 640, 480, 27780, 80, 56, 25, 1, 56, 3,
59 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
60 	  FB_VMODE_NONINTERLACED, 0 },
61 	/* 640x480-90 */
62 	{ NULL, 90, 640, 480, 26392, 96, 32, 22, 1, 64, 3,
63 	  0, FB_VMODE_NONINTERLACED, 0 },
64 	/* 640x480-100 */
65 	{ NULL, 100, 640, 480, 23167, 104, 40, 25, 1, 64, 3,
66 	  0, FB_VMODE_NONINTERLACED, 0 },
67 	/* 640x480-60 */
68 	{ NULL, 60, 640, 480, 39682, 48, 16, 25, 10, 88, 2,
69 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
70 	  FB_VMODE_NONINTERLACED, 0 },
71 	/* 800x600-56 */
72 	{ NULL, 56, 800, 600, 27901, 128, 24, 22, 1, 72, 2,
73 	  0, FB_VMODE_NONINTERLACED, 0 },
74 	/* 800x600-60 */
75 	{ NULL, 60, 800, 600, 25131, 72, 32, 23, 1, 136, 4,
76 	  0, FB_VMODE_NONINTERLACED, 0 },
77 	/* 800x600-70 */
78 	{ NULL, 70, 800, 600, 21873, 120, 40, 21, 4, 80, 3,
79 	  0, FB_VMODE_NONINTERLACED, 0 },
80 	/* 800x600-72 */
81 	{ NULL, 72, 800, 600, 20052, 64, 56, 23, 37, 120, 6,
82 	  0, FB_VMODE_NONINTERLACED, 0 },
83 	/* 800x600-75 */
84 	{ NULL, 75, 800, 600, 20202, 160, 16, 21, 1, 80, 3,
85 	  0, FB_VMODE_NONINTERLACED, 0 },
86 	/* 800x600-85 */
87 	{ NULL, 85, 800, 600, 17790, 152, 32, 27, 1, 64, 3,
88 	  0, FB_VMODE_NONINTERLACED, 0 },
89 	/* 800x600-90 */
90 	{ NULL, 90, 800, 600, 16648, 128, 40, 28, 1, 88, 3,
91 	  0, FB_VMODE_NONINTERLACED, 0 },
92 	/* 800x600-100 */
93 	{ NULL, 100, 800, 600, 14667, 136, 48, 27, 1, 88, 3,
94 	  0, FB_VMODE_NONINTERLACED, 0 },
95 	/* 800x600-60 */
96 	{ NULL, 60, 800, 600, 25131, 88, 40, 23, 1, 128, 4,
97 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
98 	  FB_VMODE_NONINTERLACED, 0 },
99 	/* 1024x768-60 */
100 	{ NULL, 60, 1024, 768, 15385, 160, 24, 29, 3, 136, 6,
101 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
102 	  FB_VMODE_NONINTERLACED, 0 },
103 	/* 1024x768-70 */
104 	{ NULL, 70, 1024, 768, 13346, 144, 24, 29, 3, 136, 6,
105 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
106 	  FB_VMODE_NONINTERLACED, 0 },
107 	/* 1024x768-72 */
108 	{ NULL, 72, 1024, 768, 12702, 168, 56, 29, 4, 112, 3,
109 	  0, FB_VMODE_NONINTERLACED, 0 },
110 	/* 1024x768-75 */
111 	{ NULL, 75, 1024, 768, 12703, 176, 16, 28, 1, 96, 3,
112 	  0, FB_VMODE_NONINTERLACED, 0 },
113 	/* 1024x768-85 */
114 	{ NULL, 85, 1024, 768, 10581, 208, 48, 36, 1, 96, 3,
115 	  0, FB_VMODE_NONINTERLACED, 0 },
116 	/* 1024x768-90 */
117 	{ NULL, 90, 1024, 768, 9981, 176, 64, 37, 1, 112, 3,
118 	  0, FB_VMODE_NONINTERLACED, 0 },
119 	/* 1024x768-100 */
120 	{ NULL, 100, 1024, 768, 8825, 184, 72, 42, 1, 112, 3,
121 	  0, FB_VMODE_NONINTERLACED, 0 },
122 	/* 1024x768-60 */
123 	{ NULL, 60, 1024, 768, 15385, 160, 24, 29, 3, 136, 6,
124 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
125 	  FB_VMODE_NONINTERLACED, 0 },
126 	/* 1152x864-60 */
127 	{ NULL, 60, 1152, 864, 12251, 184, 64, 27, 1, 120, 3,
128 	  0, FB_VMODE_NONINTERLACED, 0 },
129 	/* 1152x864-70 */
130 	{ NULL, 70, 1152, 864, 10254, 192, 72, 32, 8, 120, 3,
131 	  0, FB_VMODE_NONINTERLACED, 0 },
132 	/* 1152x864-72 */
133 	{ NULL, 72, 1152, 864, 9866, 200, 72, 33, 7, 128, 3,
134 	  0, FB_VMODE_NONINTERLACED, 0 },
135 	/* 1152x864-75 */
136 	{ NULL, 75, 1152, 864, 9259, 256, 64, 32, 1, 128, 3,
137 	  0, FB_VMODE_NONINTERLACED, 0 },
138 	/* 1152x864-85 */
139 	{ NULL, 85, 1152, 864, 8357, 200, 72, 37, 3, 128, 3,
140 	  0, FB_VMODE_NONINTERLACED, 0 },
141 	/* 1152x864-90 */
142 	{ NULL, 90, 1152, 864, 7719, 208, 80, 42, 9, 128, 3,
143 	  0, FB_VMODE_NONINTERLACED, 0 },
144 	/* 1152x864-100 */
145 	{ NULL, 100, 1152, 864, 6947, 208, 80, 48, 3, 128, 3,
146 	  0, FB_VMODE_NONINTERLACED, 0 },
147 	/* 1152x864-60 */
148 	{ NULL, 60, 1152, 864, 12251, 184, 64, 27, 1, 120, 3,
149 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
150 	  FB_VMODE_NONINTERLACED, 0 },
151 	/* 1280x1024-60 */
152 	{ NULL, 60, 1280, 1024, 9262, 248, 48, 38, 1, 112, 3,
153 	  0, FB_VMODE_NONINTERLACED, 0 },
154 	/* 1280x1024-70 */
155 	{ NULL, 70, 1280, 1024, 7719, 224, 88, 38, 6, 136, 3,
156 	  0, FB_VMODE_NONINTERLACED, 0 },
157 	/* 1280x1024-72 */
158 	{ NULL, 72, 1280, 1024, 7490, 224, 88, 39, 7, 136, 3,
159 	  0, FB_VMODE_NONINTERLACED, 0 },
160 	/* 1280x1024-75 */
161 	{ NULL, 75, 1280, 1024, 7409, 248, 16, 38, 1, 144, 3,
162 	  0, FB_VMODE_NONINTERLACED, 0 },
163 	/* 1280x1024-85 */
164 	{ NULL, 85, 1280, 1024, 6351, 224, 64, 44, 1, 160, 3,
165 	  0, FB_VMODE_NONINTERLACED, 0 },
166 	/* 1280x1024-90 */
167 	{ NULL, 90, 1280, 1024, 5791, 240, 96, 51, 12, 144, 3,
168 	  0, FB_VMODE_NONINTERLACED, 0 },
169 	/* 1280x1024-100 */
170 	{ NULL, 100, 1280, 1024, 5212, 240, 96, 57, 6, 144, 3,
171 	  0, FB_VMODE_NONINTERLACED, 0 },
172 	/* 1280x1024-60 */
173 	{ NULL, 60, 1280, 1024, 9262, 248, 48, 38, 1, 112, 3,
174 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
175 	  FB_VMODE_NONINTERLACED, 0 },
176 	/* 1600x1200-60 */
177 	{ NULL, 60, 1600, 1200, 6172, 304, 64, 46, 1, 192, 3,
178 	  0, FB_VMODE_NONINTERLACED, 0 },
179 	/* 1600x1200-70 */
180 	{ NULL, 70, 1600, 1200, 5291, 304, 64, 46, 1, 192, 3,
181 	  0, FB_VMODE_NONINTERLACED, 0 },
182 	/* 1600x1200-72 */
183 	{ NULL, 72, 1600, 1200, 5053, 288, 112, 47, 13, 176, 3,
184 	  0, FB_VMODE_NONINTERLACED, 0 },
185 	/* 1600x1200-75 */
186 	{ NULL, 75, 1600, 1200, 4938, 304, 64, 46, 1, 192, 3,
187 	  0, FB_VMODE_NONINTERLACED, 0 },
188 	/* 1600x1200-85 */
189 	{ NULL, 85, 1600, 1200, 4357, 304, 64, 46, 1, 192, 3,
190 	  0, FB_VMODE_NONINTERLACED, 0 },
191 	/* 1600x1200-90 */
192 	{ NULL, 90, 1600, 1200, 3981, 304, 128, 60, 1, 176, 3,
193 	  0, FB_VMODE_NONINTERLACED, 0 },
194 	/* 1600x1200-100 */
195 	{ NULL, 100, 1600, 1200, 3563, 304, 128, 67, 1, 176, 3,
196 	  0, FB_VMODE_NONINTERLACED, 0 },
197 	/* 1600x1200-60 */
198 	{ NULL, 60, 1600, 1200, 6172, 304, 64, 46, 1, 192, 3,
199 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
200 	  FB_VMODE_NONINTERLACED, 0 },
201 	/* 1920x1440-60 */
202 	{ NULL, 60, 1920, 1440, 4273, 344, 128, 56, 1, 208, 3,
203 	  0, FB_VMODE_NONINTERLACED, 0 },
204 	/* 1920x1440-70 */
205 	{ NULL, 70, 1920, 1440, 3593, 360, 152, 55, 8, 208, 3,
206 	  0, FB_VMODE_NONINTERLACED, 0 },
207 	/* 1920x1440-72 */
208 	{ NULL, 72, 1920, 1440, 3472, 360, 152, 68, 4, 208, 3,
209 	  0, FB_VMODE_NONINTERLACED, 0 },
210 	/* 1920x1440-75 */
211 	{ NULL, 75, 1920, 1440, 3367, 352, 144, 56, 1, 224, 3,
212 	  0, FB_VMODE_NONINTERLACED, 0 },
213 	/* 1920x1440-85 */
214 	{ NULL, 85, 1920, 1440, 2929, 368, 152, 68, 1, 216, 3,
215 	  0, FB_VMODE_NONINTERLACED, 0 },
216 };
217 
218 static struct fb_videomode olpc_dcon_modedb[] = {
219 	/* The only mode the DCON has is 1200x900 */
220 	{ NULL, 50, 1200, 900, 17460, 24, 8, 4, 5, 8, 3,
221 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
222 	  FB_VMODE_NONINTERLACED, 0 }
223 };
224 
225 static void get_modedb(struct fb_videomode **modedb, unsigned int *size)
226 {
227 	if (olpc_has_dcon()) {
228 		*modedb = (struct fb_videomode *) olpc_dcon_modedb;
229 		*size = ARRAY_SIZE(olpc_dcon_modedb);
230 	} else {
231 		*modedb = (struct fb_videomode *) geode_modedb;
232 		*size = ARRAY_SIZE(geode_modedb);
233 	}
234 }
235 
236 static int lxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
237 {
238 	if (var->xres > 1920 || var->yres > 1440)
239 		return -EINVAL;
240 
241 	if (var->bits_per_pixel == 32) {
242 		var->red.offset   = 16; var->red.length   = 8;
243 		var->green.offset =  8; var->green.length = 8;
244 		var->blue.offset  =  0; var->blue.length  = 8;
245 	} else if (var->bits_per_pixel == 16) {
246 		var->red.offset   = 11; var->red.length   = 5;
247 		var->green.offset =  5; var->green.length = 6;
248 		var->blue.offset  =  0; var->blue.length  = 5;
249 	} else if (var->bits_per_pixel == 8) {
250 		var->red.offset   = 0; var->red.length   = 8;
251 		var->green.offset = 0; var->green.length = 8;
252 		var->blue.offset  = 0; var->blue.length  = 8;
253 	} else
254 		return -EINVAL;
255 
256 	var->transp.offset = 0; var->transp.length = 0;
257 
258 	/* Enough video memory? */
259 	if ((lx_get_pitch(var->xres, var->bits_per_pixel) * var->yres)
260 	    > info->fix.smem_len)
261 	  return -EINVAL;
262 
263 	return 0;
264 }
265 
266 static int lxfb_set_par(struct fb_info *info)
267 {
268 	if (info->var.bits_per_pixel > 8)
269 		info->fix.visual = FB_VISUAL_TRUECOLOR;
270 	else
271 		info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
272 
273 	info->fix.line_length = lx_get_pitch(info->var.xres,
274 		info->var.bits_per_pixel);
275 
276 	lx_set_mode(info);
277 	return 0;
278 }
279 
280 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
281 {
282 	chan &= 0xffff;
283 	chan >>= 16 - bf->length;
284 	return chan << bf->offset;
285 }
286 
287 static int lxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
288 			   unsigned blue, unsigned transp,
289 			   struct fb_info *info)
290 {
291 	if (info->var.grayscale) {
292 		/* grayscale = 0.30*R + 0.59*G + 0.11*B */
293 		red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
294 	}
295 
296 	/* Truecolor has hardware independent palette */
297 	if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
298 		u32 *pal = info->pseudo_palette;
299 		u32 v;
300 
301 		if (regno >= 16)
302 			return -EINVAL;
303 
304 		v  = chan_to_field(red, &info->var.red);
305 		v |= chan_to_field(green, &info->var.green);
306 		v |= chan_to_field(blue, &info->var.blue);
307 
308 		pal[regno] = v;
309 	} else {
310 		if (regno >= 256)
311 			return -EINVAL;
312 
313 		lx_set_palette_reg(info, regno, red, green, blue);
314 	}
315 
316 	return 0;
317 }
318 
319 static int lxfb_blank(int blank_mode, struct fb_info *info)
320 {
321 	return lx_blank_display(info, blank_mode);
322 }
323 
324 
325 static int lxfb_map_video_memory(struct fb_info *info, struct pci_dev *dev)
326 {
327 	struct lxfb_par *par = info->par;
328 	int ret;
329 
330 	ret = pci_enable_device(dev);
331 
332 	if (ret)
333 		return ret;
334 
335 	ret = pci_request_region(dev, 0, "lxfb-framebuffer");
336 
337 	if (ret)
338 		return ret;
339 
340 	ret = pci_request_region(dev, 1, "lxfb-gp");
341 
342 	if (ret)
343 		return ret;
344 
345 	ret = pci_request_region(dev, 2, "lxfb-vg");
346 
347 	if (ret)
348 		return ret;
349 
350 	ret = pci_request_region(dev, 3, "lxfb-vp");
351 
352 	if (ret)
353 		return ret;
354 
355 	info->fix.smem_start = pci_resource_start(dev, 0);
356 	info->fix.smem_len = vram ? vram : lx_framebuffer_size();
357 
358 	info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
359 
360 	ret = -ENOMEM;
361 
362 	if (info->screen_base == NULL)
363 		return ret;
364 
365 	par->gp_regs = pci_ioremap_bar(dev, 1);
366 
367 	if (par->gp_regs == NULL)
368 		return ret;
369 
370 	par->dc_regs = pci_ioremap_bar(dev, 2);
371 
372 	if (par->dc_regs == NULL)
373 		return ret;
374 
375 	par->vp_regs = pci_ioremap_bar(dev, 3);
376 
377 	if (par->vp_regs == NULL)
378 		return ret;
379 
380 	write_dc(par, DC_UNLOCK, DC_UNLOCK_UNLOCK);
381 	write_dc(par, DC_GLIU0_MEM_OFFSET, info->fix.smem_start & 0xFF000000);
382 	write_dc(par, DC_UNLOCK, DC_UNLOCK_LOCK);
383 
384 	dev_info(&dev->dev, "%d KB of video memory at 0x%lx\n",
385 		 info->fix.smem_len / 1024, info->fix.smem_start);
386 
387 	return 0;
388 }
389 
390 static const struct fb_ops lxfb_ops = {
391 	.owner		= THIS_MODULE,
392 	.fb_check_var	= lxfb_check_var,
393 	.fb_set_par	= lxfb_set_par,
394 	.fb_setcolreg	= lxfb_setcolreg,
395 	.fb_blank       = lxfb_blank,
396 	/* No HW acceleration for now. */
397 	.fb_fillrect	= cfb_fillrect,
398 	.fb_copyarea	= cfb_copyarea,
399 	.fb_imageblit	= cfb_imageblit,
400 };
401 
402 static struct fb_info *lxfb_init_fbinfo(struct device *dev)
403 {
404 	struct lxfb_par *par;
405 	struct fb_info *info;
406 
407 	/* Alloc enough space for the pseudo palette. */
408 	info = framebuffer_alloc(sizeof(struct lxfb_par) + sizeof(u32) * 16,
409 				 dev);
410 	if (!info)
411 		return NULL;
412 
413 	par = info->par;
414 
415 	strcpy(info->fix.id, "Geode LX");
416 
417 	info->fix.type		= FB_TYPE_PACKED_PIXELS;
418 	info->fix.type_aux	= 0;
419 	info->fix.xpanstep	= 0;
420 	info->fix.ypanstep	= 0;
421 	info->fix.ywrapstep	= 0;
422 	info->fix.accel		= FB_ACCEL_NONE;
423 
424 	info->var.nonstd	= 0;
425 	info->var.activate	= FB_ACTIVATE_NOW;
426 	info->var.height	= -1;
427 	info->var.width	= -1;
428 	info->var.accel_flags = 0;
429 	info->var.vmode	= FB_VMODE_NONINTERLACED;
430 
431 	info->fbops		= &lxfb_ops;
432 	info->flags		= FBINFO_DEFAULT;
433 	info->node		= -1;
434 
435 	info->pseudo_palette	= (void *)par + sizeof(struct lxfb_par);
436 
437 	if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
438 		framebuffer_release(info);
439 		return NULL;
440 	}
441 
442 	info->var.grayscale	= 0;
443 
444 	return info;
445 }
446 
447 static int __maybe_unused lxfb_suspend(struct device *dev)
448 {
449 	struct fb_info *info = dev_get_drvdata(dev);
450 
451 	console_lock();
452 	lx_powerdown(info);
453 	fb_set_suspend(info, 1);
454 	console_unlock();
455 
456 	/* there's no point in setting PCI states; we emulate PCI, so
457 	 * we don't end up getting power savings anyways */
458 
459 	return 0;
460 }
461 
462 static int __maybe_unused lxfb_resume(struct device *dev)
463 {
464 	struct fb_info *info = dev_get_drvdata(dev);
465 	int ret;
466 
467 	console_lock();
468 	ret = lx_powerup(info);
469 	if (ret) {
470 		printk(KERN_ERR "lxfb:  power up failed!\n");
471 		return ret;
472 	}
473 
474 	fb_set_suspend(info, 0);
475 	console_unlock();
476 	return 0;
477 }
478 
479 static int lxfb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
480 {
481 	struct lxfb_par *par;
482 	struct fb_info *info;
483 	int ret;
484 
485 	struct fb_videomode *modedb_ptr;
486 	unsigned int modedb_size;
487 
488 	ret = aperture_remove_conflicting_pci_devices(pdev, "lxfb");
489 	if (ret)
490 		return ret;
491 
492 	info = lxfb_init_fbinfo(&pdev->dev);
493 
494 	if (info == NULL)
495 		return -ENOMEM;
496 
497 	par = info->par;
498 
499 	ret = lxfb_map_video_memory(info, pdev);
500 
501 	if (ret < 0) {
502 		dev_err(&pdev->dev,
503 			"failed to map frame buffer or controller registers\n");
504 		goto err;
505 	}
506 
507 	/* Set up the desired outputs */
508 
509 	par->output = 0;
510 	par->output |= (nopanel) ? 0 : OUTPUT_PANEL;
511 	par->output |= (nocrt) ? 0 : OUTPUT_CRT;
512 
513 	/* Set up the mode database */
514 
515 	get_modedb(&modedb_ptr, &modedb_size);
516 	ret = fb_find_mode(&info->var, info, mode_option,
517 			   modedb_ptr, modedb_size, NULL, 16);
518 
519 	if (ret == 0 || ret == 4) {
520 		dev_err(&pdev->dev, "could not find valid video mode\n");
521 		ret = -EINVAL;
522 		goto err;
523 	}
524 
525 	/* Clear the screen of garbage, unless noclear was specified,
526 	 * in which case we assume the user knows what he is doing */
527 
528 	if (!noclear)
529 		memset_io(info->screen_base, 0, info->fix.smem_len);
530 
531 	/* Set the mode */
532 
533 	lxfb_check_var(&info->var, info);
534 	lxfb_set_par(info);
535 
536 	pm_set_vt_switch(vt_switch);
537 
538 	if (register_framebuffer(info) < 0) {
539 		ret = -EINVAL;
540 		goto err;
541 	}
542 	pci_set_drvdata(pdev, info);
543 	fb_info(info, "%s frame buffer device\n", info->fix.id);
544 
545 	return 0;
546 
547 err:
548 	if (info->screen_base) {
549 		iounmap(info->screen_base);
550 		pci_release_region(pdev, 0);
551 	}
552 	if (par->gp_regs) {
553 		iounmap(par->gp_regs);
554 		pci_release_region(pdev, 1);
555 	}
556 	if (par->dc_regs) {
557 		iounmap(par->dc_regs);
558 		pci_release_region(pdev, 2);
559 	}
560 	if (par->vp_regs) {
561 		iounmap(par->vp_regs);
562 		pci_release_region(pdev, 3);
563 	}
564 
565 	fb_dealloc_cmap(&info->cmap);
566 	framebuffer_release(info);
567 
568 	return ret;
569 }
570 
571 static void lxfb_remove(struct pci_dev *pdev)
572 {
573 	struct fb_info *info = pci_get_drvdata(pdev);
574 	struct lxfb_par *par = info->par;
575 
576 	unregister_framebuffer(info);
577 
578 	iounmap(info->screen_base);
579 	pci_release_region(pdev, 0);
580 
581 	iounmap(par->gp_regs);
582 	pci_release_region(pdev, 1);
583 
584 	iounmap(par->dc_regs);
585 	pci_release_region(pdev, 2);
586 
587 	iounmap(par->vp_regs);
588 	pci_release_region(pdev, 3);
589 
590 	fb_dealloc_cmap(&info->cmap);
591 	framebuffer_release(info);
592 }
593 
594 static struct pci_device_id lxfb_id_table[] = {
595 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LX_VIDEO) },
596 	{ 0, }
597 };
598 
599 MODULE_DEVICE_TABLE(pci, lxfb_id_table);
600 
601 static const struct dev_pm_ops lxfb_pm_ops = {
602 #ifdef CONFIG_PM_SLEEP
603 	.suspend	= lxfb_suspend,
604 	.resume		= lxfb_resume,
605 	.freeze		= NULL,
606 	.thaw		= lxfb_resume,
607 	.poweroff	= NULL,
608 	.restore	= lxfb_resume,
609 #endif
610 };
611 
612 static struct pci_driver lxfb_driver = {
613 	.name		= "lxfb",
614 	.id_table	= lxfb_id_table,
615 	.probe		= lxfb_probe,
616 	.remove		= lxfb_remove,
617 	.driver.pm	= &lxfb_pm_ops,
618 };
619 
620 #ifndef MODULE
621 static int __init lxfb_setup(char *options)
622 {
623 	char *opt;
624 
625 	if (!options || !*options)
626 		return 0;
627 
628 	while ((opt = strsep(&options, ",")) != NULL) {
629 		if (!*opt)
630 			continue;
631 
632 		if (!strcmp(opt, "noclear"))
633 			noclear = 1;
634 		else if (!strcmp(opt, "nopanel"))
635 			nopanel = 1;
636 		else if (!strcmp(opt, "nocrt"))
637 			nocrt = 1;
638 		else
639 			mode_option = opt;
640 	}
641 
642 	return 0;
643 }
644 #endif
645 
646 static int __init lxfb_init(void)
647 {
648 #ifndef MODULE
649 	char *option = NULL;
650 
651 	if (fb_get_options("lxfb", &option))
652 		return -ENODEV;
653 
654 	lxfb_setup(option);
655 #endif
656 	return pci_register_driver(&lxfb_driver);
657 }
658 static void __exit lxfb_cleanup(void)
659 {
660 	pci_unregister_driver(&lxfb_driver);
661 }
662 
663 module_init(lxfb_init);
664 module_exit(lxfb_cleanup);
665 
666 module_param(mode_option, charp, 0);
667 MODULE_PARM_DESC(mode_option, "video mode (<x>x<y>[-<bpp>][@<refr>])");
668 
669 module_param(vram, int, 0);
670 MODULE_PARM_DESC(vram, "video memory size");
671 
672 module_param(vt_switch, int, 0);
673 MODULE_PARM_DESC(vt_switch, "enable VT switch during suspend/resume");
674 
675 MODULE_DESCRIPTION("Framebuffer driver for the AMD Geode LX");
676 MODULE_LICENSE("GPL");
677