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