Linux 4.2.1
[linux/fpc-iii.git] / drivers / gpu / drm / rockchip / rockchip_drm_fb.c
blob002645bb5bbf9b83f30c0f383050c817d8e43dbc
1 /*
2 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3 * Author:Mark Yao <mark.yao@rock-chips.com>
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/kernel.h>
16 #include <drm/drm.h>
17 #include <drm/drmP.h>
18 #include <drm/drm_fb_helper.h>
19 #include <drm/drm_crtc_helper.h>
21 #include "rockchip_drm_drv.h"
22 #include "rockchip_drm_gem.h"
24 #define to_rockchip_fb(x) container_of(x, struct rockchip_drm_fb, fb)
26 struct rockchip_drm_fb {
27 struct drm_framebuffer fb;
28 struct drm_gem_object *obj[ROCKCHIP_MAX_FB_BUFFER];
31 struct drm_gem_object *rockchip_fb_get_gem_obj(struct drm_framebuffer *fb,
32 unsigned int plane)
34 struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
36 if (plane >= ROCKCHIP_MAX_FB_BUFFER)
37 return NULL;
39 return rk_fb->obj[plane];
41 EXPORT_SYMBOL_GPL(rockchip_fb_get_gem_obj);
43 static void rockchip_drm_fb_destroy(struct drm_framebuffer *fb)
45 struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
46 struct drm_gem_object *obj;
47 int i;
49 for (i = 0; i < ROCKCHIP_MAX_FB_BUFFER; i++) {
50 obj = rockchip_fb->obj[i];
51 if (obj)
52 drm_gem_object_unreference_unlocked(obj);
55 drm_framebuffer_cleanup(fb);
56 kfree(rockchip_fb);
59 static int rockchip_drm_fb_create_handle(struct drm_framebuffer *fb,
60 struct drm_file *file_priv,
61 unsigned int *handle)
63 struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
65 return drm_gem_handle_create(file_priv,
66 rockchip_fb->obj[0], handle);
69 static struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
70 .destroy = rockchip_drm_fb_destroy,
71 .create_handle = rockchip_drm_fb_create_handle,
74 static struct rockchip_drm_fb *
75 rockchip_fb_alloc(struct drm_device *dev, struct drm_mode_fb_cmd2 *mode_cmd,
76 struct drm_gem_object **obj, unsigned int num_planes)
78 struct rockchip_drm_fb *rockchip_fb;
79 int ret;
80 int i;
82 rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL);
83 if (!rockchip_fb)
84 return ERR_PTR(-ENOMEM);
86 drm_helper_mode_fill_fb_struct(&rockchip_fb->fb, mode_cmd);
88 for (i = 0; i < num_planes; i++)
89 rockchip_fb->obj[i] = obj[i];
91 ret = drm_framebuffer_init(dev, &rockchip_fb->fb,
92 &rockchip_drm_fb_funcs);
93 if (ret) {
94 dev_err(dev->dev, "Failed to initialize framebuffer: %d\n",
95 ret);
96 kfree(rockchip_fb);
97 return ERR_PTR(ret);
100 return rockchip_fb;
103 static struct drm_framebuffer *
104 rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
105 struct drm_mode_fb_cmd2 *mode_cmd)
107 struct rockchip_drm_fb *rockchip_fb;
108 struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER];
109 struct drm_gem_object *obj;
110 unsigned int hsub;
111 unsigned int vsub;
112 int num_planes;
113 int ret;
114 int i;
116 hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
117 vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
118 num_planes = min(drm_format_num_planes(mode_cmd->pixel_format),
119 ROCKCHIP_MAX_FB_BUFFER);
121 for (i = 0; i < num_planes; i++) {
122 unsigned int width = mode_cmd->width / (i ? hsub : 1);
123 unsigned int height = mode_cmd->height / (i ? vsub : 1);
124 unsigned int min_size;
126 obj = drm_gem_object_lookup(dev, file_priv,
127 mode_cmd->handles[i]);
128 if (!obj) {
129 dev_err(dev->dev, "Failed to lookup GEM object\n");
130 ret = -ENXIO;
131 goto err_gem_object_unreference;
134 min_size = (height - 1) * mode_cmd->pitches[i] +
135 mode_cmd->offsets[i] +
136 width * drm_format_plane_cpp(mode_cmd->pixel_format, i);
138 if (obj->size < min_size) {
139 drm_gem_object_unreference_unlocked(obj);
140 ret = -EINVAL;
141 goto err_gem_object_unreference;
143 objs[i] = obj;
146 rockchip_fb = rockchip_fb_alloc(dev, mode_cmd, objs, i);
147 if (IS_ERR(rockchip_fb)) {
148 ret = PTR_ERR(rockchip_fb);
149 goto err_gem_object_unreference;
152 return &rockchip_fb->fb;
154 err_gem_object_unreference:
155 for (i--; i >= 0; i--)
156 drm_gem_object_unreference_unlocked(objs[i]);
157 return ERR_PTR(ret);
160 static void rockchip_drm_output_poll_changed(struct drm_device *dev)
162 struct rockchip_drm_private *private = dev->dev_private;
163 struct drm_fb_helper *fb_helper = &private->fbdev_helper;
165 if (fb_helper)
166 drm_fb_helper_hotplug_event(fb_helper);
169 static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
170 .fb_create = rockchip_user_fb_create,
171 .output_poll_changed = rockchip_drm_output_poll_changed,
174 struct drm_framebuffer *
175 rockchip_drm_framebuffer_init(struct drm_device *dev,
176 struct drm_mode_fb_cmd2 *mode_cmd,
177 struct drm_gem_object *obj)
179 struct rockchip_drm_fb *rockchip_fb;
181 rockchip_fb = rockchip_fb_alloc(dev, mode_cmd, &obj, 1);
182 if (IS_ERR(rockchip_fb))
183 return NULL;
185 return &rockchip_fb->fb;
188 void rockchip_drm_mode_config_init(struct drm_device *dev)
190 dev->mode_config.min_width = 0;
191 dev->mode_config.min_height = 0;
194 * set max width and height as default value(4096x4096).
195 * this value would be used to check framebuffer size limitation
196 * at drm_mode_addfb().
198 dev->mode_config.max_width = 4096;
199 dev->mode_config.max_height = 4096;
201 dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;