mcam-core.c 49.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * The Marvell camera core.  This device appears in a number of settings,
 * so it needs platform-specific support outside of the core.
 *
 * Copyright 2011 Jonathan Corbet corbet@lwn.net
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/wait.h>
#include <linux/list.h>
#include <linux/dma-mapping.h>
#include <linux/delay.h>
#include <linux/vmalloc.h>
#include <linux/io.h>
22
#include <linux/clk.h>
23 24 25
#include <linux/videodev2.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
26
#include <media/v4l2-ctrls.h>
27
#include <media/v4l2-event.h>
28 29
#include <media/ov7670.h>
#include <media/videobuf2-vmalloc.h>
30
#include <media/videobuf2-dma-contig.h>
31
#include <media/videobuf2-dma-sg.h>
32 33 34

#include "mcam-core.h"

35
#ifdef MCAM_MODE_VMALLOC
36 37 38 39 40 41 42 43 44 45 46 47 48
/*
 * Internal DMA buffer management.  Since the controller cannot do S/G I/O,
 * we must have physically contiguous buffers to bring frames into.
 * These parameters control how many buffers we use, whether we
 * allocate them at load time (better chance of success, but nails down
 * memory) or when somebody tries to use the camera (riskier), and,
 * for load-time allocation, how big they should be.
 *
 * The controller can cycle through three buffers.  We could use
 * more by flipping pointers around, but it probably makes little
 * sense.
 */

49
static bool alloc_bufs_at_read;
50 51 52 53 54
module_param(alloc_bufs_at_read, bool, 0444);
MODULE_PARM_DESC(alloc_bufs_at_read,
		"Non-zero value causes DMA buffers to be allocated when the "
		"video capture device is read, rather than at module load "
		"time.  This saves memory, but decreases the chances of "
55 56
		"successfully getting those buffers.  This parameter is "
		"only used in the vmalloc buffer mode");
57 58 59 60 61 62 63 64 65 66 67 68 69

static int n_dma_bufs = 3;
module_param(n_dma_bufs, uint, 0644);
MODULE_PARM_DESC(n_dma_bufs,
		"The number of DMA buffers to allocate.  Can be either two "
		"(saves memory, makes timing tighter) or three.");

static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2;  /* Worst case */
module_param(dma_buf_size, uint, 0444);
MODULE_PARM_DESC(dma_buf_size,
		"The size of the allocated DMA buffers.  If actual operating "
		"parameters require larger buffers, an attempt to reallocate "
		"will be made.");
70
#else /* MCAM_MODE_VMALLOC */
71
static const bool alloc_bufs_at_read;
72 73
static const int n_dma_bufs = 3;  /* Used by S/G_PARM */
#endif /* MCAM_MODE_VMALLOC */
74

75
static bool flip;
76 77 78 79 80
module_param(flip, bool, 0444);
MODULE_PARM_DESC(flip,
		"If set, the sensor will be instructed to flip the image "
		"vertically.");

81 82 83 84 85 86 87
static int buffer_mode = -1;
module_param(buffer_mode, int, 0444);
MODULE_PARM_DESC(buffer_mode,
		"Set the buffer mode to be used; default is to go with what "
		"the platform driver asks for.  Set to 0 for vmalloc, 1 for "
		"DMA contiguous.");

88 89 90 91 92 93 94 95
/*
 * Status flags.  Always manipulated with bit operations.
 */
#define CF_BUF0_VALID	 0	/* Buffers valid - first three */
#define CF_BUF1_VALID	 1
#define CF_BUF2_VALID	 2
#define CF_DMA_ACTIVE	 3	/* A frame is incoming */
#define CF_CONFIG_NEEDED 4	/* Must configure hardware */
96
#define CF_SINGLE_BUFFER 5	/* Running with a single buffer */
97
#define CF_SG_RESTART	 6	/* SG restart needed */
98 99 100
#define CF_FRAME_SOF0	 7	/* Frame 0 started */
#define CF_FRAME_SOF1	 8
#define CF_FRAME_SOF2	 9
101 102 103 104 105 106 107 108

