3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * Inki Dae <inki.dae@samsung.com>
6 * Joonyoung Shim <jy0922.shim@samsung.com>
7 * Seung-Woo Kim <sw0312.kim@samsung.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
31 #include "drm_crtc_helper.h"
32 #include "drm_fb_helper.h"
34 #include "exynos_drm_drv.h"
35 #include "exynos_drm_fb.h"
36 #include "exynos_drm_gem.h"
38 #define to_exynos_fb(x) container_of(x, struct exynos_drm_fb, fb)
41 * exynos specific framebuffer structure.
43 * @fb: drm framebuffer obejct.
44 * @exynos_gem_obj: array of exynos specific gem object containing a gem object.
46 struct exynos_drm_fb {
47 struct drm_framebuffer fb;
48 struct exynos_drm_gem_obj *exynos_gem_obj[MAX_FB_BUFFER];
51 static void exynos_drm_fb_destroy(struct drm_framebuffer *fb)
53 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
56 DRM_DEBUG_KMS("%s\n", __FILE__);
58 drm_framebuffer_cleanup(fb);
60 for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem_obj); i++) {
61 struct drm_gem_object *obj;
63 if (exynos_fb->exynos_gem_obj[i] == NULL)
66 obj = &exynos_fb->exynos_gem_obj[i]->base;
67 drm_gem_object_unreference_unlocked(obj);
74 static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb,
75 struct drm_file *file_priv,
78 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
80 DRM_DEBUG_KMS("%s\n", __FILE__);
82 return drm_gem_handle_create(file_priv,
83 &exynos_fb->exynos_gem_obj[0]->base, handle);
86 static int exynos_drm_fb_dirty(struct drm_framebuffer *fb,
87 struct drm_file *file_priv, unsigned flags,
88 unsigned color, struct drm_clip_rect *clips,
91 DRM_DEBUG_KMS("%s\n", __FILE__);
98 static struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
99 .destroy = exynos_drm_fb_destroy,
100 .create_handle = exynos_drm_fb_create_handle,
101 .dirty = exynos_drm_fb_dirty,
104 struct drm_framebuffer *
105 exynos_drm_framebuffer_init(struct drm_device *dev,
106 struct drm_mode_fb_cmd2 *mode_cmd,
107 struct drm_gem_object *obj)
109 struct exynos_drm_fb *exynos_fb;
112 exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
114 DRM_ERROR("failed to allocate exynos drm framebuffer\n");
115 return ERR_PTR(-ENOMEM);
118 ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
120 DRM_ERROR("failed to initialize framebuffer\n");
124 drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
125 exynos_fb->exynos_gem_obj[0] = to_exynos_gem_obj(obj);
127 return &exynos_fb->fb;
130 static struct drm_framebuffer *
131 exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
132 struct drm_mode_fb_cmd2 *mode_cmd)
134 struct drm_gem_object *obj;
135 struct drm_framebuffer *fb;
136 struct exynos_drm_fb *exynos_fb;
140 DRM_DEBUG_KMS("%s\n", __FILE__);
142 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
144 DRM_ERROR("failed to lookup gem object\n");
145 return ERR_PTR(-ENOENT);
148 fb = exynos_drm_framebuffer_init(dev, mode_cmd, obj);
150 drm_gem_object_unreference_unlocked(obj);
154 exynos_fb = to_exynos_fb(fb);
155 nr = exynos_drm_format_num_buffers(fb->pixel_format);
157 for (i = 1; i < nr; i++) {
158 obj = drm_gem_object_lookup(dev, file_priv,
159 mode_cmd->handles[i]);
161 DRM_ERROR("failed to lookup gem object\n");
162 exynos_drm_fb_destroy(fb);
163 return ERR_PTR(-ENOENT);
166 exynos_fb->exynos_gem_obj[i] = to_exynos_gem_obj(obj);
172 struct exynos_drm_gem_buf *exynos_drm_fb_buffer(struct drm_framebuffer *fb,
175 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
176 struct exynos_drm_gem_buf *buffer;
178 DRM_DEBUG_KMS("%s\n", __FILE__);
180 if (index >= MAX_FB_BUFFER)
183 buffer = exynos_fb->exynos_gem_obj[index]->buffer;
187 DRM_DEBUG_KMS("vaddr = 0x%lx, dma_addr = 0x%lx\n",
188 (unsigned long)buffer->kvaddr,
189 (unsigned long)buffer->dma_addr);
194 static void exynos_drm_output_poll_changed(struct drm_device *dev)
196 struct exynos_drm_private *private = dev->dev_private;
197 struct drm_fb_helper *fb_helper = private->fb_helper;
200 drm_fb_helper_hotplug_event(fb_helper);
203 static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
204 .fb_create = exynos_user_fb_create,
205 .output_poll_changed = exynos_drm_output_poll_changed,
208 void exynos_drm_mode_config_init(struct drm_device *dev)
210 dev->mode_config.min_width = 0;
211 dev->mode_config.min_height = 0;
214 * set max width and height as default value(4096x4096).
215 * this value would be used to check framebuffer size limitation
216 * at drm_mode_addfb().
218 dev->mode_config.max_width = 4096;
219 dev->mode_config.max_height = 4096;
221 dev->mode_config.funcs = &exynos_drm_mode_config_funcs;