meson_viu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <drm/drmP.h>
  23. #include "meson_drv.h"
  24. #include "meson_viu.h"
  25. #include "meson_vpp.h"
  26. #include "meson_venc.h"
  27. #include "meson_canvas.h"
  28. #include "meson_registers.h"
  29. /**
  30. * DOC: Video Input Unit
  31. *
  32. * VIU Handles the Pixel scanout and the basic Colorspace conversions
  33. * We handle the following features :
  34. *
  35. * - OSD1 RGB565/RGB888/xRGB8888 scanout
  36. * - RGB conversion to x/cb/cr
  37. * - Progressive or Interlace buffer scanout
  38. * - OSD1 Commit on Vsync
  39. * - HDR OSD matrix for GXL/GXM
  40. *
  41. * What is missing :
  42. *
  43. * - BGR888/xBGR8888/BGRx8888/BGRx8888 modes
  44. * - YUV4:2:2 Y0CbY1Cr scanout
  45. * - Conversion to YUV 4:4:4 from 4:2:2 input
  46. * - Colorkey Alpha matching
  47. * - Big endian scanout
  48. * - X/Y reverse scanout
  49. * - Global alpha setup
  50. * - OSD2 support, would need interlace switching on vsync
  51. * - OSD1 full scaling to support TV overscan
  52. */
  53. /* OSD csc defines */
  54. enum viu_matrix_sel_e {
  55. VIU_MATRIX_OSD_EOTF = 0,
  56. VIU_MATRIX_OSD,
  57. };
  58. enum viu_lut_sel_e {
  59. VIU_LUT_OSD_EOTF = 0,
  60. VIU_LUT_OSD_OETF,
  61. };
  62. #define COEFF_NORM(a) ((int)((((a) * 2048.0) + 1) / 2))
  63. #define MATRIX_5X3_COEF_SIZE 24
  64. #define EOTF_COEFF_NORM(a) ((int)((((a) * 4096.0) + 1) / 2))
  65. #define EOTF_COEFF_SIZE 10
  66. #define EOTF_COEFF_RIGHTSHIFT 1
  67. static int RGB709_to_YUV709l_coeff[MATRIX_5X3_COEF_SIZE] = {
  68. 0, 0, 0, /* pre offset */
  69. COEFF_NORM(0.181873), COEFF_NORM(0.611831), COEFF_NORM(0.061765),
  70. COEFF_NORM(-0.100251), COEFF_NORM(-0.337249), COEFF_NORM(0.437500),
  71. COEFF_NORM(0.437500), COEFF_NORM(-0.397384), COEFF_NORM(-0.040116),
  72. 0, 0, 0, /* 10'/11'/12' */
  73. 0, 0, 0, /* 20'/21'/22' */
  74. 64, 512, 512, /* offset */
  75. 0, 0, 0 /* mode, right_shift, clip_en */
  76. };
  77. /* eotf matrix: bypass */
  78. static int eotf_bypass_coeff[EOTF_COEFF_SIZE] = {
  79. EOTF_COEFF_NORM(1.0), EOTF_COEFF_NORM(0.0), EOTF_COEFF_NORM(0.0),
  80. EOTF_COEFF_NORM(0.0), EOTF_COEFF_NORM(1.0), EOTF_COEFF_NORM(0.0),
  81. EOTF_COEFF_NORM(0.0), EOTF_COEFF_NORM(0.0), EOTF_COEFF_NORM(1.0),
  82. EOTF_COEFF_RIGHTSHIFT /* right shift */
  83. };
  84. void meson_viu_set_osd_matrix(struct meson_drm *priv,
  85. enum viu_matrix_sel_e m_select,
  86. int *m, bool csc_on)
  87. {
  88. if (m_select == VIU_MATRIX_OSD) {
  89. /* osd matrix, VIU_MATRIX_0 */
  90. writel(((m[0] & 0xfff) << 16) | (m[1] & 0xfff),
  91. priv->io_base + _REG(VIU_OSD1_MATRIX_PRE_OFFSET0_1));
  92. writel(m[2] & 0xfff,
  93. priv->io_base + _REG(VIU_OSD1_MATRIX_PRE_OFFSET2));
  94. writel(((m[3] & 0x1fff) << 16) | (m[4] & 0x1fff),
  95. priv->io_base + _REG(VIU_OSD1_MATRIX_COEF00_01));
  96. writel(((m[5] & 0x1fff) << 16) | (m[6] & 0x1fff),
  97. priv->io_base + _REG(VIU_OSD1_MATRIX_COEF02_10));
  98. writel(((m[7] & 0x1fff) << 16) | (m[8] & 0x1fff),
  99. priv->io_base + _REG(VIU_OSD1_MATRIX_COEF11_12));
  100. writel(((m[9] & 0x1fff) << 16) | (m[10] & 0x1fff),
  101. priv->io_base + _REG(VIU_OSD1_MATRIX_COEF20_21));
  102. if (m[21]) {
  103. writel(((m[11] & 0x1fff) << 16) | (m[12] & 0x1fff),
  104. priv->io_base +
  105. _REG(VIU_OSD1_MATRIX_COEF22_30));
  106. writel(((m[13] & 0x1fff) << 16) | (m[14] & 0x1fff),
  107. priv->io_base +
  108. _REG(VIU_OSD1_MATRIX_COEF31_32));
  109. writel(((m[15] & 0x1fff) << 16) | (m[16] & 0x1fff),
  110. priv->io_base +
  111. _REG(VIU_OSD1_MATRIX_COEF40_41));
  112. writel(m[17] & 0x1fff, priv->io_base +
  113. _REG(VIU_OSD1_MATRIX_COLMOD_COEF42));
  114. } else
  115. writel((m[11] & 0x1fff) << 16, priv->io_base +
  116. _REG(VIU_OSD1_MATRIX_COEF22_30));
  117. writel(((m[18] & 0xfff) << 16) | (m[19] & 0xfff),
  118. priv->io_base + _REG(VIU_OSD1_MATRIX_OFFSET0_1));
  119. writel(m[20] & 0xfff,
  120. priv->io_base + _REG(VIU_OSD1_MATRIX_OFFSET2));
  121. writel_bits_relaxed(3 << 30, m[21] << 30,
  122. priv->io_base + _REG(VIU_OSD1_MATRIX_COLMOD_COEF42));
  123. writel_bits_relaxed(7 << 16, m[22] << 16,
  124. priv->io_base + _REG(VIU_OSD1_MATRIX_COLMOD_COEF42));
  125. /* 23 reserved for clipping control */
  126. writel_bits_relaxed(BIT(0), csc_on ? BIT(0) : 0,
  127. priv->io_base + _REG(VIU_OSD1_MATRIX_CTRL));
  128. writel_bits_relaxed(BIT(1), 0,
  129. priv->io_base + _REG(VIU_OSD1_MATRIX_CTRL));
  130. } else if (m_select == VIU_MATRIX_OSD_EOTF) {
  131. int i;
  132. /* osd eotf matrix, VIU_MATRIX_OSD_EOTF */
  133. for (i = 0; i < 5; i++)
  134. writel(((m[i * 2] & 0x1fff) << 16) |
  135. (m[i * 2 + 1] & 0x1fff), priv->io_base +
  136. _REG(VIU_OSD1_EOTF_CTL + i + 1));
  137. writel_bits_relaxed(BIT(30), csc_on ? BIT(30) : 0,
  138. priv->io_base + _REG(VIU_OSD1_EOTF_CTL));
  139. writel_bits_relaxed(BIT(31), csc_on ? BIT(31) : 0,
  140. priv->io_base + _REG(VIU_OSD1_EOTF_CTL));
  141. }
  142. }
  143. #define OSD_EOTF_LUT_SIZE 33
  144. #define OSD_OETF_LUT_SIZE 41
  145. void meson_viu_set_osd_lut(struct meson_drm *priv, enum viu_lut_sel_e lut_sel,
  146. unsigned int *r_map, unsigned int *g_map,
  147. unsigned int *b_map,
  148. bool csc_on)
  149. {
  150. unsigned int addr_port;
  151. unsigned int data_port;
  152. unsigned int ctrl_port;
  153. int i;
  154. if (lut_sel == VIU_LUT_OSD_EOTF) {
  155. addr_port = VIU_OSD1_EOTF_LUT_ADDR_PORT;
  156. data_port = VIU_OSD1_EOTF_LUT_DATA_PORT;
  157. ctrl_port = VIU_OSD1_EOTF_CTL;
  158. } else if (lut_sel == VIU_LUT_OSD_OETF) {
  159. addr_port = VIU_OSD1_OETF_LUT_ADDR_PORT;
  160. data_port = VIU_OSD1_OETF_LUT_DATA_PORT;
  161. ctrl_port = VIU_OSD1_OETF_CTL;
  162. } else
  163. return;
  164. if (lut_sel == VIU_LUT_OSD_OETF) {
  165. writel(0, priv->io_base + _REG(addr_port));
  166. for (i = 0; i < 20; i++)
  167. writel(r_map[i * 2] | (r_map[i * 2 + 1] << 16),
  168. priv->io_base + _REG(data_port));
  169. writel(r_map[OSD_OETF_LUT_SIZE - 1] | (g_map[0] << 16),
  170. priv->io_base + _REG(data_port));
  171. for (i = 0; i < 20; i++)
  172. writel(g_map[i * 2 + 1] | (g_map[i * 2 + 2] << 16),
  173. priv->io_base + _REG(data_port));
  174. for (i = 0; i < 20; i++)
  175. writel(b_map[i * 2] | (b_map[i * 2 + 1] << 16),
  176. priv->io_base + _REG(data_port));
  177. writel(b_map[OSD_OETF_LUT_SIZE - 1],
  178. priv->io_base + _REG(data_port));
  179. if (csc_on)
  180. writel_bits_relaxed(0x7 << 29, 7 << 29,
  181. priv->io_base + _REG(ctrl_port));
  182. else
  183. writel_bits_relaxed(0x7 << 29, 0,
  184. priv->io_base + _REG(ctrl_port));
  185. } else if (lut_sel == VIU_LUT_OSD_EOTF) {
  186. writel(0, priv->io_base + _REG(addr_port));
  187. for (i = 0; i < 20; i++)
  188. writel(r_map[i * 2] | (r_map[i * 2 + 1] << 16),
  189. priv->io_base + _REG(data_port));
  190. writel(r_map[OSD_EOTF_LUT_SIZE - 1] | (g_map[0] << 16),
  191. priv->io_base + _REG(data_port));
  192. for (i = 0; i < 20; i++)
  193. writel(g_map[i * 2 + 1] | (g_map[i * 2 + 2] << 16),
  194. priv->io_base + _REG(data_port));
  195. for (i = 0; i < 20; i++)
  196. writel(b_map[i * 2] | (b_map[i * 2 + 1] << 16),
  197. priv->io_base + _REG(data_port));
  198. writel(b_map[OSD_EOTF_LUT_SIZE - 1],
  199. priv->io_base + _REG(data_port));
  200. if (csc_on)
  201. writel_bits_relaxed(7 << 27, 7 << 27,
  202. priv->io_base + _REG(ctrl_port));
  203. else
  204. writel_bits_relaxed(7 << 27, 0,
  205. priv->io_base + _REG(ctrl_port));
  206. writel_bits_relaxed(BIT(31), BIT(31),
  207. priv->io_base + _REG(ctrl_port));
  208. }
  209. }
  210. /* eotf lut: linear */
  211. static unsigned int eotf_33_linear_mapping[OSD_EOTF_LUT_SIZE] = {
  212. 0x0000, 0x0200, 0x0400, 0x0600,
  213. 0x0800, 0x0a00, 0x0c00, 0x0e00,
  214. 0x1000, 0x1200, 0x1400, 0x1600,
  215. 0x1800, 0x1a00, 0x1c00, 0x1e00,
  216. 0x2000, 0x2200, 0x2400, 0x2600,
  217. 0x2800, 0x2a00, 0x2c00, 0x2e00,
  218. 0x3000, 0x3200, 0x3400, 0x3600,
  219. 0x3800, 0x3a00, 0x3c00, 0x3e00,
  220. 0x4000
  221. };
  222. /* osd oetf lut: linear */
  223. static unsigned int oetf_41_linear_mapping[OSD_OETF_LUT_SIZE] = {
  224. 0, 0, 0, 0,
  225. 0, 32, 64, 96,
  226. 128, 160, 196, 224,
  227. 256, 288, 320, 352,
  228. 384, 416, 448, 480,
  229. 512, 544, 576, 608,
  230. 640, 672, 704, 736,
  231. 768, 800, 832, 864,
  232. 896, 928, 960, 992,
  233. 1023, 1023, 1023, 1023,
  234. 1023
  235. };
  236. static void meson_viu_load_matrix(struct meson_drm *priv)
  237. {
  238. /* eotf lut bypass */
  239. meson_viu_set_osd_lut(priv, VIU_LUT_OSD_EOTF,
  240. eotf_33_linear_mapping, /* R */
  241. eotf_33_linear_mapping, /* G */
  242. eotf_33_linear_mapping, /* B */
  243. false);
  244. /* eotf matrix bypass */
  245. meson_viu_set_osd_matrix(priv, VIU_MATRIX_OSD_EOTF,
  246. eotf_bypass_coeff,
  247. false);
  248. /* oetf lut bypass */
  249. meson_viu_set_osd_lut(priv, VIU_LUT_OSD_OETF,
  250. oetf_41_linear_mapping, /* R */
  251. oetf_41_linear_mapping, /* G */
  252. oetf_41_linear_mapping, /* B */
  253. false);
  254. /* osd matrix RGB709 to YUV709 limit */
  255. meson_viu_set_osd_matrix(priv, VIU_MATRIX_OSD,
  256. RGB709_to_YUV709l_coeff,
  257. true);
  258. }
  259. void meson_viu_init(struct meson_drm *priv)
  260. {
  261. uint32_t reg;
  262. /* Disable OSDs */
  263. writel_bits_relaxed(BIT(0) | BIT(21), 0,
  264. priv->io_base + _REG(VIU_OSD1_CTRL_STAT));
  265. writel_bits_relaxed(BIT(0) | BIT(21), 0,
  266. priv->io_base + _REG(VIU_OSD2_CTRL_STAT));
  267. /* On GXL/GXM, Use the 10bit HDR conversion matrix */
  268. if (meson_vpu_is_compatible(priv, "amlogic,meson-gxm-vpu") ||
  269. meson_vpu_is_compatible(priv, "amlogic,meson-gxl-vpu"))
  270. meson_viu_load_matrix(priv);
  271. /* Initialize OSD1 fifo control register */
  272. reg = BIT(0) | /* Urgent DDR request priority */
  273. (4 << 5) | /* hold_fifo_lines */
  274. (3 << 10) | /* burst length 64 */
  275. (32 << 12) | /* fifo_depth_val: 32*8=256 */
  276. (2 << 22) | /* 4 words in 1 burst */
  277. (2 << 24);
  278. writel_relaxed(reg, priv->io_base + _REG(VIU_OSD1_FIFO_CTRL_STAT));
  279. writel_relaxed(reg, priv->io_base + _REG(VIU_OSD2_FIFO_CTRL_STAT));
  280. /* Set OSD alpha replace value */
  281. writel_bits_relaxed(0xff << OSD_REPLACE_SHIFT,
  282. 0xff << OSD_REPLACE_SHIFT,
  283. priv->io_base + _REG(VIU_OSD1_CTRL_STAT2));
  284. writel_bits_relaxed(0xff << OSD_REPLACE_SHIFT,
  285. 0xff << OSD_REPLACE_SHIFT,
  286. priv->io_base + _REG(VIU_OSD2_CTRL_STAT2));
  287. priv->viu.osd1_enabled = false;
  288. priv->viu.osd1_commit = false;
  289. priv->viu.osd1_interlace = false;
  290. }