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_util.h"
17 #include <ia_css_frame.h>
18 #include <assert_support.h>
19 #include <math_support.h>
20 
21 /* for ia_css_binary_max_vf_width() */
22 #include "ia_css_binary.h"
23 
24 /* MW: Table look-up ??? */
ia_css_util_input_format_bpp(enum atomisp_input_format format,bool two_ppc)25 unsigned int ia_css_util_input_format_bpp(
26     enum atomisp_input_format format,
27     bool two_ppc)
28 {
29 	unsigned int rval = 0;
30 
31 	switch (format) {
32 	case ATOMISP_INPUT_FORMAT_YUV420_8_LEGACY:
33 	case ATOMISP_INPUT_FORMAT_YUV420_8:
34 	case ATOMISP_INPUT_FORMAT_YUV422_8:
35 	case ATOMISP_INPUT_FORMAT_RGB_888:
36 	case ATOMISP_INPUT_FORMAT_RAW_8:
37 	case ATOMISP_INPUT_FORMAT_BINARY_8:
38 	case ATOMISP_INPUT_FORMAT_EMBEDDED:
39 		rval = 8;
40 		break;
41 	case ATOMISP_INPUT_FORMAT_YUV420_10:
42 	case ATOMISP_INPUT_FORMAT_YUV422_10:
43 	case ATOMISP_INPUT_FORMAT_RAW_10:
44 		rval = 10;
45 		break;
46 	case ATOMISP_INPUT_FORMAT_YUV420_16:
47 	case ATOMISP_INPUT_FORMAT_YUV422_16:
48 		rval = 16;
49 		break;
50 	case ATOMISP_INPUT_FORMAT_RGB_444:
51 		rval = 4;
52 		break;
53 	case ATOMISP_INPUT_FORMAT_RGB_555:
54 		rval = 5;
55 		break;
56 	case ATOMISP_INPUT_FORMAT_RGB_565:
57 		rval = 65;
58 		break;
59 	case ATOMISP_INPUT_FORMAT_RGB_666:
60 	case ATOMISP_INPUT_FORMAT_RAW_6:
61 		rval = 6;
62 		break;
63 	case ATOMISP_INPUT_FORMAT_RAW_7:
64 		rval = 7;
65 		break;
66 	case ATOMISP_INPUT_FORMAT_RAW_12:
67 		rval = 12;
68 		break;
69 	case ATOMISP_INPUT_FORMAT_RAW_14:
70 		if (two_ppc)
71 			rval = 14;
72 		else
73 			rval = 12;
74 		break;
75 	case ATOMISP_INPUT_FORMAT_RAW_16:
76 		if (two_ppc)
77 			rval = 16;
78 		else
79 			rval = 12;
80 		break;
81 	default:
82 		rval = 0;
83 		break;
84 	}
85 	return rval;
86 }
87 
ia_css_util_check_vf_info(const struct ia_css_frame_info * const info)88 int ia_css_util_check_vf_info(
89     const struct ia_css_frame_info *const info)
90 {
91 	int err;
92 	unsigned int max_vf_width;
93 
94 	assert(info);
95 	err = ia_css_frame_check_info(info);
96 	if (err)
97 		return err;
98 	max_vf_width = ia_css_binary_max_vf_width();
99 	if (max_vf_width != 0 && info->res.width > max_vf_width * 2)
100 		return -EINVAL;
101 	return 0;
102 }
103 
ia_css_util_check_vf_out_info(const struct ia_css_frame_info * const out_info,const struct ia_css_frame_info * const vf_info)104 int ia_css_util_check_vf_out_info(
105     const struct ia_css_frame_info *const out_info,
106     const struct ia_css_frame_info *const vf_info)
107 {
108 	int err;
109 
110 	assert(out_info);
111 	assert(vf_info);
112 
113 	err = ia_css_frame_check_info(out_info);
114 	if (err)
115 		return err;
116 	err = ia_css_util_check_vf_info(vf_info);
117 	if (err)
118 		return err;
119 	return 0;
120 }
121 
122 /* ISP2401 */
ia_css_util_res_leq(struct ia_css_resolution a,struct ia_css_resolution b)123 bool ia_css_util_res_leq(struct ia_css_resolution a, struct ia_css_resolution b)
124 {
125 	return a.width <= b.width && a.height <= b.height;
126 }
127 
128 /* ISP2401 */
ia_css_util_resolution_is_zero(const struct ia_css_resolution resolution)129 bool ia_css_util_resolution_is_zero(const struct ia_css_resolution resolution)
130 {
131 	return (resolution.width == 0) || (resolution.height == 0);
132 }
133 
ia_css_util_check_res(unsigned int width,unsigned int height)134 int ia_css_util_check_res(unsigned int width, unsigned int height)
135 {
136 	const struct ia_css_resolution resolution = { .width = width, .height = height };
137 
138 	if (ia_css_util_resolution_is_zero(resolution))
139 		return -EINVAL;
140 
141 	/* height can be odd number for jpeg/embedded data from ISYS2401 */
142 	if (width & 1)
143 		return -EINVAL;
144 
145 	return 0;
146 }
147 
ia_css_util_is_input_format_raw(enum atomisp_input_format format)148 bool ia_css_util_is_input_format_raw(enum atomisp_input_format format)
149 {
150 	return ((format == ATOMISP_INPUT_FORMAT_RAW_6) ||
151 		(format == ATOMISP_INPUT_FORMAT_RAW_7) ||
152 		(format == ATOMISP_INPUT_FORMAT_RAW_8) ||
153 		(format == ATOMISP_INPUT_FORMAT_RAW_10) ||
154 		(format == ATOMISP_INPUT_FORMAT_RAW_12));
155 	/* raw_14 and raw_16 are not supported as input formats to the ISP.
156 	 * They can only be copied to a frame in memory using the
157 	 * copy binary.
158 	 */
159 }
160 
ia_css_util_is_input_format_yuv(enum atomisp_input_format format)161 bool ia_css_util_is_input_format_yuv(enum atomisp_input_format format)
162 {
163 	return format == ATOMISP_INPUT_FORMAT_YUV420_8_LEGACY ||
164 	       format == ATOMISP_INPUT_FORMAT_YUV420_8  ||
165 	       format == ATOMISP_INPUT_FORMAT_YUV420_10 ||
166 	       format == ATOMISP_INPUT_FORMAT_YUV420_16 ||
167 	       format == ATOMISP_INPUT_FORMAT_YUV422_8  ||
168 	       format == ATOMISP_INPUT_FORMAT_YUV422_10 ||
169 	       format == ATOMISP_INPUT_FORMAT_YUV422_16;
170 }
171 
ia_css_util_check_input(const struct ia_css_stream_config * const stream_config,bool must_be_raw,bool must_be_yuv)172 int ia_css_util_check_input(
173     const struct ia_css_stream_config *const stream_config,
174     bool must_be_raw,
175     bool must_be_yuv)
176 {
177 	assert(stream_config);
178 
179 	if (!stream_config)
180 		return -EINVAL;
181 
182 	if (stream_config->input_config.effective_res.width == 0 ||
183 	    stream_config->input_config.effective_res.height == 0)
184 		return -EINVAL;
185 	if (must_be_raw &&
186 	    !ia_css_util_is_input_format_raw(stream_config->input_config.format))
187 		return -EINVAL;
188 
189 	if (must_be_yuv &&
190 	    !ia_css_util_is_input_format_yuv(stream_config->input_config.format))
191 		return -EINVAL;
192 
193 	return 0;
194 }
195