Merge branch 'develop-3.10' of ssh://10.10.10.29/rk/kernel 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
298         return 0;
299 }
300
301 static ssize_t set_dsp_cabc(struct device *dev, struct device_attribute *attr,
302                            const char *buf, size_t count)
303 {
304         struct fb_info *fbi = dev_get_drvdata(dev);
305         struct rk_lcdc_driver *dev_drv =
306             (struct rk_lcdc_driver *)fbi->par;
307         int ret,mode=0;
308         
309         ret = kstrtoint(buf, 0, &mode);
310         if (ret)
311                 return ret;
312
313         ret = dev_drv->ops->set_dsp_cabc(dev_drv, mode);
314         if(ret < 0)
315                 return ret;
316         
317         return count;
318
319         
320 }
321
322 static ssize_t show_hue(struct device *dev,
323                             struct device_attribute *attr, char *buf)
324 {
325
326         return 0;
327 }
328
329 static ssize_t set_hue(struct device *dev, struct device_attribute *attr,
330                            const char *buf, size_t count)
331 {
332         struct fb_info *fbi = dev_get_drvdata(dev);
333         struct rk_lcdc_driver *dev_drv =
334             (struct rk_lcdc_driver *)fbi->par;
335         int ret,hue;
336         
337         ret = kstrtoint(buf, 0, &hue);
338         if (ret)
339                 return ret;
340
341         ret = dev_drv->ops->set_dsp_hue(dev_drv, hue);
342         if(ret < 0)
343                 return ret;
344         
345         return count;
346 }
347
348 static ssize_t show_dsp_bcs(struct device *dev,
349                             struct device_attribute *attr, char *buf)
350 {
351
352         return 0;
353 }
354
355 static ssize_t set_dsp_bcs(struct device *dev, struct device_attribute *attr,
356                            const char *buf, size_t count)
357 {
358         struct fb_info *fbi = dev_get_drvdata(dev);
359         struct rk_lcdc_driver *dev_drv =
360             (struct rk_lcdc_driver *)fbi->par;
361         int ret,bri,con,sat;
362         
363         ret = kstrtoint(buf, 0, &bri);
364         if (ret)
365                 return ret;
366
367         ret = dev_drv->ops->set_dsp_bcsh_bcs(dev_drv, bri,con,sat);
368         if(ret < 0)
369                 return ret;
370         
371         return count;
372 }
373
374 static struct device_attribute rkfb_attrs[] = {
375         __ATTR(phys_addr, S_IRUGO, show_phys, NULL),
376         __ATTR(virt_addr, S_IRUGO, show_virt, NULL),
377         __ATTR(disp_info, S_IRUGO, show_disp_info, NULL),
378         __ATTR(screen_info, S_IRUGO, show_screen_info, NULL),
379         __ATTR(dual_mode, S_IRUGO, show_dual_mode, NULL),
380         __ATTR(enable, S_IRUGO | S_IWUSR, show_fb_state, set_fb_state),
381         __ATTR(overlay, S_IRUGO | S_IWUSR, show_overlay, set_overlay),
382         __ATTR(fps, S_IRUGO | S_IWUSR, show_fps, set_fps),
383         __ATTR(map, S_IRUGO | S_IWUSR, show_fb_win_map, set_fb_win_map),
384         __ATTR(dsp_lut, S_IRUGO | S_IWUSR, show_dsp_lut, set_dsp_lut),
385         __ATTR(cabc, S_IRUGO | S_IWUSR, show_dsp_cabc, set_dsp_cabc),
386         __ATTR(hue, S_IRUGO | S_IWUSR, show_hue, set_hue),
387         __ATTR(bcs, S_IRUGO | S_IWUSR, show_dsp_bcs, set_dsp_bcs),
388 };
389
390 int rkfb_create_sysfs(struct fb_info *fbi)
391 {
392         int r;
393         int t;
394         for (t = 0; t < ARRAY_SIZE(rkfb_attrs); t++) {
395                 r = device_create_file(fbi->dev, &rkfb_attrs[t]);
396                 if (r) {
397                         dev_err(fbi->dev, "failed to create sysfs " "file\n");
398                         return r;
399                 }
400         }
401
402         return 0;
403 }
404
405 void rkfb_remove_sysfs(struct rk_fb *rk_fb)
406 {
407         int i, t;
408
409         for (i = 0; i < rk_fb->num_fb; i++) {
410                 for (t = 0; t < ARRAY_SIZE(rkfb_attrs); t++)
411                         device_remove_file(rk_fb->fb[i]->dev, &rkfb_attrs[t]);
412         }
413 }