#define sensor_call(cam, o, f, args...) \
	v4l2_subdev_call(cam->sensor, o, f, ##args)

static struct mcam_format_struct {
	__u8 *desc;
	__u32 pixelformat;
	int bpp;   /* Bytes per pixel */
109
	bool planar;
110
	u32 mbus_code;
111 112 113 114
} mcam_formats[] = {
	{
		.desc		= "YUYV 4:2:2",
		.pixelformat	= V4L2_PIX_FMT_YUYV,
115
		.mbus_code	= MEDIA_BUS_FMT_YUYV8_2X8,
116
		.bpp		= 2,
117 118 119 120 121
		.planar		= false,
	},
	{
		.desc		= "UYVY 4:2:2",
		.pixelformat	= V4L2_PIX_FMT_UYVY,
122
		.mbus_code	= MEDIA_BUS_FMT_YUYV8_2X8,
123 124 125 126 127 128
		.bpp		= 2,
		.planar		= false,
	},
	{
		.desc		= "YUV 4:2:2 PLANAR",
		.pixelformat	= V4L2_PIX_FMT_YUV422P,
129
		.mbus_code	= MEDIA_BUS_FMT_YUYV8_2X8,
130 131 132 133 134 135
		.bpp		= 2,
		.planar		= true,
	},
	{
		.desc		= "YUV 4:2:0 PLANAR",
		.pixelformat	= V4L2_PIX_FMT_YUV420,
136
		.mbus_code	= MEDIA_BUS_FMT_YUYV8_2X8,
137 138 139 140 141 142
		.bpp		= 2,
		.planar		= true,
	},
	{
		.desc		= "YVU 4:2:0 PLANAR",
		.pixelformat	= V4L2_PIX_FMT_YVU420,
143
		.mbus_code	= MEDIA_BUS_FMT_YUYV8_2X8,
144 145
		.bpp		= 2,
		.planar		= true,
146 147 148 149
	},
	{
		.desc		= "RGB 444",
		.pixelformat	= V4L2_PIX_FMT_RGB444,
150
		.mbus_code	= MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE,
151
		.bpp		= 2,
152
		.planar		= false,
153 154 155 156
	},
	{
		.desc		= "RGB 565",
		.pixelformat	= V4L2_PIX_FMT_RGB565,
157
		.mbus_code	= MEDIA_BUS_FMT_RGB565_2X8_LE,
158
		.bpp		= 2,
159
		.planar		= false,
160 161 162 163
	},
	{
		.desc		= "Raw RGB Bayer",
		.pixelformat	= V4L2_PIX_FMT_SBGGR8,
164
		.mbus_code	= MEDIA_BUS_FMT_SBGGR8_1X8,
165 166
		.bpp		= 1,
		.planar		= false,
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	},
};
#define N_MCAM_FMTS ARRAY_SIZE(mcam_formats)

static struct mcam_format_struct *mcam_find_format(u32 pixelformat)
{
	unsigned i;

	for (i = 0; i < N_MCAM_FMTS; i++)
		if (mcam_formats[i].pixelformat == pixelformat)
			return mcam_formats + i;
	/* Not found? Then return the first format. */
	return mcam_formats;
}

/*
183
 * The default format we use until somebody says otherwise.
184
 */
185 186 187 188 189 190 191
static const struct v4l2_pix_format mcam_def_pix_format = {
	.width		= VGA_WIDTH,
	.height		= VGA_HEIGHT,
	.pixelformat	= V4L2_PIX_FMT_YUYV,
	.field		= V4L2_FIELD_NONE,
	.bytesperline	= VGA_WIDTH*2,
	.sizeimage	= VGA_WIDTH*VGA_HEIGHT*2,
192
	.colorspace	= V4L2_COLORSPACE_SRGB,
193
};
194

195
static const u32 mcam_def_mbus_code = MEDIA_BUS_FMT_YUYV8_2X8;
196 197


198 199 200 201 202 203 204 205 206 207 208
/*
 * The two-word DMA descriptor format used by the Armada 610 and like.  There
 * Is a three-word format as well (set C1_DESC_3WORD) where the third
 * word is a pointer to the next descriptor, but we don't use it.  Two-word
 * descriptors have to be contiguous in memory.
 */
struct mcam_dma_desc {
	u32 dma_addr;
	u32 segment_len;
};

209 210 211 212 213 214 215 216
/*
 * Our buffer type for working with videobuf2.  Note that the vb2
 * developers have decreed that struct vb2_buffer must be at the
 * beginning of this structure.
 */
struct mcam_vb_buffer {
	struct vb2_buffer vb_buf;
	struct list_head queue;
217 218 219
	struct mcam_dma_desc *dma_desc;	/* Descriptor virtual address */
	dma_addr_t dma_desc_pa;		/* Descriptor physical address */
	int dma_desc_nent;		/* Number of mapped descriptors */
220 221 222 223 224 225 226
};

static inline struct mcam_vb_buffer *vb_to_mvb(struct vb2_buffer *vb)
{
	return container_of(vb, struct mcam_vb_buffer, vb_buf);
}

227 228 229 230 231 232 233 234
/*
 * Hand a completed buffer back to user space.
 */
static void mcam_buffer_done(struct mcam_camera *cam, int frame,
		struct vb2_buffer *vbuf)
{
	vbuf->v4l2_buf.bytesused = cam->pix_format.sizeimage;
	vbuf->v4l2_buf.sequence = cam->buf_seq[frame];
235 236
	vbuf->v4l2_buf.field = V4L2_FIELD_NONE;
	v4l2_get_timestamp(&vbuf->v4l2_buf.timestamp);
237 238 239 240 241
	vb2_set_plane_payload(vbuf, 0, cam->pix_format.sizeimage);
	vb2_buffer_done(vbuf, VB2_BUF_STATE_DONE);
}


242 243

/*
244
 * Debugging and related.
245 246 247 248 249 250 251 252 253
 */
#define cam_err(cam, fmt, arg...) \
	dev_err((cam)->dev, fmt, ##arg);
#define cam_warn(cam, fmt, arg...) \
	dev_warn((cam)->dev, fmt, ##arg);
#define cam_dbg(cam, fmt, arg...) \
	dev_dbg((cam)->dev, fmt, ##arg);


254 255 256 257 258 259 260 261
/*
 * Flag manipulation helpers
 */
static void mcam_reset_buffers(struct mcam_camera *cam)
{
	int i;

	cam->next_buf = -1;
262
	for (i = 0; i < cam->nbufs; i++) {
263
		clear_bit(i, &cam->flags);
264 265
		clear_bit(CF_FRAME_SOF0 + i, &cam->flags);
	}
266 267 268 269 270 271 272 273 274 275 276 277 278 279
}

static inline int mcam_needs_config(struct mcam_camera *cam)
{
	return test_bit(CF_CONFIG_NEEDED, &cam->flags);
}

static void mcam_set_config_needed(struct mcam_camera *cam, int needed)
{
	if (needed)
		set_bit(CF_CONFIG_NEEDED, &cam->flags);
	else
		clear_bit(CF_CONFIG_NEEDED, &cam->flags);
}
280 281 282

/* ------------------------------------------------------------------- */
/*
283 284
 * Make the controller start grabbing images.  Everything must
 * be set up before doing this.
285
 */
286 287 288 289 290 291 292 293 294 295 296 297
static void mcam_ctlr_start(struct mcam_camera *cam)
{
	/* set_bit performs a read, so no other barrier should be
	   needed here */
	mcam_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
}

static void mcam_ctlr_stop(struct mcam_camera *cam)
{
	mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
static void mcam_enable_mipi(struct mcam_camera *mcam)
{
	/* Using MIPI mode and enable MIPI */
	cam_dbg(mcam, "camera: DPHY3=0x%x, DPHY5=0x%x, DPHY6=0x%x\n",
			mcam->dphy[0], mcam->dphy[1], mcam->dphy[2]);
	mcam_reg_write(mcam, REG_CSI2_DPHY3, mcam->dphy[0]);
	mcam_reg_write(mcam, REG_CSI2_DPHY5, mcam->dphy[1]);
	mcam_reg_write(mcam, REG_CSI2_DPHY6, mcam->dphy[2]);

	if (!mcam->mipi_enabled) {
		if (mcam->lane > 4 || mcam->lane <= 0) {
			cam_warn(mcam, "lane number error\n");
			mcam->lane = 1;	/* set the default value */
		}
		/*
		 * 0x41 actives 1 lane
		 * 0x43 actives 2 lanes
		 * 0x45 actives 3 lanes (never happen)
		 * 0x47 actives 4 lanes
		 */
		mcam_reg_write(mcam, REG_CSI2_CTRL0,
			CSI2_C0_MIPI_EN | CSI2_C0_ACT_LANE(mcam->lane));
		mcam_reg_write(mcam, REG_CLKCTRL,
			(mcam->mclk_src << 29) | mcam->mclk_div);

		mcam->mipi_enabled = true;
	}
}

static void mcam_disable_mipi(struct mcam_camera *mcam)
{
	/* Using Parallel mode or disable MIPI */
	mcam_reg_write(mcam, REG_CSI2_CTRL0, 0x0);
	mcam_reg_write(mcam, REG_CSI2_DPHY3, 0x0);
	mcam_reg_write(mcam, REG_CSI2_DPHY5, 0x0);
	mcam_reg_write(mcam, REG_CSI2_DPHY6, 0x0);
	mcam->mipi_enabled = false;
}

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 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
static bool mcam_fmt_is_planar(__u32 pfmt)
{
	struct mcam_format_struct *f;

	f = mcam_find_format(pfmt);
	return f->planar;
}

static void mcam_write_yuv_bases(struct mcam_camera *cam,
				 unsigned frame, dma_addr_t base)
{
	struct v4l2_pix_format *fmt = &cam->pix_format;
	u32 pixel_count = fmt->width * fmt->height;
	dma_addr_t y, u = 0, v = 0;

	y = base;

	switch (fmt->pixelformat) {
	case V4L2_PIX_FMT_YUV422P:
		u = y + pixel_count;
		v = u + pixel_count / 2;
		break;
	case V4L2_PIX_FMT_YUV420:
		u = y + pixel_count;
		v = u + pixel_count / 4;
		break;
	case V4L2_PIX_FMT_YVU420:
		v = y + pixel_count;
		u = v + pixel_count / 4;
		break;
	default:
		break;
	}

	mcam_reg_write(cam, REG_Y0BAR + frame * 4, y);
	if (mcam_fmt_is_planar(fmt->pixelformat)) {
		mcam_reg_write(cam, REG_U0BAR + frame * 4, u);
		mcam_reg_write(cam, REG_V0BAR + frame * 4, v);
	}
}

378
/* ------------------------------------------------------------------- */
379 380

#ifdef MCAM_MODE_VMALLOC
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
/*
 * Code specific to the vmalloc buffer mode.
 */

/*
 * Allocate in-kernel DMA buffers for vmalloc mode.
 */
static int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
{
	int i;

	mcam_set_config_needed(cam, 1);
	if (loadtime)
		cam->dma_buf_size = dma_buf_size;
	else
		cam->dma_buf_size = cam->pix_format.sizeimage;
	if (n_dma_bufs > 3)
		n_dma_bufs = 3;

	cam->nbufs = 0;
	for (i = 0; i < n_dma_bufs; i++) {
		cam->dma_bufs[i] = dma_alloc_coherent(cam->dev,
				cam->dma_buf_size, cam->dma_handles + i,
				GFP_KERNEL);
		if (cam->dma_bufs[i] == NULL) {
			cam_warn(cam, "Failed to allocate DMA buffer\n");
			break;
		}
		(cam->nbufs)++;
	}

	switch (cam->nbufs) {
	case 1:
		dma_free_coherent(cam->dev, cam->dma_buf_size,
				cam->dma_bufs[0], cam->dma_handles[0]);
		cam->nbufs = 0;
	case 0:
		cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
		return -ENOMEM;

	case 2:
		if (n_dma_bufs > 2)
			cam_warn(cam, "Will limp along with only 2 buffers\n");
		break;
	}
	return 0;
}

static void mcam_free_dma_bufs(struct mcam_camera *cam)
{
	int i;

	for (i = 0; i < cam->nbufs; i++) {
		dma_free_coherent(cam->dev, cam->dma_buf_size,
				cam->dma_bufs[i], cam->dma_handles[i]);
		cam->dma_bufs[i] = NULL;
	}
	cam->nbufs = 0;
}

441 442

/*
443
 * Set up DMA buffers when operating in vmalloc mode
444
 */
445
static void mcam_ctlr_dma_vmalloc(struct mcam_camera *cam)
446 447
{
	/*
448
	 * Store the first two YUV buffers. Then either
449 450 451
	 * set the third if it exists, or tell the controller
	 * to just use two.
	 */
452 453
	mcam_write_yuv_bases(cam, 0, cam->dma_handles[0]);
	mcam_write_yuv_bases(cam, 1, cam->dma_handles[1]);
454
	if (cam->nbufs > 2) {
455
		mcam_write_yuv_bases(cam, 2, cam->dma_handles[2]);
456 457 458
		mcam_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
	} else
		mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
459
	if (cam->chip_id == MCAM_CAFE)
460
		mcam_reg_write(cam, REG_UBAR, 0); /* 32 bits only */
461 462
}

463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
/*
 * Copy data out to user space in the vmalloc case
 */
static void mcam_frame_tasklet(unsigned long data)
{
	struct mcam_camera *cam = (struct mcam_camera *) data;
	int i;
	unsigned long flags;
	struct mcam_vb_buffer *buf;

	spin_lock_irqsave(&cam->dev_lock, flags);
	for (i = 0; i < cam->nbufs; i++) {
		int bufno = cam->next_buf;

		if (cam->state != S_STREAMING || bufno < 0)
			break;  /* I/O got stopped */
		if (++(cam->next_buf) >= cam->nbufs)
			cam->next_buf = 0;
		if (!test_bit(bufno, &cam->flags))
			continue;
		if (list_empty(&cam->buffers)) {
484
			cam->frame_state.singles++;
485 486
			break;  /* Leave it valid, hope for better later */
		}
487
		cam->frame_state.delivered++;
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
		clear_bit(bufno, &cam->flags);
		buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer,
				queue);
		list_del_init(&buf->queue);
		/*
		 * Drop the lock during the big copy.  This *should* be safe...
		 */
		spin_unlock_irqrestore(&cam->dev_lock, flags);
		memcpy(vb2_plane_vaddr(&buf->vb_buf, 0), cam->dma_bufs[bufno],
				cam->pix_format.sizeimage);
		mcam_buffer_done(cam, bufno, &buf->vb_buf);
		spin_lock_irqsave(&cam->dev_lock, flags);
	}
	spin_unlock_irqrestore(&cam->dev_lock, flags);
}


505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
/*
 * Make sure our allocated buffers are up to the task.
 */
static int mcam_check_dma_buffers(struct mcam_camera *cam)
{
	if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
			mcam_free_dma_bufs(cam);
	if (cam->nbufs == 0)
		return mcam_alloc_dma_bufs(cam, 0);
	return 0;
}

static void mcam_vmalloc_done(struct mcam_camera *cam, int frame)
{
	tasklet_schedule(&cam->s_tasklet);
}

#else /* MCAM_MODE_VMALLOC */

static inline int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
{
	return 0;
}

static inline void mcam_free_dma_bufs(struct mcam_camera *cam)
{
	return;
}

static inline int mcam_check_dma_buffers(struct mcam_camera *cam)
{
	return 0;
}



#endif /* MCAM_MODE_VMALLOC */


#ifdef MCAM_MODE_DMA_CONTIG
545 546 547 548
/* ---------------------------------------------------------------------- */
/*
 * DMA-contiguous code.
 */
549

550 551 552 553 554 555 556 557 558 559 560
/*
 * Set up a contiguous buffer for the given frame.  Here also is where
 * the underrun strategy is set: if there is no buffer available, reuse
 * the buffer from the other BAR and set the CF_SINGLE_BUFFER flag to
 * keep the interrupt handler from giving that buffer back to user
 * space.  In this way, we always have a buffer to DMA to and don't
 * have to try to play games stopping and restarting the controller.
 */
static void mcam_set_contig_buffer(struct mcam_camera *cam, int frame)
{
	struct mcam_vb_buffer *buf;
561 562 563
	dma_addr_t dma_handle;
	struct vb2_buffer *vb;

564 565 566 567 568 569
	/*
	 * If there are no available buffers, go into single mode
	 */
	if (list_empty(&cam->buffers)) {
		buf = cam->vb_bufs[frame ^ 0x1];
		set_bit(CF_SINGLE_BUFFER, &cam->flags);
570
		cam->frame_state.singles++;
571 572 573 574 575 576 577 578
	} else {
		/*
		 * OK, we have a buffer we can use.
		 */
		buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer,
					queue);
		list_del_init(&buf->queue);
		clear_bit(CF_SINGLE_BUFFER, &cam->flags);
579
	}
580 581

	cam->vb_bufs[frame] = buf;
582 583 584
	vb = &buf->vb_buf;

	dma_handle = vb2_dma_contig_plane_dma_addr(vb, 0);
585
	mcam_write_yuv_bases(cam, frame, dma_handle);
586 587
}

588 589 590
/*
 * Initial B_DMA_contig setup.
 */
591 592 593 594 595 596 597 598
static void mcam_ctlr_dma_contig(struct mcam_camera *cam)
{
	mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
	cam->nbufs = 2;
	mcam_set_contig_buffer(cam, 0);
	mcam_set_contig_buffer(cam, 1);
}

599 600 601 602 603 604 605 606
/*
 * Frame completion handling.
 */
static void mcam_dma_contig_done(struct mcam_camera *cam, int frame)
{
	struct mcam_vb_buffer *buf = cam->vb_bufs[frame];

	if (!test_bit(CF_SINGLE_BUFFER, &cam->flags)) {
607
		cam->frame_state.delivered++;
608
		cam->vb_bufs[frame] = NULL;
609 610 611 612 613
		mcam_buffer_done(cam, frame, &buf->vb_buf);
	}
	mcam_set_contig_buffer(cam, frame);
}

614
#endif /* MCAM_MODE_DMA_CONTIG */
615

616
#ifdef MCAM_MODE_DMA_SG
617 618 619 620
/* ---------------------------------------------------------------------- */
/*
 * Scatter/gather-specific code.
 */
621

622 623 624 625 626
/*
 * Set up the next buffer for S/G I/O; caller should be sure that
 * the controller is stopped and a buffer is available.
 */
static void mcam_sg_next_buffer(struct mcam_camera *cam)
627
{
628 629 630 631
	struct mcam_vb_buffer *buf;

	buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer, queue);
	list_del_init(&buf->queue);
632 633 634 635 636
	/*
	 * Very Bad Not Good Things happen if you don't clear
	 * C1_DESC_ENA before making any descriptor changes.
	 */
	mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_ENA);
637 638 639 640 641
	mcam_reg_write(cam, REG_DMA_DESC_Y, buf->dma_desc_pa);
	mcam_reg_write(cam, REG_DESC_LEN_Y,
			buf->dma_desc_nent*sizeof(struct mcam_dma_desc));
	mcam_reg_write(cam, REG_DESC_LEN_U, 0);
	mcam_reg_write(cam, REG_DESC_LEN_V, 0);
642
	mcam_reg_set_bit(cam, REG_CTRL1, C1_DESC_ENA);
643
	cam->vb_bufs[0] = buf;
644 645
}

646 647 648 649 650
/*
 * Initial B_DMA_sg setup
 */
static void mcam_ctlr_dma_sg(struct mcam_camera *cam)
{
651 652 653 654 655 656 657 658 659
	/*
	 * The list-empty condition can hit us at resume time
	 * if the buffer list was empty when the system was suspended.
	 */
	if (list_empty(&cam->buffers)) {
		set_bit(CF_SG_RESTART, &cam->flags);
		return;
	}

660 661 662 663 664
	mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_3WORD);
	mcam_sg_next_buffer(cam);
	cam->nbufs = 3;
}

