xref: /linux/drivers/gpu/drm/drm_simple_kms_helper.c (revision 62cacc79398383d3ff47011557c77e71369126ca)
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
265b809074SNoralf Trønnes  * 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 
375b809074SNoralf Trønnes static void drm_simple_kms_crtc_enable(struct drm_crtc *crtc)
385b809074SNoralf Trønnes {
395b809074SNoralf Trønnes 	struct drm_simple_display_pipe *pipe;
405b809074SNoralf Trønnes 
415b809074SNoralf Trønnes 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
425b809074SNoralf Trønnes 	if (!pipe->funcs || !pipe->funcs->enable)
435b809074SNoralf Trønnes 		return;
445b809074SNoralf Trønnes 
455b809074SNoralf Trønnes 	pipe->funcs->enable(pipe, crtc->state);
465b809074SNoralf Trønnes }
475b809074SNoralf Trønnes 
485b809074SNoralf Trønnes static void drm_simple_kms_crtc_disable(struct drm_crtc *crtc)
495b809074SNoralf Trønnes {
505b809074SNoralf Trønnes 	struct drm_simple_display_pipe *pipe;
515b809074SNoralf Trønnes 
525b809074SNoralf Trønnes 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
535b809074SNoralf Trønnes 	if (!pipe->funcs || !pipe->funcs->disable)
545b809074SNoralf Trønnes 		return;
555b809074SNoralf Trønnes 
565b809074SNoralf Trønnes 	pipe->funcs->disable(pipe);
575b809074SNoralf Trønnes }
585b809074SNoralf Trønnes 
595b809074SNoralf Trønnes static const struct drm_crtc_helper_funcs drm_simple_kms_crtc_helper_funcs = {
605b809074SNoralf Trønnes 	.disable = drm_simple_kms_crtc_disable,
615b809074SNoralf Trønnes 	.enable = drm_simple_kms_crtc_enable,
625b809074SNoralf Trønnes };
635b809074SNoralf Trønnes 
645b809074SNoralf Trønnes static const struct drm_crtc_funcs drm_simple_kms_crtc_funcs = {
655b809074SNoralf Trønnes 	.reset = drm_atomic_helper_crtc_reset,
665b809074SNoralf Trønnes 	.destroy = drm_crtc_cleanup,
675b809074SNoralf Trønnes 	.set_config = drm_atomic_helper_set_config,
685b809074SNoralf Trønnes 	.page_flip = drm_atomic_helper_page_flip,
695b809074SNoralf Trønnes 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
705b809074SNoralf Trønnes 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
715b809074SNoralf Trønnes };
725b809074SNoralf Trønnes 
735b809074SNoralf Trønnes static int drm_simple_kms_plane_atomic_check(struct drm_plane *plane,
745b809074SNoralf Trønnes 					struct drm_plane_state *plane_state)
755b809074SNoralf Trønnes {
765b809074SNoralf Trønnes 	struct drm_rect clip = { 0 };
775b809074SNoralf Trønnes 	struct drm_simple_display_pipe *pipe;
785b809074SNoralf Trønnes 	struct drm_crtc_state *crtc_state;
795b809074SNoralf Trønnes 	int ret;
805b809074SNoralf Trønnes 
815b809074SNoralf Trønnes 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
825b809074SNoralf Trønnes 	crtc_state = drm_atomic_get_existing_crtc_state(plane_state->state,
835b809074SNoralf Trønnes 							&pipe->crtc);
845b809074SNoralf Trønnes 	if (crtc_state->enable != !!plane_state->crtc)
855b809074SNoralf Trønnes 		return -EINVAL; /* plane must match crtc enable state */
865b809074SNoralf Trønnes 
875b809074SNoralf Trønnes 	if (!crtc_state->enable)
885b809074SNoralf Trønnes 		return 0; /* nothing to check when disabling or disabled */
895b809074SNoralf Trønnes 
905b809074SNoralf Trønnes 	clip.x2 = crtc_state->adjusted_mode.hdisplay;
915b809074SNoralf Trønnes 	clip.y2 = crtc_state->adjusted_mode.vdisplay;
924be12cc2SVille Syrjälä 
934be12cc2SVille Syrjälä 	ret = drm_plane_helper_check_state(plane_state, &clip,
945b809074SNoralf Trønnes 					   DRM_PLANE_HELPER_NO_SCALING,
955b809074SNoralf Trønnes 					   DRM_PLANE_HELPER_NO_SCALING,
964be12cc2SVille Syrjälä 					   false, true);
975b809074SNoralf Trønnes 	if (ret)
985b809074SNoralf Trønnes 		return ret;
995b809074SNoralf Trønnes 
1004be12cc2SVille Syrjälä 	if (!plane_state->visible)
1015b809074SNoralf Trønnes 		return -EINVAL;
1025b809074SNoralf Trønnes 
1035b809074SNoralf Trønnes 	if (!pipe->funcs || !pipe->funcs->check)
1045b809074SNoralf Trønnes 		return 0;
1055b809074SNoralf Trønnes 
1065b809074SNoralf Trønnes 	return pipe->funcs->check(pipe, plane_state, crtc_state);
1075b809074SNoralf Trønnes }
1085b809074SNoralf Trønnes 
1095b809074SNoralf Trønnes static void drm_simple_kms_plane_atomic_update(struct drm_plane *plane,
1105b809074SNoralf Trønnes 					struct drm_plane_state *pstate)
1115b809074SNoralf Trønnes {
1125b809074SNoralf Trønnes 	struct drm_simple_display_pipe *pipe;
1135b809074SNoralf Trønnes 
1145b809074SNoralf Trønnes 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
1155b809074SNoralf Trønnes 	if (!pipe->funcs || !pipe->funcs->update)
1165b809074SNoralf Trønnes 		return;
1175b809074SNoralf Trønnes 
1185b809074SNoralf Trønnes 	pipe->funcs->update(pipe, pstate);
1195b809074SNoralf Trønnes }
1205b809074SNoralf Trønnes 
1215b809074SNoralf Trønnes static const struct drm_plane_helper_funcs drm_simple_kms_plane_helper_funcs = {
1225b809074SNoralf Trønnes 	.atomic_check = drm_simple_kms_plane_atomic_check,
1235b809074SNoralf Trønnes 	.atomic_update = drm_simple_kms_plane_atomic_update,
1245b809074SNoralf Trønnes };
1255b809074SNoralf Trønnes 
1265b809074SNoralf Trønnes static const struct drm_plane_funcs drm_simple_kms_plane_funcs = {
1275b809074SNoralf Trønnes 	.update_plane		= drm_atomic_helper_update_plane,
1285b809074SNoralf Trønnes 	.disable_plane		= drm_atomic_helper_disable_plane,
1295b809074SNoralf Trønnes 	.destroy		= drm_plane_cleanup,
1305b809074SNoralf Trønnes 	.reset			= drm_atomic_helper_plane_reset,
1315b809074SNoralf Trønnes 	.atomic_duplicate_state	= drm_atomic_helper_plane_duplicate_state,
1325b809074SNoralf Trønnes 	.atomic_destroy_state	= drm_atomic_helper_plane_destroy_state,
1335b809074SNoralf Trønnes };
1345b809074SNoralf Trønnes 
1355b809074SNoralf Trønnes /**
1365b809074SNoralf Trønnes  * drm_simple_display_pipe_init - Initialize a simple display pipeline
1375b809074SNoralf Trønnes  * @dev: DRM device
1385b809074SNoralf Trønnes  * @pipe: simple display pipe object to initialize
1395b809074SNoralf Trønnes  * @funcs: callbacks for the display pipe (optional)
140*62cacc79SDaniel Vetter  * @formats: array of supported formats (DRM_FORMAT\_\*)
1415b809074SNoralf Trønnes  * @format_count: number of elements in @formats
1425b809074SNoralf Trønnes  * @connector: connector to attach and register
1435b809074SNoralf Trønnes  *
1445b809074SNoralf Trønnes  * Sets up a display pipeline which consist of a really simple
1455b809074SNoralf Trønnes  * plane-crtc-encoder pipe coupled with the provided connector.
1465b809074SNoralf Trønnes  * Teardown of a simple display pipe is all handled automatically by the drm
1475b809074SNoralf Trønnes  * core through calling drm_mode_config_cleanup(). Drivers afterwards need to
1485b809074SNoralf Trønnes  * release the memory for the structure themselves.
1495b809074SNoralf Trønnes  *
1505b809074SNoralf Trønnes  * Returns:
1515b809074SNoralf Trønnes  * Zero on success, negative error code on failure.
1525b809074SNoralf Trønnes  */
1535b809074SNoralf Trønnes int drm_simple_display_pipe_init(struct drm_device *dev,
1545b809074SNoralf Trønnes 			struct drm_simple_display_pipe *pipe,
1555b809074SNoralf Trønnes 			const struct drm_simple_display_pipe_funcs *funcs,
1565b809074SNoralf Trønnes 			const uint32_t *formats, unsigned int format_count,
1575b809074SNoralf Trønnes 			struct drm_connector *connector)
1585b809074SNoralf Trønnes {
1595b809074SNoralf Trønnes 	struct drm_encoder *encoder = &pipe->encoder;
1605b809074SNoralf Trønnes 	struct drm_plane *plane = &pipe->plane;
1615b809074SNoralf Trønnes 	struct drm_crtc *crtc = &pipe->crtc;
1625b809074SNoralf Trønnes 	int ret;
1635b809074SNoralf Trønnes 
1645b809074SNoralf Trønnes 	pipe->connector = connector;
1655b809074SNoralf Trønnes 	pipe->funcs = funcs;
1665b809074SNoralf Trønnes 
1675b809074SNoralf Trønnes 	drm_plane_helper_add(plane, &drm_simple_kms_plane_helper_funcs);
1685b809074SNoralf Trønnes 	ret = drm_universal_plane_init(dev, plane, 0,
1695b809074SNoralf Trønnes 				       &drm_simple_kms_plane_funcs,
1705b809074SNoralf Trønnes 				       formats, format_count,
1715b809074SNoralf Trønnes 				       DRM_PLANE_TYPE_PRIMARY, NULL);
1725b809074SNoralf Trønnes 	if (ret)
1735b809074SNoralf Trønnes 		return ret;
1745b809074SNoralf Trønnes 
1755b809074SNoralf Trønnes 	drm_crtc_helper_add(crtc, &drm_simple_kms_crtc_helper_funcs);
1765b809074SNoralf Trønnes 	ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
1775b809074SNoralf Trønnes 					&drm_simple_kms_crtc_funcs, NULL);
1785b809074SNoralf Trønnes 	if (ret)
1795b809074SNoralf Trønnes 		return ret;
1805b809074SNoralf Trønnes 
1815b809074SNoralf Trønnes 	encoder->possible_crtcs = 1 << drm_crtc_index(crtc);
1825b809074SNoralf Trønnes 	ret = drm_encoder_init(dev, encoder, &drm_simple_kms_encoder_funcs,
1835b809074SNoralf Trønnes 			       DRM_MODE_ENCODER_NONE, NULL);
1845b809074SNoralf Trønnes 	if (ret)
1855b809074SNoralf Trønnes 		return ret;
1865b809074SNoralf Trønnes 
1875b809074SNoralf Trønnes 	return drm_mode_connector_attach_encoder(connector, encoder);
1885b809074SNoralf Trønnes }
1895b809074SNoralf Trønnes EXPORT_SYMBOL(drm_simple_display_pipe_init);
1905b809074SNoralf Trønnes 
1915b809074SNoralf Trønnes MODULE_LICENSE("GPL");
192