dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / gpu / drm / sun4i / sun8i_csc.c
blobe7608a72f26fc1fe92e4424511e5a5a483971f0f
1 /*
2 * Copyright (C) Jernej Skrabec <jernej.skrabec@siol.net>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 */
10 #include <drm/drmP.h>
12 #include "sun8i_csc.h"
13 #include "sun8i_mixer.h"
15 static const u32 ccsc_base[2][2] = {
16 {CCSC00_OFFSET, CCSC01_OFFSET},
17 {CCSC10_OFFSET, CCSC11_OFFSET},
21 * Factors are in two's complement format, 10 bits for fractinal part.
22 * First tree values in each line are multiplication factor and last
23 * value is constant, which is added at the end.
25 static const u32 yuv2rgb[] = {
26 0x000004A8, 0x00000000, 0x00000662, 0xFFFC845A,
27 0x000004A8, 0xFFFFFE6F, 0xFFFFFCBF, 0x00021DF4,
28 0x000004A8, 0x00000813, 0x00000000, 0xFFFBAC4A,
31 static const u32 yvu2rgb[] = {
32 0x000004A8, 0x00000662, 0x00000000, 0xFFFC845A,
33 0x000004A8, 0xFFFFFCBF, 0xFFFFFE6F, 0x00021DF4,
34 0x000004A8, 0x00000000, 0x00000813, 0xFFFBAC4A,
38 * DE3 has a bit different CSC units. Factors are in two's complement format.
39 * First three factors in a row are multiplication factors which have 17 bits
40 * for fractional part. Fourth value in a row is comprised of two factors.
41 * Upper 16 bits represents difference, which is subtracted from the input
42 * value before multiplication and lower 16 bits represents constant, which
43 * is addes at the end.
45 * x' = c00 * (x + d0) + c01 * (y + d1) + c02 * (z + d2) + const0
46 * y' = c10 * (x + d0) + c11 * (y + d1) + c12 * (z + d2) + const1
47 * z' = c20 * (x + d0) + c21 * (y + d1) + c22 * (z + d2) + const2
49 * Please note that above formula is true only for Blender CSC. Other DE3 CSC
50 * units takes only positive value for difference. From what can be deducted
51 * from BSP driver code, those units probably automatically assume that
52 * difference has to be subtracted.
54 * Layout of factors in table:
55 * c00 c01 c02 [d0 const0]
56 * c10 c11 c12 [d1 const1]
57 * c20 c21 c22 [d2 const2]
60 static const u32 yuv2rgb_de3[] = {
61 0x0002542a, 0x00000000, 0x0003312a, 0xffc00000,
62 0x0002542a, 0xffff376b, 0xfffe5fc3, 0xfe000000,
63 0x0002542a, 0x000408d3, 0x00000000, 0xfe000000,
66 static const u32 yvu2rgb_de3[] = {
67 0x0002542a, 0x0003312a, 0x00000000, 0xffc00000,
68 0x0002542a, 0xfffe5fc3, 0xffff376b, 0xfe000000,
69 0x0002542a, 0x00000000, 0x000408d3, 0xfe000000,
72 static void sun8i_csc_set_coefficients(struct regmap *map, u32 base,
73 enum sun8i_csc_mode mode)
75 const u32 *table;
76 int i, data;
78 switch (mode) {
79 case SUN8I_CSC_MODE_YUV2RGB:
80 table = yuv2rgb;
81 break;
82 case SUN8I_CSC_MODE_YVU2RGB:
83 table = yvu2rgb;
84 break;
85 default:
86 DRM_WARN("Wrong CSC mode specified.\n");
87 return;
90 for (i = 0; i < 12; i++) {
91 data = table[i];
92 /* For some reason, 0x200 must be added to constant parts */
93 if (((i + 1) & 3) == 0)
94 data += 0x200;
95 regmap_write(map, SUN8I_CSC_COEFF(base, i), data);
99 static void sun8i_de3_ccsc_set_coefficients(struct regmap *map, int layer,
100 enum sun8i_csc_mode mode)
102 const u32 *table;
103 u32 base_reg;
105 switch (mode) {
106 case SUN8I_CSC_MODE_YUV2RGB:
107 table = yuv2rgb_de3;
108 break;
109 case SUN8I_CSC_MODE_YVU2RGB:
110 table = yvu2rgb_de3;
111 break;
112 default:
113 DRM_WARN("Wrong CSC mode specified.\n");
114 return;
117 base_reg = SUN50I_MIXER_BLEND_CSC_COEFF(DE3_BLD_BASE, layer, 0, 0);
118 regmap_bulk_write(map, base_reg, table, 12);
121 static void sun8i_csc_enable(struct regmap *map, u32 base, bool enable)
123 u32 val;
125 if (enable)
126 val = SUN8I_CSC_CTRL_EN;
127 else
128 val = 0;
130 regmap_update_bits(map, SUN8I_CSC_CTRL(base), SUN8I_CSC_CTRL_EN, val);
133 static void sun8i_de3_ccsc_enable(struct regmap *map, int layer, bool enable)
135 u32 val, mask;
137 mask = SUN50I_MIXER_BLEND_CSC_CTL_EN(layer);
139 if (enable)
140 val = mask;
141 else
142 val = 0;
144 regmap_update_bits(map, SUN50I_MIXER_BLEND_CSC_CTL(DE3_BLD_BASE),
145 mask, val);
148 void sun8i_csc_set_ccsc_coefficients(struct sun8i_mixer *mixer, int layer,
149 enum sun8i_csc_mode mode)
151 u32 base;
153 if (mixer->cfg->is_de3) {
154 sun8i_de3_ccsc_set_coefficients(mixer->engine.regs,
155 layer, mode);
156 return;
159 base = ccsc_base[mixer->cfg->ccsc][layer];
161 sun8i_csc_set_coefficients(mixer->engine.regs, base, mode);
164 void sun8i_csc_enable_ccsc(struct sun8i_mixer *mixer, int layer, bool enable)
166 u32 base;
168 if (mixer->cfg->is_de3) {
169 sun8i_de3_ccsc_enable(mixer->engine.regs, layer, enable);
170 return;
173 base = ccsc_base[mixer->cfg->ccsc][layer];
175 sun8i_csc_enable(mixer->engine.regs, base, enable);