18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) Jernej Skrabec <jernej.skrabec@siol.net> 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <drm/drm_print.h> 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include "sun8i_csc.h" 98c2ecf20Sopenharmony_ci#include "sun8i_mixer.h" 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_cistatic const u32 ccsc_base[2][2] = { 128c2ecf20Sopenharmony_ci {CCSC00_OFFSET, CCSC01_OFFSET}, 138c2ecf20Sopenharmony_ci {CCSC10_OFFSET, CCSC11_OFFSET}, 148c2ecf20Sopenharmony_ci}; 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci/* 178c2ecf20Sopenharmony_ci * Factors are in two's complement format, 10 bits for fractinal part. 188c2ecf20Sopenharmony_ci * First tree values in each line are multiplication factor and last 198c2ecf20Sopenharmony_ci * value is constant, which is added at the end. 208c2ecf20Sopenharmony_ci */ 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_cistatic const u32 yuv2rgb[2][2][12] = { 238c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_LIMITED_RANGE] = { 248c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_BT601] = { 258c2ecf20Sopenharmony_ci 0x000004A8, 0x00000000, 0x00000662, 0xFFFC8451, 268c2ecf20Sopenharmony_ci 0x000004A8, 0xFFFFFE6F, 0xFFFFFCC0, 0x00021E4D, 278c2ecf20Sopenharmony_ci 0x000004A8, 0x00000811, 0x00000000, 0xFFFBACA9, 288c2ecf20Sopenharmony_ci }, 298c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_BT709] = { 308c2ecf20Sopenharmony_ci 0x000004A8, 0x00000000, 0x0000072B, 0xFFFC1F99, 318c2ecf20Sopenharmony_ci 0x000004A8, 0xFFFFFF26, 0xFFFFFDDF, 0x00013383, 328c2ecf20Sopenharmony_ci 0x000004A8, 0x00000873, 0x00000000, 0xFFFB7BEF, 338c2ecf20Sopenharmony_ci } 348c2ecf20Sopenharmony_ci }, 358c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_FULL_RANGE] = { 368c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_BT601] = { 378c2ecf20Sopenharmony_ci 0x00000400, 0x00000000, 0x0000059B, 0xFFFD322E, 388c2ecf20Sopenharmony_ci 0x00000400, 0xFFFFFEA0, 0xFFFFFD25, 0x00021DD5, 398c2ecf20Sopenharmony_ci 0x00000400, 0x00000716, 0x00000000, 0xFFFC74BD, 408c2ecf20Sopenharmony_ci }, 418c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_BT709] = { 428c2ecf20Sopenharmony_ci 0x00000400, 0x00000000, 0x0000064C, 0xFFFCD9B4, 438c2ecf20Sopenharmony_ci 0x00000400, 0xFFFFFF41, 0xFFFFFE21, 0x00014F96, 448c2ecf20Sopenharmony_ci 0x00000400, 0x0000076C, 0x00000000, 0xFFFC49EF, 458c2ecf20Sopenharmony_ci } 468c2ecf20Sopenharmony_ci }, 478c2ecf20Sopenharmony_ci}; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistatic const u32 yvu2rgb[2][2][12] = { 508c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_LIMITED_RANGE] = { 518c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_BT601] = { 528c2ecf20Sopenharmony_ci 0x000004A8, 0x00000662, 0x00000000, 0xFFFC8451, 538c2ecf20Sopenharmony_ci 0x000004A8, 0xFFFFFCC0, 0xFFFFFE6F, 0x00021E4D, 548c2ecf20Sopenharmony_ci 0x000004A8, 0x00000000, 0x00000811, 0xFFFBACA9, 558c2ecf20Sopenharmony_ci }, 568c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_BT709] = { 578c2ecf20Sopenharmony_ci 0x000004A8, 0x0000072B, 0x00000000, 0xFFFC1F99, 588c2ecf20Sopenharmony_ci 0x000004A8, 0xFFFFFDDF, 0xFFFFFF26, 0x00013383, 598c2ecf20Sopenharmony_ci 0x000004A8, 0x00000000, 0x00000873, 0xFFFB7BEF, 608c2ecf20Sopenharmony_ci } 618c2ecf20Sopenharmony_ci }, 628c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_FULL_RANGE] = { 638c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_BT601] = { 648c2ecf20Sopenharmony_ci 0x00000400, 0x0000059B, 0x00000000, 0xFFFD322E, 658c2ecf20Sopenharmony_ci 0x00000400, 0xFFFFFD25, 0xFFFFFEA0, 0x00021DD5, 668c2ecf20Sopenharmony_ci 0x00000400, 0x00000000, 0x00000716, 0xFFFC74BD, 678c2ecf20Sopenharmony_ci }, 688c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_BT709] = { 698c2ecf20Sopenharmony_ci 0x00000400, 0x0000064C, 0x00000000, 0xFFFCD9B4, 708c2ecf20Sopenharmony_ci 0x00000400, 0xFFFFFE21, 0xFFFFFF41, 0x00014F96, 718c2ecf20Sopenharmony_ci 0x00000400, 0x00000000, 0x0000076C, 0xFFFC49EF, 728c2ecf20Sopenharmony_ci } 738c2ecf20Sopenharmony_ci }, 748c2ecf20Sopenharmony_ci}; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci/* 778c2ecf20Sopenharmony_ci * DE3 has a bit different CSC units. Factors are in two's complement format. 788c2ecf20Sopenharmony_ci * First three factors in a row are multiplication factors which have 17 bits 798c2ecf20Sopenharmony_ci * for fractional part. Fourth value in a row is comprised of two factors. 808c2ecf20Sopenharmony_ci * Upper 16 bits represents difference, which is subtracted from the input 818c2ecf20Sopenharmony_ci * value before multiplication and lower 16 bits represents constant, which 828c2ecf20Sopenharmony_ci * is addes at the end. 838c2ecf20Sopenharmony_ci * 848c2ecf20Sopenharmony_ci * x' = c00 * (x + d0) + c01 * (y + d1) + c02 * (z + d2) + const0 858c2ecf20Sopenharmony_ci * y' = c10 * (x + d0) + c11 * (y + d1) + c12 * (z + d2) + const1 868c2ecf20Sopenharmony_ci * z' = c20 * (x + d0) + c21 * (y + d1) + c22 * (z + d2) + const2 878c2ecf20Sopenharmony_ci * 888c2ecf20Sopenharmony_ci * Please note that above formula is true only for Blender CSC. Other DE3 CSC 898c2ecf20Sopenharmony_ci * units takes only positive value for difference. From what can be deducted 908c2ecf20Sopenharmony_ci * from BSP driver code, those units probably automatically assume that 918c2ecf20Sopenharmony_ci * difference has to be subtracted. 928c2ecf20Sopenharmony_ci * 938c2ecf20Sopenharmony_ci * Layout of factors in table: 948c2ecf20Sopenharmony_ci * c00 c01 c02 [d0 const0] 958c2ecf20Sopenharmony_ci * c10 c11 c12 [d1 const1] 968c2ecf20Sopenharmony_ci * c20 c21 c22 [d2 const2] 978c2ecf20Sopenharmony_ci */ 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_cistatic const u32 yuv2rgb_de3[2][2][12] = { 1008c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_LIMITED_RANGE] = { 1018c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_BT601] = { 1028c2ecf20Sopenharmony_ci 0x0002542A, 0x00000000, 0x0003312A, 0xFFC00000, 1038c2ecf20Sopenharmony_ci 0x0002542A, 0xFFFF376B, 0xFFFE5FC3, 0xFE000000, 1048c2ecf20Sopenharmony_ci 0x0002542A, 0x000408D2, 0x00000000, 0xFE000000, 1058c2ecf20Sopenharmony_ci }, 1068c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_BT709] = { 1078c2ecf20Sopenharmony_ci 0x0002542A, 0x00000000, 0x000395E2, 0xFFC00000, 1088c2ecf20Sopenharmony_ci 0x0002542A, 0xFFFF92D2, 0xFFFEEF27, 0xFE000000, 1098c2ecf20Sopenharmony_ci 0x0002542A, 0x0004398C, 0x00000000, 0xFE000000, 1108c2ecf20Sopenharmony_ci } 1118c2ecf20Sopenharmony_ci }, 1128c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_FULL_RANGE] = { 1138c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_BT601] = { 1148c2ecf20Sopenharmony_ci 0x00020000, 0x00000000, 0x0002CDD2, 0x00000000, 1158c2ecf20Sopenharmony_ci 0x00020000, 0xFFFF4FCE, 0xFFFE925D, 0xFE000000, 1168c2ecf20Sopenharmony_ci 0x00020000, 0x00038B43, 0x00000000, 0xFE000000, 1178c2ecf20Sopenharmony_ci }, 1188c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_BT709] = { 1198c2ecf20Sopenharmony_ci 0x00020000, 0x00000000, 0x0003264C, 0x00000000, 1208c2ecf20Sopenharmony_ci 0x00020000, 0xFFFFA018, 0xFFFF1053, 0xFE000000, 1218c2ecf20Sopenharmony_ci 0x00020000, 0x0003B611, 0x00000000, 0xFE000000, 1228c2ecf20Sopenharmony_ci } 1238c2ecf20Sopenharmony_ci }, 1248c2ecf20Sopenharmony_ci}; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_cistatic const u32 yvu2rgb_de3[2][2][12] = { 1278c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_LIMITED_RANGE] = { 1288c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_BT601] = { 1298c2ecf20Sopenharmony_ci 0x0002542A, 0x0003312A, 0x00000000, 0xFFC00000, 1308c2ecf20Sopenharmony_ci 0x0002542A, 0xFFFE5FC3, 0xFFFF376B, 0xFE000000, 1318c2ecf20Sopenharmony_ci 0x0002542A, 0x00000000, 0x000408D2, 0xFE000000, 1328c2ecf20Sopenharmony_ci }, 1338c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_BT709] = { 1348c2ecf20Sopenharmony_ci 0x0002542A, 0x000395E2, 0x00000000, 0xFFC00000, 1358c2ecf20Sopenharmony_ci 0x0002542A, 0xFFFEEF27, 0xFFFF92D2, 0xFE000000, 1368c2ecf20Sopenharmony_ci 0x0002542A, 0x00000000, 0x0004398C, 0xFE000000, 1378c2ecf20Sopenharmony_ci } 1388c2ecf20Sopenharmony_ci }, 1398c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_FULL_RANGE] = { 1408c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_BT601] = { 1418c2ecf20Sopenharmony_ci 0x00020000, 0x0002CDD2, 0x00000000, 0x00000000, 1428c2ecf20Sopenharmony_ci 0x00020000, 0xFFFE925D, 0xFFFF4FCE, 0xFE000000, 1438c2ecf20Sopenharmony_ci 0x00020000, 0x00000000, 0x00038B43, 0xFE000000, 1448c2ecf20Sopenharmony_ci }, 1458c2ecf20Sopenharmony_ci [DRM_COLOR_YCBCR_BT709] = { 1468c2ecf20Sopenharmony_ci 0x00020000, 0x0003264C, 0x00000000, 0x00000000, 1478c2ecf20Sopenharmony_ci 0x00020000, 0xFFFF1053, 0xFFFFA018, 0xFE000000, 1488c2ecf20Sopenharmony_ci 0x00020000, 0x00000000, 0x0003B611, 0xFE000000, 1498c2ecf20Sopenharmony_ci } 1508c2ecf20Sopenharmony_ci }, 1518c2ecf20Sopenharmony_ci}; 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_cistatic void sun8i_csc_set_coefficients(struct regmap *map, u32 base, 1548c2ecf20Sopenharmony_ci enum sun8i_csc_mode mode, 1558c2ecf20Sopenharmony_ci enum drm_color_encoding encoding, 1568c2ecf20Sopenharmony_ci enum drm_color_range range) 1578c2ecf20Sopenharmony_ci{ 1588c2ecf20Sopenharmony_ci const u32 *table; 1598c2ecf20Sopenharmony_ci u32 base_reg; 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci switch (mode) { 1628c2ecf20Sopenharmony_ci case SUN8I_CSC_MODE_YUV2RGB: 1638c2ecf20Sopenharmony_ci table = yuv2rgb[range][encoding]; 1648c2ecf20Sopenharmony_ci break; 1658c2ecf20Sopenharmony_ci case SUN8I_CSC_MODE_YVU2RGB: 1668c2ecf20Sopenharmony_ci table = yvu2rgb[range][encoding]; 1678c2ecf20Sopenharmony_ci break; 1688c2ecf20Sopenharmony_ci default: 1698c2ecf20Sopenharmony_ci DRM_WARN("Wrong CSC mode specified.\n"); 1708c2ecf20Sopenharmony_ci return; 1718c2ecf20Sopenharmony_ci } 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci base_reg = SUN8I_CSC_COEFF(base, 0); 1748c2ecf20Sopenharmony_ci regmap_bulk_write(map, base_reg, table, 12); 1758c2ecf20Sopenharmony_ci} 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_cistatic void sun8i_de3_ccsc_set_coefficients(struct regmap *map, int layer, 1788c2ecf20Sopenharmony_ci enum sun8i_csc_mode mode, 1798c2ecf20Sopenharmony_ci enum drm_color_encoding encoding, 1808c2ecf20Sopenharmony_ci enum drm_color_range range) 1818c2ecf20Sopenharmony_ci{ 1828c2ecf20Sopenharmony_ci const u32 *table; 1838c2ecf20Sopenharmony_ci u32 base_reg; 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci switch (mode) { 1868c2ecf20Sopenharmony_ci case SUN8I_CSC_MODE_YUV2RGB: 1878c2ecf20Sopenharmony_ci table = yuv2rgb_de3[range][encoding]; 1888c2ecf20Sopenharmony_ci break; 1898c2ecf20Sopenharmony_ci case SUN8I_CSC_MODE_YVU2RGB: 1908c2ecf20Sopenharmony_ci table = yvu2rgb_de3[range][encoding]; 1918c2ecf20Sopenharmony_ci break; 1928c2ecf20Sopenharmony_ci default: 1938c2ecf20Sopenharmony_ci DRM_WARN("Wrong CSC mode specified.\n"); 1948c2ecf20Sopenharmony_ci return; 1958c2ecf20Sopenharmony_ci } 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci base_reg = SUN50I_MIXER_BLEND_CSC_COEFF(DE3_BLD_BASE, layer, 0, 0); 1988c2ecf20Sopenharmony_ci regmap_bulk_write(map, base_reg, table, 12); 1998c2ecf20Sopenharmony_ci} 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_cistatic void sun8i_csc_enable(struct regmap *map, u32 base, bool enable) 2028c2ecf20Sopenharmony_ci{ 2038c2ecf20Sopenharmony_ci u32 val; 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci if (enable) 2068c2ecf20Sopenharmony_ci val = SUN8I_CSC_CTRL_EN; 2078c2ecf20Sopenharmony_ci else 2088c2ecf20Sopenharmony_ci val = 0; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci regmap_update_bits(map, SUN8I_CSC_CTRL(base), SUN8I_CSC_CTRL_EN, val); 2118c2ecf20Sopenharmony_ci} 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_cistatic void sun8i_de3_ccsc_enable(struct regmap *map, int layer, bool enable) 2148c2ecf20Sopenharmony_ci{ 2158c2ecf20Sopenharmony_ci u32 val, mask; 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci mask = SUN50I_MIXER_BLEND_CSC_CTL_EN(layer); 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci if (enable) 2208c2ecf20Sopenharmony_ci val = mask; 2218c2ecf20Sopenharmony_ci else 2228c2ecf20Sopenharmony_ci val = 0; 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci regmap_update_bits(map, SUN50I_MIXER_BLEND_CSC_CTL(DE3_BLD_BASE), 2258c2ecf20Sopenharmony_ci mask, val); 2268c2ecf20Sopenharmony_ci} 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_civoid sun8i_csc_set_ccsc_coefficients(struct sun8i_mixer *mixer, int layer, 2298c2ecf20Sopenharmony_ci enum sun8i_csc_mode mode, 2308c2ecf20Sopenharmony_ci enum drm_color_encoding encoding, 2318c2ecf20Sopenharmony_ci enum drm_color_range range) 2328c2ecf20Sopenharmony_ci{ 2338c2ecf20Sopenharmony_ci u32 base; 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci if (mixer->cfg->is_de3) { 2368c2ecf20Sopenharmony_ci sun8i_de3_ccsc_set_coefficients(mixer->engine.regs, layer, 2378c2ecf20Sopenharmony_ci mode, encoding, range); 2388c2ecf20Sopenharmony_ci return; 2398c2ecf20Sopenharmony_ci } 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci base = ccsc_base[mixer->cfg->ccsc][layer]; 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci sun8i_csc_set_coefficients(mixer->engine.regs, base, 2448c2ecf20Sopenharmony_ci mode, encoding, range); 2458c2ecf20Sopenharmony_ci} 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_civoid sun8i_csc_enable_ccsc(struct sun8i_mixer *mixer, int layer, bool enable) 2488c2ecf20Sopenharmony_ci{ 2498c2ecf20Sopenharmony_ci u32 base; 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci if (mixer->cfg->is_de3) { 2528c2ecf20Sopenharmony_ci sun8i_de3_ccsc_enable(mixer->engine.regs, layer, enable); 2538c2ecf20Sopenharmony_ci return; 2548c2ecf20Sopenharmony_ci } 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci base = ccsc_base[mixer->cfg->ccsc][layer]; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci sun8i_csc_enable(mixer->engine.regs, base, enable); 2598c2ecf20Sopenharmony_ci} 260