rcar_du_kms.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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_kms.h"
  21. #include "rcar_du_lvds.h"
  22. #include "rcar_du_regs.h"
  23. #include "rcar_du_vga.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. * Common connector and encoder functions
  102. */
  103. struct drm_encoder *
  104. rcar_du_connector_best_encoder(struct drm_connector *connector)
  105. {
  106. struct rcar_du_connector *rcon = to_rcar_connector(connector);
  107. return &rcon->encoder->encoder;
  108. }
  109. void rcar_du_encoder_mode_prepare(struct drm_encoder *encoder)
  110. {
  111. }
  112. void rcar_du_encoder_mode_set(struct drm_encoder *encoder,
  113. struct drm_display_mode *mode,
  114. struct drm_display_mode *adjusted_mode)
  115. {
  116. struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
  117. rcar_du_crtc_route_output(encoder->crtc, renc->output);
  118. }
  119. void rcar_du_encoder_mode_commit(struct drm_encoder *encoder)
  120. {
  121. }
  122. /* -----------------------------------------------------------------------------
  123. * Frame buffer
  124. */
  125. static struct drm_framebuffer *
  126. rcar_du_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  127. struct drm_mode_fb_cmd2 *mode_cmd)
  128. {
  129. const struct rcar_du_format_info *format;
  130. format = rcar_du_format_info(mode_cmd->pixel_format);
  131. if (format == NULL) {
  132. dev_dbg(dev->dev, "unsupported pixel format %08x\n",
  133. mode_cmd->pixel_format);
  134. return ERR_PTR(-EINVAL);
  135. }
  136. if (mode_cmd->pitches[0] & 15 || 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. struct drm_device *dev = rcdu->ddev;
  156. struct drm_encoder *encoder;
  157. unsigned int i;
  158. int ret;
  159. drm_mode_config_init(rcdu->ddev);
  160. rcdu->ddev->mode_config.min_width = 0;
  161. rcdu->ddev->mode_config.min_height = 0;
  162. rcdu->ddev->mode_config.max_width = 4095;
  163. rcdu->ddev->mode_config.max_height = 2047;
  164. rcdu->ddev->mode_config.funcs = &rcar_du_mode_config_funcs;
  165. ret = rcar_du_plane_init(rcdu);
  166. if (ret < 0)
  167. return ret;
  168. for (i = 0; i < ARRAY_SIZE(rcdu->crtcs); ++i)
  169. rcar_du_crtc_create(rcdu, i);
  170. rcdu->used_crtcs = 0;
  171. rcdu->num_crtcs = i;
  172. for (i = 0; i < rcdu->pdata->num_encoders; ++i) {
  173. const struct rcar_du_encoder_data *pdata =
  174. &rcdu->pdata->encoders[i];
  175. if (pdata->output >= ARRAY_SIZE(rcdu->crtcs)) {
  176. dev_warn(rcdu->dev,
  177. "encoder %u references unexisting output %u, skipping\n",
  178. i, pdata->output);
  179. continue;
  180. }
  181. switch (pdata->encoder) {
  182. case RCAR_DU_ENCODER_VGA:
  183. rcar_du_vga_init(rcdu, &pdata->u.vga, pdata->output);
  184. break;
  185. case RCAR_DU_ENCODER_LVDS:
  186. rcar_du_lvds_init(rcdu, &pdata->u.lvds, pdata->output);
  187. break;
  188. default:
  189. break;
  190. }
  191. }
  192. /* Set the possible CRTCs and possible clones. All encoders can be
  193. * driven by the CRTC associated with the output they're connected to,
  194. * as well as by CRTC 0.
  195. */
  196. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  197. struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
  198. encoder->possible_crtcs = (1 << 0) | (1 << renc->output);
  199. encoder->possible_clones = 1 << 0;
  200. }
  201. ret = rcar_du_plane_register(rcdu);
  202. if (ret < 0)
  203. return ret;
  204. drm_kms_helper_poll_init(rcdu->ddev);
  205. drm_helper_disable_unused_functions(rcdu->ddev);
  206. return 0;
  207. }