Merge branch 'drm-fixes' of git://people.freedesktop.org/~airlied/linux
[firefly-linux-kernel-4.4.55.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_gem.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 exynos_drm_gem_obj       *exynos_gem_obj;
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_gem_buf *buffer;
93         unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3);
94         unsigned long offset;
95
96         DRM_DEBUG_KMS("%s\n", __FILE__);
97
98         drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
99         drm_fb_helper_fill_var(fbi, helper, fb->width, fb->height);
100
101         /* RGB formats use only one buffer */
102         buffer = exynos_drm_fb_buffer(fb, 0);
103         if (!buffer) {
104                 DRM_LOG_KMS("buffer is null.\n");
105                 return -EFAULT;
106         }
107
108         offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3);
109         offset += fbi->var.yoffset * fb->pitches[0];
110
111         dev->mode_config.fb_base = (resource_size_t)buffer->dma_addr;
112         fbi->screen_base = buffer->kvaddr + offset;
113         fbi->fix.smem_start = (unsigned long)(buffer->dma_addr + offset);
114         fbi->screen_size = size;
115         fbi->fix.smem_len = size;
116
117         return 0;
118 }
119
120 static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
121                                     struct drm_fb_helper_surface_size *sizes)
122 {
123         struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
124         struct exynos_drm_gem_obj *exynos_gem_obj;
125         struct drm_device *dev = helper->dev;
126         struct fb_info *fbi;
127         struct drm_mode_fb_cmd2 mode_cmd = { 0 };
128         struct platform_device *pdev = dev->platformdev;
129         unsigned long size;
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.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
141         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
142                                                           sizes->surface_depth);
143
144         mutex_lock(&dev->struct_mutex);
145
146         fbi = framebuffer_alloc(0, &pdev->dev);
147         if (!fbi) {
148                 DRM_ERROR("failed to allocate fb info.\n");
149                 ret = -ENOMEM;
150                 goto out;
151         }
152
153         size = mode_cmd.pitches[0] * mode_cmd.height;
154         exynos_gem_obj = exynos_drm_gem_create(dev, size);
155         if (IS_ERR(exynos_gem_obj)) {
156                 ret = PTR_ERR(exynos_gem_obj);
157                 goto out;
158         }
159
160         exynos_fbdev->exynos_gem_obj = exynos_gem_obj;
161
162         helper->fb = exynos_drm_framebuffer_init(dev, &mode_cmd,
163                         &exynos_gem_obj->base);
164         if (IS_ERR_OR_NULL(helper->fb)) {
165                 DRM_ERROR("failed to create drm framebuffer.\n");
166                 ret = PTR_ERR(helper->fb);
167                 goto out;
168         }
169
170         helper->fbdev = fbi;
171
172         fbi->par = helper;
173         fbi->flags = FBINFO_FLAG_DEFAULT;
174         fbi->fbops = &exynos_drm_fb_ops;
175
176         ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
177         if (ret) {
178                 DRM_ERROR("failed to allocate cmap.\n");
179                 goto out;
180         }
181
182         ret = exynos_drm_fbdev_update(helper, helper->fb);
183         if (ret < 0) {
184                 fb_dealloc_cmap(&fbi->cmap);
185                 goto out;
186         }
187
188 /*
189  * if failed, all resources allocated above would be released by
190  * drm_mode_config_cleanup() when drm_load() had been called prior
191  * to any specific driver such as fimd or hdmi driver.
192  */
193 out:
194         mutex_unlock(&dev->struct_mutex);
195         return ret;
196 }
197
198 static int exynos_drm_fbdev_probe(struct drm_fb_helper *helper,
199                                    struct drm_fb_helper_surface_size *sizes)
200 {
201         int ret = 0;
202
203         DRM_DEBUG_KMS("%s\n", __FILE__);
204
205         /*
206          * with !helper->fb, it means that this funcion is called first time
207          * and after that, the helper->fb would be used as clone mode.
208          */
209         if (!helper->fb) {
210                 ret = exynos_drm_fbdev_create(helper, sizes);
211                 if (ret < 0) {
212                         DRM_ERROR("failed to create fbdev.\n");
213                         return ret;
214                 }
215
216                 /*
217                  * fb_helper expects a value more than 1 if succeed
218                  * because register_framebuffer() should be called.
219                  */
220                 ret = 1;
221         }
222
223         return ret;
224 }
225
226 static struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
227         .fb_probe =     exynos_drm_fbdev_probe,
228 };
229
230 int exynos_drm_fbdev_init(struct drm_device *dev)
231 {
232         struct exynos_drm_fbdev *fbdev;
233         struct exynos_drm_private *private = dev->dev_private;
234         struct drm_fb_helper *helper;
235         unsigned int num_crtc;
236         int ret;
237
238         DRM_DEBUG_KMS("%s\n", __FILE__);
239
240         if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
241                 return 0;
242
243         fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
244         if (!fbdev) {
245                 DRM_ERROR("failed to allocate drm fbdev.\n");
246                 return -ENOMEM;
247         }
248
249         private->fb_helper = helper = &fbdev->drm_fb_helper;
250         helper->funcs = &exynos_drm_fb_helper_funcs;
251
252         num_crtc = dev->mode_config.num_crtc;
253
254         ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
255         if (ret < 0) {
256                 DRM_ERROR("failed to initialize drm fb helper.\n");
257                 goto err_init;
258         }
259
260         ret = drm_fb_helper_single_add_all_connectors(helper);
261         if (ret < 0) {
262                 DRM_ERROR("failed to register drm_fb_helper_connector.\n");
263                 goto err_setup;
264
265         }
266
267         ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
268         if (ret < 0) {
269                 DRM_ERROR("failed to set up hw configuration.\n");
270                 goto err_setup;
271         }
272
273         return 0;
274
275 err_setup:
276         drm_fb_helper_fini(helper);
277
278 err_init:
279         private->fb_helper = NULL;
280         kfree(fbdev);
281
282         return ret;
283 }
284
285 static void exynos_drm_fbdev_destroy(struct drm_device *dev,
286                                       struct drm_fb_helper *fb_helper)
287 {
288         struct drm_framebuffer *fb;
289
290         /* release drm framebuffer and real buffer */
291         if (fb_helper->fb && fb_helper->fb->funcs) {
292                 fb = fb_helper->fb;
293                 if (fb && fb->funcs->destroy)
294                         fb->funcs->destroy(fb);
295         }
296
297         /* release linux framebuffer */
298         if (fb_helper->fbdev) {
299                 struct fb_info *info;
300                 int ret;
301
302                 info = fb_helper->fbdev;
303                 ret = unregister_framebuffer(info);
304                 if (ret < 0)
305                         DRM_DEBUG_KMS("failed unregister_framebuffer()\n");
306
307                 if (info->cmap.len)
308                         fb_dealloc_cmap(&info->cmap);
309
310                 framebuffer_release(info);
311         }
312
313         drm_fb_helper_fini(fb_helper);
314 }
315
316 void exynos_drm_fbdev_fini(struct drm_device *dev)
317 {
318         struct exynos_drm_private *private = dev->dev_private;
319         struct exynos_drm_fbdev *fbdev;
320
321         if (!private || !private->fb_helper)
322                 return;
323
324         fbdev = to_exynos_fbdev(private->fb_helper);
325
326         if (fbdev->exynos_gem_obj)
327                 exynos_drm_gem_destroy(fbdev->exynos_gem_obj);
328
329         exynos_drm_fbdev_destroy(dev, private->fb_helper);
330         kfree(fbdev);
331         private->fb_helper = NULL;
332 }
333
334 void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
335 {
336         struct exynos_drm_private *private = dev->dev_private;
337
338         if (!private || !private->fb_helper)
339                 return;
340
341         drm_fb_helper_restore_fbdev_mode(private->fb_helper);
342 }
343
344 int exynos_drm_fbdev_reinit(struct drm_device *dev)
345 {
346         struct exynos_drm_private *private = dev->dev_private;
347         struct drm_fb_helper *fb_helper;
348         int ret;
349
350         if (!private)
351                 return -EINVAL;
352
353         /*
354          * if all sub drivers were unloaded then num_connector is 0
355          * so at this time, the framebuffers also should be destroyed.
356          */
357         if (!dev->mode_config.num_connector) {
358                 exynos_drm_fbdev_fini(dev);
359                 return 0;
360         }
361
362         fb_helper = private->fb_helper;
363
364         if (fb_helper) {
365                 struct list_head temp_list;
366
367                 INIT_LIST_HEAD(&temp_list);
368
369                 /*
370                  * fb_helper is reintialized but kernel fb is reused
371                  * so kernel_fb_list need to be backuped and restored
372                  */
373                 if (!list_empty(&fb_helper->kernel_fb_list))
374                         list_replace_init(&fb_helper->kernel_fb_list,
375                                         &temp_list);
376
377                 drm_fb_helper_fini(fb_helper);
378
379                 ret = drm_fb_helper_init(dev, fb_helper,
380                                 dev->mode_config.num_crtc, MAX_CONNECTOR);
381                 if (ret < 0) {
382                         DRM_ERROR("failed to initialize drm fb helper\n");
383                         return ret;
384                 }
385
386                 if (!list_empty(&temp_list))
387                         list_replace(&temp_list, &fb_helper->kernel_fb_list);
388
389                 ret = drm_fb_helper_single_add_all_connectors(fb_helper);
390                 if (ret < 0) {
391                         DRM_ERROR("failed to add fb helper to connectors\n");
392                         goto err;
393                 }
394
395                 ret = drm_fb_helper_initial_config(fb_helper, PREFERRED_BPP);
396                 if (ret < 0) {
397                         DRM_ERROR("failed to set up hw configuration.\n");
398                         goto err;
399                 }
400         } else {
401                 /*
402                  * if drm_load() failed whem drm load() was called prior
403                  * to specific drivers, fb_helper must be NULL and so
404                  * this fuction should be called again to re-initialize and
405                  * re-configure the fb helper. it means that this function
406                  * has been called by the specific drivers.
407                  */
408                 ret = exynos_drm_fbdev_init(dev);
409         }
410
411         return ret;
412
413 err:
414         /*
415          * if drm_load() failed when drm load() was called prior
416          * to specific drivers, the fb_helper must be NULL and so check it.
417          */
418         if (fb_helper)
419                 drm_fb_helper_fini(fb_helper);
420
421         return ret;
422 }
423
424 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
425 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
426 MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
427 MODULE_DESCRIPTION("Samsung SoC DRM FBDEV Driver");
428 MODULE_LICENSE("GPL");