1 /*        $NetBSD: drm_encoder.h,v 1.2 2021/12/18 23:45:45 riastradh Exp $      */
2 
3 /*
4  * Copyright (c) 2016 Intel Corporation
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that copyright
9  * notice and this permission notice appear in supporting documentation, and
10  * that the name of the copyright holders not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  The copyright holders make no representations
13  * about the suitability of this software for any purpose.  It is provided "as
14  * is" without express or implied warranty.
15  *
16  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22  * OF THIS SOFTWARE.
23  */
24 
25 #ifndef __DRM_ENCODER_H__
26 #define __DRM_ENCODER_H__
27 
28 #include <linux/list.h>
29 #include <linux/ctype.h>
30 #include <drm/drm_crtc.h>
31 #include <drm/drm_mode.h>
32 #include <drm/drm_mode_object.h>
33 #include <drm/drm_util.h>
34 
35 struct drm_encoder;
36 
37 /**
38  * struct drm_encoder_funcs - encoder controls
39  *
40  * Encoders sit between CRTCs and connectors.
41  */
42 struct drm_encoder_funcs {
43           /**
44            * @reset:
45            *
46            * Reset encoder hardware and software state to off. This function isn't
47            * called by the core directly, only through drm_mode_config_reset().
48            * It's not a helper hook only for historical reasons.
49            */
50           void (*reset)(struct drm_encoder *encoder);
51 
52           /**
53            * @destroy:
54            *
55            * Clean up encoder resources. This is only called at driver unload time
56            * through drm_mode_config_cleanup() since an encoder cannot be
57            * hotplugged in DRM.
58            */
59           void (*destroy)(struct drm_encoder *encoder);
60 
61           /**
62            * @late_register:
63            *
64            * This optional hook can be used to register additional userspace
65            * interfaces attached to the encoder like debugfs interfaces.
66            * It is called late in the driver load sequence from drm_dev_register().
67            * Everything added from this callback should be unregistered in
68            * the early_unregister callback.
69            *
70            * Returns:
71            *
72            * 0 on success, or a negative error code on failure.
73            */
74           int (*late_register)(struct drm_encoder *encoder);
75 
76           /**
77            * @early_unregister:
78            *
79            * This optional hook should be used to unregister the additional
80            * userspace interfaces attached to the encoder from
81            * @late_register. It is called from drm_dev_unregister(),
82            * early in the driver unload sequence to disable userspace access
83            * before data structures are torndown.
84            */
85           void (*early_unregister)(struct drm_encoder *encoder);
86 };
87 
88 /**
89  * struct drm_encoder - central DRM encoder structure
90  * @dev: parent DRM device
91  * @head: list management
92  * @base: base KMS object
93  * @name: human readable name, can be overwritten by the driver
94  * @bridge: bridge associated to the encoder
95  * @funcs: control functions
96  * @helper_private: mid-layer private data
97  *
98  * CRTCs drive pixels to encoders, which convert them into signals
99  * appropriate for a given connector or set of connectors.
100  */
101 struct drm_encoder {
102           struct drm_device *dev;
103           struct list_head head;
104 
105           struct drm_mode_object base;
106           char *name;
107           /**
108            * @encoder_type:
109            *
110            * One of the DRM_MODE_ENCODER_<foo> types in drm_mode.h. The following
111            * encoder types are defined thus far:
112            *
113            * - DRM_MODE_ENCODER_DAC for VGA and analog on DVI-I/DVI-A.
114            *
115            * - DRM_MODE_ENCODER_TMDS for DVI, HDMI and (embedded) DisplayPort.
116            *
117            * - DRM_MODE_ENCODER_LVDS for display panels, or in general any panel
118            *   with a proprietary parallel connector.
119            *
120            * - DRM_MODE_ENCODER_TVDAC for TV output (Composite, S-Video,
121            *   Component, SCART).
122            *
123            * - DRM_MODE_ENCODER_VIRTUAL for virtual machine displays
124            *
125            * - DRM_MODE_ENCODER_DSI for panels connected using the DSI serial bus.
126            *
127            * - DRM_MODE_ENCODER_DPI for panels connected using the DPI parallel
128            *   bus.
129            *
130            * - DRM_MODE_ENCODER_DPMST for special fake encoders used to allow
131            *   mutliple DP MST streams to share one physical encoder.
132            */
133           int encoder_type;
134 
135           /**
136            * @index: Position inside the mode_config.list, can be used as an array
137            * index. It is invariant over the lifetime of the encoder.
138            */
139           unsigned index;
140 
141           /**
142            * @possible_crtcs: Bitmask of potential CRTC bindings, using
143            * drm_crtc_index() as the index into the bitfield. The driver must set
144            * the bits for all &drm_crtc objects this encoder can be connected to
145            * before calling drm_dev_register().
146            *
147            * In reality almost every driver gets this wrong.
148            *
149            * Note that since CRTC objects can't be hotplugged the assigned indices
150            * are stable and hence known before registering all objects.
151            */
152           uint32_t possible_crtcs;
153 
154           /**
155            * @possible_clones: Bitmask of potential sibling encoders for cloning,
156            * using drm_encoder_index() as the index into the bitfield. The driver
157            * must set the bits for all &drm_encoder objects which can clone a
158            * &drm_crtc together with this encoder before calling
159            * drm_dev_register(). Drivers should set the bit representing the
160            * encoder itself, too. Cloning bits should be set such that when two
161            * encoders can be used in a cloned configuration, they both should have
162            * each another bits set.
163            *
164            * In reality almost every driver gets this wrong.
165            *
166            * Note that since encoder objects can't be hotplugged the assigned indices
167            * are stable and hence known before registering all objects.
168            */
169           uint32_t possible_clones;
170 
171           /**
172            * @crtc: Currently bound CRTC, only really meaningful for non-atomic
173            * drivers.  Atomic drivers should instead check
174            * &drm_connector_state.crtc.
175            */
176           struct drm_crtc *crtc;
177 
178           /**
179            * @bridge_chain: Bridges attached to this encoder.
180            */
181           struct list_head bridge_chain;
182 
183           const struct drm_encoder_funcs *funcs;
184           const struct drm_encoder_helper_funcs *helper_private;
185 };
186 
187 #define obj_to_encoder(x) container_of(x, struct drm_encoder, base)
188 
189 __printf(5, 6)
190 int drm_encoder_init(struct drm_device *dev,
191                          struct drm_encoder *encoder,
192                          const struct drm_encoder_funcs *funcs,
193                          int encoder_type, const char *name, ...);
194 
195 /**
196  * drm_encoder_index - find the index of a registered encoder
197  * @encoder: encoder to find index for
198  *
199  * Given a registered encoder, return the index of that encoder within a DRM
200  * device's list of encoders.
201  */
drm_encoder_index(const struct drm_encoder * encoder)202 static inline unsigned int drm_encoder_index(const struct drm_encoder *encoder)
203 {
204           return encoder->index;
205 }
206 
207 /**
208  * drm_encoder_mask - find the mask of a registered encoder
209  * @encoder: encoder to find mask for
210  *
211  * Given a registered encoder, return the mask bit of that encoder for an
212  * encoder's possible_clones field.
213  */
drm_encoder_mask(const struct drm_encoder * encoder)214 static inline u32 drm_encoder_mask(const struct drm_encoder *encoder)
215 {
216           return 1 << drm_encoder_index(encoder);
217 }
218 
219 /**
220  * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
221  * @encoder: encoder to test
222  * @crtc: crtc to test
223  *
224  * Returns false if @encoder can't be driven by @crtc, true otherwise.
225  */
drm_encoder_crtc_ok(struct drm_encoder * encoder,struct drm_crtc * crtc)226 static inline bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
227                                                struct drm_crtc *crtc)
228 {
229           return !!(encoder->possible_crtcs & drm_crtc_mask(crtc));
230 }
231 
232 /**
233  * drm_encoder_find - find a &drm_encoder
234  * @dev: DRM device
235  * @file_priv: drm file to check for lease against.
236  * @id: encoder id
237  *
238  * Returns the encoder with @id, NULL if it doesn't exist. Simple wrapper around
239  * drm_mode_object_find().
240  */
drm_encoder_find(struct drm_device * dev,struct drm_file * file_priv,uint32_t id)241 static inline struct drm_encoder *drm_encoder_find(struct drm_device *dev,
242                                                                struct drm_file *file_priv,
243                                                                uint32_t id)
244 {
245           struct drm_mode_object *mo;
246 
247           mo = drm_mode_object_find(dev, file_priv, id, DRM_MODE_OBJECT_ENCODER);
248 
249           return mo ? obj_to_encoder(mo) : NULL;
250 }
251 
252 void drm_encoder_cleanup(struct drm_encoder *encoder);
253 
254 /**
255  * drm_for_each_encoder_mask - iterate over encoders specified by bitmask
256  * @encoder: the loop cursor
257  * @dev: the DRM device
258  * @encoder_mask: bitmask of encoder indices
259  *
260  * Iterate over all encoders specified by bitmask.
261  */
262 #define drm_for_each_encoder_mask(encoder, dev, encoder_mask) \
263           list_for_each_entry((encoder), &(dev)->mode_config.encoder_list, head) \
264                     for_each_if ((encoder_mask) & drm_encoder_mask(encoder))
265 
266 /**
267  * drm_for_each_encoder - iterate over all encoders
268  * @encoder: the loop cursor
269  * @dev: the DRM device
270  *
271  * Iterate over all encoders of @dev.
272  */
273 #define drm_for_each_encoder(encoder, dev) \
274           list_for_each_entry(encoder, &(dev)->mode_config.encoder_list, head)
275 
276 #endif
277