mx1_camera.c 21.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * V4L2 Driver for i.MXL/i.MXL camera (CSI) host
 *
 * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
 * Copyright (C) 2009, Darius Augulis <augulis.darius@gmail.com>
 *
 * Based on PXA SoC camera driver
 * Copyright (C) 2006, Sascha Hauer, Pengutronix
 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
 *
 * 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/clk.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
31
#include <linux/sched.h>
32
#include <linux/slab.h>
33 34 35 36 37 38 39
#include <linux/time.h>
#include <linux/videodev2.h>

#include <media/soc_camera.h>
#include <media/v4l2-common.h>
#include <media/v4l2-dev.h>
#include <media/videobuf-dma-contig.h>
40
#include <media/soc_mediabus.h>
41 42 43 44 45

#include <asm/dma.h>
#include <asm/fiq.h>
#include <mach/dma-mx1-mx2.h>
#include <mach/hardware.h>
46
#include <mach/irqs.h>
47
#include <linux/platform_data/camera-mx1.h>
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

/*
 * CSI registers
 */
#define CSICR1		0x00			/* CSI Control Register 1 */
#define CSISR		0x08			/* CSI Status Register */
#define CSIRXR		0x10			/* CSI RxFIFO Register */

#define CSICR1_RXFF_LEVEL(x)	(((x) & 0x3) << 19)
#define CSICR1_SOF_POL		(1 << 17)
#define CSICR1_SOF_INTEN	(1 << 16)
#define CSICR1_MCLKDIV(x)	(((x) & 0xf) << 12)
#define CSICR1_MCLKEN		(1 << 9)
#define CSICR1_FCC		(1 << 8)
#define CSICR1_BIG_ENDIAN	(1 << 7)
#define CSICR1_CLR_RXFIFO	(1 << 5)
#define CSICR1_GCLK_MODE	(1 << 4)
#define CSICR1_DATA_POL		(1 << 2)
#define CSICR1_REDGE		(1 << 1)
#define CSICR1_EN		(1 << 0)

#define CSISR_SFF_OR_INT	(1 << 25)
#define CSISR_RFF_OR_INT	(1 << 24)
#define CSISR_STATFF_INT	(1 << 21)
#define CSISR_RXFF_INT		(1 << 18)
#define CSISR_SOF_INT		(1 << 16)
#define CSISR_DRDY		(1 << 0)

76
#define DRIVER_VERSION "0.0.2"
77 78 79 80 81
#define DRIVER_NAME "mx1-camera"

#define CSI_IRQ_MASK	(CSISR_SFF_OR_INT | CSISR_RFF_OR_INT | \
			CSISR_STATFF_INT | CSISR_RXFF_INT | CSISR_SOF_INT)

82 83 84 85
#define CSI_BUS_FLAGS	(V4L2_MBUS_MASTER | V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
			V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW | \
			V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING | \
			V4L2_MBUS_DATA_ACTIVE_HIGH | V4L2_MBUS_DATA_ACTIVE_LOW)
86 87 88 89 90 91 92 93 94 95

#define MAX_VIDEO_MEM 16	/* Video memory limit in megabytes */

/*
 * Structures
 */

/* buffer for one video frame */
struct mx1_buffer {
	/* common v4l buffer stuff -- must be first */
96 97 98
	struct videobuf_buffer		vb;
	enum v4l2_mbus_pixelcode	code;
	int				inwork;
99 100
};

101 102
/*
 * i.MX1/i.MXL is only supposed to handle one camera on its Camera Sensor
103
 * Interface. If anyone ever builds hardware to enable more than
104 105
 * one camera, they will have to modify this driver too
 */
106
struct mx1_camera_dev {
107
	struct soc_camera_host		soc_host;
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	struct soc_camera_device	*icd;
	struct mx1_camera_pdata		*pdata;
	struct mx1_buffer		*active;
	struct resource			*res;
	struct clk			*clk;
	struct list_head		capture;

	void __iomem			*base;
	int				dma_chan;
	unsigned int			irq;
	unsigned long			mclk;

	spinlock_t			lock;
};

/*
 *  Videobuf operations
 */
