• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Coda multi-standard codec IP
4  *
5  * Copyright (C) 2012 Vista Silicon S.L.
6  *    Javier Martin, <javier.martin@vista-silicon.com>
7  *    Xavier Duret
8  */
9 
10 #include <linux/clk.h>
11 #include <linux/debugfs.h>
12 #include <linux/delay.h>
13 #include <linux/firmware.h>
14 #include <linux/gcd.h>
15 #include <linux/genalloc.h>
16 #include <linux/idr.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/irq.h>
20 #include <linux/kfifo.h>
21 #include <linux/module.h>
22 #include <linux/of_device.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/slab.h>
26 #include <linux/videodev2.h>
27 #include <linux/of.h>
28 #include <linux/platform_data/media/coda.h>
29 #include <linux/reset.h>
30 
31 #include <media/v4l2-ctrls.h>
32 #include <media/v4l2-device.h>
33 #include <media/v4l2-event.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/v4l2-mem2mem.h>
36 #include <media/videobuf2-v4l2.h>
37 #include <media/videobuf2-dma-contig.h>
38 #include <media/videobuf2-vmalloc.h>
39 
40 #include "coda.h"
41 #include "imx-vdoa.h"
42 
43 #define CODA_NAME		"coda"
44 
45 #define CODADX6_MAX_INSTANCES	4
46 #define CODA_MAX_FORMATS	4
47 
48 #define CODA_ISRAM_SIZE	(2048 * 2)
49 
50 #define MIN_W 48
51 #define MIN_H 16
52 
53 #define S_ALIGN		1 /* multiple of 2 */
54 #define W_ALIGN		1 /* multiple of 2 */
55 #define H_ALIGN		1 /* multiple of 2 */
56 
57 #define fh_to_ctx(__fh)	container_of(__fh, struct coda_ctx, fh)
58 
59 int coda_debug;
60 module_param(coda_debug, int, 0644);
61 MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
62 
63 static int disable_tiling;
64 module_param(disable_tiling, int, 0644);
65 MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers");
66 
67 static int disable_vdoa;
68 module_param(disable_vdoa, int, 0644);
69 MODULE_PARM_DESC(disable_vdoa, "Disable Video Data Order Adapter tiled to raster-scan conversion");
70 
71 static int enable_bwb = 0;
72 module_param(enable_bwb, int, 0644);
73 MODULE_PARM_DESC(enable_bwb, "Enable BWB unit for decoding, may crash on certain streams");
74 
coda_write(struct coda_dev * dev,u32 data,u32 reg)75 void coda_write(struct coda_dev *dev, u32 data, u32 reg)
76 {
77 	v4l2_dbg(3, coda_debug, &dev->v4l2_dev,
78 		 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
79 	writel(data, dev->regs_base + reg);
80 }
81 
coda_read(struct coda_dev * dev,u32 reg)82 unsigned int coda_read(struct coda_dev *dev, u32 reg)
83 {
84 	u32 data;
85 
86 	data = readl(dev->regs_base + reg);
87 	v4l2_dbg(3, coda_debug, &dev->v4l2_dev,
88 		 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
89 	return data;
90 }
91 
coda_write_base(struct coda_ctx * ctx,struct coda_q_data * q_data,struct vb2_v4l2_buffer * buf,unsigned int reg_y)92 void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
93 		     struct vb2_v4l2_buffer *buf, unsigned int reg_y)
94 {
95 	u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
96 	u32 base_cb, base_cr;
97 
98 	switch (q_data->fourcc) {
99 	case V4L2_PIX_FMT_YUYV:
100 		/* Fallthrough: IN -H264-> CODA -NV12 MB-> VDOA -YUYV-> OUT */
101 	case V4L2_PIX_FMT_NV12:
102 	case V4L2_PIX_FMT_YUV420:
103 	default:
104 		base_cb = base_y + q_data->bytesperline * q_data->height;
105 		base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
106 		break;
107 	case V4L2_PIX_FMT_YVU420:
108 		/* Switch Cb and Cr for YVU420 format */
109 		base_cr = base_y + q_data->bytesperline * q_data->height;
110 		base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
111 		break;
112 	case V4L2_PIX_FMT_YUV422P:
113 		base_cb = base_y + q_data->bytesperline * q_data->height;
114 		base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
115 	}
116 
117 	coda_write(ctx->dev, base_y, reg_y);
118 	coda_write(ctx->dev, base_cb, reg_y + 4);
119 	coda_write(ctx->dev, base_cr, reg_y + 8);
120 }
121 
122 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
123 	{ mode, src_fourcc, dst_fourcc, max_w, max_h }
124 
125 /*
126  * Arrays of codecs supported by each given version of Coda:
127  *  i.MX27 -> codadx6
128  *  i.MX51 -> codahx4
129  *  i.MX53 -> coda7
130  *  i.MX6  -> coda960
131  * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
132  */
133 static const struct coda_codec codadx6_codecs[] = {
134 	CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,  720, 576),
135 	CODA_CODEC(CODADX6_MODE_ENCODE_MP4,  V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
136 };
137 
138 static const struct coda_codec codahx4_codecs[] = {
139 	CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,   720, 576),
140 	CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264,   V4L2_PIX_FMT_YUV420, 1920, 1088),
141 	CODA_CODEC(CODA7_MODE_DECODE_MP2,  V4L2_PIX_FMT_MPEG2,  V4L2_PIX_FMT_YUV420, 1920, 1088),
142 	CODA_CODEC(CODA7_MODE_DECODE_MP4,  V4L2_PIX_FMT_MPEG4,  V4L2_PIX_FMT_YUV420, 1280, 720),
143 };
144 
145 static const struct coda_codec coda7_codecs[] = {
146 	CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,   1280, 720),
147 	CODA_CODEC(CODA7_MODE_ENCODE_MP4,  V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4,  1280, 720),
148 	CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG,   8192, 8192),
149 	CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264,   V4L2_PIX_FMT_YUV420, 1920, 1088),
150 	CODA_CODEC(CODA7_MODE_DECODE_MP2,  V4L2_PIX_FMT_MPEG2,  V4L2_PIX_FMT_YUV420, 1920, 1088),
151 	CODA_CODEC(CODA7_MODE_DECODE_MP4,  V4L2_PIX_FMT_MPEG4,  V4L2_PIX_FMT_YUV420, 1920, 1088),
152 	CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG,   V4L2_PIX_FMT_YUV420, 8192, 8192),
153 };
154 
155 static const struct coda_codec coda9_codecs[] = {
156 	CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,   1920, 1088),
157 	CODA_CODEC(CODA9_MODE_ENCODE_MP4,  V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4,  1920, 1088),
158 	CODA_CODEC(CODA9_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG,   8192, 8192),
159 	CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264,   V4L2_PIX_FMT_YUV420, 1920, 1088),
160 	CODA_CODEC(CODA9_MODE_DECODE_MP2,  V4L2_PIX_FMT_MPEG2,  V4L2_PIX_FMT_YUV420, 1920, 1088),
161 	CODA_CODEC(CODA9_MODE_DECODE_MP4,  V4L2_PIX_FMT_MPEG4,  V4L2_PIX_FMT_YUV420, 1920, 1088),
162 	CODA_CODEC(CODA9_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG,	V4L2_PIX_FMT_YUV420, 8192, 8192),
163 };
164 
165 struct coda_video_device {
166 	const char *name;
167 	enum coda_inst_type type;
168 	const struct coda_context_ops *ops;
169 	bool direct;
170 	u32 src_formats[CODA_MAX_FORMATS];
171 	u32 dst_formats[CODA_MAX_FORMATS];
172 };
173 
174 static const struct coda_video_device coda_bit_encoder = {
175 	.name = "coda-encoder",
176 	.type = CODA_INST_ENCODER,
177 	.ops = &coda_bit_encode_ops,
178 	.src_formats = {
179 		V4L2_PIX_FMT_NV12,
180 		V4L2_PIX_FMT_YUV420,
181 		V4L2_PIX_FMT_YVU420,
182 	},
183 	.dst_formats = {
184 		V4L2_PIX_FMT_H264,
185 		V4L2_PIX_FMT_MPEG4,
186 	},
187 };
188 
189 static const struct coda_video_device coda_bit_jpeg_encoder = {
190 	.name = "coda-jpeg-encoder",
191 	.type = CODA_INST_ENCODER,
192 	.ops = &coda_bit_encode_ops,
193 	.src_formats = {
194 		V4L2_PIX_FMT_NV12,
195 		V4L2_PIX_FMT_YUV420,
196 		V4L2_PIX_FMT_YVU420,
197 		V4L2_PIX_FMT_YUV422P,
198 	},
199 	.dst_formats = {
200 		V4L2_PIX_FMT_JPEG,
201 	},
202 };
203 
204 static const struct coda_video_device coda_bit_decoder = {
205 	.name = "coda-decoder",
206 	.type = CODA_INST_DECODER,
207 	.ops = &coda_bit_decode_ops,
208 	.src_formats = {
209 		V4L2_PIX_FMT_H264,
210 		V4L2_PIX_FMT_MPEG2,
211 		V4L2_PIX_FMT_MPEG4,
212 	},
213 	.dst_formats = {
214 		V4L2_PIX_FMT_NV12,
215 		V4L2_PIX_FMT_YUV420,
216 		V4L2_PIX_FMT_YVU420,
217 		/*
218 		 * If V4L2_PIX_FMT_YUYV should be default,
219 		 * set_default_params() must be adjusted.
220 		 */
221 		V4L2_PIX_FMT_YUYV,
222 	},
223 };
224 
225 static const struct coda_video_device coda_bit_jpeg_decoder = {
226 	.name = "coda-jpeg-decoder",
227 	.type = CODA_INST_DECODER,
228 	.ops = &coda_bit_decode_ops,
229 	.src_formats = {
230 		V4L2_PIX_FMT_JPEG,
231 	},
232 	.dst_formats = {
233 		V4L2_PIX_FMT_NV12,
234 		V4L2_PIX_FMT_YUV420,
235 		V4L2_PIX_FMT_YVU420,
236 		V4L2_PIX_FMT_YUV422P,
237 	},
238 };
239 
240 static const struct coda_video_device coda9_jpeg_encoder = {
241 	.name = "coda-jpeg-encoder",
242 	.type = CODA_INST_ENCODER,
243 	.ops = &coda9_jpeg_encode_ops,
244 	.direct = true,
245 	.src_formats = {
246 		V4L2_PIX_FMT_NV12,
247 		V4L2_PIX_FMT_YUV420,
248 		V4L2_PIX_FMT_YVU420,
249 		V4L2_PIX_FMT_YUV422P,
250 	},
251 	.dst_formats = {
252 		V4L2_PIX_FMT_JPEG,
253 	},
254 };
255 
256 static const struct coda_video_device coda9_jpeg_decoder = {
257 	.name = "coda-jpeg-decoder",
258 	.type = CODA_INST_DECODER,
259 	.ops = &coda9_jpeg_decode_ops,
260 	.direct = true,
261 	.src_formats = {
262 		V4L2_PIX_FMT_JPEG,
263 	},
264 	.dst_formats = {
265 		V4L2_PIX_FMT_NV12,
266 		V4L2_PIX_FMT_YUV420,
267 		V4L2_PIX_FMT_YVU420,
268 		V4L2_PIX_FMT_YUV422P,
269 	},
270 };
271 
272 static const struct coda_video_device *codadx6_video_devices[] = {
273 	&coda_bit_encoder,
274 };
275 
276 static const struct coda_video_device *codahx4_video_devices[] = {
277 	&coda_bit_encoder,
278 	&coda_bit_decoder,
279 };
280 
281 static const struct coda_video_device *coda7_video_devices[] = {
282 	&coda_bit_jpeg_encoder,
283 	&coda_bit_jpeg_decoder,
284 	&coda_bit_encoder,
285 	&coda_bit_decoder,
286 };
287 
288 static const struct coda_video_device *coda9_video_devices[] = {
289 	&coda9_jpeg_encoder,
290 	&coda9_jpeg_decoder,
291 	&coda_bit_encoder,
292 	&coda_bit_decoder,
293 };
294 
295 /*
296  * Normalize all supported YUV 4:2:0 formats to the value used in the codec
297  * tables.
298  */
coda_format_normalize_yuv(u32 fourcc)299 static u32 coda_format_normalize_yuv(u32 fourcc)
300 {
301 	switch (fourcc) {
302 	case V4L2_PIX_FMT_NV12:
303 	case V4L2_PIX_FMT_YUV420:
304 	case V4L2_PIX_FMT_YVU420:
305 	case V4L2_PIX_FMT_YUV422P:
306 	case V4L2_PIX_FMT_YUYV:
307 		return V4L2_PIX_FMT_YUV420;
308 	default:
309 		return fourcc;
310 	}
311 }
312 
coda_find_codec(struct coda_dev * dev,int src_fourcc,int dst_fourcc)313 static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
314 						int src_fourcc, int dst_fourcc)
315 {
316 	const struct coda_codec *codecs = dev->devtype->codecs;
317 	int num_codecs = dev->devtype->num_codecs;
318 	int k;
319 
320 	src_fourcc = coda_format_normalize_yuv(src_fourcc);
321 	dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
322 	if (src_fourcc == dst_fourcc)
323 		return NULL;
324 
325 	for (k = 0; k < num_codecs; k++) {
326 		if (codecs[k].src_fourcc == src_fourcc &&
327 		    codecs[k].dst_fourcc == dst_fourcc)
328 			break;
329 	}
330 
331 	if (k == num_codecs)
332 		return NULL;
333 
334 	return &codecs[k];
335 }
336 
coda_get_max_dimensions(struct coda_dev * dev,const struct coda_codec * codec,int * max_w,int * max_h)337 static void coda_get_max_dimensions(struct coda_dev *dev,
338 				    const struct coda_codec *codec,
339 				    int *max_w, int *max_h)
340 {
341 	const struct coda_codec *codecs = dev->devtype->codecs;
342 	int num_codecs = dev->devtype->num_codecs;
343 	unsigned int w, h;
344 	int k;
345 
346 	if (codec) {
347 		w = codec->max_w;
348 		h = codec->max_h;
349 	} else {
350 		for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
351 			w = max(w, codecs[k].max_w);
352 			h = max(h, codecs[k].max_h);
353 		}
354 	}
355 
356 	if (max_w)
357 		*max_w = w;
358 	if (max_h)
359 		*max_h = h;
360 }
361 
to_coda_video_device(struct video_device * vdev)362 static const struct coda_video_device *to_coda_video_device(struct video_device
363 							    *vdev)
364 {
365 	struct coda_dev *dev = video_get_drvdata(vdev);
366 	unsigned int i = vdev - dev->vfd;
367 
368 	if (i >= dev->devtype->num_vdevs)
369 		return NULL;
370 
371 	return dev->devtype->vdevs[i];
372 }
373 
coda_product_name(int product)374 const char *coda_product_name(int product)
375 {
376 	static char buf[9];
377 
378 	switch (product) {
379 	case CODA_DX6:
380 		return "CodaDx6";
381 	case CODA_HX4:
382 		return "CodaHx4";
383 	case CODA_7541:
384 		return "CODA7541";
385 	case CODA_960:
386 		return "CODA960";
387 	default:
388 		snprintf(buf, sizeof(buf), "(0x%04x)", product);
389 		return buf;
390 	}
391 }
392 
coda_get_vdoa_data(void)393 static struct vdoa_data *coda_get_vdoa_data(void)
394 {
395 	struct device_node *vdoa_node;
396 	struct platform_device *vdoa_pdev;
397 	struct vdoa_data *vdoa_data = NULL;
398 
399 	vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa");
400 	if (!vdoa_node)
401 		return NULL;
402 
403 	vdoa_pdev = of_find_device_by_node(vdoa_node);
404 	if (!vdoa_pdev)
405 		goto out;
406 
407 	vdoa_data = platform_get_drvdata(vdoa_pdev);
408 	if (!vdoa_data)
409 		vdoa_data = ERR_PTR(-EPROBE_DEFER);
410 
411 out:
412 	of_node_put(vdoa_node);
413 
414 	return vdoa_data;
415 }
416 
417 /*
418  * V4L2 ioctl() operations.
419  */
coda_querycap(struct file * file,void * priv,struct v4l2_capability * cap)420 static int coda_querycap(struct file *file, void *priv,
421 			 struct v4l2_capability *cap)
422 {
423 	struct coda_ctx *ctx = fh_to_ctx(priv);
424 
425 	strscpy(cap->driver, CODA_NAME, sizeof(cap->driver));
426 	strscpy(cap->card, coda_product_name(ctx->dev->devtype->product),
427 		sizeof(cap->card));
428 	strscpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
429 	return 0;
430 }
431 
432 static const u32 coda_formats_420[CODA_MAX_FORMATS] = {
433 		V4L2_PIX_FMT_NV12,
434 		V4L2_PIX_FMT_YUV420,
435 		V4L2_PIX_FMT_YVU420,
436 };
437 
coda_enum_fmt(struct file * file,void * priv,struct v4l2_fmtdesc * f)438 static int coda_enum_fmt(struct file *file, void *priv,
439 			 struct v4l2_fmtdesc *f)
440 {
441 	struct video_device *vdev = video_devdata(file);
442 	const struct coda_video_device *cvd = to_coda_video_device(vdev);
443 	struct coda_ctx *ctx = fh_to_ctx(priv);
444 	const u32 *formats;
445 
446 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
447 		formats = cvd->src_formats;
448 	else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
449 		struct coda_q_data *q_data_src;
450 		struct vb2_queue *src_vq;
451 
452 		formats = cvd->dst_formats;
453 
454 		/*
455 		 * If the source format is already fixed, only allow the same
456 		 * chroma subsampling.
457 		 */
458 		q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
459 		src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
460 					 V4L2_BUF_TYPE_VIDEO_OUTPUT);
461 		if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
462 		    vb2_is_streaming(src_vq)) {
463 			if (ctx->params.jpeg_chroma_subsampling ==
464 			    V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
465 				formats = coda_formats_420;
466 			} else if (ctx->params.jpeg_chroma_subsampling ==
467 				   V4L2_JPEG_CHROMA_SUBSAMPLING_422) {
468 				f->pixelformat = V4L2_PIX_FMT_YUV422P;
469 				return f->index ? -EINVAL : 0;
470 			}
471 		}
472 	} else {
473 		return -EINVAL;
474 	}
475 
476 	if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
477 		return -EINVAL;
478 
479 	/* Skip YUYV if the vdoa is not available */
480 	if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
481 	    formats[f->index] == V4L2_PIX_FMT_YUYV)
482 		return -EINVAL;
483 
484 	f->pixelformat = formats[f->index];
485 
486 	return 0;
487 }
488 
coda_g_fmt(struct file * file,void * priv,struct v4l2_format * f)489 static int coda_g_fmt(struct file *file, void *priv,
490 		      struct v4l2_format *f)
491 {
492 	struct coda_q_data *q_data;
493 	struct coda_ctx *ctx = fh_to_ctx(priv);
494 
495 	q_data = get_q_data(ctx, f->type);
496 	if (!q_data)
497 		return -EINVAL;
498 
499 	f->fmt.pix.field	= V4L2_FIELD_NONE;
500 	f->fmt.pix.pixelformat	= q_data->fourcc;
501 	f->fmt.pix.width	= q_data->width;
502 	f->fmt.pix.height	= q_data->height;
503 	f->fmt.pix.bytesperline = q_data->bytesperline;
504 
505 	f->fmt.pix.sizeimage	= q_data->sizeimage;
506 	f->fmt.pix.colorspace	= ctx->colorspace;
507 	f->fmt.pix.xfer_func	= ctx->xfer_func;
508 	f->fmt.pix.ycbcr_enc	= ctx->ycbcr_enc;
509 	f->fmt.pix.quantization	= ctx->quantization;
510 
511 	return 0;
512 }
513 
coda_try_pixelformat(struct coda_ctx * ctx,struct v4l2_format * f)514 static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
515 {
516 	struct coda_q_data *q_data;
517 	const u32 *formats;
518 	int i;
519 
520 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
521 		formats = ctx->cvd->src_formats;
522 	else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
523 		formats = ctx->cvd->dst_formats;
524 	else
525 		return -EINVAL;
526 
527 	for (i = 0; i < CODA_MAX_FORMATS; i++) {
528 		/* Skip YUYV if the vdoa is not available */
529 		if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
530 		    formats[i] == V4L2_PIX_FMT_YUYV)
531 			continue;
532 
533 		if (formats[i] == f->fmt.pix.pixelformat) {
534 			f->fmt.pix.pixelformat = formats[i];
535 			return 0;
536 		}
537 	}
538 
539 	/* Fall back to currently set pixelformat */
540 	q_data = get_q_data(ctx, f->type);
541 	f->fmt.pix.pixelformat = q_data->fourcc;
542 
543 	return 0;
544 }
545 
coda_try_fmt_vdoa(struct coda_ctx * ctx,struct v4l2_format * f,bool * use_vdoa)546 static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f,
547 			     bool *use_vdoa)
548 {
549 	int err;
550 
551 	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
552 		return -EINVAL;
553 
554 	if (!use_vdoa)
555 		return -EINVAL;
556 
557 	if (!ctx->vdoa) {
558 		*use_vdoa = false;
559 		return 0;
560 	}
561 
562 	err = vdoa_context_configure(NULL, round_up(f->fmt.pix.width, 16),
563 				     f->fmt.pix.height, f->fmt.pix.pixelformat);
564 	if (err) {
565 		*use_vdoa = false;
566 		return 0;
567 	}
568 
569 	*use_vdoa = true;
570 	return 0;
571 }
572 
coda_estimate_sizeimage(struct coda_ctx * ctx,u32 sizeimage,u32 width,u32 height)573 static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
574 					    u32 width, u32 height)
575 {
576 	/*
577 	 * This is a rough estimate for sensible compressed buffer
578 	 * sizes (between 1 and 16 bits per pixel). This could be
579 	 * improved by better format specific worst case estimates.
580 	 */
581 	return round_up(clamp(sizeimage, width * height / 8,
582 					 width * height * 2), PAGE_SIZE);
583 }
584 
coda_try_fmt(struct coda_ctx * ctx,const struct coda_codec * codec,struct v4l2_format * f)585 static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
586 			struct v4l2_format *f)
587 {
588 	struct coda_dev *dev = ctx->dev;
589 	unsigned int max_w, max_h;
590 	enum v4l2_field field;
591 
592 	field = f->fmt.pix.field;
593 	if (field == V4L2_FIELD_ANY)
594 		field = V4L2_FIELD_NONE;
595 	else if (V4L2_FIELD_NONE != field)
596 		return -EINVAL;
597 
598 	/* V4L2 specification suggests the driver corrects the format struct
599 	 * if any of the dimensions is unsupported */
600 	f->fmt.pix.field = field;
601 
602 	coda_get_max_dimensions(dev, codec, &max_w, &max_h);
603 	v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
604 			      &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
605 			      S_ALIGN);
606 
607 	switch (f->fmt.pix.pixelformat) {
608 	case V4L2_PIX_FMT_NV12:
609 	case V4L2_PIX_FMT_YUV420:
610 	case V4L2_PIX_FMT_YVU420:
611 		/*
612 		 * Frame stride must be at least multiple of 8,
613 		 * but multiple of 16 for h.264 or JPEG 4:2:x
614 		 */
615 		f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
616 		f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
617 					f->fmt.pix.height * 3 / 2;
618 		break;
619 	case V4L2_PIX_FMT_YUYV:
620 		f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
621 		f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
622 					f->fmt.pix.height;
623 		break;
624 	case V4L2_PIX_FMT_YUV422P:
625 		f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
626 		f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
627 					f->fmt.pix.height * 2;
628 		break;
629 	case V4L2_PIX_FMT_JPEG:
630 	case V4L2_PIX_FMT_H264:
631 	case V4L2_PIX_FMT_MPEG4:
632 	case V4L2_PIX_FMT_MPEG2:
633 		f->fmt.pix.bytesperline = 0;
634 		f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
635 							f->fmt.pix.sizeimage,
636 							f->fmt.pix.width,
637 							f->fmt.pix.height);
638 		break;
639 	default:
640 		BUG();
641 	}
642 
643 	return 0;
644 }
645 
coda_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)646 static int coda_try_fmt_vid_cap(struct file *file, void *priv,
647 				struct v4l2_format *f)
648 {
649 	struct coda_ctx *ctx = fh_to_ctx(priv);
650 	const struct coda_q_data *q_data_src;
651 	const struct coda_codec *codec;
652 	struct vb2_queue *src_vq;
653 	int ret;
654 	bool use_vdoa;
655 
656 	ret = coda_try_pixelformat(ctx, f);
657 	if (ret < 0)
658 		return ret;
659 
660 	q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
661 
662 	/*
663 	 * If the source format is already fixed, only allow the same output
664 	 * resolution. When decoding JPEG images, we also have to make sure to
665 	 * use the same chroma subsampling.
666 	 */
667 	src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
668 	if (vb2_is_streaming(src_vq)) {
669 		f->fmt.pix.width = q_data_src->width;
670 		f->fmt.pix.height = q_data_src->height;
671 
672 		if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG) {
673 			if (ctx->params.jpeg_chroma_subsampling ==
674 			    V4L2_JPEG_CHROMA_SUBSAMPLING_420 &&
675 			    f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV422P)
676 				f->fmt.pix.pixelformat = V4L2_PIX_FMT_NV12;
677 			else if (ctx->params.jpeg_chroma_subsampling ==
678 				 V4L2_JPEG_CHROMA_SUBSAMPLING_422)
679 				f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
680 		}
681 	}
682 
683 	f->fmt.pix.colorspace = ctx->colorspace;
684 	f->fmt.pix.xfer_func = ctx->xfer_func;
685 	f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
686 	f->fmt.pix.quantization = ctx->quantization;
687 
688 	q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
689 	codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
690 				f->fmt.pix.pixelformat);
691 	if (!codec)
692 		return -EINVAL;
693 
694 	ret = coda_try_fmt(ctx, codec, f);
695 	if (ret < 0)
696 		return ret;
697 
698 	/* The decoders always write complete macroblocks or MCUs */
699 	if (ctx->inst_type == CODA_INST_DECODER) {
700 		f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
701 		f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
702 		if (codec->src_fourcc == V4L2_PIX_FMT_JPEG &&
703 		    f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV422P) {
704 			f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
705 					       f->fmt.pix.height * 2;
706 		} else {
707 			f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
708 					       f->fmt.pix.height * 3 / 2;
709 		}
710 
711 		ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa);
712 		if (ret < 0)
713 			return ret;
714 
715 		if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
716 			if (!use_vdoa)
717 				return -EINVAL;
718 
719 			f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
720 			f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
721 				f->fmt.pix.height;
722 		}
723 	}
724 
725 	return 0;
726 }
727 
coda_set_default_colorspace(struct v4l2_pix_format * fmt)728 static void coda_set_default_colorspace(struct v4l2_pix_format *fmt)
729 {
730 	enum v4l2_colorspace colorspace;
731 
732 	if (fmt->pixelformat == V4L2_PIX_FMT_JPEG)
733 		colorspace = V4L2_COLORSPACE_JPEG;
734 	else if (fmt->width <= 720 && fmt->height <= 576)
735 		colorspace = V4L2_COLORSPACE_SMPTE170M;
736 	else
737 		colorspace = V4L2_COLORSPACE_REC709;
738 
739 	fmt->colorspace = colorspace;
740 	fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
741 	fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
742 	fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
743 }
744 
coda_try_fmt_vid_out(struct file * file,void * priv,struct v4l2_format * f)745 static int coda_try_fmt_vid_out(struct file *file, void *priv,
746 				struct v4l2_format *f)
747 {
748 	struct coda_ctx *ctx = fh_to_ctx(priv);
749 	struct coda_dev *dev = ctx->dev;
750 	const struct coda_q_data *q_data_dst;
751 	const struct coda_codec *codec;
752 	int ret;
753 
754 	ret = coda_try_pixelformat(ctx, f);
755 	if (ret < 0)
756 		return ret;
757 
758 	if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT)
759 		coda_set_default_colorspace(&f->fmt.pix);
760 
761 	q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
762 	codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
763 
764 	return coda_try_fmt(ctx, codec, f);
765 }
766 
coda_s_fmt(struct coda_ctx * ctx,struct v4l2_format * f,struct v4l2_rect * r)767 static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
768 		      struct v4l2_rect *r)
769 {
770 	struct coda_q_data *q_data;
771 	struct vb2_queue *vq;
772 
773 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
774 	if (!vq)
775 		return -EINVAL;
776 
777 	q_data = get_q_data(ctx, f->type);
778 	if (!q_data)
779 		return -EINVAL;
780 
781 	if (vb2_is_busy(vq)) {
782 		v4l2_err(&ctx->dev->v4l2_dev, "%s: %s queue busy: %d\n",
783 			 __func__, v4l2_type_names[f->type], vq->num_buffers);
784 		return -EBUSY;
785 	}
786 
787 	q_data->fourcc = f->fmt.pix.pixelformat;
788 	q_data->width = f->fmt.pix.width;
789 	q_data->height = f->fmt.pix.height;
790 	q_data->bytesperline = f->fmt.pix.bytesperline;
791 	q_data->sizeimage = f->fmt.pix.sizeimage;
792 	if (r) {
793 		q_data->rect = *r;
794 	} else {
795 		q_data->rect.left = 0;
796 		q_data->rect.top = 0;
797 		q_data->rect.width = f->fmt.pix.width;
798 		q_data->rect.height = f->fmt.pix.height;
799 	}
800 
801 	switch (f->fmt.pix.pixelformat) {
802 	case V4L2_PIX_FMT_YUYV:
803 		ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
804 		break;
805 	case V4L2_PIX_FMT_NV12:
806 		if (!disable_tiling && ctx->use_bit &&
807 		    ctx->dev->devtype->product == CODA_960) {
808 			ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
809 			break;
810 		}
811 		fallthrough;
812 	case V4L2_PIX_FMT_YUV420:
813 	case V4L2_PIX_FMT_YVU420:
814 	case V4L2_PIX_FMT_YUV422P:
815 		ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
816 		break;
817 	default:
818 		break;
819 	}
820 
821 	if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP &&
822 	    !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) &&
823 	    ctx->use_vdoa)
824 		vdoa_context_configure(ctx->vdoa,
825 				       round_up(f->fmt.pix.width, 16),
826 				       f->fmt.pix.height,
827 				       f->fmt.pix.pixelformat);
828 	else
829 		ctx->use_vdoa = false;
830 
831 	coda_dbg(1, ctx, "Setting %s format, wxh: %dx%d, fmt: %4.4s %c\n",
832 		 v4l2_type_names[f->type], q_data->width, q_data->height,
833 		 (char *)&q_data->fourcc,
834 		 (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
835 
836 	return 0;
837 }
838 
coda_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)839 static int coda_s_fmt_vid_cap(struct file *file, void *priv,
840 			      struct v4l2_format *f)
841 {
842 	struct coda_ctx *ctx = fh_to_ctx(priv);
843 	struct coda_q_data *q_data_src;
844 	const struct coda_codec *codec;
845 	struct v4l2_rect r;
846 	int ret;
847 
848 	ret = coda_try_fmt_vid_cap(file, priv, f);
849 	if (ret)
850 		return ret;
851 
852 	q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
853 	r.left = 0;
854 	r.top = 0;
855 	r.width = q_data_src->width;
856 	r.height = q_data_src->height;
857 
858 	ret = coda_s_fmt(ctx, f, &r);
859 	if (ret)
860 		return ret;
861 
862 	if (ctx->inst_type != CODA_INST_ENCODER)
863 		return 0;
864 
865 	/* Setting the coded format determines the selected codec */
866 	codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
867 				f->fmt.pix.pixelformat);
868 	if (!codec) {
869 		v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
870 		return -EINVAL;
871 	}
872 	ctx->codec = codec;
873 
874 	ctx->colorspace = f->fmt.pix.colorspace;
875 	ctx->xfer_func = f->fmt.pix.xfer_func;
876 	ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
877 	ctx->quantization = f->fmt.pix.quantization;
878 
879 	return 0;
880 }
881 
coda_s_fmt_vid_out(struct file * file,void * priv,struct v4l2_format * f)882 static int coda_s_fmt_vid_out(struct file *file, void *priv,
883 			      struct v4l2_format *f)
884 {
885 	struct coda_ctx *ctx = fh_to_ctx(priv);
886 	const struct coda_codec *codec;
887 	struct v4l2_format f_cap;
888 	struct vb2_queue *dst_vq;
889 	int ret;
890 
891 	ret = coda_try_fmt_vid_out(file, priv, f);
892 	if (ret)
893 		return ret;
894 
895 	ret = coda_s_fmt(ctx, f, NULL);
896 	if (ret)
897 		return ret;
898 
899 	ctx->colorspace = f->fmt.pix.colorspace;
900 	ctx->xfer_func = f->fmt.pix.xfer_func;
901 	ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
902 	ctx->quantization = f->fmt.pix.quantization;
903 
904 	if (ctx->inst_type != CODA_INST_DECODER)
905 		return 0;
906 
907 	/* Setting the coded format determines the selected codec */
908 	codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
909 				V4L2_PIX_FMT_YUV420);
910 	if (!codec) {
911 		v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
912 		return -EINVAL;
913 	}
914 	ctx->codec = codec;
915 
916 	dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
917 	if (!dst_vq)
918 		return -EINVAL;
919 
920 	/*
921 	 * Setting the capture queue format is not possible while the capture
922 	 * queue is still busy. This is not an error, but the user will have to
923 	 * make sure themselves that the capture format is set correctly before
924 	 * starting the output queue again.
925 	 */
926 	if (vb2_is_busy(dst_vq))
927 		return 0;
928 
929 	memset(&f_cap, 0, sizeof(f_cap));
930 	f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
931 	coda_g_fmt(file, priv, &f_cap);
932 	f_cap.fmt.pix.width = f->fmt.pix.width;
933 	f_cap.fmt.pix.height = f->fmt.pix.height;
934 
935 	return coda_s_fmt_vid_cap(file, priv, &f_cap);
936 }
937 
coda_reqbufs(struct file * file,void * priv,struct v4l2_requestbuffers * rb)938 static int coda_reqbufs(struct file *file, void *priv,
939 			struct v4l2_requestbuffers *rb)
940 {
941 	struct coda_ctx *ctx = fh_to_ctx(priv);
942 	int ret;
943 
944 	ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
945 	if (ret)
946 		return ret;
947 
948 	/*
949 	 * Allow to allocate instance specific per-context buffers, such as
950 	 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
951 	 */
952 	if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
953 		return ctx->ops->reqbufs(ctx, rb);
954 
955 	return 0;
956 }
957 
coda_qbuf(struct file * file,void * priv,struct v4l2_buffer * buf)958 static int coda_qbuf(struct file *file, void *priv,
959 		     struct v4l2_buffer *buf)
960 {
961 	struct coda_ctx *ctx = fh_to_ctx(priv);
962 
963 	if (ctx->inst_type == CODA_INST_DECODER &&
964 	    buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
965 		buf->flags &= ~V4L2_BUF_FLAG_LAST;
966 
967 	return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
968 }
969 
coda_dqbuf(struct file * file,void * priv,struct v4l2_buffer * buf)970 static int coda_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
971 {
972 	struct coda_ctx *ctx = fh_to_ctx(priv);
973 	int ret;
974 
975 	ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
976 
977 	if (ctx->inst_type == CODA_INST_DECODER &&
978 	    buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
979 		buf->flags &= ~V4L2_BUF_FLAG_LAST;
980 
981 	return ret;
982 }
983 
coda_m2m_buf_done(struct coda_ctx * ctx,struct vb2_v4l2_buffer * buf,enum vb2_buffer_state state)984 void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
985 		       enum vb2_buffer_state state)
986 {
987 	const struct v4l2_event eos_event = {
988 		.type = V4L2_EVENT_EOS
989 	};
990 
991 	if (buf->flags & V4L2_BUF_FLAG_LAST)
992 		v4l2_event_queue_fh(&ctx->fh, &eos_event);
993 
994 	v4l2_m2m_buf_done(buf, state);
995 }
996 
coda_g_selection(struct file * file,void * fh,struct v4l2_selection * s)997 static int coda_g_selection(struct file *file, void *fh,
998 			    struct v4l2_selection *s)
999 {
1000 	struct coda_ctx *ctx = fh_to_ctx(fh);
1001 	struct coda_q_data *q_data;
1002 	struct v4l2_rect r, *rsel;
1003 
1004 	q_data = get_q_data(ctx, s->type);
1005 	if (!q_data)
1006 		return -EINVAL;
1007 
1008 	r.left = 0;
1009 	r.top = 0;
1010 	r.width = q_data->width;
1011 	r.height = q_data->height;
1012 	rsel = &q_data->rect;
1013 
1014 	switch (s->target) {
1015 	case V4L2_SEL_TGT_CROP_DEFAULT:
1016 	case V4L2_SEL_TGT_CROP_BOUNDS:
1017 		rsel = &r;
1018 		fallthrough;
1019 	case V4L2_SEL_TGT_CROP:
1020 		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
1021 		    ctx->inst_type == CODA_INST_DECODER)
1022 			return -EINVAL;
1023 		break;
1024 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1025 	case V4L2_SEL_TGT_COMPOSE_PADDED:
1026 		rsel = &r;
1027 		fallthrough;
1028 	case V4L2_SEL_TGT_COMPOSE:
1029 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1030 		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1031 		    ctx->inst_type == CODA_INST_ENCODER)
1032 			return -EINVAL;
1033 		break;
1034 	default:
1035 		return -EINVAL;
1036 	}
1037 
1038 	s->r = *rsel;
1039 
1040 	return 0;
1041 }
1042 
coda_s_selection(struct file * file,void * fh,struct v4l2_selection * s)1043 static int coda_s_selection(struct file *file, void *fh,
1044 			    struct v4l2_selection *s)
1045 {
1046 	struct coda_ctx *ctx = fh_to_ctx(fh);
1047 	struct coda_q_data *q_data;
1048 
1049 	switch (s->target) {
1050 	case V4L2_SEL_TGT_CROP:
1051 		if (ctx->inst_type == CODA_INST_ENCODER &&
1052 		    s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1053 			q_data = get_q_data(ctx, s->type);
1054 			if (!q_data)
1055 				return -EINVAL;
1056 
1057 			s->r.left = 0;
1058 			s->r.top = 0;
1059 			s->r.width = clamp(s->r.width, 2U, q_data->width);
1060 			s->r.height = clamp(s->r.height, 2U, q_data->height);
1061 
1062 			if (s->flags & V4L2_SEL_FLAG_LE) {
1063 				s->r.width = round_up(s->r.width, 2);
1064 				s->r.height = round_up(s->r.height, 2);
1065 			} else {
1066 				s->r.width = round_down(s->r.width, 2);
1067 				s->r.height = round_down(s->r.height, 2);
1068 			}
1069 
1070 			q_data->rect = s->r;
1071 
1072 			coda_dbg(1, ctx, "Setting crop rectangle: %dx%d\n",
1073 				 s->r.width, s->r.height);
1074 
1075 			return 0;
1076 		}
1077 		fallthrough;
1078 	case V4L2_SEL_TGT_NATIVE_SIZE:
1079 	case V4L2_SEL_TGT_COMPOSE:
1080 		return coda_g_selection(file, fh, s);
1081 	default:
1082 		/* v4l2-compliance expects this to fail for read-only targets */
1083 		return -EINVAL;
1084 	}
1085 }
1086 
coda_try_encoder_cmd(struct file * file,void * fh,struct v4l2_encoder_cmd * ec)1087 static int coda_try_encoder_cmd(struct file *file, void *fh,
1088 				struct v4l2_encoder_cmd *ec)
1089 {
1090 	struct coda_ctx *ctx = fh_to_ctx(fh);
1091 
1092 	if (ctx->inst_type != CODA_INST_ENCODER)
1093 		return -ENOTTY;
1094 
1095 	return v4l2_m2m_ioctl_try_encoder_cmd(file, fh, ec);
1096 }
1097 
coda_wake_up_capture_queue(struct coda_ctx * ctx)1098 static void coda_wake_up_capture_queue(struct coda_ctx *ctx)
1099 {
1100 	struct vb2_queue *dst_vq;
1101 
1102 	coda_dbg(1, ctx, "waking up capture queue\n");
1103 
1104 	dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1105 	dst_vq->last_buffer_dequeued = true;
1106 	wake_up(&dst_vq->done_wq);
1107 }
1108 
coda_encoder_cmd(struct file * file,void * fh,struct v4l2_encoder_cmd * ec)1109 static int coda_encoder_cmd(struct file *file, void *fh,
1110 			    struct v4l2_encoder_cmd *ec)
1111 {
1112 	struct coda_ctx *ctx = fh_to_ctx(fh);
1113 	struct vb2_v4l2_buffer *buf;
1114 	int ret;
1115 
1116 	ret = coda_try_encoder_cmd(file, fh, ec);
1117 	if (ret < 0)
1118 		return ret;
1119 
1120 	mutex_lock(&ctx->wakeup_mutex);
1121 	buf = v4l2_m2m_last_src_buf(ctx->fh.m2m_ctx);
1122 	if (buf) {
1123 		/*
1124 		 * If the last output buffer is still on the queue, make sure
1125 		 * that decoder finish_run will see the last flag and report it
1126 		 * to userspace.
1127 		 */
1128 		buf->flags |= V4L2_BUF_FLAG_LAST;
1129 	} else {
1130 		/* Set the stream-end flag on this context */
1131 		ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1132 
1133 		/*
1134 		 * If the last output buffer has already been taken from the
1135 		 * queue, wake up the capture queue and signal end of stream
1136 		 * via the -EPIPE mechanism.
1137 		 */
1138 		coda_wake_up_capture_queue(ctx);
1139 	}
1140 	mutex_unlock(&ctx->wakeup_mutex);
1141 
1142 	return 0;
1143 }
1144 
coda_try_decoder_cmd(struct file * file,void * fh,struct v4l2_decoder_cmd * dc)1145 static int coda_try_decoder_cmd(struct file *file, void *fh,
1146 				struct v4l2_decoder_cmd *dc)
1147 {
1148 	struct coda_ctx *ctx = fh_to_ctx(fh);
1149 
1150 	if (ctx->inst_type != CODA_INST_DECODER)
1151 		return -ENOTTY;
1152 
1153 	return v4l2_m2m_ioctl_try_decoder_cmd(file, fh, dc);
1154 }
1155 
coda_mark_last_meta(struct coda_ctx * ctx)1156 static bool coda_mark_last_meta(struct coda_ctx *ctx)
1157 {
1158 	struct coda_buffer_meta *meta;
1159 
1160 	coda_dbg(1, ctx, "marking last meta\n");
1161 
1162 	spin_lock(&ctx->buffer_meta_lock);
1163 	if (list_empty(&ctx->buffer_meta_list)) {
1164 		spin_unlock(&ctx->buffer_meta_lock);
1165 		return false;
1166 	}
1167 
1168 	meta = list_last_entry(&ctx->buffer_meta_list, struct coda_buffer_meta,
1169 			       list);
1170 	meta->last = true;
1171 
1172 	spin_unlock(&ctx->buffer_meta_lock);
1173 	return true;
1174 }
1175 
coda_mark_last_dst_buf(struct coda_ctx * ctx)1176 static bool coda_mark_last_dst_buf(struct coda_ctx *ctx)
1177 {
1178 	struct vb2_v4l2_buffer *buf;
1179 	struct vb2_buffer *dst_vb;
1180 	struct vb2_queue *dst_vq;
1181 	unsigned long flags;
1182 
1183 	coda_dbg(1, ctx, "marking last capture buffer\n");
1184 
1185 	dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1186 	spin_lock_irqsave(&dst_vq->done_lock, flags);
1187 	if (list_empty(&dst_vq->done_list)) {
1188 		spin_unlock_irqrestore(&dst_vq->done_lock, flags);
1189 		return false;
1190 	}
1191 
1192 	dst_vb = list_last_entry(&dst_vq->done_list, struct vb2_buffer,
1193 				 done_entry);
1194 	buf = to_vb2_v4l2_buffer(dst_vb);
1195 	buf->flags |= V4L2_BUF_FLAG_LAST;
1196 
1197 	spin_unlock_irqrestore(&dst_vq->done_lock, flags);
1198 	return true;
1199 }
1200 
coda_decoder_cmd(struct file * file,void * fh,struct v4l2_decoder_cmd * dc)1201 static int coda_decoder_cmd(struct file *file, void *fh,
1202 			    struct v4l2_decoder_cmd *dc)
1203 {
1204 	struct coda_ctx *ctx = fh_to_ctx(fh);
1205 	struct coda_dev *dev = ctx->dev;
1206 	struct vb2_v4l2_buffer *buf;
1207 	struct vb2_queue *dst_vq;
1208 	bool stream_end;
1209 	bool wakeup;
1210 	int ret;
1211 
1212 	ret = coda_try_decoder_cmd(file, fh, dc);
1213 	if (ret < 0)
1214 		return ret;
1215 
1216 	switch (dc->cmd) {
1217 	case V4L2_DEC_CMD_START:
1218 		mutex_lock(&dev->coda_mutex);
1219 		mutex_lock(&ctx->bitstream_mutex);
1220 		coda_bitstream_flush(ctx);
1221 		dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
1222 					 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1223 		vb2_clear_last_buffer_dequeued(dst_vq);
1224 		ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
1225 		coda_fill_bitstream(ctx, NULL);
1226 		mutex_unlock(&ctx->bitstream_mutex);
1227 		mutex_unlock(&dev->coda_mutex);
1228 		break;
1229 	case V4L2_DEC_CMD_STOP:
1230 		stream_end = false;
1231 		wakeup = false;
1232 
1233 		mutex_lock(&ctx->wakeup_mutex);
1234 
1235 		buf = v4l2_m2m_last_src_buf(ctx->fh.m2m_ctx);
1236 		if (buf) {
1237 			coda_dbg(1, ctx, "marking last pending buffer\n");
1238 
1239 			/* Mark last buffer */
1240 			buf->flags |= V4L2_BUF_FLAG_LAST;
1241 
1242 			if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) == 0) {
1243 				coda_dbg(1, ctx, "all remaining buffers queued\n");
1244 				stream_end = true;
1245 			}
1246 		} else {
1247 			if (ctx->use_bit)
1248 				if (coda_mark_last_meta(ctx))
1249 					stream_end = true;
1250 				else
1251 					wakeup = true;
1252 			else
1253 				if (!coda_mark_last_dst_buf(ctx))
1254 					wakeup = true;
1255 		}
1256 
1257 		if (stream_end) {
1258 			coda_dbg(1, ctx, "all remaining buffers queued\n");
1259 
1260 			/* Set the stream-end flag on this context */
1261 			coda_bit_stream_end_flag(ctx);
1262 			ctx->hold = false;
1263 			v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
1264 		}
1265 
1266 		if (wakeup) {
1267 			/* If there is no buffer in flight, wake up */
1268 			coda_wake_up_capture_queue(ctx);
1269 		}
1270 
1271 		mutex_unlock(&ctx->wakeup_mutex);
1272 		break;
1273 	default:
1274 		return -EINVAL;
1275 	}
1276 
1277 	return 0;
1278 }
1279 
coda_enum_framesizes(struct file * file,void * fh,struct v4l2_frmsizeenum * fsize)1280 static int coda_enum_framesizes(struct file *file, void *fh,
1281 				struct v4l2_frmsizeenum *fsize)
1282 {
1283 	struct coda_ctx *ctx = fh_to_ctx(fh);
1284 	struct coda_q_data *q_data_dst;
1285 	const struct coda_codec *codec;
1286 
1287 	if (ctx->inst_type != CODA_INST_ENCODER)
1288 		return -ENOTTY;
1289 
1290 	if (fsize->index)
1291 		return -EINVAL;
1292 
1293 	if (coda_format_normalize_yuv(fsize->pixel_format) ==
1294 	    V4L2_PIX_FMT_YUV420) {
1295 		q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1296 		codec = coda_find_codec(ctx->dev, fsize->pixel_format,
1297 					q_data_dst->fourcc);
1298 	} else {
1299 		codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
1300 					fsize->pixel_format);
1301 	}
1302 	if (!codec)
1303 		return -EINVAL;
1304 
1305 	fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1306 	fsize->stepwise.min_width = MIN_W;
1307 	fsize->stepwise.max_width = codec->max_w;
1308 	fsize->stepwise.step_width = 1;
1309 	fsize->stepwise.min_height = MIN_H;
1310 	fsize->stepwise.max_height = codec->max_h;
1311 	fsize->stepwise.step_height = 1;
1312 
1313 	return 0;
1314 }
1315 
coda_enum_frameintervals(struct file * file,void * fh,struct v4l2_frmivalenum * f)1316 static int coda_enum_frameintervals(struct file *file, void *fh,
1317 				    struct v4l2_frmivalenum *f)
1318 {
1319 	struct coda_ctx *ctx = fh_to_ctx(fh);
1320 	int i;
1321 
1322 	if (f->index)
1323 		return -EINVAL;
1324 
1325 	/* Disallow YUYV if the vdoa is not available */
1326 	if (!ctx->vdoa && f->pixel_format == V4L2_PIX_FMT_YUYV)
1327 		return -EINVAL;
1328 
1329 	for (i = 0; i < CODA_MAX_FORMATS; i++) {
1330 		if (f->pixel_format == ctx->cvd->src_formats[i] ||
1331 		    f->pixel_format == ctx->cvd->dst_formats[i])
1332 			break;
1333 	}
1334 	if (i == CODA_MAX_FORMATS)
1335 		return -EINVAL;
1336 
1337 	f->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1338 	f->stepwise.min.numerator = 1;
1339 	f->stepwise.min.denominator = 65535;
1340 	f->stepwise.max.numerator = 65536;
1341 	f->stepwise.max.denominator = 1;
1342 	f->stepwise.step.numerator = 1;
1343 	f->stepwise.step.denominator = 1;
1344 
1345 	return 0;
1346 }
1347 
coda_g_parm(struct file * file,void * fh,struct v4l2_streamparm * a)1348 static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1349 {
1350 	struct coda_ctx *ctx = fh_to_ctx(fh);
1351 	struct v4l2_fract *tpf;
1352 
1353 	if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1354 		return -EINVAL;
1355 
1356 	a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1357 	tpf = &a->parm.output.timeperframe;
1358 	tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
1359 	tpf->numerator = 1 + (ctx->params.framerate >>
1360 			      CODA_FRATE_DIV_OFFSET);
1361 
1362 	return 0;
1363 }
1364 
1365 /*
1366  * Approximate timeperframe v4l2_fract with values that can be written
1367  * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
1368  */
coda_approximate_timeperframe(struct v4l2_fract * timeperframe)1369 static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
1370 {
1371 	struct v4l2_fract s = *timeperframe;
1372 	struct v4l2_fract f0;
1373 	struct v4l2_fract f1 = { 1, 0 };
1374 	struct v4l2_fract f2 = { 0, 1 };
1375 	unsigned int i, div, s_denominator;
1376 
1377 	/* Lower bound is 1/65535 */
1378 	if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
1379 		timeperframe->numerator = 1;
1380 		timeperframe->denominator = 65535;
1381 		return;
1382 	}
1383 
1384 	/* Upper bound is 65536/1 */
1385 	if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
1386 		timeperframe->numerator = 65536;
1387 		timeperframe->denominator = 1;
1388 		return;
1389 	}
1390 
1391 	/* Reduce fraction to lowest terms */
1392 	div = gcd(s.numerator, s.denominator);
1393 	if (div > 1) {
1394 		s.numerator /= div;
1395 		s.denominator /= div;
1396 	}
1397 
1398 	if (s.numerator <= 65536 && s.denominator < 65536) {
1399 		*timeperframe = s;
1400 		return;
1401 	}
1402 
1403 	/* Find successive convergents from continued fraction expansion */
1404 	while (f2.numerator <= 65536 && f2.denominator < 65536) {
1405 		f0 = f1;
1406 		f1 = f2;
1407 
1408 		/* Stop when f2 exactly equals timeperframe */
1409 		if (s.numerator == 0)
1410 			break;
1411 
1412 		i = s.denominator / s.numerator;
1413 
1414 		f2.numerator = f0.numerator + i * f1.numerator;
1415 		f2.denominator = f0.denominator + i * f2.denominator;
1416 
1417 		s_denominator = s.numerator;
1418 		s.numerator = s.denominator % s.numerator;
1419 		s.denominator = s_denominator;
1420 	}
1421 
1422 	*timeperframe = f1;
1423 }
1424 
coda_timeperframe_to_frate(struct v4l2_fract * timeperframe)1425 static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
1426 {
1427 	return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
1428 		timeperframe->denominator;
1429 }
1430 
coda_s_parm(struct file * file,void * fh,struct v4l2_streamparm * a)1431 static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1432 {
1433 	struct coda_ctx *ctx = fh_to_ctx(fh);
1434 	struct v4l2_fract *tpf;
1435 
1436 	if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1437 		return -EINVAL;
1438 
1439 	a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1440 	tpf = &a->parm.output.timeperframe;
1441 	coda_approximate_timeperframe(tpf);
1442 	ctx->params.framerate = coda_timeperframe_to_frate(tpf);
1443 	ctx->params.framerate_changed = true;
1444 
1445 	return 0;
1446 }
1447 
coda_subscribe_event(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)1448 static int coda_subscribe_event(struct v4l2_fh *fh,
1449 				const struct v4l2_event_subscription *sub)
1450 {
1451 	struct coda_ctx *ctx = fh_to_ctx(fh);
1452 
1453 	switch (sub->type) {
1454 	case V4L2_EVENT_EOS:
1455 		return v4l2_event_subscribe(fh, sub, 0, NULL);
1456 	case V4L2_EVENT_SOURCE_CHANGE:
1457 		if (ctx->inst_type == CODA_INST_DECODER)
1458 			return v4l2_event_subscribe(fh, sub, 0, NULL);
1459 		else
1460 			return -EINVAL;
1461 	default:
1462 		return v4l2_ctrl_subscribe_event(fh, sub);
1463 	}
1464 }
1465 
1466 static const struct v4l2_ioctl_ops coda_ioctl_ops = {
1467 	.vidioc_querycap	= coda_querycap,
1468 
1469 	.vidioc_enum_fmt_vid_cap = coda_enum_fmt,
1470 	.vidioc_g_fmt_vid_cap	= coda_g_fmt,
1471 	.vidioc_try_fmt_vid_cap	= coda_try_fmt_vid_cap,
1472 	.vidioc_s_fmt_vid_cap	= coda_s_fmt_vid_cap,
1473 
1474 	.vidioc_enum_fmt_vid_out = coda_enum_fmt,
1475 	.vidioc_g_fmt_vid_out	= coda_g_fmt,
1476 	.vidioc_try_fmt_vid_out	= coda_try_fmt_vid_out,
1477 	.vidioc_s_fmt_vid_out	= coda_s_fmt_vid_out,
1478 
1479 	.vidioc_reqbufs		= coda_reqbufs,
1480 	.vidioc_querybuf	= v4l2_m2m_ioctl_querybuf,
1481 
1482 	.vidioc_qbuf		= coda_qbuf,
1483 	.vidioc_expbuf		= v4l2_m2m_ioctl_expbuf,
1484 	.vidioc_dqbuf		= coda_dqbuf,
1485 	.vidioc_create_bufs	= v4l2_m2m_ioctl_create_bufs,
1486 	.vidioc_prepare_buf	= v4l2_m2m_ioctl_prepare_buf,
1487 
1488 	.vidioc_streamon	= v4l2_m2m_ioctl_streamon,
1489 	.vidioc_streamoff	= v4l2_m2m_ioctl_streamoff,
1490 
1491 	.vidioc_g_selection	= coda_g_selection,
1492 	.vidioc_s_selection	= coda_s_selection,
1493 
1494 	.vidioc_try_encoder_cmd	= coda_try_encoder_cmd,
1495 	.vidioc_encoder_cmd	= coda_encoder_cmd,
1496 	.vidioc_try_decoder_cmd	= coda_try_decoder_cmd,
1497 	.vidioc_decoder_cmd	= coda_decoder_cmd,
1498 
1499 	.vidioc_g_parm		= coda_g_parm,
1500 	.vidioc_s_parm		= coda_s_parm,
1501 
1502 	.vidioc_enum_framesizes	= coda_enum_framesizes,
1503 	.vidioc_enum_frameintervals = coda_enum_frameintervals,
1504 
1505 	.vidioc_subscribe_event = coda_subscribe_event,
1506 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1507 };
1508 
1509 /*
1510  * Mem-to-mem operations.
1511  */
1512 
coda_device_run(void * m2m_priv)1513 static void coda_device_run(void *m2m_priv)
1514 {
1515 	struct coda_ctx *ctx = m2m_priv;
1516 	struct coda_dev *dev = ctx->dev;
1517 
1518 	queue_work(dev->workqueue, &ctx->pic_run_work);
1519 }
1520 
coda_pic_run_work(struct work_struct * work)1521 static void coda_pic_run_work(struct work_struct *work)
1522 {
1523 	struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1524 	struct coda_dev *dev = ctx->dev;
1525 	int ret;
1526 
1527 	mutex_lock(&ctx->buffer_mutex);
1528 	mutex_lock(&dev->coda_mutex);
1529 
1530 	ret = ctx->ops->prepare_run(ctx);
1531 	if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
1532 		mutex_unlock(&dev->coda_mutex);
1533 		mutex_unlock(&ctx->buffer_mutex);
1534 		/* job_finish scheduled by prepare_decode */
1535 		return;
1536 	}
1537 
1538 	if (!wait_for_completion_timeout(&ctx->completion,
1539 					 msecs_to_jiffies(1000))) {
1540 		if (ctx->use_bit) {
1541 			dev_err(dev->dev, "CODA PIC_RUN timeout\n");
1542 
1543 			ctx->hold = true;
1544 
1545 			coda_hw_reset(ctx);
1546 		}
1547 
1548 		if (ctx->ops->run_timeout)
1549 			ctx->ops->run_timeout(ctx);
1550 	} else {
1551 		ctx->ops->finish_run(ctx);
1552 	}
1553 
1554 	if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
1555 	    ctx->ops->seq_end_work)
1556 		queue_work(dev->workqueue, &ctx->seq_end_work);
1557 
1558 	mutex_unlock(&dev->coda_mutex);
1559 	mutex_unlock(&ctx->buffer_mutex);
1560 
1561 	v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1562 }
1563 
coda_job_ready(void * m2m_priv)1564 static int coda_job_ready(void *m2m_priv)
1565 {
1566 	struct coda_ctx *ctx = m2m_priv;
1567 	int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
1568 
1569 	/*
1570 	 * For both 'P' and 'key' frame cases 1 picture
1571 	 * and 1 frame are needed. In the decoder case,
1572 	 * the compressed frame can be in the bitstream.
1573 	 */
1574 	if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
1575 		coda_dbg(1, ctx, "not ready: not enough vid-out buffers.\n");
1576 		return 0;
1577 	}
1578 
1579 	if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
1580 		coda_dbg(1, ctx, "not ready: not enough vid-cap buffers.\n");
1581 		return 0;
1582 	}
1583 
1584 	if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1585 		bool stream_end = ctx->bit_stream_param &
1586 				  CODA_BIT_STREAM_END_FLAG;
1587 		int num_metas = ctx->num_metas;
1588 		struct coda_buffer_meta *meta;
1589 		unsigned int count;
1590 
1591 		count = hweight32(ctx->frm_dis_flg);
1592 		if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) {
1593 			coda_dbg(1, ctx,
1594 				 "not ready: all internal buffers in use: %d/%d (0x%x)",
1595 				 count, ctx->num_internal_frames,
1596 				 ctx->frm_dis_flg);
1597 			return 0;
1598 		}
1599 
1600 		if (ctx->hold && !src_bufs) {
1601 			coda_dbg(1, ctx,
1602 				 "not ready: on hold for more buffers.\n");
1603 			return 0;
1604 		}
1605 
1606 		if (!stream_end && (num_metas + src_bufs) < 2) {
1607 			coda_dbg(1, ctx,
1608 				 "not ready: need 2 buffers available (queue:%d + bitstream:%d)\n",
1609 				 num_metas, src_bufs);
1610 			return 0;
1611 		}
1612 
1613 		meta = list_first_entry(&ctx->buffer_meta_list,
1614 					struct coda_buffer_meta, list);
1615 		if (!coda_bitstream_can_fetch_past(ctx, meta->end) &&
1616 		    !stream_end) {
1617 			coda_dbg(1, ctx,
1618 				 "not ready: not enough bitstream data to read past %u (%u)\n",
1619 				 meta->end, ctx->bitstream_fifo.kfifo.in);
1620 			return 0;
1621 		}
1622 	}
1623 
1624 	if (ctx->aborting) {
1625 		coda_dbg(1, ctx, "not ready: aborting\n");
1626 		return 0;
1627 	}
1628 
1629 	coda_dbg(2, ctx, "job ready\n");
1630 
1631 	return 1;
1632 }
1633 
coda_job_abort(void * priv)1634 static void coda_job_abort(void *priv)
1635 {
1636 	struct coda_ctx *ctx = priv;
1637 
1638 	ctx->aborting = 1;
1639 
1640 	coda_dbg(1, ctx, "job abort\n");
1641 }
1642 
1643 static const struct v4l2_m2m_ops coda_m2m_ops = {
1644 	.device_run	= coda_device_run,
1645 	.job_ready	= coda_job_ready,
1646 	.job_abort	= coda_job_abort,
1647 };
1648 
set_default_params(struct coda_ctx * ctx)1649 static void set_default_params(struct coda_ctx *ctx)
1650 {
1651 	unsigned int max_w, max_h, usize, csize;
1652 
1653 	ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1654 				     ctx->cvd->dst_formats[0]);
1655 	max_w = min(ctx->codec->max_w, 1920U);
1656 	max_h = min(ctx->codec->max_h, 1088U);
1657 	usize = max_w * max_h * 3 / 2;
1658 	csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
1659 
1660 	ctx->params.codec_mode = ctx->codec->mode;
1661 	if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_JPEG)
1662 		ctx->colorspace = V4L2_COLORSPACE_JPEG;
1663 	else
1664 		ctx->colorspace = V4L2_COLORSPACE_REC709;
1665 	ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1666 	ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1667 	ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1668 	ctx->params.framerate = 30;
1669 
1670 	/* Default formats for output and input queues */
1671 	ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1672 	ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
1673 	ctx->q_data[V4L2_M2M_SRC].width = max_w;
1674 	ctx->q_data[V4L2_M2M_SRC].height = max_h;
1675 	ctx->q_data[V4L2_M2M_DST].width = max_w;
1676 	ctx->q_data[V4L2_M2M_DST].height = max_h;
1677 	if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1678 		ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1679 		ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
1680 		ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1681 		ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
1682 	} else {
1683 		ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1684 		ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
1685 		ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1686 		ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
1687 	}
1688 	ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1689 	ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1690 	ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1691 	ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
1692 
1693 	/*
1694 	 * Since the RBC2AXI logic only supports a single chroma plane,
1695 	 * macroblock tiling only works for to NV12 pixel format.
1696 	 */
1697 	ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
1698 }
1699 
1700 /*
1701  * Queue operations
1702  */
coda_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])1703 static int coda_queue_setup(struct vb2_queue *vq,
1704 				unsigned int *nbuffers, unsigned int *nplanes,
1705 				unsigned int sizes[], struct device *alloc_devs[])
1706 {
1707 	struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1708 	struct coda_q_data *q_data;
1709 	unsigned int size;
1710 
1711 	q_data = get_q_data(ctx, vq->type);
1712 	size = q_data->sizeimage;
1713 
1714 	if (*nplanes)
1715 		return sizes[0] < size ? -EINVAL : 0;
1716 
1717 	*nplanes = 1;
1718 	sizes[0] = size;
1719 
1720 	coda_dbg(1, ctx, "get %d buffer(s) of size %d each.\n", *nbuffers,
1721 		 size);
1722 
1723 	return 0;
1724 }
1725 
coda_buf_prepare(struct vb2_buffer * vb)1726 static int coda_buf_prepare(struct vb2_buffer *vb)
1727 {
1728 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1729 	struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1730 	struct coda_q_data *q_data;
1731 
1732 	q_data = get_q_data(ctx, vb->vb2_queue->type);
1733 	if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
1734 		if (vbuf->field == V4L2_FIELD_ANY)
1735 			vbuf->field = V4L2_FIELD_NONE;
1736 		if (vbuf->field != V4L2_FIELD_NONE) {
1737 			v4l2_warn(&ctx->dev->v4l2_dev,
1738 				  "%s field isn't supported\n", __func__);
1739 			return -EINVAL;
1740 		}
1741 	}
1742 
1743 	if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1744 		v4l2_warn(&ctx->dev->v4l2_dev,
1745 			  "%s data will not fit into plane (%lu < %lu)\n",
1746 			  __func__, vb2_plane_size(vb, 0),
1747 			  (long)q_data->sizeimage);
1748 		return -EINVAL;
1749 	}
1750 
1751 	return 0;
1752 }
1753 
coda_update_menu_ctrl(struct v4l2_ctrl * ctrl,int value)1754 static void coda_update_menu_ctrl(struct v4l2_ctrl *ctrl, int value)
1755 {
1756 	if (!ctrl)
1757 		return;
1758 
1759 	v4l2_ctrl_lock(ctrl);
1760 
1761 	/*
1762 	 * Extend the control range if the parsed stream contains a known but
1763 	 * unsupported value or level.
1764 	 */
1765 	if (value > ctrl->maximum) {
1766 		__v4l2_ctrl_modify_range(ctrl, ctrl->minimum, value,
1767 			ctrl->menu_skip_mask & ~(1 << value),
1768 			ctrl->default_value);
1769 	} else if (value < ctrl->minimum) {
1770 		__v4l2_ctrl_modify_range(ctrl, value, ctrl->maximum,
1771 			ctrl->menu_skip_mask & ~(1 << value),
1772 			ctrl->default_value);
1773 	}
1774 
1775 	__v4l2_ctrl_s_ctrl(ctrl, value);
1776 
1777 	v4l2_ctrl_unlock(ctrl);
1778 }
1779 
coda_update_profile_level_ctrls(struct coda_ctx * ctx,u8 profile_idc,u8 level_idc)1780 void coda_update_profile_level_ctrls(struct coda_ctx *ctx, u8 profile_idc,
1781 				     u8 level_idc)
1782 {
1783 	const char * const *profile_names;
1784 	const char * const *level_names;
1785 	struct v4l2_ctrl *profile_ctrl;
1786 	struct v4l2_ctrl *level_ctrl;
1787 	const char *codec_name;
1788 	u32 profile_cid;
1789 	u32 level_cid;
1790 	int profile;
1791 	int level;
1792 
1793 	switch (ctx->codec->src_fourcc) {
1794 	case V4L2_PIX_FMT_H264:
1795 		codec_name = "H264";
1796 		profile_cid = V4L2_CID_MPEG_VIDEO_H264_PROFILE;
1797 		level_cid = V4L2_CID_MPEG_VIDEO_H264_LEVEL;
1798 		profile_ctrl = ctx->h264_profile_ctrl;
1799 		level_ctrl = ctx->h264_level_ctrl;
1800 		profile = coda_h264_profile(profile_idc);
1801 		level = coda_h264_level(level_idc);
1802 		break;
1803 	case V4L2_PIX_FMT_MPEG2:
1804 		codec_name = "MPEG-2";
1805 		profile_cid = V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE;
1806 		level_cid = V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL;
1807 		profile_ctrl = ctx->mpeg2_profile_ctrl;
1808 		level_ctrl = ctx->mpeg2_level_ctrl;
1809 		profile = coda_mpeg2_profile(profile_idc);
1810 		level = coda_mpeg2_level(level_idc);
1811 		break;
1812 	case V4L2_PIX_FMT_MPEG4:
1813 		codec_name = "MPEG-4";
1814 		profile_cid = V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE;
1815 		level_cid = V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL;
1816 		profile_ctrl = ctx->mpeg4_profile_ctrl;
1817 		level_ctrl = ctx->mpeg4_level_ctrl;
1818 		profile = coda_mpeg4_profile(profile_idc);
1819 		level = coda_mpeg4_level(level_idc);
1820 		break;
1821 	default:
1822 		return;
1823 	}
1824 
1825 	profile_names = v4l2_ctrl_get_menu(profile_cid);
1826 	level_names = v4l2_ctrl_get_menu(level_cid);
1827 
1828 	if (profile < 0) {
1829 		v4l2_warn(&ctx->dev->v4l2_dev, "Invalid %s profile: %u\n",
1830 			  codec_name, profile_idc);
1831 	} else {
1832 		coda_dbg(1, ctx, "Parsed %s profile: %s\n", codec_name,
1833 			 profile_names[profile]);
1834 		coda_update_menu_ctrl(profile_ctrl, profile);
1835 	}
1836 
1837 	if (level < 0) {
1838 		v4l2_warn(&ctx->dev->v4l2_dev, "Invalid %s level: %u\n",
1839 			  codec_name, level_idc);
1840 	} else {
1841 		coda_dbg(1, ctx, "Parsed %s level: %s\n", codec_name,
1842 			 level_names[level]);
1843 		coda_update_menu_ctrl(level_ctrl, level);
1844 	}
1845 }
1846 
coda_queue_source_change_event(struct coda_ctx * ctx)1847 static void coda_queue_source_change_event(struct coda_ctx *ctx)
1848 {
1849 	static const struct v4l2_event source_change_event = {
1850 		.type = V4L2_EVENT_SOURCE_CHANGE,
1851 		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
1852 	};
1853 
1854 	v4l2_event_queue_fh(&ctx->fh, &source_change_event);
1855 }
1856 
coda_buf_queue(struct vb2_buffer * vb)1857 static void coda_buf_queue(struct vb2_buffer *vb)
1858 {
1859 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1860 	struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1861 	struct vb2_queue *vq = vb->vb2_queue;
1862 	struct coda_q_data *q_data;
1863 
1864 	q_data = get_q_data(ctx, vb->vb2_queue->type);
1865 
1866 	/*
1867 	 * In the decoder case, immediately try to copy the buffer into the
1868 	 * bitstream ringbuffer and mark it as ready to be dequeued.
1869 	 */
1870 	if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1871 		/*
1872 		 * For backwards compatibility, queuing an empty buffer marks
1873 		 * the stream end
1874 		 */
1875 		if (vb2_get_plane_payload(vb, 0) == 0)
1876 			coda_bit_stream_end_flag(ctx);
1877 
1878 		if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1879 			/*
1880 			 * Unless already done, try to obtain profile_idc and
1881 			 * level_idc from the SPS header. This allows to decide
1882 			 * whether to enable reordering during sequence
1883 			 * initialization.
1884 			 */
1885 			if (!ctx->params.h264_profile_idc) {
1886 				coda_sps_parse_profile(ctx, vb);
1887 				coda_update_profile_level_ctrls(ctx,
1888 						ctx->params.h264_profile_idc,
1889 						ctx->params.h264_level_idc);
1890 			}
1891 		}
1892 
1893 		mutex_lock(&ctx->bitstream_mutex);
1894 		v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1895 		if (vb2_is_streaming(vb->vb2_queue))
1896 			/* This set buf->sequence = ctx->qsequence++ */
1897 			coda_fill_bitstream(ctx, NULL);
1898 		mutex_unlock(&ctx->bitstream_mutex);
1899 
1900 		if (!ctx->initialized) {
1901 			/*
1902 			 * Run sequence initialization in case the queued
1903 			 * buffer contained headers.
1904 			 */
1905 			if (vb2_is_streaming(vb->vb2_queue) &&
1906 			    ctx->ops->seq_init_work) {
1907 				queue_work(ctx->dev->workqueue,
1908 					   &ctx->seq_init_work);
1909 				flush_work(&ctx->seq_init_work);
1910 			}
1911 
1912 			if (ctx->initialized)
1913 				coda_queue_source_change_event(ctx);
1914 		}
1915 	} else {
1916 		if ((ctx->inst_type == CODA_INST_ENCODER || !ctx->use_bit) &&
1917 		    vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1918 			vbuf->sequence = ctx->qsequence++;
1919 		v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1920 	}
1921 }
1922 
coda_alloc_aux_buf(struct coda_dev * dev,struct coda_aux_buf * buf,size_t size,const char * name,struct dentry * parent)1923 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1924 		       size_t size, const char *name, struct dentry *parent)
1925 {
1926 	buf->vaddr = dma_alloc_coherent(dev->dev, size, &buf->paddr,
1927 					GFP_KERNEL);
1928 	if (!buf->vaddr) {
1929 		v4l2_err(&dev->v4l2_dev,
1930 			 "Failed to allocate %s buffer of size %zu\n",
1931 			 name, size);
1932 		return -ENOMEM;
1933 	}
1934 
1935 	buf->size = size;
1936 
1937 	if (name && parent) {
1938 		buf->blob.data = buf->vaddr;
1939 		buf->blob.size = size;
1940 		buf->dentry = debugfs_create_blob(name, 0644, parent,
1941 						  &buf->blob);
1942 	}
1943 
1944 	return 0;
1945 }
1946 
coda_free_aux_buf(struct coda_dev * dev,struct coda_aux_buf * buf)1947 void coda_free_aux_buf(struct coda_dev *dev,
1948 		       struct coda_aux_buf *buf)
1949 {
1950 	if (buf->vaddr) {
1951 		dma_free_coherent(dev->dev, buf->size, buf->vaddr, buf->paddr);
1952 		buf->vaddr = NULL;
1953 		buf->size = 0;
1954 		debugfs_remove(buf->dentry);
1955 		buf->dentry = NULL;
1956 	}
1957 }
1958 
coda_start_streaming(struct vb2_queue * q,unsigned int count)1959 static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1960 {
1961 	struct coda_ctx *ctx = vb2_get_drv_priv(q);
1962 	struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1963 	struct coda_q_data *q_data_src, *q_data_dst;
1964 	struct v4l2_m2m_buffer *m2m_buf, *tmp;
1965 	struct vb2_v4l2_buffer *buf;
1966 	struct list_head list;
1967 	int ret = 0;
1968 
1969 	if (count < 1)
1970 		return -EINVAL;
1971 
1972 	coda_dbg(1, ctx, "start streaming %s\n", v4l2_type_names[q->type]);
1973 
1974 	INIT_LIST_HEAD(&list);
1975 
1976 	q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1977 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1978 		if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1979 			/* copy the buffers that were queued before streamon */
1980 			mutex_lock(&ctx->bitstream_mutex);
1981 			coda_fill_bitstream(ctx, &list);
1982 			mutex_unlock(&ctx->bitstream_mutex);
1983 
1984 			if (ctx->dev->devtype->product != CODA_960 &&
1985 			    coda_get_bitstream_payload(ctx) < 512) {
1986 				v4l2_err(v4l2_dev, "start payload < 512\n");
1987 				ret = -EINVAL;
1988 				goto err;
1989 			}
1990 
1991 			if (!ctx->initialized) {
1992 				/* Run sequence initialization */
1993 				if (ctx->ops->seq_init_work) {
1994 					queue_work(ctx->dev->workqueue,
1995 						   &ctx->seq_init_work);
1996 					flush_work(&ctx->seq_init_work);
1997 				}
1998 			}
1999 		}
2000 
2001 		/*
2002 		 * Check the first input JPEG buffer to determine chroma
2003 		 * subsampling.
2004 		 */
2005 		if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG) {
2006 			buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
2007 			ret = coda_jpeg_decode_header(ctx, &buf->vb2_buf);
2008 			if (ret < 0) {
2009 				v4l2_err(v4l2_dev,
2010 					 "failed to decode JPEG header: %d\n",
2011 					 ret);
2012 				goto err;
2013 			}
2014 
2015 			q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2016 			q_data_dst->width = round_up(q_data_src->width, 16);
2017 			q_data_dst->height = round_up(q_data_src->height, 16);
2018 			q_data_dst->bytesperline = q_data_dst->width;
2019 			if (ctx->params.jpeg_chroma_subsampling ==
2020 			    V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
2021 				q_data_dst->sizeimage =
2022 						q_data_dst->bytesperline *
2023 						q_data_dst->height * 3 / 2;
2024 				if (q_data_dst->fourcc != V4L2_PIX_FMT_YUV420)
2025 					q_data_dst->fourcc = V4L2_PIX_FMT_NV12;
2026 			} else {
2027 				q_data_dst->sizeimage =
2028 						q_data_dst->bytesperline *
2029 						q_data_dst->height * 2;
2030 				q_data_dst->fourcc = V4L2_PIX_FMT_YUV422P;
2031 			}
2032 			q_data_dst->rect.left = 0;
2033 			q_data_dst->rect.top = 0;
2034 			q_data_dst->rect.width = q_data_src->width;
2035 			q_data_dst->rect.height = q_data_src->height;
2036 		}
2037 		ctx->streamon_out = 1;
2038 	} else {
2039 		ctx->streamon_cap = 1;
2040 	}
2041 
2042 	/* Don't start the coda unless both queues are on */
2043 	if (!(ctx->streamon_out && ctx->streamon_cap))
2044 		goto out;
2045 
2046 	q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2047 	if ((q_data_src->rect.width != q_data_dst->width &&
2048 	     round_up(q_data_src->rect.width, 16) != q_data_dst->width) ||
2049 	    (q_data_src->rect.height != q_data_dst->height &&
2050 	     round_up(q_data_src->rect.height, 16) != q_data_dst->height)) {
2051 		v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
2052 			 q_data_src->rect.width, q_data_src->rect.height,
2053 			 q_data_dst->width, q_data_dst->height);
2054 		ret = -EINVAL;
2055 		goto err;
2056 	}
2057 
2058 	/* Allow BIT decoder device_run with no new buffers queued */
2059 	if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
2060 		v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
2061 
2062 	ctx->gopcounter = ctx->params.gop_size - 1;
2063 
2064 	if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
2065 		ctx->params.gop_size = 1;
2066 	ctx->gopcounter = ctx->params.gop_size - 1;
2067 
2068 	ret = ctx->ops->start_streaming(ctx);
2069 	if (ctx->inst_type == CODA_INST_DECODER) {
2070 		if (ret == -EAGAIN)
2071 			goto out;
2072 	}
2073 	if (ret < 0)
2074 		goto err;
2075 
2076 out:
2077 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2078 		list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
2079 			list_del(&m2m_buf->list);
2080 			v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_DONE);
2081 		}
2082 	}
2083 	return 0;
2084 
2085 err:
2086 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2087 		list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
2088 			list_del(&m2m_buf->list);
2089 			v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_QUEUED);
2090 		}
2091 		while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
2092 			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
2093 	} else {
2094 		while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
2095 			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
2096 	}
2097 	return ret;
2098 }
2099 
coda_stop_streaming(struct vb2_queue * q)2100 static void coda_stop_streaming(struct vb2_queue *q)
2101 {
2102 	struct coda_ctx *ctx = vb2_get_drv_priv(q);
2103 	struct coda_dev *dev = ctx->dev;
2104 	struct vb2_v4l2_buffer *buf;
2105 	bool stop;
2106 
2107 	stop = ctx->streamon_out && ctx->streamon_cap;
2108 
2109 	coda_dbg(1, ctx, "stop streaming %s\n", v4l2_type_names[q->type]);
2110 
2111 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2112 		ctx->streamon_out = 0;
2113 
2114 		coda_bit_stream_end_flag(ctx);
2115 
2116 		ctx->qsequence = 0;
2117 
2118 		while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
2119 			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
2120 	} else {
2121 		ctx->streamon_cap = 0;
2122 
2123 		ctx->osequence = 0;
2124 		ctx->sequence_offset = 0;
2125 
2126 		while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
2127 			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
2128 	}
2129 
2130 	if (stop) {
2131 		struct coda_buffer_meta *meta;
2132 
2133 		if (ctx->ops->seq_end_work) {
2134 			queue_work(dev->workqueue, &ctx->seq_end_work);
2135 			flush_work(&ctx->seq_end_work);
2136 		}
2137 		spin_lock(&ctx->buffer_meta_lock);
2138 		while (!list_empty(&ctx->buffer_meta_list)) {
2139 			meta = list_first_entry(&ctx->buffer_meta_list,
2140 						struct coda_buffer_meta, list);
2141 			list_del(&meta->list);
2142 			kfree(meta);
2143 		}
2144 		ctx->num_metas = 0;
2145 		spin_unlock(&ctx->buffer_meta_lock);
2146 		kfifo_init(&ctx->bitstream_fifo,
2147 			ctx->bitstream.vaddr, ctx->bitstream.size);
2148 		ctx->runcounter = 0;
2149 		ctx->aborting = 0;
2150 		ctx->hold = false;
2151 	}
2152 
2153 	if (!ctx->streamon_out && !ctx->streamon_cap)
2154 		ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
2155 }
2156 
2157 static const struct vb2_ops coda_qops = {
2158 	.queue_setup		= coda_queue_setup,
2159 	.buf_prepare		= coda_buf_prepare,
2160 	.buf_queue		= coda_buf_queue,
2161 	.start_streaming	= coda_start_streaming,
2162 	.stop_streaming		= coda_stop_streaming,
2163 	.wait_prepare		= vb2_ops_wait_prepare,
2164 	.wait_finish		= vb2_ops_wait_finish,
2165 };
2166 
coda_s_ctrl(struct v4l2_ctrl * ctrl)2167 static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2168 {
2169 	const char * const *val_names = v4l2_ctrl_get_menu(ctrl->id);
2170 	struct coda_ctx *ctx =
2171 			container_of(ctrl->handler, struct coda_ctx, ctrls);
2172 
2173 	if (val_names)
2174 		coda_dbg(2, ctx, "s_ctrl: id = 0x%x, name = \"%s\", val = %d (\"%s\")\n",
2175 			 ctrl->id, ctrl->name, ctrl->val, val_names[ctrl->val]);
2176 	else
2177 		coda_dbg(2, ctx, "s_ctrl: id = 0x%x, name = \"%s\", val = %d\n",
2178 			 ctrl->id, ctrl->name, ctrl->val);
2179 
2180 	switch (ctrl->id) {
2181 	case V4L2_CID_HFLIP:
2182 		if (ctrl->val)
2183 			ctx->params.rot_mode |= CODA_MIR_HOR;
2184 		else
2185 			ctx->params.rot_mode &= ~CODA_MIR_HOR;
2186 		break;
2187 	case V4L2_CID_VFLIP:
2188 		if (ctrl->val)
2189 			ctx->params.rot_mode |= CODA_MIR_VER;
2190 		else
2191 			ctx->params.rot_mode &= ~CODA_MIR_VER;
2192 		break;
2193 	case V4L2_CID_MPEG_VIDEO_BITRATE:
2194 		ctx->params.bitrate = ctrl->val / 1000;
2195 		ctx->params.bitrate_changed = true;
2196 		break;
2197 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2198 		ctx->params.gop_size = ctrl->val;
2199 		break;
2200 	case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2201 		ctx->params.h264_intra_qp = ctrl->val;
2202 		ctx->params.h264_intra_qp_changed = true;
2203 		break;
2204 	case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2205 		ctx->params.h264_inter_qp = ctrl->val;
2206 		break;
2207 	case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2208 		ctx->params.h264_min_qp = ctrl->val;
2209 		break;
2210 	case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2211 		ctx->params.h264_max_qp = ctrl->val;
2212 		break;
2213 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2214 		ctx->params.h264_slice_alpha_c0_offset_div2 = ctrl->val;
2215 		break;
2216 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2217 		ctx->params.h264_slice_beta_offset_div2 = ctrl->val;
2218 		break;
2219 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2220 		ctx->params.h264_disable_deblocking_filter_idc = ctrl->val;
2221 		break;
2222 	case V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION:
2223 		ctx->params.h264_constrained_intra_pred_flag = ctrl->val;
2224 		break;
2225 	case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
2226 		ctx->params.frame_rc_enable = ctrl->val;
2227 		break;
2228 	case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
2229 		ctx->params.mb_rc_enable = ctrl->val;
2230 		break;
2231 	case V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET:
2232 		ctx->params.h264_chroma_qp_index_offset = ctrl->val;
2233 		break;
2234 	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
2235 		/* TODO: switch between baseline and constrained baseline */
2236 		if (ctx->inst_type == CODA_INST_ENCODER)
2237 			ctx->params.h264_profile_idc = 66;
2238 		break;
2239 	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
2240 		/* nothing to do, this is set by the encoder */
2241 		break;
2242 	case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2243 		ctx->params.mpeg4_intra_qp = ctrl->val;
2244 		break;
2245 	case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2246 		ctx->params.mpeg4_inter_qp = ctrl->val;
2247 		break;
2248 	case V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE:
2249 	case V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL:
2250 	case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
2251 	case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
2252 		/* nothing to do, these are fixed */
2253 		break;
2254 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2255 		ctx->params.slice_mode = ctrl->val;
2256 		ctx->params.slice_mode_changed = true;
2257 		break;
2258 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2259 		ctx->params.slice_max_mb = ctrl->val;
2260 		ctx->params.slice_mode_changed = true;
2261 		break;
2262 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2263 		ctx->params.slice_max_bits = ctrl->val * 8;
2264 		ctx->params.slice_mode_changed = true;
2265 		break;
2266 	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2267 		break;
2268 	case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2269 		ctx->params.intra_refresh = ctrl->val;
2270 		ctx->params.intra_refresh_changed = true;
2271 		break;
2272 	case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
2273 		ctx->params.force_ipicture = true;
2274 		break;
2275 	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
2276 		coda_set_jpeg_compression_quality(ctx, ctrl->val);
2277 		break;
2278 	case V4L2_CID_JPEG_RESTART_INTERVAL:
2279 		ctx->params.jpeg_restart_interval = ctrl->val;
2280 		break;
2281 	case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
2282 		ctx->params.vbv_delay = ctrl->val;
2283 		break;
2284 	case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
2285 		ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
2286 		break;
2287 	default:
2288 		coda_dbg(1, ctx, "Invalid control, id=%d, val=%d\n",
2289 			 ctrl->id, ctrl->val);
2290 		return -EINVAL;
2291 	}
2292 
2293 	return 0;
2294 }
2295 
2296 static const struct v4l2_ctrl_ops coda_ctrl_ops = {
2297 	.s_ctrl = coda_s_ctrl,
2298 };
2299 
coda_encode_ctrls(struct coda_ctx * ctx)2300 static void coda_encode_ctrls(struct coda_ctx *ctx)
2301 {
2302 	int max_gop_size = (ctx->dev->devtype->product == CODA_DX6) ? 60 : 99;
2303 
2304 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2305 		V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
2306 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2307 		V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, max_gop_size, 1, 16);
2308 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2309 		V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
2310 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2311 		V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
2312 	if (ctx->dev->devtype->product != CODA_960) {
2313 		v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2314 			V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2315 	}
2316 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2317 		V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
2318 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2319 		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, -6, 6, 1, 0);
2320 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2321 		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, -6, 6, 1, 0);
2322 	v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2323 		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2324 		V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
2325 		0x0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2326 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2327 		V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION, 0, 1, 1,
2328 		0);
2329 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2330 		V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 0, 1, 1, 1);
2331 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2332 		V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE, 0, 1, 1, 1);
2333 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2334 		V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET, -12, 12, 1, 0);
2335 	v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2336 		V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2337 		V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE, 0x0,
2338 		V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE);
2339 	if (ctx->dev->devtype->product == CODA_HX4 ||
2340 	    ctx->dev->devtype->product == CODA_7541) {
2341 		v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2342 			V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2343 			V4L2_MPEG_VIDEO_H264_LEVEL_3_1,
2344 			~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
2345 			  (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
2346 			  (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1)),
2347 			V4L2_MPEG_VIDEO_H264_LEVEL_3_1);
2348 	}
2349 	if (ctx->dev->devtype->product == CODA_960) {
2350 		v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2351 			V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2352 			V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
2353 			~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
2354 			  (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
2355 			  (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
2356 			  (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
2357 			  (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0)),
2358 			V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
2359 	}
2360 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2361 		V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2362 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2363 		V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2364 	v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2365 		V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
2366 		V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE, 0x0,
2367 		V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
2368 	if (ctx->dev->devtype->product == CODA_HX4 ||
2369 	    ctx->dev->devtype->product == CODA_7541 ||
2370 	    ctx->dev->devtype->product == CODA_960) {
2371 		v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2372 			V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
2373 			V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
2374 			~(1 << V4L2_MPEG_VIDEO_MPEG4_LEVEL_5),
2375 			V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
2376 	}
2377 	v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2378 		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
2379 		V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_BYTES, 0x0,
2380 		V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
2381 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2382 		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
2383 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2384 		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
2385 		500);
2386 	v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2387 		V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2388 		V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2389 		(1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2390 		V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
2391 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2392 		V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
2393 		1920 * 1088 / 256, 1, 0);
2394 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2395 		V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
2396 	/*
2397 	 * The maximum VBV size value is 0x7fffffff bits,
2398 	 * one bit less than 262144 KiB
2399 	 */
2400 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2401 		V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
2402 }
2403 
coda_jpeg_encode_ctrls(struct coda_ctx * ctx)2404 static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
2405 {
2406 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2407 		V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
2408 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2409 		V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
2410 }
2411 
coda_decode_ctrls(struct coda_ctx * ctx)2412 static void coda_decode_ctrls(struct coda_ctx *ctx)
2413 {
2414 	u8 max;
2415 
2416 	ctx->h264_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2417 		&coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2418 		V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
2419 		~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
2420 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
2421 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
2422 		V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
2423 	if (ctx->h264_profile_ctrl)
2424 		ctx->h264_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2425 
2426 	if (ctx->dev->devtype->product == CODA_HX4 ||
2427 	    ctx->dev->devtype->product == CODA_7541)
2428 		max = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
2429 	else if (ctx->dev->devtype->product == CODA_960)
2430 		max = V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
2431 	else
2432 		return;
2433 	ctx->h264_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2434 		&coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL, max, 0, max);
2435 	if (ctx->h264_level_ctrl)
2436 		ctx->h264_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2437 
2438 	ctx->mpeg2_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2439 		&coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE,
2440 		V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH, 0,
2441 		V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH);
2442 	if (ctx->mpeg2_profile_ctrl)
2443 		ctx->mpeg2_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2444 
2445 	ctx->mpeg2_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2446 		&coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL,
2447 		V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH, 0,
2448 		V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH);
2449 	if (ctx->mpeg2_level_ctrl)
2450 		ctx->mpeg2_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2451 
2452 	ctx->mpeg4_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2453 		&coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
2454 		V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY, 0,
2455 		V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY);
2456 	if (ctx->mpeg4_profile_ctrl)
2457 		ctx->mpeg4_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2458 
2459 	ctx->mpeg4_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2460 		&coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
2461 		V4L2_MPEG_VIDEO_MPEG4_LEVEL_5, 0,
2462 		V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
2463 	if (ctx->mpeg4_level_ctrl)
2464 		ctx->mpeg4_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2465 }
2466 
coda_ctrls_setup(struct coda_ctx * ctx)2467 static int coda_ctrls_setup(struct coda_ctx *ctx)
2468 {
2469 	v4l2_ctrl_handler_init(&ctx->ctrls, 2);
2470 
2471 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2472 		V4L2_CID_HFLIP, 0, 1, 1, 0);
2473 	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2474 		V4L2_CID_VFLIP, 0, 1, 1, 0);
2475 	if (ctx->inst_type == CODA_INST_ENCODER) {
2476 		v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2477 				  V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
2478 				  1, 1, 1, 1);
2479 		if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
2480 			coda_jpeg_encode_ctrls(ctx);
2481 		else
2482 			coda_encode_ctrls(ctx);
2483 	} else {
2484 		v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2485 				  V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
2486 				  1, 1, 1, 1);
2487 		if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_H264)
2488 			coda_decode_ctrls(ctx);
2489 	}
2490 
2491 	if (ctx->ctrls.error) {
2492 		v4l2_err(&ctx->dev->v4l2_dev,
2493 			"control initialization error (%d)",
2494 			ctx->ctrls.error);
2495 		return -EINVAL;
2496 	}
2497 
2498 	return v4l2_ctrl_handler_setup(&ctx->ctrls);
2499 }
2500 
coda_queue_init(struct coda_ctx * ctx,struct vb2_queue * vq)2501 static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
2502 {
2503 	vq->drv_priv = ctx;
2504 	vq->ops = &coda_qops;
2505 	vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2506 	vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2507 	vq->lock = &ctx->dev->dev_mutex;
2508 	/* One way to indicate end-of-stream for coda is to set the
2509 	 * bytesused == 0. However by default videobuf2 handles bytesused
2510 	 * equal to 0 as a special case and changes its value to the size
2511 	 * of the buffer. Set the allow_zero_bytesused flag, so
2512 	 * that videobuf2 will keep the value of bytesused intact.
2513 	 */
2514 	vq->allow_zero_bytesused = 1;
2515 	/*
2516 	 * We might be fine with no buffers on some of the queues, but that
2517 	 * would need to be reflected in job_ready(). Currently we expect all
2518 	 * queues to have at least one buffer queued.
2519 	 */
2520 	vq->min_buffers_needed = 1;
2521 	vq->dev = ctx->dev->dev;
2522 
2523 	return vb2_queue_init(vq);
2524 }
2525 
coda_encoder_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)2526 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
2527 			    struct vb2_queue *dst_vq)
2528 {
2529 	int ret;
2530 
2531 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2532 	src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2533 	src_vq->mem_ops = &vb2_dma_contig_memops;
2534 
2535 	ret = coda_queue_init(priv, src_vq);
2536 	if (ret)
2537 		return ret;
2538 
2539 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2540 	dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2541 	dst_vq->mem_ops = &vb2_dma_contig_memops;
2542 
2543 	return coda_queue_init(priv, dst_vq);
2544 }
2545 
coda_decoder_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)2546 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
2547 			    struct vb2_queue *dst_vq)
2548 {
2549 	int ret;
2550 
2551 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2552 	src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
2553 	src_vq->mem_ops = &vb2_vmalloc_memops;
2554 
2555 	ret = coda_queue_init(priv, src_vq);
2556 	if (ret)
2557 		return ret;
2558 
2559 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2560 	dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2561 	dst_vq->dma_attrs = DMA_ATTR_NO_KERNEL_MAPPING;
2562 	dst_vq->mem_ops = &vb2_dma_contig_memops;
2563 
2564 	return coda_queue_init(priv, dst_vq);
2565 }
2566 
2567 /*
2568  * File operations
2569  */
2570 
coda_open(struct file * file)2571 static int coda_open(struct file *file)
2572 {
2573 	struct video_device *vdev = video_devdata(file);
2574 	struct coda_dev *dev = video_get_drvdata(vdev);
2575 	struct coda_ctx *ctx;
2576 	unsigned int max = ~0;
2577 	char *name;
2578 	int ret;
2579 	int idx;
2580 
2581 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2582 	if (!ctx)
2583 		return -ENOMEM;
2584 
2585 	if (dev->devtype->product == CODA_DX6)
2586 		max = CODADX6_MAX_INSTANCES - 1;
2587 	idx = ida_alloc_max(&dev->ida, max, GFP_KERNEL);
2588 	if (idx < 0) {
2589 		ret = idx;
2590 		goto err_coda_max;
2591 	}
2592 
2593 	name = kasprintf(GFP_KERNEL, "context%d", idx);
2594 	if (!name) {
2595 		ret = -ENOMEM;
2596 		goto err_coda_name_init;
2597 	}
2598 
2599 	ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2600 	kfree(name);
2601 
2602 	ctx->cvd = to_coda_video_device(vdev);
2603 	ctx->inst_type = ctx->cvd->type;
2604 	ctx->ops = ctx->cvd->ops;
2605 	ctx->use_bit = !ctx->cvd->direct;
2606 	init_completion(&ctx->completion);
2607 	INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2608 	if (ctx->ops->seq_init_work)
2609 		INIT_WORK(&ctx->seq_init_work, ctx->ops->seq_init_work);
2610 	if (ctx->ops->seq_end_work)
2611 		INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
2612 	v4l2_fh_init(&ctx->fh, video_devdata(file));
2613 	file->private_data = &ctx->fh;
2614 	v4l2_fh_add(&ctx->fh);
2615 	ctx->dev = dev;
2616 	ctx->idx = idx;
2617 
2618 	coda_dbg(1, ctx, "open instance (%p)\n", ctx);
2619 
2620 	switch (dev->devtype->product) {
2621 	case CODA_960:
2622 		/*
2623 		 * Enabling the BWB when decoding can hang the firmware with
2624 		 * certain streams. The issue was tracked as ENGR00293425 by
2625 		 * Freescale. As a workaround, disable BWB for all decoders.
2626 		 * The enable_bwb module parameter allows to override this.
2627 		 */
2628 		if (enable_bwb || ctx->inst_type == CODA_INST_ENCODER)
2629 			ctx->frame_mem_ctrl = CODA9_FRAME_ENABLE_BWB;
2630 		fallthrough;
2631 	case CODA_HX4:
2632 	case CODA_7541:
2633 		ctx->reg_idx = 0;
2634 		break;
2635 	default:
2636 		ctx->reg_idx = idx;
2637 	}
2638 	if (ctx->dev->vdoa && !disable_vdoa) {
2639 		ctx->vdoa = vdoa_context_create(dev->vdoa);
2640 		if (!ctx->vdoa)
2641 			v4l2_warn(&dev->v4l2_dev,
2642 				  "Failed to create vdoa context: not using vdoa");
2643 	}
2644 	ctx->use_vdoa = false;
2645 
2646 	/* Power up and upload firmware if necessary */
2647 	ret = pm_runtime_get_sync(dev->dev);
2648 	if (ret < 0) {
2649 		v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2650 		goto err_pm_get;
2651 	}
2652 
2653 	ret = clk_prepare_enable(dev->clk_per);
2654 	if (ret)
2655 		goto err_pm_get;
2656 
2657 	ret = clk_prepare_enable(dev->clk_ahb);
2658 	if (ret)
2659 		goto err_clk_ahb;
2660 
2661 	set_default_params(ctx);
2662 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2663 					    ctx->ops->queue_init);
2664 	if (IS_ERR(ctx->fh.m2m_ctx)) {
2665 		ret = PTR_ERR(ctx->fh.m2m_ctx);
2666 
2667 		v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2668 			 __func__, ret);
2669 		goto err_ctx_init;
2670 	}
2671 
2672 	ret = coda_ctrls_setup(ctx);
2673 	if (ret) {
2674 		v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
2675 		goto err_ctrls_setup;
2676 	}
2677 
2678 	ctx->fh.ctrl_handler = &ctx->ctrls;
2679 
2680 	mutex_init(&ctx->bitstream_mutex);
2681 	mutex_init(&ctx->buffer_mutex);
2682 	mutex_init(&ctx->wakeup_mutex);
2683 	INIT_LIST_HEAD(&ctx->buffer_meta_list);
2684 	spin_lock_init(&ctx->buffer_meta_lock);
2685 
2686 	return 0;
2687 
2688 err_ctrls_setup:
2689 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2690 err_ctx_init:
2691 	clk_disable_unprepare(dev->clk_ahb);
2692 err_clk_ahb:
2693 	clk_disable_unprepare(dev->clk_per);
2694 err_pm_get:
2695 	pm_runtime_put_sync(dev->dev);
2696 	v4l2_fh_del(&ctx->fh);
2697 	v4l2_fh_exit(&ctx->fh);
2698 err_coda_name_init:
2699 	ida_free(&dev->ida, ctx->idx);
2700 err_coda_max:
2701 	kfree(ctx);
2702 	return ret;
2703 }
2704 
coda_release(struct file * file)2705 static int coda_release(struct file *file)
2706 {
2707 	struct coda_dev *dev = video_drvdata(file);
2708 	struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2709 
2710 	coda_dbg(1, ctx, "release instance (%p)\n", ctx);
2711 
2712 	if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
2713 		coda_bit_stream_end_flag(ctx);
2714 
2715 	/* If this instance is running, call .job_abort and wait for it to end */
2716 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2717 
2718 	if (ctx->vdoa)
2719 		vdoa_context_destroy(ctx->vdoa);
2720 
2721 	/* In case the instance was not running, we still need to call SEQ_END */
2722 	if (ctx->ops->seq_end_work) {
2723 		queue_work(dev->workqueue, &ctx->seq_end_work);
2724 		flush_work(&ctx->seq_end_work);
2725 	}
2726 
2727 	if (ctx->dev->devtype->product == CODA_DX6)
2728 		coda_free_aux_buf(dev, &ctx->workbuf);
2729 
2730 	v4l2_ctrl_handler_free(&ctx->ctrls);
2731 	clk_disable_unprepare(dev->clk_ahb);
2732 	clk_disable_unprepare(dev->clk_per);
2733 	pm_runtime_put_sync(dev->dev);
2734 	v4l2_fh_del(&ctx->fh);
2735 	v4l2_fh_exit(&ctx->fh);
2736 	ida_free(&dev->ida, ctx->idx);
2737 	if (ctx->ops->release)
2738 		ctx->ops->release(ctx);
2739 	debugfs_remove_recursive(ctx->debugfs_entry);
2740 	kfree(ctx);
2741 
2742 	return 0;
2743 }
2744 
2745 static const struct v4l2_file_operations coda_fops = {
2746 	.owner		= THIS_MODULE,
2747 	.open		= coda_open,
2748 	.release	= coda_release,
2749 	.poll		= v4l2_m2m_fop_poll,
2750 	.unlocked_ioctl	= video_ioctl2,
2751 	.mmap		= v4l2_m2m_fop_mmap,
2752 };
2753 
coda_hw_init(struct coda_dev * dev)2754 static int coda_hw_init(struct coda_dev *dev)
2755 {
2756 	u32 data;
2757 	u16 *p;
2758 	int i, ret;
2759 
2760 	ret = clk_prepare_enable(dev->clk_per);
2761 	if (ret)
2762 		goto err_clk_per;
2763 
2764 	ret = clk_prepare_enable(dev->clk_ahb);
2765 	if (ret)
2766 		goto err_clk_ahb;
2767 
2768 	reset_control_reset(dev->rstc);
2769 
2770 	/*
2771 	 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
2772 	 * The 16-bit chars in the code buffer are in memory access
2773 	 * order, re-sort them to CODA order for register download.
2774 	 * Data in this SRAM survives a reboot.
2775 	 */
2776 	p = (u16 *)dev->codebuf.vaddr;
2777 	if (dev->devtype->product == CODA_DX6) {
2778 		for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++)  {
2779 			data = CODA_DOWN_ADDRESS_SET(i) |
2780 				CODA_DOWN_DATA_SET(p[i ^ 1]);
2781 			coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2782 		}
2783 	} else {
2784 		for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2785 			data = CODA_DOWN_ADDRESS_SET(i) |
2786 				CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2787 							3 - (i % 4)]);
2788 			coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2789 		}
2790 	}
2791 
2792 	/* Clear registers */
2793 	for (i = 0; i < 64; i++)
2794 		coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2795 
2796 	/* Tell the BIT where to find everything it needs */
2797 	if (dev->devtype->product == CODA_960 ||
2798 	    dev->devtype->product == CODA_7541 ||
2799 	    dev->devtype->product == CODA_HX4) {
2800 		coda_write(dev, dev->tempbuf.paddr,
2801 				CODA_REG_BIT_TEMP_BUF_ADDR);
2802 		coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2803 	} else {
2804 		coda_write(dev, dev->workbuf.paddr,
2805 			      CODA_REG_BIT_WORK_BUF_ADDR);
2806 	}
2807 	coda_write(dev, dev->codebuf.paddr,
2808 		      CODA_REG_BIT_CODE_BUF_ADDR);
2809 	coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2810 
2811 	/* Set default values */
2812 	switch (dev->devtype->product) {
2813 	case CODA_DX6:
2814 		coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
2815 			   CODA_REG_BIT_STREAM_CTRL);
2816 		break;
2817 	default:
2818 		coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
2819 			   CODA_REG_BIT_STREAM_CTRL);
2820 	}
2821 	if (dev->devtype->product == CODA_960)
2822 		coda_write(dev, CODA9_FRAME_ENABLE_BWB,
2823 				CODA_REG_BIT_FRAME_MEM_CTRL);
2824 	else
2825 		coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
2826 
2827 	if (dev->devtype->product != CODA_DX6)
2828 		coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2829 
2830 	coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2831 		      CODA_REG_BIT_INT_ENABLE);
2832 
2833 	/* Reset VPU and start processor */
2834 	data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2835 	data |= CODA_REG_RESET_ENABLE;
2836 	coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2837 	udelay(10);
2838 	data &= ~CODA_REG_RESET_ENABLE;
2839 	coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2840 	coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2841 
2842 	clk_disable_unprepare(dev->clk_ahb);
2843 	clk_disable_unprepare(dev->clk_per);
2844 
2845 	return 0;
2846 
2847 err_clk_ahb:
2848 	clk_disable_unprepare(dev->clk_per);
2849 err_clk_per:
2850 	return ret;
2851 }
2852 
coda_register_device(struct coda_dev * dev,int i)2853 static int coda_register_device(struct coda_dev *dev, int i)
2854 {
2855 	struct video_device *vfd = &dev->vfd[i];
2856 	enum coda_inst_type type;
2857 	int ret;
2858 
2859 	if (i >= dev->devtype->num_vdevs)
2860 		return -EINVAL;
2861 	type = dev->devtype->vdevs[i]->type;
2862 
2863 	strscpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
2864 	vfd->fops	= &coda_fops;
2865 	vfd->ioctl_ops	= &coda_ioctl_ops;
2866 	vfd->release	= video_device_release_empty,
2867 	vfd->lock	= &dev->dev_mutex;
2868 	vfd->v4l2_dev	= &dev->v4l2_dev;
2869 	vfd->vfl_dir	= VFL_DIR_M2M;
2870 	vfd->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
2871 	video_set_drvdata(vfd, dev);
2872 
2873 	/* Not applicable, use the selection API instead */
2874 	v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
2875 	v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
2876 	v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
2877 
2878 	ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0);
2879 	if (!ret)
2880 		v4l2_info(&dev->v4l2_dev, "%s registered as %s\n",
2881 			  type == CODA_INST_ENCODER ? "encoder" : "decoder",
2882 			  video_device_node_name(vfd));
2883 	return ret;
2884 }
2885 
coda_copy_firmware(struct coda_dev * dev,const u8 * const buf,size_t size)2886 static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
2887 			       size_t size)
2888 {
2889 	u32 *src = (u32 *)buf;
2890 
2891 	/* Check if the firmware has a 16-byte Freescale header, skip it */
2892 	if (buf[0] == 'M' && buf[1] == 'X')
2893 		src += 4;
2894 	/*
2895 	 * Check whether the firmware is in native order or pre-reordered for
2896 	 * memory access. The first instruction opcode always is 0xe40e.
2897 	 */
2898 	if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
2899 		u32 *dst = dev->codebuf.vaddr;
2900 		int i;
2901 
2902 		/* Firmware in native order, reorder while copying */
2903 		if (dev->devtype->product == CODA_DX6) {
2904 			for (i = 0; i < (size - 16) / 4; i++)
2905 				dst[i] = (src[i] << 16) | (src[i] >> 16);
2906 		} else {
2907 			for (i = 0; i < (size - 16) / 4; i += 2) {
2908 				dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
2909 				dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
2910 			}
2911 		}
2912 	} else {
2913 		/* Copy the already reordered firmware image */
2914 		memcpy(dev->codebuf.vaddr, src, size);
2915 	}
2916 }
2917 
2918 static void coda_fw_callback(const struct firmware *fw, void *context);
2919 
coda_firmware_request(struct coda_dev * dev)2920 static int coda_firmware_request(struct coda_dev *dev)
2921 {
2922 	char *fw;
2923 
2924 	if (dev->firmware >= ARRAY_SIZE(dev->devtype->firmware))
2925 		return -EINVAL;
2926 
2927 	fw = dev->devtype->firmware[dev->firmware];
2928 
2929 	dev_dbg(dev->dev, "requesting firmware '%s' for %s\n", fw,
2930 		coda_product_name(dev->devtype->product));
2931 
2932 	return request_firmware_nowait(THIS_MODULE, true, fw, dev->dev,
2933 				       GFP_KERNEL, dev, coda_fw_callback);
2934 }
2935 
coda_fw_callback(const struct firmware * fw,void * context)2936 static void coda_fw_callback(const struct firmware *fw, void *context)
2937 {
2938 	struct coda_dev *dev = context;
2939 	int i, ret;
2940 
2941 	if (!fw) {
2942 		dev->firmware++;
2943 		ret = coda_firmware_request(dev);
2944 		if (ret < 0) {
2945 			v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2946 			goto put_pm;
2947 		}
2948 		return;
2949 	}
2950 	if (dev->firmware > 0) {
2951 		/*
2952 		 * Since we can't suppress warnings for failed asynchronous
2953 		 * firmware requests, report that the fallback firmware was
2954 		 * found.
2955 		 */
2956 		dev_info(dev->dev, "Using fallback firmware %s\n",
2957 			 dev->devtype->firmware[dev->firmware]);
2958 	}
2959 
2960 	/* allocate auxiliary per-device code buffer for the BIT processor */
2961 	ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
2962 				 dev->debugfs_root);
2963 	if (ret < 0)
2964 		goto put_pm;
2965 
2966 	coda_copy_firmware(dev, fw->data, fw->size);
2967 	release_firmware(fw);
2968 
2969 	ret = coda_hw_init(dev);
2970 	if (ret < 0) {
2971 		v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2972 		goto put_pm;
2973 	}
2974 
2975 	ret = coda_check_firmware(dev);
2976 	if (ret < 0)
2977 		goto put_pm;
2978 
2979 	dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2980 	if (IS_ERR(dev->m2m_dev)) {
2981 		v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
2982 		goto put_pm;
2983 	}
2984 
2985 	for (i = 0; i < dev->devtype->num_vdevs; i++) {
2986 		ret = coda_register_device(dev, i);
2987 		if (ret) {
2988 			v4l2_err(&dev->v4l2_dev,
2989 				 "Failed to register %s video device: %d\n",
2990 				 dev->devtype->vdevs[i]->name, ret);
2991 			goto rel_vfd;
2992 		}
2993 	}
2994 
2995 	pm_runtime_put_sync(dev->dev);
2996 	return;
2997 
2998 rel_vfd:
2999 	while (--i >= 0)
3000 		video_unregister_device(&dev->vfd[i]);
3001 	v4l2_m2m_release(dev->m2m_dev);
3002 put_pm:
3003 	pm_runtime_put_sync(dev->dev);
3004 }
3005 
3006 enum coda_platform {
3007 	CODA_IMX27,
3008 	CODA_IMX51,
3009 	CODA_IMX53,
3010 	CODA_IMX6Q,
3011 	CODA_IMX6DL,
3012 };
3013 
3014 static const struct coda_devtype coda_devdata[] = {
3015 	[CODA_IMX27] = {
3016 		.firmware     = {
3017 			"vpu_fw_imx27_TO2.bin",
3018 			"vpu/vpu_fw_imx27_TO2.bin",
3019 			"v4l-codadx6-imx27.bin"
3020 		},
3021 		.product      = CODA_DX6,
3022 		.codecs       = codadx6_codecs,
3023 		.num_codecs   = ARRAY_SIZE(codadx6_codecs),
3024 		.vdevs        = codadx6_video_devices,
3025 		.num_vdevs    = ARRAY_SIZE(codadx6_video_devices),
3026 		.workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
3027 		.iram_size    = 0xb000,
3028 	},
3029 	[CODA_IMX51] = {
3030 		.firmware     = {
3031 			"vpu_fw_imx51.bin",
3032 			"vpu/vpu_fw_imx51.bin",
3033 			"v4l-codahx4-imx51.bin"
3034 		},
3035 		.product      = CODA_HX4,
3036 		.codecs       = codahx4_codecs,
3037 		.num_codecs   = ARRAY_SIZE(codahx4_codecs),
3038 		.vdevs        = codahx4_video_devices,
3039 		.num_vdevs    = ARRAY_SIZE(codahx4_video_devices),
3040 		.workbuf_size = 128 * 1024,
3041 		.tempbuf_size = 304 * 1024,
3042 		.iram_size    = 0x14000,
3043 	},
3044 	[CODA_IMX53] = {
3045 		.firmware     = {
3046 			"vpu_fw_imx53.bin",
3047 			"vpu/vpu_fw_imx53.bin",
3048 			"v4l-coda7541-imx53.bin"
3049 		},
3050 		.product      = CODA_7541,
3051 		.codecs       = coda7_codecs,
3052 		.num_codecs   = ARRAY_SIZE(coda7_codecs),
3053 		.vdevs        = coda7_video_devices,
3054 		.num_vdevs    = ARRAY_SIZE(coda7_video_devices),
3055 		.workbuf_size = 128 * 1024,
3056 		.tempbuf_size = 304 * 1024,
3057 		.iram_size    = 0x14000,
3058 	},
3059 	[CODA_IMX6Q] = {
3060 		.firmware     = {
3061 			"vpu_fw_imx6q.bin",
3062 			"vpu/vpu_fw_imx6q.bin",
3063 			"v4l-coda960-imx6q.bin"
3064 		},
3065 		.product      = CODA_960,
3066 		.codecs       = coda9_codecs,
3067 		.num_codecs   = ARRAY_SIZE(coda9_codecs),
3068 		.vdevs        = coda9_video_devices,
3069 		.num_vdevs    = ARRAY_SIZE(coda9_video_devices),
3070 		.workbuf_size = 80 * 1024,
3071 		.tempbuf_size = 204 * 1024,
3072 		.iram_size    = 0x21000,
3073 	},
3074 	[CODA_IMX6DL] = {
3075 		.firmware     = {
3076 			"vpu_fw_imx6d.bin",
3077 			"vpu/vpu_fw_imx6d.bin",
3078 			"v4l-coda960-imx6dl.bin"
3079 		},
3080 		.product      = CODA_960,
3081 		.codecs       = coda9_codecs,
3082 		.num_codecs   = ARRAY_SIZE(coda9_codecs),
3083 		.vdevs        = coda9_video_devices,
3084 		.num_vdevs    = ARRAY_SIZE(coda9_video_devices),
3085 		.workbuf_size = 80 * 1024,
3086 		.tempbuf_size = 204 * 1024,
3087 		.iram_size    = 0x1f000, /* leave 4k for suspend code */
3088 	},
3089 };
3090 
3091 static const struct platform_device_id coda_platform_ids[] = {
3092 	{ .name = "coda-imx27", .driver_data = CODA_IMX27 },
3093 	{ /* sentinel */ }
3094 };
3095 MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3096 
3097 #ifdef CONFIG_OF
3098 static const struct of_device_id coda_dt_ids[] = {
3099 	{ .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
3100 	{ .compatible = "fsl,imx51-vpu", .data = &coda_devdata[CODA_IMX51] },
3101 	{ .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
3102 	{ .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3103 	{ .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
3104 	{ /* sentinel */ }
3105 };
3106 MODULE_DEVICE_TABLE(of, coda_dt_ids);
3107 #endif
3108 
coda_probe(struct platform_device * pdev)3109 static int coda_probe(struct platform_device *pdev)
3110 {
3111 	const struct of_device_id *of_id =
3112 			of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3113 	const struct platform_device_id *pdev_id;
3114 	struct coda_platform_data *pdata = pdev->dev.platform_data;
3115 	struct device_node *np = pdev->dev.of_node;
3116 	struct gen_pool *pool;
3117 	struct coda_dev *dev;
3118 	int ret, irq;
3119 
3120 	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
3121 	if (!dev)
3122 		return -ENOMEM;
3123 
3124 	pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3125 
3126 	if (of_id)
3127 		dev->devtype = of_id->data;
3128 	else if (pdev_id)
3129 		dev->devtype = &coda_devdata[pdev_id->driver_data];
3130 	else
3131 		return -EINVAL;
3132 
3133 	dev->dev = &pdev->dev;
3134 	dev->clk_per = devm_clk_get(&pdev->dev, "per");
3135 	if (IS_ERR(dev->clk_per)) {
3136 		dev_err(&pdev->dev, "Could not get per clock\n");
3137 		return PTR_ERR(dev->clk_per);
3138 	}
3139 
3140 	dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3141 	if (IS_ERR(dev->clk_ahb)) {
3142 		dev_err(&pdev->dev, "Could not get ahb clock\n");
3143 		return PTR_ERR(dev->clk_ahb);
3144 	}
3145 
3146 	/* Get  memory for physical registers */
3147 	dev->regs_base = devm_platform_ioremap_resource(pdev, 0);
3148 	if (IS_ERR(dev->regs_base))
3149 		return PTR_ERR(dev->regs_base);
3150 
3151 	/* IRQ */
3152 	irq = platform_get_irq_byname(pdev, "bit");
3153 	if (irq < 0)
3154 		irq = platform_get_irq(pdev, 0);
3155 	if (irq < 0)
3156 		return irq;
3157 
3158 	ret = devm_request_irq(&pdev->dev, irq, coda_irq_handler, 0,
3159 			       dev_name(&pdev->dev), dev);
3160 	if (ret < 0) {
3161 		dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
3162 		return ret;
3163 	}
3164 
3165 	/* JPEG IRQ */
3166 	if (dev->devtype->product == CODA_960) {
3167 		irq = platform_get_irq_byname(pdev, "jpeg");
3168 		if (irq < 0)
3169 			return irq;
3170 
3171 		ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
3172 						coda9_jpeg_irq_handler,
3173 						IRQF_ONESHOT, CODA_NAME " jpeg",
3174 						dev);
3175 		if (ret < 0) {
3176 			dev_err(&pdev->dev, "failed to request jpeg irq\n");
3177 			return ret;
3178 		}
3179 	}
3180 
3181 	dev->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev,
3182 							      NULL);
3183 	if (IS_ERR(dev->rstc)) {
3184 		ret = PTR_ERR(dev->rstc);
3185 		dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
3186 		return ret;
3187 	}
3188 
3189 	/* Get IRAM pool from device tree or platform data */
3190 	pool = of_gen_pool_get(np, "iram", 0);
3191 	if (!pool && pdata)
3192 		pool = gen_pool_get(pdata->iram_dev, NULL);
3193 	if (!pool) {
3194 		dev_err(&pdev->dev, "iram pool not available\n");
3195 		return -ENOMEM;
3196 	}
3197 	dev->iram_pool = pool;
3198 
3199 	/* Get vdoa_data if supported by the platform */
3200 	dev->vdoa = coda_get_vdoa_data();
3201 	if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER)
3202 		return -EPROBE_DEFER;
3203 
3204 	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3205 	if (ret)
3206 		return ret;
3207 
3208 	mutex_init(&dev->dev_mutex);
3209 	mutex_init(&dev->coda_mutex);
3210 	ida_init(&dev->ida);
3211 
3212 	dev->debugfs_root = debugfs_create_dir("coda", NULL);
3213 
3214 	/* allocate auxiliary per-device buffers for the BIT processor */
3215 	if (dev->devtype->product == CODA_DX6) {
3216 		ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3217 					 dev->devtype->workbuf_size, "workbuf",
3218 					 dev->debugfs_root);
3219 		if (ret < 0)
3220 			goto err_v4l2_register;
3221 	}
3222 
3223 	if (dev->devtype->tempbuf_size) {
3224 		ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3225 					 dev->devtype->tempbuf_size, "tempbuf",
3226 					 dev->debugfs_root);
3227 		if (ret < 0)
3228 			goto err_v4l2_register;
3229 	}
3230 
3231 	dev->iram.size = dev->devtype->iram_size;
3232 	dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3233 					     &dev->iram.paddr);
3234 	if (!dev->iram.vaddr) {
3235 		dev_warn(&pdev->dev, "unable to alloc iram\n");
3236 	} else {
3237 		memset(dev->iram.vaddr, 0, dev->iram.size);
3238 		dev->iram.blob.data = dev->iram.vaddr;
3239 		dev->iram.blob.size = dev->iram.size;
3240 		dev->iram.dentry = debugfs_create_blob("iram", 0644,
3241 						       dev->debugfs_root,
3242 						       &dev->iram.blob);
3243 	}
3244 
3245 	dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3246 	if (!dev->workqueue) {
3247 		dev_err(&pdev->dev, "unable to alloc workqueue\n");
3248 		ret = -ENOMEM;
3249 		goto err_v4l2_register;
3250 	}
3251 
3252 	platform_set_drvdata(pdev, dev);
3253 
3254 	/*
3255 	 * Start activated so we can directly call coda_hw_init in
3256 	 * coda_fw_callback regardless of whether CONFIG_PM is
3257 	 * enabled or whether the device is associated with a PM domain.
3258 	 */
3259 	pm_runtime_get_noresume(&pdev->dev);
3260 	pm_runtime_set_active(&pdev->dev);
3261 	pm_runtime_enable(&pdev->dev);
3262 
3263 	ret = coda_firmware_request(dev);
3264 	if (ret)
3265 		goto err_alloc_workqueue;
3266 	return 0;
3267 
3268 err_alloc_workqueue:
3269 	pm_runtime_disable(&pdev->dev);
3270 	pm_runtime_put_noidle(&pdev->dev);
3271 	destroy_workqueue(dev->workqueue);
3272 err_v4l2_register:
3273 	v4l2_device_unregister(&dev->v4l2_dev);
3274 	return ret;
3275 }
3276 
coda_remove(struct platform_device * pdev)3277 static int coda_remove(struct platform_device *pdev)
3278 {
3279 	struct coda_dev *dev = platform_get_drvdata(pdev);
3280 	int i;
3281 
3282 	for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
3283 		if (video_get_drvdata(&dev->vfd[i]))
3284 			video_unregister_device(&dev->vfd[i]);
3285 	}
3286 	if (dev->m2m_dev)
3287 		v4l2_m2m_release(dev->m2m_dev);
3288 	pm_runtime_disable(&pdev->dev);
3289 	v4l2_device_unregister(&dev->v4l2_dev);
3290 	destroy_workqueue(dev->workqueue);
3291 	if (dev->iram.vaddr)
3292 		gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3293 			      dev->iram.size);
3294 	coda_free_aux_buf(dev, &dev->codebuf);
3295 	coda_free_aux_buf(dev, &dev->tempbuf);
3296 	coda_free_aux_buf(dev, &dev->workbuf);
3297 	debugfs_remove_recursive(dev->debugfs_root);
3298 	ida_destroy(&dev->ida);
3299 	return 0;
3300 }
3301 
3302 #ifdef CONFIG_PM
coda_runtime_resume(struct device * dev)3303 static int coda_runtime_resume(struct device *dev)
3304 {
3305 	struct coda_dev *cdev = dev_get_drvdata(dev);
3306 	int ret = 0;
3307 
3308 	if (dev->pm_domain && cdev->codebuf.vaddr) {
3309 		ret = coda_hw_init(cdev);
3310 		if (ret)
3311 			v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3312 	}
3313 
3314 	return ret;
3315 }
3316 #endif
3317 
3318 static const struct dev_pm_ops coda_pm_ops = {
3319 	SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3320 };
3321 
3322 static struct platform_driver coda_driver = {
3323 	.probe	= coda_probe,
3324 	.remove	= coda_remove,
3325 	.driver	= {
3326 		.name	= CODA_NAME,
3327 		.of_match_table = of_match_ptr(coda_dt_ids),
3328 		.pm	= &coda_pm_ops,
3329 	},
3330 	.id_table = coda_platform_ids,
3331 };
3332 
3333 module_platform_driver(coda_driver);
3334 
3335 MODULE_LICENSE("GPL");
3336 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3337 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");
3338