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