xref: /linux/drivers/gpu/drm/drm_simple_kms_helper.c (revision 2874c5fd284268364ece81a7bd936f3c8168e567)
1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
25b809074SNoralf Trønnes /*
35b809074SNoralf Trønnes  * Copyright (C) 2016 Noralf Trønnes
45b809074SNoralf Trønnes  */
55b809074SNoralf Trønnes 
65b809074SNoralf Trønnes #include <drm/drmP.h>
75b809074SNoralf Trønnes #include <drm/drm_atomic.h>
85b809074SNoralf Trønnes #include <drm/drm_atomic_helper.h>
95b809074SNoralf Trønnes #include <drm/drm_plane_helper.h>
10fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
115b809074SNoralf Trønnes #include <drm/drm_simple_kms_helper.h>
125b809074SNoralf Trønnes #include <linux/slab.h>
135b809074SNoralf Trønnes 
145b809074SNoralf Trønnes /**
155b809074SNoralf Trønnes  * DOC: overview
165b809074SNoralf Trønnes  *
175b809074SNoralf Trønnes  * This helper library provides helpers for drivers for simple display
185b809074SNoralf Trønnes  * hardware.
195b809074SNoralf Trønnes  *
205b809074SNoralf Trønnes  * drm_simple_display_pipe_init() initializes a simple display pipeline
215b809074SNoralf Trønnes  * which has only one full-screen scanout buffer feeding one output. The
22ea0dd85aSDaniel Vetter  * pipeline is represented by &struct drm_simple_display_pipe and binds
235b809074SNoralf Trønnes  * together &drm_plane, &drm_crtc and &drm_encoder structures into one fixed
245b809074SNoralf Trønnes  * entity. Some flexibility for code reuse is provided through a separately
255b809074SNoralf Trønnes  * allocated &drm_connector object and supporting optional &drm_bridge
265b809074SNoralf Trønnes  * encoder drivers.
275b809074SNoralf Trønnes  */
285b809074SNoralf Trønnes 
295b809074SNoralf Trønnes static const struct drm_encoder_funcs drm_simple_kms_encoder_funcs = {
305b809074SNoralf Trønnes 	.destroy = drm_encoder_cleanup,
315b809074SNoralf Trønnes };
325b809074SNoralf Trønnes 
3340275dc4SLinus Walleij static enum drm_mode_status
3440275dc4SLinus Walleij drm_simple_kms_crtc_mode_valid(struct drm_crtc *crtc,
3540275dc4SLinus Walleij 			       const struct drm_display_mode *mode)
3640275dc4SLinus Walleij {
3740275dc4SLinus Walleij 	struct drm_simple_display_pipe *pipe;
3840275dc4SLinus Walleij 
3940275dc4SLinus Walleij 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
4040275dc4SLinus Walleij 	if (!pipe->funcs || !pipe->funcs->mode_valid)
4140275dc4SLinus Walleij 		/* Anything goes */
4240275dc4SLinus Walleij 		return MODE_OK;
4340275dc4SLinus Walleij 
4440275dc4SLinus Walleij 	return pipe->funcs->mode_valid(crtc, mode);
4540275dc4SLinus Walleij }
4640275dc4SLinus Walleij 
476dcf0de7SDaniel Vetter static int drm_simple_kms_crtc_check(struct drm_crtc *crtc,
486dcf0de7SDaniel Vetter 				     struct drm_crtc_state *state)
496dcf0de7SDaniel Vetter {
50765831dcSMaarten Lankhorst 	bool has_primary = state->plane_mask &
5162f77ad0SVille Syrjälä 			   drm_plane_mask(crtc->primary);
52765831dcSMaarten Lankhorst 
53765831dcSMaarten Lankhorst 	/* We always want to have an active plane with an active CRTC */
54765831dcSMaarten Lankhorst 	if (has_primary != state->enable)
55765831dcSMaarten Lankhorst 		return -EINVAL;
56765831dcSMaarten Lankhorst 
576dcf0de7SDaniel Vetter 	return drm_atomic_add_affected_planes(state->state, crtc);
586dcf0de7SDaniel Vetter }
596dcf0de7SDaniel Vetter 
600b20a0f8SLaurent Pinchart static void drm_simple_kms_crtc_enable(struct drm_crtc *crtc,
610b20a0f8SLaurent Pinchart 				       struct drm_crtc_state *old_state)
625b809074SNoralf Trønnes {
630c9c7fd0SVille Syrjälä 	struct drm_plane *plane;
645b809074SNoralf Trønnes 	struct drm_simple_display_pipe *pipe;
655b809074SNoralf Trønnes 
665b809074SNoralf Trønnes 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
675b809074SNoralf Trønnes 	if (!pipe->funcs || !pipe->funcs->enable)
685b809074SNoralf Trønnes 		return;
695b809074SNoralf Trønnes 
700c9c7fd0SVille Syrjälä 	plane = &pipe->plane;
710c9c7fd0SVille Syrjälä 	pipe->funcs->enable(pipe, crtc->state, plane->state);
725b809074SNoralf Trønnes }
735b809074SNoralf Trønnes 
7464581714SLaurent Pinchart static void drm_simple_kms_crtc_disable(struct drm_crtc *crtc,
7564581714SLaurent Pinchart 					struct drm_crtc_state *old_state)
765b809074SNoralf Trønnes {
775b809074SNoralf Trønnes 	struct drm_simple_display_pipe *pipe;
785b809074SNoralf Trønnes 
795b809074SNoralf Trønnes 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
805b809074SNoralf Trønnes 	if (!pipe->funcs || !pipe->funcs->disable)
815b809074SNoralf Trønnes 		return;
825b809074SNoralf Trønnes 
835b809074SNoralf Trønnes 	pipe->funcs->disable(pipe);
845b809074SNoralf Trønnes }
855b809074SNoralf Trønnes 
865b809074SNoralf Trønnes static const struct drm_crtc_helper_funcs drm_simple_kms_crtc_helper_funcs = {
8740275dc4SLinus Walleij 	.mode_valid = drm_simple_kms_crtc_mode_valid,
886dcf0de7SDaniel Vetter 	.atomic_check = drm_simple_kms_crtc_check,
890b20a0f8SLaurent Pinchart 	.atomic_enable = drm_simple_kms_crtc_enable,
9064581714SLaurent Pinchart 	.atomic_disable = drm_simple_kms_crtc_disable,
915b809074SNoralf Trønnes };
925b809074SNoralf Trønnes 
93ac86cba9SOleksandr Andrushchenko static int drm_simple_kms_crtc_enable_vblank(struct drm_crtc *crtc)
94ac86cba9SOleksandr Andrushchenko {
95ac86cba9SOleksandr Andrushchenko 	struct drm_simple_display_pipe *pipe;
96ac86cba9SOleksandr Andrushchenko 
97ac86cba9SOleksandr Andrushchenko 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
98ac86cba9SOleksandr Andrushchenko 	if (!pipe->funcs || !pipe->funcs->enable_vblank)
99ac86cba9SOleksandr Andrushchenko 		return 0;
100ac86cba9SOleksandr Andrushchenko 
101ac86cba9SOleksandr Andrushchenko 	return pipe->funcs->enable_vblank(pipe);
102ac86cba9SOleksandr Andrushchenko }
103ac86cba9SOleksandr Andrushchenko 
104ac86cba9SOleksandr Andrushchenko static void drm_simple_kms_crtc_disable_vblank(struct drm_crtc *crtc)
105ac86cba9SOleksandr Andrushchenko {
106ac86cba9SOleksandr Andrushchenko 	struct drm_simple_display_pipe *pipe;
107ac86cba9SOleksandr Andrushchenko 
108ac86cba9SOleksandr Andrushchenko 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
109ac86cba9SOleksandr Andrushchenko 	if (!pipe->funcs || !pipe->funcs->disable_vblank)
110ac86cba9SOleksandr Andrushchenko 		return;
111ac86cba9SOleksandr Andrushchenko 
112ac86cba9SOleksandr Andrushchenko 	pipe->funcs->disable_vblank(pipe);
113ac86cba9SOleksandr Andrushchenko }
114ac86cba9SOleksandr Andrushchenko 
1155b809074SNoralf Trønnes static const struct drm_crtc_funcs drm_simple_kms_crtc_funcs = {
1165b809074SNoralf Trønnes 	.reset = drm_atomic_helper_crtc_reset,
1175b809074SNoralf Trønnes 	.destroy = drm_crtc_cleanup,
1185b809074SNoralf Trønnes 	.set_config = drm_atomic_helper_set_config,
1195b809074SNoralf Trønnes 	.page_flip = drm_atomic_helper_page_flip,
1205b809074SNoralf Trønnes 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
1215b809074SNoralf Trønnes 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
122ac86cba9SOleksandr Andrushchenko 	.enable_vblank = drm_simple_kms_crtc_enable_vblank,
123ac86cba9SOleksandr Andrushchenko 	.disable_vblank = drm_simple_kms_crtc_disable_vblank,
1245b809074SNoralf Trønnes };
1255b809074SNoralf Trønnes 
1265b809074SNoralf Trønnes static int drm_simple_kms_plane_atomic_check(struct drm_plane *plane,
1275b809074SNoralf Trønnes 					struct drm_plane_state *plane_state)
1285b809074SNoralf Trønnes {
1295b809074SNoralf Trønnes 	struct drm_simple_display_pipe *pipe;
1305b809074SNoralf Trønnes 	struct drm_crtc_state *crtc_state;
1315b809074SNoralf Trønnes 	int ret;
1325b809074SNoralf Trønnes 
1335b809074SNoralf Trønnes 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
134b4d93679SMaarten Lankhorst 	crtc_state = drm_atomic_get_new_crtc_state(plane_state->state,
1355b809074SNoralf Trønnes 						   &pipe->crtc);
1364be12cc2SVille Syrjälä 
137a01cb8baSVille Syrjälä 	ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
1385b809074SNoralf Trønnes 						  DRM_PLANE_HELPER_NO_SCALING,
1395b809074SNoralf Trønnes 						  DRM_PLANE_HELPER_NO_SCALING,
1404be12cc2SVille Syrjälä 						  false, true);
1415b809074SNoralf Trønnes 	if (ret)
1425b809074SNoralf Trønnes 		return ret;
1435b809074SNoralf Trønnes 
1444be12cc2SVille Syrjälä 	if (!plane_state->visible)
1454751cf73SOleksandr Andrushchenko 		return 0;
1464751cf73SOleksandr Andrushchenko 
1475b809074SNoralf Trønnes 	if (!pipe->funcs || !pipe->funcs->check)
1485b809074SNoralf Trønnes 		return 0;
1495b809074SNoralf Trønnes 
1505b809074SNoralf Trønnes 	return pipe->funcs->check(pipe, plane_state, crtc_state);
1515b809074SNoralf Trønnes }
1525b809074SNoralf Trønnes 
1535b809074SNoralf Trønnes static void drm_simple_kms_plane_atomic_update(struct drm_plane *plane,
154bcd2ba02SEric Anholt 					struct drm_plane_state *old_pstate)
1555b809074SNoralf Trønnes {
1565b809074SNoralf Trønnes 	struct drm_simple_display_pipe *pipe;
1575b809074SNoralf Trønnes 
1585b809074SNoralf Trønnes 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
1595b809074SNoralf Trønnes 	if (!pipe->funcs || !pipe->funcs->update)
1605b809074SNoralf Trønnes 		return;
1615b809074SNoralf Trønnes 
162bcd2ba02SEric Anholt 	pipe->funcs->update(pipe, old_pstate);
1635b809074SNoralf Trønnes }
1645b809074SNoralf Trønnes 
1657d83a155SMarek Vasut static int drm_simple_kms_plane_prepare_fb(struct drm_plane *plane,
1667d83a155SMarek Vasut 					   struct drm_plane_state *state)
1677d83a155SMarek Vasut {
1687d83a155SMarek Vasut 	struct drm_simple_display_pipe *pipe;
1697d83a155SMarek Vasut 
1707d83a155SMarek Vasut 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
1717d83a155SMarek Vasut 	if (!pipe->funcs || !pipe->funcs->prepare_fb)
1727d83a155SMarek Vasut 		return 0;
1737d83a155SMarek Vasut 
1747d83a155SMarek Vasut 	return pipe->funcs->prepare_fb(pipe, state);
1757d83a155SMarek Vasut }
1767d83a155SMarek Vasut 
1777d83a155SMarek Vasut static void drm_simple_kms_plane_cleanup_fb(struct drm_plane *plane,
1787d83a155SMarek Vasut 					    struct drm_plane_state *state)
1797d83a155SMarek Vasut {
1807d83a155SMarek Vasut 	struct drm_simple_display_pipe *pipe;
1817d83a155SMarek Vasut 
1827d83a155SMarek Vasut 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
1837d83a155SMarek Vasut 	if (!pipe->funcs || !pipe->funcs->cleanup_fb)
1847d83a155SMarek Vasut 		return;
1857d83a155SMarek Vasut 
1867d83a155SMarek Vasut 	pipe->funcs->cleanup_fb(pipe, state);
1877d83a155SMarek Vasut }
1887d83a155SMarek Vasut 
189dff906c3SEric Anholt static bool drm_simple_kms_format_mod_supported(struct drm_plane *plane,
190dff906c3SEric Anholt 						uint32_t format,
191dff906c3SEric Anholt 						uint64_t modifier)
192dff906c3SEric Anholt {
193dff906c3SEric Anholt 	return modifier == DRM_FORMAT_MOD_LINEAR;
194dff906c3SEric Anholt }
195dff906c3SEric Anholt 
1965b809074SNoralf Trønnes static const struct drm_plane_helper_funcs drm_simple_kms_plane_helper_funcs = {
1977d83a155SMarek Vasut 	.prepare_fb = drm_simple_kms_plane_prepare_fb,
1987d83a155SMarek Vasut 	.cleanup_fb = drm_simple_kms_plane_cleanup_fb,
1995b809074SNoralf Trønnes 	.atomic_check = drm_simple_kms_plane_atomic_check,
2005b809074SNoralf Trønnes 	.atomic_update = drm_simple_kms_plane_atomic_update,
2015b809074SNoralf Trønnes };
2025b809074SNoralf Trønnes 
2035b809074SNoralf Trønnes static const struct drm_plane_funcs drm_simple_kms_plane_funcs = {
2045b809074SNoralf Trønnes 	.update_plane		= drm_atomic_helper_update_plane,
2055b809074SNoralf Trønnes 	.disable_plane		= drm_atomic_helper_disable_plane,
2065b809074SNoralf Trønnes 	.destroy		= drm_plane_cleanup,
2075b809074SNoralf Trønnes 	.reset			= drm_atomic_helper_plane_reset,
2085b809074SNoralf Trønnes 	.atomic_duplicate_state	= drm_atomic_helper_plane_duplicate_state,
2095b809074SNoralf Trønnes 	.atomic_destroy_state	= drm_atomic_helper_plane_destroy_state,
210dff906c3SEric Anholt 	.format_mod_supported   = drm_simple_kms_format_mod_supported,
2115b809074SNoralf Trønnes };
2125b809074SNoralf Trønnes 
2135b809074SNoralf Trønnes /**
214315486c6SAndrea Merello  * drm_simple_display_pipe_attach_bridge - Attach a bridge to the display pipe
215315486c6SAndrea Merello  * @pipe: simple display pipe object
216315486c6SAndrea Merello  * @bridge: bridge to attach
217315486c6SAndrea Merello  *
218315486c6SAndrea Merello  * Makes it possible to still use the drm_simple_display_pipe helpers when
219315486c6SAndrea Merello  * a DRM bridge has to be used.
220315486c6SAndrea Merello  *
221315486c6SAndrea Merello  * Note that you probably want to initialize the pipe by passing a NULL
222315486c6SAndrea Merello  * connector to drm_simple_display_pipe_init().
223315486c6SAndrea Merello  *
224315486c6SAndrea Merello  * Returns:
225315486c6SAndrea Merello  * Zero on success, negative error code on failure.
226315486c6SAndrea Merello  */
227315486c6SAndrea Merello int drm_simple_display_pipe_attach_bridge(struct drm_simple_display_pipe *pipe,
228315486c6SAndrea Merello 					  struct drm_bridge *bridge)
229315486c6SAndrea Merello {
2303bb80f24SLaurent Pinchart 	return drm_bridge_attach(&pipe->encoder, bridge, NULL);
231315486c6SAndrea Merello }
232315486c6SAndrea Merello EXPORT_SYMBOL(drm_simple_display_pipe_attach_bridge);
233315486c6SAndrea Merello 
234315486c6SAndrea Merello /**
2355b809074SNoralf Trønnes  * drm_simple_display_pipe_init - Initialize a simple display pipeline
2365b809074SNoralf Trønnes  * @dev: DRM device
2375b809074SNoralf Trønnes  * @pipe: simple display pipe object to initialize
2385b809074SNoralf Trønnes  * @funcs: callbacks for the display pipe (optional)
23962cacc79SDaniel Vetter  * @formats: array of supported formats (DRM_FORMAT\_\*)
2405b809074SNoralf Trønnes  * @format_count: number of elements in @formats
241e6fc3b68SBen Widawsky  * @format_modifiers: array of formats modifiers
2424f993973SAndrea Merello  * @connector: connector to attach and register (optional)
2435b809074SNoralf Trønnes  *
2445b809074SNoralf Trønnes  * Sets up a display pipeline which consist of a really simple
2454f993973SAndrea Merello  * plane-crtc-encoder pipe.
2464f993973SAndrea Merello  *
2474f993973SAndrea Merello  * If a connector is supplied, the pipe will be coupled with the provided
2484f993973SAndrea Merello  * connector. You may supply a NULL connector when using drm bridges, that
2494f993973SAndrea Merello  * handle connectors themselves (see drm_simple_display_pipe_attach_bridge()).
2504f993973SAndrea Merello  *
2515b809074SNoralf Trønnes  * Teardown of a simple display pipe is all handled automatically by the drm
2525b809074SNoralf Trønnes  * core through calling drm_mode_config_cleanup(). Drivers afterwards need to
2535b809074SNoralf Trønnes  * release the memory for the structure themselves.
2545b809074SNoralf Trønnes  *
2555b809074SNoralf Trønnes  * Returns:
2565b809074SNoralf Trønnes  * Zero on success, negative error code on failure.
2575b809074SNoralf Trønnes  */
2585b809074SNoralf Trønnes int drm_simple_display_pipe_init(struct drm_device *dev,
2595b809074SNoralf Trønnes 			struct drm_simple_display_pipe *pipe,
2605b809074SNoralf Trønnes 			const struct drm_simple_display_pipe_funcs *funcs,
2615b809074SNoralf Trønnes 			const uint32_t *formats, unsigned int format_count,
262e6fc3b68SBen Widawsky 			const uint64_t *format_modifiers,
2635b809074SNoralf Trønnes 			struct drm_connector *connector)
2645b809074SNoralf Trønnes {
2655b809074SNoralf Trønnes 	struct drm_encoder *encoder = &pipe->encoder;
2665b809074SNoralf Trønnes 	struct drm_plane *plane = &pipe->plane;
2675b809074SNoralf Trønnes 	struct drm_crtc *crtc = &pipe->crtc;
2685b809074SNoralf Trønnes 	int ret;
2695b809074SNoralf Trønnes 
2705b809074SNoralf Trønnes 	pipe->connector = connector;
2715b809074SNoralf Trønnes 	pipe->funcs = funcs;
2725b809074SNoralf Trønnes 
2735b809074SNoralf Trønnes 	drm_plane_helper_add(plane, &drm_simple_kms_plane_helper_funcs);
2745b809074SNoralf Trønnes 	ret = drm_universal_plane_init(dev, plane, 0,
2755b809074SNoralf Trønnes 				       &drm_simple_kms_plane_funcs,
2765b809074SNoralf Trønnes 				       formats, format_count,
277e6fc3b68SBen Widawsky 				       format_modifiers,
2785b809074SNoralf Trønnes 				       DRM_PLANE_TYPE_PRIMARY, NULL);
2795b809074SNoralf Trønnes 	if (ret)
2805b809074SNoralf Trønnes 		return ret;
2815b809074SNoralf Trønnes 
2825b809074SNoralf Trønnes 	drm_crtc_helper_add(crtc, &drm_simple_kms_crtc_helper_funcs);
2835b809074SNoralf Trønnes 	ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
2845b809074SNoralf Trønnes 					&drm_simple_kms_crtc_funcs, NULL);
2855b809074SNoralf Trønnes 	if (ret)
2865b809074SNoralf Trønnes 		return ret;
2875b809074SNoralf Trønnes 
2886a52193bSVille Syrjälä 	encoder->possible_crtcs = drm_crtc_mask(crtc);
2895b809074SNoralf Trønnes 	ret = drm_encoder_init(dev, encoder, &drm_simple_kms_encoder_funcs,
2905b809074SNoralf Trønnes 			       DRM_MODE_ENCODER_NONE, NULL);
2914f993973SAndrea Merello 	if (ret || !connector)
2925b809074SNoralf Trønnes 		return ret;
2935b809074SNoralf Trønnes 
294cde4c44dSDaniel Vetter 	return drm_connector_attach_encoder(connector, encoder);
2955b809074SNoralf Trønnes }
2965b809074SNoralf Trønnes EXPORT_SYMBOL(drm_simple_display_pipe_init);
2975b809074SNoralf Trønnes 
2985b809074SNoralf Trønnes MODULE_LICENSE("GPL");
299