coda-common.c 60.6 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>
18
#include <linux/gcd.h>
P
Philipp Zabel 已提交
19
#include <linux/genalloc.h>
20 21 22
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/irq.h>
23
#include <linux/kfifo.h>
24 25 26
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
27
#include <linux/pm_runtime.h>
28 29 30
#include <linux/slab.h>
#include <linux/videodev2.h>
#include <linux/of.h>
P
Philipp Zabel 已提交
31
#include <linux/platform_data/coda.h>
32
#include <linux/reset.h>
33 34 35

#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
36
#include <media/v4l2-event.h>
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 <media/videobuf2-vmalloc.h>
42

43
#include "coda.h"
44 45 46

#define CODA_NAME		"coda"

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

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

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

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

72
unsigned int coda_read(struct coda_dev *dev, u32 reg)
73 74
{
	u32 data;
P
Philipp Zabel 已提交
75

76
	data = readl(dev->regs_base + reg);
77
	v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
78 79 80 81
		 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
	return data;
}

82 83 84 85 86 87 88 89 90 91 92 93 94
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:
95
	case V4L2_PIX_FMT_NV12:
96 97 98 99
	default:
		base_cb = base_y + q_data->bytesperline * q_data->height;
		base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
		break;
100 101 102
	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;
103 104 105 106 107 108 109
	}

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

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

125
static const struct coda_codec coda7_codecs[] = {
126 127
	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),
128
	CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG,   8192, 8192),
129 130
	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),
131
	CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG,   V4L2_PIX_FMT_YUV420, 8192, 8192),
132 133
};

134
static const struct coda_codec coda9_codecs[] = {
135 136 137 138
	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),
139 140
};

141 142 143 144
struct coda_video_device {
	const char *name;
	enum coda_inst_type type;
	const struct coda_context_ops *ops;
145
	bool direct;
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	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,
	},
};

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
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,
	},
};

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
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,
	},
};

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
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,
	},
};

210 211 212 213 214
static const struct coda_video_device *codadx6_video_devices[] = {
	&coda_bit_encoder,
};

static const struct coda_video_device *coda7_video_devices[] = {
215 216
	&coda_bit_jpeg_encoder,
	&coda_bit_jpeg_decoder,
217 218 219 220 221 222 223 224 225
	&coda_bit_encoder,
	&coda_bit_decoder,
};

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

226 227 228 229 230
/*
 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
 * tables.
 */
static u32 coda_format_normalize_yuv(u32 fourcc)
231 232 233 234
{
	switch (fourcc) {
	case V4L2_PIX_FMT_YUV420:
	case V4L2_PIX_FMT_YVU420:
235
	case V4L2_PIX_FMT_NV12:
236
	case V4L2_PIX_FMT_YUV422P:
237
		return V4L2_PIX_FMT_YUV420;
238
	default:
239
		return fourcc;
240
	}
241 242
}

243 244
static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
						int src_fourcc, int dst_fourcc)
245
{
246
	const struct coda_codec *codecs = dev->devtype->codecs;
247 248 249 250 251 252 253
	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;
254

255 256 257
	for (k = 0; k < num_codecs; k++) {
		if (codecs[k].src_fourcc == src_fourcc &&
		    codecs[k].dst_fourcc == dst_fourcc)
258 259 260
			break;
	}

261
	if (k == num_codecs)
262 263
		return NULL;

264
	return &codecs[k];
265 266
}

267
static void coda_get_max_dimensions(struct coda_dev *dev,
268
				    const struct coda_codec *codec,
269 270
				    int *max_w, int *max_h)
{
271
	const struct coda_codec *codecs = dev->devtype->codecs;
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	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;
}

292 293 294 295 296 297 298 299 300 301 302
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];
}

303
const char *coda_product_name(int product)
304 305 306 307 308 309 310 311
{
	static char buf[9];

	switch (product) {
	case CODA_DX6:
		return "CodaDx6";
	case CODA_7541:
		return "CODA7541";
312 313
	case CODA_960:
		return "CODA960";
314 315 316 317 318 319
	default:
		snprintf(buf, sizeof(buf), "(0x%04x)", product);
		return buf;
	}
}

320 321 322
/*
 * V4L2 ioctl() operations.
 */
323 324
static int coda_querycap(struct file *file, void *priv,
			 struct v4l2_capability *cap)
325
{
326 327
	struct coda_ctx *ctx = fh_to_ctx(priv);

328
	strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
329 330
	strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
		sizeof(cap->card));
331
	strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
332
	cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
333 334 335 336 337
	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;

	return 0;
}

338 339
static int coda_enum_fmt(struct file *file, void *priv,
			 struct v4l2_fmtdesc *f)
340
{
341 342 343 344 345 346 347 348
	struct video_device *vdev = video_devdata(file);
	const struct coda_video_device *cvd = to_coda_video_device(vdev);
	const u32 *formats;

	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;
349
	else
350
		return -EINVAL;
351

352 353 354 355
	if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
		return -EINVAL;

	f->pixelformat = formats[f->index];
356

357
	return 0;
358 359
}

360 361
static int coda_g_fmt(struct file *file, void *priv,
		      struct v4l2_format *f)
362 363 364 365 366
{
	struct coda_q_data *q_data;
	struct coda_ctx *ctx = fh_to_ctx(priv);

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

	f->fmt.pix.field	= V4L2_FIELD_NONE;
371
	f->fmt.pix.pixelformat	= q_data->fourcc;
372 373
	f->fmt.pix.width	= q_data->width;
	f->fmt.pix.height	= q_data->height;
374
	f->fmt.pix.bytesperline = q_data->bytesperline;
375 376

	f->fmt.pix.sizeimage	= q_data->sizeimage;
377 378 379 380
	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
		f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
	else
		f->fmt.pix.colorspace = ctx->colorspace;
381 382 383 384

	return 0;
}

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
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;
408 409 410 411

	return 0;
}

412 413 414 415 416 417 418 419 420 421 422 423
static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
					    u32 width, u32 height)
{
	/*
	 * 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.
	 */
	return round_up(clamp(sizeimage, width * height / 8,
					 width * height * 2), PAGE_SIZE);
}

424
static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
425
			struct v4l2_format *f)
