]> git.ipfire.org Git - people/arne_f/kernel.git/blame - drivers/gpu/drm/drm_encoder.c
Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[people/arne_f/kernel.git] / drivers / gpu / drm / drm_encoder.c
CommitLineData
321a95ae
DV
1/*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include <linux/export.h>
24#include <drm/drmP.h>
25#include <drm/drm_encoder.h>
26
27#include "drm_crtc_internal.h"
28
e03e6de0
DV
29/**
30 * DOC: overview
31 *
32 * Encoders represent the connecting element between the CRTC (as the overall
ea0dd85a
DV
33 * pixel pipeline, represented by &struct drm_crtc) and the connectors (as the
34 * generic sink entity, represented by &struct drm_connector). An encoder takes
f15e6bb0
DP
35 * pixel data from a CRTC and converts it to a format suitable for any attached
36 * connector. Encoders are objects exposed to userspace, originally to allow
37 * userspace to infer cloning and connector/CRTC restrictions. Unfortunately
38 * almost all drivers get this wrong, making the uabi pretty much useless. On
39 * top of that the exposed restrictions are too simple for today's hardware, and
40 * the recommended way to infer restrictions is by using the
41 * DRM_MODE_ATOMIC_TEST_ONLY flag for the atomic IOCTL.
e03e6de0
DV
42 *
43 * Otherwise encoders aren't used in the uapi at all (any modeset request from
44 * userspace directly connects a connector with a CRTC), drivers are therefore
45 * free to use them however they wish. Modeset helper libraries make strong use
46 * of encoders to facilitate code sharing. But for more complex settings it is
47 * usually better to move shared code into a separate &drm_bridge. Compared to
f15e6bb0 48 * encoders, bridges also have the benefit of being purely an internal
e03e6de0
DV
49 * abstraction since they are not exposed to userspace at all.
50 *
51 * Encoders are initialized with drm_encoder_init() and cleaned up using
52 * drm_encoder_cleanup().
53 */
321a95ae
DV
54static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
55 { DRM_MODE_ENCODER_NONE, "None" },
56 { DRM_MODE_ENCODER_DAC, "DAC" },
57 { DRM_MODE_ENCODER_TMDS, "TMDS" },
58 { DRM_MODE_ENCODER_LVDS, "LVDS" },
59 { DRM_MODE_ENCODER_TVDAC, "TV" },
60 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
61 { DRM_MODE_ENCODER_DSI, "DSI" },
62 { DRM_MODE_ENCODER_DPMST, "DP MST" },
63 { DRM_MODE_ENCODER_DPI, "DPI" },
64};
65
66int drm_encoder_register_all(struct drm_device *dev)
67{
68 struct drm_encoder *encoder;
69 int ret = 0;
70
71 drm_for_each_encoder(encoder, dev) {
72 if (encoder->funcs->late_register)
73 ret = encoder->funcs->late_register(encoder);
74 if (ret)
75 return ret;
76 }
77
78 return 0;
79}
80
81void drm_encoder_unregister_all(struct drm_device *dev)
82{
83 struct drm_encoder *encoder;
84
85 drm_for_each_encoder(encoder, dev) {
86 if (encoder->funcs->early_unregister)
87 encoder->funcs->early_unregister(encoder);
88 }
89}
90
91/**
92 * drm_encoder_init - Init a preallocated encoder
93 * @dev: drm device
94 * @encoder: the encoder to init
95 * @funcs: callbacks for this encoder
96 * @encoder_type: user visible type of the encoder
97 * @name: printf style format string for the encoder name, or NULL for default name
98 *
e03e6de0
DV
99 * Initialises a preallocated encoder. Encoder should be subclassed as part of
100 * driver encoder objects. At driver unload time drm_encoder_cleanup() should be
d574528a 101 * called from the driver's &drm_encoder_funcs.destroy hook.
321a95ae
DV
102 *
103 * Returns:
104 * Zero on success, error code on failure.
105 */
106int drm_encoder_init(struct drm_device *dev,
e03e6de0
DV
107 struct drm_encoder *encoder,
108 const struct drm_encoder_funcs *funcs,
109 int encoder_type, const char *name, ...)
321a95ae
DV
110{
111 int ret;
112
2135ea7a 113 ret = drm_mode_object_add(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
321a95ae 114 if (ret)
661a3755 115 return ret;
321a95ae
DV
116
117 encoder->dev = dev;
118 encoder->encoder_type = encoder_type;
119 encoder->funcs = funcs;
120 if (name) {
121 va_list ap;
122
123 va_start(ap, name);
124 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
125 va_end(ap);
126 } else {
127 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
128 drm_encoder_enum_list[encoder_type].name,
129 encoder->base.id);
130 }
131 if (!encoder->name) {
132 ret = -ENOMEM;
133 goto out_put;
134 }
135
136 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
137 encoder->index = dev->mode_config.num_encoder++;
138
139out_put:
140 if (ret)
141 drm_mode_object_unregister(dev, &encoder->base);
142
321a95ae
DV
143 return ret;
144}
145EXPORT_SYMBOL(drm_encoder_init);
146
147/**
148 * drm_encoder_cleanup - cleans up an initialised encoder
149 * @encoder: encoder to cleanup
150 *
151 * Cleans up the encoder but doesn't free the object.
152 */
153void drm_encoder_cleanup(struct drm_encoder *encoder)
154{
155 struct drm_device *dev = encoder->dev;
156
157 /* Note that the encoder_list is considered to be static; should we
158 * remove the drm_encoder at runtime we would have to decrement all
159 * the indices on the drm_encoder after us in the encoder_list.
160 */
161
6e15174e
LP
162 if (encoder->bridge) {
163 struct drm_bridge *bridge = encoder->bridge;
164 struct drm_bridge *next;
165
166 while (bridge) {
167 next = bridge->next;
168 drm_bridge_detach(bridge);
169 bridge = next;
170 }
171 }
4a878c03 172
321a95ae
DV
173 drm_mode_object_unregister(dev, &encoder->base);
174 kfree(encoder->name);
175 list_del(&encoder->head);
176 dev->mode_config.num_encoder--;
321a95ae
DV
177
178 memset(encoder, 0, sizeof(*encoder));
179}
180EXPORT_SYMBOL(drm_encoder_cleanup);
181
182static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
183{
184 struct drm_connector *connector;
185 struct drm_device *dev = encoder->dev;
186 bool uses_atomic = false;
613051da 187 struct drm_connector_list_iter conn_iter;
321a95ae
DV
188
189 /* For atomic drivers only state objects are synchronously updated and
190 * protected by modeset locks, so check those first. */
b982dab1 191 drm_connector_list_iter_begin(dev, &conn_iter);
613051da 192 drm_for_each_connector_iter(connector, &conn_iter) {
321a95ae
DV
193 if (!connector->state)
194 continue;
195
196 uses_atomic = true;
197
198 if (connector->state->best_encoder != encoder)
199 continue;
200
b982dab1 201 drm_connector_list_iter_end(&conn_iter);
321a95ae
DV
202 return connector->state->crtc;
203 }
b982dab1 204 drm_connector_list_iter_end(&conn_iter);
321a95ae
DV
205
206 /* Don't return stale data (e.g. pending async disable). */
207 if (uses_atomic)
208 return NULL;
209
210 return encoder->crtc;
211}
212
321a95ae
DV
213int drm_mode_getencoder(struct drm_device *dev, void *data,
214 struct drm_file *file_priv)
215{
216 struct drm_mode_get_encoder *enc_resp = data;
217 struct drm_encoder *encoder;
218 struct drm_crtc *crtc;
219
220 if (!drm_core_check_feature(dev, DRIVER_MODESET))
221 return -EINVAL;
222
223 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
224 if (!encoder)
225 return -ENOENT;
226
227 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
228 crtc = drm_encoder_get_crtc(encoder);
229 if (crtc)
230 enc_resp->crtc_id = crtc->base.id;
231 else
232 enc_resp->crtc_id = 0;
233 drm_modeset_unlock(&dev->mode_config.connection_mutex);
234
235 enc_resp->encoder_type = encoder->encoder_type;
236 enc_resp->encoder_id = encoder->base.id;
237 enc_resp->possible_crtcs = encoder->possible_crtcs;
238 enc_resp->possible_clones = encoder->possible_clones;
239
240 return 0;
241}