rcar_du_kms.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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_needs(rcdu, RCAR_DU_QUIRK_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 max_pitch;
  126. unsigned int align;
  127. unsigned int bpp;
  128. format = rcar_du_format_info(mode_cmd->pixel_format);
  129. if (format == NULL) {
  130. dev_dbg(dev->dev, "unsupported pixel format %08x\n",
  131. mode_cmd->pixel_format);
  132. return ERR_PTR(-EINVAL);
  133. }
  134. /*
  135. * The pitch and alignment constraints are expressed in pixels on the
  136. * hardware side and in bytes in the DRM API.
  137. */
  138. bpp = format->planes == 2 ? 1 : format->bpp / 8;
  139. max_pitch = 4096 * bpp;
  140. if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B))
  141. align = 128;
  142. else
  143. align = 16 * bpp;
  144. if (mode_cmd->pitches[0] & (align - 1) ||
  145. mode_cmd->pitches[0] >= max_pitch) {
  146. dev_dbg(dev->dev, "invalid pitch value %u\n",
  147. mode_cmd->pitches[0]);
  148. return ERR_PTR(-EINVAL);
  149. }
  150. if (format->planes == 2) {
  151. if (mode_cmd->pitches[1] != mode_cmd->pitches[0]) {
  152. dev_dbg(dev->dev,
  153. "luma and chroma pitches do not match\n");
  154. return ERR_PTR(-EINVAL);
  155. }
  156. }
  157. return drm_fb_cma_create(dev, file_priv, mode_cmd);
  158. }
  159. static void rcar_du_output_poll_changed(struct drm_device *dev)
  160. {
  161. struct rcar_du_device *rcdu = dev->dev_private;
  162. drm_fbdev_cma_hotplug_event(rcdu->fbdev);
  163. }
  164. static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = {
  165. .fb_create = rcar_du_fb_create,
  166. .output_poll_changed = rcar_du_output_poll_changed,
  167. };
  168. int rcar_du_modeset_init(struct rcar_du_device *rcdu)
  169. {
  170. static const unsigned int mmio_offsets[] = {
  171. DU0_REG_OFFSET, DU2_REG_OFFSET
  172. };
  173. struct drm_device *dev = rcdu->ddev;
  174. struct drm_encoder *encoder;
  175. struct drm_fbdev_cma *fbdev;
  176. unsigned int num_groups;
  177. unsigned int i;
  178. int ret;
  179. drm_mode_config_init(dev);
  180. dev->mode_config.min_width = 0;
  181. dev->mode_config.min_height = 0;
  182. dev->mode_config.max_width = 4095;
  183. dev->mode_config.max_height = 2047;
  184. dev->mode_config.funcs = &rcar_du_mode_config_funcs;
  185. rcdu->num_crtcs = rcdu->info->num_crtcs;
  186. /* Initialize the groups. */
  187. num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2);
  188. for (i = 0; i < num_groups; ++i) {
  189. struct rcar_du_group *rgrp = &rcdu->groups[i];
  190. rgrp->dev = rcdu;
  191. rgrp->mmio_offset = mmio_offsets[i];
  192. rgrp->index = i;
  193. ret = rcar_du_planes_init(rgrp);
  194. if (ret < 0)
  195. return ret;
  196. }
  197. /* Create the CRTCs. */
  198. for (i = 0; i < rcdu->num_crtcs; ++i) {
  199. struct rcar_du_group *rgrp = &rcdu->groups[i / 2];
  200. ret = rcar_du_crtc_create(rgrp, i);
  201. if (ret < 0)
  202. return ret;
  203. }
  204. /* Initialize the encoders. */
  205. ret = rcar_du_lvdsenc_init(rcdu);
  206. if (ret < 0)
  207. return ret;
  208. for (i = 0; i < rcdu->pdata->num_encoders; ++i) {
  209. const struct rcar_du_encoder_data *pdata =
  210. &rcdu->pdata->encoders[i];
  211. const struct rcar_du_output_routing *route =
  212. &rcdu->info->routes[pdata->output];
  213. if (pdata->type == RCAR_DU_ENCODER_UNUSED)
  214. continue;
  215. if (pdata->output >= RCAR_DU_OUTPUT_MAX ||
  216. route->possible_crtcs == 0) {
  217. dev_warn(rcdu->dev,
  218. "encoder %u references unexisting output %u, skipping\n",
  219. i, pdata->output);
  220. continue;
  221. }
  222. ret = rcar_du_encoder_init(rcdu, pdata->type, pdata->output,
  223. pdata);
  224. if (ret < 0)
  225. return ret;
  226. }
  227. /* Set the possible CRTCs and possible clones. There's always at least
  228. * one way for all encoders to clone each other, set all bits in the
  229. * possible clones field.
  230. */
  231. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  232. struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
  233. const struct rcar_du_output_routing *route =
  234. &rcdu->info->routes[renc->output];
  235. encoder->possible_crtcs = route->possible_crtcs;
  236. encoder->possible_clones = (1 << rcdu->pdata->num_encoders) - 1;
  237. }
  238. /* Now that the CRTCs have been initialized register the planes. */
  239. for (i = 0; i < num_groups; ++i) {
  240. ret = rcar_du_planes_register(&rcdu->groups[i]);
  241. if (ret < 0)
  242. return ret;
  243. }
  244. drm_kms_helper_poll_init(dev);
  245. drm_helper_disable_unused_functions(dev);
  246. fbdev = drm_fbdev_cma_init(dev, 32, dev->mode_config.num_crtc,
  247. dev->mode_config.num_connector);
  248. if (IS_ERR(fbdev))
  249. return PTR_ERR(fbdev);
  250. #ifndef CONFIG_FRAMEBUFFER_CONSOLE
  251. drm_fbdev_cma_restore_mode(fbdev);
  252. #endif
  253. rcdu->fbdev = fbdev;
  254. return 0;
  255. }