coda-common.c 52.5 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
#include "coda_regs.h"
43 44 45

#define CODA_NAME		"coda"

46
#define CODADX6_MAX_INSTANCES	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 63 64 65 66
MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");

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

69
void coda_write(struct coda_dev *dev, u32 data, u32 reg)
70 71 72 73 74 75
{
	v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
		 "%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 79 80 81 82 83 84 85
{
	u32 data;
	data = readl(dev->regs_base + reg);
	v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
		 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
	return data;
}

/*
86
 * Array of all formats supported by any version of Coda:
87
 */
88
static const struct coda_fmt coda_formats[] = {
89
	{
90
		.name = "YUV 4:2:0 Planar, YCbCr",
91 92
		.fourcc = V4L2_PIX_FMT_YUV420,
	},
93
	{
94 95
		.name = "YUV 4:2:0 Planar, YCrCb",
		.fourcc = V4L2_PIX_FMT_YVU420,
96 97 98 99 100 101 102 103 104 105 106
	},
	{
		.name = "H264 Encoded Stream",
		.fourcc = V4L2_PIX_FMT_H264,
	},
	{
		.name = "MPEG4 Encoded Stream",
		.fourcc = V4L2_PIX_FMT_MPEG4,
	},
};

107 108 109 110 111 112 113 114 115 116
#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
 */
117
static const struct coda_codec codadx6_codecs[] = {
118 119 120 121
	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),
};

122
static const struct coda_codec coda7_codecs[] = {
123 124
	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),
125 126
	CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264,   V4L2_PIX_FMT_YUV420, 1920, 1080),
	CODA_CODEC(CODA7_MODE_DECODE_MP4,  V4L2_PIX_FMT_MPEG4,  V4L2_PIX_FMT_YUV420, 1920, 1080),
127 128
};

129
static const struct coda_codec coda9_codecs[] = {
130 131 132 133 134 135
	CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,   1920, 1080),
	CODA_CODEC(CODA9_MODE_ENCODE_MP4,  V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4,  1920, 1080),
	CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264,   V4L2_PIX_FMT_YUV420, 1920, 1080),
	CODA_CODEC(CODA9_MODE_DECODE_MP4,  V4L2_PIX_FMT_MPEG4,  V4L2_PIX_FMT_YUV420, 1920, 1080),
};

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
static bool coda_format_is_yuv(u32 fourcc)
{
	switch (fourcc) {
	case V4L2_PIX_FMT_YUV420:
	case V4L2_PIX_FMT_YVU420:
		return true;
	default:
		return false;
	}
}

/*
 * 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;
}

156 157
static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
						int src_fourcc, int dst_fourcc)
158
{
159
	const struct coda_codec *codecs = dev->devtype->codecs;
160 161 162 163 164 165 166
	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;
167

168 169 170
	for (k = 0; k < num_codecs; k++) {
		if (codecs[k].src_fourcc == src_fourcc &&
		    codecs[k].dst_fourcc == dst_fourcc)
171 172 173
			break;
	}

174
	if (k == num_codecs)
175 176
		return NULL;

177
	return &codecs[k];
178 179
}

180
static void coda_get_max_dimensions(struct coda_dev *dev,
181
				    const struct coda_codec *codec,
182 183
				    int *max_w, int *max_h)
{
184
	const struct coda_codec *codecs = dev->devtype->codecs;
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
	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;
}

205
const char *coda_product_name(int product)
206 207 208 209 210 211 212 213
{
	static char buf[9];

	switch (product) {
	case CODA_DX6:
		return "CodaDx6";
	case CODA_7541:
		return "CODA7541";
214 215
	case CODA_960:
		return "CODA960";
216 217 218 219 220 221
	default:
		snprintf(buf, sizeof(buf), "(0x%04x)", product);
		return buf;
	}
}

222 223 224
/*
 * V4L2 ioctl() operations.
 */
225 226
static int coda_querycap(struct file *file, void *priv,
			 struct v4l2_capability *cap)
227
{
228 229
	struct coda_ctx *ctx = fh_to_ctx(priv);

230
	strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
231 232
	strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
		sizeof(cap->card));
233
	strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
234
	cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
235 236 237 238 239
	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;

	return 0;
}

240 241
static int coda_enum_fmt(struct file *file, void *priv,
			 struct v4l2_fmtdesc *f)
242 243
{
	struct coda_ctx *ctx = fh_to_ctx(priv);
244 245 246
	const struct coda_codec *codecs = ctx->dev->devtype->codecs;
	const struct coda_fmt *formats = coda_formats;
	const struct coda_fmt *fmt;
247 248 249
	int num_codecs = ctx->dev->devtype->num_codecs;
	int num_formats = ARRAY_SIZE(coda_formats);
	int i, k, num = 0;
250 251 252 253 254 255
	bool yuv;

	if (ctx->inst_type == CODA_INST_ENCODER)
		yuv = (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
	else
		yuv = (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE);
256 257

	for (i = 0; i < num_formats; i++) {
258 259 260 261 262
		/* Skip either raw or compressed formats */
		if (yuv != coda_format_is_yuv(formats[i].fourcc))
			continue;
		/* All uncompressed formats are always supported */
		if (yuv) {
263 264 265 266 267 268 269
			if (num == f->index)
				break;
			++num;
			continue;
		}
		/* Compressed formats may be supported, check the codec list */
		for (k = 0; k < num_codecs; k++) {
270 271
			if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
			    formats[i].fourcc == codecs[k].dst_fourcc)
272
				break;
273
			if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
274 275 276 277
			    formats[i].fourcc == codecs[k].src_fourcc)
				break;
		}
		if (k < num_codecs) {
278 279 280 281 282 283 284 285 286 287
			if (num == f->index)
				break;
			++num;
		}
	}

	if (i < num_formats) {
		fmt = &formats[i];
		strlcpy(f->description, fmt->name, sizeof(f->description));
		f->pixelformat = fmt->fourcc;
288
		if (!yuv)
289
			f->flags |= V4L2_FMT_FLAG_COMPRESSED;
290 291 292 293 294 295 296
		return 0;
	}

	/* Format not found */
	return -EINVAL;
}

