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