1
2
3
4
5
6#include <drm/drmP.h>
7#include <drm/drm_atomic.h>
8#include <drm/drm_atomic_helper.h>
9#include <drm/drm_plane_helper.h>
10#include <drm/drm_probe_helper.h>
11#include <drm/drm_simple_kms_helper.h>
12#include <linux/slab.h>
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29static const struct drm_encoder_funcs drm_simple_kms_encoder_funcs = {
30 .destroy = drm_encoder_cleanup,
31};
32
33static enum drm_mode_status
34drm_simple_kms_crtc_mode_valid(struct drm_crtc *crtc,
35 const struct drm_display_mode *mode)
36{
37 struct drm_simple_display_pipe *pipe;
38
39 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
40 if (!pipe->funcs || !pipe->funcs->mode_valid)
41
42 return MODE_OK;
43
44 return pipe->funcs->mode_valid(crtc, mode);
45}
46
47static int drm_simple_kms_crtc_check(struct drm_crtc *crtc,
48 struct drm_crtc_state *state)
49{
50 bool has_primary = state->plane_mask &
51 drm_plane_mask(crtc->primary);
52
53
54 if (has_primary != state->enable)
55 return -EINVAL;
56
57 return drm_atomic_add_affected_planes(state->state, crtc);
58}
59
60static void drm_simple_kms_crtc_enable(struct drm_crtc *crtc,
61 struct drm_crtc_state *old_state)
62{
63 struct drm_plane *plane;
64 struct drm_simple_display_pipe *pipe;
65
66 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
67 if (!pipe->funcs || !pipe->funcs->enable)
68 return;
69
70 plane = &pipe->plane;
71 pipe->funcs->enable(pipe, crtc->state, plane->state);
72}
73
74static void drm_simple_kms_crtc_disable(struct drm_crtc *crtc,
75 struct drm_crtc_state *old_state)
76{
77 struct drm_simple_display_pipe *pipe;
78
79 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
80 if (!pipe->funcs || !pipe->funcs->disable)
81 return;
82
83 pipe->funcs->disable(pipe);
84}
85
86static const struct drm_crtc_helper_funcs drm_simple_kms_crtc_helper_funcs = {
87 .mode_valid = drm_simple_kms_crtc_mode_valid,
88 .atomic_check = drm_simple_kms_crtc_check,
89 .atomic_enable = drm_simple_kms_crtc_enable,
90 .atomic_disable = drm_simple_kms_crtc_disable,
91};
92
93static int drm_simple_kms_crtc_enable_vblank(struct drm_crtc *crtc)
94{
95 struct drm_simple_display_pipe *pipe;
96
97 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
98 if (!pipe->funcs || !pipe->funcs->enable_vblank)
99 return 0;
100
101 return pipe->funcs->enable_vblank(pipe);
102}
103
104static void drm_simple_kms_crtc_disable_vblank(struct drm_crtc *crtc)
105{
106 struct drm_simple_display_pipe *pipe;
107
108 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
109 if (!pipe->funcs || !pipe->funcs->disable_vblank)
110 return;
111
112 pipe->funcs->disable_vblank(pipe);
113}
114
115static const struct drm_crtc_funcs drm_simple_kms_crtc_funcs = {
116 .reset = drm_atomic_helper_crtc_reset,
117 .destroy = drm_crtc_cleanup,
118 .set_config = drm_atomic_helper_set_config,
119 .page_flip = drm_atomic_helper_page_flip,
120 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
121 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
122 .enable_vblank = drm_simple_kms_crtc_enable_vblank,
123 .disable_vblank = drm_simple_kms_crtc_disable_vblank,
124};
125
126static int drm_simple_kms_plane_atomic_check(struct drm_plane *plane,
127 struct drm_plane_state *plane_state)
128{
129 struct drm_simple_display_pipe *pipe;
130 struct drm_crtc_state *crtc_state;
131 int ret;
132
133 pipe = container_of(plane, struct drm_simple_display_pipe, plane);
134 crtc_state = drm_atomic_get_new_crtc_state(plane_state->state,
135 &pipe->crtc);
136
137 ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
138 DRM_PLANE_HELPER_NO_SCALING,
139 DRM_PLANE_HELPER_NO_SCALING,
140 false, true);
141 if (ret)
142 return ret;
143
144 if (!plane_state->visible)
145 return 0;
146
147 if (!pipe->funcs || !pipe->funcs->check)
148 return 0;
149
150 return pipe->funcs->check(pipe, plane_state, crtc_state);
151}
152
153static void drm_simple_kms_plane_atomic_update(struct drm_plane *plane,
154 struct drm_plane_state *old_pstate)
155{
156 struct drm_simple_display_pipe *pipe;
157
158 pipe = container_of(plane, struct drm_simple_display_pipe, plane);
159 if (!pipe->funcs || !pipe->funcs->update)
160 return;
161
162 pipe->funcs->update(pipe, old_pstate);
163}
164
165static int drm_simple_kms_plane_prepare_fb(struct drm_plane *plane,
166 struct drm_plane_state *state)
167{
168 struct drm_simple_display_pipe *pipe;
169
170 pipe = container_of(plane, struct drm_simple_display_pipe, plane);
171 if (!pipe->funcs || !pipe->funcs->prepare_fb)
172 return 0;
173
174 return pipe->funcs->prepare_fb(pipe, state);
175}
176
177static void drm_simple_kms_plane_cleanup_fb(struct drm_plane *plane,
178 struct drm_plane_state *state)
179{
180 struct drm_simple_display_pipe *pipe;
181
182 pipe = container_of(plane, struct drm_simple_display_pipe, plane);
183 if (!pipe->funcs || !pipe->funcs->cleanup_fb)
184 return;
185
186 pipe->funcs->cleanup_fb(pipe, state);
187}
188
189static bool drm_simple_kms_format_mod_supported(struct drm_plane *plane,
190 uint32_t format,
191 uint64_t modifier)
192{
193 return modifier == DRM_FORMAT_MOD_LINEAR;
194}
195
196static const struct drm_plane_helper_funcs drm_simple_kms_plane_helper_funcs = {
197 .prepare_fb = drm_simple_kms_plane_prepare_fb,
198 .cleanup_fb = drm_simple_kms_plane_cleanup_fb,
199 .atomic_check = drm_simple_kms_plane_atomic_check,
200 .atomic_update = drm_simple_kms_plane_atomic_update,
201};
202
203static const struct drm_plane_funcs drm_simple_kms_plane_funcs = {
204 .update_plane = drm_atomic_helper_update_plane,
205 .disable_plane = drm_atomic_helper_disable_plane,
206 .destroy = drm_plane_cleanup,
207 .reset = drm_atomic_helper_plane_reset,
208 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
209 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
210 .format_mod_supported = drm_simple_kms_format_mod_supported,
211};
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227int drm_simple_display_pipe_attach_bridge(struct drm_simple_display_pipe *pipe,
228 struct drm_bridge *bridge)
229{
230 return drm_bridge_attach(&pipe->encoder, bridge, NULL);
231}
232EXPORT_SYMBOL(drm_simple_display_pipe_attach_bridge);
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258int drm_simple_display_pipe_init(struct drm_device *dev,
259 struct drm_simple_display_pipe *pipe,
260 const struct drm_simple_display_pipe_funcs *funcs,
261 const uint32_t *formats, unsigned int format_count,
262 const uint64_t *format_modifiers,
263 struct drm_connector *connector)
264{
265 struct drm_encoder *encoder = &pipe->encoder;
266 struct drm_plane *plane = &pipe->plane;
267 struct drm_crtc *crtc = &pipe->crtc;
268 int ret;
269
270 pipe->connector = connector;
271 pipe->funcs = funcs;
272
273 drm_plane_helper_add(plane, &drm_simple_kms_plane_helper_funcs);
274 ret = drm_universal_plane_init(dev, plane, 0,
275 &drm_simple_kms_plane_funcs,
276 formats, format_count,
277 format_modifiers,
278 DRM_PLANE_TYPE_PRIMARY, NULL);
279 if (ret)
280 return ret;
281
282 drm_crtc_helper_add(crtc, &drm_simple_kms_crtc_helper_funcs);
283 ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
284 &drm_simple_kms_crtc_funcs, NULL);
285 if (ret)
286 return ret;
287
288 encoder->possible_crtcs = drm_crtc_mask(crtc);
289 ret = drm_encoder_init(dev, encoder, &drm_simple_kms_encoder_funcs,
290 DRM_MODE_ENCODER_NONE, NULL);
291 if (ret || !connector)
292 return ret;
293
294 return drm_connector_attach_encoder(connector, encoder);
295}
296EXPORT_SYMBOL(drm_simple_display_pipe_init);
297
298MODULE_LICENSE("GPL");
299