2 * linux/drivers/video/rockchip/rkfb-sysfs.c
4 * Copyright (C) 2012 Rockchip Corporation
5 * Author: yxj<yxj@rock-chips.com>
7 * Some code and ideas taken from
8 *drivers/video/omap2/omapfb/omapfb-sys.c
9 *driver by Tomi Valkeinen.
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.
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
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/>.
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>
32 #include <asm/div64.h>
33 #include <linux/rk_screen.h>
34 #include <linux/rk_fb.h>
36 static ssize_t show_screen_info(struct device *dev,
37 struct device_attribute *attr, char *buf)
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;
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);
51 static ssize_t show_disp_info(struct device *dev,
52 struct device_attribute *attr, char *buf)
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);
63 static ssize_t show_phys(struct device *dev,
64 struct device_attribute *attr, char *buf)
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);
71 static ssize_t show_virt(struct device *dev,
72 struct device_attribute *attr, char *buf)
74 struct fb_info *fbi = dev_get_drvdata(dev);
76 return snprintf(buf, PAGE_SIZE, "0x%p-----0x%x\n",
77 fbi->screen_base, fbi->fix.smem_len);
80 static ssize_t show_fb_state(struct device *dev,
81 struct device_attribute *attr, char *buf)
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");
92 static ssize_t show_dual_mode(struct device *dev,
93 struct device_attribute *attr, char *buf)
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);
102 static ssize_t set_fb_state(struct device *dev, struct device_attribute *attr,
103 const char *buf, size_t count)
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);
111 ret = kstrtoint(buf, 0, &state);
115 dev_drv->ops->open(dev_drv, win_id, state);
119 static ssize_t show_overlay(struct device *dev,
120 struct device_attribute *attr, char *buf)
122 struct fb_info *fbi = dev_get_drvdata(dev);
123 struct rk_lcdc_driver *dev_drv =
124 (struct rk_lcdc_driver *)fbi->par;
126 ovl = dev_drv->ops->ovl_mgr(dev_drv, 0, 0);
131 return snprintf(buf, PAGE_SIZE, "%s\n",
132 ovl ? "win0 on the top of win1" :
133 "win1 on the top of win0");
137 static ssize_t set_overlay(struct device *dev, struct device_attribute *attr,
138 const char *buf, size_t count)
140 struct fb_info *fbi = dev_get_drvdata(dev);
141 struct rk_lcdc_driver *dev_drv =
142 (struct rk_lcdc_driver *)fbi->par;
145 ret = kstrtoint(buf, 0, &ovl);
149 ret = dev_drv->ops->ovl_mgr(dev_drv, ovl, 1);
157 static ssize_t show_fps(struct device *dev,
158 struct device_attribute *attr, char *buf)
160 struct fb_info *fbi = dev_get_drvdata(dev);
161 struct rk_lcdc_driver *dev_drv =
162 (struct rk_lcdc_driver *)fbi->par;
164 fps = dev_drv->ops->fps_mgr(dev_drv, 0, 0);
169 return snprintf(buf, PAGE_SIZE, "fps:%d\n", fps);
173 static ssize_t set_fps(struct device *dev, struct device_attribute *attr,
174 const char *buf, size_t count)
176 struct fb_info *fbi = dev_get_drvdata(dev);
177 struct rk_lcdc_driver *dev_drv =
178 (struct rk_lcdc_driver *)fbi->par;
181 ret = kstrtoint(buf, 0, &fps);
185 ret = dev_drv->ops->fps_mgr(dev_drv, fps, 1);
193 static ssize_t show_fb_win_map(struct device *dev,
194 struct device_attribute *attr, char *buf)
197 struct fb_info *fbi = dev_get_drvdata(dev);
198 struct rk_lcdc_driver *dev_drv =
199 (struct rk_lcdc_driver *)fbi->par;
201 mutex_lock(&dev_drv->fb_win_id_mutex);
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);
212 static ssize_t set_fb_win_map(struct device *dev, struct device_attribute *attr,
213 const char *buf, size_t count)
215 struct fb_info *fbi = dev_get_drvdata(dev);
216 struct rk_lcdc_driver *dev_drv =
217 (struct rk_lcdc_driver *)fbi->par;
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");
237 dev_drv->ops->fb_win_remap(dev_drv, order);
244 static ssize_t show_dsp_lut(struct device *dev,
245 struct device_attribute *attr, char *buf)
251 static ssize_t set_dsp_lut(struct device *dev, struct device_attribute *attr,
252 const char *buf, size_t count)
255 const char *start = buf;
259 struct fb_info *fbi = dev_get_drvdata(dev);
260 struct rk_lcdc_driver *dev_drv =
261 (struct rk_lcdc_driver *)fbi->par;
263 for (i = 0; i < 256; i++) {
265 dsp_lut[i] = temp + (temp << 8) + (temp << 16); //init by default value
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);
275 } while ((*start != ' ') && space_max);
283 for (i = 0; i < 16; i++) {
284 for (j = 0; j < 16; j++)
285 printk("0x%08x ", dsp_lut[i * 16 + j]);
289 dev_drv->ops->set_dsp_lut(dev_drv, dsp_lut);
294 static ssize_t show_dsp_cabc(struct device *dev,
295 struct device_attribute *attr, char *buf)
301 static ssize_t set_dsp_cabc(struct device *dev, struct device_attribute *attr,
302 const char *buf, size_t count)
304 struct fb_info *fbi = dev_get_drvdata(dev);
305 struct rk_lcdc_driver *dev_drv =
306 (struct rk_lcdc_driver *)fbi->par;
309 ret = kstrtoint(buf, 0, &mode);
313 ret = dev_drv->ops->set_dsp_cabc(dev_drv, mode);
321 static ssize_t show_dsp_bcsh(struct device *dev,
322 struct device_attribute *attr, char *buf)
324 struct fb_info *fbi = dev_get_drvdata(dev);
325 struct rk_lcdc_driver *dev_drv =
326 (struct rk_lcdc_driver *)fbi->par;
327 int brightness, contrast, sat_con, sin_hue, cos_hue;
329 brightness = dev_drv->ops->get_dsp_bcsh_bcs(dev_drv, BRIGHTNESS);
330 contrast = dev_drv->ops->get_dsp_bcsh_bcs(dev_drv, CONTRAST);
331 sat_con = dev_drv->ops->get_dsp_bcsh_bcs(dev_drv, SAT_CON);
332 sin_hue = dev_drv->ops->get_dsp_bcsh_hue(dev_drv,H_SIN);
333 cos_hue = dev_drv->ops->get_dsp_bcsh_hue(dev_drv,H_COS);
335 snprintf(buf, PAGE_SIZE, "brightness:%4d,contrast:%4d,sat_con:%4d,"
336 "sin_hue:%4d,cos_hue:%4d\n",
337 brightness, contrast,sat_con,sin_hue,cos_hue);
341 static ssize_t set_dsp_bcsh(struct device *dev, struct device_attribute *attr,
342 const char *buf, size_t count)
344 struct fb_info *fbi = dev_get_drvdata(dev);
345 struct rk_lcdc_driver *dev_drv =
346 (struct rk_lcdc_driver *)fbi->par;
347 int brightness, contrast, sat_con, hue, ret, open, sin_hue, cos_hue;
348 if (!strncmp(buf, "open", 4)) {
349 ret = dev_drv->ops->open_bcsh(dev_drv, 1);
350 } else if (!strncmp(buf, "close", 5)) {
351 ret = dev_drv->ops->open_bcsh(dev_drv, 0);
352 } else if (!strncmp(buf, "brightness", 10)) {
353 sscanf(buf, "brightness %d", &brightness);
354 if (unlikely(brightness > 255)) {
355 dev_err(fbi->dev,"brightness should be [0:255],now=%d\n\n",brightness);
358 ret = dev_drv->ops->set_dsp_bcsh_bcs(dev_drv, BRIGHTNESS,brightness);
359 } else if (!strncmp(buf, "contrast", 8)) {
360 sscanf(buf, "contrast %d", &contrast);
361 if (unlikely(contrast > 510)) {
362 dev_err(fbi->dev,"contrast should be [0:510],now=%d\n",contrast);
365 ret = dev_drv->ops->set_dsp_bcsh_bcs(dev_drv, CONTRAST,contrast);
366 } else if (!strncmp(buf, "sat_con", 7)) {
367 sscanf(buf, "sat_con %d", &sat_con);
368 if (unlikely(sat_con > 1015)) {
369 dev_err(fbi->dev,"sat_con should be [0:1015],now=%d\n",sat_con);
372 ret = dev_drv->ops->set_dsp_bcsh_bcs(dev_drv, SAT_CON,sat_con);
373 } else if (!strncmp(buf, "hue", 3)) {
374 sscanf(buf, "hue %d %d", &sin_hue,&cos_hue);
375 if (unlikely(sin_hue > 511 || cos_hue > 511)) {
376 dev_err(fbi->dev,"sin_hue=%d,cos_hue=%d\n",sin_hue,cos_hue);
378 ret = dev_drv->ops->set_dsp_bcsh_hue(dev_drv,sin_hue,cos_hue);
380 printk("format error\n");
389 static ssize_t show_scale(struct device *dev,
390 struct device_attribute *attr, char *buf)
392 struct fb_info *fbi = dev_get_drvdata(dev);
393 struct rk_lcdc_driver *dev_drv =
394 (struct rk_lcdc_driver *)fbi->par;
395 struct rk_screen *screen = dev_drv->cur_screen;
396 return snprintf(buf, PAGE_SIZE, "xscale=%d yscale=%d\nleft=%d top=%d right=%d bottom=%d\n",
397 (screen->overscan.left + screen->overscan.right)/2,
398 (screen->overscan.top + screen->overscan.bottom)/2,
399 screen->overscan.left, screen->overscan.top,
400 screen->overscan.right, screen->overscan.bottom);
403 static ssize_t set_scale(struct device *dev, struct device_attribute *attr,
404 const char *buf, size_t count)
406 struct fb_info *fbi = dev_get_drvdata(dev);
407 struct rk_lcdc_driver *dev_drv =
408 (struct rk_lcdc_driver *)fbi->par;
409 struct rk_screen *screen = dev_drv->cur_screen;
410 u32 left, top, right, bottom;
412 if (!strncmp(buf, "overscan", 8)) {
413 sscanf(buf, "overscan %d,%d,%d,%d", &left, &top, &right, &bottom);
414 if (left > 0 && left <= 100)
415 screen->overscan.left = left;
416 if (top > 0 && top <= 100)
417 screen->overscan.top = top;
418 if (right > 0 && right <= 100)
419 screen->overscan.right = right;
420 if (bottom > 0 && bottom <= 100)
421 screen->overscan.bottom = bottom;
422 } else if (!strncmp(buf, "left", 4)) {
423 sscanf(buf, "left=%d", &left);
424 if (left > 0 && left <= 100)
425 screen->overscan.left = left;
426 } else if (!strncmp(buf, "top", 3)) {
427 sscanf(buf, "top=%d", &top);
428 if (top > 0 && top <= 100)
429 screen->overscan.top = top;
430 } else if (!strncmp(buf, "right", 5)) {
431 sscanf(buf, "right=%d", &right);
432 if (right > 0 && right <= 100)
433 screen->overscan.right = right;
434 } else if (!strncmp(buf, "bottom", 6)) {
435 sscanf(buf, "bottom=%d", &bottom);
436 if (bottom > 0 && bottom <= 100)
437 screen->overscan.bottom = bottom;
438 } else if (!strncmp(buf, "xscale", 6)) {
439 sscanf(buf, "xscale=%d", &left);
440 if (left > 0 && left <= 100) {
441 screen->overscan.left = left;
442 screen->overscan.right = left;
444 } else if (!strncmp(buf, "yscale", 6)) {
445 sscanf(buf, "yscale=%d", &left);
446 if (left > 0 && left <= 100) {
447 screen->overscan.top = left;
448 screen->overscan.bottom = left;
451 sscanf(buf, "%d", &left);
452 if (left > 0 && left <= 100) {
453 screen->overscan.left = left;
454 screen->overscan.right = left;
455 screen->overscan.top = left;
456 screen->overscan.bottom = left;
459 // printk("%d %d %d %d\n", dev_drv->overscan.left, dev_drv->overscan.top, dev_drv->overscan.right, dev_drv->overscan.bottom);
460 dev_drv->ops->load_screen(dev_drv, 1);
461 dev_drv->ops->cfg_done(dev_drv);
465 static struct device_attribute rkfb_attrs[] = {
466 __ATTR(phys_addr, S_IRUGO, show_phys, NULL),
467 __ATTR(virt_addr, S_IRUGO, show_virt, NULL),
468 __ATTR(disp_info, S_IRUGO, show_disp_info, NULL),
469 __ATTR(screen_info, S_IRUGO, show_screen_info, NULL),
470 __ATTR(dual_mode, S_IRUGO, show_dual_mode, NULL),
471 __ATTR(enable, S_IRUGO | S_IWUSR, show_fb_state, set_fb_state),
472 __ATTR(overlay, S_IRUGO | S_IWUSR, show_overlay, set_overlay),
473 __ATTR(fps, S_IRUGO | S_IWUSR, show_fps, set_fps),
474 __ATTR(map, S_IRUGO | S_IWUSR, show_fb_win_map, set_fb_win_map),
475 __ATTR(dsp_lut, S_IRUGO | S_IWUSR, show_dsp_lut, set_dsp_lut),
476 __ATTR(cabc, S_IRUGO | S_IWUSR, show_dsp_cabc, set_dsp_cabc),
477 __ATTR(bcsh, S_IRUGO | S_IWUSR, show_dsp_bcsh, set_dsp_bcsh),
478 __ATTR(scale, S_IRUGO | S_IWUSR, show_scale, set_scale),
481 int rkfb_create_sysfs(struct fb_info *fbi)
485 for (t = 0; t < ARRAY_SIZE(rkfb_attrs); t++) {
486 r = device_create_file(fbi->dev, &rkfb_attrs[t]);
488 dev_err(fbi->dev, "failed to create sysfs " "file\n");
496 void rkfb_remove_sysfs(struct rk_fb *rk_fb)
500 for (i = 0; i < rk_fb->num_fb; i++) {
501 for (t = 0; t < ARRAY_SIZE(rkfb_attrs); t++)
502 device_remove_file(rk_fb->fb[i]->dev, &rkfb_attrs[t]);