coda-common.c 58.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Coda multi-standard codec IP
 *
 * Copyright (C) 2012 Vista Silicon S.L.
 *    Javier Martin, <javier.martin@vista-silicon.com>
 *    Xavier Duret
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/clk.h>
15
#include <linux/debugfs.h>
16 17
#include <linux/delay.h>
#include <linux/firmware.h>
P
Philipp Zabel 已提交
18
#include <linux/genalloc.h>
19 20 21
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/irq.h>
22
#include <linux/kfifo.h>
23 24 25
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
26
#include <linux/pm_runtime.h>
27 28 29
#include <linux/slab.h>
#include <linux/videodev2.h>
#include <linux/of.h>
P
Philipp Zabel 已提交
30
#include <linux/platform_data/coda.h>
31
#include <linux/reset.h>
32 33 34

#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
35
#include <media/v4l2-event.h>
36 37 38 39 40
#include <media/v4l2-ioctl.h>
#include <media/v4l2-mem2mem.h>
#include <media/videobuf2-core.h>
#include <media/videobuf2-dma-contig.h>

41
#include "coda.h"
42 43 44

#define CODA_NAME		"coda"

45
#define CODADX6_MAX_INSTANCES	4
46
#define CODA_MAX_FORMATS	4
47 48 49 50 51 52 53 54 55 56 57 58 59

#define CODA_PARA_BUF_SIZE	(10 * 1024)
#define CODA_ISRAM_SIZE	(2048 * 2)

#define MIN_W 176
#define MIN_H 144

#define S_ALIGN		1 /* multiple of 2 */
#define W_ALIGN		1 /* multiple of 2 */
#define H_ALIGN		1 /* multiple of 2 */

#define fh_to_ctx(__fh)	container_of(__fh, struct coda_ctx, fh)

60
int coda_debug;
61
module_param(coda_debug, int, 0644);
62
MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
63 64 65 66

struct coda_fmt {
	char *name;
	u32 fourcc;
67 68
};

69
void coda_write(struct coda_dev *dev, u32 data, u32 reg)
70
{
71
	v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
72 73 74 75
		 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
	writel(data, dev->regs_base + reg);
}

76
unsigned int coda_read(struct coda_dev *dev, u32 reg)
77 78
{
	u32 data;
P
Philipp Zabel 已提交
79

80
	data = readl(dev->regs_base + reg);
81
	v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
82 83 84 85
		 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
	return data;
}

86 87 88 89 90 91 92 93 94 95 96 97 98
void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
		     struct vb2_buffer *buf, unsigned int reg_y)
{
	u32 base_y = vb2_dma_contig_plane_dma_addr(buf, 0);
	u32 base_cb, base_cr;

	switch (q_data->fourcc) {
	case V4L2_PIX_FMT_YVU420:
		/* Switch Cb and Cr for YVU420 format */
		base_cr = base_y + q_data->bytesperline * q_data->height;
		base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
		break;
	case V4L2_PIX_FMT_YUV420:
99
	case V4L2_PIX_FMT_NV12:
100 101 102 103
	default:
		base_cb = base_y + q_data->bytesperline * q_data->height;
		base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
		break;
104 105 106
	case V4L2_PIX_FMT_YUV422P:
		base_cb = base_y + q_data->bytesperline * q_data->height;
		base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
107 108 109 110 111 112 113
	}

	coda_write(ctx->dev, base_y, reg_y);
	coda_write(ctx->dev, base_cb, reg_y + 4);
	coda_write(ctx->dev, base_cr, reg_y + 8);
}

114
/*
115
 * Array of all formats supported by any version of Coda:
116
 */
117
static const struct coda_fmt coda_formats[] = {
118
	{
119
		.name = "YUV 4:2:0 Planar, YCbCr",
120 121
		.fourcc = V4L2_PIX_FMT_YUV420,
	},
122
	{
123 124
		.name = "YUV 4:2:0 Planar, YCrCb",
		.fourcc = V4L2_PIX_FMT_YVU420,
125
	},
126 127 128 129
	{
		.name = "YUV 4:2:0 Partial interleaved Y/CbCr",
		.fourcc = V4L2_PIX_FMT_NV12,
	},
130 131 132 133
	{
		.name = "YUV 4:2:2 Planar, YCbCr",
		.fourcc = V4L2_PIX_FMT_YUV422P,
	},
134 135 136 137 138 139 140 141
	{
		.name = "H264 Encoded Stream",
		.fourcc = V4L2_PIX_FMT_H264,
	},
	{
		.name = "MPEG4 Encoded Stream",
		.fourcc = V4L2_PIX_FMT_MPEG4,
	},
142 143 144 145
	{
		.name = "JPEG Encoded Images",
		.fourcc = V4L2_PIX_FMT_JPEG,
	},
146 147
};

148 149 150 151 152 153 154 155 156 157
#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
	{ mode, src_fourcc, dst_fourcc, max_w, max_h }

/*
 * Arrays of codecs supported by each given version of Coda:
 *  i.MX27 -> codadx6
 *  i.MX5x -> coda7
 *  i.MX6  -> coda960
 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
 */
158
static const struct coda_codec codadx6_codecs[] = {
159 160 161 162
	CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,  720, 576),
	CODA_CODEC(CODADX6_MODE_ENCODE_MP4,  V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
};

163
static const struct coda_codec coda7_codecs[] = {
164 165
	CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,   1280, 720),
	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 168
	CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264,   V4L2_PIX_FMT_YUV420, 1920, 1088),
	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
};

172
static const struct coda_codec coda9_codecs[] = {
173 174 175 176
	CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,   1920, 1088),
	CODA_CODEC(CODA9_MODE_ENCODE_MP4,  V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4,  1920, 1088),
	CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264,   V4L2_PIX_FMT_YUV420, 1920, 1088),
	CODA_CODEC(CODA9_MODE_DECODE_MP4,  V4L2_PIX_FMT_MPEG4,  V4L2_PIX_FMT_YUV420, 1920, 1088),
177 178
};

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
struct coda_video_device {
	const char *name;
	enum coda_inst_type type;
	const struct coda_context_ops *ops;
	u32 src_formats[CODA_MAX_FORMATS];
	u32 dst_formats[CODA_MAX_FORMATS];
};

static const struct coda_video_device coda_bit_encoder = {
	.name = "coda-encoder",
	.type = CODA_INST_ENCODER,
	.ops = &coda_bit_encode_ops,
	.src_formats = {
		V4L2_PIX_FMT_YUV420,
		V4L2_PIX_FMT_YVU420,
		V4L2_PIX_FMT_NV12,
	},
	.dst_formats = {
		V4L2_PIX_FMT_H264,
		V4L2_PIX_FMT_MPEG4,
	},
};

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
static const struct coda_video_device coda_bit_jpeg_encoder = {
	.name = "coda-jpeg-encoder",
	.type = CODA_INST_ENCODER,
	.ops = &coda_bit_encode_ops,
	.src_formats = {
		V4L2_PIX_FMT_YUV420,
		V4L2_PIX_FMT_YVU420,
		V4L2_PIX_FMT_NV12,
		V4L2_PIX_FMT_YUV422P,
	},
	.dst_formats = {
		V4L2_PIX_FMT_JPEG,
	},
};

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
static const struct coda_video_device coda_bit_decoder = {
	.name = "coda-decoder",
	.type = CODA_INST_DECODER,
	.ops = &coda_bit_decode_ops,
	.src_formats = {
		V4L2_PIX_FMT_H264,
		V4L2_PIX_FMT_MPEG4,
	},
	.dst_formats = {
		V4L2_PIX_FMT_YUV420,
		V4L2_PIX_FMT_YVU420,
		V4L2_PIX_FMT_NV12,
	},
};

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
static const struct coda_video_device coda_bit_jpeg_decoder = {
	.name = "coda-jpeg-decoder",
	.type = CODA_INST_DECODER,
	.ops = &coda_bit_decode_ops,
	.src_formats = {
		V4L2_PIX_FMT_JPEG,
	},
	.dst_formats = {
		V4L2_PIX_FMT_YUV420,
		V4L2_PIX_FMT_YVU420,
		V4L2_PIX_FMT_NV12,
		V4L2_PIX_FMT_YUV422P,
	},
};

247 248 249 250 251
static const struct coda_video_device *codadx6_video_devices[] = {
	&coda_bit_encoder,
};

static const struct coda_video_device *coda7_video_devices[] = {
252 253
	&coda_bit_jpeg_encoder,
	&coda_bit_jpeg_decoder,
254 255 256 257 258 259 260 261 262
	&coda_bit_encoder,
	&coda_bit_decoder,
};

static const struct coda_video_device *coda9_video_devices[] = {
	&coda_bit_encoder,
	&coda_bit_decoder,
};

263 264 265 266 267
static bool coda_format_is_yuv(u32 fourcc)
{
	switch (fourcc) {
	case V4L2_PIX_FMT_YUV420:
	case V4L2_PIX_FMT_YVU420:
268
	case V4L2_PIX_FMT_NV12:
269
	case V4L2_PIX_FMT_YUV422P:
270 271 272 273 274 275
		return true;
	default:
		return false;
	}
}

276 277 278 279 280 281 282 283 284 285 286 287
static const char *coda_format_name(u32 fourcc)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(coda_formats); i++) {
		if (coda_formats[i].fourcc == fourcc)
			return coda_formats[i].name;
	}

	return NULL;
}

