rt2800: initialize BBP_R104 on proper subroutines
[linux/fpc-iii.git] / drivers / media / platform / soc_camera / mx1_camera.c
bloba3fd8d63546cf08bfae801a01f24d1b940908c31
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 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;
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->sizeimage;
132 if (!*count)
133 *count = 32;
135 if (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
136 *count = (MAX_VIDEO_MEM * 1024 * 1024) / *size;
138 dev_dbg(icd->parent, "count=%d, size=%d\n", *count, *size);
140 return 0;
143 static void free_buffer(struct videobuf_queue *vq, struct mx1_buffer *buf)
145 struct soc_camera_device *icd = vq->priv_data;
146 struct videobuf_buffer *vb = &buf->vb;
148 BUG_ON(in_interrupt());
150 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
151 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
157 videobuf_waiton(vq, vb, 0, 0);
158 videobuf_dma_contig_free(vq, vb);
160 vb->state = VIDEOBUF_NEEDS_INIT;
163 static int mx1_videobuf_prepare(struct videobuf_queue *vq,
164 struct videobuf_buffer *vb, enum v4l2_field field)
166 struct soc_camera_device *icd = vq->priv_data;
167 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
168 int ret;
170 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
171 vb, vb->baddr, vb->bsize);
173 /* Added list head initialization on alloc */
174 WARN_ON(!list_empty(&vb->queue));
176 BUG_ON(NULL == icd->current_fmt);
179 * I think, in buf_prepare you only have to protect global data,
180 * the actual buffer is yours
182 buf->inwork = 1;
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;
195 vb->size = icd->sizeimage;
196 if (0 != vb->baddr && vb->bsize < vb->size) {
197 ret = -EINVAL;
198 goto out;
201 if (vb->state == VIDEOBUF_NEEDS_INIT) {
202 ret = videobuf_iolock(vq, vb, NULL);
203 if (ret)
204 goto fail;
206 vb->state = VIDEOBUF_PREPARED;
209 buf->inwork = 0;
211 return 0;
213 fail:
214 free_buffer(vq, buf);
215 out:
216 buf->inwork = 0;
217 return ret;
220 static int mx1_camera_setup_dma(struct mx1_camera_dev *pcdev)
222 struct videobuf_buffer *vbuf = &pcdev->active->vb;
223 struct device *dev = pcdev->icd->parent;
224 int ret;
226 if (unlikely(!pcdev->active)) {
227 dev_err(dev, "DMA End IRQ with no active buffer\n");
228 return -EFAULT;
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");
239 return ret;
242 /* Called under spinlock_irqsave(&pcdev->lock, ...) */
243 static void mx1_videobuf_queue(struct videobuf_queue *vq,
244 struct videobuf_buffer *vb)
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);
251 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
252 vb, vb->baddr, vb->bsize);
254 list_add_tail(&vb->queue, &pcdev->capture);
256 vb->state = VIDEOBUF_ACTIVE;
258 if (!pcdev->active) {
259 pcdev->active = buf;
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);
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;
278 struct device *dev = icd->parent;
280 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
281 vb, vb->baddr, vb->bsize);
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;
297 #endif
299 free_buffer(vq, buf);
302 static void mx1_camera_wakeup(struct mx1_camera_dev *pcdev,
303 struct videobuf_buffer *vb,
304 struct mx1_buffer *buf)
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 v4l2_get_timestamp(&vb->ts);
310 vb->field_count++;
311 wake_up(&vb->done);
313 if (list_empty(&pcdev->capture)) {
314 pcdev->active = NULL;
315 return;
318 pcdev->active = list_entry(pcdev->capture.next,
319 struct mx1_buffer, vb.queue);
321 /* setup sg list for future DMA */
322 if (likely(!mx1_camera_setup_dma(pcdev))) {
323 unsigned int temp;
325 /* enable SOF irq */
326 temp = __raw_readl(pcdev->base + CSICR1) | CSICR1_SOF_INTEN;
327 __raw_writel(temp, pcdev->base + CSICR1);
331 static void mx1_camera_dma_irq(int channel, void *data)
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;
339 spin_lock_irqsave(&pcdev->lock, flags);
341 imx_dma_disable(channel);
343 if (unlikely(!pcdev->active)) {
344 dev_err(dev, "DMA End IRQ with no active buffer\n");
345 goto out;
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);
354 mx1_camera_wakeup(pcdev, vb, buf);
355 out:
356 spin_unlock_irqrestore(&pcdev->lock, flags);
359 static 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,
366 static void mx1_camera_init_videobuf(struct videobuf_queue *q,
367 struct soc_camera_device *icd)
369 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
370 struct mx1_camera_dev *pcdev = ici->priv;
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, &ici->host_lock);
378 static int mclk_get_divisor(struct mx1_camera_dev *pcdev)
380 unsigned int mclk = pcdev->mclk;
381 unsigned long div;
382 unsigned long lcdclk;
384 lcdclk = clk_get_rate(pcdev->clk);
387 * We verify platform_mclk_10khz != 0, so if anyone breaks it, here
388 * they get a nice Oops
390 div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
392 dev_dbg(pcdev->icd->parent,
393 "System clock %lukHz, target freq %dkHz, divisor %lu\n",
394 lcdclk / 1000, mclk / 1000, div);
396 return div;
399 static void mx1_camera_activate(struct mx1_camera_dev *pcdev)
401 unsigned int csicr1 = CSICR1_EN;
403 dev_dbg(pcdev->icd->parent, "Activate device\n");
405 clk_prepare_enable(pcdev->clk);
407 /* enable CSI before doing anything else */
408 __raw_writel(csicr1, pcdev->base + CSICR1);
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 */
414 __raw_writel(csicr1, pcdev->base + CSICR1);
417 static void mx1_camera_deactivate(struct mx1_camera_dev *pcdev)
419 dev_dbg(pcdev->icd->parent, "Deactivate device\n");
421 /* Disable all CSI interface */
422 __raw_writel(0x00, pcdev->base + CSICR1);
424 clk_disable_unprepare(pcdev->clk);
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
431 static int mx1_camera_add_device(struct soc_camera_device *icd)
433 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
434 struct mx1_camera_dev *pcdev = ici->priv;
436 if (pcdev->icd)
437 return -EBUSY;
439 dev_info(icd->parent, "MX1 Camera driver attached to camera %d\n",
440 icd->devnum);
442 mx1_camera_activate(pcdev);
444 pcdev->icd = icd;
446 return 0;
449 static void mx1_camera_remove_device(struct soc_camera_device *icd)
451 struct soc_camera_host *ici = to_soc_camera_host(icd->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->parent, "MX1 Camera driver detached from camera %d\n",
465 icd->devnum);
467 mx1_camera_deactivate(pcdev);
469 pcdev->icd = NULL;
472 static int mx1_camera_set_bus_param(struct soc_camera_device *icd)
474 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
475 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
476 struct mx1_camera_dev *pcdev = ici->priv;
477 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
478 unsigned long common_flags;
479 unsigned int csicr1;
480 int ret;
482 /* MX1 supports only 8bit buswidth */
483 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
484 if (!ret) {
485 common_flags = soc_mbus_config_compatible(&cfg, CSI_BUS_FLAGS);
486 if (!common_flags) {
487 dev_warn(icd->parent,
488 "Flags incompatible: camera 0x%x, host 0x%x\n",
489 cfg.flags, CSI_BUS_FLAGS);
490 return -EINVAL;
492 } else if (ret != -ENOIOCTLCMD) {
493 return ret;
494 } else {
495 common_flags = CSI_BUS_FLAGS;
498 /* Make choises, based on platform choice */
499 if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
500 (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
501 if (!pcdev->pdata ||
502 pcdev->pdata->flags & MX1_CAMERA_VSYNC_HIGH)
503 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
504 else
505 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
508 if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
509 (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
510 if (!pcdev->pdata ||
511 pcdev->pdata->flags & MX1_CAMERA_PCLK_RISING)
512 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
513 else
514 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
517 if ((common_flags & V4L2_MBUS_DATA_ACTIVE_HIGH) &&
518 (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)) {
519 if (!pcdev->pdata ||
520 pcdev->pdata->flags & MX1_CAMERA_DATA_HIGH)
521 common_flags &= ~V4L2_MBUS_DATA_ACTIVE_LOW;
522 else
523 common_flags &= ~V4L2_MBUS_DATA_ACTIVE_HIGH;
526 cfg.flags = common_flags;
527 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
528 if (ret < 0 && ret != -ENOIOCTLCMD) {
529 dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n",
530 common_flags, ret);
531 return ret;
534 csicr1 = __raw_readl(pcdev->base + CSICR1);
536 if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
537 csicr1 |= CSICR1_REDGE;
538 if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
539 csicr1 |= CSICR1_SOF_POL;
540 if (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)
541 csicr1 |= CSICR1_DATA_POL;
543 __raw_writel(csicr1, pcdev->base + CSICR1);
545 return 0;
548 static int mx1_camera_set_fmt(struct soc_camera_device *icd,
549 struct v4l2_format *f)
551 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
552 const struct soc_camera_format_xlate *xlate;
553 struct v4l2_pix_format *pix = &f->fmt.pix;
554 struct v4l2_mbus_framefmt mf;
555 int ret, buswidth;
557 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
558 if (!xlate) {
559 dev_warn(icd->parent, "Format %x not found\n",
560 pix->pixelformat);
561 return -EINVAL;
564 buswidth = xlate->host_fmt->bits_per_sample;
565 if (buswidth > 8) {
566 dev_warn(icd->parent,
567 "bits-per-sample %d for format %x unsupported\n",
568 buswidth, pix->pixelformat);
569 return -EINVAL;
572 mf.width = pix->width;
573 mf.height = pix->height;
574 mf.field = pix->field;
575 mf.colorspace = pix->colorspace;
576 mf.code = xlate->code;
578 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
579 if (ret < 0)
580 return ret;
582 if (mf.code != xlate->code)
583 return -EINVAL;
585 pix->width = mf.width;
586 pix->height = mf.height;
587 pix->field = mf.field;
588 pix->colorspace = mf.colorspace;
589 icd->current_fmt = xlate;
591 return ret;
594 static int mx1_camera_try_fmt(struct soc_camera_device *icd,
595 struct v4l2_format *f)
597 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
598 const struct soc_camera_format_xlate *xlate;
599 struct v4l2_pix_format *pix = &f->fmt.pix;
600 struct v4l2_mbus_framefmt mf;
601 int ret;
602 /* TODO: limit to mx1 hardware capabilities */
604 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
605 if (!xlate) {
606 dev_warn(icd->parent, "Format %x not found\n",
607 pix->pixelformat);
608 return -EINVAL;
611 mf.width = pix->width;
612 mf.height = pix->height;
613 mf.field = pix->field;
614 mf.colorspace = pix->colorspace;
615 mf.code = xlate->code;
617 /* limit to sensor capabilities */
618 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
619 if (ret < 0)
620 return ret;
622 pix->width = mf.width;
623 pix->height = mf.height;
624 pix->field = mf.field;
625 pix->colorspace = mf.colorspace;
627 return 0;
630 static int mx1_camera_reqbufs(struct soc_camera_device *icd,
631 struct v4l2_requestbuffers *p)
633 int i;
636 * This is for locking debugging only. I removed spinlocks and now I
637 * check whether .prepare is ever called on a linked buffer, or whether
638 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
639 * it hadn't triggered
641 for (i = 0; i < p->count; i++) {
642 struct mx1_buffer *buf = container_of(icd->vb_vidq.bufs[i],
643 struct mx1_buffer, vb);
644 buf->inwork = 0;
645 INIT_LIST_HEAD(&buf->vb.queue);
648 return 0;
651 static unsigned int mx1_camera_poll(struct file *file, poll_table *pt)
653 struct soc_camera_device *icd = file->private_data;
654 struct mx1_buffer *buf;
656 buf = list_entry(icd->vb_vidq.stream.next, struct mx1_buffer,
657 vb.stream);
659 poll_wait(file, &buf->vb.done, pt);
661 if (buf->vb.state == VIDEOBUF_DONE ||
662 buf->vb.state == VIDEOBUF_ERROR)
663 return POLLIN | POLLRDNORM;
665 return 0;
668 static int mx1_camera_querycap(struct soc_camera_host *ici,
669 struct v4l2_capability *cap)
671 /* cap->name is set by the friendly caller:-> */
672 strlcpy(cap->card, "i.MX1/i.MXL Camera", sizeof(cap->card));
673 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
675 return 0;
678 static struct soc_camera_host_ops mx1_soc_camera_host_ops = {
679 .owner = THIS_MODULE,
680 .add = mx1_camera_add_device,
681 .remove = mx1_camera_remove_device,
682 .set_bus_param = mx1_camera_set_bus_param,
683 .set_fmt = mx1_camera_set_fmt,
684 .try_fmt = mx1_camera_try_fmt,
685 .init_videobuf = mx1_camera_init_videobuf,
686 .reqbufs = mx1_camera_reqbufs,
687 .poll = mx1_camera_poll,
688 .querycap = mx1_camera_querycap,
691 static struct fiq_handler fh = {
692 .name = "csi_sof"
695 static int __init mx1_camera_probe(struct platform_device *pdev)
697 struct mx1_camera_dev *pcdev;
698 struct resource *res;
699 struct pt_regs regs;
700 struct clk *clk;
701 void __iomem *base;
702 unsigned int irq;
703 int err = 0;
705 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
706 irq = platform_get_irq(pdev, 0);
707 if (!res || (int)irq <= 0) {
708 err = -ENODEV;
709 goto exit;
712 clk = clk_get(&pdev->dev, "csi_clk");
713 if (IS_ERR(clk)) {
714 err = PTR_ERR(clk);
715 goto exit;
718 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
719 if (!pcdev) {
720 dev_err(&pdev->dev, "Could not allocate pcdev\n");
721 err = -ENOMEM;
722 goto exit_put_clk;
725 pcdev->res = res;
726 pcdev->clk = clk;
728 pcdev->pdata = pdev->dev.platform_data;
730 if (pcdev->pdata)
731 pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
733 if (!pcdev->mclk) {
734 dev_warn(&pdev->dev,
735 "mclk_10khz == 0! Please, fix your platform data. "
736 "Using default 20MHz\n");
737 pcdev->mclk = 20000000;
740 INIT_LIST_HEAD(&pcdev->capture);
741 spin_lock_init(&pcdev->lock);
744 * Request the regions.
746 if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME)) {
747 err = -EBUSY;
748 goto exit_kfree;
751 base = ioremap(res->start, resource_size(res));
752 if (!base) {
753 err = -ENOMEM;
754 goto exit_release;
756 pcdev->irq = irq;
757 pcdev->base = base;
759 /* request dma */
760 pcdev->dma_chan = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_HIGH);
761 if (pcdev->dma_chan < 0) {
762 dev_err(&pdev->dev, "Can't request DMA for MX1 CSI\n");
763 err = -EBUSY;
764 goto exit_iounmap;
766 dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chan);
768 imx_dma_setup_handlers(pcdev->dma_chan, mx1_camera_dma_irq, NULL,
769 pcdev);
771 imx_dma_config_channel(pcdev->dma_chan, IMX_DMA_TYPE_FIFO,
772 IMX_DMA_MEMSIZE_32, MX1_DMA_REQ_CSI_R, 0);
773 /* burst length : 16 words = 64 bytes */
774 imx_dma_config_burstlen(pcdev->dma_chan, 0);
776 /* request irq */
777 err = claim_fiq(&fh);
778 if (err) {
779 dev_err(&pdev->dev, "Camera interrupt register failed\n");
780 goto exit_free_dma;
783 set_fiq_handler(&mx1_camera_sof_fiq_start, &mx1_camera_sof_fiq_end -
784 &mx1_camera_sof_fiq_start);
786 regs.ARM_r8 = (long)MX1_DMA_DIMR;
787 regs.ARM_r9 = (long)MX1_DMA_CCR(pcdev->dma_chan);
788 regs.ARM_r10 = (long)pcdev->base + CSICR1;
789 regs.ARM_fp = (long)pcdev->base + CSISR;
790 regs.ARM_sp = 1 << pcdev->dma_chan;
791 set_fiq_regs(&regs);
793 mxc_set_irq_fiq(irq, 1);
794 enable_fiq(irq);
796 pcdev->soc_host.drv_name = DRIVER_NAME;
797 pcdev->soc_host.ops = &mx1_soc_camera_host_ops;
798 pcdev->soc_host.priv = pcdev;
799 pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
800 pcdev->soc_host.nr = pdev->id;
801 err = soc_camera_host_register(&pcdev->soc_host);
802 if (err)
803 goto exit_free_irq;
805 dev_info(&pdev->dev, "MX1 Camera driver loaded\n");
807 return 0;
809 exit_free_irq:
810 disable_fiq(irq);
811 mxc_set_irq_fiq(irq, 0);
812 release_fiq(&fh);
813 exit_free_dma:
814 imx_dma_free(pcdev->dma_chan);
815 exit_iounmap:
816 iounmap(base);
817 exit_release:
818 release_mem_region(res->start, resource_size(res));
819 exit_kfree:
820 kfree(pcdev);
821 exit_put_clk:
822 clk_put(clk);
823 exit:
824 return err;
827 static int __exit mx1_camera_remove(struct platform_device *pdev)
829 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
830 struct mx1_camera_dev *pcdev = container_of(soc_host,
831 struct mx1_camera_dev, soc_host);
832 struct resource *res;
834 imx_dma_free(pcdev->dma_chan);
835 disable_fiq(pcdev->irq);
836 mxc_set_irq_fiq(pcdev->irq, 0);
837 release_fiq(&fh);
839 clk_put(pcdev->clk);
841 soc_camera_host_unregister(soc_host);
843 iounmap(pcdev->base);
845 res = pcdev->res;
846 release_mem_region(res->start, resource_size(res));
848 kfree(pcdev);
850 dev_info(&pdev->dev, "MX1 Camera driver unloaded\n");
852 return 0;
855 static struct platform_driver mx1_camera_driver = {
856 .driver = {
857 .name = DRIVER_NAME,
859 .remove = __exit_p(mx1_camera_remove),
862 module_platform_driver_probe(mx1_camera_driver, mx1_camera_probe);
864 MODULE_DESCRIPTION("i.MX1/i.MXL SoC Camera Host driver");
865 MODULE_AUTHOR("Paulius Zaleckas <paulius.zaleckas@teltonika.lt>");
866 MODULE_LICENSE("GPL v2");
867 MODULE_VERSION(DRIVER_VERSION);
868 MODULE_ALIAS("platform:" DRIVER_NAME);