2 * Copyright (c) 2016 Intel Corporation
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.
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
23 #include <linux/export.h>
25 #include <drm/drm_encoder.h>
27 #include "drm_crtc_internal.h"
32 * Encoders represent the connecting element between the CRTC (as the overall
33 * pixel pipeline, represented by struct &drm_crtc) and the connectors (as the
34 * generic sink entity, represented by struct &drm_connector). Encoders are
35 * objects exposed to userspace, originally to allow userspace to infer cloning
36 * and connector/CRTC restrictions. Unfortunately almost all drivers get this
37 * wrong, making the uabi pretty much useless. On top of that the exposed
38 * restrictions are too simple for todays hardware, and the recommend way to
39 * infer restrictions is by using the DRM_MODE_ATOMIC_TEST_ONLY flag for the
42 * Otherwise encoders aren't used in the uapi at all (any modeset request from
43 * userspace directly connects a connector with a CRTC), drivers are therefore
44 * free to use them however they wish. Modeset helper libraries make strong use
45 * of encoders to facilitate code sharing. But for more complex settings it is
46 * usually better to move shared code into a separate &drm_bridge. Compared to
47 * encoders bridges also have the benefit of not being purely an internal
48 * abstraction since they are not exposed to userspace at all.
50 * Encoders are initialized with drm_encoder_init() and cleaned up using
51 * drm_encoder_cleanup().
53 static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
54 { DRM_MODE_ENCODER_NONE, "None" },
55 { DRM_MODE_ENCODER_DAC, "DAC" },
56 { DRM_MODE_ENCODER_TMDS, "TMDS" },
57 { DRM_MODE_ENCODER_LVDS, "LVDS" },
58 { DRM_MODE_ENCODER_TVDAC, "TV" },
59 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
60 { DRM_MODE_ENCODER_DSI, "DSI" },
61 { DRM_MODE_ENCODER_DPMST, "DP MST" },
62 { DRM_MODE_ENCODER_DPI, "DPI" },
65 int drm_encoder_register_all(struct drm_device *dev)
67 struct drm_encoder *encoder;
70 drm_for_each_encoder(encoder, dev) {
71 if (encoder->funcs->late_register)
72 ret = encoder->funcs->late_register(encoder);
80 void drm_encoder_unregister_all(struct drm_device *dev)
82 struct drm_encoder *encoder;
84 drm_for_each_encoder(encoder, dev) {
85 if (encoder->funcs->early_unregister)
86 encoder->funcs->early_unregister(encoder);
91 * drm_encoder_init - Init a preallocated encoder
93 * @encoder: the encoder to init
94 * @funcs: callbacks for this encoder
95 * @encoder_type: user visible type of the encoder
96 * @name: printf style format string for the encoder name, or NULL for default name
98 * Initialises a preallocated encoder. Encoder should be subclassed as part of
99 * driver encoder objects. At driver unload time drm_encoder_cleanup() should be
100 * called from the driver's destroy hook in &drm_encoder_funcs.
103 * Zero on success, error code on failure.
105 int drm_encoder_init(struct drm_device *dev,
106 struct drm_encoder *encoder,
107 const struct drm_encoder_funcs *funcs,
108 int encoder_type, const char *name, ...)
112 drm_modeset_lock_all(dev);
114 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
119 encoder->encoder_type = encoder_type;
120 encoder->funcs = funcs;
125 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
128 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
129 drm_encoder_enum_list[encoder_type].name,
132 if (!encoder->name) {
137 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
138 encoder->index = dev->mode_config.num_encoder++;
142 drm_mode_object_unregister(dev, &encoder->base);
145 drm_modeset_unlock_all(dev);
149 EXPORT_SYMBOL(drm_encoder_init);
152 * drm_encoder_cleanup - cleans up an initialised encoder
153 * @encoder: encoder to cleanup
155 * Cleans up the encoder but doesn't free the object.
157 void drm_encoder_cleanup(struct drm_encoder *encoder)
159 struct drm_device *dev = encoder->dev;
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.
166 drm_modeset_lock_all(dev);
167 drm_mode_object_unregister(dev, &encoder->base);
168 kfree(encoder->name);
169 list_del(&encoder->head);
170 dev->mode_config.num_encoder--;
171 drm_modeset_unlock_all(dev);
173 memset(encoder, 0, sizeof(*encoder));
175 EXPORT_SYMBOL(drm_encoder_cleanup);
177 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
179 struct drm_connector *connector;
180 struct drm_device *dev = encoder->dev;
181 bool uses_atomic = false;
183 /* For atomic drivers only state objects are synchronously updated and
184 * protected by modeset locks, so check those first. */
185 drm_for_each_connector(connector, dev) {
186 if (!connector->state)
191 if (connector->state->best_encoder != encoder)
194 return connector->state->crtc;
197 /* Don't return stale data (e.g. pending async disable). */
201 return encoder->crtc;
204 int drm_mode_getencoder(struct drm_device *dev, void *data,
205 struct drm_file *file_priv)
207 struct drm_mode_get_encoder *enc_resp = data;
208 struct drm_encoder *encoder;
209 struct drm_crtc *crtc;
211 if (!drm_core_check_feature(dev, DRIVER_MODESET))
214 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
218 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
219 crtc = drm_encoder_get_crtc(encoder);
221 enc_resp->crtc_id = crtc->base.id;
223 enc_resp->crtc_id = 0;
224 drm_modeset_unlock(&dev->mode_config.connection_mutex);
226 enc_resp->encoder_type = encoder->encoder_type;
227 enc_resp->encoder_id = encoder->base.id;
228 enc_resp->possible_crtcs = encoder->possible_crtcs;
229 enc_resp->possible_clones = encoder->possible_clones;