426
{
427
	struct coda_dev *dev = ctx->dev;
428
	unsigned int max_w, max_h;
429 430 431 432 433 434 435 436 437 438 439 440
	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;

441 442 443 444 445 446 447 448
	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:
449
	case V4L2_PIX_FMT_NV12:
450 451 452 453
		/*
		 * Frame stride must be at least multiple of 8,
		 * but multiple of 16 for h.264 or JPEG 4:2:x
		 */
454
		f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
455
		f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
456
					f->fmt.pix.height * 3 / 2;
457
		break;
458 459 460 461 462
	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;
463 464 465
	case V4L2_PIX_FMT_JPEG:
		f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
		/* fallthrough */
466 467
	case V4L2_PIX_FMT_H264:
	case V4L2_PIX_FMT_MPEG4:
468
		f->fmt.pix.bytesperline = 0;
469 470 471 472
		f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
							f->fmt.pix.sizeimage,
							f->fmt.pix.width,
							f->fmt.pix.height);
473 474 475
		break;
	default:
		BUG();
476 477 478 479 480
	}

	return 0;
}

481 482
static int coda_try_fmt_vid_cap(struct file *file, void *priv,
				struct v4l2_format *f)
483 484
{
	struct coda_ctx *ctx = fh_to_ctx(priv);
485 486
	const struct coda_q_data *q_data_src;
	const struct coda_codec *codec;
487 488 489
	struct vb2_queue *src_vq;
	int ret;

490 491 492 493 494 495
	ret = coda_try_pixelformat(ctx, f);
	if (ret < 0)
		return ret;

	q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);

496
	/*
497 498
	 * If the source format is already fixed, only allow the same output
	 * resolution
499
	 */
500
	src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
501
	if (vb2_is_streaming(src_vq)) {
502 503
		f->fmt.pix.width = q_data_src->width;
		f->fmt.pix.height = q_data_src->height;
504
	}
505 506 507

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

508 509 510 511 512 513
	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;

514
	ret = coda_try_fmt(ctx, codec, f);
515 516 517 518 519
	if (ret < 0)
		return ret;

	/* The h.264 decoder only returns complete 16x16 macroblocks */
	if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
520
		f->fmt.pix.width = f->fmt.pix.width;
521
		f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
522
		f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
523 524 525 526 527
		f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
				       f->fmt.pix.height * 3 / 2;
	}

	return 0;
528 529
}

530 531
static int coda_try_fmt_vid_out(struct file *file, void *priv,
				struct v4l2_format *f)
532 533
{
	struct coda_ctx *ctx = fh_to_ctx(priv);
534 535 536 537
	struct coda_dev *dev = ctx->dev;
	const struct coda_q_data *q_data_dst;
	const struct coda_codec *codec;
	int ret;
538

539 540 541
	ret = coda_try_pixelformat(ctx, f);
	if (ret < 0)
		return ret;
542

543 544 545 546 547
	switch (f->fmt.pix.colorspace) {
	case V4L2_COLORSPACE_REC709:
	case V4L2_COLORSPACE_JPEG:
		break;
	default:
548 549 550 551
		if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
			f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
		else
			f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
552
	}
553

554 555
	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);
556

557
	return coda_try_fmt(ctx, codec, f);
558 559
}

560
static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
561 562 563 564
{
	struct coda_q_data *q_data;
	struct vb2_queue *vq;

565
	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
566 567 568 569 570 571 572 573 574 575 576 577
	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;
	}

578
	q_data->fourcc = f->fmt.pix.pixelformat;
579 580
	q_data->width = f->fmt.pix.width;
	q_data->height = f->fmt.pix.height;
581
	q_data->bytesperline = f->fmt.pix.bytesperline;
582
	q_data->sizeimage = f->fmt.pix.sizeimage;
583 584 585 586
	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;
587 588 589

	v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
		"Setting format for type %d, wxh: %dx%d, fmt: %d\n",
590
		f->type, q_data->width, q_data->height, q_data->fourcc);
591 592 593 594

	return 0;
}

595 596
static int coda_s_fmt_vid_cap(struct file *file, void *priv,
			      struct v4l2_format *f)
597
{
598
	struct coda_ctx *ctx = fh_to_ctx(priv);
599 600
	int ret;

601
	ret = coda_try_fmt_vid_cap(file, priv, f);
602 603 604
	if (ret)
		return ret;

605
	return coda_s_fmt(ctx, f);
606 607
}

608 609
static int coda_s_fmt_vid_out(struct file *file, void *priv,
			      struct v4l2_format *f)
610 611
{
	struct coda_ctx *ctx = fh_to_ctx(priv);
612
	struct v4l2_format f_cap;
613 614
	int ret;

615
	ret = coda_try_fmt_vid_out(file, priv, f);
616 617 618
	if (ret)
		return ret;

619
	ret = coda_s_fmt(ctx, f);
620
	if (ret)
621 622 623
		return ret;

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

625
	memset(&f_cap, 0, sizeof(f_cap));
626 627 628 629 630 631 632 633 634 635
	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);
636 637
}

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
static int coda_reqbufs(struct file *file, void *priv,
			struct v4l2_requestbuffers *rb)
{
	struct coda_ctx *ctx = fh_to_ctx(priv);
	int ret;

	ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
	if (ret)
		return ret;

	/*
	 * Allow to allocate instance specific per-context buffers, such as
	 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
	 */
	if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
		return ctx->ops->reqbufs(ctx, rb);

	return 0;
}

658 659
static int coda_qbuf(struct file *file, void *priv,
		     struct v4l2_buffer *buf)
660 661 662
{
	struct coda_ctx *ctx = fh_to_ctx(priv);

663
	return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
664 665
}

666
static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
667
				      struct vb2_buffer *buf)
668 669 670
{
	struct vb2_queue *src_vq;

671
	src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
672 673

	return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
674
		(buf->v4l2_buf.sequence == (ctx->qsequence - 1)));
675 676
}

677 678
void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_buffer *buf,
		       enum vb2_buffer_state state)
679
{
680 681 682
	const struct v4l2_event eos_event = {
		.type = V4L2_EVENT_EOS
	};
683

684 685
	if (coda_buf_is_end_of_stream(ctx, buf)) {
		buf->v4l2_buf.flags |= V4L2_BUF_FLAG_LAST;
686 687 688 689

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

690
	v4l2_m2m_buf_done(buf, state);
691 692
}

693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
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;
}

737 738
static int coda_try_decoder_cmd(struct file *file, void *fh,
				struct v4l2_decoder_cmd *dc)
