fimc-capture.c 18.5 KB
Newer Older
1
/*
2
 * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
3
 *
4
 * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd.
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * Author: Sylwester Nawrocki, <s.nawrocki@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/bug.h>
#include <linux/interrupt.h>
#include <linux/device.h>
19
#include <linux/pm_runtime.h>
20 21 22 23 24 25 26
#include <linux/list.h>
#include <linux/slab.h>

#include <linux/videodev2.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-mem2mem.h>
27 28
#include <media/videobuf2-core.h>
#include <media/videobuf2-dma-contig.h>
29 30 31

#include "fimc-core.h"

32
static void fimc_capture_state_cleanup(struct fimc_dev *fimc)
33
{
34
	struct fimc_vid_cap *cap = &fimc->vid_cap;
35
	struct fimc_vid_buffer *buf;
36
	unsigned long flags;
37 38 39

	spin_lock_irqsave(&fimc->slock, flags);
	fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_PEND |
40
			 1 << ST_CAPT_SHUT | 1 << ST_CAPT_STREAM);
41 42

	fimc->vid_cap.active_buf_cnt = 0;
43 44 45 46 47 48 49 50 51 52 53 54

	/* Release buffers that were enqueued in the driver by videobuf2. */
	while (!list_empty(&cap->pending_buf_q)) {
		buf = pending_queue_pop(cap);
		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
	}

	while (!list_empty(&cap->active_buf_q)) {
		buf = active_queue_pop(cap);
		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
	}

55
	spin_unlock_irqrestore(&fimc->slock, flags);
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
}

static int fimc_stop_capture(struct fimc_dev *fimc)
{
	struct fimc_vid_cap *cap = &fimc->vid_cap;
	unsigned long flags;

	if (!fimc_capture_active(fimc))
		return 0;

	spin_lock_irqsave(&fimc->slock, flags);
	set_bit(ST_CAPT_SHUT, &fimc->state);
	fimc_deactivate_capture(fimc);
	spin_unlock_irqrestore(&fimc->slock, flags);

	wait_event_timeout(fimc->irq_queue,
			   !test_bit(ST_CAPT_SHUT, &fimc->state),
			   FIMC_SHUTDOWN_TIMEOUT);
74

75 76 77
	v4l2_subdev_call(cap->sd, video, s_stream, 0);

	fimc_capture_state_cleanup(fimc);
78 79 80 81
	dbg("state: 0x%lx", fimc->state);
	return 0;
}

82 83

static int start_streaming(struct vb2_queue *q, unsigned int count)
84 85 86
{
	struct fimc_ctx *ctx = q->drv_priv;
	struct fimc_dev *fimc = ctx->fimc_dev;
87
	struct s5p_fimc_isp_info *isp_info;
88
	int min_bufs;
89 90
	int ret;

91 92
	fimc_hw_reset(fimc);

93 94
	ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_stream, 1);
	if (ret && ret != -ENOIOCTLCMD)
95
		goto error;
96 97 98

	ret = fimc_prepare_config(ctx, ctx->state);
	if (ret)
99
		goto error;
100

101
	isp_info = &fimc->pdata->isp_info[fimc->vid_cap.input_index];
102 103 104 105 106 107 108 109
	fimc_hw_set_camera_type(fimc, isp_info);
	fimc_hw_set_camera_source(fimc, isp_info);
	fimc_hw_set_camera_offset(fimc, &ctx->s_frame);

	if (ctx->state & FIMC_PARAMS) {
		ret = fimc_set_scaler_info(ctx);
		if (ret) {
			err("Scaler setup error");
110
			goto error;
111 112
		}
		fimc_hw_set_input_path(ctx);
113
		fimc_hw_set_prescaler(ctx);
114
		fimc_hw_set_mainscaler(ctx);
115 116 117 118 119 120 121 122 123 124 125
		fimc_hw_set_target_format(ctx);
		fimc_hw_set_rotation(ctx);
		fimc_hw_set_effect(ctx);
	}

	fimc_hw_set_output_path(ctx);
	fimc_hw_set_out_dma(ctx);

	INIT_LIST_HEAD(&fimc->vid_cap.pending_buf_q);
	INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
	fimc->vid_cap.frame_count = 0;
126
	fimc->vid_cap.buf_index = 0;
127 128 129

	set_bit(ST_CAPT_PEND, &fimc->state);

