meson_viu.c 10 KB

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