1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/export.h>
24
25#include <drm/drm_bridge.h>
26#include <drm/drm_device.h>
27#include <drm/drm_drv.h>
28#include <drm/drm_encoder.h>
29
30#include "drm_crtc_internal.h"
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
58 { DRM_MODE_ENCODER_NONE, "None" },
59 { DRM_MODE_ENCODER_DAC, "DAC" },
60 { DRM_MODE_ENCODER_TMDS, "TMDS" },
61 { DRM_MODE_ENCODER_LVDS, "LVDS" },
62 { DRM_MODE_ENCODER_TVDAC, "TV" },
63 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
64 { DRM_MODE_ENCODER_DSI, "DSI" },
65 { DRM_MODE_ENCODER_DPMST, "DP MST" },
66 { DRM_MODE_ENCODER_DPI, "DPI" },
67};
68
69int drm_encoder_register_all(struct drm_device *dev)
70{
71 struct drm_encoder *encoder;
72 int ret = 0;
73
74 drm_for_each_encoder(encoder, dev) {
75 if (encoder->funcs->late_register)
76 ret = encoder->funcs->late_register(encoder);
77 if (ret)
78 return ret;
79 }
80
81 return 0;
82}
83
84void drm_encoder_unregister_all(struct drm_device *dev)
85{
86 struct drm_encoder *encoder;
87
88 drm_for_each_encoder(encoder, dev) {
89 if (encoder->funcs->early_unregister)
90 encoder->funcs->early_unregister(encoder);
91 }
92}
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109int drm_encoder_init(struct drm_device *dev,
110 struct drm_encoder *encoder,
111 const struct drm_encoder_funcs *funcs,
112 int encoder_type, const char *name, ...)
113{
114 int ret;
115
116
117 if (WARN_ON(dev->mode_config.num_encoder >= 32))
118 return -EINVAL;
119
120 ret = drm_mode_object_add(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
121 if (ret)
122 return ret;
123
124 encoder->dev = dev;
125 encoder->encoder_type = encoder_type;
126 encoder->funcs = funcs;
127 if (name) {
128 va_list ap;
129
130 va_start(ap, name);
131 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
132 va_end(ap);
133 } else {
134 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
135 drm_encoder_enum_list[encoder_type].name,
136 encoder->base.id);
137 }
138 if (!encoder->name) {
139 ret = -ENOMEM;
140 goto out_put;
141 }
142
143 INIT_LIST_HEAD(&encoder->bridge_chain);
144 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
145 encoder->index = dev->mode_config.num_encoder++;
146
147out_put:
148 if (ret)
149 drm_mode_object_unregister(dev, &encoder->base);
150
151 return ret;
152}
153EXPORT_SYMBOL(drm_encoder_init);
154
155
156
157
158
159
160
161void drm_encoder_cleanup(struct drm_encoder *encoder)
162{
163 struct drm_device *dev = encoder->dev;
164 struct drm_bridge *bridge, *next;
165
166
167
168
169
170
171 list_for_each_entry_safe(bridge, next, &encoder->bridge_chain,
172 chain_node)
173 drm_bridge_detach(bridge);
174
175 drm_mode_object_unregister(dev, &encoder->base);
176 kfree(encoder->name);
177 list_del(&encoder->head);
178 dev->mode_config.num_encoder--;
179
180 memset(encoder, 0, sizeof(*encoder));
181}
182EXPORT_SYMBOL(drm_encoder_cleanup);
183
184static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
185{
186 struct drm_connector *connector;
187 struct drm_device *dev = encoder->dev;
188 bool uses_atomic = false;
189 struct drm_connector_list_iter conn_iter;
190
191
192
193 drm_connector_list_iter_begin(dev, &conn_iter);
194 drm_for_each_connector_iter(connector, &conn_iter) {
195 if (!connector->state)
196 continue;
197
198 uses_atomic = true;
199
200 if (connector->state->best_encoder != encoder)
201 continue;
202
203 drm_connector_list_iter_end(&conn_iter);
204 return connector->state->crtc;
205 }
206 drm_connector_list_iter_end(&conn_iter);
207
208
209 if (uses_atomic)
210 return NULL;
211
212 return encoder->crtc;
213}
214
215int drm_mode_getencoder(struct drm_device *dev, void *data,
216 struct drm_file *file_priv)
217{
218 struct drm_mode_get_encoder *enc_resp = data;
219 struct drm_encoder *encoder;
220 struct drm_crtc *crtc;
221
222 if (!drm_core_check_feature(dev, DRIVER_MODESET))
223 return -EOPNOTSUPP;
224
225 encoder = drm_encoder_find(dev, file_priv, enc_resp->encoder_id);
226 if (!encoder)
227 return -ENOENT;
228
229 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
230 crtc = drm_encoder_get_crtc(encoder);
231 if (crtc && drm_lease_held(file_priv, crtc->base.id))
232 enc_resp->crtc_id = crtc->base.id;
233 else
234 enc_resp->crtc_id = 0;
235 drm_modeset_unlock(&dev->mode_config.connection_mutex);
236
237 enc_resp->encoder_type = encoder->encoder_type;
238 enc_resp->encoder_id = encoder->base.id;
239 enc_resp->possible_crtcs = drm_lease_filter_crtcs(file_priv,
240 encoder->possible_crtcs);
241 enc_resp->possible_clones = encoder->possible_clones;
242
243 return 0;
244}
245