shmob_drm_kms.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * shmob_drm_kms.c -- SH Mobile DRM Mode Setting
  4. *
  5. * Copyright (C) 2012 Renesas Electronics Corporation
  6. *
  7. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  8. */
  9. #include <drm/drm_crtc.h>
  10. #include <drm/drm_crtc_helper.h>
  11. #include <drm/drm_fb_cma_helper.h>
  12. #include <drm/drm_gem_cma_helper.h>
  13. #include <drm/drm_gem_framebuffer_helper.h>
  14. #include <drm/drm_probe_helper.h>
  15. #include "shmob_drm_crtc.h"
  16. #include "shmob_drm_drv.h"
  17. #include "shmob_drm_kms.h"
  18. #include "shmob_drm_regs.h"
  19. /* -----------------------------------------------------------------------------
  20. * Format helpers
  21. */
  22. static const struct shmob_drm_format_info shmob_drm_format_infos[] = {
  23. {
  24. .fourcc = DRM_FORMAT_RGB565,
  25. .bpp = 16,
  26. .yuv = false,
  27. .lddfr = LDDFR_PKF_RGB16,
  28. }, {
  29. .fourcc = DRM_FORMAT_RGB888,
  30. .bpp = 24,
  31. .yuv = false,
  32. .lddfr = LDDFR_PKF_RGB24,
  33. }, {
  34. .fourcc = DRM_FORMAT_ARGB8888,
  35. .bpp = 32,
  36. .yuv = false,
  37. .lddfr = LDDFR_PKF_ARGB32,
  38. }, {
  39. .fourcc = DRM_FORMAT_NV12,
  40. .bpp = 12,
  41. .yuv = true,
  42. .lddfr = LDDFR_CC | LDDFR_YF_420,
  43. }, {
  44. .fourcc = DRM_FORMAT_NV21,
  45. .bpp = 12,
  46. .yuv = true,
  47. .lddfr = LDDFR_CC | LDDFR_YF_420,
  48. }, {
  49. .fourcc = DRM_FORMAT_NV16,
  50. .bpp = 16,
  51. .yuv = true,
  52. .lddfr = LDDFR_CC | LDDFR_YF_422,
  53. }, {
  54. .fourcc = DRM_FORMAT_NV61,
  55. .bpp = 16,
  56. .yuv = true,
  57. .lddfr = LDDFR_CC | LDDFR_YF_422,
  58. }, {
  59. .fourcc = DRM_FORMAT_NV24,
  60. .bpp = 24,
  61. .yuv = true,
  62. .lddfr = LDDFR_CC | LDDFR_YF_444,
  63. }, {
  64. .fourcc = DRM_FORMAT_NV42,
  65. .bpp = 24,
  66. .yuv = true,
  67. .lddfr = LDDFR_CC | LDDFR_YF_444,
  68. },
  69. };
  70. const struct shmob_drm_format_info *shmob_drm_format_info(u32 fourcc)
  71. {
  72. unsigned int i;
  73. for (i = 0; i < ARRAY_SIZE(shmob_drm_format_infos); ++i) {
  74. if (shmob_drm_format_infos[i].fourcc == fourcc)
  75. return &shmob_drm_format_infos[i];
  76. }
  77. return NULL;
  78. }
  79. /* -----------------------------------------------------------------------------
  80. * Frame buffer
  81. */
  82. static struct drm_framebuffer *
  83. shmob_drm_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  84. const struct drm_mode_fb_cmd2 *mode_cmd)
  85. {
  86. const struct shmob_drm_format_info *format;
  87. format = shmob_drm_format_info(mode_cmd->pixel_format);
  88. if (format == NULL) {
  89. dev_dbg(dev->dev, "unsupported pixel format %08x\n",
  90. mode_cmd->pixel_format);
  91. return ERR_PTR(-EINVAL);
  92. }
  93. if (mode_cmd->pitches[0] & 7 || mode_cmd->pitches[0] >= 65536) {
  94. dev_dbg(dev->dev, "invalid pitch value %u\n",
  95. mode_cmd->pitches[0]);
  96. return ERR_PTR(-EINVAL);
  97. }
  98. if (format->yuv) {
  99. unsigned int chroma_cpp = format->bpp == 24 ? 2 : 1;
  100. if (mode_cmd->pitches[1] != mode_cmd->pitches[0] * chroma_cpp) {
  101. dev_dbg(dev->dev,
  102. "luma and chroma pitches do not match\n");
  103. return ERR_PTR(-EINVAL);
  104. }
  105. }
  106. return drm_gem_fb_create(dev, file_priv, mode_cmd);
  107. }
  108. static const struct drm_mode_config_funcs shmob_drm_mode_config_funcs = {
  109. .fb_create = shmob_drm_fb_create,
  110. };
  111. int shmob_drm_modeset_init(struct shmob_drm_device *sdev)
  112. {
  113. int ret;
  114. ret = drmm_mode_config_init(sdev->ddev);
  115. if (ret)
  116. return ret;
  117. shmob_drm_crtc_create(sdev);
  118. shmob_drm_encoder_create(sdev);
  119. shmob_drm_connector_create(sdev, &sdev->encoder.encoder);
  120. drm_kms_helper_poll_init(sdev->ddev);
  121. sdev->ddev->mode_config.min_width = 0;
  122. sdev->ddev->mode_config.min_height = 0;
  123. sdev->ddev->mode_config.max_width = 4095;
  124. sdev->ddev->mode_config.max_height = 4095;
  125. sdev->ddev->mode_config.funcs = &shmob_drm_mode_config_funcs;
  126. drm_helper_disable_unused_functions(sdev->ddev);
  127. return 0;
  128. }