665

666
/*
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
 * Frame completion with S/G is trickier.  We can't muck with
 * a descriptor chain on the fly, since the controller buffers it
 * internally.  So we have to actually stop and restart; Marvell
 * says this is the way to do it.
 *
 * Of course, stopping is easier said than done; experience shows
 * that the controller can start a frame *after* C0_ENABLE has been
 * cleared.  So when running in S/G mode, the controller is "stopped"
 * on receipt of the start-of-frame interrupt.  That means we can
 * safely change the DMA descriptor array here and restart things
 * (assuming there's another buffer waiting to go).
 */
static void mcam_dma_sg_done(struct mcam_camera *cam, int frame)
{
	struct mcam_vb_buffer *buf = cam->vb_bufs[0];

683 684 685 686 687
	/*
	 * If we're no longer supposed to be streaming, don't do anything.
	 */
	if (cam->state != S_STREAMING)
		return;
688 689 690 691 692 693 694 695 696 697 698 699 700
	/*
	 * If we have another buffer available, put it in and
	 * restart the engine.
	 */
	if (!list_empty(&cam->buffers)) {
		mcam_sg_next_buffer(cam);
		mcam_ctlr_start(cam);
	/*
	 * Otherwise set CF_SG_RESTART and the controller will
	 * be restarted once another buffer shows up.
	 */
	} else {
		set_bit(CF_SG_RESTART, &cam->flags);
701
		cam->frame_state.singles++;
702
		cam->vb_bufs[0] = NULL;
703 704 705 706
	}
	/*
	 * Now we can give the completed frame back to user space.
	 */
707
	cam->frame_state.delivered++;
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
	mcam_buffer_done(cam, frame, &buf->vb_buf);
}


/*
 * Scatter/gather mode requires stopping the controller between
 * frames so we can put in a new DMA descriptor array.  If no new
 * buffer exists at frame completion, the controller is left stopped;
 * this function is charged with gettig things going again.
 */
static void mcam_sg_restart(struct mcam_camera *cam)
{
	mcam_ctlr_dma_sg(cam);
	mcam_ctlr_start(cam);
	clear_bit(CF_SG_RESTART, &cam->flags);
}

725 726 727 728 729 730 731 732
#else /* MCAM_MODE_DMA_SG */

static inline void mcam_sg_restart(struct mcam_camera *cam)
{
	return;
}

#endif /* MCAM_MODE_DMA_SG */
733 734 735 736 737 738 739 740

/* ---------------------------------------------------------------------- */
/*
 * Buffer-mode-independent controller code.
 */

/*
 * Image format setup
741
 */