static int mx1_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
			      unsigned int *size)
{
	struct soc_camera_device *icd = vq->priv_data;

131
	*size = icd->sizeimage;
132 133 134 135

	if (!*count)
		*count = 32;

136 137
	if (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
		*count = (MAX_VIDEO_MEM * 1024 * 1024) / *size;
138

139
	dev_dbg(icd->parent, "count=%d, size=%d\n", *count, *size);
140 141 142 143 144 145 146 147 148 149 150

	return 0;
}

static void free_buffer(struct videobuf_queue *vq, struct mx1_buffer *buf)
{
	struct soc_camera_device *icd = vq->priv_data;
	struct videobuf_buffer *vb = &buf->vb;

	BUG_ON(in_interrupt());

151
	dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
152 153
		vb, vb->baddr, vb->bsize);

154 155 156 157
	/*
	 * This waits until this buffer is out of danger, i.e., until it is no
	 * longer in STATE_QUEUED or STATE_ACTIVE
	 */
158
	videobuf_waiton(vq, vb, 0, 0);
159 160 161 162 163 164 165 166 167 168 169 170
	videobuf_dma_contig_free(vq, vb);

	vb->state = VIDEOBUF_NEEDS_INIT;
}

static int mx1_videobuf_prepare(struct videobuf_queue *vq,
		struct videobuf_buffer *vb, enum v4l2_field field)
{
	struct soc_camera_device *icd = vq->priv_data;
	struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
	int ret;

171
	dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
172 173 174 175 176 177 178
		vb, vb->baddr, vb->bsize);

	/* Added list head initialization on alloc */
	WARN_ON(!list_empty(&vb->queue));

	BUG_ON(NULL == icd->current_fmt);

179 180 181 182
	/*
	 * I think, in buf_prepare you only have to protect global data,
	 * the actual buffer is yours
	 */
183 184
	buf->inwork = 1;

185
	if (buf->code	!= icd->current_fmt->code ||
186 187
	    vb->width	!= icd->user_width ||
	    vb->height	!= icd->user_height ||
188
	    vb->field	!= field) {
189
		buf->code	= icd->current_fmt->code;
190 191
		vb->width	= icd->user_width;
		vb->height	= icd->user_height;
192 193 194 195
		vb->field	= field;
		vb->state	= VIDEOBUF_NEEDS_INIT;
	}

196
	vb->size = icd->sizeimage;
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	if (0 != vb->baddr && vb->bsize < vb->size) {
		ret = -EINVAL;
		goto out;
	}

	if (vb->state == VIDEOBUF_NEEDS_INIT) {
		ret = videobuf_iolock(vq, vb, NULL);
		if (ret)
			goto fail;

		vb->state = VIDEOBUF_PREPARED;
	}

	buf->inwork = 0;

	return 0;

fail:
	free_buffer(vq, buf);
out:
	buf->inwork = 0;
	return ret;
}

static int mx1_camera_setup_dma(struct mx1_camera_dev *pcdev)
{
	struct videobuf_buffer *vbuf = &pcdev->active->vb;
224
	struct device *dev = pcdev->icd->parent;
225 226 227
	int ret;

	if (unlikely(!pcdev->active)) {
228
		dev_err(dev, "DMA End IRQ with no active buffer\n");
229 230 231 232 233 234 235 236 237
		return -EFAULT;
	}

	/* setup sg list for future DMA */
	ret = imx_dma_setup_single(pcdev->dma_chan,
		videobuf_to_dma_contig(vbuf),
		vbuf->size, pcdev->res->start +
		CSIRXR, DMA_MODE_READ);
	if (unlikely(ret))
238
		dev_err(dev, "Failed to setup DMA sg list\n");
239 240 241 242

	return ret;
}

243
/* Called under spinlock_irqsave(&pcdev->lock, ...) */
244 245 246 247
static void mx1_videobuf_queue(struct videobuf_queue *vq,
						struct videobuf_buffer *vb)
{
	struct soc_camera_device *icd = vq->priv_data;
248
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
249 250 251
	struct mx1_camera_dev *pcdev = ici->priv;
	struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);

