/*1* Copyright (c) 2016 Intel Corporation2*3* Permission to use, copy, modify, distribute, and sell this software and its4* documentation for any purpose is hereby granted without fee, provided that5* the above copyright notice appear in all copies and that both that copyright6* notice and this permission notice appear in supporting documentation, and7* that the name of the copyright holders not be used in advertising or8* publicity pertaining to distribution of the software without specific,9* written prior permission. The copyright holders make no representations10* about the suitability of this software for any purpose. It is provided "as11* is" without express or implied warranty.12*13* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,14* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO15* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR16* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,17* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER18* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE19* OF THIS SOFTWARE.20*/2122#ifndef __DRM_ENCODER_H__23#define __DRM_ENCODER_H__2425#include <linux/list.h>26#include <linux/ctype.h>27#include <drm/drm_crtc.h>28#include <drm/drm_mode.h>29#include <drm/drm_mode_object.h>30#include <drm/drm_util.h>3132struct drm_encoder;3334/**35* struct drm_encoder_funcs - encoder controls36*37* Encoders sit between CRTCs and connectors.38*/39struct drm_encoder_funcs {40/**41* @reset:42*43* Reset encoder hardware and software state to off. This function isn't44* called by the core directly, only through drm_mode_config_reset().45* It's not a helper hook only for historical reasons.46*/47void (*reset)(struct drm_encoder *encoder);4849/**50* @destroy:51*52* Clean up encoder resources. This is only called at driver unload time53* through drm_mode_config_cleanup() since an encoder cannot be54* hotplugged in DRM.55*/56void (*destroy)(struct drm_encoder *encoder);5758/**59* @late_register:60*61* This optional hook can be used to register additional userspace62* interfaces attached to the encoder.63* It is called late in the driver load sequence from drm_dev_register().64* Everything added from this callback should be unregistered in65* the early_unregister callback.66*67* Returns:68*69* 0 on success, or a negative error code on failure.70*/71int (*late_register)(struct drm_encoder *encoder);7273/**74* @early_unregister:75*76* This optional hook should be used to unregister the additional77* userspace interfaces attached to the encoder from78* @late_register. It is called from drm_dev_unregister(),79* early in the driver unload sequence to disable userspace access80* before data structures are torndown.81*/82void (*early_unregister)(struct drm_encoder *encoder);8384/**85* @debugfs_init:86*87* Allows encoders to create encoder-specific debugfs files.88*/89void (*debugfs_init)(struct drm_encoder *encoder, struct dentry *root);90};9192/**93* struct drm_encoder - central DRM encoder structure94* @dev: parent DRM device95* @head: list management96* @base: base KMS object97* @name: human readable name, can be overwritten by the driver98* @funcs: control functions, can be NULL for simple managed encoders99* @helper_private: mid-layer private data100*101* CRTCs drive pixels to encoders, which convert them into signals102* appropriate for a given connector or set of connectors.103*/104struct drm_encoder {105struct drm_device *dev;106struct list_head head;107108struct drm_mode_object base;109char *name;110/**111* @encoder_type:112*113* One of the DRM_MODE_ENCODER_<foo> types in drm_mode.h. The following114* encoder types are defined thus far:115*116* - DRM_MODE_ENCODER_DAC for VGA and analog on DVI-I/DVI-A.117*118* - DRM_MODE_ENCODER_TMDS for DVI, HDMI and (embedded) DisplayPort.119*120* - DRM_MODE_ENCODER_LVDS for display panels, or in general any panel121* with a proprietary parallel connector.122*123* - DRM_MODE_ENCODER_TVDAC for TV output (Composite, S-Video,124* Component, SCART).125*126* - DRM_MODE_ENCODER_VIRTUAL for virtual machine displays127*128* - DRM_MODE_ENCODER_DSI for panels connected using the DSI serial bus.129*130* - DRM_MODE_ENCODER_DPI for panels connected using the DPI parallel131* bus.132*133* - DRM_MODE_ENCODER_DPMST for special fake encoders used to allow134* mutliple DP MST streams to share one physical encoder.135*/136int encoder_type;137138/**139* @index: Position inside the mode_config.list, can be used as an array140* index. It is invariant over the lifetime of the encoder.141*/142unsigned index;143144/**145* @possible_crtcs: Bitmask of potential CRTC bindings, using146* drm_crtc_index() as the index into the bitfield. The driver must set147* the bits for all &drm_crtc objects this encoder can be connected to148* before calling drm_dev_register().149*150* You will get a WARN if you get this wrong in the driver.151*152* Note that since CRTC objects can't be hotplugged the assigned indices153* are stable and hence known before registering all objects.154*/155uint32_t possible_crtcs;156157/**158* @possible_clones: Bitmask of potential sibling encoders for cloning,159* using drm_encoder_index() as the index into the bitfield. The driver160* must set the bits for all &drm_encoder objects which can clone a161* &drm_crtc together with this encoder before calling162* drm_dev_register(). Drivers should set the bit representing the163* encoder itself, too. Cloning bits should be set such that when two164* encoders can be used in a cloned configuration, they both should have165* each another bits set.166*167* As an exception to the above rule if the driver doesn't implement168* any cloning it can leave @possible_clones set to 0. The core will169* automagically fix this up by setting the bit for the encoder itself.170*171* You will get a WARN if you get this wrong in the driver.172*173* Note that since encoder objects can't be hotplugged the assigned indices174* are stable and hence known before registering all objects.175*/176uint32_t possible_clones;177178/**179* @crtc: Currently bound CRTC, only really meaningful for non-atomic180* drivers. Atomic drivers should instead check181* &drm_connector_state.crtc.182*/183struct drm_crtc *crtc;184185/**186* @bridge_chain: Bridges attached to this encoder. Drivers shall not187* access this field directly.188*/189struct list_head bridge_chain;190191const struct drm_encoder_funcs *funcs;192const struct drm_encoder_helper_funcs *helper_private;193194/**195* @debugfs_entry:196*197* Debugfs directory for this CRTC.198*/199struct dentry *debugfs_entry;200};201202#define obj_to_encoder(x) container_of(x, struct drm_encoder, base)203204__printf(5, 6)205int drm_encoder_init(struct drm_device *dev,206struct drm_encoder *encoder,207const struct drm_encoder_funcs *funcs,208int encoder_type, const char *name, ...);209210__printf(5, 6)211int drmm_encoder_init(struct drm_device *dev,212struct drm_encoder *encoder,213const struct drm_encoder_funcs *funcs,214int encoder_type, const char *name, ...);215216__printf(6, 7)217void *__drmm_encoder_alloc(struct drm_device *dev,218size_t size, size_t offset,219const struct drm_encoder_funcs *funcs,220int encoder_type,221const char *name, ...);222223/**224* drmm_encoder_alloc - Allocate and initialize an encoder225* @dev: drm device226* @type: the type of the struct which contains struct &drm_encoder227* @member: the name of the &drm_encoder within @type228* @funcs: callbacks for this encoder (optional)229* @encoder_type: user visible type of the encoder230* @name: printf style format string for the encoder name, or NULL for default name231*232* Allocates and initializes an encoder. Encoder should be subclassed as part of233* driver encoder objects. Cleanup is automatically handled through registering234* drm_encoder_cleanup() with drmm_add_action().235*236* The @drm_encoder_funcs.destroy hook must be NULL.237*238* Returns:239* Pointer to new encoder, or ERR_PTR on failure.240*/241#define drmm_encoder_alloc(dev, type, member, funcs, encoder_type, name, ...) \242((type *)__drmm_encoder_alloc(dev, sizeof(type), \243offsetof(type, member), funcs, \244encoder_type, name, ##__VA_ARGS__))245246/**247* drmm_plain_encoder_alloc - Allocate and initialize an encoder248* @dev: drm device249* @funcs: callbacks for this encoder (optional)250* @encoder_type: user visible type of the encoder251* @name: printf style format string for the encoder name, or NULL for default name252*253* This is a simplified version of drmm_encoder_alloc(), which only allocates254* and returns a struct drm_encoder instance, with no subclassing.255*256* Returns:257* Pointer to the new drm_encoder struct, or ERR_PTR on failure.258*/259#define drmm_plain_encoder_alloc(dev, funcs, encoder_type, name, ...) \260((struct drm_encoder *) \261__drmm_encoder_alloc(dev, sizeof(struct drm_encoder), \2620, funcs, encoder_type, name, ##__VA_ARGS__))263264/**265* drm_encoder_index - find the index of a registered encoder266* @encoder: encoder to find index for267*268* Given a registered encoder, return the index of that encoder within a DRM269* device's list of encoders.270*/271static inline unsigned int drm_encoder_index(const struct drm_encoder *encoder)272{273return encoder->index;274}275276/**277* drm_encoder_mask - find the mask of a registered encoder278* @encoder: encoder to find mask for279*280* Given a registered encoder, return the mask bit of that encoder for an281* encoder's possible_clones field.282*/283static inline u32 drm_encoder_mask(const struct drm_encoder *encoder)284{285return 1 << drm_encoder_index(encoder);286}287288/**289* drm_encoder_crtc_ok - can a given crtc drive a given encoder?290* @encoder: encoder to test291* @crtc: crtc to test292*293* Returns false if @encoder can't be driven by @crtc, true otherwise.294*/295static inline bool drm_encoder_crtc_ok(struct drm_encoder *encoder,296struct drm_crtc *crtc)297{298return !!(encoder->possible_crtcs & drm_crtc_mask(crtc));299}300301/**302* drm_encoder_find - find a &drm_encoder303* @dev: DRM device304* @file_priv: drm file to check for lease against.305* @id: encoder id306*307* Returns the encoder with @id, NULL if it doesn't exist. Simple wrapper around308* drm_mode_object_find().309*/310static inline struct drm_encoder *drm_encoder_find(struct drm_device *dev,311struct drm_file *file_priv,312uint32_t id)313{314struct drm_mode_object *mo;315316mo = drm_mode_object_find(dev, file_priv, id, DRM_MODE_OBJECT_ENCODER);317318return mo ? obj_to_encoder(mo) : NULL;319}320321void drm_encoder_cleanup(struct drm_encoder *encoder);322323/**324* drm_for_each_encoder_mask - iterate over encoders specified by bitmask325* @encoder: the loop cursor326* @dev: the DRM device327* @encoder_mask: bitmask of encoder indices328*329* Iterate over all encoders specified by bitmask.330*/331#define drm_for_each_encoder_mask(encoder, dev, encoder_mask) \332list_for_each_entry((encoder), &(dev)->mode_config.encoder_list, head) \333for_each_if ((encoder_mask) & drm_encoder_mask(encoder))334335/**336* drm_for_each_encoder - iterate over all encoders337* @encoder: the loop cursor338* @dev: the DRM device339*340* Iterate over all encoders of @dev.341*/342#define drm_for_each_encoder(encoder, dev) \343list_for_each_entry(encoder, &(dev)->mode_config.encoder_list, head)344345#endif346347348