a9db6366d20bcab28a72a8a07325cf6debfe74e7
[linux-2.6-microblaze.git] / drivers / staging / media / atomisp / pci / isp / kernels / xnr / xnr_3.0 / ia_css_xnr3.host.c
1 /*
2  * Support for Intel Camera Imaging ISP subsystem.
3  * Copyright (c) 2015, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  */
14
15 #include "type_support.h"
16 #include "math_support.h"
17 #include "sh_css_defs.h"
18 #include "ia_css_types.h"
19 #include "assert_support.h"
20 #include "ia_css_xnr3.host.h"
21
22 /* Maximum value for alpha on ISP interface */
23 #define XNR_MAX_ALPHA  ((1 << (ISP_VEC_ELEMBITS - 1)) - 1)
24
25 /* Minimum value for sigma on host interface. Lower values translate to
26  * max_alpha.
27  */
28 #define XNR_MIN_SIGMA  (IA_CSS_XNR3_SIGMA_SCALE / 100)
29
30 /*
31  * division look-up table
32  * Refers to XNR3.0.5
33  */
34 #define XNR3_LOOK_UP_TABLE_POINTS 16
35
36 static const s16 x[XNR3_LOOK_UP_TABLE_POINTS] = {
37         1024, 1164, 1320, 1492, 1680, 1884, 2108, 2352,
38         2616, 2900, 3208, 3540, 3896, 4276, 4684, 5120
39 };
40
41 static const s16 a[XNR3_LOOK_UP_TABLE_POINTS] = {
42         -7213, -5580, -4371, -3421, -2722, -2159, -6950, -5585,
43             -4529, -3697, -3010, -2485, -2070, -1727, -1428, 0
44     };
45
46 static const s16 b[XNR3_LOOK_UP_TABLE_POINTS] = {
47         4096, 3603, 3178, 2811, 2497, 2226, 1990, 1783,
48         1603, 1446, 1307, 1185, 1077, 981, 895, 819
49 };
50
51 static const s16 c[XNR3_LOOK_UP_TABLE_POINTS] = {
52         1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
53 };
54
55 /*
56  * Default kernel parameters. In general, default is bypass mode or as close
57  * to the ineffective values as possible. Due to the chroma down+upsampling,
58  * perfect bypass mode is not possible for xnr3 filter itself. Instead, the
59  * 'blending' parameter is used to create a bypass.
60  */
61 const struct ia_css_xnr3_config default_xnr3_config = {
62         /* sigma */
63         { 0, 0, 0, 0, 0, 0 },
64         /* coring */
65         { 0, 0, 0, 0 },
66         /* blending */
67         { 0 }
68 };
69
70 /*
71  * Compute an alpha value for the ISP kernel from sigma value on the host
72  * parameter interface as: alpha_scale * 1/(sigma/sigma_scale)
73  */
74 static int32_t
75 compute_alpha(int sigma)
76 {
77         s32 alpha;
78         int offset = sigma / 2;
79
80         if (sigma < XNR_MIN_SIGMA) {
81                 alpha = XNR_MAX_ALPHA;
82         } else {
83                 alpha = ((IA_CSS_XNR3_SIGMA_SCALE * XNR_ALPHA_SCALE_FACTOR) + offset) / sigma;
84
85                 if (alpha > XNR_MAX_ALPHA)
86                         alpha = XNR_MAX_ALPHA;
87         }
88
89         return alpha;
90 }
91
92 /*
93  * Compute the scaled coring value for the ISP kernel from the value on the
94  * host parameter interface.
95  */
96 static int32_t
97 compute_coring(int coring)
98 {
99         s32 isp_coring;
100         s32 isp_scale = XNR_CORING_SCALE_FACTOR;
101         s32 host_scale = IA_CSS_XNR3_CORING_SCALE;
102         s32 offset = host_scale / 2; /* fixed-point 0.5 */
103
104         /* Convert from public host-side scale factor to isp-side scale
105          * factor. Clip to [0, isp_scale-1).
106          */
107         isp_coring = ((coring * isp_scale) + offset) / host_scale;
108         return min(max(isp_coring, 0), isp_scale - 1);
109 }
110
111 /*
112  * Compute the scaled blending strength for the ISP kernel from the value on
113  * the host parameter interface.
114  */
115 static int32_t
116 compute_blending(int strength)
117 {
118         s32 isp_strength;
119         s32 isp_scale = XNR_BLENDING_SCALE_FACTOR;
120         s32 host_scale = IA_CSS_XNR3_BLENDING_SCALE;
121         s32 offset = host_scale / 2; /* fixed-point 0.5 */
122
123         /* Convert from public host-side scale factor to isp-side scale
124          * factor. The blending factor is positive on the host side, but
125          * negative on the ISP side because +1.0 cannot be represented
126          * exactly as s0.11 fixed point, but -1.0 can.
127          */
128         isp_strength = -(((strength * isp_scale) + offset) / host_scale);
129         return MAX(MIN(isp_strength, 0), -XNR_BLENDING_SCALE_FACTOR);
130 }
131
132 void
133 ia_css_xnr3_encode(
134     struct sh_css_isp_xnr3_params *to,
135     const struct ia_css_xnr3_config *from,
136     unsigned int size)
137 {
138         int kernel_size = XNR_FILTER_SIZE;
139         /* The adjust factor is the next power of 2
140            w.r.t. the kernel size*/
141         int adjust_factor = ceil_pow2(kernel_size);
142         s32 max_diff = (1 << (ISP_VEC_ELEMBITS - 1)) - 1;
143         s32 min_diff = -(1 << (ISP_VEC_ELEMBITS - 1));
144
145         s32 alpha_y0 = compute_alpha(from->sigma.y0);
146         s32 alpha_y1 = compute_alpha(from->sigma.y1);
147         s32 alpha_u0 = compute_alpha(from->sigma.u0);
148         s32 alpha_u1 = compute_alpha(from->sigma.u1);
149         s32 alpha_v0 = compute_alpha(from->sigma.v0);
150         s32 alpha_v1 = compute_alpha(from->sigma.v1);
151         s32 alpha_ydiff = (alpha_y1 - alpha_y0) * adjust_factor / kernel_size;
152         s32 alpha_udiff = (alpha_u1 - alpha_u0) * adjust_factor / kernel_size;
153         s32 alpha_vdiff = (alpha_v1 - alpha_v0) * adjust_factor / kernel_size;
154
155         s32 coring_u0 = compute_coring(from->coring.u0);
156         s32 coring_u1 = compute_coring(from->coring.u1);
157         s32 coring_v0 = compute_coring(from->coring.v0);
158         s32 coring_v1 = compute_coring(from->coring.v1);
159         s32 coring_udiff = (coring_u1 - coring_u0) * adjust_factor / kernel_size;
160         s32 coring_vdiff = (coring_v1 - coring_v0) * adjust_factor / kernel_size;
161
162         s32 blending = compute_blending(from->blending.strength);
163
164         (void)size;
165
166         /* alpha's are represented in qN.5 format */
167         to->alpha.y0 = alpha_y0;
168         to->alpha.u0 = alpha_u0;
169         to->alpha.v0 = alpha_v0;
170         to->alpha.ydiff = min(max(alpha_ydiff, min_diff), max_diff);
171         to->alpha.udiff = min(max(alpha_udiff, min_diff), max_diff);
172         to->alpha.vdiff = min(max(alpha_vdiff, min_diff), max_diff);
173
174         /* coring parameters are expressed in q1.NN format */
175         to->coring.u0 = coring_u0;
176         to->coring.v0 = coring_v0;
177         to->coring.udiff = min(max(coring_udiff, min_diff), max_diff);
178         to->coring.vdiff = min(max(coring_vdiff, min_diff), max_diff);
179
180         /* blending strength is expressed in q1.NN format */
181         to->blending.strength = blending;
182 }
183
184 /* ISP2401 */
185 /* (void) = ia_css_xnr3_vmem_encode(*to, *from)
186  * -----------------------------------------------
187  * VMEM Encode Function to translate UV parameters from userspace into ISP space
188 */
189 void
190 ia_css_xnr3_vmem_encode(
191     struct sh_css_isp_xnr3_vmem_params *to,
192     const struct ia_css_xnr3_config *from,
193     unsigned int size)
194 {
195         unsigned int i, j, base;
196         const unsigned int total_blocks = 4;
197         const unsigned int shuffle_block = 16;
198
199         (void)from;
200         (void)size;
201
202         /* Init */
203         for (i = 0; i < ISP_VEC_NELEMS; i++) {
204                 to->x[0][i] = 0;
205                 to->a[0][i] = 0;
206                 to->b[0][i] = 0;
207                 to->c[0][i] = 0;
208         }
209
210         /* Constraints on "x":
211          * - values should be greater or equal to 0.
212          * - values should be ascending.
213          */
214         assert(x[0] >= 0);
215
216         for (j = 1; j < XNR3_LOOK_UP_TABLE_POINTS; j++) {
217                 assert(x[j] >= 0);
218                 assert(x[j] > x[j - 1]);
219         }
220
221         /* The implementation of the calulating 1/x is based on the availability
222          * of the OP_vec_shuffle16 operation.
223          * A 64 element vector is split up in 4 blocks of 16 element. Each array is copied to
224          * a vector 4 times, (starting at 0, 16, 32 and 48). All array elements are copied or
225          * initialised as described in the KFS. The remaining elements of a vector are set to 0.
226          */
227         /* TODO: guard this code with above assumptions */
228         for (i = 0; i < total_blocks; i++) {
229                 base = shuffle_block * i;
230
231                 for (j = 0; j < XNR3_LOOK_UP_TABLE_POINTS; j++) {
232                         to->x[0][base + j] = x[j];
233                         to->a[0][base + j] = a[j];
234                         to->b[0][base + j] = b[j];
235                         to->c[0][base + j] = c[j];
236                 }
237         }
238 }
239
240 /* Dummy Function added as the tool expects it*/
241 void
242 ia_css_xnr3_debug_dtrace(
243     const struct ia_css_xnr3_config *config,
244     unsigned int level)
245 {
246         (void)config;
247         (void)level;
248 }