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