1/*
2 * Coda multi-standard codec IP
3 *
4 * Copyright (C) 2012 Vista Silicon S.L.
5 *    Javier Martin, <javier.martin@vista-silicon.com>
6 *    Xavier Duret
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/clk.h>
15#include <linux/debugfs.h>
16#include <linux/delay.h>
17#include <linux/firmware.h>
18#include <linux/genalloc.h>
19#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/irq.h>
22#include <linux/kfifo.h>
23#include <linux/module.h>
24#include <linux/of_device.h>
25#include <linux/platform_device.h>
26#include <linux/pm_runtime.h>
27#include <linux/slab.h>
28#include <linux/videodev2.h>
29#include <linux/of.h>
30#include <linux/platform_data/coda.h>
31#include <linux/reset.h>
32
33#include <media/v4l2-ctrls.h>
34#include <media/v4l2-device.h>
35#include <media/v4l2-event.h>
36#include <media/v4l2-ioctl.h>
37#include <media/v4l2-mem2mem.h>
38#include <media/videobuf2-core.h>
39#include <media/videobuf2-dma-contig.h>
40#include <media/videobuf2-vmalloc.h>
41
42#include "coda.h"
43
44#define CODA_NAME		"coda"
45
46#define CODADX6_MAX_INSTANCES	4
47#define CODA_MAX_FORMATS	4
48
49#define CODA_ISRAM_SIZE	(2048 * 2)
50
51#define MIN_W 176
52#define MIN_H 144
53
54#define S_ALIGN		1 /* multiple of 2 */
55#define W_ALIGN		1 /* multiple of 2 */
56#define H_ALIGN		1 /* multiple of 2 */
57
58#define fh_to_ctx(__fh)	container_of(__fh, struct coda_ctx, fh)
59
60int coda_debug;
61module_param(coda_debug, int, 0644);
62MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
63
64struct coda_fmt {
65	char *name;
66	u32 fourcc;
67};
68
69void coda_write(struct coda_dev *dev, u32 data, u32 reg)
70{
71	v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
72		 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
73	writel(data, dev->regs_base + reg);
74}
75
76unsigned int coda_read(struct coda_dev *dev, u32 reg)
77{
78	u32 data;
79
80	data = readl(dev->regs_base + reg);
81	v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
82		 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
83	return data;
84}
85
86void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
87		     struct vb2_buffer *buf, unsigned int reg_y)
88{
89	u32 base_y = vb2_dma_contig_plane_dma_addr(buf, 0);
90	u32 base_cb, base_cr;
91
92	switch (q_data->fourcc) {
93	case V4L2_PIX_FMT_YVU420:
94		/* Switch Cb and Cr for YVU420 format */
95		base_cr = base_y + q_data->bytesperline * q_data->height;
96		base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
97		break;
98	case V4L2_PIX_FMT_YUV420:
99	case V4L2_PIX_FMT_NV12:
100	default:
101		base_cb = base_y + q_data->bytesperline * q_data->height;
102		base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
103		break;
104	case V4L2_PIX_FMT_YUV422P:
105		base_cb = base_y + q_data->bytesperline * q_data->height;
106		base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
107	}
108
109	coda_write(ctx->dev, base_y, reg_y);
110	coda_write(ctx->dev, base_cb, reg_y + 4);
111	coda_write(ctx->dev, base_cr, reg_y + 8);
112}
113
114/*
115 * Array of all formats supported by any version of Coda:
116 */
117static const struct coda_fmt coda_formats[] = {
118	{
119		.name = "YUV 4:2:0 Planar, YCbCr",
120		.fourcc = V4L2_PIX_FMT_YUV420,
121	},
122	{
123		.name = "YUV 4:2:0 Planar, YCrCb",
124		.fourcc = V4L2_PIX_FMT_YVU420,
125	},
126	{
127		.name = "YUV 4:2:0 Partial interleaved Y/CbCr",
128		.fourcc = V4L2_PIX_FMT_NV12,
129	},
130	{
131		.name = "YUV 4:2:2 Planar, YCbCr",
132		.fourcc = V4L2_PIX_FMT_YUV422P,
133	},
134	{
135		.name = "H264 Encoded Stream",
136		.fourcc = V4L2_PIX_FMT_H264,
137	},
138	{
139		.name = "MPEG4 Encoded Stream",
140		.fourcc = V4L2_PIX_FMT_MPEG4,
141	},
142	{
143		.name = "JPEG Encoded Images",
144		.fourcc = V4L2_PIX_FMT_JPEG,
145	},
146};
147
148#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
149	{ mode, src_fourcc, dst_fourcc, max_w, max_h }
150
151/*
152 * Arrays of codecs supported by each given version of Coda:
153 *  i.MX27 -> codadx6
154 *  i.MX5x -> coda7
155 *  i.MX6  -> coda960
156 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
157 */
158static const struct coda_codec codadx6_codecs[] = {
159	CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,  720, 576),
160	CODA_CODEC(CODADX6_MODE_ENCODE_MP4,  V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
161};
162
163static const struct coda_codec coda7_codecs[] = {
164	CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,   1280, 720),
165	CODA_CODEC(CODA7_MODE_ENCODE_MP4,  V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4,  1280, 720),
166	CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG,   8192, 8192),
167	CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264,   V4L2_PIX_FMT_YUV420, 1920, 1088),
168	CODA_CODEC(CODA7_MODE_DECODE_MP4,  V4L2_PIX_FMT_MPEG4,  V4L2_PIX_FMT_YUV420, 1920, 1088),
169	CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG,   V4L2_PIX_FMT_YUV420, 8192, 8192),
170};
171
172static const struct coda_codec coda9_codecs[] = {
173	CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,   1920, 1088),
174	CODA_CODEC(CODA9_MODE_ENCODE_MP4,  V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4,  1920, 1088),
175	CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264,   V4L2_PIX_FMT_YUV420, 1920, 1088),
176	CODA_CODEC(CODA9_MODE_DECODE_MP4,  V4L2_PIX_FMT_MPEG4,  V4L2_PIX_FMT_YUV420, 1920, 1088),
177};
178
179struct coda_video_device {
180	const char *name;
181	enum coda_inst_type type;
182	const struct coda_context_ops *ops;
183	bool direct;
184	u32 src_formats[CODA_MAX_FORMATS];
185	u32 dst_formats[CODA_MAX_FORMATS];
186};
187
188static const struct coda_video_device coda_bit_encoder = {
189	.name = "coda-encoder",
190	.type = CODA_INST_ENCODER,
191	.ops = &coda_bit_encode_ops,
192	.src_formats = {
193		V4L2_PIX_FMT_YUV420,
194		V4L2_PIX_FMT_YVU420,
195		V4L2_PIX_FMT_NV12,
196	},
197	.dst_formats = {
198		V4L2_PIX_FMT_H264,
199		V4L2_PIX_FMT_MPEG4,
200	},
201};
202
203static const struct coda_video_device coda_bit_jpeg_encoder = {
204	.name = "coda-jpeg-encoder",
205	.type = CODA_INST_ENCODER,
206	.ops = &coda_bit_encode_ops,
207	.src_formats = {
208		V4L2_PIX_FMT_YUV420,
209		V4L2_PIX_FMT_YVU420,
210		V4L2_PIX_FMT_NV12,
211		V4L2_PIX_FMT_YUV422P,
212	},
213	.dst_formats = {
214		V4L2_PIX_FMT_JPEG,
215	},
216};
217
218static const struct coda_video_device coda_bit_decoder = {
219	.name = "coda-decoder",
220	.type = CODA_INST_DECODER,
221	.ops = &coda_bit_decode_ops,
222	.src_formats = {
223		V4L2_PIX_FMT_H264,
224		V4L2_PIX_FMT_MPEG4,
225	},
226	.dst_formats = {
227		V4L2_PIX_FMT_YUV420,
228		V4L2_PIX_FMT_YVU420,
229		V4L2_PIX_FMT_NV12,
230	},
231};
232
233static const struct coda_video_device coda_bit_jpeg_decoder = {
234	.name = "coda-jpeg-decoder",
235	.type = CODA_INST_DECODER,
236	.ops = &coda_bit_decode_ops,
237	.src_formats = {
238		V4L2_PIX_FMT_JPEG,
239	},
240	.dst_formats = {
241		V4L2_PIX_FMT_YUV420,
242		V4L2_PIX_FMT_YVU420,
243		V4L2_PIX_FMT_NV12,
244		V4L2_PIX_FMT_YUV422P,
245	},
246};
247
248static const struct coda_video_device *codadx6_video_devices[] = {
249	&coda_bit_encoder,
250};
251
252static const struct coda_video_device *coda7_video_devices[] = {
253	&coda_bit_jpeg_encoder,
254	&coda_bit_jpeg_decoder,
255	&coda_bit_encoder,
256	&coda_bit_decoder,
257};
258
259static const struct coda_video_device *coda9_video_devices[] = {
260	&coda_bit_encoder,
261	&coda_bit_decoder,
262};
263
264static bool coda_format_is_yuv(u32 fourcc)
265{
266	switch (fourcc) {
267	case V4L2_PIX_FMT_YUV420:
268	case V4L2_PIX_FMT_YVU420:
269	case V4L2_PIX_FMT_NV12:
270	case V4L2_PIX_FMT_YUV422P:
271		return true;
272	default:
273		return false;
274	}
275}
276
277static const char *coda_format_name(u32 fourcc)
278{
279	int i;
280
281	for (i = 0; i < ARRAY_SIZE(coda_formats); i++) {
282		if (coda_formats[i].fourcc == fourcc)
283			return coda_formats[i].name;
284	}
285
286	return NULL;
287}
288
289/*
290 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
291 * tables.
292 */
293static u32 coda_format_normalize_yuv(u32 fourcc)
294{
295	return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
296}
297
298static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
299						int src_fourcc, int dst_fourcc)
300{
301	const struct coda_codec *codecs = dev->devtype->codecs;
302	int num_codecs = dev->devtype->num_codecs;
303	int k;
304
305	src_fourcc = coda_format_normalize_yuv(src_fourcc);
306	dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
307	if (src_fourcc == dst_fourcc)
308		return NULL;
309
310	for (k = 0; k < num_codecs; k++) {
311		if (codecs[k].src_fourcc == src_fourcc &&
312		    codecs[k].dst_fourcc == dst_fourcc)
313			break;
314	}
315
316	if (k == num_codecs)
317		return NULL;
318
319	return &codecs[k];
320}
321
322static void coda_get_max_dimensions(struct coda_dev *dev,
323				    const struct coda_codec *codec,
324				    int *max_w, int *max_h)
325{
326	const struct coda_codec *codecs = dev->devtype->codecs;
327	int num_codecs = dev->devtype->num_codecs;
328	unsigned int w, h;
329	int k;
330
331	if (codec) {
332		w = codec->max_w;
333		h = codec->max_h;
334	} else {
335		for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
336			w = max(w, codecs[k].max_w);
337			h = max(h, codecs[k].max_h);
338		}
339	}
340
341	if (max_w)
342		*max_w = w;
343	if (max_h)
344		*max_h = h;
345}
346
347const struct coda_video_device *to_coda_video_device(struct video_device *vdev)
348{
349	struct coda_dev *dev = video_get_drvdata(vdev);
350	unsigned int i = vdev - dev->vfd;
351
352	if (i >= dev->devtype->num_vdevs)
353		return NULL;
354
355	return dev->devtype->vdevs[i];
356}
357
358const char *coda_product_name(int product)
359{
360	static char buf[9];
361
362	switch (product) {
363	case CODA_DX6:
364		return "CodaDx6";
365	case CODA_7541:
366		return "CODA7541";
367	case CODA_960:
368		return "CODA960";
369	default:
370		snprintf(buf, sizeof(buf), "(0x%04x)", product);
371		return buf;
372	}
373}
374
375/*
376 * V4L2 ioctl() operations.
377 */
378static int coda_querycap(struct file *file, void *priv,
379			 struct v4l2_capability *cap)
380{
381	struct coda_ctx *ctx = fh_to_ctx(priv);
382
383	strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
384	strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
385		sizeof(cap->card));
386	strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
387	cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
388	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
389
390	return 0;
391}
392
393static int coda_enum_fmt(struct file *file, void *priv,
394			 struct v4l2_fmtdesc *f)
395{
396	struct video_device *vdev = video_devdata(file);
397	const struct coda_video_device *cvd = to_coda_video_device(vdev);
398	const u32 *formats;
399	const char *name;
400
401	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
402		formats = cvd->src_formats;
403	else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
404		formats = cvd->dst_formats;
405	else
406		return -EINVAL;
407
408	if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
409		return -EINVAL;
410
411	name = coda_format_name(formats[f->index]);
412	strlcpy(f->description, name, sizeof(f->description));
413	f->pixelformat = formats[f->index];
414	if (!coda_format_is_yuv(formats[f->index]))
415		f->flags |= V4L2_FMT_FLAG_COMPRESSED;
416
417	return 0;
418}
419
420static int coda_g_fmt(struct file *file, void *priv,
421		      struct v4l2_format *f)
422{
423	struct coda_q_data *q_data;
424	struct coda_ctx *ctx = fh_to_ctx(priv);
425
426	q_data = get_q_data(ctx, f->type);
427	if (!q_data)
428		return -EINVAL;
429
430	f->fmt.pix.field	= V4L2_FIELD_NONE;
431	f->fmt.pix.pixelformat	= q_data->fourcc;
432	f->fmt.pix.width	= q_data->width;
433	f->fmt.pix.height	= q_data->height;
434	f->fmt.pix.bytesperline = q_data->bytesperline;
435
436	f->fmt.pix.sizeimage	= q_data->sizeimage;
437	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
438		f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
439	else
440		f->fmt.pix.colorspace = ctx->colorspace;
441
442	return 0;
443}
444
445static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
446{
447	struct coda_q_data *q_data;
448	const u32 *formats;
449	int i;
450
451	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
452		formats = ctx->cvd->src_formats;
453	else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
454		formats = ctx->cvd->dst_formats;
455	else
456		return -EINVAL;
457
458	for (i = 0; i < CODA_MAX_FORMATS; i++) {
459		if (formats[i] == f->fmt.pix.pixelformat) {
460			f->fmt.pix.pixelformat = formats[i];
461			return 0;
462		}
463	}
464
465	/* Fall back to currently set pixelformat */
466	q_data = get_q_data(ctx, f->type);
467	f->fmt.pix.pixelformat = q_data->fourcc;
468
469	return 0;
470}
471
472static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
473					    u32 width, u32 height)
474{
475	/*
476	 * This is a rough estimate for sensible compressed buffer
477	 * sizes (between 1 and 16 bits per pixel). This could be
478	 * improved by better format specific worst case estimates.
479	 */
480	return round_up(clamp(sizeimage, width * height / 8,
481					 width * height * 2), PAGE_SIZE);
482}
483
484static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
485			struct v4l2_format *f)
486{
487	struct coda_dev *dev = ctx->dev;
488	unsigned int max_w, max_h;
489	enum v4l2_field field;
490
491	field = f->fmt.pix.field;
492	if (field == V4L2_FIELD_ANY)
493		field = V4L2_FIELD_NONE;
494	else if (V4L2_FIELD_NONE != field)
495		return -EINVAL;
496
497	/* V4L2 specification suggests the driver corrects the format struct
498	 * if any of the dimensions is unsupported */
499	f->fmt.pix.field = field;
500
501	coda_get_max_dimensions(dev, codec, &max_w, &max_h);
502	v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
503			      &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
504			      S_ALIGN);
505
506	switch (f->fmt.pix.pixelformat) {
507	case V4L2_PIX_FMT_YUV420:
508	case V4L2_PIX_FMT_YVU420:
509	case V4L2_PIX_FMT_NV12:
510		/*
511		 * Frame stride must be at least multiple of 8,
512		 * but multiple of 16 for h.264 or JPEG 4:2:x
513		 */
514		f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
515		f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
516					f->fmt.pix.height * 3 / 2;
517		break;
518	case V4L2_PIX_FMT_YUV422P:
519		f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
520		f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
521					f->fmt.pix.height * 2;
522		break;
523	case V4L2_PIX_FMT_JPEG:
524		f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
525		/* fallthrough */
526	case V4L2_PIX_FMT_H264:
527	case V4L2_PIX_FMT_MPEG4:
528		f->fmt.pix.bytesperline = 0;
529		f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
530							f->fmt.pix.sizeimage,
531							f->fmt.pix.width,
532							f->fmt.pix.height);
533		break;
534	default:
535		BUG();
536	}
537
538	return 0;
539}
540
541static int coda_try_fmt_vid_cap(struct file *file, void *priv,
542				struct v4l2_format *f)
543{
544	struct coda_ctx *ctx = fh_to_ctx(priv);
545	const struct coda_q_data *q_data_src;
546	const struct coda_codec *codec;
547	struct vb2_queue *src_vq;
548	int ret;
549
550	ret = coda_try_pixelformat(ctx, f);
551	if (ret < 0)
552		return ret;
553
554	q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
555
556	/*
557	 * If the source format is already fixed, only allow the same output
558	 * resolution
559	 */
560	src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
561	if (vb2_is_streaming(src_vq)) {
562		f->fmt.pix.width = q_data_src->width;
563		f->fmt.pix.height = q_data_src->height;
564	}
565
566	f->fmt.pix.colorspace = ctx->colorspace;
567
568	q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
569	codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
570				f->fmt.pix.pixelformat);
571	if (!codec)
572		return -EINVAL;
573
574	ret = coda_try_fmt(ctx, codec, f);
575	if (ret < 0)
576		return ret;
577
578	/* The h.264 decoder only returns complete 16x16 macroblocks */
579	if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
580		f->fmt.pix.width = f->fmt.pix.width;
581		f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
582		f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
583		f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
584				       f->fmt.pix.height * 3 / 2;
585	}
586
587	return 0;
588}
589
590static int coda_try_fmt_vid_out(struct file *file, void *priv,
591				struct v4l2_format *f)
592{
593	struct coda_ctx *ctx = fh_to_ctx(priv);
594	struct coda_dev *dev = ctx->dev;
595	const struct coda_q_data *q_data_dst;
596	const struct coda_codec *codec;
597	int ret;
598
599	ret = coda_try_pixelformat(ctx, f);
600	if (ret < 0)
601		return ret;
602
603	switch (f->fmt.pix.colorspace) {
604	case V4L2_COLORSPACE_REC709:
605	case V4L2_COLORSPACE_JPEG:
606		break;
607	default:
608		if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
609			f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
610		else
611			f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
612	}
613
614	q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
615	codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
616
617	return coda_try_fmt(ctx, codec, f);
618}
619
620static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
621{
622	struct coda_q_data *q_data;
623	struct vb2_queue *vq;
624
625	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
626	if (!vq)
627		return -EINVAL;
628
629	q_data = get_q_data(ctx, f->type);
630	if (!q_data)
631		return -EINVAL;
632
633	if (vb2_is_busy(vq)) {
634		v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
635		return -EBUSY;
636	}
637
638	q_data->fourcc = f->fmt.pix.pixelformat;
639	q_data->width = f->fmt.pix.width;
640	q_data->height = f->fmt.pix.height;
641	q_data->bytesperline = f->fmt.pix.bytesperline;
642	q_data->sizeimage = f->fmt.pix.sizeimage;
643	q_data->rect.left = 0;
644	q_data->rect.top = 0;
645	q_data->rect.width = f->fmt.pix.width;
646	q_data->rect.height = f->fmt.pix.height;
647
648	v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
649		"Setting format for type %d, wxh: %dx%d, fmt: %d\n",
650		f->type, q_data->width, q_data->height, q_data->fourcc);
651
652	return 0;
653}
654
655static int coda_s_fmt_vid_cap(struct file *file, void *priv,
656			      struct v4l2_format *f)
657{
658	struct coda_ctx *ctx = fh_to_ctx(priv);
659	int ret;
660
661	ret = coda_try_fmt_vid_cap(file, priv, f);
662	if (ret)
663		return ret;
664
665	return coda_s_fmt(ctx, f);
666}
667
668static int coda_s_fmt_vid_out(struct file *file, void *priv,
669			      struct v4l2_format *f)
670{
671	struct coda_ctx *ctx = fh_to_ctx(priv);
672	struct v4l2_format f_cap;
673	int ret;
674
675	ret = coda_try_fmt_vid_out(file, priv, f);
676	if (ret)
677		return ret;
678
679	ret = coda_s_fmt(ctx, f);
680	if (ret)
681		return ret;
682
683	ctx->colorspace = f->fmt.pix.colorspace;
684
685	memset(&f_cap, 0, sizeof(f_cap));
686	f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
687	coda_g_fmt(file, priv, &f_cap);
688	f_cap.fmt.pix.width = f->fmt.pix.width;
689	f_cap.fmt.pix.height = f->fmt.pix.height;
690
691	ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
692	if (ret)
693		return ret;
694
695	return coda_s_fmt(ctx, &f_cap);
696}
697
698static int coda_reqbufs(struct file *file, void *priv,
699			struct v4l2_requestbuffers *rb)
700{
701	struct coda_ctx *ctx = fh_to_ctx(priv);
702	int ret;
703
704	ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
705	if (ret)
706		return ret;
707
708	/*
709	 * Allow to allocate instance specific per-context buffers, such as
710	 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
711	 */
712	if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
713		return ctx->ops->reqbufs(ctx, rb);
714
715	return 0;
716}
717
718static int coda_qbuf(struct file *file, void *priv,
719		     struct v4l2_buffer *buf)
720{
721	struct coda_ctx *ctx = fh_to_ctx(priv);
722
723	return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
724}
725
726static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
727				      struct v4l2_buffer *buf)
728{
729	struct vb2_queue *src_vq;
730
731	src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
732
733	return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
734		(buf->sequence == (ctx->qsequence - 1)));
735}
736
737static int coda_dqbuf(struct file *file, void *priv,
738		      struct v4l2_buffer *buf)
739{
740	struct coda_ctx *ctx = fh_to_ctx(priv);
741	int ret;
742
743	ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
744
745	/* If this is the last capture buffer, emit an end-of-stream event */
746	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
747	    coda_buf_is_end_of_stream(ctx, buf)) {
748		const struct v4l2_event eos_event = {
749			.type = V4L2_EVENT_EOS
750		};
751
752		v4l2_event_queue_fh(&ctx->fh, &eos_event);
753	}
754
755	return ret;
756}
757
758static int coda_g_selection(struct file *file, void *fh,
759			    struct v4l2_selection *s)
760{
761	struct coda_ctx *ctx = fh_to_ctx(fh);
762	struct coda_q_data *q_data;
763	struct v4l2_rect r, *rsel;
764
765	q_data = get_q_data(ctx, s->type);
766	if (!q_data)
767		return -EINVAL;
768
769	r.left = 0;
770	r.top = 0;
771	r.width = q_data->width;
772	r.height = q_data->height;
773	rsel = &q_data->rect;
774
775	switch (s->target) {
776	case V4L2_SEL_TGT_CROP_DEFAULT:
777	case V4L2_SEL_TGT_CROP_BOUNDS:
778		rsel = &r;
779		/* fallthrough */
780	case V4L2_SEL_TGT_CROP:
781		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
782			return -EINVAL;
783		break;
784	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
785	case V4L2_SEL_TGT_COMPOSE_PADDED:
786		rsel = &r;
787		/* fallthrough */
788	case V4L2_SEL_TGT_COMPOSE:
789	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
790		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
791			return -EINVAL;
792		break;
793	default:
794		return -EINVAL;
795	}
796
797	s->r = *rsel;
798
799	return 0;
800}
801
802static int coda_try_decoder_cmd(struct file *file, void *fh,
803				struct v4l2_decoder_cmd *dc)
804{
805	if (dc->cmd != V4L2_DEC_CMD_STOP)
806		return -EINVAL;
807
808	if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
809		return -EINVAL;
810
811	if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
812		return -EINVAL;
813
814	return 0;
815}
816
817static int coda_decoder_cmd(struct file *file, void *fh,
818			    struct v4l2_decoder_cmd *dc)
819{
820	struct coda_ctx *ctx = fh_to_ctx(fh);
821	int ret;
822
823	ret = coda_try_decoder_cmd(file, fh, dc);
824	if (ret < 0)
825		return ret;
826
827	/* Ignore decoder stop command silently in encoder context */
828	if (ctx->inst_type != CODA_INST_DECODER)
829		return 0;
830
831	/* Set the stream-end flag on this context */
832	coda_bit_stream_end_flag(ctx);
833	ctx->hold = false;
834	v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
835
836	return 0;
837}
838
839static int coda_subscribe_event(struct v4l2_fh *fh,
840				const struct v4l2_event_subscription *sub)
841{
842	switch (sub->type) {
843	case V4L2_EVENT_EOS:
844		return v4l2_event_subscribe(fh, sub, 0, NULL);
845	default:
846		return v4l2_ctrl_subscribe_event(fh, sub);
847	}
848}
849
850static const struct v4l2_ioctl_ops coda_ioctl_ops = {
851	.vidioc_querycap	= coda_querycap,
852
853	.vidioc_enum_fmt_vid_cap = coda_enum_fmt,
854	.vidioc_g_fmt_vid_cap	= coda_g_fmt,
855	.vidioc_try_fmt_vid_cap	= coda_try_fmt_vid_cap,
856	.vidioc_s_fmt_vid_cap	= coda_s_fmt_vid_cap,
857
858	.vidioc_enum_fmt_vid_out = coda_enum_fmt,
859	.vidioc_g_fmt_vid_out	= coda_g_fmt,
860	.vidioc_try_fmt_vid_out	= coda_try_fmt_vid_out,
861	.vidioc_s_fmt_vid_out	= coda_s_fmt_vid_out,
862
863	.vidioc_reqbufs		= coda_reqbufs,
864	.vidioc_querybuf	= v4l2_m2m_ioctl_querybuf,
865
866	.vidioc_qbuf		= coda_qbuf,
867	.vidioc_expbuf		= v4l2_m2m_ioctl_expbuf,
868	.vidioc_dqbuf		= coda_dqbuf,
869	.vidioc_create_bufs	= v4l2_m2m_ioctl_create_bufs,
870
871	.vidioc_streamon	= v4l2_m2m_ioctl_streamon,
872	.vidioc_streamoff	= v4l2_m2m_ioctl_streamoff,
873
874	.vidioc_g_selection	= coda_g_selection,
875
876	.vidioc_try_decoder_cmd	= coda_try_decoder_cmd,
877	.vidioc_decoder_cmd	= coda_decoder_cmd,
878
879	.vidioc_subscribe_event = coda_subscribe_event,
880	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
881};
882
883void coda_set_gdi_regs(struct coda_ctx *ctx)
884{
885	struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
886	struct coda_dev *dev = ctx->dev;
887	int i;
888
889	for (i = 0; i < 16; i++)
890		coda_write(dev, tiled_map->xy2ca_map[i],
891				CODA9_GDI_XY2_CAS_0 + 4 * i);
892	for (i = 0; i < 4; i++)
893		coda_write(dev, tiled_map->xy2ba_map[i],
894				CODA9_GDI_XY2_BA_0 + 4 * i);
895	for (i = 0; i < 16; i++)
896		coda_write(dev, tiled_map->xy2ra_map[i],
897				CODA9_GDI_XY2_RAS_0 + 4 * i);
898	coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
899	for (i = 0; i < 32; i++)
900		coda_write(dev, tiled_map->rbc2axi_map[i],
901				CODA9_GDI_RBC2_AXI_0 + 4 * i);
902}
903
904/*
905 * Mem-to-mem operations.
906 */
907
908static void coda_device_run(void *m2m_priv)
909{
910	struct coda_ctx *ctx = m2m_priv;
911	struct coda_dev *dev = ctx->dev;
912
913	queue_work(dev->workqueue, &ctx->pic_run_work);
914}
915
916static void coda_pic_run_work(struct work_struct *work)
917{
918	struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
919	struct coda_dev *dev = ctx->dev;
920	int ret;
921
922	mutex_lock(&ctx->buffer_mutex);
923	mutex_lock(&dev->coda_mutex);
924
925	ret = ctx->ops->prepare_run(ctx);
926	if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
927		mutex_unlock(&dev->coda_mutex);
928		mutex_unlock(&ctx->buffer_mutex);
929		/* job_finish scheduled by prepare_decode */
930		return;
931	}
932
933	if (!wait_for_completion_timeout(&ctx->completion,
934					 msecs_to_jiffies(1000))) {
935		dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
936
937		ctx->hold = true;
938
939		coda_hw_reset(ctx);
940	} else if (!ctx->aborting) {
941		ctx->ops->finish_run(ctx);
942	}
943
944	if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
945	    ctx->ops->seq_end_work)
946		queue_work(dev->workqueue, &ctx->seq_end_work);
947
948	mutex_unlock(&dev->coda_mutex);
949	mutex_unlock(&ctx->buffer_mutex);
950
951	v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
952}
953
954static int coda_job_ready(void *m2m_priv)
955{
956	struct coda_ctx *ctx = m2m_priv;
957
958	/*
959	 * For both 'P' and 'key' frame cases 1 picture
960	 * and 1 frame are needed. In the decoder case,
961	 * the compressed frame can be in the bitstream.
962	 */
963	if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
964	    ctx->inst_type != CODA_INST_DECODER) {
965		v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
966			 "not ready: not enough video buffers.\n");
967		return 0;
968	}
969
970	if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
971		v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
972			 "not ready: not enough video capture buffers.\n");
973		return 0;
974	}
975
976	if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
977		struct list_head *meta;
978		bool stream_end;
979		int num_metas;
980		int src_bufs;
981
982		if (ctx->hold && !v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx)) {
983			v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
984				 "%d: not ready: on hold for more buffers.\n",
985				 ctx->idx);
986			return 0;
987		}
988
989		stream_end = ctx->bit_stream_param &
990			     CODA_BIT_STREAM_END_FLAG;
991
992		num_metas = 0;
993		list_for_each(meta, &ctx->buffer_meta_list)
994			num_metas++;
995
996		src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
997
998		if (!stream_end && (num_metas + src_bufs) < 2) {
999			v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1000				 "%d: not ready: need 2 buffers available (%d, %d)\n",
1001				 ctx->idx, num_metas, src_bufs);
1002			return 0;
1003		}
1004
1005
1006		if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
1007		    !stream_end && (coda_get_bitstream_payload(ctx) < 512)) {
1008			v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1009				 "%d: not ready: not enough bitstream data (%d).\n",
1010				 ctx->idx, coda_get_bitstream_payload(ctx));
1011			return 0;
1012		}
1013	}
1014
1015	if (ctx->aborting) {
1016		v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1017			 "not ready: aborting\n");
1018		return 0;
1019	}
1020
1021	v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1022			"job ready\n");
1023	return 1;
1024}
1025
1026static void coda_job_abort(void *priv)
1027{
1028	struct coda_ctx *ctx = priv;
1029
1030	ctx->aborting = 1;
1031
1032	v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1033		 "Aborting task\n");
1034}
1035
1036static void coda_lock(void *m2m_priv)
1037{
1038	struct coda_ctx *ctx = m2m_priv;
1039	struct coda_dev *pcdev = ctx->dev;
1040
1041	mutex_lock(&pcdev->dev_mutex);
1042}
1043
1044static void coda_unlock(void *m2m_priv)
1045{
1046	struct coda_ctx *ctx = m2m_priv;
1047	struct coda_dev *pcdev = ctx->dev;
1048
1049	mutex_unlock(&pcdev->dev_mutex);
1050}
1051
1052static const struct v4l2_m2m_ops coda_m2m_ops = {
1053	.device_run	= coda_device_run,
1054	.job_ready	= coda_job_ready,
1055	.job_abort	= coda_job_abort,
1056	.lock		= coda_lock,
1057	.unlock		= coda_unlock,
1058};
1059
1060static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1061{
1062	struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1063	int luma_map, chro_map, i;
1064
1065	memset(tiled_map, 0, sizeof(*tiled_map));
1066
1067	luma_map = 64;
1068	chro_map = 64;
1069	tiled_map->map_type = tiled_map_type;
1070	for (i = 0; i < 16; i++)
1071		tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1072	for (i = 0; i < 4; i++)
1073		tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1074	for (i = 0; i < 16; i++)
1075		tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1076
1077	if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1078		tiled_map->xy2rbc_config = 0;
1079	} else {
1080		dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1081			tiled_map_type);
1082		return;
1083	}
1084}
1085
1086static void set_default_params(struct coda_ctx *ctx)
1087{
1088	unsigned int max_w, max_h, usize, csize;
1089
1090	ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1091				     ctx->cvd->dst_formats[0]);
1092	max_w = min(ctx->codec->max_w, 1920U);
1093	max_h = min(ctx->codec->max_h, 1088U);
1094	usize = max_w * max_h * 3 / 2;
1095	csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
1096
1097	ctx->params.codec_mode = ctx->codec->mode;
1098	ctx->colorspace = V4L2_COLORSPACE_REC709;
1099	ctx->params.framerate = 30;
1100
1101	/* Default formats for output and input queues */
1102	ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1103	ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1104	ctx->q_data[V4L2_M2M_SRC].width = max_w;
1105	ctx->q_data[V4L2_M2M_SRC].height = max_h;
1106	ctx->q_data[V4L2_M2M_DST].width = max_w;
1107	ctx->q_data[V4L2_M2M_DST].height = max_h;
1108	if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1109		ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1110		ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
1111		ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1112		ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
1113	} else {
1114		ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1115		ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
1116		ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1117		ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
1118	}
1119	ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1120	ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1121	ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1122	ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
1123
1124	if (ctx->dev->devtype->product == CODA_960)
1125		coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
1126}
1127
1128/*
1129 * Queue operations
1130 */
1131static int coda_queue_setup(struct vb2_queue *vq,
1132				const struct v4l2_format *fmt,
1133				unsigned int *nbuffers, unsigned int *nplanes,
1134				unsigned int sizes[], void *alloc_ctxs[])
1135{
1136	struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1137	struct coda_q_data *q_data;
1138	unsigned int size;
1139
1140	q_data = get_q_data(ctx, vq->type);
1141	size = q_data->sizeimage;
1142
1143	*nplanes = 1;
1144	sizes[0] = size;
1145
1146	/* Set to vb2-dma-contig allocator context, ignored by vb2-vmalloc */
1147	alloc_ctxs[0] = ctx->dev->alloc_ctx;
1148
1149	v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1150		 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1151
1152	return 0;
1153}
1154
1155static int coda_buf_prepare(struct vb2_buffer *vb)
1156{
1157	struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1158	struct coda_q_data *q_data;
1159
1160	q_data = get_q_data(ctx, vb->vb2_queue->type);
1161
1162	if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1163		v4l2_warn(&ctx->dev->v4l2_dev,
1164			  "%s data will not fit into plane (%lu < %lu)\n",
1165			  __func__, vb2_plane_size(vb, 0),
1166			  (long)q_data->sizeimage);
1167		return -EINVAL;
1168	}
1169
1170	return 0;
1171}
1172
1173static void coda_buf_queue(struct vb2_buffer *vb)
1174{
1175	struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1176	struct vb2_queue *vq = vb->vb2_queue;
1177	struct coda_q_data *q_data;
1178
1179	q_data = get_q_data(ctx, vb->vb2_queue->type);
1180
1181	/*
1182	 * In the decoder case, immediately try to copy the buffer into the
1183	 * bitstream ringbuffer and mark it as ready to be dequeued.
1184	 */
1185	if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1186		/*
1187		 * For backwards compatibility, queuing an empty buffer marks
1188		 * the stream end
1189		 */
1190		if (vb2_get_plane_payload(vb, 0) == 0)
1191			coda_bit_stream_end_flag(ctx);
1192		mutex_lock(&ctx->bitstream_mutex);
1193		v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
1194		if (vb2_is_streaming(vb->vb2_queue))
1195			coda_fill_bitstream(ctx, true);
1196		mutex_unlock(&ctx->bitstream_mutex);
1197	} else {
1198		v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
1199	}
1200}
1201
1202int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1203		       size_t size, const char *name, struct dentry *parent)
1204{
1205	buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1206					GFP_KERNEL);
1207	if (!buf->vaddr) {
1208		v4l2_err(&dev->v4l2_dev,
1209			 "Failed to allocate %s buffer of size %u\n",
1210			 name, size);
1211		return -ENOMEM;
1212	}
1213
1214	buf->size = size;
1215
1216	if (name && parent) {
1217		buf->blob.data = buf->vaddr;
1218		buf->blob.size = size;
1219		buf->dentry = debugfs_create_blob(name, 0644, parent,
1220						  &buf->blob);
1221		if (!buf->dentry)
1222			dev_warn(&dev->plat_dev->dev,
1223				 "failed to create debugfs entry %s\n", name);
1224	}
1225
1226	return 0;
1227}
1228
1229void coda_free_aux_buf(struct coda_dev *dev,
1230		       struct coda_aux_buf *buf)
1231{
1232	if (buf->vaddr) {
1233		dma_free_coherent(&dev->plat_dev->dev, buf->size,
1234				  buf->vaddr, buf->paddr);
1235		buf->vaddr = NULL;
1236		buf->size = 0;
1237		debugfs_remove(buf->dentry);
1238		buf->dentry = NULL;
1239	}
1240}
1241
1242static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1243{
1244	struct coda_ctx *ctx = vb2_get_drv_priv(q);
1245	struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1246	struct coda_q_data *q_data_src, *q_data_dst;
1247	struct vb2_buffer *buf;
1248	int ret = 0;
1249
1250	q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1251	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1252		if (q_data_src->fourcc == V4L2_PIX_FMT_H264 ||
1253		    (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
1254		     ctx->dev->devtype->product == CODA_7541)) {
1255			/* copy the buffers that were queued before streamon */
1256			mutex_lock(&ctx->bitstream_mutex);
1257			coda_fill_bitstream(ctx, false);
1258			mutex_unlock(&ctx->bitstream_mutex);
1259
1260			if (coda_get_bitstream_payload(ctx) < 512) {
1261				ret = -EINVAL;
1262				goto err;
1263			}
1264		} else {
1265			if (count < 1) {
1266				ret = -EINVAL;
1267				goto err;
1268			}
1269		}
1270
1271		ctx->streamon_out = 1;
1272	} else {
1273		if (count < 1) {
1274			ret = -EINVAL;
1275			goto err;
1276		}
1277
1278		ctx->streamon_cap = 1;
1279	}
1280
1281	/* Don't start the coda unless both queues are on */
1282	if (!(ctx->streamon_out & ctx->streamon_cap))
1283		return 0;
1284
1285	q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1286	if ((q_data_src->width != q_data_dst->width &&
1287	     round_up(q_data_src->width, 16) != q_data_dst->width) ||
1288	    (q_data_src->height != q_data_dst->height &&
1289	     round_up(q_data_src->height, 16) != q_data_dst->height)) {
1290		v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
1291			 q_data_src->width, q_data_src->height,
1292			 q_data_dst->width, q_data_dst->height);
1293		ret = -EINVAL;
1294		goto err;
1295	}
1296
1297	/* Allow BIT decoder device_run with no new buffers queued */
1298	if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
1299		v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
1300
1301	ctx->gopcounter = ctx->params.gop_size - 1;
1302
1303	ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1304				     q_data_dst->fourcc);
1305	if (!ctx->codec) {
1306		v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1307		ret = -EINVAL;
1308		goto err;
1309	}
1310
1311	if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1312		ctx->params.gop_size = 1;
1313	ctx->gopcounter = ctx->params.gop_size - 1;
1314
1315	ret = ctx->ops->start_streaming(ctx);
1316	if (ctx->inst_type == CODA_INST_DECODER) {
1317		if (ret == -EAGAIN)
1318			return 0;
1319		else if (ret < 0)
1320			goto err;
1321	}
1322
1323	ctx->initialized = 1;
1324	return ret;
1325
1326err:
1327	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1328		while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1329			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1330	} else {
1331		while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1332			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1333	}
1334	return ret;
1335}
1336
1337static void coda_stop_streaming(struct vb2_queue *q)
1338{
1339	struct coda_ctx *ctx = vb2_get_drv_priv(q);
1340	struct coda_dev *dev = ctx->dev;
1341	struct vb2_buffer *buf;
1342	bool stop;
1343
1344	stop = ctx->streamon_out && ctx->streamon_cap;
1345
1346	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1347		v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1348			 "%s: output\n", __func__);
1349		ctx->streamon_out = 0;
1350
1351		coda_bit_stream_end_flag(ctx);
1352
1353		ctx->qsequence = 0;
1354
1355		while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1356			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1357	} else {
1358		v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1359			 "%s: capture\n", __func__);
1360		ctx->streamon_cap = 0;
1361
1362		ctx->osequence = 0;
1363		ctx->sequence_offset = 0;
1364
1365		while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1366			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1367	}
1368
1369	if (stop) {
1370		struct coda_buffer_meta *meta;
1371
1372		if (ctx->ops->seq_end_work) {
1373			queue_work(dev->workqueue, &ctx->seq_end_work);
1374			flush_work(&ctx->seq_end_work);
1375		}
1376		mutex_lock(&ctx->bitstream_mutex);
1377		while (!list_empty(&ctx->buffer_meta_list)) {
1378			meta = list_first_entry(&ctx->buffer_meta_list,
1379						struct coda_buffer_meta, list);
1380			list_del(&meta->list);
1381			kfree(meta);
1382		}
1383		mutex_unlock(&ctx->bitstream_mutex);
1384		kfifo_init(&ctx->bitstream_fifo,
1385			ctx->bitstream.vaddr, ctx->bitstream.size);
1386		ctx->initialized = 0;
1387		ctx->runcounter = 0;
1388		ctx->aborting = 0;
1389	}
1390}
1391
1392static const struct vb2_ops coda_qops = {
1393	.queue_setup		= coda_queue_setup,
1394	.buf_prepare		= coda_buf_prepare,
1395	.buf_queue		= coda_buf_queue,
1396	.start_streaming	= coda_start_streaming,
1397	.stop_streaming		= coda_stop_streaming,
1398	.wait_prepare		= vb2_ops_wait_prepare,
1399	.wait_finish		= vb2_ops_wait_finish,
1400};
1401
1402static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1403{
1404	struct coda_ctx *ctx =
1405			container_of(ctrl->handler, struct coda_ctx, ctrls);
1406
1407	v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1408		 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1409
1410	switch (ctrl->id) {
1411	case V4L2_CID_HFLIP:
1412		if (ctrl->val)
1413			ctx->params.rot_mode |= CODA_MIR_HOR;
1414		else
1415			ctx->params.rot_mode &= ~CODA_MIR_HOR;
1416		break;
1417	case V4L2_CID_VFLIP:
1418		if (ctrl->val)
1419			ctx->params.rot_mode |= CODA_MIR_VER;
1420		else
1421			ctx->params.rot_mode &= ~CODA_MIR_VER;
1422		break;
1423	case V4L2_CID_MPEG_VIDEO_BITRATE:
1424		ctx->params.bitrate = ctrl->val / 1000;
1425		break;
1426	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1427		ctx->params.gop_size = ctrl->val;
1428		break;
1429	case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1430		ctx->params.h264_intra_qp = ctrl->val;
1431		break;
1432	case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1433		ctx->params.h264_inter_qp = ctrl->val;
1434		break;
1435	case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1436		ctx->params.h264_min_qp = ctrl->val;
1437		break;
1438	case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1439		ctx->params.h264_max_qp = ctrl->val;
1440		break;
1441	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1442		ctx->params.h264_deblk_alpha = ctrl->val;
1443		break;
1444	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1445		ctx->params.h264_deblk_beta = ctrl->val;
1446		break;
1447	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1448		ctx->params.h264_deblk_enabled = (ctrl->val ==
1449				V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1450		break;
1451	case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1452		ctx->params.mpeg4_intra_qp = ctrl->val;
1453		break;
1454	case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1455		ctx->params.mpeg4_inter_qp = ctrl->val;
1456		break;
1457	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1458		ctx->params.slice_mode = ctrl->val;
1459		break;
1460	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1461		ctx->params.slice_max_mb = ctrl->val;
1462		break;
1463	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1464		ctx->params.slice_max_bits = ctrl->val * 8;
1465		break;
1466	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1467		break;
1468	case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1469		ctx->params.intra_refresh = ctrl->val;
1470		break;
1471	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1472		coda_set_jpeg_compression_quality(ctx, ctrl->val);
1473		break;
1474	case V4L2_CID_JPEG_RESTART_INTERVAL:
1475		ctx->params.jpeg_restart_interval = ctrl->val;
1476		break;
1477	default:
1478		v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1479			"Invalid control, id=%d, val=%d\n",
1480			ctrl->id, ctrl->val);
1481		return -EINVAL;
1482	}
1483
1484	return 0;
1485}
1486
1487static const struct v4l2_ctrl_ops coda_ctrl_ops = {
1488	.s_ctrl = coda_s_ctrl,
1489};
1490
1491static void coda_encode_ctrls(struct coda_ctx *ctx)
1492{
1493	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1494		V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
1495	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1496		V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1497	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1498		V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
1499	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1500		V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
1501	if (ctx->dev->devtype->product != CODA_960) {
1502		v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1503			V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1504	}
1505	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1506		V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
1507	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1508		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1509	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1510		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1511	v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1512		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1513		V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1514		V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1515	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1516		V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1517	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1518		V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1519	v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1520		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
1521		V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1522		V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
1523	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1524		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
1525	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1526		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1527		500);
1528	v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1529		V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1530		V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1531		(1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1532		V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1533	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1534		V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1535		1920 * 1088 / 256, 1, 0);
1536}
1537
1538static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1539{
1540	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1541		V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1542	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1543		V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1544}
1545
1546static int coda_ctrls_setup(struct coda_ctx *ctx)
1547{
1548	v4l2_ctrl_handler_init(&ctx->ctrls, 2);
1549
1550	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1551		V4L2_CID_HFLIP, 0, 1, 1, 0);
1552	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1553		V4L2_CID_VFLIP, 0, 1, 1, 0);
1554	if (ctx->inst_type == CODA_INST_ENCODER) {
1555		if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
1556			coda_jpeg_encode_ctrls(ctx);
1557		else
1558			coda_encode_ctrls(ctx);
1559	}
1560
1561	if (ctx->ctrls.error) {
1562		v4l2_err(&ctx->dev->v4l2_dev,
1563			"control initialization error (%d)",
1564			ctx->ctrls.error);
1565		return -EINVAL;
1566	}
1567
1568	return v4l2_ctrl_handler_setup(&ctx->ctrls);
1569}
1570
1571static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
1572{
1573	vq->drv_priv = ctx;
1574	vq->ops = &coda_qops;
1575	vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1576	vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1577	vq->lock = &ctx->dev->dev_mutex;
1578	/* One way to indicate end-of-stream for coda is to set the
1579	 * bytesused == 0. However by default videobuf2 handles bytesused
1580	 * equal to 0 as a special case and changes its value to the size
1581	 * of the buffer. Set the allow_zero_bytesused flag, so
1582	 * that videobuf2 will keep the value of bytesused intact.
1583	 */
1584	vq->allow_zero_bytesused = 1;
1585
1586	return vb2_queue_init(vq);
1587}
1588
1589int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1590			    struct vb2_queue *dst_vq)
1591{
1592	int ret;
1593
1594	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1595	src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1596	src_vq->mem_ops = &vb2_dma_contig_memops;
1597
1598	ret = coda_queue_init(priv, src_vq);
1599	if (ret)
1600		return ret;
1601
1602	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1603	dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1604	dst_vq->mem_ops = &vb2_dma_contig_memops;
1605
1606	return coda_queue_init(priv, dst_vq);
1607}
1608
1609int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1610			    struct vb2_queue *dst_vq)
1611{
1612	int ret;
1613
1614	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1615	src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1616	src_vq->mem_ops = &vb2_vmalloc_memops;
1617
1618	ret = coda_queue_init(priv, src_vq);
1619	if (ret)
1620		return ret;
1621
1622	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1623	dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1624	dst_vq->mem_ops = &vb2_dma_contig_memops;
1625
1626	return coda_queue_init(priv, dst_vq);
1627}
1628
1629static int coda_next_free_instance(struct coda_dev *dev)
1630{
1631	int idx = ffz(dev->instance_mask);
1632
1633	if ((idx < 0) ||
1634	    (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1635		return -EBUSY;
1636
1637	return idx;
1638}
1639
1640/*
1641 * File operations
1642 */
1643
1644static int coda_open(struct file *file)
1645{
1646	struct video_device *vdev = video_devdata(file);
1647	struct coda_dev *dev = video_get_drvdata(vdev);
1648	struct coda_ctx *ctx = NULL;
1649	char *name;
1650	int ret;
1651	int idx;
1652
1653	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1654	if (!ctx)
1655		return -ENOMEM;
1656
1657	idx = coda_next_free_instance(dev);
1658	if (idx < 0) {
1659		ret = idx;
1660		goto err_coda_max;
1661	}
1662	set_bit(idx, &dev->instance_mask);
1663
1664	name = kasprintf(GFP_KERNEL, "context%d", idx);
1665	if (!name) {
1666		ret = -ENOMEM;
1667		goto err_coda_name_init;
1668	}
1669
1670	ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1671	kfree(name);
1672
1673	ctx->cvd = to_coda_video_device(vdev);
1674	ctx->inst_type = ctx->cvd->type;
1675	ctx->ops = ctx->cvd->ops;
1676	ctx->use_bit = !ctx->cvd->direct;
1677	init_completion(&ctx->completion);
1678	INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
1679	if (ctx->ops->seq_end_work)
1680		INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
1681	v4l2_fh_init(&ctx->fh, video_devdata(file));
1682	file->private_data = &ctx->fh;
1683	v4l2_fh_add(&ctx->fh);
1684	ctx->dev = dev;
1685	ctx->idx = idx;
1686	switch (dev->devtype->product) {
1687	case CODA_960:
1688		ctx->frame_mem_ctrl = 1 << 12;
1689		/* fallthrough */
1690	case CODA_7541:
1691		ctx->reg_idx = 0;
1692		break;
1693	default:
1694		ctx->reg_idx = idx;
1695	}
1696
1697	/* Power up and upload firmware if necessary */
1698	ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1699	if (ret < 0) {
1700		v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1701		goto err_pm_get;
1702	}
1703
1704	ret = clk_prepare_enable(dev->clk_per);
1705	if (ret)
1706		goto err_clk_per;
1707
1708	ret = clk_prepare_enable(dev->clk_ahb);
1709	if (ret)
1710		goto err_clk_ahb;
1711
1712	set_default_params(ctx);
1713	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1714					    ctx->ops->queue_init);
1715	if (IS_ERR(ctx->fh.m2m_ctx)) {
1716		ret = PTR_ERR(ctx->fh.m2m_ctx);
1717
1718		v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1719			 __func__, ret);
1720		goto err_ctx_init;
1721	}
1722
1723	ret = coda_ctrls_setup(ctx);
1724	if (ret) {
1725		v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
1726		goto err_ctrls_setup;
1727	}
1728
1729	ctx->fh.ctrl_handler = &ctx->ctrls;
1730
1731	mutex_init(&ctx->bitstream_mutex);
1732	mutex_init(&ctx->buffer_mutex);
1733	INIT_LIST_HEAD(&ctx->buffer_meta_list);
1734
1735	coda_lock(ctx);
1736	list_add(&ctx->list, &dev->instances);
1737	coda_unlock(ctx);
1738
1739	v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1740		 ctx->idx, ctx);
1741
1742	return 0;
1743
1744err_ctrls_setup:
1745	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1746err_ctx_init:
1747	clk_disable_unprepare(dev->clk_ahb);
1748err_clk_ahb:
1749	clk_disable_unprepare(dev->clk_per);
1750err_clk_per:
1751	pm_runtime_put_sync(&dev->plat_dev->dev);
1752err_pm_get:
1753	v4l2_fh_del(&ctx->fh);
1754	v4l2_fh_exit(&ctx->fh);
1755	clear_bit(ctx->idx, &dev->instance_mask);
1756err_coda_name_init:
1757err_coda_max:
1758	kfree(ctx);
1759	return ret;
1760}
1761
1762static int coda_release(struct file *file)
1763{
1764	struct coda_dev *dev = video_drvdata(file);
1765	struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1766
1767	v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1768		 ctx);
1769
1770	if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
1771		coda_bit_stream_end_flag(ctx);
1772
1773	/* If this instance is running, call .job_abort and wait for it to end */
1774	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1775
1776	/* In case the instance was not running, we still need to call SEQ_END */
1777	if (ctx->initialized && ctx->ops->seq_end_work) {
1778		queue_work(dev->workqueue, &ctx->seq_end_work);
1779		flush_work(&ctx->seq_end_work);
1780	}
1781
1782	coda_lock(ctx);
1783	list_del(&ctx->list);
1784	coda_unlock(ctx);
1785
1786	if (ctx->dev->devtype->product == CODA_DX6)
1787		coda_free_aux_buf(dev, &ctx->workbuf);
1788
1789	v4l2_ctrl_handler_free(&ctx->ctrls);
1790	clk_disable_unprepare(dev->clk_ahb);
1791	clk_disable_unprepare(dev->clk_per);
1792	pm_runtime_put_sync(&dev->plat_dev->dev);
1793	v4l2_fh_del(&ctx->fh);
1794	v4l2_fh_exit(&ctx->fh);
1795	clear_bit(ctx->idx, &dev->instance_mask);
1796	if (ctx->ops->release)
1797		ctx->ops->release(ctx);
1798	debugfs_remove_recursive(ctx->debugfs_entry);
1799	kfree(ctx);
1800
1801	return 0;
1802}
1803
1804static const struct v4l2_file_operations coda_fops = {
1805	.owner		= THIS_MODULE,
1806	.open		= coda_open,
1807	.release	= coda_release,
1808	.poll		= v4l2_m2m_fop_poll,
1809	.unlocked_ioctl	= video_ioctl2,
1810	.mmap		= v4l2_m2m_fop_mmap,
1811};
1812
1813static int coda_hw_init(struct coda_dev *dev)
1814{
1815	u32 data;
1816	u16 *p;
1817	int i, ret;
1818
1819	ret = clk_prepare_enable(dev->clk_per);
1820	if (ret)
1821		goto err_clk_per;
1822
1823	ret = clk_prepare_enable(dev->clk_ahb);
1824	if (ret)
1825		goto err_clk_ahb;
1826
1827	if (dev->rstc)
1828		reset_control_reset(dev->rstc);
1829
1830	/*
1831	 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
1832	 * The 16-bit chars in the code buffer are in memory access
1833	 * order, re-sort them to CODA order for register download.
1834	 * Data in this SRAM survives a reboot.
1835	 */
1836	p = (u16 *)dev->codebuf.vaddr;
1837	if (dev->devtype->product == CODA_DX6) {
1838		for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++)  {
1839			data = CODA_DOWN_ADDRESS_SET(i) |
1840				CODA_DOWN_DATA_SET(p[i ^ 1]);
1841			coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1842		}
1843	} else {
1844		for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1845			data = CODA_DOWN_ADDRESS_SET(i) |
1846				CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1847							3 - (i % 4)]);
1848			coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1849		}
1850	}
1851
1852	/* Clear registers */
1853	for (i = 0; i < 64; i++)
1854		coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1855
1856	/* Tell the BIT where to find everything it needs */
1857	if (dev->devtype->product == CODA_960 ||
1858	    dev->devtype->product == CODA_7541) {
1859		coda_write(dev, dev->tempbuf.paddr,
1860				CODA_REG_BIT_TEMP_BUF_ADDR);
1861		coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1862	} else {
1863		coda_write(dev, dev->workbuf.paddr,
1864			      CODA_REG_BIT_WORK_BUF_ADDR);
1865	}
1866	coda_write(dev, dev->codebuf.paddr,
1867		      CODA_REG_BIT_CODE_BUF_ADDR);
1868	coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1869
1870	/* Set default values */
1871	switch (dev->devtype->product) {
1872	case CODA_DX6:
1873		coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
1874			   CODA_REG_BIT_STREAM_CTRL);
1875		break;
1876	default:
1877		coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
1878			   CODA_REG_BIT_STREAM_CTRL);
1879	}
1880	if (dev->devtype->product == CODA_960)
1881		coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
1882	else
1883		coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
1884
1885	if (dev->devtype->product != CODA_DX6)
1886		coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1887
1888	coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1889		      CODA_REG_BIT_INT_ENABLE);
1890
1891	/* Reset VPU and start processor */
1892	data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1893	data |= CODA_REG_RESET_ENABLE;
1894	coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1895	udelay(10);
1896	data &= ~CODA_REG_RESET_ENABLE;
1897	coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1898	coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1899
1900	clk_disable_unprepare(dev->clk_ahb);
1901	clk_disable_unprepare(dev->clk_per);
1902
1903	return 0;
1904
1905err_clk_ahb:
1906	clk_disable_unprepare(dev->clk_per);
1907err_clk_per:
1908	return ret;
1909}
1910
1911static int coda_register_device(struct coda_dev *dev, int i)
1912{
1913	struct video_device *vfd = &dev->vfd[i];
1914
1915	if (i >= dev->devtype->num_vdevs)
1916		return -EINVAL;
1917
1918	strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
1919	vfd->fops	= &coda_fops;
1920	vfd->ioctl_ops	= &coda_ioctl_ops;
1921	vfd->release	= video_device_release_empty,
1922	vfd->lock	= &dev->dev_mutex;
1923	vfd->v4l2_dev	= &dev->v4l2_dev;
1924	vfd->vfl_dir	= VFL_DIR_M2M;
1925	video_set_drvdata(vfd, dev);
1926
1927	/* Not applicable, use the selection API instead */
1928	v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
1929	v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
1930	v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
1931
1932	return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1933}
1934
1935static void coda_fw_callback(const struct firmware *fw, void *context)
1936{
1937	struct coda_dev *dev = context;
1938	struct platform_device *pdev = dev->plat_dev;
1939	int i, ret;
1940
1941	if (!fw) {
1942		v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
1943		goto put_pm;
1944	}
1945
1946	/* allocate auxiliary per-device code buffer for the BIT processor */
1947	ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
1948				 dev->debugfs_root);
1949	if (ret < 0)
1950		goto put_pm;
1951
1952	/* Copy the whole firmware image to the code buffer */
1953	memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1954	release_firmware(fw);
1955
1956	ret = coda_hw_init(dev);
1957	if (ret < 0) {
1958		v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1959		goto put_pm;
1960	}
1961
1962	ret = coda_check_firmware(dev);
1963	if (ret < 0)
1964		goto put_pm;
1965
1966	dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1967	if (IS_ERR(dev->alloc_ctx)) {
1968		v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
1969		goto put_pm;
1970	}
1971
1972	dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1973	if (IS_ERR(dev->m2m_dev)) {
1974		v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1975		goto rel_ctx;
1976	}
1977
1978	for (i = 0; i < dev->devtype->num_vdevs; i++) {
1979		ret = coda_register_device(dev, i);
1980		if (ret) {
1981			v4l2_err(&dev->v4l2_dev,
1982				 "Failed to register %s video device: %d\n",
1983				 dev->devtype->vdevs[i]->name, ret);
1984			goto rel_vfd;
1985		}
1986	}
1987
1988	v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
1989		  dev->vfd[0].num, dev->vfd[i - 1].num);
1990
1991	pm_runtime_put_sync(&pdev->dev);
1992	return;
1993
1994rel_vfd:
1995	while (--i >= 0)
1996		video_unregister_device(&dev->vfd[i]);
1997	v4l2_m2m_release(dev->m2m_dev);
1998rel_ctx:
1999	vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2000put_pm:
2001	pm_runtime_put_sync(&pdev->dev);
2002}
2003
2004static int coda_firmware_request(struct coda_dev *dev)
2005{
2006	char *fw = dev->devtype->firmware;
2007
2008	dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2009		coda_product_name(dev->devtype->product));
2010
2011	return request_firmware_nowait(THIS_MODULE, true,
2012		fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
2013}
2014
2015enum coda_platform {
2016	CODA_IMX27,
2017	CODA_IMX53,
2018	CODA_IMX6Q,
2019	CODA_IMX6DL,
2020};
2021
2022static const struct coda_devtype coda_devdata[] = {
2023	[CODA_IMX27] = {
2024		.firmware     = "v4l-codadx6-imx27.bin",
2025		.product      = CODA_DX6,
2026		.codecs       = codadx6_codecs,
2027		.num_codecs   = ARRAY_SIZE(codadx6_codecs),
2028		.vdevs        = codadx6_video_devices,
2029		.num_vdevs    = ARRAY_SIZE(codadx6_video_devices),
2030		.workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
2031		.iram_size    = 0xb000,
2032	},
2033	[CODA_IMX53] = {
2034		.firmware     = "v4l-coda7541-imx53.bin",
2035		.product      = CODA_7541,
2036		.codecs       = coda7_codecs,
2037		.num_codecs   = ARRAY_SIZE(coda7_codecs),
2038		.vdevs        = coda7_video_devices,
2039		.num_vdevs    = ARRAY_SIZE(coda7_video_devices),
2040		.workbuf_size = 128 * 1024,
2041		.tempbuf_size = 304 * 1024,
2042		.iram_size    = 0x14000,
2043	},
2044	[CODA_IMX6Q] = {
2045		.firmware     = "v4l-coda960-imx6q.bin",
2046		.product      = CODA_960,
2047		.codecs       = coda9_codecs,
2048		.num_codecs   = ARRAY_SIZE(coda9_codecs),
2049		.vdevs        = coda9_video_devices,
2050		.num_vdevs    = ARRAY_SIZE(coda9_video_devices),
2051		.workbuf_size = 80 * 1024,
2052		.tempbuf_size = 204 * 1024,
2053		.iram_size    = 0x21000,
2054	},
2055	[CODA_IMX6DL] = {
2056		.firmware     = "v4l-coda960-imx6dl.bin",
2057		.product      = CODA_960,
2058		.codecs       = coda9_codecs,
2059		.num_codecs   = ARRAY_SIZE(coda9_codecs),
2060		.vdevs        = coda9_video_devices,
2061		.num_vdevs    = ARRAY_SIZE(coda9_video_devices),
2062		.workbuf_size = 80 * 1024,
2063		.tempbuf_size = 204 * 1024,
2064		.iram_size    = 0x20000,
2065	},
2066};
2067
2068static struct platform_device_id coda_platform_ids[] = {
2069	{ .name = "coda-imx27", .driver_data = CODA_IMX27 },
2070	{ /* sentinel */ }
2071};
2072MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2073
2074#ifdef CONFIG_OF
2075static const struct of_device_id coda_dt_ids[] = {
2076	{ .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
2077	{ .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
2078	{ .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2079	{ .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
2080	{ /* sentinel */ }
2081};
2082MODULE_DEVICE_TABLE(of, coda_dt_ids);
2083#endif
2084
2085static int coda_probe(struct platform_device *pdev)
2086{
2087	const struct of_device_id *of_id =
2088			of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2089	const struct platform_device_id *pdev_id;
2090	struct coda_platform_data *pdata = pdev->dev.platform_data;
2091	struct device_node *np = pdev->dev.of_node;
2092	struct gen_pool *pool;
2093	struct coda_dev *dev;
2094	struct resource *res;
2095	int ret, irq;
2096
2097	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2098	if (!dev)
2099		return -ENOMEM;
2100
2101	pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2102
2103	if (of_id) {
2104		dev->devtype = of_id->data;
2105	} else if (pdev_id) {
2106		dev->devtype = &coda_devdata[pdev_id->driver_data];
2107	} else {
2108		ret = -EINVAL;
2109		goto err_v4l2_register;
2110	}
2111
2112	spin_lock_init(&dev->irqlock);
2113	INIT_LIST_HEAD(&dev->instances);
2114
2115	dev->plat_dev = pdev;
2116	dev->clk_per = devm_clk_get(&pdev->dev, "per");
2117	if (IS_ERR(dev->clk_per)) {
2118		dev_err(&pdev->dev, "Could not get per clock\n");
2119		return PTR_ERR(dev->clk_per);
2120	}
2121
2122	dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2123	if (IS_ERR(dev->clk_ahb)) {
2124		dev_err(&pdev->dev, "Could not get ahb clock\n");
2125		return PTR_ERR(dev->clk_ahb);
2126	}
2127
2128	/* Get  memory for physical registers */
2129	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2130	dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2131	if (IS_ERR(dev->regs_base))
2132		return PTR_ERR(dev->regs_base);
2133
2134	/* IRQ */
2135	irq = platform_get_irq_byname(pdev, "bit");
2136	if (irq < 0)
2137		irq = platform_get_irq(pdev, 0);
2138	if (irq < 0) {
2139		dev_err(&pdev->dev, "failed to get irq resource\n");
2140		return irq;
2141	}
2142
2143	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2144			IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2145	if (ret < 0) {
2146		dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2147		return ret;
2148	}
2149
2150	dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
2151	if (IS_ERR(dev->rstc)) {
2152		ret = PTR_ERR(dev->rstc);
2153		if (ret == -ENOENT || ret == -ENOSYS) {
2154			dev->rstc = NULL;
2155		} else {
2156			dev_err(&pdev->dev, "failed get reset control: %d\n",
2157				ret);
2158			return ret;
2159		}
2160	}
2161
2162	/* Get IRAM pool from device tree or platform data */
2163	pool = of_get_named_gen_pool(np, "iram", 0);
2164	if (!pool && pdata)
2165		pool = dev_get_gen_pool(pdata->iram_dev);
2166	if (!pool) {
2167		dev_err(&pdev->dev, "iram pool not available\n");
2168		return -ENOMEM;
2169	}
2170	dev->iram_pool = pool;
2171
2172	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2173	if (ret)
2174		return ret;
2175
2176	mutex_init(&dev->dev_mutex);
2177	mutex_init(&dev->coda_mutex);
2178
2179	dev->debugfs_root = debugfs_create_dir("coda", NULL);
2180	if (!dev->debugfs_root)
2181		dev_warn(&pdev->dev, "failed to create debugfs root\n");
2182
2183	/* allocate auxiliary per-device buffers for the BIT processor */
2184	if (dev->devtype->product == CODA_DX6) {
2185		ret = coda_alloc_aux_buf(dev, &dev->workbuf,
2186					 dev->devtype->workbuf_size, "workbuf",
2187					 dev->debugfs_root);
2188		if (ret < 0)
2189			goto err_v4l2_register;
2190	}
2191
2192	if (dev->devtype->tempbuf_size) {
2193		ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
2194					 dev->devtype->tempbuf_size, "tempbuf",
2195					 dev->debugfs_root);
2196		if (ret < 0)
2197			goto err_v4l2_register;
2198	}
2199
2200	dev->iram.size = dev->devtype->iram_size;
2201	dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2202					     &dev->iram.paddr);
2203	if (!dev->iram.vaddr) {
2204		dev_warn(&pdev->dev, "unable to alloc iram\n");
2205	} else {
2206		memset(dev->iram.vaddr, 0, dev->iram.size);
2207		dev->iram.blob.data = dev->iram.vaddr;
2208		dev->iram.blob.size = dev->iram.size;
2209		dev->iram.dentry = debugfs_create_blob("iram", 0644,
2210						       dev->debugfs_root,
2211						       &dev->iram.blob);
2212	}
2213
2214	dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2215	if (!dev->workqueue) {
2216		dev_err(&pdev->dev, "unable to alloc workqueue\n");
2217		ret = -ENOMEM;
2218		goto err_v4l2_register;
2219	}
2220
2221	platform_set_drvdata(pdev, dev);
2222
2223	/*
2224	 * Start activated so we can directly call coda_hw_init in
2225	 * coda_fw_callback regardless of whether CONFIG_PM is
2226	 * enabled or whether the device is associated with a PM domain.
2227	 */
2228	pm_runtime_get_noresume(&pdev->dev);
2229	pm_runtime_set_active(&pdev->dev);
2230	pm_runtime_enable(&pdev->dev);
2231
2232	return coda_firmware_request(dev);
2233
2234err_v4l2_register:
2235	v4l2_device_unregister(&dev->v4l2_dev);
2236	return ret;
2237}
2238
2239static int coda_remove(struct platform_device *pdev)
2240{
2241	struct coda_dev *dev = platform_get_drvdata(pdev);
2242	int i;
2243
2244	for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2245		if (video_get_drvdata(&dev->vfd[i]))
2246			video_unregister_device(&dev->vfd[i]);
2247	}
2248	if (dev->m2m_dev)
2249		v4l2_m2m_release(dev->m2m_dev);
2250	pm_runtime_disable(&pdev->dev);
2251	if (dev->alloc_ctx)
2252		vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2253	v4l2_device_unregister(&dev->v4l2_dev);
2254	destroy_workqueue(dev->workqueue);
2255	if (dev->iram.vaddr)
2256		gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2257			      dev->iram.size);
2258	coda_free_aux_buf(dev, &dev->codebuf);
2259	coda_free_aux_buf(dev, &dev->tempbuf);
2260	coda_free_aux_buf(dev, &dev->workbuf);
2261	debugfs_remove_recursive(dev->debugfs_root);
2262	return 0;
2263}
2264
2265#ifdef CONFIG_PM
2266static int coda_runtime_resume(struct device *dev)
2267{
2268	struct coda_dev *cdev = dev_get_drvdata(dev);
2269	int ret = 0;
2270
2271	if (dev->pm_domain && cdev->codebuf.vaddr) {
2272		ret = coda_hw_init(cdev);
2273		if (ret)
2274			v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2275	}
2276
2277	return ret;
2278}
2279#endif
2280
2281static const struct dev_pm_ops coda_pm_ops = {
2282	SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2283};
2284
2285static struct platform_driver coda_driver = {
2286	.probe	= coda_probe,
2287	.remove	= coda_remove,
2288	.driver	= {
2289		.name	= CODA_NAME,
2290		.of_match_table = of_match_ptr(coda_dt_ids),
2291		.pm	= &coda_pm_ops,
2292	},
2293	.id_table = coda_platform_ids,
2294};
2295
2296module_platform_driver(coda_driver);
2297
2298MODULE_LICENSE("GPL");
2299MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2300MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");
2301