xref: /linux/drivers/gpu/drm/drm_simple_kms_helper.c (revision a25b988ff83f3ca0d8f5acf855fb1717c1c61a69)
12874c5fdSThomas 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 
60500c04eSSam Ravnborg #include <linux/module.h>
70500c04eSSam Ravnborg #include <linux/slab.h>
80500c04eSSam Ravnborg 
95b809074SNoralf Trønnes #include <drm/drm_atomic.h>
105b809074SNoralf Trønnes #include <drm/drm_atomic_helper.h>
11ee68c743SBoris Brezillon #include <drm/drm_bridge.h>
125b809074SNoralf Trønnes #include <drm/drm_plane_helper.h>
13fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
145b809074SNoralf Trønnes #include <drm/drm_simple_kms_helper.h>
155b809074SNoralf Trønnes 
165b809074SNoralf Trønnes /**
175b809074SNoralf Trønnes  * DOC: overview
185b809074SNoralf Trønnes  *
195b809074SNoralf Trønnes  * This helper library provides helpers for drivers for simple display
205b809074SNoralf Trønnes  * hardware.
215b809074SNoralf Trønnes  *
225b809074SNoralf Trønnes  * drm_simple_display_pipe_init() initializes a simple display pipeline
235b809074SNoralf Trønnes  * which has only one full-screen scanout buffer feeding one output. The
24ea0dd85aSDaniel Vetter  * pipeline is represented by &struct drm_simple_display_pipe and binds
255b809074SNoralf Trønnes  * together &drm_plane, &drm_crtc and &drm_encoder structures into one fixed
265b809074SNoralf Trønnes  * entity. Some flexibility for code reuse is provided through a separately
275b809074SNoralf Trønnes  * allocated &drm_connector object and supporting optional &drm_bridge
285b809074SNoralf Trønnes  * encoder drivers.
295b809074SNoralf Trønnes  */
305b809074SNoralf Trønnes 
315b809074SNoralf Trønnes static const struct drm_encoder_funcs drm_simple_kms_encoder_funcs = {
325b809074SNoralf Trønnes 	.destroy = drm_encoder_cleanup,
335b809074SNoralf Trønnes };
345b809074SNoralf Trønnes 
3540275dc4SLinus Walleij static enum drm_mode_status
3640275dc4SLinus Walleij drm_simple_kms_crtc_mode_valid(struct drm_crtc *crtc,
3740275dc4SLinus Walleij 			       const struct drm_display_mode *mode)
3840275dc4SLinus Walleij {
3940275dc4SLinus Walleij 	struct drm_simple_display_pipe *pipe;
4040275dc4SLinus Walleij 
4140275dc4SLinus Walleij 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
4240275dc4SLinus Walleij 	if (!pipe->funcs || !pipe->funcs->mode_valid)
4340275dc4SLinus Walleij 		/* Anything goes */
4440275dc4SLinus Walleij 		return MODE_OK;
4540275dc4SLinus Walleij 
4662db7d1eSDaniel Vetter 	return pipe->funcs->mode_valid(pipe, mode);
4740275dc4SLinus Walleij }
4840275dc4SLinus Walleij 
496dcf0de7SDaniel Vetter static int drm_simple_kms_crtc_check(struct drm_crtc *crtc,
506dcf0de7SDaniel Vetter 				     struct drm_crtc_state *state)
516dcf0de7SDaniel Vetter {
52765831dcSMaarten Lankhorst 	bool has_primary = state->plane_mask &
5362f77ad0SVille Syrjälä 			   drm_plane_mask(crtc->primary);
54765831dcSMaarten Lankhorst 
55765831dcSMaarten Lankhorst 	/* We always want to have an active plane with an active CRTC */
56765831dcSMaarten Lankhorst 	if (has_primary != state->enable)
57765831dcSMaarten Lankhorst 		return -EINVAL;
58765831dcSMaarten Lankhorst 
596dcf0de7SDaniel Vetter 	return drm_atomic_add_affected_planes(state->state, crtc);
606dcf0de7SDaniel Vetter }
616dcf0de7SDaniel Vetter 
620b20a0f8SLaurent Pinchart static void drm_simple_kms_crtc_enable(struct drm_crtc *crtc,
630b20a0f8SLaurent Pinchart 				       struct drm_crtc_state *old_state)
645b809074SNoralf Trønnes {
650c9c7fd0SVille Syrjälä 	struct drm_plane *plane;
665b809074SNoralf Trønnes 	struct drm_simple_display_pipe *pipe;
675b809074SNoralf Trønnes 
685b809074SNoralf Trønnes 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
695b809074SNoralf Trønnes 	if (!pipe->funcs || !pipe->funcs->enable)
705b809074SNoralf Trønnes 		return;
715b809074SNoralf Trønnes 
720c9c7fd0SVille Syrjälä 	plane = &pipe->plane;
730c9c7fd0SVille Syrjälä 	pipe->funcs->enable(pipe, crtc->state, plane->state);
745b809074SNoralf Trønnes }
755b809074SNoralf Trønnes 
7664581714SLaurent Pinchart static void drm_simple_kms_crtc_disable(struct drm_crtc *crtc,
7764581714SLaurent Pinchart 					struct drm_crtc_state *old_state)
785b809074SNoralf Trønnes {
795b809074SNoralf Trønnes 	struct drm_simple_display_pipe *pipe;
805b809074SNoralf Trønnes 
815b809074SNoralf Trønnes 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
825b809074SNoralf Trønnes 	if (!pipe->funcs || !pipe->funcs->disable)
835b809074SNoralf Trønnes 		return;
845b809074SNoralf Trønnes 
855b809074SNoralf Trønnes 	pipe->funcs->disable(pipe);
865b809074SNoralf Trønnes }
875b809074SNoralf Trønnes 
885b809074SNoralf Trønnes static const struct drm_crtc_helper_funcs drm_simple_kms_crtc_helper_funcs = {
8940275dc4SLinus Walleij 	.mode_valid = drm_simple_kms_crtc_mode_valid,
906dcf0de7SDaniel Vetter 	.atomic_check = drm_simple_kms_crtc_check,
910b20a0f8SLaurent Pinchart 	.atomic_enable = drm_simple_kms_crtc_enable,
9264581714SLaurent Pinchart 	.atomic_disable = drm_simple_kms_crtc_disable,
935b809074SNoralf Trønnes };
945b809074SNoralf Trønnes 
95ac86cba9SOleksandr Andrushchenko static int drm_simple_kms_crtc_enable_vblank(struct drm_crtc *crtc)
96ac86cba9SOleksandr Andrushchenko {
97ac86cba9SOleksandr Andrushchenko 	struct drm_simple_display_pipe *pipe;
98ac86cba9SOleksandr Andrushchenko 
99ac86cba9SOleksandr Andrushchenko 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
100ac86cba9SOleksandr Andrushchenko 	if (!pipe->funcs || !pipe->funcs->enable_vblank)
101ac86cba9SOleksandr Andrushchenko 		return 0;
102ac86cba9SOleksandr Andrushchenko 
103ac86cba9SOleksandr Andrushchenko 	return pipe->funcs->enable_vblank(pipe);
104ac86cba9SOleksandr Andrushchenko }
105ac86cba9SOleksandr Andrushchenko 
106ac86cba9SOleksandr Andrushchenko static void drm_simple_kms_crtc_disable_vblank(struct drm_crtc *crtc)
107ac86cba9SOleksandr Andrushchenko {
108ac86cba9SOleksandr Andrushchenko 	struct drm_simple_display_pipe *pipe;
109ac86cba9SOleksandr Andrushchenko 
110ac86cba9SOleksandr Andrushchenko 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
111ac86cba9SOleksandr Andrushchenko 	if (!pipe->funcs || !pipe->funcs->disable_vblank)
112ac86cba9SOleksandr Andrushchenko 		return;
113ac86cba9SOleksandr Andrushchenko 
114ac86cba9SOleksandr Andrushchenko 	pipe->funcs->disable_vblank(pipe);
115ac86cba9SOleksandr Andrushchenko }
116ac86cba9SOleksandr Andrushchenko 
1175b809074SNoralf Trønnes static const struct drm_crtc_funcs drm_simple_kms_crtc_funcs = {
1185b809074SNoralf Trønnes 	.reset = drm_atomic_helper_crtc_reset,
1195b809074SNoralf Trønnes 	.destroy = drm_crtc_cleanup,
1205b809074SNoralf Trønnes 	.set_config = drm_atomic_helper_set_config,
1215b809074SNoralf Trønnes 	.page_flip = drm_atomic_helper_page_flip,
1225b809074SNoralf Trønnes 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
1235b809074SNoralf Trønnes 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
124ac86cba9SOleksandr Andrushchenko 	.enable_vblank = drm_simple_kms_crtc_enable_vblank,
125ac86cba9SOleksandr Andrushchenko 	.disable_vblank = drm_simple_kms_crtc_disable_vblank,
1265b809074SNoralf Trønnes };
1275b809074SNoralf Trønnes 
1285b809074SNoralf Trønnes static int drm_simple_kms_plane_atomic_check(struct drm_plane *plane,
1295b809074SNoralf Trønnes 					struct drm_plane_state *plane_state)
1305b809074SNoralf Trønnes {
1315b809074SNoralf Trønnes 	struct drm_simple_display_pipe *pipe;
1325b809074SNoralf Trønnes 	struct drm_crtc_state *crtc_state;
1335b809074SNoralf Trønnes 	int ret;
1345b809074SNoralf Trønnes 
1355b809074SNoralf Trønnes 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
136b4d93679SMaarten Lankhorst 	crtc_state = drm_atomic_get_new_crtc_state(plane_state->state,
1375b809074SNoralf Trønnes 						   &pipe->crtc);
1384be12cc2SVille Syrjälä 
139a01cb8baSVille Syrjälä 	ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
1405b809074SNoralf Trønnes 						  DRM_PLANE_HELPER_NO_SCALING,
1415b809074SNoralf Trønnes 						  DRM_PLANE_HELPER_NO_SCALING,
1424be12cc2SVille Syrjälä 						  false, true);
1435b809074SNoralf Trønnes 	if (ret)
1445b809074SNoralf Trønnes 		return ret;
1455b809074SNoralf Trønnes 
1464be12cc2SVille Syrjälä 	if (!plane_state->visible)
1474751cf73SOleksandr Andrushchenko 		return 0;
1484751cf73SOleksandr Andrushchenko 
1495b809074SNoralf Trønnes 	if (!pipe->funcs || !pipe->funcs->check)
1505b809074SNoralf Trønnes 		return 0;
1515b809074SNoralf Trønnes 
1525b809074SNoralf Trønnes 	return pipe->funcs->check(pipe, plane_state, crtc_state);
1535b809074SNoralf Trønnes }
1545b809074SNoralf Trønnes 
1555b809074SNoralf Trønnes static void drm_simple_kms_plane_atomic_update(struct drm_plane *plane,
156bcd2ba02SEric Anholt 					struct drm_plane_state *old_pstate)
1575b809074SNoralf Trønnes {
1585b809074SNoralf Trønnes 	struct drm_simple_display_pipe *pipe;
1595b809074SNoralf Trønnes 
1605b809074SNoralf Trønnes 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
1615b809074SNoralf Trønnes 	if (!pipe->funcs || !pipe->funcs->update)
1625b809074SNoralf Trønnes 		return;
1635b809074SNoralf Trønnes 
164bcd2ba02SEric Anholt 	pipe->funcs->update(pipe, old_pstate);
1655b809074SNoralf Trønnes }
1665b809074SNoralf Trønnes 
1677d83a155SMarek Vasut static int drm_simple_kms_plane_prepare_fb(struct drm_plane *plane,
1687d83a155SMarek Vasut 					   struct drm_plane_state *state)
1697d83a155SMarek Vasut {
1707d83a155SMarek Vasut 	struct drm_simple_display_pipe *pipe;
1717d83a155SMarek Vasut 
1727d83a155SMarek Vasut 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
1737d83a155SMarek Vasut 	if (!pipe->funcs || !pipe->funcs->prepare_fb)
1747d83a155SMarek Vasut 		return 0;
1757d83a155SMarek Vasut 
1767d83a155SMarek Vasut 	return pipe->funcs->prepare_fb(pipe, state);
1777d83a155SMarek Vasut }
1787d83a155SMarek Vasut 
1797d83a155SMarek Vasut static void drm_simple_kms_plane_cleanup_fb(struct drm_plane *plane,
1807d83a155SMarek Vasut 					    struct drm_plane_state *state)
1817d83a155SMarek Vasut {
1827d83a155SMarek Vasut 	struct drm_simple_display_pipe *pipe;
1837d83a155SMarek Vasut 
1847d83a155SMarek Vasut 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
1857d83a155SMarek Vasut 	if (!pipe->funcs || !pipe->funcs->cleanup_fb)
1867d83a155SMarek Vasut 		return;
1877d83a155SMarek Vasut 
1887d83a155SMarek Vasut 	pipe->funcs->cleanup_fb(pipe, state);
1897d83a155SMarek Vasut }
1907d83a155SMarek Vasut 
191dff906c3SEric Anholt static bool drm_simple_kms_format_mod_supported(struct drm_plane *plane,
192dff906c3SEric Anholt 						uint32_t format,
193dff906c3SEric Anholt 						uint64_t modifier)
194dff906c3SEric Anholt {
195dff906c3SEric Anholt 	return modifier == DRM_FORMAT_MOD_LINEAR;
196dff906c3SEric Anholt }
197dff906c3SEric Anholt 
1985b809074SNoralf Trønnes static const struct drm_plane_helper_funcs drm_simple_kms_plane_helper_funcs = {
1997d83a155SMarek Vasut 	.prepare_fb = drm_simple_kms_plane_prepare_fb,
2007d83a155SMarek Vasut 	.cleanup_fb = drm_simple_kms_plane_cleanup_fb,
2015b809074SNoralf Trønnes 	.atomic_check = drm_simple_kms_plane_atomic_check,
2025b809074SNoralf Trønnes 	.atomic_update = drm_simple_kms_plane_atomic_update,
2035b809074SNoralf Trønnes };
2045b809074SNoralf Trønnes 
2055b809074SNoralf Trønnes static const struct drm_plane_funcs drm_simple_kms_plane_funcs = {
2065b809074SNoralf Trønnes 	.update_plane		= drm_atomic_helper_update_plane,
2075b809074SNoralf Trønnes 	.disable_plane		= drm_atomic_helper_disable_plane,
2085b809074SNoralf Trønnes 	.destroy		= drm_plane_cleanup,
2095b809074SNoralf Trønnes 	.reset			= drm_atomic_helper_plane_reset,
2105b809074SNoralf Trønnes 	.atomic_duplicate_state	= drm_atomic_helper_plane_duplicate_state,
2115b809074SNoralf Trønnes 	.atomic_destroy_state	= drm_atomic_helper_plane_destroy_state,
212dff906c3SEric Anholt 	.format_mod_supported   = drm_simple_kms_format_mod_supported,
2135b809074SNoralf Trønnes };
2145b809074SNoralf Trønnes 
2155b809074SNoralf Trønnes /**
216315486c6SAndrea Merello  * drm_simple_display_pipe_attach_bridge - Attach a bridge to the display pipe
217315486c6SAndrea Merello  * @pipe: simple display pipe object
218315486c6SAndrea Merello  * @bridge: bridge to attach
219315486c6SAndrea Merello  *
220315486c6SAndrea Merello  * Makes it possible to still use the drm_simple_display_pipe helpers when
221315486c6SAndrea Merello  * a DRM bridge has to be used.
222315486c6SAndrea Merello  *
223315486c6SAndrea Merello  * Note that you probably want to initialize the pipe by passing a NULL
224315486c6SAndrea Merello  * connector to drm_simple_display_pipe_init().
225315486c6SAndrea Merello  *
226315486c6SAndrea Merello  * Returns:
227315486c6SAndrea Merello  * Zero on success, negative error code on failure.
228315486c6SAndrea Merello  */
229315486c6SAndrea Merello int drm_simple_display_pipe_attach_bridge(struct drm_simple_display_pipe *pipe,
230315486c6SAndrea Merello 					  struct drm_bridge *bridge)
231315486c6SAndrea Merello {
232*a25b988fSLaurent Pinchart 	return drm_bridge_attach(&pipe->encoder, bridge, NULL, 0);
233315486c6SAndrea Merello }
234315486c6SAndrea Merello EXPORT_SYMBOL(drm_simple_display_pipe_attach_bridge);
235315486c6SAndrea Merello 
236315486c6SAndrea Merello /**
2375b809074SNoralf Trønnes  * drm_simple_display_pipe_init - Initialize a simple display pipeline
2385b809074SNoralf Trønnes  * @dev: DRM device
2395b809074SNoralf Trønnes  * @pipe: simple display pipe object to initialize
2405b809074SNoralf Trønnes  * @funcs: callbacks for the display pipe (optional)
24162cacc79SDaniel Vetter  * @formats: array of supported formats (DRM_FORMAT\_\*)
2425b809074SNoralf Trønnes  * @format_count: number of elements in @formats
243e6fc3b68SBen Widawsky  * @format_modifiers: array of formats modifiers
2444f993973SAndrea Merello  * @connector: connector to attach and register (optional)
2455b809074SNoralf Trønnes  *
2465b809074SNoralf Trønnes  * Sets up a display pipeline which consist of a really simple
2474f993973SAndrea Merello  * plane-crtc-encoder pipe.
2484f993973SAndrea Merello  *
2494f993973SAndrea Merello  * If a connector is supplied, the pipe will be coupled with the provided
2504f993973SAndrea Merello  * connector. You may supply a NULL connector when using drm bridges, that
2514f993973SAndrea Merello  * handle connectors themselves (see drm_simple_display_pipe_attach_bridge()).
2524f993973SAndrea Merello  *
2535b809074SNoralf Trønnes  * Teardown of a simple display pipe is all handled automatically by the drm
2545b809074SNoralf Trønnes  * core through calling drm_mode_config_cleanup(). Drivers afterwards need to
2555b809074SNoralf Trønnes  * release the memory for the structure themselves.
2565b809074SNoralf Trønnes  *
2575b809074SNoralf Trønnes  * Returns:
2585b809074SNoralf Trønnes  * Zero on success, negative error code on failure.
2595b809074SNoralf Trønnes  */
2605b809074SNoralf Trønnes int drm_simple_display_pipe_init(struct drm_device *dev,
2615b809074SNoralf Trønnes 			struct drm_simple_display_pipe *pipe,
2625b809074SNoralf Trønnes 			const struct drm_simple_display_pipe_funcs *funcs,
2635b809074SNoralf Trønnes 			const uint32_t *formats, unsigned int format_count,
264e6fc3b68SBen Widawsky 			const uint64_t *format_modifiers,
2655b809074SNoralf Trønnes 			struct drm_connector *connector)
2665b809074SNoralf Trønnes {
2675b809074SNoralf Trønnes 	struct drm_encoder *encoder = &pipe->encoder;
2685b809074SNoralf Trønnes 	struct drm_plane *plane = &pipe->plane;
2695b809074SNoralf Trønnes 	struct drm_crtc *crtc = &pipe->crtc;
2705b809074SNoralf Trønnes 	int ret;
2715b809074SNoralf Trønnes 
2725b809074SNoralf Trønnes 	pipe->connector = connector;
2735b809074SNoralf Trønnes 	pipe->funcs = funcs;
2745b809074SNoralf Trønnes 
2755b809074SNoralf Trønnes 	drm_plane_helper_add(plane, &drm_simple_kms_plane_helper_funcs);
2765b809074SNoralf Trønnes 	ret = drm_universal_plane_init(dev, plane, 0,
2775b809074SNoralf Trønnes 				       &drm_simple_kms_plane_funcs,
2785b809074SNoralf Trønnes 				       formats, format_count,
279e6fc3b68SBen Widawsky 				       format_modifiers,
2805b809074SNoralf Trønnes 				       DRM_PLANE_TYPE_PRIMARY, NULL);
2815b809074SNoralf Trønnes 	if (ret)
2825b809074SNoralf Trønnes 		return ret;
2835b809074SNoralf Trønnes 
2845b809074SNoralf Trønnes 	drm_crtc_helper_add(crtc, &drm_simple_kms_crtc_helper_funcs);
2855b809074SNoralf Trønnes 	ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
2865b809074SNoralf Trønnes 					&drm_simple_kms_crtc_funcs, NULL);
2875b809074SNoralf Trønnes 	if (ret)
2885b809074SNoralf Trønnes 		return ret;
2895b809074SNoralf Trønnes 
2906a52193bSVille Syrjälä 	encoder->possible_crtcs = drm_crtc_mask(crtc);
2915b809074SNoralf Trønnes 	ret = drm_encoder_init(dev, encoder, &drm_simple_kms_encoder_funcs,
2925b809074SNoralf Trønnes 			       DRM_MODE_ENCODER_NONE, NULL);
2934f993973SAndrea Merello 	if (ret || !connector)
2945b809074SNoralf Trønnes 		return ret;
2955b809074SNoralf Trønnes 
296cde4c44dSDaniel Vetter 	return drm_connector_attach_encoder(connector, encoder);
2975b809074SNoralf Trønnes }
2985b809074SNoralf Trønnes EXPORT_SYMBOL(drm_simple_display_pipe_init);
2995b809074SNoralf Trønnes 
3005b809074SNoralf Trønnes MODULE_LICENSE("GPL");
301