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 "ia_css_types.h"
8 #include "sh_css_defs.h"
9 #include "ia_css_debug.h"
10 #include "assert_support.h"
11 
12 #include "ctc/ctc_1.0/ia_css_ctc.host.h"
13 #include "ia_css_ctc1_5.host.h"
14 
ctc_gradient(int * dydx,int * shift,int y1,int y0,int x1,int x0)15 static void ctc_gradient(
16     int *dydx, int *shift,
17     int y1, int y0, int x1, int x0)
18 {
19 	int frc_bits = max(IA_CSS_CTC_COEF_SHIFT, 16);
20 	int dy = y1 - y0;
21 	int dx = x1 - x0;
22 	int dydx_int;
23 	int dydx_frc;
24 	int sft;
25 	/* max_dydx = the maxinum gradient = the maximum y (gain) */
26 	int max_dydx = (1 << IA_CSS_CTC_COEF_SHIFT) - 1;
27 
28 	if (dx == 0) {
29 		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
30 				    "ctc_gradient() error, illegal division operation\n");
31 		return;
32 	} else {
33 		dydx_int = dy / dx;
34 		dydx_frc = ((dy - dydx_int * dx) << frc_bits) / dx;
35 	}
36 
37 	assert(y0 >= 0 && y0 <= max_dydx);
38 	assert(y1 >= 0 && y1 <= max_dydx);
39 	assert(x0 < x1);
40 	assert(dydx);
41 	assert(shift);
42 
43 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE, "ctc_gradient() enter:\n");
44 
45 	/* search "sft" which meets this condition:
46 		   (1 << (IA_CSS_CTC_COEF_SHIFT - 1))
47 		<= (((float)dy / (float)dx) * (1 << sft))
48 		<= ((1 << IA_CSS_CTC_COEF_SHIFT) - 1) */
49 	for (sft = 0; sft <= IA_CSS_CTC_COEF_SHIFT; sft++) {
50 		int tmp_dydx = (dydx_int << sft)
51 			       + (dydx_frc >> (frc_bits - sft));
52 		if (tmp_dydx <= max_dydx) {
53 			*dydx = tmp_dydx;
54 			*shift = sft;
55 		}
56 		if (tmp_dydx >= max_dydx)
57 			break;
58 	}
59 
60 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE, "ctc_gradient() leave:\n");
61 }
62 
63 void
ia_css_ctc_encode(struct sh_css_isp_ctc_params * to,const struct ia_css_ctc_config * from,unsigned int size)64 ia_css_ctc_encode(
65     struct sh_css_isp_ctc_params *to,
66     const struct ia_css_ctc_config *from,
67     unsigned int size)
68 {
69 	(void)size;
70 	to->y0 = from->y0;
71 	to->y1 = from->y1;
72 	to->y2 = from->y2;
73 	to->y3 = from->y3;
74 	to->y4 = from->y4;
75 	to->y5 = from->y5;
76 
77 	to->ce_gain_exp = from->ce_gain_exp;
78 
79 	to->x1 = from->x1;
80 	to->x2 = from->x2;
81 	to->x3 = from->x3;
82 	to->x4 = from->x4;
83 
84 	ctc_gradient(&to->dydx0,
85 		     &to->dydx0_shift,
86 		     from->y1, from->y0,
87 		     from->x1, 0);
88 
89 	ctc_gradient(&to->dydx1,
90 		     &to->dydx1_shift,
91 		     from->y2, from->y1,
92 		     from->x2, from->x1);
93 
94 	ctc_gradient(&to->dydx2,
95 		     &to->dydx2_shift,
96 		     from->y3, from->y2,
97 		     from->x3, from->x2);
98 
99 	ctc_gradient(&to->dydx3,
100 		     &to->dydx3_shift,
101 		     from->y4, from->y3,
102 		     from->x4, from->x3);
103 
104 	ctc_gradient(&to->dydx4,
105 		     &to->dydx4_shift,
106 		     from->y5, from->y4,
107 		     SH_CSS_BAYER_MAXVAL, from->x4);
108 }
109 
110 void
111 ia_css_ctc_dump(
112     const struct sh_css_isp_ctc_params *ctc,
113     unsigned int level);
114