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>
282407d1dcSLaurent Pinchart #include <drm/drm_crtc.h>
299338203cSLaurent Pinchart #include <drm/drm_mode.h>
30949619f3SDaniel Vetter #include <drm/drm_mode_object.h>
31d78aa650SDaniel Vetter #include <drm/drm_util.h>
32321a95aeSDaniel Vetter
339338203cSLaurent Pinchart struct drm_encoder;
349338203cSLaurent Pinchart
35321a95aeSDaniel Vetter /**
36321a95aeSDaniel Vetter * struct drm_encoder_funcs - encoder controls
37321a95aeSDaniel Vetter *
38321a95aeSDaniel Vetter * Encoders sit between CRTCs and connectors.
39321a95aeSDaniel Vetter */
40321a95aeSDaniel Vetter struct drm_encoder_funcs {
41321a95aeSDaniel Vetter /**
42321a95aeSDaniel Vetter * @reset:
43321a95aeSDaniel Vetter *
44321a95aeSDaniel Vetter * Reset encoder hardware and software state to off. This function isn't
45321a95aeSDaniel Vetter * called by the core directly, only through drm_mode_config_reset().
46321a95aeSDaniel Vetter * It's not a helper hook only for historical reasons.
47321a95aeSDaniel Vetter */
48321a95aeSDaniel Vetter void (*reset)(struct drm_encoder *encoder);
49321a95aeSDaniel Vetter
50321a95aeSDaniel Vetter /**
51321a95aeSDaniel Vetter * @destroy:
52321a95aeSDaniel Vetter *
53321a95aeSDaniel Vetter * Clean up encoder resources. This is only called at driver unload time
54321a95aeSDaniel Vetter * through drm_mode_config_cleanup() since an encoder cannot be
55321a95aeSDaniel Vetter * hotplugged in DRM.
56321a95aeSDaniel Vetter */
57321a95aeSDaniel Vetter void (*destroy)(struct drm_encoder *encoder);
58321a95aeSDaniel Vetter
59321a95aeSDaniel Vetter /**
60321a95aeSDaniel Vetter * @late_register:
61321a95aeSDaniel Vetter *
62321a95aeSDaniel Vetter * This optional hook can be used to register additional userspace
63*caf525edSDmitry Baryshkov * interfaces attached to the encoder.
64321a95aeSDaniel Vetter * It is called late in the driver load sequence from drm_dev_register().
65321a95aeSDaniel Vetter * Everything added from this callback should be unregistered in
66321a95aeSDaniel Vetter * the early_unregister callback.
67321a95aeSDaniel Vetter *
68321a95aeSDaniel Vetter * Returns:
69321a95aeSDaniel Vetter *
70321a95aeSDaniel Vetter * 0 on success, or a negative error code on failure.
71321a95aeSDaniel Vetter */
72321a95aeSDaniel Vetter int (*late_register)(struct drm_encoder *encoder);
73321a95aeSDaniel Vetter
74321a95aeSDaniel Vetter /**
75321a95aeSDaniel Vetter * @early_unregister:
76321a95aeSDaniel Vetter *
77321a95aeSDaniel Vetter * This optional hook should be used to unregister the additional
78321a95aeSDaniel Vetter * userspace interfaces attached to the encoder from
79559bdaf7SDaniel Vetter * @late_register. It is called from drm_dev_unregister(),
80321a95aeSDaniel Vetter * early in the driver unload sequence to disable userspace access
81321a95aeSDaniel Vetter * before data structures are torndown.
82321a95aeSDaniel Vetter */
83321a95aeSDaniel Vetter void (*early_unregister)(struct drm_encoder *encoder);
84*caf525edSDmitry Baryshkov
85*caf525edSDmitry Baryshkov /**
86*caf525edSDmitry Baryshkov * @debugfs_init:
87*caf525edSDmitry Baryshkov *
88*caf525edSDmitry Baryshkov * Allows encoders to create encoder-specific debugfs files.
89*caf525edSDmitry Baryshkov */
90*caf525edSDmitry Baryshkov void (*debugfs_init)(struct drm_encoder *encoder, struct dentry *root);
91321a95aeSDaniel Vetter };
92321a95aeSDaniel Vetter
93321a95aeSDaniel Vetter /**
94321a95aeSDaniel Vetter * struct drm_encoder - central DRM encoder structure
95321a95aeSDaniel Vetter * @dev: parent DRM device
96321a95aeSDaniel Vetter * @head: list management
97321a95aeSDaniel Vetter * @base: base KMS object
98321a95aeSDaniel Vetter * @name: human readable name, can be overwritten by the driver
99f35a2a99SPhilipp Zabel * @funcs: control functions, can be NULL for simple managed encoders
100321a95aeSDaniel Vetter * @helper_private: mid-layer private data
101321a95aeSDaniel Vetter *
102321a95aeSDaniel Vetter * CRTCs drive pixels to encoders, which convert them into signals
103321a95aeSDaniel Vetter * appropriate for a given connector or set of connectors.
104321a95aeSDaniel Vetter */
105321a95aeSDaniel Vetter struct drm_encoder {
106321a95aeSDaniel Vetter struct drm_device *dev;
107321a95aeSDaniel Vetter struct list_head head;
108321a95aeSDaniel Vetter
109321a95aeSDaniel Vetter struct drm_mode_object base;
110321a95aeSDaniel Vetter char *name;
111e03e6de0SDaniel Vetter /**
112e03e6de0SDaniel Vetter * @encoder_type:
113e03e6de0SDaniel Vetter *
114e03e6de0SDaniel Vetter * One of the DRM_MODE_ENCODER_<foo> types in drm_mode.h. The following
115e03e6de0SDaniel Vetter * encoder types are defined thus far:
116e03e6de0SDaniel Vetter *
117e03e6de0SDaniel Vetter * - DRM_MODE_ENCODER_DAC for VGA and analog on DVI-I/DVI-A.
118e03e6de0SDaniel Vetter *
119e03e6de0SDaniel Vetter * - DRM_MODE_ENCODER_TMDS for DVI, HDMI and (embedded) DisplayPort.
120e03e6de0SDaniel Vetter *
121e03e6de0SDaniel Vetter * - DRM_MODE_ENCODER_LVDS for display panels, or in general any panel
122e03e6de0SDaniel Vetter * with a proprietary parallel connector.
123e03e6de0SDaniel Vetter *
124e03e6de0SDaniel Vetter * - DRM_MODE_ENCODER_TVDAC for TV output (Composite, S-Video,
125e03e6de0SDaniel Vetter * Component, SCART).
126e03e6de0SDaniel Vetter *
127e03e6de0SDaniel Vetter * - DRM_MODE_ENCODER_VIRTUAL for virtual machine displays
128e03e6de0SDaniel Vetter *
129e03e6de0SDaniel Vetter * - DRM_MODE_ENCODER_DSI for panels connected using the DSI serial bus.
130e03e6de0SDaniel Vetter *
131e03e6de0SDaniel Vetter * - DRM_MODE_ENCODER_DPI for panels connected using the DPI parallel
132e03e6de0SDaniel Vetter * bus.
133e03e6de0SDaniel Vetter *
134e03e6de0SDaniel Vetter * - DRM_MODE_ENCODER_DPMST for special fake encoders used to allow
135e03e6de0SDaniel Vetter * mutliple DP MST streams to share one physical encoder.
136e03e6de0SDaniel Vetter */
137321a95aeSDaniel Vetter int encoder_type;
138321a95aeSDaniel Vetter
139321a95aeSDaniel Vetter /**
140321a95aeSDaniel Vetter * @index: Position inside the mode_config.list, can be used as an array
141321a95aeSDaniel Vetter * index. It is invariant over the lifetime of the encoder.
142321a95aeSDaniel Vetter */
143321a95aeSDaniel Vetter unsigned index;
144321a95aeSDaniel Vetter
145e03e6de0SDaniel Vetter /**
146e03e6de0SDaniel Vetter * @possible_crtcs: Bitmask of potential CRTC bindings, using
147e03e6de0SDaniel Vetter * drm_crtc_index() as the index into the bitfield. The driver must set
148e03e6de0SDaniel Vetter * the bits for all &drm_crtc objects this encoder can be connected to
149c873724dSLyude Paul * before calling drm_dev_register().
150e03e6de0SDaniel Vetter *
1510df10823SVille Syrjälä * You will get a WARN if you get this wrong in the driver.
152e03e6de0SDaniel Vetter *
153e03e6de0SDaniel Vetter * Note that since CRTC objects can't be hotplugged the assigned indices
154e03e6de0SDaniel Vetter * are stable and hence known before registering all objects.
155e03e6de0SDaniel Vetter */
156321a95aeSDaniel Vetter uint32_t possible_crtcs;
157e03e6de0SDaniel Vetter
158e03e6de0SDaniel Vetter /**
159e03e6de0SDaniel Vetter * @possible_clones: Bitmask of potential sibling encoders for cloning,
160e03e6de0SDaniel Vetter * using drm_encoder_index() as the index into the bitfield. The driver
161e03e6de0SDaniel Vetter * must set the bits for all &drm_encoder objects which can clone a
162e03e6de0SDaniel Vetter * &drm_crtc together with this encoder before calling
1638c8ae2feSLyude Paul * drm_dev_register(). Drivers should set the bit representing the
164e03e6de0SDaniel Vetter * encoder itself, too. Cloning bits should be set such that when two
165e03e6de0SDaniel Vetter * encoders can be used in a cloned configuration, they both should have
166e03e6de0SDaniel Vetter * each another bits set.
167e03e6de0SDaniel Vetter *
16871427795SVille Syrjälä * As an exception to the above rule if the driver doesn't implement
16971427795SVille Syrjälä * any cloning it can leave @possible_clones set to 0. The core will
17071427795SVille Syrjälä * automagically fix this up by setting the bit for the encoder itself.
171e03e6de0SDaniel Vetter *
17274d2aacbSVille Syrjälä * You will get a WARN if you get this wrong in the driver.
17374d2aacbSVille Syrjälä *
174e03e6de0SDaniel Vetter * Note that since encoder objects can't be hotplugged the assigned indices
175e03e6de0SDaniel Vetter * are stable and hence known before registering all objects.
176e03e6de0SDaniel Vetter */
177321a95aeSDaniel Vetter uint32_t possible_clones;
178321a95aeSDaniel Vetter
1792e2b96efSDaniel Vetter /**
1802e2b96efSDaniel Vetter * @crtc: Currently bound CRTC, only really meaningful for non-atomic
1812e2b96efSDaniel Vetter * drivers. Atomic drivers should instead check
1822e2b96efSDaniel Vetter * &drm_connector_state.crtc.
1832e2b96efSDaniel Vetter */
184321a95aeSDaniel Vetter struct drm_crtc *crtc;
18505193dc3SBoris Brezillon
18605193dc3SBoris Brezillon /**
187be39f3dbSLaurent Pinchart * @bridge_chain: Bridges attached to this encoder. Drivers shall not
188be39f3dbSLaurent Pinchart * access this field directly.
18905193dc3SBoris Brezillon */
19005193dc3SBoris Brezillon struct list_head bridge_chain;
19105193dc3SBoris Brezillon
192321a95aeSDaniel Vetter const struct drm_encoder_funcs *funcs;
193321a95aeSDaniel Vetter const struct drm_encoder_helper_funcs *helper_private;
194*caf525edSDmitry Baryshkov
195*caf525edSDmitry Baryshkov /**
196*caf525edSDmitry Baryshkov * @debugfs_entry:
197*caf525edSDmitry Baryshkov *
198*caf525edSDmitry Baryshkov * Debugfs directory for this CRTC.
199*caf525edSDmitry Baryshkov */
200*caf525edSDmitry Baryshkov struct dentry *debugfs_entry;
201321a95aeSDaniel Vetter };
202321a95aeSDaniel Vetter
203321a95aeSDaniel Vetter #define obj_to_encoder(x) container_of(x, struct drm_encoder, base)
204321a95aeSDaniel Vetter
205321a95aeSDaniel Vetter __printf(5, 6)
206321a95aeSDaniel Vetter int drm_encoder_init(struct drm_device *dev,
207321a95aeSDaniel Vetter struct drm_encoder *encoder,
208321a95aeSDaniel Vetter const struct drm_encoder_funcs *funcs,
209321a95aeSDaniel Vetter int encoder_type, const char *name, ...);
210321a95aeSDaniel Vetter
211f134c9cdSMaxime Ripard __printf(5, 6)
212f134c9cdSMaxime Ripard int drmm_encoder_init(struct drm_device *dev,
213f134c9cdSMaxime Ripard struct drm_encoder *encoder,
214f134c9cdSMaxime Ripard const struct drm_encoder_funcs *funcs,
215f134c9cdSMaxime Ripard int encoder_type, const char *name, ...);
216f134c9cdSMaxime Ripard
217ca5092d0SPhilipp Zabel __printf(6, 7)
218ca5092d0SPhilipp Zabel void *__drmm_encoder_alloc(struct drm_device *dev,
219ca5092d0SPhilipp Zabel size_t size, size_t offset,
220ca5092d0SPhilipp Zabel const struct drm_encoder_funcs *funcs,
221ca5092d0SPhilipp Zabel int encoder_type,
222ca5092d0SPhilipp Zabel const char *name, ...);
223ca5092d0SPhilipp Zabel
224ca5092d0SPhilipp Zabel /**
225ca5092d0SPhilipp Zabel * drmm_encoder_alloc - Allocate and initialize an encoder
226ca5092d0SPhilipp Zabel * @dev: drm device
227ca5092d0SPhilipp Zabel * @type: the type of the struct which contains struct &drm_encoder
228ca5092d0SPhilipp Zabel * @member: the name of the &drm_encoder within @type
229ca5092d0SPhilipp Zabel * @funcs: callbacks for this encoder (optional)
230ca5092d0SPhilipp Zabel * @encoder_type: user visible type of the encoder
231ca5092d0SPhilipp Zabel * @name: printf style format string for the encoder name, or NULL for default name
232ca5092d0SPhilipp Zabel *
233ca5092d0SPhilipp Zabel * Allocates and initializes an encoder. Encoder should be subclassed as part of
234ca5092d0SPhilipp Zabel * driver encoder objects. Cleanup is automatically handled through registering
235ca5092d0SPhilipp Zabel * drm_encoder_cleanup() with drmm_add_action().
236ca5092d0SPhilipp Zabel *
237ca5092d0SPhilipp Zabel * The @drm_encoder_funcs.destroy hook must be NULL.
238ca5092d0SPhilipp Zabel *
239ca5092d0SPhilipp Zabel * Returns:
240ca5092d0SPhilipp Zabel * Pointer to new encoder, or ERR_PTR on failure.
241ca5092d0SPhilipp Zabel */
242ca5092d0SPhilipp Zabel #define drmm_encoder_alloc(dev, type, member, funcs, encoder_type, name, ...) \
243ca5092d0SPhilipp Zabel ((type *)__drmm_encoder_alloc(dev, sizeof(type), \
244ca5092d0SPhilipp Zabel offsetof(type, member), funcs, \
245ca5092d0SPhilipp Zabel encoder_type, name, ##__VA_ARGS__))
246ca5092d0SPhilipp Zabel
247321a95aeSDaniel Vetter /**
248c7b04342SPaul Cercueil * drmm_plain_encoder_alloc - Allocate and initialize an encoder
249c7b04342SPaul Cercueil * @dev: drm device
250c7b04342SPaul Cercueil * @funcs: callbacks for this encoder (optional)
251c7b04342SPaul Cercueil * @encoder_type: user visible type of the encoder
252c7b04342SPaul Cercueil * @name: printf style format string for the encoder name, or NULL for default name
253c7b04342SPaul Cercueil *
254c7b04342SPaul Cercueil * This is a simplified version of drmm_encoder_alloc(), which only allocates
255c7b04342SPaul Cercueil * and returns a struct drm_encoder instance, with no subclassing.
256c7b04342SPaul Cercueil *
257c7b04342SPaul Cercueil * Returns:
258c7b04342SPaul Cercueil * Pointer to the new drm_encoder struct, or ERR_PTR on failure.
259c7b04342SPaul Cercueil */
260c7b04342SPaul Cercueil #define drmm_plain_encoder_alloc(dev, funcs, encoder_type, name, ...) \
261c7b04342SPaul Cercueil ((struct drm_encoder *) \
262c7b04342SPaul Cercueil __drmm_encoder_alloc(dev, sizeof(struct drm_encoder), \
263c7b04342SPaul Cercueil 0, funcs, encoder_type, name, ##__VA_ARGS__))
264c7b04342SPaul Cercueil
265c7b04342SPaul Cercueil /**
266321a95aeSDaniel Vetter * drm_encoder_index - find the index of a registered encoder
267321a95aeSDaniel Vetter * @encoder: encoder to find index for
268321a95aeSDaniel Vetter *
269321a95aeSDaniel Vetter * Given a registered encoder, return the index of that encoder within a DRM
270321a95aeSDaniel Vetter * device's list of encoders.
271321a95aeSDaniel Vetter */
drm_encoder_index(const struct drm_encoder * encoder)2726f3be036SVille Syrjälä static inline unsigned int drm_encoder_index(const struct drm_encoder *encoder)
273321a95aeSDaniel Vetter {
274321a95aeSDaniel Vetter return encoder->index;
275321a95aeSDaniel Vetter }
276321a95aeSDaniel Vetter
277321a95aeSDaniel Vetter /**
278a859f127SLyude Paul * drm_encoder_mask - find the mask of a registered encoder
2796f3be036SVille Syrjälä * @encoder: encoder to find mask for
2806f3be036SVille Syrjälä *
2816f3be036SVille Syrjälä * Given a registered encoder, return the mask bit of that encoder for an
2826f3be036SVille Syrjälä * encoder's possible_clones field.
2836f3be036SVille Syrjälä */
drm_encoder_mask(const struct drm_encoder * encoder)2846f3be036SVille Syrjälä static inline u32 drm_encoder_mask(const struct drm_encoder *encoder)
2856f3be036SVille Syrjälä {
2866f3be036SVille Syrjälä return 1 << drm_encoder_index(encoder);
2876f3be036SVille Syrjälä }
2886f3be036SVille Syrjälä
2896f3be036SVille Syrjälä /**
290321a95aeSDaniel Vetter * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
291321a95aeSDaniel Vetter * @encoder: encoder to test
292321a95aeSDaniel Vetter * @crtc: crtc to test
293321a95aeSDaniel Vetter *
294e03e6de0SDaniel Vetter * Returns false if @encoder can't be driven by @crtc, true otherwise.
295321a95aeSDaniel Vetter */
drm_encoder_crtc_ok(struct drm_encoder * encoder,struct drm_crtc * crtc)296321a95aeSDaniel Vetter static inline bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
297321a95aeSDaniel Vetter struct drm_crtc *crtc)
298321a95aeSDaniel Vetter {
299321a95aeSDaniel Vetter return !!(encoder->possible_crtcs & drm_crtc_mask(crtc));
300321a95aeSDaniel Vetter }
301321a95aeSDaniel Vetter
302e03e6de0SDaniel Vetter /**
303e03e6de0SDaniel Vetter * drm_encoder_find - find a &drm_encoder
304e03e6de0SDaniel Vetter * @dev: DRM device
305e7e62c7eSDave Airlie * @file_priv: drm file to check for lease against.
306e03e6de0SDaniel Vetter * @id: encoder id
307e03e6de0SDaniel Vetter *
308e03e6de0SDaniel Vetter * Returns the encoder with @id, NULL if it doesn't exist. Simple wrapper around
309e03e6de0SDaniel Vetter * drm_mode_object_find().
310e03e6de0SDaniel Vetter */
drm_encoder_find(struct drm_device * dev,struct drm_file * file_priv,uint32_t id)311321a95aeSDaniel Vetter static inline struct drm_encoder *drm_encoder_find(struct drm_device *dev,
312418da172SKeith Packard struct drm_file *file_priv,
313321a95aeSDaniel Vetter uint32_t id)
314321a95aeSDaniel Vetter {
315321a95aeSDaniel Vetter struct drm_mode_object *mo;
316e03e6de0SDaniel Vetter
317418da172SKeith Packard mo = drm_mode_object_find(dev, file_priv, id, DRM_MODE_OBJECT_ENCODER);
318e03e6de0SDaniel Vetter
319321a95aeSDaniel Vetter return mo ? obj_to_encoder(mo) : NULL;
320321a95aeSDaniel Vetter }
321321a95aeSDaniel Vetter
322321a95aeSDaniel Vetter void drm_encoder_cleanup(struct drm_encoder *encoder);
323321a95aeSDaniel Vetter
324afb21ea6SDaniel Vetter /**
325afb21ea6SDaniel Vetter * drm_for_each_encoder_mask - iterate over encoders specified by bitmask
326afb21ea6SDaniel Vetter * @encoder: the loop cursor
327afb21ea6SDaniel Vetter * @dev: the DRM device
328afb21ea6SDaniel Vetter * @encoder_mask: bitmask of encoder indices
329afb21ea6SDaniel Vetter *
330afb21ea6SDaniel Vetter * Iterate over all encoders specified by bitmask.
331afb21ea6SDaniel Vetter */
332afb21ea6SDaniel Vetter #define drm_for_each_encoder_mask(encoder, dev, encoder_mask) \
333afb21ea6SDaniel Vetter list_for_each_entry((encoder), &(dev)->mode_config.encoder_list, head) \
3346f3be036SVille Syrjälä for_each_if ((encoder_mask) & drm_encoder_mask(encoder))
335afb21ea6SDaniel Vetter
336afb21ea6SDaniel Vetter /**
337afb21ea6SDaniel Vetter * drm_for_each_encoder - iterate over all encoders
338afb21ea6SDaniel Vetter * @encoder: the loop cursor
339afb21ea6SDaniel Vetter * @dev: the DRM device
340afb21ea6SDaniel Vetter *
341afb21ea6SDaniel Vetter * Iterate over all encoders of @dev.
342afb21ea6SDaniel Vetter */
343afb21ea6SDaniel Vetter #define drm_for_each_encoder(encoder, dev) \
344afb21ea6SDaniel Vetter list_for_each_entry(encoder, &(dev)->mode_config.encoder_list, head)
345afb21ea6SDaniel Vetter
346321a95aeSDaniel Vetter #endif
347