rcar_du_kms.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. struct rcar_du_device *rcdu = dev->dev_private;
  106. unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
  107. unsigned int align;
  108. /* The R8A7779 DU requires a 16 pixels pitch alignment as documented,
  109. * but the R8A7790 DU seems to require a 128 bytes pitch alignment.
  110. */
  111. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_ALIGN_128B))
  112. align = 128;
  113. else
  114. align = 16 * args->bpp / 8;
  115. args->pitch = roundup(max(args->pitch, min_pitch), align);
  116. return drm_gem_cma_dumb_create(file, dev, args);
  117. }
  118. static struct drm_framebuffer *
  119. rcar_du_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  120. struct drm_mode_fb_cmd2 *mode_cmd)
  121. {
  122. struct rcar_du_device *rcdu = dev->dev_private;
  123. const struct rcar_du_format_info *format;
  124. unsigned int align;
  125. format = rcar_du_format_info(mode_cmd->pixel_format);
  126. if (format == NULL) {
  127. dev_dbg(dev->dev, "unsupported pixel format %08x\n",
  128. mode_cmd->pixel_format);
  129. return ERR_PTR(-EINVAL);
  130. }
  131. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_ALIGN_128B))
  132. align = 128;
  133. else
  134. align = 16 * format->bpp / 8;
  135. if (mode_cmd->pitches[0] & (align - 1) ||
  136. mode_cmd->pitches[0] >= 8192) {
  137. dev_dbg(dev->dev, "invalid pitch value %u\n",
  138. mode_cmd->pitches[0]);
  139. return ERR_PTR(-EINVAL);
  140. }
  141. if (format->planes == 2) {
  142. if (mode_cmd->pitches[1] != mode_cmd->pitches[0]) {
  143. dev_dbg(dev->dev,
  144. "luma and chroma pitches do not match\n");
  145. return ERR_PTR(-EINVAL);
  146. }
  147. }
  148. return drm_fb_cma_create(dev, file_priv, mode_cmd);
  149. }
  150. static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = {
  151. .fb_create = rcar_du_fb_create,
  152. };
  153. int rcar_du_modeset_init(struct rcar_du_device *rcdu)
  154. {
  155. static const unsigned int mmio_offsets[] = {
  156. DU0_REG_OFFSET, DU2_REG_OFFSET
  157. };
  158. struct drm_device *dev = rcdu->ddev;
  159. struct drm_encoder *encoder;
  160. unsigned int num_groups;
  161. unsigned int i;
  162. int ret;
  163. drm_mode_config_init(rcdu->ddev);
  164. rcdu->ddev->mode_config.min_width = 0;
  165. rcdu->ddev->mode_config.min_height = 0;
  166. rcdu->ddev->mode_config.max_width = 4095;
  167. rcdu->ddev->mode_config.max_height = 2047;
  168. rcdu->ddev->mode_config.funcs = &rcar_du_mode_config_funcs;
  169. rcdu->num_crtcs = rcdu->info->num_crtcs;
  170. /* Initialize the groups. */
  171. num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2);
  172. for (i = 0; i < num_groups; ++i) {
  173. struct rcar_du_group *rgrp = &rcdu->groups[i];
  174. rgrp->dev = rcdu;
  175. rgrp->mmio_offset = mmio_offsets[i];
  176. rgrp->index = i;
  177. ret = rcar_du_planes_init(rgrp);
  178. if (ret < 0)
  179. return ret;
  180. }
  181. /* Create the CRTCs. */
  182. for (i = 0; i < rcdu->num_crtcs; ++i) {
  183. struct rcar_du_group *rgrp = &rcdu->groups[i / 2];
  184. ret = rcar_du_crtc_create(rgrp, i);
  185. if (ret < 0)
  186. return ret;
  187. }
  188. /* Initialize the encoders. */
  189. for (i = 0; i < rcdu->pdata->num_encoders; ++i) {
  190. const struct rcar_du_encoder_data *pdata =
  191. &rcdu->pdata->encoders[i];
  192. const struct rcar_du_output_routing *route =
  193. &rcdu->info->routes[pdata->output];
  194. if (pdata->type == RCAR_DU_ENCODER_UNUSED)
  195. continue;
  196. if (pdata->output >= RCAR_DU_OUTPUT_MAX ||
  197. route->possible_crtcs == 0) {
  198. dev_warn(rcdu->dev,
  199. "encoder %u references unexisting output %u, skipping\n",
  200. i, pdata->output);
  201. continue;
  202. }
  203. rcar_du_encoder_init(rcdu, pdata->type, pdata->output, pdata);
  204. }
  205. /* Set the possible CRTCs and possible clones. There's always at least
  206. * one way for all encoders to clone each other, set all bits in the
  207. * possible clones field.
  208. */
  209. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  210. struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
  211. const struct rcar_du_output_routing *route =
  212. &rcdu->info->routes[renc->output];
  213. encoder->possible_crtcs = route->possible_crtcs;
  214. encoder->possible_clones = (1 << rcdu->pdata->num_encoders) - 1;
  215. }
  216. /* Now that the CRTCs have been initialized register the planes. */
  217. for (i = 0; i < num_groups; ++i) {
  218. ret = rcar_du_planes_register(&rcdu->groups[i]);
  219. if (ret < 0)
  220. return ret;
  221. }
  222. drm_kms_helper_poll_init(rcdu->ddev);
  223. drm_helper_disable_unused_functions(rcdu->ddev);
  224. return 0;
  225. }