xref: /linux/drivers/gpu/drm/drm_simple_kms_helper.c (revision 62f77ad0969594ee428043523bf28329df191b39)
15b809074SNoralf Trønnes /*
25b809074SNoralf Trønnes  * Copyright (C) 2016 Noralf Trønnes
35b809074SNoralf Trønnes  *
45b809074SNoralf Trønnes  * This program is free software; you can redistribute it and/or modify
55b809074SNoralf Trønnes  * it under the terms of the GNU General Public License as published by
65b809074SNoralf Trønnes  * the Free Software Foundation; either version 2 of the License, or
75b809074SNoralf Trønnes  * (at your option) any later version.
85b809074SNoralf Trønnes  */
95b809074SNoralf Trønnes 
105b809074SNoralf Trønnes #include <drm/drmP.h>
115b809074SNoralf Trønnes #include <drm/drm_atomic.h>
125b809074SNoralf Trønnes #include <drm/drm_atomic_helper.h>
135b809074SNoralf Trønnes #include <drm/drm_crtc_helper.h>
145b809074SNoralf Trønnes #include <drm/drm_plane_helper.h>
155b809074SNoralf Trønnes #include <drm/drm_simple_kms_helper.h>
165b809074SNoralf Trønnes #include <linux/slab.h>
175b809074SNoralf Trønnes 
185b809074SNoralf Trønnes /**
195b809074SNoralf Trønnes  * DOC: overview
205b809074SNoralf Trønnes  *
215b809074SNoralf Trønnes  * This helper library provides helpers for drivers for simple display
225b809074SNoralf Trønnes  * hardware.
235b809074SNoralf Trønnes  *
245b809074SNoralf Trønnes  * drm_simple_display_pipe_init() initializes a simple display pipeline
255b809074SNoralf Trønnes  * which has only one full-screen scanout buffer feeding one output. The
26ea0dd85aSDaniel Vetter  * pipeline is represented by &struct drm_simple_display_pipe and binds
275b809074SNoralf Trønnes  * together &drm_plane, &drm_crtc and &drm_encoder structures into one fixed
285b809074SNoralf Trønnes  * entity. Some flexibility for code reuse is provided through a separately
295b809074SNoralf Trønnes  * allocated &drm_connector object and supporting optional &drm_bridge
305b809074SNoralf Trønnes  * encoder drivers.
315b809074SNoralf Trønnes  */
325b809074SNoralf Trønnes 
335b809074SNoralf Trønnes static const struct drm_encoder_funcs drm_simple_kms_encoder_funcs = {
345b809074SNoralf Trønnes 	.destroy = drm_encoder_cleanup,
355b809074SNoralf Trønnes };
365b809074SNoralf Trønnes 
3740275dc4SLinus Walleij static enum drm_mode_status
3840275dc4SLinus Walleij drm_simple_kms_crtc_mode_valid(struct drm_crtc *crtc,
3940275dc4SLinus Walleij 			       const struct drm_display_mode *mode)
4040275dc4SLinus Walleij {
4140275dc4SLinus Walleij 	struct drm_simple_display_pipe *pipe;
4240275dc4SLinus Walleij 
4340275dc4SLinus Walleij 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
4440275dc4SLinus Walleij 	if (!pipe->funcs || !pipe->funcs->mode_valid)
4540275dc4SLinus Walleij 		/* Anything goes */
4640275dc4SLinus Walleij 		return MODE_OK;
4740275dc4SLinus Walleij 
4840275dc4SLinus Walleij 	return pipe->funcs->mode_valid(crtc, mode);
4940275dc4SLinus Walleij }
5040275dc4SLinus Walleij 
516dcf0de7SDaniel Vetter static int drm_simple_kms_crtc_check(struct drm_crtc *crtc,
526dcf0de7SDaniel Vetter 				     struct drm_crtc_state *state)
536dcf0de7SDaniel Vetter {
54765831dcSMaarten Lankhorst 	bool has_primary = state->plane_mask &
55*62f77ad0SVille Syrjälä 			   drm_plane_mask(crtc->primary);
56765831dcSMaarten Lankhorst 
57765831dcSMaarten Lankhorst 	/* We always want to have an active plane with an active CRTC */
58765831dcSMaarten Lankhorst 	if (has_primary != state->enable)
59765831dcSMaarten Lankhorst 		return -EINVAL;
60765831dcSMaarten Lankhorst 
616dcf0de7SDaniel Vetter 	return drm_atomic_add_affected_planes(state->state, crtc);
626dcf0de7SDaniel Vetter }
636dcf0de7SDaniel Vetter 
640b20a0f8SLaurent Pinchart static void drm_simple_kms_crtc_enable(struct drm_crtc *crtc,
650b20a0f8SLaurent Pinchart 				       struct drm_crtc_state *old_state)
665b809074SNoralf Trønnes {
670c9c7fd0SVille Syrjälä 	struct drm_plane *plane;
685b809074SNoralf Trønnes 	struct drm_simple_display_pipe *pipe;
695b809074SNoralf Trønnes 
705b809074SNoralf Trønnes 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
715b809074SNoralf Trønnes 	if (!pipe->funcs || !pipe->funcs->enable)
725b809074SNoralf Trønnes 		return;
735b809074SNoralf Trønnes 
740c9c7fd0SVille Syrjälä 	plane = &pipe->plane;
750c9c7fd0SVille Syrjälä 	pipe->funcs->enable(pipe, crtc->state, plane->state);
765b809074SNoralf Trønnes }
775b809074SNoralf Trønnes 
7864581714SLaurent Pinchart static void drm_simple_kms_crtc_disable(struct drm_crtc *crtc,
7964581714SLaurent Pinchart 					struct drm_crtc_state *old_state)
805b809074SNoralf Trønnes {
815b809074SNoralf Trønnes 	struct drm_simple_display_pipe *pipe;
825b809074SNoralf Trønnes 
835b809074SNoralf Trønnes 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
845b809074SNoralf Trønnes 	if (!pipe->funcs || !pipe->funcs->disable)
855b809074SNoralf Trønnes 		return;
865b809074SNoralf Trønnes 
875b809074SNoralf Trønnes 	pipe->funcs->disable(pipe);
885b809074SNoralf Trønnes }
895b809074SNoralf Trønnes 
905b809074SNoralf Trønnes static const struct drm_crtc_helper_funcs drm_simple_kms_crtc_helper_funcs = {
9140275dc4SLinus Walleij 	.mode_valid = drm_simple_kms_crtc_mode_valid,
926dcf0de7SDaniel Vetter 	.atomic_check = drm_simple_kms_crtc_check,
930b20a0f8SLaurent Pinchart 	.atomic_enable = drm_simple_kms_crtc_enable,
9464581714SLaurent Pinchart 	.atomic_disable = drm_simple_kms_crtc_disable,
955b809074SNoralf Trønnes };
965b809074SNoralf Trønnes 
97ac86cba9SOleksandr Andrushchenko static int drm_simple_kms_crtc_enable_vblank(struct drm_crtc *crtc)
98ac86cba9SOleksandr Andrushchenko {
99ac86cba9SOleksandr Andrushchenko 	struct drm_simple_display_pipe *pipe;
100ac86cba9SOleksandr Andrushchenko 
101ac86cba9SOleksandr Andrushchenko 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
102ac86cba9SOleksandr Andrushchenko 	if (!pipe->funcs || !pipe->funcs->enable_vblank)
103ac86cba9SOleksandr Andrushchenko 		return 0;
104ac86cba9SOleksandr Andrushchenko 
105ac86cba9SOleksandr Andrushchenko 	return pipe->funcs->enable_vblank(pipe);
106ac86cba9SOleksandr Andrushchenko }
107ac86cba9SOleksandr Andrushchenko 
108ac86cba9SOleksandr Andrushchenko static void drm_simple_kms_crtc_disable_vblank(struct drm_crtc *crtc)
109ac86cba9SOleksandr Andrushchenko {
110ac86cba9SOleksandr Andrushchenko 	struct drm_simple_display_pipe *pipe;
111ac86cba9SOleksandr Andrushchenko 
112ac86cba9SOleksandr Andrushchenko 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
113ac86cba9SOleksandr Andrushchenko 	if (!pipe->funcs || !pipe->funcs->disable_vblank)
114ac86cba9SOleksandr Andrushchenko 		return;
115ac86cba9SOleksandr Andrushchenko 
116ac86cba9SOleksandr Andrushchenko 	pipe->funcs->disable_vblank(pipe);
117ac86cba9SOleksandr Andrushchenko }
118ac86cba9SOleksandr Andrushchenko 
1195b809074SNoralf Trønnes static const struct drm_crtc_funcs drm_simple_kms_crtc_funcs = {
1205b809074SNoralf Trønnes 	.reset = drm_atomic_helper_crtc_reset,
1215b809074SNoralf Trønnes 	.destroy = drm_crtc_cleanup,
1225b809074SNoralf Trønnes 	.set_config = drm_atomic_helper_set_config,
1235b809074SNoralf Trønnes 	.page_flip = drm_atomic_helper_page_flip,
1245b809074SNoralf Trønnes 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
1255b809074SNoralf Trønnes 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
126ac86cba9SOleksandr Andrushchenko 	.enable_vblank = drm_simple_kms_crtc_enable_vblank,
127ac86cba9SOleksandr Andrushchenko 	.disable_vblank = drm_simple_kms_crtc_disable_vblank,
1285b809074SNoralf Trønnes };
1295b809074SNoralf Trønnes 
1305b809074SNoralf Trønnes static int drm_simple_kms_plane_atomic_check(struct drm_plane *plane,
1315b809074SNoralf Trønnes 					struct drm_plane_state *plane_state)
1325b809074SNoralf Trønnes {
1335b809074SNoralf Trønnes 	struct drm_simple_display_pipe *pipe;
1345b809074SNoralf Trønnes 	struct drm_crtc_state *crtc_state;
1355b809074SNoralf Trønnes 	int ret;
1365b809074SNoralf Trønnes 
1375b809074SNoralf Trønnes 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
138b4d93679SMaarten Lankhorst 	crtc_state = drm_atomic_get_new_crtc_state(plane_state->state,
1395b809074SNoralf Trønnes 						   &pipe->crtc);
1404be12cc2SVille Syrjälä 
141a01cb8baSVille Syrjälä 	ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
1425b809074SNoralf Trønnes 						  DRM_PLANE_HELPER_NO_SCALING,
1435b809074SNoralf Trønnes 						  DRM_PLANE_HELPER_NO_SCALING,
1444be12cc2SVille Syrjälä 						  false, true);
1455b809074SNoralf Trønnes 	if (ret)
1465b809074SNoralf Trønnes 		return ret;
1475b809074SNoralf Trønnes 
1484be12cc2SVille Syrjälä 	if (!plane_state->visible)
1494751cf73SOleksandr Andrushchenko 		return 0;
1504751cf73SOleksandr Andrushchenko 
1515b809074SNoralf Trønnes 	if (!pipe->funcs || !pipe->funcs->check)
1525b809074SNoralf Trønnes 		return 0;
1535b809074SNoralf Trønnes 
1545b809074SNoralf Trønnes 	return pipe->funcs->check(pipe, plane_state, crtc_state);
1555b809074SNoralf Trønnes }
1565b809074SNoralf Trønnes 
1575b809074SNoralf Trønnes static void drm_simple_kms_plane_atomic_update(struct drm_plane *plane,
158bcd2ba02SEric Anholt 					struct drm_plane_state *old_pstate)
1595b809074SNoralf Trønnes {
1605b809074SNoralf Trønnes 	struct drm_simple_display_pipe *pipe;
1615b809074SNoralf Trønnes 
1625b809074SNoralf Trønnes 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
1635b809074SNoralf Trønnes 	if (!pipe->funcs || !pipe->funcs->update)
1645b809074SNoralf Trønnes 		return;
1655b809074SNoralf Trønnes 
166bcd2ba02SEric Anholt 	pipe->funcs->update(pipe, old_pstate);
1675b809074SNoralf Trønnes }
1685b809074SNoralf Trønnes 
1697d83a155SMarek Vasut static int drm_simple_kms_plane_prepare_fb(struct drm_plane *plane,
1707d83a155SMarek Vasut 					   struct drm_plane_state *state)
1717d83a155SMarek Vasut {
1727d83a155SMarek Vasut 	struct drm_simple_display_pipe *pipe;
1737d83a155SMarek Vasut 
1747d83a155SMarek Vasut 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
1757d83a155SMarek Vasut 	if (!pipe->funcs || !pipe->funcs->prepare_fb)
1767d83a155SMarek Vasut 		return 0;
1777d83a155SMarek Vasut 
1787d83a155SMarek Vasut 	return pipe->funcs->prepare_fb(pipe, state);
1797d83a155SMarek Vasut }
1807d83a155SMarek Vasut 
1817d83a155SMarek Vasut static void drm_simple_kms_plane_cleanup_fb(struct drm_plane *plane,
1827d83a155SMarek Vasut 					    struct drm_plane_state *state)
1837d83a155SMarek Vasut {
1847d83a155SMarek Vasut 	struct drm_simple_display_pipe *pipe;
1857d83a155SMarek Vasut 
1867d83a155SMarek Vasut 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
1877d83a155SMarek Vasut 	if (!pipe->funcs || !pipe->funcs->cleanup_fb)
1887d83a155SMarek Vasut 		return;
1897d83a155SMarek Vasut 
1907d83a155SMarek Vasut 	pipe->funcs->cleanup_fb(pipe, state);
1917d83a155SMarek Vasut }
1927d83a155SMarek Vasut 
1935b809074SNoralf Trønnes static const struct drm_plane_helper_funcs drm_simple_kms_plane_helper_funcs = {
1947d83a155SMarek Vasut 	.prepare_fb = drm_simple_kms_plane_prepare_fb,
1957d83a155SMarek Vasut 	.cleanup_fb = drm_simple_kms_plane_cleanup_fb,
1965b809074SNoralf Trønnes 	.atomic_check = drm_simple_kms_plane_atomic_check,
1975b809074SNoralf Trønnes 	.atomic_update = drm_simple_kms_plane_atomic_update,
1985b809074SNoralf Trønnes };
1995b809074SNoralf Trønnes 
2005b809074SNoralf Trønnes static const struct drm_plane_funcs drm_simple_kms_plane_funcs = {
2015b809074SNoralf Trønnes 	.update_plane		= drm_atomic_helper_update_plane,
2025b809074SNoralf Trønnes 	.disable_plane		= drm_atomic_helper_disable_plane,
2035b809074SNoralf Trønnes 	.destroy		= drm_plane_cleanup,
2045b809074SNoralf Trønnes 	.reset			= drm_atomic_helper_plane_reset,
2055b809074SNoralf Trønnes 	.atomic_duplicate_state	= drm_atomic_helper_plane_duplicate_state,
2065b809074SNoralf Trønnes 	.atomic_destroy_state	= drm_atomic_helper_plane_destroy_state,
2075b809074SNoralf Trønnes };
2085b809074SNoralf Trønnes 
2095b809074SNoralf Trønnes /**
210315486c6SAndrea Merello  * drm_simple_display_pipe_attach_bridge - Attach a bridge to the display pipe
211315486c6SAndrea Merello  * @pipe: simple display pipe object
212315486c6SAndrea Merello  * @bridge: bridge to attach
213315486c6SAndrea Merello  *
214315486c6SAndrea Merello  * Makes it possible to still use the drm_simple_display_pipe helpers when
215315486c6SAndrea Merello  * a DRM bridge has to be used.
216315486c6SAndrea Merello  *
217315486c6SAndrea Merello  * Note that you probably want to initialize the pipe by passing a NULL
218315486c6SAndrea Merello  * connector to drm_simple_display_pipe_init().
219315486c6SAndrea Merello  *
220315486c6SAndrea Merello  * Returns:
221315486c6SAndrea Merello  * Zero on success, negative error code on failure.
222315486c6SAndrea Merello  */
223315486c6SAndrea Merello int drm_simple_display_pipe_attach_bridge(struct drm_simple_display_pipe *pipe,
224315486c6SAndrea Merello 					  struct drm_bridge *bridge)
225315486c6SAndrea Merello {
2263bb80f24SLaurent Pinchart 	return drm_bridge_attach(&pipe->encoder, bridge, NULL);
227315486c6SAndrea Merello }
228315486c6SAndrea Merello EXPORT_SYMBOL(drm_simple_display_pipe_attach_bridge);
229315486c6SAndrea Merello 
230315486c6SAndrea Merello /**
2315b809074SNoralf Trønnes  * drm_simple_display_pipe_init - Initialize a simple display pipeline
2325b809074SNoralf Trønnes  * @dev: DRM device
2335b809074SNoralf Trønnes  * @pipe: simple display pipe object to initialize
2345b809074SNoralf Trønnes  * @funcs: callbacks for the display pipe (optional)
23562cacc79SDaniel Vetter  * @formats: array of supported formats (DRM_FORMAT\_\*)
2365b809074SNoralf Trønnes  * @format_count: number of elements in @formats
237e6fc3b68SBen Widawsky  * @format_modifiers: array of formats modifiers
2384f993973SAndrea Merello  * @connector: connector to attach and register (optional)
2395b809074SNoralf Trønnes  *
2405b809074SNoralf Trønnes  * Sets up a display pipeline which consist of a really simple
2414f993973SAndrea Merello  * plane-crtc-encoder pipe.
2424f993973SAndrea Merello  *
2434f993973SAndrea Merello  * If a connector is supplied, the pipe will be coupled with the provided
2444f993973SAndrea Merello  * connector. You may supply a NULL connector when using drm bridges, that
2454f993973SAndrea Merello  * handle connectors themselves (see drm_simple_display_pipe_attach_bridge()).
2464f993973SAndrea Merello  *
2475b809074SNoralf Trønnes  * Teardown of a simple display pipe is all handled automatically by the drm
2485b809074SNoralf Trønnes  * core through calling drm_mode_config_cleanup(). Drivers afterwards need to
2495b809074SNoralf Trønnes  * release the memory for the structure themselves.
2505b809074SNoralf Trønnes  *
2515b809074SNoralf Trønnes  * Returns:
2525b809074SNoralf Trønnes  * Zero on success, negative error code on failure.
2535b809074SNoralf Trønnes  */
2545b809074SNoralf Trønnes int drm_simple_display_pipe_init(struct drm_device *dev,
2555b809074SNoralf Trønnes 			struct drm_simple_display_pipe *pipe,
2565b809074SNoralf Trønnes 			const struct drm_simple_display_pipe_funcs *funcs,
2575b809074SNoralf Trønnes 			const uint32_t *formats, unsigned int format_count,
258e6fc3b68SBen Widawsky 			const uint64_t *format_modifiers,
2595b809074SNoralf Trønnes 			struct drm_connector *connector)
2605b809074SNoralf Trønnes {
2615b809074SNoralf Trønnes 	struct drm_encoder *encoder = &pipe->encoder;
2625b809074SNoralf Trønnes 	struct drm_plane *plane = &pipe->plane;
2635b809074SNoralf Trønnes 	struct drm_crtc *crtc = &pipe->crtc;
2645b809074SNoralf Trønnes 	int ret;
2655b809074SNoralf Trønnes 
2665b809074SNoralf Trønnes 	pipe->connector = connector;
2675b809074SNoralf Trønnes 	pipe->funcs = funcs;
2685b809074SNoralf Trønnes 
2695b809074SNoralf Trønnes 	drm_plane_helper_add(plane, &drm_simple_kms_plane_helper_funcs);
2705b809074SNoralf Trønnes 	ret = drm_universal_plane_init(dev, plane, 0,
2715b809074SNoralf Trønnes 				       &drm_simple_kms_plane_funcs,
2725b809074SNoralf Trønnes 				       formats, format_count,
273e6fc3b68SBen Widawsky 				       format_modifiers,
2745b809074SNoralf Trønnes 				       DRM_PLANE_TYPE_PRIMARY, NULL);
2755b809074SNoralf Trønnes 	if (ret)
2765b809074SNoralf Trønnes 		return ret;
2775b809074SNoralf Trønnes 
2785b809074SNoralf Trønnes 	drm_crtc_helper_add(crtc, &drm_simple_kms_crtc_helper_funcs);
2795b809074SNoralf Trønnes 	ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
2805b809074SNoralf Trønnes 					&drm_simple_kms_crtc_funcs, NULL);
2815b809074SNoralf Trønnes 	if (ret)
2825b809074SNoralf Trønnes 		return ret;
2835b809074SNoralf Trønnes 
2845b809074SNoralf Trønnes 	encoder->possible_crtcs = 1 << drm_crtc_index(crtc);
2855b809074SNoralf Trønnes 	ret = drm_encoder_init(dev, encoder, &drm_simple_kms_encoder_funcs,
2865b809074SNoralf Trønnes 			       DRM_MODE_ENCODER_NONE, NULL);
2874f993973SAndrea Merello 	if (ret || !connector)
2885b809074SNoralf Trønnes 		return ret;
2895b809074SNoralf Trønnes 
2905b809074SNoralf Trønnes 	return drm_mode_connector_attach_encoder(connector, encoder);
2915b809074SNoralf Trønnes }
2925b809074SNoralf Trønnes EXPORT_SYMBOL(drm_simple_display_pipe_init);
2935b809074SNoralf Trønnes 
2945b809074SNoralf Trønnes MODULE_LICENSE("GPL");
295