WIP FPC-III support
[linux/fpc-iii.git] / drivers / video / fbdev / omap2 / omapfb / omapfb-sysfs.c
blob2d39dbfa742e77abd11e50ae8bb0f05d026f027b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/drivers/video/omap2/omapfb-sysfs.c
5 * Copyright (C) 2008 Nokia Corporation
6 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
8 * Some code and ideas taken from drivers/video/omap/ driver
9 * by Imre Deak.
12 #include <linux/fb.h>
13 #include <linux/sysfs.h>
14 #include <linux/device.h>
15 #include <linux/uaccess.h>
16 #include <linux/platform_device.h>
17 #include <linux/kernel.h>
18 #include <linux/mm.h>
19 #include <linux/omapfb.h>
21 #include <video/omapfb_dss.h>
22 #include <video/omapvrfb.h>
24 #include "omapfb.h"
26 static ssize_t show_rotate_type(struct device *dev,
27 struct device_attribute *attr, char *buf)
29 struct fb_info *fbi = dev_get_drvdata(dev);
30 struct omapfb_info *ofbi = FB2OFB(fbi);
32 return snprintf(buf, PAGE_SIZE, "%d\n", ofbi->rotation_type);
35 static ssize_t store_rotate_type(struct device *dev,
36 struct device_attribute *attr,
37 const char *buf, size_t count)
39 struct fb_info *fbi = dev_get_drvdata(dev);
40 struct omapfb_info *ofbi = FB2OFB(fbi);
41 struct omapfb2_mem_region *rg;
42 int rot_type;
43 int r;
45 r = kstrtoint(buf, 0, &rot_type);
46 if (r)
47 return r;
49 if (rot_type != OMAP_DSS_ROT_DMA && rot_type != OMAP_DSS_ROT_VRFB)
50 return -EINVAL;
52 lock_fb_info(fbi);
54 r = 0;
55 if (rot_type == ofbi->rotation_type)
56 goto out;
58 rg = omapfb_get_mem_region(ofbi->region);
60 if (rg->size) {
61 r = -EBUSY;
62 goto put_region;
65 ofbi->rotation_type = rot_type;
68 * Since the VRAM for this FB is not allocated at the moment we don't
69 * need to do any further parameter checking at this point.
71 put_region:
72 omapfb_put_mem_region(rg);
73 out:
74 unlock_fb_info(fbi);
76 return r ? r : count;
80 static ssize_t show_mirror(struct device *dev,
81 struct device_attribute *attr, char *buf)
83 struct fb_info *fbi = dev_get_drvdata(dev);
84 struct omapfb_info *ofbi = FB2OFB(fbi);
86 return snprintf(buf, PAGE_SIZE, "%d\n", ofbi->mirror);
89 static ssize_t store_mirror(struct device *dev,
90 struct device_attribute *attr,
91 const char *buf, size_t count)
93 struct fb_info *fbi = dev_get_drvdata(dev);
94 struct omapfb_info *ofbi = FB2OFB(fbi);
95 bool mirror;
96 int r;
97 struct fb_var_screeninfo new_var;
99 r = strtobool(buf, &mirror);
100 if (r)
101 return r;
103 lock_fb_info(fbi);
105 ofbi->mirror = mirror;
107 omapfb_get_mem_region(ofbi->region);
109 memcpy(&new_var, &fbi->var, sizeof(new_var));
110 r = check_fb_var(fbi, &new_var);
111 if (r)
112 goto out;
113 memcpy(&fbi->var, &new_var, sizeof(fbi->var));
115 set_fb_fix(fbi);
117 r = omapfb_apply_changes(fbi, 0);
118 if (r)
119 goto out;
121 r = count;
122 out:
123 omapfb_put_mem_region(ofbi->region);
125 unlock_fb_info(fbi);
127 return r;
130 static ssize_t show_overlays(struct device *dev,
131 struct device_attribute *attr, char *buf)
133 struct fb_info *fbi = dev_get_drvdata(dev);
134 struct omapfb_info *ofbi = FB2OFB(fbi);
135 struct omapfb2_device *fbdev = ofbi->fbdev;
136 ssize_t l = 0;
137 int t;
139 lock_fb_info(fbi);
140 omapfb_lock(fbdev);
142 for (t = 0; t < ofbi->num_overlays; t++) {
143 struct omap_overlay *ovl = ofbi->overlays[t];
144 int ovlnum;
146 for (ovlnum = 0; ovlnum < fbdev->num_overlays; ++ovlnum)
147 if (ovl == fbdev->overlays[ovlnum])
148 break;
150 l += scnprintf(buf + l, PAGE_SIZE - l, "%s%d",
151 t == 0 ? "" : ",", ovlnum);
154 l += scnprintf(buf + l, PAGE_SIZE - l, "\n");
156 omapfb_unlock(fbdev);
157 unlock_fb_info(fbi);
159 return l;
162 static struct omapfb_info *get_overlay_fb(struct omapfb2_device *fbdev,
163 struct omap_overlay *ovl)
165 int i, t;
167 for (i = 0; i < fbdev->num_fbs; i++) {
168 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
170 for (t = 0; t < ofbi->num_overlays; t++) {
171 if (ofbi->overlays[t] == ovl)
172 return ofbi;
176 return NULL;
179 static ssize_t store_overlays(struct device *dev, struct device_attribute *attr,
180 const char *buf, size_t count)
182 struct fb_info *fbi = dev_get_drvdata(dev);
183 struct omapfb_info *ofbi = FB2OFB(fbi);
184 struct omapfb2_device *fbdev = ofbi->fbdev;
185 struct omap_overlay *ovls[OMAPFB_MAX_OVL_PER_FB];
186 struct omap_overlay *ovl;
187 int num_ovls, r, i;
188 int len;
189 bool added = false;
191 num_ovls = 0;
193 len = strlen(buf);
194 if (buf[len - 1] == '\n')
195 len = len - 1;
197 lock_fb_info(fbi);
198 omapfb_lock(fbdev);
200 if (len > 0) {
201 char *p = (char *)buf;
202 int ovlnum;
204 while (p < buf + len) {
205 int found;
206 if (num_ovls == OMAPFB_MAX_OVL_PER_FB) {
207 r = -EINVAL;
208 goto out;
211 ovlnum = simple_strtoul(p, &p, 0);
212 if (ovlnum > fbdev->num_overlays) {
213 r = -EINVAL;
214 goto out;
217 found = 0;
218 for (i = 0; i < num_ovls; ++i) {
219 if (ovls[i] == fbdev->overlays[ovlnum]) {
220 found = 1;
221 break;
225 if (!found)
226 ovls[num_ovls++] = fbdev->overlays[ovlnum];
228 p++;
232 for (i = 0; i < num_ovls; ++i) {
233 struct omapfb_info *ofbi2 = get_overlay_fb(fbdev, ovls[i]);
234 if (ofbi2 && ofbi2 != ofbi) {
235 dev_err(fbdev->dev, "overlay already in use\n");
236 r = -EINVAL;
237 goto out;
241 /* detach unused overlays */
242 for (i = 0; i < ofbi->num_overlays; ++i) {
243 int t, found;
245 ovl = ofbi->overlays[i];
247 found = 0;
249 for (t = 0; t < num_ovls; ++t) {
250 if (ovl == ovls[t]) {
251 found = 1;
252 break;
256 if (found)
257 continue;
259 DBG("detaching %d\n", ofbi->overlays[i]->id);
261 omapfb_get_mem_region(ofbi->region);
263 omapfb_overlay_enable(ovl, 0);
265 if (ovl->manager)
266 ovl->manager->apply(ovl->manager);
268 omapfb_put_mem_region(ofbi->region);
270 for (t = i + 1; t < ofbi->num_overlays; t++) {
271 ofbi->rotation[t-1] = ofbi->rotation[t];
272 ofbi->overlays[t-1] = ofbi->overlays[t];
275 ofbi->num_overlays--;
276 i--;
279 for (i = 0; i < num_ovls; ++i) {
280 int t, found;
282 ovl = ovls[i];
284 found = 0;
286 for (t = 0; t < ofbi->num_overlays; ++t) {
287 if (ovl == ofbi->overlays[t]) {
288 found = 1;
289 break;
293 if (found)
294 continue;
295 ofbi->rotation[ofbi->num_overlays] = 0;
296 ofbi->overlays[ofbi->num_overlays++] = ovl;
298 added = true;
301 if (added) {
302 omapfb_get_mem_region(ofbi->region);
304 r = omapfb_apply_changes(fbi, 0);
306 omapfb_put_mem_region(ofbi->region);
308 if (r)
309 goto out;
312 r = count;
313 out:
314 omapfb_unlock(fbdev);
315 unlock_fb_info(fbi);
317 return r;
320 static ssize_t show_overlays_rotate(struct device *dev,
321 struct device_attribute *attr, char *buf)
323 struct fb_info *fbi = dev_get_drvdata(dev);
324 struct omapfb_info *ofbi = FB2OFB(fbi);
325 ssize_t l = 0;
326 int t;
328 lock_fb_info(fbi);
330 for (t = 0; t < ofbi->num_overlays; t++) {
331 l += scnprintf(buf + l, PAGE_SIZE - l, "%s%d",
332 t == 0 ? "" : ",", ofbi->rotation[t]);
335 l += scnprintf(buf + l, PAGE_SIZE - l, "\n");
337 unlock_fb_info(fbi);
339 return l;
342 static ssize_t store_overlays_rotate(struct device *dev,
343 struct device_attribute *attr, const char *buf, size_t count)
345 struct fb_info *fbi = dev_get_drvdata(dev);
346 struct omapfb_info *ofbi = FB2OFB(fbi);
347 int num_ovls = 0, r, i;
348 int len;
349 bool changed = false;
350 u8 rotation[OMAPFB_MAX_OVL_PER_FB];
352 len = strlen(buf);
353 if (buf[len - 1] == '\n')
354 len = len - 1;
356 lock_fb_info(fbi);
358 if (len > 0) {
359 char *p = (char *)buf;
361 while (p < buf + len) {
362 int rot;
364 if (num_ovls == ofbi->num_overlays) {
365 r = -EINVAL;
366 goto out;
369 rot = simple_strtoul(p, &p, 0);
370 if (rot < 0 || rot > 3) {
371 r = -EINVAL;
372 goto out;
375 if (ofbi->rotation[num_ovls] != rot)
376 changed = true;
378 rotation[num_ovls++] = rot;
380 p++;
384 if (num_ovls != ofbi->num_overlays) {
385 r = -EINVAL;
386 goto out;
389 if (changed) {
390 for (i = 0; i < num_ovls; ++i)
391 ofbi->rotation[i] = rotation[i];
393 omapfb_get_mem_region(ofbi->region);
395 r = omapfb_apply_changes(fbi, 0);
397 omapfb_put_mem_region(ofbi->region);
399 if (r)
400 goto out;
402 /* FIXME error handling? */
405 r = count;
406 out:
407 unlock_fb_info(fbi);
409 return r;
412 static ssize_t show_size(struct device *dev,
413 struct device_attribute *attr, char *buf)
415 struct fb_info *fbi = dev_get_drvdata(dev);
416 struct omapfb_info *ofbi = FB2OFB(fbi);
418 return snprintf(buf, PAGE_SIZE, "%lu\n", ofbi->region->size);
421 static ssize_t store_size(struct device *dev, struct device_attribute *attr,
422 const char *buf, size_t count)
424 struct fb_info *fbi = dev_get_drvdata(dev);
425 struct omapfb_info *ofbi = FB2OFB(fbi);
426 struct omapfb2_device *fbdev = ofbi->fbdev;
427 struct omap_dss_device *display = fb2display(fbi);
428 struct omapfb2_mem_region *rg;
429 unsigned long size;
430 int r;
431 int i;
433 r = kstrtoul(buf, 0, &size);
434 if (r)
435 return r;
437 size = PAGE_ALIGN(size);
439 lock_fb_info(fbi);
441 if (display && display->driver->sync)
442 display->driver->sync(display);
444 rg = ofbi->region;
446 down_write_nested(&rg->lock, rg->id);
447 atomic_inc(&rg->lock_count);
449 if (atomic_read(&rg->map_count)) {
450 r = -EBUSY;
451 goto out;
454 for (i = 0; i < fbdev->num_fbs; i++) {
455 struct omapfb_info *ofbi2 = FB2OFB(fbdev->fbs[i]);
456 int j;
458 if (ofbi2->region != rg)
459 continue;
461 for (j = 0; j < ofbi2->num_overlays; j++) {
462 struct omap_overlay *ovl;
463 ovl = ofbi2->overlays[j];
464 if (ovl->is_enabled(ovl)) {
465 r = -EBUSY;
466 goto out;
471 if (size != ofbi->region->size) {
472 r = omapfb_realloc_fbmem(fbi, size, ofbi->region->type);
473 if (r) {
474 dev_err(dev, "realloc fbmem failed\n");
475 goto out;
479 r = count;
480 out:
481 atomic_dec(&rg->lock_count);
482 up_write(&rg->lock);
484 unlock_fb_info(fbi);
486 return r;
489 static ssize_t show_phys(struct device *dev,
490 struct device_attribute *attr, char *buf)
492 struct fb_info *fbi = dev_get_drvdata(dev);
493 struct omapfb_info *ofbi = FB2OFB(fbi);
495 return snprintf(buf, PAGE_SIZE, "%0x\n", ofbi->region->paddr);
498 static ssize_t show_virt(struct device *dev,
499 struct device_attribute *attr, char *buf)
501 struct fb_info *fbi = dev_get_drvdata(dev);
502 struct omapfb_info *ofbi = FB2OFB(fbi);
504 return snprintf(buf, PAGE_SIZE, "%p\n", ofbi->region->vaddr);
507 static ssize_t show_upd_mode(struct device *dev,
508 struct device_attribute *attr, char *buf)
510 struct fb_info *fbi = dev_get_drvdata(dev);
511 enum omapfb_update_mode mode;
512 int r;
514 r = omapfb_get_update_mode(fbi, &mode);
516 if (r)
517 return r;
519 return snprintf(buf, PAGE_SIZE, "%u\n", (unsigned)mode);
522 static ssize_t store_upd_mode(struct device *dev, struct device_attribute *attr,
523 const char *buf, size_t count)
525 struct fb_info *fbi = dev_get_drvdata(dev);
526 unsigned mode;
527 int r;
529 r = kstrtouint(buf, 0, &mode);
530 if (r)
531 return r;
533 r = omapfb_set_update_mode(fbi, mode);
534 if (r)
535 return r;
537 return count;
540 static struct device_attribute omapfb_attrs[] = {
541 __ATTR(rotate_type, S_IRUGO | S_IWUSR, show_rotate_type,
542 store_rotate_type),
543 __ATTR(mirror, S_IRUGO | S_IWUSR, show_mirror, store_mirror),
544 __ATTR(size, S_IRUGO | S_IWUSR, show_size, store_size),
545 __ATTR(overlays, S_IRUGO | S_IWUSR, show_overlays, store_overlays),
546 __ATTR(overlays_rotate, S_IRUGO | S_IWUSR, show_overlays_rotate,
547 store_overlays_rotate),
548 __ATTR(phys_addr, S_IRUGO, show_phys, NULL),
549 __ATTR(virt_addr, S_IRUGO, show_virt, NULL),
550 __ATTR(update_mode, S_IRUGO | S_IWUSR, show_upd_mode, store_upd_mode),
553 int omapfb_create_sysfs(struct omapfb2_device *fbdev)
555 int i;
556 int r;
558 DBG("create sysfs for fbs\n");
559 for (i = 0; i < fbdev->num_fbs; i++) {
560 int t;
561 for (t = 0; t < ARRAY_SIZE(omapfb_attrs); t++) {
562 r = device_create_file(fbdev->fbs[i]->dev,
563 &omapfb_attrs[t]);
565 if (r) {
566 dev_err(fbdev->dev, "failed to create sysfs "
567 "file\n");
568 return r;
573 return 0;
576 void omapfb_remove_sysfs(struct omapfb2_device *fbdev)
578 int i, t;
580 DBG("remove sysfs for fbs\n");
581 for (i = 0; i < fbdev->num_fbs; i++) {
582 for (t = 0; t < ARRAY_SIZE(omapfb_attrs); t++)
583 device_remove_file(fbdev->fbs[i]->dev,
584 &omapfb_attrs[t]);