739 740 741 742
{
	if (dc->cmd != V4L2_DEC_CMD_STOP)
		return -EINVAL;

743
	if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
744 745
		return -EINVAL;

746
	if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
747 748
		return -EINVAL;

749 750 751 752 753 754 755 756 757 758 759 760 761 762
	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 */
763
	if (ctx->inst_type != CODA_INST_DECODER)
764
		return 0;
765

766 767
	/* Set the stream-end flag on this context */
	coda_bit_stream_end_flag(ctx);
768
	ctx->hold = false;
769
	v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
770

771 772 773
	return 0;
}

774 775 776 777 778 779 780 781 782 783 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 813 814 815 816 817 818 819 820 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 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
{
	struct coda_ctx *ctx = fh_to_ctx(fh);
	struct v4l2_fract *tpf;

	if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
		return -EINVAL;

	a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
	tpf = &a->parm.output.timeperframe;
	tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
	tpf->numerator = 1 + (ctx->params.framerate >>
			      CODA_FRATE_DIV_OFFSET);

	return 0;
}

/*
 * Approximate timeperframe v4l2_fract with values that can be written
 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
 */
static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
{
	struct v4l2_fract s = *timeperframe;
	struct v4l2_fract f0;
	struct v4l2_fract f1 = { 1, 0 };
	struct v4l2_fract f2 = { 0, 1 };
	unsigned int i, div, s_denominator;

	/* Lower bound is 1/65535 */
	if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
		timeperframe->numerator = 1;
		timeperframe->denominator = 65535;
		return;
	}

	/* Upper bound is 65536/1, map everything above to infinity */
	if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
		timeperframe->numerator = 1;
		timeperframe->denominator = 0;
		return;
	}

	/* Reduce fraction to lowest terms */
	div = gcd(s.numerator, s.denominator);
	if (div > 1) {
		s.numerator /= div;
		s.denominator /= div;
	}

	if (s.numerator <= 65536 && s.denominator < 65536) {
		*timeperframe = s;
		return;
	}

	/* Find successive convergents from continued fraction expansion */
	while (f2.numerator <= 65536 && f2.denominator < 65536) {
		f0 = f1;
		f1 = f2;

		/* Stop when f2 exactly equals timeperframe */
		if (s.numerator == 0)
			break;

		i = s.denominator / s.numerator;

		f2.numerator = f0.numerator + i * f1.numerator;
		f2.denominator = f0.denominator + i * f2.denominator;

		s_denominator = s.numerator;
		s.numerator = s.denominator % s.numerator;
		s.denominator = s_denominator;
	}

	*timeperframe = f1;
}

static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
{
	return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
		timeperframe->denominator;
}

static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
{
	struct coda_ctx *ctx = fh_to_ctx(fh);
	struct v4l2_fract *tpf;

	if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
		return -EINVAL;

	tpf = &a->parm.output.timeperframe;
	coda_approximate_timeperframe(tpf);
	ctx->params.framerate = coda_timeperframe_to_frate(tpf);

	return 0;
}

872 873
static int coda_subscribe_event(struct v4l2_fh *fh,
				const struct v4l2_event_subscription *sub)
874 875 876 877 878 879 880
{
	switch (sub->type) {
	case V4L2_EVENT_EOS:
		return v4l2_event_subscribe(fh, sub, 0, NULL);
	default:
		return v4l2_ctrl_subscribe_event(fh, sub);
	}
881 882 883
}

static const struct v4l2_ioctl_ops coda_ioctl_ops = {
884
	.vidioc_querycap	= coda_querycap,
885

886
	.vidioc_enum_fmt_vid_cap = coda_enum_fmt,
887 888 889
	.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,
890

891
	.vidioc_enum_fmt_vid_out = coda_enum_fmt,
892 893 894
	.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,
895

896
	.vidioc_reqbufs		= coda_reqbufs,
897
	.vidioc_querybuf	= v4l2_m2m_ioctl_querybuf,
898

899
	.vidioc_qbuf		= coda_qbuf,
900
	.vidioc_expbuf		= v4l2_m2m_ioctl_expbuf,
901
	.vidioc_dqbuf		= v4l2_m2m_ioctl_dqbuf,
902
	.vidioc_create_bufs	= v4l2_m2m_ioctl_create_bufs,
903

904 905
	.vidioc_streamon	= v4l2_m2m_ioctl_streamon,
	.vidioc_streamoff	= v4l2_m2m_ioctl_streamoff,
906

907 908
	.vidioc_g_selection	= coda_g_selection,

909
	.vidioc_try_decoder_cmd	= coda_try_decoder_cmd,
910
	.vidioc_decoder_cmd	= coda_decoder_cmd,
911

912 913 914
	.vidioc_g_parm		= coda_g_parm,
	.vidioc_s_parm		= coda_s_parm,

915
	.vidioc_subscribe_event = coda_subscribe_event,
916
	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
917 918
};

919
void coda_set_gdi_regs(struct coda_ctx *ctx)
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
{
	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);
}

940 941 942
/*
 * Mem-to-mem operations.
 */
943 944 945 946 947

static void coda_device_run(void *m2m_priv)
{
	struct coda_ctx *ctx = m2m_priv;
	struct coda_dev *dev = ctx->dev;
948 949 950 951 952 953 954 955 956 957 958

	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);
959 960
	mutex_lock(&dev->coda_mutex);

P
Philipp Zabel 已提交
961 962 963 964 965 966
	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;
967 968
	}

P
Philipp Zabel 已提交
969 970
	if (!wait_for_completion_timeout(&ctx->completion,
					 msecs_to_jiffies(1000))) {
971
		dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
972 973

		ctx->hold = true;
974 975

		coda_hw_reset(ctx);
976
	} else if (!ctx->aborting) {
P
Philipp Zabel 已提交
977
		ctx->ops->finish_run(ctx);
978 979
	}

980 981
	if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
	    ctx->ops->seq_end_work)
982 983 984 985 986
		queue_work(dev->workqueue, &ctx->seq_end_work);

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

987
	v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
988 989 990 991 992
}

static int coda_job_ready(void *m2m_priv)
{
	struct coda_ctx *ctx = m2m_priv;
993
	int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
994 995 996

	/*
	 * For both 'P' and 'key' frame cases 1 picture
997 998
	 * and 1 frame are needed. In the decoder case,
	 * the compressed frame can be in the bitstream.
999
	 */
1000
	if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
1001 1002 1003 1004 1005
		v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
			 "not ready: not enough video buffers.\n");
		return 0;
	}

1006
	if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
1007 1008 1009 1010 1011
		v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
			 "not ready: not enough video capture buffers.\n");
		return 0;
	}

