1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2016 Noralf Trønnes
4  */
5 
6 #include <linux/module.h>
7 #include <linux/slab.h>
8 
9 #include <drm/drm_atomic.h>
10 #include <drm/drm_atomic_helper.h>
11 #include <drm/drm_bridge.h>
12 #include <drm/drm_managed.h>
13 #include <drm/drm_plane_helper.h>
14 #include <drm/drm_probe_helper.h>
15 #include <drm/drm_simple_kms_helper.h>
16 
17 /**
18  * DOC: overview
19  *
20  * This helper library provides helpers for drivers for simple display
21  * hardware.
22  *
23  * drm_simple_display_pipe_init() initializes a simple display pipeline
24  * which has only one full-screen scanout buffer feeding one output. The
25  * pipeline is represented by &struct drm_simple_display_pipe and binds
26  * together &drm_plane, &drm_crtc and &drm_encoder structures into one fixed
27  * entity. Some flexibility for code reuse is provided through a separately
28  * allocated &drm_connector object and supporting optional &drm_bridge
29  * encoder drivers.
30  *
31  * Many drivers require only a very simple encoder that fulfills the minimum
32  * requirements of the display pipeline and does not add additional
33  * functionality. The function drm_simple_encoder_init() provides an
34  * implementation of such an encoder.
35  */
36 
37 static const struct drm_encoder_funcs drm_simple_encoder_funcs_cleanup = {
38 	.destroy = drm_encoder_cleanup,
39 };
40 
41 /**
42  * drm_simple_encoder_init - Initialize a preallocated encoder with
43  *                           basic functionality.
44  * @dev: drm device
45  * @encoder: the encoder to initialize
46  * @encoder_type: user visible type of the encoder
47  *
48  * Initialises a preallocated encoder that has no further functionality.
49  * Settings for possible CRTC and clones are left to their initial values.
50  * The encoder will be cleaned up automatically as part of the mode-setting
51  * cleanup.
52  *
53  * The caller of drm_simple_encoder_init() is responsible for freeing
54  * the encoder's memory after the encoder has been cleaned up. At the
55  * moment this only works reliably if the encoder data structure is
56  * stored in the device structure. Free the encoder's memory as part of
57  * the device release function.
58  *
59  * Note: consider using drmm_simple_encoder_alloc() instead of
60  * drm_simple_encoder_init() to let the DRM managed resource infrastructure
61  * take care of cleanup and deallocation.
62  *
63  * Returns:
64  * Zero on success, error code on failure.
65  */
66 int drm_simple_encoder_init(struct drm_device *dev,
67 			    struct drm_encoder *encoder,
68 			    int encoder_type)
69 {
70 	return drm_encoder_init(dev, encoder,
71 				&drm_simple_encoder_funcs_cleanup,
72 				encoder_type, NULL);
73 }
74 EXPORT_SYMBOL(drm_simple_encoder_init);
75 
76 void *__drmm_simple_encoder_alloc(struct drm_device *dev, size_t size,
77 				  size_t offset, int encoder_type)
78 {
79 	return __drmm_encoder_alloc(dev, size, offset, NULL, encoder_type,
80 				    NULL);
81 }
82 EXPORT_SYMBOL(__drmm_simple_encoder_alloc);
83 
84 static enum drm_mode_status
85 drm_simple_kms_crtc_mode_valid(struct drm_crtc *crtc,
86 			       const struct drm_display_mode *mode)
87 {
88 	struct drm_simple_display_pipe *pipe;
89 
90 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
91 	if (!pipe->funcs || !pipe->funcs->mode_valid)
92 		/* Anything goes */
93 		return MODE_OK;
94 
95 	return pipe->funcs->mode_valid(pipe, mode);
96 }
97 
98 static int drm_simple_kms_crtc_check(struct drm_crtc *crtc,
99 				     struct drm_atomic_state *state)
100 {
101 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
102 									  crtc);
103 	bool has_primary = crtc_state->plane_mask &
104 			   drm_plane_mask(crtc->primary);
105 
106 	/* We always want to have an active plane with an active CRTC */
107 	if (has_primary != crtc_state->enable)
108 		return -EINVAL;
109 
110 	return drm_atomic_add_affected_planes(state, crtc);
111 }
112 
113 static void drm_simple_kms_crtc_enable(struct drm_crtc *crtc,
114 				       struct drm_atomic_state *state)
115 {
116 	struct drm_plane *plane;
117 	struct drm_simple_display_pipe *pipe;
118 
119 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
120 	if (!pipe->funcs || !pipe->funcs->enable)
121 		return;
122 
123 	plane = &pipe->plane;
124 	pipe->funcs->enable(pipe, crtc->state, plane->state);
125 }
126 
127 static void drm_simple_kms_crtc_disable(struct drm_crtc *crtc,
128 					struct drm_atomic_state *state)
129 {
130 	struct drm_simple_display_pipe *pipe;
131 
132 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
133 	if (!pipe->funcs || !pipe->funcs->disable)
134 		return;
135 
136 	pipe->funcs->disable(pipe);
137 }
138 
139 static const struct drm_crtc_helper_funcs drm_simple_kms_crtc_helper_funcs = {
140 	.mode_valid = drm_simple_kms_crtc_mode_valid,
141 	.atomic_check = drm_simple_kms_crtc_check,
142 	.atomic_enable = drm_simple_kms_crtc_enable,
143 	.atomic_disable = drm_simple_kms_crtc_disable,
144 };
145 
146 static int drm_simple_kms_crtc_enable_vblank(struct drm_crtc *crtc)
147 {
148 	struct drm_simple_display_pipe *pipe;
149 
150 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
151 	if (!pipe->funcs || !pipe->funcs->enable_vblank)
152 		return 0;
153 
154 	return pipe->funcs->enable_vblank(pipe);
155 }
156 
157 static void drm_simple_kms_crtc_disable_vblank(struct drm_crtc *crtc)
158 {
159 	struct drm_simple_display_pipe *pipe;
160 
161 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
162 	if (!pipe->funcs || !pipe->funcs->disable_vblank)
163 		return;
164 
165 	pipe->funcs->disable_vblank(pipe);
166 }
167 
168 static const struct drm_crtc_funcs drm_simple_kms_crtc_funcs = {
169 	.reset = drm_atomic_helper_crtc_reset,
170 	.destroy = drm_crtc_cleanup,
171 	.set_config = drm_atomic_helper_set_config,
172 	.page_flip = drm_atomic_helper_page_flip,
173 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
174 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
175 	.enable_vblank = drm_simple_kms_crtc_enable_vblank,
176 	.disable_vblank = drm_simple_kms_crtc_disable_vblank,
177 };
178 
179 static int drm_simple_kms_plane_atomic_check(struct drm_plane *plane,
180 					struct drm_atomic_state *state)
181 {
182 	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state,
183 									     plane);
184 	struct drm_simple_display_pipe *pipe;
185 	struct drm_crtc_state *crtc_state;
186 	int ret;
187 
188 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
189 	crtc_state = drm_atomic_get_new_crtc_state(state,
190 						   &pipe->crtc);
191 
192 	ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
193 						  DRM_PLANE_HELPER_NO_SCALING,
194 						  DRM_PLANE_HELPER_NO_SCALING,
195 						  false, true);
196 	if (ret)
197 		return ret;
198 
199 	if (!plane_state->visible)
200 		return 0;
201 
202 	if (!pipe->funcs || !pipe->funcs->check)
203 		return 0;
204 
205 	return pipe->funcs->check(pipe, plane_state, crtc_state);
206 }
207 
208 static void drm_simple_kms_plane_atomic_update(struct drm_plane *plane,
209 					struct drm_atomic_state *state)
210 {
211 	struct drm_plane_state *old_pstate = drm_atomic_get_old_plane_state(state,
212 									    plane);
213 	struct drm_simple_display_pipe *pipe;
214 
215 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
216 	if (!pipe->funcs || !pipe->funcs->update)
217 		return;
218 
219 	pipe->funcs->update(pipe, old_pstate);
220 }
221 
222 static int drm_simple_kms_plane_prepare_fb(struct drm_plane *plane,
223 					   struct drm_plane_state *state)
224 {
225 	struct drm_simple_display_pipe *pipe;
226 
227 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
228 	if (!pipe->funcs || !pipe->funcs->prepare_fb)
229 		return 0;
230 
231 	return pipe->funcs->prepare_fb(pipe, state);
232 }
233 
234 static void drm_simple_kms_plane_cleanup_fb(struct drm_plane *plane,
235 					    struct drm_plane_state *state)
236 {
237 	struct drm_simple_display_pipe *pipe;
238 
239 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
240 	if (!pipe->funcs || !pipe->funcs->cleanup_fb)
241 		return;
242 
243 	pipe->funcs->cleanup_fb(pipe, state);
244 }
245 
246 static bool drm_simple_kms_format_mod_supported(struct drm_plane *plane,
247 						uint32_t format,
248 						uint64_t modifier)
249 {
250 	return modifier == DRM_FORMAT_MOD_LINEAR;
251 }
252 
253 static const struct drm_plane_helper_funcs drm_simple_kms_plane_helper_funcs = {
254 	.prepare_fb = drm_simple_kms_plane_prepare_fb,
255 	.cleanup_fb = drm_simple_kms_plane_cleanup_fb,
256 	.atomic_check = drm_simple_kms_plane_atomic_check,
257 	.atomic_update = drm_simple_kms_plane_atomic_update,
258 };
259 
260 static void drm_simple_kms_plane_reset(struct drm_plane *plane)
261 {
262 	struct drm_simple_display_pipe *pipe;
263 
264 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
265 	if (!pipe->funcs || !pipe->funcs->reset_plane)
266 		return drm_atomic_helper_plane_reset(plane);
267 
268 	return pipe->funcs->reset_plane(pipe);
269 }
270 
271 static struct drm_plane_state *drm_simple_kms_plane_duplicate_state(struct drm_plane *plane)
272 {
273 	struct drm_simple_display_pipe *pipe;
274 
275 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
276 	if (!pipe->funcs || !pipe->funcs->duplicate_plane_state)
277 		return drm_atomic_helper_plane_duplicate_state(plane);
278 
279 	return pipe->funcs->duplicate_plane_state(pipe);
280 }
281 
282 static void drm_simple_kms_plane_destroy_state(struct drm_plane *plane,
283 					       struct drm_plane_state *state)
284 {
285 	struct drm_simple_display_pipe *pipe;
286 
287 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
288 	if (!pipe->funcs || !pipe->funcs->destroy_plane_state)
289 		drm_atomic_helper_plane_destroy_state(plane, state);
290 	else
291 		pipe->funcs->destroy_plane_state(pipe, state);
292 }
293 
294 static const struct drm_plane_funcs drm_simple_kms_plane_funcs = {
295 	.update_plane		= drm_atomic_helper_update_plane,
296 	.disable_plane		= drm_atomic_helper_disable_plane,
297 	.destroy		= drm_plane_cleanup,
298 	.reset			= drm_simple_kms_plane_reset,
299 	.atomic_duplicate_state	= drm_simple_kms_plane_duplicate_state,
300 	.atomic_destroy_state	= drm_simple_kms_plane_destroy_state,
301 	.format_mod_supported   = drm_simple_kms_format_mod_supported,
302 };
303 
304 /**
305  * drm_simple_display_pipe_attach_bridge - Attach a bridge to the display pipe
306  * @pipe: simple display pipe object
307  * @bridge: bridge to attach
308  *
309  * Makes it possible to still use the drm_simple_display_pipe helpers when
310  * a DRM bridge has to be used.
311  *
312  * Note that you probably want to initialize the pipe by passing a NULL
313  * connector to drm_simple_display_pipe_init().
314  *
315  * Returns:
316  * Zero on success, negative error code on failure.
317  */
318 int drm_simple_display_pipe_attach_bridge(struct drm_simple_display_pipe *pipe,
319 					  struct drm_bridge *bridge)
320 {
321 	return drm_bridge_attach(&pipe->encoder, bridge, NULL, 0);
322 }
323 EXPORT_SYMBOL(drm_simple_display_pipe_attach_bridge);
324 
325 /**
326  * drm_simple_display_pipe_init - Initialize a simple display pipeline
327  * @dev: DRM device
328  * @pipe: simple display pipe object to initialize
329  * @funcs: callbacks for the display pipe (optional)
330  * @formats: array of supported formats (DRM_FORMAT\_\*)
331  * @format_count: number of elements in @formats
332  * @format_modifiers: array of formats modifiers
333  * @connector: connector to attach and register (optional)
334  *
335  * Sets up a display pipeline which consist of a really simple
336  * plane-crtc-encoder pipe.
337  *
338  * If a connector is supplied, the pipe will be coupled with the provided
339  * connector. You may supply a NULL connector when using drm bridges, that
340  * handle connectors themselves (see drm_simple_display_pipe_attach_bridge()).
341  *
342  * Teardown of a simple display pipe is all handled automatically by the drm
343  * core through calling drm_mode_config_cleanup(). Drivers afterwards need to
344  * release the memory for the structure themselves.
345  *
346  * Returns:
347  * Zero on success, negative error code on failure.
348  */
349 int drm_simple_display_pipe_init(struct drm_device *dev,
350 			struct drm_simple_display_pipe *pipe,
351 			const struct drm_simple_display_pipe_funcs *funcs,
352 			const uint32_t *formats, unsigned int format_count,
353 			const uint64_t *format_modifiers,
354 			struct drm_connector *connector)
355 {
356 	struct drm_encoder *encoder = &pipe->encoder;
357 	struct drm_plane *plane = &pipe->plane;
358 	struct drm_crtc *crtc = &pipe->crtc;
359 	int ret;
360 
361 	pipe->connector = connector;
362 	pipe->funcs = funcs;
363 
364 	drm_plane_helper_add(plane, &drm_simple_kms_plane_helper_funcs);
365 	ret = drm_universal_plane_init(dev, plane, 0,
366 				       &drm_simple_kms_plane_funcs,
367 				       formats, format_count,
368 				       format_modifiers,
369 				       DRM_PLANE_TYPE_PRIMARY, NULL);
370 	if (ret)
371 		return ret;
372 
373 	drm_crtc_helper_add(crtc, &drm_simple_kms_crtc_helper_funcs);
374 	ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
375 					&drm_simple_kms_crtc_funcs, NULL);
376 	if (ret)
377 		return ret;
378 
379 	encoder->possible_crtcs = drm_crtc_mask(crtc);
380 	ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_NONE);
381 	if (ret || !connector)
382 		return ret;
383 
384 	return drm_connector_attach_encoder(connector, encoder);
385 }
386 EXPORT_SYMBOL(drm_simple_display_pipe_init);
387 
388 MODULE_LICENSE("GPL");
389