Linux-libre 4.14.138-gnu
[librecmc/linux-libre.git] / drivers / staging / media / atomisp / pci / atomisp2 / css2400 / sh_css_param_dvs.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 "sh_css_param_dvs.h"
16 #include <assert_support.h>
17 #include <type_support.h>
18 #include <ia_css_err.h>
19 #include <ia_css_types.h>
20 #include "ia_css_debug.h"
21 #include "memory_access.h"
22
23 static struct ia_css_dvs_6axis_config *
24 alloc_dvs_6axis_table(const struct ia_css_resolution *frame_res, struct ia_css_dvs_6axis_config  *dvs_config_src)
25 {
26         unsigned int width_y = 0;
27         unsigned int height_y = 0;
28         unsigned int width_uv = 0;
29         unsigned int height_uv = 0;
30         enum ia_css_err err = IA_CSS_SUCCESS;
31         struct ia_css_dvs_6axis_config  *dvs_config = NULL;
32
33         dvs_config = (struct ia_css_dvs_6axis_config *)sh_css_malloc(sizeof(struct ia_css_dvs_6axis_config));
34         if (dvs_config == NULL) {
35                 IA_CSS_ERROR("out of memory");
36                 err = IA_CSS_ERR_CANNOT_ALLOCATE_MEMORY;
37         }
38         else
39         {       /*Initialize new struct with latest config settings*/
40                 if (NULL != dvs_config_src) {
41                         dvs_config->width_y = width_y = dvs_config_src->width_y;
42                         dvs_config->height_y = height_y = dvs_config_src->height_y;
43                         dvs_config->width_uv = width_uv = dvs_config_src->width_uv;
44                         dvs_config->height_uv = height_uv = dvs_config_src->height_uv;
45                         IA_CSS_LOG("alloc_dvs_6axis_table Y: W %d H %d", width_y, height_y);
46                 }
47                 else if (NULL != frame_res) {
48                         dvs_config->width_y = width_y = DVS_TABLE_IN_BLOCKDIM_X_LUMA(frame_res->width);
49                         dvs_config->height_y = height_y = DVS_TABLE_IN_BLOCKDIM_Y_LUMA(frame_res->height);
50                         dvs_config->width_uv = width_uv = DVS_TABLE_IN_BLOCKDIM_X_CHROMA(frame_res->width / 2); /* UV = Y/2, depens on colour format YUV 4.2.0*/
51                         dvs_config->height_uv = height_uv = DVS_TABLE_IN_BLOCKDIM_Y_CHROMA(frame_res->height / 2);/* UV = Y/2, depens on colour format YUV 4.2.0*/
52                         IA_CSS_LOG("alloc_dvs_6axis_table Y: W %d H %d", width_y, height_y);
53                 }
54
55                 /* Generate Y buffers  */
56                 dvs_config->xcoords_y = (uint32_t *)sh_css_malloc(width_y * height_y * sizeof(uint32_t));
57                 if (dvs_config->xcoords_y == NULL) {
58                         IA_CSS_ERROR("out of memory");
59                         err = IA_CSS_ERR_CANNOT_ALLOCATE_MEMORY;
60                         goto exit;
61                 }
62
63                 dvs_config->ycoords_y = (uint32_t *)sh_css_malloc(width_y * height_y * sizeof(uint32_t));
64                 if (dvs_config->ycoords_y == NULL) {
65                         IA_CSS_ERROR("out of memory");
66                         err = IA_CSS_ERR_CANNOT_ALLOCATE_MEMORY;
67                         goto exit;
68                 }
69
70                 /* Generate UV buffers  */
71                 IA_CSS_LOG("UV W %d H %d", width_uv, height_uv);
72
73                 dvs_config->xcoords_uv = (uint32_t *)sh_css_malloc(width_uv * height_uv * sizeof(uint32_t));
74                 if (dvs_config->xcoords_uv == NULL) {
75                         IA_CSS_ERROR("out of memory");
76                         err = IA_CSS_ERR_CANNOT_ALLOCATE_MEMORY;
77                         goto exit;
78                 }
79
80                 dvs_config->ycoords_uv = (uint32_t *)sh_css_malloc(width_uv * height_uv * sizeof(uint32_t));
81                 if (dvs_config->ycoords_uv == NULL) {
82                         IA_CSS_ERROR("out of memory");
83                         err = IA_CSS_ERR_CANNOT_ALLOCATE_MEMORY;
84                 }
85 exit:
86                 if (err != IA_CSS_SUCCESS) {
87                         free_dvs_6axis_table(&dvs_config); /* we might have allocated some memory, release this */
88                         dvs_config = NULL;
89                 }
90         }
91
92         IA_CSS_LEAVE("dvs_config=%p", dvs_config);
93         return dvs_config;
94 }
95
96 static void
97 init_dvs_6axis_table_from_default(struct ia_css_dvs_6axis_config *dvs_config, const struct ia_css_resolution *dvs_offset)
98 {
99         unsigned int x, y;
100         unsigned int width_y = dvs_config->width_y;
101         unsigned int height_y = dvs_config->height_y;
102         unsigned int width_uv = dvs_config->width_uv;
103         unsigned int height_uv = dvs_config->height_uv;
104
105         IA_CSS_LOG("Env_X=%d, Env_Y=%d, width_y=%d, height_y=%d",
106                            dvs_offset->width, dvs_offset->height, width_y, height_y);
107         for (y = 0; y < height_y; y++) {
108                 for (x = 0; x < width_y; x++) {
109                         dvs_config->xcoords_y[y*width_y + x] =  (dvs_offset->width + x*DVS_BLOCKDIM_X) << DVS_COORD_FRAC_BITS;
110                 }
111         }
112
113         for (y = 0; y < height_y; y++) {
114                 for (x = 0; x < width_y; x++) {
115                         dvs_config->ycoords_y[y*width_y + x] =  (dvs_offset->height + y*DVS_BLOCKDIM_Y_LUMA) << DVS_COORD_FRAC_BITS;
116                 }
117         }
118
119         for (y = 0; y < height_uv; y++) {
120                 for (x = 0; x < width_uv; x++) { /* Envelope dimensions set in Ypixels hence offset UV = offset Y/2 */
121                         dvs_config->xcoords_uv[y*width_uv + x] =  ((dvs_offset->width / 2) + x*DVS_BLOCKDIM_X) << DVS_COORD_FRAC_BITS;
122                 }
123         }
124
125         for (y = 0; y < height_uv; y++) {
126                 for (x = 0; x < width_uv; x++) { /* Envelope dimensions set in Ypixels hence offset UV = offset Y/2 */
127                         dvs_config->ycoords_uv[y*width_uv + x] =  ((dvs_offset->height / 2) + y*DVS_BLOCKDIM_Y_CHROMA) << DVS_COORD_FRAC_BITS;
128                 }
129         }
130
131 }
132
133 static void
134 init_dvs_6axis_table_from_config(struct ia_css_dvs_6axis_config *dvs_config, struct ia_css_dvs_6axis_config  *dvs_config_src)
135 {
136         unsigned int width_y = dvs_config->width_y;
137         unsigned int height_y = dvs_config->height_y;
138         unsigned int width_uv = dvs_config->width_uv;
139         unsigned int height_uv = dvs_config->height_uv;
140
141         memcpy(dvs_config->xcoords_y, dvs_config_src->xcoords_y, (width_y * height_y * sizeof(uint32_t)));
142         memcpy(dvs_config->ycoords_y, dvs_config_src->ycoords_y, (width_y * height_y * sizeof(uint32_t)));
143         memcpy(dvs_config->xcoords_uv, dvs_config_src->xcoords_uv, (width_uv * height_uv * sizeof(uint32_t)));
144         memcpy(dvs_config->ycoords_uv, dvs_config_src->ycoords_uv, (width_uv * height_uv * sizeof(uint32_t)));
145 }
146
147 struct ia_css_dvs_6axis_config *
148 generate_dvs_6axis_table(const struct ia_css_resolution *frame_res, const struct ia_css_resolution *dvs_offset)
149 {
150         struct ia_css_dvs_6axis_config *dvs_6axis_table;
151
152         assert(frame_res != NULL);
153         assert(dvs_offset != NULL);
154
155         dvs_6axis_table = alloc_dvs_6axis_table(frame_res, NULL);
156         if (dvs_6axis_table) {
157                 init_dvs_6axis_table_from_default(dvs_6axis_table, dvs_offset);
158                 return dvs_6axis_table;
159         }
160         return NULL;
161 }
162
163 struct ia_css_dvs_6axis_config *
164 generate_dvs_6axis_table_from_config(struct ia_css_dvs_6axis_config  *dvs_config_src)
165 {
166         struct ia_css_dvs_6axis_config *dvs_6axis_table;
167
168         assert(NULL != dvs_config_src);
169
170         dvs_6axis_table = alloc_dvs_6axis_table(NULL, dvs_config_src);
171         if (dvs_6axis_table) {
172                 init_dvs_6axis_table_from_config(dvs_6axis_table, dvs_config_src);
173                 return dvs_6axis_table;
174         }
175         return NULL;
176 }
177
178 void
179 free_dvs_6axis_table(struct ia_css_dvs_6axis_config  **dvs_6axis_config)
180 {
181         assert(dvs_6axis_config != NULL);
182         assert(*dvs_6axis_config != NULL);
183
184         if ((dvs_6axis_config != NULL) && (*dvs_6axis_config != NULL))
185         {
186                 IA_CSS_ENTER_PRIVATE("dvs_6axis_config %p", (*dvs_6axis_config));
187                 if ((*dvs_6axis_config)->xcoords_y != NULL)
188                 {
189                         sh_css_free((*dvs_6axis_config)->xcoords_y);
190                         (*dvs_6axis_config)->xcoords_y = NULL;
191                 }
192
193                 if ((*dvs_6axis_config)->ycoords_y != NULL)
194                 {
195                         sh_css_free((*dvs_6axis_config)->ycoords_y);
196                         (*dvs_6axis_config)->ycoords_y = NULL;
197                 }
198
199                 /* Free up UV buffers */
200                 if ((*dvs_6axis_config)->xcoords_uv != NULL)
201                 {
202                         sh_css_free((*dvs_6axis_config)->xcoords_uv);
203                         (*dvs_6axis_config)->xcoords_uv = NULL;
204                 }
205
206                 if ((*dvs_6axis_config)->ycoords_uv != NULL)
207                 {
208                         sh_css_free((*dvs_6axis_config)->ycoords_uv);
209                         (*dvs_6axis_config)->ycoords_uv = NULL;
210                 }
211
212                 IA_CSS_LEAVE_PRIVATE("dvs_6axis_config %p", (*dvs_6axis_config));
213                 sh_css_free(*dvs_6axis_config);
214                 *dvs_6axis_config = NULL;
215         }
216 }
217
218 void copy_dvs_6axis_table(struct ia_css_dvs_6axis_config *dvs_config_dst,
219                         const struct ia_css_dvs_6axis_config *dvs_config_src)
220 {
221         unsigned int width_y;
222         unsigned int height_y;
223         unsigned int width_uv;
224         unsigned int height_uv;
225
226         assert(dvs_config_src != NULL);
227         assert(dvs_config_dst != NULL);
228         assert(dvs_config_src->xcoords_y != NULL);
229         assert(dvs_config_src->xcoords_uv != NULL);
230         assert(dvs_config_src->ycoords_y != NULL);
231         assert(dvs_config_src->ycoords_uv != NULL);
232         assert(dvs_config_src->width_y == dvs_config_dst->width_y);
233         assert(dvs_config_src->width_uv == dvs_config_dst->width_uv);
234         assert(dvs_config_src->height_y == dvs_config_dst->height_y);
235         assert(dvs_config_src->height_uv == dvs_config_dst->height_uv);
236
237         width_y = dvs_config_src->width_y;
238         height_y = dvs_config_src->height_y;
239         width_uv = dvs_config_src->width_uv; /* = Y/2, depens on colour format YUV 4.2.0*/
240         height_uv = dvs_config_src->height_uv;
241
242         memcpy(dvs_config_dst->xcoords_y, dvs_config_src->xcoords_y, (width_y * height_y * sizeof(uint32_t)));
243         memcpy(dvs_config_dst->ycoords_y, dvs_config_src->ycoords_y, (width_y * height_y * sizeof(uint32_t)));
244
245         memcpy(dvs_config_dst->xcoords_uv, dvs_config_src->xcoords_uv, (width_uv * height_uv * sizeof(uint32_t)));
246         memcpy(dvs_config_dst->ycoords_uv, dvs_config_src->ycoords_uv, (width_uv * height_uv * sizeof(uint32_t)));
247
248 }
249
250 void
251 ia_css_dvs_statistics_get(enum dvs_statistics_type type,
252                           union ia_css_dvs_statistics_host  *host_stats,
253                           const union ia_css_dvs_statistics_isp *isp_stats)
254 {
255
256         if (DVS_STATISTICS == type)
257         {
258                 ia_css_get_dvs_statistics(host_stats->p_dvs_statistics_host,
259                         isp_stats->p_dvs_statistics_isp);
260         } else if (DVS2_STATISTICS == type)
261         {
262                 ia_css_get_dvs2_statistics(host_stats->p_dvs2_statistics_host,
263                         isp_stats->p_dvs_statistics_isp);
264         }
265         return;
266 }
267