xref: /openbmc/linux/drivers/staging/sm750fb/sm750.c (revision 69f03be1)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/aperture.h>
3 #include <linux/kernel.h>
4 #include <linux/module.h>
5 #include <linux/errno.h>
6 #include <linux/string.h>
7 #include <linux/mm.h>
8 #include <linux/slab.h>
9 #include <linux/delay.h>
10 #include <linux/fb.h>
11 #include <linux/ioport.h>
12 #include <linux/init.h>
13 #include <linux/pci.h>
14 #include <linux/mm_types.h>
15 #include <linux/vmalloc.h>
16 #include <linux/pagemap.h>
17 #include <linux/console.h>
18 
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 = -1;
390 	var->width = -1;
391 	var->accel_flags = 0;/*FB_ACCELF_TEXT;*/
392 
393 	if (ret) {
394 		pr_err("bpp %d not supported\n", var->bits_per_pixel);
395 		return ret;
396 	}
397 	ret = hw_sm750_crtc_setMode(crtc, var, fix);
398 	if (!ret)
399 		ret = hw_sm750_output_setMode(output, var, fix);
400 	return ret;
401 }
402 
403 static inline unsigned int chan_to_field(unsigned int chan,
404 					 struct fb_bitfield *bf)
405 {
406 	chan &= 0xffff;
407 	chan >>= 16 - bf->length;
408 	return chan << bf->offset;
409 }
410 
411 static int __maybe_unused lynxfb_suspend(struct device *dev)
412 {
413 	struct fb_info *info;
414 	struct sm750_dev *sm750_dev;
415 
416 	sm750_dev = dev_get_drvdata(dev);
417 
418 	console_lock();
419 	info = sm750_dev->fbinfo[0];
420 	if (info)
421 		/* 1 means do suspend */
422 		fb_set_suspend(info, 1);
423 	info = sm750_dev->fbinfo[1];
424 	if (info)
425 		/* 1 means do suspend */
426 		fb_set_suspend(info, 1);
427 
428 	console_unlock();
429 	return 0;
430 }
431 
432 static int __maybe_unused lynxfb_resume(struct device *dev)
433 {
434 	struct pci_dev *pdev = to_pci_dev(dev);
435 	struct fb_info *info;
436 	struct sm750_dev *sm750_dev;
437 
438 	struct lynxfb_par *par;
439 	struct lynxfb_crtc *crtc;
440 	struct lynx_cursor *cursor;
441 
442 	sm750_dev = pci_get_drvdata(pdev);
443 
444 	console_lock();
445 
446 	hw_sm750_inithw(sm750_dev, pdev);
447 
448 	info = sm750_dev->fbinfo[0];
449 
450 	if (info) {
451 		par = info->par;
452 		crtc = &par->crtc;
453 		cursor = &crtc->cursor;
454 		memset_io(cursor->vstart, 0x0, cursor->size);
455 		memset_io(crtc->v_screen, 0x0, crtc->vidmem_size);
456 		lynxfb_ops_set_par(info);
457 		fb_set_suspend(info, 0);
458 	}
459 
460 	info = sm750_dev->fbinfo[1];
461 
462 	if (info) {
463 		par = info->par;
464 		crtc = &par->crtc;
465 		cursor = &crtc->cursor;
466 		memset_io(cursor->vstart, 0x0, cursor->size);
467 		memset_io(crtc->v_screen, 0x0, crtc->vidmem_size);
468 		lynxfb_ops_set_par(info);
469 		fb_set_suspend(info, 0);
470 	}
471 
472 	pdev->dev.power.power_state.event = PM_EVENT_RESUME;
473 
474 	console_unlock();
475 	return 0;
476 }
477 
478 static int lynxfb_ops_check_var(struct fb_var_screeninfo *var,
479 				struct fb_info *info)
480 {
481 	int ret;
482 	struct lynxfb_par *par;
483 	struct lynxfb_crtc *crtc;
484 	resource_size_t request;
485 
486 	ret = 0;
487 	par = info->par;
488 	crtc = &par->crtc;
489 
490 	pr_debug("check var:%dx%d-%d\n",
491 		 var->xres,
492 		 var->yres,
493 		 var->bits_per_pixel);
494 
495 	ret = lynxfb_set_color_offsets(info);
496 
497 	if (ret) {
498 		pr_err("bpp %d not supported\n", var->bits_per_pixel);
499 		return ret;
500 	}
501 
502 	var->height = -1;
503 	var->width = -1;
504 	var->accel_flags = 0;/* FB_ACCELF_TEXT; */
505 
506 	/* check if current fb's video memory big enough to hold the onscreen*/
507 	request = var->xres_virtual * (var->bits_per_pixel >> 3);
508 	/* defaulty crtc->channel go with par->index */
509 
510 	request = ALIGN(request, crtc->line_pad);
511 	request = request * var->yres_virtual;
512 	if (crtc->vidmem_size < request) {
513 		pr_err("not enough video memory for mode\n");
514 		return -ENOMEM;
515 	}
516 
517 	return hw_sm750_crtc_checkMode(crtc, var);
518 }
519 
520 static int lynxfb_ops_setcolreg(unsigned int regno,
521 				unsigned int red,
522 				unsigned int green,
523 				unsigned int blue,
524 				unsigned int transp,
525 				struct fb_info *info)
526 {
527 	struct lynxfb_par *par;
528 	struct lynxfb_crtc *crtc;
529 	struct fb_var_screeninfo *var;
530 	int ret;
531 
532 	par = info->par;
533 	crtc = &par->crtc;
534 	var = &info->var;
535 	ret = 0;
536 
537 	if (regno > 256) {
538 		pr_err("regno = %d\n", regno);
539 		return -EINVAL;
540 	}
541 
542 	if (info->var.grayscale)
543 		red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
544 
545 	if (var->bits_per_pixel == 8 &&
546 	    info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
547 		red >>= 8;
548 		green >>= 8;
549 		blue >>= 8;
550 		ret = hw_sm750_setColReg(crtc, regno, red, green, blue);
551 		goto exit;
552 	}
553 
554 	if (info->fix.visual == FB_VISUAL_TRUECOLOR && regno < 256) {
555 		u32 val;
556 
557 		if (var->bits_per_pixel == 16 ||
558 		    var->bits_per_pixel == 32 ||
559 		    var->bits_per_pixel == 24) {
560 			val = chan_to_field(red, &var->red);
561 			val |= chan_to_field(green, &var->green);
562 			val |= chan_to_field(blue, &var->blue);
563 			par->pseudo_palette[regno] = val;
564 			goto exit;
565 		}
566 	}
567 
568 	ret = -EINVAL;
569 
570 exit:
571 	return ret;
572 }
573 
574 static int lynxfb_ops_blank(int blank, struct fb_info *info)
575 {
576 	struct lynxfb_par *par;
577 	struct lynxfb_output *output;
578 
579 	pr_debug("blank = %d.\n", blank);
580 	par = info->par;
581 	output = &par->output;
582 	return output->proc_setBLANK(output, blank);
583 }
584 
585 static int sm750fb_set_drv(struct lynxfb_par *par)
586 {
587 	int ret;
588 	struct sm750_dev *sm750_dev;
589 	struct lynxfb_output *output;
590 	struct lynxfb_crtc *crtc;
591 
592 	ret = 0;
593 
594 	sm750_dev = par->dev;
595 	output = &par->output;
596 	crtc = &par->crtc;
597 
598 	crtc->vidmem_size = sm750_dev->vidmem_size;
599 	if (sm750_dev->fb_count > 1)
600 		crtc->vidmem_size >>= 1;
601 
602 	/* setup crtc and output member */
603 	sm750_dev->hwCursor = g_hwcursor;
604 
605 	crtc->line_pad = 16;
606 	crtc->xpanstep = 8;
607 	crtc->ypanstep = 1;
608 	crtc->ywrapstep = 0;
609 
610 	output->proc_setBLANK = (sm750_dev->revid == SM750LE_REVISION_ID) ?
611 				 hw_sm750le_setBLANK : hw_sm750_setBLANK;
612 	/* chip specific phase */
613 	sm750_dev->accel.de_wait = (sm750_dev->revid == SM750LE_REVISION_ID) ?
614 				    hw_sm750le_deWait : hw_sm750_deWait;
615 	switch (sm750_dev->dataflow) {
616 	case sm750_simul_pri:
617 		output->paths = sm750_pnc;
618 		crtc->channel = sm750_primary;
619 		crtc->o_screen = 0;
620 		crtc->v_screen = sm750_dev->pvMem;
621 		pr_info("use simul primary mode\n");
622 		break;
623 	case sm750_simul_sec:
624 		output->paths = sm750_pnc;
625 		crtc->channel = sm750_secondary;
626 		crtc->o_screen = 0;
627 		crtc->v_screen = sm750_dev->pvMem;
628 		break;
629 	case sm750_dual_normal:
630 		if (par->index == 0) {
631 			output->paths = sm750_panel;
632 			crtc->channel = sm750_primary;
633 			crtc->o_screen = 0;
634 			crtc->v_screen = sm750_dev->pvMem;
635 		} else {
636 			output->paths = sm750_crt;
637 			crtc->channel = sm750_secondary;
638 			/* not consider of padding stuffs for o_screen,need fix */
639 			crtc->o_screen = sm750_dev->vidmem_size >> 1;
640 			crtc->v_screen = sm750_dev->pvMem + crtc->o_screen;
641 		}
642 		break;
643 	case sm750_dual_swap:
644 		if (par->index == 0) {
645 			output->paths = sm750_panel;
646 			crtc->channel = sm750_secondary;
647 			crtc->o_screen = 0;
648 			crtc->v_screen = sm750_dev->pvMem;
649 		} else {
650 			output->paths = sm750_crt;
651 			crtc->channel = sm750_primary;
652 			/* not consider of padding stuffs for o_screen,
653 			 * need fix
654 			 */
655 			crtc->o_screen = sm750_dev->vidmem_size >> 1;
656 			crtc->v_screen = sm750_dev->pvMem + crtc->o_screen;
657 		}
658 		break;
659 	default:
660 		ret = -EINVAL;
661 	}
662 
663 	return ret;
664 }
665 
666 static struct fb_ops lynxfb_ops = {
667 	.owner = THIS_MODULE,
668 	.fb_check_var =  lynxfb_ops_check_var,
669 	.fb_set_par = lynxfb_ops_set_par,
670 	.fb_setcolreg = lynxfb_ops_setcolreg,
671 	.fb_blank = lynxfb_ops_blank,
672 	.fb_fillrect = cfb_fillrect,
673 	.fb_imageblit = cfb_imageblit,
674 	.fb_copyarea = cfb_copyarea,
675 	/* cursor */
676 	.fb_cursor = lynxfb_ops_cursor,
677 };
678 
679 static int lynxfb_set_fbinfo(struct fb_info *info, int index)
680 {
681 	int i;
682 	struct lynxfb_par *par;
683 	struct sm750_dev *sm750_dev;
684 	struct lynxfb_crtc *crtc;
685 	struct lynxfb_output *output;
686 	struct fb_var_screeninfo *var;
687 	struct fb_fix_screeninfo *fix;
688 
689 	const struct fb_videomode *pdb[] = {
690 		lynx750_ext, NULL, vesa_modes,
691 	};
692 	int cdb[] = {ARRAY_SIZE(lynx750_ext), 0, VESA_MODEDB_SIZE};
693 	static const char * const mdb_desc[] = {
694 		"driver prepared modes",
695 		"kernel prepared default modedb",
696 		"kernel HELPERS prepared vesa_modes",
697 	};
698 
699 	static const char *fixId[2] = {
700 		"sm750_fb1", "sm750_fb2",
701 	};
702 
703 	int ret, line_length;
704 
705 	ret = 0;
706 	par = (struct lynxfb_par *)info->par;
707 	sm750_dev = par->dev;
708 	crtc = &par->crtc;
709 	output = &par->output;
710 	var = &info->var;
711 	fix = &info->fix;
712 
713 	/* set index */
714 	par->index = index;
715 	output->channel = &crtc->channel;
716 	sm750fb_set_drv(par);
717 	lynxfb_ops.fb_pan_display = lynxfb_ops_pan_display;
718 
719 	/*
720 	 * set current cursor variable and proc pointer,
721 	 * must be set after crtc member initialized
722 	 */
723 	crtc->cursor.offset = crtc->o_screen + crtc->vidmem_size - 1024;
724 	crtc->cursor.mmio = sm750_dev->pvReg +
725 		0x800f0 + (int)crtc->channel * 0x140;
726 
727 	pr_info("crtc->cursor.mmio = %p\n", crtc->cursor.mmio);
728 	crtc->cursor.max_h = 64;
729 	crtc->cursor.max_w = 64;
730 	crtc->cursor.size = crtc->cursor.max_h * crtc->cursor.max_w * 2 / 8;
731 	crtc->cursor.vstart = sm750_dev->pvMem + crtc->cursor.offset;
732 
733 	memset_io(crtc->cursor.vstart, 0, crtc->cursor.size);
734 	if (!g_hwcursor) {
735 		lynxfb_ops.fb_cursor = NULL;
736 		sm750_hw_cursor_disable(&crtc->cursor);
737 	}
738 
739 	/* set info->fbops, must be set before fb_find_mode */
740 	if (!sm750_dev->accel_off) {
741 		/* use 2d acceleration */
742 		lynxfb_ops.fb_fillrect = lynxfb_ops_fillrect;
743 		lynxfb_ops.fb_copyarea = lynxfb_ops_copyarea;
744 		lynxfb_ops.fb_imageblit = lynxfb_ops_imageblit;
745 	}
746 	info->fbops = &lynxfb_ops;
747 
748 	if (!g_fbmode[index]) {
749 		g_fbmode[index] = g_def_fbmode;
750 		if (index)
751 			g_fbmode[index] = g_fbmode[0];
752 	}
753 
754 	for (i = 0; i < 3; i++) {
755 		ret = fb_find_mode(var, info, g_fbmode[index],
756 				   pdb[i], cdb[i], NULL, 8);
757 
758 		if (ret == 1) {
759 			pr_info("success! use specified mode:%s in %s\n",
760 				g_fbmode[index],
761 				mdb_desc[i]);
762 			break;
763 		} else if (ret == 2) {
764 			pr_warn("use specified mode:%s in %s,with an ignored refresh rate\n",
765 				g_fbmode[index],
766 				mdb_desc[i]);
767 			break;
768 		} else if (ret == 3) {
769 			pr_warn("wanna use default mode\n");
770 			/*break;*/
771 		} else if (ret == 4) {
772 			pr_warn("fall back to any valid mode\n");
773 		} else {
774 			pr_warn("ret = %d,fb_find_mode failed,with %s\n",
775 				ret,
776 				mdb_desc[i]);
777 		}
778 	}
779 
780 	/* some member of info->var had been set by fb_find_mode */
781 
782 	pr_info("Member of info->var is :\n"
783 		"xres=%d\n"
784 		"yres=%d\n"
785 		"xres_virtual=%d\n"
786 		"yres_virtual=%d\n"
787 		"xoffset=%d\n"
788 		"yoffset=%d\n"
789 		"bits_per_pixel=%d\n"
790 		" ...\n",
791 		var->xres,
792 		var->yres,
793 		var->xres_virtual,
794 		var->yres_virtual,
795 		var->xoffset,
796 		var->yoffset,
797 		var->bits_per_pixel);
798 
799 	/* set par */
800 	par->info = info;
801 
802 	/* set info */
803 	line_length = ALIGN((var->xres_virtual * var->bits_per_pixel / 8),
804 			    crtc->line_pad);
805 
806 	info->pseudo_palette = &par->pseudo_palette[0];
807 	info->screen_base = crtc->v_screen;
808 	pr_debug("screen_base vaddr = %p\n", info->screen_base);
809 	info->screen_size = line_length * var->yres_virtual;
810 
811 	/* set info->fix */
812 	fix->type = FB_TYPE_PACKED_PIXELS;
813 	fix->type_aux = 0;
814 	fix->xpanstep = crtc->xpanstep;
815 	fix->ypanstep = crtc->ypanstep;
816 	fix->ywrapstep = crtc->ywrapstep;
817 	fix->accel = FB_ACCEL_SMI;
818 
819 	strscpy(fix->id, fixId[index], sizeof(fix->id));
820 
821 	fix->smem_start = crtc->o_screen + sm750_dev->vidmem_start;
822 	pr_info("fix->smem_start = %lx\n", fix->smem_start);
823 	/*
824 	 * according to mmap experiment from user space application,
825 	 * fix->mmio_len should not larger than virtual size
826 	 * (xres_virtual x yres_virtual x ByPP)
827 	 * Below line maybe buggy when user mmap fb dev node and write
828 	 * data into the bound over virtual size
829 	 */
830 	fix->smem_len = crtc->vidmem_size;
831 	pr_info("fix->smem_len = %x\n", fix->smem_len);
832 	info->screen_size = fix->smem_len;
833 	fix->line_length = line_length;
834 	fix->mmio_start = sm750_dev->vidreg_start;
835 	pr_info("fix->mmio_start = %lx\n", fix->mmio_start);
836 	fix->mmio_len = sm750_dev->vidreg_size;
837 	pr_info("fix->mmio_len = %x\n", fix->mmio_len);
838 
839 	lynxfb_set_visual_mode(info);
840 
841 	/* set var */
842 	var->activate = FB_ACTIVATE_NOW;
843 	var->accel_flags = 0;
844 	var->vmode = FB_VMODE_NONINTERLACED;
845 
846 	pr_debug("#1 show info->cmap :\nstart=%d,len=%d,red=%p,green=%p,blue=%p,transp=%p\n",
847 		 info->cmap.start, info->cmap.len,
848 		 info->cmap.red, info->cmap.green, info->cmap.blue,
849 		 info->cmap.transp);
850 
851 	ret = fb_alloc_cmap(&info->cmap, 256, 0);
852 	if (ret < 0) {
853 		pr_err("Could not allocate memory for cmap.\n");
854 		goto exit;
855 	}
856 
857 	pr_debug("#2 show info->cmap :\nstart=%d,len=%d,red=%p,green=%p,blue=%p,transp=%p\n",
858 		 info->cmap.start, info->cmap.len,
859 		 info->cmap.red, info->cmap.green, info->cmap.blue,
860 		 info->cmap.transp);
861 
862 exit:
863 	lynxfb_ops_check_var(var, info);
864 	return ret;
865 }
866 
867 /*	chip specific g_option configuration routine */
868 static void sm750fb_setup(struct sm750_dev *sm750_dev, char *src)
869 {
870 	char *opt;
871 	int swap;
872 
873 	swap = 0;
874 
875 	sm750_dev->initParm.chip_clk = 0;
876 	sm750_dev->initParm.mem_clk = 0;
877 	sm750_dev->initParm.master_clk = 0;
878 	sm750_dev->initParm.powerMode = 0;
879 	sm750_dev->initParm.setAllEngOff = 0;
880 	sm750_dev->initParm.resetMemory = 1;
881 
882 	/* defaultly turn g_hwcursor on for both view */
883 	g_hwcursor = 3;
884 
885 	if (!src || !*src) {
886 		dev_warn(&sm750_dev->pdev->dev, "no specific g_option.\n");
887 		goto NO_PARAM;
888 	}
889 
890 	while ((opt = strsep(&src, ":")) != NULL && *opt != 0) {
891 		dev_info(&sm750_dev->pdev->dev, "opt=%s\n", opt);
892 		dev_info(&sm750_dev->pdev->dev, "src=%s\n", src);
893 
894 		if (!strncmp(opt, "swap", strlen("swap"))) {
895 			swap = 1;
896 		} else if (!strncmp(opt, "nocrt", strlen("nocrt"))) {
897 			sm750_dev->nocrt = 1;
898 		} else if (!strncmp(opt, "36bit", strlen("36bit"))) {
899 			sm750_dev->pnltype = sm750_doubleTFT;
900 		} else if (!strncmp(opt, "18bit", strlen("18bit"))) {
901 			sm750_dev->pnltype = sm750_dualTFT;
902 		} else if (!strncmp(opt, "24bit", strlen("24bit"))) {
903 			sm750_dev->pnltype = sm750_24TFT;
904 		} else if (!strncmp(opt, "nohwc0", strlen("nohwc0"))) {
905 			g_hwcursor &= ~0x1;
906 		} else if (!strncmp(opt, "nohwc1", strlen("nohwc1"))) {
907 			g_hwcursor &= ~0x2;
908 		} else if (!strncmp(opt, "nohwc", strlen("nohwc"))) {
909 			g_hwcursor = 0;
910 		} else {
911 			if (!g_fbmode[0]) {
912 				g_fbmode[0] = opt;
913 				dev_info(&sm750_dev->pdev->dev,
914 					 "find fbmode0 : %s\n", g_fbmode[0]);
915 			} else if (!g_fbmode[1]) {
916 				g_fbmode[1] = opt;
917 				dev_info(&sm750_dev->pdev->dev,
918 					 "find fbmode1 : %s\n", g_fbmode[1]);
919 			} else {
920 				dev_warn(&sm750_dev->pdev->dev, "How many view you wann set?\n");
921 			}
922 		}
923 	}
924 
925 NO_PARAM:
926 	if (sm750_dev->revid != SM750LE_REVISION_ID) {
927 		if (sm750_dev->fb_count > 1) {
928 			if (swap)
929 				sm750_dev->dataflow = sm750_dual_swap;
930 			else
931 				sm750_dev->dataflow = sm750_dual_normal;
932 		} else {
933 			if (swap)
934 				sm750_dev->dataflow = sm750_simul_sec;
935 			else
936 				sm750_dev->dataflow = sm750_simul_pri;
937 		}
938 	} else {
939 		/* SM750LE only have one crt channel */
940 		sm750_dev->dataflow = sm750_simul_sec;
941 		/* sm750le do not have complex attributes */
942 		sm750_dev->nocrt = 0;
943 	}
944 }
945 
946 static void sm750fb_framebuffer_release(struct sm750_dev *sm750_dev)
947 {
948 	struct fb_info *fb_info;
949 
950 	while (sm750_dev->fb_count) {
951 		fb_info = sm750_dev->fbinfo[sm750_dev->fb_count - 1];
952 		unregister_framebuffer(fb_info);
953 		framebuffer_release(fb_info);
954 		sm750_dev->fb_count--;
955 	}
956 }
957 
958 static int sm750fb_framebuffer_alloc(struct sm750_dev *sm750_dev, int fbidx)
959 {
960 	struct fb_info *fb_info;
961 	struct lynxfb_par *par;
962 	int err;
963 
964 	fb_info = framebuffer_alloc(sizeof(struct lynxfb_par),
965 				    &sm750_dev->pdev->dev);
966 	if (!fb_info)
967 		return -ENOMEM;
968 
969 	sm750_dev->fbinfo[fbidx] = fb_info;
970 	par = fb_info->par;
971 	par->dev = sm750_dev;
972 
973 	err = lynxfb_set_fbinfo(fb_info, fbidx);
974 	if (err)
975 		goto release_fb;
976 
977 	err = register_framebuffer(fb_info);
978 	if (err < 0)
979 		goto release_fb;
980 
981 	sm750_dev->fb_count++;
982 
983 	return 0;
984 
985 release_fb:
986 	framebuffer_release(fb_info);
987 	return err;
988 }
989 
990 static int lynxfb_pci_probe(struct pci_dev *pdev,
991 			    const struct pci_device_id *ent)
992 {
993 	struct sm750_dev *sm750_dev = NULL;
994 	int max_fb;
995 	int fbidx;
996 	int err;
997 
998 	err = aperture_remove_conflicting_pci_devices(pdev, "sm750_fb1");
999 	if (err)
1000 		return err;
1001 
1002 	/* enable device */
1003 	err = pcim_enable_device(pdev);
1004 	if (err)
1005 		return err;
1006 
1007 	err = -ENOMEM;
1008 	sm750_dev = devm_kzalloc(&pdev->dev, sizeof(*sm750_dev), GFP_KERNEL);
1009 	if (!sm750_dev)
1010 		return err;
1011 
1012 	sm750_dev->fbinfo[0] = NULL;
1013 	sm750_dev->fbinfo[1] = NULL;
1014 	sm750_dev->devid = pdev->device;
1015 	sm750_dev->revid = pdev->revision;
1016 	sm750_dev->pdev = pdev;
1017 	sm750_dev->mtrr_off = g_nomtrr;
1018 	sm750_dev->mtrr.vram = 0;
1019 	sm750_dev->accel_off = g_noaccel;
1020 	spin_lock_init(&sm750_dev->slock);
1021 
1022 	if (!sm750_dev->accel_off) {
1023 		/*
1024 		 * hook deInit and 2d routines, notes that below hw_xxx
1025 		 * routine can work on most of lynx chips
1026 		 * if some chip need specific function,
1027 		 * please hook it in smXXX_set_drv routine
1028 		 */
1029 		sm750_dev->accel.de_init = sm750_hw_de_init;
1030 		sm750_dev->accel.de_fillrect = sm750_hw_fillrect;
1031 		sm750_dev->accel.de_copyarea = sm750_hw_copyarea;
1032 		sm750_dev->accel.de_imageblit = sm750_hw_imageblit;
1033 	}
1034 
1035 	/* call chip specific setup routine  */
1036 	sm750fb_setup(sm750_dev, g_settings);
1037 
1038 	/* call chip specific mmap routine */
1039 	err = hw_sm750_map(sm750_dev, pdev);
1040 	if (err)
1041 		return err;
1042 
1043 	if (!sm750_dev->mtrr_off)
1044 		sm750_dev->mtrr.vram = arch_phys_wc_add(sm750_dev->vidmem_start,
1045 							sm750_dev->vidmem_size);
1046 
1047 	memset_io(sm750_dev->pvMem, 0, sm750_dev->vidmem_size);
1048 
1049 	pci_set_drvdata(pdev, sm750_dev);
1050 
1051 	/* call chipInit routine */
1052 	hw_sm750_inithw(sm750_dev, pdev);
1053 
1054 	/* allocate frame buffer info structures according to g_dualview */
1055 	max_fb = g_dualview ? 2 : 1;
1056 	for (fbidx = 0; fbidx < max_fb; fbidx++) {
1057 		err = sm750fb_framebuffer_alloc(sm750_dev, fbidx);
1058 		if (err)
1059 			goto release_fb;
1060 	}
1061 
1062 	return 0;
1063 
1064 release_fb:
1065 	sm750fb_framebuffer_release(sm750_dev);
1066 	return err;
1067 }
1068 
1069 static void lynxfb_pci_remove(struct pci_dev *pdev)
1070 {
1071 	struct sm750_dev *sm750_dev;
1072 
1073 	sm750_dev = pci_get_drvdata(pdev);
1074 
1075 	sm750fb_framebuffer_release(sm750_dev);
1076 	arch_phys_wc_del(sm750_dev->mtrr.vram);
1077 
1078 	iounmap(sm750_dev->pvReg);
1079 	iounmap(sm750_dev->pvMem);
1080 	kfree(g_settings);
1081 }
1082 
1083 static int __init lynxfb_setup(char *options)
1084 {
1085 	int len;
1086 	char *opt, *tmp;
1087 
1088 	if (!options || !*options) {
1089 		pr_warn("no options.\n");
1090 		return 0;
1091 	}
1092 
1093 	pr_info("options:%s\n", options);
1094 
1095 	len = strlen(options) + 1;
1096 	g_settings = kzalloc(len, GFP_KERNEL);
1097 	if (!g_settings)
1098 		return -ENOMEM;
1099 
1100 	tmp = g_settings;
1101 
1102 	/*
1103 	 * Notes:
1104 	 * char * strsep(char **s,const char * ct);
1105 	 * @s: the string to be searched
1106 	 * @ct :the characters to search for
1107 	 *
1108 	 * strsep() updates @options to pointer after the first found token
1109 	 * it also returns the pointer ahead the token.
1110 	 */
1111 	while ((opt = strsep(&options, ":")) != NULL) {
1112 		/* options that mean for any lynx chips are configured here */
1113 		if (!strncmp(opt, "noaccel", strlen("noaccel"))) {
1114 			g_noaccel = 1;
1115 		} else if (!strncmp(opt, "nomtrr", strlen("nomtrr"))) {
1116 			g_nomtrr = 1;
1117 		} else if (!strncmp(opt, "dual", strlen("dual"))) {
1118 			g_dualview = 1;
1119 		} else {
1120 			strcat(tmp, opt);
1121 			tmp += strlen(opt);
1122 			if (options)
1123 				*tmp++ = ':';
1124 			else
1125 				*tmp++ = 0;
1126 		}
1127 	}
1128 
1129 	/* misc g_settings are transport to chip specific routines */
1130 	pr_info("parameter left for chip specific analysis:%s\n", g_settings);
1131 	return 0;
1132 }
1133 
1134 static const struct pci_device_id smi_pci_table[] = {
1135 	{ PCI_DEVICE(0x126f, 0x0750), },
1136 	{0,}
1137 };
1138 
1139 MODULE_DEVICE_TABLE(pci, smi_pci_table);
1140 
1141 static SIMPLE_DEV_PM_OPS(lynxfb_pm_ops, lynxfb_suspend, lynxfb_resume);
1142 
1143 static struct pci_driver lynxfb_driver = {
1144 	.name =		"sm750fb",
1145 	.id_table =	smi_pci_table,
1146 	.probe =	lynxfb_pci_probe,
1147 	.remove =	lynxfb_pci_remove,
1148 	.driver.pm =	&lynxfb_pm_ops,
1149 };
1150 
1151 static int __init lynxfb_init(void)
1152 {
1153 	char *option;
1154 
1155 	if (fb_modesetting_disabled("sm750fb"))
1156 		return -ENODEV;
1157 
1158 #ifdef MODULE
1159 	option = g_option;
1160 #else
1161 	if (fb_get_options("sm750fb", &option))
1162 		return -ENODEV;
1163 #endif
1164 
1165 	lynxfb_setup(option);
1166 	return pci_register_driver(&lynxfb_driver);
1167 }
1168 module_init(lynxfb_init);
1169 
1170 static void __exit lynxfb_exit(void)
1171 {
1172 	pci_unregister_driver(&lynxfb_driver);
1173 }
1174 module_exit(lynxfb_exit);
1175 
1176 module_param(g_option, charp, 0444);
1177 
1178 MODULE_PARM_DESC(g_option,
1179 		 "\n\t\tCommon options:\n"
1180 		 "\t\tnoaccel:disable 2d capabilities\n"
1181 		 "\t\tnomtrr:disable MTRR attribute for video memory\n"
1182 		 "\t\tdualview:dual frame buffer feature enabled\n"
1183 		 "\t\tnohwc:disable hardware cursor\n"
1184 		 "\t\tUsual example:\n"
1185 		 "\t\tinsmod ./sm750fb.ko g_option=\"noaccel,nohwc,1280x1024-8@60\"\n"
1186 		 );
1187 
1188 MODULE_AUTHOR("monk liu <monk.liu@siliconmotion.com>");
1189 MODULE_AUTHOR("Sudip Mukherjee <sudip@vectorindia.org>");
1190 MODULE_DESCRIPTION("Frame buffer driver for SM750 chipset");
1191 MODULE_LICENSE("Dual BSD/GPL");
1192