130 131 132 133 134
	min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;

	if (fimc->vid_cap.active_buf_cnt >= min_bufs)
		fimc_activate_capture(ctx);

135
	return 0;
136 137 138
error:
	fimc_capture_state_cleanup(fimc);
	return ret;
139 140 141 142 143 144 145
}

static int stop_streaming(struct vb2_queue *q)
{
	struct fimc_ctx *ctx = q->drv_priv;
	struct fimc_dev *fimc = ctx->fimc_dev;

146
	if (!fimc_capture_active(fimc))
147 148 149 150 151
		return -EINVAL;

	return fimc_stop_capture(fimc);
}

152 153 154 155 156 157 158 159 160 161
int fimc_capture_suspend(struct fimc_dev *fimc)
{
	return -EBUSY;
}

int fimc_capture_resume(struct fimc_dev *fimc)
{
	return 0;
}

162
static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
163
{
164
	if (!fr || plane >= fr->fmt->memplanes)
165
		return 0;
166
	return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8;
167 168 169
}

static int queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
170
		       unsigned int *num_planes, unsigned int sizes[],
171 172 173
		       void *allocators[])
{
	struct fimc_ctx *ctx = vq->drv_priv;
174 175
	struct fimc_fmt *fmt = ctx->d_frame.fmt;
	int i;
176 177 178 179

	if (!fmt)
		return -EINVAL;

180
	*num_planes = fmt->memplanes;
181

182 183 184 185
	for (i = 0; i < fmt->memplanes; i++) {
		sizes[i] = get_plane_size(&ctx->d_frame, i);
		allocators[i] = ctx->fimc_dev->alloc_ctx;
	}
186

187
	return 0;
188 189 190 191 192 193 194 195
}

static int buffer_prepare(struct vb2_buffer *vb)
{
	struct vb2_queue *vq = vb->vb2_queue;
	struct fimc_ctx *ctx = vq->drv_priv;
	int i;

196 197
	if (!ctx->d_frame.fmt || vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
		return -EINVAL;
198

199 200
	for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
		unsigned long size = get_plane_size(&ctx->d_frame, i);
201 202

		if (vb2_plane_size(vb, i) < size) {
203 204
			v4l2_err(ctx->fimc_dev->vid_cap.vfd,
				 "User buffer too small (%ld < %ld)\n",
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
				 vb2_plane_size(vb, i), size);
			return -EINVAL;
		}

		vb2_set_plane_payload(vb, i, size);
	}

	return 0;
}

static void buffer_queue(struct vb2_buffer *vb)
{
	struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
	struct fimc_dev *fimc = ctx->fimc_dev;
	struct fimc_vid_buffer *buf
		= container_of(vb, struct fimc_vid_buffer, vb);
	struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
	unsigned long flags;
223
	int min_bufs;
224 225

	spin_lock_irqsave(&fimc->slock, flags);
226 227 228 229 230 231 232
	fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);

	if (!test_bit(ST_CAPT_STREAM, &fimc->state)
	     && vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
		/* Setup the buffer directly for processing. */
		int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
				vid_cap->buf_index;
233

234 235 236
		fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
		buf->index = vid_cap->buf_index;
		active_queue_add(vid_cap, buf);
237

238 239 240 241
		if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
			vid_cap->buf_index = 0;
	} else {
		fimc_pending_queue_add(vid_cap, buf);
242
	}
243 244 245

	min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;

246 247
	if (vb2_is_streaming(&vid_cap->vbq) &&
	    vid_cap->active_buf_cnt >= min_bufs &&
248 249 250
	    !test_and_set_bit(ST_CAPT_STREAM, &fimc->state))
		fimc_activate_capture(ctx);

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	spin_unlock_irqrestore(&fimc->slock, flags);
}

static void fimc_lock(struct vb2_queue *vq)
{
	struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
	mutex_lock(&ctx->fimc_dev->lock);
}

static void fimc_unlock(struct vb2_queue *vq)
{
	struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
	mutex_unlock(&ctx->fimc_dev->lock);
}

static struct vb2_ops fimc_capture_qops = {
	.queue_setup		= queue_setup,
	.buf_prepare		= buffer_prepare,
	.buf_queue		= buffer_queue,
	.wait_prepare		= fimc_unlock,
	.wait_finish		= fimc_lock,
	.start_streaming	= start_streaming,
	.stop_streaming		= stop_streaming,
};

