drm_simple_kms_helper.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2016 Noralf Trønnes
  4. */
  5. #include <linux/module.h>
  6. #include <linux/slab.h>
  7. #include <drm/drm_atomic.h>
  8. #include <drm/drm_atomic_helper.h>
  9. #include <drm/drm_bridge.h>
  10. #include <drm/drm_plane_helper.h>
  11. #include <drm/drm_probe_helper.h>
  12. #include <drm/drm_simple_kms_helper.h>
  13. /**
  14. * DOC: overview
  15. *
  16. * This helper library provides helpers for drivers for simple display
  17. * hardware.
  18. *
  19. * drm_simple_display_pipe_init() initializes a simple display pipeline
  20. * which has only one full-screen scanout buffer feeding one output. The
  21. * pipeline is represented by &struct drm_simple_display_pipe and binds
  22. * together &drm_plane, &drm_crtc and &drm_encoder structures into one fixed
  23. * entity. Some flexibility for code reuse is provided through a separately
  24. * allocated &drm_connector object and supporting optional &drm_bridge
  25. * encoder drivers.
  26. *
  27. * Many drivers require only a very simple encoder that fulfills the minimum
  28. * requirements of the display pipeline and does not add additional
  29. * functionality. The function drm_simple_encoder_init() provides an
  30. * implementation of such an encoder.
  31. */
  32. static const struct drm_encoder_funcs drm_simple_encoder_funcs_cleanup = {
  33. .destroy = drm_encoder_cleanup,
  34. };
  35. /**
  36. * drm_simple_encoder_init - Initialize a preallocated encoder with
  37. * basic functionality.
  38. * @dev: drm device
  39. * @encoder: the encoder to initialize
  40. * @encoder_type: user visible type of the encoder
  41. *
  42. * Initialises a preallocated encoder that has no further functionality.
  43. * Settings for possible CRTC and clones are left to their initial values.
  44. * The encoder will be cleaned up automatically as part of the mode-setting
  45. * cleanup.
  46. *
  47. * The caller of drm_simple_encoder_init() is responsible for freeing
  48. * the encoder's memory after the encoder has been cleaned up. At the
  49. * moment this only works reliably if the encoder data structure is
  50. * stored in the device structure. Free the encoder's memory as part of
  51. * the device release function.
  52. *
  53. * FIXME: Later improvements to DRM's resource management may allow for
  54. * an automated kfree() of the encoder's memory.
  55. *
  56. * Returns:
  57. * Zero on success, error code on failure.
  58. */
  59. int drm_simple_encoder_init(struct drm_device *dev,
  60. struct drm_encoder *encoder,
  61. int encoder_type)
  62. {
  63. return drm_encoder_init(dev, encoder,
  64. &drm_simple_encoder_funcs_cleanup,
  65. encoder_type, NULL);
  66. }
  67. EXPORT_SYMBOL(drm_simple_encoder_init);
  68. static enum drm_mode_status
  69. drm_simple_kms_crtc_mode_valid(struct drm_crtc *crtc,
  70. const struct drm_display_mode *mode)
  71. {
  72. struct drm_simple_display_pipe *pipe;
  73. pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
  74. if (!pipe->funcs || !pipe->funcs->mode_valid)
  75. /* Anything goes */
  76. return MODE_OK;
  77. return pipe->funcs->mode_valid(pipe, mode);
  78. }
  79. static int drm_simple_kms_crtc_check(struct drm_crtc *crtc,
  80. struct drm_crtc_state *state)
  81. {
  82. bool has_primary = state->plane_mask &
  83. drm_plane_mask(crtc->primary);
  84. /* We always want to have an active plane with an active CRTC */
  85. if (has_primary != state->enable)
  86. return -EINVAL;
  87. return drm_atomic_add_affected_planes(state->state, crtc);
  88. }
  89. static void drm_simple_kms_crtc_enable(struct drm_crtc *crtc,
  90. struct drm_crtc_state *old_state)
  91. {
  92. struct drm_plane *plane;
  93. struct drm_simple_display_pipe *pipe;
  94. pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
  95. if (!pipe->funcs || !pipe->funcs->enable)
  96. return;
  97. plane = &pipe->plane;
  98. pipe->funcs->enable(pipe, crtc->state, plane->state);
  99. }
  100. static void drm_simple_kms_crtc_disable(struct drm_crtc *crtc,
  101. struct drm_crtc_state *old_state)
  102. {
  103. struct drm_simple_display_pipe *pipe;
  104. pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
  105. if (!pipe->funcs || !pipe->funcs->disable)
  106. return;
  107. pipe->funcs->disable(pipe);
  108. }
  109. static const struct drm_crtc_helper_funcs drm_simple_kms_crtc_helper_funcs = {
  110. .mode_valid = drm_simple_kms_crtc_mode_valid,
  111. .atomic_check = drm_simple_kms_crtc_check,
  112. .atomic_enable = drm_simple_kms_crtc_enable,
  113. .atomic_disable = drm_simple_kms_crtc_disable,
  114. };
  115. static int drm_simple_kms_crtc_enable_vblank(struct drm_crtc *crtc)
  116. {
  117. struct drm_simple_display_pipe *pipe;
  118. pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
  119. if (!pipe->funcs || !pipe->funcs->enable_vblank)
  120. return 0;
  121. return pipe->funcs->enable_vblank(pipe);
  122. }
  123. static void drm_simple_kms_crtc_disable_vblank(struct drm_crtc *crtc)
  124. {
  125. struct drm_simple_display_pipe *pipe;
  126. pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
  127. if (!pipe->funcs || !pipe->funcs->disable_vblank)
  128. return;
  129. pipe->funcs->disable_vblank(pipe);
  130. }
  131. static const struct drm_crtc_funcs drm_simple_kms_crtc_funcs = {
  132. .reset = drm_atomic_helper_crtc_reset,
  133. .destroy = drm_crtc_cleanup,
  134. .set_config = drm_atomic_helper_set_config,
  135. .page_flip = drm_atomic_helper_page_flip,
  136. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  137. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  138. .enable_vblank = drm_simple_kms_crtc_enable_vblank,
  139. .disable_vblank = drm_simple_kms_crtc_disable_vblank,
  140. };
  141. static int drm_simple_kms_plane_atomic_check(struct drm_plane *plane,
  142. struct drm_plane_state *plane_state)
  143. {
  144. struct drm_simple_display_pipe *pipe;
  145. struct drm_crtc_state *crtc_state;
  146. int ret;
  147. pipe = container_of(plane, struct drm_simple_display_pipe, plane);
  148. crtc_state = drm_atomic_get_new_crtc_state(plane_state->state,
  149. &pipe->crtc);
  150. ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
  151. DRM_PLANE_HELPER_NO_SCALING,
  152. DRM_PLANE_HELPER_NO_SCALING,
  153. false, true);
  154. if (ret)
  155. return ret;
  156. if (!plane_state->visible)
  157. return 0;
  158. if (!pipe->funcs || !pipe->funcs->check)
  159. return 0;
  160. return pipe->funcs->check(pipe, plane_state, crtc_state);
  161. }
  162. static void drm_simple_kms_plane_atomic_update(struct drm_plane *plane,
  163. struct drm_plane_state *old_pstate)
  164. {
  165. struct drm_simple_display_pipe *pipe;
  166. pipe = container_of(plane, struct drm_simple_display_pipe, plane);
  167. if (!pipe->funcs || !pipe->funcs->update)
  168. return;
  169. pipe->funcs->update(pipe, old_pstate);
  170. }
  171. static int drm_simple_kms_plane_prepare_fb(struct drm_plane *plane,
  172. struct drm_plane_state *state)
  173. {
  174. struct drm_simple_display_pipe *pipe;
  175. pipe = container_of(plane, struct drm_simple_display_pipe, plane);
  176. if (!pipe->funcs || !pipe->funcs->prepare_fb)
  177. return 0;
  178. return pipe->funcs->prepare_fb(pipe, state);
  179. }
  180. static void drm_simple_kms_plane_cleanup_fb(struct drm_plane *plane,
  181. struct drm_plane_state *state)
  182. {
  183. struct drm_simple_display_pipe *pipe;
  184. pipe = container_of(plane, struct drm_simple_display_pipe, plane);
  185. if (!pipe->funcs || !pipe->funcs->cleanup_fb)
  186. return;
  187. pipe->funcs->cleanup_fb(pipe, state);
  188. }
  189. static bool drm_simple_kms_format_mod_supported(struct drm_plane *plane,
  190. uint32_t format,
  191. uint64_t modifier)
  192. {
  193. return modifier == DRM_FORMAT_MOD_LINEAR;
  194. }
  195. static const struct drm_plane_helper_funcs drm_simple_kms_plane_helper_funcs = {
  196. .prepare_fb = drm_simple_kms_plane_prepare_fb,
  197. .cleanup_fb = drm_simple_kms_plane_cleanup_fb,
  198. .atomic_check = drm_simple_kms_plane_atomic_check,
  199. .atomic_update = drm_simple_kms_plane_atomic_update,
  200. };
  201. static const struct drm_plane_funcs drm_simple_kms_plane_funcs = {
  202. .update_plane = drm_atomic_helper_update_plane,
  203. .disable_plane = drm_atomic_helper_disable_plane,
  204. .destroy = drm_plane_cleanup,
  205. .reset = drm_atomic_helper_plane_reset,
  206. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  207. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  208. .format_mod_supported = drm_simple_kms_format_mod_supported,
  209. };
  210. /**
  211. * drm_simple_display_pipe_attach_bridge - Attach a bridge to the display pipe
  212. * @pipe: simple display pipe object
  213. * @bridge: bridge to attach
  214. *
  215. * Makes it possible to still use the drm_simple_display_pipe helpers when
  216. * a DRM bridge has to be used.
  217. *
  218. * Note that you probably want to initialize the pipe by passing a NULL
  219. * connector to drm_simple_display_pipe_init().
  220. *
  221. * Returns:
  222. * Zero on success, negative error code on failure.
  223. */
  224. int drm_simple_display_pipe_attach_bridge(struct drm_simple_display_pipe *pipe,
  225. struct drm_bridge *bridge)
  226. {
  227. return drm_bridge_attach(&pipe->encoder, bridge, NULL, 0);
  228. }
  229. EXPORT_SYMBOL(drm_simple_display_pipe_attach_bridge);
  230. /**
  231. * drm_simple_display_pipe_init - Initialize a simple display pipeline
  232. * @dev: DRM device
  233. * @pipe: simple display pipe object to initialize
  234. * @funcs: callbacks for the display pipe (optional)
  235. * @formats: array of supported formats (DRM_FORMAT\_\*)
  236. * @format_count: number of elements in @formats
  237. * @format_modifiers: array of formats modifiers
  238. * @connector: connector to attach and register (optional)
  239. *
  240. * Sets up a display pipeline which consist of a really simple
  241. * plane-crtc-encoder pipe.
  242. *
  243. * If a connector is supplied, the pipe will be coupled with the provided
  244. * connector. You may supply a NULL connector when using drm bridges, that
  245. * handle connectors themselves (see drm_simple_display_pipe_attach_bridge()).
  246. *
  247. * Teardown of a simple display pipe is all handled automatically by the drm
  248. * core through calling drm_mode_config_cleanup(). Drivers afterwards need to
  249. * release the memory for the structure themselves.
  250. *
  251. * Returns:
  252. * Zero on success, negative error code on failure.
  253. */
  254. int drm_simple_display_pipe_init(struct drm_device *dev,
  255. struct drm_simple_display_pipe *pipe,
  256. const struct drm_simple_display_pipe_funcs *funcs,
  257. const uint32_t *formats, unsigned int format_count,
  258. const uint64_t *format_modifiers,
  259. struct drm_connector *connector)
  260. {
  261. struct drm_encoder *encoder = &pipe->encoder;
  262. struct drm_plane *plane = &pipe->plane;
  263. struct drm_crtc *crtc = &pipe->crtc;
  264. int ret;
  265. pipe->connector = connector;
  266. pipe->funcs = funcs;
  267. drm_plane_helper_add(plane, &drm_simple_kms_plane_helper_funcs);
  268. ret = drm_universal_plane_init(dev, plane, 0,
  269. &drm_simple_kms_plane_funcs,
  270. formats, format_count,
  271. format_modifiers,
  272. DRM_PLANE_TYPE_PRIMARY, NULL);
  273. if (ret)
  274. return ret;
  275. drm_crtc_helper_add(crtc, &drm_simple_kms_crtc_helper_funcs);
  276. ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
  277. &drm_simple_kms_crtc_funcs, NULL);
  278. if (ret)
  279. return ret;
  280. encoder->possible_crtcs = drm_crtc_mask(crtc);
  281. ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_NONE);
  282. if (ret || !connector)
  283. return ret;
  284. return drm_connector_attach_encoder(connector, encoder);
  285. }
  286. EXPORT_SYMBOL(drm_simple_display_pipe_init);
  287. MODULE_LICENSE("GPL");