Merge remote-tracking branch 'md/for-next'
[deliverable/linux.git] / drivers / gpu / drm / drm_encoder.c
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
29 /**
30 * DOC: overview
31 *
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
40 * atomic IOCTL.
41 *
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.
49 *
50 * Encoders are initialized with drm_encoder_init() and cleaned up using
51 * drm_encoder_cleanup().
52 */
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" },
63 };
64
65 int drm_encoder_register_all(struct drm_device *dev)
66 {
67 struct drm_encoder *encoder;
68 int ret = 0;
69
70 drm_for_each_encoder(encoder, dev) {
71 if (encoder->funcs->late_register)
72 ret = encoder->funcs->late_register(encoder);
73 if (ret)
74 return ret;
75 }
76
77 return 0;
78 }
79
80 void drm_encoder_unregister_all(struct drm_device *dev)
81 {
82 struct drm_encoder *encoder;
83
84 drm_for_each_encoder(encoder, dev) {
85 if (encoder->funcs->early_unregister)
86 encoder->funcs->early_unregister(encoder);
87 }
88 }
89
90 /**
91 * drm_encoder_init - Init a preallocated encoder
92 * @dev: drm device
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
97 *
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.
101 *
102 * Returns:
103 * Zero on success, error code on failure.
104 */
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, ...)
109 {
110 int ret;
111
112 drm_modeset_lock_all(dev);
113
114 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
115 if (ret)
116 goto out_unlock;
117
118 encoder->dev = dev;
119 encoder->encoder_type = encoder_type;
120 encoder->funcs = funcs;
121 if (name) {
122 va_list ap;
123
124 va_start(ap, name);
125 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
126 va_end(ap);
127 } else {
128 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
129 drm_encoder_enum_list[encoder_type].name,
130 encoder->base.id);
131 }
132 if (!encoder->name) {
133 ret = -ENOMEM;
134 goto out_put;
135 }
136
137 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
138 encoder->index = dev->mode_config.num_encoder++;
139
140 out_put:
141 if (ret)
142 drm_mode_object_unregister(dev, &encoder->base);
143
144 out_unlock:
145 drm_modeset_unlock_all(dev);
146
147 return ret;
148 }
149 EXPORT_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 */
157 void 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
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);
172
173 memset(encoder, 0, sizeof(*encoder));
174 }
175 EXPORT_SYMBOL(drm_encoder_cleanup);
176
177 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
178 {
179 struct drm_connector *connector;
180 struct drm_device *dev = encoder->dev;
181 bool uses_atomic = false;
182
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)
187 continue;
188
189 uses_atomic = true;
190
191 if (connector->state->best_encoder != encoder)
192 continue;
193
194 return connector->state->crtc;
195 }
196
197 /* Don't return stale data (e.g. pending async disable). */
198 if (uses_atomic)
199 return NULL;
200
201 return encoder->crtc;
202 }
203
204 int drm_mode_getencoder(struct drm_device *dev, void *data,
205 struct drm_file *file_priv)
206 {
207 struct drm_mode_get_encoder *enc_resp = data;
208 struct drm_encoder *encoder;
209 struct drm_crtc *crtc;
210
211 if (!drm_core_check_feature(dev, DRIVER_MODESET))
212 return -EINVAL;
213
214 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
215 if (!encoder)
216 return -ENOENT;
217
218 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
219 crtc = drm_encoder_get_crtc(encoder);
220 if (crtc)
221 enc_resp->crtc_id = crtc->base.id;
222 else
223 enc_resp->crtc_id = 0;
224 drm_modeset_unlock(&dev->mode_config.connection_mutex);
225
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;
230
231 return 0;
232 }
This page took 0.034193 seconds and 5 git commands to generate.