malidp_crtc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. /* We rely on firmware to set mclk to a sensible level. */
  56. clk_set_rate(hwdev->pxlclk, crtc->state->adjusted_mode.crtc_clock * 1000);
  57. hwdev->modeset(hwdev, &vm);
  58. hwdev->leave_config_mode(hwdev);
  59. drm_crtc_vblank_on(crtc);
  60. }
  61. static void malidp_crtc_disable(struct drm_crtc *crtc)
  62. {
  63. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  64. struct malidp_hw_device *hwdev = malidp->dev;
  65. drm_crtc_vblank_off(crtc);
  66. hwdev->enter_config_mode(hwdev);
  67. clk_disable_unprepare(hwdev->pxlclk);
  68. }
  69. static int malidp_crtc_atomic_check(struct drm_crtc *crtc,
  70. struct drm_crtc_state *state)
  71. {
  72. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  73. struct malidp_hw_device *hwdev = malidp->dev;
  74. struct drm_plane *plane;
  75. const struct drm_plane_state *pstate;
  76. u32 rot_mem_free, rot_mem_usable;
  77. int rotated_planes = 0;
  78. /*
  79. * check if there is enough rotation memory available for planes
  80. * that need 90° and 270° rotation. Each plane has set its required
  81. * memory size in the ->plane_check() callback, here we only make
  82. * sure that the sums are less that the total usable memory.
  83. *
  84. * The rotation memory allocation algorithm (for each plane):
  85. * a. If no more rotated planes exist, all remaining rotate
  86. * memory in the bank is available for use by the plane.
  87. * b. If other rotated planes exist, and plane's layer ID is
  88. * DE_VIDEO1, it can use all the memory from first bank if
  89. * secondary rotation memory bank is available, otherwise it can
  90. * use up to half the bank's memory.
  91. * c. If other rotated planes exist, and plane's layer ID is not
  92. * DE_VIDEO1, it can use half of the available memory
  93. *
  94. * Note: this algorithm assumes that the order in which the planes are
  95. * checked always has DE_VIDEO1 plane first in the list if it is
  96. * rotated. Because that is how we create the planes in the first
  97. * place, under current DRM version things work, but if ever the order
  98. * in which drm_atomic_crtc_state_for_each_plane() iterates over planes
  99. * changes, we need to pre-sort the planes before validation.
  100. */
  101. /* first count the number of rotated planes */
  102. drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
  103. if (pstate->rotation & MALIDP_ROTATED_MASK)
  104. rotated_planes++;
  105. }
  106. rot_mem_free = hwdev->rotation_memory[0];
  107. /*
  108. * if we have more than 1 plane using rotation memory, use the second
  109. * block of rotation memory as well
  110. */
  111. if (rotated_planes > 1)
  112. rot_mem_free += hwdev->rotation_memory[1];
  113. /* now validate the rotation memory requirements */
  114. drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
  115. struct malidp_plane *mp = to_malidp_plane(plane);
  116. struct malidp_plane_state *ms = to_malidp_plane_state(pstate);
  117. if (pstate->rotation & MALIDP_ROTATED_MASK) {
  118. /* process current plane */
  119. rotated_planes--;
  120. if (!rotated_planes) {
  121. /* no more rotated planes, we can use what's left */
  122. rot_mem_usable = rot_mem_free;
  123. } else {
  124. if ((mp->layer->id != DE_VIDEO1) ||
  125. (hwdev->rotation_memory[1] == 0))
  126. rot_mem_usable = rot_mem_free / 2;
  127. else
  128. rot_mem_usable = hwdev->rotation_memory[0];
  129. }
  130. rot_mem_free -= rot_mem_usable;
  131. if (ms->rotmem_size > rot_mem_usable)
  132. return -EINVAL;
  133. }
  134. }
  135. return 0;
  136. }
  137. static const struct drm_crtc_helper_funcs malidp_crtc_helper_funcs = {
  138. .mode_fixup = malidp_crtc_mode_fixup,
  139. .enable = malidp_crtc_enable,
  140. .disable = malidp_crtc_disable,
  141. .atomic_check = malidp_crtc_atomic_check,
  142. };
  143. static const struct drm_crtc_funcs malidp_crtc_funcs = {
  144. .destroy = drm_crtc_cleanup,
  145. .set_config = drm_atomic_helper_set_config,
  146. .page_flip = drm_atomic_helper_page_flip,
  147. .reset = drm_atomic_helper_crtc_reset,
  148. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  149. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  150. };
  151. int malidp_crtc_init(struct drm_device *drm)
  152. {
  153. struct malidp_drm *malidp = drm->dev_private;
  154. struct drm_plane *primary = NULL, *plane;
  155. int ret;
  156. ret = malidp_de_planes_init(drm);
  157. if (ret < 0) {
  158. DRM_ERROR("Failed to initialise planes\n");
  159. return ret;
  160. }
  161. drm_for_each_plane(plane, drm) {
  162. if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
  163. primary = plane;
  164. break;
  165. }
  166. }
  167. if (!primary) {
  168. DRM_ERROR("no primary plane found\n");
  169. ret = -EINVAL;
  170. goto crtc_cleanup_planes;
  171. }
  172. ret = drm_crtc_init_with_planes(drm, &malidp->crtc, primary, NULL,
  173. &malidp_crtc_funcs, NULL);
  174. if (!ret) {
  175. drm_crtc_helper_add(&malidp->crtc, &malidp_crtc_helper_funcs);
  176. return 0;
  177. }
  178. crtc_cleanup_planes:
  179. malidp_de_planes_destroy(drm);
  180. return ret;
  181. }