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
29static unsigned int	def_accel;
30static unsigned long	def_vram[OMAPFB_PLANE_NUM];
31static unsigned int	def_vram_cnt;
32static unsigned long	def_vxres;
33static unsigned long	def_vyres;
34static unsigned int	def_rotate;
35static unsigned int	def_mirror;
36
37static bool	manual_update = IS_BUILTIN(CONFIG_FB_OMAP_MANUAL_UPDATE);
38
39static struct platform_device	*fbdev_pdev;
40static struct lcd_panel		*fbdev_panel;
41static struct omapfb_device	*omapfb_dev;
42
43struct caps_table_struct {
44	unsigned long flag;
45	const char *name;
46};
47
48static 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
60static 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
72static void omapdss_release(struct device *dev)
73{
74}
75
76/* dummy device for clocks */
77static 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 */
90extern struct lcd_ctrl hwa742_ctrl;
91
92static 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
101extern struct lcd_ctrl_extif omap1_ext_if;
102#endif
103
104static void omapfb_rqueue_lock(struct omapfb_device *fbdev)
105{
106	mutex_lock(&fbdev->rqueue_mutex);
107}
108
109static 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 */
125static 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
169static void ctrl_cleanup(struct omapfb_device *fbdev)
170{
171	fbdev->ctrl->cleanup();
172}
173
174/* Must be called with fbdev->rqueue_mutex held. */
175static 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 */
216static int omapfb_open(struct fb_info *info, int user)
217{
218	return 0;
219}
220
221static 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. */
225static 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 */
235static 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
278static 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
284static 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
308static int omapfb_update_full_screen(struct fb_info *fbi);
309
310static 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
351static 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 */
366static 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
409static 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 */
462static 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;
548		var->red.length		= 4;
549		var->red.msb_right	= 0;
550		var->green.offset	= 4;
551		var->green.length	= 4;
552		var->green.msb_right	= 0;
553		var->blue.offset	= 0;
554		var->blue.length	= 4;
555		var->blue.msb_right	= 0;
556	} else {
557		var->red.offset		= 11;
558		var->red.length		= 5;
559		var->red.msb_right	= 0;
560		var->green.offset	= 5;
561		var->green.length	= 6;
562		var->green.msb_right	= 0;
563		var->blue.offset	= 0;
564		var->blue.length	= 5;
565		var->blue.msb_right	= 0;
566	}
567
568	var->height		= -1;
569	var->width		= -1;
570	var->grayscale		= 0;
571
572	/* pixclock in ps, the rest in pixclock */
573	var->pixclock		= 10000000 / (panel->pixel_clock / 100);
574	var->left_margin	= panel->hfp;
575	var->right_margin	= panel->hbp;
576	var->upper_margin	= panel->vfp;
577	var->lower_margin	= panel->vbp;
578	var->hsync_len		= panel->hsw;
579	var->vsync_len		= panel->vsw;
580
581	/* TODO: get these from panel->config */
582	var->vmode		= FB_VMODE_NONINTERLACED;
583	var->sync		= 0;
584
585	return 0;
586}
587
588
589/*
590 * Set new x,y offsets in the virtual display for the visible area and switch
591 * to the new mode.
592 */
593static int omapfb_pan_display(struct fb_var_screeninfo *var,
594			       struct fb_info *fbi)
595{
596	struct omapfb_plane_struct *plane = fbi->par;
597	struct omapfb_device *fbdev = plane->fbdev;
598	int r = 0;
599
600	omapfb_rqueue_lock(fbdev);
601	if (var->xoffset != fbi->var.xoffset ||
602	    var->yoffset != fbi->var.yoffset) {
603		struct fb_var_screeninfo *new_var = &fbdev->new_var;
604
605		memcpy(new_var, &fbi->var, sizeof(*new_var));
606		new_var->xoffset = var->xoffset;
607		new_var->yoffset = var->yoffset;
608		if (set_fb_var(fbi, new_var))
609			r = -EINVAL;
610		else {
611			memcpy(&fbi->var, new_var, sizeof(*new_var));
612			ctrl_change_mode(fbi);
613		}
614	}
615	omapfb_rqueue_unlock(fbdev);
616
617	return r;
618}
619
620/* Set mirror to vertical axis and switch to the new mode. */
621static int omapfb_mirror(struct fb_info *fbi, int mirror)
622{
623	struct omapfb_plane_struct *plane = fbi->par;
624	struct omapfb_device *fbdev = plane->fbdev;
625	int r = 0;
626
627	omapfb_rqueue_lock(fbdev);
628	mirror = mirror ? 1 : 0;
629	if (cpu_is_omap15xx())
630		r = -EINVAL;
631	else if (mirror != plane->info.mirror) {
632		plane->info.mirror = mirror;
633		r = ctrl_change_mode(fbi);
634	}
635	omapfb_rqueue_unlock(fbdev);
636
637	return r;
638}
639
640/*
641 * Check values in var, try to adjust them in case of out of bound values if
642 * possible, or return error.
643 */
644static int omapfb_check_var(struct fb_var_screeninfo *var, struct fb_info *fbi)
645{
646	struct omapfb_plane_struct *plane = fbi->par;
647	struct omapfb_device *fbdev = plane->fbdev;
648	int r;
649
650	omapfb_rqueue_lock(fbdev);
651	if (fbdev->ctrl->sync != NULL)
652		fbdev->ctrl->sync();
653	r = set_fb_var(fbi, var);
654	omapfb_rqueue_unlock(fbdev);
655
656	return r;
657}
658
659/*
660 * Switch to a new mode. The parameters for it has been check already by
661 * omapfb_check_var.
662 */
663static int omapfb_set_par(struct fb_info *fbi)
664{
665	struct omapfb_plane_struct *plane = fbi->par;
666	struct omapfb_device *fbdev = plane->fbdev;
667	int r = 0;
668
669	omapfb_rqueue_lock(fbdev);
670	set_fb_fix(fbi, 0);
671	r = ctrl_change_mode(fbi);
672	omapfb_rqueue_unlock(fbdev);
673
674	return r;
675}
676
677static int omapfb_update_window_async(struct fb_info *fbi,
678				struct omapfb_update_window *win,
679				void (*callback)(void *),
680				void *callback_data)
681{
682	int xres, yres;
683	struct omapfb_plane_struct *plane = fbi->par;
684	struct omapfb_device *fbdev = plane->fbdev;
685	struct fb_var_screeninfo *var = &fbi->var;
686
687	switch (var->rotate) {
688	case 0:
689	case 180:
690		xres = fbdev->panel->x_res;
691		yres = fbdev->panel->y_res;
692		break;
693	case 90:
694	case 270:
695		xres = fbdev->panel->y_res;
696		yres = fbdev->panel->x_res;
697		break;
698	default:
699		return -EINVAL;
700	}
701
702	if (win->x >= xres || win->y >= yres ||
703	    win->out_x > xres || win->out_y > yres)
704		return -EINVAL;
705
706	if (!fbdev->ctrl->update_window ||
707	    fbdev->ctrl->get_update_mode() != OMAPFB_MANUAL_UPDATE)
708		return -ENODEV;
709
710	if (win->x + win->width > xres)
711		win->width = xres - win->x;
712	if (win->y + win->height > yres)
713		win->height = yres - win->y;
714	if (win->out_x + win->out_width > xres)
715		win->out_width = xres - win->out_x;
716	if (win->out_y + win->out_height > yres)
717		win->out_height = yres - win->out_y;
718	if (!win->width || !win->height || !win->out_width || !win->out_height)
719		return 0;
720
721	return fbdev->ctrl->update_window(fbi, win, callback, callback_data);
722}
723
724static int omapfb_update_win(struct fb_info *fbi,
725				struct omapfb_update_window *win)
726{
727	struct omapfb_plane_struct *plane = fbi->par;
728	int ret;
729
730	omapfb_rqueue_lock(plane->fbdev);
731	ret = omapfb_update_window_async(fbi, win, NULL, NULL);
732	omapfb_rqueue_unlock(plane->fbdev);
733
734	return ret;
735}
736
737static int omapfb_update_full_screen(struct fb_info *fbi)
738{
739	struct omapfb_plane_struct *plane = fbi->par;
740	struct omapfb_device *fbdev = plane->fbdev;
741	struct omapfb_update_window win;
742	int r;
743
744	if (!fbdev->ctrl->update_window ||
745	    fbdev->ctrl->get_update_mode() != OMAPFB_MANUAL_UPDATE)
746		return -ENODEV;
747
748	win.x = 0;
749	win.y = 0;
750	win.width = fbi->var.xres;
751	win.height = fbi->var.yres;
752	win.out_x = 0;
753	win.out_y = 0;
754	win.out_width = fbi->var.xres;
755	win.out_height = fbi->var.yres;
756	win.format = 0;
757
758	omapfb_rqueue_lock(fbdev);
759	r = fbdev->ctrl->update_window(fbi, &win, NULL, NULL);
760	omapfb_rqueue_unlock(fbdev);
761
762	return r;
763}
764
765static int omapfb_setup_plane(struct fb_info *fbi, struct omapfb_plane_info *pi)
766{
767	struct omapfb_plane_struct *plane = fbi->par;
768	struct omapfb_device *fbdev = plane->fbdev;
769	struct lcd_panel *panel = fbdev->panel;
770	struct omapfb_plane_info old_info;
771	int r = 0;
772
773	if (pi->pos_x + pi->out_width > panel->x_res ||
774	    pi->pos_y + pi->out_height > panel->y_res)
775		return -EINVAL;
776
777	omapfb_rqueue_lock(fbdev);
778	if (pi->enabled && !fbdev->mem_desc.region[plane->idx].size) {
779		/*
780		 * This plane's memory was freed, can't enable it
781		 * until it's reallocated.
782		 */
783		r = -EINVAL;
784		goto out;
785	}
786	old_info = plane->info;
787	plane->info = *pi;
788	if (pi->enabled) {
789		r = ctrl_change_mode(fbi);
790		if (r < 0) {
791			plane->info = old_info;
792			goto out;
793		}
794	}
795	r = fbdev->ctrl->enable_plane(plane->idx, pi->enabled);
796	if (r < 0) {
797		plane->info = old_info;
798		goto out;
799	}
800out:
801	omapfb_rqueue_unlock(fbdev);
802	return r;
803}
804
805static int omapfb_query_plane(struct fb_info *fbi, struct omapfb_plane_info *pi)
806{
807	struct omapfb_plane_struct *plane = fbi->par;
808
809	*pi = plane->info;
810	return 0;
811}
812
813static int omapfb_setup_mem(struct fb_info *fbi, struct omapfb_mem_info *mi)
814{
815	struct omapfb_plane_struct *plane = fbi->par;
816	struct omapfb_device *fbdev = plane->fbdev;
817	struct omapfb_mem_region *rg = &fbdev->mem_desc.region[plane->idx];
818	size_t size;
819	int r = 0;
820
821	if (fbdev->ctrl->setup_mem == NULL)
822		return -ENODEV;
823	if (mi->type != OMAPFB_MEMTYPE_SDRAM)
824		return -EINVAL;
825
826	size = PAGE_ALIGN(mi->size);
827	omapfb_rqueue_lock(fbdev);
828	if (plane->info.enabled) {
829		r = -EBUSY;
830		goto out;
831	}
832	if (rg->size != size || rg->type != mi->type) {
833		struct fb_var_screeninfo *new_var = &fbdev->new_var;
834		unsigned long old_size = rg->size;
835		u8	      old_type = rg->type;
836		unsigned long paddr;
837
838		rg->size = size;
839		rg->type = mi->type;
840		/*
841		 * size == 0 is a special case, for which we
842		 * don't check / adjust the screen parameters.
843		 * This isn't a problem since the plane can't
844		 * be reenabled unless its size is > 0.
845		 */
846		if (old_size != size && size) {
847			if (size) {
848				memcpy(new_var, &fbi->var, sizeof(*new_var));
849				r = set_fb_var(fbi, new_var);
850				if (r < 0)
851					goto out;
852			}
853		}
854
855		if (fbdev->ctrl->sync)
856			fbdev->ctrl->sync();
857		r = fbdev->ctrl->setup_mem(plane->idx, size, mi->type, &paddr);
858		if (r < 0) {
859			/* Revert changes. */
860			rg->size = old_size;
861			rg->type = old_type;
862			goto out;
863		}
864		rg->paddr = paddr;
865
866		if (old_size != size) {
867			if (size) {
868				memcpy(&fbi->var, new_var, sizeof(fbi->var));
869				set_fb_fix(fbi, 0);
870			} else {
871				/*
872				 * Set these explicitly to indicate that the
873				 * plane memory is dealloce'd, the other
874				 * screen parameters in var / fix are invalid.
875				 */
876				mutex_lock(&fbi->mm_lock);
877				fbi->fix.smem_start = 0;
878				fbi->fix.smem_len = 0;
879				mutex_unlock(&fbi->mm_lock);
880			}
881		}
882	}
883out:
884	omapfb_rqueue_unlock(fbdev);
885
886	return r;
887}
888
889static int omapfb_query_mem(struct fb_info *fbi, struct omapfb_mem_info *mi)
890{
891	struct omapfb_plane_struct *plane = fbi->par;
892	struct omapfb_device *fbdev = plane->fbdev;
893	struct omapfb_mem_region *rg;
894
895	rg = &fbdev->mem_desc.region[plane->idx];
896	memset(mi, 0, sizeof(*mi));
897	mi->size = rg->size;
898	mi->type = rg->type;
899
900	return 0;
901}
902
903static int omapfb_set_color_key(struct omapfb_device *fbdev,
904				struct omapfb_color_key *ck)
905{
906	int r;
907
908	if (!fbdev->ctrl->set_color_key)
909		return -ENODEV;
910
911	omapfb_rqueue_lock(fbdev);
912	r = fbdev->ctrl->set_color_key(ck);
913	omapfb_rqueue_unlock(fbdev);
914
915	return r;
916}
917
918static int omapfb_get_color_key(struct omapfb_device *fbdev,
919				struct omapfb_color_key *ck)
920{
921	int r;
922
923	if (!fbdev->ctrl->get_color_key)
924		return -ENODEV;
925
926	omapfb_rqueue_lock(fbdev);
927	r = fbdev->ctrl->get_color_key(ck);
928	omapfb_rqueue_unlock(fbdev);
929
930	return r;
931}
932
933static struct blocking_notifier_head omapfb_client_list[OMAPFB_PLANE_NUM];
934static int notifier_inited;
935
936static void omapfb_init_notifier(void)
937{
938	int i;
939
940	for (i = 0; i < OMAPFB_PLANE_NUM; i++)
941		BLOCKING_INIT_NOTIFIER_HEAD(&omapfb_client_list[i]);
942}
943
944int omapfb_register_client(struct omapfb_notifier_block *omapfb_nb,
945				omapfb_notifier_callback_t callback,
946				void *callback_data)
947{
948	int r;
949
950	if ((unsigned)omapfb_nb->plane_idx >= OMAPFB_PLANE_NUM)
951		return -EINVAL;
952
953	if (!notifier_inited) {
954		omapfb_init_notifier();
955		notifier_inited = 1;
956	}
957
958	omapfb_nb->nb.notifier_call = (int (*)(struct notifier_block *,
959					unsigned long, void *))callback;
960	omapfb_nb->data = callback_data;
961	r = blocking_notifier_chain_register(
962				&omapfb_client_list[omapfb_nb->plane_idx],
963				&omapfb_nb->nb);
964	if (r)
965		return r;
966	if (omapfb_dev != NULL &&
967	    omapfb_dev->ctrl && omapfb_dev->ctrl->bind_client) {
968		omapfb_dev->ctrl->bind_client(omapfb_nb);
969	}
970
971	return 0;
972}
973EXPORT_SYMBOL(omapfb_register_client);
974
975int omapfb_unregister_client(struct omapfb_notifier_block *omapfb_nb)
976{
977	return blocking_notifier_chain_unregister(
978		&omapfb_client_list[omapfb_nb->plane_idx], &omapfb_nb->nb);
979}
980EXPORT_SYMBOL(omapfb_unregister_client);
981
982void omapfb_notify_clients(struct omapfb_device *fbdev, unsigned long event)
983{
984	int i;
985
986	if (!notifier_inited)
987		/* no client registered yet */
988		return;
989
990	for (i = 0; i < OMAPFB_PLANE_NUM; i++)
991		blocking_notifier_call_chain(&omapfb_client_list[i], event,
992				    fbdev->fb_info[i]);
993}
994EXPORT_SYMBOL(omapfb_notify_clients);
995
996static int omapfb_set_update_mode(struct omapfb_device *fbdev,
997				   enum omapfb_update_mode mode)
998{
999	int r;
1000
1001	omapfb_rqueue_lock(fbdev);
1002	r = fbdev->ctrl->set_update_mode(mode);
1003	omapfb_rqueue_unlock(fbdev);
1004
1005	return r;
1006}
1007
1008static enum omapfb_update_mode omapfb_get_update_mode(struct omapfb_device *fbdev)
1009{
1010	int r;
1011
1012	omapfb_rqueue_lock(fbdev);
1013	r = fbdev->ctrl->get_update_mode();
1014	omapfb_rqueue_unlock(fbdev);
1015
1016	return r;
1017}
1018
1019static void omapfb_get_caps(struct omapfb_device *fbdev, int plane,
1020				     struct omapfb_caps *caps)
1021{
1022	memset(caps, 0, sizeof(*caps));
1023	fbdev->ctrl->get_caps(plane, caps);
1024	if (fbdev->panel->get_caps)
1025		caps->ctrl |= fbdev->panel->get_caps(fbdev->panel);
1026}
1027
1028/* For lcd testing */
1029void omapfb_write_first_pixel(struct omapfb_device *fbdev, u16 pixval)
1030{
1031	omapfb_rqueue_lock(fbdev);
1032	*(u16 *)fbdev->mem_desc.region[0].vaddr = pixval;
1033	if (fbdev->ctrl->get_update_mode() == OMAPFB_MANUAL_UPDATE) {
1034		struct omapfb_update_window win;
1035
1036		memset(&win, 0, sizeof(win));
1037		win.width = 2;
1038		win.height = 2;
1039		win.out_width = 2;
1040		win.out_height = 2;
1041		fbdev->ctrl->update_window(fbdev->fb_info[0], &win, NULL, NULL);
1042	}
1043	omapfb_rqueue_unlock(fbdev);
1044}
1045EXPORT_SYMBOL(omapfb_write_first_pixel);
1046
1047/*
1048 * Ioctl interface. Part of the kernel mode frame buffer API is duplicated
1049 * here to be accessible by user mode code.
1050 */
1051static int omapfb_ioctl(struct fb_info *fbi, unsigned int cmd,
1052			unsigned long arg)
1053{
1054	struct omapfb_plane_struct *plane = fbi->par;
1055	struct omapfb_device	*fbdev = plane->fbdev;
1056	const struct fb_ops *ops = fbi->fbops;
1057	union {
1058		struct omapfb_update_window	update_window;
1059		struct omapfb_plane_info	plane_info;
1060		struct omapfb_mem_info		mem_info;
1061		struct omapfb_color_key		color_key;
1062		enum omapfb_update_mode		update_mode;
1063		struct omapfb_caps		caps;
1064		unsigned int		mirror;
1065		int			plane_out;
1066		int			enable_plane;
1067	} p;
1068	int r = 0;
1069
1070	BUG_ON(!ops);
1071	switch (cmd) {
1072	case OMAPFB_MIRROR:
1073		if (get_user(p.mirror, (int __user *)arg))
1074			r = -EFAULT;
1075		else
1076			omapfb_mirror(fbi, p.mirror);
1077		break;
1078	case OMAPFB_SYNC_GFX:
1079		omapfb_sync(fbi);
1080		break;
1081	case OMAPFB_VSYNC:
1082		break;
1083	case OMAPFB_SET_UPDATE_MODE:
1084		if (get_user(p.update_mode, (int __user *)arg))
1085			r = -EFAULT;
1086		else
1087			r = omapfb_set_update_mode(fbdev, p.update_mode);
1088		break;
1089	case OMAPFB_GET_UPDATE_MODE:
1090		p.update_mode = omapfb_get_update_mode(fbdev);
1091		if (put_user(p.update_mode,
1092					(enum omapfb_update_mode __user *)arg))
1093			r = -EFAULT;
1094		break;
1095	case OMAPFB_UPDATE_WINDOW_OLD:
1096		if (copy_from_user(&p.update_window, (void __user *)arg,
1097				   sizeof(struct omapfb_update_window_old)))
1098			r = -EFAULT;
1099		else {
1100			struct omapfb_update_window *u = &p.update_window;
1101			u->out_x = u->x;
1102			u->out_y = u->y;
1103			u->out_width = u->width;
1104			u->out_height = u->height;
1105			memset(u->reserved, 0, sizeof(u->reserved));
1106			r = omapfb_update_win(fbi, u);
1107		}
1108		break;
1109	case OMAPFB_UPDATE_WINDOW:
1110		if (copy_from_user(&p.update_window, (void __user *)arg,
1111				   sizeof(p.update_window)))
1112			r = -EFAULT;
1113		else
1114			r = omapfb_update_win(fbi, &p.update_window);
1115		break;
1116	case OMAPFB_SETUP_PLANE:
1117		if (copy_from_user(&p.plane_info, (void __user *)arg,
1118				   sizeof(p.plane_info)))
1119			r = -EFAULT;
1120		else
1121			r = omapfb_setup_plane(fbi, &p.plane_info);
1122		break;
1123	case OMAPFB_QUERY_PLANE:
1124		if ((r = omapfb_query_plane(fbi, &p.plane_info)) < 0)
1125			break;
1126		if (copy_to_user((void __user *)arg, &p.plane_info,
1127				   sizeof(p.plane_info)))
1128			r = -EFAULT;
1129		break;
1130	case OMAPFB_SETUP_MEM:
1131		if (copy_from_user(&p.mem_info, (void __user *)arg,
1132				   sizeof(p.mem_info)))
1133			r = -EFAULT;
1134		else
1135			r = omapfb_setup_mem(fbi, &p.mem_info);
1136		break;
1137	case OMAPFB_QUERY_MEM:
1138		if ((r = omapfb_query_mem(fbi, &p.mem_info)) < 0)
1139			break;
1140		if (copy_to_user((void __user *)arg, &p.mem_info,
1141				   sizeof(p.mem_info)))
1142			r = -EFAULT;
1143		break;
1144	case OMAPFB_SET_COLOR_KEY:
1145		if (copy_from_user(&p.color_key, (void __user *)arg,
1146				   sizeof(p.color_key)))
1147			r = -EFAULT;
1148		else
1149			r = omapfb_set_color_key(fbdev, &p.color_key);
1150		break;
1151	case OMAPFB_GET_COLOR_KEY:
1152		if ((r = omapfb_get_color_key(fbdev, &p.color_key)) < 0)
1153			break;
1154		if (copy_to_user((void __user *)arg, &p.color_key,
1155				 sizeof(p.color_key)))
1156			r = -EFAULT;
1157		break;
1158	case OMAPFB_GET_CAPS:
1159		omapfb_get_caps(fbdev, plane->idx, &p.caps);
1160		if (copy_to_user((void __user *)arg, &p.caps, sizeof(p.caps)))
1161			r = -EFAULT;
1162		break;
1163	case OMAPFB_LCD_TEST:
1164		{
1165			int test_num;
1166
1167			if (get_user(test_num, (int __user *)arg)) {
1168				r = -EFAULT;
1169				break;
1170			}
1171			if (!fbdev->panel->run_test) {
1172				r = -EINVAL;
1173				break;
1174			}
1175			r = fbdev->panel->run_test(fbdev->panel, test_num);
1176			break;
1177		}
1178	case OMAPFB_CTRL_TEST:
1179		{
1180			int test_num;
1181
1182			if (get_user(test_num, (int __user *)arg)) {
1183				r = -EFAULT;
1184				break;
1185			}
1186			if (!fbdev->ctrl->run_test) {
1187				r = -EINVAL;
1188				break;
1189			}
1190			r = fbdev->ctrl->run_test(test_num);
1191			break;
1192		}
1193	default:
1194		r = -EINVAL;
1195	}
1196
1197	return r;
1198}
1199
1200static int omapfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
1201{
1202	struct omapfb_plane_struct *plane = info->par;
1203	struct omapfb_device *fbdev = plane->fbdev;
1204	int r;
1205
1206	vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
1207
1208	omapfb_rqueue_lock(fbdev);
1209	r = fbdev->ctrl->mmap(info, vma);
1210	omapfb_rqueue_unlock(fbdev);
1211
1212	return r;
1213}
1214
1215/*
1216 * Callback table for the frame buffer framework. Some of these pointers
1217 * will be changed according to the current setting of fb_info->accel_flags.
1218 */
1219static struct fb_ops omapfb_ops = {
1220	.owner		= THIS_MODULE,
1221	FB_DEFAULT_IOMEM_OPS,
1222	.fb_open        = omapfb_open,
1223	.fb_release     = omapfb_release,
1224	.fb_setcolreg	= omapfb_setcolreg,
1225	.fb_setcmap	= omapfb_setcmap,
1226	.fb_blank       = omapfb_blank,
1227	.fb_ioctl	= omapfb_ioctl,
1228	.fb_check_var	= omapfb_check_var,
1229	.fb_set_par	= omapfb_set_par,
1230	.fb_pan_display = omapfb_pan_display,
1231};
1232
1233/*
1234 * ---------------------------------------------------------------------------
1235 * Sysfs interface
1236 * ---------------------------------------------------------------------------
1237 */
1238/* omapfbX sysfs entries */
1239static ssize_t omapfb_show_caps_num(struct device *dev,
1240				    struct device_attribute *attr, char *buf)
1241{
1242	struct omapfb_device *fbdev = dev_get_drvdata(dev);
1243	int plane;
1244	size_t size;
1245	struct omapfb_caps caps;
1246
1247	plane = 0;
1248	size = 0;
1249	while (size < PAGE_SIZE && plane < OMAPFB_PLANE_NUM) {
1250		omapfb_get_caps(fbdev, plane, &caps);
1251		size += scnprintf(&buf[size], PAGE_SIZE - size,
1252			"plane#%d %#010x %#010x %#010x\n",
1253			plane, caps.ctrl, caps.plane_color, caps.wnd_color);
1254		plane++;
1255	}
1256	return size;
1257}
1258
1259static ssize_t omapfb_show_caps_text(struct device *dev,
1260				     struct device_attribute *attr, char *buf)
1261{
1262	struct omapfb_device *fbdev = dev_get_drvdata(dev);
1263	int i;
1264	struct omapfb_caps caps;
1265	int plane;
1266	size_t size;
1267
1268	plane = 0;
1269	size = 0;
1270	while (size < PAGE_SIZE && plane < OMAPFB_PLANE_NUM) {
1271		omapfb_get_caps(fbdev, plane, &caps);
1272		size += scnprintf(&buf[size], PAGE_SIZE - size,
1273				 "plane#%d:\n", plane);
1274		for (i = 0; i < ARRAY_SIZE(ctrl_caps) &&
1275		     size < PAGE_SIZE; i++) {
1276			if (ctrl_caps[i].flag & caps.ctrl)
1277				size += scnprintf(&buf[size], PAGE_SIZE - size,
1278					" %s\n", ctrl_caps[i].name);
1279		}
1280		size += scnprintf(&buf[size], PAGE_SIZE - size,
1281				 " plane colors:\n");
1282		for (i = 0; i < ARRAY_SIZE(color_caps) &&
1283		     size < PAGE_SIZE; i++) {
1284			if (color_caps[i].flag & caps.plane_color)
1285				size += scnprintf(&buf[size], PAGE_SIZE - size,
1286					"  %s\n", color_caps[i].name);
1287		}
1288		size += scnprintf(&buf[size], PAGE_SIZE - size,
1289				 " window colors:\n");
1290		for (i = 0; i < ARRAY_SIZE(color_caps) &&
1291		     size < PAGE_SIZE; i++) {
1292			if (color_caps[i].flag & caps.wnd_color)
1293				size += scnprintf(&buf[size], PAGE_SIZE - size,
1294					"  %s\n", color_caps[i].name);
1295		}
1296
1297		plane++;
1298	}
1299	return size;
1300}
1301
1302static DEVICE_ATTR(caps_num, 0444, omapfb_show_caps_num, NULL);
1303static DEVICE_ATTR(caps_text, 0444, omapfb_show_caps_text, NULL);
1304
1305/* panel sysfs entries */
1306static ssize_t omapfb_show_panel_name(struct device *dev,
1307				      struct device_attribute *attr, char *buf)
1308{
1309	struct omapfb_device *fbdev = dev_get_drvdata(dev);
1310
1311	return sysfs_emit(buf, "%s\n", fbdev->panel->name);
1312}
1313
1314static ssize_t omapfb_show_bklight_level(struct device *dev,
1315					 struct device_attribute *attr,
1316					 char *buf)
1317{
1318	struct omapfb_device *fbdev = dev_get_drvdata(dev);
1319	int r;
1320
1321	if (fbdev->panel->get_bklight_level) {
1322		r = sysfs_emit(buf, "%d\n",
1323			       fbdev->panel->get_bklight_level(fbdev->panel));
1324	} else
1325		r = -ENODEV;
1326	return r;
1327}
1328
1329static ssize_t omapfb_store_bklight_level(struct device *dev,
1330					  struct device_attribute *attr,
1331					  const char *buf, size_t size)
1332{
1333	struct omapfb_device *fbdev = dev_get_drvdata(dev);
1334	int r;
1335
1336	if (fbdev->panel->set_bklight_level) {
1337		unsigned int level;
1338
1339		if (sscanf(buf, "%10d", &level) == 1) {
1340			r = fbdev->panel->set_bklight_level(fbdev->panel,
1341							    level);
1342		} else
1343			r = -EINVAL;
1344	} else
1345		r = -ENODEV;
1346	return r ? r : size;
1347}
1348
1349static ssize_t omapfb_show_bklight_max(struct device *dev,
1350				       struct device_attribute *attr, char *buf)
1351{
1352	struct omapfb_device *fbdev = dev_get_drvdata(dev);
1353	int r;
1354
1355	if (fbdev->panel->get_bklight_level) {
1356		r = sysfs_emit(buf, "%d\n",
1357			       fbdev->panel->get_bklight_max(fbdev->panel));
1358	} else
1359		r = -ENODEV;
1360	return r;
1361}
1362
1363static struct device_attribute dev_attr_panel_name =
1364	__ATTR(name, 0444, omapfb_show_panel_name, NULL);
1365static DEVICE_ATTR(backlight_level, 0664,
1366		   omapfb_show_bklight_level, omapfb_store_bklight_level);
1367static DEVICE_ATTR(backlight_max, 0444, omapfb_show_bklight_max, NULL);
1368
1369static struct attribute *panel_attrs[] = {
1370	&dev_attr_panel_name.attr,
1371	&dev_attr_backlight_level.attr,
1372	&dev_attr_backlight_max.attr,
1373	NULL,
1374};
1375
1376static const struct attribute_group panel_attr_grp = {
1377	.name  = "panel",
1378	.attrs = panel_attrs,
1379};
1380
1381/* ctrl sysfs entries */
1382static ssize_t omapfb_show_ctrl_name(struct device *dev,
1383				     struct device_attribute *attr, char *buf)
1384{
1385	struct omapfb_device *fbdev = dev_get_drvdata(dev);
1386
1387	return sysfs_emit(buf, "%s\n", fbdev->ctrl->name);
1388}
1389
1390static struct device_attribute dev_attr_ctrl_name =
1391	__ATTR(name, 0444, omapfb_show_ctrl_name, NULL);
1392
1393static struct attribute *ctrl_attrs[] = {
1394	&dev_attr_ctrl_name.attr,
1395	NULL,
1396};
1397
1398static const struct attribute_group ctrl_attr_grp = {
1399	.name  = "ctrl",
1400	.attrs = ctrl_attrs,
1401};
1402
1403static int omapfb_register_sysfs(struct omapfb_device *fbdev)
1404{
1405	int r;
1406
1407	if ((r = device_create_file(fbdev->dev, &dev_attr_caps_num)))
1408		goto fail0;
1409
1410	if ((r = device_create_file(fbdev->dev, &dev_attr_caps_text)))
1411		goto fail1;
1412
1413	if ((r = sysfs_create_group(&fbdev->dev->kobj, &panel_attr_grp)))
1414		goto fail2;
1415
1416	if ((r = sysfs_create_group(&fbdev->dev->kobj, &ctrl_attr_grp)))
1417		goto fail3;
1418
1419	return 0;
1420fail3:
1421	sysfs_remove_group(&fbdev->dev->kobj, &panel_attr_grp);
1422fail2:
1423	device_remove_file(fbdev->dev, &dev_attr_caps_text);
1424fail1:
1425	device_remove_file(fbdev->dev, &dev_attr_caps_num);
1426fail0:
1427	dev_err(fbdev->dev, "unable to register sysfs interface\n");
1428	return r;
1429}
1430
1431static void omapfb_unregister_sysfs(struct omapfb_device *fbdev)
1432{
1433	sysfs_remove_group(&fbdev->dev->kobj, &ctrl_attr_grp);
1434	sysfs_remove_group(&fbdev->dev->kobj, &panel_attr_grp);
1435	device_remove_file(fbdev->dev, &dev_attr_caps_num);
1436	device_remove_file(fbdev->dev, &dev_attr_caps_text);
1437}
1438
1439/*
1440 * ---------------------------------------------------------------------------
1441 * LDM callbacks
1442 * ---------------------------------------------------------------------------
1443 */
1444/* Initialize system fb_info object and set the default video mode.
1445 * The frame buffer memory already allocated by lcddma_init
1446 */
1447static int fbinfo_init(struct omapfb_device *fbdev, struct fb_info *info)
1448{
1449	struct fb_var_screeninfo	*var = &info->var;
1450	struct fb_fix_screeninfo	*fix = &info->fix;
1451	int				r = 0;
1452
1453	info->fbops = &omapfb_ops;
1454
1455	strscpy(fix->id, MODULE_NAME, sizeof(fix->id));
1456
1457	info->pseudo_palette = fbdev->pseudo_palette;
1458
1459	var->accel_flags  = def_accel ? FB_ACCELF_TEXT : 0;
1460	var->xres = def_vxres;
1461	var->yres = def_vyres;
1462	var->xres_virtual = def_vxres;
1463	var->yres_virtual = def_vyres;
1464	var->rotate	  = def_rotate;
1465	var->bits_per_pixel = fbdev->panel->bpp;
1466
1467	set_fb_var(info, var);
1468	set_fb_fix(info, 1);
1469
1470	r = fb_alloc_cmap(&info->cmap, 16, 0);
1471	if (r != 0)
1472		dev_err(fbdev->dev, "unable to allocate color map memory\n");
1473
1474	return r;
1475}
1476
1477/* Release the fb_info object */
1478static void fbinfo_cleanup(struct omapfb_device *fbdev, struct fb_info *fbi)
1479{
1480	fb_dealloc_cmap(&fbi->cmap);
1481}
1482
1483static void planes_cleanup(struct omapfb_device *fbdev)
1484{
1485	int i;
1486
1487	for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
1488		if (fbdev->fb_info[i] == NULL)
1489			break;
1490		fbinfo_cleanup(fbdev, fbdev->fb_info[i]);
1491		framebuffer_release(fbdev->fb_info[i]);
1492	}
1493}
1494
1495static int planes_init(struct omapfb_device *fbdev)
1496{
1497	struct fb_info *fbi;
1498	int i;
1499	int r;
1500
1501	for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
1502		struct omapfb_plane_struct *plane;
1503		fbi = framebuffer_alloc(sizeof(struct omapfb_plane_struct),
1504					fbdev->dev);
1505		if (fbi == NULL) {
1506			planes_cleanup(fbdev);
1507			return -ENOMEM;
1508		}
1509		plane = fbi->par;
1510		plane->idx = i;
1511		plane->fbdev = fbdev;
1512		plane->info.mirror = def_mirror;
1513		fbdev->fb_info[i] = fbi;
1514
1515		if ((r = fbinfo_init(fbdev, fbi)) < 0) {
1516			framebuffer_release(fbi);
1517			planes_cleanup(fbdev);
1518			return r;
1519		}
1520		plane->info.out_width = fbi->var.xres;
1521		plane->info.out_height = fbi->var.yres;
1522	}
1523	return 0;
1524}
1525
1526/*
1527 * Free driver resources. Can be called to rollback an aborted initialization
1528 * sequence.
1529 */
1530static void omapfb_free_resources(struct omapfb_device *fbdev, int state)
1531{
1532	int i;
1533
1534	switch (state) {
1535	case OMAPFB_ACTIVE:
1536		for (i = 0; i < fbdev->mem_desc.region_cnt; i++)
1537			unregister_framebuffer(fbdev->fb_info[i]);
1538		fallthrough;
1539	case 7:
1540		omapfb_unregister_sysfs(fbdev);
1541		fallthrough;
1542	case 6:
1543		if (fbdev->panel->disable)
1544			fbdev->panel->disable(fbdev->panel);
1545		fallthrough;
1546	case 5:
1547		omapfb_set_update_mode(fbdev, OMAPFB_UPDATE_DISABLED);
1548		fallthrough;
1549	case 4:
1550		planes_cleanup(fbdev);
1551		fallthrough;
1552	case 3:
1553		ctrl_cleanup(fbdev);
1554		fallthrough;
1555	case 2:
1556		if (fbdev->panel->cleanup)
1557			fbdev->panel->cleanup(fbdev->panel);
1558		fallthrough;
1559	case 1:
1560		dev_set_drvdata(fbdev->dev, NULL);
1561		kfree(fbdev);
1562		break;
1563	case 0:
1564		/* nothing to free */
1565		break;
1566	default:
1567		BUG();
1568	}
1569}
1570
1571static int omapfb_find_ctrl(struct omapfb_device *fbdev)
1572{
1573	struct omapfb_platform_data *conf;
1574	char name[17];
1575	int i;
1576
1577	conf = dev_get_platdata(fbdev->dev);
1578
1579	fbdev->ctrl = NULL;
1580
1581	strscpy(name, conf->lcd.ctrl_name, sizeof(name));
1582
1583	if (strcmp(name, "internal") == 0) {
1584		fbdev->ctrl = fbdev->int_ctrl;
1585		return 0;
1586	}
1587
1588	for (i = 0; i < ARRAY_SIZE(ctrls); i++) {
1589		dev_dbg(fbdev->dev, "ctrl %s\n", ctrls[i]->name);
1590		if (strcmp(ctrls[i]->name, name) == 0) {
1591			fbdev->ctrl = ctrls[i];
1592			break;
1593		}
1594	}
1595
1596	if (fbdev->ctrl == NULL) {
1597		dev_dbg(fbdev->dev, "ctrl %s not supported\n", name);
1598		return -1;
1599	}
1600
1601	return 0;
1602}
1603
1604/*
1605 * Called by LDM binding to probe and attach a new device.
1606 * Initialization sequence:
1607 *   1. allocate system omapfb_device structure
1608 *   2. select controller type according to platform configuration
1609 *      init LCD panel
1610 *   3. init LCD controller and LCD DMA
1611 *   4. init system fb_info structure for all planes
1612 *   5. setup video mode for first plane and enable it
1613 *   6. enable LCD panel
1614 *   7. register sysfs attributes
1615 *   OMAPFB_ACTIVE: register system fb_info structure for all planes
1616 */
1617static int omapfb_do_probe(struct platform_device *pdev,
1618				struct lcd_panel *panel)
1619{
1620	struct omapfb_device	*fbdev = NULL;
1621	int			init_state;
1622	unsigned long		phz, hhz, vhz;
1623	unsigned long		vram;
1624	int			i;
1625	int			r = 0;
1626
1627	init_state = 0;
1628
1629	if (pdev->num_resources != 2) {
1630		dev_err(&pdev->dev, "probed for an unknown device\n");
1631		r = -ENODEV;
1632		goto cleanup;
1633	}
1634
1635	if (dev_get_platdata(&pdev->dev) == NULL) {
1636		dev_err(&pdev->dev, "missing platform data\n");
1637		r = -ENOENT;
1638		goto cleanup;
1639	}
1640
1641	fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
1642	if (fbdev == NULL) {
1643		dev_err(&pdev->dev,
1644			"unable to allocate memory for device info\n");
1645		r = -ENOMEM;
1646		goto cleanup;
1647	}
1648
1649	r = platform_get_irq(pdev, 0);
1650	if (r < 0)
1651		goto cleanup;
1652	fbdev->int_irq = r;
1653
1654	r = platform_get_irq(pdev, 1);
1655	if (r < 0)
1656		goto cleanup;
1657	fbdev->ext_irq = r;
1658
1659	init_state++;
1660
1661	fbdev->dev = &pdev->dev;
1662	fbdev->panel = panel;
1663	fbdev->dssdev = &omapdss_device;
1664	platform_set_drvdata(pdev, fbdev);
1665
1666	mutex_init(&fbdev->rqueue_mutex);
1667
1668	fbdev->int_ctrl = &omap1_int_ctrl;
1669#ifdef CONFIG_FB_OMAP_LCDC_EXTERNAL
1670	fbdev->ext_if = &omap1_ext_if;
1671#endif
1672	if (omapfb_find_ctrl(fbdev) < 0) {
1673		dev_err(fbdev->dev,
1674			"LCD controller not found, board not supported\n");
1675		r = -ENODEV;
1676		goto cleanup;
1677	}
1678
1679	if (fbdev->panel->init) {
1680		r = fbdev->panel->init(fbdev->panel, fbdev);
1681		if (r)
1682			goto cleanup;
1683	}
1684
1685	pr_info("omapfb: configured for panel %s\n", fbdev->panel->name);
1686
1687	def_vxres = def_vxres ? def_vxres : fbdev->panel->x_res;
1688	def_vyres = def_vyres ? def_vyres : fbdev->panel->y_res;
1689
1690	init_state++;
1691
1692	r = ctrl_init(fbdev);
1693	if (r)
1694		goto cleanup;
1695	if (fbdev->ctrl->mmap != NULL)
1696		omapfb_ops.fb_mmap = omapfb_mmap;
1697	init_state++;
1698
1699	r = planes_init(fbdev);
1700	if (r)
1701		goto cleanup;
1702	init_state++;
1703
1704#ifdef CONFIG_FB_OMAP_DMA_TUNE
1705	/* Set DMA priority for EMIFF access to highest */
1706	omap_set_dma_priority(0, OMAP_DMA_PORT_EMIFF, 15);
1707#endif
1708
1709	r = ctrl_change_mode(fbdev->fb_info[0]);
1710	if (r) {
1711		dev_err(fbdev->dev, "mode setting failed\n");
1712		goto cleanup;
1713	}
1714
1715	/* GFX plane is enabled by default */
1716	r = fbdev->ctrl->enable_plane(OMAPFB_PLANE_GFX, 1);
1717	if (r)
1718		goto cleanup;
1719
1720	omapfb_set_update_mode(fbdev, manual_update ?
1721				   OMAPFB_MANUAL_UPDATE : OMAPFB_AUTO_UPDATE);
1722	init_state++;
1723
1724	if (fbdev->panel->enable) {
1725		r = fbdev->panel->enable(fbdev->panel);
1726		if (r)
1727			goto cleanup;
1728	}
1729	init_state++;
1730
1731	r = omapfb_register_sysfs(fbdev);
1732	if (r)
1733		goto cleanup;
1734	init_state++;
1735
1736	vram = 0;
1737	for (i = 0; i < fbdev->mem_desc.region_cnt; i++) {
1738		r = register_framebuffer(fbdev->fb_info[i]);
1739		if (r != 0) {
1740			dev_err(fbdev->dev,
1741				"registering framebuffer %d failed\n", i);
1742			goto cleanup;
1743		}
1744		vram += fbdev->mem_desc.region[i].size;
1745	}
1746
1747	fbdev->state = OMAPFB_ACTIVE;
1748
1749	panel = fbdev->panel;
1750	phz = panel->pixel_clock * 1000;
1751	hhz = phz * 10 / (panel->hfp + panel->x_res + panel->hbp + panel->hsw);
1752	vhz = hhz / (panel->vfp + panel->y_res + panel->vbp + panel->vsw);
1753
1754	omapfb_dev = fbdev;
1755
1756	pr_info("omapfb: Framebuffer initialized. Total vram %lu planes %d\n",
1757			vram, fbdev->mem_desc.region_cnt);
1758	pr_info("omapfb: Pixclock %lu kHz hfreq %lu.%lu kHz "
1759			"vfreq %lu.%lu Hz\n",
1760			phz / 1000, hhz / 10000, hhz % 10, vhz / 10, vhz % 10);
1761
1762	return 0;
1763
1764cleanup:
1765	omapfb_free_resources(fbdev, init_state);
1766
1767	return r;
1768}
1769
1770static int omapfb_probe(struct platform_device *pdev)
1771{
1772	int r;
1773
1774	BUG_ON(fbdev_pdev != NULL);
1775
1776	r = platform_device_register(&omapdss_device);
1777	if (r) {
1778		dev_err(&pdev->dev, "can't register omapdss device\n");
1779		return r;
1780	}
1781
1782	/* Delay actual initialization until the LCD is registered */
1783	fbdev_pdev = pdev;
1784	if (fbdev_panel != NULL)
1785		omapfb_do_probe(fbdev_pdev, fbdev_panel);
1786	return 0;
1787}
1788
1789void omapfb_register_panel(struct lcd_panel *panel)
1790{
1791	BUG_ON(fbdev_panel != NULL);
1792
1793	fbdev_panel = panel;
1794	if (fbdev_pdev != NULL)
1795		omapfb_do_probe(fbdev_pdev, fbdev_panel);
1796}
1797EXPORT_SYMBOL_GPL(omapfb_register_panel);
1798
1799/* Called when the device is being detached from the driver */
1800static void omapfb_remove(struct platform_device *pdev)
1801{
1802	struct omapfb_device *fbdev = platform_get_drvdata(pdev);
1803	enum omapfb_state saved_state = fbdev->state;
1804
1805	/* FIXME: wait till completion of pending events */
1806
1807	fbdev->state = OMAPFB_DISABLED;
1808	omapfb_free_resources(fbdev, saved_state);
1809
1810	platform_device_unregister(&omapdss_device);
1811	fbdev->dssdev = NULL;
1812}
1813
1814/* PM suspend */
1815static int omapfb_suspend(struct platform_device *pdev, pm_message_t mesg)
1816{
1817	struct omapfb_device *fbdev = platform_get_drvdata(pdev);
1818
1819	if (fbdev != NULL)
1820		omapfb_blank(FB_BLANK_POWERDOWN, fbdev->fb_info[0]);
1821	return 0;
1822}
1823
1824/* PM resume */
1825static int omapfb_resume(struct platform_device *pdev)
1826{
1827	struct omapfb_device *fbdev = platform_get_drvdata(pdev);
1828
1829	if (fbdev != NULL)
1830		omapfb_blank(FB_BLANK_UNBLANK, fbdev->fb_info[0]);
1831	return 0;
1832}
1833
1834static struct platform_driver omapfb_driver = {
1835	.probe		= omapfb_probe,
1836	.remove_new	= omapfb_remove,
1837	.suspend	= omapfb_suspend,
1838	.resume		= omapfb_resume,
1839	.driver		= {
1840		.name	= MODULE_NAME,
1841	},
1842};
1843
1844#ifndef MODULE
1845
1846/* Process kernel command line parameters */
1847static int __init omapfb_setup(char *options)
1848{
1849	char *this_opt = NULL;
1850	int r = 0;
1851
1852	pr_debug("omapfb: options %s\n", options);
1853
1854	if (!options || !*options)
1855		return 0;
1856
1857	while (!r && (this_opt = strsep(&options, ",")) != NULL) {
1858		if (!strncmp(this_opt, "accel", 5))
1859			def_accel = 1;
1860		else if (!strncmp(this_opt, "vram:", 5)) {
1861			unsigned long long vram;
1862			char *suffix;
1863
1864			vram = memparse(this_opt + 5, &suffix);
1865			switch (suffix[0]) {
1866			case '\0':
1867				break;
1868			default:
1869				pr_debug("omapfb: invalid vram suffix %c\n",
1870					 suffix[0]);
1871				r = -1;
1872			}
1873			def_vram[def_vram_cnt++] = vram;
1874		}
1875		else if (!strncmp(this_opt, "vxres:", 6))
1876			def_vxres = simple_strtoul(this_opt + 6, NULL, 0);
1877		else if (!strncmp(this_opt, "vyres:", 6))
1878			def_vyres = simple_strtoul(this_opt + 6, NULL, 0);
1879		else if (!strncmp(this_opt, "rotate:", 7))
1880			def_rotate = (simple_strtoul(this_opt + 7, NULL, 0));
1881		else if (!strncmp(this_opt, "mirror:", 7))
1882			def_mirror = (simple_strtoul(this_opt + 7, NULL, 0));
1883		else if (!strncmp(this_opt, "manual_update", 13))
1884			manual_update = 1;
1885		else {
1886			pr_debug("omapfb: invalid option\n");
1887			r = -1;
1888		}
1889	}
1890
1891	return r;
1892}
1893
1894#endif
1895
1896/* Register both the driver and the device */
1897static int __init omapfb_init(void)
1898{
1899#ifndef MODULE
1900	char *option;
1901
1902	if (fb_get_options("omapfb", &option))
1903		return -ENODEV;
1904	omapfb_setup(option);
1905#endif
1906	/* Register the driver with LDM */
1907	if (platform_driver_register(&omapfb_driver)) {
1908		pr_debug("failed to register omapfb driver\n");
1909		return -ENODEV;
1910	}
1911
1912	return 0;
1913}
1914
1915static void __exit omapfb_cleanup(void)
1916{
1917	platform_driver_unregister(&omapfb_driver);
1918}
1919
1920module_param_named(accel, def_accel, uint, 0664);
1921module_param_array_named(vram, def_vram, ulong, &def_vram_cnt, 0664);
1922module_param_named(vxres, def_vxres, long, 0664);
1923module_param_named(vyres, def_vyres, long, 0664);
1924module_param_named(rotate, def_rotate, uint, 0664);
1925module_param_named(mirror, def_mirror, uint, 0664);
1926module_param_named(manual_update, manual_update, bool, 0664);
1927
1928module_init(omapfb_init);
1929module_exit(omapfb_cleanup);
1930
1931MODULE_DESCRIPTION("TI OMAP framebuffer driver");
1932MODULE_AUTHOR("Imre Deak <imre.deak@nokia.com>");
1933MODULE_LICENSE("GPL");
1934