1012
	if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1013 1014 1015
		bool stream_end = ctx->bit_stream_param &
				  CODA_BIT_STREAM_END_FLAG;
		int num_metas = ctx->num_metas;
1016

1017
		if (ctx->hold && !src_bufs) {
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
			v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
				 "%d: not ready: on hold for more buffers.\n",
				 ctx->idx);
			return 0;
		}

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


1032 1033
		if (!src_bufs && !stream_end &&
		    (coda_get_bitstream_payload(ctx) < 512)) {
1034 1035 1036 1037 1038
			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;
		}
1039 1040
	}

1041 1042 1043 1044 1045 1046
	if (ctx->aborting) {
		v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
			 "not ready: aborting\n");
		return 0;
	}

1047 1048
	v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
			"job ready\n");
1049

1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
	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 已提交
1067

1068 1069 1070 1071 1072 1073 1074
	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 已提交
1075

1076 1077 1078
	mutex_unlock(&pcdev->dev_mutex);
}

1079
static const struct v4l2_m2m_ops coda_m2m_ops = {
1080 1081 1082 1083 1084 1085 1086
	.device_run	= coda_device_run,
	.job_ready	= coda_job_ready,
	.job_abort	= coda_job_abort,
	.lock		= coda_lock,
	.unlock		= coda_unlock,
};

1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
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;
	}
}

1113 1114
static void set_default_params(struct coda_ctx *ctx)
{
1115
	unsigned int max_w, max_h, usize, csize;
1116

1117 1118
	ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
				     ctx->cvd->dst_formats[0]);
1119 1120
	max_w = min(ctx->codec->max_w, 1920U);
	max_h = min(ctx->codec->max_h, 1088U);
1121 1122
	usize = max_w * max_h * 3 / 2;
	csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
1123

1124
	ctx->params.codec_mode = ctx->codec->mode;
1125 1126 1127 1128
	ctx->colorspace = V4L2_COLORSPACE_REC709;
	ctx->params.framerate = 30;

	/* Default formats for output and input queues */
1129 1130 1131 1132 1133 1134
	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;
1135 1136
	if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
		ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1137
		ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
1138
		ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1139
		ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
1140 1141
	} else {
		ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1142
		ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
1143
		ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1144
		ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
1145
	}
1146 1147 1148 1149
	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;
1150 1151 1152

	if (ctx->dev->devtype->product == CODA_960)
		coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
}

/*
 * 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);
1164
	struct coda_q_data *q_data;
1165 1166
	unsigned int size;

1167 1168
	q_data = get_q_data(ctx, vq->type);
	size = q_data->sizeimage;
1169 1170 1171 1172

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

1173
	/* Set to vb2-dma-contig allocator context, ignored by vb2-vmalloc */
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
	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);
1203
	struct vb2_queue *vq = vb->vb2_queue;
1204 1205 1206 1207 1208 1209 1210 1211
	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.
	 */
1212
	if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1213
		/*
1214
		 * For backwards compatibility, queuing an empty buffer marks
1215 1216
		 * the stream end
		 */
1217 1218
		if (vb2_get_plane_payload(vb, 0) == 0)
			coda_bit_stream_end_flag(ctx);
1219
		mutex_lock(&ctx->bitstream_mutex);
1220
		v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
1221
		if (vb2_is_streaming(vb->vb2_queue))
1222
			coda_fill_bitstream(ctx, true);
1223 1224
		mutex_unlock(&ctx->bitstream_mutex);
	} else {
1225
		v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
1226
	}
1227 1228
}

1229 1230
int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
		       size_t size, const char *name, struct dentry *parent)
1231 1232 1233
{
	buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
					GFP_KERNEL);
1234 1235 1236 1237
	if (!buf->vaddr) {
		v4l2_err(&dev->v4l2_dev,
			 "Failed to allocate %s buffer of size %u\n",
			 name, size);
1238
		return -ENOMEM;
1239
	}
1240 1241 1242

	buf->size = size;

1243 1244 1245
	if (name && parent) {
		buf->blob.data = buf->vaddr;
		buf->blob.size = size;
P
Philipp Zabel 已提交
1246 1247
		buf->dentry = debugfs_create_blob(name, 0644, parent,
						  &buf->blob);
1248 1249 1250 1251 1252
		if (!buf->dentry)
			dev_warn(&dev->plat_dev->dev,
				 "failed to create debugfs entry %s\n", name);
	}

1253 1254 1255
	return 0;
}

1256 1257
void coda_free_aux_buf(struct coda_dev *dev,
		       struct coda_aux_buf *buf)
1258 1259 1260 1261 1262 1263
{
	if (buf->vaddr) {
		dma_free_coherent(&dev->plat_dev->dev, buf->size,
				  buf->vaddr, buf->paddr);
		buf->vaddr = NULL;
		buf->size = 0;
1264 1265
		debugfs_remove(buf->dentry);
		buf->dentry = NULL;
1266 1267 1268
	}
}

1269
static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1270
{
1271 1272 1273
	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;
1274
	struct vb2_buffer *buf;
1275
	int ret = 0;
1276

1277 1278
	q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1279
		if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1280
			/* copy the buffers that were queued before streamon */
1281
			mutex_lock(&ctx->bitstream_mutex);
1282
			coda_fill_bitstream(ctx, false);
1283
			mutex_unlock(&ctx->bitstream_mutex);
1284

1285 1286 1287 1288
			if (coda_get_bitstream_payload(ctx) < 512) {
				ret = -EINVAL;
				goto err;
			}
1289
		} else {
1290 1291 1292 1293
			if (count < 1) {
				ret = -EINVAL;
				goto err;
			}
1294
		}
1295

1296
		ctx->streamon_out = 1;
1297
	} else {
1298 1299 1300 1301
		if (count < 1) {
			ret = -EINVAL;
			goto err;
		}
1302 1303

		ctx->streamon_cap = 1;
1304
	}
1305

1306 1307 1308
	/* Don't start the coda unless both queues are on */
	if (!(ctx->streamon_out & ctx->streamon_cap))
		return 0;
1309

1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
	q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
	if ((q_data_src->width != q_data_dst->width &&
	     round_up(q_data_src->width, 16) != q_data_dst->width) ||
	    (q_data_src->height != q_data_dst->height &&
	     round_up(q_data_src->height, 16) != q_data_dst->height)) {
		v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
			 q_data_src->width, q_data_src->height,
			 q_data_dst->width, q_data_dst->height);
		ret = -EINVAL;
		goto err;
	}

