blob: 2f5e817b2a9a65904e4f41631036fb350c478784 [file] [log] [blame]
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001/*
2 * linux/drivers/video/omap2/omapfb-sysfs.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/sysfs.h>
25#include <linux/device.h>
26#include <linux/uaccess.h>
27#include <linux/platform_device.h>
28#include <linux/kernel.h>
29#include <linux/mm.h>
30#include <linux/omapfb.h>
31
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
35#include "omapfb.h"
36
37static ssize_t show_rotate_type(struct device *dev,
38 struct device_attribute *attr, char *buf)
39{
40 struct fb_info *fbi = dev_get_drvdata(dev);
41 struct omapfb_info *ofbi = FB2OFB(fbi);
42
43 return snprintf(buf, PAGE_SIZE, "%d\n", ofbi->rotation_type);
44}
45
46static ssize_t store_rotate_type(struct device *dev,
47 struct device_attribute *attr,
48 const char *buf, size_t count)
49{
50 struct fb_info *fbi = dev_get_drvdata(dev);
51 struct omapfb_info *ofbi = FB2OFB(fbi);
Ville Syrjälä430571d2010-03-17 20:43:23 +020052 struct omapfb2_mem_region *rg;
Tomi Valkeinene3502ce2011-04-04 15:40:23 +030053 int rot_type;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030054 int r;
55
Tomi Valkeinene3502ce2011-04-04 15:40:23 +030056 r = kstrtoint(buf, 0, &rot_type);
57 if (r)
58 return r;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030059
60 if (rot_type != OMAP_DSS_ROT_DMA && rot_type != OMAP_DSS_ROT_VRFB)
61 return -EINVAL;
62
Jani Nikula27b67c92010-03-18 10:32:06 +010063 if (!lock_fb_info(fbi))
64 return -ENODEV;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030065
66 r = 0;
67 if (rot_type == ofbi->rotation_type)
68 goto out;
69
Ville Syrjälä430571d2010-03-17 20:43:23 +020070 rg = omapfb_get_mem_region(ofbi->region);
71
72 if (rg->size) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030073 r = -EBUSY;
Ville Syrjälä430571d2010-03-17 20:43:23 +020074 goto put_region;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030075 }
76
77 ofbi->rotation_type = rot_type;
78
79 /*
80 * Since the VRAM for this FB is not allocated at the moment we don't
81 * need to do any further parameter checking at this point.
82 */
Ville Syrjälä430571d2010-03-17 20:43:23 +020083put_region:
84 omapfb_put_mem_region(rg);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030085out:
86 unlock_fb_info(fbi);
87
88 return r ? r : count;
89}
90
91
92static ssize_t show_mirror(struct device *dev,
93 struct device_attribute *attr, char *buf)
94{
95 struct fb_info *fbi = dev_get_drvdata(dev);
96 struct omapfb_info *ofbi = FB2OFB(fbi);
97
98 return snprintf(buf, PAGE_SIZE, "%d\n", ofbi->mirror);
99}
100
101static ssize_t store_mirror(struct device *dev,
102 struct device_attribute *attr,
103 const char *buf, size_t count)
104{
105 struct fb_info *fbi = dev_get_drvdata(dev);
106 struct omapfb_info *ofbi = FB2OFB(fbi);
Tomi Valkeinene3502ce2011-04-04 15:40:23 +0300107 int mirror;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300108 int r;
109 struct fb_var_screeninfo new_var;
110
Tomi Valkeinene3502ce2011-04-04 15:40:23 +0300111 r = kstrtoint(buf, 0, &mirror);
112 if (r)
113 return r;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300114
Tomi Valkeinene3502ce2011-04-04 15:40:23 +0300115 mirror = !!mirror;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300116
Jani Nikula27b67c92010-03-18 10:32:06 +0100117 if (!lock_fb_info(fbi))
118 return -ENODEV;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300119
120 ofbi->mirror = mirror;
121
Ville Syrjälä430571d2010-03-17 20:43:23 +0200122 omapfb_get_mem_region(ofbi->region);
123
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300124 memcpy(&new_var, &fbi->var, sizeof(new_var));
125 r = check_fb_var(fbi, &new_var);
126 if (r)
127 goto out;
128 memcpy(&fbi->var, &new_var, sizeof(fbi->var));
129
130 set_fb_fix(fbi);
131
132 r = omapfb_apply_changes(fbi, 0);
133 if (r)
134 goto out;
135
136 r = count;
137out:
Ville Syrjälä430571d2010-03-17 20:43:23 +0200138 omapfb_put_mem_region(ofbi->region);
139
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300140 unlock_fb_info(fbi);
141
142 return r;
143}
144
145static ssize_t show_overlays(struct device *dev,
146 struct device_attribute *attr, char *buf)
147{
148 struct fb_info *fbi = dev_get_drvdata(dev);
149 struct omapfb_info *ofbi = FB2OFB(fbi);
150 struct omapfb2_device *fbdev = ofbi->fbdev;
151 ssize_t l = 0;
152 int t;
153
Jani Nikula27b67c92010-03-18 10:32:06 +0100154 if (!lock_fb_info(fbi))
155 return -ENODEV;
Jani Nikula238a4132010-03-18 10:32:05 +0100156 omapfb_lock(fbdev);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300157
158 for (t = 0; t < ofbi->num_overlays; t++) {
159 struct omap_overlay *ovl = ofbi->overlays[t];
160 int ovlnum;
161
162 for (ovlnum = 0; ovlnum < fbdev->num_overlays; ++ovlnum)
163 if (ovl == fbdev->overlays[ovlnum])
164 break;
165
166 l += snprintf(buf + l, PAGE_SIZE - l, "%s%d",
167 t == 0 ? "" : ",", ovlnum);
168 }
169
170 l += snprintf(buf + l, PAGE_SIZE - l, "\n");
171
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300172 omapfb_unlock(fbdev);
Jani Nikula238a4132010-03-18 10:32:05 +0100173 unlock_fb_info(fbi);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300174
175 return l;
176}
177
178static struct omapfb_info *get_overlay_fb(struct omapfb2_device *fbdev,
179 struct omap_overlay *ovl)
180{
181 int i, t;
182
183 for (i = 0; i < fbdev->num_fbs; i++) {
184 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
185
186 for (t = 0; t < ofbi->num_overlays; t++) {
187 if (ofbi->overlays[t] == ovl)
188 return ofbi;
189 }
190 }
191
192 return NULL;
193}
194
195static ssize_t store_overlays(struct device *dev, struct device_attribute *attr,
196 const char *buf, size_t count)
197{
198 struct fb_info *fbi = dev_get_drvdata(dev);
199 struct omapfb_info *ofbi = FB2OFB(fbi);
200 struct omapfb2_device *fbdev = ofbi->fbdev;
201 struct omap_overlay *ovls[OMAPFB_MAX_OVL_PER_FB];
202 struct omap_overlay *ovl;
203 int num_ovls, r, i;
204 int len;
205 bool added = false;
206
207 num_ovls = 0;
208
209 len = strlen(buf);
210 if (buf[len - 1] == '\n')
211 len = len - 1;
212
Jani Nikula27b67c92010-03-18 10:32:06 +0100213 if (!lock_fb_info(fbi))
214 return -ENODEV;
Jani Nikula238a4132010-03-18 10:32:05 +0100215 omapfb_lock(fbdev);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300216
217 if (len > 0) {
218 char *p = (char *)buf;
219 int ovlnum;
220
221 while (p < buf + len) {
222 int found;
223 if (num_ovls == OMAPFB_MAX_OVL_PER_FB) {
224 r = -EINVAL;
225 goto out;
226 }
227
228 ovlnum = simple_strtoul(p, &p, 0);
229 if (ovlnum > fbdev->num_overlays) {
230 r = -EINVAL;
231 goto out;
232 }
233
234 found = 0;
235 for (i = 0; i < num_ovls; ++i) {
236 if (ovls[i] == fbdev->overlays[ovlnum]) {
237 found = 1;
238 break;
239 }
240 }
241
242 if (!found)
243 ovls[num_ovls++] = fbdev->overlays[ovlnum];
244
245 p++;
246 }
247 }
248
249 for (i = 0; i < num_ovls; ++i) {
250 struct omapfb_info *ofbi2 = get_overlay_fb(fbdev, ovls[i]);
251 if (ofbi2 && ofbi2 != ofbi) {
252 dev_err(fbdev->dev, "overlay already in use\n");
253 r = -EINVAL;
254 goto out;
255 }
256 }
257
258 /* detach unused overlays */
259 for (i = 0; i < ofbi->num_overlays; ++i) {
260 int t, found;
261
262 ovl = ofbi->overlays[i];
263
264 found = 0;
265
266 for (t = 0; t < num_ovls; ++t) {
267 if (ovl == ovls[t]) {
268 found = 1;
269 break;
270 }
271 }
272
273 if (found)
274 continue;
275
276 DBG("detaching %d\n", ofbi->overlays[i]->id);
277
Ville Syrjälä430571d2010-03-17 20:43:23 +0200278 omapfb_get_mem_region(ofbi->region);
279
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300280 omapfb_overlay_enable(ovl, 0);
281
282 if (ovl->manager)
283 ovl->manager->apply(ovl->manager);
284
Ville Syrjälä430571d2010-03-17 20:43:23 +0200285 omapfb_put_mem_region(ofbi->region);
286
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300287 for (t = i + 1; t < ofbi->num_overlays; t++) {
288 ofbi->rotation[t-1] = ofbi->rotation[t];
289 ofbi->overlays[t-1] = ofbi->overlays[t];
290 }
291
292 ofbi->num_overlays--;
293 i--;
294 }
295
296 for (i = 0; i < num_ovls; ++i) {
297 int t, found;
298
299 ovl = ovls[i];
300
301 found = 0;
302
303 for (t = 0; t < ofbi->num_overlays; ++t) {
304 if (ovl == ofbi->overlays[t]) {
305 found = 1;
306 break;
307 }
308 }
309
310 if (found)
311 continue;
312 ofbi->rotation[ofbi->num_overlays] = 0;
313 ofbi->overlays[ofbi->num_overlays++] = ovl;
314
315 added = true;
316 }
317
318 if (added) {
Ville Syrjälä430571d2010-03-17 20:43:23 +0200319 omapfb_get_mem_region(ofbi->region);
320
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300321 r = omapfb_apply_changes(fbi, 0);
Ville Syrjälä430571d2010-03-17 20:43:23 +0200322
323 omapfb_put_mem_region(ofbi->region);
324
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300325 if (r)
326 goto out;
327 }
328
329 r = count;
330out:
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300331 omapfb_unlock(fbdev);
Jani Nikula238a4132010-03-18 10:32:05 +0100332 unlock_fb_info(fbi);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300333
334 return r;
335}
336
337static ssize_t show_overlays_rotate(struct device *dev,
338 struct device_attribute *attr, char *buf)
339{
340 struct fb_info *fbi = dev_get_drvdata(dev);
341 struct omapfb_info *ofbi = FB2OFB(fbi);
342 ssize_t l = 0;
343 int t;
344
Jani Nikula27b67c92010-03-18 10:32:06 +0100345 if (!lock_fb_info(fbi))
346 return -ENODEV;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300347
348 for (t = 0; t < ofbi->num_overlays; t++) {
349 l += snprintf(buf + l, PAGE_SIZE - l, "%s%d",
350 t == 0 ? "" : ",", ofbi->rotation[t]);
351 }
352
353 l += snprintf(buf + l, PAGE_SIZE - l, "\n");
354
355 unlock_fb_info(fbi);
356
357 return l;
358}
359
360static ssize_t store_overlays_rotate(struct device *dev,
361 struct device_attribute *attr, const char *buf, size_t count)
362{
363 struct fb_info *fbi = dev_get_drvdata(dev);
364 struct omapfb_info *ofbi = FB2OFB(fbi);
365 int num_ovls = 0, r, i;
366 int len;
367 bool changed = false;
368 u8 rotation[OMAPFB_MAX_OVL_PER_FB];
369
370 len = strlen(buf);
371 if (buf[len - 1] == '\n')
372 len = len - 1;
373
Jani Nikula27b67c92010-03-18 10:32:06 +0100374 if (!lock_fb_info(fbi))
375 return -ENODEV;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300376
377 if (len > 0) {
378 char *p = (char *)buf;
379
380 while (p < buf + len) {
381 int rot;
382
383 if (num_ovls == ofbi->num_overlays) {
384 r = -EINVAL;
385 goto out;
386 }
387
388 rot = simple_strtoul(p, &p, 0);
389 if (rot < 0 || rot > 3) {
390 r = -EINVAL;
391 goto out;
392 }
393
394 if (ofbi->rotation[num_ovls] != rot)
395 changed = true;
396
397 rotation[num_ovls++] = rot;
398
399 p++;
400 }
401 }
402
403 if (num_ovls != ofbi->num_overlays) {
404 r = -EINVAL;
405 goto out;
406 }
407
408 if (changed) {
409 for (i = 0; i < num_ovls; ++i)
410 ofbi->rotation[i] = rotation[i];
411
Ville Syrjälä430571d2010-03-17 20:43:23 +0200412 omapfb_get_mem_region(ofbi->region);
413
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300414 r = omapfb_apply_changes(fbi, 0);
Ville Syrjälä430571d2010-03-17 20:43:23 +0200415
416 omapfb_put_mem_region(ofbi->region);
417
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300418 if (r)
419 goto out;
420
421 /* FIXME error handling? */
422 }
423
424 r = count;
425out:
426 unlock_fb_info(fbi);
427
428 return r;
429}
430
431static ssize_t show_size(struct device *dev,
432 struct device_attribute *attr, char *buf)
433{
434 struct fb_info *fbi = dev_get_drvdata(dev);
435 struct omapfb_info *ofbi = FB2OFB(fbi);
436
Ville Syrjälä078ff542010-03-17 20:36:51 +0200437 return snprintf(buf, PAGE_SIZE, "%lu\n", ofbi->region->size);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300438}
439
440static ssize_t store_size(struct device *dev, struct device_attribute *attr,
441 const char *buf, size_t count)
442{
443 struct fb_info *fbi = dev_get_drvdata(dev);
444 struct omapfb_info *ofbi = FB2OFB(fbi);
Ville Syrjälä078ff542010-03-17 20:36:51 +0200445 struct omapfb2_device *fbdev = ofbi->fbdev;
446 struct omapfb2_mem_region *rg;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300447 unsigned long size;
448 int r;
449 int i;
450
Tomi Valkeinene3502ce2011-04-04 15:40:23 +0300451 r = kstrtoul(buf, 0, &size);
452 if (r)
453 return r;
454
455 size = PAGE_ALIGN(size);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300456
Jani Nikula27b67c92010-03-18 10:32:06 +0100457 if (!lock_fb_info(fbi))
458 return -ENODEV;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300459
Ville Syrjälä078ff542010-03-17 20:36:51 +0200460 rg = ofbi->region;
461
Ville Syrjälä3d84b652010-03-17 21:42:06 +0200462 down_write_nested(&rg->lock, rg->id);
Ville Syrjälä1ceafc02010-03-17 21:28:50 +0200463 atomic_inc(&rg->lock_count);
Ville Syrjälä430571d2010-03-17 20:43:23 +0200464
Ville Syrjälä078ff542010-03-17 20:36:51 +0200465 if (atomic_read(&rg->map_count)) {
466 r = -EBUSY;
467 goto out;
468 }
469
470 for (i = 0; i < fbdev->num_fbs; i++) {
471 struct omapfb_info *ofbi2 = FB2OFB(fbdev->fbs[i]);
472 int j;
473
474 if (ofbi2->region != rg)
475 continue;
476
477 for (j = 0; j < ofbi2->num_overlays; j++) {
478 if (ofbi2->overlays[j]->info.enabled) {
479 r = -EBUSY;
480 goto out;
481 }
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300482 }
483 }
484
Ville Syrjälä078ff542010-03-17 20:36:51 +0200485 if (size != ofbi->region->size) {
486 r = omapfb_realloc_fbmem(fbi, size, ofbi->region->type);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300487 if (r) {
488 dev_err(dev, "realloc fbmem failed\n");
489 goto out;
490 }
491 }
492
493 r = count;
494out:
Ville Syrjälä1ceafc02010-03-17 21:28:50 +0200495 atomic_dec(&rg->lock_count);
Ville Syrjälä2f642a12010-03-17 20:58:03 +0200496 up_write(&rg->lock);
Ville Syrjälä430571d2010-03-17 20:43:23 +0200497
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300498 unlock_fb_info(fbi);
499
500 return r;
501}
502
503static ssize_t show_phys(struct device *dev,
504 struct device_attribute *attr, char *buf)
505{
506 struct fb_info *fbi = dev_get_drvdata(dev);
507 struct omapfb_info *ofbi = FB2OFB(fbi);
508
Ville Syrjälä078ff542010-03-17 20:36:51 +0200509 return snprintf(buf, PAGE_SIZE, "%0x\n", ofbi->region->paddr);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300510}
511
512static ssize_t show_virt(struct device *dev,
513 struct device_attribute *attr, char *buf)
514{
515 struct fb_info *fbi = dev_get_drvdata(dev);
516 struct omapfb_info *ofbi = FB2OFB(fbi);
517
Ville Syrjälä078ff542010-03-17 20:36:51 +0200518 return snprintf(buf, PAGE_SIZE, "%p\n", ofbi->region->vaddr);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300519}
520
521static struct device_attribute omapfb_attrs[] = {
522 __ATTR(rotate_type, S_IRUGO | S_IWUSR, show_rotate_type,
523 store_rotate_type),
524 __ATTR(mirror, S_IRUGO | S_IWUSR, show_mirror, store_mirror),
525 __ATTR(size, S_IRUGO | S_IWUSR, show_size, store_size),
526 __ATTR(overlays, S_IRUGO | S_IWUSR, show_overlays, store_overlays),
527 __ATTR(overlays_rotate, S_IRUGO | S_IWUSR, show_overlays_rotate,
528 store_overlays_rotate),
529 __ATTR(phys_addr, S_IRUGO, show_phys, NULL),
530 __ATTR(virt_addr, S_IRUGO, show_virt, NULL),
531};
532
533int omapfb_create_sysfs(struct omapfb2_device *fbdev)
534{
535 int i;
536 int r;
537
538 DBG("create sysfs for fbs\n");
539 for (i = 0; i < fbdev->num_fbs; i++) {
540 int t;
541 for (t = 0; t < ARRAY_SIZE(omapfb_attrs); t++) {
542 r = device_create_file(fbdev->fbs[i]->dev,
543 &omapfb_attrs[t]);
544
545 if (r) {
546 dev_err(fbdev->dev, "failed to create sysfs "
547 "file\n");
548 return r;
549 }
550 }
551 }
552
553 return 0;
554}
555
556void omapfb_remove_sysfs(struct omapfb2_device *fbdev)
557{
558 int i, t;
559
560 DBG("remove sysfs for fbs\n");
561 for (i = 0; i < fbdev->num_fbs; i++) {
562 for (t = 0; t < ARRAY_SIZE(omapfb_attrs); t++)
563 device_remove_file(fbdev->fbs[i]->dev,
564 &omapfb_attrs[t]);
565 }
566}
567