288 289 290 291 292 293 294 295 296
/*
 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
 * tables.
 */
static u32 coda_format_normalize_yuv(u32 fourcc)
{
	return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
}

297 298
static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
						int src_fourcc, int dst_fourcc)
299
{
300
	const struct coda_codec *codecs = dev->devtype->codecs;
301 302 303 304 305 306 307
	int num_codecs = dev->devtype->num_codecs;
	int k;

	src_fourcc = coda_format_normalize_yuv(src_fourcc);
	dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
	if (src_fourcc == dst_fourcc)
		return NULL;
308

309 310 311
	for (k = 0; k < num_codecs; k++) {
		if (codecs[k].src_fourcc == src_fourcc &&
		    codecs[k].dst_fourcc == dst_fourcc)
312 313 314
			break;
	}

315
	if (k == num_codecs)
316 317
		return NULL;

318
	return &codecs[k];
319 320
}

321
static void coda_get_max_dimensions(struct coda_dev *dev,
322
				    const struct coda_codec *codec,
323 324
				    int *max_w, int *max_h)
{
325
	const struct coda_codec *codecs = dev->devtype->codecs;
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
	int num_codecs = dev->devtype->num_codecs;
	unsigned int w, h;
	int k;

	if (codec) {
		w = codec->max_w;
		h = codec->max_h;
	} else {
		for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
			w = max(w, codecs[k].max_w);
			h = max(h, codecs[k].max_h);
		}
	}

	if (max_w)
		*max_w = w;
	if (max_h)
		*max_h = h;
}

346 347 348 349 350 351 352 353 354 355 356
const struct coda_video_device *to_coda_video_device(struct video_device *vdev)
{
	struct coda_dev *dev = video_get_drvdata(vdev);
	unsigned int i = vdev - dev->vfd;

	if (i >= dev->devtype->num_vdevs)
		return NULL;

	return dev->devtype->vdevs[i];
}

357
const char *coda_product_name(int product)
358 359 360 361 362 363 364 365
{
	static char buf[9];

	switch (product) {
	case CODA_DX6:
		return "CodaDx6";
	case CODA_7541:
		return "CODA7541";
366 367
	case CODA_960:
		return "CODA960";
368 369 370 371 372 373
	default:
		snprintf(buf, sizeof(buf), "(0x%04x)", product);
		return buf;
	}
}

374 375 376
/*
 * V4L2 ioctl() operations.
 */
377 378
static int coda_querycap(struct file *file, void *priv,
			 struct v4l2_capability *cap)
379
{
380 381
	struct coda_ctx *ctx = fh_to_ctx(priv);

382
	strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
383 384
	strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
		sizeof(cap->card));
385
	strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
386
	cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
387 388 389 390 391
	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;

	return 0;
}

392 393
static int coda_enum_fmt(struct file *file, void *priv,
			 struct v4l2_fmtdesc *f)
394
{
395 396 397 398 399 400 401 402 403
	struct video_device *vdev = video_devdata(file);
	const struct coda_video_device *cvd = to_coda_video_device(vdev);
	const u32 *formats;
	const char *name;

	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
		formats = cvd->src_formats;
	else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
		formats = cvd->dst_formats;
404
	else
405
		return -EINVAL;
406

407 408 409 410 411 412 413 414
	if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
		return -EINVAL;

	name = coda_format_name(formats[f->index]);
	strlcpy(f->description, name, sizeof(f->description));
	f->pixelformat = formats[f->index];
	if (!coda_format_is_yuv(formats[f->index]))
		f->flags |= V4L2_FMT_FLAG_COMPRESSED;
415

416
	return 0;
417 418
}

419 420
static int coda_g_fmt(struct file *file, void *priv,
		      struct v4l2_format *f)
421 422 423 424 425
{
	struct coda_q_data *q_data;
	struct coda_ctx *ctx = fh_to_ctx(priv);

	q_data = get_q_data(ctx, f->type);
426 427
	if (!q_data)
		return -EINVAL;
428 429

	f->fmt.pix.field	= V4L2_FIELD_NONE;
430
	f->fmt.pix.pixelformat	= q_data->fourcc;
431 432
	f->fmt.pix.width	= q_data->width;
	f->fmt.pix.height	= q_data->height;
433
	f->fmt.pix.bytesperline = q_data->bytesperline;
434 435

	f->fmt.pix.sizeimage	= q_data->sizeimage;
436 437 438 439
	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
		f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
	else
		f->fmt.pix.colorspace = ctx->colorspace;
440 441 442 443

	return 0;
}

444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
{
	struct coda_q_data *q_data;
	const u32 *formats;
	int i;

	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
		formats = ctx->cvd->src_formats;
	else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
		formats = ctx->cvd->dst_formats;
	else
		return -EINVAL;

	for (i = 0; i < CODA_MAX_FORMATS; i++) {
		if (formats[i] == f->fmt.pix.pixelformat) {
			f->fmt.pix.pixelformat = formats[i];
			return 0;
		}
	}

	/* Fall back to currently set pixelformat */
	q_data = get_q_data(ctx, f->type);
	f->fmt.pix.pixelformat = q_data->fourcc;
467 468 469 470

	return 0;
}

471
static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
472
			struct v4l2_format *f)
473
{
474
	struct coda_dev *dev = ctx->dev;
475
	unsigned int max_w, max_h;
476 477 478 479 480 481 482 483 484 485 486 487
	enum v4l2_field field;

	field = f->fmt.pix.field;
	if (field == V4L2_FIELD_ANY)
		field = V4L2_FIELD_NONE;
	else if (V4L2_FIELD_NONE != field)
		return -EINVAL;

	/* V4L2 specification suggests the driver corrects the format struct
	 * if any of the dimensions is unsupported */
	f->fmt.pix.field = field;

488 489 490 491 492 493 494 495
	coda_get_max_dimensions(dev, codec, &max_w, &max_h);
	v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
			      &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
			      S_ALIGN);

	switch (f->fmt.pix.pixelformat) {
	case V4L2_PIX_FMT_YUV420:
	case V4L2_PIX_FMT_YVU420:
496
	case V4L2_PIX_FMT_NV12:
497 498 499 500
		/*
		 * Frame stride must be at least multiple of 8,
		 * but multiple of 16 for h.264 or JPEG 4:2:x
		 */
501
		f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
502
		f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
503
					f->fmt.pix.height * 3 / 2;
504
		break;
505 506 507 508 509
	case V4L2_PIX_FMT_YUV422P:
		f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
		f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
					f->fmt.pix.height * 2;
		break;
510 511 512
	case V4L2_PIX_FMT_JPEG:
		f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
		/* fallthrough */
513 514
	case V4L2_PIX_FMT_H264:
	case V4L2_PIX_FMT_MPEG4:
515
		f->fmt.pix.bytesperline = 0;
516 517 518 519 520 521 522 523 524
		/*
		 * This is a rough estimate for sensible compressed buffer
		 * sizes (between 1 and 16 bits per pixel). This could be
		 * improved by better format specific worst case estimates.
		 */
		f->fmt.pix.sizeimage = round_up(clamp(f->fmt.pix.sizeimage,
				f->fmt.pix.width * f->fmt.pix.height / 8,
				f->fmt.pix.width * f->fmt.pix.height * 2),
				PAGE_SIZE);
525 526 527
		break;
	default:
		BUG();
528 529 530 531 532
	}

	return 0;
}

533 534
static int coda_try_fmt_vid_cap(struct file *file, void *priv,
				struct v4l2_format *f)
535 536
{
	struct coda_ctx *ctx = fh_to_ctx(priv);
537 538
	const struct coda_q_data *q_data_src;
	const struct coda_codec *codec;
539 540 541
	struct vb2_queue *src_vq;
	int ret;

542 543 544 545 546 547
	ret = coda_try_pixelformat(ctx, f);
	if (ret < 0)
		return ret;

	q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);

548
	/*
549 550
	 * If the source format is already fixed, only allow the same output
	 * resolution
551
	 */
552
	src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
553
	if (vb2_is_streaming(src_vq)) {
554 555
		f->fmt.pix.width = q_data_src->width;
		f->fmt.pix.height = q_data_src->height;
556
	}
557 558 559

	f->fmt.pix.colorspace = ctx->colorspace;

560 561 562 563 564 565
	q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
	codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
				f->fmt.pix.pixelformat);
	if (!codec)
		return -EINVAL;

566
	ret = coda_try_fmt(ctx, codec, f);
567 568 569 570 571
	if (ret < 0)
		return ret;

	/* The h.264 decoder only returns complete 16x16 macroblocks */
	if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
572
		f->fmt.pix.width = f->fmt.pix.width;
573
		f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
574
		f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
575 576 577 578 579
		f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
				       f->fmt.pix.height * 3 / 2;
	}

	return 0;
580 581
}

582 583
static int coda_try_fmt_vid_out(struct file *file, void *priv,
				struct v4l2_format *f)
