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.
30 #include <drm/drm_crtc.h>
31 #include <drm/drm_fb_helper.h>
32 #include <drm/drm_crtc_helper.h>
34 #include "exynos_drm_drv.h"
35 #include "exynos_drm_fb.h"
36 #include "exynos_drm_gem.h"
38 #define MAX_CONNECTOR 4
39 #define PREFERRED_BPP 32
41 #define to_exynos_fbdev(x) container_of(x, struct exynos_drm_fbdev,\
44 struct exynos_drm_fbdev
{
45 struct drm_fb_helper drm_fb_helper
;
46 struct exynos_drm_gem_obj
*exynos_gem_obj
;
49 static int exynos_drm_fb_mmap(struct fb_info
*info
,
50 struct vm_area_struct
*vma
)
52 struct drm_fb_helper
*helper
= info
->par
;
53 struct exynos_drm_fbdev
*exynos_fbd
= to_exynos_fbdev(helper
);
54 struct exynos_drm_gem_obj
*exynos_gem_obj
= exynos_fbd
->exynos_gem_obj
;
55 struct exynos_drm_gem_buf
*buffer
= exynos_gem_obj
->buffer
;
56 unsigned long vm_size
;
59 DRM_DEBUG_KMS("%s\n", __func__
);
61 vma
->vm_flags
|= VM_IO
| VM_DONTEXPAND
| VM_DONTDUMP
;
63 vm_size
= vma
->vm_end
- vma
->vm_start
;
65 if (vm_size
> buffer
->size
)
68 ret
= dma_mmap_attrs(helper
->dev
->dev
, vma
, buffer
->pages
,
69 buffer
->dma_addr
, buffer
->size
, &buffer
->dma_attrs
);
71 DRM_ERROR("failed to mmap.\n");
78 static struct fb_ops exynos_drm_fb_ops
= {
80 .fb_mmap
= exynos_drm_fb_mmap
,
81 .fb_fillrect
= cfb_fillrect
,
82 .fb_copyarea
= cfb_copyarea
,
83 .fb_imageblit
= cfb_imageblit
,
84 .fb_check_var
= drm_fb_helper_check_var
,
85 .fb_set_par
= drm_fb_helper_set_par
,
86 .fb_blank
= drm_fb_helper_blank
,
87 .fb_pan_display
= drm_fb_helper_pan_display
,
88 .fb_setcmap
= drm_fb_helper_setcmap
,
91 static int exynos_drm_fbdev_update(struct drm_fb_helper
*helper
,
92 struct drm_framebuffer
*fb
)
94 struct fb_info
*fbi
= helper
->fbdev
;
95 struct drm_device
*dev
= helper
->dev
;
96 struct exynos_drm_gem_buf
*buffer
;
97 unsigned int size
= fb
->width
* fb
->height
* (fb
->bits_per_pixel
>> 3);
100 DRM_DEBUG_KMS("%s\n", __FILE__
);
102 drm_fb_helper_fill_fix(fbi
, fb
->pitches
[0], fb
->depth
);
103 drm_fb_helper_fill_var(fbi
, helper
, fb
->width
, fb
->height
);
105 /* RGB formats use only one buffer */
106 buffer
= exynos_drm_fb_buffer(fb
, 0);
108 DRM_LOG_KMS("buffer is null.\n");
112 /* map pages with kernel virtual space. */
113 if (!buffer
->kvaddr
) {
114 unsigned int nr_pages
= buffer
->size
>> PAGE_SHIFT
;
115 buffer
->kvaddr
= vmap(buffer
->pages
, nr_pages
, VM_MAP
,
116 pgprot_writecombine(PAGE_KERNEL
));
117 if (!buffer
->kvaddr
) {
118 DRM_ERROR("failed to map pages to kernel space.\n");
123 /* buffer count to framebuffer always is 1 at booting time. */
124 exynos_drm_fb_set_buf_cnt(fb
, 1);
126 offset
= fbi
->var
.xoffset
* (fb
->bits_per_pixel
>> 3);
127 offset
+= fbi
->var
.yoffset
* fb
->pitches
[0];
129 dev
->mode_config
.fb_base
= (resource_size_t
)buffer
->dma_addr
;
130 fbi
->screen_base
= buffer
->kvaddr
+ offset
;
131 fbi
->fix
.smem_start
= (unsigned long)
132 (page_to_phys(sg_page(buffer
->sgt
->sgl
)) + offset
);
133 fbi
->screen_size
= size
;
134 fbi
->fix
.smem_len
= size
;
139 static int exynos_drm_fbdev_create(struct drm_fb_helper
*helper
,
140 struct drm_fb_helper_surface_size
*sizes
)
142 struct exynos_drm_fbdev
*exynos_fbdev
= to_exynos_fbdev(helper
);
143 struct exynos_drm_gem_obj
*exynos_gem_obj
;
144 struct drm_device
*dev
= helper
->dev
;
146 struct drm_mode_fb_cmd2 mode_cmd
= { 0 };
147 struct platform_device
*pdev
= dev
->platformdev
;
151 DRM_DEBUG_KMS("%s\n", __FILE__
);
153 DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
154 sizes
->surface_width
, sizes
->surface_height
,
157 mode_cmd
.width
= sizes
->surface_width
;
158 mode_cmd
.height
= sizes
->surface_height
;
159 mode_cmd
.pitches
[0] = sizes
->surface_width
* (sizes
->surface_bpp
>> 3);
160 mode_cmd
.pixel_format
= drm_mode_legacy_fb_format(sizes
->surface_bpp
,
161 sizes
->surface_depth
);
163 mutex_lock(&dev
->struct_mutex
);
165 fbi
= framebuffer_alloc(0, &pdev
->dev
);
167 DRM_ERROR("failed to allocate fb info.\n");
172 size
= mode_cmd
.pitches
[0] * mode_cmd
.height
;
174 /* 0 means to allocate physically continuous memory */
175 exynos_gem_obj
= exynos_drm_gem_create(dev
, 0, size
);
176 if (IS_ERR(exynos_gem_obj
)) {
177 ret
= PTR_ERR(exynos_gem_obj
);
178 goto err_release_framebuffer
;
181 exynos_fbdev
->exynos_gem_obj
= exynos_gem_obj
;
183 helper
->fb
= exynos_drm_framebuffer_init(dev
, &mode_cmd
,
184 &exynos_gem_obj
->base
);
185 if (IS_ERR_OR_NULL(helper
->fb
)) {
186 DRM_ERROR("failed to create drm framebuffer.\n");
187 ret
= PTR_ERR(helper
->fb
);
188 goto err_destroy_gem
;
194 fbi
->flags
= FBINFO_FLAG_DEFAULT
;
195 fbi
->fbops
= &exynos_drm_fb_ops
;
197 ret
= fb_alloc_cmap(&fbi
->cmap
, 256, 0);
199 DRM_ERROR("failed to allocate cmap.\n");
200 goto err_destroy_framebuffer
;
203 ret
= exynos_drm_fbdev_update(helper
, helper
->fb
);
205 goto err_dealloc_cmap
;
207 mutex_unlock(&dev
->struct_mutex
);
211 fb_dealloc_cmap(&fbi
->cmap
);
212 err_destroy_framebuffer
:
213 drm_framebuffer_cleanup(helper
->fb
);
215 exynos_drm_gem_destroy(exynos_gem_obj
);
216 err_release_framebuffer
:
217 framebuffer_release(fbi
);
220 * if failed, all resources allocated above would be released by
221 * drm_mode_config_cleanup() when drm_load() had been called prior
222 * to any specific driver such as fimd or hdmi driver.
225 mutex_unlock(&dev
->struct_mutex
);
229 static int exynos_drm_fbdev_probe(struct drm_fb_helper
*helper
,
230 struct drm_fb_helper_surface_size
*sizes
)
234 DRM_DEBUG_KMS("%s\n", __FILE__
);
237 * with !helper->fb, it means that this funcion is called first time
238 * and after that, the helper->fb would be used as clone mode.
241 ret
= exynos_drm_fbdev_create(helper
, sizes
);
243 DRM_ERROR("failed to create fbdev.\n");
248 * fb_helper expects a value more than 1 if succeed
249 * because register_framebuffer() should be called.
257 static struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs
= {
258 .fb_probe
= exynos_drm_fbdev_probe
,
261 int exynos_drm_fbdev_init(struct drm_device
*dev
)
263 struct exynos_drm_fbdev
*fbdev
;
264 struct exynos_drm_private
*private = dev
->dev_private
;
265 struct drm_fb_helper
*helper
;
266 unsigned int num_crtc
;
269 DRM_DEBUG_KMS("%s\n", __FILE__
);
271 if (!dev
->mode_config
.num_crtc
|| !dev
->mode_config
.num_connector
)
274 fbdev
= kzalloc(sizeof(*fbdev
), GFP_KERNEL
);
276 DRM_ERROR("failed to allocate drm fbdev.\n");
280 private->fb_helper
= helper
= &fbdev
->drm_fb_helper
;
281 helper
->funcs
= &exynos_drm_fb_helper_funcs
;
283 num_crtc
= dev
->mode_config
.num_crtc
;
285 ret
= drm_fb_helper_init(dev
, helper
, num_crtc
, MAX_CONNECTOR
);
287 DRM_ERROR("failed to initialize drm fb helper.\n");
291 ret
= drm_fb_helper_single_add_all_connectors(helper
);
293 DRM_ERROR("failed to register drm_fb_helper_connector.\n");
298 ret
= drm_fb_helper_initial_config(helper
, PREFERRED_BPP
);
300 DRM_ERROR("failed to set up hw configuration.\n");
307 drm_fb_helper_fini(helper
);
310 private->fb_helper
= NULL
;
316 static void exynos_drm_fbdev_destroy(struct drm_device
*dev
,
317 struct drm_fb_helper
*fb_helper
)
319 struct exynos_drm_fbdev
*exynos_fbd
= to_exynos_fbdev(fb_helper
);
320 struct exynos_drm_gem_obj
*exynos_gem_obj
= exynos_fbd
->exynos_gem_obj
;
321 struct drm_framebuffer
*fb
;
323 if (exynos_gem_obj
->buffer
->kvaddr
)
324 vunmap(exynos_gem_obj
->buffer
->kvaddr
);
326 /* release drm framebuffer and real buffer */
327 if (fb_helper
->fb
&& fb_helper
->fb
->funcs
) {
330 drm_framebuffer_remove(fb
);
333 /* release linux framebuffer */
334 if (fb_helper
->fbdev
) {
335 struct fb_info
*info
;
338 info
= fb_helper
->fbdev
;
339 ret
= unregister_framebuffer(info
);
341 DRM_DEBUG_KMS("failed unregister_framebuffer()\n");
344 fb_dealloc_cmap(&info
->cmap
);
346 framebuffer_release(info
);
349 drm_fb_helper_fini(fb_helper
);
352 void exynos_drm_fbdev_fini(struct drm_device
*dev
)
354 struct exynos_drm_private
*private = dev
->dev_private
;
355 struct exynos_drm_fbdev
*fbdev
;
357 if (!private || !private->fb_helper
)
360 fbdev
= to_exynos_fbdev(private->fb_helper
);
362 if (fbdev
->exynos_gem_obj
)
363 exynos_drm_gem_destroy(fbdev
->exynos_gem_obj
);
365 exynos_drm_fbdev_destroy(dev
, private->fb_helper
);
367 private->fb_helper
= NULL
;
370 void exynos_drm_fbdev_restore_mode(struct drm_device
*dev
)
372 struct exynos_drm_private
*private = dev
->dev_private
;
374 if (!private || !private->fb_helper
)
377 drm_fb_helper_restore_fbdev_mode(private->fb_helper
);