Merge tag 'arc-5.2-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/vgupta/arc
[linux-2.6-microblaze.git] / drivers / media / platform / ti-vpe / csc.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Color space converter library
4  *
5  * Copyright (c) 2013 Texas Instruments Inc.
6  *
7  * David Griego, <dagriego@biglakesoftware.com>
8  * Dale Farnsworth, <dale@farnsworth.org>
9  * Archit Taneja, <archit@ti.com>
10  */
11
12 #include <linux/err.h>
13 #include <linux/io.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/videodev2.h>
18
19 #include "csc.h"
20
21 /*
22  * 16 coefficients in the order:
23  * a0, b0, c0, a1, b1, c1, a2, b2, c2, d0, d1, d2
24  * (we may need to pass non-default values from user space later on, we might
25  * need to make the coefficient struct more easy to populate)
26  */
27 struct colorspace_coeffs {
28         u16     sd[12];
29         u16     hd[12];
30 };
31
32 /* VIDEO_RANGE: limited range, GRAPHICS_RANGE: full range */
33 #define CSC_COEFFS_VIDEO_RANGE_Y2R      0
34 #define CSC_COEFFS_GRAPHICS_RANGE_Y2R   1
35 #define CSC_COEFFS_VIDEO_RANGE_R2Y      2
36 #define CSC_COEFFS_GRAPHICS_RANGE_R2Y   3
37
38 /* default colorspace coefficients */
39 static struct colorspace_coeffs colorspace_coeffs[4] = {
40         [CSC_COEFFS_VIDEO_RANGE_Y2R] = {
41                 {
42                         /* SDTV */
43                         0x0400, 0x0000, 0x057D, 0x0400, 0x1EA7, 0x1D35,
44                         0x0400, 0x06EF, 0x1FFE, 0x0D40, 0x0210, 0x0C88,
45                 },
46                 {
47                         /* HDTV */
48                         0x0400, 0x0000, 0x0629, 0x0400, 0x1F45, 0x1E2B,
49                         0x0400, 0x0742, 0x0000, 0x0CEC, 0x0148, 0x0C60,
50                 },
51         },
52         [CSC_COEFFS_GRAPHICS_RANGE_Y2R] = {
53                 {
54                         /* SDTV */
55                         0x04A8, 0x1FFE, 0x0662, 0x04A8, 0x1E6F, 0x1CBF,
56                         0x04A8, 0x0812, 0x1FFF, 0x0C84, 0x0220, 0x0BAC,
57                 },
58                 {
59                         /* HDTV */
60                         0x04A8, 0x0000, 0x072C, 0x04A8, 0x1F26, 0x1DDE,
61                         0x04A8, 0x0873, 0x0000, 0x0C20, 0x0134, 0x0B7C,
62                 },
63         },
64         [CSC_COEFFS_VIDEO_RANGE_R2Y] = {
65                 {
66                         /* SDTV */
67                         0x0132, 0x0259, 0x0075, 0x1F50, 0x1EA5, 0x020B,
68                         0x020B, 0x1E4A, 0x1FAB, 0x0000, 0x0200, 0x0200,
69                 },
70                 {
71                         /* HDTV */
72                         0x00DA, 0x02DC, 0x004A, 0x1F88, 0x1E6C, 0x020C,
73                         0x020C, 0x1E24, 0x1FD0, 0x0000, 0x0200, 0x0200,
74                 },
75         },
76         [CSC_COEFFS_GRAPHICS_RANGE_R2Y] = {
77                 {
78                         /* SDTV */
79                         0x0107, 0x0204, 0x0064, 0x1F68, 0x1ED6, 0x01C2,
80                         0x01C2, 0x1E87, 0x1FB7, 0x0040, 0x0200, 0x0200,
81                 },
82                 {
83                         /* HDTV */
84                         0x04A8, 0x0000, 0x072C, 0x04A8, 0x1F26, 0x1DDE,
85                         0x04A8, 0x0873, 0x0000, 0x0C20, 0x0134, 0x0B7C,
86                 },
87         },
88 };
89
90 void csc_dump_regs(struct csc_data *csc)
91 {
92         struct device *dev = &csc->pdev->dev;
93
94 #define DUMPREG(r) dev_dbg(dev, "%-35s %08x\n", #r, \
95         ioread32(csc->base + CSC_##r))
96
97         dev_dbg(dev, "CSC Registers @ %pa:\n", &csc->res->start);
98
99         DUMPREG(CSC00);
100         DUMPREG(CSC01);
101         DUMPREG(CSC02);
102         DUMPREG(CSC03);
103         DUMPREG(CSC04);
104         DUMPREG(CSC05);
105
106 #undef DUMPREG
107 }
108 EXPORT_SYMBOL(csc_dump_regs);
109
110 void csc_set_coeff_bypass(struct csc_data *csc, u32 *csc_reg5)
111 {
112         *csc_reg5 |= CSC_BYPASS;
113 }
114 EXPORT_SYMBOL(csc_set_coeff_bypass);
115
116 /*
117  * set the color space converter coefficient shadow register values
118  */
119 void csc_set_coeff(struct csc_data *csc, u32 *csc_reg0,
120                 enum v4l2_colorspace src_colorspace,
121                 enum v4l2_colorspace dst_colorspace)
122 {
123         u32 *csc_reg5 = csc_reg0 + 5;
124         u32 *shadow_csc = csc_reg0;
125         struct colorspace_coeffs *sd_hd_coeffs;
126         u16 *coeff, *end_coeff;
127         enum v4l2_colorspace yuv_colorspace;
128         int sel = 0;
129
130         /*
131          * support only graphics data range(full range) for now, a control ioctl
132          * would be nice here
133          */
134         /* Y2R */
135         if (dst_colorspace == V4L2_COLORSPACE_SRGB &&
136                         (src_colorspace == V4L2_COLORSPACE_SMPTE170M ||
137                         src_colorspace == V4L2_COLORSPACE_REC709)) {
138                 /* Y2R */
139                 sel = 1;
140                 yuv_colorspace = src_colorspace;
141         } else if ((dst_colorspace == V4L2_COLORSPACE_SMPTE170M ||
142                         dst_colorspace == V4L2_COLORSPACE_REC709) &&
143                         src_colorspace == V4L2_COLORSPACE_SRGB) {
144                 /* R2Y */
145                 sel = 3;
146                 yuv_colorspace = dst_colorspace;
147         } else {
148                 *csc_reg5 |= CSC_BYPASS;
149                 return;
150         }
151
152         sd_hd_coeffs = &colorspace_coeffs[sel];
153
154         /* select between SD or HD coefficients */
155         if (yuv_colorspace == V4L2_COLORSPACE_SMPTE170M)
156                 coeff = sd_hd_coeffs->sd;
157         else
158                 coeff = sd_hd_coeffs->hd;
159
160         end_coeff = coeff + 12;
161
162         for (; coeff < end_coeff; coeff += 2)
163                 *shadow_csc++ = (*(coeff + 1) << 16) | *coeff;
164 }
165 EXPORT_SYMBOL(csc_set_coeff);
166
167 struct csc_data *csc_create(struct platform_device *pdev, const char *res_name)
168 {
169         struct csc_data *csc;
170
171         dev_dbg(&pdev->dev, "csc_create\n");
172
173         csc = devm_kzalloc(&pdev->dev, sizeof(*csc), GFP_KERNEL);
174         if (!csc) {
175                 dev_err(&pdev->dev, "couldn't alloc csc_data\n");
176                 return ERR_PTR(-ENOMEM);
177         }
178
179         csc->pdev = pdev;
180
181         csc->res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
182                                                 res_name);
183         if (csc->res == NULL) {
184                 dev_err(&pdev->dev, "missing '%s' platform resources data\n",
185                         res_name);
186                 return ERR_PTR(-ENODEV);
187         }
188
189         csc->base = devm_ioremap_resource(&pdev->dev, csc->res);
190         if (IS_ERR(csc->base)) {
191                 dev_err(&pdev->dev, "failed to ioremap\n");
192                 return ERR_CAST(csc->base);
193         }
194
195         return csc;
196 }
197 EXPORT_SYMBOL(csc_create);
198
199 MODULE_DESCRIPTION("TI VIP/VPE Color Space Converter");
200 MODULE_AUTHOR("Texas Instruments Inc.");
201 MODULE_LICENSE("GPL v2");