742 743 744
static void mcam_ctlr_image(struct mcam_camera *cam)
{
	struct v4l2_pix_format *fmt = &cam->pix_format;
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
	u32 widthy = 0, widthuv = 0, imgsz_h, imgsz_w;

	cam_dbg(cam, "camera: bytesperline = %d; height = %d\n",
		fmt->bytesperline, fmt->sizeimage / fmt->bytesperline);
	imgsz_h = (fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK;
	imgsz_w = (fmt->width * 2) & IMGSZ_H_MASK;

	switch (fmt->pixelformat) {
	case V4L2_PIX_FMT_YUYV:
	case V4L2_PIX_FMT_UYVY:
		widthy = fmt->width * 2;
		widthuv = 0;
		break;
	case V4L2_PIX_FMT_YUV422P:
	case V4L2_PIX_FMT_YUV420:
	case V4L2_PIX_FMT_YVU420:
		widthy = fmt->width;
		widthuv = fmt->width / 2;
		break;
	default:
		widthy = fmt->bytesperline;
		widthuv = 0;
	}

	mcam_reg_write_mask(cam, REG_IMGPITCH, widthuv << 16 | widthy,
			IMGP_YP_MASK | IMGP_UVP_MASK);
	mcam_reg_write(cam, REG_IMGSIZE, imgsz_h | imgsz_w);
	mcam_reg_write(cam, REG_IMGOFFSET, 0x0);
773 774 775 776

	/*
	 * Tell the controller about the image format we are using.
	 */
777 778 779 780 781 782 783 784 785 786
	switch (fmt->pixelformat) {
	case V4L2_PIX_FMT_YUV422P:
		mcam_reg_write_mask(cam, REG_CTRL0,
			C0_DF_YUV | C0_YUV_PLANAR | C0_YUVE_YVYU, C0_DF_MASK);
		break;
	case V4L2_PIX_FMT_YUV420:
	case V4L2_PIX_FMT_YVU420:
		mcam_reg_write_mask(cam, REG_CTRL0,
			C0_DF_YUV | C0_YUV_420PL | C0_YUVE_YVYU, C0_DF_MASK);
		break;
787
	case V4L2_PIX_FMT_YUYV:
788 789 790 791 792 793 794
		mcam_reg_write_mask(cam, REG_CTRL0,
			C0_DF_YUV | C0_YUV_PACKED | C0_YUVE_UYVY, C0_DF_MASK);
		break;
	case V4L2_PIX_FMT_UYVY:
		mcam_reg_write_mask(cam, REG_CTRL0,
			C0_DF_YUV | C0_YUV_PACKED | C0_YUVE_YUYV, C0_DF_MASK);
		break;
795
	case V4L2_PIX_FMT_RGB444:
796 797
		mcam_reg_write_mask(cam, REG_CTRL0,
			C0_DF_RGB | C0_RGBF_444 | C0_RGB4_XRGB, C0_DF_MASK);
798
		/* Alpha value? */
799
		break;
800
	case V4L2_PIX_FMT_RGB565:
801 802 803
		mcam_reg_write_mask(cam, REG_CTRL0,
			C0_DF_RGB | C0_RGBF_565 | C0_RGB5_BGGR, C0_DF_MASK);
		break;
804
	default:
805 806
		cam_err(cam, "camera: unknown format: %#x\n", fmt->pixelformat);
		break;
807
	}
808

809 810 811
	/*
	 * Make sure it knows we want to use hsync/vsync.
	 */
812
	mcam_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC, C0_SIFM_MASK);
813 814 815 816 817 818
	/*
	 * This field controls the generation of EOF(DVP only)
	 */
	if (cam->bus_type != V4L2_MBUS_CSI2)
		mcam_reg_set_bit(cam, REG_CTRL0,
				C0_EOF_VSYNC | C0_VEDGE_CTRL);
819 820 821 822 823 824 825 826 827 828 829 830
}


/*
 * Configure the controller for operation; caller holds the
 * device mutex.
 */
static int mcam_ctlr_configure(struct mcam_camera *cam)
{
	unsigned long flags;

	spin_lock_irqsave(&cam->dev_lock, flags);
831
	clear_bit(CF_SG_RESTART, &cam->flags);
832
	cam->dma_setup(cam);
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
	mcam_ctlr_image(cam);
	mcam_set_config_needed(cam, 0);
	spin_unlock_irqrestore(&cam->dev_lock, flags);
	return 0;
}

static void mcam_ctlr_irq_enable(struct mcam_camera *cam)
{
	/*
	 * Clear any pending interrupts, since we do not
	 * expect to have I/O active prior to enabling.
	 */
	mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
	mcam_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
}

static void mcam_ctlr_irq_disable(struct mcam_camera *cam)
{
	mcam_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
}


855

856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
static void mcam_ctlr_init(struct mcam_camera *cam)
{
	unsigned long flags;

	spin_lock_irqsave(&cam->dev_lock, flags);
	/*
	 * Make sure it's not powered down.
	 */
	mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
	/*
	 * Turn off the enable bit.  It sure should be off anyway,
	 * but it's good to be sure.
	 */
	mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
	/*
	 * Clock the sensor appropriately.  Controller clock should
	 * be 48MHz, sensor "typical" value is half that.
	 */
	mcam_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
	spin_unlock_irqrestore(&cam->dev_lock, flags);
}


/*
 * Stop the controller, and don't return until we're really sure that no
 * further DMA is going on.
 */
static void mcam_ctlr_stop_dma(struct mcam_camera *cam)
{
	unsigned long flags;

	/*
	 * Theory: stop the camera controller (whether it is operating
	 * or not).  Delay briefly just in case we race with the SOF
	 * interrupt, then wait until no DMA is active.
	 */
	spin_lock_irqsave(&cam->dev_lock, flags);
893
	clear_bit(CF_SG_RESTART, &cam->flags);
894
	mcam_ctlr_stop(cam);
895
	cam->state = S_IDLE;
896
	spin_unlock_irqrestore(&cam->dev_lock, flags);
897 898 899 900 901 902 903 904
	/*
	 * This is a brutally long sleep, but experience shows that
	 * it can take the controller a while to get the message that
	 * it needs to stop grabbing frames.  In particular, we can
	 * sometimes (on mmp) get a frame at the end WITHOUT the
	 * start-of-frame indication.
	 */
	msleep(150);
905 906 907 908 909 910 911 912 913 914 915
	if (test_bit(CF_DMA_ACTIVE, &cam->flags))
		cam_err(cam, "Timeout waiting for DMA to end\n");
		/* This would be bad news - what now? */
	spin_lock_irqsave(&cam->dev_lock, flags);
	mcam_ctlr_irq_disable(cam);
	spin_unlock_irqrestore(&cam->dev_lock, flags);
}

/*
 * Power up and down.
 */
916
static int mcam_ctlr_power_up(struct mcam_camera *cam)
917 918
{
	unsigned long flags;
919
	int ret;
920 921

	spin_lock_irqsave(&cam->dev_lock, flags);
922 923 924 925 926
	ret = cam->plat_power_up(cam);
	if (ret) {
		spin_unlock_irqrestore(&cam->dev_lock, flags);
		return ret;
	}
927
	mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
928 929
	spin_unlock_irqrestore(&cam->dev_lock, flags);
	msleep(5); /* Just to be sure */
930
	return 0;
931 932 933 934 935 936 937
}

static void mcam_ctlr_power_down(struct mcam_camera *cam)
{
	unsigned long flags;

	spin_lock_irqsave(&cam->dev_lock, flags);
938 939 940 941 942
	/*
	 * School of hard knocks department: be sure we do any register
	 * twiddling on the controller *before* calling the platform
	 * power down routine.
	 */
943
	mcam_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
944
	cam->plat_power_down(cam);
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
	spin_unlock_irqrestore(&cam->dev_lock, flags);
}

/* -------------------------------------------------------------------- */
/*
 * Communications with the sensor.
 */

static int __mcam_cam_reset(struct mcam_camera *cam)
{
	return sensor_call(cam, core, reset, 0);
}

/*
 * We have found the sensor on the i2c.  Let's try to have a
 * conversation.
 */
static int mcam_cam_init(struct mcam_camera *cam)
{
	int ret;

	if (cam->state != S_NOTREADY)
		cam_warn(cam, "Cam init with device in funky state %d",
				cam->state);
	ret = __mcam_cam_reset(cam);
970
	/* Get/set parameters? */
971 972 973
	cam->state = S_IDLE;
	mcam_ctlr_power_down(cam);
	return ret;
974 975
}

976 977 978 979 980
/*
 * Configure the sensor to match the parameters we have.  Caller should
 * hold s_mutex
 */
static int mcam_cam_set_flip(struct mcam_camera *cam)
981
{
982
	struct v4l2_control ctrl;
983

984 985 986 987
	memset(&ctrl, 0, sizeof(ctrl));
	ctrl.id = V4L2_CID_VFLIP;
	ctrl.value = flip;
	return sensor_call(cam, core, s_ctrl, &ctrl);
988 989 990
}