276 277 278
static int fimc_capture_open(struct file *file)
{
	struct fimc_dev *fimc = video_drvdata(file);
279 280 281 282
	int ret = v4l2_fh_open(file);

	if (ret)
		return ret;
283 284 285 286 287 288 289

	dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);

	/* Return if the corresponding video mem2mem node is already opened. */
	if (fimc_m2m_active(fimc))
		return -EBUSY;

290
	ret = pm_runtime_get_sync(&fimc->pdev->dev);
291 292
	if (ret < 0) {
		v4l2_fh_release(file);
293
		return ret;
294
	}
295

296
	++fimc->vid_cap.refcnt;
297

298
	return 0;
299 300 301 302 303 304 305 306 307 308
}

static int fimc_capture_close(struct file *file)
{
	struct fimc_dev *fimc = video_drvdata(file);

	dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);

	if (--fimc->vid_cap.refcnt == 0) {
		fimc_stop_capture(fimc);
309
		vb2_queue_release(&fimc->vid_cap.vbq);
310 311
	}

312 313
	pm_runtime_put(&fimc->pdev->dev);

314
	return v4l2_fh_release(file);
315 316 317 318 319
}

static unsigned int fimc_capture_poll(struct file *file,
				      struct poll_table_struct *wait)
{
320
	struct fimc_dev *fimc = video_drvdata(file);
321

322
	return vb2_poll(&fimc->vid_cap.vbq, file, wait);
323 324 325 326
}

static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
{
327
	struct fimc_dev *fimc = video_drvdata(file);
328

329
	return vb2_mmap(&fimc->vid_cap.vbq, vma);
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
}

/* video device file operations */
static const struct v4l2_file_operations fimc_capture_fops = {
	.owner		= THIS_MODULE,
	.open		= fimc_capture_open,
	.release	= fimc_capture_close,
	.poll		= fimc_capture_poll,
	.unlocked_ioctl	= video_ioctl2,
	.mmap		= fimc_capture_mmap,
};

static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
					struct v4l2_capability *cap)
{
345
	struct fimc_dev *fimc = video_drvdata(file);
346 347 348 349

	strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
	strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
	cap->bus_info[0] = 0;
350 351
	cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
			    V4L2_CAP_VIDEO_CAPTURE_MPLANE;
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

	return 0;
}

/* Synchronize formats of the camera interface input and attached  sensor. */
static int sync_capture_fmt(struct fimc_ctx *ctx)
{
	struct fimc_frame *frame = &ctx->s_frame;
	struct fimc_dev *fimc = ctx->fimc_dev;
	struct v4l2_mbus_framefmt *fmt = &fimc->vid_cap.fmt;
	int ret;

	fmt->width  = ctx->d_frame.o_width;
	fmt->height = ctx->d_frame.o_height;

	ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_mbus_fmt, fmt);
	if (ret == -ENOIOCTLCMD) {
		err("s_mbus_fmt failed");
		return ret;
	}
	dbg("w: %d, h: %d, code= %d", fmt->width, fmt->height, fmt->code);

	frame->fmt = find_mbus_format(fmt, FMT_FLAGS_CAM);
	if (!frame->fmt) {
		err("fimc source format not found\n");
		return -EINVAL;
	}

	frame->f_width	= fmt->width;
	frame->f_height = fmt->height;
	frame->width	= fmt->width;
	frame->height	= fmt->height;
	frame->o_width	= fmt->width;
	frame->o_height = fmt->height;
	frame->offs_h	= 0;
	frame->offs_v	= 0;

	return 0;
}

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
				 struct v4l2_format *f)
{
	struct fimc_dev *fimc = video_drvdata(file);
	struct fimc_ctx *ctx = fimc->vid_cap.ctx;

	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
		return -EINVAL;

	return fimc_fill_format(&ctx->d_frame, f);
}

static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
				   struct v4l2_format *f)
{
	struct fimc_dev *fimc = video_drvdata(file);
	struct fimc_ctx *ctx = fimc->vid_cap.ctx;

	return fimc_try_fmt_mplane(ctx, f);
}

413 414
static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
				 struct v4l2_format *f)