1322
	/* Allow BIT decoder device_run with no new buffers queued */
1323
	if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
1324
		v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
1325

1326 1327 1328 1329 1330
	ctx->gopcounter = ctx->params.gop_size - 1;

	ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
				     q_data_dst->fourcc);
	if (!ctx->codec) {
1331
		v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1332 1333
		ret = -EINVAL;
		goto err;
1334 1335
	}

1336 1337 1338 1339
	if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
		ctx->params.gop_size = 1;
	ctx->gopcounter = ctx->params.gop_size - 1;

P
Philipp Zabel 已提交
1340
	ret = ctx->ops->start_streaming(ctx);
1341
	if (ctx->inst_type == CODA_INST_DECODER) {
1342
		if (ret == -EAGAIN)
1343
			return 0;
1344
		else if (ret < 0)
1345
			goto err;
1346 1347
	}

1348
	return ret;
1349 1350 1351 1352

err:
	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
		while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1353
			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1354 1355
	} else {
		while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1356
			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1357 1358
	}
	return ret;
1359 1360
}

1361
static void coda_stop_streaming(struct vb2_queue *q)
1362 1363
{
	struct coda_ctx *ctx = vb2_get_drv_priv(q);
1364
	struct coda_dev *dev = ctx->dev;
1365
	struct vb2_buffer *buf;
1366
	unsigned long flags;
1367 1368 1369
	bool stop;

	stop = ctx->streamon_out && ctx->streamon_cap;
1370 1371

	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1372
		v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1373
			 "%s: output\n", __func__);
1374
		ctx->streamon_out = 0;
1375

1376
		coda_bit_stream_end_flag(ctx);
1377

1378
		ctx->qsequence = 0;
1379 1380 1381

		while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1382
	} else {
1383
		v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1384
			 "%s: capture\n", __func__);
1385
		ctx->streamon_cap = 0;
1386

1387
		ctx->osequence = 0;
1388
		ctx->sequence_offset = 0;
1389 1390 1391

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

1394
	if (stop) {
1395
		struct coda_buffer_meta *meta;
1396

1397 1398 1399 1400
		if (ctx->ops->seq_end_work) {
			queue_work(dev->workqueue, &ctx->seq_end_work);
			flush_work(&ctx->seq_end_work);
		}
1401
		spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
1402 1403 1404 1405 1406
		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);
1407
		}
1408 1409
		ctx->num_metas = 0;
		spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
1410 1411 1412
		kfifo_init(&ctx->bitstream_fifo,
			ctx->bitstream.vaddr, ctx->bitstream.size);
		ctx->runcounter = 0;
1413
		ctx->aborting = 0;
1414
	}
1415 1416 1417

	if (!ctx->streamon_out && !ctx->streamon_cap)
		ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
1418 1419
}

1420
static const struct vb2_ops coda_qops = {
1421 1422 1423 1424 1425
	.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,
1426 1427
	.wait_prepare		= vb2_ops_wait_prepare,
	.wait_finish		= vb2_ops_wait_finish,
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
};

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) {
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
	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;
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
	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;
1463 1464 1465 1466 1467 1468
	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;
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
	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;
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
	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;
1491 1492 1493
	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
		ctx->params.slice_max_bits = ctrl->val * 8;
		break;
1494 1495
	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
		break;
1496 1497 1498
	case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
		ctx->params.intra_refresh = ctrl->val;
		break;
1499 1500 1501 1502 1503 1504
	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;
1505 1506 1507 1508 1509 1510
	case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
		ctx->params.vbv_delay = ctrl->val;
		break;
	case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
		ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
		break;
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
	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;
}

1521
static const struct v4l2_ctrl_ops coda_ctrl_ops = {
1522 1523 1524
	.s_ctrl = coda_s_ctrl,
};

1525
static void coda_encode_ctrls(struct coda_ctx *ctx)
1526 1527
{
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1528
		V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
1529 1530 1531
	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,
1532
		V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
1533
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1534
		V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
1535 1536 1537 1538 1539 1540
	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);
1541 1542 1543 1544 1545 1546 1547 1548
	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);
1549 1550 1551 1552 1553 1554
	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,
1555 1556
		V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
		V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
1557 1558
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
1559
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
P
Philipp Zabel 已提交
1560 1561
		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
		500);
1562 1563 1564 1565 1566
	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);
1567
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
P
Philipp Zabel 已提交
1568 1569
		V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
		1920 * 1088 / 256, 1, 0);
1570 1571 1572 1573 1574 1575 1576 1577
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
	/*
	 * The maximum VBV size value is 0x7fffffff bits,
	 * one bit less than 262144 KiB
	 */
	v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
		V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
1578 1579
}

1580 1581 1582 1583 1584 1585 1586 1587
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);
}

1588 1589 1590 1591 1592 1593 1594 1595
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);
1596 1597 1598 1599 1600 1601
	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);
	}
1602 1603

	if (ctx->ctrls.error) {
P
Philipp Zabel 已提交
1604 1605
		v4l2_err(&ctx->dev->v4l2_dev,
			"control initialization error (%d)",
1606 1607 1608 1609 1610 1611 1612
			ctx->ctrls.error);
		return -EINVAL;
	}

	return v4l2_ctrl_handler_setup(&ctx->ctrls);
}

1613 1614 1615 1616 1617 1618 1619
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;
1620 1621 1622 1623 1624 1625 1626
	/* One way to indicate end-of-stream for coda is to set the
	 * bytesused == 0. However by default videobuf2 handles bytesused
	 * equal to 0 as a special case and changes its value to the size
	 * of the buffer. Set the allow_zero_bytesused flag, so
	 * that videobuf2 will keep the value of bytesused intact.
	 */
	vq->allow_zero_bytesused = 1;
1627 1628 1629 1630

	return vb2_queue_init(vq);
}

1631 1632
int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
			    struct vb2_queue *dst_vq)
1633 1634 1635 1636
{
	int ret;

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

1640
	ret = coda_queue_init(priv, src_vq);
1641 1642 1643 1644
	if (ret)
		return ret;

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

1648 1649 1650
	return coda_queue_init(priv, dst_vq);
}

1651 1652
int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
			    struct vb2_queue *dst_vq)
1653 1654 1655 1656
{
	int ret;

	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1657 1658
	src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
	src_vq->mem_ops = &vb2_vmalloc_memops;
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668

	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);
1669 1670
}

