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