blob: 58c27ab1756f46fdbd4289b9eebc3eb3e6a77946 [file] [log] [blame]
Noralf Trønnes5b809072016-06-10 16:55:59 +02001/*
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
Daniel Vetterea0dd852016-12-29 21:48:26 +010026 * pipeline is represented by &struct drm_simple_display_pipe and binds
Noralf Trønnes5b809072016-06-10 16:55:59 +020027 * 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
33static const struct drm_encoder_funcs drm_simple_kms_encoder_funcs = {
34 .destroy = drm_encoder_cleanup,
35};
36
Daniel Vetter6dcf0de2016-08-23 08:25:40 +020037static int drm_simple_kms_crtc_check(struct drm_crtc *crtc,
38 struct drm_crtc_state *state)
39{
40 return drm_atomic_add_affected_planes(state->state, crtc);
41}
42
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +030043static void drm_simple_kms_crtc_enable(struct drm_crtc *crtc,
44 struct drm_crtc_state *old_state)
Noralf Trønnes5b809072016-06-10 16:55:59 +020045{
46 struct drm_simple_display_pipe *pipe;
47
48 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
49 if (!pipe->funcs || !pipe->funcs->enable)
50 return;
51
52 pipe->funcs->enable(pipe, crtc->state);
53}
54
55static void drm_simple_kms_crtc_disable(struct drm_crtc *crtc)
56{
57 struct drm_simple_display_pipe *pipe;
58
59 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
60 if (!pipe->funcs || !pipe->funcs->disable)
61 return;
62
63 pipe->funcs->disable(pipe);
64}
65
66static const struct drm_crtc_helper_funcs drm_simple_kms_crtc_helper_funcs = {
Daniel Vetter6dcf0de2016-08-23 08:25:40 +020067 .atomic_check = drm_simple_kms_crtc_check,
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +030068 .atomic_enable = drm_simple_kms_crtc_enable,
Noralf Trønnes5b809072016-06-10 16:55:59 +020069 .disable = drm_simple_kms_crtc_disable,
Noralf Trønnes5b809072016-06-10 16:55:59 +020070};
71
72static const struct drm_crtc_funcs drm_simple_kms_crtc_funcs = {
73 .reset = drm_atomic_helper_crtc_reset,
74 .destroy = drm_crtc_cleanup,
75 .set_config = drm_atomic_helper_set_config,
76 .page_flip = drm_atomic_helper_page_flip,
77 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
78 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
79};
80
81static int drm_simple_kms_plane_atomic_check(struct drm_plane *plane,
82 struct drm_plane_state *plane_state)
83{
Noralf Trønnes5b809072016-06-10 16:55:59 +020084 struct drm_rect clip = { 0 };
85 struct drm_simple_display_pipe *pipe;
86 struct drm_crtc_state *crtc_state;
Noralf Trønnes5b809072016-06-10 16:55:59 +020087 int ret;
88
89 pipe = container_of(plane, struct drm_simple_display_pipe, plane);
Maarten Lankhorstb4d93672017-03-01 10:22:10 +010090 crtc_state = drm_atomic_get_new_crtc_state(plane_state->state,
91 &pipe->crtc);
Noralf Trønnes5b809072016-06-10 16:55:59 +020092 if (crtc_state->enable != !!plane_state->crtc)
93 return -EINVAL; /* plane must match crtc enable state */
94
95 if (!crtc_state->enable)
96 return 0; /* nothing to check when disabling or disabled */
97
98 clip.x2 = crtc_state->adjusted_mode.hdisplay;
99 clip.y2 = crtc_state->adjusted_mode.vdisplay;
Ville Syrjälä4be12cc2016-07-26 19:07:04 +0300100
101 ret = drm_plane_helper_check_state(plane_state, &clip,
102 DRM_PLANE_HELPER_NO_SCALING,
103 DRM_PLANE_HELPER_NO_SCALING,
104 false, true);
Noralf Trønnes5b809072016-06-10 16:55:59 +0200105 if (ret)
106 return ret;
107
Ville Syrjälä4be12cc2016-07-26 19:07:04 +0300108 if (!plane_state->visible)
Noralf Trønnes5b809072016-06-10 16:55:59 +0200109 return -EINVAL;
110
111 if (!pipe->funcs || !pipe->funcs->check)
112 return 0;
113
114 return pipe->funcs->check(pipe, plane_state, crtc_state);
115}
116
117static void drm_simple_kms_plane_atomic_update(struct drm_plane *plane,
Eric Anholtbcd2ba02017-03-20 16:36:15 -0700118 struct drm_plane_state *old_pstate)
Noralf Trønnes5b809072016-06-10 16:55:59 +0200119{
120 struct drm_simple_display_pipe *pipe;
121
122 pipe = container_of(plane, struct drm_simple_display_pipe, plane);
123 if (!pipe->funcs || !pipe->funcs->update)
124 return;
125
Eric Anholtbcd2ba02017-03-20 16:36:15 -0700126 pipe->funcs->update(pipe, old_pstate);
Noralf Trønnes5b809072016-06-10 16:55:59 +0200127}
128
Marek Vasut7d83a152016-10-02 19:01:24 +0200129static int drm_simple_kms_plane_prepare_fb(struct drm_plane *plane,
130 struct drm_plane_state *state)
131{
132 struct drm_simple_display_pipe *pipe;
133
134 pipe = container_of(plane, struct drm_simple_display_pipe, plane);
135 if (!pipe->funcs || !pipe->funcs->prepare_fb)
136 return 0;
137
138 return pipe->funcs->prepare_fb(pipe, state);
139}
140
141static void drm_simple_kms_plane_cleanup_fb(struct drm_plane *plane,
142 struct drm_plane_state *state)
143{
144 struct drm_simple_display_pipe *pipe;
145
146 pipe = container_of(plane, struct drm_simple_display_pipe, plane);
147 if (!pipe->funcs || !pipe->funcs->cleanup_fb)
148 return;
149
150 pipe->funcs->cleanup_fb(pipe, state);
151}
152
Noralf Trønnes5b809072016-06-10 16:55:59 +0200153static const struct drm_plane_helper_funcs drm_simple_kms_plane_helper_funcs = {
Marek Vasut7d83a152016-10-02 19:01:24 +0200154 .prepare_fb = drm_simple_kms_plane_prepare_fb,
155 .cleanup_fb = drm_simple_kms_plane_cleanup_fb,
Noralf Trønnes5b809072016-06-10 16:55:59 +0200156 .atomic_check = drm_simple_kms_plane_atomic_check,
157 .atomic_update = drm_simple_kms_plane_atomic_update,
158};
159
160static const struct drm_plane_funcs drm_simple_kms_plane_funcs = {
161 .update_plane = drm_atomic_helper_update_plane,
162 .disable_plane = drm_atomic_helper_disable_plane,
163 .destroy = drm_plane_cleanup,
164 .reset = drm_atomic_helper_plane_reset,
165 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
166 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
167};
168
169/**
Andrea Merello315486c2016-08-25 11:04:34 +0200170 * drm_simple_display_pipe_attach_bridge - Attach a bridge to the display pipe
171 * @pipe: simple display pipe object
172 * @bridge: bridge to attach
173 *
174 * Makes it possible to still use the drm_simple_display_pipe helpers when
175 * a DRM bridge has to be used.
176 *
177 * Note that you probably want to initialize the pipe by passing a NULL
178 * connector to drm_simple_display_pipe_init().
179 *
180 * Returns:
181 * Zero on success, negative error code on failure.
182 */
183int drm_simple_display_pipe_attach_bridge(struct drm_simple_display_pipe *pipe,
184 struct drm_bridge *bridge)
185{
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200186 return drm_bridge_attach(&pipe->encoder, bridge, NULL);
Andrea Merello315486c2016-08-25 11:04:34 +0200187}
188EXPORT_SYMBOL(drm_simple_display_pipe_attach_bridge);
189
190/**
Noralf Trønnes5b809072016-06-10 16:55:59 +0200191 * drm_simple_display_pipe_init - Initialize a simple display pipeline
192 * @dev: DRM device
193 * @pipe: simple display pipe object to initialize
194 * @funcs: callbacks for the display pipe (optional)
Daniel Vetter62cacc72016-08-12 22:48:37 +0200195 * @formats: array of supported formats (DRM_FORMAT\_\*)
Noralf Trønnes5b809072016-06-10 16:55:59 +0200196 * @format_count: number of elements in @formats
Andrea Merello4f993972016-08-25 11:04:33 +0200197 * @connector: connector to attach and register (optional)
Noralf Trønnes5b809072016-06-10 16:55:59 +0200198 *
199 * Sets up a display pipeline which consist of a really simple
Andrea Merello4f993972016-08-25 11:04:33 +0200200 * plane-crtc-encoder pipe.
201 *
202 * If a connector is supplied, the pipe will be coupled with the provided
203 * connector. You may supply a NULL connector when using drm bridges, that
204 * handle connectors themselves (see drm_simple_display_pipe_attach_bridge()).
205 *
Noralf Trønnes5b809072016-06-10 16:55:59 +0200206 * Teardown of a simple display pipe is all handled automatically by the drm
207 * core through calling drm_mode_config_cleanup(). Drivers afterwards need to
208 * release the memory for the structure themselves.
209 *
210 * Returns:
211 * Zero on success, negative error code on failure.
212 */
213int drm_simple_display_pipe_init(struct drm_device *dev,
214 struct drm_simple_display_pipe *pipe,
215 const struct drm_simple_display_pipe_funcs *funcs,
216 const uint32_t *formats, unsigned int format_count,
217 struct drm_connector *connector)
218{
219 struct drm_encoder *encoder = &pipe->encoder;
220 struct drm_plane *plane = &pipe->plane;
221 struct drm_crtc *crtc = &pipe->crtc;
222 int ret;
223
224 pipe->connector = connector;
225 pipe->funcs = funcs;
226
227 drm_plane_helper_add(plane, &drm_simple_kms_plane_helper_funcs);
228 ret = drm_universal_plane_init(dev, plane, 0,
229 &drm_simple_kms_plane_funcs,
230 formats, format_count,
231 DRM_PLANE_TYPE_PRIMARY, NULL);
232 if (ret)
233 return ret;
234
235 drm_crtc_helper_add(crtc, &drm_simple_kms_crtc_helper_funcs);
236 ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
237 &drm_simple_kms_crtc_funcs, NULL);
238 if (ret)
239 return ret;
240
241 encoder->possible_crtcs = 1 << drm_crtc_index(crtc);
242 ret = drm_encoder_init(dev, encoder, &drm_simple_kms_encoder_funcs,
243 DRM_MODE_ENCODER_NONE, NULL);
Andrea Merello4f993972016-08-25 11:04:33 +0200244 if (ret || !connector)
Noralf Trønnes5b809072016-06-10 16:55:59 +0200245 return ret;
246
247 return drm_mode_connector_attach_encoder(connector, encoder);
248}
249EXPORT_SYMBOL(drm_simple_display_pipe_init);
250
251MODULE_LICENSE("GPL");