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