GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / staging / media / atomisp / pci / atomisp2 / css2400 / sh_css_defs.h
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 #ifndef _SH_CSS_DEFS_H_
16 #define _SH_CSS_DEFS_H_
17
18 #include "isp.h"
19
20 /*#include "vamem.h"*/ /* Cannot include for VAMEM properties this file is visible on ISP -> pipeline generator */
21
22 #include "math_support.h"       /* max(), min, etc etc */
23
24 /* ID's for refcount */
25 #define IA_CSS_REFCOUNT_PARAM_SET_POOL  0xCAFE0001
26 #define IA_CSS_REFCOUNT_PARAM_BUFFER    0xCAFE0002
27
28 /* Digital Image Stabilization */
29 #define SH_CSS_DIS_DECI_FACTOR_LOG2       6
30
31 /* UV offset: 1:uv=-128...127, 0:uv=0...255 */
32 #define SH_CSS_UV_OFFSET_IS_0             0
33
34 /* Bits of bayer is adjusted as 13 in ISP */
35 #define SH_CSS_BAYER_BITS                 13
36
37 /* Max value of bayer data (unsigned 13bit in ISP) */
38 #define SH_CSS_BAYER_MAXVAL               ((1U << SH_CSS_BAYER_BITS) - 1)
39
40 /* Bits of yuv in ISP */
41 #define SH_CSS_ISP_YUV_BITS               8
42
43 #define SH_CSS_DP_GAIN_SHIFT              5
44 #define SH_CSS_BNR_GAIN_SHIFT             13
45 #define SH_CSS_YNR_GAIN_SHIFT             13
46 #define SH_CSS_AE_YCOEF_SHIFT             13
47 #define SH_CSS_AF_FIR_SHIFT               13
48 #define SH_CSS_YEE_DETAIL_GAIN_SHIFT      8  /* [u5.8] */
49 #define SH_CSS_YEE_SCALE_SHIFT            8
50 #define SH_CSS_TNR_COEF_SHIFT             13
51 #define SH_CSS_MACC_COEF_SHIFT            11 /* [s2.11] for ISP1 */
52 #define SH_CSS_MACC2_COEF_SHIFT           13 /* [s[exp].[13-exp]] for ISP2 */
53 #define SH_CSS_DIS_COEF_SHIFT             13
54
55 /* enumeration of the bayer downscale factors. When a binary supports multiple
56  * factors, the OR of these defines is used to build the mask of supported
57  * factors. The BDS factor is used in pre-processor expressions so we cannot
58  * use an enum here. */
59 #define SH_CSS_BDS_FACTOR_1_00  (0)
60 #define SH_CSS_BDS_FACTOR_1_25  (1)
61 #define SH_CSS_BDS_FACTOR_1_50  (2)
62 #define SH_CSS_BDS_FACTOR_2_00  (3)
63 #define SH_CSS_BDS_FACTOR_2_25  (4)
64 #define SH_CSS_BDS_FACTOR_2_50  (5)
65 #define SH_CSS_BDS_FACTOR_3_00  (6)
66 #define SH_CSS_BDS_FACTOR_4_00  (7)
67 #define SH_CSS_BDS_FACTOR_4_50  (8)
68 #define SH_CSS_BDS_FACTOR_5_00  (9)
69 #define SH_CSS_BDS_FACTOR_6_00  (10)
70 #define SH_CSS_BDS_FACTOR_8_00  (11)
71 #define NUM_BDS_FACTORS         (12)
72
73 #define PACK_BDS_FACTOR(factor) (1<<(factor))
74
75 /* Following macros should match with the type enum ia_css_pipe_version in
76  * ia_css_pipe_public.h. The reason to add these macros is that enum type
77  * will be evaluted to 0 in preprocessing time. */
78 #define SH_CSS_ISP_PIPE_VERSION_1       1
79 #define SH_CSS_ISP_PIPE_VERSION_2_2     2
80 #define SH_CSS_ISP_PIPE_VERSION_2_6_1   3
81 #define SH_CSS_ISP_PIPE_VERSION_2_7     4
82
83 /*--------------- sRGB Gamma -----------------
84 CCM        : YCgCo[0,8191] -> RGB[0,4095]
85 sRGB Gamma : RGB  [0,4095] -> RGB[0,8191]
86 CSC        : RGB  [0,8191] -> YUV[0,8191]
87
88 CCM:
89 Y[0,8191],CgCo[-4096,4095],coef[-8192,8191] -> RGB[0,4095]
90
91 sRGB Gamma:
92 RGB[0,4095] -(interpolation step16)-> RGB[0,255] -(LUT 12bit)-> RGB[0,4095] -> RGB[0,8191]
93
94 CSC:
95 RGB[0,8191],coef[-8192,8191] -> RGB[0,8191]
96 --------------------------------------------*/
97 /* Bits of input/output of sRGB Gamma */
98 #define SH_CSS_RGB_GAMMA_INPUT_BITS       12 /* [0,4095] */
99 #define SH_CSS_RGB_GAMMA_OUTPUT_BITS      13 /* [0,8191] */
100
101 /* Bits of fractional part of interpolation in vamem, [0,4095]->[0,255] */
102 #define SH_CSS_RGB_GAMMA_FRAC_BITS        \
103         (SH_CSS_RGB_GAMMA_INPUT_BITS - SH_CSS_ISP_RGB_GAMMA_TABLE_SIZE_LOG2)
104 #define SH_CSS_RGB_GAMMA_ONE              (1 << SH_CSS_RGB_GAMMA_FRAC_BITS)
105
106 /* Bits of input of CCM,  = 13, Y[0,8191],CgCo[-4096,4095] */
107 #define SH_CSS_YUV2RGB_CCM_INPUT_BITS     SH_CSS_BAYER_BITS
108
109 /* Bits of output of CCM,  = 12, RGB[0,4095] */
110 #define SH_CSS_YUV2RGB_CCM_OUTPUT_BITS    SH_CSS_RGB_GAMMA_INPUT_BITS
111
112 /* Maximum value of output of CCM */
113 #define SH_CSS_YUV2RGB_CCM_MAX_OUTPUT     \
114         ((1 << SH_CSS_YUV2RGB_CCM_OUTPUT_BITS) - 1)
115
116 #define SH_CSS_NUM_INPUT_BUF_LINES        4
117
118 /* Left cropping only applicable for sufficiently large nway */
119 #if ISP_VEC_NELEMS == 16
120 #define SH_CSS_MAX_LEFT_CROPPING          0
121 #define SH_CSS_MAX_TOP_CROPPING           0
122 #else
123 #define SH_CSS_MAX_LEFT_CROPPING          12
124 #define SH_CSS_MAX_TOP_CROPPING           12
125 #endif
126
127 #define SH_CSS_SP_MAX_WIDTH               1280
128
129 /* This is the maximum grid we can handle in the ISP binaries.
130  * The host code makes sure no bigger grid is ever selected. */
131 #define SH_CSS_MAX_BQ_GRID_WIDTH          80
132 #define SH_CSS_MAX_BQ_GRID_HEIGHT         60
133
134 /* The minimum dvs envelope is 12x12(for IPU2) to make sure the 
135  * invalid rows/columns that result from filter initialization are skipped. */
136 #define SH_CSS_MIN_DVS_ENVELOPE           12U
137
138 /* The FPGA system (vec_nelems == 16) only supports upto 5MP */
139 #if ISP_VEC_NELEMS == 16
140 #define SH_CSS_MAX_SENSOR_WIDTH           2560
141 #define SH_CSS_MAX_SENSOR_HEIGHT          1920
142 #else
143 #define SH_CSS_MAX_SENSOR_WIDTH           4608
144 #define SH_CSS_MAX_SENSOR_HEIGHT          3450
145 #endif
146
147 /* Limited to reduce vmem pressure */
148 #if ISP_VMEM_DEPTH >= 3072
149 #define SH_CSS_MAX_CONTINUOUS_SENSOR_WIDTH  SH_CSS_MAX_SENSOR_WIDTH
150 #define SH_CSS_MAX_CONTINUOUS_SENSOR_HEIGHT SH_CSS_MAX_SENSOR_HEIGHT
151 #else
152 #define SH_CSS_MAX_CONTINUOUS_SENSOR_WIDTH  3264
153 #define SH_CSS_MAX_CONTINUOUS_SENSOR_HEIGHT 2448
154 #endif
155 /* When using bayer decimation */
156 /*
157 #define SH_CSS_MAX_CONTINUOUS_SENSOR_WIDTH_DEC  4224
158 #define SH_CSS_MAX_CONTINUOUS_SENSOR_HEIGHT_DEC 3168
159 */
160 #define SH_CSS_MAX_CONTINUOUS_SENSOR_WIDTH_DEC  SH_CSS_MAX_SENSOR_WIDTH
161 #define SH_CSS_MAX_CONTINUOUS_SENSOR_HEIGHT_DEC SH_CSS_MAX_SENSOR_HEIGHT
162
163 #define SH_CSS_MIN_SENSOR_WIDTH           2
164 #define SH_CSS_MIN_SENSOR_HEIGHT          2
165
166 #if defined(IS_ISP_2400_SYSTEM)
167 /* MAX width and height set to the same to allow for rotated
168  * resolutions. */
169 #define SH_CSS_MAX_VF_WIDTH               1920
170 #define SH_CSS_MAX_VF_HEIGHT              1920
171 #else
172 #define SH_CSS_MAX_VF_WIDTH               1280
173 #define SH_CSS_MAX_VF_HEIGHT              960
174 #endif
175 /*
176 #define SH_CSS_MAX_VF_WIDTH_DEC               1920
177 #define SH_CSS_MAX_VF_HEIGHT_DEC              1080
178 */
179 #define SH_CSS_MAX_VF_WIDTH_DEC               SH_CSS_MAX_VF_WIDTH
180 #define SH_CSS_MAX_VF_HEIGHT_DEC              SH_CSS_MAX_VF_HEIGHT
181
182 /* We use 16 bits per coordinate component, including integer
183    and fractional bits */
184 #define SH_CSS_MORPH_TABLE_GRID               ISP_VEC_NELEMS
185 #define SH_CSS_MORPH_TABLE_ELEM_BYTES         2
186 #define SH_CSS_MORPH_TABLE_ELEMS_PER_DDR_WORD \
187         (HIVE_ISP_DDR_WORD_BYTES/SH_CSS_MORPH_TABLE_ELEM_BYTES)
188
189 #ifndef ISP2401
190 #define SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR   (SH_CSS_MAX_BQ_GRID_WIDTH + 1)
191 #define SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR   (SH_CSS_MAX_BQ_GRID_HEIGHT + 1)
192 #else
193 /* TODO: I will move macros of "*_SCTBL_*" to SC kernel.
194    "+ 2" should be "+ SH_CSS_SCTBL_CENTERING_MARGIN + SH_CSS_SCTBL_LAST_GRID_COUNT". (michie, Sep/23/2014) */
195 #define SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR   (SH_CSS_MAX_BQ_GRID_WIDTH + 2)
196 #define SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR   (SH_CSS_MAX_BQ_GRID_HEIGHT + 2)
197 #endif
198 #define SH_CSS_MAX_SCTBL_ALIGNED_WIDTH_PER_COLOR \
199         CEIL_MUL(SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR, ISP_VEC_NELEMS)
200
201 /* Each line of this table is aligned to the maximum line width. */
202 #define SH_CSS_MAX_S3ATBL_WIDTH              SH_CSS_MAX_BQ_GRID_WIDTH
203
204 #ifndef ISP2401
205 /* The video binary supports a delay of 1 or 2 */
206 #define MAX_DVS_FRAME_DELAY             2
207 /* We always need one additional frame because the video binary
208  * reads the previous and writes the current frame concurrently */
209 #define MAX_NUM_VIDEO_DELAY_FRAMES      (MAX_DVS_FRAME_DELAY + 1)
210 #define NUM_VIDEO_TNR_FRAMES            2
211
212 #define NUM_TNR_FRAMES                  2       /* FIXME */
213
214
215 #define MAX_NUM_DELAY_FRAMES            MAX_NUM_VIDEO_DELAY_FRAMES
216
217 #else
218 /* Video mode specific DVS define */
219 /* The video binary supports a delay of 1 or 2 frames */
220 #define VIDEO_FRAME_DELAY               2
221 /* +1 because DVS reads the previous and writes the current frame concurrently */
222 #define MAX_NUM_VIDEO_DELAY_FRAMES      (VIDEO_FRAME_DELAY + 1)
223
224 /* Preview mode specific DVS define. */
225 /* In preview we only need GDC functionality (and not the DVS functionality) */
226 /* The minimum number of DVS frames you need is 2, one were GDC reads from and another where GDC writes into */
227 #define NUM_PREVIEW_DVS_FRAMES          (2)
228
229 /* TNR is no longer exclusive to video, SkyCam preview has TNR too (same kernel as video).
230  * All uses the generic define NUM_TNR_FRAMES. The define NUM_VIDEO_TNR_FRAMES has been deprecated.
231  *
232  * Notes
233  * 1) The value depends on the used TNR kernel and is not something that depends on the mode
234  *    and it is not something you just could choice.
235  * 2) For the luma only pipeline a version that supports two different sets of TNR reference frames
236  * is being used.
237  *.
238  */
239 #define NUM_VALID_TNR_REF_FRAMES                (1) /* At least one valid TNR reference frame is required */
240 #define NUM_TNR_FRAMES_PER_REF_BUF_SET          (2)
241
242 /* In luma-only mode alternate illuminated frames are supported, that requires two double buffers */
243 #ifdef ENABLE_LUMA_ONLY
244 #define NUM_TNR_REF_BUF_SETS    (2)
245 #else
246 #define NUM_TNR_REF_BUF_SETS    (1)
247 #endif
248
249 #define NUM_TNR_FRAMES          (NUM_TNR_FRAMES_PER_REF_BUF_SET * NUM_TNR_REF_BUF_SETS)
250
251 #define MAX_NUM_DELAY_FRAMES    MAX(MAX_NUM_VIDEO_DELAY_FRAMES, NUM_PREVIEW_DVS_FRAMES)
252
253 #endif
254
255 /* Note that this is the define used to configure all data structures common for all modes */
256 /* It should be equal or bigger to the max number of DVS frames for all possible modes */
257 /* Rules: these implement logic shared between the host code and ISP firmware.
258    The ISP firmware needs these rules to be applied at pre-processor time,
259    that's why these are macros, not functions. */
260 #define _ISP_BQS(num)  ((num)/2)
261 #define _ISP_VECS(width) CEIL_DIV(width, ISP_VEC_NELEMS)
262
263 #define ISP_BQ_GRID_WIDTH(elements_per_line, deci_factor_log2) \
264         CEIL_SHIFT(elements_per_line/2,  deci_factor_log2)
265 #define ISP_BQ_GRID_HEIGHT(lines_per_frame, deci_factor_log2) \
266         CEIL_SHIFT(lines_per_frame/2,  deci_factor_log2)
267 #define ISP_C_VECTORS_PER_LINE(elements_per_line) \
268         _ISP_VECS(elements_per_line/2)
269
270 /* The morphing table is similar to the shading table in the sense that we
271    have 1 more value than we have cells in the grid. */
272 #define _ISP_MORPH_TABLE_WIDTH(int_width) \
273         (CEIL_DIV(int_width, SH_CSS_MORPH_TABLE_GRID) + 1)
274 #define _ISP_MORPH_TABLE_HEIGHT(int_height) \
275         (CEIL_DIV(int_height, SH_CSS_MORPH_TABLE_GRID) + 1)
276 #define _ISP_MORPH_TABLE_ALIGNED_WIDTH(width) \
277         CEIL_MUL(_ISP_MORPH_TABLE_WIDTH(width), \
278                  SH_CSS_MORPH_TABLE_ELEMS_PER_DDR_WORD)
279
280 #ifndef ISP2401
281 #define _ISP_SCTBL_WIDTH_PER_COLOR(input_width, deci_factor_log2) \
282         (ISP_BQ_GRID_WIDTH(input_width, deci_factor_log2) + 1)
283 #define _ISP_SCTBL_HEIGHT(input_height, deci_factor_log2) \
284         (ISP_BQ_GRID_HEIGHT(input_height, deci_factor_log2) + 1)
285 #define _ISP_SCTBL_ALIGNED_WIDTH_PER_COLOR(input_width, deci_factor_log2) \
286         CEIL_MUL(_ISP_SCTBL_WIDTH_PER_COLOR(input_width, deci_factor_log2), \
287                  ISP_VEC_NELEMS)
288
289 #endif
290 /* *****************************************************************
291  * Statistics for 3A (Auto Focus, Auto White Balance, Auto Exposure)
292  * *****************************************************************/
293 /* if left cropping is used, 3A statistics are also cropped by 2 vectors. */
294 #define _ISP_S3ATBL_WIDTH(in_width, deci_factor_log2) \
295         (_ISP_BQS(in_width) >> deci_factor_log2)
296 #define _ISP_S3ATBL_HEIGHT(in_height, deci_factor_log2) \
297         (_ISP_BQS(in_height) >> deci_factor_log2)
298 #define _ISP_S3A_ELEMS_ISP_WIDTH(width, left_crop) \
299         (width - ((left_crop) ? 2 * ISP_VEC_NELEMS : 0))
300
301 #define _ISP_S3ATBL_ISP_WIDTH(in_width, deci_factor_log2) \
302         CEIL_SHIFT(_ISP_BQS(in_width), deci_factor_log2)
303 #define _ISP_S3ATBL_ISP_HEIGHT(in_height, deci_factor_log2) \
304         CEIL_SHIFT(_ISP_BQS(in_height), deci_factor_log2)
305 #define ISP_S3ATBL_VECTORS \
306         _ISP_VECS(SH_CSS_MAX_S3ATBL_WIDTH * \
307                   (sizeof(struct ia_css_3a_output)/sizeof(int32_t)))
308 #define ISP_S3ATBL_HI_LO_STRIDE \
309         (ISP_S3ATBL_VECTORS * ISP_VEC_NELEMS)
310 #define ISP_S3ATBL_HI_LO_STRIDE_BYTES \
311         (sizeof(unsigned short) * ISP_S3ATBL_HI_LO_STRIDE)
312
313 /* Viewfinder support */
314 #define __ISP_MAX_VF_OUTPUT_WIDTH(width, left_crop) \
315         (width - 2*ISP_VEC_NELEMS + ((left_crop) ? 2 * ISP_VEC_NELEMS : 0))
316
317 #define __ISP_VF_OUTPUT_WIDTH_VECS(out_width, vf_log_downscale) \
318         (_ISP_VECS((out_width) >> (vf_log_downscale)))
319
320 #define _ISP_VF_OUTPUT_WIDTH(vf_out_vecs) ((vf_out_vecs) * ISP_VEC_NELEMS)
321 #define _ISP_VF_OUTPUT_HEIGHT(out_height, vf_log_ds) \
322         ((out_height) >> (vf_log_ds))
323
324 #define _ISP_LOG_VECTOR_STEP(mode) \
325         ((mode) == IA_CSS_BINARY_MODE_CAPTURE_PP ? 2 : 1)
326
327 /* It is preferred to have not more than 2x scaling at one step
328  * in GDC (assumption is for capture_pp and yuv_scale stages) */
329 #define MAX_PREFERRED_YUV_DS_PER_STEP   2
330
331 /* Rules for computing the internal width. This is extremely complicated
332  * and definitely needs to be commented and explained. */
333 #define _ISP_LEFT_CROP_EXTRA(left_crop) ((left_crop) > 0 ? 2*ISP_VEC_NELEMS : 0)
334
335 #define __ISP_MIN_INTERNAL_WIDTH(num_chunks, pipelining, mode) \
336         ((num_chunks) * (pipelining) * (1<<_ISP_LOG_VECTOR_STEP(mode)) * \
337          ISP_VEC_NELEMS)
338
339 #define __ISP_PADDED_OUTPUT_WIDTH(out_width, dvs_env_width, left_crop) \
340         ((out_width) + MAX(dvs_env_width, _ISP_LEFT_CROP_EXTRA(left_crop)))
341
342 #define __ISP_CHUNK_STRIDE_ISP(mode) \
343         ((1<<_ISP_LOG_VECTOR_STEP(mode)) * ISP_VEC_NELEMS)
344
345 #define __ISP_CHUNK_STRIDE_DDR(c_subsampling, num_chunks) \
346         ((c_subsampling) * (num_chunks) * HIVE_ISP_DDR_WORD_BYTES)
347 #define __ISP_INTERNAL_WIDTH(out_width, \
348                              dvs_env_width, \
349                              left_crop, \
350                              mode, \
351                              c_subsampling, \
352                              num_chunks, \
353                              pipelining) \
354         CEIL_MUL2(CEIL_MUL2(MAX(__ISP_PADDED_OUTPUT_WIDTH(out_width, \
355                                                             dvs_env_width, \
356                                                             left_crop), \
357                                   __ISP_MIN_INTERNAL_WIDTH(num_chunks, \
358                                                            pipelining, \
359                                                            mode) \
360                                  ), \
361                           __ISP_CHUNK_STRIDE_ISP(mode) \
362                          ), \
363                  __ISP_CHUNK_STRIDE_DDR(c_subsampling, num_chunks) \
364                 )
365
366 #define __ISP_INTERNAL_HEIGHT(out_height, dvs_env_height, top_crop) \
367         ((out_height) + (dvs_env_height) + top_crop)
368
369 /* @GC: Input can be up to sensor resolution when either bayer downscaling
370  *      or raw binning is enabled.
371  *      Also, during continuous mode, we need to align to 4*NWAY since input
372  *      should support binning */
373 #define _ISP_MAX_INPUT_WIDTH(max_internal_width, enable_ds, enable_fixed_bayer_ds, enable_raw_bin, \
374                                 enable_continuous) \
375         ((enable_ds) ? \
376            SH_CSS_MAX_SENSOR_WIDTH :\
377          (enable_fixed_bayer_ds) ? \
378            CEIL_MUL(SH_CSS_MAX_CONTINUOUS_SENSOR_WIDTH_DEC, 4*ISP_VEC_NELEMS) : \
379          (enable_raw_bin) ? \
380            CEIL_MUL(SH_CSS_MAX_CONTINUOUS_SENSOR_WIDTH, 4*ISP_VEC_NELEMS) : \
381          (enable_continuous) ? \
382            SH_CSS_MAX_CONTINUOUS_SENSOR_WIDTH \
383            : max_internal_width)
384
385 #define _ISP_INPUT_WIDTH(internal_width, ds_input_width, enable_ds) \
386         ((enable_ds) ? (ds_input_width) : (internal_width))
387
388 #define _ISP_MAX_INPUT_HEIGHT(max_internal_height, enable_ds, enable_fixed_bayer_ds, enable_raw_bin, \
389                                 enable_continuous) \
390         ((enable_ds) ? \
391            SH_CSS_MAX_SENSOR_HEIGHT :\
392          (enable_fixed_bayer_ds) ? \
393            SH_CSS_MAX_CONTINUOUS_SENSOR_HEIGHT_DEC : \
394          (enable_raw_bin || enable_continuous) ? \
395            SH_CSS_MAX_CONTINUOUS_SENSOR_HEIGHT \
396            : max_internal_height)
397
398 #define _ISP_INPUT_HEIGHT(internal_height, ds_input_height, enable_ds) \
399         ((enable_ds) ? (ds_input_height) : (internal_height))
400
401 #define SH_CSS_MAX_STAGES 8 /* primary_stage[1-6], capture_pp, vf_pp */
402
403 /* For CSI2+ input system, it requires extra paddinga from vmem */
404 #ifdef CONFIG_CSI2_PLUS
405 #define _ISP_EXTRA_PADDING_VECS 2
406 #else
407 #define _ISP_EXTRA_PADDING_VECS 0
408 #endif /* CONFIG_CSI2_PLUS */
409
410 #endif /* _SH_CSS_DEFS_H_ */