252
	dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
		vb, vb->baddr, vb->bsize);

	list_add_tail(&vb->queue, &pcdev->capture);

	vb->state = VIDEOBUF_ACTIVE;

	if (!pcdev->active) {
		pcdev->active = buf;

		/* setup sg list for future DMA */
		if (!mx1_camera_setup_dma(pcdev)) {
			unsigned int temp;
			/* enable SOF irq */
			temp = __raw_readl(pcdev->base + CSICR1) |
							CSICR1_SOF_INTEN;
			__raw_writel(temp, pcdev->base + CSICR1);
		}
	}
}

static void mx1_videobuf_release(struct videobuf_queue *vq,
				 struct videobuf_buffer *vb)
{
	struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
#ifdef DEBUG
	struct soc_camera_device *icd = vq->priv_data;
279
	struct device *dev = icd->parent;
280

281
	dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
282 283 284 285
		vb, vb->baddr, vb->bsize);

	switch (vb->state) {
	case VIDEOBUF_ACTIVE:
286
		dev_dbg(dev, "%s (active)\n", __func__);
287 288
		break;
	case VIDEOBUF_QUEUED:
289
		dev_dbg(dev, "%s (queued)\n", __func__);
290 291
		break;
	case VIDEOBUF_PREPARED:
292
		dev_dbg(dev, "%s (prepared)\n", __func__);
293 294
		break;
	default:
295
		dev_dbg(dev, "%s (unknown)\n", __func__);
296 297 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
		break;
	}
#endif

	free_buffer(vq, buf);
}

static void mx1_camera_wakeup(struct mx1_camera_dev *pcdev,
			      struct videobuf_buffer *vb,
			      struct mx1_buffer *buf)
{
	/* _init is used to debug races, see comment in mx1_camera_reqbufs() */
	list_del_init(&vb->queue);
	vb->state = VIDEOBUF_DONE;
	do_gettimeofday(&vb->ts);
	vb->field_count++;
	wake_up(&vb->done);

	if (list_empty(&pcdev->capture)) {
		pcdev->active = NULL;
		return;
	}

	pcdev->active = list_entry(pcdev->capture.next,
				   struct mx1_buffer, vb.queue);

	/* setup sg list for future DMA */
	if (likely(!mx1_camera_setup_dma(pcdev))) {
		unsigned int temp;

		/* enable SOF irq */
		temp = __raw_readl(pcdev->base + CSICR1) | CSICR1_SOF_INTEN;
		__raw_writel(temp, pcdev->base + CSICR1);
	}
}

static void mx1_camera_dma_irq(int channel, void *data)
{
	struct mx1_camera_dev *pcdev = data;
335
	struct device *dev = pcdev->icd->parent;
336 337 338 339 340 341 342 343 344
	struct mx1_buffer *buf;
	struct videobuf_buffer *vb;
	unsigned long flags;

	spin_lock_irqsave(&pcdev->lock, flags);

	imx_dma_disable(channel);

	if (unlikely(!pcdev->active)) {
345
		dev_err(dev, "DMA End IRQ with no active buffer\n");
346 347 348 349 350 351
		goto out;
	}

	vb = &pcdev->active->vb;
	buf = container_of(vb, struct mx1_buffer, vb);
	WARN_ON(buf->inwork || list_empty(&vb->queue));
352
	dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
		vb, vb->baddr, vb->bsize);

	mx1_camera_wakeup(pcdev, vb, buf);
out:
	spin_unlock_irqrestore(&pcdev->lock, flags);
}

static struct videobuf_queue_ops mx1_videobuf_ops = {
	.buf_setup	= mx1_videobuf_setup,
	.buf_prepare	= mx1_videobuf_prepare,
	.buf_queue	= mx1_videobuf_queue,
	.buf_release	= mx1_videobuf_release,
};

static void mx1_camera_init_videobuf(struct videobuf_queue *q,
				     struct soc_camera_device *icd)
{
370
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
371 372
	struct mx1_camera_dev *pcdev = ici->priv;

373
	videobuf_queue_dma_contig_init(q, &mx1_videobuf_ops, icd->parent,
374 375 376
				&pcdev->lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
				V4L2_FIELD_NONE,
				sizeof(struct mx1_buffer), icd, &icd->video_lock);
377 378 379 380 381 382 383 384 385 386
}

