]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/video/omap2/omapfb/omapfb-sysfs.c
OMAP: DSS2: Convert simple/strict_strto* to kstrto*
[mv-sheeva.git] / drivers / video / omap2 / omapfb / omapfb-sysfs.c
1 /*
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
32 #include <video/omapdss.h>
33 #include <plat/vrfb.h>
34
35 #include "omapfb.h"
36
37 static 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
46 static 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);
52         struct omapfb2_mem_region *rg;
53         int rot_type;
54         int r;
55
56         r = kstrtoint(buf, 0, &rot_type);
57         if (r)
58                 return r;
59
60         if (rot_type != OMAP_DSS_ROT_DMA && rot_type != OMAP_DSS_ROT_VRFB)
61                 return -EINVAL;
62
63         if (!lock_fb_info(fbi))
64                 return -ENODEV;
65
66         r = 0;
67         if (rot_type == ofbi->rotation_type)
68                 goto out;
69
70         rg = omapfb_get_mem_region(ofbi->region);
71
72         if (rg->size) {
73                 r = -EBUSY;
74                 goto put_region;
75         }
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          */
83 put_region:
84         omapfb_put_mem_region(rg);
85 out:
86         unlock_fb_info(fbi);
87
88         return r ? r : count;
89 }
90
91
92 static 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
101 static 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);
107         int mirror;
108         int r;
109         struct fb_var_screeninfo new_var;
110
111         r = kstrtoint(buf, 0, &mirror);
112         if (r)
113                 return r;
114
115         mirror = !!mirror;
116
117         if (!lock_fb_info(fbi))
118                 return -ENODEV;
119
120         ofbi->mirror = mirror;
121
122         omapfb_get_mem_region(ofbi->region);
123
124         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;
137 out:
138         omapfb_put_mem_region(ofbi->region);
139
140         unlock_fb_info(fbi);
141
142         return r;
143 }
144
145 static 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
154         if (!lock_fb_info(fbi))
155                 return -ENODEV;
156         omapfb_lock(fbdev);
157
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
172         omapfb_unlock(fbdev);
173         unlock_fb_info(fbi);
174
175         return l;
176 }
177
178 static 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
195 static 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
213         if (!lock_fb_info(fbi))
214                 return -ENODEV;
215         omapfb_lock(fbdev);
216
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
278                 omapfb_get_mem_region(ofbi->region);
279
280                 omapfb_overlay_enable(ovl, 0);
281
282                 if (ovl->manager)
283                         ovl->manager->apply(ovl->manager);
284
285                 omapfb_put_mem_region(ofbi->region);
286
287                 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) {
319                 omapfb_get_mem_region(ofbi->region);
320
321                 r = omapfb_apply_changes(fbi, 0);
322
323                 omapfb_put_mem_region(ofbi->region);
324
325                 if (r)
326                         goto out;
327         }
328
329         r = count;
330 out:
331         omapfb_unlock(fbdev);
332         unlock_fb_info(fbi);
333
334         return r;
335 }
336
337 static 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
345         if (!lock_fb_info(fbi))
346                 return -ENODEV;
347
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
360 static 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
374         if (!lock_fb_info(fbi))
375                 return -ENODEV;
376
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
412                 omapfb_get_mem_region(ofbi->region);
413
414                 r = omapfb_apply_changes(fbi, 0);
415
416                 omapfb_put_mem_region(ofbi->region);
417
418                 if (r)
419                         goto out;
420
421                 /* FIXME error handling? */
422         }
423
424         r = count;
425 out:
426         unlock_fb_info(fbi);
427
428         return r;
429 }
430
431 static 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
437         return snprintf(buf, PAGE_SIZE, "%lu\n", ofbi->region->size);
438 }
439
440 static 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);
445         struct omapfb2_device *fbdev = ofbi->fbdev;
446         struct omapfb2_mem_region *rg;
447         unsigned long size;
448         int r;
449         int i;
450
451         r = kstrtoul(buf, 0, &size);
452         if (r)
453                 return r;
454
455         size = PAGE_ALIGN(size);
456
457         if (!lock_fb_info(fbi))
458                 return -ENODEV;
459
460         rg = ofbi->region;
461
462         down_write_nested(&rg->lock, rg->id);
463         atomic_inc(&rg->lock_count);
464
465         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                         }
482                 }
483         }
484
485         if (size != ofbi->region->size) {
486                 r = omapfb_realloc_fbmem(fbi, size, ofbi->region->type);
487                 if (r) {
488                         dev_err(dev, "realloc fbmem failed\n");
489                         goto out;
490                 }
491         }
492
493         r = count;
494 out:
495         atomic_dec(&rg->lock_count);
496         up_write(&rg->lock);
497
498         unlock_fb_info(fbi);
499
500         return r;
501 }
502
503 static 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
509         return snprintf(buf, PAGE_SIZE, "%0x\n", ofbi->region->paddr);
510 }
511
512 static 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
518         return snprintf(buf, PAGE_SIZE, "%p\n", ofbi->region->vaddr);
519 }
520
521 static 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
533 int 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
556 void 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