11dedbd3bSFrançois Tigeot /*
21dedbd3bSFrançois Tigeot * Copyright (c) 2016 Intel Corporation
31dedbd3bSFrançois Tigeot *
41dedbd3bSFrançois Tigeot * Permission to use, copy, modify, distribute, and sell this software and its
51dedbd3bSFrançois Tigeot * documentation for any purpose is hereby granted without fee, provided that
61dedbd3bSFrançois Tigeot * the above copyright notice appear in all copies and that both that copyright
71dedbd3bSFrançois Tigeot * notice and this permission notice appear in supporting documentation, and
81dedbd3bSFrançois Tigeot * that the name of the copyright holders not be used in advertising or
91dedbd3bSFrançois Tigeot * publicity pertaining to distribution of the software without specific,
101dedbd3bSFrançois Tigeot * written prior permission. The copyright holders make no representations
111dedbd3bSFrançois Tigeot * about the suitability of this software for any purpose. It is provided "as
121dedbd3bSFrançois Tigeot * is" without express or implied warranty.
131dedbd3bSFrançois Tigeot *
141dedbd3bSFrançois Tigeot * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
151dedbd3bSFrançois Tigeot * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
161dedbd3bSFrançois Tigeot * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
171dedbd3bSFrançois Tigeot * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
181dedbd3bSFrançois Tigeot * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
191dedbd3bSFrançois Tigeot * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
201dedbd3bSFrançois Tigeot * OF THIS SOFTWARE.
211dedbd3bSFrançois Tigeot */
221dedbd3bSFrançois Tigeot
231dedbd3bSFrançois Tigeot #include <linux/export.h>
241dedbd3bSFrançois Tigeot #include <drm/drmP.h>
251dedbd3bSFrançois Tigeot #include <drm/drm_encoder.h>
261dedbd3bSFrançois Tigeot
271dedbd3bSFrançois Tigeot #include "drm_crtc_internal.h"
281dedbd3bSFrançois Tigeot
291dedbd3bSFrançois Tigeot /**
301dedbd3bSFrançois Tigeot * DOC: overview
311dedbd3bSFrançois Tigeot *
321dedbd3bSFrançois Tigeot * Encoders represent the connecting element between the CRTC (as the overall
33a85cb24fSFrançois Tigeot * pixel pipeline, represented by &struct drm_crtc) and the connectors (as the
34a85cb24fSFrançois Tigeot * generic sink entity, represented by &struct drm_connector). An encoder takes
351dedbd3bSFrançois Tigeot * pixel data from a CRTC and converts it to a format suitable for any attached
361dedbd3bSFrançois Tigeot * connector. Encoders are objects exposed to userspace, originally to allow
371dedbd3bSFrançois Tigeot * userspace to infer cloning and connector/CRTC restrictions. Unfortunately
381dedbd3bSFrançois Tigeot * almost all drivers get this wrong, making the uabi pretty much useless. On
391dedbd3bSFrançois Tigeot * top of that the exposed restrictions are too simple for today's hardware, and
401dedbd3bSFrançois Tigeot * the recommended way to infer restrictions is by using the
411dedbd3bSFrançois Tigeot * DRM_MODE_ATOMIC_TEST_ONLY flag for the atomic IOCTL.
421dedbd3bSFrançois Tigeot *
431dedbd3bSFrançois Tigeot * Otherwise encoders aren't used in the uapi at all (any modeset request from
441dedbd3bSFrançois Tigeot * userspace directly connects a connector with a CRTC), drivers are therefore
451dedbd3bSFrançois Tigeot * free to use them however they wish. Modeset helper libraries make strong use
461dedbd3bSFrançois Tigeot * of encoders to facilitate code sharing. But for more complex settings it is
471dedbd3bSFrançois Tigeot * usually better to move shared code into a separate &drm_bridge. Compared to
481dedbd3bSFrançois Tigeot * encoders, bridges also have the benefit of being purely an internal
491dedbd3bSFrançois Tigeot * abstraction since they are not exposed to userspace at all.
501dedbd3bSFrançois Tigeot *
511dedbd3bSFrançois Tigeot * Encoders are initialized with drm_encoder_init() and cleaned up using
521dedbd3bSFrançois Tigeot * drm_encoder_cleanup().
531dedbd3bSFrançois Tigeot */
541dedbd3bSFrançois Tigeot static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
551dedbd3bSFrançois Tigeot { DRM_MODE_ENCODER_NONE, "None" },
561dedbd3bSFrançois Tigeot { DRM_MODE_ENCODER_DAC, "DAC" },
571dedbd3bSFrançois Tigeot { DRM_MODE_ENCODER_TMDS, "TMDS" },
581dedbd3bSFrançois Tigeot { DRM_MODE_ENCODER_LVDS, "LVDS" },
591dedbd3bSFrançois Tigeot { DRM_MODE_ENCODER_TVDAC, "TV" },
601dedbd3bSFrançois Tigeot { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
611dedbd3bSFrançois Tigeot { DRM_MODE_ENCODER_DSI, "DSI" },
621dedbd3bSFrançois Tigeot { DRM_MODE_ENCODER_DPMST, "DP MST" },
631dedbd3bSFrançois Tigeot { DRM_MODE_ENCODER_DPI, "DPI" },
641dedbd3bSFrançois Tigeot };
651dedbd3bSFrançois Tigeot
drm_encoder_register_all(struct drm_device * dev)661dedbd3bSFrançois Tigeot int drm_encoder_register_all(struct drm_device *dev)
671dedbd3bSFrançois Tigeot {
681dedbd3bSFrançois Tigeot struct drm_encoder *encoder;
691dedbd3bSFrançois Tigeot int ret = 0;
701dedbd3bSFrançois Tigeot
711dedbd3bSFrançois Tigeot drm_for_each_encoder(encoder, dev) {
721dedbd3bSFrançois Tigeot if (encoder->funcs->late_register)
731dedbd3bSFrançois Tigeot ret = encoder->funcs->late_register(encoder);
741dedbd3bSFrançois Tigeot if (ret)
751dedbd3bSFrançois Tigeot return ret;
761dedbd3bSFrançois Tigeot }
771dedbd3bSFrançois Tigeot
781dedbd3bSFrançois Tigeot return 0;
791dedbd3bSFrançois Tigeot }
801dedbd3bSFrançois Tigeot
drm_encoder_unregister_all(struct drm_device * dev)811dedbd3bSFrançois Tigeot void drm_encoder_unregister_all(struct drm_device *dev)
821dedbd3bSFrançois Tigeot {
831dedbd3bSFrançois Tigeot struct drm_encoder *encoder;
841dedbd3bSFrançois Tigeot
851dedbd3bSFrançois Tigeot drm_for_each_encoder(encoder, dev) {
861dedbd3bSFrançois Tigeot if (encoder->funcs->early_unregister)
871dedbd3bSFrançois Tigeot encoder->funcs->early_unregister(encoder);
881dedbd3bSFrançois Tigeot }
891dedbd3bSFrançois Tigeot }
901dedbd3bSFrançois Tigeot
911dedbd3bSFrançois Tigeot /**
921dedbd3bSFrançois Tigeot * drm_encoder_init - Init a preallocated encoder
931dedbd3bSFrançois Tigeot * @dev: drm device
941dedbd3bSFrançois Tigeot * @encoder: the encoder to init
951dedbd3bSFrançois Tigeot * @funcs: callbacks for this encoder
961dedbd3bSFrançois Tigeot * @encoder_type: user visible type of the encoder
971dedbd3bSFrançois Tigeot * @name: printf style format string for the encoder name, or NULL for default name
981dedbd3bSFrançois Tigeot *
991dedbd3bSFrançois Tigeot * Initialises a preallocated encoder. Encoder should be subclassed as part of
1001dedbd3bSFrançois Tigeot * driver encoder objects. At driver unload time drm_encoder_cleanup() should be
101a85cb24fSFrançois Tigeot * called from the driver's &drm_encoder_funcs.destroy hook.
1021dedbd3bSFrançois Tigeot *
1031dedbd3bSFrançois Tigeot * Returns:
1041dedbd3bSFrançois Tigeot * Zero on success, error code on failure.
1051dedbd3bSFrançois Tigeot */
drm_encoder_init(struct drm_device * dev,struct drm_encoder * encoder,const struct drm_encoder_funcs * funcs,int encoder_type,const char * name,...)1061dedbd3bSFrançois Tigeot int drm_encoder_init(struct drm_device *dev,
1071dedbd3bSFrançois Tigeot struct drm_encoder *encoder,
1081dedbd3bSFrançois Tigeot const struct drm_encoder_funcs *funcs,
1091dedbd3bSFrançois Tigeot int encoder_type, const char *name, ...)
1101dedbd3bSFrançois Tigeot {
1111dedbd3bSFrançois Tigeot int ret;
1121dedbd3bSFrançois Tigeot
113a85cb24fSFrançois Tigeot ret = drm_mode_object_add(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1141dedbd3bSFrançois Tigeot if (ret)
1154be47400SFrançois Tigeot return ret;
1161dedbd3bSFrançois Tigeot
1171dedbd3bSFrançois Tigeot encoder->dev = dev;
1181dedbd3bSFrançois Tigeot encoder->encoder_type = encoder_type;
1191dedbd3bSFrançois Tigeot encoder->funcs = funcs;
1201dedbd3bSFrançois Tigeot if (name) {
1211dedbd3bSFrançois Tigeot va_list ap;
1221dedbd3bSFrançois Tigeot
1231dedbd3bSFrançois Tigeot va_start(ap, name);
1241dedbd3bSFrançois Tigeot encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1251dedbd3bSFrançois Tigeot va_end(ap);
1261dedbd3bSFrançois Tigeot } else {
1271dedbd3bSFrançois Tigeot encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1281dedbd3bSFrançois Tigeot drm_encoder_enum_list[encoder_type].name,
1291dedbd3bSFrançois Tigeot encoder->base.id);
1301dedbd3bSFrançois Tigeot }
1311dedbd3bSFrançois Tigeot if (!encoder->name) {
1321dedbd3bSFrançois Tigeot ret = -ENOMEM;
1331dedbd3bSFrançois Tigeot goto out_put;
1341dedbd3bSFrançois Tigeot }
1351dedbd3bSFrançois Tigeot
1361dedbd3bSFrançois Tigeot list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1371dedbd3bSFrançois Tigeot encoder->index = dev->mode_config.num_encoder++;
1381dedbd3bSFrançois Tigeot
1391dedbd3bSFrançois Tigeot out_put:
1401dedbd3bSFrançois Tigeot if (ret)
1411dedbd3bSFrançois Tigeot drm_mode_object_unregister(dev, &encoder->base);
1421dedbd3bSFrançois Tigeot
1431dedbd3bSFrançois Tigeot return ret;
1441dedbd3bSFrançois Tigeot }
1451dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_encoder_init);
1461dedbd3bSFrançois Tigeot
1471dedbd3bSFrançois Tigeot /**
1481dedbd3bSFrançois Tigeot * drm_encoder_cleanup - cleans up an initialised encoder
1491dedbd3bSFrançois Tigeot * @encoder: encoder to cleanup
1501dedbd3bSFrançois Tigeot *
1511dedbd3bSFrançois Tigeot * Cleans up the encoder but doesn't free the object.
1521dedbd3bSFrançois Tigeot */
drm_encoder_cleanup(struct drm_encoder * encoder)1531dedbd3bSFrançois Tigeot void drm_encoder_cleanup(struct drm_encoder *encoder)
1541dedbd3bSFrançois Tigeot {
1551dedbd3bSFrançois Tigeot struct drm_device *dev = encoder->dev;
1561dedbd3bSFrançois Tigeot
1571dedbd3bSFrançois Tigeot /* Note that the encoder_list is considered to be static; should we
1581dedbd3bSFrançois Tigeot * remove the drm_encoder at runtime we would have to decrement all
1591dedbd3bSFrançois Tigeot * the indices on the drm_encoder after us in the encoder_list.
1601dedbd3bSFrançois Tigeot */
1611dedbd3bSFrançois Tigeot
162a85cb24fSFrançois Tigeot if (encoder->bridge) {
163a85cb24fSFrançois Tigeot struct drm_bridge *bridge = encoder->bridge;
164a85cb24fSFrançois Tigeot struct drm_bridge *next;
165a85cb24fSFrançois Tigeot
166a85cb24fSFrançois Tigeot while (bridge) {
167a85cb24fSFrançois Tigeot next = bridge->next;
168a85cb24fSFrançois Tigeot drm_bridge_detach(bridge);
169a85cb24fSFrançois Tigeot bridge = next;
170a85cb24fSFrançois Tigeot }
171a85cb24fSFrançois Tigeot }
172a85cb24fSFrançois Tigeot
1731dedbd3bSFrançois Tigeot drm_mode_object_unregister(dev, &encoder->base);
1741dedbd3bSFrançois Tigeot kfree(encoder->name);
1751dedbd3bSFrançois Tigeot list_del(&encoder->head);
1761dedbd3bSFrançois Tigeot dev->mode_config.num_encoder--;
1771dedbd3bSFrançois Tigeot
1781dedbd3bSFrançois Tigeot memset(encoder, 0, sizeof(*encoder));
1791dedbd3bSFrançois Tigeot }
1801dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_encoder_cleanup);
1811dedbd3bSFrançois Tigeot
drm_encoder_get_crtc(struct drm_encoder * encoder)1821dedbd3bSFrançois Tigeot static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
1831dedbd3bSFrançois Tigeot {
1841dedbd3bSFrançois Tigeot struct drm_connector *connector;
1851dedbd3bSFrançois Tigeot struct drm_device *dev = encoder->dev;
1861dedbd3bSFrançois Tigeot bool uses_atomic = false;
187a85cb24fSFrançois Tigeot struct drm_connector_list_iter conn_iter;
1881dedbd3bSFrançois Tigeot
1891dedbd3bSFrançois Tigeot /* For atomic drivers only state objects are synchronously updated and
1901dedbd3bSFrançois Tigeot * protected by modeset locks, so check those first. */
191a85cb24fSFrançois Tigeot drm_connector_list_iter_begin(dev, &conn_iter);
192a85cb24fSFrançois Tigeot drm_for_each_connector_iter(connector, &conn_iter) {
1931dedbd3bSFrançois Tigeot if (!connector->state)
1941dedbd3bSFrançois Tigeot continue;
1951dedbd3bSFrançois Tigeot
1961dedbd3bSFrançois Tigeot uses_atomic = true;
1971dedbd3bSFrançois Tigeot
1981dedbd3bSFrançois Tigeot if (connector->state->best_encoder != encoder)
1991dedbd3bSFrançois Tigeot continue;
2001dedbd3bSFrançois Tigeot
201a85cb24fSFrançois Tigeot drm_connector_list_iter_end(&conn_iter);
2021dedbd3bSFrançois Tigeot return connector->state->crtc;
2031dedbd3bSFrançois Tigeot }
204a85cb24fSFrançois Tigeot drm_connector_list_iter_end(&conn_iter);
2051dedbd3bSFrançois Tigeot
2061dedbd3bSFrançois Tigeot /* Don't return stale data (e.g. pending async disable). */
2071dedbd3bSFrançois Tigeot if (uses_atomic)
2081dedbd3bSFrançois Tigeot return NULL;
2091dedbd3bSFrançois Tigeot
2101dedbd3bSFrançois Tigeot return encoder->crtc;
2111dedbd3bSFrançois Tigeot }
2121dedbd3bSFrançois Tigeot
drm_mode_getencoder(struct drm_device * dev,void * data,struct drm_file * file_priv)2131dedbd3bSFrançois Tigeot int drm_mode_getencoder(struct drm_device *dev, void *data,
2141dedbd3bSFrançois Tigeot struct drm_file *file_priv)
2151dedbd3bSFrançois Tigeot {
2161dedbd3bSFrançois Tigeot struct drm_mode_get_encoder *enc_resp = data;
2171dedbd3bSFrançois Tigeot struct drm_encoder *encoder;
2181dedbd3bSFrançois Tigeot struct drm_crtc *crtc;
2191dedbd3bSFrançois Tigeot
2201dedbd3bSFrançois Tigeot if (!drm_core_check_feature(dev, DRIVER_MODESET))
2211dedbd3bSFrançois Tigeot return -EINVAL;
2221dedbd3bSFrançois Tigeot
223*3f2dd94aSFrançois Tigeot encoder = drm_encoder_find(dev, file_priv, enc_resp->encoder_id);
2241dedbd3bSFrançois Tigeot if (!encoder)
2251dedbd3bSFrançois Tigeot return -ENOENT;
2261dedbd3bSFrançois Tigeot
2271dedbd3bSFrançois Tigeot drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2281dedbd3bSFrançois Tigeot crtc = drm_encoder_get_crtc(encoder);
229*3f2dd94aSFrançois Tigeot if (crtc && drm_lease_held(file_priv, crtc->base.id))
2301dedbd3bSFrançois Tigeot enc_resp->crtc_id = crtc->base.id;
2311dedbd3bSFrançois Tigeot else
2321dedbd3bSFrançois Tigeot enc_resp->crtc_id = 0;
2331dedbd3bSFrançois Tigeot drm_modeset_unlock(&dev->mode_config.connection_mutex);
2341dedbd3bSFrançois Tigeot
2351dedbd3bSFrançois Tigeot enc_resp->encoder_type = encoder->encoder_type;
2361dedbd3bSFrançois Tigeot enc_resp->encoder_id = encoder->base.id;
237*3f2dd94aSFrançois Tigeot enc_resp->possible_crtcs = drm_lease_filter_crtcs(file_priv,
238*3f2dd94aSFrançois Tigeot encoder->possible_crtcs);
2391dedbd3bSFrançois Tigeot enc_resp->possible_clones = encoder->possible_clones;
2401dedbd3bSFrançois Tigeot
2411dedbd3bSFrançois Tigeot return 0;
2421dedbd3bSFrançois Tigeot }
243