FRV: Use generic show_interrupts()
[cris-mirror.git] / drivers / media / video / mx1_camera.c
blobbc0c23a1009c2a89b5b40841d2de802c17a62037
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/sched.h>
32 #include <linux/slab.h>
33 #include <linux/time.h>
34 #include <linux/version.h>
35 #include <linux/videodev2.h>
37 #include <media/soc_camera.h>
38 #include <media/v4l2-common.h>
39 #include <media/v4l2-dev.h>
40 #include <media/videobuf-dma-contig.h>
41 #include <media/soc_mediabus.h>
43 #include <asm/dma.h>
44 #include <asm/fiq.h>
45 #include <mach/dma-mx1-mx2.h>
46 #include <mach/hardware.h>
47 #include <mach/mx1_camera.h>
50 * CSI registers
52 #define CSICR1 0x00 /* CSI Control Register 1 */
53 #define CSISR 0x08 /* CSI Status Register */
54 #define CSIRXR 0x10 /* CSI RxFIFO Register */
56 #define CSICR1_RXFF_LEVEL(x) (((x) & 0x3) << 19)
57 #define CSICR1_SOF_POL (1 << 17)
58 #define CSICR1_SOF_INTEN (1 << 16)
59 #define CSICR1_MCLKDIV(x) (((x) & 0xf) << 12)
60 #define CSICR1_MCLKEN (1 << 9)
61 #define CSICR1_FCC (1 << 8)
62 #define CSICR1_BIG_ENDIAN (1 << 7)
63 #define CSICR1_CLR_RXFIFO (1 << 5)
64 #define CSICR1_GCLK_MODE (1 << 4)
65 #define CSICR1_DATA_POL (1 << 2)
66 #define CSICR1_REDGE (1 << 1)
67 #define CSICR1_EN (1 << 0)
69 #define CSISR_SFF_OR_INT (1 << 25)
70 #define CSISR_RFF_OR_INT (1 << 24)
71 #define CSISR_STATFF_INT (1 << 21)
72 #define CSISR_RXFF_INT (1 << 18)
73 #define CSISR_SOF_INT (1 << 16)
74 #define CSISR_DRDY (1 << 0)
76 #define VERSION_CODE KERNEL_VERSION(0, 0, 1)
77 #define DRIVER_NAME "mx1-camera"
79 #define CSI_IRQ_MASK (CSISR_SFF_OR_INT | CSISR_RFF_OR_INT | \
80 CSISR_STATFF_INT | CSISR_RXFF_INT | CSISR_SOF_INT)
82 #define CSI_BUS_FLAGS (SOCAM_MASTER | SOCAM_HSYNC_ACTIVE_HIGH | \
83 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW | \
84 SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING | \
85 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_LOW | \
86 SOCAM_DATAWIDTH_8)
88 #define MAX_VIDEO_MEM 16 /* Video memory limit in megabytes */
91 * Structures
94 /* buffer for one video frame */
95 struct mx1_buffer {
96 /* common v4l buffer stuff -- must be first */
97 struct videobuf_buffer vb;
98 enum v4l2_mbus_pixelcode code;
99 int inwork;
103 * i.MX1/i.MXL is only supposed to handle one camera on its Camera Sensor
104 * Interface. If anyone ever builds hardware to enable more than
105 * one camera, they will have to modify this driver too
107 struct mx1_camera_dev {
108 struct soc_camera_host soc_host;
109 struct soc_camera_device *icd;
110 struct mx1_camera_pdata *pdata;
111 struct mx1_buffer *active;
112 struct resource *res;
113 struct clk *clk;
114 struct list_head capture;
116 void __iomem *base;
117 int dma_chan;
118 unsigned int irq;
119 unsigned long mclk;
121 spinlock_t lock;
125 * Videobuf operations
127 static int mx1_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
128 unsigned int *size)
130 struct soc_camera_device *icd = vq->priv_data;
131 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
132 icd->current_fmt->host_fmt);
134 if (bytes_per_line < 0)
135 return bytes_per_line;
137 *size = bytes_per_line * icd->user_height;
139 if (!*count)
140 *count = 32;
142 if (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
143 *count = (MAX_VIDEO_MEM * 1024 * 1024) / *size;
145 dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
147 return 0;
150 static void free_buffer(struct videobuf_queue *vq, struct mx1_buffer *buf)
152 struct soc_camera_device *icd = vq->priv_data;
153 struct videobuf_buffer *vb = &buf->vb;
155 BUG_ON(in_interrupt());
157 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
158 vb, vb->baddr, vb->bsize);
161 * This waits until this buffer is out of danger, i.e., until it is no
162 * longer in STATE_QUEUED or STATE_ACTIVE
164 videobuf_waiton(vq, vb, 0, 0);
165 videobuf_dma_contig_free(vq, vb);
167 vb->state = VIDEOBUF_NEEDS_INIT;
170 static int mx1_videobuf_prepare(struct videobuf_queue *vq,
171 struct videobuf_buffer *vb, enum v4l2_field field)
173 struct soc_camera_device *icd = vq->priv_data;
174 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
175 int ret;
176 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
177 icd->current_fmt->host_fmt);
179 if (bytes_per_line < 0)
180 return bytes_per_line;
182 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
183 vb, vb->baddr, vb->bsize);
185 /* Added list head initialization on alloc */
186 WARN_ON(!list_empty(&vb->queue));
188 BUG_ON(NULL == icd->current_fmt);
191 * I think, in buf_prepare you only have to protect global data,
192 * the actual buffer is yours
194 buf->inwork = 1;
196 if (buf->code != icd->current_fmt->code ||
197 vb->width != icd->user_width ||
198 vb->height != icd->user_height ||
199 vb->field != field) {
200 buf->code = icd->current_fmt->code;
201 vb->width = icd->user_width;
202 vb->height = icd->user_height;
203 vb->field = field;
204 vb->state = VIDEOBUF_NEEDS_INIT;
207 vb->size = bytes_per_line * vb->height;
208 if (0 != vb->baddr && vb->bsize < vb->size) {
209 ret = -EINVAL;
210 goto out;
213 if (vb->state == VIDEOBUF_NEEDS_INIT) {
214 ret = videobuf_iolock(vq, vb, NULL);
215 if (ret)
216 goto fail;
218 vb->state = VIDEOBUF_PREPARED;
221 buf->inwork = 0;
223 return 0;
225 fail:
226 free_buffer(vq, buf);
227 out:
228 buf->inwork = 0;
229 return ret;
232 static int mx1_camera_setup_dma(struct mx1_camera_dev *pcdev)
234 struct videobuf_buffer *vbuf = &pcdev->active->vb;
235 struct device *dev = pcdev->icd->dev.parent;
236 int ret;
238 if (unlikely(!pcdev->active)) {
239 dev_err(dev, "DMA End IRQ with no active buffer\n");
240 return -EFAULT;
243 /* setup sg list for future DMA */
244 ret = imx_dma_setup_single(pcdev->dma_chan,
245 videobuf_to_dma_contig(vbuf),
246 vbuf->size, pcdev->res->start +
247 CSIRXR, DMA_MODE_READ);
248 if (unlikely(ret))
249 dev_err(dev, "Failed to setup DMA sg list\n");
251 return ret;
254 /* Called under spinlock_irqsave(&pcdev->lock, ...) */
255 static void mx1_videobuf_queue(struct videobuf_queue *vq,
256 struct videobuf_buffer *vb)
258 struct soc_camera_device *icd = vq->priv_data;
259 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
260 struct mx1_camera_dev *pcdev = ici->priv;
261 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
263 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
264 vb, vb->baddr, vb->bsize);
266 list_add_tail(&vb->queue, &pcdev->capture);
268 vb->state = VIDEOBUF_ACTIVE;
270 if (!pcdev->active) {
271 pcdev->active = buf;
273 /* setup sg list for future DMA */
274 if (!mx1_camera_setup_dma(pcdev)) {
275 unsigned int temp;
276 /* enable SOF irq */
277 temp = __raw_readl(pcdev->base + CSICR1) |
278 CSICR1_SOF_INTEN;
279 __raw_writel(temp, pcdev->base + CSICR1);
284 static void mx1_videobuf_release(struct videobuf_queue *vq,
285 struct videobuf_buffer *vb)
287 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
288 #ifdef DEBUG
289 struct soc_camera_device *icd = vq->priv_data;
290 struct device *dev = icd->dev.parent;
292 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
293 vb, vb->baddr, vb->bsize);
295 switch (vb->state) {
296 case VIDEOBUF_ACTIVE:
297 dev_dbg(dev, "%s (active)\n", __func__);
298 break;
299 case VIDEOBUF_QUEUED:
300 dev_dbg(dev, "%s (queued)\n", __func__);
301 break;
302 case VIDEOBUF_PREPARED:
303 dev_dbg(dev, "%s (prepared)\n", __func__);
304 break;
305 default:
306 dev_dbg(dev, "%s (unknown)\n", __func__);
307 break;
309 #endif
311 free_buffer(vq, buf);
314 static void mx1_camera_wakeup(struct mx1_camera_dev *pcdev,
315 struct videobuf_buffer *vb,
316 struct mx1_buffer *buf)
318 /* _init is used to debug races, see comment in mx1_camera_reqbufs() */
319 list_del_init(&vb->queue);
320 vb->state = VIDEOBUF_DONE;
321 do_gettimeofday(&vb->ts);
322 vb->field_count++;
323 wake_up(&vb->done);
325 if (list_empty(&pcdev->capture)) {
326 pcdev->active = NULL;
327 return;
330 pcdev->active = list_entry(pcdev->capture.next,
331 struct mx1_buffer, vb.queue);
333 /* setup sg list for future DMA */
334 if (likely(!mx1_camera_setup_dma(pcdev))) {
335 unsigned int temp;
337 /* enable SOF irq */
338 temp = __raw_readl(pcdev->base + CSICR1) | CSICR1_SOF_INTEN;
339 __raw_writel(temp, pcdev->base + CSICR1);
343 static void mx1_camera_dma_irq(int channel, void *data)
345 struct mx1_camera_dev *pcdev = data;
346 struct device *dev = pcdev->icd->dev.parent;
347 struct mx1_buffer *buf;
348 struct videobuf_buffer *vb;
349 unsigned long flags;
351 spin_lock_irqsave(&pcdev->lock, flags);
353 imx_dma_disable(channel);
355 if (unlikely(!pcdev->active)) {
356 dev_err(dev, "DMA End IRQ with no active buffer\n");
357 goto out;
360 vb = &pcdev->active->vb;
361 buf = container_of(vb, struct mx1_buffer, vb);
362 WARN_ON(buf->inwork || list_empty(&vb->queue));
363 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
364 vb, vb->baddr, vb->bsize);
366 mx1_camera_wakeup(pcdev, vb, buf);
367 out:
368 spin_unlock_irqrestore(&pcdev->lock, flags);
371 static struct videobuf_queue_ops mx1_videobuf_ops = {
372 .buf_setup = mx1_videobuf_setup,
373 .buf_prepare = mx1_videobuf_prepare,
374 .buf_queue = mx1_videobuf_queue,
375 .buf_release = mx1_videobuf_release,
378 static void mx1_camera_init_videobuf(struct videobuf_queue *q,
379 struct soc_camera_device *icd)
381 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
382 struct mx1_camera_dev *pcdev = ici->priv;
384 videobuf_queue_dma_contig_init(q, &mx1_videobuf_ops, icd->dev.parent,
385 &pcdev->lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
386 V4L2_FIELD_NONE,
387 sizeof(struct mx1_buffer), icd, &icd->video_lock);
390 static int mclk_get_divisor(struct mx1_camera_dev *pcdev)
392 unsigned int mclk = pcdev->mclk;
393 unsigned long div;
394 unsigned long lcdclk;
396 lcdclk = clk_get_rate(pcdev->clk);
399 * We verify platform_mclk_10khz != 0, so if anyone breaks it, here
400 * they get a nice Oops
402 div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
404 dev_dbg(pcdev->icd->dev.parent,
405 "System clock %lukHz, target freq %dkHz, divisor %lu\n",
406 lcdclk / 1000, mclk / 1000, div);
408 return div;
411 static void mx1_camera_activate(struct mx1_camera_dev *pcdev)
413 unsigned int csicr1 = CSICR1_EN;
415 dev_dbg(pcdev->icd->dev.parent, "Activate device\n");
417 clk_enable(pcdev->clk);
419 /* enable CSI before doing anything else */
420 __raw_writel(csicr1, pcdev->base + CSICR1);
422 csicr1 |= CSICR1_MCLKEN | CSICR1_FCC | CSICR1_GCLK_MODE;
423 csicr1 |= CSICR1_MCLKDIV(mclk_get_divisor(pcdev));
424 csicr1 |= CSICR1_RXFF_LEVEL(2); /* 16 words */
426 __raw_writel(csicr1, pcdev->base + CSICR1);
429 static void mx1_camera_deactivate(struct mx1_camera_dev *pcdev)
431 dev_dbg(pcdev->icd->dev.parent, "Deactivate device\n");
433 /* Disable all CSI interface */
434 __raw_writel(0x00, pcdev->base + CSICR1);
436 clk_disable(pcdev->clk);
440 * The following two functions absolutely depend on the fact, that
441 * there can be only one camera on i.MX1/i.MXL camera sensor interface
443 static int mx1_camera_add_device(struct soc_camera_device *icd)
445 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
446 struct mx1_camera_dev *pcdev = ici->priv;
447 int ret;
449 if (pcdev->icd) {
450 ret = -EBUSY;
451 goto ebusy;
454 dev_info(icd->dev.parent, "MX1 Camera driver attached to camera %d\n",
455 icd->devnum);
457 mx1_camera_activate(pcdev);
459 pcdev->icd = icd;
461 ebusy:
462 return ret;
465 static void mx1_camera_remove_device(struct soc_camera_device *icd)
467 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
468 struct mx1_camera_dev *pcdev = ici->priv;
469 unsigned int csicr1;
471 BUG_ON(icd != pcdev->icd);
473 /* disable interrupts */
474 csicr1 = __raw_readl(pcdev->base + CSICR1) & ~CSI_IRQ_MASK;
475 __raw_writel(csicr1, pcdev->base + CSICR1);
477 /* Stop DMA engine */
478 imx_dma_disable(pcdev->dma_chan);
480 dev_info(icd->dev.parent, "MX1 Camera driver detached from camera %d\n",
481 icd->devnum);
483 mx1_camera_deactivate(pcdev);
485 pcdev->icd = NULL;
488 static int mx1_camera_set_crop(struct soc_camera_device *icd,
489 struct v4l2_crop *a)
491 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
493 return v4l2_subdev_call(sd, video, s_crop, a);
496 static int mx1_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
498 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
499 struct mx1_camera_dev *pcdev = ici->priv;
500 unsigned long camera_flags, common_flags;
501 unsigned int csicr1;
502 int ret;
504 camera_flags = icd->ops->query_bus_param(icd);
506 /* MX1 supports only 8bit buswidth */
507 common_flags = soc_camera_bus_param_compatible(camera_flags,
508 CSI_BUS_FLAGS);
509 if (!common_flags)
510 return -EINVAL;
512 /* Make choises, based on platform choice */
513 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
514 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
515 if (!pcdev->pdata ||
516 pcdev->pdata->flags & MX1_CAMERA_VSYNC_HIGH)
517 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
518 else
519 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
522 if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
523 (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
524 if (!pcdev->pdata ||
525 pcdev->pdata->flags & MX1_CAMERA_PCLK_RISING)
526 common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
527 else
528 common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
531 if ((common_flags & SOCAM_DATA_ACTIVE_HIGH) &&
532 (common_flags & SOCAM_DATA_ACTIVE_LOW)) {
533 if (!pcdev->pdata ||
534 pcdev->pdata->flags & MX1_CAMERA_DATA_HIGH)
535 common_flags &= ~SOCAM_DATA_ACTIVE_LOW;
536 else
537 common_flags &= ~SOCAM_DATA_ACTIVE_HIGH;
540 ret = icd->ops->set_bus_param(icd, common_flags);
541 if (ret < 0)
542 return ret;
544 csicr1 = __raw_readl(pcdev->base + CSICR1);
546 if (common_flags & SOCAM_PCLK_SAMPLE_RISING)
547 csicr1 |= CSICR1_REDGE;
548 if (common_flags & SOCAM_VSYNC_ACTIVE_HIGH)
549 csicr1 |= CSICR1_SOF_POL;
550 if (common_flags & SOCAM_DATA_ACTIVE_LOW)
551 csicr1 |= CSICR1_DATA_POL;
553 __raw_writel(csicr1, pcdev->base + CSICR1);
555 return 0;
558 static int mx1_camera_set_fmt(struct soc_camera_device *icd,
559 struct v4l2_format *f)
561 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
562 const struct soc_camera_format_xlate *xlate;
563 struct v4l2_pix_format *pix = &f->fmt.pix;
564 struct v4l2_mbus_framefmt mf;
565 int ret, buswidth;
567 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
568 if (!xlate) {
569 dev_warn(icd->dev.parent, "Format %x not found\n",
570 pix->pixelformat);
571 return -EINVAL;
574 buswidth = xlate->host_fmt->bits_per_sample;
575 if (buswidth > 8) {
576 dev_warn(icd->dev.parent,
577 "bits-per-sample %d for format %x unsupported\n",
578 buswidth, pix->pixelformat);
579 return -EINVAL;
582 mf.width = pix->width;
583 mf.height = pix->height;
584 mf.field = pix->field;
585 mf.colorspace = pix->colorspace;
586 mf.code = xlate->code;
588 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
589 if (ret < 0)
590 return ret;
592 if (mf.code != xlate->code)
593 return -EINVAL;
595 pix->width = mf.width;
596 pix->height = mf.height;
597 pix->field = mf.field;
598 pix->colorspace = mf.colorspace;
599 icd->current_fmt = xlate;
601 return ret;
604 static int mx1_camera_try_fmt(struct soc_camera_device *icd,
605 struct v4l2_format *f)
607 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
608 const struct soc_camera_format_xlate *xlate;
609 struct v4l2_pix_format *pix = &f->fmt.pix;
610 struct v4l2_mbus_framefmt mf;
611 int ret;
612 /* TODO: limit to mx1 hardware capabilities */
614 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
615 if (!xlate) {
616 dev_warn(icd->dev.parent, "Format %x not found\n",
617 pix->pixelformat);
618 return -EINVAL;
621 mf.width = pix->width;
622 mf.height = pix->height;
623 mf.field = pix->field;
624 mf.colorspace = pix->colorspace;
625 mf.code = xlate->code;
627 /* limit to sensor capabilities */
628 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
629 if (ret < 0)
630 return ret;
632 pix->width = mf.width;
633 pix->height = mf.height;
634 pix->field = mf.field;
635 pix->colorspace = mf.colorspace;
637 return 0;
640 static int mx1_camera_reqbufs(struct soc_camera_device *icd,
641 struct v4l2_requestbuffers *p)
643 int i;
646 * This is for locking debugging only. I removed spinlocks and now I
647 * check whether .prepare is ever called on a linked buffer, or whether
648 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
649 * it hadn't triggered
651 for (i = 0; i < p->count; i++) {
652 struct mx1_buffer *buf = container_of(icd->vb_vidq.bufs[i],
653 struct mx1_buffer, vb);
654 buf->inwork = 0;
655 INIT_LIST_HEAD(&buf->vb.queue);
658 return 0;
661 static unsigned int mx1_camera_poll(struct file *file, poll_table *pt)
663 struct soc_camera_device *icd = file->private_data;
664 struct mx1_buffer *buf;
666 buf = list_entry(icd->vb_vidq.stream.next, struct mx1_buffer,
667 vb.stream);
669 poll_wait(file, &buf->vb.done, pt);
671 if (buf->vb.state == VIDEOBUF_DONE ||
672 buf->vb.state == VIDEOBUF_ERROR)
673 return POLLIN | POLLRDNORM;
675 return 0;
678 static int mx1_camera_querycap(struct soc_camera_host *ici,
679 struct v4l2_capability *cap)
681 /* cap->name is set by the friendly caller:-> */
682 strlcpy(cap->card, "i.MX1/i.MXL Camera", sizeof(cap->card));
683 cap->version = VERSION_CODE;
684 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
686 return 0;
689 static struct soc_camera_host_ops mx1_soc_camera_host_ops = {
690 .owner = THIS_MODULE,
691 .add = mx1_camera_add_device,
692 .remove = mx1_camera_remove_device,
693 .set_bus_param = mx1_camera_set_bus_param,
694 .set_crop = mx1_camera_set_crop,
695 .set_fmt = mx1_camera_set_fmt,
696 .try_fmt = mx1_camera_try_fmt,
697 .init_videobuf = mx1_camera_init_videobuf,
698 .reqbufs = mx1_camera_reqbufs,
699 .poll = mx1_camera_poll,
700 .querycap = mx1_camera_querycap,
703 static struct fiq_handler fh = {
704 .name = "csi_sof"
707 static int __init mx1_camera_probe(struct platform_device *pdev)
709 struct mx1_camera_dev *pcdev;
710 struct resource *res;
711 struct pt_regs regs;
712 struct clk *clk;
713 void __iomem *base;
714 unsigned int irq;
715 int err = 0;
717 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
718 irq = platform_get_irq(pdev, 0);
719 if (!res || (int)irq <= 0) {
720 err = -ENODEV;
721 goto exit;
724 clk = clk_get(&pdev->dev, "csi_clk");
725 if (IS_ERR(clk)) {
726 err = PTR_ERR(clk);
727 goto exit;
730 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
731 if (!pcdev) {
732 dev_err(&pdev->dev, "Could not allocate pcdev\n");
733 err = -ENOMEM;
734 goto exit_put_clk;
737 pcdev->res = res;
738 pcdev->clk = clk;
740 pcdev->pdata = pdev->dev.platform_data;
742 if (pcdev->pdata)
743 pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
745 if (!pcdev->mclk) {
746 dev_warn(&pdev->dev,
747 "mclk_10khz == 0! Please, fix your platform data. "
748 "Using default 20MHz\n");
749 pcdev->mclk = 20000000;
752 INIT_LIST_HEAD(&pcdev->capture);
753 spin_lock_init(&pcdev->lock);
756 * Request the regions.
758 if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME)) {
759 err = -EBUSY;
760 goto exit_kfree;
763 base = ioremap(res->start, resource_size(res));
764 if (!base) {
765 err = -ENOMEM;
766 goto exit_release;
768 pcdev->irq = irq;
769 pcdev->base = base;
771 /* request dma */
772 pcdev->dma_chan = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_HIGH);
773 if (pcdev->dma_chan < 0) {
774 dev_err(&pdev->dev, "Can't request DMA for MX1 CSI\n");
775 err = -EBUSY;
776 goto exit_iounmap;
778 dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chan);
780 imx_dma_setup_handlers(pcdev->dma_chan, mx1_camera_dma_irq, NULL,
781 pcdev);
783 imx_dma_config_channel(pcdev->dma_chan, IMX_DMA_TYPE_FIFO,
784 IMX_DMA_MEMSIZE_32, MX1_DMA_REQ_CSI_R, 0);
785 /* burst length : 16 words = 64 bytes */
786 imx_dma_config_burstlen(pcdev->dma_chan, 0);
788 /* request irq */
789 err = claim_fiq(&fh);
790 if (err) {
791 dev_err(&pdev->dev, "Camera interrupt register failed \n");
792 goto exit_free_dma;
795 set_fiq_handler(&mx1_camera_sof_fiq_start, &mx1_camera_sof_fiq_end -
796 &mx1_camera_sof_fiq_start);
798 regs.ARM_r8 = (long)MX1_DMA_DIMR;
799 regs.ARM_r9 = (long)MX1_DMA_CCR(pcdev->dma_chan);
800 regs.ARM_r10 = (long)pcdev->base + CSICR1;
801 regs.ARM_fp = (long)pcdev->base + CSISR;
802 regs.ARM_sp = 1 << pcdev->dma_chan;
803 set_fiq_regs(&regs);
805 mxc_set_irq_fiq(irq, 1);
806 enable_fiq(irq);
808 pcdev->soc_host.drv_name = DRIVER_NAME;
809 pcdev->soc_host.ops = &mx1_soc_camera_host_ops;
810 pcdev->soc_host.priv = pcdev;
811 pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
812 pcdev->soc_host.nr = pdev->id;
813 err = soc_camera_host_register(&pcdev->soc_host);
814 if (err)
815 goto exit_free_irq;
817 dev_info(&pdev->dev, "MX1 Camera driver loaded\n");
819 return 0;
821 exit_free_irq:
822 disable_fiq(irq);
823 mxc_set_irq_fiq(irq, 0);
824 release_fiq(&fh);
825 exit_free_dma:
826 imx_dma_free(pcdev->dma_chan);
827 exit_iounmap:
828 iounmap(base);
829 exit_release:
830 release_mem_region(res->start, resource_size(res));
831 exit_kfree:
832 kfree(pcdev);
833 exit_put_clk:
834 clk_put(clk);
835 exit:
836 return err;
839 static int __exit mx1_camera_remove(struct platform_device *pdev)
841 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
842 struct mx1_camera_dev *pcdev = container_of(soc_host,
843 struct mx1_camera_dev, soc_host);
844 struct resource *res;
846 imx_dma_free(pcdev->dma_chan);
847 disable_fiq(pcdev->irq);
848 mxc_set_irq_fiq(pcdev->irq, 0);
849 release_fiq(&fh);
851 clk_put(pcdev->clk);
853 soc_camera_host_unregister(soc_host);
855 iounmap(pcdev->base);
857 res = pcdev->res;
858 release_mem_region(res->start, resource_size(res));
860 kfree(pcdev);
862 dev_info(&pdev->dev, "MX1 Camera driver unloaded\n");
864 return 0;
867 static struct platform_driver mx1_camera_driver = {
868 .driver = {
869 .name = DRIVER_NAME,
871 .remove = __exit_p(mx1_camera_remove),
874 static int __init mx1_camera_init(void)
876 return platform_driver_probe(&mx1_camera_driver, mx1_camera_probe);
879 static void __exit mx1_camera_exit(void)
881 return platform_driver_unregister(&mx1_camera_driver);
884 module_init(mx1_camera_init);
885 module_exit(mx1_camera_exit);
887 MODULE_DESCRIPTION("i.MX1/i.MXL SoC Camera Host driver");
888 MODULE_AUTHOR("Paulius Zaleckas <paulius.zaleckas@teltonika.lt>");
889 MODULE_LICENSE("GPL v2");
890 MODULE_ALIAS("platform:" DRIVER_NAME);