rcar_du_kms.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 void rcar_du_output_poll_changed(struct drm_device *dev)
  152. {
  153. struct rcar_du_device *rcdu = dev->dev_private;
  154. drm_fbdev_cma_hotplug_event(rcdu->fbdev);
  155. }
  156. static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = {
  157. .fb_create = rcar_du_fb_create,
  158. .output_poll_changed = rcar_du_output_poll_changed,
  159. };
  160. int rcar_du_modeset_init(struct rcar_du_device *rcdu)
  161. {
  162. static const unsigned int mmio_offsets[] = {
  163. DU0_REG_OFFSET, DU2_REG_OFFSET
  164. };
  165. struct drm_device *dev = rcdu->ddev;
  166. struct drm_encoder *encoder;
  167. struct drm_fbdev_cma *fbdev;
  168. unsigned int num_groups;
  169. unsigned int i;
  170. int ret;
  171. drm_mode_config_init(dev);
  172. dev->mode_config.min_width = 0;
  173. dev->mode_config.min_height = 0;
  174. dev->mode_config.max_width = 4095;
  175. dev->mode_config.max_height = 2047;
  176. dev->mode_config.funcs = &rcar_du_mode_config_funcs;
  177. rcdu->num_crtcs = rcdu->info->num_crtcs;
  178. /* Initialize the groups. */
  179. num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2);
  180. for (i = 0; i < num_groups; ++i) {
  181. struct rcar_du_group *rgrp = &rcdu->groups[i];
  182. rgrp->dev = rcdu;
  183. rgrp->mmio_offset = mmio_offsets[i];
  184. rgrp->index = i;
  185. ret = rcar_du_planes_init(rgrp);
  186. if (ret < 0)
  187. return ret;
  188. }
  189. /* Create the CRTCs. */
  190. for (i = 0; i < rcdu->num_crtcs; ++i) {
  191. struct rcar_du_group *rgrp = &rcdu->groups[i / 2];
  192. ret = rcar_du_crtc_create(rgrp, i);
  193. if (ret < 0)
  194. return ret;
  195. }
  196. /* Initialize the encoders. */
  197. ret = rcar_du_lvdsenc_init(rcdu);
  198. if (ret < 0)
  199. return ret;
  200. for (i = 0; i < rcdu->pdata->num_encoders; ++i) {
  201. const struct rcar_du_encoder_data *pdata =
  202. &rcdu->pdata->encoders[i];
  203. const struct rcar_du_output_routing *route =
  204. &rcdu->info->routes[pdata->output];
  205. if (pdata->type == RCAR_DU_ENCODER_UNUSED)
  206. continue;
  207. if (pdata->output >= RCAR_DU_OUTPUT_MAX ||
  208. route->possible_crtcs == 0) {
  209. dev_warn(rcdu->dev,
  210. "encoder %u references unexisting output %u, skipping\n",
  211. i, pdata->output);
  212. continue;
  213. }
  214. rcar_du_encoder_init(rcdu, pdata->type, pdata->output, pdata);
  215. }
  216. /* Set the possible CRTCs and possible clones. There's always at least
  217. * one way for all encoders to clone each other, set all bits in the
  218. * possible clones field.
  219. */
  220. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  221. struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
  222. const struct rcar_du_output_routing *route =
  223. &rcdu->info->routes[renc->output];
  224. encoder->possible_crtcs = route->possible_crtcs;
  225. encoder->possible_clones = (1 << rcdu->pdata->num_encoders) - 1;
  226. }
  227. /* Now that the CRTCs have been initialized register the planes. */
  228. for (i = 0; i < num_groups; ++i) {
  229. ret = rcar_du_planes_register(&rcdu->groups[i]);
  230. if (ret < 0)
  231. return ret;
  232. }
  233. drm_kms_helper_poll_init(dev);
  234. drm_helper_disable_unused_functions(dev);
  235. fbdev = drm_fbdev_cma_init(dev, 32, dev->mode_config.num_crtc,
  236. dev->mode_config.num_connector);
  237. if (IS_ERR(fbdev))
  238. return PTR_ERR(fbdev);
  239. #ifndef CONFIG_FRAMEBUFFER_CONSOLE
  240. drm_fbdev_cma_restore_mode(fbdev);
  241. #endif
  242. rcdu->fbdev = fbdev;
  243. return 0;
  244. }