1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Color space converter library
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2013 Texas Instruments Inc.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * David Griego, <dagriego@biglakesoftware.com>
8*4882a593Smuzhiyun * Dale Farnsworth, <dale@farnsworth.org>
9*4882a593Smuzhiyun * Archit Taneja, <archit@ti.com>
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/err.h>
13*4882a593Smuzhiyun #include <linux/io.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/platform_device.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun #include <linux/videodev2.h>
18*4882a593Smuzhiyun #include <media/v4l2-common.h>
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #include "csc.h"
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun /*
23*4882a593Smuzhiyun * 12 coefficients in the order:
24*4882a593Smuzhiyun * a0, b0, c0, a1, b1, c1, a2, b2, c2, d0, d1, d2
25*4882a593Smuzhiyun */
26*4882a593Smuzhiyun struct quantization {
27*4882a593Smuzhiyun u16 coeff[12];
28*4882a593Smuzhiyun };
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun struct colorspace {
31*4882a593Smuzhiyun struct quantization limited;
32*4882a593Smuzhiyun struct quantization full;
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun struct encoding_direction {
36*4882a593Smuzhiyun struct colorspace r601;
37*4882a593Smuzhiyun struct colorspace r709;
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun struct csc_coeffs {
41*4882a593Smuzhiyun struct encoding_direction y2r;
42*4882a593Smuzhiyun struct encoding_direction r2y;
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun /* default colorspace coefficients */
46*4882a593Smuzhiyun static struct csc_coeffs csc_coeffs = {
47*4882a593Smuzhiyun .y2r = {
48*4882a593Smuzhiyun .r601 = {
49*4882a593Smuzhiyun .limited = {
50*4882a593Smuzhiyun { /* SDTV */
51*4882a593Smuzhiyun 0x0400, 0x0000, 0x057D, 0x0400, 0x1EA7, 0x1D35,
52*4882a593Smuzhiyun 0x0400, 0x06EF, 0x1FFE, 0x0D40, 0x0210, 0x0C88,
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun },
55*4882a593Smuzhiyun .full = {
56*4882a593Smuzhiyun { /* SDTV */
57*4882a593Smuzhiyun 0x04A8, 0x1FFE, 0x0662, 0x04A8, 0x1E6F, 0x1CBF,
58*4882a593Smuzhiyun 0x04A8, 0x0812, 0x1FFF, 0x0C84, 0x0220, 0x0BAC,
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun },
61*4882a593Smuzhiyun },
62*4882a593Smuzhiyun .r709 = {
63*4882a593Smuzhiyun .limited = {
64*4882a593Smuzhiyun { /* HDTV */
65*4882a593Smuzhiyun 0x0400, 0x0000, 0x0629, 0x0400, 0x1F45, 0x1E2B,
66*4882a593Smuzhiyun 0x0400, 0x0742, 0x0000, 0x0CEC, 0x0148, 0x0C60,
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun },
69*4882a593Smuzhiyun .full = {
70*4882a593Smuzhiyun { /* HDTV */
71*4882a593Smuzhiyun 0x04A8, 0x0000, 0x072C, 0x04A8, 0x1F26, 0x1DDE,
72*4882a593Smuzhiyun 0x04A8, 0x0873, 0x0000, 0x0C20, 0x0134, 0x0B7C,
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun },
75*4882a593Smuzhiyun },
76*4882a593Smuzhiyun },
77*4882a593Smuzhiyun .r2y = {
78*4882a593Smuzhiyun .r601 = {
79*4882a593Smuzhiyun .limited = {
80*4882a593Smuzhiyun { /* SDTV */
81*4882a593Smuzhiyun 0x0132, 0x0259, 0x0075, 0x1F50, 0x1EA5, 0x020B,
82*4882a593Smuzhiyun 0x020B, 0x1E4A, 0x1FAB, 0x0000, 0x0200, 0x0200,
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun },
85*4882a593Smuzhiyun .full = {
86*4882a593Smuzhiyun { /* SDTV */
87*4882a593Smuzhiyun 0x0107, 0x0204, 0x0064, 0x1F68, 0x1ED6, 0x01C2,
88*4882a593Smuzhiyun 0x01C2, 0x1E87, 0x1FB7, 0x0040, 0x0200, 0x0200,
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun },
91*4882a593Smuzhiyun },
92*4882a593Smuzhiyun .r709 = {
93*4882a593Smuzhiyun .limited = {
94*4882a593Smuzhiyun { /* HDTV */
95*4882a593Smuzhiyun 0x00DA, 0x02DC, 0x004A, 0x1F88, 0x1E6C, 0x020C,
96*4882a593Smuzhiyun 0x020C, 0x1E24, 0x1FD0, 0x0000, 0x0200, 0x0200,
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun },
99*4882a593Smuzhiyun .full = {
100*4882a593Smuzhiyun { /* HDTV */
101*4882a593Smuzhiyun 0x00bb, 0x0275, 0x003f, 0x1f99, 0x1ea5, 0x01c2,
102*4882a593Smuzhiyun 0x01c2, 0x1e67, 0x1fd7, 0x0040, 0x0200, 0x0200,
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun },
105*4882a593Smuzhiyun },
106*4882a593Smuzhiyun },
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun };
109*4882a593Smuzhiyun
csc_dump_regs(struct csc_data * csc)110*4882a593Smuzhiyun void csc_dump_regs(struct csc_data *csc)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun struct device *dev = &csc->pdev->dev;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun #define DUMPREG(r) dev_dbg(dev, "%-35s %08x\n", #r, \
115*4882a593Smuzhiyun ioread32(csc->base + CSC_##r))
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun dev_dbg(dev, "CSC Registers @ %pa:\n", &csc->res->start);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun DUMPREG(CSC00);
120*4882a593Smuzhiyun DUMPREG(CSC01);
121*4882a593Smuzhiyun DUMPREG(CSC02);
122*4882a593Smuzhiyun DUMPREG(CSC03);
123*4882a593Smuzhiyun DUMPREG(CSC04);
124*4882a593Smuzhiyun DUMPREG(CSC05);
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun #undef DUMPREG
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun EXPORT_SYMBOL(csc_dump_regs);
129*4882a593Smuzhiyun
csc_set_coeff_bypass(struct csc_data * csc,u32 * csc_reg5)130*4882a593Smuzhiyun void csc_set_coeff_bypass(struct csc_data *csc, u32 *csc_reg5)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun *csc_reg5 |= CSC_BYPASS;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun EXPORT_SYMBOL(csc_set_coeff_bypass);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /*
137*4882a593Smuzhiyun * set the color space converter coefficient shadow register values
138*4882a593Smuzhiyun */
csc_set_coeff(struct csc_data * csc,u32 * csc_reg0,struct v4l2_format * src_fmt,struct v4l2_format * dst_fmt)139*4882a593Smuzhiyun void csc_set_coeff(struct csc_data *csc, u32 *csc_reg0,
140*4882a593Smuzhiyun struct v4l2_format *src_fmt, struct v4l2_format *dst_fmt)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun u32 *csc_reg5 = csc_reg0 + 5;
143*4882a593Smuzhiyun u32 *shadow_csc = csc_reg0;
144*4882a593Smuzhiyun u16 *coeff, *end_coeff;
145*4882a593Smuzhiyun const struct v4l2_pix_format *pix;
146*4882a593Smuzhiyun const struct v4l2_pix_format_mplane *mp;
147*4882a593Smuzhiyun const struct v4l2_format_info *src_finfo, *dst_finfo;
148*4882a593Smuzhiyun enum v4l2_ycbcr_encoding src_ycbcr_enc, dst_ycbcr_enc;
149*4882a593Smuzhiyun enum v4l2_quantization src_quantization, dst_quantization;
150*4882a593Smuzhiyun u32 src_pixelformat, dst_pixelformat;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun if (V4L2_TYPE_IS_MULTIPLANAR(src_fmt->type)) {
153*4882a593Smuzhiyun mp = &src_fmt->fmt.pix_mp;
154*4882a593Smuzhiyun src_pixelformat = mp->pixelformat;
155*4882a593Smuzhiyun src_ycbcr_enc = mp->ycbcr_enc;
156*4882a593Smuzhiyun src_quantization = mp->quantization;
157*4882a593Smuzhiyun } else {
158*4882a593Smuzhiyun pix = &src_fmt->fmt.pix;
159*4882a593Smuzhiyun src_pixelformat = pix->pixelformat;
160*4882a593Smuzhiyun src_ycbcr_enc = pix->ycbcr_enc;
161*4882a593Smuzhiyun src_quantization = pix->quantization;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun if (V4L2_TYPE_IS_MULTIPLANAR(dst_fmt->type)) {
165*4882a593Smuzhiyun mp = &dst_fmt->fmt.pix_mp;
166*4882a593Smuzhiyun dst_pixelformat = mp->pixelformat;
167*4882a593Smuzhiyun dst_ycbcr_enc = mp->ycbcr_enc;
168*4882a593Smuzhiyun dst_quantization = mp->quantization;
169*4882a593Smuzhiyun } else {
170*4882a593Smuzhiyun pix = &dst_fmt->fmt.pix;
171*4882a593Smuzhiyun dst_pixelformat = pix->pixelformat;
172*4882a593Smuzhiyun dst_ycbcr_enc = pix->ycbcr_enc;
173*4882a593Smuzhiyun dst_quantization = pix->quantization;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun src_finfo = v4l2_format_info(src_pixelformat);
177*4882a593Smuzhiyun dst_finfo = v4l2_format_info(dst_pixelformat);
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun if (v4l2_is_format_yuv(src_finfo) &&
180*4882a593Smuzhiyun v4l2_is_format_rgb(dst_finfo)) {
181*4882a593Smuzhiyun /* Y2R */
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun /*
184*4882a593Smuzhiyun * These are not the standard default values but are
185*4882a593Smuzhiyun * set this way for historical compatibility
186*4882a593Smuzhiyun */
187*4882a593Smuzhiyun if (src_ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT)
188*4882a593Smuzhiyun src_ycbcr_enc = V4L2_YCBCR_ENC_601;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun if (src_quantization == V4L2_QUANTIZATION_DEFAULT)
191*4882a593Smuzhiyun src_quantization = V4L2_QUANTIZATION_FULL_RANGE;
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun if (src_ycbcr_enc == V4L2_YCBCR_ENC_601) {
194*4882a593Smuzhiyun if (src_quantization == V4L2_QUANTIZATION_FULL_RANGE)
195*4882a593Smuzhiyun coeff = csc_coeffs.y2r.r601.full.coeff;
196*4882a593Smuzhiyun else
197*4882a593Smuzhiyun coeff = csc_coeffs.y2r.r601.limited.coeff;
198*4882a593Smuzhiyun } else if (src_ycbcr_enc == V4L2_YCBCR_ENC_709) {
199*4882a593Smuzhiyun if (src_quantization == V4L2_QUANTIZATION_FULL_RANGE)
200*4882a593Smuzhiyun coeff = csc_coeffs.y2r.r709.full.coeff;
201*4882a593Smuzhiyun else
202*4882a593Smuzhiyun coeff = csc_coeffs.y2r.r709.limited.coeff;
203*4882a593Smuzhiyun } else {
204*4882a593Smuzhiyun /* Should never reach this, but it keeps gcc happy */
205*4882a593Smuzhiyun coeff = csc_coeffs.y2r.r601.full.coeff;
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun } else if (v4l2_is_format_rgb(src_finfo) &&
208*4882a593Smuzhiyun v4l2_is_format_yuv(dst_finfo)) {
209*4882a593Smuzhiyun /* R2Y */
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun /*
212*4882a593Smuzhiyun * These are not the standard default values but are
213*4882a593Smuzhiyun * set this way for historical compatibility
214*4882a593Smuzhiyun */
215*4882a593Smuzhiyun if (dst_ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT)
216*4882a593Smuzhiyun dst_ycbcr_enc = V4L2_YCBCR_ENC_601;
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun if (dst_quantization == V4L2_QUANTIZATION_DEFAULT)
219*4882a593Smuzhiyun dst_quantization = V4L2_QUANTIZATION_FULL_RANGE;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun if (dst_ycbcr_enc == V4L2_YCBCR_ENC_601) {
222*4882a593Smuzhiyun if (dst_quantization == V4L2_QUANTIZATION_FULL_RANGE)
223*4882a593Smuzhiyun coeff = csc_coeffs.r2y.r601.full.coeff;
224*4882a593Smuzhiyun else
225*4882a593Smuzhiyun coeff = csc_coeffs.r2y.r601.limited.coeff;
226*4882a593Smuzhiyun } else if (dst_ycbcr_enc == V4L2_YCBCR_ENC_709) {
227*4882a593Smuzhiyun if (dst_quantization == V4L2_QUANTIZATION_FULL_RANGE)
228*4882a593Smuzhiyun coeff = csc_coeffs.r2y.r709.full.coeff;
229*4882a593Smuzhiyun else
230*4882a593Smuzhiyun coeff = csc_coeffs.r2y.r709.limited.coeff;
231*4882a593Smuzhiyun } else {
232*4882a593Smuzhiyun /* Should never reach this, but it keeps gcc happy */
233*4882a593Smuzhiyun coeff = csc_coeffs.r2y.r601.full.coeff;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun } else {
236*4882a593Smuzhiyun *csc_reg5 |= CSC_BYPASS;
237*4882a593Smuzhiyun return;
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun end_coeff = coeff + 12;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun for (; coeff < end_coeff; coeff += 2)
243*4882a593Smuzhiyun *shadow_csc++ = (*(coeff + 1) << 16) | *coeff;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun EXPORT_SYMBOL(csc_set_coeff);
246*4882a593Smuzhiyun
csc_create(struct platform_device * pdev,const char * res_name)247*4882a593Smuzhiyun struct csc_data *csc_create(struct platform_device *pdev, const char *res_name)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun struct csc_data *csc;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun dev_dbg(&pdev->dev, "csc_create\n");
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun csc = devm_kzalloc(&pdev->dev, sizeof(*csc), GFP_KERNEL);
254*4882a593Smuzhiyun if (!csc) {
255*4882a593Smuzhiyun dev_err(&pdev->dev, "couldn't alloc csc_data\n");
256*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun csc->pdev = pdev;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun csc->res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
262*4882a593Smuzhiyun res_name);
263*4882a593Smuzhiyun if (csc->res == NULL) {
264*4882a593Smuzhiyun dev_err(&pdev->dev, "missing '%s' platform resources data\n",
265*4882a593Smuzhiyun res_name);
266*4882a593Smuzhiyun return ERR_PTR(-ENODEV);
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun csc->base = devm_ioremap_resource(&pdev->dev, csc->res);
270*4882a593Smuzhiyun if (IS_ERR(csc->base)) {
271*4882a593Smuzhiyun dev_err(&pdev->dev, "failed to ioremap\n");
272*4882a593Smuzhiyun return ERR_CAST(csc->base);
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun return csc;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun EXPORT_SYMBOL(csc_create);
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun MODULE_DESCRIPTION("TI VIP/VPE Color Space Converter");
280*4882a593Smuzhiyun MODULE_AUTHOR("Texas Instruments Inc.");
281*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
282