tinydrm-pipe.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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_dev_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. .reset = drm_atomic_helper_connector_reset,
  60. .detect = tinydrm_connector_detect,
  61. .fill_modes = drm_helper_probe_single_connector_modes,
  62. .destroy = tinydrm_connector_destroy,
  63. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  64. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  65. };
  66. struct drm_connector *
  67. tinydrm_connector_create(struct drm_device *drm,
  68. const struct drm_display_mode *mode,
  69. int connector_type)
  70. {
  71. struct tinydrm_connector *tconn;
  72. struct drm_connector *connector;
  73. int ret;
  74. tconn = kzalloc(sizeof(*tconn), GFP_KERNEL);
  75. if (!tconn)
  76. return ERR_PTR(-ENOMEM);
  77. tconn->mode = mode;
  78. connector = &tconn->base;
  79. drm_connector_helper_add(connector, &tinydrm_connector_hfuncs);
  80. ret = drm_connector_init(drm, connector, &tinydrm_connector_funcs,
  81. connector_type);
  82. if (ret) {
  83. kfree(tconn);
  84. return ERR_PTR(ret);
  85. }
  86. connector->status = connector_status_connected;
  87. return connector;
  88. }
  89. /**
  90. * tinydrm_display_pipe_update - Display pipe update helper
  91. * @pipe: Simple display pipe
  92. * @old_state: Old plane state
  93. *
  94. * This function does a full framebuffer flush if the plane framebuffer
  95. * has changed. It also handles vblank events. Drivers can use this as their
  96. * &drm_simple_display_pipe_funcs->update callback.
  97. */
  98. void tinydrm_display_pipe_update(struct drm_simple_display_pipe *pipe,
  99. struct drm_plane_state *old_state)
  100. {
  101. struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
  102. struct drm_framebuffer *fb = pipe->plane.state->fb;
  103. struct drm_crtc *crtc = &tdev->pipe.crtc;
  104. if (fb && (fb != old_state->fb)) {
  105. pipe->plane.fb = fb;
  106. if (fb->funcs->dirty)
  107. fb->funcs->dirty(fb, NULL, 0, 0, NULL, 0);
  108. }
  109. if (crtc->state->event) {
  110. spin_lock_irq(&crtc->dev->event_lock);
  111. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  112. spin_unlock_irq(&crtc->dev->event_lock);
  113. crtc->state->event = NULL;
  114. }
  115. }
  116. EXPORT_SYMBOL(tinydrm_display_pipe_update);
  117. /**
  118. * tinydrm_display_pipe_prepare_fb - Display pipe prepare_fb helper
  119. * @pipe: Simple display pipe
  120. * @plane_state: Plane state
  121. *
  122. * This function uses drm_fb_cma_prepare_fb() to check if the plane FB has an
  123. * dma-buf attached, extracts the exclusive fence and attaches it to plane
  124. * state for the atomic helper to wait on. Drivers can use this as their
  125. * &drm_simple_display_pipe_funcs->prepare_fb callback.
  126. */
  127. int tinydrm_display_pipe_prepare_fb(struct drm_simple_display_pipe *pipe,
  128. struct drm_plane_state *plane_state)
  129. {
  130. return drm_fb_cma_prepare_fb(&pipe->plane, plane_state);
  131. }
  132. EXPORT_SYMBOL(tinydrm_display_pipe_prepare_fb);
  133. static int tinydrm_rotate_mode(struct drm_display_mode *mode,
  134. unsigned int rotation)
  135. {
  136. if (rotation == 0 || rotation == 180) {
  137. return 0;
  138. } else if (rotation == 90 || rotation == 270) {
  139. swap(mode->hdisplay, mode->vdisplay);
  140. swap(mode->hsync_start, mode->vsync_start);
  141. swap(mode->hsync_end, mode->vsync_end);
  142. swap(mode->htotal, mode->vtotal);
  143. swap(mode->width_mm, mode->height_mm);
  144. return 0;
  145. } else {
  146. return -EINVAL;
  147. }
  148. }
  149. /**
  150. * tinydrm_display_pipe_init - Initialize display pipe
  151. * @tdev: tinydrm device
  152. * @funcs: Display pipe functions
  153. * @connector_type: Connector type
  154. * @formats: Array of supported formats (DRM_FORMAT\_\*)
  155. * @format_count: Number of elements in @formats
  156. * @mode: Supported mode
  157. * @rotation: Initial @mode rotation in degrees Counter Clock Wise
  158. *
  159. * This function sets up a &drm_simple_display_pipe with a &drm_connector that
  160. * has one fixed &drm_display_mode which is rotated according to @rotation.
  161. *
  162. * Returns:
  163. * Zero on success, negative error code on failure.
  164. */
  165. int
  166. tinydrm_display_pipe_init(struct tinydrm_device *tdev,
  167. const struct drm_simple_display_pipe_funcs *funcs,
  168. int connector_type,
  169. const uint32_t *formats,
  170. unsigned int format_count,
  171. const struct drm_display_mode *mode,
  172. unsigned int rotation)
  173. {
  174. struct drm_device *drm = tdev->drm;
  175. struct drm_display_mode *mode_copy;
  176. struct drm_connector *connector;
  177. int ret;
  178. mode_copy = devm_kmalloc(drm->dev, sizeof(*mode_copy), GFP_KERNEL);
  179. if (!mode_copy)
  180. return -ENOMEM;
  181. *mode_copy = *mode;
  182. ret = tinydrm_rotate_mode(mode_copy, rotation);
  183. if (ret) {
  184. DRM_ERROR("Illegal rotation value %u\n", rotation);
  185. return -EINVAL;
  186. }
  187. drm->mode_config.min_width = mode_copy->hdisplay;
  188. drm->mode_config.max_width = mode_copy->hdisplay;
  189. drm->mode_config.min_height = mode_copy->vdisplay;
  190. drm->mode_config.max_height = mode_copy->vdisplay;
  191. connector = tinydrm_connector_create(drm, mode_copy, connector_type);
  192. if (IS_ERR(connector))
  193. return PTR_ERR(connector);
  194. ret = drm_simple_display_pipe_init(drm, &tdev->pipe, funcs, formats,
  195. format_count, NULL, connector);
  196. if (ret)
  197. return ret;
  198. return 0;
  199. }
  200. EXPORT_SYMBOL(tinydrm_display_pipe_init);