297 298
static int coda_g_fmt(struct file *file, void *priv,
		      struct v4l2_format *f)
299 300 301 302 303
{
	struct coda_q_data *q_data;
	struct coda_ctx *ctx = fh_to_ctx(priv);

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

	f->fmt.pix.field	= V4L2_FIELD_NONE;
308
	f->fmt.pix.pixelformat	= q_data->fourcc;
309 310
	f->fmt.pix.width	= q_data->width;
	f->fmt.pix.height	= q_data->height;
311
	f->fmt.pix.bytesperline = q_data->bytesperline;
312 313 314 315 316 317 318

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

	return 0;
}

319
static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
320
			struct v4l2_format *f)
321
{
322 323
	struct coda_dev *dev = ctx->dev;
	struct coda_q_data *q_data;
324
	unsigned int max_w, max_h;
325 326 327 328 329 330 331 332 333 334 335 336
	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;

337 338 339 340 341 342 343 344 345 346 347 348 349 350
	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:
	case V4L2_PIX_FMT_H264:
	case V4L2_PIX_FMT_MPEG4:
	case V4L2_PIX_FMT_JPEG:
		break;
	default:
		q_data = get_q_data(ctx, f->type);
351 352
		if (!q_data)
			return -EINVAL;
353
		f->fmt.pix.pixelformat = q_data->fourcc;
354 355
	}

356 357 358
	switch (f->fmt.pix.pixelformat) {
	case V4L2_PIX_FMT_YUV420:
	case V4L2_PIX_FMT_YVU420:
359 360
		/* Frame stride must be multiple of 8, but 16 for h.264 */
		f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
361
		f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
362
					f->fmt.pix.height * 3 / 2;
363 364 365 366
		break;
	case V4L2_PIX_FMT_H264:
	case V4L2_PIX_FMT_MPEG4:
	case V4L2_PIX_FMT_JPEG:
367 368
		f->fmt.pix.bytesperline = 0;
		f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
369 370 371
		break;
	default:
		BUG();
372 373 374 375 376
	}

	return 0;
}

377 378
static int coda_try_fmt_vid_cap(struct file *file, void *priv,
				struct v4l2_format *f)
379 380
{
	struct coda_ctx *ctx = fh_to_ctx(priv);
381
	const struct coda_codec *codec = NULL;
382 383 384 385 386 387 388
	struct vb2_queue *src_vq;
	int ret;

	/*
	 * If the source format is already fixed, try to find a codec that
	 * converts to the given destination format
	 */
389
	src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
390 391
	if (vb2_is_streaming(src_vq)) {
		struct coda_q_data *q_data_src;
392

393 394 395 396 397
		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;
398 399 400

		f->fmt.pix.width = q_data_src->width;
		f->fmt.pix.height = q_data_src->height;
401 402 403 404 405
	} else {
		/* Otherwise determine codec by encoded format, if possible */
		codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
					f->fmt.pix.pixelformat);
	}
406 407 408

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

409
	ret = coda_try_fmt(ctx, codec, f);
410 411 412 413 414
	if (ret < 0)
		return ret;

	/* The h.264 decoder only returns complete 16x16 macroblocks */
	if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
415
		f->fmt.pix.width = f->fmt.pix.width;
416
		f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
417
		f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
418 419 420 421 422
		f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
				       f->fmt.pix.height * 3 / 2;
	}

	return 0;
423 424
}

425 426
static int coda_try_fmt_vid_out(struct file *file, void *priv,
				struct v4l2_format *f)
427 428
{
	struct coda_ctx *ctx = fh_to_ctx(priv);
429
	const struct coda_codec *codec;
430

431 432 433
	/* Determine codec by encoded format, returns NULL if raw or invalid */
	codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
				V4L2_PIX_FMT_YUV420);
434 435 436 437 438 439
	if (!codec && ctx->inst_type == CODA_INST_DECODER) {
		codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_H264,
					V4L2_PIX_FMT_YUV420);
		if (!codec)
			return -EINVAL;
	}
440 441 442 443

	if (!f->fmt.pix.colorspace)
		f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;

444
	return coda_try_fmt(ctx, codec, f);
445 446
}

447
static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
448 449 450 451
{
	struct coda_q_data *q_data;
	struct vb2_queue *vq;

452
	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
453 454 455 456 457 458 459 460 461 462 463 464
	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;
	}

465
	q_data->fourcc = f->fmt.pix.pixelformat;
466 467
	q_data->width = f->fmt.pix.width;
	q_data->height = f->fmt.pix.height;
468
	q_data->bytesperline = f->fmt.pix.bytesperline;
469
	q_data->sizeimage = f->fmt.pix.sizeimage;
470 471 472 473
	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;
474 475 476

	v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
		"Setting format for type %d, wxh: %dx%d, fmt: %d\n",
477
		f->type, q_data->width, q_data->height, q_data->fourcc);
478 479 480 481

	return 0;
}

482 483
static int coda_s_fmt_vid_cap(struct file *file, void *priv,
			      struct v4l2_format *f)
484
{
485
	struct coda_ctx *ctx = fh_to_ctx(priv);
486 487
	int ret;

488
	ret = coda_try_fmt_vid_cap(file, priv, f);
489 490 491
	if (ret)
		return ret;

492
	return coda_s_fmt(ctx, f);
493 494
}

495 496
static int coda_s_fmt_vid_out(struct file *file, void *priv,
			      struct v4l2_format *f)