static int mclk_get_divisor(struct mx1_camera_dev *pcdev)
{
	unsigned int mclk = pcdev->mclk;
	unsigned long div;
	unsigned long lcdclk;

	lcdclk = clk_get_rate(pcdev->clk);

387 388 389 390
	/*
	 * We verify platform_mclk_10khz != 0, so if anyone breaks it, here
	 * they get a nice Oops
	 */
391 392
	div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;

393
	dev_dbg(pcdev->icd->parent,
394 395
		"System clock %lukHz, target freq %dkHz, divisor %lu\n",
		lcdclk / 1000, mclk / 1000, div);
396 397 398 399 400 401 402 403

	return div;
}

static void mx1_camera_activate(struct mx1_camera_dev *pcdev)
{
	unsigned int csicr1 = CSICR1_EN;

404
	dev_dbg(pcdev->icd->parent, "Activate device\n");
405

406
	clk_prepare_enable(pcdev->clk);
407 408 409 410 411 412 413 414 415 416 417 418 419

	/* enable CSI before doing anything else */
	__raw_writel(csicr1, pcdev->base + CSICR1);

	csicr1 |= CSICR1_MCLKEN | CSICR1_FCC | CSICR1_GCLK_MODE;
	csicr1 |= CSICR1_MCLKDIV(mclk_get_divisor(pcdev));
	csicr1 |= CSICR1_RXFF_LEVEL(2); /* 16 words */

	__raw_writel(csicr1, pcdev->base + CSICR1);
}

static void mx1_camera_deactivate(struct mx1_camera_dev *pcdev)
{
420
	dev_dbg(pcdev->icd->parent, "Deactivate device\n");
421 422 423 424

	/* Disable all CSI interface */
	__raw_writel(0x00, pcdev->base + CSICR1);

425
	clk_disable_unprepare(pcdev->clk);
426 427
}

428 429 430 431
/*
 * The following two functions absolutely depend on the fact, that
 * there can be only one camera on i.MX1/i.MXL camera sensor interface
 */
432 433
static int mx1_camera_add_device(struct soc_camera_device *icd)
{
434
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
435 436
	struct mx1_camera_dev *pcdev = ici->priv;

437 438
	if (pcdev->icd)
		return -EBUSY;
439

440
	dev_info(icd->parent, "MX1 Camera driver attached to camera %d\n",
441 442 443 444
		 icd->devnum);

	mx1_camera_activate(pcdev);

445
	pcdev->icd = icd;
446

447
	return 0;
448 449 450 451
}

static void mx1_camera_remove_device(struct soc_camera_device *icd)
{
452
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
453 454 455 456 457 458 459 460 461 462 463 464
	struct mx1_camera_dev *pcdev = ici->priv;
	unsigned int csicr1;

	BUG_ON(icd != pcdev->icd);

	/* disable interrupts */
	csicr1 = __raw_readl(pcdev->base + CSICR1) & ~CSI_IRQ_MASK;
	__raw_writel(csicr1, pcdev->base + CSICR1);

	/* Stop DMA engine */
	imx_dma_disable(pcdev->dma_chan);

465
	dev_info(icd->parent, "MX1 Camera driver detached from camera %d\n",
466 467 468 469 470 471 472
		 icd->devnum);

	mx1_camera_deactivate(pcdev);

	pcdev->icd = NULL;
}

