1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Noralf Trønnes
6 #include <linux/module.h>
7 #include <linux/slab.h>
9 #include <drm/drm_atomic.h>
10 #include <drm/drm_atomic_helper.h>
11 #include <drm/drm_bridge.h>
12 #include <drm/drm_drv.h>
13 #include <drm/drm_gem_atomic_helper.h>
14 #include <drm/drm_managed.h>
15 #include <drm/drm_plane_helper.h>
16 #include <drm/drm_probe_helper.h>
17 #include <drm/drm_simple_kms_helper.h>
22 * This helper library provides helpers for drivers for simple display
25 * drm_simple_display_pipe_init() initializes a simple display pipeline
26 * which has only one full-screen scanout buffer feeding one output. The
27 * pipeline is represented by &struct drm_simple_display_pipe and binds
28 * together &drm_plane, &drm_crtc and &drm_encoder structures into one fixed
29 * entity. Some flexibility for code reuse is provided through a separately
30 * allocated &drm_connector object and supporting optional &drm_bridge
33 * Many drivers require only a very simple encoder that fulfills the minimum
34 * requirements of the display pipeline and does not add additional
35 * functionality. The function drm_simple_encoder_init() provides an
36 * implementation of such an encoder.
39 static const struct drm_encoder_funcs drm_simple_encoder_funcs_cleanup = {
40 .destroy = drm_encoder_cleanup,
44 * drm_simple_encoder_init - Initialize a preallocated encoder with
45 * basic functionality.
47 * @encoder: the encoder to initialize
48 * @encoder_type: user visible type of the encoder
50 * Initialises a preallocated encoder that has no further functionality.
51 * Settings for possible CRTC and clones are left to their initial values.
52 * The encoder will be cleaned up automatically as part of the mode-setting
55 * The caller of drm_simple_encoder_init() is responsible for freeing
56 * the encoder's memory after the encoder has been cleaned up. At the
57 * moment this only works reliably if the encoder data structure is
58 * stored in the device structure. Free the encoder's memory as part of
59 * the device release function.
61 * Note: consider using drmm_simple_encoder_alloc() instead of
62 * drm_simple_encoder_init() to let the DRM managed resource infrastructure
63 * take care of cleanup and deallocation.
66 * Zero on success, error code on failure.
68 int drm_simple_encoder_init(struct drm_device *dev,
69 struct drm_encoder *encoder,
72 return drm_encoder_init(dev, encoder,
73 &drm_simple_encoder_funcs_cleanup,
76 EXPORT_SYMBOL(drm_simple_encoder_init);
78 void *__drmm_simple_encoder_alloc(struct drm_device *dev, size_t size,
79 size_t offset, int encoder_type)
81 return __drmm_encoder_alloc(dev, size, offset, NULL, encoder_type,
84 EXPORT_SYMBOL(__drmm_simple_encoder_alloc);
86 static enum drm_mode_status
87 drm_simple_kms_crtc_mode_valid(struct drm_crtc *crtc,
88 const struct drm_display_mode *mode)
90 struct drm_simple_display_pipe *pipe;
92 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
93 if (!pipe->funcs || !pipe->funcs->mode_valid)
97 return pipe->funcs->mode_valid(pipe, mode);
100 static int drm_simple_kms_crtc_check(struct drm_crtc *crtc,
101 struct drm_atomic_state *state)
103 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
105 bool has_primary = crtc_state->plane_mask &
106 drm_plane_mask(crtc->primary);
108 /* We always want to have an active plane with an active CRTC */
109 if (has_primary != crtc_state->enable)
112 return drm_atomic_add_affected_planes(state, crtc);
115 static void drm_simple_kms_crtc_enable(struct drm_crtc *crtc,
116 struct drm_atomic_state *state)
118 struct drm_plane *plane;
119 struct drm_simple_display_pipe *pipe;
121 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
122 if (!pipe->funcs || !pipe->funcs->enable)
125 plane = &pipe->plane;
126 pipe->funcs->enable(pipe, crtc->state, plane->state);
129 static void drm_simple_kms_crtc_disable(struct drm_crtc *crtc,
130 struct drm_atomic_state *state)
132 struct drm_simple_display_pipe *pipe;
134 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
135 if (!pipe->funcs || !pipe->funcs->disable)
138 pipe->funcs->disable(pipe);
141 static const struct drm_crtc_helper_funcs drm_simple_kms_crtc_helper_funcs = {
142 .mode_valid = drm_simple_kms_crtc_mode_valid,
143 .atomic_check = drm_simple_kms_crtc_check,
144 .atomic_enable = drm_simple_kms_crtc_enable,
145 .atomic_disable = drm_simple_kms_crtc_disable,
148 static void drm_simple_kms_crtc_reset(struct drm_crtc *crtc)
150 struct drm_simple_display_pipe *pipe;
152 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
153 if (!pipe->funcs || !pipe->funcs->reset_crtc)
154 return drm_atomic_helper_crtc_reset(crtc);
156 return pipe->funcs->reset_crtc(pipe);
159 static struct drm_crtc_state *drm_simple_kms_crtc_duplicate_state(struct drm_crtc *crtc)
161 struct drm_simple_display_pipe *pipe;
163 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
164 if (!pipe->funcs || !pipe->funcs->duplicate_crtc_state)
165 return drm_atomic_helper_crtc_duplicate_state(crtc);
167 return pipe->funcs->duplicate_crtc_state(pipe);
170 static void drm_simple_kms_crtc_destroy_state(struct drm_crtc *crtc, struct drm_crtc_state *state)
172 struct drm_simple_display_pipe *pipe;
174 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
175 if (!pipe->funcs || !pipe->funcs->destroy_crtc_state)
176 drm_atomic_helper_crtc_destroy_state(crtc, state);
178 pipe->funcs->destroy_crtc_state(pipe, state);
181 static int drm_simple_kms_crtc_enable_vblank(struct drm_crtc *crtc)
183 struct drm_simple_display_pipe *pipe;
185 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
186 if (!pipe->funcs || !pipe->funcs->enable_vblank)
189 return pipe->funcs->enable_vblank(pipe);
192 static void drm_simple_kms_crtc_disable_vblank(struct drm_crtc *crtc)
194 struct drm_simple_display_pipe *pipe;
196 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
197 if (!pipe->funcs || !pipe->funcs->disable_vblank)
200 pipe->funcs->disable_vblank(pipe);
203 static const struct drm_crtc_funcs drm_simple_kms_crtc_funcs = {
204 .reset = drm_simple_kms_crtc_reset,
205 .destroy = drm_crtc_cleanup,
206 .set_config = drm_atomic_helper_set_config,
207 .page_flip = drm_atomic_helper_page_flip,
208 .atomic_duplicate_state = drm_simple_kms_crtc_duplicate_state,
209 .atomic_destroy_state = drm_simple_kms_crtc_destroy_state,
210 .enable_vblank = drm_simple_kms_crtc_enable_vblank,
211 .disable_vblank = drm_simple_kms_crtc_disable_vblank,
214 static int drm_simple_kms_plane_atomic_check(struct drm_plane *plane,
215 struct drm_atomic_state *state)
217 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state,
219 struct drm_simple_display_pipe *pipe;
220 struct drm_crtc_state *crtc_state;
223 pipe = container_of(plane, struct drm_simple_display_pipe, plane);
224 crtc_state = drm_atomic_get_new_crtc_state(state,
227 ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
228 DRM_PLANE_HELPER_NO_SCALING,
229 DRM_PLANE_HELPER_NO_SCALING,
234 if (!plane_state->visible)
237 if (!pipe->funcs || !pipe->funcs->check)
240 return pipe->funcs->check(pipe, plane_state, crtc_state);
243 static void drm_simple_kms_plane_atomic_update(struct drm_plane *plane,
244 struct drm_atomic_state *state)
246 struct drm_plane_state *old_pstate = drm_atomic_get_old_plane_state(state,
248 struct drm_simple_display_pipe *pipe;
250 pipe = container_of(plane, struct drm_simple_display_pipe, plane);
251 if (!pipe->funcs || !pipe->funcs->update)
254 pipe->funcs->update(pipe, old_pstate);
257 static int drm_simple_kms_plane_prepare_fb(struct drm_plane *plane,
258 struct drm_plane_state *state)
260 struct drm_simple_display_pipe *pipe;
262 pipe = container_of(plane, struct drm_simple_display_pipe, plane);
263 if (!pipe->funcs || !pipe->funcs->prepare_fb) {
264 if (WARN_ON_ONCE(!drm_core_check_feature(plane->dev, DRIVER_GEM)))
267 WARN_ON_ONCE(pipe->funcs && pipe->funcs->cleanup_fb);
269 return drm_gem_simple_display_pipe_prepare_fb(pipe, state);
272 return pipe->funcs->prepare_fb(pipe, state);
275 static void drm_simple_kms_plane_cleanup_fb(struct drm_plane *plane,
276 struct drm_plane_state *state)
278 struct drm_simple_display_pipe *pipe;
280 pipe = container_of(plane, struct drm_simple_display_pipe, plane);
281 if (!pipe->funcs || !pipe->funcs->cleanup_fb)
284 pipe->funcs->cleanup_fb(pipe, state);
287 static bool drm_simple_kms_format_mod_supported(struct drm_plane *plane,
291 return modifier == DRM_FORMAT_MOD_LINEAR;
294 static const struct drm_plane_helper_funcs drm_simple_kms_plane_helper_funcs = {
295 .prepare_fb = drm_simple_kms_plane_prepare_fb,
296 .cleanup_fb = drm_simple_kms_plane_cleanup_fb,
297 .atomic_check = drm_simple_kms_plane_atomic_check,
298 .atomic_update = drm_simple_kms_plane_atomic_update,
301 static void drm_simple_kms_plane_reset(struct drm_plane *plane)
303 struct drm_simple_display_pipe *pipe;
305 pipe = container_of(plane, struct drm_simple_display_pipe, plane);
306 if (!pipe->funcs || !pipe->funcs->reset_plane)
307 return drm_atomic_helper_plane_reset(plane);
309 return pipe->funcs->reset_plane(pipe);
312 static struct drm_plane_state *drm_simple_kms_plane_duplicate_state(struct drm_plane *plane)
314 struct drm_simple_display_pipe *pipe;
316 pipe = container_of(plane, struct drm_simple_display_pipe, plane);
317 if (!pipe->funcs || !pipe->funcs->duplicate_plane_state)
318 return drm_atomic_helper_plane_duplicate_state(plane);
320 return pipe->funcs->duplicate_plane_state(pipe);
323 static void drm_simple_kms_plane_destroy_state(struct drm_plane *plane,
324 struct drm_plane_state *state)
326 struct drm_simple_display_pipe *pipe;
328 pipe = container_of(plane, struct drm_simple_display_pipe, plane);
329 if (!pipe->funcs || !pipe->funcs->destroy_plane_state)
330 drm_atomic_helper_plane_destroy_state(plane, state);
332 pipe->funcs->destroy_plane_state(pipe, state);
335 static const struct drm_plane_funcs drm_simple_kms_plane_funcs = {
336 .update_plane = drm_atomic_helper_update_plane,
337 .disable_plane = drm_atomic_helper_disable_plane,
338 .destroy = drm_plane_cleanup,
339 .reset = drm_simple_kms_plane_reset,
340 .atomic_duplicate_state = drm_simple_kms_plane_duplicate_state,
341 .atomic_destroy_state = drm_simple_kms_plane_destroy_state,
342 .format_mod_supported = drm_simple_kms_format_mod_supported,
346 * drm_simple_display_pipe_attach_bridge - Attach a bridge to the display pipe
347 * @pipe: simple display pipe object
348 * @bridge: bridge to attach
350 * Makes it possible to still use the drm_simple_display_pipe helpers when
351 * a DRM bridge has to be used.
353 * Note that you probably want to initialize the pipe by passing a NULL
354 * connector to drm_simple_display_pipe_init().
357 * Zero on success, negative error code on failure.
359 int drm_simple_display_pipe_attach_bridge(struct drm_simple_display_pipe *pipe,
360 struct drm_bridge *bridge)
362 return drm_bridge_attach(&pipe->encoder, bridge, NULL, 0);
364 EXPORT_SYMBOL(drm_simple_display_pipe_attach_bridge);
367 * drm_simple_display_pipe_init - Initialize a simple display pipeline
369 * @pipe: simple display pipe object to initialize
370 * @funcs: callbacks for the display pipe (optional)
371 * @formats: array of supported formats (DRM_FORMAT\_\*)
372 * @format_count: number of elements in @formats
373 * @format_modifiers: array of formats modifiers
374 * @connector: connector to attach and register (optional)
376 * Sets up a display pipeline which consist of a really simple
377 * plane-crtc-encoder pipe.
379 * If a connector is supplied, the pipe will be coupled with the provided
380 * connector. You may supply a NULL connector when using drm bridges, that
381 * handle connectors themselves (see drm_simple_display_pipe_attach_bridge()).
383 * Teardown of a simple display pipe is all handled automatically by the drm
384 * core through calling drm_mode_config_cleanup(). Drivers afterwards need to
385 * release the memory for the structure themselves.
388 * Zero on success, negative error code on failure.
390 int drm_simple_display_pipe_init(struct drm_device *dev,
391 struct drm_simple_display_pipe *pipe,
392 const struct drm_simple_display_pipe_funcs *funcs,
393 const uint32_t *formats, unsigned int format_count,
394 const uint64_t *format_modifiers,
395 struct drm_connector *connector)
397 struct drm_encoder *encoder = &pipe->encoder;
398 struct drm_plane *plane = &pipe->plane;
399 struct drm_crtc *crtc = &pipe->crtc;
402 pipe->connector = connector;
405 drm_plane_helper_add(plane, &drm_simple_kms_plane_helper_funcs);
406 ret = drm_universal_plane_init(dev, plane, 0,
407 &drm_simple_kms_plane_funcs,
408 formats, format_count,
410 DRM_PLANE_TYPE_PRIMARY, NULL);
414 drm_crtc_helper_add(crtc, &drm_simple_kms_crtc_helper_funcs);
415 ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
416 &drm_simple_kms_crtc_funcs, NULL);
420 encoder->possible_crtcs = drm_crtc_mask(crtc);
421 ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_NONE);
422 if (ret || !connector)
425 return drm_connector_attach_encoder(connector, encoder);
427 EXPORT_SYMBOL(drm_simple_display_pipe_init);
429 MODULE_LICENSE("GPL");