497 498 499 500
{
	struct coda_ctx *ctx = fh_to_ctx(priv);
	int ret;

501
	ret = coda_try_fmt_vid_out(file, priv, f);
502 503 504
	if (ret)
		return ret;

505
	ret = coda_s_fmt(ctx, f);
506 507 508 509 510 511
	if (ret)
		ctx->colorspace = f->fmt.pix.colorspace;

	return ret;
}

512 513
static int coda_qbuf(struct file *file, void *priv,
		     struct v4l2_buffer *buf)
514 515 516
{
	struct coda_ctx *ctx = fh_to_ctx(priv);

517
	return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
518 519
}

520 521 522 523 524
static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
				      struct v4l2_buffer *buf)
{
	struct vb2_queue *src_vq;

525
	src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
526 527 528 529 530

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

531 532
static int coda_dqbuf(struct file *file, void *priv,
		      struct v4l2_buffer *buf)
533 534
{
	struct coda_ctx *ctx = fh_to_ctx(priv);
535 536
	int ret;

537
	ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
538

539 540 541 542 543 544 545 546 547 548 549
	/* 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;
550 551
}

552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
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;
}

596 597
static int coda_try_decoder_cmd(struct file *file, void *fh,
				struct v4l2_decoder_cmd *dc)
598 599 600 601
{
	if (dc->cmd != V4L2_DEC_CMD_STOP)
		return -EINVAL;

602
	if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
603 604
		return -EINVAL;

605
	if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
606 607
		return -EINVAL;

608 609 610 611 612 613 614 615 616 617 618 619 620 621
	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 */
622
	if (ctx->inst_type != CODA_INST_DECODER)
623
		return 0;
624

625 626
	/* Set the stream-end flag on this context */
	coda_bit_stream_end_flag(ctx);
627
	ctx->hold = false;
628
	v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
629

630 631 632
	return 0;
}

633 634
static int coda_subscribe_event(struct v4l2_fh *fh,
				const struct v4l2_event_subscription *sub)
635 636 637 638 639 640 641
{
	switch (sub->type) {
	case V4L2_EVENT_EOS:
		return v4l2_event_subscribe(fh, sub, 0, NULL);
	default:
		return v4l2_ctrl_subscribe_event(fh, sub);
	}
642 643 644
}

static const struct v4l2_ioctl_ops coda_ioctl_ops = {
645
	.vidioc_querycap	= coda_querycap,
646

647
	.vidioc_enum_fmt_vid_cap = coda_enum_fmt,
648 649 650
	.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,
651

652
	.vidioc_enum_fmt_vid_out = coda_enum_fmt,
653 654 655
	.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,
656

657 658
	.vidioc_reqbufs		= v4l2_m2m_ioctl_reqbufs,
	.vidioc_querybuf	= v4l2_m2m_ioctl_querybuf,
659

660
	.vidioc_qbuf		= coda_qbuf,
661
	.vidioc_expbuf		= v4l2_m2m_ioctl_expbuf,
662
	.vidioc_dqbuf		= coda_dqbuf,
663
	.vidioc_create_bufs	= v4l2_m2m_ioctl_create_bufs,
664

665 666
	.vidioc_streamon	= v4l2_m2m_ioctl_streamon,
	.vidioc_streamoff	= v4l2_m2m_ioctl_streamoff,
667

668 669
	.vidioc_g_selection	= coda_g_selection,

670
	.vidioc_try_decoder_cmd	= coda_try_decoder_cmd,
671
	.vidioc_decoder_cmd	= coda_decoder_cmd,
672

673
	.vidioc_subscribe_event = coda_subscribe_event,
674
	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
675 676
};

677
void coda_set_gdi_regs(struct coda_ctx *ctx)
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
{
	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);
}

698 699 700
/*
 * Mem-to-mem operations.
 */
701 702 703 704 705

static void coda_device_run(void *m2m_priv)
{
	struct coda_ctx *ctx = m2m_priv;
	struct coda_dev *dev = ctx->dev;
706 707 708 709 710 711 712 713 714 715 716

	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);
717 718
	mutex_lock(&dev->coda_mutex);

P
Philipp Zabel 已提交
719 720 721 722 723 724
	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;
725 726
	}

727 728
	if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
		dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
729 730

		ctx->hold = true;
731 732

		coda_hw_reset(ctx);
733
	} else if (!ctx->aborting) {
P
Philipp Zabel 已提交
734
		ctx->ops->finish_run(ctx);
735 736 737 738 739 740 741 742
	}

	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);

743
	v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
744 745 746 747 748 749 750 751
}

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

	/*
	 * For both 'P' and 'key' frame cases 1 picture
752 753
	 * and 1 frame are needed. In the decoder case,
	 * the compressed frame can be in the bitstream.
754
	 */
755
	if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
756
	    ctx->inst_type != CODA_INST_DECODER) {
757 758 759 760 761
		v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
			 "not ready: not enough video buffers.\n");
		return 0;
	}

762
	if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
763 764 765 766 767
		v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
			 "not ready: not enough video capture buffers.\n");
		return 0;
	}

768
	if (ctx->hold ||
769 770 771 772 773 774 775 776 777
	    ((ctx->inst_type == CODA_INST_DECODER) &&
	     (coda_get_bitstream_payload(ctx) < 512) &&
	     !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
		v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
			 "%d: not ready: not enough bitstream data.\n",
			 ctx->idx);
		return 0;
	}

778 779 780 781 782 783
	if (ctx->aborting) {
		v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
			 "not ready: aborting\n");
		return 0;
	}

784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
	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;
	mutex_lock(&pcdev->dev_mutex);
}

static void coda_unlock(void *m2m_priv)
{
	struct coda_ctx *ctx = m2m_priv;
	struct coda_dev *pcdev = ctx->dev;
	mutex_unlock(&pcdev->dev_mutex);
}

813
static const struct v4l2_m2m_ops coda_m2m_ops = {
814 815 816 817 818 819 820
	.device_run	= coda_device_run,
	.job_ready	= coda_job_ready,
	.job_abort	= coda_job_abort,
	.lock		= coda_lock,
	.unlock		= coda_unlock,
};