1671 1672
static int coda_next_free_instance(struct coda_dev *dev)
{
1673 1674 1675 1676 1677 1678 1679
	int idx = ffz(dev->instance_mask);

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

	return idx;
1680 1681
}

1682 1683 1684 1685 1686
/*
 * File operations
 */

static int coda_open(struct file *file)
1687
{
1688 1689
	struct video_device *vdev = video_devdata(file);
	struct coda_dev *dev = video_get_drvdata(vdev);
1690
	struct coda_ctx *ctx = NULL;
1691
	char *name;
1692
	int ret;
1693
	int idx;
1694

P
Philipp Zabel 已提交
1695
	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1696 1697 1698
	if (!ctx)
		return -ENOMEM;

F
Fabio Estevam 已提交
1699
	idx = coda_next_free_instance(dev);
1700 1701
	if (idx < 0) {
		ret = idx;
F
Fabio Estevam 已提交
1702 1703 1704 1705
		goto err_coda_max;
	}
	set_bit(idx, &dev->instance_mask);

1706
	name = kasprintf(GFP_KERNEL, "context%d", idx);
1707 1708 1709 1710 1711
	if (!name) {
		ret = -ENOMEM;
		goto err_coda_name_init;
	}

1712 1713 1714
	ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
	kfree(name);

1715 1716 1717
	ctx->cvd = to_coda_video_device(vdev);
	ctx->inst_type = ctx->cvd->type;
	ctx->ops = ctx->cvd->ops;
1718
	ctx->use_bit = !ctx->cvd->direct;
1719 1720
	init_completion(&ctx->completion);
	INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
1721 1722
	if (ctx->ops->seq_end_work)
		INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
1723 1724 1725 1726
	v4l2_fh_init(&ctx->fh, video_devdata(file));
	file->private_data = &ctx->fh;
	v4l2_fh_add(&ctx->fh);
	ctx->dev = dev;
1727
	ctx->idx = idx;
1728
	switch (dev->devtype->product) {
1729
	case CODA_960:
1730 1731 1732
		ctx->frame_mem_ctrl = 1 << 12;
		/* fallthrough */
	case CODA_7541:
1733 1734 1735 1736 1737
		ctx->reg_idx = 0;
		break;
	default:
		ctx->reg_idx = idx;
	}
F
Fabio Estevam 已提交
1738

1739 1740 1741 1742 1743 1744 1745
	/* 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;
	}

1746 1747 1748 1749 1750 1751 1752 1753
	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;

1754
	set_default_params(ctx);
P
Philipp Zabel 已提交
1755 1756
	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
					    ctx->ops->queue_init);
1757 1758
	if (IS_ERR(ctx->fh.m2m_ctx)) {
		ret = PTR_ERR(ctx->fh.m2m_ctx);
1759 1760 1761

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

1765 1766 1767
	ret = coda_ctrls_setup(ctx);
	if (ret) {
		v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
F
Fabio Estevam 已提交
1768
		goto err_ctrls_setup;
1769 1770 1771 1772
	}

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

1773
	mutex_init(&ctx->bitstream_mutex);
1774
	mutex_init(&ctx->buffer_mutex);
1775
	INIT_LIST_HEAD(&ctx->buffer_meta_list);
1776
	spin_lock_init(&ctx->buffer_meta_lock);
1777

1778
	coda_lock(ctx);
1779
	list_add(&ctx->list, &dev->instances);
1780 1781 1782 1783 1784 1785 1786
	coda_unlock(ctx);

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

	return 0;

F
Fabio Estevam 已提交
1787
err_ctrls_setup:
1788
	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
F
Fabio Estevam 已提交
1789 1790
err_ctx_init:
	clk_disable_unprepare(dev->clk_ahb);
1791
err_clk_ahb:
F
Fabio Estevam 已提交
1792
	clk_disable_unprepare(dev->clk_per);
1793
err_clk_per:
1794 1795
	pm_runtime_put_sync(&dev->plat_dev->dev);
err_pm_get:
1796 1797
	v4l2_fh_del(&ctx->fh);
	v4l2_fh_exit(&ctx->fh);
F
Fabio Estevam 已提交
1798
	clear_bit(ctx->idx, &dev->instance_mask);
1799
err_coda_name_init:
F
Fabio Estevam 已提交
1800
err_coda_max:
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
	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);

1813
	if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
1814 1815
		coda_bit_stream_end_flag(ctx);

1816
	/* If this instance is running, call .job_abort and wait for it to end */
1817
	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1818 1819

	/* In case the instance was not running, we still need to call SEQ_END */
1820
	if (ctx->ops->seq_end_work) {
1821 1822
		queue_work(dev->workqueue, &ctx->seq_end_work);
		flush_work(&ctx->seq_end_work);
1823 1824
	}

1825
	coda_lock(ctx);
1826
	list_del(&ctx->list);
1827 1828
	coda_unlock(ctx);

1829 1830 1831
	if (ctx->dev->devtype->product == CODA_DX6)
		coda_free_aux_buf(dev, &ctx->workbuf);

1832 1833
	v4l2_ctrl_handler_free(&ctx->ctrls);
	clk_disable_unprepare(dev->clk_ahb);
F
Fabio Estevam 已提交
1834
	clk_disable_unprepare(dev->clk_per);
1835
	pm_runtime_put_sync(&dev->plat_dev->dev);
1836 1837
	v4l2_fh_del(&ctx->fh);
	v4l2_fh_exit(&ctx->fh);
1838
	clear_bit(ctx->idx, &dev->instance_mask);
1839 1840
	if (ctx->ops->release)
		ctx->ops->release(ctx);
1841
	debugfs_remove_recursive(ctx->debugfs_entry);
1842 1843 1844 1845 1846
	kfree(ctx);

	return 0;
}

1847
static const struct v4l2_file_operations coda_fops = {
1848
	.owner		= THIS_MODULE,
1849
	.open		= coda_open,
1850
	.release	= coda_release,
1851
	.poll		= v4l2_m2m_fop_poll,
1852
	.unlocked_ioctl	= video_ioctl2,
1853
	.mmap		= v4l2_m2m_fop_mmap,
1854 1855
};

1856
static int coda_hw_init(struct coda_dev *dev)
1857 1858 1859
{
	u32 data;
	u16 *p;
1860 1861 1862 1863
	int i, ret;

	ret = clk_prepare_enable(dev->clk_per);
	if (ret)
1864
		goto err_clk_per;
1865

1866 1867 1868
	ret = clk_prepare_enable(dev->clk_ahb);
	if (ret)
		goto err_clk_ahb;
1869

1870 1871 1872
	if (dev->rstc)
		reset_control_reset(dev->rstc);

1873 1874
	/*
	 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
1875 1876
	 * The 16-bit chars in the code buffer are in memory access
	 * order, re-sort them to CODA order for register download.
1877 1878
	 * Data in this SRAM survives a reboot.
	 */
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
	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);
		}
