drm/nouveau: consume the return of large GSP message
[drm/drm-misc.git] / drivers / video / fbdev / omap2 / omapfb / omapfb-sysfs.c
blob831b2c2fbdf9ae709edf7050fc779fdc0e3d94a5
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/kstrtox.h>
19 #include <linux/mm.h>
20 #include <linux/omapfb.h>
22 #include <video/omapfb_dss.h>
23 #include <video/omapvrfb.h>
25 #include "omapfb.h"
27 static ssize_t show_rotate_type(struct device *dev,
28 struct device_attribute *attr, char *buf)
30 struct fb_info *fbi = dev_get_drvdata(dev);
31 struct omapfb_info *ofbi = FB2OFB(fbi);
33 return sysfs_emit(buf, "%d\n", ofbi->rotation_type);
36 static ssize_t store_rotate_type(struct device *dev,
37 struct device_attribute *attr,
38 const char *buf, size_t count)
40 struct fb_info *fbi = dev_get_drvdata(dev);
41 struct omapfb_info *ofbi = FB2OFB(fbi);
42 struct omapfb2_mem_region *rg;
43 int rot_type;
44 int r;
46 r = kstrtoint(buf, 0, &rot_type);
47 if (r)
48 return r;
50 if (rot_type != OMAP_DSS_ROT_DMA && rot_type != OMAP_DSS_ROT_VRFB)
51 return -EINVAL;
53 lock_fb_info(fbi);
55 r = 0;
56 if (rot_type == ofbi->rotation_type)
57 goto out;
59 rg = omapfb_get_mem_region(ofbi->region);
61 if (rg->size) {
62 r = -EBUSY;
63 goto put_region;
66 ofbi->rotation_type = rot_type;
69 * Since the VRAM for this FB is not allocated at the moment we don't
70 * need to do any further parameter checking at this point.
72 put_region:
73 omapfb_put_mem_region(rg);
74 out:
75 unlock_fb_info(fbi);
77 return r ? r : count;
81 static ssize_t show_mirror(struct device *dev,
82 struct device_attribute *attr, char *buf)
84 struct fb_info *fbi = dev_get_drvdata(dev);
85 struct omapfb_info *ofbi = FB2OFB(fbi);
87 return sysfs_emit(buf, "%d\n", ofbi->mirror);
90 static ssize_t store_mirror(struct device *dev,
91 struct device_attribute *attr,
92 const char *buf, size_t count)
94 struct fb_info *fbi = dev_get_drvdata(dev);
95 struct omapfb_info *ofbi = FB2OFB(fbi);
96 bool mirror;
97 int r;
98 struct fb_var_screeninfo new_var;
100 r = kstrtobool(buf, &mirror);
101 if (r)
102 return r;
104 lock_fb_info(fbi);
106 ofbi->mirror = mirror;
108 omapfb_get_mem_region(ofbi->region);
110 memcpy(&new_var, &fbi->var, sizeof(new_var));
111 r = check_fb_var(fbi, &new_var);
112 if (r)
113 goto out;
114 memcpy(&fbi->var, &new_var, sizeof(fbi->var));
116 set_fb_fix(fbi);
118 r = omapfb_apply_changes(fbi, 0);
119 if (r)
120 goto out;
122 r = count;
123 out:
124 omapfb_put_mem_region(ofbi->region);
126 unlock_fb_info(fbi);
128 return r;
131 static ssize_t show_overlays(struct device *dev,
132 struct device_attribute *attr, char *buf)
134 struct fb_info *fbi = dev_get_drvdata(dev);
135 struct omapfb_info *ofbi = FB2OFB(fbi);
136 struct omapfb2_device *fbdev = ofbi->fbdev;
137 ssize_t l = 0;
138 int t;
140 lock_fb_info(fbi);
141 omapfb_lock(fbdev);
143 for (t = 0; t < ofbi->num_overlays; t++) {
144 struct omap_overlay *ovl = ofbi->overlays[t];
145 int ovlnum;
147 for (ovlnum = 0; ovlnum < fbdev->num_overlays; ++ovlnum)
148 if (ovl == fbdev->overlays[ovlnum])
149 break;
151 l += scnprintf(buf + l, PAGE_SIZE - l, "%s%d",
152 t == 0 ? "" : ",", ovlnum);
155 l += scnprintf(buf + l, PAGE_SIZE - l, "\n");
157 omapfb_unlock(fbdev);
158 unlock_fb_info(fbi);
160 return l;
163 static struct omapfb_info *get_overlay_fb(struct omapfb2_device *fbdev,
164 struct omap_overlay *ovl)
166 int i, t;
168 for (i = 0; i < fbdev->num_fbs; i++) {
169 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
171 for (t = 0; t < ofbi->num_overlays; t++) {
172 if (ofbi->overlays[t] == ovl)
173 return ofbi;
177 return NULL;
180 static ssize_t store_overlays(struct device *dev, struct device_attribute *attr,
181 const char *buf, size_t count)
183 struct fb_info *fbi = dev_get_drvdata(dev);
184 struct omapfb_info *ofbi = FB2OFB(fbi);
185 struct omapfb2_device *fbdev = ofbi->fbdev;
186 struct omap_overlay *ovls[OMAPFB_MAX_OVL_PER_FB];
187 struct omap_overlay *ovl;
188 int num_ovls, r, i;
189 int len;
190 bool added = false;
192 num_ovls = 0;
194 len = strlen(buf);
195 if (buf[len - 1] == '\n')
196 len = len - 1;
198 lock_fb_info(fbi);
199 omapfb_lock(fbdev);
201 if (len > 0) {
202 char *p = (char *)buf;
203 int ovlnum;
205 while (p < buf + len) {
206 int found;
207 if (num_ovls == OMAPFB_MAX_OVL_PER_FB) {
208 r = -EINVAL;
209 goto out;
212 ovlnum = simple_strtoul(p, &p, 0);
213 if (ovlnum > fbdev->num_overlays) {
214 r = -EINVAL;
215 goto out;
218 found = 0;
219 for (i = 0; i < num_ovls; ++i) {
220 if (ovls[i] == fbdev->overlays[ovlnum]) {
221 found = 1;
222 break;
226 if (!found)
227 ovls[num_ovls++] = fbdev->overlays[ovlnum];
229 p++;
233 for (i = 0; i < num_ovls; ++i) {
234 struct omapfb_info *ofbi2 = get_overlay_fb(fbdev, ovls[i]);
235 if (ofbi2 && ofbi2 != ofbi) {
236 dev_err(fbdev->dev, "overlay already in use\n");
237 r = -EINVAL;
238 goto out;
242 /* detach unused overlays */
243 for (i = 0; i < ofbi->num_overlays; ++i) {
244 int t, found;
246 ovl = ofbi->overlays[i];
248 found = 0;
250 for (t = 0; t < num_ovls; ++t) {
251 if (ovl == ovls[t]) {
252 found = 1;
253 break;
257 if (found)
258 continue;
260 DBG("detaching %d\n", ofbi->overlays[i]->id);
262 omapfb_get_mem_region(ofbi->region);
264 omapfb_overlay_enable(ovl, 0);
266 if (ovl->manager)
267 ovl->manager->apply(ovl->manager);
269 omapfb_put_mem_region(ofbi->region);
271 for (t = i + 1; t < ofbi->num_overlays; t++) {
272 ofbi->rotation[t-1] = ofbi->rotation[t];
273 ofbi->overlays[t-1] = ofbi->overlays[t];
276 ofbi->num_overlays--;
277 i--;
280 for (i = 0; i < num_ovls; ++i) {
281 int t, found;
283 ovl = ovls[i];
285 found = 0;
287 for (t = 0; t < ofbi->num_overlays; ++t) {
288 if (ovl == ofbi->overlays[t]) {
289 found = 1;
290 break;
294 if (found)
295 continue;
296 ofbi->rotation[ofbi->num_overlays] = 0;
297 ofbi->overlays[ofbi->num_overlays++] = ovl;
299 added = true;
302 if (added) {
303 omapfb_get_mem_region(ofbi->region);
305 r = omapfb_apply_changes(fbi, 0);
307 omapfb_put_mem_region(ofbi->region);
309 if (r)
310 goto out;
313 r = count;
314 out:
315 omapfb_unlock(fbdev);
316 unlock_fb_info(fbi);
318 return r;
321 static ssize_t show_overlays_rotate(struct device *dev,
322 struct device_attribute *attr, char *buf)
324 struct fb_info *fbi = dev_get_drvdata(dev);
325 struct omapfb_info *ofbi = FB2OFB(fbi);
326 ssize_t l = 0;
327 int t;
329 lock_fb_info(fbi);
331 for (t = 0; t < ofbi->num_overlays; t++) {
332 l += scnprintf(buf + l, PAGE_SIZE - l, "%s%d",
333 t == 0 ? "" : ",", ofbi->rotation[t]);
336 l += scnprintf(buf + l, PAGE_SIZE - l, "\n");
338 unlock_fb_info(fbi);
340 return l;
343 static ssize_t store_overlays_rotate(struct device *dev,
344 struct device_attribute *attr, const char *buf, size_t count)
346 struct fb_info *fbi = dev_get_drvdata(dev);
347 struct omapfb_info *ofbi = FB2OFB(fbi);
348 int num_ovls = 0, r, i;
349 int len;
350 bool changed = false;
351 u8 rotation[OMAPFB_MAX_OVL_PER_FB];
353 len = strlen(buf);
354 if (buf[len - 1] == '\n')
355 len = len - 1;
357 lock_fb_info(fbi);
359 if (len > 0) {
360 char *p = (char *)buf;
362 while (p < buf + len) {
363 int rot;
365 if (num_ovls == ofbi->num_overlays) {
366 r = -EINVAL;
367 goto out;
370 rot = simple_strtoul(p, &p, 0);
371 if (rot < 0 || rot > 3) {
372 r = -EINVAL;
373 goto out;
376 if (ofbi->rotation[num_ovls] != rot)
377 changed = true;
379 rotation[num_ovls++] = rot;
381 p++;
385 if (num_ovls != ofbi->num_overlays) {
386 r = -EINVAL;
387 goto out;
390 if (changed) {
391 for (i = 0; i < num_ovls; ++i)
392 ofbi->rotation[i] = rotation[i];
394 omapfb_get_mem_region(ofbi->region);
396 r = omapfb_apply_changes(fbi, 0);
398 omapfb_put_mem_region(ofbi->region);
400 if (r)
401 goto out;
403 /* FIXME error handling? */
406 r = count;
407 out:
408 unlock_fb_info(fbi);
410 return r;
413 static ssize_t show_size(struct device *dev,
414 struct device_attribute *attr, char *buf)
416 struct fb_info *fbi = dev_get_drvdata(dev);
417 struct omapfb_info *ofbi = FB2OFB(fbi);
419 return sysfs_emit(buf, "%lu\n", ofbi->region->size);
422 static ssize_t store_size(struct device *dev, struct device_attribute *attr,
423 const char *buf, size_t count)
425 struct fb_info *fbi = dev_get_drvdata(dev);
426 struct omapfb_info *ofbi = FB2OFB(fbi);
427 struct omapfb2_device *fbdev = ofbi->fbdev;
428 struct omap_dss_device *display = fb2display(fbi);
429 struct omapfb2_mem_region *rg;
430 unsigned long size;
431 int r;
432 int i;
434 r = kstrtoul(buf, 0, &size);
435 if (r)
436 return r;
438 size = PAGE_ALIGN(size);
440 lock_fb_info(fbi);
442 if (display && display->driver->sync)
443 display->driver->sync(display);
445 rg = ofbi->region;
447 down_write_nested(&rg->lock, rg->id);
448 atomic_inc(&rg->lock_count);
450 if (atomic_read(&rg->map_count)) {
451 r = -EBUSY;
452 goto out;
455 for (i = 0; i < fbdev->num_fbs; i++) {
456 struct omapfb_info *ofbi2 = FB2OFB(fbdev->fbs[i]);
457 int j;
459 if (ofbi2->region != rg)
460 continue;
462 for (j = 0; j < ofbi2->num_overlays; j++) {
463 struct omap_overlay *ovl;
464 ovl = ofbi2->overlays[j];
465 if (ovl->is_enabled(ovl)) {
466 r = -EBUSY;
467 goto out;
472 if (size != ofbi->region->size) {
473 r = omapfb_realloc_fbmem(fbi, size, ofbi->region->type);
474 if (r) {
475 dev_err(dev, "realloc fbmem failed\n");
476 goto out;
480 r = count;
481 out:
482 atomic_dec(&rg->lock_count);
483 up_write(&rg->lock);
485 unlock_fb_info(fbi);
487 return r;
490 static ssize_t show_phys(struct device *dev,
491 struct device_attribute *attr, char *buf)
493 struct fb_info *fbi = dev_get_drvdata(dev);
494 struct omapfb_info *ofbi = FB2OFB(fbi);
496 return sysfs_emit(buf, "%0x\n", ofbi->region->paddr);
499 static ssize_t show_virt(struct device *dev,
500 struct device_attribute *attr, char *buf)
502 struct fb_info *fbi = dev_get_drvdata(dev);
503 struct omapfb_info *ofbi = FB2OFB(fbi);
505 return sysfs_emit(buf, "%p\n", ofbi->region->vaddr);
508 static ssize_t show_upd_mode(struct device *dev,
509 struct device_attribute *attr, char *buf)
511 struct fb_info *fbi = dev_get_drvdata(dev);
512 enum omapfb_update_mode mode;
513 int r;
515 r = omapfb_get_update_mode(fbi, &mode);
517 if (r)
518 return r;
520 return sysfs_emit(buf, "%u\n", (unsigned int)mode);
523 static ssize_t store_upd_mode(struct device *dev, struct device_attribute *attr,
524 const char *buf, size_t count)
526 struct fb_info *fbi = dev_get_drvdata(dev);
527 unsigned mode;
528 int r;
530 r = kstrtouint(buf, 0, &mode);
531 if (r)
532 return r;
534 r = omapfb_set_update_mode(fbi, mode);
535 if (r)
536 return r;
538 return count;
541 static struct device_attribute omapfb_attrs[] = {
542 __ATTR(rotate_type, S_IRUGO | S_IWUSR, show_rotate_type,
543 store_rotate_type),
544 __ATTR(mirror, S_IRUGO | S_IWUSR, show_mirror, store_mirror),
545 __ATTR(size, S_IRUGO | S_IWUSR, show_size, store_size),
546 __ATTR(overlays, S_IRUGO | S_IWUSR, show_overlays, store_overlays),
547 __ATTR(overlays_rotate, S_IRUGO | S_IWUSR, show_overlays_rotate,
548 store_overlays_rotate),
549 __ATTR(phys_addr, S_IRUGO, show_phys, NULL),
550 __ATTR(virt_addr, S_IRUGO, show_virt, NULL),
551 __ATTR(update_mode, S_IRUGO | S_IWUSR, show_upd_mode, store_upd_mode),
554 int omapfb_create_sysfs(struct omapfb2_device *fbdev)
556 int i;
557 int r;
559 DBG("create sysfs for fbs\n");
560 for (i = 0; i < fbdev->num_fbs; i++) {
561 int t;
562 for (t = 0; t < ARRAY_SIZE(omapfb_attrs); t++) {
563 r = device_create_file(fbdev->fbs[i]->dev,
564 &omapfb_attrs[t]);
566 if (r) {
567 dev_err(fbdev->dev, "failed to create sysfs "
568 "file\n");
569 return r;
574 return 0;
577 void omapfb_remove_sysfs(struct omapfb2_device *fbdev)
579 int i, t;
581 DBG("remove sysfs for fbs\n");
582 for (i = 0; i < fbdev->num_fbs; i++) {
583 for (t = 0; t < ARRAY_SIZE(omapfb_attrs); t++)
584 device_remove_file(fbdev->fbs[i]->dev,
585 &omapfb_attrs[t]);