drm_simple_kms_helper.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (C) 2016 Noralf Trønnes
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. */
  9. #include <drm/drmP.h>
  10. #include <drm/drm_atomic.h>
  11. #include <drm/drm_atomic_helper.h>
  12. #include <drm/drm_crtc_helper.h>
  13. #include <drm/drm_plane_helper.h>
  14. #include <drm/drm_simple_kms_helper.h>
  15. #include <linux/slab.h>
  16. /**
  17. * DOC: overview
  18. *
  19. * This helper library provides helpers for drivers for simple display
  20. * hardware.
  21. *
  22. * drm_simple_display_pipe_init() initializes a simple display pipeline
  23. * which has only one full-screen scanout buffer feeding one output. The
  24. * pipeline is represented by &struct drm_simple_display_pipe and binds
  25. * together &drm_plane, &drm_crtc and &drm_encoder structures into one fixed
  26. * entity. Some flexibility for code reuse is provided through a separately
  27. * allocated &drm_connector object and supporting optional &drm_bridge
  28. * encoder drivers.
  29. */
  30. static const struct drm_encoder_funcs drm_simple_kms_encoder_funcs = {
  31. .destroy = drm_encoder_cleanup,
  32. };
  33. static enum drm_mode_status
  34. drm_simple_kms_crtc_mode_valid(struct drm_crtc *crtc,
  35. const struct drm_display_mode *mode)
  36. {
  37. struct drm_simple_display_pipe *pipe;
  38. pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
  39. if (!pipe->funcs || !pipe->funcs->mode_valid)
  40. /* Anything goes */
  41. return MODE_OK;
  42. return pipe->funcs->mode_valid(crtc, mode);
  43. }
  44. static int drm_simple_kms_crtc_check(struct drm_crtc *crtc,
  45. struct drm_crtc_state *state)
  46. {
  47. bool has_primary = state->plane_mask &
  48. BIT(drm_plane_index(crtc->primary));
  49. /* We always want to have an active plane with an active CRTC */
  50. if (has_primary != state->enable)
  51. return -EINVAL;
  52. return drm_atomic_add_affected_planes(state->state, crtc);
  53. }
  54. static void drm_simple_kms_crtc_enable(struct drm_crtc *crtc,
  55. struct drm_crtc_state *old_state)
  56. {
  57. struct drm_simple_display_pipe *pipe;
  58. pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
  59. if (!pipe->funcs || !pipe->funcs->enable)
  60. return;
  61. pipe->funcs->enable(pipe, crtc->state);
  62. }
  63. static void drm_simple_kms_crtc_disable(struct drm_crtc *crtc,
  64. struct drm_crtc_state *old_state)
  65. {
  66. struct drm_simple_display_pipe *pipe;
  67. pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
  68. if (!pipe->funcs || !pipe->funcs->disable)
  69. return;
  70. pipe->funcs->disable(pipe);
  71. }
  72. static const struct drm_crtc_helper_funcs drm_simple_kms_crtc_helper_funcs = {
  73. .mode_valid = drm_simple_kms_crtc_mode_valid,
  74. .atomic_check = drm_simple_kms_crtc_check,
  75. .atomic_enable = drm_simple_kms_crtc_enable,
  76. .atomic_disable = drm_simple_kms_crtc_disable,
  77. };
  78. static int drm_simple_kms_crtc_enable_vblank(struct drm_crtc *crtc)
  79. {
  80. struct drm_simple_display_pipe *pipe;
  81. pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
  82. if (!pipe->funcs || !pipe->funcs->enable_vblank)
  83. return 0;
  84. return pipe->funcs->enable_vblank(pipe);
  85. }
  86. static void drm_simple_kms_crtc_disable_vblank(struct drm_crtc *crtc)
  87. {
  88. struct drm_simple_display_pipe *pipe;
  89. pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
  90. if (!pipe->funcs || !pipe->funcs->disable_vblank)
  91. return;
  92. pipe->funcs->disable_vblank(pipe);
  93. }
  94. static const struct drm_crtc_funcs drm_simple_kms_crtc_funcs = {
  95. .reset = drm_atomic_helper_crtc_reset,
  96. .destroy = drm_crtc_cleanup,
  97. .set_config = drm_atomic_helper_set_config,
  98. .page_flip = drm_atomic_helper_page_flip,
  99. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  100. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  101. .enable_vblank = drm_simple_kms_crtc_enable_vblank,
  102. .disable_vblank = drm_simple_kms_crtc_disable_vblank,
  103. };
  104. static int drm_simple_kms_plane_atomic_check(struct drm_plane *plane,
  105. struct drm_plane_state *plane_state)
  106. {
  107. struct drm_simple_display_pipe *pipe;
  108. struct drm_crtc_state *crtc_state;
  109. int ret;
  110. pipe = container_of(plane, struct drm_simple_display_pipe, plane);
  111. crtc_state = drm_atomic_get_new_crtc_state(plane_state->state,
  112. &pipe->crtc);
  113. ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
  114. DRM_PLANE_HELPER_NO_SCALING,
  115. DRM_PLANE_HELPER_NO_SCALING,
  116. false, true);
  117. if (ret)
  118. return ret;
  119. if (!plane_state->visible)
  120. return 0;
  121. if (!pipe->funcs || !pipe->funcs->check)
  122. return 0;
  123. return pipe->funcs->check(pipe, plane_state, crtc_state);
  124. }
  125. static void drm_simple_kms_plane_atomic_update(struct drm_plane *plane,
  126. struct drm_plane_state *old_pstate)
  127. {
  128. struct drm_simple_display_pipe *pipe;
  129. pipe = container_of(plane, struct drm_simple_display_pipe, plane);
  130. if (!pipe->funcs || !pipe->funcs->update)
  131. return;
  132. pipe->funcs->update(pipe, old_pstate);
  133. }
  134. static int drm_simple_kms_plane_prepare_fb(struct drm_plane *plane,
  135. struct drm_plane_state *state)
  136. {
  137. struct drm_simple_display_pipe *pipe;
  138. pipe = container_of(plane, struct drm_simple_display_pipe, plane);
  139. if (!pipe->funcs || !pipe->funcs->prepare_fb)
  140. return 0;
  141. return pipe->funcs->prepare_fb(pipe, state);
  142. }
  143. static void drm_simple_kms_plane_cleanup_fb(struct drm_plane *plane,
  144. struct drm_plane_state *state)
  145. {
  146. struct drm_simple_display_pipe *pipe;
  147. pipe = container_of(plane, struct drm_simple_display_pipe, plane);
  148. if (!pipe->funcs || !pipe->funcs->cleanup_fb)
  149. return;
  150. pipe->funcs->cleanup_fb(pipe, state);
  151. }
  152. static const struct drm_plane_helper_funcs drm_simple_kms_plane_helper_funcs = {
  153. .prepare_fb = drm_simple_kms_plane_prepare_fb,
  154. .cleanup_fb = drm_simple_kms_plane_cleanup_fb,
  155. .atomic_check = drm_simple_kms_plane_atomic_check,
  156. .atomic_update = drm_simple_kms_plane_atomic_update,
  157. };
  158. static const struct drm_plane_funcs drm_simple_kms_plane_funcs = {
  159. .update_plane = drm_atomic_helper_update_plane,
  160. .disable_plane = drm_atomic_helper_disable_plane,
  161. .destroy = drm_plane_cleanup,
  162. .reset = drm_atomic_helper_plane_reset,
  163. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  164. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  165. };
  166. /**
  167. * drm_simple_display_pipe_attach_bridge - Attach a bridge to the display pipe
  168. * @pipe: simple display pipe object
  169. * @bridge: bridge to attach
  170. *
  171. * Makes it possible to still use the drm_simple_display_pipe helpers when
  172. * a DRM bridge has to be used.
  173. *
  174. * Note that you probably want to initialize the pipe by passing a NULL
  175. * connector to drm_simple_display_pipe_init().
  176. *
  177. * Returns:
  178. * Zero on success, negative error code on failure.
  179. */
  180. int drm_simple_display_pipe_attach_bridge(struct drm_simple_display_pipe *pipe,
  181. struct drm_bridge *bridge)
  182. {
  183. return drm_bridge_attach(&pipe->encoder, bridge, NULL);
  184. }
  185. EXPORT_SYMBOL(drm_simple_display_pipe_attach_bridge);
  186. /**
  187. * drm_simple_display_pipe_init - Initialize a simple display pipeline
  188. * @dev: DRM device
  189. * @pipe: simple display pipe object to initialize
  190. * @funcs: callbacks for the display pipe (optional)
  191. * @formats: array of supported formats (DRM_FORMAT\_\*)
  192. * @format_count: number of elements in @formats
  193. * @format_modifiers: array of formats modifiers
  194. * @connector: connector to attach and register (optional)
  195. *
  196. * Sets up a display pipeline which consist of a really simple
  197. * plane-crtc-encoder pipe.
  198. *
  199. * If a connector is supplied, the pipe will be coupled with the provided
  200. * connector. You may supply a NULL connector when using drm bridges, that
  201. * handle connectors themselves (see drm_simple_display_pipe_attach_bridge()).
  202. *
  203. * Teardown of a simple display pipe is all handled automatically by the drm
  204. * core through calling drm_mode_config_cleanup(). Drivers afterwards need to
  205. * release the memory for the structure themselves.
  206. *
  207. * Returns:
  208. * Zero on success, negative error code on failure.
  209. */
  210. int drm_simple_display_pipe_init(struct drm_device *dev,
  211. struct drm_simple_display_pipe *pipe,
  212. const struct drm_simple_display_pipe_funcs *funcs,
  213. const uint32_t *formats, unsigned int format_count,
  214. const uint64_t *format_modifiers,
  215. struct drm_connector *connector)
  216. {
  217. struct drm_encoder *encoder = &pipe->encoder;
  218. struct drm_plane *plane = &pipe->plane;
  219. struct drm_crtc *crtc = &pipe->crtc;
  220. int ret;
  221. pipe->connector = connector;
  222. pipe->funcs = funcs;
  223. drm_plane_helper_add(plane, &drm_simple_kms_plane_helper_funcs);
  224. ret = drm_universal_plane_init(dev, plane, 0,
  225. &drm_simple_kms_plane_funcs,
  226. formats, format_count,
  227. format_modifiers,
  228. DRM_PLANE_TYPE_PRIMARY, NULL);
  229. if (ret)
  230. return ret;
  231. drm_crtc_helper_add(crtc, &drm_simple_kms_crtc_helper_funcs);
  232. ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
  233. &drm_simple_kms_crtc_funcs, NULL);
  234. if (ret)
  235. return ret;
  236. encoder->possible_crtcs = 1 << drm_crtc_index(crtc);
  237. ret = drm_encoder_init(dev, encoder, &drm_simple_kms_encoder_funcs,
  238. DRM_MODE_ENCODER_NONE, NULL);
  239. if (ret || !connector)
  240. return ret;
  241. return drm_mode_connector_attach_encoder(connector, encoder);
  242. }
  243. EXPORT_SYMBOL(drm_simple_display_pipe_init);
  244. MODULE_LICENSE("GPL");