1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Framebuffer driver for TI OMAP boards
4  *
5  * Copyright (C) 2004 Nokia Corporation
6  * Author: Imre Deak <imre.deak@nokia.com>
7  *
8  * Acknowledgements:
9  *   Alex McMains <aam@ridgerun.com>       - Original driver
10  *   Juha Yrjola <juha.yrjola@nokia.com>   - Original driver and improvements
11  *   Dirk Behme <dirk.behme@de.bosch.com>  - changes for 2.6 kernel API
12  *   Texas Instruments                     - H3 support
13  */
14 #include <linux/platform_device.h>
15 #include <linux/mm.h>
16 #include <linux/slab.h>
17 #include <linux/uaccess.h>
18 #include <linux/module.h>
19 #include <linux/sysfs.h>
20 
21 #include <linux/omap-dma.h>
22 
23 #include <mach/hardware.h>
24 
25 #include "omapfb.h"
26 #include "lcdc.h"
27 
28 #define MODULE_NAME	"omapfb"
29 
30 static unsigned int	def_accel;
31 static unsigned long	def_vram[OMAPFB_PLANE_NUM];
32 static unsigned int	def_vram_cnt;
33 static unsigned long	def_vxres;
34 static unsigned long	def_vyres;
35 static unsigned int	def_rotate;
36 static unsigned int	def_mirror;
37 
38 static bool	manual_update = IS_BUILTIN(CONFIG_FB_OMAP_MANUAL_UPDATE);
39 
40 static struct platform_device	*fbdev_pdev;
41 static struct lcd_panel		*fbdev_panel;
42 static struct omapfb_device	*omapfb_dev;
43 
44 struct caps_table_struct {
45 	unsigned long flag;
46 	const char *name;
47 };
48 
49 static const struct caps_table_struct ctrl_caps[] = {
50 	{ OMAPFB_CAPS_MANUAL_UPDATE,  "manual update" },
51 	{ OMAPFB_CAPS_TEARSYNC,       "tearing synchronization" },
52 	{ OMAPFB_CAPS_PLANE_RELOCATE_MEM, "relocate plane memory" },
53 	{ OMAPFB_CAPS_PLANE_SCALE,    "scale plane" },
54 	{ OMAPFB_CAPS_WINDOW_PIXEL_DOUBLE, "pixel double window" },
55 	{ OMAPFB_CAPS_WINDOW_SCALE,   "scale window" },
56 	{ OMAPFB_CAPS_WINDOW_OVERLAY, "overlay window" },
57 	{ OMAPFB_CAPS_WINDOW_ROTATE,  "rotate window" },
58 	{ OMAPFB_CAPS_SET_BACKLIGHT,  "backlight setting" },
59 };
60 
61 static const struct caps_table_struct color_caps[] = {
62 	{ 1 << OMAPFB_COLOR_RGB565,	"RGB565", },
63 	{ 1 << OMAPFB_COLOR_YUV422,	"YUV422", },
64 	{ 1 << OMAPFB_COLOR_YUV420,	"YUV420", },
65 	{ 1 << OMAPFB_COLOR_CLUT_8BPP,	"CLUT8", },
66 	{ 1 << OMAPFB_COLOR_CLUT_4BPP,	"CLUT4", },
67 	{ 1 << OMAPFB_COLOR_CLUT_2BPP,	"CLUT2", },
68 	{ 1 << OMAPFB_COLOR_CLUT_1BPP,	"CLUT1", },
69 	{ 1 << OMAPFB_COLOR_RGB444,	"RGB444", },
70 	{ 1 << OMAPFB_COLOR_YUY422,	"YUY422", },
71 };
72 
73 static void omapdss_release(struct device *dev)
74 {
75 }
76 
77 /* dummy device for clocks */
78 static struct platform_device omapdss_device = {
79 	.name		= "omapdss_dss",
80 	.id		= -1,
81 	.dev            = {
82 		.release = omapdss_release,
83 	},
84 };
85 
86 /*
87  * ---------------------------------------------------------------------------
88  * LCD panel
89  * ---------------------------------------------------------------------------
90  */
91 extern struct lcd_ctrl hwa742_ctrl;
92 
93 static const struct lcd_ctrl *ctrls[] = {
94 	&omap1_int_ctrl,
95 
96 #ifdef CONFIG_FB_OMAP_LCDC_HWA742
97 	&hwa742_ctrl,
98 #endif
99 };
100 
101 #ifdef CONFIG_FB_OMAP_LCDC_EXTERNAL
102 extern struct lcd_ctrl_extif omap1_ext_if;
103 #endif
104 
105 static void omapfb_rqueue_lock(struct omapfb_device *fbdev)
106 {
107 	mutex_lock(&fbdev->rqueue_mutex);
108 }
109 
110 static void omapfb_rqueue_unlock(struct omapfb_device *fbdev)
111 {
112 	mutex_unlock(&fbdev->rqueue_mutex);
113 }
114 
115 /*
116  * ---------------------------------------------------------------------------
117  * LCD controller and LCD DMA
118  * ---------------------------------------------------------------------------
119  */
120 /*
121  * Allocate resources needed for LCD controller and LCD DMA operations. Video
122  * memory is allocated from system memory according to the virtual display
123  * size, except if a bigger memory size is specified explicitly as a kernel
124  * parameter.
125  */
126 static int ctrl_init(struct omapfb_device *fbdev)
127 {
128 	int r;
129 	int i;
130 
131 	/* kernel/module vram parameters override boot tags/board config */
132 	if (def_vram_cnt) {
133 		for (i = 0; i < def_vram_cnt; i++)
134 			fbdev->mem_desc.region[i].size =
135 				PAGE_ALIGN(def_vram[i]);
136 		fbdev->mem_desc.region_cnt = i;
137 	}
138 
139 	if (!fbdev->mem_desc.region_cnt) {
140 		struct lcd_panel *panel = fbdev->panel;
141 		int def_size;
142 		int bpp = panel->bpp;
143 
144 		/* 12 bpp is packed in 16 bits */
145 		if (bpp == 12)
146 			bpp = 16;
147 		def_size = def_vxres * def_vyres * bpp / 8;
148 		fbdev->mem_desc.region_cnt = 1;
149 		fbdev->mem_desc.region[0].size = PAGE_ALIGN(def_size);
150 	}
151 	r = fbdev->ctrl->init(fbdev, 0, &fbdev->mem_desc);
152 	if (r < 0) {
153 		dev_err(fbdev->dev, "controller initialization failed (%d)\n",
154 			r);
155 		return r;
156 	}
157 
158 #ifdef DEBUG
159 	for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
160 		dev_dbg(fbdev->dev, "region%d phys %08x virt %p size=%lu\n",
161 			 i,
162 			 fbdev->mem_desc.region[i].paddr,
163 			 fbdev->mem_desc.region[i].vaddr,
164 			 fbdev->mem_desc.region[i].size);
165 	}
166 #endif
167 	return 0;
168 }
169 
170 static void ctrl_cleanup(struct omapfb_device *fbdev)
171 {
172 	fbdev->ctrl->cleanup();
173 }
174 
175 /* Must be called with fbdev->rqueue_mutex held. */
176 static int ctrl_change_mode(struct fb_info *fbi)
177 {
178 	int r;
179 	unsigned long offset;
180 	struct omapfb_plane_struct *plane = fbi->par;
181 	struct omapfb_device *fbdev = plane->fbdev;
182 	struct fb_var_screeninfo *var = &fbi->var;
183 
184 	offset = var->yoffset * fbi->fix.line_length +
185 		 var->xoffset * var->bits_per_pixel / 8;
186 
187 	if (fbdev->ctrl->sync)
188 		fbdev->ctrl->sync();
189 	r = fbdev->ctrl->setup_plane(plane->idx, plane->info.channel_out,
190 				 offset, var->xres_virtual,
191 				 plane->info.pos_x, plane->info.pos_y,
192 				 var->xres, var->yres, plane->color_mode);
193 	if (r < 0)
194 		return r;
195 
196 	if (fbdev->ctrl->set_rotate != NULL) {
197 		r = fbdev->ctrl->set_rotate(var->rotate);
198 		if (r < 0)
199 			return r;
200 	}
201 
202 	if (fbdev->ctrl->set_scale != NULL)
203 		r = fbdev->ctrl->set_scale(plane->idx,
204 				   var->xres, var->yres,
205 				   plane->info.out_width,
206 				   plane->info.out_height);
207 
208 	return r;
209 }
210 
211 /*
212  * ---------------------------------------------------------------------------
213  * fbdev framework callbacks and the ioctl interface
214  * ---------------------------------------------------------------------------
215  */
216 /* Called each time the omapfb device is opened */
217 static int omapfb_open(struct fb_info *info, int user)
218 {
219 	return 0;
220 }
221 
222 static void omapfb_sync(struct fb_info *info);
223 
224 /* Called when the omapfb device is closed. We make sure that any pending
225  * gfx DMA operations are ended, before we return. */
226 static int omapfb_release(struct fb_info *info, int user)
227 {
228 	omapfb_sync(info);
229 	return 0;
230 }
231 
232 /* Store a single color palette entry into a pseudo palette or the hardware
233  * palette if one is available. For now we support only 16bpp and thus store
234  * the entry only to the pseudo palette.
235  */
236 static int _setcolreg(struct fb_info *info, u_int regno, u_int red, u_int green,
237 			u_int blue, u_int transp, int update_hw_pal)
238 {
239 	struct omapfb_plane_struct *plane = info->par;
240 	struct omapfb_device *fbdev = plane->fbdev;
241 	struct fb_var_screeninfo *var = &info->var;
242 	int r = 0;
243 
244 	switch (plane->color_mode) {
245 	case OMAPFB_COLOR_YUV422:
246 	case OMAPFB_COLOR_YUV420:
247 	case OMAPFB_COLOR_YUY422:
248 		r = -EINVAL;
249 		break;
250 	case OMAPFB_COLOR_CLUT_8BPP:
251 	case OMAPFB_COLOR_CLUT_4BPP:
252 	case OMAPFB_COLOR_CLUT_2BPP:
253 	case OMAPFB_COLOR_CLUT_1BPP:
254 		if (fbdev->ctrl->setcolreg)
255 			r = fbdev->ctrl->setcolreg(regno, red, green, blue,
256 							transp, update_hw_pal);
257 		fallthrough;
258 	case OMAPFB_COLOR_RGB565:
259 	case OMAPFB_COLOR_RGB444:
260 		if (r != 0)
261 			break;
262 
263 		if (regno < 16) {
264 			u16 pal;
265 			pal = ((red >> (16 - var->red.length)) <<
266 					var->red.offset) |
267 			      ((green >> (16 - var->green.length)) <<
268 					var->green.offset) |
269 			      (blue >> (16 - var->blue.length));
270 			((u32 *)(info->pseudo_palette))[regno] = pal;
271 		}
272 		break;
273 	default:
274 		BUG();
275 	}
276 	return r;
277 }
278 
279 static int omapfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
280 			    u_int transp, struct fb_info *info)
281 {
282 	return _setcolreg(info, regno, red, green, blue, transp, 1);
283 }
284 
285 static int omapfb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
286 {
287 	int count, index, r;
288 	u16 *red, *green, *blue, *transp;
289 	u16 trans = 0xffff;
290 
291 	red     = cmap->red;
292 	green   = cmap->green;
293 	blue    = cmap->blue;
294 	transp  = cmap->transp;
295 	index   = cmap->start;
296 
297 	for (count = 0; count < cmap->len; count++) {
298 		if (transp)
299 			trans = *transp++;
300 		r = _setcolreg(info, index++, *red++, *green++, *blue++, trans,
301 				count == cmap->len - 1);
302 		if (r != 0)
303 			return r;
304 	}
305 
306 	return 0;
307 }
308 
309 static int omapfb_update_full_screen(struct fb_info *fbi);
310 
311 static int omapfb_blank(int blank, struct fb_info *fbi)
312 {
313 	struct omapfb_plane_struct *plane = fbi->par;
314 	struct omapfb_device *fbdev = plane->fbdev;
315 	int do_update = 0;
316 	int r = 0;
317 
318 	omapfb_rqueue_lock(fbdev);
319 	switch (blank) {
320 	case FB_BLANK_UNBLANK:
321 		if (fbdev->state == OMAPFB_SUSPENDED) {
322 			if (fbdev->ctrl->resume)
323 				fbdev->ctrl->resume();
324 			if (fbdev->panel->enable)
325 				fbdev->panel->enable(fbdev->panel);
326 			fbdev->state = OMAPFB_ACTIVE;
327 			if (fbdev->ctrl->get_update_mode() ==
328 					OMAPFB_MANUAL_UPDATE)
329 				do_update = 1;
330 		}
331 		break;
332 	case FB_BLANK_POWERDOWN:
333 		if (fbdev->state == OMAPFB_ACTIVE) {
334 			if (fbdev->panel->disable)
335 				fbdev->panel->disable(fbdev->panel);
336 			if (fbdev->ctrl->suspend)
337 				fbdev->ctrl->suspend();
338 			fbdev->state = OMAPFB_SUSPENDED;
339 		}
340 		break;
341 	default:
342 		r = -EINVAL;
343 	}
344 	omapfb_rqueue_unlock(fbdev);
345 
346 	if (r == 0 && do_update)
347 		r = omapfb_update_full_screen(fbi);
348 
349 	return r;
350 }
351 
352 static void omapfb_sync(struct fb_info *fbi)
353 {
354 	struct omapfb_plane_struct *plane = fbi->par;
355 	struct omapfb_device *fbdev = plane->fbdev;
356 
357 	omapfb_rqueue_lock(fbdev);
358 	if (fbdev->ctrl->sync)
359 		fbdev->ctrl->sync();
360 	omapfb_rqueue_unlock(fbdev);
361 }
362 
363 /*
364  * Set fb_info.fix fields and also updates fbdev.
365  * When calling this fb_info.var must be set up already.
366  */
367 static void set_fb_fix(struct fb_info *fbi, int from_init)
368 {
369 	struct fb_fix_screeninfo *fix = &fbi->fix;
370 	struct fb_var_screeninfo *var = &fbi->var;
371 	struct omapfb_plane_struct *plane = fbi->par;
372 	struct omapfb_mem_region *rg;
373 	int bpp;
374 
375 	rg = &plane->fbdev->mem_desc.region[plane->idx];
376 	fbi->screen_base	= rg->vaddr;
377 
378 	if (!from_init) {
379 		mutex_lock(&fbi->mm_lock);
380 		fix->smem_start		= rg->paddr;
381 		fix->smem_len		= rg->size;
382 		mutex_unlock(&fbi->mm_lock);
383 	} else {
384 		fix->smem_start		= rg->paddr;
385 		fix->smem_len		= rg->size;
386 	}
387 
388 	fix->type = FB_TYPE_PACKED_PIXELS;
389 	bpp = var->bits_per_pixel;
390 	if (var->nonstd)
391 		fix->visual = FB_VISUAL_PSEUDOCOLOR;
392 	else switch (var->bits_per_pixel) {
393 	case 16:
394 	case 12:
395 		fix->visual = FB_VISUAL_TRUECOLOR;
396 		/* 12bpp is stored in 16 bits */
397 		bpp = 16;
398 		break;
399 	case 1:
400 	case 2:
401 	case 4:
402 	case 8:
403 		fix->visual = FB_VISUAL_PSEUDOCOLOR;
404 		break;
405 	}
406 	fix->accel		= FB_ACCEL_OMAP1610;
407 	fix->line_length	= var->xres_virtual * bpp / 8;
408 }
409 
410 static int set_color_mode(struct omapfb_plane_struct *plane,
411 			  struct fb_var_screeninfo *var)
412 {
413 	switch (var->nonstd) {
414 	case 0:
415 		break;
416 	case OMAPFB_COLOR_YUV422:
417 		var->bits_per_pixel = 16;
418 		plane->color_mode = var->nonstd;
419 		return 0;
420 	case OMAPFB_COLOR_YUV420:
421 		var->bits_per_pixel = 12;
422 		plane->color_mode = var->nonstd;
423 		return 0;
424 	case OMAPFB_COLOR_YUY422:
425 		var->bits_per_pixel = 16;
426 		plane->color_mode = var->nonstd;
427 		return 0;
428 	default:
429 		return -EINVAL;
430 	}
431 
432 	switch (var->bits_per_pixel) {
433 	case 1:
434 		plane->color_mode = OMAPFB_COLOR_CLUT_1BPP;
435 		return 0;
436 	case 2:
437 		plane->color_mode = OMAPFB_COLOR_CLUT_2BPP;
438 		return 0;
439 	case 4:
440 		plane->color_mode = OMAPFB_COLOR_CLUT_4BPP;
441 		return 0;
442 	case 8:
443 		plane->color_mode = OMAPFB_COLOR_CLUT_8BPP;
444 		return 0;
445 	case 12:
446 		var->bits_per_pixel = 16;
447 		fallthrough;
448 	case 16:
449 		if (plane->fbdev->panel->bpp == 12)
450 			plane->color_mode = OMAPFB_COLOR_RGB444;
451 		else
452 			plane->color_mode = OMAPFB_COLOR_RGB565;
453 		return 0;
454 	default:
455 		return -EINVAL;
456 	}
457 }
458 
459 /*
460  * Check the values in var against our capabilities and in case of out of
461  * bound values try to adjust them.
462  */
463 static int set_fb_var(struct fb_info *fbi,
464 		      struct fb_var_screeninfo *var)
465 {
466 	int		bpp;
467 	unsigned long	max_frame_size;
468 	unsigned long	line_size;
469 	int		xres_min, xres_max;
470 	int		yres_min, yres_max;
471 	struct omapfb_plane_struct *plane = fbi->par;
472 	struct omapfb_device *fbdev = plane->fbdev;
473 	struct lcd_panel *panel = fbdev->panel;
474 
475 	if (set_color_mode(plane, var) < 0)
476 		return -EINVAL;
477 
478 	bpp = var->bits_per_pixel;
479 	if (plane->color_mode == OMAPFB_COLOR_RGB444)
480 		bpp = 16;
481 
482 	switch (var->rotate) {
483 	case 0:
484 	case 180:
485 		xres_min = OMAPFB_PLANE_XRES_MIN;
486 		xres_max = panel->x_res;
487 		yres_min = OMAPFB_PLANE_YRES_MIN;
488 		yres_max = panel->y_res;
489 		if (cpu_is_omap15xx()) {
490 			var->xres = panel->x_res;
491 			var->yres = panel->y_res;
492 		}
493 		break;
494 	case 90:
495 	case 270:
496 		xres_min = OMAPFB_PLANE_YRES_MIN;
497 		xres_max = panel->y_res;
498 		yres_min = OMAPFB_PLANE_XRES_MIN;
499 		yres_max = panel->x_res;
500 		if (cpu_is_omap15xx()) {
501 			var->xres = panel->y_res;
502 			var->yres = panel->x_res;
503 		}
504 		break;
505 	default:
506 		return -EINVAL;
507 	}
508 
509 	if (var->xres < xres_min)
510 		var->xres = xres_min;
511 	if (var->yres < yres_min)
512 		var->yres = yres_min;
513 	if (var->xres > xres_max)
514 		var->xres = xres_max;
515 	if (var->yres > yres_max)
516 		var->yres = yres_max;
517 
518 	if (var->xres_virtual < var->xres)
519 		var->xres_virtual = var->xres;
520 	if (var->yres_virtual < var->yres)
521 		var->yres_virtual = var->yres;
522 	max_frame_size = fbdev->mem_desc.region[plane->idx].size;
523 	line_size = var->xres_virtual * bpp / 8;
524 	if (line_size * var->yres_virtual > max_frame_size) {
525 		/* Try to keep yres_virtual first */
526 		line_size = max_frame_size / var->yres_virtual;
527 		var->xres_virtual = line_size * 8 / bpp;
528 		if (var->xres_virtual < var->xres) {
529 			/* Still doesn't fit. Shrink yres_virtual too */
530 			var->xres_virtual = var->xres;
531 			line_size = var->xres * bpp / 8;
532 			var->yres_virtual = max_frame_size / line_size;
533 		}
534 		/* Recheck this, as the virtual size changed. */
535 		if (var->xres_virtual < var->xres)
536 			var->xres = var->xres_virtual;
537 		if (var->yres_virtual < var->yres)
538 			var->yres = var->yres_virtual;
539 		if (var->xres < xres_min || var->yres < yres_min)
540 			return -EINVAL;
541 	}
542 	if (var->xres + var->xoffset > var->xres_virtual)
543 		var->xoffset = var->xres_virtual - var->xres;
544 	if (var->yres + var->yoffset > var->yres_virtual)
545 		var->yoffset = var->yres_virtual - var->yres;
546 
547 	if (plane->color_mode == OMAPFB_COLOR_RGB444) {
548 		var->red.offset	  = 8; var->red.length	 = 4;
549 						var->red.msb_right   = 0;
550 		var->green.offset = 4; var->green.length = 4;
551 						var->green.msb_right = 0;
552 		var->blue.offset  = 0; var->blue.length  = 4;
553 						var->blue.msb_right  = 0;
554 	} else {
555 		var->red.offset	 = 11; var->red.length	 = 5;
556 						var->red.msb_right   = 0;
557 		var->green.offset = 5;  var->green.length = 6;
558 						var->green.msb_right = 0;
559 		var->blue.offset = 0;  var->blue.length  = 5;
560 						var->blue.msb_right  = 0;
561 	}
562 
563 	var->height		= -1;
564 	var->width		= -1;
565 	var->grayscale		= 0;
566 
567 	/* pixclock in ps, the rest in pixclock */
568 	var->pixclock		= 10000000 / (panel->pixel_clock / 100);
569 	var->left_margin	= panel->hfp;
570 	var->right_margin	= panel->hbp;
571 	var->upper_margin	= panel->vfp;
572 	var->lower_margin	= panel->vbp;
573 	var->hsync_len		= panel->hsw;
574 	var->vsync_len		= panel->vsw;
575 
576 	/* TODO: get these from panel->config */
577 	var->vmode		= FB_VMODE_NONINTERLACED;
578 	var->sync		= 0;
579 
580 	return 0;
581 }
582 
583 
584 /*
585  * Set new x,y offsets in the virtual display for the visible area and switch
586  * to the new mode.
587  */
588 static int omapfb_pan_display(struct fb_var_screeninfo *var,
589 			       struct fb_info *fbi)
590 {
591 	struct omapfb_plane_struct *plane = fbi->par;
592 	struct omapfb_device *fbdev = plane->fbdev;
593 	int r = 0;
594 
595 	omapfb_rqueue_lock(fbdev);
596 	if (var->xoffset != fbi->var.xoffset ||
597 	    var->yoffset != fbi->var.yoffset) {
598 		struct fb_var_screeninfo *new_var = &fbdev->new_var;
599 
600 		memcpy(new_var, &fbi->var, sizeof(*new_var));
601 		new_var->xoffset = var->xoffset;
602 		new_var->yoffset = var->yoffset;
603 		if (set_fb_var(fbi, new_var))
604 			r = -EINVAL;
605 		else {
606 			memcpy(&fbi->var, new_var, sizeof(*new_var));
607 			ctrl_change_mode(fbi);
608 		}
609 	}
610 	omapfb_rqueue_unlock(fbdev);
611 
612 	return r;
613 }
614 
615 /* Set mirror to vertical axis and switch to the new mode. */
616 static int omapfb_mirror(struct fb_info *fbi, int mirror)
617 {
618 	struct omapfb_plane_struct *plane = fbi->par;
619 	struct omapfb_device *fbdev = plane->fbdev;
620 	int r = 0;
621 
622 	omapfb_rqueue_lock(fbdev);
623 	mirror = mirror ? 1 : 0;
624 	if (cpu_is_omap15xx())
625 		r = -EINVAL;
626 	else if (mirror != plane->info.mirror) {
627 		plane->info.mirror = mirror;
628 		r = ctrl_change_mode(fbi);
629 	}
630 	omapfb_rqueue_unlock(fbdev);
631 
632 	return r;
633 }
634 
635 /*
636  * Check values in var, try to adjust them in case of out of bound values if
637  * possible, or return error.
638  */
639 static int omapfb_check_var(struct fb_var_screeninfo *var, struct fb_info *fbi)
640 {
641 	struct omapfb_plane_struct *plane = fbi->par;
642 	struct omapfb_device *fbdev = plane->fbdev;
643 	int r;
644 
645 	omapfb_rqueue_lock(fbdev);
646 	if (fbdev->ctrl->sync != NULL)
647 		fbdev->ctrl->sync();
648 	r = set_fb_var(fbi, var);
649 	omapfb_rqueue_unlock(fbdev);
650 
651 	return r;
652 }
653 
654 /*
655  * Switch to a new mode. The parameters for it has been check already by
656  * omapfb_check_var.
657  */
658 static int omapfb_set_par(struct fb_info *fbi)
659 {
660 	struct omapfb_plane_struct *plane = fbi->par;
661 	struct omapfb_device *fbdev = plane->fbdev;
662 	int r = 0;
663 
664 	omapfb_rqueue_lock(fbdev);
665 	set_fb_fix(fbi, 0);
666 	r = ctrl_change_mode(fbi);
667 	omapfb_rqueue_unlock(fbdev);
668 
669 	return r;
670 }
671 
672 int omapfb_update_window_async(struct fb_info *fbi,
673 				struct omapfb_update_window *win,
674 				void (*callback)(void *),
675 				void *callback_data)
676 {
677 	int xres, yres;
678 	struct omapfb_plane_struct *plane = fbi->par;
679 	struct omapfb_device *fbdev = plane->fbdev;
680 	struct fb_var_screeninfo *var = &fbi->var;
681 
682 	switch (var->rotate) {
683 	case 0:
684 	case 180:
685 		xres = fbdev->panel->x_res;
686 		yres = fbdev->panel->y_res;
687 		break;
688 	case 90:
689 	case 270:
690 		xres = fbdev->panel->y_res;
691 		yres = fbdev->panel->x_res;
692 		break;
693 	default:
694 		return -EINVAL;
695 	}
696 
697 	if (win->x >= xres || win->y >= yres ||
698 	    win->out_x > xres || win->out_y > yres)
699 		return -EINVAL;
700 
701 	if (!fbdev->ctrl->update_window ||
702 	    fbdev->ctrl->get_update_mode() != OMAPFB_MANUAL_UPDATE)
703 		return -ENODEV;
704 
705 	if (win->x + win->width > xres)
706 		win->width = xres - win->x;
707 	if (win->y + win->height > yres)
708 		win->height = yres - win->y;
709 	if (win->out_x + win->out_width > xres)
710 		win->out_width = xres - win->out_x;
711 	if (win->out_y + win->out_height > yres)
712 		win->out_height = yres - win->out_y;
713 	if (!win->width || !win->height || !win->out_width || !win->out_height)
714 		return 0;
715 
716 	return fbdev->ctrl->update_window(fbi, win, callback, callback_data);
717 }
718 EXPORT_SYMBOL(omapfb_update_window_async);
719 
720 static int omapfb_update_win(struct fb_info *fbi,
721 				struct omapfb_update_window *win)
722 {
723 	struct omapfb_plane_struct *plane = fbi->par;
724 	int ret;
725 
726 	omapfb_rqueue_lock(plane->fbdev);
727 	ret = omapfb_update_window_async(fbi, win, NULL, NULL);
728 	omapfb_rqueue_unlock(plane->fbdev);
729 
730 	return ret;
731 }
732 
733 static int omapfb_update_full_screen(struct fb_info *fbi)
734 {
735 	struct omapfb_plane_struct *plane = fbi->par;
736 	struct omapfb_device *fbdev = plane->fbdev;
737 	struct omapfb_update_window win;
738 	int r;
739 
740 	if (!fbdev->ctrl->update_window ||
741 	    fbdev->ctrl->get_update_mode() != OMAPFB_MANUAL_UPDATE)
742 		return -ENODEV;
743 
744 	win.x = 0;
745 	win.y = 0;
746 	win.width = fbi->var.xres;
747 	win.height = fbi->var.yres;
748 	win.out_x = 0;
749 	win.out_y = 0;
750 	win.out_width = fbi->var.xres;
751 	win.out_height = fbi->var.yres;
752 	win.format = 0;
753 
754 	omapfb_rqueue_lock(fbdev);
755 	r = fbdev->ctrl->update_window(fbi, &win, NULL, NULL);
756 	omapfb_rqueue_unlock(fbdev);
757 
758 	return r;
759 }
760 
761 static int omapfb_setup_plane(struct fb_info *fbi, struct omapfb_plane_info *pi)
762 {
763 	struct omapfb_plane_struct *plane = fbi->par;
764 	struct omapfb_device *fbdev = plane->fbdev;
765 	struct lcd_panel *panel = fbdev->panel;
766 	struct omapfb_plane_info old_info;
767 	int r = 0;
768 
769 	if (pi->pos_x + pi->out_width > panel->x_res ||
770 	    pi->pos_y + pi->out_height > panel->y_res)
771 		return -EINVAL;
772 
773 	omapfb_rqueue_lock(fbdev);
774 	if (pi->enabled && !fbdev->mem_desc.region[plane->idx].size) {
775 		/*
776 		 * This plane's memory was freed, can't enable it
777 		 * until it's reallocated.
778 		 */
779 		r = -EINVAL;
780 		goto out;
781 	}
782 	old_info = plane->info;
783 	plane->info = *pi;
784 	if (pi->enabled) {
785 		r = ctrl_change_mode(fbi);
786 		if (r < 0) {
787 			plane->info = old_info;
788 			goto out;
789 		}
790 	}
791 	r = fbdev->ctrl->enable_plane(plane->idx, pi->enabled);
792 	if (r < 0) {
793 		plane->info = old_info;
794 		goto out;
795 	}
796 out:
797 	omapfb_rqueue_unlock(fbdev);
798 	return r;
799 }
800 
801 static int omapfb_query_plane(struct fb_info *fbi, struct omapfb_plane_info *pi)
802 {
803 	struct omapfb_plane_struct *plane = fbi->par;
804 
805 	*pi = plane->info;
806 	return 0;
807 }
808 
809 static int omapfb_setup_mem(struct fb_info *fbi, struct omapfb_mem_info *mi)
810 {
811 	struct omapfb_plane_struct *plane = fbi->par;
812 	struct omapfb_device *fbdev = plane->fbdev;
813 	struct omapfb_mem_region *rg = &fbdev->mem_desc.region[plane->idx];
814 	size_t size;
815 	int r = 0;
816 
817 	if (fbdev->ctrl->setup_mem == NULL)
818 		return -ENODEV;
819 	if (mi->type != OMAPFB_MEMTYPE_SDRAM)
820 		return -EINVAL;
821 
822 	size = PAGE_ALIGN(mi->size);
823 	omapfb_rqueue_lock(fbdev);
824 	if (plane->info.enabled) {
825 		r = -EBUSY;
826 		goto out;
827 	}
828 	if (rg->size != size || rg->type != mi->type) {
829 		struct fb_var_screeninfo *new_var = &fbdev->new_var;
830 		unsigned long old_size = rg->size;
831 		u8	      old_type = rg->type;
832 		unsigned long paddr;
833 
834 		rg->size = size;
835 		rg->type = mi->type;
836 		/*
837 		 * size == 0 is a special case, for which we
838 		 * don't check / adjust the screen parameters.
839 		 * This isn't a problem since the plane can't
840 		 * be reenabled unless its size is > 0.
841 		 */
842 		if (old_size != size && size) {
843 			if (size) {
844 				memcpy(new_var, &fbi->var, sizeof(*new_var));
845 				r = set_fb_var(fbi, new_var);
846 				if (r < 0)
847 					goto out;
848 			}
849 		}
850 
851 		if (fbdev->ctrl->sync)
852 			fbdev->ctrl->sync();
853 		r = fbdev->ctrl->setup_mem(plane->idx, size, mi->type, &paddr);
854 		if (r < 0) {
855 			/* Revert changes. */
856 			rg->size = old_size;
857 			rg->type = old_type;
858 			goto out;
859 		}
860 		rg->paddr = paddr;
861 
862 		if (old_size != size) {
863 			if (size) {
864 				memcpy(&fbi->var, new_var, sizeof(fbi->var));
865 				set_fb_fix(fbi, 0);
866 			} else {
867 				/*
868 				 * Set these explicitly to indicate that the
869 				 * plane memory is dealloce'd, the other
870 				 * screen parameters in var / fix are invalid.
871 				 */
872 				mutex_lock(&fbi->mm_lock);
873 				fbi->fix.smem_start = 0;
874 				fbi->fix.smem_len = 0;
875 				mutex_unlock(&fbi->mm_lock);
876 			}
877 		}
878 	}
879 out:
880 	omapfb_rqueue_unlock(fbdev);
881 
882 	return r;
883 }
884 
885 static int omapfb_query_mem(struct fb_info *fbi, struct omapfb_mem_info *mi)
886 {
887 	struct omapfb_plane_struct *plane = fbi->par;
888 	struct omapfb_device *fbdev = plane->fbdev;
889 	struct omapfb_mem_region *rg;
890 
891 	rg = &fbdev->mem_desc.region[plane->idx];
892 	memset(mi, 0, sizeof(*mi));
893 	mi->size = rg->size;
894 	mi->type = rg->type;
895 
896 	return 0;
897 }
898 
899 static int omapfb_set_color_key(struct omapfb_device *fbdev,
900 				struct omapfb_color_key *ck)
901 {
902 	int r;
903 
904 	if (!fbdev->ctrl->set_color_key)
905 		return -ENODEV;
906 
907 	omapfb_rqueue_lock(fbdev);
908 	r = fbdev->ctrl->set_color_key(ck);
909 	omapfb_rqueue_unlock(fbdev);
910 
911 	return r;
912 }
913 
914 static int omapfb_get_color_key(struct omapfb_device *fbdev,
915 				struct omapfb_color_key *ck)
916 {
917 	int r;
918 
919 	if (!fbdev->ctrl->get_color_key)
920 		return -ENODEV;
921 
922 	omapfb_rqueue_lock(fbdev);
923 	r = fbdev->ctrl->get_color_key(ck);
924 	omapfb_rqueue_unlock(fbdev);
925 
926 	return r;
927 }
928 
929 static struct blocking_notifier_head omapfb_client_list[OMAPFB_PLANE_NUM];
930 static int notifier_inited;
931 
932 static void omapfb_init_notifier(void)
933 {
934 	int i;
935 
936 	for (i = 0; i < OMAPFB_PLANE_NUM; i++)
937 		BLOCKING_INIT_NOTIFIER_HEAD(&omapfb_client_list[i]);
938 }
939 
940 int omapfb_register_client(struct omapfb_notifier_block *omapfb_nb,
941 				omapfb_notifier_callback_t callback,
942 				void *callback_data)
943 {
944 	int r;
945 
946 	if ((unsigned)omapfb_nb->plane_idx >= OMAPFB_PLANE_NUM)
947 		return -EINVAL;
948 
949 	if (!notifier_inited) {
950 		omapfb_init_notifier();
951 		notifier_inited = 1;
952 	}
953 
954 	omapfb_nb->nb.notifier_call = (int (*)(struct notifier_block *,
955 					unsigned long, void *))callback;
956 	omapfb_nb->data = callback_data;
957 	r = blocking_notifier_chain_register(
958 				&omapfb_client_list[omapfb_nb->plane_idx],
959 				&omapfb_nb->nb);
960 	if (r)
961 		return r;
962 	if (omapfb_dev != NULL &&
963 	    omapfb_dev->ctrl && omapfb_dev->ctrl->bind_client) {
964 		omapfb_dev->ctrl->bind_client(omapfb_nb);
965 	}
966 
967 	return 0;
968 }
969 EXPORT_SYMBOL(omapfb_register_client);
970 
971 int omapfb_unregister_client(struct omapfb_notifier_block *omapfb_nb)
972 {
973 	return blocking_notifier_chain_unregister(
974 		&omapfb_client_list[omapfb_nb->plane_idx], &omapfb_nb->nb);
975 }
976 EXPORT_SYMBOL(omapfb_unregister_client);
977 
978 void omapfb_notify_clients(struct omapfb_device *fbdev, unsigned long event)
979 {
980 	int i;
981 
982 	if (!notifier_inited)
983 		/* no client registered yet */
984 		return;
985 
986 	for (i = 0; i < OMAPFB_PLANE_NUM; i++)
987 		blocking_notifier_call_chain(&omapfb_client_list[i], event,
988 				    fbdev->fb_info[i]);
989 }
990 EXPORT_SYMBOL(omapfb_notify_clients);
991 
992 static int omapfb_set_update_mode(struct omapfb_device *fbdev,
993 				   enum omapfb_update_mode mode)
994 {
995 	int r;
996 
997 	omapfb_rqueue_lock(fbdev);
998 	r = fbdev->ctrl->set_update_mode(mode);
999 	omapfb_rqueue_unlock(fbdev);
1000 
1001 	return r;
1002 }
1003 
1004 static enum omapfb_update_mode omapfb_get_update_mode(struct omapfb_device *fbdev)
1005 {
1006 	int r;
1007 
1008 	omapfb_rqueue_lock(fbdev);
1009 	r = fbdev->ctrl->get_update_mode();
1010 	omapfb_rqueue_unlock(fbdev);
1011 
1012 	return r;
1013 }
1014 
1015 static void omapfb_get_caps(struct omapfb_device *fbdev, int plane,
1016 				     struct omapfb_caps *caps)
1017 {
1018 	memset(caps, 0, sizeof(*caps));
1019 	fbdev->ctrl->get_caps(plane, caps);
1020 	if (fbdev->panel->get_caps)
1021 		caps->ctrl |= fbdev->panel->get_caps(fbdev->panel);
1022 }
1023 
1024 /* For lcd testing */
1025 void omapfb_write_first_pixel(struct omapfb_device *fbdev, u16 pixval)
1026 {
1027 	omapfb_rqueue_lock(fbdev);
1028 	*(u16 *)fbdev->mem_desc.region[0].vaddr = pixval;
1029 	if (fbdev->ctrl->get_update_mode() == OMAPFB_MANUAL_UPDATE) {
1030 		struct omapfb_update_window win;
1031 
1032 		memset(&win, 0, sizeof(win));
1033 		win.width = 2;
1034 		win.height = 2;
1035 		win.out_width = 2;
1036 		win.out_height = 2;
1037 		fbdev->ctrl->update_window(fbdev->fb_info[0], &win, NULL, NULL);
1038 	}
1039 	omapfb_rqueue_unlock(fbdev);
1040 }
1041 EXPORT_SYMBOL(omapfb_write_first_pixel);
1042 
1043 /*
1044  * Ioctl interface. Part of the kernel mode frame buffer API is duplicated
1045  * here to be accessible by user mode code.
1046  */
1047 static int omapfb_ioctl(struct fb_info *fbi, unsigned int cmd,
1048 			unsigned long arg)
1049 {
1050 	struct omapfb_plane_struct *plane = fbi->par;
1051 	struct omapfb_device	*fbdev = plane->fbdev;
1052 	const struct fb_ops *ops = fbi->fbops;
1053 	union {
1054 		struct omapfb_update_window	update_window;
1055 		struct omapfb_plane_info	plane_info;
1056 		struct omapfb_mem_info		mem_info;
1057 		struct omapfb_color_key		color_key;
1058 		enum omapfb_update_mode		update_mode;
1059 		struct omapfb_caps		caps;
1060 		unsigned int		mirror;
1061 		int			plane_out;
1062 		int			enable_plane;
1063 	} p;
1064 	int r = 0;
1065 
1066 	BUG_ON(!ops);
1067 	switch (cmd) {
1068 	case OMAPFB_MIRROR:
1069 		if (get_user(p.mirror, (int __user *)arg))
1070 			r = -EFAULT;
1071 		else
1072 			omapfb_mirror(fbi, p.mirror);
1073 		break;
1074 	case OMAPFB_SYNC_GFX:
1075 		omapfb_sync(fbi);
1076 		break;
1077 	case OMAPFB_VSYNC:
1078 		break;
1079 	case OMAPFB_SET_UPDATE_MODE:
1080 		if (get_user(p.update_mode, (int __user *)arg))
1081 			r = -EFAULT;
1082 		else
1083 			r = omapfb_set_update_mode(fbdev, p.update_mode);
1084 		break;
1085 	case OMAPFB_GET_UPDATE_MODE:
1086 		p.update_mode = omapfb_get_update_mode(fbdev);
1087 		if (put_user(p.update_mode,
1088 					(enum omapfb_update_mode __user *)arg))
1089 			r = -EFAULT;
1090 		break;
1091 	case OMAPFB_UPDATE_WINDOW_OLD:
1092 		if (copy_from_user(&p.update_window, (void __user *)arg,
1093 				   sizeof(struct omapfb_update_window_old)))
1094 			r = -EFAULT;
1095 		else {
1096 			struct omapfb_update_window *u = &p.update_window;
1097 			u->out_x = u->x;
1098 			u->out_y = u->y;
1099 			u->out_width = u->width;
1100 			u->out_height = u->height;
1101 			memset(u->reserved, 0, sizeof(u->reserved));
1102 			r = omapfb_update_win(fbi, u);
1103 		}
1104 		break;
1105 	case OMAPFB_UPDATE_WINDOW:
1106 		if (copy_from_user(&p.update_window, (void __user *)arg,
1107 				   sizeof(p.update_window)))
1108 			r = -EFAULT;
1109 		else
1110 			r = omapfb_update_win(fbi, &p.update_window);
1111 		break;
1112 	case OMAPFB_SETUP_PLANE:
1113 		if (copy_from_user(&p.plane_info, (void __user *)arg,
1114 				   sizeof(p.plane_info)))
1115 			r = -EFAULT;
1116 		else
1117 			r = omapfb_setup_plane(fbi, &p.plane_info);
1118 		break;
1119 	case OMAPFB_QUERY_PLANE:
1120 		if ((r = omapfb_query_plane(fbi, &p.plane_info)) < 0)
1121 			break;
1122 		if (copy_to_user((void __user *)arg, &p.plane_info,
1123 				   sizeof(p.plane_info)))
1124 			r = -EFAULT;
1125 		break;
1126 	case OMAPFB_SETUP_MEM:
1127 		if (copy_from_user(&p.mem_info, (void __user *)arg,
1128 				   sizeof(p.mem_info)))
1129 			r = -EFAULT;
1130 		else
1131 			r = omapfb_setup_mem(fbi, &p.mem_info);
1132 		break;
1133 	case OMAPFB_QUERY_MEM:
1134 		if ((r = omapfb_query_mem(fbi, &p.mem_info)) < 0)
1135 			break;
1136 		if (copy_to_user((void __user *)arg, &p.mem_info,
1137 				   sizeof(p.mem_info)))
1138 			r = -EFAULT;
1139 		break;
1140 	case OMAPFB_SET_COLOR_KEY:
1141 		if (copy_from_user(&p.color_key, (void __user *)arg,
1142 				   sizeof(p.color_key)))
1143 			r = -EFAULT;
1144 		else
1145 			r = omapfb_set_color_key(fbdev, &p.color_key);
1146 		break;
1147 	case OMAPFB_GET_COLOR_KEY:
1148 		if ((r = omapfb_get_color_key(fbdev, &p.color_key)) < 0)
1149 			break;
1150 		if (copy_to_user((void __user *)arg, &p.color_key,
1151 				 sizeof(p.color_key)))
1152 			r = -EFAULT;
1153 		break;
1154 	case OMAPFB_GET_CAPS:
1155 		omapfb_get_caps(fbdev, plane->idx, &p.caps);
1156 		if (copy_to_user((void __user *)arg, &p.caps, sizeof(p.caps)))
1157 			r = -EFAULT;
1158 		break;
1159 	case OMAPFB_LCD_TEST:
1160 		{
1161 			int test_num;
1162 
1163 			if (get_user(test_num, (int __user *)arg)) {
1164 				r = -EFAULT;
1165 				break;
1166 			}
1167 			if (!fbdev->panel->run_test) {
1168 				r = -EINVAL;
1169 				break;
1170 			}
1171 			r = fbdev->panel->run_test(fbdev->panel, test_num);
1172 			break;
1173 		}
1174 	case OMAPFB_CTRL_TEST:
1175 		{
1176 			int test_num;
1177 
1178 			if (get_user(test_num, (int __user *)arg)) {
1179 				r = -EFAULT;
1180 				break;
1181 			}
1182 			if (!fbdev->ctrl->run_test) {
1183 				r = -EINVAL;
1184 				break;
1185 			}
1186 			r = fbdev->ctrl->run_test(test_num);
1187 			break;
1188 		}
1189 	default:
1190 		r = -EINVAL;
1191 	}
1192 
1193 	return r;
1194 }
1195 
1196 static int omapfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
1197 {
1198 	struct omapfb_plane_struct *plane = info->par;
1199 	struct omapfb_device *fbdev = plane->fbdev;
1200 	int r;
1201 
1202 	omapfb_rqueue_lock(fbdev);
1203 	r = fbdev->ctrl->mmap(info, vma);
1204 	omapfb_rqueue_unlock(fbdev);
1205 
1206 	return r;
1207 }
1208 
1209 /*
1210  * Callback table for the frame buffer framework. Some of these pointers
1211  * will be changed according to the current setting of fb_info->accel_flags.
1212  */
1213 static struct fb_ops omapfb_ops = {
1214 	.owner		= THIS_MODULE,
1215 	.fb_open        = omapfb_open,
1216 	.fb_release     = omapfb_release,
1217 	.fb_setcolreg	= omapfb_setcolreg,
1218 	.fb_setcmap	= omapfb_setcmap,
1219 	.fb_fillrect	= cfb_fillrect,
1220 	.fb_copyarea	= cfb_copyarea,
1221 	.fb_imageblit	= cfb_imageblit,
1222 	.fb_blank       = omapfb_blank,
1223 	.fb_ioctl	= omapfb_ioctl,
1224 	.fb_check_var	= omapfb_check_var,
1225 	.fb_set_par	= omapfb_set_par,
1226 	.fb_pan_display = omapfb_pan_display,
1227 };
1228 
1229 /*
1230  * ---------------------------------------------------------------------------
1231  * Sysfs interface
1232  * ---------------------------------------------------------------------------
1233  */
1234 /* omapfbX sysfs entries */
1235 static ssize_t omapfb_show_caps_num(struct device *dev,
1236 				    struct device_attribute *attr, char *buf)
1237 {
1238 	struct omapfb_device *fbdev = dev_get_drvdata(dev);
1239 	int plane;
1240 	size_t size;
1241 	struct omapfb_caps caps;
1242 
1243 	plane = 0;
1244 	size = 0;
1245 	while (size < PAGE_SIZE && plane < OMAPFB_PLANE_NUM) {
1246 		omapfb_get_caps(fbdev, plane, &caps);
1247 		size += scnprintf(&buf[size], PAGE_SIZE - size,
1248 			"plane#%d %#010x %#010x %#010x\n",
1249 			plane, caps.ctrl, caps.plane_color, caps.wnd_color);
1250 		plane++;
1251 	}
1252 	return size;
1253 }
1254 
1255 static ssize_t omapfb_show_caps_text(struct device *dev,
1256 				     struct device_attribute *attr, char *buf)
1257 {
1258 	struct omapfb_device *fbdev = dev_get_drvdata(dev);
1259 	int i;
1260 	struct omapfb_caps caps;
1261 	int plane;
1262 	size_t size;
1263 
1264 	plane = 0;
1265 	size = 0;
1266 	while (size < PAGE_SIZE && plane < OMAPFB_PLANE_NUM) {
1267 		omapfb_get_caps(fbdev, plane, &caps);
1268 		size += scnprintf(&buf[size], PAGE_SIZE - size,
1269 				 "plane#%d:\n", plane);
1270 		for (i = 0; i < ARRAY_SIZE(ctrl_caps) &&
1271 		     size < PAGE_SIZE; i++) {
1272 			if (ctrl_caps[i].flag & caps.ctrl)
1273 				size += scnprintf(&buf[size], PAGE_SIZE - size,
1274 					" %s\n", ctrl_caps[i].name);
1275 		}
1276 		size += scnprintf(&buf[size], PAGE_SIZE - size,
1277 				 " plane colors:\n");
1278 		for (i = 0; i < ARRAY_SIZE(color_caps) &&
1279 		     size < PAGE_SIZE; i++) {
1280 			if (color_caps[i].flag & caps.plane_color)
1281 				size += scnprintf(&buf[size], PAGE_SIZE - size,
1282 					"  %s\n", color_caps[i].name);
1283 		}
1284 		size += scnprintf(&buf[size], PAGE_SIZE - size,
1285 				 " window colors:\n");
1286 		for (i = 0; i < ARRAY_SIZE(color_caps) &&
1287 		     size < PAGE_SIZE; i++) {
1288 			if (color_caps[i].flag & caps.wnd_color)
1289 				size += scnprintf(&buf[size], PAGE_SIZE - size,
1290 					"  %s\n", color_caps[i].name);
1291 		}
1292 
1293 		plane++;
1294 	}
1295 	return size;
1296 }
1297 
1298 static DEVICE_ATTR(caps_num, 0444, omapfb_show_caps_num, NULL);
1299 static DEVICE_ATTR(caps_text, 0444, omapfb_show_caps_text, NULL);
1300 
1301 /* panel sysfs entries */
1302 static ssize_t omapfb_show_panel_name(struct device *dev,
1303 				      struct device_attribute *attr, char *buf)
1304 {
1305 	struct omapfb_device *fbdev = dev_get_drvdata(dev);
1306 
1307 	return sysfs_emit(buf, "%s\n", fbdev->panel->name);
1308 }
1309 
1310 static ssize_t omapfb_show_bklight_level(struct device *dev,
1311 					 struct device_attribute *attr,
1312 					 char *buf)
1313 {
1314 	struct omapfb_device *fbdev = dev_get_drvdata(dev);
1315 	int r;
1316 
1317 	if (fbdev->panel->get_bklight_level) {
1318 		r = sysfs_emit(buf, "%d\n",
1319 			       fbdev->panel->get_bklight_level(fbdev->panel));
1320 	} else
1321 		r = -ENODEV;
1322 	return r;
1323 }
1324 
1325 static ssize_t omapfb_store_bklight_level(struct device *dev,
1326 					  struct device_attribute *attr,
1327 					  const char *buf, size_t size)
1328 {
1329 	struct omapfb_device *fbdev = dev_get_drvdata(dev);
1330 	int r;
1331 
1332 	if (fbdev->panel->set_bklight_level) {
1333 		unsigned int level;
1334 
1335 		if (sscanf(buf, "%10d", &level) == 1) {
1336 			r = fbdev->panel->set_bklight_level(fbdev->panel,
1337 							    level);
1338 		} else
1339 			r = -EINVAL;
1340 	} else
1341 		r = -ENODEV;
1342 	return r ? r : size;
1343 }
1344 
1345 static ssize_t omapfb_show_bklight_max(struct device *dev,
1346 				       struct device_attribute *attr, char *buf)
1347 {
1348 	struct omapfb_device *fbdev = dev_get_drvdata(dev);
1349 	int r;
1350 
1351 	if (fbdev->panel->get_bklight_level) {
1352 		r = sysfs_emit(buf, "%d\n",
1353 			       fbdev->panel->get_bklight_max(fbdev->panel));
1354 	} else
1355 		r = -ENODEV;
1356 	return r;
1357 }
1358 
1359 static struct device_attribute dev_attr_panel_name =
1360 	__ATTR(name, 0444, omapfb_show_panel_name, NULL);
1361 static DEVICE_ATTR(backlight_level, 0664,
1362 		   omapfb_show_bklight_level, omapfb_store_bklight_level);
1363 static DEVICE_ATTR(backlight_max, 0444, omapfb_show_bklight_max, NULL);
1364 
1365 static struct attribute *panel_attrs[] = {
1366 	&dev_attr_panel_name.attr,
1367 	&dev_attr_backlight_level.attr,
1368 	&dev_attr_backlight_max.attr,
1369 	NULL,
1370 };
1371 
1372 static const struct attribute_group panel_attr_grp = {
1373 	.name  = "panel",
1374 	.attrs = panel_attrs,
1375 };
1376 
1377 /* ctrl sysfs entries */
1378 static ssize_t omapfb_show_ctrl_name(struct device *dev,
1379 				     struct device_attribute *attr, char *buf)
1380 {
1381 	struct omapfb_device *fbdev = dev_get_drvdata(dev);
1382 
1383 	return sysfs_emit(buf, "%s\n", fbdev->ctrl->name);
1384 }
1385 
1386 static struct device_attribute dev_attr_ctrl_name =
1387 	__ATTR(name, 0444, omapfb_show_ctrl_name, NULL);
1388 
1389 static struct attribute *ctrl_attrs[] = {
1390 	&dev_attr_ctrl_name.attr,
1391 	NULL,
1392 };
1393 
1394 static const struct attribute_group ctrl_attr_grp = {
1395 	.name  = "ctrl",
1396 	.attrs = ctrl_attrs,
1397 };
1398 
1399 static int omapfb_register_sysfs(struct omapfb_device *fbdev)
1400 {
1401 	int r;
1402 
1403 	if ((r = device_create_file(fbdev->dev, &dev_attr_caps_num)))
1404 		goto fail0;
1405 
1406 	if ((r = device_create_file(fbdev->dev, &dev_attr_caps_text)))
1407 		goto fail1;
1408 
1409 	if ((r = sysfs_create_group(&fbdev->dev->kobj, &panel_attr_grp)))
1410 		goto fail2;
1411 
1412 	if ((r = sysfs_create_group(&fbdev->dev->kobj, &ctrl_attr_grp)))
1413 		goto fail3;
1414 
1415 	return 0;
1416 fail3:
1417 	sysfs_remove_group(&fbdev->dev->kobj, &panel_attr_grp);
1418 fail2:
1419 	device_remove_file(fbdev->dev, &dev_attr_caps_text);
1420 fail1:
1421 	device_remove_file(fbdev->dev, &dev_attr_caps_num);
1422 fail0:
1423 	dev_err(fbdev->dev, "unable to register sysfs interface\n");
1424 	return r;
1425 }
1426 
1427 static void omapfb_unregister_sysfs(struct omapfb_device *fbdev)
1428 {
1429 	sysfs_remove_group(&fbdev->dev->kobj, &ctrl_attr_grp);
1430 	sysfs_remove_group(&fbdev->dev->kobj, &panel_attr_grp);
1431 	device_remove_file(fbdev->dev, &dev_attr_caps_num);
1432 	device_remove_file(fbdev->dev, &dev_attr_caps_text);
1433 }
1434 
1435 /*
1436  * ---------------------------------------------------------------------------
1437  * LDM callbacks
1438  * ---------------------------------------------------------------------------
1439  */
1440 /* Initialize system fb_info object and set the default video mode.
1441  * The frame buffer memory already allocated by lcddma_init
1442  */
1443 static int fbinfo_init(struct omapfb_device *fbdev, struct fb_info *info)
1444 {
1445 	struct fb_var_screeninfo	*var = &info->var;
1446 	struct fb_fix_screeninfo	*fix = &info->fix;
1447 	int				r = 0;
1448 
1449 	info->fbops = &omapfb_ops;
1450 	info->flags = FBINFO_FLAG_DEFAULT;
1451 
1452 	strncpy(fix->id, MODULE_NAME, sizeof(fix->id));
1453 
1454 	info->pseudo_palette = fbdev->pseudo_palette;
1455 
1456 	var->accel_flags  = def_accel ? FB_ACCELF_TEXT : 0;
1457 	var->xres = def_vxres;
1458 	var->yres = def_vyres;
1459 	var->xres_virtual = def_vxres;
1460 	var->yres_virtual = def_vyres;
1461 	var->rotate	  = def_rotate;
1462 	var->bits_per_pixel = fbdev->panel->bpp;
1463 
1464 	set_fb_var(info, var);
1465 	set_fb_fix(info, 1);
1466 
1467 	r = fb_alloc_cmap(&info->cmap, 16, 0);
1468 	if (r != 0)
1469 		dev_err(fbdev->dev, "unable to allocate color map memory\n");
1470 
1471 	return r;
1472 }
1473 
1474 /* Release the fb_info object */
1475 static void fbinfo_cleanup(struct omapfb_device *fbdev, struct fb_info *fbi)
1476 {
1477 	fb_dealloc_cmap(&fbi->cmap);
1478 }
1479 
1480 static void planes_cleanup(struct omapfb_device *fbdev)
1481 {
1482 	int i;
1483 
1484 	for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
1485 		if (fbdev->fb_info[i] == NULL)
1486 			break;
1487 		fbinfo_cleanup(fbdev, fbdev->fb_info[i]);
1488 		framebuffer_release(fbdev->fb_info[i]);
1489 	}
1490 }
1491 
1492 static int planes_init(struct omapfb_device *fbdev)
1493 {
1494 	struct fb_info *fbi;
1495 	int i;
1496 	int r;
1497 
1498 	for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
1499 		struct omapfb_plane_struct *plane;
1500 		fbi = framebuffer_alloc(sizeof(struct omapfb_plane_struct),
1501 					fbdev->dev);
1502 		if (fbi == NULL) {
1503 			planes_cleanup(fbdev);
1504 			return -ENOMEM;
1505 		}
1506 		plane = fbi->par;
1507 		plane->idx = i;
1508 		plane->fbdev = fbdev;
1509 		plane->info.mirror = def_mirror;
1510 		fbdev->fb_info[i] = fbi;
1511 
1512 		if ((r = fbinfo_init(fbdev, fbi)) < 0) {
1513 			framebuffer_release(fbi);
1514 			planes_cleanup(fbdev);
1515 			return r;
1516 		}
1517 		plane->info.out_width = fbi->var.xres;
1518 		plane->info.out_height = fbi->var.yres;
1519 	}
1520 	return 0;
1521 }
1522 
1523 /*
1524  * Free driver resources. Can be called to rollback an aborted initialization
1525  * sequence.
1526  */
1527 static void omapfb_free_resources(struct omapfb_device *fbdev, int state)
1528 {
1529 	int i;
1530 
1531 	switch (state) {
1532 	case OMAPFB_ACTIVE:
1533 		for (i = 0; i < fbdev->mem_desc.region_cnt; i++)
1534 			unregister_framebuffer(fbdev->fb_info[i]);
1535 		fallthrough;
1536 	case 7:
1537 		omapfb_unregister_sysfs(fbdev);
1538 		fallthrough;
1539 	case 6:
1540 		if (fbdev->panel->disable)
1541 			fbdev->panel->disable(fbdev->panel);
1542 		fallthrough;
1543 	case 5:
1544 		omapfb_set_update_mode(fbdev, OMAPFB_UPDATE_DISABLED);
1545 		fallthrough;
1546 	case 4:
1547 		planes_cleanup(fbdev);
1548 		fallthrough;
1549 	case 3:
1550 		ctrl_cleanup(fbdev);
1551 		fallthrough;
1552 	case 2:
1553 		if (fbdev->panel->cleanup)
1554 			fbdev->panel->cleanup(fbdev->panel);
1555 		fallthrough;
1556 	case 1:
1557 		dev_set_drvdata(fbdev->dev, NULL);
1558 		kfree(fbdev);
1559 		break;
1560 	case 0:
1561 		/* nothing to free */
1562 		break;
1563 	default:
1564 		BUG();
1565 	}
1566 }
1567 
1568 static int omapfb_find_ctrl(struct omapfb_device *fbdev)
1569 {
1570 	struct omapfb_platform_data *conf;
1571 	char name[17];
1572 	int i;
1573 
1574 	conf = dev_get_platdata(fbdev->dev);
1575 
1576 	fbdev->ctrl = NULL;
1577 
1578 	strncpy(name, conf->lcd.ctrl_name, sizeof(name) - 1);
1579 	name[sizeof(name) - 1] = '\0';
1580 
1581 	if (strcmp(name, "internal") == 0) {
1582 		fbdev->ctrl = fbdev->int_ctrl;
1583 		return 0;
1584 	}
1585 
1586 	for (i = 0; i < ARRAY_SIZE(ctrls); i++) {
1587 		dev_dbg(fbdev->dev, "ctrl %s\n", ctrls[i]->name);
1588 		if (strcmp(ctrls[i]->name, name) == 0) {
1589 			fbdev->ctrl = ctrls[i];
1590 			break;
1591 		}
1592 	}
1593 
1594 	if (fbdev->ctrl == NULL) {
1595 		dev_dbg(fbdev->dev, "ctrl %s not supported\n", name);
1596 		return -1;
1597 	}
1598 
1599 	return 0;
1600 }
1601 
1602 /*
1603  * Called by LDM binding to probe and attach a new device.
1604  * Initialization sequence:
1605  *   1. allocate system omapfb_device structure
1606  *   2. select controller type according to platform configuration
1607  *      init LCD panel
1608  *   3. init LCD controller and LCD DMA
1609  *   4. init system fb_info structure for all planes
1610  *   5. setup video mode for first plane and enable it
1611  *   6. enable LCD panel
1612  *   7. register sysfs attributes
1613  *   OMAPFB_ACTIVE: register system fb_info structure for all planes
1614  */
1615 static int omapfb_do_probe(struct platform_device *pdev,
1616 				struct lcd_panel *panel)
1617 {
1618 	struct omapfb_device	*fbdev = NULL;
1619 	int			init_state;
1620 	unsigned long		phz, hhz, vhz;
1621 	unsigned long		vram;
1622 	int			i;
1623 	int			r = 0;
1624 
1625 	init_state = 0;
1626 
1627 	if (pdev->num_resources != 0) {
1628 		dev_err(&pdev->dev, "probed for an unknown device\n");
1629 		r = -ENODEV;
1630 		goto cleanup;
1631 	}
1632 
1633 	if (dev_get_platdata(&pdev->dev) == NULL) {
1634 		dev_err(&pdev->dev, "missing platform data\n");
1635 		r = -ENOENT;
1636 		goto cleanup;
1637 	}
1638 
1639 	fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
1640 	if (fbdev == NULL) {
1641 		dev_err(&pdev->dev,
1642 			"unable to allocate memory for device info\n");
1643 		r = -ENOMEM;
1644 		goto cleanup;
1645 	}
1646 	init_state++;
1647 
1648 	fbdev->dev = &pdev->dev;
1649 	fbdev->panel = panel;
1650 	fbdev->dssdev = &omapdss_device;
1651 	platform_set_drvdata(pdev, fbdev);
1652 
1653 	mutex_init(&fbdev->rqueue_mutex);
1654 
1655 	fbdev->int_ctrl = &omap1_int_ctrl;
1656 #ifdef CONFIG_FB_OMAP_LCDC_EXTERNAL
1657 	fbdev->ext_if = &omap1_ext_if;
1658 #endif
1659 	if (omapfb_find_ctrl(fbdev) < 0) {
1660 		dev_err(fbdev->dev,
1661 			"LCD controller not found, board not supported\n");
1662 		r = -ENODEV;
1663 		goto cleanup;
1664 	}
1665 
1666 	if (fbdev->panel->init) {
1667 		r = fbdev->panel->init(fbdev->panel, fbdev);
1668 		if (r)
1669 			goto cleanup;
1670 	}
1671 
1672 	pr_info("omapfb: configured for panel %s\n", fbdev->panel->name);
1673 
1674 	def_vxres = def_vxres ? def_vxres : fbdev->panel->x_res;
1675 	def_vyres = def_vyres ? def_vyres : fbdev->panel->y_res;
1676 
1677 	init_state++;
1678 
1679 	r = ctrl_init(fbdev);
1680 	if (r)
1681 		goto cleanup;
1682 	if (fbdev->ctrl->mmap != NULL)
1683 		omapfb_ops.fb_mmap = omapfb_mmap;
1684 	init_state++;
1685 
1686 	r = planes_init(fbdev);
1687 	if (r)
1688 		goto cleanup;
1689 	init_state++;
1690 
1691 #ifdef CONFIG_FB_OMAP_DMA_TUNE
1692 	/* Set DMA priority for EMIFF access to highest */
1693 	omap_set_dma_priority(0, OMAP_DMA_PORT_EMIFF, 15);
1694 #endif
1695 
1696 	r = ctrl_change_mode(fbdev->fb_info[0]);
1697 	if (r) {
1698 		dev_err(fbdev->dev, "mode setting failed\n");
1699 		goto cleanup;
1700 	}
1701 
1702 	/* GFX plane is enabled by default */
1703 	r = fbdev->ctrl->enable_plane(OMAPFB_PLANE_GFX, 1);
1704 	if (r)
1705 		goto cleanup;
1706 
1707 	omapfb_set_update_mode(fbdev, manual_update ?
1708 				   OMAPFB_MANUAL_UPDATE : OMAPFB_AUTO_UPDATE);
1709 	init_state++;
1710 
1711 	if (fbdev->panel->enable) {
1712 		r = fbdev->panel->enable(fbdev->panel);
1713 		if (r)
1714 			goto cleanup;
1715 	}
1716 	init_state++;
1717 
1718 	r = omapfb_register_sysfs(fbdev);
1719 	if (r)
1720 		goto cleanup;
1721 	init_state++;
1722 
1723 	vram = 0;
1724 	for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
1725 		r = register_framebuffer(fbdev->fb_info[i]);
1726 		if (r != 0) {
1727 			dev_err(fbdev->dev,
1728 				"registering framebuffer %d failed\n", i);
1729 			goto cleanup;
1730 		}
1731 		vram += fbdev->mem_desc.region[i].size;
1732 	}
1733 
1734 	fbdev->state = OMAPFB_ACTIVE;
1735 
1736 	panel = fbdev->panel;
1737 	phz = panel->pixel_clock * 1000;
1738 	hhz = phz * 10 / (panel->hfp + panel->x_res + panel->hbp + panel->hsw);
1739 	vhz = hhz / (panel->vfp + panel->y_res + panel->vbp + panel->vsw);
1740 
1741 	omapfb_dev = fbdev;
1742 
1743 	pr_info("omapfb: Framebuffer initialized. Total vram %lu planes %d\n",
1744 			vram, fbdev->mem_desc.region_cnt);
1745 	pr_info("omapfb: Pixclock %lu kHz hfreq %lu.%lu kHz "
1746 			"vfreq %lu.%lu Hz\n",
1747 			phz / 1000, hhz / 10000, hhz % 10, vhz / 10, vhz % 10);
1748 
1749 	return 0;
1750 
1751 cleanup:
1752 	omapfb_free_resources(fbdev, init_state);
1753 
1754 	return r;
1755 }
1756 
1757 static int omapfb_probe(struct platform_device *pdev)
1758 {
1759 	int r;
1760 
1761 	BUG_ON(fbdev_pdev != NULL);
1762 
1763 	r = platform_device_register(&omapdss_device);
1764 	if (r) {
1765 		dev_err(&pdev->dev, "can't register omapdss device\n");
1766 		return r;
1767 	}
1768 
1769 	/* Delay actual initialization until the LCD is registered */
1770 	fbdev_pdev = pdev;
1771 	if (fbdev_panel != NULL)
1772 		omapfb_do_probe(fbdev_pdev, fbdev_panel);
1773 	return 0;
1774 }
1775 
1776 void omapfb_register_panel(struct lcd_panel *panel)
1777 {
1778 	BUG_ON(fbdev_panel != NULL);
1779 
1780 	fbdev_panel = panel;
1781 	if (fbdev_pdev != NULL)
1782 		omapfb_do_probe(fbdev_pdev, fbdev_panel);
1783 }
1784 EXPORT_SYMBOL_GPL(omapfb_register_panel);
1785 
1786 /* Called when the device is being detached from the driver */
1787 static int omapfb_remove(struct platform_device *pdev)
1788 {
1789 	struct omapfb_device *fbdev = platform_get_drvdata(pdev);
1790 	enum omapfb_state saved_state = fbdev->state;
1791 
1792 	/* FIXME: wait till completion of pending events */
1793 
1794 	fbdev->state = OMAPFB_DISABLED;
1795 	omapfb_free_resources(fbdev, saved_state);
1796 
1797 	platform_device_unregister(&omapdss_device);
1798 	fbdev->dssdev = NULL;
1799 
1800 	return 0;
1801 }
1802 
1803 /* PM suspend */
1804 static int omapfb_suspend(struct platform_device *pdev, pm_message_t mesg)
1805 {
1806 	struct omapfb_device *fbdev = platform_get_drvdata(pdev);
1807 
1808 	if (fbdev != NULL)
1809 		omapfb_blank(FB_BLANK_POWERDOWN, fbdev->fb_info[0]);
1810 	return 0;
1811 }
1812 
1813 /* PM resume */
1814 static int omapfb_resume(struct platform_device *pdev)
1815 {
1816 	struct omapfb_device *fbdev = platform_get_drvdata(pdev);
1817 
1818 	if (fbdev != NULL)
1819 		omapfb_blank(FB_BLANK_UNBLANK, fbdev->fb_info[0]);
1820 	return 0;
1821 }
1822 
1823 static struct platform_driver omapfb_driver = {
1824 	.probe		= omapfb_probe,
1825 	.remove		= omapfb_remove,
1826 	.suspend	= omapfb_suspend,
1827 	.resume		= omapfb_resume,
1828 	.driver		= {
1829 		.name	= MODULE_NAME,
1830 	},
1831 };
1832 
1833 #ifndef MODULE
1834 
1835 /* Process kernel command line parameters */
1836 static int __init omapfb_setup(char *options)
1837 {
1838 	char *this_opt = NULL;
1839 	int r = 0;
1840 
1841 	pr_debug("omapfb: options %s\n", options);
1842 
1843 	if (!options || !*options)
1844 		return 0;
1845 
1846 	while (!r && (this_opt = strsep(&options, ",")) != NULL) {
1847 		if (!strncmp(this_opt, "accel", 5))
1848 			def_accel = 1;
1849 		else if (!strncmp(this_opt, "vram:", 5)) {
1850 			char *suffix;
1851 			unsigned long vram;
1852 			vram = (simple_strtoul(this_opt + 5, &suffix, 0));
1853 			switch (suffix[0]) {
1854 			case '\0':
1855 				break;
1856 			case 'm':
1857 			case 'M':
1858 				vram *= 1024;
1859 				fallthrough;
1860 			case 'k':
1861 			case 'K':
1862 				vram *= 1024;
1863 				break;
1864 			default:
1865 				pr_debug("omapfb: invalid vram suffix %c\n",
1866 					 suffix[0]);
1867 				r = -1;
1868 			}
1869 			def_vram[def_vram_cnt++] = vram;
1870 		}
1871 		else if (!strncmp(this_opt, "vxres:", 6))
1872 			def_vxres = simple_strtoul(this_opt + 6, NULL, 0);
1873 		else if (!strncmp(this_opt, "vyres:", 6))
1874 			def_vyres = simple_strtoul(this_opt + 6, NULL, 0);
1875 		else if (!strncmp(this_opt, "rotate:", 7))
1876 			def_rotate = (simple_strtoul(this_opt + 7, NULL, 0));
1877 		else if (!strncmp(this_opt, "mirror:", 7))
1878 			def_mirror = (simple_strtoul(this_opt + 7, NULL, 0));
1879 		else if (!strncmp(this_opt, "manual_update", 13))
1880 			manual_update = 1;
1881 		else {
1882 			pr_debug("omapfb: invalid option\n");
1883 			r = -1;
1884 		}
1885 	}
1886 
1887 	return r;
1888 }
1889 
1890 #endif
1891 
1892 /* Register both the driver and the device */
1893 static int __init omapfb_init(void)
1894 {
1895 #ifndef MODULE
1896 	char *option;
1897 
1898 	if (fb_get_options("omapfb", &option))
1899 		return -ENODEV;
1900 	omapfb_setup(option);
1901 #endif
1902 	/* Register the driver with LDM */
1903 	if (platform_driver_register(&omapfb_driver)) {
1904 		pr_debug("failed to register omapfb driver\n");
1905 		return -ENODEV;
1906 	}
1907 
1908 	return 0;
1909 }
1910 
1911 static void __exit omapfb_cleanup(void)
1912 {
1913 	platform_driver_unregister(&omapfb_driver);
1914 }
1915 
1916 module_param_named(accel, def_accel, uint, 0664);
1917 module_param_array_named(vram, def_vram, ulong, &def_vram_cnt, 0664);
1918 module_param_named(vxres, def_vxres, long, 0664);
1919 module_param_named(vyres, def_vyres, long, 0664);
1920 module_param_named(rotate, def_rotate, uint, 0664);
1921 module_param_named(mirror, def_mirror, uint, 0664);
1922 module_param_named(manual_update, manual_update, bool, 0664);
1923 
1924 module_init(omapfb_init);
1925 module_exit(omapfb_cleanup);
1926 
1927 MODULE_DESCRIPTION("TI OMAP framebuffer driver");
1928 MODULE_AUTHOR("Imre Deak <imre.deak@nokia.com>");
1929 MODULE_LICENSE("GPL");
1930