Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Jernej Skrabec | 8830293 | 2017-12-01 07:05:46 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) Jernej Skrabec <jernej.skrabec@siol.net> |
Jernej Skrabec | 8830293 | 2017-12-01 07:05:46 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
Sam Ravnborg | 9c25a29 | 2019-07-16 08:42:06 +0200 | [diff] [blame] | 6 | #include <drm/drm_print.h> |
Jernej Skrabec | 8830293 | 2017-12-01 07:05:46 +0100 | [diff] [blame] | 7 | |
| 8 | #include "sun8i_csc.h" |
| 9 | #include "sun8i_mixer.h" |
| 10 | |
| 11 | static const u32 ccsc_base[2][2] = { |
| 12 | {CCSC00_OFFSET, CCSC01_OFFSET}, |
| 13 | {CCSC10_OFFSET, CCSC11_OFFSET}, |
| 14 | }; |
| 15 | |
| 16 | /* |
| 17 | * Factors are in two's complement format, 10 bits for fractinal part. |
| 18 | * First tree values in each line are multiplication factor and last |
| 19 | * value is constant, which is added at the end. |
| 20 | */ |
| 21 | static const u32 yuv2rgb[] = { |
| 22 | 0x000004A8, 0x00000000, 0x00000662, 0xFFFC845A, |
| 23 | 0x000004A8, 0xFFFFFE6F, 0xFFFFFCBF, 0x00021DF4, |
| 24 | 0x000004A8, 0x00000813, 0x00000000, 0xFFFBAC4A, |
| 25 | }; |
| 26 | |
| 27 | static const u32 yvu2rgb[] = { |
| 28 | 0x000004A8, 0x00000662, 0x00000000, 0xFFFC845A, |
| 29 | 0x000004A8, 0xFFFFFCBF, 0xFFFFFE6F, 0x00021DF4, |
| 30 | 0x000004A8, 0x00000000, 0x00000813, 0xFFFBAC4A, |
| 31 | }; |
| 32 | |
Jernej Skrabec | c50519e | 2018-11-04 19:26:49 +0100 | [diff] [blame] | 33 | /* |
| 34 | * DE3 has a bit different CSC units. Factors are in two's complement format. |
| 35 | * First three factors in a row are multiplication factors which have 17 bits |
| 36 | * for fractional part. Fourth value in a row is comprised of two factors. |
| 37 | * Upper 16 bits represents difference, which is subtracted from the input |
| 38 | * value before multiplication and lower 16 bits represents constant, which |
| 39 | * is addes at the end. |
| 40 | * |
| 41 | * x' = c00 * (x + d0) + c01 * (y + d1) + c02 * (z + d2) + const0 |
| 42 | * y' = c10 * (x + d0) + c11 * (y + d1) + c12 * (z + d2) + const1 |
| 43 | * z' = c20 * (x + d0) + c21 * (y + d1) + c22 * (z + d2) + const2 |
| 44 | * |
| 45 | * Please note that above formula is true only for Blender CSC. Other DE3 CSC |
| 46 | * units takes only positive value for difference. From what can be deducted |
| 47 | * from BSP driver code, those units probably automatically assume that |
| 48 | * difference has to be subtracted. |
| 49 | * |
| 50 | * Layout of factors in table: |
| 51 | * c00 c01 c02 [d0 const0] |
| 52 | * c10 c11 c12 [d1 const1] |
| 53 | * c20 c21 c22 [d2 const2] |
| 54 | */ |
| 55 | |
| 56 | static const u32 yuv2rgb_de3[] = { |
| 57 | 0x0002542a, 0x00000000, 0x0003312a, 0xffc00000, |
| 58 | 0x0002542a, 0xffff376b, 0xfffe5fc3, 0xfe000000, |
| 59 | 0x0002542a, 0x000408d3, 0x00000000, 0xfe000000, |
| 60 | }; |
| 61 | |
| 62 | static const u32 yvu2rgb_de3[] = { |
| 63 | 0x0002542a, 0x0003312a, 0x00000000, 0xffc00000, |
| 64 | 0x0002542a, 0xfffe5fc3, 0xffff376b, 0xfe000000, |
| 65 | 0x0002542a, 0x00000000, 0x000408d3, 0xfe000000, |
| 66 | }; |
| 67 | |
Jernej Skrabec | 8830293 | 2017-12-01 07:05:46 +0100 | [diff] [blame] | 68 | static void sun8i_csc_set_coefficients(struct regmap *map, u32 base, |
| 69 | enum sun8i_csc_mode mode) |
| 70 | { |
| 71 | const u32 *table; |
Jernej Skrabec | ab38c3b | 2019-07-13 14:03:45 +0200 | [diff] [blame^] | 72 | u32 base_reg; |
Jernej Skrabec | 8830293 | 2017-12-01 07:05:46 +0100 | [diff] [blame] | 73 | |
| 74 | switch (mode) { |
| 75 | case SUN8I_CSC_MODE_YUV2RGB: |
| 76 | table = yuv2rgb; |
| 77 | break; |
| 78 | case SUN8I_CSC_MODE_YVU2RGB: |
| 79 | table = yvu2rgb; |
| 80 | break; |
| 81 | default: |
| 82 | DRM_WARN("Wrong CSC mode specified.\n"); |
| 83 | return; |
| 84 | } |
| 85 | |
Jernej Skrabec | ab38c3b | 2019-07-13 14:03:45 +0200 | [diff] [blame^] | 86 | base_reg = SUN8I_CSC_COEFF(base, 0); |
| 87 | regmap_bulk_write(map, base_reg, table, 12); |
Jernej Skrabec | 8830293 | 2017-12-01 07:05:46 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Jernej Skrabec | c50519e | 2018-11-04 19:26:49 +0100 | [diff] [blame] | 90 | static void sun8i_de3_ccsc_set_coefficients(struct regmap *map, int layer, |
| 91 | enum sun8i_csc_mode mode) |
| 92 | { |
| 93 | const u32 *table; |
| 94 | u32 base_reg; |
| 95 | |
| 96 | switch (mode) { |
| 97 | case SUN8I_CSC_MODE_YUV2RGB: |
| 98 | table = yuv2rgb_de3; |
| 99 | break; |
| 100 | case SUN8I_CSC_MODE_YVU2RGB: |
| 101 | table = yvu2rgb_de3; |
| 102 | break; |
| 103 | default: |
| 104 | DRM_WARN("Wrong CSC mode specified.\n"); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | base_reg = SUN50I_MIXER_BLEND_CSC_COEFF(DE3_BLD_BASE, layer, 0, 0); |
| 109 | regmap_bulk_write(map, base_reg, table, 12); |
| 110 | } |
| 111 | |
Jernej Skrabec | 8830293 | 2017-12-01 07:05:46 +0100 | [diff] [blame] | 112 | static void sun8i_csc_enable(struct regmap *map, u32 base, bool enable) |
| 113 | { |
| 114 | u32 val; |
| 115 | |
| 116 | if (enable) |
| 117 | val = SUN8I_CSC_CTRL_EN; |
| 118 | else |
| 119 | val = 0; |
| 120 | |
| 121 | regmap_update_bits(map, SUN8I_CSC_CTRL(base), SUN8I_CSC_CTRL_EN, val); |
| 122 | } |
| 123 | |
Jernej Skrabec | c50519e | 2018-11-04 19:26:49 +0100 | [diff] [blame] | 124 | static void sun8i_de3_ccsc_enable(struct regmap *map, int layer, bool enable) |
| 125 | { |
| 126 | u32 val, mask; |
| 127 | |
| 128 | mask = SUN50I_MIXER_BLEND_CSC_CTL_EN(layer); |
| 129 | |
| 130 | if (enable) |
| 131 | val = mask; |
| 132 | else |
| 133 | val = 0; |
| 134 | |
| 135 | regmap_update_bits(map, SUN50I_MIXER_BLEND_CSC_CTL(DE3_BLD_BASE), |
| 136 | mask, val); |
| 137 | } |
| 138 | |
Jernej Skrabec | 8830293 | 2017-12-01 07:05:46 +0100 | [diff] [blame] | 139 | void sun8i_csc_set_ccsc_coefficients(struct sun8i_mixer *mixer, int layer, |
| 140 | enum sun8i_csc_mode mode) |
| 141 | { |
| 142 | u32 base; |
| 143 | |
Jernej Skrabec | c50519e | 2018-11-04 19:26:49 +0100 | [diff] [blame] | 144 | if (mixer->cfg->is_de3) { |
| 145 | sun8i_de3_ccsc_set_coefficients(mixer->engine.regs, |
| 146 | layer, mode); |
| 147 | return; |
| 148 | } |
| 149 | |
Jernej Skrabec | 8830293 | 2017-12-01 07:05:46 +0100 | [diff] [blame] | 150 | base = ccsc_base[mixer->cfg->ccsc][layer]; |
| 151 | |
| 152 | sun8i_csc_set_coefficients(mixer->engine.regs, base, mode); |
| 153 | } |
| 154 | |
| 155 | void sun8i_csc_enable_ccsc(struct sun8i_mixer *mixer, int layer, bool enable) |
| 156 | { |
| 157 | u32 base; |
| 158 | |
Jernej Skrabec | c50519e | 2018-11-04 19:26:49 +0100 | [diff] [blame] | 159 | if (mixer->cfg->is_de3) { |
| 160 | sun8i_de3_ccsc_enable(mixer->engine.regs, layer, enable); |
| 161 | return; |
| 162 | } |
| 163 | |
Jernej Skrabec | 8830293 | 2017-12-01 07:05:46 +0100 | [diff] [blame] | 164 | base = ccsc_base[mixer->cfg->ccsc][layer]; |
| 165 | |
| 166 | sun8i_csc_enable(mixer->engine.regs, base, enable); |
| 167 | } |