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/platform_device.h>
18#include <linux/slab.h>
19#include <linux/videodev2.h>
20
21#include "csc.h"
22
23/*
24 * 16 coefficients in the order:
25 * a0, b0, c0, a1, b1, c1, a2, b2, c2, d0, d1, d2
26 * (we may need to pass non-default values from user space later on, we might
27 * need to make the coefficient struct more easy to populate)
28 */
29struct colorspace_coeffs {
30	u16	sd[12];
31	u16	hd[12];
32};
33
34/* VIDEO_RANGE: limited range, GRAPHICS_RANGE: full range */
35#define	CSC_COEFFS_VIDEO_RANGE_Y2R	0
36#define	CSC_COEFFS_GRAPHICS_RANGE_Y2R	1
37#define	CSC_COEFFS_VIDEO_RANGE_R2Y	2
38#define	CSC_COEFFS_GRAPHICS_RANGE_R2Y	3
39
40/* default colorspace coefficients */
41static struct colorspace_coeffs colorspace_coeffs[4] = {
42	[CSC_COEFFS_VIDEO_RANGE_Y2R] = {
43		{
44			/* SDTV */
45			0x0400, 0x0000, 0x057D, 0x0400, 0x1EA7, 0x1D35,
46			0x0400, 0x06EF, 0x1FFE, 0x0D40, 0x0210, 0x0C88,
47		},
48		{
49			/* HDTV */
50			0x0400, 0x0000, 0x0629, 0x0400, 0x1F45, 0x1E2B,
51			0x0400, 0x0742, 0x0000, 0x0CEC, 0x0148, 0x0C60,
52		},
53	},
54	[CSC_COEFFS_GRAPHICS_RANGE_Y2R] = {
55		{
56			/* SDTV */
57			0x04A8, 0x1FFE, 0x0662, 0x04A8, 0x1E6F, 0x1CBF,
58			0x04A8, 0x0812, 0x1FFF, 0x0C84, 0x0220, 0x0BAC,
59		},
60		{
61			/* HDTV */
62			0x04A8, 0x0000, 0x072C, 0x04A8, 0x1F26, 0x1DDE,
63			0x04A8, 0x0873, 0x0000, 0x0C20, 0x0134, 0x0B7C,
64		},
65	},
66	[CSC_COEFFS_VIDEO_RANGE_R2Y] = {
67		{
68			/* SDTV */
69			0x0132, 0x0259, 0x0075, 0x1F50, 0x1EA5, 0x020B,
70			0x020B, 0x1E4A, 0x1FAB, 0x0000, 0x0200, 0x0200,
71		},
72		{
73			/* HDTV */
74			0x00DA, 0x02DC, 0x004A, 0x1F88, 0x1E6C, 0x020C,
75			0x020C, 0x1E24, 0x1FD0, 0x0000, 0x0200, 0x0200,
76		},
77	},
78	[CSC_COEFFS_GRAPHICS_RANGE_R2Y] = {
79		{
80			/* SDTV */
81			0x0107, 0x0204, 0x0064, 0x1F68, 0x1ED6, 0x01C2,
82			0x01C2, 0x1E87, 0x1FB7, 0x0040, 0x0200, 0x0200,
83		},
84		{
85			/* HDTV */
86			0x04A8, 0x0000, 0x072C, 0x04A8, 0x1F26, 0x1DDE,
87			0x04A8, 0x0873, 0x0000, 0x0C20, 0x0134, 0x0B7C,
88		},
89	},
90};
91
92void csc_dump_regs(struct csc_data *csc)
93{
94	struct device *dev = &csc->pdev->dev;
95
96#define DUMPREG(r) dev_dbg(dev, "%-35s %08x\n", #r, \
97	ioread32(csc->base + CSC_##r))
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
109void csc_set_coeff_bypass(struct csc_data *csc, u32 *csc_reg5)
110{
111	*csc_reg5 |= CSC_BYPASS;
112}
113
114/*
115 * set the color space converter coefficient shadow register values
116 */
117void csc_set_coeff(struct csc_data *csc, u32 *csc_reg0,
118		enum v4l2_colorspace src_colorspace,
119		enum v4l2_colorspace dst_colorspace)
120{
121	u32 *csc_reg5 = csc_reg0 + 5;
122	u32 *shadow_csc = csc_reg0;
123	struct colorspace_coeffs *sd_hd_coeffs;
124	u16 *coeff, *end_coeff;
125	enum v4l2_colorspace yuv_colorspace;
126	int sel = 0;
127
128	/*
129	 * support only graphics data range(full range) for now, a control ioctl
130	 * would be nice here
131	 */
132	/* Y2R */
133	if (dst_colorspace == V4L2_COLORSPACE_SRGB &&
134			(src_colorspace == V4L2_COLORSPACE_SMPTE170M ||
135			src_colorspace == V4L2_COLORSPACE_REC709)) {
136		/* Y2R */
137		sel = 1;
138		yuv_colorspace = src_colorspace;
139	} else if ((dst_colorspace == V4L2_COLORSPACE_SMPTE170M ||
140			dst_colorspace == V4L2_COLORSPACE_REC709) &&
141			src_colorspace == V4L2_COLORSPACE_SRGB) {
142		/* R2Y */
143		sel = 3;
144		yuv_colorspace = dst_colorspace;
145	} else {
146		*csc_reg5 |= CSC_BYPASS;
147		return;
148	}
149
150	sd_hd_coeffs = &colorspace_coeffs[sel];
151
152	/* select between SD or HD coefficients */
153	if (yuv_colorspace == V4L2_COLORSPACE_SMPTE170M)
154		coeff = sd_hd_coeffs->sd;
155	else
156		coeff = sd_hd_coeffs->hd;
157
158	end_coeff = coeff + 12;
159
160	for (; coeff < end_coeff; coeff += 2)
161		*shadow_csc++ = (*(coeff + 1) << 16) | *coeff;
162}
163
164struct csc_data *csc_create(struct platform_device *pdev)
165{
166	struct csc_data *csc;
167
168	dev_dbg(&pdev->dev, "csc_create\n");
169
170	csc = devm_kzalloc(&pdev->dev, sizeof(*csc), GFP_KERNEL);
171	if (!csc) {
172		dev_err(&pdev->dev, "couldn't alloc csc_data\n");
173		return ERR_PTR(-ENOMEM);
174	}
175
176	csc->pdev = pdev;
177
178	csc->res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
179			"csc");
180	if (csc->res == NULL) {
181		dev_err(&pdev->dev, "missing platform resources data\n");
182		return ERR_PTR(-ENODEV);
183	}
184
185	csc->base = devm_ioremap_resource(&pdev->dev, csc->res);
186	if (IS_ERR(csc->base)) {
187		dev_err(&pdev->dev, "failed to ioremap\n");
188		return ERR_CAST(csc->base);
189	}
190
191	return csc;
192}
193