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 "ia_css_frame.h"
18 #include "sh_css_defs.h"
19 #include "ia_css_debug.h"
20 #include "sh_css_frac.h"
21 #include "assert_support.h"
22 #define IA_CSS_INCLUDE_CONFIGURATIONS
23 #include "ia_css_isp_configs.h"
24 #include "isp.h"
25 
26 #include "ia_css_tnr.host.h"
27 const struct ia_css_tnr_config default_tnr_config = {
28 	32768,
29 	32,
30 	32,
31 };
32 
33 void
ia_css_tnr_encode(struct sh_css_isp_tnr_params * to,const struct ia_css_tnr_config * from,unsigned int size)34 ia_css_tnr_encode(
35     struct sh_css_isp_tnr_params *to,
36     const struct ia_css_tnr_config *from,
37     unsigned int size)
38 {
39 	(void)size;
40 	to->coef =
41 	    uDIGIT_FITTING(from->gain, 16, SH_CSS_TNR_COEF_SHIFT);
42 	to->threshold_Y =
43 	    uDIGIT_FITTING(from->threshold_y, 16, SH_CSS_ISP_YUV_BITS);
44 	to->threshold_C =
45 	    uDIGIT_FITTING(from->threshold_uv, 16, SH_CSS_ISP_YUV_BITS);
46 }
47 
48 void
ia_css_tnr_dump(const struct sh_css_isp_tnr_params * tnr,unsigned int level)49 ia_css_tnr_dump(
50     const struct sh_css_isp_tnr_params *tnr,
51     unsigned int level)
52 {
53 	if (!tnr) return;
54 	ia_css_debug_dtrace(level, "Temporal Noise Reduction:\n");
55 	ia_css_debug_dtrace(level, "\t%-32s = %d\n",
56 			    "tnr_coef", tnr->coef);
57 	ia_css_debug_dtrace(level, "\t%-32s = %d\n",
58 			    "tnr_threshold_Y", tnr->threshold_Y);
59 	ia_css_debug_dtrace(level, "\t%-32s = %d\n",
60 			    "tnr_threshold_C", tnr->threshold_C);
61 }
62 
63 void
ia_css_tnr_debug_dtrace(const struct ia_css_tnr_config * config,unsigned int level)64 ia_css_tnr_debug_dtrace(
65     const struct ia_css_tnr_config *config,
66     unsigned int level)
67 {
68 	ia_css_debug_dtrace(level,
69 			    "config.gain=%d, config.threshold_y=%d, config.threshold_uv=%d\n",
70 			    config->gain,
71 			    config->threshold_y, config->threshold_uv);
72 }
73 
ia_css_tnr_config(struct sh_css_isp_tnr_isp_config * to,const struct ia_css_tnr_configuration * from,unsigned int size)74 int ia_css_tnr_config(struct sh_css_isp_tnr_isp_config *to,
75 		      const struct ia_css_tnr_configuration *from,
76 		      unsigned int size)
77 {
78 	unsigned int elems_a = ISP_VEC_NELEMS;
79 	unsigned int i;
80 	int ret;
81 
82 	ret = ia_css_dma_configure_from_info(&to->port_b, &from->tnr_frames[0]->frame_info);
83 	if (ret)
84 		return ret;
85 	to->width_a_over_b = elems_a / to->port_b.elems;
86 	to->frame_height = from->tnr_frames[0]->frame_info.res.height;
87 	for (i = 0; i < NUM_VIDEO_TNR_FRAMES; i++) {
88 		to->tnr_frame_addr[i] = from->tnr_frames[i]->data +
89 					from->tnr_frames[i]->planes.yuyv.offset;
90 	}
91 
92 	/* Assume divisiblity here, may need to generalize to fixed point. */
93 	if (elems_a % to->port_b.elems != 0)
94 		return -EINVAL;
95 
96 	return 0;
97 }
98 
ia_css_tnr_configure(const struct ia_css_binary * binary,const struct ia_css_frame * const * frames)99 int ia_css_tnr_configure(const struct ia_css_binary     *binary,
100 			 const struct ia_css_frame * const *frames)
101 {
102 	struct ia_css_tnr_configuration config;
103 	unsigned int i;
104 
105 	for (i = 0; i < NUM_VIDEO_TNR_FRAMES; i++)
106 		config.tnr_frames[i] = frames[i];
107 
108 	return ia_css_configure_tnr(binary, &config);
109 }
110 
111 void
ia_css_init_tnr_state(struct sh_css_isp_tnr_dmem_state * state,size_t size)112 ia_css_init_tnr_state(
113     struct sh_css_isp_tnr_dmem_state *state,
114     size_t size)
115 {
116 	(void)size;
117 
118 	assert(NUM_VIDEO_TNR_FRAMES >= 2);
119 	assert(sizeof(*state) == size);
120 	state->tnr_in_buf_idx = 0;
121 	state->tnr_out_buf_idx = 1;
122 }
123