xref: /linux/drivers/media/platform/qcom/camss/camss-format.c (revision 170aafe35cb98e0f3fbacb446ea86389fbce22ea)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * camss-format.c
4  *
5  * Qualcomm MSM Camera Subsystem - Format helpers
6  *
7  * Copyright (c) 2023, The Linux Foundation. All rights reserved.
8  * Copyright (c) 2023 Qualcomm Technologies, Inc.
9  */
10 #include <linux/bug.h>
11 #include <linux/errno.h>
12 
13 #include "camss-format.h"
14 
15 /*
16  * camss_format_get_bpp - Map media bus format to bits per pixel
17  * @formats: supported media bus formats array
18  * @nformats: size of @formats array
19  * @code: media bus format code
20  *
21  * Return number of bits per pixel
22  */
23 u8 camss_format_get_bpp(const struct camss_format_info *formats, unsigned int nformats, u32 code)
24 {
25 	unsigned int i;
26 
27 	for (i = 0; i < nformats; i++)
28 		if (code == formats[i].code)
29 			return formats[i].mbus_bpp;
30 
31 	WARN(1, "Unknown format\n");
32 
33 	return formats[0].mbus_bpp;
34 }
35 
36 /*
37  * camss_format_find_code - Find a format code in an array
38  * @code: a pointer to media bus format codes array
39  * @n_code: size of @code array
40  * @index: index of code in the array
41  * @req_code: required code
42  *
43  * Return media bus format code
44  */
45 u32 camss_format_find_code(u32 *code, unsigned int n_code, unsigned int index, u32 req_code)
46 {
47 	unsigned int i;
48 
49 	if (!req_code && index >= n_code)
50 		return 0;
51 
52 	for (i = 0; i < n_code; i++) {
53 		if (req_code) {
54 			if (req_code == code[i])
55 				return req_code;
56 		} else {
57 			if (i == index)
58 				return code[i];
59 		}
60 	}
61 
62 	return code[0];
63 }
64 
65 /*
66  * camss_format_find_format - Find a format in an array
67  * @code: media bus format code
68  * @pixelformat: V4L2 pixel format FCC identifier
69  * @formats: a pointer to formats array
70  * @nformats: size of @formats array
71  *
72  * Return index of a format or a negative error code otherwise
73  */
74 int camss_format_find_format(u32 code, u32 pixelformat, const struct camss_format_info *formats,
75 			     unsigned int nformats)
76 {
77 	unsigned int i;
78 
79 	for (i = 0; i < nformats; i++) {
80 		if (formats[i].code == code &&
81 		    formats[i].pixelformat == pixelformat)
82 			return i;
83 	}
84 
85 	for (i = 0; i < nformats; i++) {
86 		if (formats[i].code == code)
87 			return i;
88 	}
89 
90 	return -EINVAL;
91 }
92