821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
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;
	}
}

847 848
static void set_default_params(struct coda_ctx *ctx)
{
849
	u32 src_fourcc, dst_fourcc;
850 851 852
	int max_w;
	int max_h;

853 854 855 856 857 858 859 860
	if (ctx->inst_type == CODA_INST_ENCODER) {
		src_fourcc = V4L2_PIX_FMT_YUV420;
		dst_fourcc = V4L2_PIX_FMT_H264;
	} else {
		src_fourcc = V4L2_PIX_FMT_H264;
		dst_fourcc = V4L2_PIX_FMT_YUV420;
	}
	ctx->codec = coda_find_codec(ctx->dev, src_fourcc, dst_fourcc);
861 862
	max_w = ctx->codec->max_w;
	max_h = ctx->codec->max_h;
863

864
	ctx->params.codec_mode = ctx->codec->mode;
865 866 867 868 869
	ctx->colorspace = V4L2_COLORSPACE_REC709;
	ctx->params.framerate = 30;
	ctx->aborting = 0;

	/* Default formats for output and input queues */
870 871 872 873 874 875
	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;
876 877 878 879 880 881 882 883 884 885 886
	if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
		ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
		ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
		ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
		ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
	} else {
		ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
		ctx->q_data[V4L2_M2M_SRC].sizeimage = CODA_MAX_FRAME_SIZE;
		ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
		ctx->q_data[V4L2_M2M_DST].sizeimage = (max_w * max_h * 3) / 2;
	}
887 888 889 890
	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;
891 892 893

	if (ctx->dev->devtype->product == CODA_960)
		coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
894 895 896 897 898 899 900 901 902 903 904
}

/*
 * 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);
905
	struct coda_q_data *q_data;
906 907
	unsigned int size;

908 909
	q_data = get_q_data(ctx, vq->type);
	size = q_data->sizeimage;
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942

	*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);
943 944 945 946 947 948 949 950 951 952 953
	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.
	 */
	if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
	    vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
		/*
954
		 * For backwards compatibility, queuing an empty buffer marks
955 956
		 * the stream end
		 */
957 958
		if (vb2_get_plane_payload(vb, 0) == 0)
			coda_bit_stream_end_flag(ctx);
959
		mutex_lock(&ctx->bitstream_mutex);
960
		v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
961 962
		if (vb2_is_streaming(vb->vb2_queue))
			coda_fill_bitstream(ctx);
963 964
		mutex_unlock(&ctx->bitstream_mutex);
	} else {
965
		v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
966
	}
967 968
}

969 970
int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
		       size_t size, const char *name, struct dentry *parent)
971 972 973 974 975 976 977 978
{
	buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
					GFP_KERNEL);
	if (!buf->vaddr)
		return -ENOMEM;

	buf->size = size;

979 980 981 982 983 984 985 986 987
	if (name && parent) {
		buf->blob.data = buf->vaddr;
		buf->blob.size = size;
		buf->dentry = debugfs_create_blob(name, 0644, parent, &buf->blob);
		if (!buf->dentry)
			dev_warn(&dev->plat_dev->dev,
				 "failed to create debugfs entry %s\n", name);
	}

988 989 990
	return 0;
}

991 992
void coda_free_aux_buf(struct coda_dev *dev,
		       struct coda_aux_buf *buf)
993 994 995 996 997 998 999
{
	if (buf->vaddr) {
		dma_free_coherent(&dev->plat_dev->dev, buf->size,
				  buf->vaddr, buf->paddr);
		buf->vaddr = NULL;
		buf->size = 0;
	}
1000
	debugfs_remove(buf->dentry);
1001 1002
}

1003
static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1004
{
1005 1006 1007
	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;
1008
	struct vb2_buffer *buf;
1009 1010
	u32 dst_fourcc;
	int ret = 0;
1011

1012 1013 1014 1015 1016 1017 1018
	q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
		if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
			/* copy the buffers that where queued before streamon */
			mutex_lock(&ctx->bitstream_mutex);
			coda_fill_bitstream(ctx);
			mutex_unlock(&ctx->bitstream_mutex);
1019

1020 1021 1022 1023
			if (coda_get_bitstream_payload(ctx) < 512) {
				ret = -EINVAL;
				goto err;
			}
1024
		} else {
1025 1026 1027 1028
			if (count < 1) {
				ret = -EINVAL;
				goto err;
			}
1029
		}
1030

1031
		ctx->streamon_out = 1;
1032
	} else {
1033 1034 1035 1036
		if (count < 1) {
			ret = -EINVAL;
			goto err;
		}
1037 1038

		ctx->streamon_cap = 1;
1039
	}
1040

1041 1042 1043
	/* Don't start the coda unless both queues are on */
	if (!(ctx->streamon_out & ctx->streamon_cap))
		return 0;
1044

1045 1046
	/* Allow decoder device_run with no new buffers queued */
	if (ctx->inst_type == CODA_INST_DECODER)
1047
		v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
1048

1049
	ctx->gopcounter = ctx->params.gop_size - 1;
1050
	q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1051 1052 1053 1054 1055
	dst_fourcc = q_data_dst->fourcc;

	ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
				     q_data_dst->fourcc);
	if (!ctx->codec) {
1056
		v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1057 1058
		ret = -EINVAL;
		goto err;
1059 1060
	}

P
Philipp Zabel 已提交
1061
	ret = ctx->ops->start_streaming(ctx);
1062
	if (ctx->inst_type == CODA_INST_DECODER) {
1063
		if (ret == -EAGAIN)
1064
			return 0;
1065
		else if (ret < 0)
1066
			goto err;
1067 1068
	}

1069 1070
	ctx->initialized = 1;
	return ret;
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080

err:
	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
		while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_DEQUEUED);
	} else {
		while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_DEQUEUED);
	}
	return ret;
