1 /*
2  * Copyright (C) 2012 Texas Instruments Inc
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation version 2.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16  *
17  * Contributors:
18  *      Manjunath Hadli <manjunath.hadli@ti.com>
19  *      Prabhakar Lad <prabhakar.lad@ti.com>
20  *
21  *
22  * IPIPE allows fine tuning of the input image using different
23  * tuning modules in IPIPE. Some examples :- Noise filter, Defect
24  * pixel correction etc. It essentially operate on Bayer Raw data
25  * or YUV raw data. To do image tuning, application call,
26  *
27  */
28 
29 #include <linux/slab.h>
30 #include <linux/bitops.h>
31 
32 #include "dm365_ipipe.h"
33 #include "dm365_ipipe_hw.h"
34 #include "vpfe_mc_capture.h"
35 
36 #define MIN_OUT_WIDTH	32
37 #define MIN_OUT_HEIGHT	32
38 
39 /* ipipe input format's */
40 static const unsigned int ipipe_input_fmts[] = {
41 	MEDIA_BUS_FMT_UYVY8_2X8,
42 	MEDIA_BUS_FMT_SGRBG12_1X12,
43 	MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
44 	MEDIA_BUS_FMT_SGRBG10_ALAW8_1X8,
45 };
46 
47 /* ipipe output format's */
48 static const unsigned int ipipe_output_fmts[] = {
49 	MEDIA_BUS_FMT_UYVY8_2X8,
50 };
51 
ipipe_validate_lutdpc_params(struct vpfe_ipipe_lutdpc * lutdpc)52 static int ipipe_validate_lutdpc_params(struct vpfe_ipipe_lutdpc *lutdpc)
53 {
54 	int i;
55 
56 	if (lutdpc->en > 1 || lutdpc->repl_white > 1 ||
57 	    lutdpc->dpc_size > LUT_DPC_MAX_SIZE)
58 		return -EINVAL;
59 
60 	if (lutdpc->en)
61 		return -EINVAL;
62 
63 	for (i = 0; i < lutdpc->dpc_size; i++)
64 		if (lutdpc->table[i].horz_pos > LUT_DPC_H_POS_MASK ||
65 		   lutdpc->table[i].vert_pos > LUT_DPC_V_POS_MASK)
66 			return -EINVAL;
67 
68 	return 0;
69 }
70 
ipipe_set_lutdpc_params(struct vpfe_ipipe_device * ipipe,void * param)71 static int ipipe_set_lutdpc_params(struct vpfe_ipipe_device *ipipe, void *param)
72 {
73 	struct vpfe_ipipe_lutdpc *lutdpc = &ipipe->config.lutdpc;
74 	struct vpfe_ipipe_lutdpc *dpc_param;
75 
76 	if (!param) {
77 		memset((void *)lutdpc, 0, sizeof(struct vpfe_ipipe_lutdpc));
78 		goto success;
79 	}
80 
81 	dpc_param = param;
82 	lutdpc->en = dpc_param->en;
83 	lutdpc->repl_white = dpc_param->repl_white;
84 	lutdpc->dpc_size = dpc_param->dpc_size;
85 	memcpy(&lutdpc->table, &dpc_param->table,
86 	       (dpc_param->dpc_size * sizeof(struct vpfe_ipipe_lutdpc_entry)));
87 	if (ipipe_validate_lutdpc_params(lutdpc) < 0)
88 		return -EINVAL;
89 
90 success:
91 	ipipe_set_lutdpc_regs(ipipe->base_addr, ipipe->isp5_base_addr, lutdpc);
92 
93 	return 0;
94 }
95 
ipipe_get_lutdpc_params(struct vpfe_ipipe_device * ipipe,void * param)96 static int ipipe_get_lutdpc_params(struct vpfe_ipipe_device *ipipe, void *param)
97 {
98 	struct vpfe_ipipe_lutdpc *lut_param = param;
99 	struct vpfe_ipipe_lutdpc *lutdpc = &ipipe->config.lutdpc;
100 
101 	lut_param->en = lutdpc->en;
102 	lut_param->repl_white = lutdpc->repl_white;
103 	lut_param->dpc_size = lutdpc->dpc_size;
104 	memcpy(&lut_param->table, &lutdpc->table,
105 	   (lutdpc->dpc_size * sizeof(struct vpfe_ipipe_lutdpc_entry)));
106 
107 	return 0;
108 }
109 
ipipe_set_input_config(struct vpfe_ipipe_device * ipipe,void * param)110 static int ipipe_set_input_config(struct vpfe_ipipe_device *ipipe, void *param)
111 {
112 	struct vpfe_ipipe_input_config *config = &ipipe->config.input_config;
113 
114 	if (!param)
115 		memset(config, 0, sizeof(struct vpfe_ipipe_input_config));
116 	else
117 		memcpy(config, param, sizeof(struct vpfe_ipipe_input_config));
118 	return 0;
119 }
120 
ipipe_get_input_config(struct vpfe_ipipe_device * ipipe,void * param)121 static int ipipe_get_input_config(struct vpfe_ipipe_device *ipipe, void *param)
122 {
123 	struct vpfe_ipipe_input_config *config = &ipipe->config.input_config;
124 
125 	if (!param)
126 		return -EINVAL;
127 
128 	memcpy(param, config, sizeof(struct vpfe_ipipe_input_config));
129 
130 	return 0;
131 }
132 
ipipe_validate_otfdpc_params(struct vpfe_ipipe_otfdpc * dpc_param)133 static int ipipe_validate_otfdpc_params(struct vpfe_ipipe_otfdpc *dpc_param)
134 {
135 	struct vpfe_ipipe_otfdpc_2_0_cfg *dpc_2_0;
136 	struct vpfe_ipipe_otfdpc_3_0_cfg *dpc_3_0;
137 
138 	if (dpc_param->en > 1)
139 		return -EINVAL;
140 
141 	if (dpc_param->alg == VPFE_IPIPE_OTFDPC_2_0) {
142 		dpc_2_0 = &dpc_param->alg_cfg.dpc_2_0;
143 		if (dpc_2_0->det_thr.r > OTFDPC_DPC2_THR_MASK ||
144 		    dpc_2_0->det_thr.gr > OTFDPC_DPC2_THR_MASK ||
145 		    dpc_2_0->det_thr.gb > OTFDPC_DPC2_THR_MASK ||
146 		    dpc_2_0->det_thr.b > OTFDPC_DPC2_THR_MASK ||
147 		    dpc_2_0->corr_thr.r > OTFDPC_DPC2_THR_MASK ||
148 		    dpc_2_0->corr_thr.gr > OTFDPC_DPC2_THR_MASK ||
149 		    dpc_2_0->corr_thr.gb > OTFDPC_DPC2_THR_MASK ||
150 		    dpc_2_0->corr_thr.b > OTFDPC_DPC2_THR_MASK)
151 			return -EINVAL;
152 		return 0;
153 	}
154 
155 	dpc_3_0 = &dpc_param->alg_cfg.dpc_3_0;
156 
157 	if (dpc_3_0->act_adj_shf > OTF_DPC3_0_SHF_MASK ||
158 	    dpc_3_0->det_thr > OTF_DPC3_0_DET_MASK ||
159 	    dpc_3_0->det_slp > OTF_DPC3_0_SLP_MASK ||
160 	    dpc_3_0->det_thr_min > OTF_DPC3_0_DET_MASK ||
161 	    dpc_3_0->det_thr_max > OTF_DPC3_0_DET_MASK ||
162 	    dpc_3_0->corr_thr > OTF_DPC3_0_CORR_MASK ||
163 	    dpc_3_0->corr_slp > OTF_DPC3_0_SLP_MASK ||
164 	    dpc_3_0->corr_thr_min > OTF_DPC3_0_CORR_MASK ||
165 	    dpc_3_0->corr_thr_max > OTF_DPC3_0_CORR_MASK)
166 		return -EINVAL;
167 
168 	return 0;
169 }
170 
ipipe_set_otfdpc_params(struct vpfe_ipipe_device * ipipe,void * param)171 static int ipipe_set_otfdpc_params(struct vpfe_ipipe_device *ipipe, void *param)
172 {
173 	struct vpfe_ipipe_otfdpc *dpc_param = param;
174 	struct vpfe_ipipe_otfdpc *otfdpc = &ipipe->config.otfdpc;
175 	struct device *dev;
176 
177 	if (!param) {
178 		memset((void *)otfdpc, 0, sizeof(struct ipipe_otfdpc_2_0));
179 		goto success;
180 	}
181 	dev = ipipe->subdev.v4l2_dev->dev;
182 	memcpy(otfdpc, dpc_param, sizeof(struct vpfe_ipipe_otfdpc));
183 	if (ipipe_validate_otfdpc_params(otfdpc) < 0) {
184 		dev_err(dev, "Invalid otfdpc params\n");
185 		return -EINVAL;
186 	}
187 
188 success:
189 	ipipe_set_otfdpc_regs(ipipe->base_addr, otfdpc);
190 
191 	return 0;
192 }
193 
ipipe_get_otfdpc_params(struct vpfe_ipipe_device * ipipe,void * param)194 static int ipipe_get_otfdpc_params(struct vpfe_ipipe_device *ipipe, void *param)
195 {
196 	struct vpfe_ipipe_otfdpc *dpc_param = param;
197 	struct vpfe_ipipe_otfdpc *otfdpc = &ipipe->config.otfdpc;
198 
199 	memcpy(dpc_param, otfdpc, sizeof(struct vpfe_ipipe_otfdpc));
200 	return 0;
201 }
202 
ipipe_validate_nf_params(struct vpfe_ipipe_nf * nf_param)203 static int ipipe_validate_nf_params(struct vpfe_ipipe_nf *nf_param)
204 {
205 	int i;
206 
207 	if (nf_param->en > 1 || nf_param->shft_val > D2F_SHFT_VAL_MASK ||
208 	    nf_param->spread_val > D2F_SPR_VAL_MASK ||
209 	    nf_param->apply_lsc_gain > 1 ||
210 	    nf_param->edge_det_min_thr > D2F_EDGE_DET_THR_MASK ||
211 	    nf_param->edge_det_max_thr > D2F_EDGE_DET_THR_MASK)
212 		return -EINVAL;
213 
214 	for (i = 0; i < VPFE_IPIPE_NF_THR_TABLE_SIZE; i++)
215 		if (nf_param->thr[i] > D2F_THR_VAL_MASK)
216 			return -EINVAL;
217 
218 	for (i = 0; i < VPFE_IPIPE_NF_STR_TABLE_SIZE; i++)
219 		if (nf_param->str[i] > D2F_STR_VAL_MASK)
220 			return -EINVAL;
221 
222 	return 0;
223 }
224 
ipipe_set_nf_params(struct vpfe_ipipe_device * ipipe,unsigned int id,void * param)225 static int ipipe_set_nf_params(struct vpfe_ipipe_device *ipipe,
226 			       unsigned int id, void *param)
227 {
228 	struct vpfe_ipipe_nf *nf_param = param;
229 	struct vpfe_ipipe_nf *nf = &ipipe->config.nf1;
230 	struct device *dev;
231 
232 	if (id == IPIPE_D2F_2ND)
233 		nf = &ipipe->config.nf2;
234 
235 	if (!nf_param) {
236 		memset((void *)nf, 0, sizeof(struct vpfe_ipipe_nf));
237 		goto success;
238 	}
239 
240 	dev = ipipe->subdev.v4l2_dev->dev;
241 	memcpy(nf, nf_param, sizeof(struct vpfe_ipipe_nf));
242 	if (ipipe_validate_nf_params(nf) < 0) {
243 		dev_err(dev, "Invalid nf params\n");
244 		return -EINVAL;
245 	}
246 
247 success:
248 	ipipe_set_d2f_regs(ipipe->base_addr, id, nf);
249 
250 	return 0;
251 }
252 
ipipe_set_nf1_params(struct vpfe_ipipe_device * ipipe,void * param)253 static int ipipe_set_nf1_params(struct vpfe_ipipe_device *ipipe, void *param)
254 {
255 	return ipipe_set_nf_params(ipipe, IPIPE_D2F_1ST, param);
256 }
257 
ipipe_set_nf2_params(struct vpfe_ipipe_device * ipipe,void * param)258 static int ipipe_set_nf2_params(struct vpfe_ipipe_device *ipipe, void *param)
259 {
260 	return ipipe_set_nf_params(ipipe, IPIPE_D2F_2ND, param);
261 }
262 
ipipe_get_nf_params(struct vpfe_ipipe_device * ipipe,unsigned int id,void * param)263 static int ipipe_get_nf_params(struct vpfe_ipipe_device *ipipe,
264 			       unsigned int id, void *param)
265 {
266 	struct vpfe_ipipe_nf *nf_param = param;
267 	struct vpfe_ipipe_nf *nf = &ipipe->config.nf1;
268 
269 	if (id == IPIPE_D2F_2ND)
270 		nf = &ipipe->config.nf2;
271 
272 	memcpy(nf_param, nf, sizeof(struct vpfe_ipipe_nf));
273 
274 	return 0;
275 }
276 
ipipe_get_nf1_params(struct vpfe_ipipe_device * ipipe,void * param)277 static int ipipe_get_nf1_params(struct vpfe_ipipe_device *ipipe, void *param)
278 {
279 	return ipipe_get_nf_params(ipipe, IPIPE_D2F_1ST, param);
280 }
281 
ipipe_get_nf2_params(struct vpfe_ipipe_device * ipipe,void * param)282 static int ipipe_get_nf2_params(struct vpfe_ipipe_device *ipipe, void *param)
283 {
284 	return ipipe_get_nf_params(ipipe, IPIPE_D2F_2ND, param);
285 }
286 
ipipe_validate_gic_params(struct vpfe_ipipe_gic * gic)287 static int ipipe_validate_gic_params(struct vpfe_ipipe_gic *gic)
288 {
289 	if (gic->en > 1 || gic->gain > GIC_GAIN_MASK ||
290 	    gic->thr > GIC_THR_MASK || gic->slope > GIC_SLOPE_MASK ||
291 	    gic->apply_lsc_gain > 1 ||
292 	    gic->nf2_thr_gain.integer > GIC_NFGAN_INT_MASK ||
293 	    gic->nf2_thr_gain.decimal > GIC_NFGAN_DECI_MASK)
294 		return -EINVAL;
295 
296 	return 0;
297 }
298 
ipipe_set_gic_params(struct vpfe_ipipe_device * ipipe,void * param)299 static int ipipe_set_gic_params(struct vpfe_ipipe_device *ipipe, void *param)
300 {
301 	struct vpfe_ipipe_gic *gic_param = param;
302 	struct device *dev = ipipe->subdev.v4l2_dev->dev;
303 	struct vpfe_ipipe_gic *gic = &ipipe->config.gic;
304 
305 	if (!gic_param) {
306 		memset((void *)gic, 0, sizeof(struct vpfe_ipipe_gic));
307 		goto success;
308 	}
309 
310 	memcpy(gic, gic_param, sizeof(struct vpfe_ipipe_gic));
311 	if (ipipe_validate_gic_params(gic) < 0) {
312 		dev_err(dev, "Invalid gic params\n");
313 		return -EINVAL;
314 	}
315 
316 success:
317 	ipipe_set_gic_regs(ipipe->base_addr, gic);
318 
319 	return 0;
320 }
321 
ipipe_get_gic_params(struct vpfe_ipipe_device * ipipe,void * param)322 static int ipipe_get_gic_params(struct vpfe_ipipe_device *ipipe, void *param)
323 {
324 	struct vpfe_ipipe_gic *gic_param = param;
325 	struct vpfe_ipipe_gic *gic = &ipipe->config.gic;
326 
327 	memcpy(gic_param, gic, sizeof(struct vpfe_ipipe_gic));
328 
329 	return 0;
330 }
331 
ipipe_validate_wb_params(struct vpfe_ipipe_wb * wbal)332 static int ipipe_validate_wb_params(struct vpfe_ipipe_wb *wbal)
333 {
334 	if (wbal->ofst_r > WB_OFFSET_MASK ||
335 	    wbal->ofst_gr > WB_OFFSET_MASK ||
336 	    wbal->ofst_gb > WB_OFFSET_MASK ||
337 	    wbal->ofst_b > WB_OFFSET_MASK ||
338 	    wbal->gain_r.integer > WB_GAIN_INT_MASK ||
339 	    wbal->gain_r.decimal > WB_GAIN_DECI_MASK ||
340 	    wbal->gain_gr.integer > WB_GAIN_INT_MASK ||
341 	    wbal->gain_gr.decimal > WB_GAIN_DECI_MASK ||
342 	    wbal->gain_gb.integer > WB_GAIN_INT_MASK ||
343 	    wbal->gain_gb.decimal > WB_GAIN_DECI_MASK ||
344 	    wbal->gain_b.integer > WB_GAIN_INT_MASK ||
345 	    wbal->gain_b.decimal > WB_GAIN_DECI_MASK)
346 		return -EINVAL;
347 
348 	return 0;
349 }
350 
ipipe_set_wb_params(struct vpfe_ipipe_device * ipipe,void * param)351 static int ipipe_set_wb_params(struct vpfe_ipipe_device *ipipe, void *param)
352 {
353 	struct vpfe_ipipe_wb *wb_param = param;
354 	struct vpfe_ipipe_wb *wbal = &ipipe->config.wbal;
355 
356 	if (!wb_param) {
357 		const struct vpfe_ipipe_wb wb_defaults = {
358 			.gain_r  = {2, 0x0},
359 			.gain_gr = {2, 0x0},
360 			.gain_gb = {2, 0x0},
361 			.gain_b  = {2, 0x0}
362 		};
363 		memcpy(wbal, &wb_defaults, sizeof(struct vpfe_ipipe_wb));
364 		goto success;
365 	}
366 
367 	memcpy(wbal, wb_param, sizeof(struct vpfe_ipipe_wb));
368 	if (ipipe_validate_wb_params(wbal) < 0)
369 		return -EINVAL;
370 
371 success:
372 	ipipe_set_wb_regs(ipipe->base_addr, wbal);
373 
374 	return 0;
375 }
376 
ipipe_get_wb_params(struct vpfe_ipipe_device * ipipe,void * param)377 static int ipipe_get_wb_params(struct vpfe_ipipe_device *ipipe, void *param)
378 {
379 	struct vpfe_ipipe_wb *wb_param = param;
380 	struct vpfe_ipipe_wb *wbal = &ipipe->config.wbal;
381 
382 	memcpy(wb_param, wbal, sizeof(struct vpfe_ipipe_wb));
383 	return 0;
384 }
385 
ipipe_validate_cfa_params(struct vpfe_ipipe_cfa * cfa)386 static int ipipe_validate_cfa_params(struct vpfe_ipipe_cfa *cfa)
387 {
388 	if (cfa->hpf_thr_2dir > CFA_HPF_THR_2DIR_MASK ||
389 	    cfa->hpf_slp_2dir > CFA_HPF_SLOPE_2DIR_MASK ||
390 	    cfa->hp_mix_thr_2dir > CFA_HPF_MIX_THR_2DIR_MASK ||
391 	    cfa->hp_mix_slope_2dir > CFA_HPF_MIX_SLP_2DIR_MASK ||
392 	    cfa->dir_thr_2dir > CFA_DIR_THR_2DIR_MASK ||
393 	    cfa->dir_slope_2dir > CFA_DIR_SLP_2DIR_MASK ||
394 	    cfa->nd_wt_2dir > CFA_ND_WT_2DIR_MASK ||
395 	    cfa->hue_fract_daa > CFA_DAA_HUE_FRA_MASK ||
396 	    cfa->edge_thr_daa > CFA_DAA_EDG_THR_MASK ||
397 	    cfa->thr_min_daa > CFA_DAA_THR_MIN_MASK ||
398 	    cfa->thr_slope_daa > CFA_DAA_THR_SLP_MASK ||
399 	    cfa->slope_min_daa > CFA_DAA_SLP_MIN_MASK ||
400 	    cfa->slope_slope_daa > CFA_DAA_SLP_SLP_MASK ||
401 	    cfa->lp_wt_daa > CFA_DAA_LP_WT_MASK)
402 		return -EINVAL;
403 
404 	return 0;
405 }
406 
ipipe_set_cfa_params(struct vpfe_ipipe_device * ipipe,void * param)407 static int ipipe_set_cfa_params(struct vpfe_ipipe_device *ipipe, void *param)
408 {
409 	struct vpfe_ipipe_cfa *cfa_param = param;
410 	struct vpfe_ipipe_cfa *cfa = &ipipe->config.cfa;
411 
412 	if (!cfa_param) {
413 		memset(cfa, 0, sizeof(struct vpfe_ipipe_cfa));
414 		cfa->alg = VPFE_IPIPE_CFA_ALG_2DIRAC;
415 		goto success;
416 	}
417 
418 	memcpy(cfa, cfa_param, sizeof(struct vpfe_ipipe_cfa));
419 	if (ipipe_validate_cfa_params(cfa) < 0)
420 		return -EINVAL;
421 
422 success:
423 	ipipe_set_cfa_regs(ipipe->base_addr, cfa);
424 
425 	return 0;
426 }
427 
ipipe_get_cfa_params(struct vpfe_ipipe_device * ipipe,void * param)428 static int ipipe_get_cfa_params(struct vpfe_ipipe_device *ipipe, void *param)
429 {
430 	struct vpfe_ipipe_cfa *cfa_param = param;
431 	struct vpfe_ipipe_cfa *cfa = &ipipe->config.cfa;
432 
433 	memcpy(cfa_param, cfa, sizeof(struct vpfe_ipipe_cfa));
434 	return 0;
435 }
436 
437 static int
ipipe_validate_rgb2rgb_params(struct vpfe_ipipe_rgb2rgb * rgb2rgb,unsigned int id)438 ipipe_validate_rgb2rgb_params(struct vpfe_ipipe_rgb2rgb *rgb2rgb,
439 			      unsigned int id)
440 {
441 	u32 gain_int_upper = RGB2RGB_1_GAIN_INT_MASK;
442 	u32 offset_upper = RGB2RGB_1_OFST_MASK;
443 
444 	if (id == IPIPE_RGB2RGB_2) {
445 		offset_upper = RGB2RGB_2_OFST_MASK;
446 		gain_int_upper = RGB2RGB_2_GAIN_INT_MASK;
447 	}
448 
449 	if (rgb2rgb->coef_rr.decimal > RGB2RGB_GAIN_DECI_MASK ||
450 	    rgb2rgb->coef_rr.integer > gain_int_upper)
451 		return -EINVAL;
452 
453 	if (rgb2rgb->coef_gr.decimal > RGB2RGB_GAIN_DECI_MASK ||
454 	    rgb2rgb->coef_gr.integer > gain_int_upper)
455 		return -EINVAL;
456 
457 	if (rgb2rgb->coef_br.decimal > RGB2RGB_GAIN_DECI_MASK ||
458 	    rgb2rgb->coef_br.integer > gain_int_upper)
459 		return -EINVAL;
460 
461 	if (rgb2rgb->coef_rg.decimal > RGB2RGB_GAIN_DECI_MASK ||
462 	    rgb2rgb->coef_rg.integer > gain_int_upper)
463 		return -EINVAL;
464 
465 	if (rgb2rgb->coef_gg.decimal > RGB2RGB_GAIN_DECI_MASK ||
466 	    rgb2rgb->coef_gg.integer > gain_int_upper)
467 		return -EINVAL;
468 
469 	if (rgb2rgb->coef_bg.decimal > RGB2RGB_GAIN_DECI_MASK ||
470 	    rgb2rgb->coef_bg.integer > gain_int_upper)
471 		return -EINVAL;
472 
473 	if (rgb2rgb->coef_rb.decimal > RGB2RGB_GAIN_DECI_MASK ||
474 	    rgb2rgb->coef_rb.integer > gain_int_upper)
475 		return -EINVAL;
476 
477 	if (rgb2rgb->coef_gb.decimal > RGB2RGB_GAIN_DECI_MASK ||
478 	    rgb2rgb->coef_gb.integer > gain_int_upper)
479 		return -EINVAL;
480 
481 	if (rgb2rgb->coef_bb.decimal > RGB2RGB_GAIN_DECI_MASK ||
482 	    rgb2rgb->coef_bb.integer > gain_int_upper)
483 		return -EINVAL;
484 
485 	if (rgb2rgb->out_ofst_r > offset_upper ||
486 	    rgb2rgb->out_ofst_g > offset_upper ||
487 	    rgb2rgb->out_ofst_b > offset_upper)
488 		return -EINVAL;
489 
490 	return 0;
491 }
492 
ipipe_set_rgb2rgb_params(struct vpfe_ipipe_device * ipipe,unsigned int id,void * param)493 static int ipipe_set_rgb2rgb_params(struct vpfe_ipipe_device *ipipe,
494 			      unsigned int id, void *param)
495 {
496 	struct vpfe_ipipe_rgb2rgb *rgb2rgb = &ipipe->config.rgb2rgb1;
497 	struct device *dev = ipipe->subdev.v4l2_dev->dev;
498 	struct vpfe_ipipe_rgb2rgb *rgb2rgb_param;
499 
500 	rgb2rgb_param = param;
501 
502 	if (id == IPIPE_RGB2RGB_2)
503 		rgb2rgb = &ipipe->config.rgb2rgb2;
504 
505 	if (!rgb2rgb_param) {
506 		const struct vpfe_ipipe_rgb2rgb rgb2rgb_defaults = {
507 			.coef_rr = {1, 0},	/* 256 */
508 			.coef_gr = {0, 0},
509 			.coef_br = {0, 0},
510 			.coef_rg = {0, 0},
511 			.coef_gg = {1, 0},	/* 256 */
512 			.coef_bg = {0, 0},
513 			.coef_rb = {0, 0},
514 			.coef_gb = {0, 0},
515 			.coef_bb = {1, 0},	/* 256 */
516 		};
517 		/* Copy defaults for rgb2rgb conversion */
518 		memcpy(rgb2rgb, &rgb2rgb_defaults,
519 		       sizeof(struct vpfe_ipipe_rgb2rgb));
520 		goto success;
521 	}
522 
523 	memcpy(rgb2rgb, rgb2rgb_param, sizeof(struct vpfe_ipipe_rgb2rgb));
524 	if (ipipe_validate_rgb2rgb_params(rgb2rgb, id) < 0) {
525 		dev_err(dev, "Invalid rgb2rgb params\n");
526 		return -EINVAL;
527 	}
528 
529 success:
530 	ipipe_set_rgb2rgb_regs(ipipe->base_addr, id, rgb2rgb);
531 
532 	return 0;
533 }
534 
535 static int
ipipe_set_rgb2rgb_1_params(struct vpfe_ipipe_device * ipipe,void * param)536 ipipe_set_rgb2rgb_1_params(struct vpfe_ipipe_device *ipipe, void *param)
537 {
538 	return ipipe_set_rgb2rgb_params(ipipe, IPIPE_RGB2RGB_1, param);
539 }
540 
541 static int
ipipe_set_rgb2rgb_2_params(struct vpfe_ipipe_device * ipipe,void * param)542 ipipe_set_rgb2rgb_2_params(struct vpfe_ipipe_device *ipipe, void *param)
543 {
544 	return ipipe_set_rgb2rgb_params(ipipe, IPIPE_RGB2RGB_2, param);
545 }
546 
ipipe_get_rgb2rgb_params(struct vpfe_ipipe_device * ipipe,unsigned int id,void * param)547 static int ipipe_get_rgb2rgb_params(struct vpfe_ipipe_device *ipipe,
548 			      unsigned int id, void *param)
549 {
550 	struct vpfe_ipipe_rgb2rgb *rgb2rgb = &ipipe->config.rgb2rgb1;
551 	struct vpfe_ipipe_rgb2rgb *rgb2rgb_param;
552 
553 	rgb2rgb_param = param;
554 
555 	if (id == IPIPE_RGB2RGB_2)
556 		rgb2rgb = &ipipe->config.rgb2rgb2;
557 
558 	memcpy(rgb2rgb_param, rgb2rgb, sizeof(struct vpfe_ipipe_rgb2rgb));
559 
560 	return 0;
561 }
562 
563 static int
ipipe_get_rgb2rgb_1_params(struct vpfe_ipipe_device * ipipe,void * param)564 ipipe_get_rgb2rgb_1_params(struct vpfe_ipipe_device *ipipe, void *param)
565 {
566 	return ipipe_get_rgb2rgb_params(ipipe, IPIPE_RGB2RGB_1, param);
567 }
568 
569 static int
ipipe_get_rgb2rgb_2_params(struct vpfe_ipipe_device * ipipe,void * param)570 ipipe_get_rgb2rgb_2_params(struct vpfe_ipipe_device *ipipe, void *param)
571 {
572 	return ipipe_get_rgb2rgb_params(ipipe, IPIPE_RGB2RGB_2, param);
573 }
574 
575 static int
ipipe_validate_gamma_entry(struct vpfe_ipipe_gamma_entry * table,int size)576 ipipe_validate_gamma_entry(struct vpfe_ipipe_gamma_entry *table, int size)
577 {
578 	int i;
579 
580 	if (!table)
581 		return -EINVAL;
582 
583 	for (i = 0; i < size; i++)
584 		if (table[i].slope > GAMMA_MASK ||
585 		    table[i].offset > GAMMA_MASK)
586 			return -EINVAL;
587 
588 	return 0;
589 }
590 
591 static int
ipipe_validate_gamma_params(struct vpfe_ipipe_gamma * gamma,struct device * dev)592 ipipe_validate_gamma_params(struct vpfe_ipipe_gamma *gamma, struct device *dev)
593 {
594 	int table_size;
595 	int err;
596 
597 	if (gamma->bypass_r > 1 ||
598 	    gamma->bypass_b > 1 ||
599 	    gamma->bypass_g > 1)
600 		return -EINVAL;
601 
602 	if (gamma->tbl_sel != VPFE_IPIPE_GAMMA_TBL_RAM)
603 		return 0;
604 
605 	table_size = gamma->tbl_size;
606 	if (!gamma->bypass_r) {
607 		err = ipipe_validate_gamma_entry(gamma->table_r, table_size);
608 		if (err) {
609 			dev_err(dev, "GAMMA R - table entry invalid\n");
610 			return err;
611 		}
612 	}
613 
614 	if (!gamma->bypass_b) {
615 		err = ipipe_validate_gamma_entry(gamma->table_b, table_size);
616 		if (err) {
617 			dev_err(dev, "GAMMA B - table entry invalid\n");
618 			return err;
619 		}
620 	}
621 
622 	if (!gamma->bypass_g) {
623 		err = ipipe_validate_gamma_entry(gamma->table_g, table_size);
624 		if (err) {
625 			dev_err(dev, "GAMMA G - table entry invalid\n");
626 			return err;
627 		}
628 	}
629 
630 	return 0;
631 }
632 
633 static int
ipipe_set_gamma_params(struct vpfe_ipipe_device * ipipe,void * param)634 ipipe_set_gamma_params(struct vpfe_ipipe_device *ipipe, void *param)
635 {
636 	struct vpfe_ipipe_gamma *gamma_param = param;
637 	struct vpfe_ipipe_gamma *gamma = &ipipe->config.gamma;
638 	struct device *dev = ipipe->subdev.v4l2_dev->dev;
639 	int table_size;
640 
641 	if (!gamma_param) {
642 		memset(gamma, 0, sizeof(struct vpfe_ipipe_gamma));
643 		gamma->tbl_sel = VPFE_IPIPE_GAMMA_TBL_ROM;
644 		goto success;
645 	}
646 
647 	gamma->bypass_r = gamma_param->bypass_r;
648 	gamma->bypass_b = gamma_param->bypass_b;
649 	gamma->bypass_g = gamma_param->bypass_g;
650 	gamma->tbl_sel = gamma_param->tbl_sel;
651 	gamma->tbl_size = gamma_param->tbl_size;
652 
653 	if (ipipe_validate_gamma_params(gamma, dev) < 0)
654 		return -EINVAL;
655 
656 	if (gamma_param->tbl_sel != VPFE_IPIPE_GAMMA_TBL_RAM)
657 		goto success;
658 
659 	table_size = gamma->tbl_size;
660 	if (!gamma_param->bypass_r)
661 		memcpy(&gamma->table_r, &gamma_param->table_r,
662 		       (table_size * sizeof(struct vpfe_ipipe_gamma_entry)));
663 
664 	if (!gamma_param->bypass_b)
665 		memcpy(&gamma->table_b, &gamma_param->table_b,
666 		       (table_size * sizeof(struct vpfe_ipipe_gamma_entry)));
667 
668 	if (!gamma_param->bypass_g)
669 		memcpy(&gamma->table_g, &gamma_param->table_g,
670 		       (table_size * sizeof(struct vpfe_ipipe_gamma_entry)));
671 
672 success:
673 	ipipe_set_gamma_regs(ipipe->base_addr, ipipe->isp5_base_addr, gamma);
674 
675 	return 0;
676 }
677 
ipipe_get_gamma_params(struct vpfe_ipipe_device * ipipe,void * param)678 static int ipipe_get_gamma_params(struct vpfe_ipipe_device *ipipe, void *param)
679 {
680 	struct vpfe_ipipe_gamma *gamma_param = param;
681 	struct vpfe_ipipe_gamma *gamma = &ipipe->config.gamma;
682 	struct device *dev = ipipe->subdev.v4l2_dev->dev;
683 	int table_size;
684 
685 	gamma_param->bypass_r = gamma->bypass_r;
686 	gamma_param->bypass_g = gamma->bypass_g;
687 	gamma_param->bypass_b = gamma->bypass_b;
688 	gamma_param->tbl_sel = gamma->tbl_sel;
689 	gamma_param->tbl_size = gamma->tbl_size;
690 
691 	if (gamma->tbl_sel != VPFE_IPIPE_GAMMA_TBL_RAM)
692 		return 0;
693 
694 	table_size = gamma->tbl_size;
695 
696 	if (!gamma->bypass_r) {
697 		dev_err(dev,
698 			"ipipe_get_gamma_params: table ptr empty for R\n");
699 		return -EINVAL;
700 	}
701 	memcpy(gamma_param->table_r, gamma->table_r,
702 	       (table_size * sizeof(struct vpfe_ipipe_gamma_entry)));
703 
704 	if (!gamma->bypass_g) {
705 		dev_err(dev, "ipipe_get_gamma_params: table ptr empty for G\n");
706 		return -EINVAL;
707 	}
708 	memcpy(gamma_param->table_g, gamma->table_g,
709 	       (table_size * sizeof(struct vpfe_ipipe_gamma_entry)));
710 
711 	if (!gamma->bypass_b) {
712 		dev_err(dev, "ipipe_get_gamma_params: table ptr empty for B\n");
713 		return -EINVAL;
714 	}
715 	memcpy(gamma_param->table_b, gamma->table_b,
716 	       (table_size * sizeof(struct vpfe_ipipe_gamma_entry)));
717 
718 	return 0;
719 }
720 
ipipe_validate_3d_lut_params(struct vpfe_ipipe_3d_lut * lut)721 static int ipipe_validate_3d_lut_params(struct vpfe_ipipe_3d_lut *lut)
722 {
723 	int i;
724 
725 	if (!lut->en)
726 		return 0;
727 
728 	for (i = 0; i < VPFE_IPIPE_MAX_SIZE_3D_LUT; i++)
729 		if (lut->table[i].r > D3_LUT_ENTRY_MASK ||
730 		    lut->table[i].g > D3_LUT_ENTRY_MASK ||
731 		    lut->table[i].b > D3_LUT_ENTRY_MASK)
732 			return -EINVAL;
733 
734 	return 0;
735 }
736 
ipipe_get_3d_lut_params(struct vpfe_ipipe_device * ipipe,void * param)737 static int ipipe_get_3d_lut_params(struct vpfe_ipipe_device *ipipe, void *param)
738 {
739 	struct vpfe_ipipe_3d_lut *lut_param = param;
740 	struct vpfe_ipipe_3d_lut *lut = &ipipe->config.lut;
741 
742 	lut_param->en = lut->en;
743 
744 	memcpy(lut_param->table, &lut->table,
745 	       (VPFE_IPIPE_MAX_SIZE_3D_LUT *
746 	       sizeof(struct vpfe_ipipe_3d_lut_entry)));
747 
748 	return 0;
749 }
750 
751 static int
ipipe_set_3d_lut_params(struct vpfe_ipipe_device * ipipe,void * param)752 ipipe_set_3d_lut_params(struct vpfe_ipipe_device *ipipe, void *param)
753 {
754 	struct vpfe_ipipe_3d_lut *lut_param = param;
755 	struct vpfe_ipipe_3d_lut *lut = &ipipe->config.lut;
756 	struct device *dev = ipipe->subdev.v4l2_dev->dev;
757 
758 	if (!lut_param) {
759 		memset(lut, 0, sizeof(struct vpfe_ipipe_3d_lut));
760 		goto success;
761 	}
762 
763 	memcpy(lut, lut_param, sizeof(struct vpfe_ipipe_3d_lut));
764 	if (ipipe_validate_3d_lut_params(lut) < 0) {
765 		dev_err(dev, "Invalid 3D-LUT Params\n");
766 		return -EINVAL;
767 	}
768 
769 success:
770 	ipipe_set_3d_lut_regs(ipipe->base_addr, ipipe->isp5_base_addr, lut);
771 
772 	return 0;
773 }
774 
ipipe_validate_rgb2yuv_params(struct vpfe_ipipe_rgb2yuv * rgb2yuv)775 static int ipipe_validate_rgb2yuv_params(struct vpfe_ipipe_rgb2yuv *rgb2yuv)
776 {
777 	if (rgb2yuv->coef_ry.decimal > RGB2YCBCR_COEF_DECI_MASK ||
778 	   rgb2yuv->coef_ry.integer > RGB2YCBCR_COEF_INT_MASK)
779 		return -EINVAL;
780 
781 	if (rgb2yuv->coef_gy.decimal > RGB2YCBCR_COEF_DECI_MASK ||
782 	   rgb2yuv->coef_gy.integer > RGB2YCBCR_COEF_INT_MASK)
783 		return -EINVAL;
784 
785 	if (rgb2yuv->coef_by.decimal > RGB2YCBCR_COEF_DECI_MASK ||
786 	   rgb2yuv->coef_by.integer > RGB2YCBCR_COEF_INT_MASK)
787 		return -EINVAL;
788 
789 	if (rgb2yuv->coef_rcb.decimal > RGB2YCBCR_COEF_DECI_MASK ||
790 	   rgb2yuv->coef_rcb.integer > RGB2YCBCR_COEF_INT_MASK)
791 		return -EINVAL;
792 
793 	if (rgb2yuv->coef_gcb.decimal > RGB2YCBCR_COEF_DECI_MASK ||
794 	   rgb2yuv->coef_gcb.integer > RGB2YCBCR_COEF_INT_MASK)
795 		return -EINVAL;
796 
797 	if (rgb2yuv->coef_bcb.decimal > RGB2YCBCR_COEF_DECI_MASK ||
798 	   rgb2yuv->coef_bcb.integer > RGB2YCBCR_COEF_INT_MASK)
799 		return -EINVAL;
800 
801 	if (rgb2yuv->coef_rcr.decimal > RGB2YCBCR_COEF_DECI_MASK ||
802 	   rgb2yuv->coef_rcr.integer > RGB2YCBCR_COEF_INT_MASK)
803 		return -EINVAL;
804 
805 	if (rgb2yuv->coef_gcr.decimal > RGB2YCBCR_COEF_DECI_MASK ||
806 	   rgb2yuv->coef_gcr.integer > RGB2YCBCR_COEF_INT_MASK)
807 		return -EINVAL;
808 
809 	if (rgb2yuv->coef_bcr.decimal > RGB2YCBCR_COEF_DECI_MASK ||
810 	   rgb2yuv->coef_bcr.integer > RGB2YCBCR_COEF_INT_MASK)
811 		return -EINVAL;
812 
813 	if (rgb2yuv->out_ofst_y > RGB2YCBCR_OFST_MASK ||
814 	   rgb2yuv->out_ofst_cb > RGB2YCBCR_OFST_MASK ||
815 	   rgb2yuv->out_ofst_cr > RGB2YCBCR_OFST_MASK)
816 		return -EINVAL;
817 
818 	return 0;
819 }
820 
821 static int
ipipe_set_rgb2yuv_params(struct vpfe_ipipe_device * ipipe,void * param)822 ipipe_set_rgb2yuv_params(struct vpfe_ipipe_device *ipipe, void *param)
823 {
824 	struct vpfe_ipipe_rgb2yuv *rgb2yuv = &ipipe->config.rgb2yuv;
825 	struct device *dev = ipipe->subdev.v4l2_dev->dev;
826 	struct vpfe_ipipe_rgb2yuv *rgb2yuv_param;
827 
828 	rgb2yuv_param = param;
829 	if (!rgb2yuv_param) {
830 		/* Defaults for rgb2yuv conversion */
831 		const struct vpfe_ipipe_rgb2yuv rgb2yuv_defaults = {
832 			.coef_ry  = {0, 0x4d},
833 			.coef_gy  = {0, 0x96},
834 			.coef_by  = {0, 0x1d},
835 			.coef_rcb = {0xf, 0xd5},
836 			.coef_gcb = {0xf, 0xab},
837 			.coef_bcb = {0, 0x80},
838 			.coef_rcr = {0, 0x80},
839 			.coef_gcr = {0xf, 0x95},
840 			.coef_bcr = {0xf, 0xeb},
841 			.out_ofst_cb = 0x80,
842 			.out_ofst_cr = 0x80,
843 		};
844 		/* Copy defaults for rgb2yuv conversion  */
845 		memcpy(rgb2yuv, &rgb2yuv_defaults,
846 		       sizeof(struct vpfe_ipipe_rgb2yuv));
847 		goto success;
848 	}
849 
850 	memcpy(rgb2yuv, rgb2yuv_param, sizeof(struct vpfe_ipipe_rgb2yuv));
851 	if (ipipe_validate_rgb2yuv_params(rgb2yuv) < 0) {
852 		dev_err(dev, "Invalid rgb2yuv params\n");
853 		return -EINVAL;
854 	}
855 
856 success:
857 	ipipe_set_rgb2ycbcr_regs(ipipe->base_addr, rgb2yuv);
858 
859 	return 0;
860 }
861 
862 static int
ipipe_get_rgb2yuv_params(struct vpfe_ipipe_device * ipipe,void * param)863 ipipe_get_rgb2yuv_params(struct vpfe_ipipe_device *ipipe, void *param)
864 {
865 	struct vpfe_ipipe_rgb2yuv *rgb2yuv = &ipipe->config.rgb2yuv;
866 	struct vpfe_ipipe_rgb2yuv *rgb2yuv_param;
867 
868 	rgb2yuv_param = param;
869 	memcpy(rgb2yuv_param, rgb2yuv, sizeof(struct vpfe_ipipe_rgb2yuv));
870 	return 0;
871 }
872 
ipipe_validate_gbce_params(struct vpfe_ipipe_gbce * gbce)873 static int ipipe_validate_gbce_params(struct vpfe_ipipe_gbce *gbce)
874 {
875 	u32 max = GBCE_Y_VAL_MASK;
876 	int i;
877 
878 	if (!gbce->en)
879 		return 0;
880 
881 	if (gbce->type == VPFE_IPIPE_GBCE_GAIN_TBL)
882 		max = GBCE_GAIN_VAL_MASK;
883 
884 	for (i = 0; i < VPFE_IPIPE_MAX_SIZE_GBCE_LUT; i++)
885 		if (gbce->table[i] > max)
886 			return -EINVAL;
887 
888 	return 0;
889 }
890 
ipipe_set_gbce_params(struct vpfe_ipipe_device * ipipe,void * param)891 static int ipipe_set_gbce_params(struct vpfe_ipipe_device *ipipe, void *param)
892 {
893 	struct vpfe_ipipe_gbce *gbce_param = param;
894 	struct vpfe_ipipe_gbce *gbce = &ipipe->config.gbce;
895 	struct device *dev = ipipe->subdev.v4l2_dev->dev;
896 
897 	if (!gbce_param) {
898 		memset(gbce, 0, sizeof(struct vpfe_ipipe_gbce));
899 	} else {
900 		memcpy(gbce, gbce_param, sizeof(struct vpfe_ipipe_gbce));
901 		if (ipipe_validate_gbce_params(gbce) < 0) {
902 			dev_err(dev, "Invalid gbce params\n");
903 			return -EINVAL;
904 		}
905 	}
906 
907 	ipipe_set_gbce_regs(ipipe->base_addr, ipipe->isp5_base_addr, gbce);
908 
909 	return 0;
910 }
911 
ipipe_get_gbce_params(struct vpfe_ipipe_device * ipipe,void * param)912 static int ipipe_get_gbce_params(struct vpfe_ipipe_device *ipipe, void *param)
913 {
914 	struct vpfe_ipipe_gbce *gbce_param = param;
915 	struct vpfe_ipipe_gbce *gbce = &ipipe->config.gbce;
916 
917 	gbce_param->en = gbce->en;
918 	gbce_param->type = gbce->type;
919 
920 	memcpy(gbce_param->table, gbce->table,
921 		(VPFE_IPIPE_MAX_SIZE_GBCE_LUT * sizeof(unsigned short)));
922 
923 	return 0;
924 }
925 
926 static int
ipipe_validate_yuv422_conv_params(struct vpfe_ipipe_yuv422_conv * yuv422_conv)927 ipipe_validate_yuv422_conv_params(struct vpfe_ipipe_yuv422_conv *yuv422_conv)
928 {
929 	if (yuv422_conv->en_chrom_lpf > 1)
930 		return -EINVAL;
931 
932 	return 0;
933 }
934 
935 static int
ipipe_set_yuv422_conv_params(struct vpfe_ipipe_device * ipipe,void * param)936 ipipe_set_yuv422_conv_params(struct vpfe_ipipe_device *ipipe, void *param)
937 {
938 	struct vpfe_ipipe_yuv422_conv *yuv422_conv = &ipipe->config.yuv422_conv;
939 	struct vpfe_ipipe_yuv422_conv *yuv422_conv_param;
940 	struct device *dev = ipipe->subdev.v4l2_dev->dev;
941 
942 	yuv422_conv_param = param;
943 	if (!yuv422_conv_param) {
944 		memset(yuv422_conv, 0, sizeof(struct vpfe_ipipe_yuv422_conv));
945 		yuv422_conv->chrom_pos = VPFE_IPIPE_YUV422_CHR_POS_COSITE;
946 	} else {
947 		memcpy(yuv422_conv, yuv422_conv_param,
948 			sizeof(struct vpfe_ipipe_yuv422_conv));
949 		if (ipipe_validate_yuv422_conv_params(yuv422_conv) < 0) {
950 			dev_err(dev, "Invalid yuv422 params\n");
951 			return -EINVAL;
952 		}
953 	}
954 
955 	ipipe_set_yuv422_conv_regs(ipipe->base_addr, yuv422_conv);
956 
957 	return 0;
958 }
959 
960 static int
ipipe_get_yuv422_conv_params(struct vpfe_ipipe_device * ipipe,void * param)961 ipipe_get_yuv422_conv_params(struct vpfe_ipipe_device *ipipe, void *param)
962 {
963 	struct vpfe_ipipe_yuv422_conv *yuv422_conv = &ipipe->config.yuv422_conv;
964 	struct vpfe_ipipe_yuv422_conv *yuv422_conv_param;
965 
966 	yuv422_conv_param = param;
967 	memcpy(yuv422_conv_param, yuv422_conv,
968 	       sizeof(struct vpfe_ipipe_yuv422_conv));
969 
970 	return 0;
971 }
972 
ipipe_validate_yee_params(struct vpfe_ipipe_yee * yee)973 static int ipipe_validate_yee_params(struct vpfe_ipipe_yee *yee)
974 {
975 	int i;
976 
977 	if (yee->en > 1 ||
978 	    yee->en_halo_red > 1 ||
979 	    yee->hpf_shft > YEE_HPF_SHIFT_MASK)
980 		return -EINVAL;
981 
982 	if (yee->hpf_coef_00 > YEE_COEF_MASK ||
983 	    yee->hpf_coef_01 > YEE_COEF_MASK ||
984 	    yee->hpf_coef_02 > YEE_COEF_MASK ||
985 	    yee->hpf_coef_10 > YEE_COEF_MASK ||
986 	    yee->hpf_coef_11 > YEE_COEF_MASK ||
987 	    yee->hpf_coef_12 > YEE_COEF_MASK ||
988 	    yee->hpf_coef_20 > YEE_COEF_MASK ||
989 	    yee->hpf_coef_21 > YEE_COEF_MASK ||
990 	    yee->hpf_coef_22 > YEE_COEF_MASK)
991 		return -EINVAL;
992 
993 	if (yee->yee_thr > YEE_THR_MASK ||
994 	    yee->es_gain > YEE_ES_GAIN_MASK ||
995 	    yee->es_thr1 > YEE_ES_THR1_MASK ||
996 	    yee->es_thr2 > YEE_THR_MASK ||
997 	    yee->es_gain_grad > YEE_THR_MASK ||
998 	    yee->es_ofst_grad > YEE_THR_MASK)
999 		return -EINVAL;
1000 
1001 	for (i = 0; i < VPFE_IPIPE_MAX_SIZE_YEE_LUT; i++)
1002 		if (yee->table[i] > YEE_ENTRY_MASK)
1003 			return -EINVAL;
1004 
1005 	return 0;
1006 }
1007 
ipipe_set_yee_params(struct vpfe_ipipe_device * ipipe,void * param)1008 static int ipipe_set_yee_params(struct vpfe_ipipe_device *ipipe, void *param)
1009 {
1010 	struct vpfe_ipipe_yee *yee_param = param;
1011 	struct device *dev = ipipe->subdev.v4l2_dev->dev;
1012 	struct vpfe_ipipe_yee *yee = &ipipe->config.yee;
1013 
1014 	if (!yee_param) {
1015 		memset(yee, 0, sizeof(struct vpfe_ipipe_yee));
1016 	} else {
1017 		memcpy(yee, yee_param, sizeof(struct vpfe_ipipe_yee));
1018 		if (ipipe_validate_yee_params(yee) < 0) {
1019 			dev_err(dev, "Invalid yee params\n");
1020 			return -EINVAL;
1021 		}
1022 	}
1023 
1024 	ipipe_set_ee_regs(ipipe->base_addr, ipipe->isp5_base_addr, yee);
1025 
1026 	return 0;
1027 }
1028 
ipipe_get_yee_params(struct vpfe_ipipe_device * ipipe,void * param)1029 static int ipipe_get_yee_params(struct vpfe_ipipe_device *ipipe, void *param)
1030 {
1031 	struct vpfe_ipipe_yee *yee_param = param;
1032 	struct vpfe_ipipe_yee *yee = &ipipe->config.yee;
1033 
1034 	yee_param->en = yee->en;
1035 	yee_param->en_halo_red = yee->en_halo_red;
1036 	yee_param->merge_meth = yee->merge_meth;
1037 	yee_param->hpf_shft = yee->hpf_shft;
1038 	yee_param->hpf_coef_00 = yee->hpf_coef_00;
1039 	yee_param->hpf_coef_01 = yee->hpf_coef_01;
1040 	yee_param->hpf_coef_02 = yee->hpf_coef_02;
1041 	yee_param->hpf_coef_10 = yee->hpf_coef_10;
1042 	yee_param->hpf_coef_11 = yee->hpf_coef_11;
1043 	yee_param->hpf_coef_12 = yee->hpf_coef_12;
1044 	yee_param->hpf_coef_20 = yee->hpf_coef_20;
1045 	yee_param->hpf_coef_21 = yee->hpf_coef_21;
1046 	yee_param->hpf_coef_22 = yee->hpf_coef_22;
1047 	yee_param->yee_thr = yee->yee_thr;
1048 	yee_param->es_gain = yee->es_gain;
1049 	yee_param->es_thr1 = yee->es_thr1;
1050 	yee_param->es_thr2 = yee->es_thr2;
1051 	yee_param->es_gain_grad = yee->es_gain_grad;
1052 	yee_param->es_ofst_grad = yee->es_ofst_grad;
1053 	memcpy(yee_param->table, &yee->table,
1054 	       (VPFE_IPIPE_MAX_SIZE_YEE_LUT * sizeof(short)));
1055 
1056 	return 0;
1057 }
1058 
ipipe_validate_car_params(struct vpfe_ipipe_car * car)1059 static int ipipe_validate_car_params(struct vpfe_ipipe_car *car)
1060 {
1061 	if (car->en > 1 || car->hpf_shft > CAR_HPF_SHIFT_MASK ||
1062 	    car->gain1.shft > CAR_GAIN1_SHFT_MASK ||
1063 	    car->gain1.gain_min > CAR_GAIN_MIN_MASK ||
1064 	    car->gain2.shft > CAR_GAIN2_SHFT_MASK ||
1065 	    car->gain2.gain_min > CAR_GAIN_MIN_MASK)
1066 		return -EINVAL;
1067 
1068 	return 0;
1069 }
1070 
ipipe_set_car_params(struct vpfe_ipipe_device * ipipe,void * param)1071 static int ipipe_set_car_params(struct vpfe_ipipe_device *ipipe, void *param)
1072 {
1073 	struct vpfe_ipipe_car *car_param = param;
1074 	struct device *dev = ipipe->subdev.v4l2_dev->dev;
1075 	struct vpfe_ipipe_car *car = &ipipe->config.car;
1076 
1077 	if (!car_param) {
1078 		memset(car, 0, sizeof(struct vpfe_ipipe_car));
1079 	} else {
1080 		memcpy(car, car_param, sizeof(struct vpfe_ipipe_car));
1081 		if (ipipe_validate_car_params(car) < 0) {
1082 			dev_err(dev, "Invalid car params\n");
1083 			return -EINVAL;
1084 		}
1085 	}
1086 
1087 	ipipe_set_car_regs(ipipe->base_addr, car);
1088 
1089 	return 0;
1090 }
1091 
ipipe_get_car_params(struct vpfe_ipipe_device * ipipe,void * param)1092 static int ipipe_get_car_params(struct vpfe_ipipe_device *ipipe, void *param)
1093 {
1094 	struct vpfe_ipipe_car *car_param = param;
1095 	struct vpfe_ipipe_car *car = &ipipe->config.car;
1096 
1097 	memcpy(car_param, car, sizeof(struct vpfe_ipipe_car));
1098 	return 0;
1099 }
1100 
ipipe_validate_cgs_params(struct vpfe_ipipe_cgs * cgs)1101 static int ipipe_validate_cgs_params(struct vpfe_ipipe_cgs *cgs)
1102 {
1103 	if (cgs->en > 1 || cgs->h_shft > CAR_SHIFT_MASK)
1104 		return -EINVAL;
1105 
1106 	return 0;
1107 }
1108 
ipipe_set_cgs_params(struct vpfe_ipipe_device * ipipe,void * param)1109 static int ipipe_set_cgs_params(struct vpfe_ipipe_device *ipipe, void *param)
1110 {
1111 	struct vpfe_ipipe_cgs *cgs_param = param;
1112 	struct device *dev = ipipe->subdev.v4l2_dev->dev;
1113 	struct vpfe_ipipe_cgs *cgs = &ipipe->config.cgs;
1114 
1115 	if (!cgs_param) {
1116 		memset(cgs, 0, sizeof(struct vpfe_ipipe_cgs));
1117 	} else {
1118 		memcpy(cgs, cgs_param, sizeof(struct vpfe_ipipe_cgs));
1119 		if (ipipe_validate_cgs_params(cgs) < 0) {
1120 			dev_err(dev, "Invalid cgs params\n");
1121 			return -EINVAL;
1122 		}
1123 	}
1124 
1125 	ipipe_set_cgs_regs(ipipe->base_addr, cgs);
1126 
1127 	return 0;
1128 }
1129 
ipipe_get_cgs_params(struct vpfe_ipipe_device * ipipe,void * param)1130 static int ipipe_get_cgs_params(struct vpfe_ipipe_device *ipipe, void *param)
1131 {
1132 	struct vpfe_ipipe_cgs *cgs_param = param;
1133 	struct vpfe_ipipe_cgs *cgs = &ipipe->config.cgs;
1134 
1135 	memcpy(cgs_param, cgs, sizeof(struct vpfe_ipipe_cgs));
1136 
1137 	return 0;
1138 }
1139 
1140 static const struct ipipe_module_if ipipe_modules[VPFE_IPIPE_MAX_MODULES] = {
1141 	/* VPFE_IPIPE_INPUT_CONFIG */ {
1142 		offsetof(struct ipipe_module_params, input_config),
1143 		FIELD_SIZEOF(struct ipipe_module_params, input_config),
1144 		offsetof(struct vpfe_ipipe_config, input_config),
1145 		ipipe_set_input_config,
1146 		ipipe_get_input_config,
1147 	}, /* VPFE_IPIPE_LUTDPC */ {
1148 		offsetof(struct ipipe_module_params, lutdpc),
1149 		FIELD_SIZEOF(struct ipipe_module_params, lutdpc),
1150 		offsetof(struct vpfe_ipipe_config, lutdpc),
1151 		ipipe_set_lutdpc_params,
1152 		ipipe_get_lutdpc_params,
1153 	}, /* VPFE_IPIPE_OTFDPC */ {
1154 		offsetof(struct ipipe_module_params, otfdpc),
1155 		FIELD_SIZEOF(struct ipipe_module_params, otfdpc),
1156 		offsetof(struct vpfe_ipipe_config, otfdpc),
1157 		ipipe_set_otfdpc_params,
1158 		ipipe_get_otfdpc_params,
1159 	}, /* VPFE_IPIPE_NF1 */ {
1160 		offsetof(struct ipipe_module_params, nf1),
1161 		FIELD_SIZEOF(struct ipipe_module_params, nf1),
1162 		offsetof(struct vpfe_ipipe_config, nf1),
1163 		ipipe_set_nf1_params,
1164 		ipipe_get_nf1_params,
1165 	}, /* VPFE_IPIPE_NF2 */ {
1166 		offsetof(struct ipipe_module_params, nf2),
1167 		FIELD_SIZEOF(struct ipipe_module_params, nf2),
1168 		offsetof(struct vpfe_ipipe_config, nf2),
1169 		ipipe_set_nf2_params,
1170 		ipipe_get_nf2_params,
1171 	}, /* VPFE_IPIPE_WB */ {
1172 		offsetof(struct ipipe_module_params, wbal),
1173 		FIELD_SIZEOF(struct ipipe_module_params, wbal),
1174 		offsetof(struct vpfe_ipipe_config, wbal),
1175 		ipipe_set_wb_params,
1176 		ipipe_get_wb_params,
1177 	}, /* VPFE_IPIPE_RGB2RGB_1 */ {
1178 		offsetof(struct ipipe_module_params, rgb2rgb1),
1179 		FIELD_SIZEOF(struct ipipe_module_params, rgb2rgb1),
1180 		offsetof(struct vpfe_ipipe_config, rgb2rgb1),
1181 		ipipe_set_rgb2rgb_1_params,
1182 		ipipe_get_rgb2rgb_1_params,
1183 	}, /* VPFE_IPIPE_RGB2RGB_2 */ {
1184 		offsetof(struct ipipe_module_params, rgb2rgb2),
1185 		FIELD_SIZEOF(struct ipipe_module_params, rgb2rgb2),
1186 		offsetof(struct vpfe_ipipe_config, rgb2rgb2),
1187 		ipipe_set_rgb2rgb_2_params,
1188 		ipipe_get_rgb2rgb_2_params,
1189 	}, /* VPFE_IPIPE_GAMMA */ {
1190 		offsetof(struct ipipe_module_params, gamma),
1191 		FIELD_SIZEOF(struct ipipe_module_params, gamma),
1192 		offsetof(struct vpfe_ipipe_config, gamma),
1193 		ipipe_set_gamma_params,
1194 		ipipe_get_gamma_params,
1195 	}, /* VPFE_IPIPE_3D_LUT */ {
1196 		offsetof(struct ipipe_module_params, lut),
1197 		FIELD_SIZEOF(struct ipipe_module_params, lut),
1198 		offsetof(struct vpfe_ipipe_config, lut),
1199 		ipipe_set_3d_lut_params,
1200 		ipipe_get_3d_lut_params,
1201 	}, /* VPFE_IPIPE_RGB2YUV */ {
1202 		offsetof(struct ipipe_module_params, rgb2yuv),
1203 		FIELD_SIZEOF(struct ipipe_module_params, rgb2yuv),
1204 		offsetof(struct vpfe_ipipe_config, rgb2yuv),
1205 		ipipe_set_rgb2yuv_params,
1206 		ipipe_get_rgb2yuv_params,
1207 	}, /* VPFE_IPIPE_YUV422_CONV */ {
1208 		offsetof(struct ipipe_module_params, yuv422_conv),
1209 		FIELD_SIZEOF(struct ipipe_module_params, yuv422_conv),
1210 		offsetof(struct vpfe_ipipe_config, yuv422_conv),
1211 		ipipe_set_yuv422_conv_params,
1212 		ipipe_get_yuv422_conv_params,
1213 	}, /* VPFE_IPIPE_YEE */ {
1214 		offsetof(struct ipipe_module_params, yee),
1215 		FIELD_SIZEOF(struct ipipe_module_params, yee),
1216 		offsetof(struct vpfe_ipipe_config, yee),
1217 		ipipe_set_yee_params,
1218 		ipipe_get_yee_params,
1219 	}, /* VPFE_IPIPE_GIC */ {
1220 		offsetof(struct ipipe_module_params, gic),
1221 		FIELD_SIZEOF(struct ipipe_module_params, gic),
1222 		offsetof(struct vpfe_ipipe_config, gic),
1223 		ipipe_set_gic_params,
1224 		ipipe_get_gic_params,
1225 	}, /* VPFE_IPIPE_CFA */ {
1226 		offsetof(struct ipipe_module_params, cfa),
1227 		FIELD_SIZEOF(struct ipipe_module_params, cfa),
1228 		offsetof(struct vpfe_ipipe_config, cfa),
1229 		ipipe_set_cfa_params,
1230 		ipipe_get_cfa_params,
1231 	}, /* VPFE_IPIPE_CAR */ {
1232 		offsetof(struct ipipe_module_params, car),
1233 		FIELD_SIZEOF(struct ipipe_module_params, car),
1234 		offsetof(struct vpfe_ipipe_config, car),
1235 		ipipe_set_car_params,
1236 		ipipe_get_car_params,
1237 	}, /* VPFE_IPIPE_CGS */ {
1238 		offsetof(struct ipipe_module_params, cgs),
1239 		FIELD_SIZEOF(struct ipipe_module_params, cgs),
1240 		offsetof(struct vpfe_ipipe_config, cgs),
1241 		ipipe_set_cgs_params,
1242 		ipipe_get_cgs_params,
1243 	}, /* VPFE_IPIPE_GBCE */ {
1244 		offsetof(struct ipipe_module_params, gbce),
1245 		FIELD_SIZEOF(struct ipipe_module_params, gbce),
1246 		offsetof(struct vpfe_ipipe_config, gbce),
1247 		ipipe_set_gbce_params,
1248 		ipipe_get_gbce_params,
1249 	},
1250 };
1251 
ipipe_s_config(struct v4l2_subdev * sd,struct vpfe_ipipe_config * cfg)1252 static int ipipe_s_config(struct v4l2_subdev *sd, struct vpfe_ipipe_config *cfg)
1253 {
1254 	struct vpfe_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
1255 	unsigned int i;
1256 	int rval = 0;
1257 
1258 	for (i = 0; i < ARRAY_SIZE(ipipe_modules); i++) {
1259 		const struct ipipe_module_if *module_if;
1260 		struct ipipe_module_params *params;
1261 		void *from, *to;
1262 		size_t size;
1263 
1264 		if (!(cfg->flag & BIT(i)))
1265 			continue;
1266 
1267 		module_if = &ipipe_modules[i];
1268 		from = *(void **)((void *)cfg + module_if->config_offset);
1269 
1270 		params = kmalloc(sizeof(struct ipipe_module_params),
1271 				 GFP_KERNEL);
1272 		to = (void *)params + module_if->param_offset;
1273 		size = module_if->param_size;
1274 
1275 		if (to && from && size) {
1276 			if (copy_from_user(to, (void __user *)from, size)) {
1277 				rval = -EFAULT;
1278 				break;
1279 			}
1280 			rval = module_if->set(ipipe, to);
1281 			if (rval)
1282 				goto error;
1283 		} else if (to && !from && size) {
1284 			rval = module_if->set(ipipe, NULL);
1285 			if (rval)
1286 				goto error;
1287 		}
1288 		kfree(params);
1289 	}
1290 error:
1291 	return rval;
1292 }
1293 
ipipe_g_config(struct v4l2_subdev * sd,struct vpfe_ipipe_config * cfg)1294 static int ipipe_g_config(struct v4l2_subdev *sd, struct vpfe_ipipe_config *cfg)
1295 {
1296 	struct vpfe_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
1297 	unsigned int i;
1298 	int rval = 0;
1299 
1300 	for (i = 1; i < ARRAY_SIZE(ipipe_modules); i++) {
1301 		const struct ipipe_module_if *module_if;
1302 		struct ipipe_module_params *params;
1303 		void *from, *to;
1304 		size_t size;
1305 
1306 		if (!(cfg->flag & BIT(i)))
1307 			continue;
1308 
1309 		module_if = &ipipe_modules[i];
1310 		to = *(void **)((void *)cfg + module_if->config_offset);
1311 
1312 		params = kmalloc(sizeof(struct ipipe_module_params),
1313 				 GFP_KERNEL);
1314 		from = (void *)params + module_if->param_offset;
1315 		size = module_if->param_size;
1316 
1317 		if (to && from && size) {
1318 			rval = module_if->get(ipipe, from);
1319 			if (rval)
1320 				goto error;
1321 			if (copy_to_user((void __user *)to, from, size)) {
1322 				rval = -EFAULT;
1323 				break;
1324 			}
1325 		}
1326 		kfree(params);
1327 	}
1328 error:
1329 	return rval;
1330 }
1331 
1332 /*
1333  * ipipe_ioctl() - Handle ipipe module private ioctl's
1334  * @sd: pointer to v4l2 subdev structure
1335  * @cmd: configuration command
1336  * @arg: configuration argument
1337  */
ipipe_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)1338 static long ipipe_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1339 {
1340 	switch (cmd) {
1341 	case VIDIOC_VPFE_IPIPE_S_CONFIG:
1342 		return ipipe_s_config(sd, arg);
1343 
1344 	case VIDIOC_VPFE_IPIPE_G_CONFIG:
1345 		return ipipe_g_config(sd, arg);
1346 
1347 	default:
1348 		return -ENOIOCTLCMD;
1349 	}
1350 }
1351 
vpfe_ipipe_enable(struct vpfe_device * vpfe_dev,int en)1352 void vpfe_ipipe_enable(struct vpfe_device *vpfe_dev, int en)
1353 {
1354 	struct vpfe_ipipeif_device *ipipeif = &vpfe_dev->vpfe_ipipeif;
1355 	struct vpfe_ipipe_device *ipipe = &vpfe_dev->vpfe_ipipe;
1356 	unsigned char val;
1357 
1358 	if (ipipe->input == IPIPE_INPUT_NONE)
1359 		return;
1360 
1361 	/* ipipe is set to single shot */
1362 	if (ipipeif->input == IPIPEIF_INPUT_MEMORY && en) {
1363 		/* for single-shot mode, need to wait for h/w to
1364 		 * reset many register bits
1365 		 */
1366 		do {
1367 			val = regr_ip(vpfe_dev->vpfe_ipipe.base_addr,
1368 				      IPIPE_SRC_EN);
1369 		} while (val);
1370 	}
1371 	regw_ip(vpfe_dev->vpfe_ipipe.base_addr, en, IPIPE_SRC_EN);
1372 }
1373 
1374 /*
1375  * ipipe_set_stream() - Enable/Disable streaming on the ipipe subdevice
1376  * @sd: pointer to v4l2 subdev structure
1377  * @enable: 1 == Enable, 0 == Disable
1378  */
ipipe_set_stream(struct v4l2_subdev * sd,int enable)1379 static int ipipe_set_stream(struct v4l2_subdev *sd, int enable)
1380 {
1381 	struct vpfe_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
1382 	struct vpfe_device *vpfe_dev = to_vpfe_device(ipipe);
1383 
1384 	if (enable && ipipe->input != IPIPE_INPUT_NONE &&
1385 		ipipe->output != IPIPE_OUTPUT_NONE) {
1386 		if (config_ipipe_hw(ipipe) < 0)
1387 			return -EINVAL;
1388 	}
1389 
1390 	vpfe_ipipe_enable(vpfe_dev, enable);
1391 
1392 	return 0;
1393 }
1394 
1395 /*
1396  * __ipipe_get_format() - helper function for getting ipipe format
1397  * @ipipe: pointer to ipipe private structure.
1398  * @pad: pad number.
1399  * @cfg: V4L2 subdev pad config
1400  * @which: wanted subdev format.
1401  *
1402  */
1403 static struct v4l2_mbus_framefmt *
__ipipe_get_format(struct vpfe_ipipe_device * ipipe,struct v4l2_subdev_pad_config * cfg,unsigned int pad,enum v4l2_subdev_format_whence which)1404 __ipipe_get_format(struct vpfe_ipipe_device *ipipe,
1405 		       struct v4l2_subdev_pad_config *cfg, unsigned int pad,
1406 		       enum v4l2_subdev_format_whence which)
1407 {
1408 	if (which == V4L2_SUBDEV_FORMAT_TRY)
1409 		return v4l2_subdev_get_try_format(&ipipe->subdev, cfg, pad);
1410 
1411 	return &ipipe->formats[pad];
1412 }
1413 
1414 /*
1415  * ipipe_try_format() - Handle try format by pad subdev method
1416  * @ipipe: VPFE ipipe device.
1417  * @cfg: V4L2 subdev pad config
1418  * @pad: pad num.
1419  * @fmt: pointer to v4l2 format structure.
1420  * @which : wanted subdev format
1421  */
1422 static void
ipipe_try_format(struct vpfe_ipipe_device * ipipe,struct v4l2_subdev_pad_config * cfg,unsigned int pad,struct v4l2_mbus_framefmt * fmt,enum v4l2_subdev_format_whence which)1423 ipipe_try_format(struct vpfe_ipipe_device *ipipe,
1424 		   struct v4l2_subdev_pad_config *cfg, unsigned int pad,
1425 		   struct v4l2_mbus_framefmt *fmt,
1426 		   enum v4l2_subdev_format_whence which)
1427 {
1428 	unsigned int max_out_height;
1429 	unsigned int max_out_width;
1430 	unsigned int i;
1431 
1432 	max_out_width = IPIPE_MAX_OUTPUT_WIDTH_A;
1433 	max_out_height = IPIPE_MAX_OUTPUT_HEIGHT_A;
1434 
1435 	if (pad == IPIPE_PAD_SINK) {
1436 		for (i = 0; i < ARRAY_SIZE(ipipe_input_fmts); i++)
1437 			if (fmt->code == ipipe_input_fmts[i])
1438 				break;
1439 
1440 		/* If not found, use SBGGR10 as default */
1441 		if (i >= ARRAY_SIZE(ipipe_input_fmts))
1442 			fmt->code = MEDIA_BUS_FMT_SGRBG12_1X12;
1443 	} else if (pad == IPIPE_PAD_SOURCE) {
1444 		for (i = 0; i < ARRAY_SIZE(ipipe_output_fmts); i++)
1445 			if (fmt->code == ipipe_output_fmts[i])
1446 				break;
1447 
1448 		/* If not found, use UYVY as default */
1449 		if (i >= ARRAY_SIZE(ipipe_output_fmts))
1450 			fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
1451 	}
1452 
1453 	fmt->width = clamp_t(u32, fmt->width, MIN_OUT_HEIGHT, max_out_width);
1454 	fmt->height = clamp_t(u32, fmt->height, MIN_OUT_WIDTH, max_out_height);
1455 }
1456 
1457 /*
1458  * ipipe_set_format() - Handle set format by pads subdev method
1459  * @sd: pointer to v4l2 subdev structure
1460  * @cfg: V4L2 subdev pad config
1461  * @fmt: pointer to v4l2 subdev format structure
1462  * return -EINVAL or zero on success
1463  */
1464 static int
ipipe_set_format(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)1465 ipipe_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
1466 		     struct v4l2_subdev_format *fmt)
1467 {
1468 	struct vpfe_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
1469 	struct v4l2_mbus_framefmt *format;
1470 
1471 	format = __ipipe_get_format(ipipe, cfg, fmt->pad, fmt->which);
1472 	if (format == NULL)
1473 		return -EINVAL;
1474 
1475 	ipipe_try_format(ipipe, cfg, fmt->pad, &fmt->format, fmt->which);
1476 	*format = fmt->format;
1477 
1478 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
1479 		return 0;
1480 
1481 	if (fmt->pad == IPIPE_PAD_SINK &&
1482 	   (ipipe->input == IPIPE_INPUT_CCDC ||
1483 	    ipipe->input == IPIPE_INPUT_MEMORY))
1484 		ipipe->formats[fmt->pad] = fmt->format;
1485 	else if (fmt->pad == IPIPE_PAD_SOURCE &&
1486 		ipipe->output == IPIPE_OUTPUT_RESIZER)
1487 		ipipe->formats[fmt->pad] = fmt->format;
1488 	else
1489 		return -EINVAL;
1490 
1491 	return 0;
1492 }
1493 
1494 /*
1495  * ipipe_get_format() - Handle get format by pads subdev method.
1496  * @sd: pointer to v4l2 subdev structure.
1497  * @cfg: V4L2 subdev pad config
1498  * @fmt: pointer to v4l2 subdev format structure.
1499  */
1500 static int
ipipe_get_format(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)1501 ipipe_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
1502 		     struct v4l2_subdev_format *fmt)
1503 {
1504 	struct vpfe_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
1505 
1506 	if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1507 		fmt->format = ipipe->formats[fmt->pad];
1508 	else
1509 		fmt->format = *(v4l2_subdev_get_try_format(sd, cfg, fmt->pad));
1510 
1511 	return 0;
1512 }
1513 
1514 /*
1515  * ipipe_enum_frame_size() - enum frame sizes on pads
1516  * @sd: pointer to v4l2 subdev structure.
1517  * @cfg: V4L2 subdev pad config
1518  * @fse: pointer to v4l2_subdev_frame_size_enum structure.
1519  */
1520 static int
ipipe_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)1521 ipipe_enum_frame_size(struct v4l2_subdev *sd,
1522 		       struct v4l2_subdev_pad_config *cfg,
1523 		       struct v4l2_subdev_frame_size_enum *fse)
1524 {
1525 	struct vpfe_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
1526 	struct v4l2_mbus_framefmt format;
1527 
1528 	if (fse->index != 0)
1529 		return -EINVAL;
1530 
1531 	format.code = fse->code;
1532 	format.width = 1;
1533 	format.height = 1;
1534 	ipipe_try_format(ipipe, cfg, fse->pad, &format, fse->which);
1535 	fse->min_width = format.width;
1536 	fse->min_height = format.height;
1537 
1538 	if (format.code != fse->code)
1539 		return -EINVAL;
1540 
1541 	format.code = fse->code;
1542 	format.width = -1;
1543 	format.height = -1;
1544 	ipipe_try_format(ipipe, cfg, fse->pad, &format, fse->which);
1545 	fse->max_width = format.width;
1546 	fse->max_height = format.height;
1547 
1548 	return 0;
1549 }
1550 
1551 /*
1552  * ipipe_enum_mbus_code() - enum mbus codes for pads
1553  * @sd: pointer to v4l2 subdev structure.
1554  * @cfg: V4L2 subdev pad config
1555  * @code: pointer to v4l2_subdev_mbus_code_enum structure
1556  */
1557 static int
ipipe_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)1558 ipipe_enum_mbus_code(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
1559 		     struct v4l2_subdev_mbus_code_enum *code)
1560 {
1561 	switch (code->pad) {
1562 	case IPIPE_PAD_SINK:
1563 		if (code->index >= ARRAY_SIZE(ipipe_input_fmts))
1564 			return -EINVAL;
1565 		code->code = ipipe_input_fmts[code->index];
1566 		break;
1567 
1568 	case IPIPE_PAD_SOURCE:
1569 		if (code->index >= ARRAY_SIZE(ipipe_output_fmts))
1570 			return -EINVAL;
1571 		code->code = ipipe_output_fmts[code->index];
1572 		break;
1573 
1574 	default:
1575 		return -EINVAL;
1576 	}
1577 
1578 	return 0;
1579 }
1580 
1581 /*
1582  * ipipe_s_ctrl() - Handle set control subdev method
1583  * @ctrl: pointer to v4l2 control structure
1584  */
ipipe_s_ctrl(struct v4l2_ctrl * ctrl)1585 static int ipipe_s_ctrl(struct v4l2_ctrl *ctrl)
1586 {
1587 	struct vpfe_ipipe_device *ipipe =
1588 	     container_of(ctrl->handler, struct vpfe_ipipe_device, ctrls);
1589 	struct ipipe_lum_adj *lum_adj = &ipipe->config.lum_adj;
1590 
1591 	switch (ctrl->id) {
1592 	case V4L2_CID_BRIGHTNESS:
1593 		lum_adj->brightness = ctrl->val;
1594 		ipipe_set_lum_adj_regs(ipipe->base_addr, lum_adj);
1595 		break;
1596 
1597 	case V4L2_CID_CONTRAST:
1598 		lum_adj->contrast = ctrl->val;
1599 		ipipe_set_lum_adj_regs(ipipe->base_addr, lum_adj);
1600 		break;
1601 
1602 	default:
1603 		return -EINVAL;
1604 	}
1605 
1606 	return 0;
1607 }
1608 
1609 /*
1610  * ipipe_init_formats() - Initialize formats on all pads
1611  * @sd: pointer to v4l2 subdev structure.
1612  * @fh: V4L2 subdev file handle
1613  *
1614  * Initialize all pad formats with default values. Try formats are initialized
1615  * on the file handle.
1616  */
1617 static int
ipipe_init_formats(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)1618 ipipe_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1619 {
1620 	struct v4l2_subdev_format format;
1621 
1622 	memset(&format, 0, sizeof(format));
1623 	format.pad = IPIPE_PAD_SINK;
1624 	format.which = V4L2_SUBDEV_FORMAT_TRY;
1625 	format.format.code = MEDIA_BUS_FMT_SGRBG12_1X12;
1626 	format.format.width = IPIPE_MAX_OUTPUT_WIDTH_A;
1627 	format.format.height = IPIPE_MAX_OUTPUT_HEIGHT_A;
1628 	ipipe_set_format(sd, fh->pad, &format);
1629 
1630 	memset(&format, 0, sizeof(format));
1631 	format.pad = IPIPE_PAD_SOURCE;
1632 	format.which = V4L2_SUBDEV_FORMAT_TRY;
1633 	format.format.code = MEDIA_BUS_FMT_UYVY8_2X8;
1634 	format.format.width = IPIPE_MAX_OUTPUT_WIDTH_A;
1635 	format.format.height = IPIPE_MAX_OUTPUT_HEIGHT_A;
1636 	ipipe_set_format(sd, fh->pad, &format);
1637 
1638 	return 0;
1639 }
1640 
1641 /* subdev core operations */
1642 static const struct v4l2_subdev_core_ops ipipe_v4l2_core_ops = {
1643 	.ioctl = ipipe_ioctl,
1644 };
1645 
1646 static const struct v4l2_ctrl_ops ipipe_ctrl_ops = {
1647 	.s_ctrl = ipipe_s_ctrl,
1648 };
1649 
1650 /* subdev file operations */
1651 static const struct  v4l2_subdev_internal_ops ipipe_v4l2_internal_ops = {
1652 	.open = ipipe_init_formats,
1653 };
1654 
1655 /* subdev video operations */
1656 static const struct v4l2_subdev_video_ops ipipe_v4l2_video_ops = {
1657 	.s_stream = ipipe_set_stream,
1658 };
1659 
1660 /* subdev pad operations */
1661 static const struct v4l2_subdev_pad_ops ipipe_v4l2_pad_ops = {
1662 	.enum_mbus_code = ipipe_enum_mbus_code,
1663 	.enum_frame_size = ipipe_enum_frame_size,
1664 	.get_fmt = ipipe_get_format,
1665 	.set_fmt = ipipe_set_format,
1666 };
1667 
1668 /* v4l2 subdev operation */
1669 static const struct v4l2_subdev_ops ipipe_v4l2_ops = {
1670 	.core = &ipipe_v4l2_core_ops,
1671 	.video = &ipipe_v4l2_video_ops,
1672 	.pad = &ipipe_v4l2_pad_ops,
1673 };
1674 
1675 /*
1676  * Media entity operations
1677  */
1678 
1679 /*
1680  * ipipe_link_setup() - Setup ipipe connections
1681  * @entity: ipipe media entity
1682  * @local: Pad at the local end of the link
1683  * @remote: Pad at the remote end of the link
1684  * @flags: Link flags
1685  *
1686  * return -EINVAL or zero on success
1687  */
1688 static int
ipipe_link_setup(struct media_entity * entity,const struct media_pad * local,const struct media_pad * remote,u32 flags)1689 ipipe_link_setup(struct media_entity *entity, const struct media_pad *local,
1690 		     const struct media_pad *remote, u32 flags)
1691 {
1692 	struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1693 	struct vpfe_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
1694 	struct vpfe_device *vpfe_dev = to_vpfe_device(ipipe);
1695 	u16 ipipeif_sink = vpfe_dev->vpfe_ipipeif.input;
1696 
1697 	if (!is_media_entity_v4l2_subdev(remote->entity))
1698 		return -EINVAL;
1699 
1700 	switch (local->index) {
1701 	case IPIPE_PAD_SINK:
1702 		if (!(flags & MEDIA_LNK_FL_ENABLED)) {
1703 			ipipe->input = IPIPE_INPUT_NONE;
1704 			break;
1705 		}
1706 		if (ipipe->input != IPIPE_INPUT_NONE)
1707 			return -EBUSY;
1708 		if (ipipeif_sink == IPIPEIF_INPUT_MEMORY)
1709 			ipipe->input = IPIPE_INPUT_MEMORY;
1710 		else
1711 			ipipe->input = IPIPE_INPUT_CCDC;
1712 		break;
1713 
1714 	case IPIPE_PAD_SOURCE:
1715 		/* out to RESIZER */
1716 		if (flags & MEDIA_LNK_FL_ENABLED)
1717 			ipipe->output = IPIPE_OUTPUT_RESIZER;
1718 		else
1719 			ipipe->output = IPIPE_OUTPUT_NONE;
1720 		break;
1721 
1722 	default:
1723 		return -EINVAL;
1724 	}
1725 
1726 	return 0;
1727 }
1728 
1729 static const struct media_entity_operations ipipe_media_ops = {
1730 	.link_setup = ipipe_link_setup,
1731 };
1732 
1733 /*
1734  * vpfe_ipipe_unregister_entities() - ipipe unregister entity
1735  * @vpfe_ipipe: pointer to ipipe subdevice structure.
1736  */
vpfe_ipipe_unregister_entities(struct vpfe_ipipe_device * vpfe_ipipe)1737 void vpfe_ipipe_unregister_entities(struct vpfe_ipipe_device *vpfe_ipipe)
1738 {
1739 	/* unregister subdev */
1740 	v4l2_device_unregister_subdev(&vpfe_ipipe->subdev);
1741 	/* cleanup entity */
1742 	media_entity_cleanup(&vpfe_ipipe->subdev.entity);
1743 }
1744 
1745 /*
1746  * vpfe_ipipe_register_entities() - ipipe register entity
1747  * @ipipe: pointer to ipipe subdevice structure.
1748  * @vdev: pointer to v4l2 device structure.
1749  */
1750 int
vpfe_ipipe_register_entities(struct vpfe_ipipe_device * ipipe,struct v4l2_device * vdev)1751 vpfe_ipipe_register_entities(struct vpfe_ipipe_device *ipipe,
1752 				 struct v4l2_device *vdev)
1753 {
1754 	int ret;
1755 
1756 	/* Register the subdev */
1757 	ret = v4l2_device_register_subdev(vdev, &ipipe->subdev);
1758 	if (ret) {
1759 		pr_err("Failed to register ipipe as v4l2 subdevice\n");
1760 		return ret;
1761 	}
1762 
1763 	return ret;
1764 }
1765 
1766 #define IPIPE_CONTRAST_HIGH		0xff
1767 #define IPIPE_BRIGHT_HIGH		0xff
1768 
1769 /*
1770  * vpfe_ipipe_init() - ipipe module initialization.
1771  * @ipipe: pointer to ipipe subdevice structure.
1772  * @pdev: platform device pointer.
1773  */
1774 int
vpfe_ipipe_init(struct vpfe_ipipe_device * ipipe,struct platform_device * pdev)1775 vpfe_ipipe_init(struct vpfe_ipipe_device *ipipe, struct platform_device *pdev)
1776 {
1777 	struct media_pad *pads = &ipipe->pads[0];
1778 	struct v4l2_subdev *sd = &ipipe->subdev;
1779 	struct media_entity *me = &sd->entity;
1780 	struct resource *res, *memres;
1781 
1782 	res = platform_get_resource(pdev, IORESOURCE_MEM, 4);
1783 	if (!res)
1784 		return -ENOENT;
1785 
1786 	memres = request_mem_region(res->start, resource_size(res), res->name);
1787 	if (!memres)
1788 		return -EBUSY;
1789 	ipipe->base_addr = ioremap_nocache(memres->start,
1790 					   resource_size(memres));
1791 	if (!ipipe->base_addr)
1792 		goto error_release;
1793 
1794 	res = platform_get_resource(pdev, IORESOURCE_MEM, 6);
1795 	if (!res)
1796 		goto error_unmap;
1797 	ipipe->isp5_base_addr = ioremap_nocache(res->start,
1798 						resource_size(res));
1799 	if (!ipipe->isp5_base_addr)
1800 		goto error_unmap;
1801 
1802 	v4l2_subdev_init(sd, &ipipe_v4l2_ops);
1803 	sd->internal_ops = &ipipe_v4l2_internal_ops;
1804 	strlcpy(sd->name, "DAVINCI IPIPE", sizeof(sd->name));
1805 	sd->grp_id = 1 << 16;	/* group ID for davinci subdevs */
1806 	v4l2_set_subdevdata(sd, ipipe);
1807 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1808 
1809 	pads[IPIPE_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1810 	pads[IPIPE_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1811 
1812 	ipipe->input = IPIPE_INPUT_NONE;
1813 	ipipe->output = IPIPE_OUTPUT_NONE;
1814 
1815 	me->ops = &ipipe_media_ops;
1816 	v4l2_ctrl_handler_init(&ipipe->ctrls, 2);
1817 	v4l2_ctrl_new_std(&ipipe->ctrls, &ipipe_ctrl_ops,
1818 			  V4L2_CID_BRIGHTNESS, 0,
1819 			  IPIPE_BRIGHT_HIGH, 1, 16);
1820 	v4l2_ctrl_new_std(&ipipe->ctrls, &ipipe_ctrl_ops,
1821 			  V4L2_CID_CONTRAST, 0,
1822 			  IPIPE_CONTRAST_HIGH, 1, 16);
1823 
1824 
1825 	v4l2_ctrl_handler_setup(&ipipe->ctrls);
1826 	sd->ctrl_handler = &ipipe->ctrls;
1827 
1828 	return media_entity_pads_init(me, IPIPE_PADS_NUM, pads);
1829 
1830 error_unmap:
1831 	iounmap(ipipe->base_addr);
1832 error_release:
1833 	release_mem_region(memres->start, resource_size(memres));
1834 	return -ENOMEM;
1835 }
1836 
1837 /*
1838  * vpfe_ipipe_cleanup() - ipipe subdevice cleanup.
1839  * @ipipe: pointer to ipipe subdevice
1840  * @dev: pointer to platform device
1841  */
vpfe_ipipe_cleanup(struct vpfe_ipipe_device * ipipe,struct platform_device * pdev)1842 void vpfe_ipipe_cleanup(struct vpfe_ipipe_device *ipipe,
1843 			struct platform_device *pdev)
1844 {
1845 	struct resource *res;
1846 
1847 	v4l2_ctrl_handler_free(&ipipe->ctrls);
1848 
1849 	iounmap(ipipe->base_addr);
1850 	iounmap(ipipe->isp5_base_addr);
1851 	res = platform_get_resource(pdev, IORESOURCE_MEM, 4);
1852 	if (res)
1853 		release_mem_region(res->start, resource_size(res));
1854 }
1855