PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / media / platform / soc_camera / mx1_camera.c
blobfea3e61476aefa3a2fe23d7237f46ac8a08311a8
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/platform_device.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
32 #include <linux/time.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>
39 #include <media/soc_mediabus.h>
41 #include <asm/dma.h>
42 #include <asm/fiq.h>
43 #include <mach/dma-mx1-mx2.h>
44 #include <mach/hardware.h>
45 #include <mach/irqs.h>
46 #include <linux/platform_data/camera-mx1.h>
49 * CSI registers
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 DRIVER_VERSION "0.0.2"
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 (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)
86 #define MAX_VIDEO_MEM 16 /* Video memory limit in megabytes */
89 * Structures
92 /* buffer for one video frame */
93 struct mx1_buffer {
94 /* common v4l buffer stuff -- must be first */
95 struct videobuf_buffer vb;
96 enum v4l2_mbus_pixelcode code;
97 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
105 struct mx1_camera_dev {
106 struct soc_camera_host soc_host;
107 struct mx1_camera_pdata *pdata;
108 struct mx1_buffer *active;
109 struct resource *res;
110 struct clk *clk;
111 struct list_head capture;
113 void __iomem *base;
114 int dma_chan;
115 unsigned int irq;
116 unsigned long mclk;
118 spinlock_t lock;
122 * Videobuf operations
124 static int mx1_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
125 unsigned int *size)
127 struct soc_camera_device *icd = vq->priv_data;
129 *size = icd->sizeimage;
131 if (!*count)
132 *count = 32;
134 if (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
135 *count = (MAX_VIDEO_MEM * 1024 * 1024) / *size;
137 dev_dbg(icd->parent, "count=%d, size=%d\n", *count, *size);
139 return 0;
142 static void free_buffer(struct videobuf_queue *vq, struct mx1_buffer *buf)
144 struct soc_camera_device *icd = vq->priv_data;
145 struct videobuf_buffer *vb = &buf->vb;
147 BUG_ON(in_interrupt());
149 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
150 vb, vb->baddr, vb->bsize);
153 * This waits until this buffer is out of danger, i.e., until it is no
154 * longer in STATE_QUEUED or STATE_ACTIVE
156 videobuf_waiton(vq, 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->parent, "%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);
178 * I think, in buf_prepare you only have to protect global data,
179 * the actual buffer is yours
181 buf->inwork = 1;
183 if (buf->code != icd->current_fmt->code ||
184 vb->width != icd->user_width ||
185 vb->height != icd->user_height ||
186 vb->field != field) {
187 buf->code = icd->current_fmt->code;
188 vb->width = icd->user_width;
189 vb->height = icd->user_height;
190 vb->field = field;
191 vb->state = VIDEOBUF_NEEDS_INIT;
194 vb->size = icd->sizeimage;
195 if (0 != vb->baddr && vb->bsize < vb->size) {
196 ret = -EINVAL;
197 goto out;
200 if (vb->state == VIDEOBUF_NEEDS_INIT) {
201 ret = videobuf_iolock(vq, vb, NULL);
202 if (ret)
203 goto fail;
205 vb->state = VIDEOBUF_PREPARED;
208 buf->inwork = 0;
210 return 0;
212 fail:
213 free_buffer(vq, buf);
214 out:
215 buf->inwork = 0;
216 return ret;
219 static int mx1_camera_setup_dma(struct mx1_camera_dev *pcdev)
221 struct videobuf_buffer *vbuf = &pcdev->active->vb;
222 struct device *dev = pcdev->soc_host.icd->parent;
223 int ret;
225 if (unlikely(!pcdev->active)) {
226 dev_err(dev, "DMA End IRQ with no active buffer\n");
227 return -EFAULT;
230 /* setup sg list for future DMA */
231 ret = imx_dma_setup_single(pcdev->dma_chan,
232 videobuf_to_dma_contig(vbuf),
233 vbuf->size, pcdev->res->start +
234 CSIRXR, DMA_MODE_READ);
235 if (unlikely(ret))
236 dev_err(dev, "Failed to setup DMA sg list\n");
238 return ret;
241 /* Called under spinlock_irqsave(&pcdev->lock, ...) */
242 static void mx1_videobuf_queue(struct videobuf_queue *vq,
243 struct videobuf_buffer *vb)
245 struct soc_camera_device *icd = vq->priv_data;
246 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
247 struct mx1_camera_dev *pcdev = ici->priv;
248 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
250 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
251 vb, vb->baddr, vb->bsize);
253 list_add_tail(&vb->queue, &pcdev->capture);
255 vb->state = VIDEOBUF_ACTIVE;
257 if (!pcdev->active) {
258 pcdev->active = buf;
260 /* setup sg list for future DMA */
261 if (!mx1_camera_setup_dma(pcdev)) {
262 unsigned int temp;
263 /* enable SOF irq */
264 temp = __raw_readl(pcdev->base + CSICR1) |
265 CSICR1_SOF_INTEN;
266 __raw_writel(temp, pcdev->base + CSICR1);
271 static void mx1_videobuf_release(struct videobuf_queue *vq,
272 struct videobuf_buffer *vb)
274 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
275 #ifdef DEBUG
276 struct soc_camera_device *icd = vq->priv_data;
277 struct device *dev = icd->parent;
279 dev_dbg(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(dev, "%s (active)\n", __func__);
285 break;
286 case VIDEOBUF_QUEUED:
287 dev_dbg(dev, "%s (queued)\n", __func__);
288 break;
289 case VIDEOBUF_PREPARED:
290 dev_dbg(dev, "%s (prepared)\n", __func__);
291 break;
292 default:
293 dev_dbg(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 v4l2_get_timestamp(&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 device *dev = pcdev->soc_host.icd->parent;
334 struct mx1_buffer *buf;
335 struct videobuf_buffer *vb;
336 unsigned long flags;
338 spin_lock_irqsave(&pcdev->lock, flags);
340 imx_dma_disable(channel);
342 if (unlikely(!pcdev->active)) {
343 dev_err(dev, "DMA End IRQ with no active buffer\n");
344 goto out;
347 vb = &pcdev->active->vb;
348 buf = container_of(vb, struct mx1_buffer, vb);
349 WARN_ON(buf->inwork || list_empty(&vb->queue));
350 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
351 vb, vb->baddr, vb->bsize);
353 mx1_camera_wakeup(pcdev, vb, buf);
354 out:
355 spin_unlock_irqrestore(&pcdev->lock, flags);
358 static struct videobuf_queue_ops mx1_videobuf_ops = {
359 .buf_setup = mx1_videobuf_setup,
360 .buf_prepare = mx1_videobuf_prepare,
361 .buf_queue = mx1_videobuf_queue,
362 .buf_release = mx1_videobuf_release,
365 static void mx1_camera_init_videobuf(struct videobuf_queue *q,
366 struct soc_camera_device *icd)
368 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
369 struct mx1_camera_dev *pcdev = ici->priv;
371 videobuf_queue_dma_contig_init(q, &mx1_videobuf_ops, icd->parent,
372 &pcdev->lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
373 V4L2_FIELD_NONE,
374 sizeof(struct mx1_buffer), icd, &ici->host_lock);
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);
386 * We verify platform_mclk_10khz != 0, so if anyone breaks it, here
387 * they get a nice Oops
389 div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
391 dev_dbg(pcdev->soc_host.icd->parent,
392 "System clock %lukHz, target freq %dkHz, divisor %lu\n",
393 lcdclk / 1000, mclk / 1000, div);
395 return div;
398 static void mx1_camera_activate(struct mx1_camera_dev *pcdev)
400 unsigned int csicr1 = CSICR1_EN;
402 dev_dbg(pcdev->soc_host.v4l2_dev.dev, "Activate device\n");
404 clk_prepare_enable(pcdev->clk);
406 /* enable CSI before doing anything else */
407 __raw_writel(csicr1, pcdev->base + CSICR1);
409 csicr1 |= CSICR1_MCLKEN | CSICR1_FCC | CSICR1_GCLK_MODE;
410 csicr1 |= CSICR1_MCLKDIV(mclk_get_divisor(pcdev));
411 csicr1 |= CSICR1_RXFF_LEVEL(2); /* 16 words */
413 __raw_writel(csicr1, pcdev->base + CSICR1);
416 static void mx1_camera_deactivate(struct mx1_camera_dev *pcdev)
418 dev_dbg(pcdev->soc_host.v4l2_dev.dev, "Deactivate device\n");
420 /* Disable all CSI interface */
421 __raw_writel(0x00, pcdev->base + CSICR1);
423 clk_disable_unprepare(pcdev->clk);
426 static int mx1_camera_add_device(struct soc_camera_device *icd)
428 dev_info(icd->parent, "MX1 Camera driver attached to camera %d\n",
429 icd->devnum);
431 return 0;
434 static void mx1_camera_remove_device(struct soc_camera_device *icd)
436 dev_info(icd->parent, "MX1 Camera driver detached from camera %d\n",
437 icd->devnum);
441 * The following two functions absolutely depend on the fact, that
442 * there can be only one camera on i.MX1/i.MXL camera sensor interface
444 static int mx1_camera_clock_start(struct soc_camera_host *ici)
446 struct mx1_camera_dev *pcdev = ici->priv;
448 mx1_camera_activate(pcdev);
450 return 0;
453 static void mx1_camera_clock_stop(struct soc_camera_host *ici)
455 struct mx1_camera_dev *pcdev = ici->priv;
456 unsigned int csicr1;
458 /* disable interrupts */
459 csicr1 = __raw_readl(pcdev->base + CSICR1) & ~CSI_IRQ_MASK;
460 __raw_writel(csicr1, pcdev->base + CSICR1);
462 /* Stop DMA engine */
463 imx_dma_disable(pcdev->dma_chan);
465 mx1_camera_deactivate(pcdev);
468 static int mx1_camera_set_bus_param(struct soc_camera_device *icd)
470 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
471 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
472 struct mx1_camera_dev *pcdev = ici->priv;
473 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
474 unsigned long common_flags;
475 unsigned int csicr1;
476 int ret;
478 /* MX1 supports only 8bit buswidth */
479 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
480 if (!ret) {
481 common_flags = soc_mbus_config_compatible(&cfg, CSI_BUS_FLAGS);
482 if (!common_flags) {
483 dev_warn(icd->parent,
484 "Flags incompatible: camera 0x%x, host 0x%x\n",
485 cfg.flags, CSI_BUS_FLAGS);
486 return -EINVAL;
488 } else if (ret != -ENOIOCTLCMD) {
489 return ret;
490 } else {
491 common_flags = CSI_BUS_FLAGS;
494 /* Make choises, based on platform choice */
495 if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
496 (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
497 if (!pcdev->pdata ||
498 pcdev->pdata->flags & MX1_CAMERA_VSYNC_HIGH)
499 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
500 else
501 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
504 if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
505 (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
506 if (!pcdev->pdata ||
507 pcdev->pdata->flags & MX1_CAMERA_PCLK_RISING)
508 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
509 else
510 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
513 if ((common_flags & V4L2_MBUS_DATA_ACTIVE_HIGH) &&
514 (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)) {
515 if (!pcdev->pdata ||
516 pcdev->pdata->flags & MX1_CAMERA_DATA_HIGH)
517 common_flags &= ~V4L2_MBUS_DATA_ACTIVE_LOW;
518 else
519 common_flags &= ~V4L2_MBUS_DATA_ACTIVE_HIGH;
522 cfg.flags = common_flags;
523 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
524 if (ret < 0 && ret != -ENOIOCTLCMD) {
525 dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n",
526 common_flags, ret);
527 return ret;
530 csicr1 = __raw_readl(pcdev->base + CSICR1);
532 if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
533 csicr1 |= CSICR1_REDGE;
534 if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
535 csicr1 |= CSICR1_SOF_POL;
536 if (common_flags & V4L2_MBUS_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 v4l2_subdev *sd = soc_camera_to_subdev(icd);
548 const struct soc_camera_format_xlate *xlate;
549 struct v4l2_pix_format *pix = &f->fmt.pix;
550 struct v4l2_mbus_framefmt mf;
551 int ret, buswidth;
553 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
554 if (!xlate) {
555 dev_warn(icd->parent, "Format %x not found\n",
556 pix->pixelformat);
557 return -EINVAL;
560 buswidth = xlate->host_fmt->bits_per_sample;
561 if (buswidth > 8) {
562 dev_warn(icd->parent,
563 "bits-per-sample %d for format %x unsupported\n",
564 buswidth, pix->pixelformat);
565 return -EINVAL;
568 mf.width = pix->width;
569 mf.height = pix->height;
570 mf.field = pix->field;
571 mf.colorspace = pix->colorspace;
572 mf.code = xlate->code;
574 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
575 if (ret < 0)
576 return ret;
578 if (mf.code != xlate->code)
579 return -EINVAL;
581 pix->width = mf.width;
582 pix->height = mf.height;
583 pix->field = mf.field;
584 pix->colorspace = mf.colorspace;
585 icd->current_fmt = xlate;
587 return ret;
590 static int mx1_camera_try_fmt(struct soc_camera_device *icd,
591 struct v4l2_format *f)
593 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
594 const struct soc_camera_format_xlate *xlate;
595 struct v4l2_pix_format *pix = &f->fmt.pix;
596 struct v4l2_mbus_framefmt mf;
597 int ret;
598 /* TODO: limit to mx1 hardware capabilities */
600 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
601 if (!xlate) {
602 dev_warn(icd->parent, "Format %x not found\n",
603 pix->pixelformat);
604 return -EINVAL;
607 mf.width = pix->width;
608 mf.height = pix->height;
609 mf.field = pix->field;
610 mf.colorspace = pix->colorspace;
611 mf.code = xlate->code;
613 /* limit to sensor capabilities */
614 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
615 if (ret < 0)
616 return ret;
618 pix->width = mf.width;
619 pix->height = mf.height;
620 pix->field = mf.field;
621 pix->colorspace = mf.colorspace;
623 return 0;
626 static int mx1_camera_reqbufs(struct soc_camera_device *icd,
627 struct v4l2_requestbuffers *p)
629 int i;
632 * This is for locking debugging only. I removed spinlocks and now I
633 * check whether .prepare is ever called on a linked buffer, or whether
634 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
635 * it hadn't triggered
637 for (i = 0; i < p->count; i++) {
638 struct mx1_buffer *buf = container_of(icd->vb_vidq.bufs[i],
639 struct mx1_buffer, vb);
640 buf->inwork = 0;
641 INIT_LIST_HEAD(&buf->vb.queue);
644 return 0;
647 static unsigned int mx1_camera_poll(struct file *file, poll_table *pt)
649 struct soc_camera_device *icd = file->private_data;
650 struct mx1_buffer *buf;
652 buf = list_entry(icd->vb_vidq.stream.next, struct mx1_buffer,
653 vb.stream);
655 poll_wait(file, &buf->vb.done, pt);
657 if (buf->vb.state == VIDEOBUF_DONE ||
658 buf->vb.state == VIDEOBUF_ERROR)
659 return POLLIN | POLLRDNORM;
661 return 0;
664 static int mx1_camera_querycap(struct soc_camera_host *ici,
665 struct v4l2_capability *cap)
667 /* cap->name is set by the friendly caller:-> */
668 strlcpy(cap->card, "i.MX1/i.MXL Camera", sizeof(cap->card));
669 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
671 return 0;
674 static struct soc_camera_host_ops mx1_soc_camera_host_ops = {
675 .owner = THIS_MODULE,
676 .add = mx1_camera_add_device,
677 .remove = mx1_camera_remove_device,
678 .clock_start = mx1_camera_clock_start,
679 .clock_stop = mx1_camera_clock_stop,
680 .set_bus_param = mx1_camera_set_bus_param,
681 .set_fmt = mx1_camera_set_fmt,
682 .try_fmt = mx1_camera_try_fmt,
683 .init_videobuf = mx1_camera_init_videobuf,
684 .reqbufs = mx1_camera_reqbufs,
685 .poll = mx1_camera_poll,
686 .querycap = mx1_camera_querycap,
689 static struct fiq_handler fh = {
690 .name = "csi_sof"
693 static int __init mx1_camera_probe(struct platform_device *pdev)
695 struct mx1_camera_dev *pcdev;
696 struct resource *res;
697 struct pt_regs regs;
698 struct clk *clk;
699 void __iomem *base;
700 unsigned int irq;
701 int err = 0;
703 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
704 irq = platform_get_irq(pdev, 0);
705 if (!res || (int)irq <= 0) {
706 err = -ENODEV;
707 goto exit;
710 clk = clk_get(&pdev->dev, "csi_clk");
711 if (IS_ERR(clk)) {
712 err = PTR_ERR(clk);
713 goto exit;
716 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
717 if (!pcdev) {
718 dev_err(&pdev->dev, "Could not allocate pcdev\n");
719 err = -ENOMEM;
720 goto exit_put_clk;
723 pcdev->res = res;
724 pcdev->clk = clk;
726 pcdev->pdata = pdev->dev.platform_data;
728 if (pcdev->pdata)
729 pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
731 if (!pcdev->mclk) {
732 dev_warn(&pdev->dev,
733 "mclk_10khz == 0! Please, fix your platform data. "
734 "Using default 20MHz\n");
735 pcdev->mclk = 20000000;
738 INIT_LIST_HEAD(&pcdev->capture);
739 spin_lock_init(&pcdev->lock);
742 * Request the regions.
744 if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME)) {
745 err = -EBUSY;
746 goto exit_kfree;
749 base = ioremap(res->start, resource_size(res));
750 if (!base) {
751 err = -ENOMEM;
752 goto exit_release;
754 pcdev->irq = irq;
755 pcdev->base = base;
757 /* request dma */
758 pcdev->dma_chan = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_HIGH);
759 if (pcdev->dma_chan < 0) {
760 dev_err(&pdev->dev, "Can't request DMA for MX1 CSI\n");
761 err = -EBUSY;
762 goto exit_iounmap;
764 dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chan);
766 imx_dma_setup_handlers(pcdev->dma_chan, mx1_camera_dma_irq, NULL,
767 pcdev);
769 imx_dma_config_channel(pcdev->dma_chan, IMX_DMA_TYPE_FIFO,
770 IMX_DMA_MEMSIZE_32, MX1_DMA_REQ_CSI_R, 0);
771 /* burst length : 16 words = 64 bytes */
772 imx_dma_config_burstlen(pcdev->dma_chan, 0);
774 /* request irq */
775 err = claim_fiq(&fh);
776 if (err) {
777 dev_err(&pdev->dev, "Camera interrupt register failed\n");
778 goto exit_free_dma;
781 set_fiq_handler(&mx1_camera_sof_fiq_start, &mx1_camera_sof_fiq_end -
782 &mx1_camera_sof_fiq_start);
784 regs.ARM_r8 = (long)MX1_DMA_DIMR;
785 regs.ARM_r9 = (long)MX1_DMA_CCR(pcdev->dma_chan);
786 regs.ARM_r10 = (long)pcdev->base + CSICR1;
787 regs.ARM_fp = (long)pcdev->base + CSISR;
788 regs.ARM_sp = 1 << pcdev->dma_chan;
789 set_fiq_regs(&regs);
791 mxc_set_irq_fiq(irq, 1);
792 enable_fiq(irq);
794 pcdev->soc_host.drv_name = DRIVER_NAME;
795 pcdev->soc_host.ops = &mx1_soc_camera_host_ops;
796 pcdev->soc_host.priv = pcdev;
797 pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
798 pcdev->soc_host.nr = pdev->id;
799 err = soc_camera_host_register(&pcdev->soc_host);
800 if (err)
801 goto exit_free_irq;
803 dev_info(&pdev->dev, "MX1 Camera driver loaded\n");
805 return 0;
807 exit_free_irq:
808 disable_fiq(irq);
809 mxc_set_irq_fiq(irq, 0);
810 release_fiq(&fh);
811 exit_free_dma:
812 imx_dma_free(pcdev->dma_chan);
813 exit_iounmap:
814 iounmap(base);
815 exit_release:
816 release_mem_region(res->start, resource_size(res));
817 exit_kfree:
818 kfree(pcdev);
819 exit_put_clk:
820 clk_put(clk);
821 exit:
822 return err;
825 static int __exit mx1_camera_remove(struct platform_device *pdev)
827 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
828 struct mx1_camera_dev *pcdev = container_of(soc_host,
829 struct mx1_camera_dev, soc_host);
830 struct resource *res;
832 imx_dma_free(pcdev->dma_chan);
833 disable_fiq(pcdev->irq);
834 mxc_set_irq_fiq(pcdev->irq, 0);
835 release_fiq(&fh);
837 clk_put(pcdev->clk);
839 soc_camera_host_unregister(soc_host);
841 iounmap(pcdev->base);
843 res = pcdev->res;
844 release_mem_region(res->start, resource_size(res));
846 kfree(pcdev);
848 dev_info(&pdev->dev, "MX1 Camera driver unloaded\n");
850 return 0;
853 static struct platform_driver mx1_camera_driver = {
854 .driver = {
855 .name = DRIVER_NAME,
857 .remove = __exit_p(mx1_camera_remove),
860 module_platform_driver_probe(mx1_camera_driver, mx1_camera_probe);
862 MODULE_DESCRIPTION("i.MX1/i.MXL SoC Camera Host driver");
863 MODULE_AUTHOR("Paulius Zaleckas <paulius.zaleckas@teltonika.lt>");
864 MODULE_LICENSE("GPL v2");
865 MODULE_VERSION(DRIVER_VERSION);
866 MODULE_ALIAS("platform:" DRIVER_NAME);