1081 1082
}

1083
static void coda_stop_streaming(struct vb2_queue *q)
1084 1085
{
	struct coda_ctx *ctx = vb2_get_drv_priv(q);
1086
	struct coda_dev *dev = ctx->dev;
1087
	struct vb2_buffer *buf;
1088 1089

	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1090
		v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1091
			 "%s: output\n", __func__);
1092
		ctx->streamon_out = 0;
1093

1094
		coda_bit_stream_end_flag(ctx);
1095

1096
		ctx->isequence = 0;
1097 1098 1099

		while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1100
	} else {
1101
		v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1102
			 "%s: capture\n", __func__);
1103
		ctx->streamon_cap = 0;
1104

1105
		ctx->osequence = 0;
1106
		ctx->sequence_offset = 0;
1107 1108 1109

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

1112
	if (!ctx->streamon_out && !ctx->streamon_cap) {
1113 1114 1115 1116 1117 1118 1119 1120
		struct coda_timestamp *ts;

		while (!list_empty(&ctx->timestamp_list)) {
			ts = list_first_entry(&ctx->timestamp_list,
					      struct coda_timestamp, list);
			list_del(&ts->list);
			kfree(ts);
		}
1121 1122 1123 1124
		kfifo_init(&ctx->bitstream_fifo,
			ctx->bitstream.vaddr, ctx->bitstream.size);
		ctx->runcounter = 0;
	}
1125 1126
}

1127
static const struct vb2_ops coda_qops = {
1128 1129 1130 1131 1132
	.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,
1133 1134
	.wait_prepare		= vb2_ops_wait_prepare,
	.wait_finish		= vb2_ops_wait_finish,
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
};

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) {
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
	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;
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
	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;
1170 1171 1172 1173 1174 1175
	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;
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
	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;
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
	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;
1198 1199 1200
	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
		ctx->params.slice_max_bits = ctrl->val * 8;
		break;
1201 1202
	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
		break;
1203 1204 1205
	case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
		ctx->params.intra_refresh = ctrl->val;
		break;
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
	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;
}

1216
static const struct v4l2_ctrl_ops coda_ctrl_ops = {
1217 1218 1219 1220 1221 1222 1223
	.s_ctrl = coda_s_ctrl,
};

static int coda_ctrls_setup(struct coda_ctx *ctx)
{
	v4l2_ctrl_handler_init(&ctx->ctrls, 9);

1224 1225 1226 1227
	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);
1228 1229 1230 1231 1232
	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,
1233
		V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
1234
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1235
		V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
1236 1237 1238 1239 1240 1241
	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);
1242 1243 1244 1245 1246 1247 1248 1249
	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);
1250 1251 1252 1253 1254 1255
	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,
1256 1257
		V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
		V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
1258 1259
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
1260 1261
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
1262 1263 1264 1265 1266
	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);
1267 1268
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278

	if (ctx->ctrls.error) {
		v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
			ctx->ctrls.error);
		return -EINVAL;
	}

	return v4l2_ctrl_handler_setup(&ctx->ctrls);
}

1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
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);
}

1290 1291
int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
			    struct vb2_queue *dst_vq)
1292 1293 1294 1295
{
	int ret;

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

1299
	ret = coda_queue_init(priv, src_vq);
1300 1301 1302 1303
	if (ret)
		return ret;

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

1307 1308 1309
	return coda_queue_init(priv, dst_vq);
}

1310 1311
int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
			    struct vb2_queue *dst_vq)
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
{
	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);
1328 1329
}

1330 1331
static int coda_next_free_instance(struct coda_dev *dev)
{
1332 1333 1334 1335 1336 1337 1338
	int idx = ffz(dev->instance_mask);

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

	return idx;
1339 1340
}

P
Philipp Zabel 已提交
1341 1342
static int coda_open(struct file *file, enum coda_inst_type inst_type,
		     const struct coda_context_ops *ctx_ops)
