sun4i_crtc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 "sun4i_layer.h"
  28. #include "sun4i_tcon.h"
  29. static void sun4i_crtc_atomic_begin(struct drm_crtc *crtc,
  30. struct drm_crtc_state *old_state)
  31. {
  32. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  33. struct drm_device *dev = crtc->dev;
  34. unsigned long flags;
  35. if (crtc->state->event) {
  36. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  37. spin_lock_irqsave(&dev->event_lock, flags);
  38. scrtc->event = crtc->state->event;
  39. spin_unlock_irqrestore(&dev->event_lock, flags);
  40. crtc->state->event = NULL;
  41. }
  42. }
  43. static void sun4i_crtc_atomic_flush(struct drm_crtc *crtc,
  44. struct drm_crtc_state *old_state)
  45. {
  46. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  47. struct drm_pending_vblank_event *event = crtc->state->event;
  48. DRM_DEBUG_DRIVER("Committing plane changes\n");
  49. sun4i_backend_commit(scrtc->backend);
  50. if (event) {
  51. crtc->state->event = NULL;
  52. spin_lock_irq(&crtc->dev->event_lock);
  53. if (drm_crtc_vblank_get(crtc) == 0)
  54. drm_crtc_arm_vblank_event(crtc, event);
  55. else
  56. drm_crtc_send_vblank_event(crtc, event);
  57. spin_unlock_irq(&crtc->dev->event_lock);
  58. }
  59. }
  60. static void sun4i_crtc_disable(struct drm_crtc *crtc)
  61. {
  62. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  63. DRM_DEBUG_DRIVER("Disabling the CRTC\n");
  64. sun4i_tcon_disable(scrtc->tcon);
  65. if (crtc->state->event && !crtc->state->active) {
  66. spin_lock_irq(&crtc->dev->event_lock);
  67. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  68. spin_unlock_irq(&crtc->dev->event_lock);
  69. crtc->state->event = NULL;
  70. }
  71. }
  72. static void sun4i_crtc_enable(struct drm_crtc *crtc)
  73. {
  74. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  75. DRM_DEBUG_DRIVER("Enabling the CRTC\n");
  76. sun4i_tcon_enable(scrtc->tcon);
  77. }
  78. static const struct drm_crtc_helper_funcs sun4i_crtc_helper_funcs = {
  79. .atomic_begin = sun4i_crtc_atomic_begin,
  80. .atomic_flush = sun4i_crtc_atomic_flush,
  81. .disable = sun4i_crtc_disable,
  82. .enable = sun4i_crtc_enable,
  83. };
  84. static int sun4i_crtc_enable_vblank(struct drm_crtc *crtc)
  85. {
  86. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  87. DRM_DEBUG_DRIVER("Enabling VBLANK on crtc %p\n", crtc);
  88. sun4i_tcon_enable_vblank(scrtc->tcon, true);
  89. return 0;
  90. }
  91. static void sun4i_crtc_disable_vblank(struct drm_crtc *crtc)
  92. {
  93. struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
  94. DRM_DEBUG_DRIVER("Disabling VBLANK on crtc %p\n", crtc);
  95. sun4i_tcon_enable_vblank(scrtc->tcon, false);
  96. }
  97. static const struct drm_crtc_funcs sun4i_crtc_funcs = {
  98. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  99. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  100. .destroy = drm_crtc_cleanup,
  101. .page_flip = drm_atomic_helper_page_flip,
  102. .reset = drm_atomic_helper_crtc_reset,
  103. .set_config = drm_atomic_helper_set_config,
  104. .enable_vblank = sun4i_crtc_enable_vblank,
  105. .disable_vblank = sun4i_crtc_disable_vblank,
  106. };
  107. struct sun4i_crtc *sun4i_crtc_init(struct drm_device *drm,
  108. struct sun4i_backend *backend,
  109. struct sun4i_tcon *tcon)
  110. {
  111. struct sun4i_crtc *scrtc;
  112. struct drm_plane *primary = NULL, *cursor = NULL;
  113. int ret, i;
  114. scrtc = devm_kzalloc(drm->dev, sizeof(*scrtc), GFP_KERNEL);
  115. if (!scrtc)
  116. return ERR_PTR(-ENOMEM);
  117. scrtc->backend = backend;
  118. scrtc->tcon = tcon;
  119. /* Create our layers */
  120. scrtc->layers = sun4i_layers_init(drm, scrtc->backend);
  121. if (IS_ERR(scrtc->layers)) {
  122. dev_err(drm->dev, "Couldn't create the planes\n");
  123. return NULL;
  124. }
  125. /* find primary and cursor planes for drm_crtc_init_with_planes */
  126. for (i = 0; scrtc->layers[i]; i++) {
  127. struct sun4i_layer *layer = scrtc->layers[i];
  128. switch (layer->plane.type) {
  129. case DRM_PLANE_TYPE_PRIMARY:
  130. primary = &layer->plane;
  131. break;
  132. case DRM_PLANE_TYPE_CURSOR:
  133. cursor = &layer->plane;
  134. break;
  135. default:
  136. break;
  137. }
  138. }
  139. ret = drm_crtc_init_with_planes(drm, &scrtc->crtc,
  140. primary,
  141. cursor,
  142. &sun4i_crtc_funcs,
  143. NULL);
  144. if (ret) {
  145. dev_err(drm->dev, "Couldn't init DRM CRTC\n");
  146. return ERR_PTR(ret);
  147. }
  148. drm_crtc_helper_add(&scrtc->crtc, &sun4i_crtc_helper_funcs);
  149. /* Set crtc.port to output port node of the tcon */
  150. scrtc->crtc.port = of_graph_get_port_by_id(scrtc->tcon->dev->of_node,
  151. 1);
  152. /* Set possible_crtcs to this crtc for overlay planes */
  153. for (i = 0; scrtc->layers[i]; i++) {
  154. uint32_t possible_crtcs = BIT(drm_crtc_index(&scrtc->crtc));
  155. struct sun4i_layer *layer = scrtc->layers[i];
  156. if (layer->plane.type == DRM_PLANE_TYPE_OVERLAY)
  157. layer->plane.possible_crtcs = possible_crtcs;
  158. }
  159. return scrtc;
  160. }