drm_simple_kms_helper.c 8.1 KB

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