991 992 993 994
static int mcam_cam_configure(struct mcam_camera *cam)
{
	struct v4l2_mbus_framefmt mbus_fmt;
	int ret;
995

996 997 998 999 1000 1001 1002 1003 1004 1005
	v4l2_fill_mbus_format(&mbus_fmt, &cam->pix_format, cam->mbus_code);
	ret = sensor_call(cam, core, init, 0);
	if (ret == 0)
		ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
	/*
	 * OV7670 does weird things if flip is set *before* format...
	 */
	ret += mcam_cam_set_flip(cam);
	return ret;
}
1006 1007 1008 1009

/*
 * Get everything ready, and start grabbing frames.
 */
1010
static int mcam_read_setup(struct mcam_camera *cam)
1011 1012 1013 1014 1015 1016 1017 1018
{
	int ret;
	unsigned long flags;

	/*
	 * Configuration.  If we still don't have DMA buffers,
	 * make one last, desperate attempt.
	 */
1019 1020 1021
	if (cam->buffer_mode == B_vmalloc && cam->nbufs == 0 &&
			mcam_alloc_dma_bufs(cam, 0))
		return -ENOMEM;
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033

	if (mcam_needs_config(cam)) {
		mcam_cam_configure(cam);
		ret = mcam_ctlr_configure(cam);
		if (ret)
			return ret;
	}

	/*
	 * Turn it loose.
	 */
	spin_lock_irqsave(&cam->dev_lock, flags);
1034
	clear_bit(CF_DMA_ACTIVE, &cam->flags);
1035
	mcam_reset_buffers(cam);
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
	/*
	 * Update CSI2_DPHY value
	 */
	if (cam->calc_dphy)
		cam->calc_dphy(cam);
	cam_dbg(cam, "camera: DPHY sets: dphy3=0x%x, dphy5=0x%x, dphy6=0x%x\n",
			cam->dphy[0], cam->dphy[1], cam->dphy[2]);
	if (cam->bus_type == V4L2_MBUS_CSI2)
		mcam_enable_mipi(cam);
	else
		mcam_disable_mipi(cam);
1047
	mcam_ctlr_irq_enable(cam);
1048
	cam->state = S_STREAMING;
1049 1050
	if (!test_bit(CF_SG_RESTART, &cam->flags))
		mcam_ctlr_start(cam);
1051 1052 1053 1054
	spin_unlock_irqrestore(&cam->dev_lock, flags);
	return 0;
}

1055 1056 1057 1058
/* ----------------------------------------------------------------------- */
/*
 * Videobuf2 interface code.
 */
1059

1060 1061
static int mcam_vb_queue_setup(struct vb2_queue *vq,
		const struct v4l2_format *fmt, unsigned int *nbufs,
1062
		unsigned int *num_planes, unsigned int sizes[],
1063
		void *alloc_ctxs[])
1064
{
1065
	struct mcam_camera *cam = vb2_get_drv_priv(vq);
1066
	int minbufs = (cam->buffer_mode == B_DMA_contig) ? 3 : 2;
1067

1068 1069 1070
	if (fmt && fmt->fmt.pix.sizeimage < cam->pix_format.sizeimage)
		return -EINVAL;
	sizes[0] = fmt ? fmt->fmt.pix.sizeimage : cam->pix_format.sizeimage;
1071
	*num_planes = 1; /* Someday we have to support planar formats... */
1072 1073
	if (*nbufs < minbufs)
		*nbufs = minbufs;
1074 1075
	if (cam->buffer_mode == B_DMA_contig)
		alloc_ctxs[0] = cam->vb_alloc_ctx;
1076 1077
	else if (cam->buffer_mode == B_DMA_sg)
		alloc_ctxs[0] = cam->vb_alloc_ctx_sg;
1078 1079 1080
	return 0;
}

1081

1082 1083 1084 1085 1086
static void mcam_vb_buf_queue(struct vb2_buffer *vb)
{
	struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
	struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
	unsigned long flags;
1087
	int start;
1088 1089

	spin_lock_irqsave(&cam->dev_lock, flags);
1090 1091
	start = (cam->state == S_BUFWAIT) && !list_empty(&cam->buffers);
	list_add(&mvb->queue, &cam->buffers);
1092
	if (cam->state == S_STREAMING && test_bit(CF_SG_RESTART, &cam->flags))
1093
		mcam_sg_restart(cam);
1094
	spin_unlock_irqrestore(&cam->dev_lock, flags);
1095 1096
	if (start)
		mcam_read_setup(cam);
1097 1098
}

1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
static void mcam_vb_requeue_bufs(struct vb2_queue *vq,
				 enum vb2_buffer_state state)
{
	struct mcam_camera *cam = vb2_get_drv_priv(vq);
	struct mcam_vb_buffer *buf, *node;
	unsigned long flags;
	unsigned i;

	spin_lock_irqsave(&cam->dev_lock, flags);
	list_for_each_entry_safe(buf, node, &cam->buffers, queue) {
		vb2_buffer_done(&buf->vb_buf, state);
		list_del(&buf->queue);
	}
	for (i = 0; i < MAX_DMA_BUFS; i++) {
		buf = cam->vb_bufs[i];

		if (buf) {
			vb2_buffer_done(&buf->vb_buf, state);
			cam->vb_bufs[i] = NULL;
		}
	}
	spin_unlock_irqrestore(&cam->dev_lock, flags);
}

1123 1124 1125
/*
 * These need to be called with the mutex held from vb2
 */
1126
static int mcam_vb_start_streaming(struct vb2_queue *vq, unsigned int count)
1127 1128
{
	struct mcam_camera *cam = vb2_get_drv_priv(vq);
1129
	unsigned int frame;
1130
	int ret;
1131

1132
	if (cam->state != S_IDLE) {
1133
		mcam_vb_requeue_bufs(vq, VB2_BUF_STATE_QUEUED);
1134
		return -EINVAL;
1135
	}
1136 1137 1138
	cam->frame_state.frames = 0;
	cam->frame_state.singles = 0;
	cam->frame_state.delivered = 0;
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
	cam->sequence = 0;
	/*
	 * Videobuf2 sneakily hoards all the buffers and won't
	 * give them to us until *after* streaming starts.  But
	 * we can't actually start streaming until we have a
	 * destination.  So go into a wait state and hope they
	 * give us buffers soon.
	 */
	if (cam->buffer_mode != B_vmalloc && list_empty(&cam->buffers)) {
		cam->state = S_BUFWAIT;
		return 0;
1150
	}
1151 1152 1153 1154 1155 1156 1157 1158

	/*
	 * Ensure clear the left over frame flags
	 * before every really start streaming
	 */
	for (frame = 0; frame < cam->nbufs; frame++)
		clear_bit(CF_FRAME_SOF0 + frame, &cam->flags);

1159 1160 1161 1162
	ret = mcam_read_setup(cam);
	if (ret)
		mcam_vb_requeue_bufs(vq, VB2_BUF_STATE_QUEUED);
	return ret;
1163 1164
}

1165
static void mcam_vb_stop_streaming(struct vb2_queue *vq)
1166 1167 1168
{
	struct mcam_camera *cam = vb2_get_drv_priv(vq);

1169 1170 1171
	cam_dbg(cam, "stop_streaming: %d frames, %d singles, %d delivered\n",
			cam->frame_state.frames, cam->frame_state.singles,
			cam->frame_state.delivered);
1172 1173 1174
	if (cam->state == S_BUFWAIT) {
		/* They never gave us buffers */
		cam->state = S_IDLE;
1175
		return;
1176
	}
1177
	if (cam->state != S_STREAMING)
1178
		return;
1179
	mcam_ctlr_stop_dma(cam);
1180 1181 1182 1183 1184 1185
	/*
	 * Reset the CCIC PHY after stopping streaming,
	 * otherwise, the CCIC may be unstable.
	 */
	if (cam->ctlr_reset)
		cam->ctlr_reset(cam);
1186
	/*
1187 1188
	 * VB2 reclaims the buffers, so we need to forget
	 * about them.
1189
	 */
1190
	mcam_vb_requeue_bufs(vq, VB2_BUF_STATE_ERROR);
1191 1192 1193
}


1194 1195 1196 1197 1198
static const struct vb2_ops mcam_vb2_ops = {
	.queue_setup		= mcam_vb_queue_setup,
	.buf_queue		= mcam_vb_buf_queue,
	.start_streaming	= mcam_vb_start_streaming,
	.stop_streaming		= mcam_vb_stop_streaming,
1199 1200
	.wait_prepare		= vb2_ops_wait_prepare,
	.wait_finish		= vb2_ops_wait_finish,
1201
};
1202

1203 1204

#ifdef MCAM_MODE_DMA_SG
1205
/*
1206 1207
 * Scatter/gather mode uses all of the above functions plus a
 * few extras to deal with DMA mapping.
1208
 */
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
static int mcam_vb_sg_buf_init(struct vb2_buffer *vb)
{
	struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
	struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
	int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;

	mvb->dma_desc = dma_alloc_coherent(cam->dev,
			ndesc * sizeof(struct mcam_dma_desc),
			&mvb->dma_desc_pa, GFP_KERNEL);
	if (mvb->dma_desc == NULL) {
		cam_err(cam, "Unable to get DMA descriptor array\n");
		return -ENOMEM;
	}
	return 0;
}

