rcar_du_kms.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * rcar_du_kms.c -- R-Car Display Unit Mode Setting
  3. *
  4. * Copyright (C) 2013 Renesas Corporation
  5. *
  6. * Contact: 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 "rcar_du_crtc.h"
  19. #include "rcar_du_drv.h"
  20. #include "rcar_du_encoder.h"
  21. #include "rcar_du_kms.h"
  22. #include "rcar_du_regs.h"
  23. /* -----------------------------------------------------------------------------
  24. * Format helpers
  25. */
  26. static const struct rcar_du_format_info rcar_du_format_infos[] = {
  27. {
  28. .fourcc = DRM_FORMAT_RGB565,
  29. .bpp = 16,
  30. .planes = 1,
  31. .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
  32. .edf = PnDDCR4_EDF_NONE,
  33. }, {
  34. .fourcc = DRM_FORMAT_ARGB1555,
  35. .bpp = 16,
  36. .planes = 1,
  37. .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
  38. .edf = PnDDCR4_EDF_NONE,
  39. }, {
  40. .fourcc = DRM_FORMAT_XRGB1555,
  41. .bpp = 16,
  42. .planes = 1,
  43. .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
  44. .edf = PnDDCR4_EDF_NONE,
  45. }, {
  46. .fourcc = DRM_FORMAT_XRGB8888,
  47. .bpp = 32,
  48. .planes = 1,
  49. .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
  50. .edf = PnDDCR4_EDF_RGB888,
  51. }, {
  52. .fourcc = DRM_FORMAT_ARGB8888,
  53. .bpp = 32,
  54. .planes = 1,
  55. .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_16BPP,
  56. .edf = PnDDCR4_EDF_ARGB8888,
  57. }, {
  58. .fourcc = DRM_FORMAT_UYVY,
  59. .bpp = 16,
  60. .planes = 1,
  61. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  62. .edf = PnDDCR4_EDF_NONE,
  63. }, {
  64. .fourcc = DRM_FORMAT_YUYV,
  65. .bpp = 16,
  66. .planes = 1,
  67. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  68. .edf = PnDDCR4_EDF_NONE,
  69. }, {
  70. .fourcc = DRM_FORMAT_NV12,
  71. .bpp = 12,
  72. .planes = 2,
  73. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  74. .edf = PnDDCR4_EDF_NONE,
  75. }, {
  76. .fourcc = DRM_FORMAT_NV21,
  77. .bpp = 12,
  78. .planes = 2,
  79. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  80. .edf = PnDDCR4_EDF_NONE,
  81. }, {
  82. /* In YUV 4:2:2, only NV16 is supported (NV61 isn't) */
  83. .fourcc = DRM_FORMAT_NV16,
  84. .bpp = 16,
  85. .planes = 2,
  86. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  87. .edf = PnDDCR4_EDF_NONE,
  88. },
  89. };
  90. const struct rcar_du_format_info *rcar_du_format_info(u32 fourcc)
  91. {
  92. unsigned int i;
  93. for (i = 0; i < ARRAY_SIZE(rcar_du_format_infos); ++i) {
  94. if (rcar_du_format_infos[i].fourcc == fourcc)
  95. return &rcar_du_format_infos[i];
  96. }
  97. return NULL;
  98. }
  99. /* -----------------------------------------------------------------------------
  100. * Frame buffer
  101. */
  102. int rcar_du_dumb_create(struct drm_file *file, struct drm_device *dev,
  103. struct drm_mode_create_dumb *args)
  104. {
  105. unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
  106. unsigned int align;
  107. /* The pitch must be aligned to a 16 pixels boundary. */
  108. align = 16 * args->bpp / 8;
  109. args->pitch = roundup(max(args->pitch, min_pitch), align);
  110. return drm_gem_cma_dumb_create(file, dev, args);
  111. }
  112. static struct drm_framebuffer *
  113. rcar_du_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  114. struct drm_mode_fb_cmd2 *mode_cmd)
  115. {
  116. const struct rcar_du_format_info *format;
  117. unsigned int align;
  118. format = rcar_du_format_info(mode_cmd->pixel_format);
  119. if (format == NULL) {
  120. dev_dbg(dev->dev, "unsupported pixel format %08x\n",
  121. mode_cmd->pixel_format);
  122. return ERR_PTR(-EINVAL);
  123. }
  124. align = 16 * format->bpp / 8;
  125. if (mode_cmd->pitches[0] & (align - 1) ||
  126. mode_cmd->pitches[0] >= 8192) {
  127. dev_dbg(dev->dev, "invalid pitch value %u\n",
  128. mode_cmd->pitches[0]);
  129. return ERR_PTR(-EINVAL);
  130. }
  131. if (format->planes == 2) {
  132. if (mode_cmd->pitches[1] != mode_cmd->pitches[0]) {
  133. dev_dbg(dev->dev,
  134. "luma and chroma pitches do not match\n");
  135. return ERR_PTR(-EINVAL);
  136. }
  137. }
  138. return drm_fb_cma_create(dev, file_priv, mode_cmd);
  139. }
  140. static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = {
  141. .fb_create = rcar_du_fb_create,
  142. };
  143. int rcar_du_modeset_init(struct rcar_du_device *rcdu)
  144. {
  145. struct drm_device *dev = rcdu->ddev;
  146. struct drm_encoder *encoder;
  147. unsigned int i;
  148. int ret;
  149. drm_mode_config_init(rcdu->ddev);
  150. rcdu->ddev->mode_config.min_width = 0;
  151. rcdu->ddev->mode_config.min_height = 0;
  152. rcdu->ddev->mode_config.max_width = 4095;
  153. rcdu->ddev->mode_config.max_height = 2047;
  154. rcdu->ddev->mode_config.funcs = &rcar_du_mode_config_funcs;
  155. rcdu->group.dev = rcdu;
  156. rcdu->group.index = 0;
  157. rcdu->group.used_crtcs = 0;
  158. ret = rcar_du_planes_init(&rcdu->group);
  159. if (ret < 0)
  160. return ret;
  161. for (i = 0; i < ARRAY_SIZE(rcdu->crtcs); ++i) {
  162. ret = rcar_du_crtc_create(&rcdu->group, i);
  163. if (ret < 0)
  164. return ret;
  165. }
  166. rcdu->num_crtcs = i;
  167. for (i = 0; i < rcdu->pdata->num_encoders; ++i) {
  168. const struct rcar_du_encoder_data *pdata =
  169. &rcdu->pdata->encoders[i];
  170. if (pdata->type == RCAR_DU_ENCODER_UNUSED)
  171. continue;
  172. if (pdata->output >= rcdu->num_crtcs) {
  173. dev_warn(rcdu->dev,
  174. "encoder %u references unexisting output %u, skipping\n",
  175. i, pdata->output);
  176. continue;
  177. }
  178. rcar_du_encoder_init(rcdu, pdata->type, pdata->output, pdata);
  179. }
  180. /* Set the possible CRTCs and possible clones. All encoders can be
  181. * driven by the CRTC associated with the output they're connected to,
  182. * as well as by CRTC 0.
  183. */
  184. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  185. struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
  186. encoder->possible_crtcs = (1 << 0) | (1 << renc->output);
  187. encoder->possible_clones = 1 << 0;
  188. }
  189. ret = rcar_du_planes_register(&rcdu->group);
  190. if (ret < 0)
  191. return ret;
  192. drm_kms_helper_poll_init(rcdu->ddev);
  193. drm_helper_disable_unused_functions(rcdu->ddev);
  194. return 0;
  195. }