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