1343 1344 1345
{
	struct coda_dev *dev = video_drvdata(file);
	struct coda_ctx *ctx = NULL;
1346
	char *name;
1347
	int ret;
1348
	int idx;
1349 1350 1351 1352 1353

	ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
	if (!ctx)
		return -ENOMEM;

F
Fabio Estevam 已提交
1354
	idx = coda_next_free_instance(dev);
1355 1356
	if (idx < 0) {
		ret = idx;
F
Fabio Estevam 已提交
1357 1358 1359 1360
		goto err_coda_max;
	}
	set_bit(idx, &dev->instance_mask);

1361 1362 1363 1364
	name = kasprintf(GFP_KERNEL, "context%d", idx);
	ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
	kfree(name);

1365
	ctx->inst_type = inst_type;
P
Philipp Zabel 已提交
1366
	ctx->ops = ctx_ops;
1367 1368
	init_completion(&ctx->completion);
	INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
P
Philipp Zabel 已提交
1369
	INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
1370 1371 1372 1373
	v4l2_fh_init(&ctx->fh, video_devdata(file));
	file->private_data = &ctx->fh;
	v4l2_fh_add(&ctx->fh);
	ctx->dev = dev;
1374
	ctx->idx = idx;
1375 1376
	switch (dev->devtype->product) {
	case CODA_7541:
1377
	case CODA_960:
1378 1379 1380 1381 1382
		ctx->reg_idx = 0;
		break;
	default:
		ctx->reg_idx = idx;
	}
F
Fabio Estevam 已提交
1383

1384 1385 1386 1387 1388 1389 1390
	/* 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;
	}

1391 1392 1393 1394 1395 1396 1397 1398
	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;

1399
	set_default_params(ctx);
P
Philipp Zabel 已提交
1400 1401
	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
					    ctx->ops->queue_init);
1402 1403
	if (IS_ERR(ctx->fh.m2m_ctx)) {
		ret = PTR_ERR(ctx->fh.m2m_ctx);
1404 1405 1406

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

1410 1411 1412
	ret = coda_ctrls_setup(ctx);
	if (ret) {
		v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
F
Fabio Estevam 已提交
1413
		goto err_ctrls_setup;
1414 1415 1416 1417
	}

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

1418 1419
	ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE,
				     "parabuf");
1420
	if (ret < 0) {
1421
		v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
F
Fabio Estevam 已提交
1422
		goto err_dma_alloc;
1423 1424
	}

1425 1426 1427 1428 1429 1430
	ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
	ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
			ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
	if (!ctx->bitstream.vaddr) {
		v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
		ret = -ENOMEM;
F
Fabio Estevam 已提交
1431
		goto err_dma_writecombine;
1432 1433 1434 1435
	}
	kfifo_init(&ctx->bitstream_fifo,
		ctx->bitstream.vaddr, ctx->bitstream.size);
	mutex_init(&ctx->bitstream_mutex);
1436
	mutex_init(&ctx->buffer_mutex);
1437
	INIT_LIST_HEAD(&ctx->timestamp_list);
1438

1439
	coda_lock(ctx);
1440
	list_add(&ctx->list, &dev->instances);
1441 1442 1443 1444 1445 1446 1447
	coda_unlock(ctx);

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

	return 0;

F
Fabio Estevam 已提交
1448 1449 1450 1451 1452 1453 1454
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:
1455
	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
F
Fabio Estevam 已提交
1456 1457
err_ctx_init:
	clk_disable_unprepare(dev->clk_ahb);
1458
err_clk_ahb:
F
Fabio Estevam 已提交
1459
	clk_disable_unprepare(dev->clk_per);
1460
err_clk_per:
1461 1462
	pm_runtime_put_sync(&dev->plat_dev->dev);
err_pm_get:
1463 1464
	v4l2_fh_del(&ctx->fh);
	v4l2_fh_exit(&ctx->fh);
F
Fabio Estevam 已提交
1465 1466
	clear_bit(ctx->idx, &dev->instance_mask);
err_coda_max:
1467 1468 1469 1470
	kfree(ctx);
	return ret;
}

1471 1472
static int coda_encoder_open(struct file *file)
{
1473
	return coda_open(file, CODA_INST_ENCODER, &coda_bit_encode_ops);
1474 1475 1476 1477
}

static int coda_decoder_open(struct file *file)
{
1478
	return coda_open(file, CODA_INST_DECODER, &coda_bit_decode_ops);
1479 1480
}

1481 1482 1483 1484 1485 1486 1487 1488
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);

1489 1490
	debugfs_remove_recursive(ctx->debugfs_entry);

1491
	/* If this instance is running, call .job_abort and wait for it to end */
1492
	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1493 1494

	/* In case the instance was not running, we still need to call SEQ_END */
1495 1496 1497
	if (ctx->initialized) {
		queue_work(dev->workqueue, &ctx->seq_end_work);
		flush_work(&ctx->seq_end_work);
1498 1499
	}

1500
	coda_lock(ctx);
1501
	list_del(&ctx->list);
1502 1503
	coda_unlock(ctx);

1504 1505
	dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
		ctx->bitstream.vaddr, ctx->bitstream.paddr);
1506 1507 1508 1509
	if (ctx->dev->devtype->product == CODA_DX6)
		coda_free_aux_buf(dev, &ctx->workbuf);

	coda_free_aux_buf(dev, &ctx->parabuf);
1510 1511
	v4l2_ctrl_handler_free(&ctx->ctrls);
	clk_disable_unprepare(dev->clk_ahb);
F
Fabio Estevam 已提交
1512
	clk_disable_unprepare(dev->clk_per);
1513
	pm_runtime_put_sync(&dev->plat_dev->dev);
1514 1515
	v4l2_fh_del(&ctx->fh);
	v4l2_fh_exit(&ctx->fh);
1516
	clear_bit(ctx->idx, &dev->instance_mask);
1517 1518
	if (ctx->ops->release)
		ctx->ops->release(ctx);
1519 1520 1521 1522 1523
	kfree(ctx);

	return 0;
}

1524
static const struct v4l2_file_operations coda_encoder_fops = {
1525
	.owner		= THIS_MODULE,
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
	.open		= coda_encoder_open,
	.release	= coda_release,
	.poll		= v4l2_m2m_fop_poll,
	.unlocked_ioctl	= video_ioctl2,
	.mmap		= v4l2_m2m_fop_mmap,
};

static const struct v4l2_file_operations coda_decoder_fops = {
	.owner		= THIS_MODULE,
	.open		= coda_decoder_open,
1536
	.release	= coda_release,
1537
	.poll		= v4l2_m2m_fop_poll,
1538
	.unlocked_ioctl	= video_ioctl2,
1539
	.mmap		= v4l2_m2m_fop_mmap,
1540 1541
};

1542
static int coda_hw_init(struct coda_dev *dev)
1543 1544 1545
{
	u32 data;
	u16 *p;
1546 1547 1548 1549
	int i, ret;

	ret = clk_prepare_enable(dev->clk_per);
	if (ret)
1550
		goto err_clk_per;
1551

1552 1553 1554
	ret = clk_prepare_enable(dev->clk_ahb);
	if (ret)
		goto err_clk_ahb;
1555

1556 1557 1558
	if (dev->rstc)
		reset_control_reset(dev->rstc);

1559 1560
	/*
	 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
1561 1562
	 * The 16-bit chars in the code buffer are in memory access
	 * order, re-sort them to CODA order for register download.
1563 1564
	 * Data in this SRAM survives a reboot.
	 */
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
	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);
		}
1579 1580
	}

1581 1582 1583 1584
	/* Clear registers */
	for (i = 0; i < 64; i++)
		coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);

1585
	/* Tell the BIT where to find everything it needs */