473
static int mx1_camera_set_bus_param(struct soc_camera_device *icd)
474
{
475
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
476
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
477
	struct mx1_camera_dev *pcdev = ici->priv;
478 479
	struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
	unsigned long common_flags;
480 481 482 483
	unsigned int csicr1;
	int ret;

	/* MX1 supports only 8bit buswidth */
484 485 486 487 488 489 490 491 492 493 494 495 496 497
	ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
	if (!ret) {
		common_flags = soc_mbus_config_compatible(&cfg, CSI_BUS_FLAGS);
		if (!common_flags) {
			dev_warn(icd->parent,
				 "Flags incompatible: camera 0x%x, host 0x%x\n",
				 cfg.flags, CSI_BUS_FLAGS);
			return -EINVAL;
		}
	} else if (ret != -ENOIOCTLCMD) {
		return ret;
	} else {
		common_flags = CSI_BUS_FLAGS;
	}
498 499

	/* Make choises, based on platform choice */
500 501
	if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
		(common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
502 503
			if (!pcdev->pdata ||
			     pcdev->pdata->flags & MX1_CAMERA_VSYNC_HIGH)
504
				common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
505
			else
506
				common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
507 508
	}

509 510
	if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
		(common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
511 512
			if (!pcdev->pdata ||
			     pcdev->pdata->flags & MX1_CAMERA_PCLK_RISING)
513
				common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
514
			else
515
				common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
516 517
	}

518 519
	if ((common_flags & V4L2_MBUS_DATA_ACTIVE_HIGH) &&
		(common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)) {
520 521
			if (!pcdev->pdata ||
			     pcdev->pdata->flags & MX1_CAMERA_DATA_HIGH)
522
				common_flags &= ~V4L2_MBUS_DATA_ACTIVE_LOW;
523
			else
524
				common_flags &= ~V4L2_MBUS_DATA_ACTIVE_HIGH;
525 526
	}

527 528 529 530 531
	cfg.flags = common_flags;
	ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
	if (ret < 0 && ret != -ENOIOCTLCMD) {
		dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n",
			common_flags, ret);
532
		return ret;
533
	}
534 535 536

	csicr1 = __raw_readl(pcdev->base + CSICR1);

537
	if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
538
		csicr1 |= CSICR1_REDGE;
539
	if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
540
		csicr1 |= CSICR1_SOF_POL;
541
	if (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)
542 543 544 545 546 547 548 549 550 551
		csicr1 |= CSICR1_DATA_POL;

	__raw_writel(csicr1, pcdev->base + CSICR1);

	return 0;
}

static int mx1_camera_set_fmt(struct soc_camera_device *icd,
			      struct v4l2_format *f)
{
552
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
553 554
	const struct soc_camera_format_xlate *xlate;
	struct v4l2_pix_format *pix = &f->fmt.pix;
555 556
	struct v4l2_mbus_framefmt mf;
	int ret, buswidth;
557 558 559

	xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
	if (!xlate) {
560
		dev_warn(icd->parent, "Format %x not found\n",
561
			 pix->pixelformat);
562 563 564
		return -EINVAL;
	}

565 566
	buswidth = xlate->host_fmt->bits_per_sample;
	if (buswidth > 8) {
567
		dev_warn(icd->parent,
568 569 570
			 "bits-per-sample %d for format %x unsupported\n",
			 buswidth, pix->pixelformat);
		return -EINVAL;
571 572
	}

573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
	mf.width	= pix->width;
	mf.height	= pix->height;
	mf.field	= pix->field;
	mf.colorspace	= pix->colorspace;
	mf.code		= xlate->code;

	ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
	if (ret < 0)
		return ret;

	if (mf.code != xlate->code)
		return -EINVAL;

	pix->width		= mf.width;
	pix->height		= mf.height;
	pix->field		= mf.field;
	pix->colorspace		= mf.colorspace;
	icd->current_fmt	= xlate;

592 593 594 595 596 597
	return ret;
}

static int mx1_camera_try_fmt(struct soc_camera_device *icd,
			      struct v4l2_format *f)
{
598
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
599 600 601 602
	const struct soc_camera_format_xlate *xlate;
	struct v4l2_pix_format *pix = &f->fmt.pix;
	struct v4l2_mbus_framefmt mf;
	int ret;
603 604
	/* TODO: limit to mx1 hardware capabilities */

605 606
	xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
	if (!xlate) {
607
		dev_warn(icd->parent, "Format %x not found\n",
608 609 610 611 612 613 614 615 616 617
			 pix->pixelformat);
		return -EINVAL;
	}

	mf.width	= pix->width;
	mf.height	= pix->height;
	mf.field	= pix->field;
	mf.colorspace	= pix->colorspace;
	mf.code		= xlate->code;

618
	/* limit to sensor capabilities */
619 620 621 622 623 624 625 626 627 628
	ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
	if (ret < 0)
		return ret;

	pix->width	= mf.width;
	pix->height	= mf.height;
	pix->field	= mf.field;
	pix->colorspace	= mf.colorspace;

	return 0;
629 630
}

