drm_simple_kms_helper.c 8.3 KB

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