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 "atomisp_internal.h"
8 
9 #include "ia_css_vf.host.h"
10 #include <assert_support.h>
11 #include <ia_css_err.h>
12 #include <ia_css_frame.h>
13 #include <ia_css_frame_public.h>
14 #include <ia_css_pipeline.h>
15 #define IA_CSS_INCLUDE_CONFIGURATIONS
16 #include "ia_css_isp_configs.h"
17 
18 #include "isp.h"
19 
ia_css_vf_config(struct sh_css_isp_vf_isp_config * to,const struct ia_css_vf_configuration * from,unsigned int size)20 int ia_css_vf_config(struct sh_css_isp_vf_isp_config      *to,
21 		    const struct ia_css_vf_configuration *from,
22 		    unsigned int size)
23 {
24 	unsigned int elems_a = ISP_VEC_NELEMS;
25 	int ret;
26 
27 	to->vf_downscale_bits = from->vf_downscale_bits;
28 	to->enable = from->info != NULL;
29 
30 	if (from->info) {
31 		ia_css_frame_info_to_frame_sp_info(&to->info, from->info);
32 		ret = ia_css_dma_configure_from_info(&to->dma.port_b, from->info);
33 		if (ret)
34 			return ret;
35 		to->dma.width_a_over_b = elems_a / to->dma.port_b.elems;
36 
37 		/* Assume divisiblity here, may need to generalize to fixed point. */
38 		if (elems_a % to->dma.port_b.elems != 0)
39 			return -EINVAL;
40 	}
41 	return 0;
42 }
43 
44 /* compute the log2 of the downscale factor needed to get closest
45  * to the requested viewfinder resolution on the upper side. The output cannot
46  * be smaller than the requested viewfinder resolution.
47  */
48 int
sh_css_vf_downscale_log2(const struct ia_css_frame_info * out_info,const struct ia_css_frame_info * vf_info,unsigned int * downscale_log2)49 sh_css_vf_downscale_log2(
50     const struct ia_css_frame_info *out_info,
51     const struct ia_css_frame_info *vf_info,
52     unsigned int *downscale_log2) {
53 	unsigned int ds_log2 = 0;
54 	unsigned int out_width;
55 
56 	if ((!out_info) || (!vf_info))
57 		return -EINVAL;
58 
59 	out_width = out_info->res.width;
60 
61 	if (out_width == 0)
62 		return -EINVAL;
63 
64 	/* downscale until width smaller than the viewfinder width. We don't
65 	* test for the height since the vmem buffers only put restrictions on
66 	* the width of a line, not on the number of lines in a frame.
67 	*/
68 	while (out_width >= vf_info->res.width)
69 	{
70 		ds_log2++;
71 		out_width /= 2;
72 	}
73 	/* now width is smaller, so we go up one step */
74 	if ((ds_log2 > 0) && (out_width < ia_css_binary_max_vf_width()))
75 		ds_log2--;
76 	/* TODO: use actual max input resolution of vf_pp binary */
77 	if ((out_info->res.width >> ds_log2) >= 2 * ia_css_binary_max_vf_width())
78 		return -EINVAL;
79 	*downscale_log2 = ds_log2;
80 	return 0;
81 }
82 
83 static int
configure_kernel(const struct ia_css_binary_info * info,const struct ia_css_frame_info * out_info,const struct ia_css_frame_info * vf_info,unsigned int * downscale_log2,struct ia_css_vf_configuration * config)84 configure_kernel(
85     const struct ia_css_binary_info *info,
86     const struct ia_css_frame_info *out_info,
87     const struct ia_css_frame_info *vf_info,
88     unsigned int *downscale_log2,
89     struct ia_css_vf_configuration *config) {
90 	int err;
91 	unsigned int vf_log_ds = 0;
92 
93 	/* First compute value */
94 	if (vf_info)
95 	{
96 		err = sh_css_vf_downscale_log2(out_info, vf_info, &vf_log_ds);
97 		if (err)
98 			return err;
99 	}
100 	vf_log_ds = min(vf_log_ds, info->vf_dec.max_log_downscale);
101 	*downscale_log2 = vf_log_ds;
102 
103 	/* Then store it in isp config section */
104 	config->vf_downscale_bits = vf_log_ds;
105 	return 0;
106 }
107 
108 static void
configure_dma(struct ia_css_vf_configuration * config,const struct ia_css_frame_info * vf_info)109 configure_dma(
110     struct ia_css_vf_configuration *config,
111     const struct ia_css_frame_info *vf_info)
112 {
113 	config->info = vf_info;
114 }
115 
ia_css_vf_configure(const struct ia_css_binary * binary,const struct ia_css_frame_info * out_info,struct ia_css_frame_info * vf_info,unsigned int * downscale_log2)116 int ia_css_vf_configure(const struct ia_css_binary *binary,
117 		        const struct ia_css_frame_info *out_info,
118 			struct ia_css_frame_info *vf_info,
119 			unsigned int *downscale_log2)
120 {
121 	int err;
122 	struct ia_css_vf_configuration config;
123 	const struct ia_css_binary_info *info = &binary->info->sp;
124 
125 	err = configure_kernel(info, out_info, vf_info, downscale_log2, &config);
126 	if (err)
127 		dev_warn(atomisp_dev, "Couldn't setup downscale\n");
128 
129 	configure_dma(&config, vf_info);
130 
131 	if (vf_info)
132 		vf_info->raw_bit_depth = info->dma.vfdec_bits_per_pixel;
133 
134 	return ia_css_configure_vf(binary, &config);
135 }
136