Input: xpad - add support for Xbox1 PDP Camo series gamepad
[linux/fpc-iii.git] / drivers / gpu / drm / drm_encoder.c
blob5c067719164d9a6c3ea93146fb47c8f7725bf198
1 /*
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
20 * OF THIS SOFTWARE.
23 #include <linux/export.h>
24 #include <drm/drmP.h>
25 #include <drm/drm_encoder.h>
27 #include "drm_crtc_internal.h"
29 /**
30 * DOC: overview
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). An encoder takes
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.
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
48 * encoders, bridges also have the benefit of being purely an internal
49 * abstraction since they are not exposed to userspace at all.
51 * Encoders are initialized with drm_encoder_init() and cleaned up using
52 * drm_encoder_cleanup().
54 static 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" },
66 int drm_encoder_register_all(struct drm_device *dev)
68 struct drm_encoder *encoder;
69 int ret = 0;
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;
78 return 0;
81 void drm_encoder_unregister_all(struct drm_device *dev)
83 struct drm_encoder *encoder;
85 drm_for_each_encoder(encoder, dev) {
86 if (encoder->funcs->early_unregister)
87 encoder->funcs->early_unregister(encoder);
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
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
101 * called from the driver's destroy hook in &drm_encoder_funcs.
103 * Returns:
104 * Zero on success, error code on failure.
106 int drm_encoder_init(struct drm_device *dev,
107 struct drm_encoder *encoder,
108 const struct drm_encoder_funcs *funcs,
109 int encoder_type, const char *name, ...)
111 int ret;
113 drm_modeset_lock_all(dev);
115 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
116 if (ret)
117 goto out_unlock;
119 encoder->dev = dev;
120 encoder->encoder_type = encoder_type;
121 encoder->funcs = funcs;
122 if (name) {
123 va_list ap;
125 va_start(ap, name);
126 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
127 va_end(ap);
128 } else {
129 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
130 drm_encoder_enum_list[encoder_type].name,
131 encoder->base.id);
133 if (!encoder->name) {
134 ret = -ENOMEM;
135 goto out_put;
138 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
139 encoder->index = dev->mode_config.num_encoder++;
141 out_put:
142 if (ret)
143 drm_mode_object_unregister(dev, &encoder->base);
145 out_unlock:
146 drm_modeset_unlock_all(dev);
148 return ret;
150 EXPORT_SYMBOL(drm_encoder_init);
153 * drm_encoder_cleanup - cleans up an initialised encoder
154 * @encoder: encoder to cleanup
156 * Cleans up the encoder but doesn't free the object.
158 void drm_encoder_cleanup(struct drm_encoder *encoder)
160 struct drm_device *dev = encoder->dev;
162 /* Note that the encoder_list is considered to be static; should we
163 * remove the drm_encoder at runtime we would have to decrement all
164 * the indices on the drm_encoder after us in the encoder_list.
167 drm_modeset_lock_all(dev);
168 drm_mode_object_unregister(dev, &encoder->base);
169 kfree(encoder->name);
170 list_del(&encoder->head);
171 dev->mode_config.num_encoder--;
172 drm_modeset_unlock_all(dev);
174 memset(encoder, 0, sizeof(*encoder));
176 EXPORT_SYMBOL(drm_encoder_cleanup);
178 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
180 struct drm_connector *connector;
181 struct drm_device *dev = encoder->dev;
182 bool uses_atomic = false;
184 /* For atomic drivers only state objects are synchronously updated and
185 * protected by modeset locks, so check those first. */
186 drm_for_each_connector(connector, dev) {
187 if (!connector->state)
188 continue;
190 uses_atomic = true;
192 if (connector->state->best_encoder != encoder)
193 continue;
195 return connector->state->crtc;
198 /* Don't return stale data (e.g. pending async disable). */
199 if (uses_atomic)
200 return NULL;
202 return encoder->crtc;
205 int drm_mode_getencoder(struct drm_device *dev, void *data,
206 struct drm_file *file_priv)
208 struct drm_mode_get_encoder *enc_resp = data;
209 struct drm_encoder *encoder;
210 struct drm_crtc *crtc;
212 if (!drm_core_check_feature(dev, DRIVER_MODESET))
213 return -EINVAL;
215 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
216 if (!encoder)
217 return -ENOENT;
219 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
220 crtc = drm_encoder_get_crtc(encoder);
221 if (crtc)
222 enc_resp->crtc_id = crtc->base.id;
223 else
224 enc_resp->crtc_id = 0;
225 drm_modeset_unlock(&dev->mode_config.connection_mutex);
227 enc_resp->encoder_type = encoder->encoder_type;
228 enc_resp->encoder_id = encoder->base.id;
229 enc_resp->possible_crtcs = encoder->possible_crtcs;
230 enc_resp->possible_clones = encoder->possible_clones;
232 return 0;