drm_simple_kms_helper.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 const struct drm_plane_helper_funcs drm_simple_kms_plane_helper_funcs = {
  105. .atomic_check = drm_simple_kms_plane_atomic_check,
  106. .atomic_update = drm_simple_kms_plane_atomic_update,
  107. };
  108. static const struct drm_plane_funcs drm_simple_kms_plane_funcs = {
  109. .update_plane = drm_atomic_helper_update_plane,
  110. .disable_plane = drm_atomic_helper_disable_plane,
  111. .destroy = drm_plane_cleanup,
  112. .reset = drm_atomic_helper_plane_reset,
  113. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  114. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  115. };
  116. /**
  117. * drm_simple_display_pipe_attach_bridge - Attach a bridge to the display pipe
  118. * @pipe: simple display pipe object
  119. * @bridge: bridge to attach
  120. *
  121. * Makes it possible to still use the drm_simple_display_pipe helpers when
  122. * a DRM bridge has to be used.
  123. *
  124. * Note that you probably want to initialize the pipe by passing a NULL
  125. * connector to drm_simple_display_pipe_init().
  126. *
  127. * Returns:
  128. * Zero on success, negative error code on failure.
  129. */
  130. int drm_simple_display_pipe_attach_bridge(struct drm_simple_display_pipe *pipe,
  131. struct drm_bridge *bridge)
  132. {
  133. bridge->encoder = &pipe->encoder;
  134. pipe->encoder.bridge = bridge;
  135. return drm_bridge_attach(pipe->encoder.dev, bridge);
  136. }
  137. EXPORT_SYMBOL(drm_simple_display_pipe_attach_bridge);
  138. /**
  139. * drm_simple_display_pipe_detach_bridge - Detach the bridge from the display pipe
  140. * @pipe: simple display pipe object
  141. *
  142. * Detaches the drm bridge previously attached with
  143. * drm_simple_display_pipe_attach_bridge()
  144. */
  145. void drm_simple_display_pipe_detach_bridge(struct drm_simple_display_pipe *pipe)
  146. {
  147. if (WARN_ON(!pipe->encoder.bridge))
  148. return;
  149. drm_bridge_detach(pipe->encoder.bridge);
  150. pipe->encoder.bridge = NULL;
  151. }
  152. EXPORT_SYMBOL(drm_simple_display_pipe_detach_bridge);
  153. /**
  154. * drm_simple_display_pipe_init - Initialize a simple display pipeline
  155. * @dev: DRM device
  156. * @pipe: simple display pipe object to initialize
  157. * @funcs: callbacks for the display pipe (optional)
  158. * @formats: array of supported formats (DRM_FORMAT\_\*)
  159. * @format_count: number of elements in @formats
  160. * @connector: connector to attach and register (optional)
  161. *
  162. * Sets up a display pipeline which consist of a really simple
  163. * plane-crtc-encoder pipe.
  164. *
  165. * If a connector is supplied, the pipe will be coupled with the provided
  166. * connector. You may supply a NULL connector when using drm bridges, that
  167. * handle connectors themselves (see drm_simple_display_pipe_attach_bridge()).
  168. *
  169. * Teardown of a simple display pipe is all handled automatically by the drm
  170. * core through calling drm_mode_config_cleanup(). Drivers afterwards need to
  171. * release the memory for the structure themselves.
  172. *
  173. * Returns:
  174. * Zero on success, negative error code on failure.
  175. */
  176. int drm_simple_display_pipe_init(struct drm_device *dev,
  177. struct drm_simple_display_pipe *pipe,
  178. const struct drm_simple_display_pipe_funcs *funcs,
  179. const uint32_t *formats, unsigned int format_count,
  180. struct drm_connector *connector)
  181. {
  182. struct drm_encoder *encoder = &pipe->encoder;
  183. struct drm_plane *plane = &pipe->plane;
  184. struct drm_crtc *crtc = &pipe->crtc;
  185. int ret;
  186. pipe->connector = connector;
  187. pipe->funcs = funcs;
  188. drm_plane_helper_add(plane, &drm_simple_kms_plane_helper_funcs);
  189. ret = drm_universal_plane_init(dev, plane, 0,
  190. &drm_simple_kms_plane_funcs,
  191. formats, format_count,
  192. DRM_PLANE_TYPE_PRIMARY, NULL);
  193. if (ret)
  194. return ret;
  195. drm_crtc_helper_add(crtc, &drm_simple_kms_crtc_helper_funcs);
  196. ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
  197. &drm_simple_kms_crtc_funcs, NULL);
  198. if (ret)
  199. return ret;
  200. encoder->possible_crtcs = 1 << drm_crtc_index(crtc);
  201. ret = drm_encoder_init(dev, encoder, &drm_simple_kms_encoder_funcs,
  202. DRM_MODE_ENCODER_NONE, NULL);
  203. if (ret || !connector)
  204. return ret;
  205. return drm_mode_connector_attach_encoder(connector, encoder);
  206. }
  207. EXPORT_SYMBOL(drm_simple_display_pipe_init);
  208. MODULE_LICENSE("GPL");