malidp_crtc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
  3. * Author: Liviu Dudau <Liviu.Dudau@arm.com>
  4. *
  5. * This program is free software and is provided to you under the terms of the
  6. * GNU General Public License version 2 as published by the Free Software
  7. * Foundation, and any use by you of this program is subject to the terms
  8. * of such GNU licence.
  9. *
  10. * ARM Mali DP500/DP550/DP650 driver (crtc operations)
  11. */
  12. #include <drm/drmP.h>
  13. #include <drm/drm_atomic.h>
  14. #include <drm/drm_atomic_helper.h>
  15. #include <drm/drm_crtc.h>
  16. #include <drm/drm_crtc_helper.h>
  17. #include <linux/clk.h>
  18. #include <video/videomode.h>
  19. #include "malidp_drv.h"
  20. #include "malidp_hw.h"
  21. static bool malidp_crtc_mode_fixup(struct drm_crtc *crtc,
  22. const struct drm_display_mode *mode,
  23. struct drm_display_mode *adjusted_mode)
  24. {
  25. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  26. struct malidp_hw_device *hwdev = malidp->dev;
  27. /*
  28. * check that the hardware can drive the required clock rate,
  29. * but skip the check if the clock is meant to be disabled (req_rate = 0)
  30. */
  31. long rate, req_rate = mode->crtc_clock * 1000;
  32. if (req_rate) {
  33. rate = clk_round_rate(hwdev->mclk, req_rate);
  34. if (rate < req_rate) {
  35. DRM_DEBUG_DRIVER("mclk clock unable to reach %d kHz\n",
  36. mode->crtc_clock);
  37. return false;
  38. }
  39. rate = clk_round_rate(hwdev->pxlclk, req_rate);
  40. if (rate != req_rate) {
  41. DRM_DEBUG_DRIVER("pxlclk doesn't support %ld Hz\n",
  42. req_rate);
  43. return false;
  44. }
  45. }
  46. return true;
  47. }
  48. static void malidp_crtc_enable(struct drm_crtc *crtc)
  49. {
  50. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  51. struct malidp_hw_device *hwdev = malidp->dev;
  52. struct videomode vm;
  53. drm_display_mode_to_videomode(&crtc->state->adjusted_mode, &vm);
  54. clk_prepare_enable(hwdev->pxlclk);
  55. /* mclk needs to be set to the same or higher rate than pxlclk */
  56. clk_set_rate(hwdev->mclk, crtc->state->adjusted_mode.crtc_clock * 1000);
  57. clk_set_rate(hwdev->pxlclk, crtc->state->adjusted_mode.crtc_clock * 1000);
  58. hwdev->modeset(hwdev, &vm);
  59. hwdev->leave_config_mode(hwdev);
  60. drm_crtc_vblank_on(crtc);
  61. }
  62. static void malidp_crtc_disable(struct drm_crtc *crtc)
  63. {
  64. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  65. struct malidp_hw_device *hwdev = malidp->dev;
  66. drm_crtc_vblank_off(crtc);
  67. hwdev->enter_config_mode(hwdev);
  68. clk_disable_unprepare(hwdev->pxlclk);
  69. }
  70. static int malidp_crtc_atomic_check(struct drm_crtc *crtc,
  71. struct drm_crtc_state *state)
  72. {
  73. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  74. struct malidp_hw_device *hwdev = malidp->dev;
  75. struct drm_plane *plane;
  76. const struct drm_plane_state *pstate;
  77. u32 rot_mem_free, rot_mem_usable;
  78. int rotated_planes = 0;
  79. /*
  80. * check if there is enough rotation memory available for planes
  81. * that need 90° and 270° rotation. Each plane has set its required
  82. * memory size in the ->plane_check() callback, here we only make
  83. * sure that the sums are less that the total usable memory.
  84. *
  85. * The rotation memory allocation algorithm (for each plane):
  86. * a. If no more rotated planes exist, all remaining rotate
  87. * memory in the bank is available for use by the plane.
  88. * b. If other rotated planes exist, and plane's layer ID is
  89. * DE_VIDEO1, it can use all the memory from first bank if
  90. * secondary rotation memory bank is available, otherwise it can
  91. * use up to half the bank's memory.
  92. * c. If other rotated planes exist, and plane's layer ID is not
  93. * DE_VIDEO1, it can use half of the available memory
  94. *
  95. * Note: this algorithm assumes that the order in which the planes are
  96. * checked always has DE_VIDEO1 plane first in the list if it is
  97. * rotated. Because that is how we create the planes in the first
  98. * place, under current DRM version things work, but if ever the order
  99. * in which drm_atomic_crtc_state_for_each_plane() iterates over planes
  100. * changes, we need to pre-sort the planes before validation.
  101. */
  102. /* first count the number of rotated planes */
  103. drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
  104. if (pstate->rotation & MALIDP_ROTATED_MASK)
  105. rotated_planes++;
  106. }
  107. rot_mem_free = hwdev->rotation_memory[0];
  108. /*
  109. * if we have more than 1 plane using rotation memory, use the second
  110. * block of rotation memory as well
  111. */
  112. if (rotated_planes > 1)
  113. rot_mem_free += hwdev->rotation_memory[1];
  114. /* now validate the rotation memory requirements */
  115. drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
  116. struct malidp_plane *mp = to_malidp_plane(plane);
  117. struct malidp_plane_state *ms = to_malidp_plane_state(pstate);
  118. if (pstate->rotation & MALIDP_ROTATED_MASK) {
  119. /* process current plane */
  120. rotated_planes--;
  121. if (!rotated_planes) {
  122. /* no more rotated planes, we can use what's left */
  123. rot_mem_usable = rot_mem_free;
  124. } else {
  125. if ((mp->layer->id != DE_VIDEO1) ||
  126. (hwdev->rotation_memory[1] == 0))
  127. rot_mem_usable = rot_mem_free / 2;
  128. else
  129. rot_mem_usable = hwdev->rotation_memory[0];
  130. }
  131. rot_mem_free -= rot_mem_usable;
  132. if (ms->rotmem_size > rot_mem_usable)
  133. return -EINVAL;
  134. }
  135. }
  136. return 0;
  137. }
  138. static const struct drm_crtc_helper_funcs malidp_crtc_helper_funcs = {
  139. .mode_fixup = malidp_crtc_mode_fixup,
  140. .enable = malidp_crtc_enable,
  141. .disable = malidp_crtc_disable,
  142. .atomic_check = malidp_crtc_atomic_check,
  143. };
  144. static const struct drm_crtc_funcs malidp_crtc_funcs = {
  145. .destroy = drm_crtc_cleanup,
  146. .set_config = drm_atomic_helper_set_config,
  147. .page_flip = drm_atomic_helper_page_flip,
  148. .reset = drm_atomic_helper_crtc_reset,
  149. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  150. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  151. };
  152. int malidp_crtc_init(struct drm_device *drm)
  153. {
  154. struct malidp_drm *malidp = drm->dev_private;
  155. struct drm_plane *primary = NULL, *plane;
  156. int ret;
  157. ret = malidp_de_planes_init(drm);
  158. if (ret < 0) {
  159. DRM_ERROR("Failed to initialise planes\n");
  160. return ret;
  161. }
  162. drm_for_each_plane(plane, drm) {
  163. if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
  164. primary = plane;
  165. break;
  166. }
  167. }
  168. if (!primary) {
  169. DRM_ERROR("no primary plane found\n");
  170. ret = -EINVAL;
  171. goto crtc_cleanup_planes;
  172. }
  173. ret = drm_crtc_init_with_planes(drm, &malidp->crtc, primary, NULL,
  174. &malidp_crtc_funcs, NULL);
  175. if (!ret) {
  176. drm_crtc_helper_add(&malidp->crtc, &malidp_crtc_helper_funcs);
  177. return 0;
  178. }
  179. crtc_cleanup_planes:
  180. malidp_de_planes_destroy(drm);
  181. return ret;
  182. }