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