1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* exynos_drm_fbdev.c
3 *
4 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * Authors:
6 * Inki Dae <inki.dae@samsung.com>
7 * Joonyoung Shim <jy0922.shim@samsung.com>
8 * Seung-Woo Kim <sw0312.kim@samsung.com>
9 */
10
11 #include <linux/fb.h>
12
13 #include <drm/drm_crtc_helper.h>
14 #include <drm/drm_drv.h>
15 #include <drm/drm_fb_helper.h>
16 #include <drm/drm_framebuffer.h>
17 #include <drm/drm_gem_framebuffer_helper.h>
18 #include <drm/drm_prime.h>
19 #include <drm/drm_print.h>
20 #include <drm/exynos_drm.h>
21
22 #include "exynos_drm_drv.h"
23 #include "exynos_drm_fb.h"
24 #include "exynos_drm_fbdev.h"
25
26 #define MAX_CONNECTOR 4
27
exynos_drm_fb_mmap(struct fb_info * info,struct vm_area_struct * vma)28 static int exynos_drm_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
29 {
30 struct drm_fb_helper *helper = info->par;
31 struct drm_gem_object *obj = drm_gem_fb_get_obj(helper->fb, 0);
32
33 return drm_gem_prime_mmap(obj, vma);
34 }
35
exynos_drm_fb_destroy(struct fb_info * info)36 static void exynos_drm_fb_destroy(struct fb_info *info)
37 {
38 struct drm_fb_helper *fb_helper = info->par;
39 struct drm_framebuffer *fb = fb_helper->fb;
40
41 drm_fb_helper_fini(fb_helper);
42
43 drm_framebuffer_remove(fb);
44
45 drm_client_release(&fb_helper->client);
46 }
47
48 static const struct fb_ops exynos_drm_fb_ops = {
49 .owner = THIS_MODULE,
50 __FB_DEFAULT_DMAMEM_OPS_RDWR,
51 DRM_FB_HELPER_DEFAULT_OPS,
52 __FB_DEFAULT_DMAMEM_OPS_DRAW,
53 .fb_mmap = exynos_drm_fb_mmap,
54 .fb_destroy = exynos_drm_fb_destroy,
55 };
56
exynos_drm_fbdev_update(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes,struct exynos_drm_gem * exynos_gem)57 static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
58 struct drm_fb_helper_surface_size *sizes,
59 struct exynos_drm_gem *exynos_gem)
60 {
61 struct fb_info *fbi = helper->info;
62 struct drm_framebuffer *fb = helper->fb;
63 unsigned int size = fb->width * fb->height * fb->format->cpp[0];
64 unsigned long offset;
65
66 fbi->fbops = &exynos_drm_fb_ops;
67
68 drm_fb_helper_fill_info(fbi, helper, sizes);
69
70 offset = fbi->var.xoffset * fb->format->cpp[0];
71 offset += fbi->var.yoffset * fb->pitches[0];
72
73 fbi->flags |= FBINFO_VIRTFB;
74 fbi->screen_buffer = exynos_gem->kvaddr + offset;
75 fbi->screen_size = size;
76 fbi->fix.smem_len = size;
77
78 return 0;
79 }
80
81 static const struct drm_fb_helper_funcs exynos_drm_fbdev_helper_funcs = {
82 };
83
exynos_drm_fbdev_driver_fbdev_probe(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes)84 int exynos_drm_fbdev_driver_fbdev_probe(struct drm_fb_helper *helper,
85 struct drm_fb_helper_surface_size *sizes)
86 {
87 struct exynos_drm_gem *exynos_gem;
88 struct drm_device *dev = helper->dev;
89 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
90 unsigned long size;
91 int ret;
92
93 DRM_DEV_DEBUG_KMS(dev->dev,
94 "surface width(%d), height(%d) and bpp(%d\n",
95 sizes->surface_width, sizes->surface_height,
96 sizes->surface_bpp);
97
98 mode_cmd.width = sizes->surface_width;
99 mode_cmd.height = sizes->surface_height;
100 mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
101 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
102 sizes->surface_depth);
103
104 size = mode_cmd.pitches[0] * mode_cmd.height;
105
106 exynos_gem = exynos_drm_gem_create(dev, EXYNOS_BO_WC, size, true);
107 if (IS_ERR(exynos_gem))
108 return PTR_ERR(exynos_gem);
109
110 helper->fb =
111 exynos_drm_framebuffer_init(dev,
112 drm_get_format_info(dev, mode_cmd.pixel_format,
113 mode_cmd.modifier[0]),
114 &mode_cmd, &exynos_gem, 1);
115 if (IS_ERR(helper->fb)) {
116 DRM_DEV_ERROR(dev->dev, "failed to create drm framebuffer.\n");
117 ret = PTR_ERR(helper->fb);
118 goto err_destroy_gem;
119 }
120 helper->funcs = &exynos_drm_fbdev_helper_funcs;
121
122 ret = exynos_drm_fbdev_update(helper, sizes, exynos_gem);
123 if (ret < 0)
124 goto err_destroy_framebuffer;
125
126 return 0;
127
128 err_destroy_framebuffer:
129 drm_framebuffer_cleanup(helper->fb);
130 helper->fb = NULL;
131 err_destroy_gem:
132 exynos_drm_gem_destroy(exynos_gem);
133 return ret;
134 }
135