1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * camss-video.c
4  *
5  * Qualcomm MSM Camera Subsystem - V4L2 device node
6  *
7  * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
8  * Copyright (C) 2015-2018 Linaro Ltd.
9  */
10 #include <linux/slab.h>
11 #include <media/media-entity.h>
12 #include <media/v4l2-dev.h>
13 #include <media/v4l2-device.h>
14 #include <media/v4l2-ioctl.h>
15 #include <media/v4l2-mc.h>
16 #include <media/videobuf2-dma-sg.h>
17 
18 #include "camss-video.h"
19 #include "camss.h"
20 
21 #define CAMSS_FRAME_MIN_WIDTH		1
22 #define CAMSS_FRAME_MAX_WIDTH		8191
23 #define CAMSS_FRAME_MIN_HEIGHT		1
24 #define CAMSS_FRAME_MAX_HEIGHT_RDI	8191
25 #define CAMSS_FRAME_MAX_HEIGHT_PIX	4096
26 
27 struct fract {
28 	u8 numerator;
29 	u8 denominator;
30 };
31 
32 /*
33  * struct camss_format_info - ISP media bus format information
34  * @code: V4L2 media bus format code
35  * @pixelformat: V4L2 pixel format FCC identifier
36  * @planes: Number of planes
37  * @hsub: Horizontal subsampling (for each plane)
38  * @vsub: Vertical subsampling (for each plane)
39  * @bpp: Bits per pixel when stored in memory (for each plane)
40  */
41 struct camss_format_info {
42 	u32 code;
43 	u32 pixelformat;
44 	u8 planes;
45 	struct fract hsub[3];
46 	struct fract vsub[3];
47 	unsigned int bpp[3];
48 };
49 
50 static const struct camss_format_info formats_rdi_8x16[] = {
51 	{ MEDIA_BUS_FMT_UYVY8_2X8, V4L2_PIX_FMT_UYVY, 1,
52 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
53 	{ MEDIA_BUS_FMT_VYUY8_2X8, V4L2_PIX_FMT_VYUY, 1,
54 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
55 	{ MEDIA_BUS_FMT_YUYV8_2X8, V4L2_PIX_FMT_YUYV, 1,
56 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
57 	{ MEDIA_BUS_FMT_YVYU8_2X8, V4L2_PIX_FMT_YVYU, 1,
58 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
59 	{ MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_PIX_FMT_SBGGR8, 1,
60 	  { { 1, 1 } }, { { 1, 1 } }, { 8 } },
61 	{ MEDIA_BUS_FMT_SGBRG8_1X8, V4L2_PIX_FMT_SGBRG8, 1,
62 	  { { 1, 1 } }, { { 1, 1 } }, { 8 } },
63 	{ MEDIA_BUS_FMT_SGRBG8_1X8, V4L2_PIX_FMT_SGRBG8, 1,
64 	  { { 1, 1 } }, { { 1, 1 } }, { 8 } },
65 	{ MEDIA_BUS_FMT_SRGGB8_1X8, V4L2_PIX_FMT_SRGGB8, 1,
66 	  { { 1, 1 } }, { { 1, 1 } }, { 8 } },
67 	{ MEDIA_BUS_FMT_SBGGR10_1X10, V4L2_PIX_FMT_SBGGR10P, 1,
68 	  { { 1, 1 } }, { { 1, 1 } }, { 10 } },
69 	{ MEDIA_BUS_FMT_SGBRG10_1X10, V4L2_PIX_FMT_SGBRG10P, 1,
70 	  { { 1, 1 } }, { { 1, 1 } }, { 10 } },
71 	{ MEDIA_BUS_FMT_SGRBG10_1X10, V4L2_PIX_FMT_SGRBG10P, 1,
72 	  { { 1, 1 } }, { { 1, 1 } }, { 10 } },
73 	{ MEDIA_BUS_FMT_SRGGB10_1X10, V4L2_PIX_FMT_SRGGB10P, 1,
74 	  { { 1, 1 } }, { { 1, 1 } }, { 10 } },
75 	{ MEDIA_BUS_FMT_SBGGR12_1X12, V4L2_PIX_FMT_SBGGR12P, 1,
76 	  { { 1, 1 } }, { { 1, 1 } }, { 12 } },
77 	{ MEDIA_BUS_FMT_SGBRG12_1X12, V4L2_PIX_FMT_SGBRG12P, 1,
78 	  { { 1, 1 } }, { { 1, 1 } }, { 12 } },
79 	{ MEDIA_BUS_FMT_SGRBG12_1X12, V4L2_PIX_FMT_SGRBG12P, 1,
80 	  { { 1, 1 } }, { { 1, 1 } }, { 12 } },
81 	{ MEDIA_BUS_FMT_SRGGB12_1X12, V4L2_PIX_FMT_SRGGB12P, 1,
82 	  { { 1, 1 } }, { { 1, 1 } }, { 12 } },
83 	{ MEDIA_BUS_FMT_Y10_1X10, V4L2_PIX_FMT_Y10P, 1,
84 	  { { 1, 1 } }, { { 1, 1 } }, { 10 } },
85 };
86 
87 static const struct camss_format_info formats_rdi_8x96[] = {
88 	{ MEDIA_BUS_FMT_UYVY8_2X8, V4L2_PIX_FMT_UYVY, 1,
89 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
90 	{ MEDIA_BUS_FMT_VYUY8_2X8, V4L2_PIX_FMT_VYUY, 1,
91 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
92 	{ MEDIA_BUS_FMT_YUYV8_2X8, V4L2_PIX_FMT_YUYV, 1,
93 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
94 	{ MEDIA_BUS_FMT_YVYU8_2X8, V4L2_PIX_FMT_YVYU, 1,
95 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
96 	{ MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_PIX_FMT_SBGGR8, 1,
97 	  { { 1, 1 } }, { { 1, 1 } }, { 8 } },
98 	{ MEDIA_BUS_FMT_SGBRG8_1X8, V4L2_PIX_FMT_SGBRG8, 1,
99 	  { { 1, 1 } }, { { 1, 1 } }, { 8 } },
100 	{ MEDIA_BUS_FMT_SGRBG8_1X8, V4L2_PIX_FMT_SGRBG8, 1,
101 	  { { 1, 1 } }, { { 1, 1 } }, { 8 } },
102 	{ MEDIA_BUS_FMT_SRGGB8_1X8, V4L2_PIX_FMT_SRGGB8, 1,
103 	  { { 1, 1 } }, { { 1, 1 } }, { 8 } },
104 	{ MEDIA_BUS_FMT_SBGGR10_1X10, V4L2_PIX_FMT_SBGGR10P, 1,
105 	  { { 1, 1 } }, { { 1, 1 } }, { 10 } },
106 	{ MEDIA_BUS_FMT_SGBRG10_1X10, V4L2_PIX_FMT_SGBRG10P, 1,
107 	  { { 1, 1 } }, { { 1, 1 } }, { 10 } },
108 	{ MEDIA_BUS_FMT_SGRBG10_1X10, V4L2_PIX_FMT_SGRBG10P, 1,
109 	  { { 1, 1 } }, { { 1, 1 } }, { 10 } },
110 	{ MEDIA_BUS_FMT_SRGGB10_1X10, V4L2_PIX_FMT_SRGGB10P, 1,
111 	  { { 1, 1 } }, { { 1, 1 } }, { 10 } },
112 	{ MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE, V4L2_PIX_FMT_SBGGR10, 1,
113 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
114 	{ MEDIA_BUS_FMT_SBGGR12_1X12, V4L2_PIX_FMT_SBGGR12P, 1,
115 	  { { 1, 1 } }, { { 1, 1 } }, { 12 } },
116 	{ MEDIA_BUS_FMT_SGBRG12_1X12, V4L2_PIX_FMT_SGBRG12P, 1,
117 	  { { 1, 1 } }, { { 1, 1 } }, { 12 } },
118 	{ MEDIA_BUS_FMT_SGRBG12_1X12, V4L2_PIX_FMT_SGRBG12P, 1,
119 	  { { 1, 1 } }, { { 1, 1 } }, { 12 } },
120 	{ MEDIA_BUS_FMT_SRGGB12_1X12, V4L2_PIX_FMT_SRGGB12P, 1,
121 	  { { 1, 1 } }, { { 1, 1 } }, { 12 } },
122 	{ MEDIA_BUS_FMT_SBGGR14_1X14, V4L2_PIX_FMT_SBGGR14P, 1,
123 	  { { 1, 1 } }, { { 1, 1 } }, { 14 } },
124 	{ MEDIA_BUS_FMT_SGBRG14_1X14, V4L2_PIX_FMT_SGBRG14P, 1,
125 	  { { 1, 1 } }, { { 1, 1 } }, { 14 } },
126 	{ MEDIA_BUS_FMT_SGRBG14_1X14, V4L2_PIX_FMT_SGRBG14P, 1,
127 	  { { 1, 1 } }, { { 1, 1 } }, { 14 } },
128 	{ MEDIA_BUS_FMT_SRGGB14_1X14, V4L2_PIX_FMT_SRGGB14P, 1,
129 	  { { 1, 1 } }, { { 1, 1 } }, { 14 } },
130 	{ MEDIA_BUS_FMT_Y10_1X10, V4L2_PIX_FMT_Y10P, 1,
131 	  { { 1, 1 } }, { { 1, 1 } }, { 10 } },
132 	{ MEDIA_BUS_FMT_Y10_2X8_PADHI_LE, V4L2_PIX_FMT_Y10, 1,
133 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
134 };
135 
136 static const struct camss_format_info formats_rdi_845[] = {
137 	{ MEDIA_BUS_FMT_UYVY8_2X8, V4L2_PIX_FMT_UYVY, 1,
138 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
139 	{ MEDIA_BUS_FMT_VYUY8_2X8, V4L2_PIX_FMT_VYUY, 1,
140 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
141 	{ MEDIA_BUS_FMT_YUYV8_2X8, V4L2_PIX_FMT_YUYV, 1,
142 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
143 	{ MEDIA_BUS_FMT_YVYU8_2X8, V4L2_PIX_FMT_YVYU, 1,
144 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
145 	{ MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_PIX_FMT_SBGGR8, 1,
146 	  { { 1, 1 } }, { { 1, 1 } }, { 8 } },
147 	{ MEDIA_BUS_FMT_SGBRG8_1X8, V4L2_PIX_FMT_SGBRG8, 1,
148 	  { { 1, 1 } }, { { 1, 1 } }, { 8 } },
149 	{ MEDIA_BUS_FMT_SGRBG8_1X8, V4L2_PIX_FMT_SGRBG8, 1,
150 	  { { 1, 1 } }, { { 1, 1 } }, { 8 } },
151 	{ MEDIA_BUS_FMT_SRGGB8_1X8, V4L2_PIX_FMT_SRGGB8, 1,
152 	  { { 1, 1 } }, { { 1, 1 } }, { 8 } },
153 	{ MEDIA_BUS_FMT_SBGGR10_1X10, V4L2_PIX_FMT_SBGGR10P, 1,
154 	  { { 1, 1 } }, { { 1, 1 } }, { 10 } },
155 	{ MEDIA_BUS_FMT_SGBRG10_1X10, V4L2_PIX_FMT_SGBRG10P, 1,
156 	  { { 1, 1 } }, { { 1, 1 } }, { 10 } },
157 	{ MEDIA_BUS_FMT_SGRBG10_1X10, V4L2_PIX_FMT_SGRBG10P, 1,
158 	  { { 1, 1 } }, { { 1, 1 } }, { 10 } },
159 	{ MEDIA_BUS_FMT_SRGGB10_1X10, V4L2_PIX_FMT_SRGGB10P, 1,
160 	  { { 1, 1 } }, { { 1, 1 } }, { 10 } },
161 	{ MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE, V4L2_PIX_FMT_SBGGR10, 1,
162 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
163 	{ MEDIA_BUS_FMT_SBGGR12_1X12, V4L2_PIX_FMT_SBGGR12P, 1,
164 	  { { 1, 1 } }, { { 1, 1 } }, { 12 } },
165 	{ MEDIA_BUS_FMT_SGBRG12_1X12, V4L2_PIX_FMT_SGBRG12P, 1,
166 	  { { 1, 1 } }, { { 1, 1 } }, { 12 } },
167 	{ MEDIA_BUS_FMT_SGRBG12_1X12, V4L2_PIX_FMT_SGRBG12P, 1,
168 	  { { 1, 1 } }, { { 1, 1 } }, { 12 } },
169 	{ MEDIA_BUS_FMT_SRGGB12_1X12, V4L2_PIX_FMT_SRGGB12P, 1,
170 	  { { 1, 1 } }, { { 1, 1 } }, { 12 } },
171 	{ MEDIA_BUS_FMT_SBGGR14_1X14, V4L2_PIX_FMT_SBGGR14P, 1,
172 	  { { 1, 1 } }, { { 1, 1 } }, { 14 } },
173 	{ MEDIA_BUS_FMT_SGBRG14_1X14, V4L2_PIX_FMT_SGBRG14P, 1,
174 	  { { 1, 1 } }, { { 1, 1 } }, { 14 } },
175 	{ MEDIA_BUS_FMT_SGRBG14_1X14, V4L2_PIX_FMT_SGRBG14P, 1,
176 	  { { 1, 1 } }, { { 1, 1 } }, { 14 } },
177 	{ MEDIA_BUS_FMT_SRGGB14_1X14, V4L2_PIX_FMT_SRGGB14P, 1,
178 	  { { 1, 1 } }, { { 1, 1 } }, { 14 } },
179 	{ MEDIA_BUS_FMT_Y8_1X8, V4L2_PIX_FMT_GREY, 1,
180 	  { { 1, 1 } }, { { 1, 1 } }, { 8 } },
181 	{ MEDIA_BUS_FMT_Y10_1X10, V4L2_PIX_FMT_Y10P, 1,
182 	  { { 1, 1 } }, { { 1, 1 } }, { 10 } },
183 	{ MEDIA_BUS_FMT_Y10_2X8_PADHI_LE, V4L2_PIX_FMT_Y10, 1,
184 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
185 };
186 
187 static const struct camss_format_info formats_pix_8x16[] = {
188 	{ MEDIA_BUS_FMT_YUYV8_1_5X8, V4L2_PIX_FMT_NV12, 1,
189 	  { { 1, 1 } }, { { 2, 3 } }, { 8 } },
190 	{ MEDIA_BUS_FMT_YVYU8_1_5X8, V4L2_PIX_FMT_NV12, 1,
191 	  { { 1, 1 } }, { { 2, 3 } }, { 8 } },
192 	{ MEDIA_BUS_FMT_UYVY8_1_5X8, V4L2_PIX_FMT_NV12, 1,
193 	  { { 1, 1 } }, { { 2, 3 } }, { 8 } },
194 	{ MEDIA_BUS_FMT_VYUY8_1_5X8, V4L2_PIX_FMT_NV12, 1,
195 	  { { 1, 1 } }, { { 2, 3 } }, { 8 } },
196 	{ MEDIA_BUS_FMT_YUYV8_1_5X8, V4L2_PIX_FMT_NV21, 1,
197 	  { { 1, 1 } }, { { 2, 3 } }, { 8 } },
198 	{ MEDIA_BUS_FMT_YVYU8_1_5X8, V4L2_PIX_FMT_NV21, 1,
199 	  { { 1, 1 } }, { { 2, 3 } }, { 8 } },
200 	{ MEDIA_BUS_FMT_UYVY8_1_5X8, V4L2_PIX_FMT_NV21, 1,
201 	  { { 1, 1 } }, { { 2, 3 } }, { 8 } },
202 	{ MEDIA_BUS_FMT_VYUY8_1_5X8, V4L2_PIX_FMT_NV21, 1,
203 	  { { 1, 1 } }, { { 2, 3 } }, { 8 } },
204 	{ MEDIA_BUS_FMT_YUYV8_2X8, V4L2_PIX_FMT_NV16, 1,
205 	  { { 1, 1 } }, { { 1, 2 } }, { 8 } },
206 	{ MEDIA_BUS_FMT_YVYU8_2X8, V4L2_PIX_FMT_NV16, 1,
207 	  { { 1, 1 } }, { { 1, 2 } }, { 8 } },
208 	{ MEDIA_BUS_FMT_UYVY8_2X8, V4L2_PIX_FMT_NV16, 1,
209 	  { { 1, 1 } }, { { 1, 2 } }, { 8 } },
210 	{ MEDIA_BUS_FMT_VYUY8_2X8, V4L2_PIX_FMT_NV16, 1,
211 	  { { 1, 1 } }, { { 1, 2 } }, { 8 } },
212 	{ MEDIA_BUS_FMT_YUYV8_2X8, V4L2_PIX_FMT_NV61, 1,
213 	  { { 1, 1 } }, { { 1, 2 } }, { 8 } },
214 	{ MEDIA_BUS_FMT_YVYU8_2X8, V4L2_PIX_FMT_NV61, 1,
215 	  { { 1, 1 } }, { { 1, 2 } }, { 8 } },
216 	{ MEDIA_BUS_FMT_UYVY8_2X8, V4L2_PIX_FMT_NV61, 1,
217 	  { { 1, 1 } }, { { 1, 2 } }, { 8 } },
218 	{ MEDIA_BUS_FMT_VYUY8_2X8, V4L2_PIX_FMT_NV61, 1,
219 	  { { 1, 1 } }, { { 1, 2 } }, { 8 } },
220 };
221 
222 static const struct camss_format_info formats_pix_8x96[] = {
223 	{ MEDIA_BUS_FMT_YUYV8_1_5X8, V4L2_PIX_FMT_NV12, 1,
224 	  { { 1, 1 } }, { { 2, 3 } }, { 8 } },
225 	{ MEDIA_BUS_FMT_YVYU8_1_5X8, V4L2_PIX_FMT_NV12, 1,
226 	  { { 1, 1 } }, { { 2, 3 } }, { 8 } },
227 	{ MEDIA_BUS_FMT_UYVY8_1_5X8, V4L2_PIX_FMT_NV12, 1,
228 	  { { 1, 1 } }, { { 2, 3 } }, { 8 } },
229 	{ MEDIA_BUS_FMT_VYUY8_1_5X8, V4L2_PIX_FMT_NV12, 1,
230 	  { { 1, 1 } }, { { 2, 3 } }, { 8 } },
231 	{ MEDIA_BUS_FMT_YUYV8_1_5X8, V4L2_PIX_FMT_NV21, 1,
232 	  { { 1, 1 } }, { { 2, 3 } }, { 8 } },
233 	{ MEDIA_BUS_FMT_YVYU8_1_5X8, V4L2_PIX_FMT_NV21, 1,
234 	  { { 1, 1 } }, { { 2, 3 } }, { 8 } },
235 	{ MEDIA_BUS_FMT_UYVY8_1_5X8, V4L2_PIX_FMT_NV21, 1,
236 	  { { 1, 1 } }, { { 2, 3 } }, { 8 } },
237 	{ MEDIA_BUS_FMT_VYUY8_1_5X8, V4L2_PIX_FMT_NV21, 1,
238 	  { { 1, 1 } }, { { 2, 3 } }, { 8 } },
239 	{ MEDIA_BUS_FMT_YUYV8_2X8, V4L2_PIX_FMT_NV16, 1,
240 	  { { 1, 1 } }, { { 1, 2 } }, { 8 } },
241 	{ MEDIA_BUS_FMT_YVYU8_2X8, V4L2_PIX_FMT_NV16, 1,
242 	  { { 1, 1 } }, { { 1, 2 } }, { 8 } },
243 	{ MEDIA_BUS_FMT_UYVY8_2X8, V4L2_PIX_FMT_NV16, 1,
244 	  { { 1, 1 } }, { { 1, 2 } }, { 8 } },
245 	{ MEDIA_BUS_FMT_VYUY8_2X8, V4L2_PIX_FMT_NV16, 1,
246 	  { { 1, 1 } }, { { 1, 2 } }, { 8 } },
247 	{ MEDIA_BUS_FMT_YUYV8_2X8, V4L2_PIX_FMT_NV61, 1,
248 	  { { 1, 1 } }, { { 1, 2 } }, { 8 } },
249 	{ MEDIA_BUS_FMT_YVYU8_2X8, V4L2_PIX_FMT_NV61, 1,
250 	  { { 1, 1 } }, { { 1, 2 } }, { 8 } },
251 	{ MEDIA_BUS_FMT_UYVY8_2X8, V4L2_PIX_FMT_NV61, 1,
252 	  { { 1, 1 } }, { { 1, 2 } }, { 8 } },
253 	{ MEDIA_BUS_FMT_VYUY8_2X8, V4L2_PIX_FMT_NV61, 1,
254 	  { { 1, 1 } }, { { 1, 2 } }, { 8 } },
255 	{ MEDIA_BUS_FMT_UYVY8_2X8, V4L2_PIX_FMT_UYVY, 1,
256 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
257 	{ MEDIA_BUS_FMT_VYUY8_2X8, V4L2_PIX_FMT_VYUY, 1,
258 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
259 	{ MEDIA_BUS_FMT_YUYV8_2X8, V4L2_PIX_FMT_YUYV, 1,
260 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
261 	{ MEDIA_BUS_FMT_YVYU8_2X8, V4L2_PIX_FMT_YVYU, 1,
262 	  { { 1, 1 } }, { { 1, 1 } }, { 16 } },
263 };
264 
265 /* -----------------------------------------------------------------------------
266  * Helper functions
267  */
268 
video_find_format(u32 code,u32 pixelformat,const struct camss_format_info * formats,unsigned int nformats)269 static int video_find_format(u32 code, u32 pixelformat,
270 			     const struct camss_format_info *formats,
271 			     unsigned int nformats)
272 {
273 	int i;
274 
275 	for (i = 0; i < nformats; i++) {
276 		if (formats[i].code == code &&
277 		    formats[i].pixelformat == pixelformat)
278 			return i;
279 	}
280 
281 	for (i = 0; i < nformats; i++)
282 		if (formats[i].code == code)
283 			return i;
284 
285 	WARN_ON(1);
286 
287 	return -EINVAL;
288 }
289 
290 /*
291  * video_mbus_to_pix_mp - Convert v4l2_mbus_framefmt to v4l2_pix_format_mplane
292  * @mbus: v4l2_mbus_framefmt format (input)
293  * @pix: v4l2_pix_format_mplane format (output)
294  * @f: a pointer to formats array element to be used for the conversion
295  * @alignment: bytesperline alignment value
296  *
297  * Fill the output pix structure with information from the input mbus format.
298  *
299  * Return 0 on success or a negative error code otherwise
300  */
video_mbus_to_pix_mp(const struct v4l2_mbus_framefmt * mbus,struct v4l2_pix_format_mplane * pix,const struct camss_format_info * f,unsigned int alignment)301 static int video_mbus_to_pix_mp(const struct v4l2_mbus_framefmt *mbus,
302 				struct v4l2_pix_format_mplane *pix,
303 				const struct camss_format_info *f,
304 				unsigned int alignment)
305 {
306 	unsigned int i;
307 	u32 bytesperline;
308 
309 	memset(pix, 0, sizeof(*pix));
310 	v4l2_fill_pix_format_mplane(pix, mbus);
311 	pix->pixelformat = f->pixelformat;
312 	pix->num_planes = f->planes;
313 	for (i = 0; i < pix->num_planes; i++) {
314 		bytesperline = pix->width / f->hsub[i].numerator *
315 			f->hsub[i].denominator * f->bpp[i] / 8;
316 		bytesperline = ALIGN(bytesperline, alignment);
317 		pix->plane_fmt[i].bytesperline = bytesperline;
318 		pix->plane_fmt[i].sizeimage = pix->height /
319 				f->vsub[i].numerator * f->vsub[i].denominator *
320 				bytesperline;
321 	}
322 
323 	return 0;
324 }
325 
video_remote_subdev(struct camss_video * video,u32 * pad)326 static struct v4l2_subdev *video_remote_subdev(struct camss_video *video,
327 					       u32 *pad)
328 {
329 	struct media_pad *remote;
330 
331 	remote = media_pad_remote_pad_first(&video->pad);
332 
333 	if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
334 		return NULL;
335 
336 	if (pad)
337 		*pad = remote->index;
338 
339 	return media_entity_to_v4l2_subdev(remote->entity);
340 }
341 
video_get_subdev_format(struct camss_video * video,struct v4l2_format * format)342 static int video_get_subdev_format(struct camss_video *video,
343 				   struct v4l2_format *format)
344 {
345 	struct v4l2_subdev_format fmt;
346 	struct v4l2_subdev *subdev;
347 	u32 pad;
348 	int ret;
349 
350 	subdev = video_remote_subdev(video, &pad);
351 	if (subdev == NULL)
352 		return -EPIPE;
353 
354 	fmt.pad = pad;
355 	fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
356 
357 	ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
358 	if (ret)
359 		return ret;
360 
361 	ret = video_find_format(fmt.format.code,
362 				format->fmt.pix_mp.pixelformat,
363 				video->formats, video->nformats);
364 	if (ret < 0)
365 		return ret;
366 
367 	format->type = video->type;
368 
369 	return video_mbus_to_pix_mp(&fmt.format, &format->fmt.pix_mp,
370 				    &video->formats[ret], video->bpl_alignment);
371 }
372 
373 /* -----------------------------------------------------------------------------
374  * Video queue operations
375  */
376 
video_queue_setup(struct vb2_queue * q,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])377 static int video_queue_setup(struct vb2_queue *q,
378 	unsigned int *num_buffers, unsigned int *num_planes,
379 	unsigned int sizes[], struct device *alloc_devs[])
380 {
381 	struct camss_video *video = vb2_get_drv_priv(q);
382 	const struct v4l2_pix_format_mplane *format =
383 						&video->active_fmt.fmt.pix_mp;
384 	unsigned int i;
385 
386 	if (*num_planes) {
387 		if (*num_planes != format->num_planes)
388 			return -EINVAL;
389 
390 		for (i = 0; i < *num_planes; i++)
391 			if (sizes[i] < format->plane_fmt[i].sizeimage)
392 				return -EINVAL;
393 
394 		return 0;
395 	}
396 
397 	*num_planes = format->num_planes;
398 
399 	for (i = 0; i < *num_planes; i++)
400 		sizes[i] = format->plane_fmt[i].sizeimage;
401 
402 	return 0;
403 }
404 
video_buf_init(struct vb2_buffer * vb)405 static int video_buf_init(struct vb2_buffer *vb)
406 {
407 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
408 	struct camss_video *video = vb2_get_drv_priv(vb->vb2_queue);
409 	struct camss_buffer *buffer = container_of(vbuf, struct camss_buffer,
410 						   vb);
411 	const struct v4l2_pix_format_mplane *format =
412 						&video->active_fmt.fmt.pix_mp;
413 	struct sg_table *sgt;
414 	unsigned int i;
415 
416 	for (i = 0; i < format->num_planes; i++) {
417 		sgt = vb2_dma_sg_plane_desc(vb, i);
418 		if (!sgt)
419 			return -EFAULT;
420 
421 		buffer->addr[i] = sg_dma_address(sgt->sgl);
422 	}
423 
424 	if (format->pixelformat == V4L2_PIX_FMT_NV12 ||
425 			format->pixelformat == V4L2_PIX_FMT_NV21 ||
426 			format->pixelformat == V4L2_PIX_FMT_NV16 ||
427 			format->pixelformat == V4L2_PIX_FMT_NV61)
428 		buffer->addr[1] = buffer->addr[0] +
429 				format->plane_fmt[0].bytesperline *
430 				format->height;
431 
432 	return 0;
433 }
434 
video_buf_prepare(struct vb2_buffer * vb)435 static int video_buf_prepare(struct vb2_buffer *vb)
436 {
437 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
438 	struct camss_video *video = vb2_get_drv_priv(vb->vb2_queue);
439 	const struct v4l2_pix_format_mplane *format =
440 						&video->active_fmt.fmt.pix_mp;
441 	unsigned int i;
442 
443 	for (i = 0; i < format->num_planes; i++) {
444 		if (format->plane_fmt[i].sizeimage > vb2_plane_size(vb, i))
445 			return -EINVAL;
446 
447 		vb2_set_plane_payload(vb, i, format->plane_fmt[i].sizeimage);
448 	}
449 
450 	vbuf->field = V4L2_FIELD_NONE;
451 
452 	return 0;
453 }
454 
video_buf_queue(struct vb2_buffer * vb)455 static void video_buf_queue(struct vb2_buffer *vb)
456 {
457 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
458 	struct camss_video *video = vb2_get_drv_priv(vb->vb2_queue);
459 	struct camss_buffer *buffer = container_of(vbuf, struct camss_buffer,
460 						   vb);
461 
462 	video->ops->queue_buffer(video, buffer);
463 }
464 
video_check_format(struct camss_video * video)465 static int video_check_format(struct camss_video *video)
466 {
467 	struct v4l2_pix_format_mplane *pix = &video->active_fmt.fmt.pix_mp;
468 	struct v4l2_format format;
469 	struct v4l2_pix_format_mplane *sd_pix = &format.fmt.pix_mp;
470 	int ret;
471 
472 	sd_pix->pixelformat = pix->pixelformat;
473 	ret = video_get_subdev_format(video, &format);
474 	if (ret < 0)
475 		return ret;
476 
477 	if (pix->pixelformat != sd_pix->pixelformat ||
478 	    pix->height != sd_pix->height ||
479 	    pix->width != sd_pix->width ||
480 	    pix->num_planes != sd_pix->num_planes ||
481 	    pix->field != format.fmt.pix_mp.field)
482 		return -EPIPE;
483 
484 	return 0;
485 }
486 
video_start_streaming(struct vb2_queue * q,unsigned int count)487 static int video_start_streaming(struct vb2_queue *q, unsigned int count)
488 {
489 	struct camss_video *video = vb2_get_drv_priv(q);
490 	struct video_device *vdev = &video->vdev;
491 	struct media_entity *entity;
492 	struct media_pad *pad;
493 	struct v4l2_subdev *subdev;
494 	int ret;
495 
496 	ret = video_device_pipeline_start(vdev, &video->pipe);
497 	if (ret < 0)
498 		goto flush_buffers;
499 
500 	ret = video_check_format(video);
501 	if (ret < 0)
502 		goto error;
503 
504 	entity = &vdev->entity;
505 	while (1) {
506 		pad = &entity->pads[0];
507 		if (!(pad->flags & MEDIA_PAD_FL_SINK))
508 			break;
509 
510 		pad = media_pad_remote_pad_first(pad);
511 		if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
512 			break;
513 
514 		entity = pad->entity;
515 		subdev = media_entity_to_v4l2_subdev(entity);
516 
517 		ret = v4l2_subdev_call(subdev, video, s_stream, 1);
518 		if (ret < 0 && ret != -ENOIOCTLCMD)
519 			goto error;
520 	}
521 
522 	return 0;
523 
524 error:
525 	video_device_pipeline_stop(vdev);
526 
527 flush_buffers:
528 	video->ops->flush_buffers(video, VB2_BUF_STATE_QUEUED);
529 
530 	return ret;
531 }
532 
video_stop_streaming(struct vb2_queue * q)533 static void video_stop_streaming(struct vb2_queue *q)
534 {
535 	struct camss_video *video = vb2_get_drv_priv(q);
536 	struct video_device *vdev = &video->vdev;
537 	struct media_entity *entity;
538 	struct media_pad *pad;
539 	struct v4l2_subdev *subdev;
540 
541 	entity = &vdev->entity;
542 	while (1) {
543 		pad = &entity->pads[0];
544 		if (!(pad->flags & MEDIA_PAD_FL_SINK))
545 			break;
546 
547 		pad = media_pad_remote_pad_first(pad);
548 		if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
549 			break;
550 
551 		entity = pad->entity;
552 		subdev = media_entity_to_v4l2_subdev(entity);
553 
554 		v4l2_subdev_call(subdev, video, s_stream, 0);
555 	}
556 
557 	video_device_pipeline_stop(vdev);
558 
559 	video->ops->flush_buffers(video, VB2_BUF_STATE_ERROR);
560 }
561 
562 static const struct vb2_ops msm_video_vb2_q_ops = {
563 	.queue_setup     = video_queue_setup,
564 	.wait_prepare    = vb2_ops_wait_prepare,
565 	.wait_finish     = vb2_ops_wait_finish,
566 	.buf_init        = video_buf_init,
567 	.buf_prepare     = video_buf_prepare,
568 	.buf_queue       = video_buf_queue,
569 	.start_streaming = video_start_streaming,
570 	.stop_streaming  = video_stop_streaming,
571 };
572 
573 /* -----------------------------------------------------------------------------
574  * V4L2 ioctls
575  */
576 
video_querycap(struct file * file,void * fh,struct v4l2_capability * cap)577 static int video_querycap(struct file *file, void *fh,
578 			  struct v4l2_capability *cap)
579 {
580 	strscpy(cap->driver, "qcom-camss", sizeof(cap->driver));
581 	strscpy(cap->card, "Qualcomm Camera Subsystem", sizeof(cap->card));
582 
583 	return 0;
584 }
585 
video_enum_fmt(struct file * file,void * fh,struct v4l2_fmtdesc * f)586 static int video_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
587 {
588 	struct camss_video *video = video_drvdata(file);
589 	int i, j, k;
590 	u32 mcode = f->mbus_code;
591 
592 	if (f->type != video->type)
593 		return -EINVAL;
594 
595 	if (f->index >= video->nformats)
596 		return -EINVAL;
597 
598 	/*
599 	 * Find index "i" of "k"th unique pixelformat in formats array.
600 	 *
601 	 * If f->mbus_code passed to video_enum_fmt() is not zero, a device
602 	 * with V4L2_CAP_IO_MC capability restricts enumeration to only the
603 	 * pixel formats that can be produced from that media bus code.
604 	 * This is implemented by skipping video->formats[] entries with
605 	 * code != f->mbus_code (if f->mbus_code is not zero).
606 	 * If the f->mbus_code passed to video_enum_fmt() is not supported,
607 	 * -EINVAL is returned.
608 	 * If f->mbus_code is zero, all the pixel formats are enumerated.
609 	 */
610 	k = -1;
611 	for (i = 0; i < video->nformats; i++) {
612 		if (mcode != 0 && video->formats[i].code != mcode)
613 			continue;
614 
615 		for (j = 0; j < i; j++) {
616 			if (mcode != 0 && video->formats[j].code != mcode)
617 				continue;
618 			if (video->formats[i].pixelformat ==
619 					video->formats[j].pixelformat)
620 				break;
621 		}
622 
623 		if (j == i)
624 			k++;
625 
626 		if (k == f->index)
627 			break;
628 	}
629 
630 	if (k == -1 || k < f->index)
631 		/*
632 		 * All the unique pixel formats matching the arguments
633 		 * have been enumerated (k >= 0 and f->index > 0), or
634 		 * no pixel formats match the non-zero f->mbus_code (k == -1).
635 		 */
636 		return -EINVAL;
637 
638 	f->pixelformat = video->formats[i].pixelformat;
639 
640 	return 0;
641 }
642 
video_enum_framesizes(struct file * file,void * fh,struct v4l2_frmsizeenum * fsize)643 static int video_enum_framesizes(struct file *file, void *fh,
644 				 struct v4l2_frmsizeenum *fsize)
645 {
646 	struct camss_video *video = video_drvdata(file);
647 	int i;
648 
649 	if (fsize->index)
650 		return -EINVAL;
651 
652 	/* Only accept pixel format present in the formats[] table */
653 	for (i = 0; i < video->nformats; i++) {
654 		if (video->formats[i].pixelformat == fsize->pixel_format)
655 			break;
656 	}
657 
658 	if (i == video->nformats)
659 		return -EINVAL;
660 
661 	fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
662 	fsize->stepwise.min_width = CAMSS_FRAME_MIN_WIDTH;
663 	fsize->stepwise.max_width = CAMSS_FRAME_MAX_WIDTH;
664 	fsize->stepwise.min_height = CAMSS_FRAME_MIN_HEIGHT;
665 	fsize->stepwise.max_height = (video->line_based) ?
666 		CAMSS_FRAME_MAX_HEIGHT_PIX : CAMSS_FRAME_MAX_HEIGHT_RDI;
667 	fsize->stepwise.step_width = 1;
668 	fsize->stepwise.step_height = 1;
669 
670 	return 0;
671 }
672 
video_g_fmt(struct file * file,void * fh,struct v4l2_format * f)673 static int video_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
674 {
675 	struct camss_video *video = video_drvdata(file);
676 
677 	*f = video->active_fmt;
678 
679 	return 0;
680 }
681 
__video_try_fmt(struct camss_video * video,struct v4l2_format * f)682 static int __video_try_fmt(struct camss_video *video, struct v4l2_format *f)
683 {
684 	struct v4l2_pix_format_mplane *pix_mp;
685 	const struct camss_format_info *fi;
686 	struct v4l2_plane_pix_format *p;
687 	u32 bytesperline[3] = { 0 };
688 	u32 sizeimage[3] = { 0 };
689 	u32 width, height;
690 	u32 bpl, lines;
691 	int i, j;
692 
693 	pix_mp = &f->fmt.pix_mp;
694 
695 	if (video->line_based)
696 		for (i = 0; i < pix_mp->num_planes && i < 3; i++) {
697 			p = &pix_mp->plane_fmt[i];
698 			bytesperline[i] = clamp_t(u32, p->bytesperline,
699 						  1, 65528);
700 			sizeimage[i] = clamp_t(u32, p->sizeimage,
701 					       bytesperline[i],
702 					       bytesperline[i] * CAMSS_FRAME_MAX_HEIGHT_PIX);
703 		}
704 
705 	for (j = 0; j < video->nformats; j++)
706 		if (pix_mp->pixelformat == video->formats[j].pixelformat)
707 			break;
708 
709 	if (j == video->nformats)
710 		j = 0; /* default format */
711 
712 	fi = &video->formats[j];
713 	width = pix_mp->width;
714 	height = pix_mp->height;
715 
716 	memset(pix_mp, 0, sizeof(*pix_mp));
717 
718 	pix_mp->pixelformat = fi->pixelformat;
719 	pix_mp->width = clamp_t(u32, width, 1, CAMSS_FRAME_MAX_WIDTH);
720 	pix_mp->height = clamp_t(u32, height, 1, CAMSS_FRAME_MAX_HEIGHT_RDI);
721 	pix_mp->num_planes = fi->planes;
722 	for (i = 0; i < pix_mp->num_planes; i++) {
723 		bpl = pix_mp->width / fi->hsub[i].numerator *
724 			fi->hsub[i].denominator * fi->bpp[i] / 8;
725 		bpl = ALIGN(bpl, video->bpl_alignment);
726 		pix_mp->plane_fmt[i].bytesperline = bpl;
727 		pix_mp->plane_fmt[i].sizeimage = pix_mp->height /
728 			fi->vsub[i].numerator * fi->vsub[i].denominator * bpl;
729 	}
730 
731 	pix_mp->field = V4L2_FIELD_NONE;
732 	pix_mp->colorspace = V4L2_COLORSPACE_SRGB;
733 	pix_mp->flags = 0;
734 	pix_mp->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix_mp->colorspace);
735 	pix_mp->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
736 					pix_mp->colorspace, pix_mp->ycbcr_enc);
737 	pix_mp->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix_mp->colorspace);
738 
739 	if (video->line_based)
740 		for (i = 0; i < pix_mp->num_planes; i++) {
741 			p = &pix_mp->plane_fmt[i];
742 			p->bytesperline = clamp_t(u32, p->bytesperline,
743 						  1, 65528);
744 			p->sizeimage = clamp_t(u32, p->sizeimage,
745 					       p->bytesperline,
746 					       p->bytesperline * CAMSS_FRAME_MAX_HEIGHT_PIX);
747 			lines = p->sizeimage / p->bytesperline;
748 
749 			if (p->bytesperline < bytesperline[i])
750 				p->bytesperline = ALIGN(bytesperline[i], 8);
751 
752 			if (p->sizeimage < p->bytesperline * lines)
753 				p->sizeimage = p->bytesperline * lines;
754 
755 			if (p->sizeimage < sizeimage[i])
756 				p->sizeimage = sizeimage[i];
757 		}
758 
759 	return 0;
760 }
761 
video_try_fmt(struct file * file,void * fh,struct v4l2_format * f)762 static int video_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
763 {
764 	struct camss_video *video = video_drvdata(file);
765 
766 	return __video_try_fmt(video, f);
767 }
768 
video_s_fmt(struct file * file,void * fh,struct v4l2_format * f)769 static int video_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
770 {
771 	struct camss_video *video = video_drvdata(file);
772 	int ret;
773 
774 	if (vb2_is_busy(&video->vb2_q))
775 		return -EBUSY;
776 
777 	ret = __video_try_fmt(video, f);
778 	if (ret < 0)
779 		return ret;
780 
781 	video->active_fmt = *f;
782 
783 	return 0;
784 }
785 
video_enum_input(struct file * file,void * fh,struct v4l2_input * input)786 static int video_enum_input(struct file *file, void *fh,
787 			    struct v4l2_input *input)
788 {
789 	if (input->index > 0)
790 		return -EINVAL;
791 
792 	strscpy(input->name, "camera", sizeof(input->name));
793 	input->type = V4L2_INPUT_TYPE_CAMERA;
794 
795 	return 0;
796 }
797 
video_g_input(struct file * file,void * fh,unsigned int * input)798 static int video_g_input(struct file *file, void *fh, unsigned int *input)
799 {
800 	*input = 0;
801 
802 	return 0;
803 }
804 
video_s_input(struct file * file,void * fh,unsigned int input)805 static int video_s_input(struct file *file, void *fh, unsigned int input)
806 {
807 	return input == 0 ? 0 : -EINVAL;
808 }
809 
810 static const struct v4l2_ioctl_ops msm_vid_ioctl_ops = {
811 	.vidioc_querycap		= video_querycap,
812 	.vidioc_enum_fmt_vid_cap	= video_enum_fmt,
813 	.vidioc_enum_framesizes		= video_enum_framesizes,
814 	.vidioc_g_fmt_vid_cap_mplane	= video_g_fmt,
815 	.vidioc_s_fmt_vid_cap_mplane	= video_s_fmt,
816 	.vidioc_try_fmt_vid_cap_mplane	= video_try_fmt,
817 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
818 	.vidioc_querybuf		= vb2_ioctl_querybuf,
819 	.vidioc_qbuf			= vb2_ioctl_qbuf,
820 	.vidioc_expbuf			= vb2_ioctl_expbuf,
821 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
822 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
823 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
824 	.vidioc_streamon		= vb2_ioctl_streamon,
825 	.vidioc_streamoff		= vb2_ioctl_streamoff,
826 	.vidioc_enum_input		= video_enum_input,
827 	.vidioc_g_input			= video_g_input,
828 	.vidioc_s_input			= video_s_input,
829 };
830 
831 /* -----------------------------------------------------------------------------
832  * V4L2 file operations
833  */
834 
video_open(struct file * file)835 static int video_open(struct file *file)
836 {
837 	struct video_device *vdev = video_devdata(file);
838 	struct camss_video *video = video_drvdata(file);
839 	struct v4l2_fh *vfh;
840 	int ret;
841 
842 	mutex_lock(&video->lock);
843 
844 	vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
845 	if (vfh == NULL) {
846 		ret = -ENOMEM;
847 		goto error_alloc;
848 	}
849 
850 	v4l2_fh_init(vfh, vdev);
851 	v4l2_fh_add(vfh);
852 
853 	file->private_data = vfh;
854 
855 	ret = v4l2_pipeline_pm_get(&vdev->entity);
856 	if (ret < 0) {
857 		dev_err(video->camss->dev, "Failed to power up pipeline: %d\n",
858 			ret);
859 		goto error_pm_use;
860 	}
861 
862 	mutex_unlock(&video->lock);
863 
864 	return 0;
865 
866 error_pm_use:
867 	v4l2_fh_release(file);
868 
869 error_alloc:
870 	mutex_unlock(&video->lock);
871 
872 	return ret;
873 }
874 
video_release(struct file * file)875 static int video_release(struct file *file)
876 {
877 	struct video_device *vdev = video_devdata(file);
878 
879 	vb2_fop_release(file);
880 
881 	v4l2_pipeline_pm_put(&vdev->entity);
882 
883 	file->private_data = NULL;
884 
885 	return 0;
886 }
887 
888 static const struct v4l2_file_operations msm_vid_fops = {
889 	.owner          = THIS_MODULE,
890 	.unlocked_ioctl = video_ioctl2,
891 	.open           = video_open,
892 	.release        = video_release,
893 	.poll           = vb2_fop_poll,
894 	.mmap		= vb2_fop_mmap,
895 	.read		= vb2_fop_read,
896 };
897 
898 /* -----------------------------------------------------------------------------
899  * CAMSS video core
900  */
901 
msm_video_release(struct video_device * vdev)902 static void msm_video_release(struct video_device *vdev)
903 {
904 	struct camss_video *video = video_get_drvdata(vdev);
905 
906 	media_entity_cleanup(&vdev->entity);
907 
908 	mutex_destroy(&video->q_lock);
909 	mutex_destroy(&video->lock);
910 
911 	if (atomic_dec_and_test(&video->camss->ref_count))
912 		camss_delete(video->camss);
913 }
914 
915 /*
916  * msm_video_init_format - Helper function to initialize format
917  * @video: struct camss_video
918  *
919  * Initialize pad format with default value.
920  *
921  * Return 0 on success or a negative error code otherwise
922  */
msm_video_init_format(struct camss_video * video)923 static int msm_video_init_format(struct camss_video *video)
924 {
925 	int ret;
926 	struct v4l2_format format = {
927 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
928 		.fmt.pix_mp = {
929 			.width = 1920,
930 			.height = 1080,
931 			.pixelformat = video->formats[0].pixelformat,
932 		},
933 	};
934 
935 	ret = __video_try_fmt(video, &format);
936 	if (ret < 0)
937 		return ret;
938 
939 	video->active_fmt = format;
940 
941 	return 0;
942 }
943 
944 /*
945  * msm_video_register - Register a video device node
946  * @video: struct camss_video
947  * @v4l2_dev: V4L2 device
948  * @name: name to be used for the video device node
949  *
950  * Initialize and register a video device node to a V4L2 device. Also
951  * initialize the vb2 queue.
952  *
953  * Return 0 on success or a negative error code otherwise
954  */
955 
msm_video_register(struct camss_video * video,struct v4l2_device * v4l2_dev,const char * name,int is_pix)956 int msm_video_register(struct camss_video *video, struct v4l2_device *v4l2_dev,
957 		       const char *name, int is_pix)
958 {
959 	struct media_pad *pad = &video->pad;
960 	struct video_device *vdev;
961 	struct vb2_queue *q;
962 	int ret;
963 
964 	vdev = &video->vdev;
965 
966 	mutex_init(&video->q_lock);
967 
968 	q = &video->vb2_q;
969 	q->drv_priv = video;
970 	q->mem_ops = &vb2_dma_sg_memops;
971 	q->ops = &msm_video_vb2_q_ops;
972 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
973 	q->io_modes = VB2_DMABUF | VB2_MMAP | VB2_READ;
974 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
975 	q->buf_struct_size = sizeof(struct camss_buffer);
976 	q->dev = video->camss->dev;
977 	q->lock = &video->q_lock;
978 	ret = vb2_queue_init(q);
979 	if (ret < 0) {
980 		dev_err(v4l2_dev->dev, "Failed to init vb2 queue: %d\n", ret);
981 		goto error_vb2_init;
982 	}
983 
984 	pad->flags = MEDIA_PAD_FL_SINK;
985 	ret = media_entity_pads_init(&vdev->entity, 1, pad);
986 	if (ret < 0) {
987 		dev_err(v4l2_dev->dev, "Failed to init video entity: %d\n",
988 			ret);
989 		goto error_vb2_init;
990 	}
991 
992 	mutex_init(&video->lock);
993 
994 	if (video->camss->version == CAMSS_8x16) {
995 		if (is_pix) {
996 			video->formats = formats_pix_8x16;
997 			video->nformats = ARRAY_SIZE(formats_pix_8x16);
998 		} else {
999 			video->formats = formats_rdi_8x16;
1000 			video->nformats = ARRAY_SIZE(formats_rdi_8x16);
1001 		}
1002 	} else if (video->camss->version == CAMSS_8x96 ||
1003 		   video->camss->version == CAMSS_660) {
1004 		if (is_pix) {
1005 			video->formats = formats_pix_8x96;
1006 			video->nformats = ARRAY_SIZE(formats_pix_8x96);
1007 		} else {
1008 			video->formats = formats_rdi_8x96;
1009 			video->nformats = ARRAY_SIZE(formats_rdi_8x96);
1010 		}
1011 	}  else if (video->camss->version == CAMSS_845 ||
1012 		    video->camss->version == CAMSS_8250) {
1013 		video->formats = formats_rdi_845;
1014 		video->nformats = ARRAY_SIZE(formats_rdi_845);
1015 	} else {
1016 		ret = -EINVAL;
1017 		goto error_video_register;
1018 	}
1019 
1020 	ret = msm_video_init_format(video);
1021 	if (ret < 0) {
1022 		dev_err(v4l2_dev->dev, "Failed to init format: %d\n", ret);
1023 		goto error_video_register;
1024 	}
1025 
1026 	vdev->fops = &msm_vid_fops;
1027 	vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_STREAMING
1028 			  | V4L2_CAP_READWRITE | V4L2_CAP_IO_MC;
1029 	vdev->ioctl_ops = &msm_vid_ioctl_ops;
1030 	vdev->release = msm_video_release;
1031 	vdev->v4l2_dev = v4l2_dev;
1032 	vdev->vfl_dir = VFL_DIR_RX;
1033 	vdev->queue = &video->vb2_q;
1034 	vdev->lock = &video->lock;
1035 	strscpy(vdev->name, name, sizeof(vdev->name));
1036 
1037 	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1038 	if (ret < 0) {
1039 		dev_err(v4l2_dev->dev, "Failed to register video device: %d\n",
1040 			ret);
1041 		goto error_video_register;
1042 	}
1043 
1044 	video_set_drvdata(vdev, video);
1045 	atomic_inc(&video->camss->ref_count);
1046 
1047 	return 0;
1048 
1049 error_video_register:
1050 	media_entity_cleanup(&vdev->entity);
1051 	mutex_destroy(&video->lock);
1052 error_vb2_init:
1053 	mutex_destroy(&video->q_lock);
1054 
1055 	return ret;
1056 }
1057 
msm_video_unregister(struct camss_video * video)1058 void msm_video_unregister(struct camss_video *video)
1059 {
1060 	atomic_inc(&video->camss->ref_count);
1061 	vb2_video_unregister_device(&video->vdev);
1062 	atomic_dec(&video->camss->ref_count);
1063 }
1064