1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Support for Intel Camera Imaging ISP subsystem.
4 * Copyright (c) 2015, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13 * more details.
14 */
15
16#include "ia_css_types.h"
17#include "sh_css_defs.h"
18#include "ia_css_debug.h"
19#include "assert_support.h"
20
21#include "ctc/ctc_1.0/ia_css_ctc.host.h"
22#include "ia_css_ctc1_5.host.h"
23
24static void ctc_gradient(
25    int *dydx, int *shift,
26    int y1, int y0, int x1, int x0)
27{
28	int frc_bits = max(IA_CSS_CTC_COEF_SHIFT, 16);
29	int dy = y1 - y0;
30	int dx = x1 - x0;
31	int dydx_int;
32	int dydx_frc;
33	int sft;
34	/* max_dydx = the maxinum gradient = the maximum y (gain) */
35	int max_dydx = (1 << IA_CSS_CTC_COEF_SHIFT) - 1;
36
37	if (dx == 0) {
38		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
39				    "ctc_gradient() error, illegal division operation\n");
40		return;
41	} else {
42		dydx_int = dy / dx;
43		dydx_frc = ((dy - dydx_int * dx) << frc_bits) / dx;
44	}
45
46	assert(y0 >= 0 && y0 <= max_dydx);
47	assert(y1 >= 0 && y1 <= max_dydx);
48	assert(x0 < x1);
49	assert(dydx);
50	assert(shift);
51
52	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE, "ctc_gradient() enter:\n");
53
54	/* search "sft" which meets this condition:
55		   (1 << (IA_CSS_CTC_COEF_SHIFT - 1))
56		<= (((float)dy / (float)dx) * (1 << sft))
57		<= ((1 << IA_CSS_CTC_COEF_SHIFT) - 1) */
58	for (sft = 0; sft <= IA_CSS_CTC_COEF_SHIFT; sft++) {
59		int tmp_dydx = (dydx_int << sft)
60			       + (dydx_frc >> (frc_bits - sft));
61		if (tmp_dydx <= max_dydx) {
62			*dydx = tmp_dydx;
63			*shift = sft;
64		}
65		if (tmp_dydx >= max_dydx)
66			break;
67	}
68
69	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE, "ctc_gradient() leave:\n");
70}
71
72void
73ia_css_ctc_encode(
74    struct sh_css_isp_ctc_params *to,
75    const struct ia_css_ctc_config *from,
76    unsigned int size)
77{
78	(void)size;
79	to->y0 = from->y0;
80	to->y1 = from->y1;
81	to->y2 = from->y2;
82	to->y3 = from->y3;
83	to->y4 = from->y4;
84	to->y5 = from->y5;
85
86	to->ce_gain_exp = from->ce_gain_exp;
87
88	to->x1 = from->x1;
89	to->x2 = from->x2;
90	to->x3 = from->x3;
91	to->x4 = from->x4;
92
93	ctc_gradient(&to->dydx0,
94		     &to->dydx0_shift,
95		     from->y1, from->y0,
96		     from->x1, 0);
97
98	ctc_gradient(&to->dydx1,
99		     &to->dydx1_shift,
100		     from->y2, from->y1,
101		     from->x2, from->x1);
102
103	ctc_gradient(&to->dydx2,
104		     &to->dydx2_shift,
105		     from->y3, from->y2,
106		     from->x3, from->x2);
107
108	ctc_gradient(&to->dydx3,
109		     &to->dydx3_shift,
110		     from->y4, from->y3,
111		     from->x4, from->x3);
112
113	ctc_gradient(&to->dydx4,
114		     &to->dydx4_shift,
115		     from->y5, from->y4,
116		     SH_CSS_BAYER_MAXVAL, from->x4);
117}
118
119void
120ia_css_ctc_dump(
121    const struct sh_css_isp_ctc_params *ctc,
122    unsigned int level);
123