blob: 273e1c59c54a0e98d8a0738dfae58e8588f1668e [file] [log] [blame]
Daniel Vetter321a95a2016-08-29 10:27:49 +02001/*
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
Daniel Vettere03e6de2016-08-29 10:27:50 +020029/**
30 * DOC: overview
31 *
32 * Encoders represent the connecting element between the CRTC (as the overall
Daniel Vetterea0dd852016-12-29 21:48:26 +010033 * pixel pipeline, represented by &struct drm_crtc) and the connectors (as the
34 * generic sink entity, represented by &struct drm_connector). An encoder takes
Dhinakaran Pandiyanf15e6bb2016-09-19 15:40:48 -070035 * 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.
Daniel Vettere03e6de2016-08-29 10:27:50 +020042 *
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
Dhinakaran Pandiyanf15e6bb2016-09-19 15:40:48 -070048 * encoders, bridges also have the benefit of being purely an internal
Daniel Vettere03e6de2016-08-29 10:27:50 +020049 * 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 */
Daniel Vetter321a95a2016-08-29 10:27:49 +020054static 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 *
Daniel Vettere03e6de2016-08-29 10:27:50 +020099 * Initialises a preallocated encoder. Encoder should be subclassed as part of
100 * driver encoder objects. At driver unload time drm_encoder_cleanup() should be
Daniel Vetterd5745282017-01-25 07:26:45 +0100101 * called from the driver's &drm_encoder_funcs.destroy hook.
Daniel Vetter321a95a2016-08-29 10:27:49 +0200102 *
103 * Returns:
104 * Zero on success, error code on failure.
105 */
106int drm_encoder_init(struct drm_device *dev,
Daniel Vettere03e6de2016-08-29 10:27:50 +0200107 struct drm_encoder *encoder,
108 const struct drm_encoder_funcs *funcs,
109 int encoder_type, const char *name, ...)
Daniel Vetter321a95a2016-08-29 10:27:49 +0200110{
111 int ret;
112
Ville Syrjälä2a8d3ea2018-01-25 15:30:20 +0200113 /* encoder index is used with 32bit bitmasks */
114 if (WARN_ON(dev->mode_config.num_encoder >= 32))
115 return -EINVAL;
116
Thierry Reding2135ea72017-02-28 15:46:37 +0100117 ret = drm_mode_object_add(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
Daniel Vetter321a95a2016-08-29 10:27:49 +0200118 if (ret)
Daniel Vetter661a3752016-11-29 10:45:38 +0100119 return ret;
Daniel Vetter321a95a2016-08-29 10:27:49 +0200120
121 encoder->dev = dev;
122 encoder->encoder_type = encoder_type;
123 encoder->funcs = funcs;
124 if (name) {
125 va_list ap;
126
127 va_start(ap, name);
128 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
129 va_end(ap);
130 } else {
131 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
132 drm_encoder_enum_list[encoder_type].name,
133 encoder->base.id);
134 }
135 if (!encoder->name) {
136 ret = -ENOMEM;
137 goto out_put;
138 }
139
140 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
141 encoder->index = dev->mode_config.num_encoder++;
142
143out_put:
144 if (ret)
145 drm_mode_object_unregister(dev, &encoder->base);
146
Daniel Vetter321a95a2016-08-29 10:27:49 +0200147 return ret;
148}
149EXPORT_SYMBOL(drm_encoder_init);
150
151/**
152 * drm_encoder_cleanup - cleans up an initialised encoder
153 * @encoder: encoder to cleanup
154 *
155 * Cleans up the encoder but doesn't free the object.
156 */
157void drm_encoder_cleanup(struct drm_encoder *encoder)
158{
159 struct drm_device *dev = encoder->dev;
160
161 /* Note that the encoder_list is considered to be static; should we
162 * remove the drm_encoder at runtime we would have to decrement all
163 * the indices on the drm_encoder after us in the encoder_list.
164 */
165
Laurent Pinchart6e151742016-11-29 20:55:44 +0200166 if (encoder->bridge) {
167 struct drm_bridge *bridge = encoder->bridge;
168 struct drm_bridge *next;
169
170 while (bridge) {
171 next = bridge->next;
172 drm_bridge_detach(bridge);
173 bridge = next;
174 }
175 }
Laurent Pinchart4a878c02016-11-28 18:32:05 +0200176
Daniel Vetter321a95a2016-08-29 10:27:49 +0200177 drm_mode_object_unregister(dev, &encoder->base);
178 kfree(encoder->name);
179 list_del(&encoder->head);
180 dev->mode_config.num_encoder--;
Daniel Vetter321a95a2016-08-29 10:27:49 +0200181
182 memset(encoder, 0, sizeof(*encoder));
183}
184EXPORT_SYMBOL(drm_encoder_cleanup);
185
186static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
187{
188 struct drm_connector *connector;
189 struct drm_device *dev = encoder->dev;
190 bool uses_atomic = false;
Daniel Vetter613051d2016-12-14 00:08:06 +0100191 struct drm_connector_list_iter conn_iter;
Daniel Vetter321a95a2016-08-29 10:27:49 +0200192
193 /* For atomic drivers only state objects are synchronously updated and
194 * protected by modeset locks, so check those first. */
Thierry Redingb982dab2017-02-28 15:46:43 +0100195 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetter613051d2016-12-14 00:08:06 +0100196 drm_for_each_connector_iter(connector, &conn_iter) {
Daniel Vetter321a95a2016-08-29 10:27:49 +0200197 if (!connector->state)
198 continue;
199
200 uses_atomic = true;
201
202 if (connector->state->best_encoder != encoder)
203 continue;
204
Thierry Redingb982dab2017-02-28 15:46:43 +0100205 drm_connector_list_iter_end(&conn_iter);
Daniel Vetter321a95a2016-08-29 10:27:49 +0200206 return connector->state->crtc;
207 }
Thierry Redingb982dab2017-02-28 15:46:43 +0100208 drm_connector_list_iter_end(&conn_iter);
Daniel Vetter321a95a2016-08-29 10:27:49 +0200209
210 /* Don't return stale data (e.g. pending async disable). */
211 if (uses_atomic)
212 return NULL;
213
214 return encoder->crtc;
215}
216
Daniel Vetter321a95a2016-08-29 10:27:49 +0200217int drm_mode_getencoder(struct drm_device *dev, void *data,
218 struct drm_file *file_priv)
219{
220 struct drm_mode_get_encoder *enc_resp = data;
221 struct drm_encoder *encoder;
222 struct drm_crtc *crtc;
223
224 if (!drm_core_check_feature(dev, DRIVER_MODESET))
225 return -EINVAL;
226
Keith Packard418da172017-03-14 23:25:07 -0700227 encoder = drm_encoder_find(dev, file_priv, enc_resp->encoder_id);
Daniel Vetter321a95a2016-08-29 10:27:49 +0200228 if (!encoder)
229 return -ENOENT;
230
231 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
232 crtc = drm_encoder_get_crtc(encoder);
Keith Packard7de440d2017-04-09 22:35:34 -0600233 if (crtc && drm_lease_held(file_priv, crtc->base.id))
Daniel Vetter321a95a2016-08-29 10:27:49 +0200234 enc_resp->crtc_id = crtc->base.id;
235 else
236 enc_resp->crtc_id = 0;
237 drm_modeset_unlock(&dev->mode_config.connection_mutex);
238
239 enc_resp->encoder_type = encoder->encoder_type;
240 enc_resp->encoder_id = encoder->base.id;
Keith Packard7de440d2017-04-09 22:35:34 -0600241 enc_resp->possible_crtcs = drm_lease_filter_crtcs(file_priv,
242 encoder->possible_crtcs);
Daniel Vetter321a95a2016-08-29 10:27:49 +0200243 enc_resp->possible_clones = encoder->possible_clones;
244
245 return 0;
246}