584 585
{
	struct coda_ctx *ctx = fh_to_ctx(priv);
586 587 588 589
	struct coda_dev *dev = ctx->dev;
	const struct coda_q_data *q_data_dst;
	const struct coda_codec *codec;
	int ret;
590

591 592 593
	ret = coda_try_pixelformat(ctx, f);
	if (ret < 0)
		return ret;
594

595 596 597 598 599
	switch (f->fmt.pix.colorspace) {
	case V4L2_COLORSPACE_REC709:
	case V4L2_COLORSPACE_JPEG:
		break;
	default:
600 601 602 603
		if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
			f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
		else
			f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
604
	}
605

606 607
	q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
	codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
608

609
	return coda_try_fmt(ctx, codec, f);
610 611
}

612
static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
613 614 615 616
{
	struct coda_q_data *q_data;
	struct vb2_queue *vq;

617
	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
618 619 620 621 622 623 624 625 626 627 628 629
	if (!vq)
		return -EINVAL;

	q_data = get_q_data(ctx, f->type);
	if (!q_data)
		return -EINVAL;

	if (vb2_is_busy(vq)) {
		v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
		return -EBUSY;
	}

630
	q_data->fourcc = f->fmt.pix.pixelformat;
631 632
	q_data->width = f->fmt.pix.width;
	q_data->height = f->fmt.pix.height;
633
	q_data->bytesperline = f->fmt.pix.bytesperline;
634
	q_data->sizeimage = f->fmt.pix.sizeimage;
635 636 637 638
	q_data->rect.left = 0;
	q_data->rect.top = 0;
	q_data->rect.width = f->fmt.pix.width;
	q_data->rect.height = f->fmt.pix.height;
639 640 641

	v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
		"Setting format for type %d, wxh: %dx%d, fmt: %d\n",
642
		f->type, q_data->width, q_data->height, q_data->fourcc);
643 644 645 646

	return 0;
}

647 648
static int coda_s_fmt_vid_cap(struct file *file, void *priv,
			      struct v4l2_format *f)
649
{
650
	struct coda_ctx *ctx = fh_to_ctx(priv);
651 652
	int ret;

653
	ret = coda_try_fmt_vid_cap(file, priv, f);
654 655 656
	if (ret)
		return ret;

657
	return coda_s_fmt(ctx, f);
658 659
}

660 661
static int coda_s_fmt_vid_out(struct file *file, void *priv,
			      struct v4l2_format *f)
662 663
{
	struct coda_ctx *ctx = fh_to_ctx(priv);
664
	struct v4l2_format f_cap;
665 666
	int ret;

667
	ret = coda_try_fmt_vid_out(file, priv, f);
668 669 670
	if (ret)
		return ret;

671
	ret = coda_s_fmt(ctx, f);
672
	if (ret)
673 674 675
		return ret;

	ctx->colorspace = f->fmt.pix.colorspace;
676

677
	memset(&f_cap, 0, sizeof(f_cap));
678 679 680 681 682 683 684 685 686 687
	f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	coda_g_fmt(file, priv, &f_cap);
	f_cap.fmt.pix.width = f->fmt.pix.width;
	f_cap.fmt.pix.height = f->fmt.pix.height;

	ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
	if (ret)
		return ret;

	return coda_s_fmt(ctx, &f_cap);
688 689
}

690 691
static int coda_qbuf(struct file *file, void *priv,
		     struct v4l2_buffer *buf)
692 693 694
{
	struct coda_ctx *ctx = fh_to_ctx(priv);

695
	return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
696 697
}

698 699 700 701 702
static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
				      struct v4l2_buffer *buf)
{
	struct vb2_queue *src_vq;

703
	src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
704 705 706 707 708

	return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
		(buf->sequence == (ctx->qsequence - 1)));
}

709 710
static int coda_dqbuf(struct file *file, void *priv,
		      struct v4l2_buffer *buf)
711 712
{
	struct coda_ctx *ctx = fh_to_ctx(priv);
713 714
	int ret;

715
	ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
716

717 718 719 720 721 722 723 724 725 726 727
	/* If this is the last capture buffer, emit an end-of-stream event */
	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
	    coda_buf_is_end_of_stream(ctx, buf)) {
		const struct v4l2_event eos_event = {
			.type = V4L2_EVENT_EOS
		};

		v4l2_event_queue_fh(&ctx->fh, &eos_event);
	}

	return ret;
728 729
}

730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
static int coda_g_selection(struct file *file, void *fh,
			    struct v4l2_selection *s)
{
	struct coda_ctx *ctx = fh_to_ctx(fh);
	struct coda_q_data *q_data;
	struct v4l2_rect r, *rsel;

	q_data = get_q_data(ctx, s->type);
	if (!q_data)
		return -EINVAL;

	r.left = 0;
	r.top = 0;
	r.width = q_data->width;
	r.height = q_data->height;
	rsel = &q_data->rect;

	switch (s->target) {
	case V4L2_SEL_TGT_CROP_DEFAULT:
	case V4L2_SEL_TGT_CROP_BOUNDS:
		rsel = &r;
		/* fallthrough */
	case V4L2_SEL_TGT_CROP:
		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
			return -EINVAL;
		break;
	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
	case V4L2_SEL_TGT_COMPOSE_PADDED:
		rsel = &r;
		/* fallthrough */
	case V4L2_SEL_TGT_COMPOSE:
	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
			return -EINVAL;
		break;
	default:
		return -EINVAL;
	}

	s->r = *rsel;

	return 0;
}

774 775
static int coda_try_decoder_cmd(struct file *file, void *fh,
				struct v4l2_decoder_cmd *dc)
776 777 778 779
{
	if (dc->cmd != V4L2_DEC_CMD_STOP)
		return -EINVAL;

780
	if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
781 782
		return -EINVAL;

783
	if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
784 785
		return -EINVAL;

786 787 788 789 790 791 792 793 794 795 796 797 798 799
	return 0;
}

static int coda_decoder_cmd(struct file *file, void *fh,
			    struct v4l2_decoder_cmd *dc)
{
	struct coda_ctx *ctx = fh_to_ctx(fh);
	int ret;

	ret = coda_try_decoder_cmd(file, fh, dc);
	if (ret < 0)
		return ret;

	/* Ignore decoder stop command silently in encoder context */
800
	if (ctx->inst_type != CODA_INST_DECODER)
801
		return 0;
802

803 804
	/* Set the stream-end flag on this context */
	coda_bit_stream_end_flag(ctx);
805
	ctx->hold = false;
806
	v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
807

808 809 810
	return 0;
}

811 812
static int coda_subscribe_event(struct v4l2_fh *fh,
				const struct v4l2_event_subscription *sub)
813 814 815 816 817 818 819
{
	switch (sub->type) {
	case V4L2_EVENT_EOS:
		return v4l2_event_subscribe(fh, sub, 0, NULL);
	default:
		return v4l2_ctrl_subscribe_event(fh, sub);
	}
820 821 822
}

static const struct v4l2_ioctl_ops coda_ioctl_ops = {
823
	.vidioc_querycap	= coda_querycap,
824

825
	.vidioc_enum_fmt_vid_cap = coda_enum_fmt,
826 827 828
	.vidioc_g_fmt_vid_cap	= coda_g_fmt,
	.vidioc_try_fmt_vid_cap	= coda_try_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap	= coda_s_fmt_vid_cap,
829

830
	.vidioc_enum_fmt_vid_out = coda_enum_fmt,
831 832 833
	.vidioc_g_fmt_vid_out	= coda_g_fmt,
	.vidioc_try_fmt_vid_out	= coda_try_fmt_vid_out,
	.vidioc_s_fmt_vid_out	= coda_s_fmt_vid_out,
834

835 836
	.vidioc_reqbufs		= v4l2_m2m_ioctl_reqbufs,
	.vidioc_querybuf	= v4l2_m2m_ioctl_querybuf,
837

838
	.vidioc_qbuf		= coda_qbuf,
839
	.vidioc_expbuf		= v4l2_m2m_ioctl_expbuf,
840
	.vidioc_dqbuf		= coda_dqbuf,
841
	.vidioc_create_bufs	= v4l2_m2m_ioctl_create_bufs,
842

843 844
	.vidioc_streamon	= v4l2_m2m_ioctl_streamon,
	.vidioc_streamoff	= v4l2_m2m_ioctl_streamoff,
845

846 847
	.vidioc_g_selection	= coda_g_selection,

848
	.vidioc_try_decoder_cmd	= coda_try_decoder_cmd,
849
	.vidioc_decoder_cmd	= coda_decoder_cmd,
850

851
	.vidioc_subscribe_event = coda_subscribe_event,
852
	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
853 854
};

855
void coda_set_gdi_regs(struct coda_ctx *ctx)
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
{
	struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
	struct coda_dev *dev = ctx->dev;
	int i;

	for (i = 0; i < 16; i++)
		coda_write(dev, tiled_map->xy2ca_map[i],
				CODA9_GDI_XY2_CAS_0 + 4 * i);
	for (i = 0; i < 4; i++)
		coda_write(dev, tiled_map->xy2ba_map[i],
				CODA9_GDI_XY2_BA_0 + 4 * i);
	for (i = 0; i < 16; i++)
		coda_write(dev, tiled_map->xy2ra_map[i],
				CODA9_GDI_XY2_RAS_0 + 4 * i);
	coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
	for (i = 0; i < 32; i++)
		coda_write(dev, tiled_map->rbc2axi_map[i],
				CODA9_GDI_RBC2_AXI_0 + 4 * i);
}

876 877 878
/*
 * Mem-to-mem operations.
 */