1586 1587
	if (dev->devtype->product == CODA_960 ||
	    dev->devtype->product == CODA_7541) {
1588 1589
		coda_write(dev, dev->tempbuf.paddr,
				CODA_REG_BIT_TEMP_BUF_ADDR);
1590
		coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1591 1592 1593 1594
	} else {
		coda_write(dev, dev->workbuf.paddr,
			      CODA_REG_BIT_WORK_BUF_ADDR);
	}
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
	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:
		coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
		break;
	default:
		coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
	}
1607 1608 1609 1610
	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);
1611 1612 1613 1614

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

1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
	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);

1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
	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;
}

1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
static int coda_register_device(struct coda_dev *dev, struct video_device *vfd)
{
	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);

	return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
}

1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
static void coda_fw_callback(const struct firmware *fw, void *context)
{
	struct coda_dev *dev = context;
	struct platform_device *pdev = dev->plat_dev;
	int ret;

	if (!fw) {
		v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
		return;
	}

	/* allocate auxiliary per-device code buffer for the BIT processor */
1661 1662
	ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
				 dev->debugfs_root);
1663
	if (ret < 0) {
1664 1665 1666 1667
		dev_err(&pdev->dev, "failed to allocate code buffer\n");
		return;
	}

1668 1669 1670 1671
	/* Copy the whole firmware image to the code buffer */
	memcpy(dev->codebuf.vaddr, fw->data, fw->size);
	release_firmware(fw);

1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
	if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
		/*
		 * Enabling power temporarily will cause coda_hw_init to be
		 * called via coda_runtime_resume by the pm domain.
		 */
		ret = pm_runtime_get_sync(&dev->plat_dev->dev);
		if (ret < 0) {
			v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
				 ret);
			return;
		}

1684 1685 1686 1687
		ret = coda_check_firmware(dev);
		if (ret < 0)
			return;

1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
		pm_runtime_put_sync(&dev->plat_dev->dev);
	} else {
		/*
		 * If runtime pm is disabled or pm_domain is not set,
		 * initialize once manually.
		 */
		ret = coda_hw_init(dev);
		if (ret < 0) {
			v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
			return;
		}
1699 1700 1701 1702

		ret = coda_check_firmware(dev);
		if (ret < 0)
			return;
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
	}

	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");
		return;
	}

	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;
	}

1717 1718 1719 1720
	dev->vfd[0].fops      = &coda_encoder_fops,
	dev->vfd[0].ioctl_ops = &coda_ioctl_ops;
	snprintf(dev->vfd[0].name, sizeof(dev->vfd[0].name), "coda-encoder");
	ret = coda_register_device(dev, &dev->vfd[0]);
1721
	if (ret) {
1722 1723
		v4l2_err(&dev->v4l2_dev,
			 "Failed to register encoder video device\n");
1724 1725
		goto rel_m2m;
	}
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738

	dev->vfd[1].fops      = &coda_decoder_fops,
	dev->vfd[1].ioctl_ops = &coda_ioctl_ops;
	snprintf(dev->vfd[1].name, sizeof(dev->vfd[1].name), "coda-decoder");
	ret = coda_register_device(dev, &dev->vfd[1]);
	if (ret) {
		v4l2_err(&dev->v4l2_dev,
			 "Failed to register decoder video device\n");
		goto rel_m2m;
	}

	v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
		  dev->vfd[0].num, dev->vfd[1].num);
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760

	return;

rel_m2m:
	v4l2_m2m_release(dev->m2m_dev);
rel_ctx:
	vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
}

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,
1761
	CODA_IMX53,
1762 1763
	CODA_IMX6Q,
	CODA_IMX6DL,
1764 1765
};

1766
static const struct coda_devtype coda_devdata[] = {
1767
	[CODA_IMX27] = {
1768 1769 1770 1771 1772
		.firmware     = "v4l-codadx6-imx27.bin",
		.product      = CODA_DX6,
		.codecs       = codadx6_codecs,
		.num_codecs   = ARRAY_SIZE(codadx6_codecs),
		.workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
1773
		.iram_size    = 0xb000,
1774
	},
1775
	[CODA_IMX53] = {
1776 1777 1778 1779 1780
		.firmware     = "v4l-coda7541-imx53.bin",
		.product      = CODA_7541,
		.codecs       = coda7_codecs,
		.num_codecs   = ARRAY_SIZE(coda7_codecs),
		.workbuf_size = 128 * 1024,
1781
		.tempbuf_size = 304 * 1024,
1782
		.iram_size    = 0x14000,
1783
	},
1784
	[CODA_IMX6Q] = {
1785 1786 1787 1788 1789
		.firmware     = "v4l-coda960-imx6q.bin",
		.product      = CODA_960,
		.codecs       = coda9_codecs,
		.num_codecs   = ARRAY_SIZE(coda9_codecs),
		.workbuf_size = 80 * 1024,
1790
		.tempbuf_size = 204 * 1024,
1791
		.iram_size    = 0x21000,
1792 1793
	},
	[CODA_IMX6DL] = {
1794 1795 1796 1797 1798
		.firmware     = "v4l-coda960-imx6dl.bin",
		.product      = CODA_960,
		.codecs       = coda9_codecs,
		.num_codecs   = ARRAY_SIZE(coda9_codecs),
		.workbuf_size = 80 * 1024,
1799
		.tempbuf_size = 204 * 1024,
1800
		.iram_size    = 0x20000,
1801
	},
1802 1803 1804 1805
};

