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