1 /*
2  * Copyright (C) 2016 Noralf Trønnes
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  */
9 
10 #include <drm/drmP.h>
11 #include <drm/drm_atomic.h>
12 #include <drm/drm_atomic_helper.h>
13 #include <drm/drm_crtc_helper.h>
14 #include <drm/drm_plane_helper.h>
15 #include <drm/drm_simple_kms_helper.h>
16 #include <linux/slab.h>
17 
18 /**
19  * DOC: overview
20  *
21  * This helper library provides helpers for drivers for simple display
22  * hardware.
23  *
24  * drm_simple_display_pipe_init() initializes a simple display pipeline
25  * which has only one full-screen scanout buffer feeding one output. The
26  * pipeline is represented by &struct drm_simple_display_pipe and binds
27  * together &drm_plane, &drm_crtc and &drm_encoder structures into one fixed
28  * entity. Some flexibility for code reuse is provided through a separately
29  * allocated &drm_connector object and supporting optional &drm_bridge
30  * encoder drivers.
31  */
32 
33 static const struct drm_encoder_funcs drm_simple_kms_encoder_funcs = {
34 	.destroy = drm_encoder_cleanup,
35 };
36 
37 static int drm_simple_kms_crtc_check(struct drm_crtc *crtc,
38 				     struct drm_crtc_state *state)
39 {
40 	bool has_primary = state->plane_mask &
41 			   BIT(drm_plane_index(crtc->primary));
42 
43 	/* We always want to have an active plane with an active CRTC */
44 	if (has_primary != state->enable)
45 		return -EINVAL;
46 
47 	return drm_atomic_add_affected_planes(state->state, crtc);
48 }
49 
50 static void drm_simple_kms_crtc_enable(struct drm_crtc *crtc,
51 				       struct drm_crtc_state *old_state)
52 {
53 	struct drm_simple_display_pipe *pipe;
54 
55 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
56 	if (!pipe->funcs || !pipe->funcs->enable)
57 		return;
58 
59 	pipe->funcs->enable(pipe, crtc->state);
60 }
61 
62 static void drm_simple_kms_crtc_disable(struct drm_crtc *crtc,
63 					struct drm_crtc_state *old_state)
64 {
65 	struct drm_simple_display_pipe *pipe;
66 
67 	pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
68 	if (!pipe->funcs || !pipe->funcs->disable)
69 		return;
70 
71 	pipe->funcs->disable(pipe);
72 }
73 
74 static const struct drm_crtc_helper_funcs drm_simple_kms_crtc_helper_funcs = {
75 	.atomic_check = drm_simple_kms_crtc_check,
76 	.atomic_enable = drm_simple_kms_crtc_enable,
77 	.atomic_disable = drm_simple_kms_crtc_disable,
78 };
79 
80 static const struct drm_crtc_funcs drm_simple_kms_crtc_funcs = {
81 	.reset = drm_atomic_helper_crtc_reset,
82 	.destroy = drm_crtc_cleanup,
83 	.set_config = drm_atomic_helper_set_config,
84 	.page_flip = drm_atomic_helper_page_flip,
85 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
86 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
87 };
88 
89 static int drm_simple_kms_plane_atomic_check(struct drm_plane *plane,
90 					struct drm_plane_state *plane_state)
91 {
92 	struct drm_rect clip = { 0 };
93 	struct drm_simple_display_pipe *pipe;
94 	struct drm_crtc_state *crtc_state;
95 	int ret;
96 
97 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
98 	crtc_state = drm_atomic_get_new_crtc_state(plane_state->state,
99 						   &pipe->crtc);
100 	if (!crtc_state->enable)
101 		return 0; /* nothing to check when disabling or disabled */
102 
103 	if (crtc_state->enable)
104 		drm_mode_get_hv_timing(&crtc_state->mode,
105 				       &clip.x2, &clip.y2);
106 
107 	ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
108 						  &clip,
109 						  DRM_PLANE_HELPER_NO_SCALING,
110 						  DRM_PLANE_HELPER_NO_SCALING,
111 						  false, true);
112 	if (ret)
113 		return ret;
114 
115 	if (!plane_state->visible)
116 		return -EINVAL;
117 
118 	if (!pipe->funcs || !pipe->funcs->check)
119 		return 0;
120 
121 	return pipe->funcs->check(pipe, plane_state, crtc_state);
122 }
123 
124 static void drm_simple_kms_plane_atomic_update(struct drm_plane *plane,
125 					struct drm_plane_state *old_pstate)
126 {
127 	struct drm_simple_display_pipe *pipe;
128 
129 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
130 	if (!pipe->funcs || !pipe->funcs->update)
131 		return;
132 
133 	pipe->funcs->update(pipe, old_pstate);
134 }
135 
136 static int drm_simple_kms_plane_prepare_fb(struct drm_plane *plane,
137 					   struct drm_plane_state *state)
138 {
139 	struct drm_simple_display_pipe *pipe;
140 
141 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
142 	if (!pipe->funcs || !pipe->funcs->prepare_fb)
143 		return 0;
144 
145 	return pipe->funcs->prepare_fb(pipe, state);
146 }
147 
148 static void drm_simple_kms_plane_cleanup_fb(struct drm_plane *plane,
149 					    struct drm_plane_state *state)
150 {
151 	struct drm_simple_display_pipe *pipe;
152 
153 	pipe = container_of(plane, struct drm_simple_display_pipe, plane);
154 	if (!pipe->funcs || !pipe->funcs->cleanup_fb)
155 		return;
156 
157 	pipe->funcs->cleanup_fb(pipe, state);
158 }
159 
160 static const struct drm_plane_helper_funcs drm_simple_kms_plane_helper_funcs = {
161 	.prepare_fb = drm_simple_kms_plane_prepare_fb,
162 	.cleanup_fb = drm_simple_kms_plane_cleanup_fb,
163 	.atomic_check = drm_simple_kms_plane_atomic_check,
164 	.atomic_update = drm_simple_kms_plane_atomic_update,
165 };
166 
167 static const struct drm_plane_funcs drm_simple_kms_plane_funcs = {
168 	.update_plane		= drm_atomic_helper_update_plane,
169 	.disable_plane		= drm_atomic_helper_disable_plane,
170 	.destroy		= drm_plane_cleanup,
171 	.reset			= drm_atomic_helper_plane_reset,
172 	.atomic_duplicate_state	= drm_atomic_helper_plane_duplicate_state,
173 	.atomic_destroy_state	= drm_atomic_helper_plane_destroy_state,
174 };
175 
176 /**
177  * drm_simple_display_pipe_attach_bridge - Attach a bridge to the display pipe
178  * @pipe: simple display pipe object
179  * @bridge: bridge to attach
180  *
181  * Makes it possible to still use the drm_simple_display_pipe helpers when
182  * a DRM bridge has to be used.
183  *
184  * Note that you probably want to initialize the pipe by passing a NULL
185  * connector to drm_simple_display_pipe_init().
186  *
187  * Returns:
188  * Zero on success, negative error code on failure.
189  */
190 int drm_simple_display_pipe_attach_bridge(struct drm_simple_display_pipe *pipe,
191 					  struct drm_bridge *bridge)
192 {
193 	return drm_bridge_attach(&pipe->encoder, bridge, NULL);
194 }
195 EXPORT_SYMBOL(drm_simple_display_pipe_attach_bridge);
196 
197 /**
198  * drm_simple_display_pipe_init - Initialize a simple display pipeline
199  * @dev: DRM device
200  * @pipe: simple display pipe object to initialize
201  * @funcs: callbacks for the display pipe (optional)
202  * @formats: array of supported formats (DRM_FORMAT\_\*)
203  * @format_count: number of elements in @formats
204  * @format_modifiers: array of formats modifiers
205  * @connector: connector to attach and register (optional)
206  *
207  * Sets up a display pipeline which consist of a really simple
208  * plane-crtc-encoder pipe.
209  *
210  * If a connector is supplied, the pipe will be coupled with the provided
211  * connector. You may supply a NULL connector when using drm bridges, that
212  * handle connectors themselves (see drm_simple_display_pipe_attach_bridge()).
213  *
214  * Teardown of a simple display pipe is all handled automatically by the drm
215  * core through calling drm_mode_config_cleanup(). Drivers afterwards need to
216  * release the memory for the structure themselves.
217  *
218  * Returns:
219  * Zero on success, negative error code on failure.
220  */
221 int drm_simple_display_pipe_init(struct drm_device *dev,
222 			struct drm_simple_display_pipe *pipe,
223 			const struct drm_simple_display_pipe_funcs *funcs,
224 			const uint32_t *formats, unsigned int format_count,
225 			const uint64_t *format_modifiers,
226 			struct drm_connector *connector)
227 {
228 	struct drm_encoder *encoder = &pipe->encoder;
229 	struct drm_plane *plane = &pipe->plane;
230 	struct drm_crtc *crtc = &pipe->crtc;
231 	int ret;
232 
233 	pipe->connector = connector;
234 	pipe->funcs = funcs;
235 
236 	drm_plane_helper_add(plane, &drm_simple_kms_plane_helper_funcs);
237 	ret = drm_universal_plane_init(dev, plane, 0,
238 				       &drm_simple_kms_plane_funcs,
239 				       formats, format_count,
240 				       format_modifiers,
241 				       DRM_PLANE_TYPE_PRIMARY, NULL);
242 	if (ret)
243 		return ret;
244 
245 	drm_crtc_helper_add(crtc, &drm_simple_kms_crtc_helper_funcs);
246 	ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
247 					&drm_simple_kms_crtc_funcs, NULL);
248 	if (ret)
249 		return ret;
250 
251 	encoder->possible_crtcs = 1 << drm_crtc_index(crtc);
252 	ret = drm_encoder_init(dev, encoder, &drm_simple_kms_encoder_funcs,
253 			       DRM_MODE_ENCODER_NONE, NULL);
254 	if (ret || !connector)
255 		return ret;
256 
257 	return drm_mode_connector_attach_encoder(connector, encoder);
258 }
259 EXPORT_SYMBOL(drm_simple_display_pipe_init);
260 
261 MODULE_LICENSE("GPL");
262