blob: c6cf372d22c58b9941051c67028df365e178657f [file] [log] [blame]
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001/*
2 * linux/drivers/video/omap2/omapfb-ioctl.c
3 *
4 * Copyright (C) 2008 Nokia Corporation
5 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
6 *
7 * Some code and ideas taken from drivers/video/omap/ driver
8 * by Imre Deak.
9 *
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.
13 *
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
17 * more details.
18 *
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/>.
21 */
22
23#include <linux/fb.h>
24#include <linux/device.h>
25#include <linux/uaccess.h>
26#include <linux/platform_device.h>
27#include <linux/mm.h>
28#include <linux/omapfb.h>
29#include <linux/vmalloc.h>
Paul Gortmakera8a35932011-07-10 13:20:26 -040030#include <linux/export.h>
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030031
Tomi Valkeinena0b38cc2011-05-11 14:05:07 +030032#include <video/omapdss.h>
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030033#include <plat/vrfb.h>
34#include <plat/vram.h>
35
36#include "omapfb.h"
37
Ville Syrjälä078ff542010-03-17 20:36:51 +020038static u8 get_mem_idx(struct omapfb_info *ofbi)
39{
40 if (ofbi->id == ofbi->region->id)
41 return 0;
42
43 return OMAPFB_MEM_IDX_ENABLED | ofbi->region->id;
44}
45
46static struct omapfb2_mem_region *get_mem_region(struct omapfb_info *ofbi,
47 u8 mem_idx)
48{
49 struct omapfb2_device *fbdev = ofbi->fbdev;
50
51 if (mem_idx & OMAPFB_MEM_IDX_ENABLED)
52 mem_idx &= OMAPFB_MEM_IDX_MASK;
53 else
54 mem_idx = ofbi->id;
55
56 if (mem_idx >= fbdev->num_fbs)
57 return NULL;
58
Ville Syrjälä3d84b652010-03-17 21:42:06 +020059 return &fbdev->regions[mem_idx];
Ville Syrjälä078ff542010-03-17 20:36:51 +020060}
61
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030062static int omapfb_setup_plane(struct fb_info *fbi, struct omapfb_plane_info *pi)
63{
64 struct omapfb_info *ofbi = FB2OFB(fbi);
65 struct omapfb2_device *fbdev = ofbi->fbdev;
66 struct omap_overlay *ovl;
Ville Syrjälä078ff542010-03-17 20:36:51 +020067 struct omap_overlay_info old_info;
68 struct omapfb2_mem_region *old_rg, *new_rg;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030069 int r = 0;
70
71 DBG("omapfb_setup_plane\n");
72
Grazvydas Ignotasb3e68d32012-05-10 14:19:56 +030073 if (ofbi->num_overlays == 0) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030074 r = -EINVAL;
75 goto out;
76 }
77
78 /* XXX uses only the first overlay */
79 ovl = ofbi->overlays[0];
80
Ville Syrjälä3d84b652010-03-17 21:42:06 +020081 old_rg = ofbi->region;
Ville Syrjälä078ff542010-03-17 20:36:51 +020082 new_rg = get_mem_region(ofbi, pi->mem_idx);
83 if (!new_rg) {
84 r = -EINVAL;
Ville Syrjälä3d84b652010-03-17 21:42:06 +020085 goto out;
Ville Syrjälä078ff542010-03-17 20:36:51 +020086 }
87
Ville Syrjälä3d84b652010-03-17 21:42:06 +020088 /* 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);
95 } else
96 omapfb_get_mem_region(old_rg);
97
Ville Syrjälä078ff542010-03-17 20:36:51 +020098 if (pi->enabled && !new_rg->size) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030099 /*
100 * This plane's memory was freed, can't enable it
101 * until it's reallocated.
102 */
103 r = -EINVAL;
Ville Syrjälä3d84b652010-03-17 21:42:06 +0200104 goto put_mem;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300105 }
106
Ville Syrjälä078ff542010-03-17 20:36:51 +0200107 ovl->get_overlay_info(ovl, &old_info);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300108
Ville Syrjälä078ff542010-03-17 20:36:51 +0200109 if (old_rg != new_rg) {
110 ofbi->region = new_rg;
111 set_fb_fix(fbi);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300112 }
113
Tomi Valkeinenaaa874a2011-11-15 16:37:53 +0200114 if (!pi->enabled) {
115 r = ovl->disable(ovl);
116 if (r)
117 goto undo;
118 }
Ville Syrjälä078ff542010-03-17 20:36:51 +0200119
Tomi Valkeinenaaa874a2011-11-15 16:37:53 +0200120 if (pi->enabled) {
Ville Syrjälä078ff542010-03-17 20:36:51 +0200121 r = omapfb_setup_overlay(fbi, ovl, pi->pos_x, pi->pos_y,
122 pi->out_width, pi->out_height);
123 if (r)
124 goto undo;
Ville Syrjälä078ff542010-03-17 20:36:51 +0200125 } else {
126 struct omap_overlay_info info;
127
128 ovl->get_overlay_info(ovl, &info);
129
Ville Syrjälä078ff542010-03-17 20:36:51 +0200130 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;
134
135 r = ovl->set_overlay_info(ovl, &info);
136 if (r)
137 goto undo;
138 }
139
140 if (ovl->manager)
141 ovl->manager->apply(ovl->manager);
142
Tomi Valkeinenaaa874a2011-11-15 16:37:53 +0200143 if (pi->enabled) {
144 r = ovl->enable(ovl);
145 if (r)
146 goto undo;
147 }
148
Ville Syrjälä3d84b652010-03-17 21:42:06 +0200149 /* Release the locks in a specific order to keep lockdep happy */
150 if (old_rg->id > new_rg->id) {
151 omapfb_put_mem_region(old_rg);
152 omapfb_put_mem_region(new_rg);
153 } else if (new_rg->id > old_rg->id) {
154 omapfb_put_mem_region(new_rg);
155 omapfb_put_mem_region(old_rg);
156 } else
157 omapfb_put_mem_region(old_rg);
Ville Syrjälä430571d2010-03-17 20:43:23 +0200158
Ville Syrjälä078ff542010-03-17 20:36:51 +0200159 return 0;
160
161 undo:
162 if (old_rg != new_rg) {
163 ofbi->region = old_rg;
164 set_fb_fix(fbi);
165 }
166
167 ovl->set_overlay_info(ovl, &old_info);
Ville Syrjälä3d84b652010-03-17 21:42:06 +0200168 put_mem:
169 /* Release the locks in a specific order to keep lockdep happy */
170 if (old_rg->id > new_rg->id) {
171 omapfb_put_mem_region(old_rg);
172 omapfb_put_mem_region(new_rg);
173 } else if (new_rg->id > old_rg->id) {
174 omapfb_put_mem_region(new_rg);
175 omapfb_put_mem_region(old_rg);
176 } else
177 omapfb_put_mem_region(old_rg);
Ville Syrjälä078ff542010-03-17 20:36:51 +0200178 out:
179 dev_err(fbdev->dev, "setup_plane failed\n");
180
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300181 return r;
182}
183
184static int omapfb_query_plane(struct fb_info *fbi, struct omapfb_plane_info *pi)
185{
186 struct omapfb_info *ofbi = FB2OFB(fbi);
187
Grazvydas Ignotasb3e68d32012-05-10 14:19:56 +0300188 if (ofbi->num_overlays == 0) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300189 memset(pi, 0, sizeof(*pi));
190 } else {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300191 struct omap_overlay *ovl;
Tomi Valkeinenc1a9feb2011-11-16 14:11:56 +0200192 struct omap_overlay_info ovli;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300193
194 ovl = ofbi->overlays[0];
Tomi Valkeinenc1a9feb2011-11-16 14:11:56 +0200195 ovl->get_overlay_info(ovl, &ovli);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300196
Tomi Valkeinenc1a9feb2011-11-16 14:11:56 +0200197 pi->pos_x = ovli.pos_x;
198 pi->pos_y = ovli.pos_y;
Tomi Valkeinenaaa874a2011-11-15 16:37:53 +0200199 pi->enabled = ovl->is_enabled(ovl);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300200 pi->channel_out = 0; /* xxx */
201 pi->mirror = 0;
Ville Syrjälä078ff542010-03-17 20:36:51 +0200202 pi->mem_idx = get_mem_idx(ofbi);
Tomi Valkeinenc1a9feb2011-11-16 14:11:56 +0200203 pi->out_width = ovli.out_width;
204 pi->out_height = ovli.out_height;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300205 }
206
207 return 0;
208}
209
210static int omapfb_setup_mem(struct fb_info *fbi, struct omapfb_mem_info *mi)
211{
212 struct omapfb_info *ofbi = FB2OFB(fbi);
213 struct omapfb2_device *fbdev = ofbi->fbdev;
214 struct omapfb2_mem_region *rg;
Ville Syrjälä430571d2010-03-17 20:43:23 +0200215 int r = 0, i;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300216 size_t size;
217
Tomi Valkeinen2a803c82011-04-18 13:18:20 +0300218 if (mi->type != OMAPFB_MEMTYPE_SDRAM)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300219 return -EINVAL;
220
221 size = PAGE_ALIGN(mi->size);
222
Ville Syrjälä078ff542010-03-17 20:36:51 +0200223 rg = ofbi->region;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300224
Ville Syrjälä3d84b652010-03-17 21:42:06 +0200225 down_write_nested(&rg->lock, rg->id);
Ville Syrjälä1ceafc02010-03-17 21:28:50 +0200226 atomic_inc(&rg->lock_count);
Ville Syrjälä430571d2010-03-17 20:43:23 +0200227
Grazvydas Ignotasf3fb3bb2012-03-09 02:42:49 +0200228 if (rg->size == size && rg->type == mi->type)
229 goto out;
230
Ville Syrjälä430571d2010-03-17 20:43:23 +0200231 if (atomic_read(&rg->map_count)) {
232 r = -EBUSY;
233 goto out;
234 }
Ville Syrjälä078ff542010-03-17 20:36:51 +0200235
236 for (i = 0; i < fbdev->num_fbs; i++) {
237 struct omapfb_info *ofbi2 = FB2OFB(fbdev->fbs[i]);
238 int j;
239
240 if (ofbi2->region != rg)
241 continue;
242
243 for (j = 0; j < ofbi2->num_overlays; j++) {
Tomi Valkeinenaaa874a2011-11-15 16:37:53 +0200244 struct omap_overlay *ovl;
245 ovl = ofbi2->overlays[j];
246 if (ovl->is_enabled(ovl)) {
Ville Syrjälä078ff542010-03-17 20:36:51 +0200247 r = -EBUSY;
Ville Syrjälä430571d2010-03-17 20:43:23 +0200248 goto out;
Ville Syrjälä078ff542010-03-17 20:36:51 +0200249 }
250 }
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300251 }
252
Grazvydas Ignotasf3fb3bb2012-03-09 02:42:49 +0200253 r = omapfb_realloc_fbmem(fbi, size, mi->type);
254 if (r) {
255 dev_err(fbdev->dev, "realloc fbmem failed\n");
256 goto out;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300257 }
258
Ville Syrjälä430571d2010-03-17 20:43:23 +0200259 out:
Ville Syrjälä1ceafc02010-03-17 21:28:50 +0200260 atomic_dec(&rg->lock_count);
Ville Syrjälä2f642a12010-03-17 20:58:03 +0200261 up_write(&rg->lock);
Ville Syrjälä430571d2010-03-17 20:43:23 +0200262
263 return r;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300264}
265
266static int omapfb_query_mem(struct fb_info *fbi, struct omapfb_mem_info *mi)
267{
268 struct omapfb_info *ofbi = FB2OFB(fbi);
269 struct omapfb2_mem_region *rg;
270
Ville Syrjälä430571d2010-03-17 20:43:23 +0200271 rg = omapfb_get_mem_region(ofbi->region);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300272 memset(mi, 0, sizeof(*mi));
273
274 mi->size = rg->size;
275 mi->type = rg->type;
276
Ville Syrjälä430571d2010-03-17 20:43:23 +0200277 omapfb_put_mem_region(rg);
278
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300279 return 0;
280}
281
282static int omapfb_update_window_nolock(struct fb_info *fbi,
283 u32 x, u32 y, u32 w, u32 h)
284{
285 struct omap_dss_device *display = fb2display(fbi);
286 u16 dw, dh;
287
288 if (!display)
289 return 0;
290
291 if (w == 0 || h == 0)
292 return 0;
293
Tomi Valkeinen96adcec2010-01-11 13:54:33 +0200294 display->driver->get_resolution(display, &dw, &dh);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300295
296 if (x + w > dw || y + h > dh)
297 return -EINVAL;
298
Tomi Valkeinen18946f62010-01-12 14:16:41 +0200299 return display->driver->update(display, x, y, w, h);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300300}
301
302/* This function is exported for SGX driver use */
303int omapfb_update_window(struct fb_info *fbi,
304 u32 x, u32 y, u32 w, u32 h)
305{
306 struct omapfb_info *ofbi = FB2OFB(fbi);
307 struct omapfb2_device *fbdev = ofbi->fbdev;
308 int r;
309
Jani Nikula27b67c92010-03-18 10:32:06 +0100310 if (!lock_fb_info(fbi))
311 return -ENODEV;
Jani Nikula238a4132010-03-18 10:32:05 +0100312 omapfb_lock(fbdev);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300313
314 r = omapfb_update_window_nolock(fbi, x, y, w, h);
315
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300316 omapfb_unlock(fbdev);
Jani Nikula238a4132010-03-18 10:32:05 +0100317 unlock_fb_info(fbi);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300318
319 return r;
320}
321EXPORT_SYMBOL(omapfb_update_window);
322
Tomi Valkeinen27cc2132011-04-30 16:55:12 +0300323int omapfb_set_update_mode(struct fb_info *fbi,
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300324 enum omapfb_update_mode mode)
325{
326 struct omap_dss_device *display = fb2display(fbi);
Tomi Valkeinen27cc2132011-04-30 16:55:12 +0300327 struct omapfb_info *ofbi = FB2OFB(fbi);
328 struct omapfb2_device *fbdev = ofbi->fbdev;
329 struct omapfb_display_data *d;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300330 int r;
331
Tomi Valkeinen446f7bf2010-01-11 16:12:31 +0200332 if (!display)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300333 return -EINVAL;
334
Tomi Valkeinen27cc2132011-04-30 16:55:12 +0300335 if (mode != OMAPFB_AUTO_UPDATE && mode != OMAPFB_MANUAL_UPDATE)
336 return -EINVAL;
337
338 omapfb_lock(fbdev);
339
340 d = get_display_data(fbdev, display);
341
342 if (d->update_mode == mode) {
343 omapfb_unlock(fbdev);
Tomi Valkeinen446f7bf2010-01-11 16:12:31 +0200344 return 0;
345 }
346
Tomi Valkeinen27cc2132011-04-30 16:55:12 +0300347 r = 0;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300348
Tomi Valkeinen27cc2132011-04-30 16:55:12 +0300349 if (display->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) {
350 if (mode == OMAPFB_AUTO_UPDATE)
351 omapfb_start_auto_update(fbdev, display);
352 else /* MANUAL_UPDATE */
353 omapfb_stop_auto_update(fbdev, display);
354
355 d->update_mode = mode;
356 } else { /* AUTO_UPDATE */
357 if (mode == OMAPFB_MANUAL_UPDATE)
358 r = -EINVAL;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300359 }
360
Tomi Valkeinen27cc2132011-04-30 16:55:12 +0300361 omapfb_unlock(fbdev);
362
363 return r;
364}
365
366int omapfb_get_update_mode(struct fb_info *fbi,
367 enum omapfb_update_mode *mode)
368{
369 struct omap_dss_device *display = fb2display(fbi);
370 struct omapfb_info *ofbi = FB2OFB(fbi);
371 struct omapfb2_device *fbdev = ofbi->fbdev;
372 struct omapfb_display_data *d;
373
374 if (!display)
375 return -EINVAL;
376
377 omapfb_lock(fbdev);
378
379 d = get_display_data(fbdev, display);
380
381 *mode = d->update_mode;
382
383 omapfb_unlock(fbdev);
384
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300385 return 0;
386}
387
388/* XXX this color key handling is a hack... */
389static struct omapfb_color_key omapfb_color_keys[2];
390
391static int _omapfb_set_color_key(struct omap_overlay_manager *mgr,
392 struct omapfb_color_key *ck)
393{
394 struct omap_overlay_manager_info info;
395 enum omap_dss_trans_key_type kt;
396 int r;
397
398 mgr->get_manager_info(mgr, &info);
399
400 if (ck->key_type == OMAPFB_COLOR_KEY_DISABLED) {
401 info.trans_enabled = false;
402 omapfb_color_keys[mgr->id] = *ck;
403
404 r = mgr->set_manager_info(mgr, &info);
405 if (r)
406 return r;
407
408 r = mgr->apply(mgr);
409
410 return r;
411 }
412
413 switch (ck->key_type) {
414 case OMAPFB_COLOR_KEY_GFX_DST:
415 kt = OMAP_DSS_COLOR_KEY_GFX_DST;
416 break;
417 case OMAPFB_COLOR_KEY_VID_SRC:
418 kt = OMAP_DSS_COLOR_KEY_VID_SRC;
419 break;
420 default:
421 return -EINVAL;
422 }
423
424 info.default_color = ck->background;
425 info.trans_key = ck->trans_key;
426 info.trans_key_type = kt;
427 info.trans_enabled = true;
428
429 omapfb_color_keys[mgr->id] = *ck;
430
431 r = mgr->set_manager_info(mgr, &info);
432 if (r)
433 return r;
434
435 r = mgr->apply(mgr);
436
437 return r;
438}
439
440static int omapfb_set_color_key(struct fb_info *fbi,
441 struct omapfb_color_key *ck)
442{
443 struct omapfb_info *ofbi = FB2OFB(fbi);
444 struct omapfb2_device *fbdev = ofbi->fbdev;
445 int r;
446 int i;
447 struct omap_overlay_manager *mgr = NULL;
448
449 omapfb_lock(fbdev);
450
451 for (i = 0; i < ofbi->num_overlays; i++) {
452 if (ofbi->overlays[i]->manager) {
453 mgr = ofbi->overlays[i]->manager;
454 break;
455 }
456 }
457
458 if (!mgr) {
459 r = -EINVAL;
460 goto err;
461 }
462
463 r = _omapfb_set_color_key(mgr, ck);
464err:
465 omapfb_unlock(fbdev);
466
467 return r;
468}
469
470static int omapfb_get_color_key(struct fb_info *fbi,
471 struct omapfb_color_key *ck)
472{
473 struct omapfb_info *ofbi = FB2OFB(fbi);
474 struct omapfb2_device *fbdev = ofbi->fbdev;
475 struct omap_overlay_manager *mgr = NULL;
476 int r = 0;
477 int i;
478
479 omapfb_lock(fbdev);
480
481 for (i = 0; i < ofbi->num_overlays; i++) {
482 if (ofbi->overlays[i]->manager) {
483 mgr = ofbi->overlays[i]->manager;
484 break;
485 }
486 }
487
488 if (!mgr) {
489 r = -EINVAL;
490 goto err;
491 }
492
493 *ck = omapfb_color_keys[mgr->id];
494err:
495 omapfb_unlock(fbdev);
496
497 return r;
498}
499
500static int omapfb_memory_read(struct fb_info *fbi,
501 struct omapfb_memory_read *mr)
502{
503 struct omap_dss_device *display = fb2display(fbi);
504 void *buf;
505 int r;
506
Tomi Valkeinenc75d9462010-01-08 16:56:44 +0200507 if (!display || !display->driver->memory_read)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300508 return -ENOENT;
509
510 if (!access_ok(VERIFY_WRITE, mr->buffer, mr->buffer_size))
511 return -EFAULT;
512
513 if (mr->w * mr->h * 3 > mr->buffer_size)
514 return -EINVAL;
515
516 buf = vmalloc(mr->buffer_size);
517 if (!buf) {
518 DBG("vmalloc failed\n");
519 return -ENOMEM;
520 }
521
Tomi Valkeinenc75d9462010-01-08 16:56:44 +0200522 r = display->driver->memory_read(display, buf, mr->buffer_size,
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300523 mr->x, mr->y, mr->w, mr->h);
524
525 if (r > 0) {
526 if (copy_to_user(mr->buffer, buf, mr->buffer_size))
527 r = -EFAULT;
528 }
529
530 vfree(buf);
531
532 return r;
533}
534
535static int omapfb_get_ovl_colormode(struct omapfb2_device *fbdev,
536 struct omapfb_ovl_colormode *mode)
537{
538 int ovl_idx = mode->overlay_idx;
539 int mode_idx = mode->mode_idx;
540 struct omap_overlay *ovl;
541 enum omap_color_mode supported_modes;
542 struct fb_var_screeninfo var;
543 int i;
544
545 if (ovl_idx >= fbdev->num_overlays)
546 return -ENODEV;
547 ovl = fbdev->overlays[ovl_idx];
548 supported_modes = ovl->supported_modes;
549
550 mode_idx = mode->mode_idx;
551
552 for (i = 0; i < sizeof(supported_modes) * 8; i++) {
553 if (!(supported_modes & (1 << i)))
554 continue;
555 /*
556 * It's possible that the FB doesn't support a mode
557 * that is supported by the overlay, so call the
558 * following here.
559 */
560 if (dss_mode_to_fb_mode(1 << i, &var) < 0)
561 continue;
562
563 mode_idx--;
564 if (mode_idx < 0)
565 break;
566 }
567
568 if (i == sizeof(supported_modes) * 8)
569 return -ENOENT;
570
571 mode->bits_per_pixel = var.bits_per_pixel;
572 mode->nonstd = var.nonstd;
573 mode->red = var.red;
574 mode->green = var.green;
575 mode->blue = var.blue;
576 mode->transp = var.transp;
577
578 return 0;
579}
580
581static int omapfb_wait_for_go(struct fb_info *fbi)
582{
583 struct omapfb_info *ofbi = FB2OFB(fbi);
584 int r = 0;
585 int i;
586
587 for (i = 0; i < ofbi->num_overlays; ++i) {
588 struct omap_overlay *ovl = ofbi->overlays[i];
589 r = ovl->wait_for_go(ovl);
590 if (r)
591 break;
592 }
593
594 return r;
595}
596
597int omapfb_ioctl(struct fb_info *fbi, unsigned int cmd, unsigned long arg)
598{
599 struct omapfb_info *ofbi = FB2OFB(fbi);
600 struct omapfb2_device *fbdev = ofbi->fbdev;
601 struct omap_dss_device *display = fb2display(fbi);
602
603 union {
604 struct omapfb_update_window_old uwnd_o;
605 struct omapfb_update_window uwnd;
606 struct omapfb_plane_info plane_info;
607 struct omapfb_caps caps;
608 struct omapfb_mem_info mem_info;
609 struct omapfb_color_key color_key;
610 struct omapfb_ovl_colormode ovl_colormode;
611 enum omapfb_update_mode update_mode;
612 int test_num;
613 struct omapfb_memory_read memory_read;
614 struct omapfb_vram_info vram_info;
615 struct omapfb_tearsync_info tearsync_info;
Tomi Valkeinen6dd2e422010-01-14 17:32:13 +0200616 struct omapfb_display_info display_info;
Grazvydas Ignotasece350d2010-07-02 22:54:56 +0200617 u32 crt;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300618 } p;
619
620 int r = 0;
621
622 switch (cmd) {
623 case OMAPFB_SYNC_GFX:
624 DBG("ioctl SYNC_GFX\n");
Tomi Valkeinen18946f62010-01-12 14:16:41 +0200625 if (!display || !display->driver->sync) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300626 /* DSS1 never returns an error here, so we neither */
627 /*r = -EINVAL;*/
628 break;
629 }
630
Tomi Valkeinen18946f62010-01-12 14:16:41 +0200631 r = display->driver->sync(display);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300632 break;
633
634 case OMAPFB_UPDATE_WINDOW_OLD:
635 DBG("ioctl UPDATE_WINDOW_OLD\n");
Tomi Valkeinen18946f62010-01-12 14:16:41 +0200636 if (!display || !display->driver->update) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300637 r = -EINVAL;
638 break;
639 }
640
641 if (copy_from_user(&p.uwnd_o,
642 (void __user *)arg,
643 sizeof(p.uwnd_o))) {
644 r = -EFAULT;
645 break;
646 }
647
648 r = omapfb_update_window_nolock(fbi, p.uwnd_o.x, p.uwnd_o.y,
649 p.uwnd_o.width, p.uwnd_o.height);
650 break;
651
652 case OMAPFB_UPDATE_WINDOW:
653 DBG("ioctl UPDATE_WINDOW\n");
Tomi Valkeinen18946f62010-01-12 14:16:41 +0200654 if (!display || !display->driver->update) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300655 r = -EINVAL;
656 break;
657 }
658
659 if (copy_from_user(&p.uwnd, (void __user *)arg,
660 sizeof(p.uwnd))) {
661 r = -EFAULT;
662 break;
663 }
664
665 r = omapfb_update_window_nolock(fbi, p.uwnd.x, p.uwnd.y,
666 p.uwnd.width, p.uwnd.height);
667 break;
668
669 case OMAPFB_SETUP_PLANE:
670 DBG("ioctl SETUP_PLANE\n");
671 if (copy_from_user(&p.plane_info, (void __user *)arg,
672 sizeof(p.plane_info)))
673 r = -EFAULT;
674 else
675 r = omapfb_setup_plane(fbi, &p.plane_info);
676 break;
677
678 case OMAPFB_QUERY_PLANE:
679 DBG("ioctl QUERY_PLANE\n");
680 r = omapfb_query_plane(fbi, &p.plane_info);
681 if (r < 0)
682 break;
683 if (copy_to_user((void __user *)arg, &p.plane_info,
684 sizeof(p.plane_info)))
685 r = -EFAULT;
686 break;
687
688 case OMAPFB_SETUP_MEM:
689 DBG("ioctl SETUP_MEM\n");
690 if (copy_from_user(&p.mem_info, (void __user *)arg,
691 sizeof(p.mem_info)))
692 r = -EFAULT;
693 else
694 r = omapfb_setup_mem(fbi, &p.mem_info);
695 break;
696
697 case OMAPFB_QUERY_MEM:
698 DBG("ioctl QUERY_MEM\n");
699 r = omapfb_query_mem(fbi, &p.mem_info);
700 if (r < 0)
701 break;
702 if (copy_to_user((void __user *)arg, &p.mem_info,
703 sizeof(p.mem_info)))
704 r = -EFAULT;
705 break;
706
707 case OMAPFB_GET_CAPS:
708 DBG("ioctl GET_CAPS\n");
709 if (!display) {
710 r = -EINVAL;
711 break;
712 }
713
714 memset(&p.caps, 0, sizeof(p.caps));
715 if (display->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE)
716 p.caps.ctrl |= OMAPFB_CAPS_MANUAL_UPDATE;
717 if (display->caps & OMAP_DSS_DISPLAY_CAP_TEAR_ELIM)
718 p.caps.ctrl |= OMAPFB_CAPS_TEARSYNC;
719
720 if (copy_to_user((void __user *)arg, &p.caps, sizeof(p.caps)))
721 r = -EFAULT;
722 break;
723
724 case OMAPFB_GET_OVERLAY_COLORMODE:
725 DBG("ioctl GET_OVERLAY_COLORMODE\n");
726 if (copy_from_user(&p.ovl_colormode, (void __user *)arg,
727 sizeof(p.ovl_colormode))) {
728 r = -EFAULT;
729 break;
730 }
731 r = omapfb_get_ovl_colormode(fbdev, &p.ovl_colormode);
732 if (r < 0)
733 break;
734 if (copy_to_user((void __user *)arg, &p.ovl_colormode,
735 sizeof(p.ovl_colormode)))
736 r = -EFAULT;
737 break;
738
739 case OMAPFB_SET_UPDATE_MODE:
740 DBG("ioctl SET_UPDATE_MODE\n");
741 if (get_user(p.update_mode, (int __user *)arg))
742 r = -EFAULT;
743 else
744 r = omapfb_set_update_mode(fbi, p.update_mode);
745 break;
746
747 case OMAPFB_GET_UPDATE_MODE:
748 DBG("ioctl GET_UPDATE_MODE\n");
749 r = omapfb_get_update_mode(fbi, &p.update_mode);
750 if (r)
751 break;
752 if (put_user(p.update_mode,
753 (enum omapfb_update_mode __user *)arg))
754 r = -EFAULT;
755 break;
756
757 case OMAPFB_SET_COLOR_KEY:
758 DBG("ioctl SET_COLOR_KEY\n");
759 if (copy_from_user(&p.color_key, (void __user *)arg,
760 sizeof(p.color_key)))
761 r = -EFAULT;
762 else
763 r = omapfb_set_color_key(fbi, &p.color_key);
764 break;
765
766 case OMAPFB_GET_COLOR_KEY:
767 DBG("ioctl GET_COLOR_KEY\n");
768 r = omapfb_get_color_key(fbi, &p.color_key);
769 if (r)
770 break;
771 if (copy_to_user((void __user *)arg, &p.color_key,
772 sizeof(p.color_key)))
773 r = -EFAULT;
774 break;
775
Grazvydas Ignotasece350d2010-07-02 22:54:56 +0200776 case FBIO_WAITFORVSYNC:
777 if (get_user(p.crt, (__u32 __user *)arg)) {
778 r = -EFAULT;
779 break;
780 }
781 if (p.crt != 0) {
782 r = -ENODEV;
783 break;
784 }
785 /* FALLTHROUGH */
786
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300787 case OMAPFB_WAITFORVSYNC:
788 DBG("ioctl WAITFORVSYNC\n");
789 if (!display) {
790 r = -EINVAL;
791 break;
792 }
793
Tomi Valkeinen3f71cbe2010-01-08 17:06:04 +0200794 r = display->manager->wait_for_vsync(display->manager);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300795 break;
796
797 case OMAPFB_WAITFORGO:
798 DBG("ioctl WAITFORGO\n");
799 if (!display) {
800 r = -EINVAL;
801 break;
802 }
803
804 r = omapfb_wait_for_go(fbi);
805 break;
806
807 /* LCD and CTRL tests do the same thing for backward
808 * compatibility */
809 case OMAPFB_LCD_TEST:
810 DBG("ioctl LCD_TEST\n");
811 if (get_user(p.test_num, (int __user *)arg)) {
812 r = -EFAULT;
813 break;
814 }
Tomi Valkeinen1a75ef42010-01-08 16:21:28 +0200815 if (!display || !display->driver->run_test) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300816 r = -EINVAL;
817 break;
818 }
819
Tomi Valkeinen1a75ef42010-01-08 16:21:28 +0200820 r = display->driver->run_test(display, p.test_num);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300821
822 break;
823
824 case OMAPFB_CTRL_TEST:
825 DBG("ioctl CTRL_TEST\n");
826 if (get_user(p.test_num, (int __user *)arg)) {
827 r = -EFAULT;
828 break;
829 }
Tomi Valkeinen1a75ef42010-01-08 16:21:28 +0200830 if (!display || !display->driver->run_test) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300831 r = -EINVAL;
832 break;
833 }
834
Tomi Valkeinen1a75ef42010-01-08 16:21:28 +0200835 r = display->driver->run_test(display, p.test_num);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300836
837 break;
838
839 case OMAPFB_MEMORY_READ:
840 DBG("ioctl MEMORY_READ\n");
841
842 if (copy_from_user(&p.memory_read, (void __user *)arg,
843 sizeof(p.memory_read))) {
844 r = -EFAULT;
845 break;
846 }
847
848 r = omapfb_memory_read(fbi, &p.memory_read);
849
850 break;
851
852 case OMAPFB_GET_VRAM_INFO: {
853 unsigned long vram, free, largest;
854
855 DBG("ioctl GET_VRAM_INFO\n");
856
857 omap_vram_get_info(&vram, &free, &largest);
858 p.vram_info.total = vram;
859 p.vram_info.free = free;
860 p.vram_info.largest_free_block = largest;
861
862 if (copy_to_user((void __user *)arg, &p.vram_info,
863 sizeof(p.vram_info)))
864 r = -EFAULT;
865 break;
866 }
867
868 case OMAPFB_SET_TEARSYNC: {
869 DBG("ioctl SET_TEARSYNC\n");
870
871 if (copy_from_user(&p.tearsync_info, (void __user *)arg,
872 sizeof(p.tearsync_info))) {
873 r = -EFAULT;
874 break;
875 }
876
Jani Nikula93255882010-06-01 15:12:12 +0300877 if (!display || !display->driver->enable_te) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300878 r = -ENODEV;
879 break;
880 }
881
Tomi Valkeinen225b6502010-01-11 15:11:01 +0200882 r = display->driver->enable_te(display,
883 !!p.tearsync_info.enabled);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300884
885 break;
886 }
887
Tomi Valkeinen6dd2e422010-01-14 17:32:13 +0200888 case OMAPFB_GET_DISPLAY_INFO: {
889 u16 xres, yres;
890
891 DBG("ioctl GET_DISPLAY_INFO\n");
892
893 if (display == NULL) {
894 r = -ENODEV;
895 break;
896 }
897
Tomi Valkeinen96adcec2010-01-11 13:54:33 +0200898 display->driver->get_resolution(display, &xres, &yres);
Tomi Valkeinen6dd2e422010-01-14 17:32:13 +0200899
900 p.display_info.xres = xres;
901 p.display_info.yres = yres;
Jani Nikula7a0987b2010-06-16 15:26:36 +0300902
903 if (display->driver->get_dimensions) {
904 u32 w, h;
905 display->driver->get_dimensions(display, &w, &h);
906 p.display_info.width = w;
907 p.display_info.height = h;
908 } else {
909 p.display_info.width = 0;
910 p.display_info.height = 0;
911 }
Tomi Valkeinen6dd2e422010-01-14 17:32:13 +0200912
913 if (copy_to_user((void __user *)arg, &p.display_info,
914 sizeof(p.display_info)))
915 r = -EFAULT;
916 break;
917 }
918
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300919 default:
920 dev_err(fbdev->dev, "Unknown ioctl 0x%x\n", cmd);
921 r = -EINVAL;
922 }
923
924 if (r < 0)
925 DBG("ioctl failed: %d\n", r);
926
927 return r;
928}
929
930