V4L/DVB (11608): soc-camera: host-driver cleanup
[linux-2.6/mini2440.git] / drivers / media / video / mx1_camera.c
blob48dd984dadff5a5de38be1a0a0e3a4eab452a3d0
1 /*
2 * V4L2 Driver for i.MXL/i.MXL camera (CSI) host
4 * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
5 * Copyright (C) 2009, Darius Augulis <augulis.darius@gmail.com>
7 * Based on PXA SoC camera driver
8 * Copyright (C) 2006, Sascha Hauer, Pengutronix
9 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
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.
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/time.h>
32 #include <linux/version.h>
33 #include <linux/videodev2.h>
35 #include <media/soc_camera.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-dev.h>
38 #include <media/videobuf-dma-contig.h>
40 #include <asm/dma.h>
41 #include <asm/fiq.h>
42 #include <mach/dma-mx1-mx2.h>
43 #include <mach/hardware.h>
44 #include <mach/mx1_camera.h>
47 * CSI registers
49 #define DMA_CCR(x) (0x8c + ((x) << 6)) /* Control Registers */
50 #define DMA_DIMR 0x08 /* Interrupt mask Register */
51 #define CSICR1 0x00 /* CSI Control Register 1 */
52 #define CSISR 0x08 /* CSI Status Register */
53 #define CSIRXR 0x10 /* CSI RxFIFO Register */
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)
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)
75 #define VERSION_CODE KERNEL_VERSION(0, 0, 1)
76 #define DRIVER_NAME "mx1-camera"
78 #define CSI_IRQ_MASK (CSISR_SFF_OR_INT | CSISR_RFF_OR_INT | \
79 CSISR_STATFF_INT | CSISR_RXFF_INT | CSISR_SOF_INT)
81 #define CSI_BUS_FLAGS (SOCAM_MASTER | SOCAM_HSYNC_ACTIVE_HIGH | \
82 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW | \
83 SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING | \
84 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_LOW | \
85 SOCAM_DATAWIDTH_8)
87 #define MAX_VIDEO_MEM 16 /* Video memory limit in megabytes */
90 * Structures
93 /* buffer for one video frame */
94 struct mx1_buffer {
95 /* common v4l buffer stuff -- must be first */
96 struct videobuf_buffer vb;
97 const struct soc_camera_data_format *fmt;
98 int inwork;
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 struct mx1_camera_dev {
105 struct soc_camera_host soc_host;
106 struct soc_camera_device *icd;
107 struct mx1_camera_pdata *pdata;
108 struct mx1_buffer *active;
109 struct device *dev;
110 struct resource *res;
111 struct clk *clk;
112 struct list_head capture;
114 void __iomem *base;
115 int dma_chan;
116 unsigned int irq;
117 unsigned long mclk;
119 spinlock_t lock;
123 * Videobuf operations
125 static int mx1_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
126 unsigned int *size)
128 struct soc_camera_device *icd = vq->priv_data;
130 *size = icd->width * icd->height *
131 ((icd->current_fmt->depth + 7) >> 3);
133 if (!*count)
134 *count = 32;
136 while (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
137 (*count)--;
139 dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
141 return 0;
144 static void free_buffer(struct videobuf_queue *vq, struct mx1_buffer *buf)
146 struct soc_camera_device *icd = vq->priv_data;
147 struct videobuf_buffer *vb = &buf->vb;
149 BUG_ON(in_interrupt());
151 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
152 vb, vb->baddr, vb->bsize);
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 videobuf_waiton(vb, 0, 0);
157 videobuf_dma_contig_free(vq, vb);
159 vb->state = VIDEOBUF_NEEDS_INIT;
162 static int mx1_videobuf_prepare(struct videobuf_queue *vq,
163 struct videobuf_buffer *vb, enum v4l2_field field)
165 struct soc_camera_device *icd = vq->priv_data;
166 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
167 int ret;
169 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
170 vb, vb->baddr, vb->bsize);
172 /* Added list head initialization on alloc */
173 WARN_ON(!list_empty(&vb->queue));
175 BUG_ON(NULL == icd->current_fmt);
177 /* I think, in buf_prepare you only have to protect global data,
178 * the actual buffer is yours */
179 buf->inwork = 1;
181 if (buf->fmt != icd->current_fmt ||
182 vb->width != icd->width ||
183 vb->height != icd->height ||
184 vb->field != field) {
185 buf->fmt = icd->current_fmt;
186 vb->width = icd->width;
187 vb->height = icd->height;
188 vb->field = field;
189 vb->state = VIDEOBUF_NEEDS_INIT;
192 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
193 if (0 != vb->baddr && vb->bsize < vb->size) {
194 ret = -EINVAL;
195 goto out;
198 if (vb->state == VIDEOBUF_NEEDS_INIT) {
199 ret = videobuf_iolock(vq, vb, NULL);
200 if (ret)
201 goto fail;
203 vb->state = VIDEOBUF_PREPARED;
206 buf->inwork = 0;
208 return 0;
210 fail:
211 free_buffer(vq, buf);
212 out:
213 buf->inwork = 0;
214 return ret;
217 static int mx1_camera_setup_dma(struct mx1_camera_dev *pcdev)
219 struct videobuf_buffer *vbuf = &pcdev->active->vb;
220 int ret;
222 if (unlikely(!pcdev->active)) {
223 dev_err(pcdev->dev, "DMA End IRQ with no active buffer\n");
224 return -EFAULT;
227 /* setup sg list for future DMA */
228 ret = imx_dma_setup_single(pcdev->dma_chan,
229 videobuf_to_dma_contig(vbuf),
230 vbuf->size, pcdev->res->start +
231 CSIRXR, DMA_MODE_READ);
232 if (unlikely(ret))
233 dev_err(pcdev->dev, "Failed to setup DMA sg list\n");
235 return ret;
238 static void mx1_videobuf_queue(struct videobuf_queue *vq,
239 struct videobuf_buffer *vb)
241 struct soc_camera_device *icd = vq->priv_data;
242 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
243 struct mx1_camera_dev *pcdev = ici->priv;
244 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
245 unsigned long flags;
247 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
248 vb, vb->baddr, vb->bsize);
250 spin_lock_irqsave(&pcdev->lock, flags);
252 list_add_tail(&vb->queue, &pcdev->capture);
254 vb->state = VIDEOBUF_ACTIVE;
256 if (!pcdev->active) {
257 pcdev->active = buf;
259 /* setup sg list for future DMA */
260 if (!mx1_camera_setup_dma(pcdev)) {
261 unsigned int temp;
262 /* enable SOF irq */
263 temp = __raw_readl(pcdev->base + CSICR1) |
264 CSICR1_SOF_INTEN;
265 __raw_writel(temp, pcdev->base + CSICR1);
269 spin_unlock_irqrestore(&pcdev->lock, flags);
272 static void mx1_videobuf_release(struct videobuf_queue *vq,
273 struct videobuf_buffer *vb)
275 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
276 #ifdef DEBUG
277 struct soc_camera_device *icd = vq->priv_data;
279 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
280 vb, vb->baddr, vb->bsize);
282 switch (vb->state) {
283 case VIDEOBUF_ACTIVE:
284 dev_dbg(&icd->dev, "%s (active)\n", __func__);
285 break;
286 case VIDEOBUF_QUEUED:
287 dev_dbg(&icd->dev, "%s (queued)\n", __func__);
288 break;
289 case VIDEOBUF_PREPARED:
290 dev_dbg(&icd->dev, "%s (prepared)\n", __func__);
291 break;
292 default:
293 dev_dbg(&icd->dev, "%s (unknown)\n", __func__);
294 break;
296 #endif
298 free_buffer(vq, buf);
301 static void mx1_camera_wakeup(struct mx1_camera_dev *pcdev,
302 struct videobuf_buffer *vb,
303 struct mx1_buffer *buf)
305 /* _init is used to debug races, see comment in mx1_camera_reqbufs() */
306 list_del_init(&vb->queue);
307 vb->state = VIDEOBUF_DONE;
308 do_gettimeofday(&vb->ts);
309 vb->field_count++;
310 wake_up(&vb->done);
312 if (list_empty(&pcdev->capture)) {
313 pcdev->active = NULL;
314 return;
317 pcdev->active = list_entry(pcdev->capture.next,
318 struct mx1_buffer, vb.queue);
320 /* setup sg list for future DMA */
321 if (likely(!mx1_camera_setup_dma(pcdev))) {
322 unsigned int temp;
324 /* enable SOF irq */
325 temp = __raw_readl(pcdev->base + CSICR1) | CSICR1_SOF_INTEN;
326 __raw_writel(temp, pcdev->base + CSICR1);
330 static void mx1_camera_dma_irq(int channel, void *data)
332 struct mx1_camera_dev *pcdev = data;
333 struct mx1_buffer *buf;
334 struct videobuf_buffer *vb;
335 unsigned long flags;
337 spin_lock_irqsave(&pcdev->lock, flags);
339 imx_dma_disable(channel);
341 if (unlikely(!pcdev->active)) {
342 dev_err(pcdev->dev, "DMA End IRQ with no active buffer\n");
343 goto out;
346 vb = &pcdev->active->vb;
347 buf = container_of(vb, struct mx1_buffer, vb);
348 WARN_ON(buf->inwork || list_empty(&vb->queue));
349 dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
350 vb, vb->baddr, vb->bsize);
352 mx1_camera_wakeup(pcdev, vb, buf);
353 out:
354 spin_unlock_irqrestore(&pcdev->lock, flags);
357 static struct videobuf_queue_ops mx1_videobuf_ops = {
358 .buf_setup = mx1_videobuf_setup,
359 .buf_prepare = mx1_videobuf_prepare,
360 .buf_queue = mx1_videobuf_queue,
361 .buf_release = mx1_videobuf_release,
364 static void mx1_camera_init_videobuf(struct videobuf_queue *q,
365 struct soc_camera_device *icd)
367 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
368 struct mx1_camera_dev *pcdev = ici->priv;
370 videobuf_queue_dma_contig_init(q, &mx1_videobuf_ops, pcdev->dev,
371 &pcdev->lock,
372 V4L2_BUF_TYPE_VIDEO_CAPTURE,
373 V4L2_FIELD_NONE,
374 sizeof(struct mx1_buffer), icd);
377 static int mclk_get_divisor(struct mx1_camera_dev *pcdev)
379 unsigned int mclk = pcdev->mclk;
380 unsigned long div;
381 unsigned long lcdclk;
383 lcdclk = clk_get_rate(pcdev->clk);
385 /* We verify platform_mclk_10khz != 0, so if anyone breaks it, here
386 * they get a nice Oops */
387 div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
389 dev_dbg(pcdev->dev, "System clock %lukHz, target freq %dkHz, "
390 "divisor %lu\n", lcdclk / 1000, mclk / 1000, div);
392 return div;
395 static void mx1_camera_activate(struct mx1_camera_dev *pcdev)
397 unsigned int csicr1 = CSICR1_EN;
399 dev_dbg(pcdev->dev, "Activate device\n");
401 clk_enable(pcdev->clk);
403 /* enable CSI before doing anything else */
404 __raw_writel(csicr1, pcdev->base + CSICR1);
406 csicr1 |= CSICR1_MCLKEN | CSICR1_FCC | CSICR1_GCLK_MODE;
407 csicr1 |= CSICR1_MCLKDIV(mclk_get_divisor(pcdev));
408 csicr1 |= CSICR1_RXFF_LEVEL(2); /* 16 words */
410 __raw_writel(csicr1, pcdev->base + CSICR1);
413 static void mx1_camera_deactivate(struct mx1_camera_dev *pcdev)
415 dev_dbg(pcdev->dev, "Deactivate device\n");
417 /* Disable all CSI interface */
418 __raw_writel(0x00, pcdev->base + CSICR1);
420 clk_disable(pcdev->clk);
423 /* The following two functions absolutely depend on the fact, that
424 * there can be only one camera on i.MX1/i.MXL camera sensor interface */
425 static int mx1_camera_add_device(struct soc_camera_device *icd)
427 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
428 struct mx1_camera_dev *pcdev = ici->priv;
429 int ret;
431 if (pcdev->icd) {
432 ret = -EBUSY;
433 goto ebusy;
436 dev_info(&icd->dev, "MX1 Camera driver attached to camera %d\n",
437 icd->devnum);
439 mx1_camera_activate(pcdev);
440 ret = icd->ops->init(icd);
442 if (!ret)
443 pcdev->icd = icd;
445 ebusy:
446 return ret;
449 static void mx1_camera_remove_device(struct soc_camera_device *icd)
451 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
452 struct mx1_camera_dev *pcdev = ici->priv;
453 unsigned int csicr1;
455 BUG_ON(icd != pcdev->icd);
457 /* disable interrupts */
458 csicr1 = __raw_readl(pcdev->base + CSICR1) & ~CSI_IRQ_MASK;
459 __raw_writel(csicr1, pcdev->base + CSICR1);
461 /* Stop DMA engine */
462 imx_dma_disable(pcdev->dma_chan);
464 dev_info(&icd->dev, "MX1 Camera driver detached from camera %d\n",
465 icd->devnum);
467 icd->ops->release(icd);
469 mx1_camera_deactivate(pcdev);
471 pcdev->icd = NULL;
474 static int mx1_camera_set_crop(struct soc_camera_device *icd,
475 struct v4l2_rect *rect)
477 return icd->ops->set_crop(icd, rect);
480 static int mx1_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
482 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
483 struct mx1_camera_dev *pcdev = ici->priv;
484 unsigned long camera_flags, common_flags;
485 unsigned int csicr1;
486 int ret;
488 camera_flags = icd->ops->query_bus_param(icd);
490 /* MX1 supports only 8bit buswidth */
491 common_flags = soc_camera_bus_param_compatible(camera_flags,
492 CSI_BUS_FLAGS);
493 if (!common_flags)
494 return -EINVAL;
496 icd->buswidth = 8;
498 /* Make choises, based on platform choice */
499 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
500 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
501 if (!pcdev->pdata ||
502 pcdev->pdata->flags & MX1_CAMERA_VSYNC_HIGH)
503 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
504 else
505 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
508 if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
509 (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
510 if (!pcdev->pdata ||
511 pcdev->pdata->flags & MX1_CAMERA_PCLK_RISING)
512 common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
513 else
514 common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
517 if ((common_flags & SOCAM_DATA_ACTIVE_HIGH) &&
518 (common_flags & SOCAM_DATA_ACTIVE_LOW)) {
519 if (!pcdev->pdata ||
520 pcdev->pdata->flags & MX1_CAMERA_DATA_HIGH)
521 common_flags &= ~SOCAM_DATA_ACTIVE_LOW;
522 else
523 common_flags &= ~SOCAM_DATA_ACTIVE_HIGH;
526 ret = icd->ops->set_bus_param(icd, common_flags);
527 if (ret < 0)
528 return ret;
530 csicr1 = __raw_readl(pcdev->base + CSICR1);
532 if (common_flags & SOCAM_PCLK_SAMPLE_RISING)
533 csicr1 |= CSICR1_REDGE;
534 if (common_flags & SOCAM_VSYNC_ACTIVE_HIGH)
535 csicr1 |= CSICR1_SOF_POL;
536 if (common_flags & SOCAM_DATA_ACTIVE_LOW)
537 csicr1 |= CSICR1_DATA_POL;
539 __raw_writel(csicr1, pcdev->base + CSICR1);
541 return 0;
544 static int mx1_camera_set_fmt(struct soc_camera_device *icd,
545 struct v4l2_format *f)
547 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
548 const struct soc_camera_format_xlate *xlate;
549 struct v4l2_pix_format *pix = &f->fmt.pix;
550 int ret;
552 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
553 if (!xlate) {
554 dev_warn(&ici->dev, "Format %x not found\n", pix->pixelformat);
555 return -EINVAL;
558 ret = icd->ops->set_fmt(icd, f);
559 if (!ret) {
560 icd->buswidth = xlate->buswidth;
561 icd->current_fmt = xlate->host_fmt;
564 return ret;
567 static int mx1_camera_try_fmt(struct soc_camera_device *icd,
568 struct v4l2_format *f)
570 /* TODO: limit to mx1 hardware capabilities */
572 /* limit to sensor capabilities */
573 return icd->ops->try_fmt(icd, f);
576 static int mx1_camera_reqbufs(struct soc_camera_file *icf,
577 struct v4l2_requestbuffers *p)
579 int i;
581 /* This is for locking debugging only. I removed spinlocks and now I
582 * check whether .prepare is ever called on a linked buffer, or whether
583 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
584 * it hadn't triggered */
585 for (i = 0; i < p->count; i++) {
586 struct mx1_buffer *buf = container_of(icf->vb_vidq.bufs[i],
587 struct mx1_buffer, vb);
588 buf->inwork = 0;
589 INIT_LIST_HEAD(&buf->vb.queue);
592 return 0;
595 static unsigned int mx1_camera_poll(struct file *file, poll_table *pt)
597 struct soc_camera_file *icf = file->private_data;
598 struct mx1_buffer *buf;
600 buf = list_entry(icf->vb_vidq.stream.next, struct mx1_buffer,
601 vb.stream);
603 poll_wait(file, &buf->vb.done, pt);
605 if (buf->vb.state == VIDEOBUF_DONE ||
606 buf->vb.state == VIDEOBUF_ERROR)
607 return POLLIN | POLLRDNORM;
609 return 0;
612 static int mx1_camera_querycap(struct soc_camera_host *ici,
613 struct v4l2_capability *cap)
615 /* cap->name is set by the friendly caller:-> */
616 strlcpy(cap->card, "i.MX1/i.MXL Camera", sizeof(cap->card));
617 cap->version = VERSION_CODE;
618 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
620 return 0;
623 static struct soc_camera_host_ops mx1_soc_camera_host_ops = {
624 .owner = THIS_MODULE,
625 .add = mx1_camera_add_device,
626 .remove = mx1_camera_remove_device,
627 .set_bus_param = mx1_camera_set_bus_param,
628 .set_crop = mx1_camera_set_crop,
629 .set_fmt = mx1_camera_set_fmt,
630 .try_fmt = mx1_camera_try_fmt,
631 .init_videobuf = mx1_camera_init_videobuf,
632 .reqbufs = mx1_camera_reqbufs,
633 .poll = mx1_camera_poll,
634 .querycap = mx1_camera_querycap,
637 static struct fiq_handler fh = {
638 .name = "csi_sof"
641 static int __init mx1_camera_probe(struct platform_device *pdev)
643 struct mx1_camera_dev *pcdev;
644 struct resource *res;
645 struct pt_regs regs;
646 struct clk *clk;
647 void __iomem *base;
648 unsigned int irq;
649 int err = 0;
651 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
652 irq = platform_get_irq(pdev, 0);
653 if (!res || !irq) {
654 err = -ENODEV;
655 goto exit;
658 clk = clk_get(&pdev->dev, "csi_clk");
659 if (IS_ERR(clk)) {
660 err = PTR_ERR(clk);
661 goto exit;
664 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
665 if (!pcdev) {
666 dev_err(&pdev->dev, "Could not allocate pcdev\n");
667 err = -ENOMEM;
668 goto exit_put_clk;
671 platform_set_drvdata(pdev, pcdev);
672 pcdev->res = res;
673 pcdev->clk = clk;
675 pcdev->pdata = pdev->dev.platform_data;
677 if (pcdev->pdata)
678 pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
680 if (!pcdev->mclk) {
681 dev_warn(&pdev->dev,
682 "mclk_10khz == 0! Please, fix your platform data. "
683 "Using default 20MHz\n");
684 pcdev->mclk = 20000000;
687 INIT_LIST_HEAD(&pcdev->capture);
688 spin_lock_init(&pcdev->lock);
691 * Request the regions.
693 if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME)) {
694 err = -EBUSY;
695 goto exit_kfree;
698 base = ioremap(res->start, resource_size(res));
699 if (!base) {
700 err = -ENOMEM;
701 goto exit_release;
703 pcdev->irq = irq;
704 pcdev->base = base;
705 pcdev->dev = &pdev->dev;
707 /* request dma */
708 pcdev->dma_chan = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_HIGH);
709 if (pcdev->dma_chan < 0) {
710 dev_err(pcdev->dev, "Can't request DMA for MX1 CSI\n");
711 err = -EBUSY;
712 goto exit_iounmap;
714 dev_dbg(pcdev->dev, "got DMA channel %d\n", pcdev->dma_chan);
716 imx_dma_setup_handlers(pcdev->dma_chan, mx1_camera_dma_irq, NULL,
717 pcdev);
719 imx_dma_config_channel(pcdev->dma_chan, IMX_DMA_TYPE_FIFO,
720 IMX_DMA_MEMSIZE_32, DMA_REQ_CSI_R, 0);
721 /* burst length : 16 words = 64 bytes */
722 imx_dma_config_burstlen(pcdev->dma_chan, 0);
724 /* request irq */
725 err = claim_fiq(&fh);
726 if (err) {
727 dev_err(pcdev->dev, "Camera interrupt register failed \n");
728 goto exit_free_dma;
731 set_fiq_handler(&mx1_camera_sof_fiq_start, &mx1_camera_sof_fiq_end -
732 &mx1_camera_sof_fiq_start);
734 regs.ARM_r8 = DMA_BASE + DMA_DIMR;
735 regs.ARM_r9 = DMA_BASE + DMA_CCR(pcdev->dma_chan);
736 regs.ARM_r10 = (long)pcdev->base + CSICR1;
737 regs.ARM_fp = (long)pcdev->base + CSISR;
738 regs.ARM_sp = 1 << pcdev->dma_chan;
739 set_fiq_regs(&regs);
741 mxc_set_irq_fiq(irq, 1);
742 enable_fiq(irq);
744 pcdev->soc_host.drv_name = DRIVER_NAME;
745 pcdev->soc_host.ops = &mx1_soc_camera_host_ops;
746 pcdev->soc_host.priv = pcdev;
747 pcdev->soc_host.dev.parent = &pdev->dev;
748 pcdev->soc_host.nr = pdev->id;
749 err = soc_camera_host_register(&pcdev->soc_host);
750 if (err)
751 goto exit_free_irq;
753 dev_info(&pdev->dev, "MX1 Camera driver loaded\n");
755 return 0;
757 exit_free_irq:
758 disable_fiq(irq);
759 mxc_set_irq_fiq(irq, 0);
760 release_fiq(&fh);
761 exit_free_dma:
762 imx_dma_free(pcdev->dma_chan);
763 exit_iounmap:
764 iounmap(base);
765 exit_release:
766 release_mem_region(res->start, resource_size(res));
767 exit_kfree:
768 kfree(pcdev);
769 exit_put_clk:
770 clk_put(clk);
771 exit:
772 return err;
775 static int __exit mx1_camera_remove(struct platform_device *pdev)
777 struct mx1_camera_dev *pcdev = platform_get_drvdata(pdev);
778 struct resource *res;
780 imx_dma_free(pcdev->dma_chan);
781 disable_fiq(pcdev->irq);
782 mxc_set_irq_fiq(pcdev->irq, 0);
783 release_fiq(&fh);
785 clk_put(pcdev->clk);
787 soc_camera_host_unregister(&pcdev->soc_host);
789 iounmap(pcdev->base);
791 res = pcdev->res;
792 release_mem_region(res->start, resource_size(res));
794 kfree(pcdev);
796 dev_info(&pdev->dev, "MX1 Camera driver unloaded\n");
798 return 0;
801 static struct platform_driver mx1_camera_driver = {
802 .driver = {
803 .name = DRIVER_NAME,
805 .remove = __exit_p(mx1_camera_remove),
808 static int __init mx1_camera_init(void)
810 return platform_driver_probe(&mx1_camera_driver, mx1_camera_probe);
813 static void __exit mx1_camera_exit(void)
815 return platform_driver_unregister(&mx1_camera_driver);
818 module_init(mx1_camera_init);
819 module_exit(mx1_camera_exit);
821 MODULE_DESCRIPTION("i.MX1/i.MXL SoC Camera Host driver");
822 MODULE_AUTHOR("Paulius Zaleckas <paulius.zaleckas@teltonika.lt>");
823 MODULE_LICENSE("GPL v2");
824 MODULE_ALIAS("platform:" DRIVER_NAME);