18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2019 Mentor Graphics Inc.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/types.h>
78c2ecf20Sopenharmony_ci#include <linux/init.h>
88c2ecf20Sopenharmony_ci#include <linux/errno.h>
98c2ecf20Sopenharmony_ci#include <linux/err.h>
108c2ecf20Sopenharmony_ci#include <linux/sizes.h>
118c2ecf20Sopenharmony_ci#include "ipu-prv.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define QUANT_MAP(q)					\
148c2ecf20Sopenharmony_ci	((q) == V4L2_QUANTIZATION_FULL_RANGE ||		\
158c2ecf20Sopenharmony_ci	 (q) == V4L2_QUANTIZATION_DEFAULT ? 0 : 1)
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/* identity matrix */
188c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params identity = {
198c2ecf20Sopenharmony_ci	.coeff = {
208c2ecf20Sopenharmony_ci		{  128,    0,    0, },
218c2ecf20Sopenharmony_ci		{    0,  128,    0, },
228c2ecf20Sopenharmony_ci		{    0,    0,  128, },
238c2ecf20Sopenharmony_ci	},
248c2ecf20Sopenharmony_ci	.offset = { 0, 0, 0, },
258c2ecf20Sopenharmony_ci	.scale = 2,
268c2ecf20Sopenharmony_ci};
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/*
298c2ecf20Sopenharmony_ci * RGB full-range to RGB limited-range
308c2ecf20Sopenharmony_ci *
318c2ecf20Sopenharmony_ci * R_lim = 0.8588 * R_full + 16
328c2ecf20Sopenharmony_ci * G_lim = 0.8588 * G_full + 16
338c2ecf20Sopenharmony_ci * B_lim = 0.8588 * B_full + 16
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params rgbf2rgbl = {
368c2ecf20Sopenharmony_ci	.coeff = {
378c2ecf20Sopenharmony_ci		{  220,    0,    0, },
388c2ecf20Sopenharmony_ci		{    0,  220,    0, },
398c2ecf20Sopenharmony_ci		{    0,    0,  220, },
408c2ecf20Sopenharmony_ci	},
418c2ecf20Sopenharmony_ci	.offset = { 64, 64, 64, },
428c2ecf20Sopenharmony_ci	.scale = 1,
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/*
468c2ecf20Sopenharmony_ci * RGB limited-range to RGB full-range
478c2ecf20Sopenharmony_ci *
488c2ecf20Sopenharmony_ci * R_full = 1.1644 * (R_lim - 16)
498c2ecf20Sopenharmony_ci * G_full = 1.1644 * (G_lim - 16)
508c2ecf20Sopenharmony_ci * B_full = 1.1644 * (B_lim - 16)
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params rgbl2rgbf = {
538c2ecf20Sopenharmony_ci	.coeff = {
548c2ecf20Sopenharmony_ci		{  149,    0,    0, },
558c2ecf20Sopenharmony_ci		{    0,  149,    0, },
568c2ecf20Sopenharmony_ci		{    0,    0,  149, },
578c2ecf20Sopenharmony_ci	},
588c2ecf20Sopenharmony_ci	.offset = { -37, -37, -37, },
598c2ecf20Sopenharmony_ci	.scale = 2,
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/*
638c2ecf20Sopenharmony_ci * YUV full-range to YUV limited-range
648c2ecf20Sopenharmony_ci *
658c2ecf20Sopenharmony_ci * Y_lim  = 0.8588 * Y_full + 16
668c2ecf20Sopenharmony_ci * Cb_lim = 0.8784 * (Cb_full - 128) + 128
678c2ecf20Sopenharmony_ci * Cr_lim = 0.8784 * (Cr_full - 128) + 128
688c2ecf20Sopenharmony_ci */
698c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params yuvf2yuvl = {
708c2ecf20Sopenharmony_ci	.coeff = {
718c2ecf20Sopenharmony_ci		{  220,    0,    0, },
728c2ecf20Sopenharmony_ci		{    0,  225,    0, },
738c2ecf20Sopenharmony_ci		{    0,    0,  225, },
748c2ecf20Sopenharmony_ci	},
758c2ecf20Sopenharmony_ci	.offset = { 64, 62, 62, },
768c2ecf20Sopenharmony_ci	.scale = 1,
778c2ecf20Sopenharmony_ci	.sat = true,
788c2ecf20Sopenharmony_ci};
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/*
818c2ecf20Sopenharmony_ci * YUV limited-range to YUV full-range
828c2ecf20Sopenharmony_ci *
838c2ecf20Sopenharmony_ci * Y_full  = 1.1644 * (Y_lim - 16)
848c2ecf20Sopenharmony_ci * Cb_full = 1.1384 * (Cb_lim - 128) + 128
858c2ecf20Sopenharmony_ci * Cr_full = 1.1384 * (Cr_lim - 128) + 128
868c2ecf20Sopenharmony_ci */
878c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params yuvl2yuvf = {
888c2ecf20Sopenharmony_ci	.coeff = {
898c2ecf20Sopenharmony_ci		{  149,    0,    0, },
908c2ecf20Sopenharmony_ci		{    0,  146,    0, },
918c2ecf20Sopenharmony_ci		{    0,    0,  146, },
928c2ecf20Sopenharmony_ci	},
938c2ecf20Sopenharmony_ci	.offset = { -37, -35, -35, },
948c2ecf20Sopenharmony_ci	.scale = 2,
958c2ecf20Sopenharmony_ci};
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params *rgb2rgb[] = {
988c2ecf20Sopenharmony_ci	&identity,
998c2ecf20Sopenharmony_ci	&rgbf2rgbl,
1008c2ecf20Sopenharmony_ci	&rgbl2rgbf,
1018c2ecf20Sopenharmony_ci	&identity,
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params *yuv2yuv[] = {
1058c2ecf20Sopenharmony_ci	&identity,
1068c2ecf20Sopenharmony_ci	&yuvf2yuvl,
1078c2ecf20Sopenharmony_ci	&yuvl2yuvf,
1088c2ecf20Sopenharmony_ci	&identity,
1098c2ecf20Sopenharmony_ci};
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/*
1128c2ecf20Sopenharmony_ci * BT.601 RGB full-range to YUV full-range
1138c2ecf20Sopenharmony_ci *
1148c2ecf20Sopenharmony_ci * Y =  .2990 * R + .5870 * G + .1140 * B
1158c2ecf20Sopenharmony_ci * U = -.1687 * R - .3313 * G + .5000 * B + 128
1168c2ecf20Sopenharmony_ci * V =  .5000 * R - .4187 * G - .0813 * B + 128
1178c2ecf20Sopenharmony_ci */
1188c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params rgbf2yuvf_601 = {
1198c2ecf20Sopenharmony_ci	.coeff = {
1208c2ecf20Sopenharmony_ci		{   77,  150,   29, },
1218c2ecf20Sopenharmony_ci		{  -43,  -85,  128, },
1228c2ecf20Sopenharmony_ci		{  128, -107,  -21, },
1238c2ecf20Sopenharmony_ci	},
1248c2ecf20Sopenharmony_ci	.offset = { 0, 512, 512, },
1258c2ecf20Sopenharmony_ci	.scale = 1,
1268c2ecf20Sopenharmony_ci};
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci/* BT.601 RGB full-range to YUV limited-range */
1298c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params rgbf2yuvl_601 = {
1308c2ecf20Sopenharmony_ci	.coeff = {
1318c2ecf20Sopenharmony_ci		{   66,  129,   25, },
1328c2ecf20Sopenharmony_ci		{  -38,  -74,  112, },
1338c2ecf20Sopenharmony_ci		{  112,  -94,  -18, },
1348c2ecf20Sopenharmony_ci	},
1358c2ecf20Sopenharmony_ci	.offset = { 64, 512, 512, },
1368c2ecf20Sopenharmony_ci	.scale = 1,
1378c2ecf20Sopenharmony_ci	.sat = true,
1388c2ecf20Sopenharmony_ci};
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci/* BT.601 RGB limited-range to YUV full-range */
1418c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params rgbl2yuvf_601 = {
1428c2ecf20Sopenharmony_ci	.coeff = {
1438c2ecf20Sopenharmony_ci		{   89,  175,   34, },
1448c2ecf20Sopenharmony_ci		{  -50,  -99,  149, },
1458c2ecf20Sopenharmony_ci		{  149, -125,  -24, },
1468c2ecf20Sopenharmony_ci	},
1478c2ecf20Sopenharmony_ci	.offset = { -75, 512, 512, },
1488c2ecf20Sopenharmony_ci	.scale = 1,
1498c2ecf20Sopenharmony_ci};
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci/* BT.601 RGB limited-range to YUV limited-range */
1528c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params rgbl2yuvl_601 = {
1538c2ecf20Sopenharmony_ci	.coeff = {
1548c2ecf20Sopenharmony_ci		{   77,  150,   29, },
1558c2ecf20Sopenharmony_ci		{  -44,  -87,  131, },
1568c2ecf20Sopenharmony_ci		{  131, -110,  -21, },
1578c2ecf20Sopenharmony_ci	},
1588c2ecf20Sopenharmony_ci	.offset = { 0, 512, 512, },
1598c2ecf20Sopenharmony_ci	.scale = 1,
1608c2ecf20Sopenharmony_ci	.sat = true,
1618c2ecf20Sopenharmony_ci};
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci/*
1648c2ecf20Sopenharmony_ci * BT.601 YUV full-range to RGB full-range
1658c2ecf20Sopenharmony_ci *
1668c2ecf20Sopenharmony_ci * R = 1. * Y +      0 * (Cb - 128) + 1.4020 * (Cr - 128)
1678c2ecf20Sopenharmony_ci * G = 1. * Y -  .3441 * (Cb - 128) -  .7141 * (Cr - 128)
1688c2ecf20Sopenharmony_ci * B = 1. * Y + 1.7720 * (Cb - 128) +      0 * (Cr - 128)
1698c2ecf20Sopenharmony_ci *
1708c2ecf20Sopenharmony_ci * equivalently (factoring out the offsets):
1718c2ecf20Sopenharmony_ci *
1728c2ecf20Sopenharmony_ci * R = 1. * Y  +      0 * Cb + 1.4020 * Cr - 179.456
1738c2ecf20Sopenharmony_ci * G = 1. * Y  -  .3441 * Cb -  .7141 * Cr + 135.450
1748c2ecf20Sopenharmony_ci * B = 1. * Y  + 1.7720 * Cb +      0 * Cr - 226.816
1758c2ecf20Sopenharmony_ci */
1768c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params yuvf2rgbf_601 = {
1778c2ecf20Sopenharmony_ci	.coeff = {
1788c2ecf20Sopenharmony_ci		{  128,    0,  179, },
1798c2ecf20Sopenharmony_ci		{  128,  -44,  -91, },
1808c2ecf20Sopenharmony_ci		{  128,  227,    0, },
1818c2ecf20Sopenharmony_ci	},
1828c2ecf20Sopenharmony_ci	.offset = { -359, 271, -454, },
1838c2ecf20Sopenharmony_ci	.scale = 2,
1848c2ecf20Sopenharmony_ci};
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci/* BT.601 YUV full-range to RGB limited-range */
1878c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params yuvf2rgbl_601 = {
1888c2ecf20Sopenharmony_ci	.coeff = {
1898c2ecf20Sopenharmony_ci		{  110,    0,  154, },
1908c2ecf20Sopenharmony_ci		{  110,  -38,  -78, },
1918c2ecf20Sopenharmony_ci		{  110,  195,    0, },
1928c2ecf20Sopenharmony_ci	},
1938c2ecf20Sopenharmony_ci	.offset = { -276, 265, -358, },
1948c2ecf20Sopenharmony_ci	.scale = 2,
1958c2ecf20Sopenharmony_ci};
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci/* BT.601 YUV limited-range to RGB full-range */
1988c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params yuvl2rgbf_601 = {
1998c2ecf20Sopenharmony_ci	.coeff = {
2008c2ecf20Sopenharmony_ci		{   75,    0,  102, },
2018c2ecf20Sopenharmony_ci		{   75,  -25,  -52, },
2028c2ecf20Sopenharmony_ci		{   75,  129,    0, },
2038c2ecf20Sopenharmony_ci	},
2048c2ecf20Sopenharmony_ci	.offset = { -223, 136, -277, },
2058c2ecf20Sopenharmony_ci	.scale = 3,
2068c2ecf20Sopenharmony_ci};
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci/* BT.601 YUV limited-range to RGB limited-range */
2098c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params yuvl2rgbl_601 = {
2108c2ecf20Sopenharmony_ci	.coeff = {
2118c2ecf20Sopenharmony_ci		{  128,    0,  175, },
2128c2ecf20Sopenharmony_ci		{  128,  -43,  -89, },
2138c2ecf20Sopenharmony_ci		{  128,  222,    0, },
2148c2ecf20Sopenharmony_ci	},
2158c2ecf20Sopenharmony_ci	.offset = { -351, 265, -443, },
2168c2ecf20Sopenharmony_ci	.scale = 2,
2178c2ecf20Sopenharmony_ci};
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params *rgb2yuv_601[] = {
2208c2ecf20Sopenharmony_ci	&rgbf2yuvf_601,
2218c2ecf20Sopenharmony_ci	&rgbf2yuvl_601,
2228c2ecf20Sopenharmony_ci	&rgbl2yuvf_601,
2238c2ecf20Sopenharmony_ci	&rgbl2yuvl_601,
2248c2ecf20Sopenharmony_ci};
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params *yuv2rgb_601[] = {
2278c2ecf20Sopenharmony_ci	&yuvf2rgbf_601,
2288c2ecf20Sopenharmony_ci	&yuvf2rgbl_601,
2298c2ecf20Sopenharmony_ci	&yuvl2rgbf_601,
2308c2ecf20Sopenharmony_ci	&yuvl2rgbl_601,
2318c2ecf20Sopenharmony_ci};
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci/*
2348c2ecf20Sopenharmony_ci * REC.709 encoding from RGB full range to YUV full range:
2358c2ecf20Sopenharmony_ci *
2368c2ecf20Sopenharmony_ci * Y =  .2126 * R + .7152 * G + .0722 * B
2378c2ecf20Sopenharmony_ci * U = -.1146 * R - .3854 * G + .5000 * B + 128
2388c2ecf20Sopenharmony_ci * V =  .5000 * R - .4542 * G - .0458 * B + 128
2398c2ecf20Sopenharmony_ci */
2408c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params rgbf2yuvf_709 = {
2418c2ecf20Sopenharmony_ci	.coeff = {
2428c2ecf20Sopenharmony_ci		{  54,  183,  19 },
2438c2ecf20Sopenharmony_ci		{ -29,  -99, 128 },
2448c2ecf20Sopenharmony_ci		{ 128, -116, -12 },
2458c2ecf20Sopenharmony_ci	},
2468c2ecf20Sopenharmony_ci	.offset = { 0, 512, 512 },
2478c2ecf20Sopenharmony_ci	.scale = 1,
2488c2ecf20Sopenharmony_ci};
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci/* Rec.709 RGB full-range to YUV limited-range */
2518c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params rgbf2yuvl_709 = {
2528c2ecf20Sopenharmony_ci	.coeff = {
2538c2ecf20Sopenharmony_ci		{   47,  157,   16, },
2548c2ecf20Sopenharmony_ci		{  -26,  -87,  112, },
2558c2ecf20Sopenharmony_ci		{  112, -102,  -10, },
2568c2ecf20Sopenharmony_ci	},
2578c2ecf20Sopenharmony_ci	.offset = { 64, 512, 512, },
2588c2ecf20Sopenharmony_ci	.scale = 1,
2598c2ecf20Sopenharmony_ci	.sat = true,
2608c2ecf20Sopenharmony_ci};
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci/* Rec.709 RGB limited-range to YUV full-range */
2638c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params rgbl2yuvf_709 = {
2648c2ecf20Sopenharmony_ci	.coeff = {
2658c2ecf20Sopenharmony_ci		{   63,  213,   22, },
2668c2ecf20Sopenharmony_ci		{  -34, -115,  149, },
2678c2ecf20Sopenharmony_ci		{  149, -135,  -14, },
2688c2ecf20Sopenharmony_ci	},
2698c2ecf20Sopenharmony_ci	.offset = { -75, 512, 512, },
2708c2ecf20Sopenharmony_ci	.scale = 1,
2718c2ecf20Sopenharmony_ci};
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci/* Rec.709 RGB limited-range to YUV limited-range */
2748c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params rgbl2yuvl_709 = {
2758c2ecf20Sopenharmony_ci	.coeff = {
2768c2ecf20Sopenharmony_ci		{   54,  183,   18, },
2778c2ecf20Sopenharmony_ci		{  -30, -101,  131, },
2788c2ecf20Sopenharmony_ci		{  131, -119,  -12, },
2798c2ecf20Sopenharmony_ci	},
2808c2ecf20Sopenharmony_ci	.offset = { 0, 512, 512, },
2818c2ecf20Sopenharmony_ci	.scale = 1,
2828c2ecf20Sopenharmony_ci	.sat = true,
2838c2ecf20Sopenharmony_ci};
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci/*
2868c2ecf20Sopenharmony_ci * Inverse REC.709 encoding from YUV full range to RGB full range:
2878c2ecf20Sopenharmony_ci *
2888c2ecf20Sopenharmony_ci * R = 1. * Y +      0 * (Cb - 128) + 1.5748 * (Cr - 128)
2898c2ecf20Sopenharmony_ci * G = 1. * Y -  .1873 * (Cb - 128) -  .4681 * (Cr - 128)
2908c2ecf20Sopenharmony_ci * B = 1. * Y + 1.8556 * (Cb - 128) +      0 * (Cr - 128)
2918c2ecf20Sopenharmony_ci *
2928c2ecf20Sopenharmony_ci * equivalently (factoring out the offsets):
2938c2ecf20Sopenharmony_ci *
2948c2ecf20Sopenharmony_ci * R = 1. * Y  +      0 * Cb + 1.5748 * Cr - 201.574
2958c2ecf20Sopenharmony_ci * G = 1. * Y  -  .1873 * Cb -  .4681 * Cr +  83.891
2968c2ecf20Sopenharmony_ci * B = 1. * Y  + 1.8556 * Cb +      0 * Cr - 237.517
2978c2ecf20Sopenharmony_ci */
2988c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params yuvf2rgbf_709 = {
2998c2ecf20Sopenharmony_ci	.coeff = {
3008c2ecf20Sopenharmony_ci		{  128,   0, 202 },
3018c2ecf20Sopenharmony_ci		{  128, -24, -60 },
3028c2ecf20Sopenharmony_ci		{  128, 238,   0 },
3038c2ecf20Sopenharmony_ci	},
3048c2ecf20Sopenharmony_ci	.offset = { -403, 168, -475 },
3058c2ecf20Sopenharmony_ci	.scale = 2,
3068c2ecf20Sopenharmony_ci};
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci/* Rec.709 YUV full-range to RGB limited-range */
3098c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params yuvf2rgbl_709 = {
3108c2ecf20Sopenharmony_ci	.coeff = {
3118c2ecf20Sopenharmony_ci		{  110,    0,  173, },
3128c2ecf20Sopenharmony_ci		{  110,  -21,  -51, },
3138c2ecf20Sopenharmony_ci		{  110,  204,    0, },
3148c2ecf20Sopenharmony_ci	},
3158c2ecf20Sopenharmony_ci	.offset = { -314, 176, -376, },
3168c2ecf20Sopenharmony_ci	.scale = 2,
3178c2ecf20Sopenharmony_ci};
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci/* Rec.709 YUV limited-range to RGB full-range */
3208c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params yuvl2rgbf_709 = {
3218c2ecf20Sopenharmony_ci	.coeff = {
3228c2ecf20Sopenharmony_ci		{   75,    0,  115, },
3238c2ecf20Sopenharmony_ci		{   75,  -14,  -34, },
3248c2ecf20Sopenharmony_ci		{   75,  135,    0, },
3258c2ecf20Sopenharmony_ci	},
3268c2ecf20Sopenharmony_ci	.offset = { -248, 77, -289, },
3278c2ecf20Sopenharmony_ci	.scale = 3,
3288c2ecf20Sopenharmony_ci};
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci/* Rec.709 YUV limited-range to RGB limited-range */
3318c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params yuvl2rgbl_709 = {
3328c2ecf20Sopenharmony_ci	.coeff = {
3338c2ecf20Sopenharmony_ci		{  128,    0,  197, },
3348c2ecf20Sopenharmony_ci		{  128,  -23,  -59, },
3358c2ecf20Sopenharmony_ci		{  128,  232,    0, },
3368c2ecf20Sopenharmony_ci	},
3378c2ecf20Sopenharmony_ci	.offset = { -394, 164, -464, },
3388c2ecf20Sopenharmony_ci	.scale = 2,
3398c2ecf20Sopenharmony_ci};
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params *rgb2yuv_709[] = {
3428c2ecf20Sopenharmony_ci	&rgbf2yuvf_709,
3438c2ecf20Sopenharmony_ci	&rgbf2yuvl_709,
3448c2ecf20Sopenharmony_ci	&rgbl2yuvf_709,
3458c2ecf20Sopenharmony_ci	&rgbl2yuvl_709,
3468c2ecf20Sopenharmony_ci};
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_cistatic const struct ipu_ic_csc_params *yuv2rgb_709[] = {
3498c2ecf20Sopenharmony_ci	&yuvf2rgbf_709,
3508c2ecf20Sopenharmony_ci	&yuvf2rgbl_709,
3518c2ecf20Sopenharmony_ci	&yuvl2rgbf_709,
3528c2ecf20Sopenharmony_ci	&yuvl2rgbl_709,
3538c2ecf20Sopenharmony_ci};
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_cistatic int calc_csc_coeffs(struct ipu_ic_csc *csc)
3568c2ecf20Sopenharmony_ci{
3578c2ecf20Sopenharmony_ci	const struct ipu_ic_csc_params **params_tbl;
3588c2ecf20Sopenharmony_ci	int tbl_idx;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	tbl_idx = (QUANT_MAP(csc->in_cs.quant) << 1) |
3618c2ecf20Sopenharmony_ci		QUANT_MAP(csc->out_cs.quant);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	if (csc->in_cs.cs == csc->out_cs.cs) {
3648c2ecf20Sopenharmony_ci		csc->params = (csc->in_cs.cs == IPUV3_COLORSPACE_YUV) ?
3658c2ecf20Sopenharmony_ci			*yuv2yuv[tbl_idx] : *rgb2rgb[tbl_idx];
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci		return 0;
3688c2ecf20Sopenharmony_ci	}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	/* YUV <-> RGB encoding is required */
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	switch (csc->out_cs.enc) {
3738c2ecf20Sopenharmony_ci	case V4L2_YCBCR_ENC_601:
3748c2ecf20Sopenharmony_ci		params_tbl = (csc->in_cs.cs == IPUV3_COLORSPACE_YUV) ?
3758c2ecf20Sopenharmony_ci			yuv2rgb_601 : rgb2yuv_601;
3768c2ecf20Sopenharmony_ci		break;
3778c2ecf20Sopenharmony_ci	case V4L2_YCBCR_ENC_709:
3788c2ecf20Sopenharmony_ci		params_tbl = (csc->in_cs.cs == IPUV3_COLORSPACE_YUV) ?
3798c2ecf20Sopenharmony_ci			yuv2rgb_709 : rgb2yuv_709;
3808c2ecf20Sopenharmony_ci		break;
3818c2ecf20Sopenharmony_ci	default:
3828c2ecf20Sopenharmony_ci		return -ENOTSUPP;
3838c2ecf20Sopenharmony_ci	}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	csc->params = *params_tbl[tbl_idx];
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	return 0;
3888c2ecf20Sopenharmony_ci}
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ciint __ipu_ic_calc_csc(struct ipu_ic_csc *csc)
3918c2ecf20Sopenharmony_ci{
3928c2ecf20Sopenharmony_ci	return calc_csc_coeffs(csc);
3938c2ecf20Sopenharmony_ci}
3948c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__ipu_ic_calc_csc);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ciint ipu_ic_calc_csc(struct ipu_ic_csc *csc,
3978c2ecf20Sopenharmony_ci		    enum v4l2_ycbcr_encoding in_enc,
3988c2ecf20Sopenharmony_ci		    enum v4l2_quantization in_quant,
3998c2ecf20Sopenharmony_ci		    enum ipu_color_space in_cs,
4008c2ecf20Sopenharmony_ci		    enum v4l2_ycbcr_encoding out_enc,
4018c2ecf20Sopenharmony_ci		    enum v4l2_quantization out_quant,
4028c2ecf20Sopenharmony_ci		    enum ipu_color_space out_cs)
4038c2ecf20Sopenharmony_ci{
4048c2ecf20Sopenharmony_ci	ipu_ic_fill_colorspace(&csc->in_cs, in_enc, in_quant, in_cs);
4058c2ecf20Sopenharmony_ci	ipu_ic_fill_colorspace(&csc->out_cs, out_enc, out_quant, out_cs);
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	return __ipu_ic_calc_csc(csc);
4088c2ecf20Sopenharmony_ci}
4098c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_ic_calc_csc);
410