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
ia_css_util_check_res(unsigned int width,unsigned int height)122 int ia_css_util_check_res(unsigned int width, unsigned int height)
123 {
124 /* height can be odd number for jpeg/embedded data from ISYS2401 */
125 if (((width == 0) ||
126 (height == 0) ||
127 IS_ODD(width))) {
128 return -EINVAL;
129 }
130 return 0;
131 }
132
133 /* ISP2401 */
ia_css_util_res_leq(struct ia_css_resolution a,struct ia_css_resolution b)134 bool ia_css_util_res_leq(struct ia_css_resolution a, struct ia_css_resolution b)
135 {
136 return a.width <= b.width && a.height <= b.height;
137 }
138
139 /* ISP2401 */
ia_css_util_resolution_is_zero(const struct ia_css_resolution resolution)140 bool ia_css_util_resolution_is_zero(const struct ia_css_resolution resolution)
141 {
142 return (resolution.width == 0) || (resolution.height == 0);
143 }
144
145 /* ISP2401 */
ia_css_util_resolution_is_even(const struct ia_css_resolution resolution)146 bool ia_css_util_resolution_is_even(const struct ia_css_resolution resolution)
147 {
148 return IS_EVEN(resolution.height) && IS_EVEN(resolution.width);
149 }
150
ia_css_util_is_input_format_raw(enum atomisp_input_format format)151 bool ia_css_util_is_input_format_raw(enum atomisp_input_format format)
152 {
153 return ((format == ATOMISP_INPUT_FORMAT_RAW_6) ||
154 (format == ATOMISP_INPUT_FORMAT_RAW_7) ||
155 (format == ATOMISP_INPUT_FORMAT_RAW_8) ||
156 (format == ATOMISP_INPUT_FORMAT_RAW_10) ||
157 (format == ATOMISP_INPUT_FORMAT_RAW_12));
158 /* raw_14 and raw_16 are not supported as input formats to the ISP.
159 * They can only be copied to a frame in memory using the
160 * copy binary.
161 */
162 }
163
ia_css_util_is_input_format_yuv(enum atomisp_input_format format)164 bool ia_css_util_is_input_format_yuv(enum atomisp_input_format format)
165 {
166 return format == ATOMISP_INPUT_FORMAT_YUV420_8_LEGACY ||
167 format == ATOMISP_INPUT_FORMAT_YUV420_8 ||
168 format == ATOMISP_INPUT_FORMAT_YUV420_10 ||
169 format == ATOMISP_INPUT_FORMAT_YUV420_16 ||
170 format == ATOMISP_INPUT_FORMAT_YUV422_8 ||
171 format == ATOMISP_INPUT_FORMAT_YUV422_10 ||
172 format == ATOMISP_INPUT_FORMAT_YUV422_16;
173 }
174
ia_css_util_check_input(const struct ia_css_stream_config * const stream_config,bool must_be_raw,bool must_be_yuv)175 int ia_css_util_check_input(
176 const struct ia_css_stream_config *const stream_config,
177 bool must_be_raw,
178 bool must_be_yuv)
179 {
180 assert(stream_config);
181
182 if (!stream_config)
183 return -EINVAL;
184
185 if (stream_config->input_config.effective_res.width == 0 ||
186 stream_config->input_config.effective_res.height == 0)
187 return -EINVAL;
188 if (must_be_raw &&
189 !ia_css_util_is_input_format_raw(stream_config->input_config.format))
190 return -EINVAL;
191
192 if (must_be_yuv &&
193 !ia_css_util_is_input_format_yuv(stream_config->input_config.format))
194 return -EINVAL;
195
196 return 0;
197 }
198