static int mcam_vb_sg_buf_prepare(struct vb2_buffer *vb)
{
	struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
1228
	struct sg_table *sg_table = vb2_dma_sg_plane_desc(vb, 0);
1229 1230 1231 1232
	struct mcam_dma_desc *desc = mvb->dma_desc;
	struct scatterlist *sg;
	int i;

1233
	for_each_sg(sg_table->sgl, sg, sg_table->nents, i) {
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
		desc->dma_addr = sg_dma_address(sg);
		desc->segment_len = sg_dma_len(sg);
		desc++;
	}
	return 0;
}

static void mcam_vb_sg_buf_cleanup(struct vb2_buffer *vb)
{
	struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
	struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
	int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;

	dma_free_coherent(cam->dev, ndesc * sizeof(struct mcam_dma_desc),
			mvb->dma_desc, mvb->dma_desc_pa);
}


1252 1253 1254 1255 1256 1257 1258 1259
static const struct vb2_ops mcam_vb2_sg_ops = {
	.queue_setup		= mcam_vb_queue_setup,
	.buf_init		= mcam_vb_sg_buf_init,
	.buf_prepare		= mcam_vb_sg_buf_prepare,
	.buf_queue		= mcam_vb_buf_queue,
	.buf_cleanup		= mcam_vb_sg_buf_cleanup,
	.start_streaming	= mcam_vb_start_streaming,
	.stop_streaming		= mcam_vb_stop_streaming,
1260 1261
	.wait_prepare		= vb2_ops_wait_prepare,
	.wait_finish		= vb2_ops_wait_finish,
1262 1263
};

1264 1265
#endif /* MCAM_MODE_DMA_SG */

1266 1267 1268
static int mcam_setup_vb2(struct mcam_camera *cam)
{
	struct vb2_queue *vq = &cam->vb_queue;
1269

1270 1271 1272
	memset(vq, 0, sizeof(*vq));
	vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	vq->drv_priv = cam;
1273
	vq->lock = &cam->s_mutex;
1274
	vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1275 1276
	vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
	vq->buf_struct_size = sizeof(struct mcam_vb_buffer);
1277 1278 1279
	INIT_LIST_HEAD(&cam->buffers);
	switch (cam->buffer_mode) {
	case B_DMA_contig:
1280
#ifdef MCAM_MODE_DMA_CONTIG
1281
		vq->ops = &mcam_vb2_ops;
1282
		vq->mem_ops = &vb2_dma_contig_memops;
1283 1284
		cam->dma_setup = mcam_ctlr_dma_contig;
		cam->frame_complete = mcam_dma_contig_done;
1285 1286 1287
		cam->vb_alloc_ctx = vb2_dma_contig_init_ctx(cam->dev);
		if (IS_ERR(cam->vb_alloc_ctx))
			return PTR_ERR(cam->vb_alloc_ctx);
1288
#endif
1289 1290
		break;
	case B_DMA_sg:
1291
#ifdef MCAM_MODE_DMA_SG
1292 1293
		vq->ops = &mcam_vb2_sg_ops;
		vq->mem_ops = &vb2_dma_sg_memops;
1294 1295
		cam->dma_setup = mcam_ctlr_dma_sg;
		cam->frame_complete = mcam_dma_sg_done;
1296 1297 1298
		cam->vb_alloc_ctx_sg = vb2_dma_sg_init_ctx(cam->dev);
		if (IS_ERR(cam->vb_alloc_ctx_sg))
			return PTR_ERR(cam->vb_alloc_ctx_sg);
1299
#endif
1300 1301
		break;
	case B_vmalloc:
1302 1303 1304
#ifdef MCAM_MODE_VMALLOC
		tasklet_init(&cam->s_tasklet, mcam_frame_tasklet,
				(unsigned long) cam);
1305
		vq->ops = &mcam_vb2_ops;
1306
		vq->mem_ops = &vb2_vmalloc_memops;
1307 1308 1309
		cam->dma_setup = mcam_ctlr_dma_vmalloc;
		cam->frame_complete = mcam_vmalloc_done;
#endif
1310 1311
		break;
	}
1312 1313 1314 1315 1316
	return vb2_queue_init(vq);
}

static void mcam_cleanup_vb2(struct mcam_camera *cam)
{
1317
#ifdef MCAM_MODE_DMA_CONTIG
1318 1319
	if (cam->buffer_mode == B_DMA_contig)
		vb2_dma_contig_cleanup_ctx(cam->vb_alloc_ctx);
1320
#endif
1321 1322 1323 1324
#ifdef MCAM_MODE_DMA_SG
	if (cam->buffer_mode == B_DMA_sg)
		vb2_dma_sg_cleanup_ctx(cam->vb_alloc_ctx_sg);
#endif
1325 1326
}

1327

1328
/* ---------------------------------------------------------------------- */
1329
/*
1330
 * The long list of V4L2 ioctl() operations.
1331 1332 1333 1334 1335
 */

static int mcam_vidioc_querycap(struct file *file, void *priv,
		struct v4l2_capability *cap)
{
1336
	struct mcam_camera *cam = video_drvdata(file);
1337

1338 1339
	strcpy(cap->driver, "marvell_ccic");
	strcpy(cap->card, "marvell_ccic");
1340
	strlcpy(cap->bus_info, cam->bus_info, sizeof(cap->bus_info));
1341
	cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
1342
		V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1343
	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
	return 0;
}


static int mcam_vidioc_enum_fmt_vid_cap(struct file *filp,
		void *priv, struct v4l2_fmtdesc *fmt)
{
	if (fmt->index >= N_MCAM_FMTS)
		return -EINVAL;
	strlcpy(fmt->description, mcam_formats[fmt->index].desc,
			sizeof(fmt->description));
	fmt->pixelformat = mcam_formats[fmt->index].pixelformat;
	return 0;
}

static int mcam_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
		struct v4l2_format *fmt)
{
1362
	struct mcam_camera *cam = video_drvdata(filp);
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
	struct mcam_format_struct *f;
	struct v4l2_pix_format *pix = &fmt->fmt.pix;
	struct v4l2_mbus_framefmt mbus_fmt;
	int ret;

	f = mcam_find_format(pix->pixelformat);
	pix->pixelformat = f->pixelformat;
	v4l2_fill_mbus_format(&mbus_fmt, pix, f->mbus_code);
	ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
	v4l2_fill_pix_format(pix, &mbus_fmt);
1373 1374 1375 1376 1377 1378 1379 1380 1381
	switch (f->pixelformat) {
	case V4L2_PIX_FMT_YUV420:
	case V4L2_PIX_FMT_YVU420:
		pix->bytesperline = pix->width * 3 / 2;
		break;
	default:
		pix->bytesperline = pix->width * f->bpp;
		break;
	}
1382
	pix->sizeimage = pix->height * pix->bytesperline;
1383
	pix->colorspace = V4L2_COLORSPACE_SRGB;
1384 1385 1386 1387 1388 1389
	return ret;
}

static int mcam_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
		struct v4l2_format *fmt)
{
1390
	struct mcam_camera *cam = video_drvdata(filp);
1391 1392 1393 1394 1395 1396 1397
	struct mcam_format_struct *f;
	int ret;

	/*
	 * Can't do anything if the device is not idle
	 * Also can't if there are streaming buffers in place.
	 */
1398
	if (cam->state != S_IDLE || vb2_is_busy(&cam->vb_queue))
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
		return -EBUSY;

	f = mcam_find_format(fmt->fmt.pix.pixelformat);

	/*
	 * See if the formatting works in principle.
	 */
	ret = mcam_vidioc_try_fmt_vid_cap(filp, priv, fmt);
	if (ret)
		return ret;
	/*
	 * Now we start to change things for real, so let's do it
	 * under lock.
	 */
	cam->pix_format = fmt->fmt.pix;
	cam->mbus_code = f->mbus_code;

	/*
	 * Make sure we have appropriate DMA buffers.
	 */
1419
	if (cam->buffer_mode == B_vmalloc) {
1420 1421 1422
		ret = mcam_check_dma_buffers(cam);
		if (ret)
			goto out;
1423
	}
1424
	mcam_set_config_needed(cam, 1);
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
out:
	return ret;
}

/*
 * Return our stored notion of how the camera is/should be configured.
 * The V4l2 spec wants us to be smarter, and actually get this from
 * the camera (and not mess with it at open time).  Someday.
 */
static int mcam_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
		struct v4l2_format *f)
{
1437
	struct mcam_camera *cam = video_drvdata(filp);
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476

	f->fmt.pix = cam->pix_format;
	return 0;
}

/*
 * We only have one input - the sensor - so minimize the nonsense here.
 */
static int mcam_vidioc_enum_input(struct file *filp, void *priv,
		struct v4l2_input *input)
{
	if (input->index != 0)
		return -EINVAL;

	input->type = V4L2_INPUT_TYPE_CAMERA;
	strcpy(input->name, "Camera");
	return 0;
}

