shmob_drm_kms.c 3.9 KB

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