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