415
{
416 417
	struct fimc_dev *fimc = video_drvdata(file);
	struct fimc_ctx *ctx = fimc->vid_cap.ctx;
418
	struct v4l2_pix_format_mplane *pix;
419
	struct fimc_frame *frame;
420
	int ret;
421
	int i;
422

423
	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
424 425
		return -EINVAL;

426
	ret = fimc_try_fmt_mplane(ctx, f);
427 428 429
	if (ret)
		return ret;

430
	if (vb2_is_busy(&fimc->vid_cap.vbq) || fimc_capture_active(fimc))
431
		return -EBUSY;
432 433 434

	frame = &ctx->d_frame;

435
	pix = &f->fmt.pix_mp;
436 437
	frame->fmt = find_format(f, FMT_FLAGS_M2M | FMT_FLAGS_CAM);
	if (!frame->fmt) {
438 439
		v4l2_err(fimc->vid_cap.vfd,
			 "Not supported capture (FIMC target) color format\n");
440
		return -EINVAL;
441 442
	}

443 444 445 446
	for (i = 0; i < frame->fmt->colplanes; i++) {
		frame->payload[i] =
			(pix->width * pix->height * frame->fmt->depth[i]) >> 3;
	}
447

448
	/* Output DMA frame pixel size and offsets. */
449 450
	frame->f_width = pix->plane_fmt[0].bytesperline * 8
			/ frame->fmt->depth[0];
451 452 453 454 455 456 457 458 459 460
	frame->f_height = pix->height;
	frame->width	= pix->width;
	frame->height	= pix->height;
	frame->o_width	= pix->width;
	frame->o_height = pix->height;
	frame->offs_h	= 0;
	frame->offs_v	= 0;

	ctx->state |= (FIMC_PARAMS | FIMC_DST_FMT);

461
	ret = sync_capture_fmt(ctx);
462 463 464 465
	return ret;
}

static int fimc_cap_enum_input(struct file *file, void *priv,
466
			       struct v4l2_input *i)
467
{
468
	struct fimc_dev *fimc = video_drvdata(file);
469

470
	if (i->index != 0)
471 472 473 474 475 476 477
		return -EINVAL;


	i->type = V4L2_INPUT_TYPE_CAMERA;
	return 0;
}

478
static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
479
{
480
	return i == 0 ? i : -EINVAL;
481 482
}

483
static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
484
{
485
	*i = 0;
486 487 488 489
	return 0;
}

static int fimc_cap_streamon(struct file *file, void *priv,
490
			     enum v4l2_buf_type type)
491
{
492 493
	struct fimc_dev *fimc = video_drvdata(file);
	struct fimc_ctx *ctx = fimc->vid_cap.ctx;
494 495

	if (fimc_capture_active(fimc) || !fimc->vid_cap.sd)
496
		return -EBUSY;
497 498

	if (!(ctx->state & FIMC_DST_FMT)) {
499
		v4l2_err(fimc->vid_cap.vfd, "Format is not set\n");
500
		return -EINVAL;
501 502
	}

503
	return vb2_streamon(&fimc->vid_cap.vbq, type);
504 505 506
}

static int fimc_cap_streamoff(struct file *file, void *priv,
507
			    enum v4l2_buf_type type)
508
{
509
	struct fimc_dev *fimc = video_drvdata(file);
510

511
	return vb2_streamoff(&fimc->vid_cap.vbq, type);
512 513 514
}

static int fimc_cap_reqbufs(struct file *file, void *priv,
515
			    struct v4l2_requestbuffers *reqbufs)
516
{
517 518
	struct fimc_dev *fimc = video_drvdata(file);
	int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
519 520

	if (!ret)
521
		fimc->vid_cap.reqbufs_count = reqbufs->count;
522 523 524 525 526 527
	return ret;
}

static int fimc_cap_querybuf(struct file *file, void *priv,
			   struct v4l2_buffer *buf)
{
528
	struct fimc_dev *fimc = video_drvdata(file);
529

530
	return vb2_querybuf(&fimc->vid_cap.vbq, buf);
531 532 533 534 535
}

static int fimc_cap_qbuf(struct file *file, void *priv,
			  struct v4l2_buffer *buf)
{
536 537 538
	struct fimc_dev *fimc = video_drvdata(file);

	return vb2_qbuf(&fimc->vid_cap.vbq, buf);
539 540 541 542 543
}

