tinydrm-pipe.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_gem_framebuffer_helper.h>
  12. #include <drm/drm_modes.h>
  13. #include <drm/tinydrm/tinydrm.h>
  14. struct tinydrm_connector {
  15. struct drm_connector base;
  16. struct drm_display_mode mode;
  17. };
  18. static inline struct tinydrm_connector *
  19. to_tinydrm_connector(struct drm_connector *connector)
  20. {
  21. return container_of(connector, struct tinydrm_connector, base);
  22. }
  23. static int tinydrm_connector_get_modes(struct drm_connector *connector)
  24. {
  25. struct tinydrm_connector *tconn = to_tinydrm_connector(connector);
  26. struct drm_display_mode *mode;
  27. mode = drm_mode_duplicate(connector->dev, &tconn->mode);
  28. if (!mode) {
  29. DRM_ERROR("Failed to duplicate mode\n");
  30. return 0;
  31. }
  32. if (mode->name[0] == '\0')
  33. drm_mode_set_name(mode);
  34. mode->type |= DRM_MODE_TYPE_PREFERRED;
  35. drm_mode_probed_add(connector, mode);
  36. if (mode->width_mm) {
  37. connector->display_info.width_mm = mode->width_mm;
  38. connector->display_info.height_mm = mode->height_mm;
  39. }
  40. return 1;
  41. }
  42. static const struct drm_connector_helper_funcs tinydrm_connector_hfuncs = {
  43. .get_modes = tinydrm_connector_get_modes,
  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. drm_mode_copy(&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. if (tdev->fb_dirty)
  106. tdev->fb_dirty(fb, NULL, 0, 0, NULL, 0);
  107. }
  108. if (crtc->state->event) {
  109. spin_lock_irq(&crtc->dev->event_lock);
  110. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  111. spin_unlock_irq(&crtc->dev->event_lock);
  112. crtc->state->event = NULL;
  113. }
  114. }
  115. EXPORT_SYMBOL(tinydrm_display_pipe_update);
  116. /**
  117. * tinydrm_display_pipe_prepare_fb - Display pipe prepare_fb helper
  118. * @pipe: Simple display pipe
  119. * @plane_state: Plane state
  120. *
  121. * This function uses drm_gem_fb_prepare_fb() to check if the plane FB has an
  122. * dma-buf attached, extracts the exclusive fence and attaches it to plane
  123. * state for the atomic helper to wait on. Drivers can use this as their
  124. * &drm_simple_display_pipe_funcs->prepare_fb callback.
  125. */
  126. int tinydrm_display_pipe_prepare_fb(struct drm_simple_display_pipe *pipe,
  127. struct drm_plane_state *plane_state)
  128. {
  129. return drm_gem_fb_prepare_fb(&pipe->plane, plane_state);
  130. }
  131. EXPORT_SYMBOL(tinydrm_display_pipe_prepare_fb);
  132. static int tinydrm_rotate_mode(struct drm_display_mode *mode,
  133. unsigned int rotation)
  134. {
  135. if (rotation == 0 || rotation == 180) {
  136. return 0;
  137. } else if (rotation == 90 || rotation == 270) {
  138. swap(mode->hdisplay, mode->vdisplay);
  139. swap(mode->hsync_start, mode->vsync_start);
  140. swap(mode->hsync_end, mode->vsync_end);
  141. swap(mode->htotal, mode->vtotal);
  142. swap(mode->width_mm, mode->height_mm);
  143. return 0;
  144. } else {
  145. return -EINVAL;
  146. }
  147. }
  148. /**
  149. * tinydrm_display_pipe_init - Initialize display pipe
  150. * @tdev: tinydrm device
  151. * @funcs: Display pipe functions
  152. * @connector_type: Connector type
  153. * @formats: Array of supported formats (DRM_FORMAT\_\*)
  154. * @format_count: Number of elements in @formats
  155. * @mode: Supported mode
  156. * @rotation: Initial @mode rotation in degrees Counter Clock Wise
  157. *
  158. * This function sets up a &drm_simple_display_pipe with a &drm_connector that
  159. * has one fixed &drm_display_mode which is rotated according to @rotation.
  160. *
  161. * Returns:
  162. * Zero on success, negative error code on failure.
  163. */
  164. int
  165. tinydrm_display_pipe_init(struct tinydrm_device *tdev,
  166. const struct drm_simple_display_pipe_funcs *funcs,
  167. int connector_type,
  168. const uint32_t *formats,
  169. unsigned int format_count,
  170. const struct drm_display_mode *mode,
  171. unsigned int rotation)
  172. {
  173. struct drm_device *drm = tdev->drm;
  174. struct drm_display_mode mode_copy;
  175. struct drm_connector *connector;
  176. int ret;
  177. drm_mode_copy(&mode_copy, mode);
  178. ret = tinydrm_rotate_mode(&mode_copy, rotation);
  179. if (ret) {
  180. DRM_ERROR("Illegal rotation value %u\n", rotation);
  181. return -EINVAL;
  182. }
  183. drm->mode_config.min_width = mode_copy.hdisplay;
  184. drm->mode_config.max_width = mode_copy.hdisplay;
  185. drm->mode_config.min_height = mode_copy.vdisplay;
  186. drm->mode_config.max_height = mode_copy.vdisplay;
  187. connector = tinydrm_connector_create(drm, &mode_copy, connector_type);
  188. if (IS_ERR(connector))
  189. return PTR_ERR(connector);
  190. return drm_simple_display_pipe_init(drm, &tdev->pipe, funcs, formats,
  191. format_count, NULL, connector);
  192. }
  193. EXPORT_SYMBOL(tinydrm_display_pipe_init);