xref: /linux/drivers/staging/sm750fb/sm750.c (revision 0be3ff0c)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/errno.h>
5 #include <linux/string.h>
6 #include <linux/mm.h>
7 #include <linux/slab.h>
8 #include <linux/delay.h>
9 #include <linux/fb.h>
10 #include <linux/ioport.h>
11 #include <linux/init.h>
12 #include <linux/pci.h>
13 #include <linux/mm_types.h>
14 #include <linux/vmalloc.h>
15 #include <linux/pagemap.h>
16 #include <linux/screen_info.h>
17 #include <linux/console.h>
18 #include <asm/fb.h>
19 #include "sm750.h"
20 #include "sm750_accel.h"
21 #include "sm750_cursor.h"
22 
23 /*
24  * #ifdef __BIG_ENDIAN
25  * ssize_t lynxfb_ops_write(struct fb_info *info, const char __user *buf,
26  * size_t count, loff_t *ppos);
27  * ssize_t lynxfb_ops_read(struct fb_info *info, char __user *buf,
28  * size_t count, loff_t *ppos);
29  * #endif
30  */
31 
32 /* common var for all device */
33 static int g_hwcursor = 1;
34 static int g_noaccel;
35 static int g_nomtrr;
36 static const char *g_fbmode[] = {NULL, NULL};
37 static const char *g_def_fbmode = "1024x768-32@60";
38 static char *g_settings;
39 static int g_dualview;
40 static char *g_option;
41 
42 static const struct fb_videomode lynx750_ext[] = {
43 	/*	1024x600-60 VESA	[1.71:1] */
44 	{NULL,  60, 1024, 600, 20423, 144,  40, 18, 1, 104, 3,
45 	 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
46 	 FB_VMODE_NONINTERLACED},
47 
48 	/*	1024x600-70 VESA */
49 	{NULL,  70, 1024, 600, 17211, 152,  48, 21, 1, 104, 3,
50 	 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
51 	 FB_VMODE_NONINTERLACED},
52 
53 	/*	1024x600-75 VESA */
54 	{NULL,  75, 1024, 600, 15822, 160,  56, 23, 1, 104, 3,
55 	 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
56 	 FB_VMODE_NONINTERLACED},
57 
58 	/*	1024x600-85 VESA */
59 	{NULL,  85, 1024, 600, 13730, 168,  56, 26, 1, 112, 3,
60 	 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
61 	 FB_VMODE_NONINTERLACED},
62 
63 	/*	720x480	*/
64 	{NULL, 60,  720,  480,  37427, 88,   16, 13, 1,   72,  3,
65 	 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
66 	 FB_VMODE_NONINTERLACED},
67 
68 	/*	1280x720		[1.78:1]	*/
69 	{NULL, 60,  1280,  720,  13426, 162, 86, 22, 1,  136, 3,
70 	 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
71 	 FB_VMODE_NONINTERLACED},
72 
73 	/*	1280x768@60 */
74 	{NULL, 60, 1280, 768, 12579, 192, 64, 20, 3, 128, 7,
75 	 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
76 	 FB_VMODE_NONINTERLACED},
77 
78 	/*	1360 x 768	[1.77083:1]	*/
79 	{NULL,  60, 1360, 768, 11804, 208,  64, 23, 1, 144, 3,
80 	 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
81 	 FB_VMODE_NONINTERLACED},
82 
83 	/*	1368 x 768      [1.78:1]	*/
84 	{NULL, 60,  1368,  768,  11647, 216, 72, 23, 1,  144, 3,
85 	 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
86 	 FB_VMODE_NONINTERLACED},
87 
88 	/*	1440 x 900		[16:10]	*/
89 	{NULL, 60, 1440, 900, 9392, 232, 80, 28, 1, 152, 3,
90 	 FB_SYNC_VERT_HIGH_ACT,
91 	 FB_VMODE_NONINTERLACED},
92 
93 	/*	1440x960		[15:10]	*/
94 	{NULL, 60, 1440, 960, 8733, 240, 88, 30, 1, 152, 3,
95 	 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
96 	 FB_VMODE_NONINTERLACED},
97 
98 	/*	1920x1080	[16:9]	*/
99 	{NULL, 60, 1920, 1080, 6734, 148, 88, 41, 1, 44, 3,
100 	 FB_SYNC_VERT_HIGH_ACT,
101 	 FB_VMODE_NONINTERLACED},
102 };
103 
104 /* no hardware cursor supported under version 2.6.10, kernel bug */
105 static int lynxfb_ops_cursor(struct fb_info *info, struct fb_cursor *fbcursor)
106 {
107 	struct lynxfb_par *par;
108 	struct lynxfb_crtc *crtc;
109 	struct lynx_cursor *cursor;
110 
111 	par = info->par;
112 	crtc = &par->crtc;
113 	cursor = &crtc->cursor;
114 
115 	if (fbcursor->image.width > cursor->max_w ||
116 	    fbcursor->image.height > cursor->max_h ||
117 	    fbcursor->image.depth > 1) {
118 		return -ENXIO;
119 	}
120 
121 	sm750_hw_cursor_disable(cursor);
122 	if (fbcursor->set & FB_CUR_SETSIZE)
123 		sm750_hw_cursor_setSize(cursor,
124 					fbcursor->image.width,
125 					fbcursor->image.height);
126 
127 	if (fbcursor->set & FB_CUR_SETPOS)
128 		sm750_hw_cursor_setPos(cursor,
129 				       fbcursor->image.dx - info->var.xoffset,
130 				       fbcursor->image.dy - info->var.yoffset);
131 
132 	if (fbcursor->set & FB_CUR_SETCMAP) {
133 		/* get the 16bit color of kernel means */
134 		u16 fg, bg;
135 
136 		fg = ((info->cmap.red[fbcursor->image.fg_color] & 0xf800)) |
137 		     ((info->cmap.green[fbcursor->image.fg_color] & 0xfc00) >> 5) |
138 		     ((info->cmap.blue[fbcursor->image.fg_color] & 0xf800) >> 11);
139 
140 		bg = ((info->cmap.red[fbcursor->image.bg_color] & 0xf800)) |
141 		     ((info->cmap.green[fbcursor->image.bg_color] & 0xfc00) >> 5) |
142 		     ((info->cmap.blue[fbcursor->image.bg_color] & 0xf800) >> 11);
143 
144 		sm750_hw_cursor_setColor(cursor, fg, bg);
145 	}
146 
147 	if (fbcursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
148 		sm750_hw_cursor_setData(cursor,
149 					fbcursor->rop,
150 					fbcursor->image.data,
151 					fbcursor->mask);
152 	}
153 
154 	if (fbcursor->enable)
155 		sm750_hw_cursor_enable(cursor);
156 
157 	return 0;
158 }
159 
160 static void lynxfb_ops_fillrect(struct fb_info *info,
161 				const struct fb_fillrect *region)
162 {
163 	struct lynxfb_par *par;
164 	struct sm750_dev *sm750_dev;
165 	unsigned int base, pitch, Bpp, rop;
166 	u32 color;
167 
168 	if (info->state != FBINFO_STATE_RUNNING)
169 		return;
170 
171 	par = info->par;
172 	sm750_dev = par->dev;
173 
174 	/*
175 	 * each time 2d function begin to work,below three variable always need
176 	 * be set, seems we can put them together in some place
177 	 */
178 	base = par->crtc.o_screen;
179 	pitch = info->fix.line_length;
180 	Bpp = info->var.bits_per_pixel >> 3;
181 
182 	color = (Bpp == 1) ? region->color :
183 		((u32 *)info->pseudo_palette)[region->color];
184 	rop = (region->rop != ROP_COPY) ? HW_ROP2_XOR : HW_ROP2_COPY;
185 
186 	/*
187 	 * If not use spin_lock, system will die if user load driver
188 	 * and immediately unload driver frequently (dual)
189 	 * since they fb_count could change during the lifetime of
190 	 * this lock, we are holding it for all cases.
191 	 */
192 	spin_lock(&sm750_dev->slock);
193 
194 	sm750_dev->accel.de_fillrect(&sm750_dev->accel,
195 				     base, pitch, Bpp,
196 				     region->dx, region->dy,
197 				     region->width, region->height,
198 				     color, rop);
199 	spin_unlock(&sm750_dev->slock);
200 }
201 
202 static void lynxfb_ops_copyarea(struct fb_info *info,
203 				const struct fb_copyarea *region)
204 {
205 	struct lynxfb_par *par;
206 	struct sm750_dev *sm750_dev;
207 	unsigned int base, pitch, Bpp;
208 
209 	par = info->par;
210 	sm750_dev = par->dev;
211 
212 	/*
213 	 * each time 2d function begin to work,below three variable always need
214 	 * be set, seems we can put them together in some place
215 	 */
216 	base = par->crtc.o_screen;
217 	pitch = info->fix.line_length;
218 	Bpp = info->var.bits_per_pixel >> 3;
219 
220 	/*
221 	 * If not use spin_lock, system will die if user load driver
222 	 * and immediately unload driver frequently (dual)
223 	 * since they fb_count could change during the lifetime of
224 	 * this lock, we are holding it for all cases.
225 	 */
226 	spin_lock(&sm750_dev->slock);
227 
228 	sm750_dev->accel.de_copyarea(&sm750_dev->accel,
229 				     base, pitch, region->sx, region->sy,
230 				     base, pitch, Bpp, region->dx, region->dy,
231 				     region->width, region->height,
232 				     HW_ROP2_COPY);
233 	spin_unlock(&sm750_dev->slock);
234 }
235 
236 static void lynxfb_ops_imageblit(struct fb_info *info,
237 				 const struct fb_image *image)
238 {
239 	unsigned int base, pitch, Bpp;
240 	unsigned int fgcol, bgcol;
241 	struct lynxfb_par *par;
242 	struct sm750_dev *sm750_dev;
243 
244 	par = info->par;
245 	sm750_dev = par->dev;
246 	/*
247 	 * each time 2d function begin to work,below three variable always need
248 	 * be set, seems we can put them together in some place
249 	 */
250 	base = par->crtc.o_screen;
251 	pitch = info->fix.line_length;
252 	Bpp = info->var.bits_per_pixel >> 3;
253 
254 	/* TODO: Implement hardware acceleration for image->depth > 1 */
255 	if (image->depth != 1) {
256 		cfb_imageblit(info, image);
257 		return;
258 	}
259 
260 	if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
261 	    info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
262 		fgcol = ((u32 *)info->pseudo_palette)[image->fg_color];
263 		bgcol = ((u32 *)info->pseudo_palette)[image->bg_color];
264 	} else {
265 		fgcol = image->fg_color;
266 		bgcol = image->bg_color;
267 	}
268 
269 	/*
270 	 * If not use spin_lock, system will die if user load driver
271 	 * and immediately unload driver frequently (dual)
272 	 * since they fb_count could change during the lifetime of
273 	 * this lock, we are holding it for all cases.
274 	 */
275 	spin_lock(&sm750_dev->slock);
276 
277 	sm750_dev->accel.de_imageblit(&sm750_dev->accel,
278 				      image->data, image->width >> 3, 0,
279 				      base, pitch, Bpp,
280 				      image->dx, image->dy,
281 				      image->width, image->height,
282 				      fgcol, bgcol, HW_ROP2_COPY);
283 	spin_unlock(&sm750_dev->slock);
284 }
285 
286 static int lynxfb_ops_pan_display(struct fb_var_screeninfo *var,
287 				  struct fb_info *info)
288 {
289 	struct lynxfb_par *par;
290 	struct lynxfb_crtc *crtc;
291 
292 	if (!info)
293 		return -EINVAL;
294 
295 	par = info->par;
296 	crtc = &par->crtc;
297 	return hw_sm750_pan_display(crtc, var, info);
298 }
299 
300 static inline void lynxfb_set_visual_mode(struct fb_info *info)
301 {
302 	switch (info->var.bits_per_pixel) {
303 	case 8:
304 		info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
305 		break;
306 	case 16:
307 	case 24:
308 	case 32:
309 		info->fix.visual = FB_VISUAL_TRUECOLOR;
310 		break;
311 	default:
312 		break;
313 	}
314 }
315 
316 static inline int lynxfb_set_color_offsets(struct fb_info *info)
317 {
318 	lynxfb_set_visual_mode(info);
319 
320 	switch (info->var.bits_per_pixel) {
321 	case 8:
322 		info->var.red.offset = 0;
323 		info->var.red.length = 8;
324 		info->var.green.offset = 0;
325 		info->var.green.length = 8;
326 		info->var.blue.offset = 0;
327 		info->var.blue.length = 8;
328 		info->var.transp.length = 0;
329 		info->var.transp.offset = 0;
330 		break;
331 	case 16:
332 		info->var.red.offset = 11;
333 		info->var.red.length = 5;
334 		info->var.green.offset = 5;
335 		info->var.green.length = 6;
336 		info->var.blue.offset = 0;
337 		info->var.blue.length = 5;
338 		info->var.transp.length = 0;
339 		info->var.transp.offset = 0;
340 		break;
341 	case 24:
342 	case 32:
343 		info->var.red.offset = 16;
344 		info->var.red.length = 8;
345 		info->var.green.offset = 8;
346 		info->var.green.length = 8;
347 		info->var.blue.offset = 0;
348 		info->var.blue.length = 8;
349 		break;
350 	default:
351 		return -EINVAL;
352 	}
353 	return 0;
354 }
355 
356 static int lynxfb_ops_set_par(struct fb_info *info)
357 {
358 	struct lynxfb_par *par;
359 	struct lynxfb_crtc *crtc;
360 	struct lynxfb_output *output;
361 	struct fb_var_screeninfo *var;
362 	struct fb_fix_screeninfo *fix;
363 	int ret;
364 	unsigned int line_length;
365 
366 	if (!info)
367 		return -EINVAL;
368 
369 	ret = 0;
370 	par = info->par;
371 	crtc = &par->crtc;
372 	output = &par->output;
373 	var = &info->var;
374 	fix = &info->fix;
375 
376 	/* fix structure is not so FIX ... */
377 	line_length = var->xres_virtual * var->bits_per_pixel / 8;
378 	line_length = ALIGN(line_length, crtc->line_pad);
379 	fix->line_length = line_length;
380 	pr_info("fix->line_length = %d\n", fix->line_length);
381 
382 	/*
383 	 * var->red,green,blue,transp are need to be set by driver
384 	 * and these data should be set before setcolreg routine
385 	 */
386 
387 	ret = lynxfb_set_color_offsets(info);
388 
389 	var->height = var->width = -1;
390 	var->accel_flags = 0;/*FB_ACCELF_TEXT;*/
391 
392 	if (ret) {
393 		pr_err("bpp %d not supported\n", var->bits_per_pixel);
394 		return ret;
395 	}
396 	ret = hw_sm750_crtc_setMode(crtc, var, fix);
397 	if (!ret)
398 		ret = hw_sm750_output_setMode(output, var, fix);
399 	return ret;
400 }
401 
402 static inline unsigned int chan_to_field(unsigned int chan,
403 					 struct fb_bitfield *bf)
404 {
405 	chan &= 0xffff;
406 	chan >>= 16 - bf->length;
407 	return chan << bf->offset;
408 }
409 
410 static int __maybe_unused lynxfb_suspend(struct device *dev)
411 {
412 	struct fb_info *info;
413 	struct sm750_dev *sm750_dev;
414 
415 	sm750_dev = dev_get_drvdata(dev);
416 
417 	console_lock();
418 	info = sm750_dev->fbinfo[0];
419 	if (info)
420 		/* 1 means do suspend */
421 		fb_set_suspend(info, 1);
422 	info = sm750_dev->fbinfo[1];
423 	if (info)
424 		/* 1 means do suspend */
425 		fb_set_suspend(info, 1);
426 
427 	console_unlock();
428 	return 0;
429 }
430 
431 static int __maybe_unused lynxfb_resume(struct device *dev)
432 {
433 	struct pci_dev *pdev = to_pci_dev(dev);
434 	struct fb_info *info;
435 	struct sm750_dev *sm750_dev;
436 
437 	struct lynxfb_par *par;
438 	struct lynxfb_crtc *crtc;
439 	struct lynx_cursor *cursor;
440 
441 	sm750_dev = pci_get_drvdata(pdev);
442 
443 	console_lock();
444 
445 	hw_sm750_inithw(sm750_dev, pdev);
446 
447 	info = sm750_dev->fbinfo[0];
448 
449 	if (info) {
450 		par = info->par;
451 		crtc = &par->crtc;
452 		cursor = &crtc->cursor;
453 		memset_io(cursor->vstart, 0x0, cursor->size);
454 		memset_io(crtc->v_screen, 0x0, crtc->vidmem_size);
455 		lynxfb_ops_set_par(info);
456 		fb_set_suspend(info, 0);
457 	}
458 
459 	info = sm750_dev->fbinfo[1];
460 
461 	if (info) {
462 		par = info->par;
463 		crtc = &par->crtc;
464 		cursor = &crtc->cursor;
465 		memset_io(cursor->vstart, 0x0, cursor->size);
466 		memset_io(crtc->v_screen, 0x0, crtc->vidmem_size);
467 		lynxfb_ops_set_par(info);
468 		fb_set_suspend(info, 0);
469 	}
470 
471 	pdev->dev.power.power_state.event = PM_EVENT_RESUME;
472 
473 	console_unlock();
474 	return 0;
475 }
476 
477 static int lynxfb_ops_check_var(struct fb_var_screeninfo *var,
478 				struct fb_info *info)
479 {
480 	int ret;
481 	struct lynxfb_par *par;
482 	struct lynxfb_crtc *crtc;
483 	resource_size_t request;
484 
485 	ret = 0;
486 	par = info->par;
487 	crtc = &par->crtc;
488 
489 	pr_debug("check var:%dx%d-%d\n",
490 		 var->xres,
491 		 var->yres,
492 		 var->bits_per_pixel);
493 
494 	ret = lynxfb_set_color_offsets(info);
495 
496 	if (ret) {
497 		pr_err("bpp %d not supported\n", var->bits_per_pixel);
498 		return ret;
499 	}
500 
501 	var->height = var->width = -1;
502 	var->accel_flags = 0;/* FB_ACCELF_TEXT; */
503 
504 	/* check if current fb's video memory big enough to hold the onscreen*/
505 	request = var->xres_virtual * (var->bits_per_pixel >> 3);
506 	/* defaulty crtc->channel go with par->index */
507 
508 	request = ALIGN(request, crtc->line_pad);
509 	request = request * var->yres_virtual;
510 	if (crtc->vidmem_size < request) {
511 		pr_err("not enough video memory for mode\n");
512 		return -ENOMEM;
513 	}
514 
515 	return hw_sm750_crtc_checkMode(crtc, var);
516 }
517 
518 static int lynxfb_ops_setcolreg(unsigned int regno,
519 				unsigned int red,
520 				unsigned int green,
521 				unsigned int blue,
522 				unsigned int transp,
523 				struct fb_info *info)
524 {
525 	struct lynxfb_par *par;
526 	struct lynxfb_crtc *crtc;
527 	struct fb_var_screeninfo *var;
528 	int ret;
529 
530 	par = info->par;
531 	crtc = &par->crtc;
532 	var = &info->var;
533 	ret = 0;
534 
535 	if (regno > 256) {
536 		pr_err("regno = %d\n", regno);
537 		return -EINVAL;
538 	}
539 
540 	if (info->var.grayscale)
541 		red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
542 
543 	if (var->bits_per_pixel == 8 &&
544 	    info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
545 		red >>= 8;
546 		green >>= 8;
547 		blue >>= 8;
548 		ret = hw_sm750_setColReg(crtc, regno, red, green, blue);
549 		goto exit;
550 	}
551 
552 	if (info->fix.visual == FB_VISUAL_TRUECOLOR && regno < 256) {
553 		u32 val;
554 
555 		if (var->bits_per_pixel == 16 ||
556 		    var->bits_per_pixel == 32 ||
557 		    var->bits_per_pixel == 24) {
558 			val = chan_to_field(red, &var->red);
559 			val |= chan_to_field(green, &var->green);
560 			val |= chan_to_field(blue, &var->blue);
561 			par->pseudo_palette[regno] = val;
562 			goto exit;
563 		}
564 	}
565 
566 	ret = -EINVAL;
567 
568 exit:
569 	return ret;
570 }
571 
572 static int lynxfb_ops_blank(int blank, struct fb_info *info)
573 {
574 	struct lynxfb_par *par;
575 	struct lynxfb_output *output;
576 
577 	pr_debug("blank = %d.\n", blank);
578 	par = info->par;
579 	output = &par->output;
580 	return output->proc_setBLANK(output, blank);
581 }
582 
583 static int sm750fb_set_drv(struct lynxfb_par *par)
584 {
585 	int ret;
586 	struct sm750_dev *sm750_dev;
587 	struct lynxfb_output *output;
588 	struct lynxfb_crtc *crtc;
589 
590 	ret = 0;
591 
592 	sm750_dev = par->dev;
593 	output = &par->output;
594 	crtc = &par->crtc;
595 
596 	crtc->vidmem_size = sm750_dev->vidmem_size;
597 	if (sm750_dev->fb_count > 1)
598 		crtc->vidmem_size >>= 1;
599 
600 	/* setup crtc and output member */
601 	sm750_dev->hwCursor = g_hwcursor;
602 
603 	crtc->line_pad = 16;
604 	crtc->xpanstep = 8;
605 	crtc->ypanstep = 1;
606 	crtc->ywrapstep = 0;
607 
608 	output->proc_setBLANK = (sm750_dev->revid == SM750LE_REVISION_ID) ?
609 				 hw_sm750le_setBLANK : hw_sm750_setBLANK;
610 	/* chip specific phase */
611 	sm750_dev->accel.de_wait = (sm750_dev->revid == SM750LE_REVISION_ID) ?
612 				    hw_sm750le_deWait : hw_sm750_deWait;
613 	switch (sm750_dev->dataflow) {
614 	case sm750_simul_pri:
615 		output->paths = sm750_pnc;
616 		crtc->channel = sm750_primary;
617 		crtc->o_screen = 0;
618 		crtc->v_screen = sm750_dev->pvMem;
619 		pr_info("use simul primary mode\n");
620 		break;
621 	case sm750_simul_sec:
622 		output->paths = sm750_pnc;
623 		crtc->channel = sm750_secondary;
624 		crtc->o_screen = 0;
625 		crtc->v_screen = sm750_dev->pvMem;
626 		break;
627 	case sm750_dual_normal:
628 		if (par->index == 0) {
629 			output->paths = sm750_panel;
630 			crtc->channel = sm750_primary;
631 			crtc->o_screen = 0;
632 			crtc->v_screen = sm750_dev->pvMem;
633 		} else {
634 			output->paths = sm750_crt;
635 			crtc->channel = sm750_secondary;
636 			/* not consider of padding stuffs for o_screen,need fix */
637 			crtc->o_screen = sm750_dev->vidmem_size >> 1;
638 			crtc->v_screen = sm750_dev->pvMem + crtc->o_screen;
639 		}
640 		break;
641 	case sm750_dual_swap:
642 		if (par->index == 0) {
643 			output->paths = sm750_panel;
644 			crtc->channel = sm750_secondary;
645 			crtc->o_screen = 0;
646 			crtc->v_screen = sm750_dev->pvMem;
647 		} else {
648 			output->paths = sm750_crt;
649 			crtc->channel = sm750_primary;
650 			/* not consider of padding stuffs for o_screen,
651 			 * need fix
652 			 */
653 			crtc->o_screen = sm750_dev->vidmem_size >> 1;
654 			crtc->v_screen = sm750_dev->pvMem + crtc->o_screen;
655 		}
656 		break;
657 	default:
658 		ret = -EINVAL;
659 	}
660 
661 	return ret;
662 }
663 
664 static struct fb_ops lynxfb_ops = {
665 	.owner = THIS_MODULE,
666 	.fb_check_var =  lynxfb_ops_check_var,
667 	.fb_set_par = lynxfb_ops_set_par,
668 	.fb_setcolreg = lynxfb_ops_setcolreg,
669 	.fb_blank = lynxfb_ops_blank,
670 	.fb_fillrect = cfb_fillrect,
671 	.fb_imageblit = cfb_imageblit,
672 	.fb_copyarea = cfb_copyarea,
673 	/* cursor */
674 	.fb_cursor = lynxfb_ops_cursor,
675 };
676 
677 static int lynxfb_set_fbinfo(struct fb_info *info, int index)
678 {
679 	int i;
680 	struct lynxfb_par *par;
681 	struct sm750_dev *sm750_dev;
682 	struct lynxfb_crtc *crtc;
683 	struct lynxfb_output *output;
684 	struct fb_var_screeninfo *var;
685 	struct fb_fix_screeninfo *fix;
686 
687 	const struct fb_videomode *pdb[] = {
688 		lynx750_ext, NULL, vesa_modes,
689 	};
690 	int cdb[] = {ARRAY_SIZE(lynx750_ext), 0, VESA_MODEDB_SIZE};
691 	static const char * const mdb_desc[] = {
692 		"driver prepared modes",
693 		"kernel prepared default modedb",
694 		"kernel HELPERS prepared vesa_modes",
695 	};
696 
697 	static const char *fixId[2] = {
698 		"sm750_fb1", "sm750_fb2",
699 	};
700 
701 	int ret, line_length;
702 
703 	ret = 0;
704 	par = (struct lynxfb_par *)info->par;
705 	sm750_dev = par->dev;
706 	crtc = &par->crtc;
707 	output = &par->output;
708 	var = &info->var;
709 	fix = &info->fix;
710 
711 	/* set index */
712 	par->index = index;
713 	output->channel = &crtc->channel;
714 	sm750fb_set_drv(par);
715 	lynxfb_ops.fb_pan_display = lynxfb_ops_pan_display;
716 
717 	/*
718 	 * set current cursor variable and proc pointer,
719 	 * must be set after crtc member initialized
720 	 */
721 	crtc->cursor.offset = crtc->o_screen + crtc->vidmem_size - 1024;
722 	crtc->cursor.mmio = sm750_dev->pvReg +
723 		0x800f0 + (int)crtc->channel * 0x140;
724 
725 	pr_info("crtc->cursor.mmio = %p\n", crtc->cursor.mmio);
726 	crtc->cursor.max_h = crtc->cursor.max_w = 64;
727 	crtc->cursor.size = crtc->cursor.max_h * crtc->cursor.max_w * 2 / 8;
728 	crtc->cursor.vstart = sm750_dev->pvMem + crtc->cursor.offset;
729 
730 	memset_io(crtc->cursor.vstart, 0, crtc->cursor.size);
731 	if (!g_hwcursor) {
732 		lynxfb_ops.fb_cursor = NULL;
733 		sm750_hw_cursor_disable(&crtc->cursor);
734 	}
735 
736 	/* set info->fbops, must be set before fb_find_mode */
737 	if (!sm750_dev->accel_off) {
738 		/* use 2d acceleration */
739 		lynxfb_ops.fb_fillrect = lynxfb_ops_fillrect;
740 		lynxfb_ops.fb_copyarea = lynxfb_ops_copyarea;
741 		lynxfb_ops.fb_imageblit = lynxfb_ops_imageblit;
742 	}
743 	info->fbops = &lynxfb_ops;
744 
745 	if (!g_fbmode[index]) {
746 		g_fbmode[index] = g_def_fbmode;
747 		if (index)
748 			g_fbmode[index] = g_fbmode[0];
749 	}
750 
751 	for (i = 0; i < 3; i++) {
752 		ret = fb_find_mode(var, info, g_fbmode[index],
753 				   pdb[i], cdb[i], NULL, 8);
754 
755 		if (ret == 1) {
756 			pr_info("success! use specified mode:%s in %s\n",
757 				g_fbmode[index],
758 				mdb_desc[i]);
759 			break;
760 		} else if (ret == 2) {
761 			pr_warn("use specified mode:%s in %s,with an ignored refresh rate\n",
762 				g_fbmode[index],
763 				mdb_desc[i]);
764 			break;
765 		} else if (ret == 3) {
766 			pr_warn("wanna use default mode\n");
767 			/*break;*/
768 		} else if (ret == 4) {
769 			pr_warn("fall back to any valid mode\n");
770 		} else {
771 			pr_warn("ret = %d,fb_find_mode failed,with %s\n",
772 				ret,
773 				mdb_desc[i]);
774 		}
775 	}
776 
777 	/* some member of info->var had been set by fb_find_mode */
778 
779 	pr_info("Member of info->var is :\n"
780 		"xres=%d\n"
781 		"yres=%d\n"
782 		"xres_virtual=%d\n"
783 		"yres_virtual=%d\n"
784 		"xoffset=%d\n"
785 		"yoffset=%d\n"
786 		"bits_per_pixel=%d\n"
787 		" ...\n",
788 		var->xres,
789 		var->yres,
790 		var->xres_virtual,
791 		var->yres_virtual,
792 		var->xoffset,
793 		var->yoffset,
794 		var->bits_per_pixel);
795 
796 	/* set par */
797 	par->info = info;
798 
799 	/* set info */
800 	line_length = ALIGN((var->xres_virtual * var->bits_per_pixel / 8),
801 			    crtc->line_pad);
802 
803 	info->pseudo_palette = &par->pseudo_palette[0];
804 	info->screen_base = crtc->v_screen;
805 	pr_debug("screen_base vaddr = %p\n", info->screen_base);
806 	info->screen_size = line_length * var->yres_virtual;
807 	info->flags = FBINFO_FLAG_DEFAULT | 0;
808 
809 	/* set info->fix */
810 	fix->type = FB_TYPE_PACKED_PIXELS;
811 	fix->type_aux = 0;
812 	fix->xpanstep = crtc->xpanstep;
813 	fix->ypanstep = crtc->ypanstep;
814 	fix->ywrapstep = crtc->ywrapstep;
815 	fix->accel = FB_ACCEL_SMI;
816 
817 	strscpy(fix->id, fixId[index], sizeof(fix->id));
818 
819 	fix->smem_start = crtc->o_screen + sm750_dev->vidmem_start;
820 	pr_info("fix->smem_start = %lx\n", fix->smem_start);
821 	/*
822 	 * according to mmap experiment from user space application,
823 	 * fix->mmio_len should not larger than virtual size
824 	 * (xres_virtual x yres_virtual x ByPP)
825 	 * Below line maybe buggy when user mmap fb dev node and write
826 	 * data into the bound over virtual size
827 	 */
828 	fix->smem_len = crtc->vidmem_size;
829 	pr_info("fix->smem_len = %x\n", fix->smem_len);
830 	info->screen_size = fix->smem_len;
831 	fix->line_length = line_length;
832 	fix->mmio_start = sm750_dev->vidreg_start;
833 	pr_info("fix->mmio_start = %lx\n", fix->mmio_start);
834 	fix->mmio_len = sm750_dev->vidreg_size;
835 	pr_info("fix->mmio_len = %x\n", fix->mmio_len);
836 
837 	lynxfb_set_visual_mode(info);
838 
839 	/* set var */
840 	var->activate = FB_ACTIVATE_NOW;
841 	var->accel_flags = 0;
842 	var->vmode = FB_VMODE_NONINTERLACED;
843 
844 	pr_debug("#1 show info->cmap :\nstart=%d,len=%d,red=%p,green=%p,blue=%p,transp=%p\n",
845 		 info->cmap.start, info->cmap.len,
846 		 info->cmap.red, info->cmap.green, info->cmap.blue,
847 		 info->cmap.transp);
848 
849 	ret = fb_alloc_cmap(&info->cmap, 256, 0);
850 	if (ret < 0) {
851 		pr_err("Could not allocate memory for cmap.\n");
852 		goto exit;
853 	}
854 
855 	pr_debug("#2 show info->cmap :\nstart=%d,len=%d,red=%p,green=%p,blue=%p,transp=%p\n",
856 		 info->cmap.start, info->cmap.len,
857 		 info->cmap.red, info->cmap.green, info->cmap.blue,
858 		 info->cmap.transp);
859 
860 exit:
861 	lynxfb_ops_check_var(var, info);
862 	return ret;
863 }
864 
865 /*	chip specific g_option configuration routine */
866 static void sm750fb_setup(struct sm750_dev *sm750_dev, char *src)
867 {
868 	char *opt;
869 	int swap;
870 
871 	swap = 0;
872 
873 	sm750_dev->initParm.chip_clk = 0;
874 	sm750_dev->initParm.mem_clk = 0;
875 	sm750_dev->initParm.master_clk = 0;
876 	sm750_dev->initParm.powerMode = 0;
877 	sm750_dev->initParm.setAllEngOff = 0;
878 	sm750_dev->initParm.resetMemory = 1;
879 
880 	/* defaultly turn g_hwcursor on for both view */
881 	g_hwcursor = 3;
882 
883 	if (!src || !*src) {
884 		dev_warn(&sm750_dev->pdev->dev, "no specific g_option.\n");
885 		goto NO_PARAM;
886 	}
887 
888 	while ((opt = strsep(&src, ":")) != NULL && *opt != 0) {
889 		dev_info(&sm750_dev->pdev->dev, "opt=%s\n", opt);
890 		dev_info(&sm750_dev->pdev->dev, "src=%s\n", src);
891 
892 		if (!strncmp(opt, "swap", strlen("swap"))) {
893 			swap = 1;
894 		} else if (!strncmp(opt, "nocrt", strlen("nocrt"))) {
895 			sm750_dev->nocrt = 1;
896 		} else if (!strncmp(opt, "36bit", strlen("36bit"))) {
897 			sm750_dev->pnltype = sm750_doubleTFT;
898 		} else if (!strncmp(opt, "18bit", strlen("18bit"))) {
899 			sm750_dev->pnltype = sm750_dualTFT;
900 		} else if (!strncmp(opt, "24bit", strlen("24bit"))) {
901 			sm750_dev->pnltype = sm750_24TFT;
902 		} else if (!strncmp(opt, "nohwc0", strlen("nohwc0"))) {
903 			g_hwcursor &= ~0x1;
904 		} else if (!strncmp(opt, "nohwc1", strlen("nohwc1"))) {
905 			g_hwcursor &= ~0x2;
906 		} else if (!strncmp(opt, "nohwc", strlen("nohwc"))) {
907 			g_hwcursor = 0;
908 		} else {
909 			if (!g_fbmode[0]) {
910 				g_fbmode[0] = opt;
911 				dev_info(&sm750_dev->pdev->dev,
912 					 "find fbmode0 : %s\n", g_fbmode[0]);
913 			} else if (!g_fbmode[1]) {
914 				g_fbmode[1] = opt;
915 				dev_info(&sm750_dev->pdev->dev,
916 					 "find fbmode1 : %s\n", g_fbmode[1]);
917 			} else {
918 				dev_warn(&sm750_dev->pdev->dev, "How many view you wann set?\n");
919 			}
920 		}
921 	}
922 
923 NO_PARAM:
924 	if (sm750_dev->revid != SM750LE_REVISION_ID) {
925 		if (sm750_dev->fb_count > 1) {
926 			if (swap)
927 				sm750_dev->dataflow = sm750_dual_swap;
928 			else
929 				sm750_dev->dataflow = sm750_dual_normal;
930 		} else {
931 			if (swap)
932 				sm750_dev->dataflow = sm750_simul_sec;
933 			else
934 				sm750_dev->dataflow = sm750_simul_pri;
935 		}
936 	} else {
937 		/* SM750LE only have one crt channel */
938 		sm750_dev->dataflow = sm750_simul_sec;
939 		/* sm750le do not have complex attributes */
940 		sm750_dev->nocrt = 0;
941 	}
942 }
943 
944 static void sm750fb_framebuffer_release(struct sm750_dev *sm750_dev)
945 {
946 	struct fb_info *fb_info;
947 
948 	while (sm750_dev->fb_count) {
949 		fb_info = sm750_dev->fbinfo[sm750_dev->fb_count - 1];
950 		unregister_framebuffer(fb_info);
951 		framebuffer_release(fb_info);
952 		sm750_dev->fb_count--;
953 	}
954 }
955 
956 static int sm750fb_framebuffer_alloc(struct sm750_dev *sm750_dev, int fbidx)
957 {
958 	struct fb_info *fb_info;
959 	struct lynxfb_par *par;
960 	int err;
961 
962 	fb_info = framebuffer_alloc(sizeof(struct lynxfb_par),
963 				    &sm750_dev->pdev->dev);
964 	if (!fb_info)
965 		return -ENOMEM;
966 
967 	sm750_dev->fbinfo[fbidx] = fb_info;
968 	par = fb_info->par;
969 	par->dev = sm750_dev;
970 
971 	err = lynxfb_set_fbinfo(fb_info, fbidx);
972 	if (err)
973 		goto release_fb;
974 
975 	err = register_framebuffer(fb_info);
976 	if (err < 0)
977 		goto release_fb;
978 
979 	sm750_dev->fb_count++;
980 
981 	return 0;
982 
983 release_fb:
984 	framebuffer_release(fb_info);
985 	return err;
986 }
987 
988 static int lynxfb_kick_out_firmware_fb(struct pci_dev *pdev)
989 {
990 	struct apertures_struct *ap;
991 	bool primary = false;
992 
993 	ap = alloc_apertures(1);
994 	if (!ap)
995 		return -ENOMEM;
996 
997 	ap->ranges[0].base = pci_resource_start(pdev, 0);
998 	ap->ranges[0].size = pci_resource_len(pdev, 0);
999 #ifdef CONFIG_X86
1000 	primary = pdev->resource[PCI_ROM_RESOURCE].flags &
1001 					IORESOURCE_ROM_SHADOW;
1002 #endif
1003 	remove_conflicting_framebuffers(ap, "sm750_fb1", primary);
1004 	kfree(ap);
1005 	return 0;
1006 }
1007 
1008 static int lynxfb_pci_probe(struct pci_dev *pdev,
1009 			    const struct pci_device_id *ent)
1010 {
1011 	struct sm750_dev *sm750_dev = NULL;
1012 	int max_fb;
1013 	int fbidx;
1014 	int err;
1015 
1016 	err = lynxfb_kick_out_firmware_fb(pdev);
1017 	if (err)
1018 		return err;
1019 
1020 	/* enable device */
1021 	err = pcim_enable_device(pdev);
1022 	if (err)
1023 		return err;
1024 
1025 	err = -ENOMEM;
1026 	sm750_dev = devm_kzalloc(&pdev->dev, sizeof(*sm750_dev), GFP_KERNEL);
1027 	if (!sm750_dev)
1028 		return err;
1029 
1030 	sm750_dev->fbinfo[0] = sm750_dev->fbinfo[1] = NULL;
1031 	sm750_dev->devid = pdev->device;
1032 	sm750_dev->revid = pdev->revision;
1033 	sm750_dev->pdev = pdev;
1034 	sm750_dev->mtrr_off = g_nomtrr;
1035 	sm750_dev->mtrr.vram = 0;
1036 	sm750_dev->accel_off = g_noaccel;
1037 	spin_lock_init(&sm750_dev->slock);
1038 
1039 	if (!sm750_dev->accel_off) {
1040 		/*
1041 		 * hook deInit and 2d routines, notes that below hw_xxx
1042 		 * routine can work on most of lynx chips
1043 		 * if some chip need specific function,
1044 		 * please hook it in smXXX_set_drv routine
1045 		 */
1046 		sm750_dev->accel.de_init = sm750_hw_de_init;
1047 		sm750_dev->accel.de_fillrect = sm750_hw_fillrect;
1048 		sm750_dev->accel.de_copyarea = sm750_hw_copyarea;
1049 		sm750_dev->accel.de_imageblit = sm750_hw_imageblit;
1050 	}
1051 
1052 	/* call chip specific setup routine  */
1053 	sm750fb_setup(sm750_dev, g_settings);
1054 
1055 	/* call chip specific mmap routine */
1056 	err = hw_sm750_map(sm750_dev, pdev);
1057 	if (err)
1058 		return err;
1059 
1060 	if (!sm750_dev->mtrr_off)
1061 		sm750_dev->mtrr.vram = arch_phys_wc_add(sm750_dev->vidmem_start,
1062 							sm750_dev->vidmem_size);
1063 
1064 	memset_io(sm750_dev->pvMem, 0, sm750_dev->vidmem_size);
1065 
1066 	pci_set_drvdata(pdev, sm750_dev);
1067 
1068 	/* call chipInit routine */
1069 	hw_sm750_inithw(sm750_dev, pdev);
1070 
1071 	/* allocate frame buffer info structures according to g_dualview */
1072 	max_fb = g_dualview ? 2 : 1;
1073 	for (fbidx = 0; fbidx < max_fb; fbidx++) {
1074 		err = sm750fb_framebuffer_alloc(sm750_dev, fbidx);
1075 		if (err)
1076 			goto release_fb;
1077 	}
1078 
1079 	return 0;
1080 
1081 release_fb:
1082 	sm750fb_framebuffer_release(sm750_dev);
1083 	return err;
1084 }
1085 
1086 static void lynxfb_pci_remove(struct pci_dev *pdev)
1087 {
1088 	struct sm750_dev *sm750_dev;
1089 
1090 	sm750_dev = pci_get_drvdata(pdev);
1091 
1092 	sm750fb_framebuffer_release(sm750_dev);
1093 	arch_phys_wc_del(sm750_dev->mtrr.vram);
1094 
1095 	iounmap(sm750_dev->pvReg);
1096 	iounmap(sm750_dev->pvMem);
1097 	kfree(g_settings);
1098 }
1099 
1100 static int __init lynxfb_setup(char *options)
1101 {
1102 	int len;
1103 	char *opt, *tmp;
1104 
1105 	if (!options || !*options) {
1106 		pr_warn("no options.\n");
1107 		return 0;
1108 	}
1109 
1110 	pr_info("options:%s\n", options);
1111 
1112 	len = strlen(options) + 1;
1113 	g_settings = kzalloc(len, GFP_KERNEL);
1114 	if (!g_settings)
1115 		return -ENOMEM;
1116 
1117 	tmp = g_settings;
1118 
1119 	/*
1120 	 * Notes:
1121 	 * char * strsep(char **s,const char * ct);
1122 	 * @s: the string to be searched
1123 	 * @ct :the characters to search for
1124 	 *
1125 	 * strsep() updates @options to pointer after the first found token
1126 	 * it also returns the pointer ahead the token.
1127 	 */
1128 	while ((opt = strsep(&options, ":")) != NULL) {
1129 		/* options that mean for any lynx chips are configured here */
1130 		if (!strncmp(opt, "noaccel", strlen("noaccel"))) {
1131 			g_noaccel = 1;
1132 		} else if (!strncmp(opt, "nomtrr", strlen("nomtrr"))) {
1133 			g_nomtrr = 1;
1134 		} else if (!strncmp(opt, "dual", strlen("dual"))) {
1135 			g_dualview = 1;
1136 		} else {
1137 			strcat(tmp, opt);
1138 			tmp += strlen(opt);
1139 			if (options)
1140 				*tmp++ = ':';
1141 			else
1142 				*tmp++ = 0;
1143 		}
1144 	}
1145 
1146 	/* misc g_settings are transport to chip specific routines */
1147 	pr_info("parameter left for chip specific analysis:%s\n", g_settings);
1148 	return 0;
1149 }
1150 
1151 static const struct pci_device_id smi_pci_table[] = {
1152 	{ PCI_DEVICE(0x126f, 0x0750), },
1153 	{0,}
1154 };
1155 
1156 MODULE_DEVICE_TABLE(pci, smi_pci_table);
1157 
1158 static SIMPLE_DEV_PM_OPS(lynxfb_pm_ops, lynxfb_suspend, lynxfb_resume);
1159 
1160 static struct pci_driver lynxfb_driver = {
1161 	.name =		"sm750fb",
1162 	.id_table =	smi_pci_table,
1163 	.probe =	lynxfb_pci_probe,
1164 	.remove =	lynxfb_pci_remove,
1165 	.driver.pm =	&lynxfb_pm_ops,
1166 };
1167 
1168 static int __init lynxfb_init(void)
1169 {
1170 	char *option;
1171 
1172 #ifdef MODULE
1173 	option = g_option;
1174 #else
1175 	if (fb_get_options("sm750fb", &option))
1176 		return -ENODEV;
1177 #endif
1178 
1179 	lynxfb_setup(option);
1180 	return pci_register_driver(&lynxfb_driver);
1181 }
1182 module_init(lynxfb_init);
1183 
1184 static void __exit lynxfb_exit(void)
1185 {
1186 	pci_unregister_driver(&lynxfb_driver);
1187 }
1188 module_exit(lynxfb_exit);
1189 
1190 module_param(g_option, charp, 0444);
1191 
1192 MODULE_PARM_DESC(g_option,
1193 		 "\n\t\tCommon options:\n"
1194 		 "\t\tnoaccel:disable 2d capabilities\n"
1195 		 "\t\tnomtrr:disable MTRR attribute for video memory\n"
1196 		 "\t\tdualview:dual frame buffer feature enabled\n"
1197 		 "\t\tnohwc:disable hardware cursor\n"
1198 		 "\t\tUsual example:\n"
1199 		 "\t\tinsmod ./sm750fb.ko g_option=\"noaccel,nohwc,1280x1024-8@60\"\n"
1200 		 );
1201 
1202 MODULE_AUTHOR("monk liu <monk.liu@siliconmotion.com>");
1203 MODULE_AUTHOR("Sudip Mukherjee <sudip@vectorindia.org>");
1204 MODULE_DESCRIPTION("Frame buffer driver for SM750 chipset");
1205 MODULE_LICENSE("Dual BSD/GPL");
1206