631
static int mx1_camera_reqbufs(struct soc_camera_device *icd,
632 633 634 635
			      struct v4l2_requestbuffers *p)
{
	int i;

636 637
	/*
	 * This is for locking debugging only. I removed spinlocks and now I
638 639
	 * check whether .prepare is ever called on a linked buffer, or whether
	 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
640 641
	 * it hadn't triggered
	 */
642
	for (i = 0; i < p->count; i++) {
643
		struct mx1_buffer *buf = container_of(icd->vb_vidq.bufs[i],
644 645 646 647 648 649 650 651 652 653
						      struct mx1_buffer, vb);
		buf->inwork = 0;
		INIT_LIST_HEAD(&buf->vb.queue);
	}

	return 0;
}

static unsigned int mx1_camera_poll(struct file *file, poll_table *pt)
{
654
	struct soc_camera_device *icd = file->private_data;
655 656
	struct mx1_buffer *buf;

657
	buf = list_entry(icd->vb_vidq.stream.next, struct mx1_buffer,
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
			 vb.stream);

	poll_wait(file, &buf->vb.done, pt);

	if (buf->vb.state == VIDEOBUF_DONE ||
	    buf->vb.state == VIDEOBUF_ERROR)
		return POLLIN | POLLRDNORM;

	return 0;
}

static int mx1_camera_querycap(struct soc_camera_host *ici,
			       struct v4l2_capability *cap)
{
	/* cap->name is set by the friendly caller:-> */
	strlcpy(cap->card, "i.MX1/i.MXL Camera", sizeof(cap->card));
	cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;

	return 0;
}

static struct soc_camera_host_ops mx1_soc_camera_host_ops = {
	.owner		= THIS_MODULE,
	.add		= mx1_camera_add_device,
	.remove		= mx1_camera_remove_device,
	.set_bus_param	= mx1_camera_set_bus_param,
	.set_fmt	= mx1_camera_set_fmt,
	.try_fmt	= mx1_camera_try_fmt,
	.init_videobuf	= mx1_camera_init_videobuf,
	.reqbufs	= mx1_camera_reqbufs,
	.poll		= mx1_camera_poll,
	.querycap	= mx1_camera_querycap,
};

static struct fiq_handler fh = {
	.name		= "csi_sof"
};

static int __init mx1_camera_probe(struct platform_device *pdev)
{
	struct mx1_camera_dev *pcdev;
	struct resource *res;
	struct pt_regs regs;
	struct clk *clk;
	void __iomem *base;
	unsigned int irq;
	int err = 0;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	irq = platform_get_irq(pdev, 0);
708
	if (!res || (int)irq <= 0) {
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 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
		err = -ENODEV;
		goto exit;
	}

	clk = clk_get(&pdev->dev, "csi_clk");
	if (IS_ERR(clk)) {
		err = PTR_ERR(clk);
		goto exit;
	}

	pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
	if (!pcdev) {
		dev_err(&pdev->dev, "Could not allocate pcdev\n");
		err = -ENOMEM;
		goto exit_put_clk;
	}

	pcdev->res = res;
	pcdev->clk = clk;

	pcdev->pdata = pdev->dev.platform_data;

	if (pcdev->pdata)
		pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;

	if (!pcdev->mclk) {
		dev_warn(&pdev->dev,
			 "mclk_10khz == 0! Please, fix your platform data. "
			 "Using default 20MHz\n");
		pcdev->mclk = 20000000;
	}

	INIT_LIST_HEAD(&pcdev->capture);
	spin_lock_init(&pcdev->lock);

	/*
	 * Request the regions.
	 */
	if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME)) {
		err = -EBUSY;
		goto exit_kfree;
	}

	base = ioremap(res->start, resource_size(res));
	if (!base) {
		err = -ENOMEM;
		goto exit_release;
	}
	pcdev->irq = irq;
	pcdev->base = base;

	/* request dma */
	pcdev->dma_chan = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_HIGH);
	if (pcdev->dma_chan < 0) {
763
		dev_err(&pdev->dev, "Can't request DMA for MX1 CSI\n");
764 765 766
		err = -EBUSY;
		goto exit_iounmap;
	}
767
	dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chan);
