xref: /linux/drivers/gpu/drm/exynos/exynos_drm_fbdev.c (revision 74ba587f402d5501af2c85e50cf1e4044263b6ca)
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 
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 
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 
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;
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 = drm_fb_helper_alloc_info(helper);
67 	if (IS_ERR(fbi)) {
68 		DRM_DEV_ERROR(to_dma_dev(helper->dev),
69 			      "failed to allocate fb info.\n");
70 		return PTR_ERR(fbi);
71 	}
72 
73 	fbi->fbops = &exynos_drm_fb_ops;
74 
75 	drm_fb_helper_fill_info(fbi, helper, sizes);
76 
77 	offset = fbi->var.xoffset * fb->format->cpp[0];
78 	offset += fbi->var.yoffset * fb->pitches[0];
79 
80 	fbi->flags |= FBINFO_VIRTFB;
81 	fbi->screen_buffer = exynos_gem->kvaddr + offset;
82 	fbi->screen_size = size;
83 	fbi->fix.smem_len = size;
84 
85 	return 0;
86 }
87 
88 static const struct drm_fb_helper_funcs exynos_drm_fbdev_helper_funcs = {
89 };
90 
91 int exynos_drm_fbdev_driver_fbdev_probe(struct drm_fb_helper *helper,
92 					struct drm_fb_helper_surface_size *sizes)
93 {
94 	struct exynos_drm_gem *exynos_gem;
95 	struct drm_device *dev = helper->dev;
96 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
97 	unsigned long size;
98 	int ret;
99 
100 	DRM_DEV_DEBUG_KMS(dev->dev,
101 			  "surface width(%d), height(%d) and bpp(%d\n",
102 			  sizes->surface_width, sizes->surface_height,
103 			  sizes->surface_bpp);
104 
105 	mode_cmd.width = sizes->surface_width;
106 	mode_cmd.height = sizes->surface_height;
107 	mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
108 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
109 							  sizes->surface_depth);
110 
111 	size = mode_cmd.pitches[0] * mode_cmd.height;
112 
113 	exynos_gem = exynos_drm_gem_create(dev, EXYNOS_BO_WC, size, true);
114 	if (IS_ERR(exynos_gem))
115 		return PTR_ERR(exynos_gem);
116 
117 	helper->fb =
118 		exynos_drm_framebuffer_init(dev,
119 					    drm_get_format_info(dev, mode_cmd.pixel_format,
120 								mode_cmd.modifier[0]),
121 					    &mode_cmd, &exynos_gem, 1);
122 	if (IS_ERR(helper->fb)) {
123 		DRM_DEV_ERROR(dev->dev, "failed to create drm framebuffer.\n");
124 		ret = PTR_ERR(helper->fb);
125 		goto err_destroy_gem;
126 	}
127 	helper->funcs = &exynos_drm_fbdev_helper_funcs;
128 
129 	ret = exynos_drm_fbdev_update(helper, sizes, exynos_gem);
130 	if (ret < 0)
131 		goto err_destroy_framebuffer;
132 
133 	return 0;
134 
135 err_destroy_framebuffer:
136 	drm_framebuffer_cleanup(helper->fb);
137 	helper->fb = NULL;
138 err_destroy_gem:
139 	exynos_drm_gem_destroy(exynos_gem);
140 	return ret;
141 }
142