meson_venc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. struct meson_cvbs_enci_mode meson_cvbs_enci_pal = {
  39. .mode_tag = MESON_VENC_MODE_CVBS_PAL,
  40. .hso_begin = 3,
  41. .hso_end = 129,
  42. .vso_even = 3,
  43. .vso_odd = 260,
  44. .macv_max_amp = 7,
  45. .video_prog_mode = 0xff,
  46. .video_mode = 0x13,
  47. .sch_adjust = 0x28,
  48. .yc_delay = 0x343,
  49. .pixel_start = 251,
  50. .pixel_end = 1691,
  51. .top_field_line_start = 22,
  52. .top_field_line_end = 310,
  53. .bottom_field_line_start = 23,
  54. .bottom_field_line_end = 311,
  55. .video_saturation = 9,
  56. .video_contrast = 0,
  57. .video_brightness = 0,
  58. .video_hue = 0,
  59. .analog_sync_adj = 0x8080,
  60. };
  61. struct meson_cvbs_enci_mode meson_cvbs_enci_ntsc = {
  62. .mode_tag = MESON_VENC_MODE_CVBS_NTSC,
  63. .hso_begin = 5,
  64. .hso_end = 129,
  65. .vso_even = 3,
  66. .vso_odd = 260,
  67. .macv_max_amp = 0xb,
  68. .video_prog_mode = 0xf0,
  69. .video_mode = 0x8,
  70. .sch_adjust = 0x20,
  71. .yc_delay = 0x333,
  72. .pixel_start = 227,
  73. .pixel_end = 1667,
  74. .top_field_line_start = 18,
  75. .top_field_line_end = 258,
  76. .bottom_field_line_start = 19,
  77. .bottom_field_line_end = 259,
  78. .video_saturation = 18,
  79. .video_contrast = 3,
  80. .video_brightness = 0,
  81. .video_hue = 0,
  82. .analog_sync_adj = 0x9c00,
  83. };
  84. void meson_venci_cvbs_mode_set(struct meson_drm *priv,
  85. struct meson_cvbs_enci_mode *mode)
  86. {
  87. if (mode->mode_tag == priv->venc.current_mode)
  88. return;
  89. /* CVBS Filter settings */
  90. writel_relaxed(0x12, priv->io_base + _REG(ENCI_CFILT_CTRL));
  91. writel_relaxed(0x12, priv->io_base + _REG(ENCI_CFILT_CTRL2));
  92. /* Digital Video Select : Interlace, clk27 clk, external */
  93. writel_relaxed(0, priv->io_base + _REG(VENC_DVI_SETTING));
  94. /* Reset Video Mode */
  95. writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_MODE));
  96. writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_MODE_ADV));
  97. /* Horizontal sync signal output */
  98. writel_relaxed(mode->hso_begin,
  99. priv->io_base + _REG(ENCI_SYNC_HSO_BEGIN));
  100. writel_relaxed(mode->hso_end,
  101. priv->io_base + _REG(ENCI_SYNC_HSO_END));
  102. /* Vertical Sync lines */
  103. writel_relaxed(mode->vso_even,
  104. priv->io_base + _REG(ENCI_SYNC_VSO_EVNLN));
  105. writel_relaxed(mode->vso_odd,
  106. priv->io_base + _REG(ENCI_SYNC_VSO_ODDLN));
  107. /* Macrovision max amplitude change */
  108. writel_relaxed(0x8100 + mode->macv_max_amp,
  109. priv->io_base + _REG(ENCI_MACV_MAX_AMP));
  110. /* Video mode */
  111. writel_relaxed(mode->video_prog_mode,
  112. priv->io_base + _REG(VENC_VIDEO_PROG_MODE));
  113. writel_relaxed(mode->video_mode,
  114. priv->io_base + _REG(ENCI_VIDEO_MODE));
  115. /* Advanced Video Mode :
  116. * Demux shifting 0x2
  117. * Blank line end at line17/22
  118. * High bandwidth Luma Filter
  119. * Low bandwidth Chroma Filter
  120. * Bypass luma low pass filter
  121. * No macrovision on CSYNC
  122. */
  123. writel_relaxed(0x26, priv->io_base + _REG(ENCI_VIDEO_MODE_ADV));
  124. writel(mode->sch_adjust, priv->io_base + _REG(ENCI_VIDEO_SCH));
  125. /* Sync mode : MASTER Master mode, free run, send HSO/VSO out */
  126. writel_relaxed(0x07, priv->io_base + _REG(ENCI_SYNC_MODE));
  127. /* 0x3 Y, C, and Component Y delay */
  128. writel_relaxed(mode->yc_delay, priv->io_base + _REG(ENCI_YC_DELAY));
  129. /* Timings */
  130. writel_relaxed(mode->pixel_start,
  131. priv->io_base + _REG(ENCI_VFIFO2VD_PIXEL_START));
  132. writel_relaxed(mode->pixel_end,
  133. priv->io_base + _REG(ENCI_VFIFO2VD_PIXEL_END));
  134. writel_relaxed(mode->top_field_line_start,
  135. priv->io_base + _REG(ENCI_VFIFO2VD_LINE_TOP_START));
  136. writel_relaxed(mode->top_field_line_end,
  137. priv->io_base + _REG(ENCI_VFIFO2VD_LINE_TOP_END));
  138. writel_relaxed(mode->bottom_field_line_start,
  139. priv->io_base + _REG(ENCI_VFIFO2VD_LINE_BOT_START));
  140. writel_relaxed(mode->bottom_field_line_end,
  141. priv->io_base + _REG(ENCI_VFIFO2VD_LINE_BOT_END));
  142. /* Internal Venc, Internal VIU Sync, Internal Vencoder */
  143. writel_relaxed(0, priv->io_base + _REG(VENC_SYNC_ROUTE));
  144. /* UNreset Interlaced TV Encoder */
  145. writel_relaxed(0, priv->io_base + _REG(ENCI_DBG_PX_RST));
  146. /* Enable Vfifo2vd, Y_Cb_Y_Cr select */
  147. writel_relaxed(0x4e01, priv->io_base + _REG(ENCI_VFIFO2VD_CTL));
  148. /* Power UP Dacs */
  149. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_SETTING));
  150. /* Video Upsampling */
  151. writel_relaxed(0x0061, priv->io_base + _REG(VENC_UPSAMPLE_CTRL0));
  152. writel_relaxed(0x4061, priv->io_base + _REG(VENC_UPSAMPLE_CTRL1));
  153. writel_relaxed(0x5061, priv->io_base + _REG(VENC_UPSAMPLE_CTRL2));
  154. /* Select Interlace Y DACs */
  155. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_DACSEL0));
  156. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_DACSEL1));
  157. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_DACSEL2));
  158. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_DACSEL3));
  159. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_DACSEL4));
  160. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_DACSEL5));
  161. /* Select ENCI for VIU */
  162. meson_vpp_setup_mux(priv, MESON_VIU_VPP_MUX_ENCI);
  163. /* Enable ENCI FIFO */
  164. writel_relaxed(0x2000, priv->io_base + _REG(VENC_VDAC_FIFO_CTRL));
  165. /* Select ENCI DACs 0, 1, 4, and 5 */
  166. writel_relaxed(0x11, priv->io_base + _REG(ENCI_DACSEL_0));
  167. writel_relaxed(0x11, priv->io_base + _REG(ENCI_DACSEL_1));
  168. /* Interlace video enable */
  169. writel_relaxed(1, priv->io_base + _REG(ENCI_VIDEO_EN));
  170. /* Configure Video Saturation / Contrast / Brightness / Hue */
  171. writel_relaxed(mode->video_saturation,
  172. priv->io_base + _REG(ENCI_VIDEO_SAT));
  173. writel_relaxed(mode->video_contrast,
  174. priv->io_base + _REG(ENCI_VIDEO_CONT));
  175. writel_relaxed(mode->video_brightness,
  176. priv->io_base + _REG(ENCI_VIDEO_BRIGHT));
  177. writel_relaxed(mode->video_hue,
  178. priv->io_base + _REG(ENCI_VIDEO_HUE));
  179. /* Enable DAC0 Filter */
  180. writel_relaxed(0x1, priv->io_base + _REG(VENC_VDAC_DAC0_FILT_CTRL0));
  181. writel_relaxed(0xfc48, priv->io_base + _REG(VENC_VDAC_DAC0_FILT_CTRL1));
  182. /* 0 in Macrovision register 0 */
  183. writel_relaxed(0, priv->io_base + _REG(ENCI_MACV_N0));
  184. /* Analog Synchronization and color burst value adjust */
  185. writel_relaxed(mode->analog_sync_adj,
  186. priv->io_base + _REG(ENCI_SYNC_ADJ));
  187. /* Setup 27MHz vclk2 for ENCI and VDAC */
  188. meson_vclk_setup(priv, MESON_VCLK_TARGET_CVBS, MESON_VCLK_CVBS);
  189. priv->venc.current_mode = mode->mode_tag;
  190. }
  191. /* Returns the current ENCI field polarity */
  192. unsigned int meson_venci_get_field(struct meson_drm *priv)
  193. {
  194. return readl_relaxed(priv->io_base + _REG(ENCI_INFO_READ)) & BIT(29);
  195. }
  196. void meson_venc_enable_vsync(struct meson_drm *priv)
  197. {
  198. writel_relaxed(2, priv->io_base + _REG(VENC_INTCTRL));
  199. }
  200. void meson_venc_disable_vsync(struct meson_drm *priv)
  201. {
  202. writel_relaxed(0, priv->io_base + _REG(VENC_INTCTRL));
  203. }
  204. void meson_venc_init(struct meson_drm *priv)
  205. {
  206. /* Disable all encoders */
  207. writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_EN));
  208. writel_relaxed(0, priv->io_base + _REG(ENCP_VIDEO_EN));
  209. writel_relaxed(0, priv->io_base + _REG(ENCL_VIDEO_EN));
  210. /* Disable VSync IRQ */
  211. meson_venc_disable_vsync(priv);
  212. priv->venc.current_mode = MESON_VENC_MODE_NONE;
  213. }