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