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