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 | ||
29 | #include "drmP.h" | |
30 | #include "drm_crtc.h" | |
31 | #include "drm_fb_helper.h" | |
32 | #include "drm_crtc_helper.h" | |
33 | ||
34 | #include "exynos_drm_drv.h" | |
35 | #include "exynos_drm_fb.h" | |
2c871127 | 36 | #include "exynos_drm_gem.h" |
19c8b834 | 37 | #include "exynos_drm_buf.h" |
1c248b7d ID |
38 | |
39 | #define MAX_CONNECTOR 4 | |
40 | #define PREFERRED_BPP 32 | |
41 | ||
42 | #define to_exynos_fbdev(x) container_of(x, struct exynos_drm_fbdev,\ | |
43 | drm_fb_helper) | |
44 | ||
45 | struct exynos_drm_fbdev { | |
46 | struct drm_fb_helper drm_fb_helper; | |
47 | struct drm_framebuffer *fb; | |
48 | }; | |
49 | ||
50 | static int exynos_drm_fbdev_set_par(struct fb_info *info) | |
51 | { | |
52 | struct fb_var_screeninfo *var = &info->var; | |
53 | ||
54 | switch (var->bits_per_pixel) { | |
55 | case 32: | |
56 | case 24: | |
57 | case 18: | |
58 | case 16: | |
59 | case 12: | |
60 | info->fix.visual = FB_VISUAL_TRUECOLOR; | |
61 | break; | |
62 | case 1: | |
63 | info->fix.visual = FB_VISUAL_MONO01; | |
64 | break; | |
65 | default: | |
66 | info->fix.visual = FB_VISUAL_PSEUDOCOLOR; | |
67 | break; | |
68 | } | |
69 | ||
70 | info->fix.line_length = (var->xres_virtual * var->bits_per_pixel) / 8; | |
71 | ||
72 | return drm_fb_helper_set_par(info); | |
73 | } | |
74 | ||
75 | ||
76 | static struct fb_ops exynos_drm_fb_ops = { | |
77 | .owner = THIS_MODULE, | |
78 | .fb_fillrect = cfb_fillrect, | |
79 | .fb_copyarea = cfb_copyarea, | |
80 | .fb_imageblit = cfb_imageblit, | |
81 | .fb_check_var = drm_fb_helper_check_var, | |
82 | .fb_set_par = exynos_drm_fbdev_set_par, | |
83 | .fb_blank = drm_fb_helper_blank, | |
84 | .fb_pan_display = drm_fb_helper_pan_display, | |
85 | .fb_setcmap = drm_fb_helper_setcmap, | |
86 | }; | |
87 | ||
19c8b834 | 88 | static int exynos_drm_fbdev_update(struct drm_fb_helper *helper, |
aa6b2b6c | 89 | struct drm_framebuffer *fb) |
1c248b7d ID |
90 | { |
91 | struct fb_info *fbi = helper->fbdev; | |
92 | struct drm_device *dev = helper->dev; | |
93 | struct exynos_drm_fbdev *exynos_fb = to_exynos_fbdev(helper); | |
2c871127 | 94 | struct exynos_drm_gem_buf *buffer; |
aa6b2b6c | 95 | unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3); |
19c8b834 | 96 | unsigned long offset; |
1c248b7d ID |
97 | |
98 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
99 | ||
100 | exynos_fb->fb = fb; | |
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 | |
2c871127 ID |
105 | buffer = exynos_drm_fb_get_buf(fb); |
106 | if (!buffer) { | |
107 | DRM_LOG_KMS("buffer is null.\n"); | |
19c8b834 ID |
108 | return -EFAULT; |
109 | } | |
1c248b7d | 110 | |
19c8b834 | 111 | offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3); |
01f2c773 | 112 | offset += fbi->var.yoffset * fb->pitches[0]; |
1c248b7d | 113 | |
2c871127 ID |
114 | dev->mode_config.fb_base = (resource_size_t)buffer->dma_addr; |
115 | fbi->screen_base = buffer->kvaddr + offset; | |
116 | fbi->fix.smem_start = (unsigned long)(buffer->dma_addr + offset); | |
1c248b7d | 117 | fbi->screen_size = size; |
1c248b7d | 118 | fbi->fix.smem_len = size; |
19c8b834 ID |
119 | |
120 | return 0; | |
1c248b7d ID |
121 | } |
122 | ||
123 | static int exynos_drm_fbdev_create(struct drm_fb_helper *helper, | |
124 | struct drm_fb_helper_surface_size *sizes) | |
125 | { | |
126 | struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper); | |
127 | struct drm_device *dev = helper->dev; | |
128 | struct fb_info *fbi; | |
a794d57d | 129 | struct drm_mode_fb_cmd2 mode_cmd = { 0 }; |
1c248b7d ID |
130 | struct platform_device *pdev = dev->platformdev; |
131 | int ret; | |
132 | ||
133 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
134 | ||
135 | DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n", | |
136 | sizes->surface_width, sizes->surface_height, | |
137 | sizes->surface_bpp); | |
138 | ||
139 | mode_cmd.width = sizes->surface_width; | |
140 | mode_cmd.height = sizes->surface_height; | |
a794d57d JS |
141 | mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3); |
142 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, | |
143 | sizes->surface_depth); | |
1c248b7d ID |
144 | |
145 | mutex_lock(&dev->struct_mutex); | |
146 | ||
147 | fbi = framebuffer_alloc(0, &pdev->dev); | |
148 | if (!fbi) { | |
149 | DRM_ERROR("failed to allocate fb info.\n"); | |
150 | ret = -ENOMEM; | |
151 | goto out; | |
152 | } | |
153 | ||
154 | exynos_fbdev->fb = exynos_drm_fb_create(dev, NULL, &mode_cmd); | |
155 | if (IS_ERR_OR_NULL(exynos_fbdev->fb)) { | |
156 | DRM_ERROR("failed to create drm framebuffer.\n"); | |
157 | ret = PTR_ERR(exynos_fbdev->fb); | |
158 | goto out; | |
159 | } | |
160 | ||
161 | helper->fb = exynos_fbdev->fb; | |
162 | helper->fbdev = fbi; | |
163 | ||
164 | fbi->par = helper; | |
165 | fbi->flags = FBINFO_FLAG_DEFAULT; | |
166 | fbi->fbops = &exynos_drm_fb_ops; | |
167 | ||
168 | ret = fb_alloc_cmap(&fbi->cmap, 256, 0); | |
169 | if (ret) { | |
170 | DRM_ERROR("failed to allocate cmap.\n"); | |
171 | goto out; | |
172 | } | |
173 | ||
aa6b2b6c | 174 | ret = exynos_drm_fbdev_update(helper, helper->fb); |
19c8b834 ID |
175 | if (ret < 0) |
176 | fb_dealloc_cmap(&fbi->cmap); | |
1c248b7d ID |
177 | |
178 | /* | |
179 | * if failed, all resources allocated above would be released by | |
180 | * drm_mode_config_cleanup() when drm_load() had been called prior | |
181 | * to any specific driver such as fimd or hdmi driver. | |
182 | */ | |
183 | out: | |
184 | mutex_unlock(&dev->struct_mutex); | |
185 | return ret; | |
186 | } | |
187 | ||
188 | static bool | |
189 | exynos_drm_fbdev_is_samefb(struct drm_framebuffer *fb, | |
190 | struct drm_fb_helper_surface_size *sizes) | |
191 | { | |
192 | if (fb->width != sizes->surface_width) | |
193 | return false; | |
194 | if (fb->height != sizes->surface_height) | |
195 | return false; | |
196 | if (fb->bits_per_pixel != sizes->surface_bpp) | |
197 | return false; | |
198 | if (fb->depth != sizes->surface_depth) | |
199 | return false; | |
200 | ||
201 | return true; | |
202 | } | |
203 | ||
204 | static int exynos_drm_fbdev_recreate(struct drm_fb_helper *helper, | |
205 | struct drm_fb_helper_surface_size *sizes) | |
206 | { | |
207 | struct drm_device *dev = helper->dev; | |
208 | struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper); | |
209 | struct drm_framebuffer *fb = exynos_fbdev->fb; | |
a794d57d | 210 | struct drm_mode_fb_cmd2 mode_cmd = { 0 }; |
1c248b7d ID |
211 | |
212 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
213 | ||
214 | if (helper->fb != fb) { | |
215 | DRM_ERROR("drm framebuffer is different\n"); | |
216 | return -EINVAL; | |
217 | } | |
218 | ||
219 | if (exynos_drm_fbdev_is_samefb(fb, sizes)) | |
220 | return 0; | |
221 | ||
222 | mode_cmd.width = sizes->surface_width; | |
223 | mode_cmd.height = sizes->surface_height; | |
a794d57d JS |
224 | mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3); |
225 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, | |
226 | sizes->surface_depth); | |
1c248b7d ID |
227 | |
228 | if (fb->funcs->destroy) | |
229 | fb->funcs->destroy(fb); | |
230 | ||
231 | exynos_fbdev->fb = exynos_drm_fb_create(dev, NULL, &mode_cmd); | |
232 | if (IS_ERR(exynos_fbdev->fb)) { | |
233 | DRM_ERROR("failed to allocate fb.\n"); | |
234 | return PTR_ERR(exynos_fbdev->fb); | |
235 | } | |
236 | ||
237 | helper->fb = exynos_fbdev->fb; | |
aa6b2b6c | 238 | return exynos_drm_fbdev_update(helper, helper->fb); |
1c248b7d ID |
239 | } |
240 | ||
241 | static int exynos_drm_fbdev_probe(struct drm_fb_helper *helper, | |
242 | struct drm_fb_helper_surface_size *sizes) | |
243 | { | |
244 | int ret = 0; | |
245 | ||
246 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
247 | ||
248 | if (!helper->fb) { | |
249 | ret = exynos_drm_fbdev_create(helper, sizes); | |
250 | if (ret < 0) { | |
251 | DRM_ERROR("failed to create fbdev.\n"); | |
252 | return ret; | |
253 | } | |
254 | ||
255 | /* | |
256 | * fb_helper expects a value more than 1 if succeed | |
257 | * because register_framebuffer() should be called. | |
258 | */ | |
259 | ret = 1; | |
260 | } else { | |
261 | ret = exynos_drm_fbdev_recreate(helper, sizes); | |
262 | if (ret < 0) { | |
263 | DRM_ERROR("failed to reconfigure fbdev\n"); | |
264 | return ret; | |
265 | } | |
266 | } | |
267 | ||
268 | return ret; | |
269 | } | |
270 | ||
271 | static struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = { | |
272 | .fb_probe = exynos_drm_fbdev_probe, | |
273 | }; | |
274 | ||
275 | int exynos_drm_fbdev_init(struct drm_device *dev) | |
276 | { | |
277 | struct exynos_drm_fbdev *fbdev; | |
278 | struct exynos_drm_private *private = dev->dev_private; | |
279 | struct drm_fb_helper *helper; | |
280 | unsigned int num_crtc; | |
281 | int ret; | |
282 | ||
283 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
284 | ||
285 | if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector) | |
286 | return 0; | |
287 | ||
288 | fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL); | |
289 | if (!fbdev) { | |
290 | DRM_ERROR("failed to allocate drm fbdev.\n"); | |
291 | return -ENOMEM; | |
292 | } | |
293 | ||
294 | private->fb_helper = helper = &fbdev->drm_fb_helper; | |
295 | helper->funcs = &exynos_drm_fb_helper_funcs; | |
296 | ||
297 | num_crtc = dev->mode_config.num_crtc; | |
298 | ||
299 | ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR); | |
300 | if (ret < 0) { | |
301 | DRM_ERROR("failed to initialize drm fb helper.\n"); | |
302 | goto err_init; | |
303 | } | |
304 | ||
305 | ret = drm_fb_helper_single_add_all_connectors(helper); | |
306 | if (ret < 0) { | |
307 | DRM_ERROR("failed to register drm_fb_helper_connector.\n"); | |
308 | goto err_setup; | |
309 | ||
310 | } | |
311 | ||
312 | ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP); | |
313 | if (ret < 0) { | |
314 | DRM_ERROR("failed to set up hw configuration.\n"); | |
315 | goto err_setup; | |
316 | } | |
317 | ||
318 | return 0; | |
319 | ||
320 | err_setup: | |
321 | drm_fb_helper_fini(helper); | |
322 | ||
323 | err_init: | |
324 | private->fb_helper = NULL; | |
325 | kfree(fbdev); | |
326 | ||
327 | return ret; | |
328 | } | |
329 | ||
330 | static void exynos_drm_fbdev_destroy(struct drm_device *dev, | |
331 | struct drm_fb_helper *fb_helper) | |
332 | { | |
333 | struct drm_framebuffer *fb; | |
334 | ||
335 | /* release drm framebuffer and real buffer */ | |
336 | if (fb_helper->fb && fb_helper->fb->funcs) { | |
337 | fb = fb_helper->fb; | |
338 | if (fb && fb->funcs->destroy) | |
339 | fb->funcs->destroy(fb); | |
340 | } | |
341 | ||
342 | /* release linux framebuffer */ | |
343 | if (fb_helper->fbdev) { | |
344 | struct fb_info *info; | |
345 | int ret; | |
346 | ||
347 | info = fb_helper->fbdev; | |
348 | ret = unregister_framebuffer(info); | |
349 | if (ret < 0) | |
350 | DRM_DEBUG_KMS("failed unregister_framebuffer()\n"); | |
351 | ||
352 | if (info->cmap.len) | |
353 | fb_dealloc_cmap(&info->cmap); | |
354 | ||
355 | framebuffer_release(info); | |
356 | } | |
357 | ||
358 | drm_fb_helper_fini(fb_helper); | |
359 | } | |
360 | ||
361 | void exynos_drm_fbdev_fini(struct drm_device *dev) | |
362 | { | |
363 | struct exynos_drm_private *private = dev->dev_private; | |
364 | struct exynos_drm_fbdev *fbdev; | |
365 | ||
366 | if (!private || !private->fb_helper) | |
367 | return; | |
368 | ||
369 | fbdev = to_exynos_fbdev(private->fb_helper); | |
370 | ||
371 | exynos_drm_fbdev_destroy(dev, private->fb_helper); | |
372 | kfree(fbdev); | |
373 | private->fb_helper = NULL; | |
374 | } | |
375 | ||
376 | void exynos_drm_fbdev_restore_mode(struct drm_device *dev) | |
377 | { | |
378 | struct exynos_drm_private *private = dev->dev_private; | |
379 | ||
380 | if (!private || !private->fb_helper) | |
381 | return; | |
382 | ||
383 | drm_fb_helper_restore_fbdev_mode(private->fb_helper); | |
384 | } | |
385 | ||
386 | int exynos_drm_fbdev_reinit(struct drm_device *dev) | |
387 | { | |
388 | struct exynos_drm_private *private = dev->dev_private; | |
389 | struct drm_fb_helper *fb_helper; | |
390 | int ret; | |
391 | ||
392 | if (!private) | |
393 | return -EINVAL; | |
394 | ||
f6b98252 ID |
395 | /* |
396 | * if all sub drivers were unloaded then num_connector is 0 | |
397 | * so at this time, the framebuffers also should be destroyed. | |
398 | */ | |
1c248b7d ID |
399 | if (!dev->mode_config.num_connector) { |
400 | exynos_drm_fbdev_fini(dev); | |
401 | return 0; | |
402 | } | |
403 | ||
404 | fb_helper = private->fb_helper; | |
405 | ||
406 | if (fb_helper) { | |
84b46990 JS |
407 | struct list_head temp_list; |
408 | ||
409 | INIT_LIST_HEAD(&temp_list); | |
410 | ||
411 | /* | |
412 | * fb_helper is reintialized but kernel fb is reused | |
413 | * so kernel_fb_list need to be backuped and restored | |
414 | */ | |
415 | if (!list_empty(&fb_helper->kernel_fb_list)) | |
416 | list_replace_init(&fb_helper->kernel_fb_list, | |
417 | &temp_list); | |
418 | ||
1c248b7d ID |
419 | drm_fb_helper_fini(fb_helper); |
420 | ||
421 | ret = drm_fb_helper_init(dev, fb_helper, | |
422 | dev->mode_config.num_crtc, MAX_CONNECTOR); | |
423 | if (ret < 0) { | |
424 | DRM_ERROR("failed to initialize drm fb helper\n"); | |
425 | return ret; | |
426 | } | |
427 | ||
84b46990 JS |
428 | if (!list_empty(&temp_list)) |
429 | list_replace(&temp_list, &fb_helper->kernel_fb_list); | |
430 | ||
1c248b7d ID |
431 | ret = drm_fb_helper_single_add_all_connectors(fb_helper); |
432 | if (ret < 0) { | |
433 | DRM_ERROR("failed to add fb helper to connectors\n"); | |
434 | goto err; | |
435 | } | |
436 | ||
437 | ret = drm_fb_helper_initial_config(fb_helper, PREFERRED_BPP); | |
438 | if (ret < 0) { | |
439 | DRM_ERROR("failed to set up hw configuration.\n"); | |
440 | goto err; | |
441 | } | |
442 | } else { | |
443 | /* | |
444 | * if drm_load() failed whem drm load() was called prior | |
445 | * to specific drivers, fb_helper must be NULL and so | |
446 | * this fuction should be called again to re-initialize and | |
447 | * re-configure the fb helper. it means that this function | |
448 | * has been called by the specific drivers. | |
449 | */ | |
f6b98252 | 450 | ret = exynos_drm_fbdev_init(dev); |
1c248b7d ID |
451 | } |
452 | ||
601b44e3 ID |
453 | return ret; |
454 | ||
1c248b7d ID |
455 | err: |
456 | /* | |
457 | * if drm_load() failed when drm load() was called prior | |
458 | * to specific drivers, the fb_helper must be NULL and so check it. | |
459 | */ | |
460 | if (fb_helper) | |
461 | drm_fb_helper_fini(fb_helper); | |
462 | ||
463 | return ret; | |
464 | } | |
465 | ||
466 | MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>"); | |
467 | MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>"); | |
468 | MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>"); | |
469 | MODULE_DESCRIPTION("Samsung SoC DRM FBDEV Driver"); | |
470 | MODULE_LICENSE("GPL"); |