Merge tag 'lsk-v3.10-android-14.07' into develop-3.10
[firefly-linux-kernel-4.4.55.git] / drivers / video / rockchip / rkfb_sysfs.c
1 /*
2  * linux/drivers/video/rockchip/rkfb-sysfs.c
3  *
4  * Copyright (C) 2012 Rockchip Corporation
5  * Author: yxj<yxj@rock-chips.com>
6  *
7  * Some code and ideas taken from
8  *drivers/video/omap2/omapfb/omapfb-sys.c
9  *driver by Tomi Valkeinen.
10  *
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License version 2 as published by
14  * the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19  * more details.
20  *
21  * You should have received a copy of the GNU General Public License along with
22  * this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25 #include <linux/fb.h>
26 #include <linux/sysfs.h>
27 #include <linux/device.h>
28 #include <linux/uaccess.h>
29 #include <linux/platform_device.h>
30 #include <linux/kernel.h>
31 #include <linux/mm.h>
32 #include <asm/div64.h>
33 #include <linux/rk_screen.h>
34 #include <linux/rk_fb.h>
35
36 static ssize_t show_screen_info(struct device *dev,
37                                 struct device_attribute *attr, char *buf)
38 {
39         struct fb_info *fbi = dev_get_drvdata(dev);
40         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)fbi->par;
41         struct rk_screen *screen = dev_drv->screen0;
42         int fps;
43         u32 x = (screen->mode.left_margin + screen->mode.right_margin + screen->mode.xres + screen->mode.hsync_len);
44         u32 y = (screen->mode.upper_margin + screen->mode.lower_margin + screen->mode.yres + screen->mode.vsync_len);
45         u64 ft = (u64)x * y * (dev_drv->pixclock);      // one frame time ,(pico seconds)
46         fps = div64_u64(1000000000000llu, ft);
47         return snprintf(buf, PAGE_SIZE, "xres:%d\nyres:%d\nfps:%d\n",
48                         screen->mode.xres, screen->mode.yres, fps);
49 }
50
51 static ssize_t show_disp_info(struct device *dev,
52                               struct device_attribute *attr, char *buf)
53 {
54         struct fb_info *fbi = dev_get_drvdata(dev);
55         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)fbi->par;
56         int win_id = dev_drv->ops->fb_get_win_id(dev_drv, fbi->fix.id);
57         if (dev_drv->ops->get_disp_info)
58                 return dev_drv->ops->get_disp_info(dev_drv, buf, win_id);
59
60         return 0;
61 }
62
63 static ssize_t show_phys(struct device *dev,
64                          struct device_attribute *attr, char *buf)
65 {
66         struct fb_info *fbi = dev_get_drvdata(dev);
67         return snprintf(buf, PAGE_SIZE, "0x%lx-----0x%x\n",
68                         fbi->fix.smem_start, fbi->fix.smem_len);
69 }
70
71 static ssize_t show_virt(struct device *dev,
72                          struct device_attribute *attr, char *buf)
73 {
74         struct fb_info *fbi = dev_get_drvdata(dev);
75
76         return snprintf(buf, PAGE_SIZE, "0x%p-----0x%x\n",
77                         fbi->screen_base, fbi->fix.smem_len);
78 }
79
80 static ssize_t show_fb_state(struct device *dev,
81                              struct device_attribute *attr, char *buf)
82 {
83         struct fb_info *fbi = dev_get_drvdata(dev);
84         struct rk_lcdc_driver *dev_drv =
85             (struct rk_lcdc_driver *)fbi->par;
86         int win_id = dev_drv->ops->fb_get_win_id(dev_drv, fbi->fix.id);
87         int state = dev_drv->ops->get_win_state(dev_drv, win_id);
88         return snprintf(buf, PAGE_SIZE, "%s\n", state ? "enabled" : "disabled");
89
90 }
91
92 static ssize_t show_dual_mode(struct device *dev,
93                               struct device_attribute *attr, char *buf)
94 {
95         struct fb_info *fbi = dev_get_drvdata(dev);
96         struct rk_fb *rk_fb = dev_get_drvdata(fbi->device);
97         int mode= rk_fb->disp_mode; 
98         return snprintf(buf, PAGE_SIZE, "%d\n", mode);
99
100 }
101
102 static ssize_t set_fb_state(struct device *dev, struct device_attribute *attr,
103                             const char *buf, size_t count)
104 {
105         struct fb_info *fbi = dev_get_drvdata(dev);
106         struct rk_lcdc_driver *dev_drv =
107             (struct rk_lcdc_driver *)fbi->par;
108         int win_id = dev_drv->ops->fb_get_win_id(dev_drv, fbi->fix.id);
109         int state;
110         int ret;
111         ret = kstrtoint(buf, 0, &state);
112         if (ret) {
113                 return ret;
114         }
115         dev_drv->ops->open(dev_drv, win_id, state);
116         return count;
117 }
118
119 static ssize_t show_overlay(struct device *dev,
120                             struct device_attribute *attr, char *buf)
121 {
122         struct fb_info *fbi = dev_get_drvdata(dev);
123         struct rk_lcdc_driver *dev_drv =
124             (struct rk_lcdc_driver *)fbi->par;
125         int ovl;
126         ovl = dev_drv->ops->ovl_mgr(dev_drv, 0, 0);
127         if (ovl < 0) {
128                 return ovl;
129         }
130
131         return snprintf(buf, PAGE_SIZE, "%s\n",
132                         ovl ? "win0 on the top of win1" :
133                         "win1 on the top of win0");
134
135 }
136
137 static ssize_t set_overlay(struct device *dev, struct device_attribute *attr,
138                            const char *buf, size_t count)
139 {
140         struct fb_info *fbi = dev_get_drvdata(dev);
141         struct rk_lcdc_driver *dev_drv =
142             (struct rk_lcdc_driver *)fbi->par;
143         int ovl;
144         int ret;
145         ret = kstrtoint(buf, 0, &ovl);
146         if (ret) {
147                 return ret;
148         }
149         ret = dev_drv->ops->ovl_mgr(dev_drv, ovl, 1);
150         if (ret < 0) {
151                 return ret;
152         }
153
154         return count;
155 }
156
157 static ssize_t show_fps(struct device *dev,
158                         struct device_attribute *attr, char *buf)
159 {
160         struct fb_info *fbi = dev_get_drvdata(dev);
161         struct rk_lcdc_driver *dev_drv =
162             (struct rk_lcdc_driver *)fbi->par;
163         int fps;
164         fps = dev_drv->ops->fps_mgr(dev_drv, 0, 0);
165         if (fps < 0) {
166                 return fps;
167         }
168
169         return snprintf(buf, PAGE_SIZE, "fps:%d\n", fps);
170
171 }
172
173 static ssize_t set_fps(struct device *dev, struct device_attribute *attr,
174                        const char *buf, size_t count)
175 {
176         struct fb_info *fbi = dev_get_drvdata(dev);
177         struct rk_lcdc_driver *dev_drv =
178             (struct rk_lcdc_driver *)fbi->par;
179         int fps;
180         int ret;
181         ret = kstrtoint(buf, 0, &fps);
182         if (ret) {
183                 return ret;
184         }
185         ret = dev_drv->ops->fps_mgr(dev_drv, fps, 1);
186         if (ret < 0) {
187                 return ret;
188         }
189
190         return count;
191 }
192
193 static ssize_t show_fb_win_map(struct device *dev,
194                                struct device_attribute *attr, char *buf)
195 {
196         int ret;
197         struct fb_info *fbi = dev_get_drvdata(dev);
198         struct rk_lcdc_driver *dev_drv =
199             (struct rk_lcdc_driver *)fbi->par;
200
201         mutex_lock(&dev_drv->fb_win_id_mutex);
202         ret =
203             snprintf(buf, PAGE_SIZE, "fb0:win%d\nfb1:win%d\nfb2:win%d\n",
204                      dev_drv->fb0_win_id, dev_drv->fb1_win_id,
205                      dev_drv->fb2_win_id);
206         mutex_unlock(&dev_drv->fb_win_id_mutex);
207
208         return ret;
209
210 }
211
212 static ssize_t set_fb_win_map(struct device *dev, struct device_attribute *attr,
213                               const char *buf, size_t count)
214 {
215         struct fb_info *fbi = dev_get_drvdata(dev);
216         struct rk_lcdc_driver *dev_drv =
217             (struct rk_lcdc_driver *)fbi->par;
218         int order;
219         int ret;
220         ret = kstrtoint(buf, 0, &order);
221         if ((order != FB0_WIN2_FB1_WIN1_FB2_WIN0)
222             && (order != FB0_WIN1_FB1_WIN2_FB2_WIN0)
223             && (order != FB0_WIN2_FB1_WIN0_FB2_WIN1)
224             && (order != FB0_WIN0_FB1_WIN2_FB2_WIN1)
225             && (order != FB0_WIN0_FB1_WIN1_FB2_WIN2)
226             && (order != FB0_WIN1_FB1_WIN0_FB2_WIN2)) {
227                 printk(KERN_ERR "un supported map\n"
228                        "you can use the following order:\n" "201:\n"
229                        "fb0-win1\n" "fb1-win0\n" "fb2-win2\n" "210:\n"
230                        "fb0-win0\n" "fb1-win1\n" "fb2-win2\n" "120:\n"
231                        "fb0-win0\n" "fb1-win2\n" "fb2-win1\n" "102:\n"
232                        "fb0-win2\n" "fb1-win0\n" "fb2-win1\n" "021:\n"
233                        "fb0-win1\n" "fb1-win2\n" "fb2-win0\n" "012:\n"
234                        "fb0-win2\n" "fb1-win1\n" "fb2-win0\n");
235                 return count;
236         } else {
237                 dev_drv->ops->fb_win_remap(dev_drv, order);
238         }
239
240         return count;
241
242 }
243
244 static ssize_t show_dsp_lut(struct device *dev,
245                             struct device_attribute *attr, char *buf)
246 {
247
248         return 0;
249 }
250
251 static ssize_t set_dsp_lut(struct device *dev, struct device_attribute *attr,
252                            const char *buf, size_t count)
253 {
254         int dsp_lut[256];
255         const char *start = buf;
256         int i = 256, temp;
257         int space_max = 10;
258
259         struct fb_info *fbi = dev_get_drvdata(dev);
260         struct rk_lcdc_driver *dev_drv =
261             (struct rk_lcdc_driver *)fbi->par;
262
263         for (i = 0; i < 256; i++) {
264                 temp = i;
265                 dsp_lut[i] = temp + (temp << 8) + (temp << 16); //init by default value
266         }
267         //printk("count:%d\n>>%s\n\n",count,start);
268         for (i = 0; i < 256; i++) {
269                 space_max = 10; //max space number 10;
270                 temp = simple_strtoul(start, NULL, 10);
271                 dsp_lut[i] = temp;
272                 do {
273                         start++;
274                         space_max--;
275                 } while ((*start != ' ') && space_max);
276
277                 if (!space_max)
278                         break;
279                 else
280                         start++;
281         }
282 #if 0
283         for (i = 0; i < 16; i++) {
284                 for (j = 0; j < 16; j++)
285                         printk("0x%08x ", dsp_lut[i * 16 + j]);
286                 printk("\n");
287         }
288 #endif
289         dev_drv->ops->set_dsp_lut(dev_drv, dsp_lut);
290
291         return count;
292
293 }
294 static ssize_t show_dsp_cabc(struct device *dev,
295                             struct device_attribute *attr, char *buf)
296 {
297         struct fb_info *fbi = dev_get_drvdata(dev);
298         struct rk_lcdc_driver *dev_drv =
299             (struct rk_lcdc_driver *)fbi->par;
300
301         return snprintf(buf, PAGE_SIZE, "cabc mode=%d\n",
302                 dev_drv->cabc_mode);
303         return 0;
304 }
305
306 static ssize_t set_dsp_cabc(struct device *dev, struct device_attribute *attr,
307                            const char *buf, size_t count)
308 {
309         struct fb_info *fbi = dev_get_drvdata(dev);
310         struct rk_lcdc_driver *dev_drv =
311             (struct rk_lcdc_driver *)fbi->par;
312         int ret,mode=0;
313         
314         ret = kstrtoint(buf, 0, &mode);
315         if (ret)
316                 return ret;
317
318         ret = dev_drv->ops->set_dsp_cabc(dev_drv, mode);
319         if(ret < 0)
320                 return ret;
321         
322         return count;
323
324         
325 }
326 static ssize_t show_dsp_bcsh(struct device *dev,
327                             struct device_attribute *attr, char *buf)
328 {
329         struct fb_info *fbi = dev_get_drvdata(dev);
330         struct rk_lcdc_driver *dev_drv =
331             (struct rk_lcdc_driver *)fbi->par;
332         int brightness, contrast, sat_con, sin_hue, cos_hue;
333
334         brightness = dev_drv->ops->get_dsp_bcsh_bcs(dev_drv, BRIGHTNESS);
335         contrast = dev_drv->ops->get_dsp_bcsh_bcs(dev_drv, CONTRAST);
336         sat_con = dev_drv->ops->get_dsp_bcsh_bcs(dev_drv, SAT_CON);
337         sin_hue = dev_drv->ops->get_dsp_bcsh_hue(dev_drv,H_SIN);
338         cos_hue = dev_drv->ops->get_dsp_bcsh_hue(dev_drv,H_COS);
339
340         snprintf(buf, PAGE_SIZE, "brightness:%4d,contrast:%4d,sat_con:%4d,"
341                                  "sin_hue:%4d,cos_hue:%4d\n",
342                                  brightness, contrast,sat_con,sin_hue,cos_hue);
343         return 0;
344 }
345
346 static ssize_t set_dsp_bcsh(struct device *dev, struct device_attribute *attr,
347                            const char *buf, size_t count)
348 {
349         struct fb_info *fbi = dev_get_drvdata(dev);
350         struct rk_lcdc_driver *dev_drv =
351             (struct rk_lcdc_driver *)fbi->par;
352         int brightness, contrast, sat_con, ret, sin_hue, cos_hue;
353         if (!strncmp(buf, "open", 4)) {
354                 ret = dev_drv->ops->open_bcsh(dev_drv, 1);
355         } else if (!strncmp(buf, "close", 5)) {
356                 ret = dev_drv->ops->open_bcsh(dev_drv, 0);
357         } else if (!strncmp(buf, "brightness", 10)) {
358                 sscanf(buf, "brightness %d", &brightness);
359                 if (unlikely(brightness > 255)) {
360                         dev_err(fbi->dev,"brightness should be [0:255],now=%d\n\n",brightness);
361                         brightness = 255;
362                 }
363                 ret = dev_drv->ops->set_dsp_bcsh_bcs(dev_drv, BRIGHTNESS,brightness);
364         } else if (!strncmp(buf, "contrast", 8)) {
365                 sscanf(buf, "contrast %d", &contrast);
366                 if (unlikely(contrast > 510)) {
367                         dev_err(fbi->dev,"contrast should be [0:510],now=%d\n",contrast);
368                         contrast = 510;
369                 }
370                 ret = dev_drv->ops->set_dsp_bcsh_bcs(dev_drv, CONTRAST,contrast);
371         } else if (!strncmp(buf, "sat_con", 7)) {
372                 sscanf(buf, "sat_con %d", &sat_con);
373                 if (unlikely(sat_con > 1015)) {
374                         dev_err(fbi->dev,"sat_con should be [0:1015],now=%d\n",sat_con);
375                         sat_con = 1015;
376                 }
377                 ret = dev_drv->ops->set_dsp_bcsh_bcs(dev_drv, SAT_CON,sat_con);
378         } else if (!strncmp(buf, "hue", 3)) {
379                 sscanf(buf, "hue %d %d", &sin_hue,&cos_hue);
380                 if (unlikely(sin_hue > 511 || cos_hue > 511)) {
381                         dev_err(fbi->dev,"sin_hue=%d,cos_hue=%d\n",sin_hue,cos_hue);
382                 }
383                 ret = dev_drv->ops->set_dsp_bcsh_hue(dev_drv,sin_hue,cos_hue);
384         } else {
385                 printk("format error\n");
386         }
387
388         if(ret < 0)
389                 return ret;
390         
391         return count;
392 }
393
394 static ssize_t show_scale(struct device *dev,
395                             struct device_attribute *attr, char *buf)
396 {
397         struct fb_info *fbi = dev_get_drvdata(dev);
398         struct rk_lcdc_driver *dev_drv =
399             (struct rk_lcdc_driver *)fbi->par;
400         struct rk_screen *screen = dev_drv->cur_screen;
401         return snprintf(buf, PAGE_SIZE, "xscale=%d yscale=%d\nleft=%d top=%d right=%d bottom=%d\n",
402                 (screen->overscan.left + screen->overscan.right)/2,
403                 (screen->overscan.top + screen->overscan.bottom)/2,
404                 screen->overscan.left, screen->overscan.top,
405                 screen->overscan.right, screen->overscan.bottom);
406 }
407
408 static ssize_t set_scale(struct device *dev, struct device_attribute *attr,
409                            const char *buf, size_t count)
410 {
411         struct fb_info *fbi = dev_get_drvdata(dev);
412         struct rk_lcdc_driver *dev_drv =
413             (struct rk_lcdc_driver *)fbi->par;
414         struct rk_screen *screen = dev_drv->cur_screen;
415         u32 left, top, right, bottom;
416
417         if (!strncmp(buf, "overscan", 8)) {
418                 sscanf(buf, "overscan %d,%d,%d,%d", &left, &top, &right, &bottom);
419                 if (left > 0 && left <= 100)
420                         screen->overscan.left = left;
421                 if (top > 0 && top <= 100)
422                         screen->overscan.top = top;
423                 if (right > 0 && right <= 100)
424                         screen->overscan.right = right;
425                 if (bottom > 0 && bottom <= 100)
426                         screen->overscan.bottom = bottom;
427         } else if (!strncmp(buf, "left", 4)) {
428                 sscanf(buf, "left=%d", &left);
429                 if (left > 0 && left <= 100)
430                         screen->overscan.left = left;
431         } else if (!strncmp(buf, "top", 3)) {
432                 sscanf(buf, "top=%d", &top);
433                 if (top > 0 && top <= 100)
434                         screen->overscan.top = top;
435         } else if (!strncmp(buf, "right", 5)) {
436                 sscanf(buf, "right=%d", &right);
437                 if (right > 0 && right <= 100)
438                         screen->overscan.right = right;
439         } else if (!strncmp(buf, "bottom", 6)) {
440                 sscanf(buf, "bottom=%d", &bottom);
441                 if (bottom > 0 && bottom <= 100)
442                         screen->overscan.bottom = bottom;
443         } else if (!strncmp(buf, "xscale", 6)) {
444                 sscanf(buf, "xscale=%d", &left);
445                 if (left > 0 && left <= 100) {
446                         screen->overscan.left = left;
447                         screen->overscan.right = left;
448                 }
449         } else if (!strncmp(buf, "yscale", 6)) {
450                 sscanf(buf, "yscale=%d", &left);
451                 if (left > 0 && left <= 100) {
452                         screen->overscan.top = left;
453                         screen->overscan.bottom = left;
454                 }
455         } else {
456                 sscanf(buf, "%d", &left);
457                 if (left > 0 && left <= 100) {
458                         screen->overscan.left = left;
459                         screen->overscan.right = left;
460                         screen->overscan.top = left;
461                         screen->overscan.bottom = left;
462                 }
463         }
464 //      printk("%d %d %d %d\n", dev_drv->overscan.left, dev_drv->overscan.top, dev_drv->overscan.right, dev_drv->overscan.bottom);
465         dev_drv->ops->load_screen(dev_drv, 1);
466         dev_drv->ops->cfg_done(dev_drv);
467         return count;
468 }
469
470 static struct device_attribute rkfb_attrs[] = {
471         __ATTR(phys_addr, S_IRUGO, show_phys, NULL),
472         __ATTR(virt_addr, S_IRUGO, show_virt, NULL),
473         __ATTR(disp_info, S_IRUGO, show_disp_info, NULL),
474         __ATTR(screen_info, S_IRUGO, show_screen_info, NULL),
475         __ATTR(dual_mode, S_IRUGO, show_dual_mode, NULL),
476         __ATTR(enable, S_IRUGO | S_IWUSR, show_fb_state, set_fb_state),
477         __ATTR(overlay, S_IRUGO | S_IWUSR, show_overlay, set_overlay),
478         __ATTR(fps, S_IRUGO | S_IWUSR, show_fps, set_fps),
479         __ATTR(map, S_IRUGO | S_IWUSR, show_fb_win_map, set_fb_win_map),
480         __ATTR(dsp_lut, S_IRUGO | S_IWUSR, show_dsp_lut, set_dsp_lut),
481         __ATTR(cabc, S_IRUGO | S_IWUSR, show_dsp_cabc, set_dsp_cabc),
482         __ATTR(bcsh, S_IRUGO | S_IWUSR, show_dsp_bcsh, set_dsp_bcsh),
483         __ATTR(scale, S_IRUGO | S_IWUSR, show_scale, set_scale),
484 };
485
486 int rkfb_create_sysfs(struct fb_info *fbi)
487 {
488         int r;
489         int t;
490         for (t = 0; t < ARRAY_SIZE(rkfb_attrs); t++) {
491                 r = device_create_file(fbi->dev, &rkfb_attrs[t]);
492                 if (r) {
493                         dev_err(fbi->dev, "failed to create sysfs " "file\n");
494                         return r;
495                 }
496         }
497
498         return 0;
499 }
500
501 void rkfb_remove_sysfs(struct rk_fb *rk_fb)
502 {
503         int i, t;
504
505         for (i = 0; i < rk_fb->num_fb; i++) {
506                 for (t = 0; t < ARRAY_SIZE(rkfb_attrs); t++)
507                         device_remove_file(rk_fb->fb[i]->dev, &rkfb_attrs[t]);
508         }
509 }