Linux Audio

Check our new training course

Loading...
  1/*
  2 * V4L2 Driver for i.MXL/i.MXL camera (CSI) host
  3 *
  4 * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
  5 * Copyright (C) 2009, Darius Augulis <augulis.darius@gmail.com>
  6 *
  7 * Based on PXA SoC camera driver
  8 * Copyright (C) 2006, Sascha Hauer, Pengutronix
  9 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
 10 *
 11 * This program is free software; you can redistribute it and/or modify
 12 * it under the terms of the GNU General Public License version 2 as
 13 * published by the Free Software Foundation.
 14 */
 15
 16#include <linux/clk.h>
 17#include <linux/delay.h>
 18#include <linux/device.h>
 19#include <linux/dma-mapping.h>
 20#include <linux/errno.h>
 21#include <linux/fs.h>
 22#include <linux/init.h>
 23#include <linux/interrupt.h>
 24#include <linux/io.h>
 25#include <linux/kernel.h>
 26#include <linux/mm.h>
 27#include <linux/module.h>
 28#include <linux/moduleparam.h>
 29#include <linux/mutex.h>
 30#include <linux/platform_device.h>
 31#include <linux/sched.h>
 32#include <linux/slab.h>
 33#include <linux/time.h>
 34#include <linux/videodev2.h>
 35
 36#include <media/soc_camera.h>
 37#include <media/v4l2-common.h>
 38#include <media/v4l2-dev.h>
 39#include <media/videobuf-dma-contig.h>
 40#include <media/soc_mediabus.h>
 41
 42#include <asm/dma.h>
 43#include <asm/fiq.h>
 44#include <mach/dma-mx1-mx2.h>
 45#include <mach/hardware.h>
 46#include <mach/mx1_camera.h>
 47
 48/*
 49 * CSI registers
 50 */
 51#define CSICR1		0x00			/* CSI Control Register 1 */
 52#define CSISR		0x08			/* CSI Status Register */
 53#define CSIRXR		0x10			/* CSI RxFIFO Register */
 54
 55#define CSICR1_RXFF_LEVEL(x)	(((x) & 0x3) << 19)
 56#define CSICR1_SOF_POL		(1 << 17)
 57#define CSICR1_SOF_INTEN	(1 << 16)
 58#define CSICR1_MCLKDIV(x)	(((x) & 0xf) << 12)
 59#define CSICR1_MCLKEN		(1 << 9)
 60#define CSICR1_FCC		(1 << 8)
 61#define CSICR1_BIG_ENDIAN	(1 << 7)
 62#define CSICR1_CLR_RXFIFO	(1 << 5)
 63#define CSICR1_GCLK_MODE	(1 << 4)
 64#define CSICR1_DATA_POL		(1 << 2)
 65#define CSICR1_REDGE		(1 << 1)
 66#define CSICR1_EN		(1 << 0)
 67
 68#define CSISR_SFF_OR_INT	(1 << 25)
 69#define CSISR_RFF_OR_INT	(1 << 24)
 70#define CSISR_STATFF_INT	(1 << 21)
 71#define CSISR_RXFF_INT		(1 << 18)
 72#define CSISR_SOF_INT		(1 << 16)
 73#define CSISR_DRDY		(1 << 0)
 74
 75#define DRIVER_VERSION "0.0.2"
 76#define DRIVER_NAME "mx1-camera"
 77
 78#define CSI_IRQ_MASK	(CSISR_SFF_OR_INT | CSISR_RFF_OR_INT | \
 79			CSISR_STATFF_INT | CSISR_RXFF_INT | CSISR_SOF_INT)
 80
 81#define CSI_BUS_FLAGS	(V4L2_MBUS_MASTER | V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
 82			V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW | \
 83			V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING | \
 84			V4L2_MBUS_DATA_ACTIVE_HIGH | V4L2_MBUS_DATA_ACTIVE_LOW)
 85
 86#define MAX_VIDEO_MEM 16	/* Video memory limit in megabytes */
 87
 88/*
 89 * Structures
 90 */
 91
 92/* buffer for one video frame */
 93struct mx1_buffer {
 94	/* common v4l buffer stuff -- must be first */
 95	struct videobuf_buffer		vb;
 96	enum v4l2_mbus_pixelcode	code;
 97	int				inwork;
 98};
 99