static struct platform_device_id coda_platform_ids[] = {
	{ .name = "coda-imx27", .driver_data = CODA_IMX27 },
1806
	{ .name = "coda-imx53", .driver_data = CODA_IMX53 },
1807 1808 1809 1810 1811 1812
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(platform, coda_platform_ids);

#ifdef CONFIG_OF
static const struct of_device_id coda_dt_ids[] = {
1813
	{ .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
1814
	{ .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
1815 1816
	{ .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
	{ .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
1817 1818 1819 1820 1821
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, coda_dt_ids);
#endif

1822
static int coda_probe(struct platform_device *pdev)
1823 1824 1825 1826
{
	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 已提交
1827 1828 1829
	struct coda_platform_data *pdata = pdev->dev.platform_data;
	struct device_node *np = pdev->dev.of_node;
	struct gen_pool *pool;
1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841
	struct coda_dev *dev;
	struct resource *res;
	int ret, irq;

	dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
	if (!dev) {
		dev_err(&pdev->dev, "Not enough memory for %s\n",
			CODA_NAME);
		return -ENOMEM;
	}

	spin_lock_init(&dev->irqlock);
1842
	INIT_LIST_HEAD(&dev->instances);
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858

	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);
1859 1860 1861
	dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(dev->regs_base))
		return PTR_ERR(dev->regs_base);
1862 1863

	/* IRQ */
1864 1865 1866
	irq = platform_get_irq_byname(pdev, "bit");
	if (irq < 0)
		irq = platform_get_irq(pdev, 0);
1867 1868
	if (irq < 0) {
		dev_err(&pdev->dev, "failed to get irq resource\n");
1869
		return irq;
1870 1871
	}

1872 1873 1874 1875 1876
	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;
1877 1878
	}

1879
	dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
1880 1881
	if (IS_ERR(dev->rstc)) {
		ret = PTR_ERR(dev->rstc);
1882
		if (ret == -ENOENT || ret == -ENOSYS) {
1883 1884 1885 1886 1887 1888 1889
			dev->rstc = NULL;
		} else {
			dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
			return ret;
		}
	}

P
Philipp Zabel 已提交
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
	/* 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;

1900 1901 1902 1903 1904
	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
	if (ret)
		return ret;

	mutex_init(&dev->dev_mutex);
1905
	mutex_init(&dev->coda_mutex);
1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917

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

	if (of_id) {
		dev->devtype = of_id->data;
	} else if (pdev_id) {
		dev->devtype = &coda_devdata[pdev_id->driver_data];
	} else {
		v4l2_device_unregister(&dev->v4l2_dev);
		return -EINVAL;
	}

1918 1919 1920 1921
	dev->debugfs_root = debugfs_create_dir("coda", NULL);
	if (!dev->debugfs_root)
		dev_warn(&pdev->dev, "failed to create debugfs root\n");

1922
	/* allocate auxiliary per-device buffers for the BIT processor */
1923
	if (dev->devtype->product == CODA_DX6) {
1924
		ret = coda_alloc_aux_buf(dev, &dev->workbuf,
1925
					 dev->devtype->workbuf_size, "workbuf",
1926
					 dev->debugfs_root);
1927 1928 1929 1930 1931
		if (ret < 0) {
			dev_err(&pdev->dev, "failed to allocate work buffer\n");
			v4l2_device_unregister(&dev->v4l2_dev);
			return ret;
		}
1932
	}
1933 1934

	if (dev->devtype->tempbuf_size) {
1935
		ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
1936
					 dev->devtype->tempbuf_size, "tempbuf",
1937
					 dev->debugfs_root);
1938 1939 1940 1941 1942
		if (ret < 0) {
			dev_err(&pdev->dev, "failed to allocate temp buffer\n");
			v4l2_device_unregister(&dev->v4l2_dev);
			return ret;
		}
1943 1944
	}

1945
	dev->iram.size = dev->devtype->iram_size;
1946 1947 1948
	dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
					     &dev->iram.paddr);
	if (!dev->iram.vaddr) {
P
Philipp Zabel 已提交
1949 1950
		dev_err(&pdev->dev, "unable to alloc iram\n");
		return -ENOMEM;
1951 1952
	}

1953 1954 1955 1956 1957
	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);

1958 1959 1960 1961 1962 1963
	dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
	if (!dev->workqueue) {
		dev_err(&pdev->dev, "unable to alloc workqueue\n");
		return -ENOMEM;
	}

1964 1965
	platform_set_drvdata(pdev, dev);

1966 1967
	pm_runtime_enable(&pdev->dev);

1968 1969 1970 1971 1972 1973 1974
	return coda_firmware_request(dev);
}

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

1975 1976
	video_unregister_device(&dev->vfd[0]);
	video_unregister_device(&dev->vfd[1]);
1977 1978
	if (dev->m2m_dev)
		v4l2_m2m_release(dev->m2m_dev);
1979
	pm_runtime_disable(&pdev->dev);
1980 1981 1982
	if (dev->alloc_ctx)
		vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
	v4l2_device_unregister(&dev->v4l2_dev);
1983
	destroy_workqueue(dev->workqueue);
1984 1985 1986
	if (dev->iram.vaddr)
		gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
			      dev->iram.size);
1987 1988 1989
	coda_free_aux_buf(dev, &dev->codebuf);
	coda_free_aux_buf(dev, &dev->tempbuf);
	coda_free_aux_buf(dev, &dev->workbuf);
1990
	debugfs_remove_recursive(dev->debugfs_root);
1991 1992 1993
	return 0;
}

1994 1995 1996 1997 1998 1999
#ifdef CONFIG_PM_RUNTIME
static int coda_runtime_resume(struct device *dev)
{
	struct coda_dev *cdev = dev_get_drvdata(dev);
	int ret = 0;

2000
	if (dev->pm_domain && cdev->codebuf.vaddr) {
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
		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)
};

2014 2015
static struct platform_driver coda_driver = {
	.probe	= coda_probe,
2016
	.remove	= coda_remove,
2017 2018 2019 2020
	.driver	= {
		.name	= CODA_NAME,
		.owner	= THIS_MODULE,
		.of_match_table = of_match_ptr(coda_dt_ids),
2021
		.pm	= &coda_pm_ops,
2022 2023 2024 2025 2026 2027 2028 2029 2030
	},
	.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");