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