1893 1894
	}

1895 1896 1897 1898
	/* Clear registers */
	for (i = 0; i < 64; i++)
		coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);

1899
	/* Tell the BIT where to find everything it needs */
1900 1901
	if (dev->devtype->product == CODA_960 ||
	    dev->devtype->product == CODA_7541) {
1902 1903
		coda_write(dev, dev->tempbuf.paddr,
				CODA_REG_BIT_TEMP_BUF_ADDR);
1904
		coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1905 1906 1907 1908
	} else {
		coda_write(dev, dev->workbuf.paddr,
			      CODA_REG_BIT_WORK_BUF_ADDR);
	}
1909 1910 1911 1912 1913 1914 1915
	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 已提交
1916 1917
		coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
			   CODA_REG_BIT_STREAM_CTRL);
1918 1919
		break;
	default:
P
Philipp Zabel 已提交
1920 1921
		coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
			   CODA_REG_BIT_STREAM_CTRL);
1922
	}
1923 1924 1925 1926
	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);
1927 1928 1929 1930

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

1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942
	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);

1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
	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;
}

1954
static int coda_register_device(struct coda_dev *dev, int i)
1955
{
1956 1957
	struct video_device *vfd = &dev->vfd[i];

1958
	if (i >= dev->devtype->num_vdevs)
1959 1960
		return -EINVAL;

1961
	strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
1962 1963
	vfd->fops	= &coda_fops;
	vfd->ioctl_ops	= &coda_ioctl_ops;
1964 1965 1966 1967 1968 1969
	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);

1970 1971 1972 1973 1974
	/* 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);

1975 1976 1977
	return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
}

1978 1979 1980 1981
static void coda_fw_callback(const struct firmware *fw, void *context)
{
	struct coda_dev *dev = context;
	struct platform_device *pdev = dev->plat_dev;
1982
	int i, ret;
1983 1984 1985

	if (!fw) {
		v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
1986
		goto put_pm;
1987 1988 1989
	}

	/* allocate auxiliary per-device code buffer for the BIT processor */
1990 1991
	ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
				 dev->debugfs_root);
1992
	if (ret < 0)
1993
		goto put_pm;
1994

1995 1996 1997 1998
	/* Copy the whole firmware image to the code buffer */
	memcpy(dev->codebuf.vaddr, fw->data, fw->size);
	release_firmware(fw);

1999 2000 2001 2002
	ret = coda_hw_init(dev);
	if (ret < 0) {
		v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
		goto put_pm;
2003 2004
	}

2005 2006 2007 2008
	ret = coda_check_firmware(dev);
	if (ret < 0)
		goto put_pm;

2009 2010 2011
	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");
2012
		goto put_pm;
2013 2014 2015 2016 2017 2018 2019 2020
	}

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

2021 2022 2023 2024 2025 2026 2027 2028
	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;
		}
2029 2030 2031
	}

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

2034
	pm_runtime_put_sync(&pdev->dev);
2035 2036
	return;

2037 2038 2039
rel_vfd:
	while (--i >= 0)
		video_unregister_device(&dev->vfd[i]);
2040 2041 2042
	v4l2_m2m_release(dev->m2m_dev);
rel_ctx:
	vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2043 2044
put_pm:
	pm_runtime_put_sync(&pdev->dev);
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
}

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,
2060
	CODA_IMX53,
2061 2062
	CODA_IMX6Q,
	CODA_IMX6DL,
2063 2064
};

