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/exynos_drm.h>
20 
21 #include "exynos_drm_drv.h"
22 #include "exynos_drm_fb.h"
23 #include "exynos_drm_fbdev.h"
24 
25 #define MAX_CONNECTOR		4
26 #define PREFERRED_BPP		32
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 	drm_fb_helper_unprepare(fb_helper);
47 	kfree(fb_helper);
48 }
49 
50 static const struct fb_ops exynos_drm_fb_ops = {
51 	.owner		= THIS_MODULE,
52 	__FB_DEFAULT_IO_OPS_RDWR,
53 	DRM_FB_HELPER_DEFAULT_OPS,
54 	__FB_DEFAULT_IO_OPS_DRAW,
55 	.fb_mmap        = exynos_drm_fb_mmap,
56 	.fb_destroy	= exynos_drm_fb_destroy,
57 };
58 
59 static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
60 				   struct drm_fb_helper_surface_size *sizes,
61 				   struct exynos_drm_gem *exynos_gem)
62 {
63 	struct fb_info *fbi;
64 	struct drm_framebuffer *fb = helper->fb;
65 	unsigned int size = fb->width * fb->height * fb->format->cpp[0];
66 	unsigned long offset;
67 
68 	fbi = drm_fb_helper_alloc_info(helper);
69 	if (IS_ERR(fbi)) {
70 		DRM_DEV_ERROR(to_dma_dev(helper->dev),
71 			      "failed to allocate fb info.\n");
72 		return PTR_ERR(fbi);
73 	}
74 
75 	fbi->fbops = &exynos_drm_fb_ops;
76 
77 	drm_fb_helper_fill_info(fbi, helper, sizes);
78 
79 	offset = fbi->var.xoffset * fb->format->cpp[0];
80 	offset += fbi->var.yoffset * fb->pitches[0];
81 
82 	fbi->screen_buffer = exynos_gem->kvaddr + offset;
83 	fbi->screen_size = size;
84 	fbi->fix.smem_len = size;
85 
86 	return 0;
87 }
88 
89 static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
90 				    struct drm_fb_helper_surface_size *sizes)
91 {
92 	struct exynos_drm_gem *exynos_gem;
93 	struct drm_device *dev = helper->dev;
94 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
95 	unsigned long size;
96 	int ret;
97 
98 	DRM_DEV_DEBUG_KMS(dev->dev,
99 			  "surface width(%d), height(%d) and bpp(%d\n",
100 			  sizes->surface_width, sizes->surface_height,
101 			  sizes->surface_bpp);
102 
103 	mode_cmd.width = sizes->surface_width;
104 	mode_cmd.height = sizes->surface_height;
105 	mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
106 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
107 							  sizes->surface_depth);
108 
109 	size = mode_cmd.pitches[0] * mode_cmd.height;
110 
111 	exynos_gem = exynos_drm_gem_create(dev, EXYNOS_BO_WC, size, true);
112 	if (IS_ERR(exynos_gem))
113 		return PTR_ERR(exynos_gem);
114 
115 	helper->fb =
116 		exynos_drm_framebuffer_init(dev, &mode_cmd, &exynos_gem, 1);
117 	if (IS_ERR(helper->fb)) {
118 		DRM_DEV_ERROR(dev->dev, "failed to create drm framebuffer.\n");
119 		ret = PTR_ERR(helper->fb);
120 		goto err_destroy_gem;
121 	}
122 
123 	ret = exynos_drm_fbdev_update(helper, sizes, exynos_gem);
124 	if (ret < 0)
125 		goto err_destroy_framebuffer;
126 
127 	return 0;
128 
129 err_destroy_framebuffer:
130 	drm_framebuffer_cleanup(helper->fb);
131 	helper->fb = NULL;
132 err_destroy_gem:
133 	exynos_drm_gem_destroy(exynos_gem);
134 	return ret;
135 }
136 
137 static const struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
138 	.fb_probe =	exynos_drm_fbdev_create,
139 };
140 
141 /*
142  * struct drm_client
143  */
144 
145 static void exynos_drm_fbdev_client_unregister(struct drm_client_dev *client)
146 {
147 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
148 
149 	if (fb_helper->info) {
150 		drm_fb_helper_unregister_info(fb_helper);
151 	} else {
152 		drm_client_release(&fb_helper->client);
153 		drm_fb_helper_unprepare(fb_helper);
154 		kfree(fb_helper);
155 	}
156 }
157 
158 static int exynos_drm_fbdev_client_restore(struct drm_client_dev *client)
159 {
160 	drm_fb_helper_lastclose(client->dev);
161 
162 	return 0;
163 }
164 
165 static int exynos_drm_fbdev_client_hotplug(struct drm_client_dev *client)
166 {
167 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
168 	struct drm_device *dev = client->dev;
169 	int ret;
170 
171 	if (dev->fb_helper)
172 		return drm_fb_helper_hotplug_event(dev->fb_helper);
173 
174 	ret = drm_fb_helper_init(dev, fb_helper);
175 	if (ret)
176 		goto err_drm_err;
177 
178 	if (!drm_drv_uses_atomic_modeset(dev))
179 		drm_helper_disable_unused_functions(dev);
180 
181 	ret = drm_fb_helper_initial_config(fb_helper);
182 	if (ret)
183 		goto err_drm_fb_helper_fini;
184 
185 	return 0;
186 
187 err_drm_fb_helper_fini:
188 	drm_fb_helper_fini(fb_helper);
189 err_drm_err:
190 	drm_err(dev, "Failed to setup fbdev emulation (ret=%d)\n", ret);
191 	return ret;
192 }
193 
194 static const struct drm_client_funcs exynos_drm_fbdev_client_funcs = {
195 	.owner		= THIS_MODULE,
196 	.unregister	= exynos_drm_fbdev_client_unregister,
197 	.restore	= exynos_drm_fbdev_client_restore,
198 	.hotplug	= exynos_drm_fbdev_client_hotplug,
199 };
200 
201 void exynos_drm_fbdev_setup(struct drm_device *dev)
202 {
203 	struct drm_fb_helper *fb_helper;
204 	int ret;
205 
206 	drm_WARN(dev, !dev->registered, "Device has not been registered.\n");
207 	drm_WARN(dev, dev->fb_helper, "fb_helper is already set!\n");
208 
209 	fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
210 	if (!fb_helper)
211 		return;
212 	drm_fb_helper_prepare(dev, fb_helper, PREFERRED_BPP, &exynos_drm_fb_helper_funcs);
213 
214 	ret = drm_client_init(dev, &fb_helper->client, "fbdev", &exynos_drm_fbdev_client_funcs);
215 	if (ret)
216 		goto err_drm_client_init;
217 
218 	ret = exynos_drm_fbdev_client_hotplug(&fb_helper->client);
219 	if (ret)
220 		drm_dbg_kms(dev, "client hotplug ret=%d\n", ret);
221 
222 	drm_client_register(&fb_helper->client);
223 
224 	return;
225 
226 err_drm_client_init:
227 	drm_fb_helper_unprepare(fb_helper);
228 	kfree(fb_helper);
229 }
230