tilcdc_plane.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (C) 2015 Texas Instruments
  3. * Author: Jyri Sarha <jsarha@ti.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <drm/drmP.h>
  18. #include <drm/drm_atomic.h>
  19. #include <drm/drm_plane_helper.h>
  20. #include <drm/drm_atomic_helper.h>
  21. #include <uapi/drm/drm_fourcc.h>
  22. #include "tilcdc_drv.h"
  23. static struct drm_plane_funcs tilcdc_plane_funcs = {
  24. .update_plane = drm_atomic_helper_update_plane,
  25. .disable_plane = drm_atomic_helper_disable_plane,
  26. .destroy = drm_plane_cleanup,
  27. .reset = drm_atomic_helper_plane_reset,
  28. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  29. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  30. };
  31. static int tilcdc_plane_atomic_check(struct drm_plane *plane,
  32. struct drm_plane_state *state)
  33. {
  34. struct drm_crtc_state *crtc_state;
  35. struct drm_plane_state *old_state = plane->state;
  36. unsigned int pitch;
  37. if (!state->crtc)
  38. return 0;
  39. if (WARN_ON(!state->fb))
  40. return -EINVAL;
  41. if (state->crtc_x || state->crtc_y) {
  42. dev_err(plane->dev->dev, "%s: crtc position must be zero.",
  43. __func__);
  44. return -EINVAL;
  45. }
  46. crtc_state = drm_atomic_get_existing_crtc_state(state->state,
  47. state->crtc);
  48. /* we should have a crtc state if the plane is attached to a crtc */
  49. if (WARN_ON(!crtc_state))
  50. return 0;
  51. if (crtc_state->mode.hdisplay != state->crtc_w ||
  52. crtc_state->mode.vdisplay != state->crtc_h) {
  53. dev_err(plane->dev->dev,
  54. "%s: Size must match mode (%dx%d == %dx%d)", __func__,
  55. crtc_state->mode.hdisplay, crtc_state->mode.vdisplay,
  56. state->crtc_w, state->crtc_h);
  57. return -EINVAL;
  58. }
  59. pitch = crtc_state->mode.hdisplay *
  60. state->fb->format->cpp[0];
  61. if (state->fb->pitches[0] != pitch) {
  62. dev_err(plane->dev->dev,
  63. "Invalid pitch: fb and crtc widths must be the same");
  64. return -EINVAL;
  65. }
  66. if (state->fb && old_state->fb &&
  67. state->fb->format != old_state->fb->format) {
  68. dev_dbg(plane->dev->dev,
  69. "%s(): pixel format change requires mode_change\n",
  70. __func__);
  71. crtc_state->mode_changed = true;
  72. }
  73. return 0;
  74. }
  75. static void tilcdc_plane_atomic_update(struct drm_plane *plane,
  76. struct drm_plane_state *old_state)
  77. {
  78. struct drm_plane_state *state = plane->state;
  79. if (!state->crtc)
  80. return;
  81. if (WARN_ON(!state->fb || !state->crtc->state))
  82. return;
  83. tilcdc_crtc_update_fb(state->crtc,
  84. state->fb,
  85. state->crtc->state->event);
  86. }
  87. static const struct drm_plane_helper_funcs plane_helper_funcs = {
  88. .atomic_check = tilcdc_plane_atomic_check,
  89. .atomic_update = tilcdc_plane_atomic_update,
  90. };
  91. int tilcdc_plane_init(struct drm_device *dev,
  92. struct drm_plane *plane)
  93. {
  94. struct tilcdc_drm_private *priv = dev->dev_private;
  95. int ret;
  96. ret = drm_plane_init(dev, plane, 1,
  97. &tilcdc_plane_funcs,
  98. priv->pixelformats,
  99. priv->num_pixelformats,
  100. true);
  101. if (ret) {
  102. dev_err(dev->dev, "Failed to initialize plane: %d\n", ret);
  103. return ret;
  104. }
  105. drm_plane_helper_add(plane, &plane_helper_funcs);
  106. return 0;
  107. }