sun4i_crtc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (C) 2015 Free Electrons
  3. * Copyright (C) 2015 NextThing Co
  4. *
  5. * Maxime Ripard <maxime.ripard@free-electrons.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. */
  12. #include <drm/drmP.h>
  13. #include <drm/drm_atomic_helper.h>
  14. #include <drm/drm_crtc.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include <drm/drm_modes.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/ioport.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_graph.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/regmap.h>
  23. #include <video/videomode.h>
  24. #include "sun4i_crtc.h"
  25. #include "sun4i_drv.h"
  26. #include "sunxi_engine.h"
  27. #include "sun4i_tcon.h"
  28. /*
  29. * While this isn't really working in the DRM theory, in practice we
  30. * can only ever have one encoder per TCON since we have a mux in our
  31. * TCON.
  32. */
  33. static struct drm_encoder *sun4i_crtc_get_encoder(struct drm_crtc *crtc)
  34. {
  35. struct drm_encoder *encoder;
  36. drm_for_each_encoder(encoder, crtc->dev)
  37. if (encoder->crtc == crtc)
  38. return encoder;
  39. return NULL;
  40. }
  41. static void sun4i_crtc_atomic_begin(struct drm_crtc *crtc,
  42. struct drm_crtc_state *old_state)
  43. {
  44. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  45. struct drm_device *dev = crtc->dev;
  46. unsigned long flags;
  47. if (crtc->state->event) {
  48. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  49. spin_lock_irqsave(&dev->event_lock, flags);
  50. scrtc->event = crtc->state->event;
  51. spin_unlock_irqrestore(&dev->event_lock, flags);
  52. crtc->state->event = NULL;
  53. }
  54. }
  55. static void sun4i_crtc_atomic_flush(struct drm_crtc *crtc,
  56. struct drm_crtc_state *old_state)
  57. {
  58. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  59. struct drm_pending_vblank_event *event = crtc->state->event;
  60. DRM_DEBUG_DRIVER("Committing plane changes\n");
  61. sunxi_engine_commit(scrtc->engine);
  62. if (event) {
  63. crtc->state->event = NULL;
  64. spin_lock_irq(&crtc->dev->event_lock);
  65. if (drm_crtc_vblank_get(crtc) == 0)
  66. drm_crtc_arm_vblank_event(crtc, event);
  67. else
  68. drm_crtc_send_vblank_event(crtc, event);
  69. spin_unlock_irq(&crtc->dev->event_lock);
  70. }
  71. }
  72. static void sun4i_crtc_atomic_disable(struct drm_crtc *crtc,
  73. struct drm_crtc_state *old_state)
  74. {
  75. struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc);
  76. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  77. DRM_DEBUG_DRIVER("Disabling the CRTC\n");
  78. sun4i_tcon_set_status(scrtc->tcon, encoder, false);
  79. if (crtc->state->event && !crtc->state->active) {
  80. spin_lock_irq(&crtc->dev->event_lock);
  81. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  82. spin_unlock_irq(&crtc->dev->event_lock);
  83. crtc->state->event = NULL;
  84. }
  85. }
  86. static void sun4i_crtc_atomic_enable(struct drm_crtc *crtc,
  87. struct drm_crtc_state *old_state)
  88. {
  89. struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc);
  90. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  91. DRM_DEBUG_DRIVER("Enabling the CRTC\n");
  92. sun4i_tcon_set_status(scrtc->tcon, encoder, true);
  93. }
  94. static void sun4i_crtc_mode_set_nofb(struct drm_crtc *crtc)
  95. {
  96. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  97. struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc);
  98. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  99. sun4i_tcon_mode_set(scrtc->tcon, encoder, mode);
  100. }
  101. static const struct drm_crtc_helper_funcs sun4i_crtc_helper_funcs = {
  102. .atomic_begin = sun4i_crtc_atomic_begin,
  103. .atomic_flush = sun4i_crtc_atomic_flush,
  104. .atomic_enable = sun4i_crtc_atomic_enable,
  105. .atomic_disable = sun4i_crtc_atomic_disable,
  106. .mode_set_nofb = sun4i_crtc_mode_set_nofb,
  107. };
  108. static int sun4i_crtc_enable_vblank(struct drm_crtc *crtc)
  109. {
  110. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  111. DRM_DEBUG_DRIVER("Enabling VBLANK on crtc %p\n", crtc);
  112. sun4i_tcon_enable_vblank(scrtc->tcon, true);
  113. return 0;
  114. }
  115. static void sun4i_crtc_disable_vblank(struct drm_crtc *crtc)
  116. {
  117. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  118. DRM_DEBUG_DRIVER("Disabling VBLANK on crtc %p\n", crtc);
  119. sun4i_tcon_enable_vblank(scrtc->tcon, false);
  120. }
  121. static const struct drm_crtc_funcs sun4i_crtc_funcs = {
  122. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  123. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  124. .destroy = drm_crtc_cleanup,
  125. .page_flip = drm_atomic_helper_page_flip,
  126. .reset = drm_atomic_helper_crtc_reset,
  127. .set_config = drm_atomic_helper_set_config,
  128. .enable_vblank = sun4i_crtc_enable_vblank,
  129. .disable_vblank = sun4i_crtc_disable_vblank,
  130. };
  131. struct sun4i_crtc *sun4i_crtc_init(struct drm_device *drm,
  132. struct sunxi_engine *engine,
  133. struct sun4i_tcon *tcon)
  134. {
  135. struct sun4i_crtc *scrtc;
  136. struct drm_plane **planes;
  137. struct drm_plane *primary = NULL, *cursor = NULL;
  138. int ret, i;
  139. scrtc = devm_kzalloc(drm->dev, sizeof(*scrtc), GFP_KERNEL);
  140. if (!scrtc)
  141. return ERR_PTR(-ENOMEM);
  142. scrtc->engine = engine;
  143. scrtc->tcon = tcon;
  144. /* Create our layers */
  145. planes = sunxi_engine_layers_init(drm, engine);
  146. if (IS_ERR(planes)) {
  147. dev_err(drm->dev, "Couldn't create the planes\n");
  148. return NULL;
  149. }
  150. /* find primary and cursor planes for drm_crtc_init_with_planes */
  151. for (i = 0; planes[i]; i++) {
  152. struct drm_plane *plane = planes[i];
  153. switch (plane->type) {
  154. case DRM_PLANE_TYPE_PRIMARY:
  155. primary = plane;
  156. break;
  157. case DRM_PLANE_TYPE_CURSOR:
  158. cursor = plane;
  159. break;
  160. default:
  161. break;
  162. }
  163. }
  164. ret = drm_crtc_init_with_planes(drm, &scrtc->crtc,
  165. primary,
  166. cursor,
  167. &sun4i_crtc_funcs,
  168. NULL);
  169. if (ret) {
  170. dev_err(drm->dev, "Couldn't init DRM CRTC\n");
  171. return ERR_PTR(ret);
  172. }
  173. drm_crtc_helper_add(&scrtc->crtc, &sun4i_crtc_helper_funcs);
  174. /* Set crtc.port to output port node of the tcon */
  175. scrtc->crtc.port = of_graph_get_port_by_id(scrtc->tcon->dev->of_node,
  176. 1);
  177. /* Set possible_crtcs to this crtc for overlay planes */
  178. for (i = 0; planes[i]; i++) {
  179. uint32_t possible_crtcs = BIT(drm_crtc_index(&scrtc->crtc));
  180. struct drm_plane *plane = planes[i];
  181. if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  182. plane->possible_crtcs = possible_crtcs;
  183. }
  184. return scrtc;
  185. }