drm_encoder.h 11 KB

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