768 769 770 771 772

	imx_dma_setup_handlers(pcdev->dma_chan, mx1_camera_dma_irq, NULL,
			       pcdev);

	imx_dma_config_channel(pcdev->dma_chan, IMX_DMA_TYPE_FIFO,
773
			       IMX_DMA_MEMSIZE_32, MX1_DMA_REQ_CSI_R, 0);
774 775 776 777 778 779
	/* burst length : 16 words = 64 bytes */
	imx_dma_config_burstlen(pcdev->dma_chan, 0);

	/* request irq */
	err = claim_fiq(&fh);
	if (err) {
780
		dev_err(&pdev->dev, "Camera interrupt register failed \n");
781 782 783 784 785 786
		goto exit_free_dma;
	}

	set_fiq_handler(&mx1_camera_sof_fiq_start, &mx1_camera_sof_fiq_end -
						   &mx1_camera_sof_fiq_start);

787 788
	regs.ARM_r8 = (long)MX1_DMA_DIMR;
	regs.ARM_r9 = (long)MX1_DMA_CCR(pcdev->dma_chan);
789 790 791 792 793 794 795 796
	regs.ARM_r10 = (long)pcdev->base + CSICR1;
	regs.ARM_fp = (long)pcdev->base + CSISR;
	regs.ARM_sp = 1 << pcdev->dma_chan;
	set_fiq_regs(&regs);

	mxc_set_irq_fiq(irq, 1);
	enable_fiq(irq);

797 798 799
	pcdev->soc_host.drv_name	= DRIVER_NAME;
	pcdev->soc_host.ops		= &mx1_soc_camera_host_ops;
	pcdev->soc_host.priv		= pcdev;
800
	pcdev->soc_host.v4l2_dev.dev	= &pdev->dev;
801 802
	pcdev->soc_host.nr		= pdev->id;
	err = soc_camera_host_register(&pcdev->soc_host);
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
	if (err)
		goto exit_free_irq;

	dev_info(&pdev->dev, "MX1 Camera driver loaded\n");

	return 0;

exit_free_irq:
	disable_fiq(irq);
	mxc_set_irq_fiq(irq, 0);
	release_fiq(&fh);
exit_free_dma:
	imx_dma_free(pcdev->dma_chan);
exit_iounmap:
	iounmap(base);
exit_release:
	release_mem_region(res->start, resource_size(res));
exit_kfree:
	kfree(pcdev);
exit_put_clk:
	clk_put(clk);
exit:
	return err;
}

static int __exit mx1_camera_remove(struct platform_device *pdev)
{
830 831 832
	struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
	struct mx1_camera_dev *pcdev = container_of(soc_host,
					struct mx1_camera_dev, soc_host);
833 834 835 836 837 838 839 840 841
	struct resource *res;

	imx_dma_free(pcdev->dma_chan);
	disable_fiq(pcdev->irq);
	mxc_set_irq_fiq(pcdev->irq, 0);
	release_fiq(&fh);

	clk_put(pcdev->clk);

842
	soc_camera_host_unregister(soc_host);
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 872 873 874 875 876 877 878

	iounmap(pcdev->base);

	res = pcdev->res;
	release_mem_region(res->start, resource_size(res));

	kfree(pcdev);

	dev_info(&pdev->dev, "MX1 Camera driver unloaded\n");

	return 0;
}

static struct platform_driver mx1_camera_driver = {
	.driver 	= {
		.name	= DRIVER_NAME,
	},
	.remove		= __exit_p(mx1_camera_remove),
};

static int __init mx1_camera_init(void)
{
	return platform_driver_probe(&mx1_camera_driver, mx1_camera_probe);
}

static void __exit mx1_camera_exit(void)
{
	return platform_driver_unregister(&mx1_camera_driver);
}

module_init(mx1_camera_init);
module_exit(mx1_camera_exit);

MODULE_DESCRIPTION("i.MX1/i.MXL SoC Camera Host driver");
MODULE_AUTHOR("Paulius Zaleckas <paulius.zaleckas@teltonika.lt>");
MODULE_LICENSE("GPL v2");
879
MODULE_VERSION(DRIVER_VERSION);
880
MODULE_ALIAS("platform:" DRIVER_NAME);