meson_venc_cvbs.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (C) 2016 BayLibre, SAS
  3. * Author: Neil Armstrong <narmstrong@baylibre.com>
  4. * Copyright (C) 2015 Amlogic, Inc. All rights reserved.
  5. * Copyright (C) 2014 Endless Mobile
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * Written by:
  21. * Jasper St. Pierre <jstpierre@mecheye.net>
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/of_graph.h>
  26. #include <drm/drmP.h>
  27. #include <drm/drm_edid.h>
  28. #include <drm/drm_crtc_helper.h>
  29. #include <drm/drm_atomic_helper.h>
  30. #include "meson_venc_cvbs.h"
  31. #include "meson_venc.h"
  32. #include "meson_vclk.h"
  33. #include "meson_registers.h"
  34. /* HHI VDAC Registers */
  35. #define HHI_VDAC_CNTL0 0x2F4 /* 0xbd offset in data sheet */
  36. #define HHI_VDAC_CNTL1 0x2F8 /* 0xbe offset in data sheet */
  37. struct meson_venc_cvbs {
  38. struct drm_encoder encoder;
  39. struct drm_connector connector;
  40. struct meson_drm *priv;
  41. };
  42. #define encoder_to_meson_venc_cvbs(x) \
  43. container_of(x, struct meson_venc_cvbs, encoder)
  44. #define connector_to_meson_venc_cvbs(x) \
  45. container_of(x, struct meson_venc_cvbs, connector)
  46. /* Supported Modes */
  47. struct meson_cvbs_mode meson_cvbs_modes[MESON_CVBS_MODES_COUNT] = {
  48. { /* PAL */
  49. .enci = &meson_cvbs_enci_pal,
  50. .mode = {
  51. DRM_MODE("720x576i", DRM_MODE_TYPE_DRIVER, 13500,
  52. 720, 732, 795, 864, 0, 576, 580, 586, 625, 0,
  53. DRM_MODE_FLAG_INTERLACE),
  54. .vrefresh = 50,
  55. .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3,
  56. },
  57. },
  58. { /* NTSC */
  59. .enci = &meson_cvbs_enci_ntsc,
  60. .mode = {
  61. DRM_MODE("720x480i", DRM_MODE_TYPE_DRIVER, 13500,
  62. 720, 739, 801, 858, 0, 480, 488, 494, 525, 0,
  63. DRM_MODE_FLAG_INTERLACE),
  64. .vrefresh = 60,
  65. .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3,
  66. },
  67. },
  68. };
  69. static const struct meson_cvbs_mode *
  70. meson_cvbs_get_mode(const struct drm_display_mode *req_mode)
  71. {
  72. int i;
  73. for (i = 0; i < MESON_CVBS_MODES_COUNT; ++i) {
  74. struct meson_cvbs_mode *meson_mode = &meson_cvbs_modes[i];
  75. if (drm_mode_match(req_mode, &meson_mode->mode,
  76. DRM_MODE_MATCH_TIMINGS |
  77. DRM_MODE_MATCH_CLOCK |
  78. DRM_MODE_MATCH_FLAGS |
  79. DRM_MODE_MATCH_3D_FLAGS))
  80. return meson_mode;
  81. }
  82. return NULL;
  83. }
  84. /* Connector */
  85. static void meson_cvbs_connector_destroy(struct drm_connector *connector)
  86. {
  87. drm_connector_cleanup(connector);
  88. }
  89. static enum drm_connector_status
  90. meson_cvbs_connector_detect(struct drm_connector *connector, bool force)
  91. {
  92. /* FIXME: Add load-detect or jack-detect if possible */
  93. return connector_status_connected;
  94. }
  95. static int meson_cvbs_connector_get_modes(struct drm_connector *connector)
  96. {
  97. struct drm_device *dev = connector->dev;
  98. struct drm_display_mode *mode;
  99. int i;
  100. for (i = 0; i < MESON_CVBS_MODES_COUNT; ++i) {
  101. struct meson_cvbs_mode *meson_mode = &meson_cvbs_modes[i];
  102. mode = drm_mode_duplicate(dev, &meson_mode->mode);
  103. if (!mode) {
  104. DRM_ERROR("Failed to create a new display mode\n");
  105. return 0;
  106. }
  107. drm_mode_probed_add(connector, mode);
  108. }
  109. return i;
  110. }
  111. static int meson_cvbs_connector_mode_valid(struct drm_connector *connector,
  112. struct drm_display_mode *mode)
  113. {
  114. /* Validate the modes added in get_modes */
  115. return MODE_OK;
  116. }
  117. static const struct drm_connector_funcs meson_cvbs_connector_funcs = {
  118. .detect = meson_cvbs_connector_detect,
  119. .fill_modes = drm_helper_probe_single_connector_modes,
  120. .destroy = meson_cvbs_connector_destroy,
  121. .reset = drm_atomic_helper_connector_reset,
  122. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  123. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  124. };
  125. static const
  126. struct drm_connector_helper_funcs meson_cvbs_connector_helper_funcs = {
  127. .get_modes = meson_cvbs_connector_get_modes,
  128. .mode_valid = meson_cvbs_connector_mode_valid,
  129. };
  130. /* Encoder */
  131. static void meson_venc_cvbs_encoder_destroy(struct drm_encoder *encoder)
  132. {
  133. drm_encoder_cleanup(encoder);
  134. }
  135. static const struct drm_encoder_funcs meson_venc_cvbs_encoder_funcs = {
  136. .destroy = meson_venc_cvbs_encoder_destroy,
  137. };
  138. static int meson_venc_cvbs_encoder_atomic_check(struct drm_encoder *encoder,
  139. struct drm_crtc_state *crtc_state,
  140. struct drm_connector_state *conn_state)
  141. {
  142. if (meson_cvbs_get_mode(&crtc_state->mode))
  143. return 0;
  144. return -EINVAL;
  145. }
  146. static void meson_venc_cvbs_encoder_disable(struct drm_encoder *encoder)
  147. {
  148. struct meson_venc_cvbs *meson_venc_cvbs =
  149. encoder_to_meson_venc_cvbs(encoder);
  150. struct meson_drm *priv = meson_venc_cvbs->priv;
  151. /* Disable CVBS VDAC */
  152. regmap_write(priv->hhi, HHI_VDAC_CNTL0, 0);
  153. regmap_write(priv->hhi, HHI_VDAC_CNTL1, 8);
  154. }
  155. static void meson_venc_cvbs_encoder_enable(struct drm_encoder *encoder)
  156. {
  157. struct meson_venc_cvbs *meson_venc_cvbs =
  158. encoder_to_meson_venc_cvbs(encoder);
  159. struct meson_drm *priv = meson_venc_cvbs->priv;
  160. /* VDAC0 source is not from ATV */
  161. writel_bits_relaxed(BIT(5), 0, priv->io_base + _REG(VENC_VDAC_DACSEL0));
  162. if (meson_vpu_is_compatible(priv, "amlogic,meson-gxbb-vpu"))
  163. regmap_write(priv->hhi, HHI_VDAC_CNTL0, 1);
  164. else if (meson_vpu_is_compatible(priv, "amlogic,meson-gxm-vpu") ||
  165. meson_vpu_is_compatible(priv, "amlogic,meson-gxl-vpu"))
  166. regmap_write(priv->hhi, HHI_VDAC_CNTL0, 0xf0001);
  167. regmap_write(priv->hhi, HHI_VDAC_CNTL1, 0);
  168. }
  169. static void meson_venc_cvbs_encoder_mode_set(struct drm_encoder *encoder,
  170. struct drm_display_mode *mode,
  171. struct drm_display_mode *adjusted_mode)
  172. {
  173. const struct meson_cvbs_mode *meson_mode = meson_cvbs_get_mode(mode);
  174. struct meson_venc_cvbs *meson_venc_cvbs =
  175. encoder_to_meson_venc_cvbs(encoder);
  176. struct meson_drm *priv = meson_venc_cvbs->priv;
  177. if (meson_mode) {
  178. meson_venci_cvbs_mode_set(priv, meson_mode->enci);
  179. /* Setup 27MHz vclk2 for ENCI and VDAC */
  180. meson_vclk_setup(priv, MESON_VCLK_TARGET_CVBS, MESON_VCLK_CVBS,
  181. MESON_VCLK_CVBS, MESON_VCLK_CVBS, true);
  182. }
  183. }
  184. static const struct drm_encoder_helper_funcs
  185. meson_venc_cvbs_encoder_helper_funcs = {
  186. .atomic_check = meson_venc_cvbs_encoder_atomic_check,
  187. .disable = meson_venc_cvbs_encoder_disable,
  188. .enable = meson_venc_cvbs_encoder_enable,
  189. .mode_set = meson_venc_cvbs_encoder_mode_set,
  190. };
  191. static bool meson_venc_cvbs_connector_is_available(struct meson_drm *priv)
  192. {
  193. struct device_node *remote;
  194. remote = of_graph_get_remote_node(priv->dev->of_node, 0, 0);
  195. if (!remote)
  196. return false;
  197. of_node_put(remote);
  198. return true;
  199. }
  200. int meson_venc_cvbs_create(struct meson_drm *priv)
  201. {
  202. struct drm_device *drm = priv->drm;
  203. struct meson_venc_cvbs *meson_venc_cvbs;
  204. struct drm_connector *connector;
  205. struct drm_encoder *encoder;
  206. int ret;
  207. if (!meson_venc_cvbs_connector_is_available(priv)) {
  208. dev_info(drm->dev, "CVBS Output connector not available\n");
  209. return 0;
  210. }
  211. meson_venc_cvbs = devm_kzalloc(priv->dev, sizeof(*meson_venc_cvbs),
  212. GFP_KERNEL);
  213. if (!meson_venc_cvbs)
  214. return -ENOMEM;
  215. meson_venc_cvbs->priv = priv;
  216. encoder = &meson_venc_cvbs->encoder;
  217. connector = &meson_venc_cvbs->connector;
  218. /* Connector */
  219. drm_connector_helper_add(connector,
  220. &meson_cvbs_connector_helper_funcs);
  221. ret = drm_connector_init(drm, connector, &meson_cvbs_connector_funcs,
  222. DRM_MODE_CONNECTOR_Composite);
  223. if (ret) {
  224. dev_err(priv->dev, "Failed to init CVBS connector\n");
  225. return ret;
  226. }
  227. connector->interlace_allowed = 1;
  228. /* Encoder */
  229. drm_encoder_helper_add(encoder, &meson_venc_cvbs_encoder_helper_funcs);
  230. ret = drm_encoder_init(drm, encoder, &meson_venc_cvbs_encoder_funcs,
  231. DRM_MODE_ENCODER_TVDAC, "meson_venc_cvbs");
  232. if (ret) {
  233. dev_err(priv->dev, "Failed to init CVBS encoder\n");
  234. return ret;
  235. }
  236. encoder->possible_crtcs = BIT(0);
  237. drm_connector_attach_encoder(connector, encoder);
  238. return 0;
  239. }