879 880 881 882 883

static void coda_device_run(void *m2m_priv)
{
	struct coda_ctx *ctx = m2m_priv;
	struct coda_dev *dev = ctx->dev;
884 885 886 887 888 889 890 891 892 893 894

	queue_work(dev->workqueue, &ctx->pic_run_work);
}

static void coda_pic_run_work(struct work_struct *work)
{
	struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
	struct coda_dev *dev = ctx->dev;
	int ret;

	mutex_lock(&ctx->buffer_mutex);
895 896
	mutex_lock(&dev->coda_mutex);

P
Philipp Zabel 已提交
897 898 899 900 901 902
	ret = ctx->ops->prepare_run(ctx);
	if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
		mutex_unlock(&dev->coda_mutex);
		mutex_unlock(&ctx->buffer_mutex);
		/* job_finish scheduled by prepare_decode */
		return;
903 904
	}

P
Philipp Zabel 已提交
905 906
	if (!wait_for_completion_timeout(&ctx->completion,
					 msecs_to_jiffies(1000))) {
907
		dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
908 909

		ctx->hold = true;
910 911

		coda_hw_reset(ctx);
912
	} else if (!ctx->aborting) {
P
Philipp Zabel 已提交
913
		ctx->ops->finish_run(ctx);
914 915 916 917 918 919 920 921
	}

	if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
		queue_work(dev->workqueue, &ctx->seq_end_work);

	mutex_unlock(&dev->coda_mutex);
	mutex_unlock(&ctx->buffer_mutex);

922
	v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
923 924 925 926 927 928 929 930
}

static int coda_job_ready(void *m2m_priv)
{
	struct coda_ctx *ctx = m2m_priv;

	/*
	 * For both 'P' and 'key' frame cases 1 picture
931 932
	 * and 1 frame are needed. In the decoder case,
	 * the compressed frame can be in the bitstream.
933
	 */
934
	if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
935
	    ctx->inst_type != CODA_INST_DECODER) {
936 937 938 939 940
		v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
			 "not ready: not enough video buffers.\n");
		return 0;
	}

941
	if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
942 943 944 945 946
		v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
			 "not ready: not enough video capture buffers.\n");
		return 0;
	}

947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
	if (ctx->inst_type == CODA_INST_DECODER) {
		struct list_head *meta;
		bool stream_end;
		int num_metas;
		int src_bufs;

		if (ctx->hold && !v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx)) {
			v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
				 "%d: not ready: on hold for more buffers.\n",
				 ctx->idx);
			return 0;
		}

		stream_end = ctx->bit_stream_param &
			     CODA_BIT_STREAM_END_FLAG;

		num_metas = 0;
		list_for_each(meta, &ctx->buffer_meta_list)
			num_metas++;

		src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);

		if (!stream_end && (num_metas + src_bufs) < 2) {
			v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
				 "%d: not ready: need 2 buffers available (%d, %d)\n",
				 ctx->idx, num_metas, src_bufs);
			return 0;
		}


		if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
		    !stream_end && (coda_get_bitstream_payload(ctx) < 512)) {
			v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
				 "%d: not ready: not enough bitstream data (%d).\n",
				 ctx->idx, coda_get_bitstream_payload(ctx));
			return 0;
		}
984 985
	}

986 987 988 989 990 991
	if (ctx->aborting) {
		v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
			 "not ready: aborting\n");
		return 0;
	}

992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
	v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
			"job ready\n");
	return 1;
}

static void coda_job_abort(void *priv)
{
	struct coda_ctx *ctx = priv;

	ctx->aborting = 1;

	v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
		 "Aborting task\n");
}

static void coda_lock(void *m2m_priv)
{
	struct coda_ctx *ctx = m2m_priv;
	struct coda_dev *pcdev = ctx->dev;
P
Philipp Zabel 已提交
1011

1012 1013 1014 1015 1016 1017 1018
	mutex_lock(&pcdev->dev_mutex);
}

static void coda_unlock(void *m2m_priv)
{
	struct coda_ctx *ctx = m2m_priv;
	struct coda_dev *pcdev = ctx->dev;
P
Philipp Zabel 已提交
1019

1020 1021 1022
	mutex_unlock(&pcdev->dev_mutex);
}

1023
static const struct v4l2_m2m_ops coda_m2m_ops = {
1024 1025 1026 1027 1028 1029 1030
	.device_run	= coda_device_run,
	.job_ready	= coda_job_ready,
	.job_abort	= coda_job_abort,
	.lock		= coda_lock,
	.unlock		= coda_unlock,
};

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
{
	struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
	int luma_map, chro_map, i;

	memset(tiled_map, 0, sizeof(*tiled_map));

	luma_map = 64;
	chro_map = 64;
	tiled_map->map_type = tiled_map_type;
	for (i = 0; i < 16; i++)
		tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
	for (i = 0; i < 4; i++)
		tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
	for (i = 0; i < 16; i++)
		tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;

	if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
		tiled_map->xy2rbc_config = 0;
	} else {
		dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
			tiled_map_type);
		return;
	}
}

1057 1058
static void set_default_params(struct coda_ctx *ctx)
{
1059
	unsigned int max_w, max_h, size;
1060

1061 1062
	ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
				     ctx->cvd->dst_formats[0]);
1063 1064 1065
	max_w = min(ctx->codec->max_w, 1920U);
	max_h = min(ctx->codec->max_h, 1088U);
	size = max_w * max_h * 3 / 2;
1066

1067
	ctx->params.codec_mode = ctx->codec->mode;
1068 1069 1070 1071
	ctx->colorspace = V4L2_COLORSPACE_REC709;
	ctx->params.framerate = 30;

	/* Default formats for output and input queues */
1072 1073 1074 1075 1076 1077
	ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
	ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
	ctx->q_data[V4L2_M2M_SRC].width = max_w;
	ctx->q_data[V4L2_M2M_SRC].height = max_h;
	ctx->q_data[V4L2_M2M_DST].width = max_w;
	ctx->q_data[V4L2_M2M_DST].height = max_h;
1078 1079
	if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
		ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1080
		ctx->q_data[V4L2_M2M_SRC].sizeimage = size;
1081
		ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1082
		ctx->q_data[V4L2_M2M_DST].sizeimage = round_up(size, PAGE_SIZE);
1083 1084
	} else {
		ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1085
		ctx->q_data[V4L2_M2M_SRC].sizeimage = round_up(size, PAGE_SIZE);
1086
		ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1087
		ctx->q_data[V4L2_M2M_DST].sizeimage = size;
1088
	}
1089 1090 1091 1092
	ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
	ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
	ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
	ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
1093 1094 1095

	if (ctx->dev->devtype->product == CODA_960)
		coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
}

/*
 * Queue operations
 */
static int coda_queue_setup(struct vb2_queue *vq,
				const struct v4l2_format *fmt,
				unsigned int *nbuffers, unsigned int *nplanes,
				unsigned int sizes[], void *alloc_ctxs[])
{
	struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1107
	struct coda_q_data *q_data;
1108 1109
	unsigned int size;

1110 1111
	q_data = get_q_data(ctx, vq->type);
	size = q_data->sizeimage;
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144

	*nplanes = 1;
	sizes[0] = size;

	alloc_ctxs[0] = ctx->dev->alloc_ctx;

	v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
		 "get %d buffer(s) of size %d each.\n", *nbuffers, size);

	return 0;
}

static int coda_buf_prepare(struct vb2_buffer *vb)
{
	struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
	struct coda_q_data *q_data;

	q_data = get_q_data(ctx, vb->vb2_queue->type);

	if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
		v4l2_warn(&ctx->dev->v4l2_dev,
			  "%s data will not fit into plane (%lu < %lu)\n",
			  __func__, vb2_plane_size(vb, 0),
			  (long)q_data->sizeimage);
		return -EINVAL;
	}

	return 0;
}

static void coda_buf_queue(struct vb2_buffer *vb)
{
	struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1145 1146 1147 1148 1149 1150 1151 1152
	struct coda_q_data *q_data;

	q_data = get_q_data(ctx, vb->vb2_queue->type);

	/*
	 * In the decoder case, immediately try to copy the buffer into the
	 * bitstream ringbuffer and mark it as ready to be dequeued.
	 */
1153
	if (ctx->inst_type == CODA_INST_DECODER &&
1154 1155
	    vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
		/*
1156
		 * For backwards compatibility, queuing an empty buffer marks
1157 1158
		 * the stream end
		 */
1159 1160
		if (vb2_get_plane_payload(vb, 0) == 0)
			coda_bit_stream_end_flag(ctx);
1161
		mutex_lock(&ctx->bitstream_mutex);
1162
		v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
1163 1164
		if (vb2_is_streaming(vb->vb2_queue))
			coda_fill_bitstream(ctx);
1165 1166
		mutex_unlock(&ctx->bitstream_mutex);
	} else {
1167
		v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
1168
	}
1169 1170
}

1171 1172
int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
		       size_t size, const char *name, struct dentry *parent)
1173 1174 1175
{
	buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
					GFP_KERNEL);
1176 1177 1178 1179
	if (!buf->vaddr) {
		v4l2_err(&dev->v4l2_dev,
			 "Failed to allocate %s buffer of size %u\n",
			 name, size);
1180
		return -ENOMEM;
1181
	}
