]>
Commit | Line | Data |
---|---|---|
1c248b7d ID |
1 | /* exynos_drm_fbdev.c |
2 | * | |
3 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. | |
4 | * Authors: | |
5 | * Inki Dae <inki.dae@samsung.com> | |
6 | * Joonyoung Shim <jy0922.shim@samsung.com> | |
7 | * Seung-Woo Kim <sw0312.kim@samsung.com> | |
8 | * | |
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: | |
15 | * | |
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 | |
18 | * Software. | |
19 | * | |
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. | |
27 | */ | |
28 | ||
760285e7 DH |
29 | #include <drm/drmP.h> |
30 | #include <drm/drm_crtc.h> | |
31 | #include <drm/drm_fb_helper.h> | |
32 | #include <drm/drm_crtc_helper.h> | |
1c248b7d ID |
33 | |
34 | #include "exynos_drm_drv.h" | |
35 | #include "exynos_drm_fb.h" | |
2c871127 | 36 | #include "exynos_drm_gem.h" |
1c248b7d ID |
37 | |
38 | #define MAX_CONNECTOR 4 | |
39 | #define PREFERRED_BPP 32 | |
40 | ||
41 | #define to_exynos_fbdev(x) container_of(x, struct exynos_drm_fbdev,\ | |
42 | drm_fb_helper) | |
43 | ||
44 | struct exynos_drm_fbdev { | |
e1533c08 JS |
45 | struct drm_fb_helper drm_fb_helper; |
46 | struct exynos_drm_gem_obj *exynos_gem_obj; | |
1c248b7d ID |
47 | }; |
48 | ||
dd265850 P |
49 | static int exynos_drm_fb_mmap(struct fb_info *info, |
50 | struct vm_area_struct *vma) | |
51 | { | |
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; | |
57 | int ret; | |
58 | ||
59 | DRM_DEBUG_KMS("%s\n", __func__); | |
60 | ||
61 | vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP; | |
62 | ||
63 | vm_size = vma->vm_end - vma->vm_start; | |
64 | ||
65 | if (vm_size > buffer->size) | |
66 | return -EINVAL; | |
67 | ||
68 | ret = dma_mmap_attrs(helper->dev->dev, vma, buffer->kvaddr, | |
69 | buffer->dma_addr, buffer->size, &buffer->dma_attrs); | |
70 | if (ret < 0) { | |
71 | DRM_ERROR("failed to mmap.\n"); | |
72 | return ret; | |
73 | } | |
74 | ||
75 | return 0; | |
76 | } | |
77 | ||
1c248b7d ID |
78 | static struct fb_ops exynos_drm_fb_ops = { |
79 | .owner = THIS_MODULE, | |
dd265850 | 80 | .fb_mmap = exynos_drm_fb_mmap, |
1c248b7d ID |
81 | .fb_fillrect = cfb_fillrect, |
82 | .fb_copyarea = cfb_copyarea, | |
83 | .fb_imageblit = cfb_imageblit, | |
84 | .fb_check_var = drm_fb_helper_check_var, | |
83b316fd | 85 | .fb_set_par = drm_fb_helper_set_par, |
1c248b7d ID |
86 | .fb_blank = drm_fb_helper_blank, |
87 | .fb_pan_display = drm_fb_helper_pan_display, | |
88 | .fb_setcmap = drm_fb_helper_setcmap, | |
89 | }; | |
90 | ||
19c8b834 | 91 | static int exynos_drm_fbdev_update(struct drm_fb_helper *helper, |
aa6b2b6c | 92 | struct drm_framebuffer *fb) |
1c248b7d ID |
93 | { |
94 | struct fb_info *fbi = helper->fbdev; | |
95 | struct drm_device *dev = helper->dev; | |
2c871127 | 96 | struct exynos_drm_gem_buf *buffer; |
aa6b2b6c | 97 | unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3); |
19c8b834 | 98 | unsigned long offset; |
1c248b7d ID |
99 | |
100 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
101 | ||
01f2c773 | 102 | drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth); |
aa6b2b6c | 103 | drm_fb_helper_fill_var(fbi, helper, fb->width, fb->height); |
1c248b7d | 104 | |
229d3534 SWK |
105 | /* RGB formats use only one buffer */ |
106 | buffer = exynos_drm_fb_buffer(fb, 0); | |
2c871127 ID |
107 | if (!buffer) { |
108 | DRM_LOG_KMS("buffer is null.\n"); | |
19c8b834 ID |
109 | return -EFAULT; |
110 | } | |
1c248b7d | 111 | |
01ed8126 ID |
112 | /* buffer count to framebuffer always is 1 at booting time. */ |
113 | exynos_drm_fb_set_buf_cnt(fb, 1); | |
114 | ||
19c8b834 | 115 | offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3); |
01f2c773 | 116 | offset += fbi->var.yoffset * fb->pitches[0]; |
1c248b7d | 117 | |
2c871127 ID |
118 | dev->mode_config.fb_base = (resource_size_t)buffer->dma_addr; |
119 | fbi->screen_base = buffer->kvaddr + offset; | |
9d934799 ID |
120 | fbi->fix.smem_start = (unsigned long)(page_to_phys(buffer->pages[0]) + |
121 | offset); | |
1c248b7d | 122 | fbi->screen_size = size; |
1c248b7d | 123 | fbi->fix.smem_len = size; |
19c8b834 ID |
124 | |
125 | return 0; | |
1c248b7d ID |
126 | } |
127 | ||
128 | static int exynos_drm_fbdev_create(struct drm_fb_helper *helper, | |
129 | struct drm_fb_helper_surface_size *sizes) | |
130 | { | |
131 | struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper); | |
e1533c08 | 132 | struct exynos_drm_gem_obj *exynos_gem_obj; |
1c248b7d ID |
133 | struct drm_device *dev = helper->dev; |
134 | struct fb_info *fbi; | |
a794d57d | 135 | struct drm_mode_fb_cmd2 mode_cmd = { 0 }; |
1c248b7d | 136 | struct platform_device *pdev = dev->platformdev; |
e1533c08 | 137 | unsigned long size; |
1c248b7d ID |
138 | int ret; |
139 | ||
140 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
141 | ||
142 | DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n", | |
143 | sizes->surface_width, sizes->surface_height, | |
144 | sizes->surface_bpp); | |
145 | ||
146 | mode_cmd.width = sizes->surface_width; | |
147 | mode_cmd.height = sizes->surface_height; | |
a794d57d JS |
148 | mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3); |
149 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, | |
150 | sizes->surface_depth); | |
1c248b7d ID |
151 | |
152 | mutex_lock(&dev->struct_mutex); | |
153 | ||
154 | fbi = framebuffer_alloc(0, &pdev->dev); | |
155 | if (!fbi) { | |
156 | DRM_ERROR("failed to allocate fb info.\n"); | |
157 | ret = -ENOMEM; | |
158 | goto out; | |
159 | } | |
160 | ||
e1533c08 | 161 | size = mode_cmd.pitches[0] * mode_cmd.height; |
2b35892e ID |
162 | |
163 | /* 0 means to allocate physically continuous memory */ | |
164 | exynos_gem_obj = exynos_drm_gem_create(dev, 0, size); | |
e1533c08 JS |
165 | if (IS_ERR(exynos_gem_obj)) { |
166 | ret = PTR_ERR(exynos_gem_obj); | |
167 | goto out; | |
168 | } | |
169 | ||
170 | exynos_fbdev->exynos_gem_obj = exynos_gem_obj; | |
171 | ||
172 | helper->fb = exynos_drm_framebuffer_init(dev, &mode_cmd, | |
173 | &exynos_gem_obj->base); | |
174 | if (IS_ERR_OR_NULL(helper->fb)) { | |
1c248b7d | 175 | DRM_ERROR("failed to create drm framebuffer.\n"); |
e1533c08 | 176 | ret = PTR_ERR(helper->fb); |
1c248b7d ID |
177 | goto out; |
178 | } | |
179 | ||
1c248b7d ID |
180 | helper->fbdev = fbi; |
181 | ||
182 | fbi->par = helper; | |
183 | fbi->flags = FBINFO_FLAG_DEFAULT; | |
184 | fbi->fbops = &exynos_drm_fb_ops; | |
185 | ||
186 | ret = fb_alloc_cmap(&fbi->cmap, 256, 0); | |
187 | if (ret) { | |
188 | DRM_ERROR("failed to allocate cmap.\n"); | |
189 | goto out; | |
190 | } | |
191 | ||
aa6b2b6c | 192 | ret = exynos_drm_fbdev_update(helper, helper->fb); |
e1533c08 | 193 | if (ret < 0) { |
19c8b834 | 194 | fb_dealloc_cmap(&fbi->cmap); |
e1533c08 JS |
195 | goto out; |
196 | } | |
1c248b7d ID |
197 | |
198 | /* | |
199 | * if failed, all resources allocated above would be released by | |
200 | * drm_mode_config_cleanup() when drm_load() had been called prior | |
201 | * to any specific driver such as fimd or hdmi driver. | |
202 | */ | |
203 | out: | |
204 | mutex_unlock(&dev->struct_mutex); | |
205 | return ret; | |
206 | } | |
207 | ||
1c248b7d ID |
208 | static int exynos_drm_fbdev_probe(struct drm_fb_helper *helper, |
209 | struct drm_fb_helper_surface_size *sizes) | |
210 | { | |
211 | int ret = 0; | |
212 | ||
213 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
214 | ||
bc41eae2 ID |
215 | /* |
216 | * with !helper->fb, it means that this funcion is called first time | |
217 | * and after that, the helper->fb would be used as clone mode. | |
218 | */ | |
1c248b7d ID |
219 | if (!helper->fb) { |
220 | ret = exynos_drm_fbdev_create(helper, sizes); | |
221 | if (ret < 0) { | |
222 | DRM_ERROR("failed to create fbdev.\n"); | |
223 | return ret; | |
224 | } | |
225 | ||
226 | /* | |
227 | * fb_helper expects a value more than 1 if succeed | |
228 | * because register_framebuffer() should be called. | |
229 | */ | |
230 | ret = 1; | |
1c248b7d ID |
231 | } |
232 | ||
233 | return ret; | |
234 | } | |
235 | ||
236 | static struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = { | |
237 | .fb_probe = exynos_drm_fbdev_probe, | |
238 | }; | |
239 | ||
240 | int exynos_drm_fbdev_init(struct drm_device *dev) | |
241 | { | |
242 | struct exynos_drm_fbdev *fbdev; | |
243 | struct exynos_drm_private *private = dev->dev_private; | |
244 | struct drm_fb_helper *helper; | |
245 | unsigned int num_crtc; | |
246 | int ret; | |
247 | ||
248 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
249 | ||
250 | if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector) | |
251 | return 0; | |
252 | ||
253 | fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL); | |
254 | if (!fbdev) { | |
255 | DRM_ERROR("failed to allocate drm fbdev.\n"); | |
256 | return -ENOMEM; | |
257 | } | |
258 | ||
259 | private->fb_helper = helper = &fbdev->drm_fb_helper; | |
260 | helper->funcs = &exynos_drm_fb_helper_funcs; | |
261 | ||
262 | num_crtc = dev->mode_config.num_crtc; | |
263 | ||
264 | ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR); | |
265 | if (ret < 0) { | |
266 | DRM_ERROR("failed to initialize drm fb helper.\n"); | |
267 | goto err_init; | |
268 | } | |
269 | ||
270 | ret = drm_fb_helper_single_add_all_connectors(helper); | |
271 | if (ret < 0) { | |
272 | DRM_ERROR("failed to register drm_fb_helper_connector.\n"); | |
273 | goto err_setup; | |
274 | ||
275 | } | |
276 | ||
277 | ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP); | |
278 | if (ret < 0) { | |
279 | DRM_ERROR("failed to set up hw configuration.\n"); | |
280 | goto err_setup; | |
281 | } | |
282 | ||
283 | return 0; | |
284 | ||
285 | err_setup: | |
286 | drm_fb_helper_fini(helper); | |
287 | ||
288 | err_init: | |
289 | private->fb_helper = NULL; | |
290 | kfree(fbdev); | |
291 | ||
292 | return ret; | |
293 | } | |
294 | ||
295 | static void exynos_drm_fbdev_destroy(struct drm_device *dev, | |
296 | struct drm_fb_helper *fb_helper) | |
297 | { | |
298 | struct drm_framebuffer *fb; | |
299 | ||
300 | /* release drm framebuffer and real buffer */ | |
301 | if (fb_helper->fb && fb_helper->fb->funcs) { | |
302 | fb = fb_helper->fb; | |
f7eff60e RC |
303 | if (fb) |
304 | drm_framebuffer_remove(fb); | |
1c248b7d ID |
305 | } |
306 | ||
307 | /* release linux framebuffer */ | |
308 | if (fb_helper->fbdev) { | |
309 | struct fb_info *info; | |
310 | int ret; | |
311 | ||
312 | info = fb_helper->fbdev; | |
313 | ret = unregister_framebuffer(info); | |
314 | if (ret < 0) | |
315 | DRM_DEBUG_KMS("failed unregister_framebuffer()\n"); | |
316 | ||
317 | if (info->cmap.len) | |
318 | fb_dealloc_cmap(&info->cmap); | |
319 | ||
320 | framebuffer_release(info); | |
321 | } | |
322 | ||
323 | drm_fb_helper_fini(fb_helper); | |
324 | } | |
325 | ||
326 | void exynos_drm_fbdev_fini(struct drm_device *dev) | |
327 | { | |
328 | struct exynos_drm_private *private = dev->dev_private; | |
329 | struct exynos_drm_fbdev *fbdev; | |
330 | ||
331 | if (!private || !private->fb_helper) | |
332 | return; | |
333 | ||
334 | fbdev = to_exynos_fbdev(private->fb_helper); | |
335 | ||
e1533c08 JS |
336 | if (fbdev->exynos_gem_obj) |
337 | exynos_drm_gem_destroy(fbdev->exynos_gem_obj); | |
338 | ||
1c248b7d ID |
339 | exynos_drm_fbdev_destroy(dev, private->fb_helper); |
340 | kfree(fbdev); | |
341 | private->fb_helper = NULL; | |
342 | } | |
343 | ||
344 | void exynos_drm_fbdev_restore_mode(struct drm_device *dev) | |
345 | { | |
346 | struct exynos_drm_private *private = dev->dev_private; | |
347 | ||
348 | if (!private || !private->fb_helper) | |
349 | return; | |
350 | ||
351 | drm_fb_helper_restore_fbdev_mode(private->fb_helper); | |
352 | } |