Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ericvh...
[pandora-kernel.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 <plat/display.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         enum omap_dss_rotation_type rot_type;
53         int r;
54
55         rot_type = simple_strtoul(buf, NULL, 0);
56
57         if (rot_type != OMAP_DSS_ROT_DMA && rot_type != OMAP_DSS_ROT_VRFB)
58                 return -EINVAL;
59
60         if (!lock_fb_info(fbi))
61                 return -ENODEV;
62
63         r = 0;
64         if (rot_type == ofbi->rotation_type)
65                 goto out;
66
67         if (ofbi->region.size) {
68                 r = -EBUSY;
69                 goto out;
70         }
71
72         ofbi->rotation_type = rot_type;
73
74         /*
75          * Since the VRAM for this FB is not allocated at the moment we don't
76          * need to do any further parameter checking at this point.
77          */
78 out:
79         unlock_fb_info(fbi);
80
81         return r ? r : count;
82 }
83
84
85 static ssize_t show_mirror(struct device *dev,
86                 struct device_attribute *attr, char *buf)
87 {
88         struct fb_info *fbi = dev_get_drvdata(dev);
89         struct omapfb_info *ofbi = FB2OFB(fbi);
90
91         return snprintf(buf, PAGE_SIZE, "%d\n", ofbi->mirror);
92 }
93
94 static ssize_t store_mirror(struct device *dev,
95                 struct device_attribute *attr,
96                 const char *buf, size_t count)
97 {
98         struct fb_info *fbi = dev_get_drvdata(dev);
99         struct omapfb_info *ofbi = FB2OFB(fbi);
100         bool mirror;
101         int r;
102         struct fb_var_screeninfo new_var;
103
104         mirror = simple_strtoul(buf, NULL, 0);
105
106         if (mirror != 0 && mirror != 1)
107                 return -EINVAL;
108
109         if (!lock_fb_info(fbi))
110                 return -ENODEV;
111
112         ofbi->mirror = mirror;
113
114         memcpy(&new_var, &fbi->var, sizeof(new_var));
115         r = check_fb_var(fbi, &new_var);
116         if (r)
117                 goto out;
118         memcpy(&fbi->var, &new_var, sizeof(fbi->var));
119
120         set_fb_fix(fbi);
121
122         r = omapfb_apply_changes(fbi, 0);
123         if (r)
124                 goto out;
125
126         r = count;
127 out:
128         unlock_fb_info(fbi);
129
130         return r;
131 }
132
133 static ssize_t show_overlays(struct device *dev,
134                 struct device_attribute *attr, char *buf)
135 {
136         struct fb_info *fbi = dev_get_drvdata(dev);
137         struct omapfb_info *ofbi = FB2OFB(fbi);
138         struct omapfb2_device *fbdev = ofbi->fbdev;
139         ssize_t l = 0;
140         int t;
141
142         if (!lock_fb_info(fbi))
143                 return -ENODEV;
144         omapfb_lock(fbdev);
145
146         for (t = 0; t < ofbi->num_overlays; t++) {
147                 struct omap_overlay *ovl = ofbi->overlays[t];
148                 int ovlnum;
149
150                 for (ovlnum = 0; ovlnum < fbdev->num_overlays; ++ovlnum)
151                         if (ovl == fbdev->overlays[ovlnum])
152                                 break;
153
154                 l += snprintf(buf + l, PAGE_SIZE - l, "%s%d",
155                                 t == 0 ? "" : ",", ovlnum);
156         }
157
158         l += snprintf(buf + l, PAGE_SIZE - l, "\n");
159
160         omapfb_unlock(fbdev);
161         unlock_fb_info(fbi);
162
163         return l;
164 }
165
166 static struct omapfb_info *get_overlay_fb(struct omapfb2_device *fbdev,
167                 struct omap_overlay *ovl)
168 {
169         int i, t;
170
171         for (i = 0; i < fbdev->num_fbs; i++) {
172                 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
173
174                 for (t = 0; t < ofbi->num_overlays; t++) {
175                         if (ofbi->overlays[t] == ovl)
176                                 return ofbi;
177                 }
178         }
179
180         return NULL;
181 }
182
183 static ssize_t store_overlays(struct device *dev, struct device_attribute *attr,
184                 const char *buf, size_t count)
185 {
186         struct fb_info *fbi = dev_get_drvdata(dev);
187         struct omapfb_info *ofbi = FB2OFB(fbi);
188         struct omapfb2_device *fbdev = ofbi->fbdev;
189         struct omap_overlay *ovls[OMAPFB_MAX_OVL_PER_FB];
190         struct omap_overlay *ovl;
191         int num_ovls, r, i;
192         int len;
193         bool added = false;
194
195         num_ovls = 0;
196
197         len = strlen(buf);
198         if (buf[len - 1] == '\n')
199                 len = len - 1;
200
201         if (!lock_fb_info(fbi))
202                 return -ENODEV;
203         omapfb_lock(fbdev);
204
205         if (len > 0) {
206                 char *p = (char *)buf;
207                 int ovlnum;
208
209                 while (p < buf + len) {
210                         int found;
211                         if (num_ovls == OMAPFB_MAX_OVL_PER_FB) {
212                                 r = -EINVAL;
213                                 goto out;
214                         }
215
216                         ovlnum = simple_strtoul(p, &p, 0);
217                         if (ovlnum > fbdev->num_overlays) {
218                                 r = -EINVAL;
219                                 goto out;
220                         }
221
222                         found = 0;
223                         for (i = 0; i < num_ovls; ++i) {
224                                 if (ovls[i] == fbdev->overlays[ovlnum]) {
225                                         found = 1;
226                                         break;
227                                 }
228                         }
229
230                         if (!found)
231                                 ovls[num_ovls++] = fbdev->overlays[ovlnum];
232
233                         p++;
234                 }
235         }
236
237         for (i = 0; i < num_ovls; ++i) {
238                 struct omapfb_info *ofbi2 = get_overlay_fb(fbdev, ovls[i]);
239                 if (ofbi2 && ofbi2 != ofbi) {
240                         dev_err(fbdev->dev, "overlay already in use\n");
241                         r = -EINVAL;
242                         goto out;
243                 }
244         }
245
246         /* detach unused overlays */
247         for (i = 0; i < ofbi->num_overlays; ++i) {
248                 int t, found;
249
250                 ovl = ofbi->overlays[i];
251
252                 found = 0;
253
254                 for (t = 0; t < num_ovls; ++t) {
255                         if (ovl == ovls[t]) {
256                                 found = 1;
257                                 break;
258                         }
259                 }
260
261                 if (found)
262                         continue;
263
264                 DBG("detaching %d\n", ofbi->overlays[i]->id);
265
266                 omapfb_overlay_enable(ovl, 0);
267
268                 if (ovl->manager)
269                         ovl->manager->apply(ovl->manager);
270
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];
274                 }
275
276                 ofbi->num_overlays--;
277                 i--;
278         }
279
280         for (i = 0; i < num_ovls; ++i) {
281                 int t, found;
282
283                 ovl = ovls[i];
284
285                 found = 0;
286
287                 for (t = 0; t < ofbi->num_overlays; ++t) {
288                         if (ovl == ofbi->overlays[t]) {
289                                 found = 1;
290                                 break;
291                         }
292                 }
293
294                 if (found)
295                         continue;
296                 ofbi->rotation[ofbi->num_overlays] = 0;
297                 ofbi->overlays[ofbi->num_overlays++] = ovl;
298
299                 added = true;
300         }
301
302         if (added) {
303                 r = omapfb_apply_changes(fbi, 0);
304                 if (r)
305                         goto out;
306         }
307
308         r = count;
309 out:
310         omapfb_unlock(fbdev);
311         unlock_fb_info(fbi);
312
313         return r;
314 }
315
316 static ssize_t show_overlays_rotate(struct device *dev,
317                 struct device_attribute *attr, char *buf)
318 {
319         struct fb_info *fbi = dev_get_drvdata(dev);
320         struct omapfb_info *ofbi = FB2OFB(fbi);
321         ssize_t l = 0;
322         int t;
323
324         if (!lock_fb_info(fbi))
325                 return -ENODEV;
326
327         for (t = 0; t < ofbi->num_overlays; t++) {
328                 l += snprintf(buf + l, PAGE_SIZE - l, "%s%d",
329                                 t == 0 ? "" : ",", ofbi->rotation[t]);
330         }
331
332         l += snprintf(buf + l, PAGE_SIZE - l, "\n");
333
334         unlock_fb_info(fbi);
335
336         return l;
337 }
338
339 static ssize_t store_overlays_rotate(struct device *dev,
340                 struct device_attribute *attr, const char *buf, size_t count)
341 {
342         struct fb_info *fbi = dev_get_drvdata(dev);
343         struct omapfb_info *ofbi = FB2OFB(fbi);
344         int num_ovls = 0, r, i;
345         int len;
346         bool changed = false;
347         u8 rotation[OMAPFB_MAX_OVL_PER_FB];
348
349         len = strlen(buf);
350         if (buf[len - 1] == '\n')
351                 len = len - 1;
352
353         if (!lock_fb_info(fbi))
354                 return -ENODEV;
355
356         if (len > 0) {
357                 char *p = (char *)buf;
358
359                 while (p < buf + len) {
360                         int rot;
361
362                         if (num_ovls == ofbi->num_overlays) {
363                                 r = -EINVAL;
364                                 goto out;
365                         }
366
367                         rot = simple_strtoul(p, &p, 0);
368                         if (rot < 0 || rot > 3) {
369                                 r = -EINVAL;
370                                 goto out;
371                         }
372
373                         if (ofbi->rotation[num_ovls] != rot)
374                                 changed = true;
375
376                         rotation[num_ovls++] = rot;
377
378                         p++;
379                 }
380         }
381
382         if (num_ovls != ofbi->num_overlays) {
383                 r = -EINVAL;
384                 goto out;
385         }
386
387         if (changed) {
388                 for (i = 0; i < num_ovls; ++i)
389                         ofbi->rotation[i] = rotation[i];
390
391                 r = omapfb_apply_changes(fbi, 0);
392                 if (r)
393                         goto out;
394
395                 /* FIXME error handling? */
396         }
397
398         r = count;
399 out:
400         unlock_fb_info(fbi);
401
402         return r;
403 }
404
405 static ssize_t show_size(struct device *dev,
406                 struct device_attribute *attr, char *buf)
407 {
408         struct fb_info *fbi = dev_get_drvdata(dev);
409         struct omapfb_info *ofbi = FB2OFB(fbi);
410
411         return snprintf(buf, PAGE_SIZE, "%lu\n", ofbi->region.size);
412 }
413
414 static ssize_t store_size(struct device *dev, struct device_attribute *attr,
415                 const char *buf, size_t count)
416 {
417         struct fb_info *fbi = dev_get_drvdata(dev);
418         struct omapfb_info *ofbi = FB2OFB(fbi);
419         unsigned long size;
420         int r;
421         int i;
422
423         size = PAGE_ALIGN(simple_strtoul(buf, NULL, 0));
424
425         if (!lock_fb_info(fbi))
426                 return -ENODEV;
427
428         for (i = 0; i < ofbi->num_overlays; i++) {
429                 if (ofbi->overlays[i]->info.enabled) {
430                         r = -EBUSY;
431                         goto out;
432                 }
433         }
434
435         if (size != ofbi->region.size) {
436                 r = omapfb_realloc_fbmem(fbi, size, ofbi->region.type);
437                 if (r) {
438                         dev_err(dev, "realloc fbmem failed\n");
439                         goto out;
440                 }
441         }
442
443         r = count;
444 out:
445         unlock_fb_info(fbi);
446
447         return r;
448 }
449
450 static ssize_t show_phys(struct device *dev,
451                 struct device_attribute *attr, char *buf)
452 {
453         struct fb_info *fbi = dev_get_drvdata(dev);
454         struct omapfb_info *ofbi = FB2OFB(fbi);
455
456         return snprintf(buf, PAGE_SIZE, "%0x\n", ofbi->region.paddr);
457 }
458
459 static ssize_t show_virt(struct device *dev,
460                 struct device_attribute *attr, char *buf)
461 {
462         struct fb_info *fbi = dev_get_drvdata(dev);
463         struct omapfb_info *ofbi = FB2OFB(fbi);
464
465         return snprintf(buf, PAGE_SIZE, "%p\n", ofbi->region.vaddr);
466 }
467
468 static struct device_attribute omapfb_attrs[] = {
469         __ATTR(rotate_type, S_IRUGO | S_IWUSR, show_rotate_type,
470                         store_rotate_type),
471         __ATTR(mirror, S_IRUGO | S_IWUSR, show_mirror, store_mirror),
472         __ATTR(size, S_IRUGO | S_IWUSR, show_size, store_size),
473         __ATTR(overlays, S_IRUGO | S_IWUSR, show_overlays, store_overlays),
474         __ATTR(overlays_rotate, S_IRUGO | S_IWUSR, show_overlays_rotate,
475                         store_overlays_rotate),
476         __ATTR(phys_addr, S_IRUGO, show_phys, NULL),
477         __ATTR(virt_addr, S_IRUGO, show_virt, NULL),
478 };
479
480 int omapfb_create_sysfs(struct omapfb2_device *fbdev)
481 {
482         int i;
483         int r;
484
485         DBG("create sysfs for fbs\n");
486         for (i = 0; i < fbdev->num_fbs; i++) {
487                 int t;
488                 for (t = 0; t < ARRAY_SIZE(omapfb_attrs); t++) {
489                         r = device_create_file(fbdev->fbs[i]->dev,
490                                         &omapfb_attrs[t]);
491
492                         if (r) {
493                                 dev_err(fbdev->dev, "failed to create sysfs "
494                                                 "file\n");
495                                 return r;
496                         }
497                 }
498         }
499
500         return 0;
501 }
502
503 void omapfb_remove_sysfs(struct omapfb2_device *fbdev)
504 {
505         int i, t;
506
507         DBG("remove sysfs for fbs\n");
508         for (i = 0; i < fbdev->num_fbs; i++) {
509                 for (t = 0; t < ARRAY_SIZE(omapfb_attrs); t++)
510                         device_remove_file(fbdev->fbs[i]->dev,
511                                         &omapfb_attrs[t]);
512         }
513 }
514