static int fimc_cap_dqbuf(struct file *file, void *priv,
			   struct v4l2_buffer *buf)
{
544 545 546
	struct fimc_dev *fimc = video_drvdata(file);

	return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
547 548 549
}

static int fimc_cap_s_ctrl(struct file *file, void *priv,
550
			   struct v4l2_control *ctrl)
551
{
552 553
	struct fimc_dev *fimc = video_drvdata(file);
	struct fimc_ctx *ctx = fimc->vid_cap.ctx;
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
	int ret = -EINVAL;

	/* Allow any controls but 90/270 rotation while streaming */
	if (!fimc_capture_active(ctx->fimc_dev) ||
	    ctrl->id != V4L2_CID_ROTATE ||
	    (ctrl->value != 90 && ctrl->value != 270)) {
		ret = check_ctrl_val(ctx, ctrl);
		if (!ret) {
			ret = fimc_s_ctrl(ctx, ctrl);
			if (!ret)
				ctx->state |= FIMC_PARAMS;
		}
	}
	if (ret == -EINVAL)
		ret = v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd,
				       core, s_ctrl, ctrl);
	return ret;
}

573 574 575
static int fimc_cap_cropcap(struct file *file, void *fh,
			    struct v4l2_cropcap *cr)
{
576 577
	struct fimc_dev *fimc = video_drvdata(file);
	struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame;
578

579
	if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
580 581 582 583 584 585 586 587 588 589 590 591 592
		return -EINVAL;

	cr->bounds.left		= 0;
	cr->bounds.top		= 0;
	cr->bounds.width	= f->o_width;
	cr->bounds.height	= f->o_height;
	cr->defrect		= cr->bounds;

	return 0;
}

static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
{
593 594
	struct fimc_dev *fimc = video_drvdata(file);
	struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame;
595

596 597 598 599 600 601 602 603
	cr->c.left	= f->offs_h;
	cr->c.top	= f->offs_v;
	cr->c.width	= f->width;
	cr->c.height	= f->height;

	return 0;
}

604
static int fimc_cap_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
605
{
606 607
	struct fimc_dev *fimc = video_drvdata(file);
	struct fimc_ctx *ctx = fimc->vid_cap.ctx;
608 609 610 611 612 613 614 615 616 617 618
	struct fimc_frame *f;
	int ret = -EINVAL;

	if (fimc_capture_active(fimc))
		return -EBUSY;

	ret = fimc_try_crop(ctx, cr);
	if (ret)
		return ret;

	if (!(ctx->state & FIMC_DST_FMT)) {
619 620
		v4l2_err(fimc->vid_cap.vfd, "Capture format is not set\n");
		return -EINVAL;
621 622 623 624
	}

	f = &ctx->s_frame;
	/* Check for the pixel scaling ratio when cropping input image. */
625 626 627
	ret = fimc_check_scaler_ratio(cr->c.width, cr->c.height,
				      ctx->d_frame.width, ctx->d_frame.height,
				      ctx->rotation);
628
	if (ret) {
629
		v4l2_err(fimc->vid_cap.vfd, "Out of the scaler range\n");
630
		return ret;
631 632
	}

633 634 635 636 637 638
	f->offs_h = cr->c.left;
	f->offs_v = cr->c.top;
	f->width  = cr->c.width;
	f->height = cr->c.height;

	return 0;
639 640 641 642 643 644
}


static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
	.vidioc_querycap		= fimc_vidioc_querycap_capture,

645
	.vidioc_enum_fmt_vid_cap_mplane	= fimc_vidioc_enum_fmt_mplane,
646
	.vidioc_try_fmt_vid_cap_mplane	= fimc_cap_try_fmt_mplane,
647
	.vidioc_s_fmt_vid_cap_mplane	= fimc_cap_s_fmt_mplane,
648
	.vidioc_g_fmt_vid_cap_mplane	= fimc_cap_g_fmt_mplane,
649 650 651 652 653 654 655 656 657 658 659 660 661 662

	.vidioc_reqbufs			= fimc_cap_reqbufs,
	.vidioc_querybuf		= fimc_cap_querybuf,

	.vidioc_qbuf			= fimc_cap_qbuf,
	.vidioc_dqbuf			= fimc_cap_dqbuf,

	.vidioc_streamon		= fimc_cap_streamon,
	.vidioc_streamoff		= fimc_cap_streamoff,

	.vidioc_queryctrl		= fimc_vidioc_queryctrl,
	.vidioc_g_ctrl			= fimc_vidioc_g_ctrl,
	.vidioc_s_ctrl			= fimc_cap_s_ctrl,

