xref: /linux/include/drm/drm_encoder.h (revision 2407d1dc04789bfb2f39d248644981d2233feb0e)
1321a95aeSDaniel Vetter /*
2321a95aeSDaniel Vetter  * Copyright (c) 2016 Intel Corporation
3321a95aeSDaniel Vetter  *
4321a95aeSDaniel Vetter  * Permission to use, copy, modify, distribute, and sell this software and its
5321a95aeSDaniel Vetter  * documentation for any purpose is hereby granted without fee, provided that
6321a95aeSDaniel Vetter  * the above copyright notice appear in all copies and that both that copyright
7321a95aeSDaniel Vetter  * notice and this permission notice appear in supporting documentation, and
8321a95aeSDaniel Vetter  * that the name of the copyright holders not be used in advertising or
9321a95aeSDaniel Vetter  * publicity pertaining to distribution of the software without specific,
10321a95aeSDaniel Vetter  * written prior permission.  The copyright holders make no representations
11321a95aeSDaniel Vetter  * about the suitability of this software for any purpose.  It is provided "as
12321a95aeSDaniel Vetter  * is" without express or implied warranty.
13321a95aeSDaniel Vetter  *
14321a95aeSDaniel Vetter  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15321a95aeSDaniel Vetter  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16321a95aeSDaniel Vetter  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17321a95aeSDaniel Vetter  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18321a95aeSDaniel Vetter  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19321a95aeSDaniel Vetter  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20321a95aeSDaniel Vetter  * OF THIS SOFTWARE.
21321a95aeSDaniel Vetter  */
22321a95aeSDaniel Vetter 
23321a95aeSDaniel Vetter #ifndef __DRM_ENCODER_H__
24321a95aeSDaniel Vetter #define __DRM_ENCODER_H__
25321a95aeSDaniel Vetter 
26321a95aeSDaniel Vetter #include <linux/list.h>
27321a95aeSDaniel Vetter #include <linux/ctype.h>
28*2407d1dcSLaurent Pinchart #include <drm/drm_crtc.h>
299338203cSLaurent Pinchart #include <drm/drm_mode.h>
30949619f3SDaniel Vetter #include <drm/drm_mode_object.h>
31321a95aeSDaniel Vetter 
329338203cSLaurent Pinchart struct drm_encoder;
339338203cSLaurent Pinchart 
34321a95aeSDaniel Vetter /**
35321a95aeSDaniel Vetter  * struct drm_encoder_funcs - encoder controls
36321a95aeSDaniel Vetter  *
37321a95aeSDaniel Vetter  * Encoders sit between CRTCs and connectors.
38321a95aeSDaniel Vetter  */
39321a95aeSDaniel Vetter struct drm_encoder_funcs {
40321a95aeSDaniel Vetter 	/**
41321a95aeSDaniel Vetter 	 * @reset:
42321a95aeSDaniel Vetter 	 *
43321a95aeSDaniel Vetter 	 * Reset encoder hardware and software state to off. This function isn't
44321a95aeSDaniel Vetter 	 * called by the core directly, only through drm_mode_config_reset().
45321a95aeSDaniel Vetter 	 * It's not a helper hook only for historical reasons.
46321a95aeSDaniel Vetter 	 */
47321a95aeSDaniel Vetter 	void (*reset)(struct drm_encoder *encoder);
48321a95aeSDaniel Vetter 
49321a95aeSDaniel Vetter 	/**
50321a95aeSDaniel Vetter 	 * @destroy:
51321a95aeSDaniel Vetter 	 *
52321a95aeSDaniel Vetter 	 * Clean up encoder resources. This is only called at driver unload time
53321a95aeSDaniel Vetter 	 * through drm_mode_config_cleanup() since an encoder cannot be
54321a95aeSDaniel Vetter 	 * hotplugged in DRM.
55321a95aeSDaniel Vetter 	 */
56321a95aeSDaniel Vetter 	void (*destroy)(struct drm_encoder *encoder);
57321a95aeSDaniel Vetter 
58321a95aeSDaniel Vetter 	/**
59321a95aeSDaniel Vetter 	 * @late_register:
60321a95aeSDaniel Vetter 	 *
61321a95aeSDaniel Vetter 	 * This optional hook can be used to register additional userspace
62321a95aeSDaniel Vetter 	 * interfaces attached to the encoder like debugfs interfaces.
63321a95aeSDaniel Vetter 	 * It is called late in the driver load sequence from drm_dev_register().
64321a95aeSDaniel Vetter 	 * Everything added from this callback should be unregistered in
65321a95aeSDaniel Vetter 	 * the early_unregister callback.
66321a95aeSDaniel Vetter 	 *
67321a95aeSDaniel Vetter 	 * Returns:
68321a95aeSDaniel Vetter 	 *
69321a95aeSDaniel Vetter 	 * 0 on success, or a negative error code on failure.
70321a95aeSDaniel Vetter 	 */
71321a95aeSDaniel Vetter 	int (*late_register)(struct drm_encoder *encoder);
72321a95aeSDaniel Vetter 
73321a95aeSDaniel Vetter 	/**
74321a95aeSDaniel Vetter 	 * @early_unregister:
75321a95aeSDaniel Vetter 	 *
76321a95aeSDaniel Vetter 	 * This optional hook should be used to unregister the additional
77321a95aeSDaniel Vetter 	 * userspace interfaces attached to the encoder from
78321a95aeSDaniel Vetter 	 * late_unregister(). It is called from drm_dev_unregister(),
79321a95aeSDaniel Vetter 	 * early in the driver unload sequence to disable userspace access
80321a95aeSDaniel Vetter 	 * before data structures are torndown.
81321a95aeSDaniel Vetter 	 */
82321a95aeSDaniel Vetter 	void (*early_unregister)(struct drm_encoder *encoder);
83321a95aeSDaniel Vetter };
84321a95aeSDaniel Vetter 
85321a95aeSDaniel Vetter /**
86321a95aeSDaniel Vetter  * struct drm_encoder - central DRM encoder structure
87321a95aeSDaniel Vetter  * @dev: parent DRM device
88321a95aeSDaniel Vetter  * @head: list management
89321a95aeSDaniel Vetter  * @base: base KMS object
90321a95aeSDaniel Vetter  * @name: human readable name, can be overwritten by the driver
91321a95aeSDaniel Vetter  * @crtc: currently bound CRTC
92321a95aeSDaniel Vetter  * @bridge: bridge associated to the encoder
93321a95aeSDaniel Vetter  * @funcs: control functions
94321a95aeSDaniel Vetter  * @helper_private: mid-layer private data
95321a95aeSDaniel Vetter  *
96321a95aeSDaniel Vetter  * CRTCs drive pixels to encoders, which convert them into signals
97321a95aeSDaniel Vetter  * appropriate for a given connector or set of connectors.
98321a95aeSDaniel Vetter  */
99321a95aeSDaniel Vetter struct drm_encoder {
100321a95aeSDaniel Vetter 	struct drm_device *dev;
101321a95aeSDaniel Vetter 	struct list_head head;
102321a95aeSDaniel Vetter 
103321a95aeSDaniel Vetter 	struct drm_mode_object base;
104321a95aeSDaniel Vetter 	char *name;
105e03e6de0SDaniel Vetter 	/**
106e03e6de0SDaniel Vetter 	 * @encoder_type:
107e03e6de0SDaniel Vetter 	 *
108e03e6de0SDaniel Vetter 	 * One of the DRM_MODE_ENCODER_<foo> types in drm_mode.h. The following
109e03e6de0SDaniel Vetter 	 * encoder types are defined thus far:
110e03e6de0SDaniel Vetter 	 *
111e03e6de0SDaniel Vetter 	 * - DRM_MODE_ENCODER_DAC for VGA and analog on DVI-I/DVI-A.
112e03e6de0SDaniel Vetter 	 *
113e03e6de0SDaniel Vetter 	 * - DRM_MODE_ENCODER_TMDS for DVI, HDMI and (embedded) DisplayPort.
114e03e6de0SDaniel Vetter 	 *
115e03e6de0SDaniel Vetter 	 * - DRM_MODE_ENCODER_LVDS for display panels, or in general any panel
116e03e6de0SDaniel Vetter 	 *   with a proprietary parallel connector.
117e03e6de0SDaniel Vetter 	 *
118e03e6de0SDaniel Vetter 	 * - DRM_MODE_ENCODER_TVDAC for TV output (Composite, S-Video,
119e03e6de0SDaniel Vetter 	 *   Component, SCART).
120e03e6de0SDaniel Vetter 	 *
121e03e6de0SDaniel Vetter 	 * - DRM_MODE_ENCODER_VIRTUAL for virtual machine displays
122e03e6de0SDaniel Vetter 	 *
123e03e6de0SDaniel Vetter 	 * - DRM_MODE_ENCODER_DSI for panels connected using the DSI serial bus.
124e03e6de0SDaniel Vetter 	 *
125e03e6de0SDaniel Vetter 	 * - DRM_MODE_ENCODER_DPI for panels connected using the DPI parallel
126e03e6de0SDaniel Vetter 	 *   bus.
127e03e6de0SDaniel Vetter 	 *
128e03e6de0SDaniel Vetter 	 * - DRM_MODE_ENCODER_DPMST for special fake encoders used to allow
129e03e6de0SDaniel Vetter 	 *   mutliple DP MST streams to share one physical encoder.
130e03e6de0SDaniel Vetter 	 */
131321a95aeSDaniel Vetter 	int encoder_type;
132321a95aeSDaniel Vetter 
133321a95aeSDaniel Vetter 	/**
134321a95aeSDaniel Vetter 	 * @index: Position inside the mode_config.list, can be used as an array
135321a95aeSDaniel Vetter 	 * index. It is invariant over the lifetime of the encoder.
136321a95aeSDaniel Vetter 	 */
137321a95aeSDaniel Vetter 	unsigned index;
138321a95aeSDaniel Vetter 
139e03e6de0SDaniel Vetter 	/**
140e03e6de0SDaniel Vetter 	 * @possible_crtcs: Bitmask of potential CRTC bindings, using
141e03e6de0SDaniel Vetter 	 * drm_crtc_index() as the index into the bitfield. The driver must set
142e03e6de0SDaniel Vetter 	 * the bits for all &drm_crtc objects this encoder can be connected to
143e03e6de0SDaniel Vetter 	 * before calling drm_encoder_init().
144e03e6de0SDaniel Vetter 	 *
145e03e6de0SDaniel Vetter 	 * In reality almost every driver gets this wrong.
146e03e6de0SDaniel Vetter 	 *
147e03e6de0SDaniel Vetter 	 * Note that since CRTC objects can't be hotplugged the assigned indices
148e03e6de0SDaniel Vetter 	 * are stable and hence known before registering all objects.
149e03e6de0SDaniel Vetter 	 */
150321a95aeSDaniel Vetter 	uint32_t possible_crtcs;
151e03e6de0SDaniel Vetter 
152e03e6de0SDaniel Vetter 	/**
153e03e6de0SDaniel Vetter 	 * @possible_clones: Bitmask of potential sibling encoders for cloning,
154e03e6de0SDaniel Vetter 	 * using drm_encoder_index() as the index into the bitfield. The driver
155e03e6de0SDaniel Vetter 	 * must set the bits for all &drm_encoder objects which can clone a
156e03e6de0SDaniel Vetter 	 * &drm_crtc together with this encoder before calling
157e03e6de0SDaniel Vetter 	 * drm_encoder_init(). Drivers should set the bit representing the
158e03e6de0SDaniel Vetter 	 * encoder itself, too. Cloning bits should be set such that when two
159e03e6de0SDaniel Vetter 	 * encoders can be used in a cloned configuration, they both should have
160e03e6de0SDaniel Vetter 	 * each another bits set.
161e03e6de0SDaniel Vetter 	 *
162e03e6de0SDaniel Vetter 	 * In reality almost every driver gets this wrong.
163e03e6de0SDaniel Vetter 	 *
164e03e6de0SDaniel Vetter 	 * Note that since encoder objects can't be hotplugged the assigned indices
165e03e6de0SDaniel Vetter 	 * are stable and hence known before registering all objects.
166e03e6de0SDaniel Vetter 	 */
167321a95aeSDaniel Vetter 	uint32_t possible_clones;
168321a95aeSDaniel Vetter 
169321a95aeSDaniel Vetter 	struct drm_crtc *crtc;
170321a95aeSDaniel Vetter 	struct drm_bridge *bridge;
171321a95aeSDaniel Vetter 	const struct drm_encoder_funcs *funcs;
172321a95aeSDaniel Vetter 	const struct drm_encoder_helper_funcs *helper_private;
173321a95aeSDaniel Vetter };
174321a95aeSDaniel Vetter 
175321a95aeSDaniel Vetter #define obj_to_encoder(x) container_of(x, struct drm_encoder, base)
176321a95aeSDaniel Vetter 
177321a95aeSDaniel Vetter __printf(5, 6)
178321a95aeSDaniel Vetter int drm_encoder_init(struct drm_device *dev,
179321a95aeSDaniel Vetter 		     struct drm_encoder *encoder,
180321a95aeSDaniel Vetter 		     const struct drm_encoder_funcs *funcs,
181321a95aeSDaniel Vetter 		     int encoder_type, const char *name, ...);
182321a95aeSDaniel Vetter 
183321a95aeSDaniel Vetter /**
184321a95aeSDaniel Vetter  * drm_encoder_index - find the index of a registered encoder
185321a95aeSDaniel Vetter  * @encoder: encoder to find index for
186321a95aeSDaniel Vetter  *
187321a95aeSDaniel Vetter  * Given a registered encoder, return the index of that encoder within a DRM
188321a95aeSDaniel Vetter  * device's list of encoders.
189321a95aeSDaniel Vetter  */
190321a95aeSDaniel Vetter static inline unsigned int drm_encoder_index(struct drm_encoder *encoder)
191321a95aeSDaniel Vetter {
192321a95aeSDaniel Vetter 	return encoder->index;
193321a95aeSDaniel Vetter }
194321a95aeSDaniel Vetter 
195321a95aeSDaniel Vetter /**
196321a95aeSDaniel Vetter  * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
197321a95aeSDaniel Vetter  * @encoder: encoder to test
198321a95aeSDaniel Vetter  * @crtc: crtc to test
199321a95aeSDaniel Vetter  *
200e03e6de0SDaniel Vetter  * Returns false if @encoder can't be driven by @crtc, true otherwise.
201321a95aeSDaniel Vetter  */
202321a95aeSDaniel Vetter static inline bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
203321a95aeSDaniel Vetter 				       struct drm_crtc *crtc)
204321a95aeSDaniel Vetter {
205321a95aeSDaniel Vetter 	return !!(encoder->possible_crtcs & drm_crtc_mask(crtc));
206321a95aeSDaniel Vetter }
207321a95aeSDaniel Vetter 
208e03e6de0SDaniel Vetter /**
209e03e6de0SDaniel Vetter  * drm_encoder_find - find a &drm_encoder
210e03e6de0SDaniel Vetter  * @dev: DRM device
211e03e6de0SDaniel Vetter  * @id: encoder id
212e03e6de0SDaniel Vetter  *
213e03e6de0SDaniel Vetter  * Returns the encoder with @id, NULL if it doesn't exist. Simple wrapper around
214e03e6de0SDaniel Vetter  * drm_mode_object_find().
215e03e6de0SDaniel Vetter  */
216321a95aeSDaniel Vetter static inline struct drm_encoder *drm_encoder_find(struct drm_device *dev,
217321a95aeSDaniel Vetter 						   uint32_t id)
218321a95aeSDaniel Vetter {
219321a95aeSDaniel Vetter 	struct drm_mode_object *mo;
220e03e6de0SDaniel Vetter 
221321a95aeSDaniel Vetter 	mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
222e03e6de0SDaniel Vetter 
223321a95aeSDaniel Vetter 	return mo ? obj_to_encoder(mo) : NULL;
224321a95aeSDaniel Vetter }
225321a95aeSDaniel Vetter 
226321a95aeSDaniel Vetter void drm_encoder_cleanup(struct drm_encoder *encoder);
227321a95aeSDaniel Vetter 
228afb21ea6SDaniel Vetter /**
229afb21ea6SDaniel Vetter  * drm_for_each_encoder_mask - iterate over encoders specified by bitmask
230afb21ea6SDaniel Vetter  * @encoder: the loop cursor
231afb21ea6SDaniel Vetter  * @dev: the DRM device
232afb21ea6SDaniel Vetter  * @encoder_mask: bitmask of encoder indices
233afb21ea6SDaniel Vetter  *
234afb21ea6SDaniel Vetter  * Iterate over all encoders specified by bitmask.
235afb21ea6SDaniel Vetter  */
236afb21ea6SDaniel Vetter #define drm_for_each_encoder_mask(encoder, dev, encoder_mask) \
237afb21ea6SDaniel Vetter 	list_for_each_entry((encoder), &(dev)->mode_config.encoder_list, head) \
238afb21ea6SDaniel Vetter 		for_each_if ((encoder_mask) & (1 << drm_encoder_index(encoder)))
239afb21ea6SDaniel Vetter 
240afb21ea6SDaniel Vetter /**
241afb21ea6SDaniel Vetter  * drm_for_each_encoder - iterate over all encoders
242afb21ea6SDaniel Vetter  * @encoder: the loop cursor
243afb21ea6SDaniel Vetter  * @dev: the DRM device
244afb21ea6SDaniel Vetter  *
245afb21ea6SDaniel Vetter  * Iterate over all encoders of @dev.
246afb21ea6SDaniel Vetter  */
247afb21ea6SDaniel Vetter #define drm_for_each_encoder(encoder, dev) \
248afb21ea6SDaniel Vetter 	list_for_each_entry(encoder, &(dev)->mode_config.encoder_list, head)
249afb21ea6SDaniel Vetter 
250321a95aeSDaniel Vetter #endif
251