tinydrm-pipe.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (C) 2016 Noralf Trønnes
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. */
  9. #include <drm/drm_atomic_helper.h>
  10. #include <drm/drm_crtc_helper.h>
  11. #include <drm/drm_modes.h>
  12. #include <drm/tinydrm/tinydrm.h>
  13. struct tinydrm_connector {
  14. struct drm_connector base;
  15. const struct drm_display_mode *mode;
  16. };
  17. static inline struct tinydrm_connector *
  18. to_tinydrm_connector(struct drm_connector *connector)
  19. {
  20. return container_of(connector, struct tinydrm_connector, base);
  21. }
  22. static int tinydrm_connector_get_modes(struct drm_connector *connector)
  23. {
  24. struct tinydrm_connector *tconn = to_tinydrm_connector(connector);
  25. struct drm_display_mode *mode;
  26. mode = drm_mode_duplicate(connector->dev, tconn->mode);
  27. if (!mode) {
  28. DRM_ERROR("Failed to duplicate mode\n");
  29. return 0;
  30. }
  31. if (mode->name[0] == '\0')
  32. drm_mode_set_name(mode);
  33. mode->type |= DRM_MODE_TYPE_PREFERRED;
  34. drm_mode_probed_add(connector, mode);
  35. if (mode->width_mm) {
  36. connector->display_info.width_mm = mode->width_mm;
  37. connector->display_info.height_mm = mode->height_mm;
  38. }
  39. return 1;
  40. }
  41. static const struct drm_connector_helper_funcs tinydrm_connector_hfuncs = {
  42. .get_modes = tinydrm_connector_get_modes,
  43. .best_encoder = drm_atomic_helper_best_encoder,
  44. };
  45. static enum drm_connector_status
  46. tinydrm_connector_detect(struct drm_connector *connector, bool force)
  47. {
  48. if (drm_device_is_unplugged(connector->dev))
  49. return connector_status_disconnected;
  50. return connector->status;
  51. }
  52. static void tinydrm_connector_destroy(struct drm_connector *connector)
  53. {
  54. struct tinydrm_connector *tconn = to_tinydrm_connector(connector);
  55. drm_connector_cleanup(connector);
  56. kfree(tconn);
  57. }
  58. static const struct drm_connector_funcs tinydrm_connector_funcs = {
  59. .dpms = drm_atomic_helper_connector_dpms,
  60. .reset = drm_atomic_helper_connector_reset,
  61. .detect = tinydrm_connector_detect,
  62. .fill_modes = drm_helper_probe_single_connector_modes,
  63. .destroy = tinydrm_connector_destroy,
  64. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  65. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  66. };
  67. struct drm_connector *
  68. tinydrm_connector_create(struct drm_device *drm,
  69. const struct drm_display_mode *mode,
  70. int connector_type)
  71. {
  72. struct tinydrm_connector *tconn;
  73. struct drm_connector *connector;
  74. int ret;
  75. tconn = kzalloc(sizeof(*tconn), GFP_KERNEL);
  76. if (!tconn)
  77. return ERR_PTR(-ENOMEM);
  78. tconn->mode = mode;
  79. connector = &tconn->base;
  80. drm_connector_helper_add(connector, &tinydrm_connector_hfuncs);
  81. ret = drm_connector_init(drm, connector, &tinydrm_connector_funcs,
  82. connector_type);
  83. if (ret) {
  84. kfree(tconn);
  85. return ERR_PTR(ret);
  86. }
  87. connector->status = connector_status_connected;
  88. return connector;
  89. }
  90. /**
  91. * tinydrm_display_pipe_update - Display pipe update helper
  92. * @pipe: Simple display pipe
  93. * @old_state: Old plane state
  94. *
  95. * This function does a full framebuffer flush if the plane framebuffer
  96. * has changed. It also handles vblank events. Drivers can use this as their
  97. * &drm_simple_display_pipe_funcs->update callback.
  98. */
  99. void tinydrm_display_pipe_update(struct drm_simple_display_pipe *pipe,
  100. struct drm_plane_state *old_state)
  101. {
  102. struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
  103. struct drm_framebuffer *fb = pipe->plane.state->fb;
  104. struct drm_crtc *crtc = &tdev->pipe.crtc;
  105. if (fb && (fb != old_state->fb)) {
  106. pipe->plane.fb = fb;
  107. if (fb->funcs->dirty)
  108. fb->funcs->dirty(fb, NULL, 0, 0, NULL, 0);
  109. }
  110. if (crtc->state->event) {
  111. spin_lock_irq(&crtc->dev->event_lock);
  112. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  113. spin_unlock_irq(&crtc->dev->event_lock);
  114. crtc->state->event = NULL;
  115. }
  116. }
  117. EXPORT_SYMBOL(tinydrm_display_pipe_update);
  118. /**
  119. * tinydrm_display_pipe_prepare_fb - Display pipe prepare_fb helper
  120. * @pipe: Simple display pipe
  121. * @plane_state: Plane state
  122. *
  123. * This function uses drm_fb_cma_prepare_fb() to check if the plane FB has an
  124. * dma-buf attached, extracts the exclusive fence and attaches it to plane
  125. * state for the atomic helper to wait on. Drivers can use this as their
  126. * &drm_simple_display_pipe_funcs->prepare_fb callback.
  127. */
  128. int tinydrm_display_pipe_prepare_fb(struct drm_simple_display_pipe *pipe,
  129. struct drm_plane_state *plane_state)
  130. {
  131. return drm_fb_cma_prepare_fb(&pipe->plane, plane_state);
  132. }
  133. EXPORT_SYMBOL(tinydrm_display_pipe_prepare_fb);
  134. static int tinydrm_rotate_mode(struct drm_display_mode *mode,
  135. unsigned int rotation)
  136. {
  137. if (rotation == 0 || rotation == 180) {
  138. return 0;
  139. } else if (rotation == 90 || rotation == 270) {
  140. swap(mode->hdisplay, mode->vdisplay);
  141. swap(mode->hsync_start, mode->vsync_start);
  142. swap(mode->hsync_end, mode->vsync_end);
  143. swap(mode->htotal, mode->vtotal);
  144. swap(mode->width_mm, mode->height_mm);
  145. return 0;
  146. } else {
  147. return -EINVAL;
  148. }
  149. }
  150. /**
  151. * tinydrm_display_pipe_init - Initialize display pipe
  152. * @tdev: tinydrm device
  153. * @funcs: Display pipe functions
  154. * @connector_type: Connector type
  155. * @formats: Array of supported formats (DRM_FORMAT\_\*)
  156. * @format_count: Number of elements in @formats
  157. * @mode: Supported mode
  158. * @rotation: Initial @mode rotation in degrees Counter Clock Wise
  159. *
  160. * This function sets up a &drm_simple_display_pipe with a &drm_connector that
  161. * has one fixed &drm_display_mode which is rotated according to @rotation.
  162. *
  163. * Returns:
  164. * Zero on success, negative error code on failure.
  165. */
  166. int
  167. tinydrm_display_pipe_init(struct tinydrm_device *tdev,
  168. const struct drm_simple_display_pipe_funcs *funcs,
  169. int connector_type,
  170. const uint32_t *formats,
  171. unsigned int format_count,
  172. const struct drm_display_mode *mode,
  173. unsigned int rotation)
  174. {
  175. struct drm_device *drm = tdev->drm;
  176. struct drm_display_mode *mode_copy;
  177. struct drm_connector *connector;
  178. int ret;
  179. mode_copy = devm_kmalloc(drm->dev, sizeof(*mode_copy), GFP_KERNEL);
  180. if (!mode_copy)
  181. return -ENOMEM;
  182. *mode_copy = *mode;
  183. ret = tinydrm_rotate_mode(mode_copy, rotation);
  184. if (ret) {
  185. DRM_ERROR("Illegal rotation value %u\n", rotation);
  186. return -EINVAL;
  187. }
  188. drm->mode_config.min_width = mode_copy->hdisplay;
  189. drm->mode_config.max_width = mode_copy->hdisplay;
  190. drm->mode_config.min_height = mode_copy->vdisplay;
  191. drm->mode_config.max_height = mode_copy->vdisplay;
  192. connector = tinydrm_connector_create(drm, mode_copy, connector_type);
  193. if (IS_ERR(connector))
  194. return PTR_ERR(connector);
  195. ret = drm_simple_display_pipe_init(drm, &tdev->pipe, funcs, formats,
  196. format_count, NULL, connector);
  197. if (ret)
  198. return ret;
  199. return 0;
  200. }
  201. EXPORT_SYMBOL(tinydrm_display_pipe_init);