2065
static const struct coda_devtype coda_devdata[] = {
2066
	[CODA_IMX27] = {
2067 2068 2069 2070
		.firmware     = "v4l-codadx6-imx27.bin",
		.product      = CODA_DX6,
		.codecs       = codadx6_codecs,
		.num_codecs   = ARRAY_SIZE(codadx6_codecs),
2071 2072
		.vdevs        = codadx6_video_devices,
		.num_vdevs    = ARRAY_SIZE(codadx6_video_devices),
2073
		.workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
2074
		.iram_size    = 0xb000,
2075
	},
2076
	[CODA_IMX53] = {
2077 2078 2079 2080
		.firmware     = "v4l-coda7541-imx53.bin",
		.product      = CODA_7541,
		.codecs       = coda7_codecs,
		.num_codecs   = ARRAY_SIZE(coda7_codecs),
2081 2082
		.vdevs        = coda7_video_devices,
		.num_vdevs    = ARRAY_SIZE(coda7_video_devices),
2083
		.workbuf_size = 128 * 1024,
2084
		.tempbuf_size = 304 * 1024,
2085
		.iram_size    = 0x14000,
2086
	},
2087
	[CODA_IMX6Q] = {
2088 2089 2090 2091
		.firmware     = "v4l-coda960-imx6q.bin",
		.product      = CODA_960,
		.codecs       = coda9_codecs,
		.num_codecs   = ARRAY_SIZE(coda9_codecs),
2092 2093
		.vdevs        = coda9_video_devices,
		.num_vdevs    = ARRAY_SIZE(coda9_video_devices),
2094
		.workbuf_size = 80 * 1024,
2095
		.tempbuf_size = 204 * 1024,
2096
		.iram_size    = 0x21000,
2097 2098
	},
	[CODA_IMX6DL] = {
2099 2100 2101 2102
		.firmware     = "v4l-coda960-imx6dl.bin",
		.product      = CODA_960,
		.codecs       = coda9_codecs,
		.num_codecs   = ARRAY_SIZE(coda9_codecs),
2103 2104
		.vdevs        = coda9_video_devices,
		.num_vdevs    = ARRAY_SIZE(coda9_video_devices),
2105
		.workbuf_size = 80 * 1024,
2106
		.tempbuf_size = 204 * 1024,
2107
		.iram_size    = 0x20000,
2108
	},
2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
};

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[] = {
2119
	{ .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
2120
	{ .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
2121 2122
	{ .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
	{ .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
2123 2124 2125 2126 2127
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, coda_dt_ids);
#endif

2128
static int coda_probe(struct platform_device *pdev)
2129 2130 2131 2132
{
	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 已提交
2133 2134 2135
	struct coda_platform_data *pdata = pdev->dev.platform_data;
	struct device_node *np = pdev->dev.of_node;
	struct gen_pool *pool;
2136 2137 2138 2139
	struct coda_dev *dev;
	struct resource *res;
	int ret, irq;

P
Philipp Zabel 已提交
2140
	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2141
	if (!dev)
2142 2143
		return -ENOMEM;

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

2146
	if (of_id) {
2147
		dev->devtype = of_id->data;
2148
	} else if (pdev_id) {
2149
		dev->devtype = &coda_devdata[pdev_id->driver_data];
2150 2151 2152
	} else {
		ret = -EINVAL;
		goto err_v4l2_register;
2153 2154 2155
	}

	spin_lock_init(&dev->irqlock);
2156
	INIT_LIST_HEAD(&dev->instances);
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172

	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);
2173 2174 2175
	dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(dev->regs_base))
		return PTR_ERR(dev->regs_base);
2176 2177

	/* IRQ */
2178 2179 2180
	irq = platform_get_irq_byname(pdev, "bit");
	if (irq < 0)
		irq = platform_get_irq(pdev, 0);
2181 2182
	if (irq < 0) {
		dev_err(&pdev->dev, "failed to get irq resource\n");
2183
		return irq;
2184 2185
	}

2186 2187 2188 2189 2190
	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;
2191 2192
	}

2193
	dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
2194 2195
	if (IS_ERR(dev->rstc)) {
		ret = PTR_ERR(dev->rstc);
2196
		if (ret == -ENOENT || ret == -ENOSYS) {
2197 2198
			dev->rstc = NULL;
		} else {
P
Philipp Zabel 已提交
2199 2200
			dev_err(&pdev->dev, "failed get reset control: %d\n",
				ret);
2201 2202 2203 2204
			return ret;
		}
	}

P
Philipp Zabel 已提交
2205
	/* Get IRAM pool from device tree or platform data */
2206
	pool = of_gen_pool_get(np, "iram", 0);
P
Philipp Zabel 已提交
2207
	if (!pool && pdata)
2208
		pool = gen_pool_get(pdata->iram_dev);
P
Philipp Zabel 已提交
2209 2210 2211 2212 2213 2214
	if (!pool) {
		dev_err(&pdev->dev, "iram pool not available\n");
		return -ENOMEM;
	}
	dev->iram_pool = pool;

2215 2216 2217 2218 2219
	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
	if (ret)
		return ret;

	mutex_init(&dev->dev_mutex);
2220
	mutex_init(&dev->coda_mutex);
2221

2222 2223 2224 2225
	dev->debugfs_root = debugfs_create_dir("coda", NULL);
	if (!dev->debugfs_root)
		dev_warn(&pdev->dev, "failed to create debugfs root\n");

2226
	/* allocate auxiliary per-device buffers for the BIT processor */
2227
	if (dev->devtype->product == CODA_DX6) {
2228
		ret = coda_alloc_aux_buf(dev, &dev->workbuf,
2229
					 dev->devtype->workbuf_size, "workbuf",
2230
					 dev->debugfs_root);
2231
		if (ret < 0)
2232
			goto err_v4l2_register;
2233
	}
2234 2235

	if (dev->devtype->tempbuf_size) {
2236
		ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
2237
					 dev->devtype->tempbuf_size, "tempbuf",
2238
					 dev->debugfs_root);
2239
		if (ret < 0)
2240
			goto err_v4l2_register;
2241 2242
	}

2243
	dev->iram.size = dev->devtype->iram_size;
2244 2245 2246
	dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
					     &dev->iram.paddr);
	if (!dev->iram.vaddr) {
2247 2248
		dev_warn(&pdev->dev, "unable to alloc iram\n");
	} else {
2249
		memset(dev->iram.vaddr, 0, dev->iram.size);
2250 2251 2252 2253 2254
		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);
2255 2256
	}

2257 2258 2259
	dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
	if (!dev->workqueue) {
		dev_err(&pdev->dev, "unable to alloc workqueue\n");
2260 2261
		ret = -ENOMEM;
		goto err_v4l2_register;
2262 2263
	}

2264 2265
	platform_set_drvdata(pdev, dev);

2266 2267
	/*
	 * Start activated so we can directly call coda_hw_init in
2268
	 * coda_fw_callback regardless of whether CONFIG_PM is
2269 2270 2271 2272
	 * enabled or whether the device is associated with a PM domain.
	 */
	pm_runtime_get_noresume(&pdev->dev);
	pm_runtime_set_active(&pdev->dev);
2273 2274
	pm_runtime_enable(&pdev->dev);

2275
	return coda_firmware_request(dev);
2276 2277 2278 2279

err_v4l2_register:
	v4l2_device_unregister(&dev->v4l2_dev);
	return ret;
2280 2281 2282 2283 2284
}

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

2287 2288 2289 2290
	for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
		if (video_get_drvdata(&dev->vfd[i]))
			video_unregister_device(&dev->vfd[i]);
	}
2291 2292
	if (dev->m2m_dev)
		v4l2_m2m_release(dev->m2m_dev);
2293
	pm_runtime_disable(&pdev->dev);
2294 2295 2296
	if (dev->alloc_ctx)
		vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
	v4l2_device_unregister(&dev->v4l2_dev);
2297
	destroy_workqueue(dev->workqueue);
2298 2299 2300
	if (dev->iram.vaddr)
		gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
			      dev->iram.size);
2301 2302 2303
	coda_free_aux_buf(dev, &dev->codebuf);
	coda_free_aux_buf(dev, &dev->tempbuf);
	coda_free_aux_buf(dev, &dev->workbuf);
2304
	debugfs_remove_recursive(dev->debugfs_root);
2305 2306 2307
	return 0;
}

2308
#ifdef CONFIG_PM
2309 2310 2311 2312 2313
static int coda_runtime_resume(struct device *dev)
{
	struct coda_dev *cdev = dev_get_drvdata(dev);
	int ret = 0;

2314
	if (dev->pm_domain && cdev->codebuf.vaddr) {
2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327
		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)
};

2328 2329
static struct platform_driver coda_driver = {
	.probe	= coda_probe,
2330
	.remove	= coda_remove,
2331 2332 2333
	.driver	= {
		.name	= CODA_NAME,
		.of_match_table = of_match_ptr(coda_dt_ids),
2334
		.pm	= &coda_pm_ops,
2335 2336 2337 2338 2339 2340 2341 2342 2343
	},
	.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");