2 * linux/drivers/video/omap2/omapfb-ioctl.c
4 * Copyright (C) 2008 Nokia Corporation
5 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
7 * Some code and ideas taken from drivers/video/omap/ driver
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published by
12 * the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <linux/device.h>
25 #include <linux/uaccess.h>
26 #include <linux/platform_device.h>
28 #include <linux/omapfb.h>
29 #include <linux/vmalloc.h>
30 #include <linux/export.h>
31 #include <linux/sizes.h>
33 #include <video/omapfb_dss.h>
34 #include <video/omapvrfb.h>
38 static u8
get_mem_idx(struct omapfb_info
*ofbi
)
40 if (ofbi
->id
== ofbi
->region
->id
)
43 return OMAPFB_MEM_IDX_ENABLED
| ofbi
->region
->id
;
46 static struct omapfb2_mem_region
*get_mem_region(struct omapfb_info
*ofbi
,
49 struct omapfb2_device
*fbdev
= ofbi
->fbdev
;
51 if (mem_idx
& OMAPFB_MEM_IDX_ENABLED
)
52 mem_idx
&= OMAPFB_MEM_IDX_MASK
;
56 if (mem_idx
>= fbdev
->num_fbs
)
59 return &fbdev
->regions
[mem_idx
];
62 static int omapfb_setup_plane(struct fb_info
*fbi
, struct omapfb_plane_info
*pi
)
64 struct omapfb_info
*ofbi
= FB2OFB(fbi
);
65 struct omapfb2_device
*fbdev
= ofbi
->fbdev
;
66 struct omap_overlay
*ovl
;
67 struct omap_overlay_info old_info
;
68 struct omapfb2_mem_region
*old_rg
, *new_rg
;
71 DBG("omapfb_setup_plane\n");
73 if (ofbi
->num_overlays
== 0) {
78 /* XXX uses only the first overlay */
79 ovl
= ofbi
->overlays
[0];
81 old_rg
= ofbi
->region
;
82 new_rg
= get_mem_region(ofbi
, pi
->mem_idx
);
88 /* Take the locks in a specific order to keep lockdep happy */
89 if (old_rg
->id
< new_rg
->id
) {
90 omapfb_get_mem_region(old_rg
);
91 omapfb_get_mem_region(new_rg
);
92 } else if (new_rg
->id
< old_rg
->id
) {
93 omapfb_get_mem_region(new_rg
);
94 omapfb_get_mem_region(old_rg
);
96 omapfb_get_mem_region(old_rg
);
98 if (pi
->enabled
&& !new_rg
->size
) {
100 * This plane's memory was freed, can't enable it
101 * until it's reallocated.
107 ovl
->get_overlay_info(ovl
, &old_info
);
109 if (old_rg
!= new_rg
) {
110 ofbi
->region
= new_rg
;
115 r
= ovl
->disable(ovl
);
121 r
= omapfb_setup_overlay(fbi
, ovl
, pi
->pos_x
, pi
->pos_y
,
122 pi
->out_width
, pi
->out_height
);
126 struct omap_overlay_info info
;
128 ovl
->get_overlay_info(ovl
, &info
);
130 info
.pos_x
= pi
->pos_x
;
131 info
.pos_y
= pi
->pos_y
;
132 info
.out_width
= pi
->out_width
;
133 info
.out_height
= pi
->out_height
;
135 r
= ovl
->set_overlay_info(ovl
, &info
);
141 r
= ovl
->manager
->apply(ovl
->manager
);
147 r
= ovl
->enable(ovl
);
152 /* Release the locks in a specific order to keep lockdep happy */
153 if (old_rg
->id
> new_rg
->id
) {
154 omapfb_put_mem_region(old_rg
);
155 omapfb_put_mem_region(new_rg
);
156 } else if (new_rg
->id
> old_rg
->id
) {
157 omapfb_put_mem_region(new_rg
);
158 omapfb_put_mem_region(old_rg
);
160 omapfb_put_mem_region(old_rg
);
165 if (old_rg
!= new_rg
) {
166 ofbi
->region
= old_rg
;
170 ovl
->set_overlay_info(ovl
, &old_info
);
172 /* Release the locks in a specific order to keep lockdep happy */
173 if (old_rg
->id
> new_rg
->id
) {
174 omapfb_put_mem_region(old_rg
);
175 omapfb_put_mem_region(new_rg
);
176 } else if (new_rg
->id
> old_rg
->id
) {
177 omapfb_put_mem_region(new_rg
);
178 omapfb_put_mem_region(old_rg
);
180 omapfb_put_mem_region(old_rg
);
182 dev_err(fbdev
->dev
, "setup_plane failed\n");
187 static int omapfb_query_plane(struct fb_info
*fbi
, struct omapfb_plane_info
*pi
)
189 struct omapfb_info
*ofbi
= FB2OFB(fbi
);
191 if (ofbi
->num_overlays
== 0) {
192 memset(pi
, 0, sizeof(*pi
));
194 struct omap_overlay
*ovl
;
195 struct omap_overlay_info ovli
;
197 ovl
= ofbi
->overlays
[0];
198 ovl
->get_overlay_info(ovl
, &ovli
);
200 pi
->pos_x
= ovli
.pos_x
;
201 pi
->pos_y
= ovli
.pos_y
;
202 pi
->enabled
= ovl
->is_enabled(ovl
);
203 pi
->channel_out
= 0; /* xxx */
205 pi
->mem_idx
= get_mem_idx(ofbi
);
206 pi
->out_width
= ovli
.out_width
;
207 pi
->out_height
= ovli
.out_height
;
213 static int omapfb_setup_mem(struct fb_info
*fbi
, struct omapfb_mem_info
*mi
)
215 struct omapfb_info
*ofbi
= FB2OFB(fbi
);
216 struct omapfb2_device
*fbdev
= ofbi
->fbdev
;
217 struct omap_dss_device
*display
= fb2display(fbi
);
218 struct omapfb2_mem_region
*rg
;
222 if (mi
->type
!= OMAPFB_MEMTYPE_SDRAM
)
225 size
= PAGE_ALIGN(mi
->size
);
227 if (display
&& display
->driver
->sync
)
228 display
->driver
->sync(display
);
232 down_write_nested(&rg
->lock
, rg
->id
);
233 atomic_inc(&rg
->lock_count
);
235 if (rg
->size
== size
&& rg
->type
== mi
->type
)
238 if (atomic_read(&rg
->map_count
)) {
243 for (i
= 0; i
< fbdev
->num_fbs
; i
++) {
244 struct omapfb_info
*ofbi2
= FB2OFB(fbdev
->fbs
[i
]);
247 if (ofbi2
->region
!= rg
)
250 for (j
= 0; j
< ofbi2
->num_overlays
; j
++) {
251 struct omap_overlay
*ovl
;
252 ovl
= ofbi2
->overlays
[j
];
253 if (ovl
->is_enabled(ovl
)) {
260 r
= omapfb_realloc_fbmem(fbi
, size
, mi
->type
);
262 dev_err(fbdev
->dev
, "realloc fbmem failed\n");
267 atomic_dec(&rg
->lock_count
);
273 static int omapfb_query_mem(struct fb_info
*fbi
, struct omapfb_mem_info
*mi
)
275 struct omapfb_info
*ofbi
= FB2OFB(fbi
);
276 struct omapfb2_mem_region
*rg
;
278 rg
= omapfb_get_mem_region(ofbi
->region
);
279 memset(mi
, 0, sizeof(*mi
));
284 omapfb_put_mem_region(rg
);
289 static int omapfb_update_window(struct fb_info
*fbi
,
290 u32 x
, u32 y
, u32 w
, u32 h
)
292 struct omap_dss_device
*display
= fb2display(fbi
);
298 if (w
== 0 || h
== 0)
301 display
->driver
->get_resolution(display
, &dw
, &dh
);
303 if (x
+ w
> dw
|| y
+ h
> dh
)
306 return display
->driver
->update(display
, x
, y
, w
, h
);
309 int omapfb_set_update_mode(struct fb_info
*fbi
,
310 enum omapfb_update_mode mode
)
312 struct omap_dss_device
*display
= fb2display(fbi
);
313 struct omapfb_info
*ofbi
= FB2OFB(fbi
);
314 struct omapfb2_device
*fbdev
= ofbi
->fbdev
;
315 struct omapfb_display_data
*d
;
321 if (mode
!= OMAPFB_AUTO_UPDATE
&& mode
!= OMAPFB_MANUAL_UPDATE
)
326 d
= get_display_data(fbdev
, display
);
328 if (d
->update_mode
== mode
) {
329 omapfb_unlock(fbdev
);
335 if (display
->caps
& OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE
) {
336 if (mode
== OMAPFB_AUTO_UPDATE
)
337 omapfb_start_auto_update(fbdev
, display
);
338 else /* MANUAL_UPDATE */
339 omapfb_stop_auto_update(fbdev
, display
);
341 d
->update_mode
= mode
;
342 } else { /* AUTO_UPDATE */
343 if (mode
== OMAPFB_MANUAL_UPDATE
)
347 omapfb_unlock(fbdev
);
352 int omapfb_get_update_mode(struct fb_info
*fbi
,
353 enum omapfb_update_mode
*mode
)
355 struct omap_dss_device
*display
= fb2display(fbi
);
356 struct omapfb_info
*ofbi
= FB2OFB(fbi
);
357 struct omapfb2_device
*fbdev
= ofbi
->fbdev
;
358 struct omapfb_display_data
*d
;
365 d
= get_display_data(fbdev
, display
);
367 *mode
= d
->update_mode
;
369 omapfb_unlock(fbdev
);
374 /* XXX this color key handling is a hack... */
375 static struct omapfb_color_key omapfb_color_keys
[2];
377 static int _omapfb_set_color_key(struct omap_overlay_manager
*mgr
,
378 struct omapfb_color_key
*ck
)
380 struct omap_overlay_manager_info info
;
381 enum omap_dss_trans_key_type kt
;
384 mgr
->get_manager_info(mgr
, &info
);
386 if (ck
->key_type
== OMAPFB_COLOR_KEY_DISABLED
) {
387 info
.trans_enabled
= false;
388 omapfb_color_keys
[mgr
->id
] = *ck
;
390 r
= mgr
->set_manager_info(mgr
, &info
);
399 switch (ck
->key_type
) {
400 case OMAPFB_COLOR_KEY_GFX_DST
:
401 kt
= OMAP_DSS_COLOR_KEY_GFX_DST
;
403 case OMAPFB_COLOR_KEY_VID_SRC
:
404 kt
= OMAP_DSS_COLOR_KEY_VID_SRC
;
410 info
.default_color
= ck
->background
;
411 info
.trans_key
= ck
->trans_key
;
412 info
.trans_key_type
= kt
;
413 info
.trans_enabled
= true;
415 omapfb_color_keys
[mgr
->id
] = *ck
;
417 r
= mgr
->set_manager_info(mgr
, &info
);
426 static int omapfb_set_color_key(struct fb_info
*fbi
,
427 struct omapfb_color_key
*ck
)
429 struct omapfb_info
*ofbi
= FB2OFB(fbi
);
430 struct omapfb2_device
*fbdev
= ofbi
->fbdev
;
433 struct omap_overlay_manager
*mgr
= NULL
;
437 for (i
= 0; i
< ofbi
->num_overlays
; i
++) {
438 if (ofbi
->overlays
[i
]->manager
) {
439 mgr
= ofbi
->overlays
[i
]->manager
;
449 r
= _omapfb_set_color_key(mgr
, ck
);
451 omapfb_unlock(fbdev
);
456 static int omapfb_get_color_key(struct fb_info
*fbi
,
457 struct omapfb_color_key
*ck
)
459 struct omapfb_info
*ofbi
= FB2OFB(fbi
);
460 struct omapfb2_device
*fbdev
= ofbi
->fbdev
;
461 struct omap_overlay_manager
*mgr
= NULL
;
467 for (i
= 0; i
< ofbi
->num_overlays
; i
++) {
468 if (ofbi
->overlays
[i
]->manager
) {
469 mgr
= ofbi
->overlays
[i
]->manager
;
479 *ck
= omapfb_color_keys
[mgr
->id
];
481 omapfb_unlock(fbdev
);
486 static int omapfb_memory_read(struct fb_info
*fbi
,
487 struct omapfb_memory_read
*mr
)
489 struct omap_dss_device
*display
= fb2display(fbi
);
493 if (!display
|| !display
->driver
->memory_read
)
496 if (!access_ok(VERIFY_WRITE
, mr
->buffer
, mr
->buffer_size
))
499 if (mr
->w
* mr
->h
* 3 > mr
->buffer_size
)
502 buf
= vmalloc(mr
->buffer_size
);
504 DBG("vmalloc failed\n");
508 r
= display
->driver
->memory_read(display
, buf
, mr
->buffer_size
,
509 mr
->x
, mr
->y
, mr
->w
, mr
->h
);
512 if (copy_to_user(mr
->buffer
, buf
, mr
->buffer_size
))
521 static int omapfb_get_ovl_colormode(struct omapfb2_device
*fbdev
,
522 struct omapfb_ovl_colormode
*mode
)
524 int ovl_idx
= mode
->overlay_idx
;
525 int mode_idx
= mode
->mode_idx
;
526 struct omap_overlay
*ovl
;
527 enum omap_color_mode supported_modes
;
528 struct fb_var_screeninfo var
;
531 if (ovl_idx
>= fbdev
->num_overlays
)
533 ovl
= fbdev
->overlays
[ovl_idx
];
534 supported_modes
= ovl
->supported_modes
;
536 mode_idx
= mode
->mode_idx
;
538 for (i
= 0; i
< sizeof(supported_modes
) * 8; i
++) {
539 if (!(supported_modes
& (1 << i
)))
542 * It's possible that the FB doesn't support a mode
543 * that is supported by the overlay, so call the
546 if (dss_mode_to_fb_mode(1 << i
, &var
) < 0)
554 if (i
== sizeof(supported_modes
) * 8)
557 mode
->bits_per_pixel
= var
.bits_per_pixel
;
558 mode
->nonstd
= var
.nonstd
;
560 mode
->green
= var
.green
;
561 mode
->blue
= var
.blue
;
562 mode
->transp
= var
.transp
;
567 static int omapfb_wait_for_go(struct fb_info
*fbi
)
569 struct omapfb_info
*ofbi
= FB2OFB(fbi
);
573 for (i
= 0; i
< ofbi
->num_overlays
; ++i
) {
574 struct omap_overlay
*ovl
= ofbi
->overlays
[i
];
575 r
= ovl
->wait_for_go(ovl
);
583 int omapfb_ioctl(struct fb_info
*fbi
, unsigned int cmd
, unsigned long arg
)
585 struct omapfb_info
*ofbi
= FB2OFB(fbi
);
586 struct omapfb2_device
*fbdev
= ofbi
->fbdev
;
587 struct omap_dss_device
*display
= fb2display(fbi
);
588 struct omap_overlay_manager
*mgr
;
591 struct omapfb_update_window_old uwnd_o
;
592 struct omapfb_update_window uwnd
;
593 struct omapfb_plane_info plane_info
;
594 struct omapfb_caps caps
;
595 struct omapfb_mem_info mem_info
;
596 struct omapfb_color_key color_key
;
597 struct omapfb_ovl_colormode ovl_colormode
;
598 enum omapfb_update_mode update_mode
;
600 struct omapfb_memory_read memory_read
;
601 struct omapfb_vram_info vram_info
;
602 struct omapfb_tearsync_info tearsync_info
;
603 struct omapfb_display_info display_info
;
610 case OMAPFB_SYNC_GFX
:
611 DBG("ioctl SYNC_GFX\n");
612 if (!display
|| !display
->driver
->sync
) {
613 /* DSS1 never returns an error here, so we neither */
618 r
= display
->driver
->sync(display
);
621 case OMAPFB_UPDATE_WINDOW_OLD
:
622 DBG("ioctl UPDATE_WINDOW_OLD\n");
623 if (!display
|| !display
->driver
->update
) {
628 if (copy_from_user(&p
.uwnd_o
,
635 r
= omapfb_update_window(fbi
, p
.uwnd_o
.x
, p
.uwnd_o
.y
,
636 p
.uwnd_o
.width
, p
.uwnd_o
.height
);
639 case OMAPFB_UPDATE_WINDOW
:
640 DBG("ioctl UPDATE_WINDOW\n");
641 if (!display
|| !display
->driver
->update
) {
646 if (copy_from_user(&p
.uwnd
, (void __user
*)arg
,
652 r
= omapfb_update_window(fbi
, p
.uwnd
.x
, p
.uwnd
.y
,
653 p
.uwnd
.width
, p
.uwnd
.height
);
656 case OMAPFB_SETUP_PLANE
:
657 DBG("ioctl SETUP_PLANE\n");
658 if (copy_from_user(&p
.plane_info
, (void __user
*)arg
,
659 sizeof(p
.plane_info
)))
662 r
= omapfb_setup_plane(fbi
, &p
.plane_info
);
665 case OMAPFB_QUERY_PLANE
:
666 DBG("ioctl QUERY_PLANE\n");
667 r
= omapfb_query_plane(fbi
, &p
.plane_info
);
670 if (copy_to_user((void __user
*)arg
, &p
.plane_info
,
671 sizeof(p
.plane_info
)))
675 case OMAPFB_SETUP_MEM
:
676 DBG("ioctl SETUP_MEM\n");
677 if (copy_from_user(&p
.mem_info
, (void __user
*)arg
,
681 r
= omapfb_setup_mem(fbi
, &p
.mem_info
);
684 case OMAPFB_QUERY_MEM
:
685 DBG("ioctl QUERY_MEM\n");
686 r
= omapfb_query_mem(fbi
, &p
.mem_info
);
689 if (copy_to_user((void __user
*)arg
, &p
.mem_info
,
694 case OMAPFB_GET_CAPS
:
695 DBG("ioctl GET_CAPS\n");
701 memset(&p
.caps
, 0, sizeof(p
.caps
));
702 if (display
->caps
& OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE
)
703 p
.caps
.ctrl
|= OMAPFB_CAPS_MANUAL_UPDATE
;
704 if (display
->caps
& OMAP_DSS_DISPLAY_CAP_TEAR_ELIM
)
705 p
.caps
.ctrl
|= OMAPFB_CAPS_TEARSYNC
;
707 if (copy_to_user((void __user
*)arg
, &p
.caps
, sizeof(p
.caps
)))
711 case OMAPFB_GET_OVERLAY_COLORMODE
:
712 DBG("ioctl GET_OVERLAY_COLORMODE\n");
713 if (copy_from_user(&p
.ovl_colormode
, (void __user
*)arg
,
714 sizeof(p
.ovl_colormode
))) {
718 r
= omapfb_get_ovl_colormode(fbdev
, &p
.ovl_colormode
);
721 if (copy_to_user((void __user
*)arg
, &p
.ovl_colormode
,
722 sizeof(p
.ovl_colormode
)))
726 case OMAPFB_SET_UPDATE_MODE
:
727 DBG("ioctl SET_UPDATE_MODE\n");
728 if (get_user(p
.update_mode
, (int __user
*)arg
))
731 r
= omapfb_set_update_mode(fbi
, p
.update_mode
);
734 case OMAPFB_GET_UPDATE_MODE
:
735 DBG("ioctl GET_UPDATE_MODE\n");
736 r
= omapfb_get_update_mode(fbi
, &p
.update_mode
);
739 if (put_user(p
.update_mode
,
740 (enum omapfb_update_mode __user
*)arg
))
744 case OMAPFB_SET_COLOR_KEY
:
745 DBG("ioctl SET_COLOR_KEY\n");
746 if (copy_from_user(&p
.color_key
, (void __user
*)arg
,
747 sizeof(p
.color_key
)))
750 r
= omapfb_set_color_key(fbi
, &p
.color_key
);
753 case OMAPFB_GET_COLOR_KEY
:
754 DBG("ioctl GET_COLOR_KEY\n");
755 r
= omapfb_get_color_key(fbi
, &p
.color_key
);
758 if (copy_to_user((void __user
*)arg
, &p
.color_key
,
759 sizeof(p
.color_key
)))
763 case FBIO_WAITFORVSYNC
:
764 if (get_user(p
.crt
, (__u32 __user
*)arg
)) {
774 case OMAPFB_WAITFORVSYNC
:
775 DBG("ioctl WAITFORVSYNC\n");
782 mgr
= omapdss_find_mgr_from_display(display
);
788 r
= mgr
->wait_for_vsync(mgr
);
791 case OMAPFB_WAITFORGO
:
792 DBG("ioctl WAITFORGO\n");
798 r
= omapfb_wait_for_go(fbi
);
801 /* LCD and CTRL tests do the same thing for backward
803 case OMAPFB_LCD_TEST
:
804 DBG("ioctl LCD_TEST\n");
805 if (get_user(p
.test_num
, (int __user
*)arg
)) {
809 if (!display
|| !display
->driver
->run_test
) {
814 r
= display
->driver
->run_test(display
, p
.test_num
);
818 case OMAPFB_CTRL_TEST
:
819 DBG("ioctl CTRL_TEST\n");
820 if (get_user(p
.test_num
, (int __user
*)arg
)) {
824 if (!display
|| !display
->driver
->run_test
) {
829 r
= display
->driver
->run_test(display
, p
.test_num
);
833 case OMAPFB_MEMORY_READ
:
834 DBG("ioctl MEMORY_READ\n");
836 if (copy_from_user(&p
.memory_read
, (void __user
*)arg
,
837 sizeof(p
.memory_read
))) {
842 r
= omapfb_memory_read(fbi
, &p
.memory_read
);
846 case OMAPFB_GET_VRAM_INFO
: {
847 DBG("ioctl GET_VRAM_INFO\n");
850 * We don't have the ability to get this vram info anymore.
851 * Fill in something that should keep the applications working.
853 p
.vram_info
.total
= SZ_1M
* 64;
854 p
.vram_info
.free
= SZ_1M
* 64;
855 p
.vram_info
.largest_free_block
= SZ_1M
* 64;
857 if (copy_to_user((void __user
*)arg
, &p
.vram_info
,
858 sizeof(p
.vram_info
)))
863 case OMAPFB_SET_TEARSYNC
: {
864 DBG("ioctl SET_TEARSYNC\n");
866 if (copy_from_user(&p
.tearsync_info
, (void __user
*)arg
,
867 sizeof(p
.tearsync_info
))) {
872 if (!display
|| !display
->driver
->enable_te
) {
877 r
= display
->driver
->enable_te(display
,
878 !!p
.tearsync_info
.enabled
);
883 case OMAPFB_GET_DISPLAY_INFO
: {
886 DBG("ioctl GET_DISPLAY_INFO\n");
888 if (display
== NULL
) {
893 display
->driver
->get_resolution(display
, &xres
, &yres
);
895 p
.display_info
.xres
= xres
;
896 p
.display_info
.yres
= yres
;
898 if (display
->driver
->get_dimensions
) {
900 display
->driver
->get_dimensions(display
, &w
, &h
);
901 p
.display_info
.width
= w
;
902 p
.display_info
.height
= h
;
904 p
.display_info
.width
= 0;
905 p
.display_info
.height
= 0;
908 if (copy_to_user((void __user
*)arg
, &p
.display_info
,
909 sizeof(p
.display_info
)))
915 dev_err(fbdev
->dev
, "Unknown ioctl 0x%x\n", cmd
);
920 DBG("ioctl failed: %d\n", r
);