static int mcam_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
{
	*i = 0;
	return 0;
}

static int mcam_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
{
	if (i != 0)
		return -EINVAL;
	return 0;
}

/*
 * G/S_PARM.  Most of this is done by the sensor, but we are
 * the level which controls the number of read buffers.
 */
static int mcam_vidioc_g_parm(struct file *filp, void *priv,
		struct v4l2_streamparm *parms)
{
1477
	struct mcam_camera *cam = video_drvdata(filp);
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
	int ret;

	ret = sensor_call(cam, video, g_parm, parms);
	parms->parm.capture.readbuffers = n_dma_bufs;
	return ret;
}

static int mcam_vidioc_s_parm(struct file *filp, void *priv,
		struct v4l2_streamparm *parms)
{
1488
	struct mcam_camera *cam = video_drvdata(filp);
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
	int ret;

	ret = sensor_call(cam, video, s_parm, parms);
	parms->parm.capture.readbuffers = n_dma_bufs;
	return ret;
}

static int mcam_vidioc_enum_framesizes(struct file *filp, void *priv,
		struct v4l2_frmsizeenum *sizes)
{
1499
	struct mcam_camera *cam = video_drvdata(filp);
1500 1501 1502 1503 1504
	struct mcam_format_struct *f;
	struct v4l2_subdev_frame_size_enum fse = {
		.index = sizes->index,
		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
	};
1505 1506
	int ret;

1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
	f = mcam_find_format(sizes->pixel_format);
	if (f->pixelformat != sizes->pixel_format)
		return -EINVAL;
	fse.code = f->mbus_code;
	ret = sensor_call(cam, pad, enum_frame_size, NULL, &fse);
	if (ret)
		return ret;
	if (fse.min_width == fse.max_width &&
	    fse.min_height == fse.max_height) {
		sizes->type = V4L2_FRMSIZE_TYPE_DISCRETE;
		sizes->discrete.width = fse.min_width;
		sizes->discrete.height = fse.min_height;
		return 0;
	}
	sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
	sizes->stepwise.min_width = fse.min_width;
	sizes->stepwise.max_width = fse.max_width;
	sizes->stepwise.min_height = fse.min_height;
	sizes->stepwise.max_height = fse.max_height;
	sizes->stepwise.step_width = 1;
	sizes->stepwise.step_height = 1;
	return 0;
1529 1530 1531 1532 1533
}

static int mcam_vidioc_enum_frameintervals(struct file *filp, void *priv,
		struct v4l2_frmivalenum *interval)
{
1534
	struct mcam_camera *cam = video_drvdata(filp);
1535 1536 1537 1538 1539 1540 1541
	struct mcam_format_struct *f;
	struct v4l2_subdev_frame_interval_enum fie = {
		.index = interval->index,
		.width = interval->width,
		.height = interval->height,
		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
	};
1542 1543
	int ret;

1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
	f = mcam_find_format(interval->pixel_format);
	if (f->pixelformat != interval->pixel_format)
		return -EINVAL;
	fie.code = f->mbus_code;
	ret = sensor_call(cam, pad, enum_frame_interval, NULL, &fie);
	if (ret)
		return ret;
	interval->type = V4L2_FRMIVAL_TYPE_DISCRETE;
	interval->discrete = fie.interval;
	return 0;
1554 1555 1556 1557 1558 1559
}

#ifdef CONFIG_VIDEO_ADV_DEBUG
static int mcam_vidioc_g_register(struct file *file, void *priv,
		struct v4l2_dbg_register *reg)
{
1560
	struct mcam_camera *cam = video_drvdata(file);
1561

1562 1563
	if (reg->reg > cam->regs_size - 4)
		return -EINVAL;
1564 1565 1566
	reg->val = mcam_reg_read(cam, reg->reg);
	reg->size = 4;
	return 0;
1567 1568 1569
}

static int mcam_vidioc_s_register(struct file *file, void *priv,
1570
		const struct v4l2_dbg_register *reg)
1571
{
1572
	struct mcam_camera *cam = video_drvdata(file);
1573

1574 1575
	if (reg->reg > cam->regs_size - 4)
		return -EINVAL;
1576 1577
	mcam_reg_write(cam, reg->reg, reg->val);
	return 0;
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
}
#endif

static const struct v4l2_ioctl_ops mcam_v4l_ioctl_ops = {
	.vidioc_querycap	= mcam_vidioc_querycap,
	.vidioc_enum_fmt_vid_cap = mcam_vidioc_enum_fmt_vid_cap,
	.vidioc_try_fmt_vid_cap	= mcam_vidioc_try_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap	= mcam_vidioc_s_fmt_vid_cap,
	.vidioc_g_fmt_vid_cap	= mcam_vidioc_g_fmt_vid_cap,
	.vidioc_enum_input	= mcam_vidioc_enum_input,
	.vidioc_g_input		= mcam_vidioc_g_input,
	.vidioc_s_input		= mcam_vidioc_s_input,
1590
	.vidioc_reqbufs		= vb2_ioctl_reqbufs,
1591
	.vidioc_create_bufs	= vb2_ioctl_create_bufs,
1592 1593 1594
	.vidioc_querybuf	= vb2_ioctl_querybuf,
	.vidioc_qbuf		= vb2_ioctl_qbuf,
	.vidioc_dqbuf		= vb2_ioctl_dqbuf,
1595
	.vidioc_expbuf		= vb2_ioctl_expbuf,
1596 1597
	.vidioc_streamon	= vb2_ioctl_streamon,
	.vidioc_streamoff	= vb2_ioctl_streamoff,
1598 1599 1600 1601
	.vidioc_g_parm		= mcam_vidioc_g_parm,
	.vidioc_s_parm		= mcam_vidioc_s_parm,
	.vidioc_enum_framesizes = mcam_vidioc_enum_framesizes,
	.vidioc_enum_frameintervals = mcam_vidioc_enum_frameintervals,
1602 1603
	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1604 1605 1606 1607 1608 1609 1610 1611
#ifdef CONFIG_VIDEO_ADV_DEBUG
	.vidioc_g_register	= mcam_vidioc_g_register,
	.vidioc_s_register	= mcam_vidioc_s_register,
#endif
};

/* ---------------------------------------------------------------------- */
/*
1612
 * Our various file operations.
1613
 */
1614 1615 1616
static int mcam_v4l_open(struct file *filp)
{
	struct mcam_camera *cam = video_drvdata(filp);
1617
	int ret;
1618

1619
	mutex_lock(&cam->s_mutex);
1620 1621 1622 1623
	ret = v4l2_fh_open(filp);
	if (ret)
		goto out;
	if (v4l2_fh_is_singular_file(filp)) {
1624 1625 1626
		ret = mcam_ctlr_power_up(cam);
		if (ret)
			goto out;
1627 1628 1629 1630 1631
		__mcam_cam_reset(cam);
		mcam_set_config_needed(cam, 1);
	}
out:
	mutex_unlock(&cam->s_mutex);
1632 1633
	if (ret)
		v4l2_fh_release(filp);
1634
	return ret;
1635
}
1636 1637


1638 1639
static int mcam_v4l_release(struct file *filp)
{
1640
	struct mcam_camera *cam = video_drvdata(filp);
1641
	bool last_open;
1642

1643
	mutex_lock(&cam->s_mutex);
1644 1645 1646
	last_open = v4l2_fh_is_singular_file(filp);
	_vb2_fop_release(filp, NULL);
	if (last_open) {
1647
		mcam_disable_mipi(cam);
1648 1649 1650 1651
		mcam_ctlr_power_down(cam);
		if (cam->buffer_mode == B_vmalloc && alloc_bufs_at_read)
			mcam_free_dma_bufs(cam);
	}
1652

1653 1654
	mutex_unlock(&cam->s_mutex);
	return 0;
1655 1656
}

1657 1658 1659 1660
static const struct v4l2_file_operations mcam_v4l_fops = {
	.owner = THIS_MODULE,
	.open = mcam_v4l_open,
	.release = mcam_v4l_release,
1661 1662 1663
	.read = vb2_fop_read,
	.poll = vb2_fop_poll,
	.mmap = vb2_fop_mmap,
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
	.unlocked_ioctl = video_ioctl2,
};


/*
 * This template device holds all of those v4l2 methods; we
 * clone it for specific real devices.
 */
static struct video_device mcam_v4l_template = {
	.name = "mcam",
	.fops = &mcam_v4l_fops,
	.ioctl_ops = &mcam_v4l_ioctl_ops,
	.release = video_device_release_empty,
};

/* ---------------------------------------------------------------------- */
/*
 * Interrupt handler stuff
 */
1683 1684 1685 1686 1687 1688 1689
static void mcam_frame_complete(struct mcam_camera *cam, int frame)
{
	/*
	 * Basic frame housekeeping.
	 */
	set_bit(frame, &cam->flags);
	clear_bit(CF_DMA_ACTIVE, &cam->flags);
1690
	cam->next_buf = frame;
1691
	cam->buf_seq[frame] = cam->sequence++;
1692
	cam->frame_state.frames++;
1693
	/*
1694
	 * "This should never happen"
1695
	 */
1696 1697 1698 1699 1700
	if (cam->state != S_STREAMING)
		return;
	/*
	 * Process the frame and set up the next one.
	 */
1701
	cam->frame_complete(cam, frame);
1702 1703 1704
}


