drm_simple_kms_helper.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. drm_plane_mask(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_plane *plane;
  58. struct drm_simple_display_pipe *pipe;
  59. pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
  60. if (!pipe->funcs || !pipe->funcs->enable)
  61. return;
  62. plane = &pipe->plane;
  63. pipe->funcs->enable(pipe, crtc->state, plane->state);
  64. }
  65. static void drm_simple_kms_crtc_disable(struct drm_crtc *crtc,
  66. struct drm_crtc_state *old_state)
  67. {
  68. struct drm_simple_display_pipe *pipe;
  69. pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
  70. if (!pipe->funcs || !pipe->funcs->disable)
  71. return;
  72. pipe->funcs->disable(pipe);
  73. }
  74. static const struct drm_crtc_helper_funcs drm_simple_kms_crtc_helper_funcs = {
  75. .mode_valid = drm_simple_kms_crtc_mode_valid,
  76. .atomic_check = drm_simple_kms_crtc_check,
  77. .atomic_enable = drm_simple_kms_crtc_enable,
  78. .atomic_disable = drm_simple_kms_crtc_disable,
  79. };
  80. static int drm_simple_kms_crtc_enable_vblank(struct drm_crtc *crtc)
  81. {
  82. struct drm_simple_display_pipe *pipe;
  83. pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
  84. if (!pipe->funcs || !pipe->funcs->enable_vblank)
  85. return 0;
  86. return pipe->funcs->enable_vblank(pipe);
  87. }
  88. static void drm_simple_kms_crtc_disable_vblank(struct drm_crtc *crtc)
  89. {
  90. struct drm_simple_display_pipe *pipe;
  91. pipe = container_of(crtc, struct drm_simple_display_pipe, crtc);
  92. if (!pipe->funcs || !pipe->funcs->disable_vblank)
  93. return;
  94. pipe->funcs->disable_vblank(pipe);
  95. }
  96. static const struct drm_crtc_funcs drm_simple_kms_crtc_funcs = {
  97. .reset = drm_atomic_helper_crtc_reset,
  98. .destroy = drm_crtc_cleanup,
  99. .set_config = drm_atomic_helper_set_config,
  100. .page_flip = drm_atomic_helper_page_flip,
  101. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  102. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  103. .enable_vblank = drm_simple_kms_crtc_enable_vblank,
  104. .disable_vblank = drm_simple_kms_crtc_disable_vblank,
  105. };
  106. static int drm_simple_kms_plane_atomic_check(struct drm_plane *plane,
  107. struct drm_plane_state *plane_state)
  108. {
  109. struct drm_simple_display_pipe *pipe;
  110. struct drm_crtc_state *crtc_state;
  111. int ret;
  112. pipe = container_of(plane, struct drm_simple_display_pipe, plane);
  113. crtc_state = drm_atomic_get_new_crtc_state(plane_state->state,
  114. &pipe->crtc);
  115. ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
  116. DRM_PLANE_HELPER_NO_SCALING,
  117. DRM_PLANE_HELPER_NO_SCALING,
  118. false, true);
  119. if (ret)
  120. return ret;
  121. if (!plane_state->visible)
  122. return 0;
  123. if (!pipe->funcs || !pipe->funcs->check)
  124. return 0;
  125. return pipe->funcs->check(pipe, plane_state, crtc_state);
  126. }
  127. static void drm_simple_kms_plane_atomic_update(struct drm_plane *plane,
  128. struct drm_plane_state *old_pstate)
  129. {
  130. struct drm_simple_display_pipe *pipe;
  131. pipe = container_of(plane, struct drm_simple_display_pipe, plane);
  132. if (!pipe->funcs || !pipe->funcs->update)
  133. return;
  134. pipe->funcs->update(pipe, old_pstate);
  135. }
  136. static int drm_simple_kms_plane_prepare_fb(struct drm_plane *plane,
  137. struct drm_plane_state *state)
  138. {
  139. struct drm_simple_display_pipe *pipe;
  140. pipe = container_of(plane, struct drm_simple_display_pipe, plane);
  141. if (!pipe->funcs || !pipe->funcs->prepare_fb)
  142. return 0;
  143. return pipe->funcs->prepare_fb(pipe, state);
  144. }
  145. static void drm_simple_kms_plane_cleanup_fb(struct drm_plane *plane,
  146. struct drm_plane_state *state)
  147. {
  148. struct drm_simple_display_pipe *pipe;
  149. pipe = container_of(plane, struct drm_simple_display_pipe, plane);
  150. if (!pipe->funcs || !pipe->funcs->cleanup_fb)
  151. return;
  152. pipe->funcs->cleanup_fb(pipe, state);
  153. }
  154. static const struct drm_plane_helper_funcs drm_simple_kms_plane_helper_funcs = {
  155. .prepare_fb = drm_simple_kms_plane_prepare_fb,
  156. .cleanup_fb = drm_simple_kms_plane_cleanup_fb,
  157. .atomic_check = drm_simple_kms_plane_atomic_check,
  158. .atomic_update = drm_simple_kms_plane_atomic_update,
  159. };
  160. static const struct drm_plane_funcs drm_simple_kms_plane_funcs = {
  161. .update_plane = drm_atomic_helper_update_plane,
  162. .disable_plane = drm_atomic_helper_disable_plane,
  163. .destroy = drm_plane_cleanup,
  164. .reset = drm_atomic_helper_plane_reset,
  165. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  166. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  167. };
  168. /**
  169. * drm_simple_display_pipe_attach_bridge - Attach a bridge to the display pipe
  170. * @pipe: simple display pipe object
  171. * @bridge: bridge to attach
  172. *
  173. * Makes it possible to still use the drm_simple_display_pipe helpers when
  174. * a DRM bridge has to be used.
  175. *
  176. * Note that you probably want to initialize the pipe by passing a NULL
  177. * connector to drm_simple_display_pipe_init().
  178. *
  179. * Returns:
  180. * Zero on success, negative error code on failure.
  181. */
  182. int drm_simple_display_pipe_attach_bridge(struct drm_simple_display_pipe *pipe,
  183. struct drm_bridge *bridge)
  184. {
  185. return drm_bridge_attach(&pipe->encoder, bridge, NULL);
  186. }
  187. EXPORT_SYMBOL(drm_simple_display_pipe_attach_bridge);
  188. /**
  189. * drm_simple_display_pipe_init - Initialize a simple display pipeline
  190. * @dev: DRM device
  191. * @pipe: simple display pipe object to initialize
  192. * @funcs: callbacks for the display pipe (optional)
  193. * @formats: array of supported formats (DRM_FORMAT\_\*)
  194. * @format_count: number of elements in @formats
  195. * @format_modifiers: array of formats modifiers
  196. * @connector: connector to attach and register (optional)
  197. *
  198. * Sets up a display pipeline which consist of a really simple
  199. * plane-crtc-encoder pipe.
  200. *
  201. * If a connector is supplied, the pipe will be coupled with the provided
  202. * connector. You may supply a NULL connector when using drm bridges, that
  203. * handle connectors themselves (see drm_simple_display_pipe_attach_bridge()).
  204. *
  205. * Teardown of a simple display pipe is all handled automatically by the drm
  206. * core through calling drm_mode_config_cleanup(). Drivers afterwards need to
  207. * release the memory for the structure themselves.
  208. *
  209. * Returns:
  210. * Zero on success, negative error code on failure.
  211. */
  212. int drm_simple_display_pipe_init(struct drm_device *dev,
  213. struct drm_simple_display_pipe *pipe,
  214. const struct drm_simple_display_pipe_funcs *funcs,
  215. const uint32_t *formats, unsigned int format_count,
  216. const uint64_t *format_modifiers,
  217. struct drm_connector *connector)
  218. {
  219. struct drm_encoder *encoder = &pipe->encoder;
  220. struct drm_plane *plane = &pipe->plane;
  221. struct drm_crtc *crtc = &pipe->crtc;
  222. int ret;
  223. pipe->connector = connector;
  224. pipe->funcs = funcs;
  225. drm_plane_helper_add(plane, &drm_simple_kms_plane_helper_funcs);
  226. ret = drm_universal_plane_init(dev, plane, 0,
  227. &drm_simple_kms_plane_funcs,
  228. formats, format_count,
  229. format_modifiers,
  230. DRM_PLANE_TYPE_PRIMARY, NULL);
  231. if (ret)
  232. return ret;
  233. drm_crtc_helper_add(crtc, &drm_simple_kms_crtc_helper_funcs);
  234. ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
  235. &drm_simple_kms_crtc_funcs, NULL);
  236. if (ret)
  237. return ret;
  238. encoder->possible_crtcs = drm_crtc_mask(crtc);
  239. ret = drm_encoder_init(dev, encoder, &drm_simple_kms_encoder_funcs,
  240. DRM_MODE_ENCODER_NONE, NULL);
  241. if (ret || !connector)
  242. return ret;
  243. return drm_connector_attach_encoder(connector, encoder);
  244. }
  245. EXPORT_SYMBOL(drm_simple_display_pipe_init);
  246. MODULE_LICENSE("GPL");