663
	.vidioc_g_crop			= fimc_cap_g_crop,
664
	.vidioc_s_crop			= fimc_cap_s_crop,
665
	.vidioc_cropcap			= fimc_cap_cropcap,
666 667 668 669 670 671

	.vidioc_enum_input		= fimc_cap_enum_input,
	.vidioc_s_input			= fimc_cap_s_input,
	.vidioc_g_input			= fimc_cap_g_input,
};

672
/* fimc->lock must be already initialized */
673 674
int fimc_register_capture_device(struct fimc_dev *fimc,
				 struct v4l2_device *v4l2_dev)
675 676 677 678 679
{
	struct video_device *vfd;
	struct fimc_vid_cap *vid_cap;
	struct fimc_ctx *ctx;
	struct v4l2_format f;
680
	struct fimc_frame *fr;
681
	struct vb2_queue *q;
682
	int ret = -ENOMEM;
683 684 685 686 687 688 689 690 691 692

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

	ctx->fimc_dev	 = fimc;
	ctx->in_path	 = FIMC_CAMERA;
	ctx->out_path	 = FIMC_DMA;
	ctx->state	 = FIMC_CTX_CAP;

693 694 695 696 697 698
	/* Default format of the output frames */
	f.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32;
	fr = &ctx->d_frame;
	fr->fmt = find_format(&f, FMT_FLAGS_M2M);
	fr->width = fr->f_width = fr->o_width = 640;
	fr->height = fr->f_height = fr->o_height = 480;
699 700 701 702

	vfd = video_device_alloc();
	if (!vfd) {
		v4l2_err(v4l2_dev, "Failed to allocate video device\n");
703
		goto err_vd_alloc;
704 705
	}

706 707
	snprintf(vfd->name, sizeof(vfd->name), "%s.capture",
		 dev_name(&fimc->pdev->dev));
708 709 710

	vfd->fops	= &fimc_capture_fops;
	vfd->ioctl_ops	= &fimc_capture_ioctl_ops;
711
	vfd->v4l2_dev	= v4l2_dev;
712 713
	vfd->minor	= -1;
	vfd->release	= video_device_release;
714
	vfd->lock	= &fimc->lock;
715 716 717 718 719 720 721
	video_set_drvdata(vfd, fimc);

	vid_cap = &fimc->vid_cap;
	vid_cap->vfd = vfd;
	vid_cap->active_buf_cnt = 0;
	vid_cap->reqbufs_count  = 0;
	vid_cap->refcnt = 0;
722
	/* Default color format for image sensor */
723 724 725 726 727 728 729
	vid_cap->fmt.code = V4L2_MBUS_FMT_YUYV8_2X8;

	INIT_LIST_HEAD(&vid_cap->pending_buf_q);
	INIT_LIST_HEAD(&vid_cap->active_buf_q);
	spin_lock_init(&ctx->slock);
	vid_cap->ctx = ctx;

730 731
	q = &fimc->vid_cap.vbq;
	memset(q, 0, sizeof(*q));
732
	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
733 734 735 736 737 738 739
	q->io_modes = VB2_MMAP | VB2_USERPTR;
	q->drv_priv = fimc->vid_cap.ctx;
	q->ops = &fimc_capture_qops;
	q->mem_ops = &vb2_dma_contig_memops;
	q->buf_struct_size = sizeof(struct fimc_vid_buffer);

	vb2_queue_init(q);
740

741 742 743 744 745
	fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK;
	ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0);
	if (ret)
		goto err_ent;

746 747
	return 0;

748
err_ent:
749
	video_device_release(vfd);
750
err_vd_alloc:
751
	kfree(ctx);
752 753 754 755 756
	return ret;
}

void fimc_unregister_capture_device(struct fimc_dev *fimc)
{
757
	struct video_device *vfd = fimc->vid_cap.vfd;
758

759 760
	if (vfd) {
		media_entity_cleanup(&vfd->entity);
761 762
		/* Can also be called if video device was
		   not registered */
763 764 765
		video_unregister_device(vfd);
	}
	kfree(fimc->vid_cap.ctx);
766
	fimc->vid_cap.ctx = NULL;
767
}