sun8i_csc.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) Jernej Skrabec <jernej.skrabec@siol.net>
  4. */
  5. #include <drm/drm_print.h>
  6. #include "sun8i_csc.h"
  7. #include "sun8i_mixer.h"
  8. static const u32 ccsc_base[][2] = {
  9. [CCSC_MIXER0_LAYOUT] = {CCSC00_OFFSET, CCSC01_OFFSET},
  10. [CCSC_MIXER1_LAYOUT] = {CCSC10_OFFSET, CCSC11_OFFSET},
  11. [CCSC_D1_MIXER0_LAYOUT] = {CCSC00_OFFSET, CCSC01_D1_OFFSET},
  12. };
  13. /*
  14. * Factors are in two's complement format, 10 bits for fractinal part.
  15. * First tree values in each line are multiplication factor and last
  16. * value is constant, which is added at the end.
  17. */
  18. static const u32 yuv2rgb[2][2][12] = {
  19. [DRM_COLOR_YCBCR_LIMITED_RANGE] = {
  20. [DRM_COLOR_YCBCR_BT601] = {
  21. 0x000004A8, 0x00000000, 0x00000662, 0xFFFC8451,
  22. 0x000004A8, 0xFFFFFE6F, 0xFFFFFCC0, 0x00021E4D,
  23. 0x000004A8, 0x00000811, 0x00000000, 0xFFFBACA9,
  24. },
  25. [DRM_COLOR_YCBCR_BT709] = {
  26. 0x000004A8, 0x00000000, 0x0000072B, 0xFFFC1F99,
  27. 0x000004A8, 0xFFFFFF26, 0xFFFFFDDF, 0x00013383,
  28. 0x000004A8, 0x00000873, 0x00000000, 0xFFFB7BEF,
  29. }
  30. },
  31. [DRM_COLOR_YCBCR_FULL_RANGE] = {
  32. [DRM_COLOR_YCBCR_BT601] = {
  33. 0x00000400, 0x00000000, 0x0000059B, 0xFFFD322E,
  34. 0x00000400, 0xFFFFFEA0, 0xFFFFFD25, 0x00021DD5,
  35. 0x00000400, 0x00000716, 0x00000000, 0xFFFC74BD,
  36. },
  37. [DRM_COLOR_YCBCR_BT709] = {
  38. 0x00000400, 0x00000000, 0x0000064C, 0xFFFCD9B4,
  39. 0x00000400, 0xFFFFFF41, 0xFFFFFE21, 0x00014F96,
  40. 0x00000400, 0x0000076C, 0x00000000, 0xFFFC49EF,
  41. }
  42. },
  43. };
  44. /*
  45. * DE3 has a bit different CSC units. Factors are in two's complement format.
  46. * First three factors in a row are multiplication factors which have 17 bits
  47. * for fractional part. Fourth value in a row is comprised of two factors.
  48. * Upper 16 bits represents difference, which is subtracted from the input
  49. * value before multiplication and lower 16 bits represents constant, which
  50. * is addes at the end.
  51. *
  52. * x' = c00 * (x + d0) + c01 * (y + d1) + c02 * (z + d2) + const0
  53. * y' = c10 * (x + d0) + c11 * (y + d1) + c12 * (z + d2) + const1
  54. * z' = c20 * (x + d0) + c21 * (y + d1) + c22 * (z + d2) + const2
  55. *
  56. * Please note that above formula is true only for Blender CSC. Other DE3 CSC
  57. * units takes only positive value for difference. From what can be deducted
  58. * from BSP driver code, those units probably automatically assume that
  59. * difference has to be subtracted.
  60. *
  61. * Layout of factors in table:
  62. * c00 c01 c02 [d0 const0]
  63. * c10 c11 c12 [d1 const1]
  64. * c20 c21 c22 [d2 const2]
  65. */
  66. static const u32 yuv2rgb_de3[2][3][12] = {
  67. [DRM_COLOR_YCBCR_LIMITED_RANGE] = {
  68. [DRM_COLOR_YCBCR_BT601] = {
  69. 0x0002542A, 0x00000000, 0x0003312A, 0xFFC00000,
  70. 0x0002542A, 0xFFFF376B, 0xFFFE5FC3, 0xFE000000,
  71. 0x0002542A, 0x000408D2, 0x00000000, 0xFE000000,
  72. },
  73. [DRM_COLOR_YCBCR_BT709] = {
  74. 0x0002542A, 0x00000000, 0x000395E2, 0xFFC00000,
  75. 0x0002542A, 0xFFFF92D2, 0xFFFEEF27, 0xFE000000,
  76. 0x0002542A, 0x0004398C, 0x00000000, 0xFE000000,
  77. },
  78. [DRM_COLOR_YCBCR_BT2020] = {
  79. 0x0002542A, 0x00000000, 0x00035B7B, 0xFFC00000,
  80. 0x0002542A, 0xFFFFA017, 0xFFFEB2FC, 0xFE000000,
  81. 0x0002542A, 0x00044896, 0x00000000, 0xFE000000,
  82. }
  83. },
  84. [DRM_COLOR_YCBCR_FULL_RANGE] = {
  85. [DRM_COLOR_YCBCR_BT601] = {
  86. 0x00020000, 0x00000000, 0x0002CDD2, 0x00000000,
  87. 0x00020000, 0xFFFF4FCE, 0xFFFE925D, 0xFE000000,
  88. 0x00020000, 0x00038B43, 0x00000000, 0xFE000000,
  89. },
  90. [DRM_COLOR_YCBCR_BT709] = {
  91. 0x00020000, 0x00000000, 0x0003264C, 0x00000000,
  92. 0x00020000, 0xFFFFA018, 0xFFFF1053, 0xFE000000,
  93. 0x00020000, 0x0003B611, 0x00000000, 0xFE000000,
  94. },
  95. [DRM_COLOR_YCBCR_BT2020] = {
  96. 0x00020000, 0x00000000, 0x0002F2FE, 0x00000000,
  97. 0x00020000, 0xFFFFABC0, 0xFFFEDB78, 0xFE000000,
  98. 0x00020000, 0x0003C346, 0x00000000, 0xFE000000,
  99. }
  100. },
  101. };
  102. static void sun8i_csc_set_coefficients(struct regmap *map, u32 base,
  103. enum sun8i_csc_mode mode,
  104. enum drm_color_encoding encoding,
  105. enum drm_color_range range)
  106. {
  107. const u32 *table;
  108. u32 base_reg;
  109. int i;
  110. table = yuv2rgb[range][encoding];
  111. switch (mode) {
  112. case SUN8I_CSC_MODE_YUV2RGB:
  113. base_reg = SUN8I_CSC_COEFF(base, 0);
  114. regmap_bulk_write(map, base_reg, table, 12);
  115. break;
  116. case SUN8I_CSC_MODE_YVU2RGB:
  117. for (i = 0; i < 12; i++) {
  118. if ((i & 3) == 1)
  119. base_reg = SUN8I_CSC_COEFF(base, i + 1);
  120. else if ((i & 3) == 2)
  121. base_reg = SUN8I_CSC_COEFF(base, i - 1);
  122. else
  123. base_reg = SUN8I_CSC_COEFF(base, i);
  124. regmap_write(map, base_reg, table[i]);
  125. }
  126. break;
  127. default:
  128. DRM_WARN("Wrong CSC mode specified.\n");
  129. return;
  130. }
  131. }
  132. static void sun8i_de3_ccsc_set_coefficients(struct regmap *map, int layer,
  133. enum sun8i_csc_mode mode,
  134. enum drm_color_encoding encoding,
  135. enum drm_color_range range)
  136. {
  137. const u32 *table;
  138. u32 addr;
  139. int i;
  140. table = yuv2rgb_de3[range][encoding];
  141. switch (mode) {
  142. case SUN8I_CSC_MODE_YUV2RGB:
  143. addr = SUN50I_MIXER_BLEND_CSC_COEFF(DE3_BLD_BASE, layer, 0);
  144. regmap_bulk_write(map, addr, table, 12);
  145. break;
  146. case SUN8I_CSC_MODE_YVU2RGB:
  147. for (i = 0; i < 12; i++) {
  148. if ((i & 3) == 1)
  149. addr = SUN50I_MIXER_BLEND_CSC_COEFF(DE3_BLD_BASE,
  150. layer,
  151. i + 1);
  152. else if ((i & 3) == 2)
  153. addr = SUN50I_MIXER_BLEND_CSC_COEFF(DE3_BLD_BASE,
  154. layer,
  155. i - 1);
  156. else
  157. addr = SUN50I_MIXER_BLEND_CSC_COEFF(DE3_BLD_BASE,
  158. layer, i);
  159. regmap_write(map, addr, table[i]);
  160. }
  161. break;
  162. default:
  163. DRM_WARN("Wrong CSC mode specified.\n");
  164. return;
  165. }
  166. }
  167. static void sun8i_csc_enable(struct regmap *map, u32 base, bool enable)
  168. {
  169. u32 val;
  170. if (enable)
  171. val = SUN8I_CSC_CTRL_EN;
  172. else
  173. val = 0;
  174. regmap_update_bits(map, SUN8I_CSC_CTRL(base), SUN8I_CSC_CTRL_EN, val);
  175. }
  176. static void sun8i_de3_ccsc_enable(struct regmap *map, int layer, bool enable)
  177. {
  178. u32 val, mask;
  179. mask = SUN50I_MIXER_BLEND_CSC_CTL_EN(layer);
  180. if (enable)
  181. val = mask;
  182. else
  183. val = 0;
  184. regmap_update_bits(map, SUN50I_MIXER_BLEND_CSC_CTL(DE3_BLD_BASE),
  185. mask, val);
  186. }
  187. void sun8i_csc_set_ccsc_coefficients(struct sun8i_mixer *mixer, int layer,
  188. enum sun8i_csc_mode mode,
  189. enum drm_color_encoding encoding,
  190. enum drm_color_range range)
  191. {
  192. u32 base;
  193. if (mixer->cfg->is_de3) {
  194. sun8i_de3_ccsc_set_coefficients(mixer->engine.regs, layer,
  195. mode, encoding, range);
  196. return;
  197. }
  198. base = ccsc_base[mixer->cfg->ccsc][layer];
  199. sun8i_csc_set_coefficients(mixer->engine.regs, base,
  200. mode, encoding, range);
  201. }
  202. void sun8i_csc_enable_ccsc(struct sun8i_mixer *mixer, int layer, bool enable)
  203. {
  204. u32 base;
  205. if (mixer->cfg->is_de3) {
  206. sun8i_de3_ccsc_enable(mixer->engine.regs, layer, enable);
  207. return;
  208. }
  209. base = ccsc_base[mixer->cfg->ccsc][layer];
  210. sun8i_csc_enable(mixer->engine.regs, base, enable);
  211. }