1182 1183 1184

	buf->size = size;

1185 1186 1187
	if (name && parent) {
		buf->blob.data = buf->vaddr;
		buf->blob.size = size;
P
Philipp Zabel 已提交
1188 1189
		buf->dentry = debugfs_create_blob(name, 0644, parent,
						  &buf->blob);
1190 1191 1192 1193 1194
		if (!buf->dentry)
			dev_warn(&dev->plat_dev->dev,
				 "failed to create debugfs entry %s\n", name);
	}

1195 1196 1197
	return 0;
}

1198 1199
void coda_free_aux_buf(struct coda_dev *dev,
		       struct coda_aux_buf *buf)
1200 1201 1202 1203 1204 1205 1206
{
	if (buf->vaddr) {
		dma_free_coherent(&dev->plat_dev->dev, buf->size,
				  buf->vaddr, buf->paddr);
		buf->vaddr = NULL;
		buf->size = 0;
	}
1207
	debugfs_remove(buf->dentry);
1208 1209
}

1210
static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1211
{
1212 1213 1214
	struct coda_ctx *ctx = vb2_get_drv_priv(q);
	struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
	struct coda_q_data *q_data_src, *q_data_dst;
1215
	struct vb2_buffer *buf;
1216
	int ret = 0;
1217

1218 1219
	q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1220 1221 1222
		if (q_data_src->fourcc == V4L2_PIX_FMT_H264 ||
		    (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
		     ctx->dev->devtype->product == CODA_7541)) {
1223 1224 1225 1226
			/* copy the buffers that where queued before streamon */
			mutex_lock(&ctx->bitstream_mutex);
			coda_fill_bitstream(ctx);
			mutex_unlock(&ctx->bitstream_mutex);
1227

1228 1229 1230 1231
			if (coda_get_bitstream_payload(ctx) < 512) {
				ret = -EINVAL;
				goto err;
			}
1232
		} else {
1233 1234 1235 1236
			if (count < 1) {
				ret = -EINVAL;
				goto err;
			}
1237
		}
1238

1239
		ctx->streamon_out = 1;
1240
	} else {
1241 1242 1243 1244
		if (count < 1) {
			ret = -EINVAL;
			goto err;
		}
1245 1246

		ctx->streamon_cap = 1;
1247
	}
1248

1249 1250 1251
	/* Don't start the coda unless both queues are on */
	if (!(ctx->streamon_out & ctx->streamon_cap))
		return 0;
1252

1253
	/* Allow BIT decoder device_run with no new buffers queued */
1254
	if (ctx->inst_type == CODA_INST_DECODER)
1255
		v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
1256

1257
	ctx->gopcounter = ctx->params.gop_size - 1;
1258
	q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1259 1260 1261 1262

	ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
				     q_data_dst->fourcc);
	if (!ctx->codec) {
1263
		v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1264 1265
		ret = -EINVAL;
		goto err;
1266 1267
	}

1268 1269 1270 1271
	if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
		ctx->params.gop_size = 1;
	ctx->gopcounter = ctx->params.gop_size - 1;

P
Philipp Zabel 已提交
1272
	ret = ctx->ops->start_streaming(ctx);
1273
	if (ctx->inst_type == CODA_INST_DECODER) {
1274
		if (ret == -EAGAIN)
1275
			return 0;
1276
		else if (ret < 0)
1277
			goto err;
1278 1279
	}

1280 1281
	ctx->initialized = 1;
	return ret;
1282 1283 1284 1285

err:
	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
		while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1286
			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1287 1288
	} else {
		while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1289
			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1290 1291
	}
	return ret;
1292 1293
}

1294
static void coda_stop_streaming(struct vb2_queue *q)
1295 1296
{
	struct coda_ctx *ctx = vb2_get_drv_priv(q);
1297
	struct coda_dev *dev = ctx->dev;
1298
	struct vb2_buffer *buf;
1299 1300

	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1301
		v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1302
			 "%s: output\n", __func__);
1303
		ctx->streamon_out = 0;
1304

1305
		coda_bit_stream_end_flag(ctx);
1306

1307
		ctx->isequence = 0;
1308 1309 1310

		while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1311
	} else {
1312
		v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1313
			 "%s: capture\n", __func__);
1314
		ctx->streamon_cap = 0;
1315

1316
		ctx->osequence = 0;
1317
		ctx->sequence_offset = 0;
1318 1319 1320

		while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1321 1322
	}

1323
	if (!ctx->streamon_out && !ctx->streamon_cap) {
1324
		struct coda_buffer_meta *meta;
1325

1326
		mutex_lock(&ctx->bitstream_mutex);
1327 1328 1329 1330 1331
		while (!list_empty(&ctx->buffer_meta_list)) {
			meta = list_first_entry(&ctx->buffer_meta_list,
						struct coda_buffer_meta, list);
			list_del(&meta->list);
			kfree(meta);
1332
		}
1333
		mutex_unlock(&ctx->bitstream_mutex);
1334 1335 1336
		kfifo_init(&ctx->bitstream_fifo,
			ctx->bitstream.vaddr, ctx->bitstream.size);
		ctx->runcounter = 0;
1337
		ctx->aborting = 0;
1338
	}
1339 1340
}

1341
static const struct vb2_ops coda_qops = {
1342 1343 1344 1345 1346
	.queue_setup		= coda_queue_setup,
	.buf_prepare		= coda_buf_prepare,
	.buf_queue		= coda_buf_queue,
	.start_streaming	= coda_start_streaming,
	.stop_streaming		= coda_stop_streaming,
1347 1348
	.wait_prepare		= vb2_ops_wait_prepare,
	.wait_finish		= vb2_ops_wait_finish,
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
};

static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
{
	struct coda_ctx *ctx =
			container_of(ctrl->handler, struct coda_ctx, ctrls);

	v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
		 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);

	switch (ctrl->id) {
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
	case V4L2_CID_HFLIP:
		if (ctrl->val)
			ctx->params.rot_mode |= CODA_MIR_HOR;
		else
			ctx->params.rot_mode &= ~CODA_MIR_HOR;
		break;
	case V4L2_CID_VFLIP:
		if (ctrl->val)
			ctx->params.rot_mode |= CODA_MIR_VER;
		else
			ctx->params.rot_mode &= ~CODA_MIR_VER;
		break;
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
	case V4L2_CID_MPEG_VIDEO_BITRATE:
		ctx->params.bitrate = ctrl->val / 1000;
		break;
	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
		ctx->params.gop_size = ctrl->val;
		break;
	case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
		ctx->params.h264_intra_qp = ctrl->val;
		break;
	case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
		ctx->params.h264_inter_qp = ctrl->val;
		break;
1384 1385 1386 1387 1388 1389
	case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
		ctx->params.h264_min_qp = ctrl->val;
		break;
	case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
		ctx->params.h264_max_qp = ctrl->val;
		break;
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
		ctx->params.h264_deblk_alpha = ctrl->val;
		break;
	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
		ctx->params.h264_deblk_beta = ctrl->val;
		break;
	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
		ctx->params.h264_deblk_enabled = (ctrl->val ==
				V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
		break;
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
	case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
		ctx->params.mpeg4_intra_qp = ctrl->val;
		break;
	case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
		ctx->params.mpeg4_inter_qp = ctrl->val;
		break;
	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
		ctx->params.slice_mode = ctrl->val;
		break;
	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
		ctx->params.slice_max_mb = ctrl->val;
		break;
1412 1413 1414
	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
		ctx->params.slice_max_bits = ctrl->val * 8;
		break;
1415 1416
	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
		break;
1417 1418 1419
	case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
		ctx->params.intra_refresh = ctrl->val;
		break;
1420 1421 1422 1423 1424 1425
	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
		coda_set_jpeg_compression_quality(ctx, ctrl->val);
		break;
	case V4L2_CID_JPEG_RESTART_INTERVAL:
		ctx->params.jpeg_restart_interval = ctrl->val;
		break;
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
	default:
		v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
			"Invalid control, id=%d, val=%d\n",
			ctrl->id, ctrl->val);
		return -EINVAL;
	}

	return 0;
}

1436
static const struct v4l2_ctrl_ops coda_ctrl_ops = {
1437 1438 1439
	.s_ctrl = coda_s_ctrl,
};

1440
static void coda_encode_ctrls(struct coda_ctx *ctx)
1441 1442 1443 1444 1445 1446
{
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1447
		V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
1448
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1449
		V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
1450 1451 1452 1453 1454 1455
	if (ctx->dev->devtype->product != CODA_960) {
		v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
			V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
	}
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
1456 1457 1458 1459 1460 1461 1462 1463
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
	v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
		V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
		V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1464 1465 1466 1467 1468 1469
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
	v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
1470 1471
		V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
		V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
1472 1473
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
1474
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
P
Philipp Zabel 已提交
1475 1476
		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
		500);
1477 1478 1479 1480 1481
	v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_MPEG_VIDEO_HEADER_MODE,
		V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
		(1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
		V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1482
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
P
Philipp Zabel 已提交
1483 1484
		V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
		1920 * 1088 / 256, 1, 0);
1485 1486
}

1487 1488 1489 1490 1491 1492 1493 1494
static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
{
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
}