1705 1706 1707 1708
/*
 * The interrupt handler; this needs to be called from the
 * platform irq handler with the lock held.
 */
1709 1710 1711 1712 1713 1714 1715 1716 1717
int mccic_irq(struct mcam_camera *cam, unsigned int irqs)
{
	unsigned int frame, handled = 0;

	mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
	/*
	 * Handle any frame completions.  There really should
	 * not be more than one of these, or we have fallen
	 * far behind.
1718 1719 1720 1721 1722
	 *
	 * When running in S/G mode, the frame number lacks any
	 * real meaning - there's only one descriptor array - but
	 * the controller still picks a different one to signal
	 * each time.
1723 1724
	 */
	for (frame = 0; frame < cam->nbufs; frame++)
1725 1726
		if (irqs & (IRQ_EOF0 << frame) &&
			test_bit(CF_FRAME_SOF0 + frame, &cam->flags)) {
1727 1728
			mcam_frame_complete(cam, frame);
			handled = 1;
1729
			clear_bit(CF_FRAME_SOF0 + frame, &cam->flags);
1730 1731
			if (cam->buffer_mode == B_DMA_sg)
				break;
1732 1733 1734 1735 1736 1737
		}
	/*
	 * If a frame starts, note that we have DMA active.  This
	 * code assumes that we won't get multiple frame interrupts
	 * at once; may want to rethink that.
	 */
1738 1739 1740 1741 1742 1743 1744 1745
	for (frame = 0; frame < cam->nbufs; frame++) {
		if (irqs & (IRQ_SOF0 << frame)) {
			set_bit(CF_FRAME_SOF0 + frame, &cam->flags);
			handled = IRQ_HANDLED;
		}
	}

	if (handled == IRQ_HANDLED) {
1746
		set_bit(CF_DMA_ACTIVE, &cam->flags);
1747 1748
		if (cam->buffer_mode == B_DMA_sg)
			mcam_ctlr_stop(cam);
1749 1750 1751 1752
	}
	return handled;
}

1753
/* ---------------------------------------------------------------------- */
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
/*
 * Registration and such.
 */
static struct ov7670_config sensor_cfg = {
	/*
	 * Exclude QCIF mode, because it only captures a tiny portion
	 * of the sensor FOV
	 */
	.min_width = 320,
	.min_height = 240,
};


int mccic_register(struct mcam_camera *cam)
{
	struct i2c_board_info ov7670_info = {
		.type = "ov7670",
1771
		.addr = 0x42 >> 1,
1772 1773 1774 1775
		.platform_data = &sensor_cfg,
	};
	int ret;

1776 1777 1778 1779 1780 1781
	/*
	 * Validate the requested buffer mode.
	 */
	if (buffer_mode >= 0)
		cam->buffer_mode = buffer_mode;
	if (cam->buffer_mode == B_DMA_sg &&
1782
			cam->chip_id == MCAM_CAFE) {
1783 1784 1785 1786 1787 1788 1789 1790 1791
		printk(KERN_ERR "marvell-cam: Cafe can't do S/G I/O, "
			"attempting vmalloc mode instead\n");
		cam->buffer_mode = B_vmalloc;
	}
	if (!mcam_buffer_mode_supported(cam->buffer_mode)) {
		printk(KERN_ERR "marvell-cam: buffer mode %d unsupported\n",
				cam->buffer_mode);
		return -EINVAL;
	}
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
	/*
	 * Register with V4L
	 */
	ret = v4l2_device_register(cam->dev, &cam->v4l2_dev);
	if (ret)
		return ret;

	mutex_init(&cam->s_mutex);
	cam->state = S_NOTREADY;
	mcam_set_config_needed(cam, 1);
	cam->pix_format = mcam_def_pix_format;
	cam->mbus_code = mcam_def_mbus_code;
	mcam_ctlr_init(cam);

1806 1807 1808 1809 1810 1811 1812 1813
	/*
	 * Get the v4l2 setup done.
	 */
	ret = v4l2_ctrl_handler_init(&cam->ctrl_handler, 10);
	if (ret)
		goto out_unregister;
	cam->v4l2_dev.ctrl_handler = &cam->ctrl_handler;

1814 1815 1816
	/*
	 * Try to find the sensor.
	 */
1817 1818
	sensor_cfg.clock_speed = cam->clock_speed;
	sensor_cfg.use_smbus = cam->use_smbus;
1819 1820
	cam->sensor_addr = ov7670_info.addr;
	cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev,
1821
			cam->i2c_adapter, &ov7670_info, NULL);
1822 1823 1824 1825 1826 1827 1828 1829
	if (cam->sensor == NULL) {
		ret = -ENODEV;
		goto out_unregister;
	}

	ret = mcam_cam_init(cam);
	if (ret)
		goto out_unregister;
1830

1831 1832 1833 1834
	ret = mcam_setup_vb2(cam);
	if (ret)
		goto out_unregister;

1835 1836 1837
	mutex_lock(&cam->s_mutex);
	cam->vdev = mcam_v4l_template;
	cam->vdev.v4l2_dev = &cam->v4l2_dev;
1838 1839
	cam->vdev.lock = &cam->s_mutex;
	cam->vdev.queue = &cam->vb_queue;
1840
	video_set_drvdata(&cam->vdev, cam);
1841
	ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1842 1843 1844 1845
	if (ret) {
		mutex_unlock(&cam->s_mutex);
		goto out_unregister;
	}
1846 1847 1848 1849

	/*
	 * If so requested, try to get our DMA buffers now.
	 */
1850
	if (cam->buffer_mode == B_vmalloc && !alloc_bufs_at_read) {
1851 1852 1853 1854 1855 1856
		if (mcam_alloc_dma_bufs(cam, 1))
			cam_warn(cam, "Unable to alloc DMA buffers at load"
					" will try again later.");
	}

	mutex_unlock(&cam->s_mutex);
1857 1858
	return 0;

1859
out_unregister:
1860
	v4l2_ctrl_handler_free(&cam->ctrl_handler);
1861 1862 1863 1864 1865 1866 1867
	v4l2_device_unregister(&cam->v4l2_dev);
	return ret;
}


void mccic_shutdown(struct mcam_camera *cam)
{
1868 1869 1870 1871 1872 1873
	/*
	 * If we have no users (and we really, really should have no
	 * users) the device will already be powered down.  Trying to
	 * take it down again will wedge the machine, which is frowned
	 * upon.
	 */
1874
	if (!list_empty(&cam->vdev.fh_list)) {
1875
		cam_warn(cam, "Removing a device with users!\n");
1876 1877
		mcam_ctlr_power_down(cam);
	}
1878
	mcam_cleanup_vb2(cam);
1879 1880
	if (cam->buffer_mode == B_vmalloc)
		mcam_free_dma_bufs(cam);
1881
	video_unregister_device(&cam->vdev);
1882
	v4l2_ctrl_handler_free(&cam->ctrl_handler);
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
	v4l2_device_unregister(&cam->v4l2_dev);
}

/*
 * Power management
 */
#ifdef CONFIG_PM

void mccic_suspend(struct mcam_camera *cam)
{
1893
	mutex_lock(&cam->s_mutex);
1894
	if (!list_empty(&cam->vdev.fh_list)) {
1895
		enum mcam_state cstate = cam->state;
1896

1897 1898 1899 1900 1901
		mcam_ctlr_stop_dma(cam);
		mcam_ctlr_power_down(cam);
		cam->state = cstate;
	}
	mutex_unlock(&cam->s_mutex);
1902 1903 1904 1905 1906 1907 1908
}

int mccic_resume(struct mcam_camera *cam)
{
	int ret = 0;

	mutex_lock(&cam->s_mutex);
1909
	if (!list_empty(&cam->vdev.fh_list)) {
1910 1911 1912 1913 1914
		ret = mcam_ctlr_power_up(cam);
		if (ret) {
			mutex_unlock(&cam->s_mutex);
			return ret;
		}
1915 1916 1917 1918 1919 1920 1921
		__mcam_cam_reset(cam);
	} else {
		mcam_ctlr_power_down(cam);
	}
	mutex_unlock(&cam->s_mutex);

	set_bit(CF_CONFIG_NEEDED, &cam->flags);
1922 1923 1924 1925 1926 1927 1928
	if (cam->state == S_STREAMING) {
		/*
		 * If there was a buffer in the DMA engine at suspend
		 * time, put it back on the queue or we'll forget about it.
		 */
		if (cam->buffer_mode == B_DMA_sg && cam->vb_bufs[0])
			list_add(&cam->vb_bufs[0]->queue, &cam->buffers);
1929
		ret = mcam_read_setup(cam);
1930
	}
1931 1932 1933
	return ret;
}
#endif /* CONFIG_PM */