tilcdc_plane.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015 Texas Instruments
  4. * Author: Jyri Sarha <jsarha@ti.com>
  5. */
  6. #include <drm/drm_atomic.h>
  7. #include <drm/drm_atomic_helper.h>
  8. #include <drm/drm_fourcc.h>
  9. #include <drm/drm_framebuffer.h>
  10. #include "tilcdc_drv.h"
  11. static const struct drm_plane_funcs tilcdc_plane_funcs = {
  12. .update_plane = drm_atomic_helper_update_plane,
  13. .disable_plane = drm_atomic_helper_disable_plane,
  14. .destroy = drm_plane_cleanup,
  15. .reset = drm_atomic_helper_plane_reset,
  16. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  17. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  18. };
  19. static int tilcdc_plane_atomic_check(struct drm_plane *plane,
  20. struct drm_atomic_state *state)
  21. {
  22. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
  23. plane);
  24. struct drm_crtc_state *crtc_state;
  25. struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
  26. plane);
  27. unsigned int pitch;
  28. if (!new_state->crtc)
  29. return 0;
  30. if (WARN_ON(!new_state->fb))
  31. return -EINVAL;
  32. if (new_state->crtc_x || new_state->crtc_y) {
  33. dev_err(plane->dev->dev, "%s: crtc position must be zero.",
  34. __func__);
  35. return -EINVAL;
  36. }
  37. crtc_state = drm_atomic_get_new_crtc_state(state, new_state->crtc);
  38. /* we should have a crtc state if the plane is attached to a crtc */
  39. if (WARN_ON(!crtc_state))
  40. return 0;
  41. if (crtc_state->mode.hdisplay != new_state->crtc_w ||
  42. crtc_state->mode.vdisplay != new_state->crtc_h) {
  43. dev_err(plane->dev->dev,
  44. "%s: Size must match mode (%dx%d == %dx%d)", __func__,
  45. crtc_state->mode.hdisplay, crtc_state->mode.vdisplay,
  46. new_state->crtc_w, new_state->crtc_h);
  47. return -EINVAL;
  48. }
  49. pitch = crtc_state->mode.hdisplay *
  50. new_state->fb->format->cpp[0];
  51. if (new_state->fb->pitches[0] != pitch) {
  52. dev_err(plane->dev->dev,
  53. "Invalid pitch: fb and crtc widths must be the same");
  54. return -EINVAL;
  55. }
  56. if (old_state->fb && new_state->fb->format != old_state->fb->format) {
  57. dev_dbg(plane->dev->dev,
  58. "%s(): pixel format change requires mode_change\n",
  59. __func__);
  60. crtc_state->mode_changed = true;
  61. }
  62. return 0;
  63. }
  64. static void tilcdc_plane_atomic_update(struct drm_plane *plane,
  65. struct drm_atomic_state *state)
  66. {
  67. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
  68. plane);
  69. if (!new_state->crtc)
  70. return;
  71. if (WARN_ON(!new_state->fb || !new_state->crtc->state))
  72. return;
  73. if (tilcdc_crtc_update_fb(new_state->crtc,
  74. new_state->fb,
  75. new_state->crtc->state->event) == 0) {
  76. new_state->crtc->state->event = NULL;
  77. }
  78. }
  79. static const struct drm_plane_helper_funcs plane_helper_funcs = {
  80. .atomic_check = tilcdc_plane_atomic_check,
  81. .atomic_update = tilcdc_plane_atomic_update,
  82. };
  83. int tilcdc_plane_init(struct drm_device *dev,
  84. struct drm_plane *plane)
  85. {
  86. struct tilcdc_drm_private *priv = dev->dev_private;
  87. int ret;
  88. ret = drm_universal_plane_init(dev, plane, 1, &tilcdc_plane_funcs,
  89. priv->pixelformats,
  90. priv->num_pixelformats,
  91. NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
  92. if (ret) {
  93. dev_err(dev->dev, "Failed to initialize plane: %d\n", ret);
  94. return ret;
  95. }
  96. drm_plane_helper_add(plane, &plane_helper_funcs);
  97. return 0;
  98. }