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