sun4i_crtc.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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_backend.h"
  25. #include "sun4i_crtc.h"
  26. #include "sun4i_drv.h"
  27. #include "sunxi_engine.h"
  28. #include "sun4i_tcon.h"
  29. /*
  30. * While this isn't really working in the DRM theory, in practice we
  31. * can only ever have one encoder per TCON since we have a mux in our
  32. * TCON.
  33. */
  34. static struct drm_encoder *sun4i_crtc_get_encoder(struct drm_crtc *crtc)
  35. {
  36. struct drm_encoder *encoder;
  37. drm_for_each_encoder(encoder, crtc->dev)
  38. if (encoder->crtc == crtc)
  39. return encoder;
  40. return NULL;
  41. }
  42. static int sun4i_crtc_atomic_check(struct drm_crtc *crtc,
  43. struct drm_crtc_state *state)
  44. {
  45. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  46. struct sunxi_engine *engine = scrtc->engine;
  47. int ret = 0;
  48. if (engine && engine->ops && engine->ops->atomic_check)
  49. ret = engine->ops->atomic_check(engine, state);
  50. return ret;
  51. }
  52. static void sun4i_crtc_atomic_begin(struct drm_crtc *crtc,
  53. struct drm_crtc_state *old_state)
  54. {
  55. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  56. struct drm_device *dev = crtc->dev;
  57. struct sunxi_engine *engine = scrtc->engine;
  58. unsigned long flags;
  59. if (crtc->state->event) {
  60. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  61. spin_lock_irqsave(&dev->event_lock, flags);
  62. scrtc->event = crtc->state->event;
  63. spin_unlock_irqrestore(&dev->event_lock, flags);
  64. crtc->state->event = NULL;
  65. }
  66. if (engine->ops->atomic_begin)
  67. engine->ops->atomic_begin(engine, old_state);
  68. }
  69. static void sun4i_crtc_atomic_flush(struct drm_crtc *crtc,
  70. struct drm_crtc_state *old_state)
  71. {
  72. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  73. struct drm_pending_vblank_event *event = crtc->state->event;
  74. DRM_DEBUG_DRIVER("Committing plane changes\n");
  75. sunxi_engine_commit(scrtc->engine);
  76. if (event) {
  77. crtc->state->event = NULL;
  78. spin_lock_irq(&crtc->dev->event_lock);
  79. if (drm_crtc_vblank_get(crtc) == 0)
  80. drm_crtc_arm_vblank_event(crtc, event);
  81. else
  82. drm_crtc_send_vblank_event(crtc, event);
  83. spin_unlock_irq(&crtc->dev->event_lock);
  84. }
  85. }
  86. static void sun4i_crtc_atomic_disable(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("Disabling the CRTC\n");
  92. drm_crtc_vblank_off(crtc);
  93. sun4i_tcon_set_status(scrtc->tcon, encoder, false);
  94. if (crtc->state->event && !crtc->state->active) {
  95. spin_lock_irq(&crtc->dev->event_lock);
  96. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  97. spin_unlock_irq(&crtc->dev->event_lock);
  98. crtc->state->event = NULL;
  99. }
  100. }
  101. static void sun4i_crtc_atomic_enable(struct drm_crtc *crtc,
  102. struct drm_crtc_state *old_state)
  103. {
  104. struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc);
  105. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  106. DRM_DEBUG_DRIVER("Enabling the CRTC\n");
  107. sun4i_tcon_set_status(scrtc->tcon, encoder, true);
  108. drm_crtc_vblank_on(crtc);
  109. }
  110. static void sun4i_crtc_mode_set_nofb(struct drm_crtc *crtc)
  111. {
  112. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  113. struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc);
  114. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  115. sun4i_tcon_mode_set(scrtc->tcon, encoder, mode);
  116. }
  117. static const struct drm_crtc_helper_funcs sun4i_crtc_helper_funcs = {
  118. .atomic_check = sun4i_crtc_atomic_check,
  119. .atomic_begin = sun4i_crtc_atomic_begin,
  120. .atomic_flush = sun4i_crtc_atomic_flush,
  121. .atomic_enable = sun4i_crtc_atomic_enable,
  122. .atomic_disable = sun4i_crtc_atomic_disable,
  123. .mode_set_nofb = sun4i_crtc_mode_set_nofb,
  124. };
  125. static int sun4i_crtc_enable_vblank(struct drm_crtc *crtc)
  126. {
  127. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  128. DRM_DEBUG_DRIVER("Enabling VBLANK on crtc %p\n", crtc);
  129. sun4i_tcon_enable_vblank(scrtc->tcon, true);
  130. return 0;
  131. }
  132. static void sun4i_crtc_disable_vblank(struct drm_crtc *crtc)
  133. {
  134. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  135. DRM_DEBUG_DRIVER("Disabling VBLANK on crtc %p\n", crtc);
  136. sun4i_tcon_enable_vblank(scrtc->tcon, false);
  137. }
  138. static const struct drm_crtc_funcs sun4i_crtc_funcs = {
  139. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  140. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  141. .destroy = drm_crtc_cleanup,
  142. .page_flip = drm_atomic_helper_page_flip,
  143. .reset = drm_atomic_helper_crtc_reset,
  144. .set_config = drm_atomic_helper_set_config,
  145. .enable_vblank = sun4i_crtc_enable_vblank,
  146. .disable_vblank = sun4i_crtc_disable_vblank,
  147. };
  148. struct sun4i_crtc *sun4i_crtc_init(struct drm_device *drm,
  149. struct sunxi_engine *engine,
  150. struct sun4i_tcon *tcon)
  151. {
  152. struct sun4i_crtc *scrtc;
  153. struct drm_plane **planes;
  154. struct drm_plane *primary = NULL, *cursor = NULL;
  155. int ret, i;
  156. scrtc = devm_kzalloc(drm->dev, sizeof(*scrtc), GFP_KERNEL);
  157. if (!scrtc)
  158. return ERR_PTR(-ENOMEM);
  159. scrtc->engine = engine;
  160. scrtc->tcon = tcon;
  161. /* Create our layers */
  162. planes = sunxi_engine_layers_init(drm, engine);
  163. if (IS_ERR(planes)) {
  164. dev_err(drm->dev, "Couldn't create the planes\n");
  165. return NULL;
  166. }
  167. /* find primary and cursor planes for drm_crtc_init_with_planes */
  168. for (i = 0; planes[i]; i++) {
  169. struct drm_plane *plane = planes[i];
  170. switch (plane->type) {
  171. case DRM_PLANE_TYPE_PRIMARY:
  172. primary = plane;
  173. break;
  174. case DRM_PLANE_TYPE_CURSOR:
  175. cursor = plane;
  176. break;
  177. default:
  178. break;
  179. }
  180. }
  181. ret = drm_crtc_init_with_planes(drm, &scrtc->crtc,
  182. primary,
  183. cursor,
  184. &sun4i_crtc_funcs,
  185. NULL);
  186. if (ret) {
  187. dev_err(drm->dev, "Couldn't init DRM CRTC\n");
  188. return ERR_PTR(ret);
  189. }
  190. drm_crtc_helper_add(&scrtc->crtc, &sun4i_crtc_helper_funcs);
  191. /* Set crtc.port to output port node of the tcon */
  192. scrtc->crtc.port = of_graph_get_port_by_id(scrtc->tcon->dev->of_node,
  193. 1);
  194. /* Set possible_crtcs to this crtc for overlay planes */
  195. for (i = 0; planes[i]; i++) {
  196. uint32_t possible_crtcs = drm_crtc_mask(&scrtc->crtc);
  197. struct drm_plane *plane = planes[i];
  198. if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  199. plane->possible_crtcs = possible_crtcs;
  200. }
  201. return scrtc;
  202. }