1495 1496 1497 1498 1499 1500 1501 1502
static int coda_ctrls_setup(struct coda_ctx *ctx)
{
	v4l2_ctrl_handler_init(&ctx->ctrls, 2);

	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_HFLIP, 0, 1, 1, 0);
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_VFLIP, 0, 1, 1, 0);
1503 1504 1505 1506 1507 1508
	if (ctx->inst_type == CODA_INST_ENCODER) {
		if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
			coda_jpeg_encode_ctrls(ctx);
		else
			coda_encode_ctrls(ctx);
	}
1509 1510

	if (ctx->ctrls.error) {
P
Philipp Zabel 已提交
1511 1512
		v4l2_err(&ctx->dev->v4l2_dev,
			"control initialization error (%d)",
1513 1514 1515 1516 1517 1518 1519
			ctx->ctrls.error);
		return -EINVAL;
	}

	return v4l2_ctrl_handler_setup(&ctx->ctrls);
}

1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
{
	vq->drv_priv = ctx;
	vq->ops = &coda_qops;
	vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
	vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
	vq->lock = &ctx->dev->dev_mutex;

	return vb2_queue_init(vq);
}

1531 1532
int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
			    struct vb2_queue *dst_vq)
1533 1534 1535 1536
{
	int ret;

	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1537
	src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1538 1539
	src_vq->mem_ops = &vb2_dma_contig_memops;

1540
	ret = coda_queue_init(priv, src_vq);
1541 1542 1543 1544
	if (ret)
		return ret;

	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1545
	dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1546 1547
	dst_vq->mem_ops = &vb2_dma_contig_memops;

1548 1549 1550
	return coda_queue_init(priv, dst_vq);
}

1551 1552
int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
			    struct vb2_queue *dst_vq)
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
{
	int ret;

	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
	src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
	src_vq->mem_ops = &vb2_dma_contig_memops;

	ret = coda_queue_init(priv, src_vq);
	if (ret)
		return ret;

	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
	dst_vq->mem_ops = &vb2_dma_contig_memops;

	return coda_queue_init(priv, dst_vq);
1569 1570
}

1571 1572
static int coda_next_free_instance(struct coda_dev *dev)
{
1573 1574 1575 1576 1577 1578 1579
	int idx = ffz(dev->instance_mask);

	if ((idx < 0) ||
	    (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
		return -EBUSY;

	return idx;
1580 1581
}

1582 1583 1584 1585 1586
/*
 * File operations
 */

static int coda_open(struct file *file)
1587
{
1588 1589
	struct video_device *vdev = video_devdata(file);
	struct coda_dev *dev = video_get_drvdata(vdev);
1590
	struct coda_ctx *ctx = NULL;
1591
	char *name;
1592
	int ret;
1593
	int idx;
1594

P
Philipp Zabel 已提交
1595
	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1596 1597 1598
	if (!ctx)
		return -ENOMEM;

F
Fabio Estevam 已提交
1599
	idx = coda_next_free_instance(dev);
1600 1601
	if (idx < 0) {
		ret = idx;
F
Fabio Estevam 已提交
1602 1603 1604 1605
		goto err_coda_max;
	}
	set_bit(idx, &dev->instance_mask);

1606 1607 1608 1609
	name = kasprintf(GFP_KERNEL, "context%d", idx);
	ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
	kfree(name);

1610 1611 1612
	ctx->cvd = to_coda_video_device(vdev);
	ctx->inst_type = ctx->cvd->type;
	ctx->ops = ctx->cvd->ops;
1613 1614
	init_completion(&ctx->completion);
	INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
P
Philipp Zabel 已提交
1615
	INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
1616 1617 1618 1619
	v4l2_fh_init(&ctx->fh, video_devdata(file));
	file->private_data = &ctx->fh;
	v4l2_fh_add(&ctx->fh);
	ctx->dev = dev;
1620
	ctx->idx = idx;
1621
	switch (dev->devtype->product) {
1622
	case CODA_960:
1623 1624 1625
		ctx->frame_mem_ctrl = 1 << 12;
		/* fallthrough */
	case CODA_7541:
1626 1627 1628 1629 1630
		ctx->reg_idx = 0;
		break;
	default:
		ctx->reg_idx = idx;
	}
F
Fabio Estevam 已提交
1631

1632 1633 1634 1635 1636 1637 1638
	/* Power up and upload firmware if necessary */
	ret = pm_runtime_get_sync(&dev->plat_dev->dev);
	if (ret < 0) {
		v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
		goto err_pm_get;
	}

1639 1640 1641 1642 1643 1644 1645 1646
	ret = clk_prepare_enable(dev->clk_per);
	if (ret)
		goto err_clk_per;

	ret = clk_prepare_enable(dev->clk_ahb);
	if (ret)
		goto err_clk_ahb;

1647
	set_default_params(ctx);
P
Philipp Zabel 已提交
1648 1649
	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
					    ctx->ops->queue_init);
1650 1651
	if (IS_ERR(ctx->fh.m2m_ctx)) {
		ret = PTR_ERR(ctx->fh.m2m_ctx);
1652 1653 1654

		v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
			 __func__, ret);
F
Fabio Estevam 已提交
1655
		goto err_ctx_init;
1656
	}
1657

1658 1659 1660
	ret = coda_ctrls_setup(ctx);
	if (ret) {
		v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
F
Fabio Estevam 已提交
1661
		goto err_ctrls_setup;
1662 1663 1664 1665
	}

	ctx->fh.ctrl_handler = &ctx->ctrls;

1666 1667
	ret = coda_alloc_context_buf(ctx, &ctx->parabuf,
				     CODA_PARA_BUF_SIZE, "parabuf");
1668
	if (ret < 0) {
1669
		v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
F
Fabio Estevam 已提交
1670
		goto err_dma_alloc;
1671 1672
	}

1673
	ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
1674 1675 1676
	ctx->bitstream.vaddr = dma_alloc_writecombine(
			&dev->plat_dev->dev, ctx->bitstream.size,
			&ctx->bitstream.paddr, GFP_KERNEL);
1677
	if (!ctx->bitstream.vaddr) {
P
Philipp Zabel 已提交
1678 1679
		v4l2_err(&dev->v4l2_dev,
			 "failed to allocate bitstream ringbuffer");
1680
		ret = -ENOMEM;
F
Fabio Estevam 已提交
1681
		goto err_dma_writecombine;
1682 1683 1684 1685
	}
	kfifo_init(&ctx->bitstream_fifo,
		ctx->bitstream.vaddr, ctx->bitstream.size);
	mutex_init(&ctx->bitstream_mutex);
1686
	mutex_init(&ctx->buffer_mutex);
1687
	INIT_LIST_HEAD(&ctx->buffer_meta_list);
1688

1689
	coda_lock(ctx);
1690
	list_add(&ctx->list, &dev->instances);
1691 1692 1693 1694 1695 1696 1697
	coda_unlock(ctx);

	v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
		 ctx->idx, ctx);

	return 0;

F
Fabio Estevam 已提交
1698 1699 1700 1701 1702 1703 1704
err_dma_writecombine:
	if (ctx->dev->devtype->product == CODA_DX6)
		coda_free_aux_buf(dev, &ctx->workbuf);
	coda_free_aux_buf(dev, &ctx->parabuf);
err_dma_alloc:
	v4l2_ctrl_handler_free(&ctx->ctrls);
err_ctrls_setup:
1705
	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
F
Fabio Estevam 已提交
1706 1707
err_ctx_init:
	clk_disable_unprepare(dev->clk_ahb);
1708
err_clk_ahb:
F
Fabio Estevam 已提交
1709
	clk_disable_unprepare(dev->clk_per);
1710
err_clk_per:
1711 1712
	pm_runtime_put_sync(&dev->plat_dev->dev);
err_pm_get:
1713 1714
	v4l2_fh_del(&ctx->fh);
	v4l2_fh_exit(&ctx->fh);
F
Fabio Estevam 已提交
1715 1716
	clear_bit(ctx->idx, &dev->instance_mask);
err_coda_max:
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
	kfree(ctx);
	return ret;
}

static int coda_release(struct file *file)
{
	struct coda_dev *dev = video_drvdata(file);
	struct coda_ctx *ctx = fh_to_ctx(file->private_data);

	v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
		 ctx);

1729 1730 1731
	if (ctx->inst_type == CODA_INST_DECODER)
		coda_bit_stream_end_flag(ctx);

1732
	/* If this instance is running, call .job_abort and wait for it to end */
1733
	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1734 1735

	/* In case the instance was not running, we still need to call SEQ_END */
1736 1737 1738
	if (ctx->initialized) {
		queue_work(dev->workqueue, &ctx->seq_end_work);
		flush_work(&ctx->seq_end_work);
1739 1740
	}

1741
	coda_lock(ctx);
1742
	list_del(&ctx->list);
1743 1744
	coda_unlock(ctx);

1745 1746 1747 1748
	if (ctx->bitstream.vaddr) {
		dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
			ctx->bitstream.vaddr, ctx->bitstream.paddr);
	}
1749 1750 1751 1752
	if (ctx->dev->devtype->product == CODA_DX6)
		coda_free_aux_buf(dev, &ctx->workbuf);

	coda_free_aux_buf(dev, &ctx->parabuf);
1753 1754
	v4l2_ctrl_handler_free(&ctx->ctrls);
	clk_disable_unprepare(dev->clk_ahb);
