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->cur_screen;
43 u32 x = screen->mode.left_margin + screen->mode.right_margin +
44 screen->mode.xres + screen->mode.hsync_len;
45 u32 y = screen->mode.upper_margin + screen->mode.lower_margin +
46 screen->mode.yres + screen->mode.vsync_len;
47 u64 ft = (u64)x * y * (dev_drv->pixclock);
50 fps = div64_u64(1000000000000llu, ft);
51 return snprintf(buf, PAGE_SIZE, "xres:%d\nyres:%d\nfps:%d\n",
52 screen->mode.xres, screen->mode.yres, fps);
55 static ssize_t show_disp_info(struct device *dev,
56 struct device_attribute *attr, char *buf)
58 struct fb_info *fbi = dev_get_drvdata(dev);
59 struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)fbi->par;
60 int win_id = dev_drv->ops->fb_get_win_id(dev_drv, fbi->fix.id);
62 if (dev_drv->ops->get_disp_info)
63 return dev_drv->ops->get_disp_info(dev_drv, buf, win_id);
68 static ssize_t show_phys(struct device *dev,
69 struct device_attribute *attr, char *buf)
71 struct fb_info *fbi = dev_get_drvdata(dev);
73 return snprintf(buf, PAGE_SIZE, "0x%lx-----0x%x\n",
74 fbi->fix.smem_start, fbi->fix.smem_len);
77 static ssize_t show_virt(struct device *dev,
78 struct device_attribute *attr, char *buf)
80 struct fb_info *fbi = dev_get_drvdata(dev);
82 return snprintf(buf, PAGE_SIZE, "0x%p-----0x%x\n",
83 fbi->screen_base, fbi->fix.smem_len);
86 static ssize_t show_fb_state(struct device *dev,
87 struct device_attribute *attr, char *buf)
89 struct fb_info *fbi = dev_get_drvdata(dev);
90 struct rk_lcdc_driver *dev_drv =
91 (struct rk_lcdc_driver *)fbi->par;
92 int win_id = dev_drv->ops->fb_get_win_id(dev_drv, fbi->fix.id);
93 int state = dev_drv->ops->get_win_state(dev_drv, win_id);
95 return snprintf(buf, PAGE_SIZE, "%s\n", state ? "enabled" : "disabled");
98 static ssize_t show_dual_mode(struct device *dev,
99 struct device_attribute *attr, char *buf)
101 struct fb_info *fbi = dev_get_drvdata(dev);
102 struct rk_fb *rk_fb = dev_get_drvdata(fbi->device);
103 int mode = rk_fb->disp_mode;
105 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
108 static ssize_t set_fb_state(struct device *dev, struct device_attribute *attr,
109 const char *buf, size_t count)
111 struct fb_info *fbi = dev_get_drvdata(dev);
112 struct rk_lcdc_driver *dev_drv =
113 (struct rk_lcdc_driver *)fbi->par;
114 int win_id = dev_drv->ops->fb_get_win_id(dev_drv, fbi->fix.id);
118 ret = kstrtoint(buf, 0, &state);
121 dev_drv->ops->open(dev_drv, win_id, state);
123 dev_drv->ops->set_par(dev_drv, win_id);
124 dev_drv->ops->pan_display(dev_drv, win_id);
125 dev_drv->ops->cfg_done(dev_drv);
130 static ssize_t show_overlay(struct device *dev,
131 struct device_attribute *attr, char *buf)
133 struct fb_info *fbi = dev_get_drvdata(dev);
134 struct rk_lcdc_driver *dev_drv =
135 (struct rk_lcdc_driver *)fbi->par;
138 if (dev_drv->ops->ovl_mgr)
139 ovl = dev_drv->ops->ovl_mgr(dev_drv, 0, 0);
144 return snprintf(buf, PAGE_SIZE, "%s\n",
145 ovl ? "win0 on the top of win1" :
146 "win1 on the top of win0");
149 static ssize_t set_overlay(struct device *dev, struct device_attribute *attr,
150 const char *buf, size_t count)
152 struct fb_info *fbi = dev_get_drvdata(dev);
153 struct rk_lcdc_driver *dev_drv =
154 (struct rk_lcdc_driver *)fbi->par;
158 ret = kstrtou32(buf, 0, &ovl);
161 if (dev_drv->ops->ovl_mgr)
162 ret = dev_drv->ops->ovl_mgr(dev_drv, ovl, 1);
169 static ssize_t show_fps(struct device *dev,
170 struct device_attribute *attr, char *buf)
172 struct fb_info *fbi = dev_get_drvdata(dev);
173 struct rk_lcdc_driver *dev_drv =
174 (struct rk_lcdc_driver *)fbi->par;
177 if (dev_drv->ops->fps_mgr)
178 fps = dev_drv->ops->fps_mgr(dev_drv, 0, 0);
182 return snprintf(buf, PAGE_SIZE, "fps:%d\n", fps);
185 static ssize_t set_fps(struct device *dev, struct device_attribute *attr,
186 const char *buf, size_t count)
188 struct fb_info *fbi = dev_get_drvdata(dev);
189 struct rk_lcdc_driver *dev_drv =
190 (struct rk_lcdc_driver *)fbi->par;
194 ret = kstrtou32(buf, 0, &fps);
198 if (fps == 0 || fps > 60) {
199 dev_info(dev, "unsupport fps value,pelase set 1~60\n");
203 if (dev_drv->ops->fps_mgr)
204 ret = dev_drv->ops->fps_mgr(dev_drv, fps, 1);
211 static ssize_t show_fb_win_map(struct device *dev,
212 struct device_attribute *attr, char *buf)
215 struct fb_info *fbi = dev_get_drvdata(dev);
216 struct rk_lcdc_driver *dev_drv =
217 (struct rk_lcdc_driver *)fbi->par;
219 mutex_lock(&dev_drv->fb_win_id_mutex);
221 snprintf(buf, PAGE_SIZE, "fb0:win%d\nfb1:win%d\nfb2:win%d\n",
222 dev_drv->fb0_win_id, dev_drv->fb1_win_id,
223 dev_drv->fb2_win_id);
224 mutex_unlock(&dev_drv->fb_win_id_mutex);
229 static ssize_t set_fb_win_map(struct device *dev, struct device_attribute *attr,
230 const char *buf, size_t count)
232 struct fb_info *fbi = dev_get_drvdata(dev);
233 struct rk_lcdc_driver *dev_drv =
234 (struct rk_lcdc_driver *)fbi->par;
238 ret = kstrtou32(buf, 0, &order);
239 if ((order != FB0_WIN2_FB1_WIN1_FB2_WIN0) &&
240 (order != FB0_WIN1_FB1_WIN2_FB2_WIN0) &&
241 (order != FB0_WIN2_FB1_WIN0_FB2_WIN1) &&
242 (order != FB0_WIN0_FB1_WIN2_FB2_WIN1) &&
243 (order != FB0_WIN0_FB1_WIN1_FB2_WIN2) &&
244 (order != FB0_WIN1_FB1_WIN0_FB2_WIN2)) {
245 dev_info(dev, "un supported map\n"
246 "you can use the following order:\n" "201:\n"
247 "fb0-win1\n" "fb1-win0\n" "fb2-win2\n" "210:\n"
248 "fb0-win0\n" "fb1-win1\n" "fb2-win2\n" "120:\n"
249 "fb0-win0\n" "fb1-win2\n" "fb2-win1\n" "102:\n"
250 "fb0-win2\n" "fb1-win0\n" "fb2-win1\n" "021:\n"
251 "fb0-win1\n" "fb1-win2\n" "fb2-win0\n" "012:\n"
252 "fb0-win2\n" "fb1-win1\n" "fb2-win0\n");
255 if (dev_drv->ops->fb_win_remap)
256 dev_drv->ops->fb_win_remap(dev_drv, order);
262 static ssize_t show_hwc_lut(struct device *dev,
263 struct device_attribute *attr, char *buf)
268 static ssize_t set_hwc_lut(struct device *dev, struct device_attribute *attr,
269 const char *buf, size_t count)
272 const char *start = buf;
276 struct fb_info *fbi = dev_get_drvdata(dev);
277 struct rk_lcdc_driver *dev_drv =
278 (struct rk_lcdc_driver *)fbi->par;
280 /*printk("count:%d\n>>%s\n\n",count,start);*/
281 for (i = 0; i < 256; i++) {
282 space_max = 15; /*max space number 15*/
283 temp = simple_strtoul(start, NULL, 16);
288 } while ((*start != ' ') && space_max);
296 for (i = 0; i < 16; i++) {
297 for (j = 0; j < 16; j++)
298 printk("0x%08x ", hwc_lut[i * 16 + j]);
302 if (dev_drv->ops->set_hwc_lut)
303 dev_drv->ops->set_hwc_lut(dev_drv, hwc_lut, 1);
308 static ssize_t show_dsp_lut(struct device *dev,
309 struct device_attribute *attr, char *buf)
314 static ssize_t set_dsp_lut(struct device *dev, struct device_attribute *attr,
315 const char *buf, size_t count)
318 const char *start = buf;
322 struct fb_info *fbi = dev_get_drvdata(dev);
323 struct rk_lcdc_driver *dev_drv =
324 (struct rk_lcdc_driver *)fbi->par;
326 for (i = 0; i < 256; i++) {
328 /*init by default value*/
329 dsp_lut[i] = temp + (temp << 8) + (temp << 16);
331 /*printk("count:%d\n>>%s\n\n",count,start);*/
332 for (i = 0; i < 256; i++) {
333 space_max = 10; /*max space number 10*/
334 temp = simple_strtoul(start, NULL, 10);
339 } while ((*start != ' ') && space_max);
347 for (i = 0; i < 16; i++) {
348 for (j = 0; j < 16; j++)
349 printk("0x%08x ", dsp_lut[i * 16 + j]);
353 if (dev_drv->ops->set_dsp_lut)
354 dev_drv->ops->set_dsp_lut(dev_drv, dsp_lut);
359 static ssize_t show_dsp_cabc(struct device *dev,
360 struct device_attribute *attr, char *buf)
362 struct fb_info *fbi = dev_get_drvdata(dev);
363 struct rk_lcdc_driver *dev_drv =
364 (struct rk_lcdc_driver *)fbi->par;
366 return snprintf(buf, PAGE_SIZE, "cabc mode=%d\n",
371 static ssize_t set_dsp_cabc(struct device *dev, struct device_attribute *attr,
372 const char *buf, size_t count)
374 struct fb_info *fbi = dev_get_drvdata(dev);
375 struct rk_lcdc_driver *dev_drv =
376 (struct rk_lcdc_driver *)fbi->par;
379 ret = kstrtoint(buf, 0, &mode);
383 if (dev_drv->ops->set_dsp_cabc)
384 ret = dev_drv->ops->set_dsp_cabc(dev_drv, mode);
391 static ssize_t show_dsp_bcsh(struct device *dev,
392 struct device_attribute *attr, char *buf)
394 struct fb_info *fbi = dev_get_drvdata(dev);
395 struct rk_lcdc_driver *dev_drv =
396 (struct rk_lcdc_driver *)fbi->par;
397 int brightness, contrast, sat_con, sin_hue, cos_hue;
399 if (dev_drv->ops->get_dsp_bcsh_bcs) {
400 brightness = dev_drv->ops->get_dsp_bcsh_bcs(dev_drv,
402 contrast = dev_drv->ops->get_dsp_bcsh_bcs(dev_drv, CONTRAST);
403 sat_con = dev_drv->ops->get_dsp_bcsh_bcs(dev_drv, SAT_CON);
405 if (dev_drv->ops->get_dsp_bcsh_hue) {
406 sin_hue = dev_drv->ops->get_dsp_bcsh_hue(dev_drv, H_SIN);
407 cos_hue = dev_drv->ops->get_dsp_bcsh_hue(dev_drv, H_COS);
409 return snprintf(buf, PAGE_SIZE,
410 "brightness:%4d,contrast:%4d,sat_con:%4d,"
411 "sin_hue:%4d,cos_hue:%4d\n",
412 brightness, contrast, sat_con, sin_hue, cos_hue);
415 static ssize_t set_dsp_bcsh(struct device *dev, struct device_attribute *attr,
416 const char *buf, size_t count)
418 struct fb_info *fbi = dev_get_drvdata(dev);
419 struct rk_lcdc_driver *dev_drv =
420 (struct rk_lcdc_driver *)fbi->par;
421 int brightness, contrast, sat_con, ret = 0, sin_hue, cos_hue;
423 if (!strncmp(buf, "open", 4)) {
424 if (dev_drv->ops->open_bcsh)
425 ret = dev_drv->ops->open_bcsh(dev_drv, 1);
428 } else if (!strncmp(buf, "close", 5)) {
429 if (dev_drv->ops->open_bcsh)
430 ret = dev_drv->ops->open_bcsh(dev_drv, 0);
433 } else if (!strncmp(buf, "brightness", 10)) {
434 sscanf(buf, "brightness %d", &brightness);
435 if (unlikely(brightness > 255)) {
437 "brightness should be [0:255],now=%d\n\n",
441 if (dev_drv->ops->set_dsp_bcsh_bcs)
442 ret = dev_drv->ops->set_dsp_bcsh_bcs(dev_drv,
447 } else if (!strncmp(buf, "contrast", 8)) {
448 sscanf(buf, "contrast %d", &contrast);
449 if (unlikely(contrast > 510)) {
451 "contrast should be [0:510],now=%d\n",
455 if (dev_drv->ops->set_dsp_bcsh_bcs)
456 ret = dev_drv->ops->set_dsp_bcsh_bcs(dev_drv,
461 } else if (!strncmp(buf, "sat_con", 7)) {
462 sscanf(buf, "sat_con %d", &sat_con);
463 if (unlikely(sat_con > 1015)) {
465 "sat_con should be [0:1015],now=%d\n",
469 if (dev_drv->ops->set_dsp_bcsh_bcs)
470 ret = dev_drv->ops->set_dsp_bcsh_bcs(dev_drv,
475 } else if (!strncmp(buf, "hue", 3)) {
476 sscanf(buf, "hue %d %d", &sin_hue, &cos_hue);
477 if (unlikely(sin_hue > 511 || cos_hue > 511)) {
478 dev_err(fbi->dev, "sin_hue=%d,cos_hue=%d\n",
481 if (dev_drv->ops->set_dsp_bcsh_hue)
482 ret = dev_drv->ops->set_dsp_bcsh_hue(dev_drv,
488 dev_info(dev, "format error\n");
497 static ssize_t show_scale(struct device *dev,
498 struct device_attribute *attr, char *buf)
500 struct fb_info *fbi = dev_get_drvdata(dev);
501 struct rk_lcdc_driver *dev_drv =
502 (struct rk_lcdc_driver *)fbi->par;
503 struct rk_screen *screen = dev_drv->cur_screen;
505 return snprintf(buf, PAGE_SIZE,
506 "xscale=%d yscale=%d\nleft=%d top=%d right=%d bottom=%d\n",
507 (screen->overscan.left + screen->overscan.right)/2,
508 (screen->overscan.top + screen->overscan.bottom)/2,
509 screen->overscan.left, screen->overscan.top,
510 screen->overscan.right, screen->overscan.bottom);
513 static ssize_t set_scale(struct device *dev, struct device_attribute *attr,
514 const char *buf, size_t count)
516 struct fb_info *fbi = dev_get_drvdata(dev);
517 struct rk_lcdc_driver *dev_drv =
518 (struct rk_lcdc_driver *)fbi->par;
519 struct rk_screen *screen = dev_drv->cur_screen;
520 u32 left, top, right, bottom;
522 if (!strncmp(buf, "overscan", 8)) {
524 "overscan %d,%d,%d,%d", &left, &top, &right, &bottom);
525 if (left > 0 && left <= 100)
526 screen->overscan.left = left;
527 if (top > 0 && top <= 100)
528 screen->overscan.top = top;
529 if (right > 0 && right <= 100)
530 screen->overscan.right = right;
531 if (bottom > 0 && bottom <= 100)
532 screen->overscan.bottom = bottom;
533 } else if (!strncmp(buf, "left", 4)) {
534 sscanf(buf, "left=%d", &left);
535 if (left > 0 && left <= 100)
536 screen->overscan.left = left;
537 } else if (!strncmp(buf, "top", 3)) {
538 sscanf(buf, "top=%d", &top);
539 if (top > 0 && top <= 100)
540 screen->overscan.top = top;
541 } else if (!strncmp(buf, "right", 5)) {
542 sscanf(buf, "right=%d", &right);
543 if (right > 0 && right <= 100)
544 screen->overscan.right = right;
545 } else if (!strncmp(buf, "bottom", 6)) {
546 sscanf(buf, "bottom=%d", &bottom);
547 if (bottom > 0 && bottom <= 100)
548 screen->overscan.bottom = bottom;
549 } else if (!strncmp(buf, "xscale", 6)) {
550 sscanf(buf, "xscale=%d", &left);
551 if (left > 0 && left <= 100) {
552 screen->overscan.left = left;
553 screen->overscan.right = left;
555 } else if (!strncmp(buf, "yscale", 6)) {
556 sscanf(buf, "yscale=%d", &left);
557 if (left > 0 && left <= 100) {
558 screen->overscan.top = left;
559 screen->overscan.bottom = left;
562 sscanf(buf, "%d", &left);
563 if (left > 0 && left <= 100) {
564 screen->overscan.left = left;
565 screen->overscan.right = left;
566 screen->overscan.top = left;
567 screen->overscan.bottom = left;
571 if (dev_drv->ops->set_overscan)
572 dev_drv->ops->set_overscan(dev_drv, &screen->overscan);
577 static struct device_attribute rkfb_attrs[] = {
578 __ATTR(phys_addr, S_IRUGO, show_phys, NULL),
579 __ATTR(virt_addr, S_IRUGO, show_virt, NULL),
580 __ATTR(disp_info, S_IRUGO, show_disp_info, NULL),
581 __ATTR(screen_info, S_IRUGO, show_screen_info, NULL),
582 __ATTR(dual_mode, S_IRUGO, show_dual_mode, NULL),
583 __ATTR(enable, S_IRUGO | S_IWUSR, show_fb_state, set_fb_state),
584 __ATTR(overlay, S_IRUGO | S_IWUSR, show_overlay, set_overlay),
585 __ATTR(fps, S_IRUGO | S_IWUSR, show_fps, set_fps),
586 __ATTR(map, S_IRUGO | S_IWUSR, show_fb_win_map, set_fb_win_map),
587 __ATTR(dsp_lut, S_IRUGO | S_IWUSR, show_dsp_lut, set_dsp_lut),
588 __ATTR(hwc_lut, S_IRUGO | S_IWUSR, show_hwc_lut, set_hwc_lut),
589 __ATTR(cabc, S_IRUGO | S_IWUSR, show_dsp_cabc, set_dsp_cabc),
590 __ATTR(bcsh, S_IRUGO | S_IWUSR, show_dsp_bcsh, set_dsp_bcsh),
591 __ATTR(scale, S_IRUGO | S_IWUSR, show_scale, set_scale),
594 int rkfb_create_sysfs(struct fb_info *fbi)
598 for (t = 0; t < ARRAY_SIZE(rkfb_attrs); t++) {
599 r = device_create_file(fbi->dev, &rkfb_attrs[t]);
601 dev_err(fbi->dev, "failed to create sysfs " "file\n");
609 void rkfb_remove_sysfs(struct rk_fb *rk_fb)
613 for (i = 0; i < rk_fb->num_fb; i++) {
614 for (t = 0; t < ARRAY_SIZE(rkfb_attrs); t++)
615 device_remove_file(rk_fb->fb[i]->dev, &rkfb_attrs[t]);