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 <video/sh_mobile_meram.h>
  19. #include "shmob_drm_crtc.h"
  20. #include "shmob_drm_drv.h"
  21. #include "shmob_drm_kms.h"
  22. #include "shmob_drm_regs.h"
  23. /* -----------------------------------------------------------------------------
  24. * Format helpers
  25. */
  26. static const struct shmob_drm_format_info shmob_drm_format_infos[] = {
  27. {
  28. .fourcc = DRM_FORMAT_RGB565,
  29. .bpp = 16,
  30. .yuv = false,
  31. .lddfr = LDDFR_PKF_RGB16,
  32. .meram = SH_MOBILE_MERAM_PF_RGB,
  33. }, {
  34. .fourcc = DRM_FORMAT_RGB888,
  35. .bpp = 24,
  36. .yuv = false,
  37. .lddfr = LDDFR_PKF_RGB24,
  38. .meram = SH_MOBILE_MERAM_PF_RGB,
  39. }, {
  40. .fourcc = DRM_FORMAT_ARGB8888,
  41. .bpp = 32,
  42. .yuv = false,
  43. .lddfr = LDDFR_PKF_ARGB32,
  44. .meram = SH_MOBILE_MERAM_PF_RGB,
  45. }, {
  46. .fourcc = DRM_FORMAT_NV12,
  47. .bpp = 12,
  48. .yuv = true,
  49. .lddfr = LDDFR_CC | LDDFR_YF_420,
  50. .meram = SH_MOBILE_MERAM_PF_NV,
  51. }, {
  52. .fourcc = DRM_FORMAT_NV21,
  53. .bpp = 12,
  54. .yuv = true,
  55. .lddfr = LDDFR_CC | LDDFR_YF_420,
  56. .meram = SH_MOBILE_MERAM_PF_NV,
  57. }, {
  58. .fourcc = DRM_FORMAT_NV16,
  59. .bpp = 16,
  60. .yuv = true,
  61. .lddfr = LDDFR_CC | LDDFR_YF_422,
  62. .meram = SH_MOBILE_MERAM_PF_NV,
  63. }, {
  64. .fourcc = DRM_FORMAT_NV61,
  65. .bpp = 16,
  66. .yuv = true,
  67. .lddfr = LDDFR_CC | LDDFR_YF_422,
  68. .meram = SH_MOBILE_MERAM_PF_NV,
  69. }, {
  70. .fourcc = DRM_FORMAT_NV24,
  71. .bpp = 24,
  72. .yuv = true,
  73. .lddfr = LDDFR_CC | LDDFR_YF_444,
  74. .meram = SH_MOBILE_MERAM_PF_NV24,
  75. }, {
  76. .fourcc = DRM_FORMAT_NV42,
  77. .bpp = 24,
  78. .yuv = true,
  79. .lddfr = LDDFR_CC | LDDFR_YF_444,
  80. .meram = SH_MOBILE_MERAM_PF_NV24,
  81. },
  82. };
  83. const struct shmob_drm_format_info *shmob_drm_format_info(u32 fourcc)
  84. {
  85. unsigned int i;
  86. for (i = 0; i < ARRAY_SIZE(shmob_drm_format_infos); ++i) {
  87. if (shmob_drm_format_infos[i].fourcc == fourcc)
  88. return &shmob_drm_format_infos[i];
  89. }
  90. return NULL;
  91. }
  92. /* -----------------------------------------------------------------------------
  93. * Frame buffer
  94. */
  95. static struct drm_framebuffer *
  96. shmob_drm_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  97. const struct drm_mode_fb_cmd2 *mode_cmd)
  98. {
  99. const struct shmob_drm_format_info *format;
  100. format = shmob_drm_format_info(mode_cmd->pixel_format);
  101. if (format == NULL) {
  102. dev_dbg(dev->dev, "unsupported pixel format %08x\n",
  103. mode_cmd->pixel_format);
  104. return ERR_PTR(-EINVAL);
  105. }
  106. if (mode_cmd->pitches[0] & 7 || mode_cmd->pitches[0] >= 65536) {
  107. dev_dbg(dev->dev, "invalid pitch value %u\n",
  108. mode_cmd->pitches[0]);
  109. return ERR_PTR(-EINVAL);
  110. }
  111. if (format->yuv) {
  112. unsigned int chroma_cpp = format->bpp == 24 ? 2 : 1;
  113. if (mode_cmd->pitches[1] != mode_cmd->pitches[0] * chroma_cpp) {
  114. dev_dbg(dev->dev,
  115. "luma and chroma pitches do not match\n");
  116. return ERR_PTR(-EINVAL);
  117. }
  118. }
  119. return drm_fb_cma_create(dev, file_priv, mode_cmd);
  120. }
  121. static const struct drm_mode_config_funcs shmob_drm_mode_config_funcs = {
  122. .fb_create = shmob_drm_fb_create,
  123. };
  124. int shmob_drm_modeset_init(struct shmob_drm_device *sdev)
  125. {
  126. drm_mode_config_init(sdev->ddev);
  127. shmob_drm_crtc_create(sdev);
  128. shmob_drm_encoder_create(sdev);
  129. shmob_drm_connector_create(sdev, &sdev->encoder.encoder);
  130. drm_kms_helper_poll_init(sdev->ddev);
  131. sdev->ddev->mode_config.min_width = 0;
  132. sdev->ddev->mode_config.min_height = 0;
  133. sdev->ddev->mode_config.max_width = 4095;
  134. sdev->ddev->mode_config.max_height = 4095;
  135. sdev->ddev->mode_config.funcs = &shmob_drm_mode_config_funcs;
  136. drm_helper_disable_unused_functions(sdev->ddev);
  137. return 0;
  138. }