meson_venc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (C) 2016 BayLibre, SAS
  3. * Author: Neil Armstrong <narmstrong@baylibre.com>
  4. * Copyright (C) 2015 Amlogic, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <drm/drmP.h>
  22. #include "meson_drv.h"
  23. #include "meson_venc.h"
  24. #include "meson_vpp.h"
  25. #include "meson_vclk.h"
  26. #include "meson_registers.h"
  27. /*
  28. * VENC Handle the pixels encoding to the output formats.
  29. * We handle the following encodings :
  30. * - CVBS Encoding via the ENCI encoder and VDAC digital to analog converter
  31. *
  32. * What is missing :
  33. * - TMDS/HDMI Encoding via ENCI_DIV and ENCP
  34. * - Setup of more clock rates for HDMI modes
  35. * - LCD Panel encoding via ENCL
  36. * - TV Panel encoding via ENCT
  37. */
  38. /* HHI Registers */
  39. #define HHI_VDAC_CNTL0 0x2F4 /* 0xbd offset in data sheet */
  40. #define HHI_VDAC_CNTL1 0x2F8 /* 0xbe offset in data sheet */
  41. #define HHI_HDMI_PHY_CNTL0 0x3a0 /* 0xe8 offset in data sheet */
  42. struct meson_cvbs_enci_mode meson_cvbs_enci_pal = {
  43. .mode_tag = MESON_VENC_MODE_CVBS_PAL,
  44. .hso_begin = 3,
  45. .hso_end = 129,
  46. .vso_even = 3,
  47. .vso_odd = 260,
  48. .macv_max_amp = 7,
  49. .video_prog_mode = 0xff,
  50. .video_mode = 0x13,
  51. .sch_adjust = 0x28,
  52. .yc_delay = 0x343,
  53. .pixel_start = 251,
  54. .pixel_end = 1691,
  55. .top_field_line_start = 22,
  56. .top_field_line_end = 310,
  57. .bottom_field_line_start = 23,
  58. .bottom_field_line_end = 311,
  59. .video_saturation = 9,
  60. .video_contrast = 0,
  61. .video_brightness = 0,
  62. .video_hue = 0,
  63. .analog_sync_adj = 0x8080,
  64. };
  65. struct meson_cvbs_enci_mode meson_cvbs_enci_ntsc = {
  66. .mode_tag = MESON_VENC_MODE_CVBS_NTSC,
  67. .hso_begin = 5,
  68. .hso_end = 129,
  69. .vso_even = 3,
  70. .vso_odd = 260,
  71. .macv_max_amp = 0xb,
  72. .video_prog_mode = 0xf0,
  73. .video_mode = 0x8,
  74. .sch_adjust = 0x20,
  75. .yc_delay = 0x333,
  76. .pixel_start = 227,
  77. .pixel_end = 1667,
  78. .top_field_line_start = 18,
  79. .top_field_line_end = 258,
  80. .bottom_field_line_start = 19,
  81. .bottom_field_line_end = 259,
  82. .video_saturation = 18,
  83. .video_contrast = 3,
  84. .video_brightness = 0,
  85. .video_hue = 0,
  86. .analog_sync_adj = 0x9c00,
  87. };
  88. void meson_venci_cvbs_mode_set(struct meson_drm *priv,
  89. struct meson_cvbs_enci_mode *mode)
  90. {
  91. if (mode->mode_tag == priv->venc.current_mode)
  92. return;
  93. /* CVBS Filter settings */
  94. writel_relaxed(0x12, priv->io_base + _REG(ENCI_CFILT_CTRL));
  95. writel_relaxed(0x12, priv->io_base + _REG(ENCI_CFILT_CTRL2));
  96. /* Digital Video Select : Interlace, clk27 clk, external */
  97. writel_relaxed(0, priv->io_base + _REG(VENC_DVI_SETTING));
  98. /* Reset Video Mode */
  99. writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_MODE));
  100. writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_MODE_ADV));
  101. /* Horizontal sync signal output */
  102. writel_relaxed(mode->hso_begin,
  103. priv->io_base + _REG(ENCI_SYNC_HSO_BEGIN));
  104. writel_relaxed(mode->hso_end,
  105. priv->io_base + _REG(ENCI_SYNC_HSO_END));
  106. /* Vertical Sync lines */
  107. writel_relaxed(mode->vso_even,
  108. priv->io_base + _REG(ENCI_SYNC_VSO_EVNLN));
  109. writel_relaxed(mode->vso_odd,
  110. priv->io_base + _REG(ENCI_SYNC_VSO_ODDLN));
  111. /* Macrovision max amplitude change */
  112. writel_relaxed(0x8100 + mode->macv_max_amp,
  113. priv->io_base + _REG(ENCI_MACV_MAX_AMP));
  114. /* Video mode */
  115. writel_relaxed(mode->video_prog_mode,
  116. priv->io_base + _REG(VENC_VIDEO_PROG_MODE));
  117. writel_relaxed(mode->video_mode,
  118. priv->io_base + _REG(ENCI_VIDEO_MODE));
  119. /* Advanced Video Mode :
  120. * Demux shifting 0x2
  121. * Blank line end at line17/22
  122. * High bandwidth Luma Filter
  123. * Low bandwidth Chroma Filter
  124. * Bypass luma low pass filter
  125. * No macrovision on CSYNC
  126. */
  127. writel_relaxed(0x26, priv->io_base + _REG(ENCI_VIDEO_MODE_ADV));
  128. writel(mode->sch_adjust, priv->io_base + _REG(ENCI_VIDEO_SCH));
  129. /* Sync mode : MASTER Master mode, free run, send HSO/VSO out */
  130. writel_relaxed(0x07, priv->io_base + _REG(ENCI_SYNC_MODE));
  131. /* 0x3 Y, C, and Component Y delay */
  132. writel_relaxed(mode->yc_delay, priv->io_base + _REG(ENCI_YC_DELAY));
  133. /* Timings */
  134. writel_relaxed(mode->pixel_start,
  135. priv->io_base + _REG(ENCI_VFIFO2VD_PIXEL_START));
  136. writel_relaxed(mode->pixel_end,
  137. priv->io_base + _REG(ENCI_VFIFO2VD_PIXEL_END));
  138. writel_relaxed(mode->top_field_line_start,
  139. priv->io_base + _REG(ENCI_VFIFO2VD_LINE_TOP_START));
  140. writel_relaxed(mode->top_field_line_end,
  141. priv->io_base + _REG(ENCI_VFIFO2VD_LINE_TOP_END));
  142. writel_relaxed(mode->bottom_field_line_start,
  143. priv->io_base + _REG(ENCI_VFIFO2VD_LINE_BOT_START));
  144. writel_relaxed(mode->bottom_field_line_end,
  145. priv->io_base + _REG(ENCI_VFIFO2VD_LINE_BOT_END));
  146. /* Internal Venc, Internal VIU Sync, Internal Vencoder */
  147. writel_relaxed(0, priv->io_base + _REG(VENC_SYNC_ROUTE));
  148. /* UNreset Interlaced TV Encoder */
  149. writel_relaxed(0, priv->io_base + _REG(ENCI_DBG_PX_RST));
  150. /* Enable Vfifo2vd, Y_Cb_Y_Cr select */
  151. writel_relaxed(0x4e01, priv->io_base + _REG(ENCI_VFIFO2VD_CTL));
  152. /* Power UP Dacs */
  153. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_SETTING));
  154. /* Video Upsampling */
  155. writel_relaxed(0x0061, priv->io_base + _REG(VENC_UPSAMPLE_CTRL0));
  156. writel_relaxed(0x4061, priv->io_base + _REG(VENC_UPSAMPLE_CTRL1));
  157. writel_relaxed(0x5061, priv->io_base + _REG(VENC_UPSAMPLE_CTRL2));
  158. /* Select Interlace Y DACs */
  159. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_DACSEL0));
  160. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_DACSEL1));
  161. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_DACSEL2));
  162. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_DACSEL3));
  163. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_DACSEL4));
  164. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_DACSEL5));
  165. /* Select ENCI for VIU */
  166. meson_vpp_setup_mux(priv, MESON_VIU_VPP_MUX_ENCI);
  167. /* Enable ENCI FIFO */
  168. writel_relaxed(0x2000, priv->io_base + _REG(VENC_VDAC_FIFO_CTRL));
  169. /* Select ENCI DACs 0, 1, 4, and 5 */
  170. writel_relaxed(0x11, priv->io_base + _REG(ENCI_DACSEL_0));
  171. writel_relaxed(0x11, priv->io_base + _REG(ENCI_DACSEL_1));
  172. /* Interlace video enable */
  173. writel_relaxed(1, priv->io_base + _REG(ENCI_VIDEO_EN));
  174. /* Configure Video Saturation / Contrast / Brightness / Hue */
  175. writel_relaxed(mode->video_saturation,
  176. priv->io_base + _REG(ENCI_VIDEO_SAT));
  177. writel_relaxed(mode->video_contrast,
  178. priv->io_base + _REG(ENCI_VIDEO_CONT));
  179. writel_relaxed(mode->video_brightness,
  180. priv->io_base + _REG(ENCI_VIDEO_BRIGHT));
  181. writel_relaxed(mode->video_hue,
  182. priv->io_base + _REG(ENCI_VIDEO_HUE));
  183. /* Enable DAC0 Filter */
  184. writel_relaxed(0x1, priv->io_base + _REG(VENC_VDAC_DAC0_FILT_CTRL0));
  185. writel_relaxed(0xfc48, priv->io_base + _REG(VENC_VDAC_DAC0_FILT_CTRL1));
  186. /* 0 in Macrovision register 0 */
  187. writel_relaxed(0, priv->io_base + _REG(ENCI_MACV_N0));
  188. /* Analog Synchronization and color burst value adjust */
  189. writel_relaxed(mode->analog_sync_adj,
  190. priv->io_base + _REG(ENCI_SYNC_ADJ));
  191. /* Setup 27MHz vclk2 for ENCI and VDAC */
  192. meson_vclk_setup(priv, MESON_VCLK_TARGET_CVBS, MESON_VCLK_CVBS);
  193. priv->venc.current_mode = mode->mode_tag;
  194. }
  195. /* Returns the current ENCI field polarity */
  196. unsigned int meson_venci_get_field(struct meson_drm *priv)
  197. {
  198. return readl_relaxed(priv->io_base + _REG(ENCI_INFO_READ)) & BIT(29);
  199. }
  200. void meson_venc_enable_vsync(struct meson_drm *priv)
  201. {
  202. writel_relaxed(2, priv->io_base + _REG(VENC_INTCTRL));
  203. }
  204. void meson_venc_disable_vsync(struct meson_drm *priv)
  205. {
  206. writel_relaxed(0, priv->io_base + _REG(VENC_INTCTRL));
  207. }
  208. void meson_venc_init(struct meson_drm *priv)
  209. {
  210. /* Disable CVBS VDAC */
  211. regmap_write(priv->hhi, HHI_VDAC_CNTL0, 0);
  212. regmap_write(priv->hhi, HHI_VDAC_CNTL1, 8);
  213. /* Power Down Dacs */
  214. writel_relaxed(0xff, priv->io_base + _REG(VENC_VDAC_SETTING));
  215. /* Disable HDMI PHY */
  216. regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0);
  217. /* Disable HDMI */
  218. writel_bits_relaxed(0x3, 0,
  219. priv->io_base + _REG(VPU_HDMI_SETTING));
  220. /* Disable all encoders */
  221. writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_EN));
  222. writel_relaxed(0, priv->io_base + _REG(ENCP_VIDEO_EN));
  223. writel_relaxed(0, priv->io_base + _REG(ENCL_VIDEO_EN));
  224. /* Disable VSync IRQ */
  225. meson_venc_disable_vsync(priv);
  226. priv->venc.current_mode = MESON_VENC_MODE_NONE;
  227. }