100/*
101 * i.MX1/i.MXL is only supposed to handle one camera on its Camera Sensor
102 * Interface. If anyone ever builds hardware to enable more than
103 * one camera, they will have to modify this driver too
104 */
105struct mx1_camera_dev {
106	struct soc_camera_host		soc_host;
107	struct soc_camera_device	*icd;
108	struct mx1_camera_pdata		*pdata;
109	struct mx1_buffer		*active;
110	struct resource			*res;
111	struct clk			*clk;
112	struct list_head		capture;
113
114	void __iomem			*base;
115	int				dma_chan;
116	unsigned int			irq;
117	unsigned long			mclk;
118
119	spinlock_t			lock;
120};
121
122/*
123 *  Videobuf operations
124 */
125static int mx1_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
126			      unsigned int *size)
127{
128	struct soc_camera_device *icd = vq->priv_data;
129
130	*size = icd->sizeimage;
131
132	if (!*count)
133		*count = 32;
134
135	if (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
136		*count = (MAX_VIDEO_MEM * 1024 * 1024) / *size;
137
138	dev_dbg(icd->parent, "count=%d, size=%d\n", *count, *size);
139
140	return 0;
141}
142
143static void free_buffer(struct videobuf_queue *vq, struct mx1_buffer *buf)
144{
145	struct soc_camera_device *icd = vq->priv_data;
146	struct videobuf_buffer *vb = &buf->vb;
147
148	BUG_ON(in_interrupt());
149
150	dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
151		vb, vb->baddr, vb->bsize);
152
153	/*
154	 * This waits until this buffer is out of danger, i.e., until it is no
155	 * longer in STATE_QUEUED or STATE_ACTIVE
156	 */
157	videobuf_waiton(vq, vb, 0, 0);
158	videobuf_dma_contig_free(vq, vb);
159
160	vb->state = VIDEOBUF_NEEDS_INIT;
161}
162
163static int mx1_videobuf_prepare(struct videobuf_queue *vq,
164		struct videobuf_buffer *vb, enum v4l2_field field)
165{
166	struct soc_camera_device *icd = vq->priv_data;
167	struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
168	int ret;
169
170	dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
171		vb, vb->baddr, vb->bsize);
172
173	/* Added list head initialization on alloc */
174	WARN_ON(!list_empty(&vb->queue));
175
176	BUG_ON(NULL == icd->current_fmt);
177
178	/*
179	 * I think, in buf_prepare you only have to protect global data,
180	 * the actual buffer is yours
181	 */
182	buf->inwork = 1;
183
184	if (buf->code	!= icd->current_fmt->code ||
185	    vb->width	!= icd->user_width ||
186	    vb->height	!= icd->user_height ||
187	    vb->field	!= field) {
188		buf->code	= icd->current_fmt->code;
189		vb->width	= icd->user_width;
190		vb->height	= icd->user_height;
191		vb->field	= field;
192		vb->state	= VIDEOBUF_NEEDS_INIT;
193	}
194
195	vb->size = icd->sizeimage;
196	if (0 != vb->baddr && vb->bsize < vb->size) {
197		ret = -EINVAL;
198		goto out;
199	}
200
201	if (vb->state == VIDEOBUF_NEEDS_INIT) {
202		ret = videobuf_iolock(vq, vb, NULL);
203		if (ret)
204			goto fail;
205
206		vb->state = VIDEOBUF_PREPARED;
207	}
208
209	buf->inwork = 0;
210
211	return 0;
212
213fail:
214	free_buffer(vq, buf);
215out:
216	buf->inwork = 0;
217	return ret;
218}
219
220static int mx1_camera_setup_dma(struct mx1_camera_dev *pcdev)
221{
222	struct videobuf_buffer *vbuf = &pcdev->active->vb;
223	struct device *dev = pcdev->icd->parent;
224	int ret;
225
226	if (unlikely(!pcdev->active)) {
227		dev_err(dev, "DMA End IRQ with no active buffer\n");
228		return -EFAULT;
229	}
230
231	/* setup sg list for future DMA */
232	ret = imx_dma_setup_single(pcdev->dma_chan,
233		videobuf_to_dma_contig(vbuf),
234		vbuf->size, pcdev->res->start +
235		CSIRXR, DMA_MODE_READ);
236	if (unlikely(ret))
237		dev_err(dev, "Failed to setup DMA sg list\n");
238
239	return ret;
240}
241
242/* Called under spinlock_irqsave(&pcdev->lock, ...) */
243static void mx1_videobuf_queue(struct videobuf_queue *vq,
244						struct videobuf_buffer *vb)
245{
246	struct soc_camera_device *icd = vq->priv_data;
247	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
248	struct mx1_camera_dev *pcdev = ici->priv;
249	struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
250
251	dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
252		vb, vb->baddr, vb->bsize);
253
254	list_add_tail(&vb->queue, &pcdev->capture);
255
256	vb->state = VIDEOBUF_ACTIVE;
257
258	if (!pcdev->active) {
259		pcdev->active = buf;
260
261		/* setup sg list for future DMA */
262		if (!mx1_camera_setup_dma(pcdev)) {
263			unsigned int temp;
264			/* enable SOF irq */
265			temp = __raw_readl(pcdev->base + CSICR1) |
266							CSICR1_SOF_INTEN;
267			__raw_writel(temp, pcdev->base + CSICR1);
268		}
269	}
270}
271
272static void mx1_videobuf_release(struct videobuf_queue *vq,
273				 struct videobuf_buffer *vb)
274{
275	struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
276#ifdef DEBUG
277	struct soc_camera_device *icd = vq->priv_data;
278	struct device *dev = icd->parent;
279
280	dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
281		vb, vb->baddr, vb->bsize);
282
283	switch (vb->state) {
284	case VIDEOBUF_ACTIVE:
285		dev_dbg(dev, "%s (active)\n", __func__);
286		break;
287	case VIDEOBUF_QUEUED:
288		dev_dbg(dev, "%s (queued)\n", __func__);
289		break;
290	case VIDEOBUF_PREPARED:
291		dev_dbg(dev, "%s (prepared)\n", __func__);
292		break;
293	default:
294		dev_dbg(dev, "%s (unknown)\n", __func__);
295		break;
296	}
297#endif
298
299	free_buffer(vq, buf);
300}
301
302static void mx1_camera_wakeup(struct mx1_camera_dev *pcdev,
303			      struct videobuf_buffer *vb,
304			      struct mx1_buffer *buf)
305{
306	/* _init is used to debug races, see comment in mx1_camera_reqbufs() */
307	list_del_init(&vb->queue);
308	vb->state = VIDEOBUF_DONE;
309	do_gettimeofday(&vb->ts);
310	vb->field_count++;
311	wake_up(&vb->done);
312
313	if (list_empty(&pcdev->capture)) {
314		pcdev->active = NULL;
315		return;
316	}
317
318	pcdev->active = list_entry(pcdev->capture.next,
319				   struct mx1_buffer, vb.queue);
320
321	/* setup sg list for future DMA */
322	if (likely(!mx1_camera_setup_dma(pcdev))) {
323		unsigned int temp;
324
325		/* enable SOF irq */
326		temp = __raw_readl(pcdev->base + CSICR1) | CSICR1_SOF_INTEN;
327		__raw_writel(temp, pcdev->base + CSICR1);
328	}
329}
330
331static void mx1_camera_dma_irq(int channel, void *data)
332{
333	struct mx1_camera_dev *pcdev = data;
334	struct device *dev = pcdev->icd->parent;
335	struct mx1_buffer *buf;
336	struct videobuf_buffer *vb;
337	unsigned long flags;
338
339	spin_lock_irqsave(&pcdev->lock, flags);
340
341	imx_dma_disable(channel);
342
343	if (unlikely(!pcdev->active)) {
344		dev_err(dev, "DMA End IRQ with no active buffer\n");
345		goto out;
346	}
347
348	vb = &pcdev->active->vb;
349	buf = container_of(vb, struct mx1_buffer, vb);
350	WARN_ON(buf->inwork || list_empty(&vb->queue));
351	dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
352		vb, vb->baddr, vb->bsize);
353
354	mx1_camera_wakeup(pcdev, vb, buf);
355out:
356	spin_unlock_irqrestore(&pcdev->lock, flags);
357}
358
359static struct videobuf_queue_ops mx1_videobuf_ops = {
360	.buf_setup	= mx1_videobuf_setup,
361	.buf_prepare	= mx1_videobuf_prepare,
362	.buf_queue	= mx1_videobuf_queue,
363	.buf_release	= mx1_videobuf_release,
364};
365
366static void mx1_camera_init_videobuf(struct videobuf_queue *q,
367				     struct soc_camera_device *icd)
368{
369	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
370	struct mx1_camera_dev *pcdev = ici->priv;
371
372	videobuf_queue_dma_contig_init(q, &mx1_videobuf_ops, icd->parent,
373				&pcdev->lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
374				V4L2_FIELD_NONE,
375				sizeof(struct mx1_buffer), icd, &icd->video_lock);
376}
377
378static int mclk_get_divisor(struct mx1_camera_dev *pcdev)
379{
380	unsigned int mclk = pcdev->mclk;
381	unsigned long div;
382	unsigned long lcdclk;
383
384	lcdclk = clk_get_rate(pcdev->clk);
385
386	/*
387	 * We verify platform_mclk_10khz != 0, so if anyone breaks it, here
388	 * they get a nice Oops
389	 */
390	div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
391
392	dev_dbg(pcdev->icd->parent,
393		"System clock %lukHz, target freq %dkHz, divisor %lu\n",
394		lcdclk / 1000, mclk / 1000, div);
395
396	return div;
397}
398
399static void mx1_camera_activate(struct mx1_camera_dev *pcdev)
400{
401	unsigned int csicr1 = CSICR1_EN;
402
403	dev_dbg(pcdev->icd->parent, "Activate device\n");
404
405	clk_enable(pcdev->clk);
406
407	/* enable CSI before doing anything else */
408	__raw_writel(csicr1, pcdev->base + CSICR1);
409
410	csicr1 |= CSICR1_MCLKEN | CSICR1_FCC | CSICR1_GCLK_MODE;
411	csicr1 |= CSICR1_MCLKDIV(mclk_get_divisor(pcdev));
412	csicr1 |= CSICR1_RXFF_LEVEL(2); /* 16 words */
413
414	__raw_writel(csicr1, pcdev->base + CSICR1);
415}
416
417static void mx1_camera_deactivate(struct mx1_camera_dev *pcdev)
418{
419	dev_dbg(pcdev->icd->parent, "Deactivate device\n");
420
421	/* Disable all CSI interface */
422	__raw_writel(0x00, pcdev->base + CSICR1);
423
424	clk_disable(pcdev->clk);
425}
426
427/*
428 * The following two functions absolutely depend on the fact, that
429 * there can be only one camera on i.MX1/i.MXL camera sensor interface
430 */
431static int mx1_camera_add_device(struct soc_camera_device *icd)
432{
433	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
434	struct mx1_camera_dev *pcdev = ici->priv;
435
436	if (pcdev->icd)
437		return -EBUSY;
438
439	dev_info(icd->parent, "MX1 Camera driver attached to camera %d\n",
440		 icd->devnum);
441
442	mx1_camera_activate(pcdev);
443
444	pcdev->icd = icd;
445
446	return 0;
447}
448
449static void mx1_camera_remove_device(struct soc_camera_device *icd)
450{
451	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
452	struct mx1_camera_dev *pcdev = ici->priv;
453	unsigned int csicr1;
454
455	BUG_ON(icd != pcdev->icd);
456
457	/* disable interrupts */
458	csicr1 = __raw_readl(pcdev->base + CSICR1) & ~CSI_IRQ_MASK;
459	__raw_writel(csicr1, pcdev->base + CSICR1);
460
461	/* Stop DMA engine */
462	imx_dma_disable(pcdev->dma_chan);
463
464	dev_info(icd->parent, "MX1 Camera driver detached from camera %d\n",
465		 icd->devnum);
466
467	mx1_camera_deactivate(pcdev);
468
469	pcdev->icd = NULL;
470}
471
472static int mx1_camera_set_crop(struct soc_camera_device *icd,
473			       struct v4l2_crop *a)
474{
475	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
476
477	return v4l2_subdev_call(sd, video, s_crop, a);
478}
479
480static int mx1_camera_set_bus_param(struct soc_camera_device *icd)
481{
482	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
483	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
484	struct mx1_camera_dev *pcdev = ici->priv;
485	struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
486	unsigned long common_flags;
487	unsigned int csicr1;
488	int ret;
489
490	/* MX1 supports only 8bit buswidth */
491	ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
492	if (!ret) {
493		common_flags = soc_mbus_config_compatible(&cfg, CSI_BUS_FLAGS);
494		if (!common_flags) {
495			dev_warn(icd->parent,
496				 "Flags incompatible: camera 0x%x, host 0x%x\n",
497				 cfg.flags, CSI_BUS_FLAGS);
498			return -EINVAL;
499		}
500	} else if (ret != -ENOIOCTLCMD) {
501		return ret;
502	} else {
503		common_flags = CSI_BUS_FLAGS;
504	}
505
506	/* Make choises, based on platform choice */
507	if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
508		(common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
509			if (!pcdev->pdata ||
510			     pcdev->pdata->flags & MX1_CAMERA_VSYNC_HIGH)
511				common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
512			else
513				common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
514	}
515
516	if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
517		(common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
518			if (!pcdev->pdata ||
519			     pcdev->pdata->flags & MX1_CAMERA_PCLK_RISING)
520				common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
521			else
522				common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
523	}
524
525	if ((common_flags & V4L2_MBUS_DATA_ACTIVE_HIGH) &&
526		(common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)) {
527			if (!pcdev->pdata ||
528			     pcdev->pdata->flags & MX1_CAMERA_DATA_HIGH)
529				common_flags &= ~V4L2_MBUS_DATA_ACTIVE_LOW;
530			else
531				common_flags &= ~V4L2_MBUS_DATA_ACTIVE_HIGH;
532	}
533
534	cfg.flags = common_flags;
535	ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
536	if (ret < 0 && ret != -ENOIOCTLCMD) {
537		dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n",
538			common_flags, ret);
539		return ret;
540	}
541
542	csicr1 = __raw_readl(pcdev->base + CSICR1);
543
544	if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
545		csicr1 |= CSICR1_REDGE;
546	if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
547		csicr1 |= CSICR1_SOF_POL;
548	if (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)
549		csicr1 |= CSICR1_DATA_POL;
550
551	__raw_writel(csicr1, pcdev->base + CSICR1);
552
553	return 0;
554}
555
556static int mx1_camera_set_fmt(struct soc_camera_device *icd,
557			      struct v4l2_format *f)
558{
559	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
560	const struct soc_camera_format_xlate *xlate;
561	struct v4l2_pix_format *pix = &f->fmt.pix;
562	struct v4l2_mbus_framefmt mf;
563	int ret, buswidth;
564
565	xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
566	if (!xlate) {
567		dev_warn(icd->parent, "Format %x not found\n",
568			 pix->pixelformat);
569		return -EINVAL;
570	}
571
572	buswidth = xlate->host_fmt->bits_per_sample;
573	if (buswidth > 8) {
574		dev_warn(icd->parent,
575			 "bits-per-sample %d for format %x unsupported\n",
576			 buswidth, pix->pixelformat);
577		return -EINVAL;
578	}
579
580	mf.width	= pix->width;
581	mf.height	= pix->height;
582	mf.field	= pix->field;
583	mf.colorspace	= pix->colorspace;
584	mf.code		= xlate->code;
585
586	ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
587	if (ret < 0)
588		return ret;
589
590	if (mf.code != xlate->code)
591		return -EINVAL;
592
593	pix->width		= mf.width;
594	pix->height		= mf.height;
595	pix->field		= mf.field;
596	pix->colorspace		= mf.colorspace;
597	icd->current_fmt	= xlate;
598
599	return ret;
600}
601
602static int mx1_camera_try_fmt(struct soc_camera_device *icd,
603			      struct v4l2_format *f)
604{
605	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
606	const struct soc_camera_format_xlate *xlate;
607	struct v4l2_pix_format *pix = &f->fmt.pix;
608	struct v4l2_mbus_framefmt mf;
609	int ret;
610	/* TODO: limit to mx1 hardware capabilities */
611
612	xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
613	if (!xlate) {
614		dev_warn(icd->parent, "Format %x not found\n",
615			 pix->pixelformat);
616		return -EINVAL;
617	}
618
619	mf.width	= pix->width;
620	mf.height	= pix->height;
621	mf.field	= pix->field;
622	mf.colorspace	= pix->colorspace;
623	mf.code		= xlate->code;
624
625	/* limit to sensor capabilities */
626	ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
627	if (ret < 0)
628		return ret;
629
630	pix->width	= mf.width;
631	pix->height	= mf.height;
632	pix->field	= mf.field;
633	pix->colorspace	= mf.colorspace;
634
635	return 0;
636}
637
638static int mx1_camera_reqbufs(struct soc_camera_device *icd,
639			      struct v4l2_requestbuffers *p)
640{
641	int i;
642
643	/*
644	 * This is for locking debugging only. I removed spinlocks and now I
645	 * check whether .prepare is ever called on a linked buffer, or whether
646	 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
647	 * it hadn't triggered
648	 */
649	for (i = 0; i < p->count; i++) {
650		struct mx1_buffer *buf = container_of(icd->vb_vidq.bufs[i],
651						      struct mx1_buffer, vb);
652		buf->inwork = 0;
653		INIT_LIST_HEAD(&buf->vb.queue);
654	}
655
656	return 0;
657}
658
659static unsigned int mx1_camera_poll(struct file *file, poll_table *pt)
660{
661	struct soc_camera_device *icd = file->private_data;
662	struct mx1_buffer *buf;
663
664	buf = list_entry(icd->vb_vidq.stream.next, struct mx1_buffer,
665			 vb.stream);
666
667	poll_wait(file, &buf->vb.done, pt);
668
669	if (buf->vb.state == VIDEOBUF_DONE ||
670	    buf->vb.state == VIDEOBUF_ERROR)
671		return POLLIN | POLLRDNORM;
672
673	return 0;
674}
675
676static int mx1_camera_querycap(struct soc_camera_host *ici,
677			       struct v4l2_capability *cap)
678{
679	/* cap->name is set by the friendly caller:-> */
680	strlcpy(cap->card, "i.MX1/i.MXL Camera", sizeof(cap->card));
681	cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
682
683	return 0;
684}
685
686static struct soc_camera_host_ops mx1_soc_camera_host_ops = {
687	.owner		= THIS_MODULE,
688	.add		= mx1_camera_add_device,
689	.remove		= mx1_camera_remove_device,
690	.set_bus_param	= mx1_camera_set_bus_param,
691	.set_crop	= mx1_camera_set_crop,
692	.set_fmt	= mx1_camera_set_fmt,
693	.try_fmt	= mx1_camera_try_fmt,
694	.init_videobuf	= mx1_camera_init_videobuf,
695	.reqbufs	= mx1_camera_reqbufs,
696	.poll		= mx1_camera_poll,
697	.querycap	= mx1_camera_querycap,
698};
699
700static struct fiq_handler fh = {
701	.name		= "csi_sof"
702};
703
704static int __init mx1_camera_probe(struct platform_device *pdev)
705{
706	struct mx1_camera_dev *pcdev;
707	struct resource *res;
708	struct pt_regs regs;
709	struct clk *clk;
710	void __iomem *base;
711	unsigned int irq;
712	int err = 0;
713
714	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
715	irq = platform_get_irq(pdev, 0);
716	if (!res || (int)irq <= 0) {
717		err = -ENODEV;
718		goto exit;
719	}
720
721	clk = clk_get(&pdev->dev, "csi_clk");
722	if (IS_ERR(clk)) {
723		err = PTR_ERR(clk);
724		goto exit;
725	}
726
727	pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
728	if (!pcdev) {
729		dev_err(&pdev->dev, "Could not allocate pcdev\n");
730		err = -ENOMEM;
731		goto exit_put_clk;
732	}
733
734	pcdev->res = res;
735	pcdev->clk = clk;
736
737	pcdev->pdata = pdev->dev.platform_data;
738
739	if (pcdev->pdata)
740		pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
741
742	if (!pcdev->mclk) {
743		dev_warn(&pdev->dev,
744			 "mclk_10khz == 0! Please, fix your platform data. "
745			 "Using default 20MHz\n");
746		pcdev->mclk = 20000000;
747	}
748
749	INIT_LIST_HEAD(&pcdev->capture);
750	spin_lock_init(&pcdev->lock);
751
752	/*
753	 * Request the regions.
754	 */
755	if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME)) {
756		err = -EBUSY;
757		goto exit_kfree;
758	}
759
760	base = ioremap(res->start, resource_size(res));
761	if (!base) {
762		err = -ENOMEM;
763		goto exit_release;
764	}
765	pcdev->irq = irq;
766	pcdev->base = base;
767
768	/* request dma */
769	pcdev->dma_chan = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_HIGH);
770	if (pcdev->dma_chan < 0) {
771		dev_err(&pdev->dev, "Can't request DMA for MX1 CSI\n");
772		err = -EBUSY;
773		goto exit_iounmap;
774	}
775	dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chan);
776
777	imx_dma_setup_handlers(pcdev->dma_chan, mx1_camera_dma_irq, NULL,
778			       pcdev);
779
780	imx_dma_config_channel(pcdev->dma_chan, IMX_DMA_TYPE_FIFO,
781			       IMX_DMA_MEMSIZE_32, MX1_DMA_REQ_CSI_R, 0);
782	/* burst length : 16 words = 64 bytes */
783	imx_dma_config_burstlen(pcdev->dma_chan, 0);
784
785	/* request irq */
786	err = claim_fiq(&fh);
787	if (err) {
788		dev_err(&pdev->dev, "Camera interrupt register failed \n");
789		goto exit_free_dma;
790	}
791
792	set_fiq_handler(&mx1_camera_sof_fiq_start, &mx1_camera_sof_fiq_end -
793						   &mx1_camera_sof_fiq_start);
794
795	regs.ARM_r8 = (long)MX1_DMA_DIMR;
796	regs.ARM_r9 = (long)MX1_DMA_CCR(pcdev->dma_chan);
797	regs.ARM_r10 = (long)pcdev->base + CSICR1;
798	regs.ARM_fp = (long)pcdev->base + CSISR;
799	regs.ARM_sp = 1 << pcdev->dma_chan;
800	set_fiq_regs(&regs);
801
802	mxc_set_irq_fiq(irq, 1);
803	enable_fiq(irq);
804
805	pcdev->soc_host.drv_name	= DRIVER_NAME;
806	pcdev->soc_host.ops		= &mx1_soc_camera_host_ops;
807	pcdev->soc_host.priv		= pcdev;
808	pcdev->soc_host.v4l2_dev.dev	= &pdev->dev;
809	pcdev->soc_host.nr		= pdev->id;
810	err = soc_camera_host_register(&pcdev->soc_host);
811	if (err)
812		goto exit_free_irq;
813
814	dev_info(&pdev->dev, "MX1 Camera driver loaded\n");
815
816	return 0;
817
818exit_free_irq:
819	disable_fiq(irq);
820	mxc_set_irq_fiq(irq, 0);
821	release_fiq(&fh);
822exit_free_dma:
823	imx_dma_free(pcdev->dma_chan);
824exit_iounmap:
825	iounmap(base);
826exit_release:
827	release_mem_region(res->start, resource_size(res));
828exit_kfree:
829	kfree(pcdev);
830exit_put_clk:
831	clk_put(clk);
832exit:
833	return err;
834}
835
836static int __exit mx1_camera_remove(struct platform_device *pdev)
837{
838	struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
839	struct mx1_camera_dev *pcdev = container_of(soc_host,
840					struct mx1_camera_dev, soc_host);
841	struct resource *res;
842
843	imx_dma_free(pcdev->dma_chan);
844	disable_fiq(pcdev->irq);
845	mxc_set_irq_fiq(pcdev->irq, 0);
846	release_fiq(&fh);
847
848	clk_put(pcdev->clk);
849
850	soc_camera_host_unregister(soc_host);
851
852	iounmap(pcdev->base);
853
854	res = pcdev->res;
855	release_mem_region(res->start, resource_size(res));
856
857	kfree(pcdev);
858
859	dev_info(&pdev->dev, "MX1 Camera driver unloaded\n");
860
861	return 0;
862}
863
864static struct platform_driver mx1_camera_driver = {
865	.driver 	= {
866		.name	= DRIVER_NAME,
867	},
868	.remove		= __exit_p(mx1_camera_remove),
869};
870
871static int __init mx1_camera_init(void)
872{
873	return platform_driver_probe(&mx1_camera_driver, mx1_camera_probe);
874}
875
876static void __exit mx1_camera_exit(void)
877{
878	return platform_driver_unregister(&mx1_camera_driver);
879}
880
881module_init(mx1_camera_init);
882module_exit(mx1_camera_exit);
883
884MODULE_DESCRIPTION("i.MX1/i.MXL SoC Camera Host driver");
885MODULE_AUTHOR("Paulius Zaleckas <paulius.zaleckas@teltonika.lt>");
886MODULE_LICENSE("GPL v2");
887MODULE_VERSION(DRIVER_VERSION);
888MODULE_ALIAS("platform:" DRIVER_NAME);