rk30 fb: add IOCTL and sys interface for overlay control
[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
37 static ssize_t show_screen_info(struct device *dev,
38                 struct device_attribute *attr, char *buf)
39 {
40         struct fb_info *fbi = dev_get_drvdata(dev);
41         struct rk_lcdc_device_driver * dev_drv = 
42                 (struct rk_lcdc_device_driver * )fbi->par;
43         rk_screen * screen = dev_drv->screen;
44         int fps;
45         u64 ft = (u64)(screen->upper_margin + screen->lower_margin + screen->y_res +screen->vsync_len)*
46                 (screen->left_margin + screen->right_margin + screen->x_res + screen->hsync_len)*
47                 (dev_drv->pixclock);       // one frame time ,(pico seconds)
48         fps = div64_u64(1000000000000llu,ft);
49         return snprintf(buf, PAGE_SIZE,"xres:%d\nyres:%d\nfps:%d\n",
50                 screen->x_res,screen->y_res,fps);
51 }
52
53 static ssize_t show_disp_info(struct device *dev,
54                 struct device_attribute *attr, char *buf)
55 {
56         struct fb_info *fbi = dev_get_drvdata(dev);
57         struct rk_lcdc_device_driver * dev_drv = 
58                 (struct rk_lcdc_device_driver * )fbi->par;
59         int layer_id = get_fb_layer_id(&fbi->fix);
60         if(dev_drv->get_disp_info)
61                 dev_drv->get_disp_info(dev_drv,layer_id);
62
63         return 0;
64 }
65
66 static ssize_t show_phys(struct device *dev,
67                 struct device_attribute *attr, char *buf)
68 {
69         struct fb_info *fbi = dev_get_drvdata(dev);
70         return snprintf(buf, PAGE_SIZE, "0x%lx-----0x%x\n",
71                 fbi->fix.smem_start,fbi->fix.smem_len);
72 }
73
74 static ssize_t show_virt(struct device *dev,
75                 struct device_attribute *attr, char *buf)
76 {
77         struct fb_info *fbi = dev_get_drvdata(dev);
78
79         return snprintf(buf, PAGE_SIZE, "0x%p-----0x%x\n",
80                 fbi->screen_base,fbi->fix.smem_len);
81 }
82
83 static ssize_t show_fb_state(struct device *dev,
84                 struct device_attribute *attr, char *buf)
85 {
86         struct fb_info *fbi = dev_get_drvdata(dev);
87         struct rk_lcdc_device_driver * dev_drv = 
88                 (struct rk_lcdc_device_driver * )fbi->par;
89         int layer_id = get_fb_layer_id(&fbi->fix);
90         int state = dev_drv->get_layer_state(dev_drv,layer_id);
91         return snprintf(buf, PAGE_SIZE, "%s\n",state?"enabled":"disabled");
92         
93 }
94 static ssize_t set_fb_state(struct device *dev,struct device_attribute *attr,
95         const char *buf, size_t count)
96 {
97         struct fb_info *fbi = dev_get_drvdata(dev);
98         int state;
99         int ret;
100         ret = kstrtoint(buf, 0, &state);
101         if(ret)
102         {
103                 return ret;
104         }
105         if(state)
106         {
107                 fbi->fbops->fb_open(fbi,1);
108         }
109         else
110         {
111                 fbi->fbops->fb_release(fbi,1);
112         }
113         return count;
114 }
115
116 static ssize_t show_overlay(struct device *dev,
117                 struct device_attribute *attr, char *buf)
118 {
119         struct fb_info *fbi = dev_get_drvdata(dev);
120         struct rk_lcdc_device_driver * dev_drv = 
121                 (struct rk_lcdc_device_driver * )fbi->par;
122         int ovl;
123         ovl =  dev_drv->ovl_mgr(dev_drv,0,0);
124         if(ovl < 0)
125         {
126                 return ovl;
127         }
128
129         return snprintf(buf, PAGE_SIZE, "%s\n",
130                 ovl?"win0 on the top of win1":"win1 on the top of win0");
131         
132 }
133 static ssize_t set_overlay(struct device *dev,struct device_attribute *attr,
134         const char *buf, size_t count)
135 {
136         struct fb_info *fbi = dev_get_drvdata(dev);
137         struct rk_lcdc_device_driver * dev_drv = 
138                 (struct rk_lcdc_device_driver * )fbi->par;
139         int ovl;
140         int ret;
141         ret = kstrtoint(buf, 0, &ovl);
142         if(ret)
143         {
144                 return ret;
145         }
146         ret = dev_drv->ovl_mgr(dev_drv,ovl,1);
147         if(ret < 0)
148         {
149                 return ret;
150         }
151
152         return count;
153 }
154
155 static struct device_attribute rkfb_attrs[] = {
156         __ATTR(phys_addr, S_IRUGO, show_phys, NULL),
157         __ATTR(virt_addr, S_IRUGO, show_virt, NULL),
158         __ATTR(disp_info, S_IRUGO, show_disp_info, NULL),
159         __ATTR(screen_info, S_IRUGO, show_screen_info, NULL),
160         __ATTR(enable, S_IRUGO | S_IWUSR, show_fb_state, set_fb_state),
161         __ATTR(overlay, S_IRUGO | S_IWUSR, show_overlay, set_overlay),
162 };
163
164 int rkfb_create_sysfs(struct fb_info *fbi)
165 {
166         int r;
167         int t;
168         for (t = 0; t < ARRAY_SIZE(rkfb_attrs); t++)
169         {
170                 r = device_create_file(fbi->dev,&rkfb_attrs[t]);
171                 if (r)
172                 {
173                         dev_err(fbi->dev, "failed to create sysfs "
174                                         "file\n");
175                         return r;
176                 }
177         }
178         
179
180         return 0;
181 }
182
183 void rkfb_remove_sysfs(struct rk_fb_inf *inf)
184 {
185         int i, t;
186
187         for (i = 0; i < inf->num_fb; i++) {
188                 for (t = 0; t < ARRAY_SIZE(rkfb_attrs); t++)
189                         device_remove_file(inf->fb[i]->dev,
190                                         &rkfb_attrs[t]);
191         }
192 }
193