rcar_du_kms.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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_lvdsenc.h"
  23. #include "rcar_du_regs.h"
  24. /* -----------------------------------------------------------------------------
  25. * Format helpers
  26. */
  27. static const struct rcar_du_format_info rcar_du_format_infos[] = {
  28. {
  29. .fourcc = DRM_FORMAT_RGB565,
  30. .bpp = 16,
  31. .planes = 1,
  32. .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
  33. .edf = PnDDCR4_EDF_NONE,
  34. }, {
  35. .fourcc = DRM_FORMAT_ARGB1555,
  36. .bpp = 16,
  37. .planes = 1,
  38. .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
  39. .edf = PnDDCR4_EDF_NONE,
  40. }, {
  41. .fourcc = DRM_FORMAT_XRGB1555,
  42. .bpp = 16,
  43. .planes = 1,
  44. .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
  45. .edf = PnDDCR4_EDF_NONE,
  46. }, {
  47. .fourcc = DRM_FORMAT_XRGB8888,
  48. .bpp = 32,
  49. .planes = 1,
  50. .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
  51. .edf = PnDDCR4_EDF_RGB888,
  52. }, {
  53. .fourcc = DRM_FORMAT_ARGB8888,
  54. .bpp = 32,
  55. .planes = 1,
  56. .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_16BPP,
  57. .edf = PnDDCR4_EDF_ARGB8888,
  58. }, {
  59. .fourcc = DRM_FORMAT_UYVY,
  60. .bpp = 16,
  61. .planes = 1,
  62. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  63. .edf = PnDDCR4_EDF_NONE,
  64. }, {
  65. .fourcc = DRM_FORMAT_YUYV,
  66. .bpp = 16,
  67. .planes = 1,
  68. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  69. .edf = PnDDCR4_EDF_NONE,
  70. }, {
  71. .fourcc = DRM_FORMAT_NV12,
  72. .bpp = 12,
  73. .planes = 2,
  74. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  75. .edf = PnDDCR4_EDF_NONE,
  76. }, {
  77. .fourcc = DRM_FORMAT_NV21,
  78. .bpp = 12,
  79. .planes = 2,
  80. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  81. .edf = PnDDCR4_EDF_NONE,
  82. }, {
  83. /* In YUV 4:2:2, only NV16 is supported (NV61 isn't) */
  84. .fourcc = DRM_FORMAT_NV16,
  85. .bpp = 16,
  86. .planes = 2,
  87. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  88. .edf = PnDDCR4_EDF_NONE,
  89. },
  90. };
  91. const struct rcar_du_format_info *rcar_du_format_info(u32 fourcc)
  92. {
  93. unsigned int i;
  94. for (i = 0; i < ARRAY_SIZE(rcar_du_format_infos); ++i) {
  95. if (rcar_du_format_infos[i].fourcc == fourcc)
  96. return &rcar_du_format_infos[i];
  97. }
  98. return NULL;
  99. }
  100. /* -----------------------------------------------------------------------------
  101. * Frame buffer
  102. */
  103. int rcar_du_dumb_create(struct drm_file *file, struct drm_device *dev,
  104. struct drm_mode_create_dumb *args)
  105. {
  106. struct rcar_du_device *rcdu = dev->dev_private;
  107. unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
  108. unsigned int align;
  109. /* The R8A7779 DU requires a 16 pixels pitch alignment as documented,
  110. * but the R8A7790 DU seems to require a 128 bytes pitch alignment.
  111. */
  112. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_ALIGN_128B))
  113. align = 128;
  114. else
  115. align = 16 * args->bpp / 8;
  116. args->pitch = roundup(max(args->pitch, min_pitch), align);
  117. return drm_gem_cma_dumb_create(file, dev, args);
  118. }
  119. static struct drm_framebuffer *
  120. rcar_du_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  121. struct drm_mode_fb_cmd2 *mode_cmd)
  122. {
  123. struct rcar_du_device *rcdu = dev->dev_private;
  124. const struct rcar_du_format_info *format;
  125. unsigned int align;
  126. format = rcar_du_format_info(mode_cmd->pixel_format);
  127. if (format == NULL) {
  128. dev_dbg(dev->dev, "unsupported pixel format %08x\n",
  129. mode_cmd->pixel_format);
  130. return ERR_PTR(-EINVAL);
  131. }
  132. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_ALIGN_128B))
  133. align = 128;
  134. else
  135. align = 16 * format->bpp / 8;
  136. if (mode_cmd->pitches[0] & (align - 1) ||
  137. mode_cmd->pitches[0] >= 8192) {
  138. dev_dbg(dev->dev, "invalid pitch value %u\n",
  139. mode_cmd->pitches[0]);
  140. return ERR_PTR(-EINVAL);
  141. }
  142. if (format->planes == 2) {
  143. if (mode_cmd->pitches[1] != mode_cmd->pitches[0]) {
  144. dev_dbg(dev->dev,
  145. "luma and chroma pitches do not match\n");
  146. return ERR_PTR(-EINVAL);
  147. }
  148. }
  149. return drm_fb_cma_create(dev, file_priv, mode_cmd);
  150. }
  151. static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = {
  152. .fb_create = rcar_du_fb_create,
  153. };
  154. int rcar_du_modeset_init(struct rcar_du_device *rcdu)
  155. {
  156. static const unsigned int mmio_offsets[] = {
  157. DU0_REG_OFFSET, DU2_REG_OFFSET
  158. };
  159. struct drm_device *dev = rcdu->ddev;
  160. struct drm_encoder *encoder;
  161. unsigned int num_groups;
  162. unsigned int i;
  163. int ret;
  164. drm_mode_config_init(rcdu->ddev);
  165. rcdu->ddev->mode_config.min_width = 0;
  166. rcdu->ddev->mode_config.min_height = 0;
  167. rcdu->ddev->mode_config.max_width = 4095;
  168. rcdu->ddev->mode_config.max_height = 2047;
  169. rcdu->ddev->mode_config.funcs = &rcar_du_mode_config_funcs;
  170. rcdu->num_crtcs = rcdu->info->num_crtcs;
  171. /* Initialize the groups. */
  172. num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2);
  173. for (i = 0; i < num_groups; ++i) {
  174. struct rcar_du_group *rgrp = &rcdu->groups[i];
  175. rgrp->dev = rcdu;
  176. rgrp->mmio_offset = mmio_offsets[i];
  177. rgrp->index = i;
  178. ret = rcar_du_planes_init(rgrp);
  179. if (ret < 0)
  180. return ret;
  181. }
  182. /* Create the CRTCs. */
  183. for (i = 0; i < rcdu->num_crtcs; ++i) {
  184. struct rcar_du_group *rgrp = &rcdu->groups[i / 2];
  185. ret = rcar_du_crtc_create(rgrp, i);
  186. if (ret < 0)
  187. return ret;
  188. }
  189. /* Initialize the encoders. */
  190. ret = rcar_du_lvdsenc_init(rcdu);
  191. if (ret < 0)
  192. return ret;
  193. for (i = 0; i < rcdu->pdata->num_encoders; ++i) {
  194. const struct rcar_du_encoder_data *pdata =
  195. &rcdu->pdata->encoders[i];
  196. const struct rcar_du_output_routing *route =
  197. &rcdu->info->routes[pdata->output];
  198. if (pdata->type == RCAR_DU_ENCODER_UNUSED)
  199. continue;
  200. if (pdata->output >= RCAR_DU_OUTPUT_MAX ||
  201. route->possible_crtcs == 0) {
  202. dev_warn(rcdu->dev,
  203. "encoder %u references unexisting output %u, skipping\n",
  204. i, pdata->output);
  205. continue;
  206. }
  207. rcar_du_encoder_init(rcdu, pdata->type, pdata->output, pdata);
  208. }
  209. /* Set the possible CRTCs and possible clones. There's always at least
  210. * one way for all encoders to clone each other, set all bits in the
  211. * possible clones field.
  212. */
  213. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  214. struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
  215. const struct rcar_du_output_routing *route =
  216. &rcdu->info->routes[renc->output];
  217. encoder->possible_crtcs = route->possible_crtcs;
  218. encoder->possible_clones = (1 << rcdu->pdata->num_encoders) - 1;
  219. }
  220. /* Now that the CRTCs have been initialized register the planes. */
  221. for (i = 0; i < num_groups; ++i) {
  222. ret = rcar_du_planes_register(&rcdu->groups[i]);
  223. if (ret < 0)
  224. return ret;
  225. }
  226. drm_kms_helper_poll_init(rcdu->ddev);
  227. drm_helper_disable_unused_functions(rcdu->ddev);
  228. return 0;
  229. }