tilcdc_plane.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 const u32 tilcdc_formats[] = { DRM_FORMAT_RGB565,
  24. DRM_FORMAT_RGB888,
  25. DRM_FORMAT_XRGB8888 };
  26. static struct drm_plane_funcs tilcdc_plane_funcs = {
  27. .update_plane = drm_atomic_helper_update_plane,
  28. .disable_plane = drm_atomic_helper_disable_plane,
  29. .destroy = drm_plane_cleanup,
  30. .set_property = drm_atomic_helper_plane_set_property,
  31. .reset = drm_atomic_helper_plane_reset,
  32. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  33. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  34. };
  35. static int tilcdc_plane_atomic_check(struct drm_plane *plane,
  36. struct drm_plane_state *state)
  37. {
  38. struct drm_crtc_state *crtc_state;
  39. struct drm_plane_state *old_state = plane->state;
  40. unsigned int depth, bpp;
  41. if (!state->crtc)
  42. return 0;
  43. if (WARN_ON(!state->fb))
  44. return -EINVAL;
  45. if (state->crtc_x || state->crtc_y) {
  46. dev_err(plane->dev->dev, "%s: crtc position must be zero.",
  47. __func__);
  48. return -EINVAL;
  49. }
  50. crtc_state = drm_atomic_get_existing_crtc_state(state->state,
  51. state->crtc);
  52. /* we should have a crtc state if the plane is attached to a crtc */
  53. if (WARN_ON(!crtc_state))
  54. return 0;
  55. if (crtc_state->mode.hdisplay != state->crtc_w ||
  56. crtc_state->mode.vdisplay != state->crtc_h) {
  57. dev_err(plane->dev->dev,
  58. "%s: Size must match mode (%dx%d == %dx%d)", __func__,
  59. crtc_state->mode.hdisplay, crtc_state->mode.vdisplay,
  60. state->crtc_w, state->crtc_h);
  61. return -EINVAL;
  62. }
  63. drm_fb_get_bpp_depth(state->fb->pixel_format, &depth, &bpp);
  64. if (state->fb->pitches[0] != crtc_state->mode.hdisplay * bpp / 8) {
  65. dev_err(plane->dev->dev,
  66. "Invalid pitch: fb and crtc widths must be the same");
  67. return -EINVAL;
  68. }
  69. if (state->fb && old_state->fb &&
  70. state->fb->pixel_format != old_state->fb->pixel_format) {
  71. dev_dbg(plane->dev->dev,
  72. "%s(): pixel format change requires mode_change\n",
  73. __func__);
  74. crtc_state->mode_changed = true;
  75. }
  76. return 0;
  77. }
  78. static void tilcdc_plane_atomic_update(struct drm_plane *plane,
  79. struct drm_plane_state *old_state)
  80. {
  81. struct drm_plane_state *state = plane->state;
  82. if (!state->crtc)
  83. return;
  84. if (WARN_ON(!state->fb || !state->crtc->state))
  85. return;
  86. tilcdc_crtc_page_flip(state->crtc,
  87. state->fb,
  88. state->crtc->state->event,
  89. 0);
  90. }
  91. static const struct drm_plane_helper_funcs plane_helper_funcs = {
  92. .atomic_check = tilcdc_plane_atomic_check,
  93. .atomic_update = tilcdc_plane_atomic_update,
  94. };
  95. int tilcdc_plane_init(struct drm_device *dev,
  96. struct drm_plane *plane)
  97. {
  98. int ret;
  99. ret = drm_plane_init(dev, plane, 1,
  100. &tilcdc_plane_funcs,
  101. tilcdc_formats,
  102. ARRAY_SIZE(tilcdc_formats),
  103. true);
  104. if (ret) {
  105. dev_err(dev->dev, "Failed to initialize plane: %d\n", ret);
  106. return ret;
  107. }
  108. drm_plane_helper_add(plane, &plane_helper_funcs);
  109. return 0;
  110. }