F
Fabio Estevam 已提交
1755
	clk_disable_unprepare(dev->clk_per);
1756
	pm_runtime_put_sync(&dev->plat_dev->dev);
1757 1758
	v4l2_fh_del(&ctx->fh);
	v4l2_fh_exit(&ctx->fh);
1759
	clear_bit(ctx->idx, &dev->instance_mask);
1760 1761
	if (ctx->ops->release)
		ctx->ops->release(ctx);
1762
	debugfs_remove_recursive(ctx->debugfs_entry);
1763 1764 1765 1766 1767
	kfree(ctx);

	return 0;
}

1768
static const struct v4l2_file_operations coda_fops = {
1769
	.owner		= THIS_MODULE,
1770
	.open		= coda_open,
1771
	.release	= coda_release,
1772
	.poll		= v4l2_m2m_fop_poll,
1773
	.unlocked_ioctl	= video_ioctl2,
1774
	.mmap		= v4l2_m2m_fop_mmap,
1775 1776
};

1777
static int coda_hw_init(struct coda_dev *dev)
1778 1779 1780
{
	u32 data;
	u16 *p;
1781 1782 1783 1784
	int i, ret;

	ret = clk_prepare_enable(dev->clk_per);
	if (ret)
1785
		goto err_clk_per;
1786

1787 1788 1789
	ret = clk_prepare_enable(dev->clk_ahb);
	if (ret)
		goto err_clk_ahb;
1790

1791 1792 1793
	if (dev->rstc)
		reset_control_reset(dev->rstc);

1794 1795
	/*
	 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
1796 1797
	 * The 16-bit chars in the code buffer are in memory access
	 * order, re-sort them to CODA order for register download.
1798 1799
	 * Data in this SRAM survives a reboot.
	 */
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
	p = (u16 *)dev->codebuf.vaddr;
	if (dev->devtype->product == CODA_DX6) {
		for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++)  {
			data = CODA_DOWN_ADDRESS_SET(i) |
				CODA_DOWN_DATA_SET(p[i ^ 1]);
			coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
		}
	} else {
		for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
			data = CODA_DOWN_ADDRESS_SET(i) |
				CODA_DOWN_DATA_SET(p[round_down(i, 4) +
							3 - (i % 4)]);
			coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
		}
1814 1815
	}

1816 1817 1818 1819
	/* Clear registers */
	for (i = 0; i < 64; i++)
		coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);

1820
	/* Tell the BIT where to find everything it needs */
1821 1822
	if (dev->devtype->product == CODA_960 ||
	    dev->devtype->product == CODA_7541) {
1823 1824
		coda_write(dev, dev->tempbuf.paddr,
				CODA_REG_BIT_TEMP_BUF_ADDR);
1825
		coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1826 1827 1828 1829
	} else {
		coda_write(dev, dev->workbuf.paddr,
			      CODA_REG_BIT_WORK_BUF_ADDR);
	}
1830 1831 1832 1833 1834 1835 1836
	coda_write(dev, dev->codebuf.paddr,
		      CODA_REG_BIT_CODE_BUF_ADDR);
	coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);

	/* Set default values */
	switch (dev->devtype->product) {
	case CODA_DX6:
P
Philipp Zabel 已提交
1837 1838
		coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
			   CODA_REG_BIT_STREAM_CTRL);
1839 1840
		break;
	default:
P
Philipp Zabel 已提交
1841 1842
		coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
			   CODA_REG_BIT_STREAM_CTRL);
1843
	}
1844 1845 1846 1847
	if (dev->devtype->product == CODA_960)
		coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
	else
		coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
1848 1849 1850 1851

	if (dev->devtype->product != CODA_DX6)
		coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);

1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
	coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
		      CODA_REG_BIT_INT_ENABLE);

	/* Reset VPU and start processor */
	data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
	data |= CODA_REG_RESET_ENABLE;
	coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
	udelay(10);
	data &= ~CODA_REG_RESET_ENABLE;
	coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
	coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);

1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
	clk_disable_unprepare(dev->clk_ahb);
	clk_disable_unprepare(dev->clk_per);

	return 0;

err_clk_ahb:
	clk_disable_unprepare(dev->clk_per);
err_clk_per:
	return ret;
}

1875
static int coda_register_device(struct coda_dev *dev, int i)
1876
{
1877 1878
	struct video_device *vfd = &dev->vfd[i];

1879
	if (i >= dev->devtype->num_vdevs)
1880 1881
		return -EINVAL;

1882 1883
	snprintf(vfd->name, sizeof(vfd->name), "%s",
		 dev->devtype->vdevs[i]->name);
1884 1885
	vfd->fops	= &coda_fops;
	vfd->ioctl_ops	= &coda_ioctl_ops;
1886 1887 1888 1889 1890 1891
	vfd->release	= video_device_release_empty,
	vfd->lock	= &dev->dev_mutex;
	vfd->v4l2_dev	= &dev->v4l2_dev;
	vfd->vfl_dir	= VFL_DIR_M2M;
	video_set_drvdata(vfd, dev);

1892 1893 1894 1895 1896
	/* Not applicable, use the selection API instead */
	v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
	v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
	v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);

1897 1898 1899
	return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
}

1900 1901 1902 1903
static void coda_fw_callback(const struct firmware *fw, void *context)
{
	struct coda_dev *dev = context;
	struct platform_device *pdev = dev->plat_dev;
1904
	int i, ret;
1905 1906 1907

	if (!fw) {
		v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
1908
		goto put_pm;
1909 1910 1911
	}

	/* allocate auxiliary per-device code buffer for the BIT processor */
1912 1913
	ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
				 dev->debugfs_root);
1914
	if (ret < 0) {
1915
		dev_err(&pdev->dev, "failed to allocate code buffer\n");
1916
		goto put_pm;
1917 1918
	}

1919 1920 1921 1922
	/* Copy the whole firmware image to the code buffer */
	memcpy(dev->codebuf.vaddr, fw->data, fw->size);
	release_firmware(fw);

1923 1924 1925 1926
	ret = coda_hw_init(dev);
	if (ret < 0) {
		v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
		goto put_pm;
1927 1928
	}

1929 1930 1931 1932
	ret = coda_check_firmware(dev);
	if (ret < 0)
		goto put_pm;

1933 1934 1935
	dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
	if (IS_ERR(dev->alloc_ctx)) {
		v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
1936
		goto put_pm;
1937 1938 1939 1940 1941 1942 1943 1944
	}

	dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
	if (IS_ERR(dev->m2m_dev)) {
		v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
		goto rel_ctx;
	}

1945 1946 1947 1948 1949 1950 1951 1952
	for (i = 0; i < dev->devtype->num_vdevs; i++) {
		ret = coda_register_device(dev, i);
		if (ret) {
			v4l2_err(&dev->v4l2_dev,
				 "Failed to register %s video device: %d\n",
				 dev->devtype->vdevs[i]->name, ret);
			goto rel_vfd;
		}
1953 1954 1955
	}

	v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
1956
		  dev->vfd[0].num, dev->vfd[i - 1].num);
1957

1958
	pm_runtime_put_sync(&pdev->dev);
1959 1960
	return;

1961 1962 1963
rel_vfd:
	while (--i >= 0)
		video_unregister_device(&dev->vfd[i]);
1964 1965 1966
	v4l2_m2m_release(dev->m2m_dev);
rel_ctx:
	vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
1967 1968
put_pm:
	pm_runtime_put_sync(&pdev->dev);
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
}

static int coda_firmware_request(struct coda_dev *dev)
{
	char *fw = dev->devtype->firmware;

	dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
		coda_product_name(dev->devtype->product));

	return request_firmware_nowait(THIS_MODULE, true,
		fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
}

enum coda_platform {
	CODA_IMX27,
1984
	CODA_IMX53,
1985 1986
	CODA_IMX6Q,
	CODA_IMX6DL,
1987 1988
};

1989
static const struct coda_devtype coda_devdata[] = {
1990
	[CODA_IMX27] = {
1991 1992 1993 1994
		.firmware     = "v4l-codadx6-imx27.bin",
		.product      = CODA_DX6,
		.codecs       = codadx6_codecs,
		.num_codecs   = ARRAY_SIZE(codadx6_codecs),
1995 1996
		.vdevs        = codadx6_video_devices,
		.num_vdevs    = ARRAY_SIZE(codadx6_video_devices),
1997
		.workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
1998
		.iram_size    = 0xb000,
1999
	},
2000
	[CODA_IMX53] = {
2001 2002 2003 2004
		.firmware     = "v4l-coda7541-imx53.bin",
		.product      = CODA_7541,
		.codecs       = coda7_codecs,
		.num_codecs   = ARRAY_SIZE(coda7_codecs),
2005 2006
		.vdevs        = coda7_video_devices,
		.num_vdevs    = ARRAY_SIZE(coda7_video_devices),
2007
		.workbuf_size = 128 * 1024,
2008
		.tempbuf_size = 304 * 1024,
2009
		.iram_size    = 0x14000,
2010
	},
2011
	[CODA_IMX6Q] = {
2012 2013 2014 2015
		.firmware     = "v4l-coda960-imx6q.bin",
		.product      = CODA_960,
		.codecs       = coda9_codecs,
		.num_codecs   = ARRAY_SIZE(coda9_codecs),
2016 2017
		.vdevs        = coda9_video_devices,
		.num_vdevs    = ARRAY_SIZE(coda9_video_devices),
2018
		.workbuf_size = 80 * 1024,
2019
		.tempbuf_size = 204 * 1024,
2020
		.iram_size    = 0x21000,
2021 2022
	},
	[CODA_IMX6DL] = {
2023 2024 2025 2026
		.firmware     = "v4l-coda960-imx6dl.bin",
		.product      = CODA_960,
		.codecs       = coda9_codecs,
		.num_codecs   = ARRAY_SIZE(coda9_codecs),
2027 2028
		.vdevs        = coda9_video_devices,
		.num_vdevs    = ARRAY_SIZE(coda9_video_devices),
2029
		.workbuf_size = 80 * 1024,
2030
		.tempbuf_size = 204 * 1024,
2031
		.iram_size    = 0x20000,
2032
	},
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
};

static struct platform_device_id coda_platform_ids[] = {
	{ .name = "coda-imx27", .driver_data = CODA_IMX27 },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(platform, coda_platform_ids);

#ifdef CONFIG_OF
static const struct of_device_id coda_dt_ids[] = {
2043
	{ .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
2044
	{ .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
2045 2046
	{ .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
	{ .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
2047 2048 2049 2050 2051
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, coda_dt_ids);
#endif

2052
static int coda_probe(struct platform_device *pdev)
2053 2054 2055 2056
{
	const struct of_device_id *of_id =
			of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
	const struct platform_device_id *pdev_id;
P
Philipp Zabel 已提交
2057 2058 2059
	struct coda_platform_data *pdata = pdev->dev.platform_data;
	struct device_node *np = pdev->dev.of_node;
	struct gen_pool *pool;
2060 2061 2062 2063
	struct coda_dev *dev;
	struct resource *res;
	int ret, irq;

P
Philipp Zabel 已提交
2064
	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2065
	if (!dev)
2066 2067
		return -ENOMEM;

2068 2069
	pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);

2070
	if (of_id) {
2071
		dev->devtype = of_id->data;
2072
	} else if (pdev_id) {
2073
		dev->devtype = &coda_devdata[pdev_id->driver_data];
2074 2075 2076
	} else {
		ret = -EINVAL;
		goto err_v4l2_register;
2077 2078 2079
	}

	spin_lock_init(&dev->irqlock);
2080
	INIT_LIST_HEAD(&dev->instances);
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096

	dev->plat_dev = pdev;
	dev->clk_per = devm_clk_get(&pdev->dev, "per");
	if (IS_ERR(dev->clk_per)) {
		dev_err(&pdev->dev, "Could not get per clock\n");
		return PTR_ERR(dev->clk_per);
	}

	dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
	if (IS_ERR(dev->clk_ahb)) {
		dev_err(&pdev->dev, "Could not get ahb clock\n");
		return PTR_ERR(dev->clk_ahb);
	}

	/* Get  memory for physical registers */
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2097 2098 2099
	dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(dev->regs_base))
		return PTR_ERR(dev->regs_base);
2100 2101

	/* IRQ */
2102 2103 2104
	irq = platform_get_irq_byname(pdev, "bit");
	if (irq < 0)
		irq = platform_get_irq(pdev, 0);
2105 2106
	if (irq < 0) {
		dev_err(&pdev->dev, "failed to get irq resource\n");
2107
		return irq;
2108 2109
	}

2110 2111 2112 2113 2114
	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
			IRQF_ONESHOT, dev_name(&pdev->dev), dev);
	if (ret < 0) {
		dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
		return ret;
2115 2116
	}

2117
	dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
2118 2119
	if (IS_ERR(dev->rstc)) {
		ret = PTR_ERR(dev->rstc);
2120
		if (ret == -ENOENT || ret == -ENOSYS) {
2121 2122
			dev->rstc = NULL;
		} else {
P
Philipp Zabel 已提交
2123 2124
			dev_err(&pdev->dev, "failed get reset control: %d\n",
				ret);
2125 2126 2127 2128
			return ret;
		}
	}

P
Philipp Zabel 已提交
2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
	/* Get IRAM pool from device tree or platform data */
	pool = of_get_named_gen_pool(np, "iram", 0);
	if (!pool && pdata)
		pool = dev_get_gen_pool(pdata->iram_dev);
	if (!pool) {
		dev_err(&pdev->dev, "iram pool not available\n");
		return -ENOMEM;
	}
	dev->iram_pool = pool;

2139 2140 2141 2142 2143
	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
	if (ret)
		return ret;

	mutex_init(&dev->dev_mutex);
2144
	mutex_init(&dev->coda_mutex);
2145

2146 2147 2148 2149
	dev->debugfs_root = debugfs_create_dir("coda", NULL);
	if (!dev->debugfs_root)
		dev_warn(&pdev->dev, "failed to create debugfs root\n");

2150
	/* allocate auxiliary per-device buffers for the BIT processor */
2151
	if (dev->devtype->product == CODA_DX6) {
2152
		ret = coda_alloc_aux_buf(dev, &dev->workbuf,
2153
					 dev->devtype->workbuf_size, "workbuf",
2154
					 dev->debugfs_root);
2155 2156
		if (ret < 0) {
			dev_err(&pdev->dev, "failed to allocate work buffer\n");
2157
			goto err_v4l2_register;
2158
		}
2159
	}
2160 2161

	if (dev->devtype->tempbuf_size) {
2162
		ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
2163
					 dev->devtype->tempbuf_size, "tempbuf",
2164
					 dev->debugfs_root);
2165 2166
		if (ret < 0) {
			dev_err(&pdev->dev, "failed to allocate temp buffer\n");
2167
			goto err_v4l2_register;
2168
		}
2169 2170
	}

2171
	dev->iram.size = dev->devtype->iram_size;
2172 2173 2174
	dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
					     &dev->iram.paddr);
	if (!dev->iram.vaddr) {
2175 2176
		dev_warn(&pdev->dev, "unable to alloc iram\n");
	} else {
2177
		memset(dev->iram.vaddr, 0, dev->iram.size);
2178 2179 2180 2181 2182
		dev->iram.blob.data = dev->iram.vaddr;
		dev->iram.blob.size = dev->iram.size;
		dev->iram.dentry = debugfs_create_blob("iram", 0644,
						       dev->debugfs_root,
						       &dev->iram.blob);
2183 2184
	}

2185 2186 2187
	dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
	if (!dev->workqueue) {
		dev_err(&pdev->dev, "unable to alloc workqueue\n");
2188 2189
		ret = -ENOMEM;
		goto err_v4l2_register;
2190 2191
	}

2192 2193
	platform_set_drvdata(pdev, dev);

2194 2195
	/*
	 * Start activated so we can directly call coda_hw_init in
2196
	 * coda_fw_callback regardless of whether CONFIG_PM is
2197 2198 2199 2200
	 * enabled or whether the device is associated with a PM domain.
	 */
	pm_runtime_get_noresume(&pdev->dev);
	pm_runtime_set_active(&pdev->dev);
2201 2202
	pm_runtime_enable(&pdev->dev);

2203
	return coda_firmware_request(dev);
2204 2205 2206 2207

err_v4l2_register:
	v4l2_device_unregister(&dev->v4l2_dev);
	return ret;
2208 2209 2210 2211 2212
}

static int coda_remove(struct platform_device *pdev)
{
	struct coda_dev *dev = platform_get_drvdata(pdev);
2213
	int i;
2214

2215 2216 2217 2218
	for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
		if (video_get_drvdata(&dev->vfd[i]))
			video_unregister_device(&dev->vfd[i]);
	}
2219 2220
	if (dev->m2m_dev)
		v4l2_m2m_release(dev->m2m_dev);
2221
	pm_runtime_disable(&pdev->dev);
2222 2223 2224
	if (dev->alloc_ctx)
		vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
	v4l2_device_unregister(&dev->v4l2_dev);
2225
	destroy_workqueue(dev->workqueue);
2226 2227 2228
	if (dev->iram.vaddr)
		gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
			      dev->iram.size);
2229 2230 2231
	coda_free_aux_buf(dev, &dev->codebuf);
	coda_free_aux_buf(dev, &dev->tempbuf);
	coda_free_aux_buf(dev, &dev->workbuf);
2232
	debugfs_remove_recursive(dev->debugfs_root);
2233 2234 2235
	return 0;
}

2236
#ifdef CONFIG_PM
2237 2238 2239 2240 2241
static int coda_runtime_resume(struct device *dev)
{
	struct coda_dev *cdev = dev_get_drvdata(dev);
	int ret = 0;

2242
	if (dev->pm_domain && cdev->codebuf.vaddr) {
2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
		ret = coda_hw_init(cdev);
		if (ret)
			v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
	}

	return ret;
}
#endif

static const struct dev_pm_ops coda_pm_ops = {
	SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
};

2256 2257
static struct platform_driver coda_driver = {
	.probe	= coda_probe,
2258
	.remove	= coda_remove,
2259 2260 2261
	.driver	= {
		.name	= CODA_NAME,
		.of_match_table = of_match_ptr(coda_dt_ids),
2262
		.pm	= &coda_pm_ops,
2263 2264 2265 2266 2267 2268 2269 2270 2271
	},
	.id_table = coda_platform_ids,
};

module_platform_driver(coda_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");