Use dentry_path() to create full path to inode object
[pohmelfs.git] / drivers / media / video / mx1_camera.c
blob055d11ddb0388aa6f3f1b2bb7244427d730c849e
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/videodev2.h>
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>
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>
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;
129 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
130 icd->current_fmt->host_fmt);
132 if (bytes_per_line < 0)
133 return bytes_per_line;
135 *size = bytes_per_line * icd->user_height;
137 if (!*count)
138 *count = 32;
140 if (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
141 *count = (MAX_VIDEO_MEM * 1024 * 1024) / *size;
143 dev_dbg(icd->parent, "count=%d, size=%d\n", *count, *size);
145 return 0;
148 static void free_buffer(struct videobuf_queue *vq, struct mx1_buffer *buf)
150 struct soc_camera_device *icd = vq->priv_data;
151 struct videobuf_buffer *vb = &buf->vb;
153 BUG_ON(in_interrupt());
155 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
156 vb, vb->baddr, vb->bsize);
159 * This waits until this buffer is out of danger, i.e., until it is no
160 * longer in STATE_QUEUED or STATE_ACTIVE
162 videobuf_waiton(vq, vb, 0, 0);
163 videobuf_dma_contig_free(vq, vb);
165 vb->state = VIDEOBUF_NEEDS_INIT;
168 static int mx1_videobuf_prepare(struct videobuf_queue *vq,
169 struct videobuf_buffer *vb, enum v4l2_field field)
171 struct soc_camera_device *icd = vq->priv_data;
172 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
173 int ret;
174 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
175 icd->current_fmt->host_fmt);
177 if (bytes_per_line < 0)
178 return bytes_per_line;
180 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
181 vb, vb->baddr, vb->bsize);
183 /* Added list head initialization on alloc */
184 WARN_ON(!list_empty(&vb->queue));
186 BUG_ON(NULL == icd->current_fmt);
189 * I think, in buf_prepare you only have to protect global data,
190 * the actual buffer is yours
192 buf->inwork = 1;
194 if (buf->code != icd->current_fmt->code ||
195 vb->width != icd->user_width ||
196 vb->height != icd->user_height ||
197 vb->field != field) {
198 buf->code = icd->current_fmt->code;
199 vb->width = icd->user_width;
200 vb->height = icd->user_height;
201 vb->field = field;
202 vb->state = VIDEOBUF_NEEDS_INIT;
205 vb->size = bytes_per_line * vb->height;
206 if (0 != vb->baddr && vb->bsize < vb->size) {
207 ret = -EINVAL;
208 goto out;
211 if (vb->state == VIDEOBUF_NEEDS_INIT) {
212 ret = videobuf_iolock(vq, vb, NULL);
213 if (ret)
214 goto fail;
216 vb->state = VIDEOBUF_PREPARED;
219 buf->inwork = 0;
221 return 0;
223 fail:
224 free_buffer(vq, buf);
225 out:
226 buf->inwork = 0;
227 return ret;
230 static int mx1_camera_setup_dma(struct mx1_camera_dev *pcdev)
232 struct videobuf_buffer *vbuf = &pcdev->active->vb;
233 struct device *dev = pcdev->icd->parent;
234 int ret;
236 if (unlikely(!pcdev->active)) {
237 dev_err(dev, "DMA End IRQ with no active buffer\n");
238 return -EFAULT;
241 /* setup sg list for future DMA */
242 ret = imx_dma_setup_single(pcdev->dma_chan,
243 videobuf_to_dma_contig(vbuf),
244 vbuf->size, pcdev->res->start +
245 CSIRXR, DMA_MODE_READ);
246 if (unlikely(ret))
247 dev_err(dev, "Failed to setup DMA sg list\n");
249 return ret;
252 /* Called under spinlock_irqsave(&pcdev->lock, ...) */
253 static void mx1_videobuf_queue(struct videobuf_queue *vq,
254 struct videobuf_buffer *vb)
256 struct soc_camera_device *icd = vq->priv_data;
257 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
258 struct mx1_camera_dev *pcdev = ici->priv;
259 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
261 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
262 vb, vb->baddr, vb->bsize);
264 list_add_tail(&vb->queue, &pcdev->capture);
266 vb->state = VIDEOBUF_ACTIVE;
268 if (!pcdev->active) {
269 pcdev->active = buf;
271 /* setup sg list for future DMA */
272 if (!mx1_camera_setup_dma(pcdev)) {
273 unsigned int temp;
274 /* enable SOF irq */
275 temp = __raw_readl(pcdev->base + CSICR1) |
276 CSICR1_SOF_INTEN;
277 __raw_writel(temp, pcdev->base + CSICR1);
282 static void mx1_videobuf_release(struct videobuf_queue *vq,
283 struct videobuf_buffer *vb)
285 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
286 #ifdef DEBUG
287 struct soc_camera_device *icd = vq->priv_data;
288 struct device *dev = icd->parent;
290 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
291 vb, vb->baddr, vb->bsize);
293 switch (vb->state) {
294 case VIDEOBUF_ACTIVE:
295 dev_dbg(dev, "%s (active)\n", __func__);
296 break;
297 case VIDEOBUF_QUEUED:
298 dev_dbg(dev, "%s (queued)\n", __func__);
299 break;
300 case VIDEOBUF_PREPARED:
301 dev_dbg(dev, "%s (prepared)\n", __func__);
302 break;
303 default:
304 dev_dbg(dev, "%s (unknown)\n", __func__);
305 break;
307 #endif
309 free_buffer(vq, buf);
312 static void mx1_camera_wakeup(struct mx1_camera_dev *pcdev,
313 struct videobuf_buffer *vb,
314 struct mx1_buffer *buf)
316 /* _init is used to debug races, see comment in mx1_camera_reqbufs() */
317 list_del_init(&vb->queue);
318 vb->state = VIDEOBUF_DONE;
319 do_gettimeofday(&vb->ts);
320 vb->field_count++;
321 wake_up(&vb->done);
323 if (list_empty(&pcdev->capture)) {
324 pcdev->active = NULL;
325 return;
328 pcdev->active = list_entry(pcdev->capture.next,
329 struct mx1_buffer, vb.queue);
331 /* setup sg list for future DMA */
332 if (likely(!mx1_camera_setup_dma(pcdev))) {
333 unsigned int temp;
335 /* enable SOF irq */
336 temp = __raw_readl(pcdev->base + CSICR1) | CSICR1_SOF_INTEN;
337 __raw_writel(temp, pcdev->base + CSICR1);
341 static void mx1_camera_dma_irq(int channel, void *data)
343 struct mx1_camera_dev *pcdev = data;
344 struct device *dev = pcdev->icd->parent;
345 struct mx1_buffer *buf;
346 struct videobuf_buffer *vb;
347 unsigned long flags;
349 spin_lock_irqsave(&pcdev->lock, flags);
351 imx_dma_disable(channel);
353 if (unlikely(!pcdev->active)) {
354 dev_err(dev, "DMA End IRQ with no active buffer\n");
355 goto out;
358 vb = &pcdev->active->vb;
359 buf = container_of(vb, struct mx1_buffer, vb);
360 WARN_ON(buf->inwork || list_empty(&vb->queue));
361 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
362 vb, vb->baddr, vb->bsize);
364 mx1_camera_wakeup(pcdev, vb, buf);
365 out:
366 spin_unlock_irqrestore(&pcdev->lock, flags);
369 static struct videobuf_queue_ops mx1_videobuf_ops = {
370 .buf_setup = mx1_videobuf_setup,
371 .buf_prepare = mx1_videobuf_prepare,
372 .buf_queue = mx1_videobuf_queue,
373 .buf_release = mx1_videobuf_release,
376 static void mx1_camera_init_videobuf(struct videobuf_queue *q,
377 struct soc_camera_device *icd)
379 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
380 struct mx1_camera_dev *pcdev = ici->priv;
382 videobuf_queue_dma_contig_init(q, &mx1_videobuf_ops, icd->parent,
383 &pcdev->lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
384 V4L2_FIELD_NONE,
385 sizeof(struct mx1_buffer), icd, &icd->video_lock);
388 static int mclk_get_divisor(struct mx1_camera_dev *pcdev)
390 unsigned int mclk = pcdev->mclk;
391 unsigned long div;
392 unsigned long lcdclk;
394 lcdclk = clk_get_rate(pcdev->clk);
397 * We verify platform_mclk_10khz != 0, so if anyone breaks it, here
398 * they get a nice Oops
400 div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
402 dev_dbg(pcdev->icd->parent,
403 "System clock %lukHz, target freq %dkHz, divisor %lu\n",
404 lcdclk / 1000, mclk / 1000, div);
406 return div;
409 static void mx1_camera_activate(struct mx1_camera_dev *pcdev)
411 unsigned int csicr1 = CSICR1_EN;
413 dev_dbg(pcdev->icd->parent, "Activate device\n");
415 clk_enable(pcdev->clk);
417 /* enable CSI before doing anything else */
418 __raw_writel(csicr1, pcdev->base + CSICR1);
420 csicr1 |= CSICR1_MCLKEN | CSICR1_FCC | CSICR1_GCLK_MODE;
421 csicr1 |= CSICR1_MCLKDIV(mclk_get_divisor(pcdev));
422 csicr1 |= CSICR1_RXFF_LEVEL(2); /* 16 words */
424 __raw_writel(csicr1, pcdev->base + CSICR1);
427 static void mx1_camera_deactivate(struct mx1_camera_dev *pcdev)
429 dev_dbg(pcdev->icd->parent, "Deactivate device\n");
431 /* Disable all CSI interface */
432 __raw_writel(0x00, pcdev->base + CSICR1);
434 clk_disable(pcdev->clk);
438 * The following two functions absolutely depend on the fact, that
439 * there can be only one camera on i.MX1/i.MXL camera sensor interface
441 static int mx1_camera_add_device(struct soc_camera_device *icd)
443 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
444 struct mx1_camera_dev *pcdev = ici->priv;
446 if (pcdev->icd)
447 return -EBUSY;
449 dev_info(icd->parent, "MX1 Camera driver attached to camera %d\n",
450 icd->devnum);
452 mx1_camera_activate(pcdev);
454 pcdev->icd = icd;
456 return 0;
459 static void mx1_camera_remove_device(struct soc_camera_device *icd)
461 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
462 struct mx1_camera_dev *pcdev = ici->priv;
463 unsigned int csicr1;
465 BUG_ON(icd != pcdev->icd);
467 /* disable interrupts */
468 csicr1 = __raw_readl(pcdev->base + CSICR1) & ~CSI_IRQ_MASK;
469 __raw_writel(csicr1, pcdev->base + CSICR1);
471 /* Stop DMA engine */
472 imx_dma_disable(pcdev->dma_chan);
474 dev_info(icd->parent, "MX1 Camera driver detached from camera %d\n",
475 icd->devnum);
477 mx1_camera_deactivate(pcdev);
479 pcdev->icd = NULL;
482 static int mx1_camera_set_crop(struct soc_camera_device *icd,
483 struct v4l2_crop *a)
485 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
487 return v4l2_subdev_call(sd, video, s_crop, a);
490 static int mx1_camera_set_bus_param(struct soc_camera_device *icd)
492 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
493 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
494 struct mx1_camera_dev *pcdev = ici->priv;
495 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
496 unsigned long common_flags;
497 unsigned int csicr1;
498 int ret;
500 /* MX1 supports only 8bit buswidth */
501 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
502 if (!ret) {
503 common_flags = soc_mbus_config_compatible(&cfg, CSI_BUS_FLAGS);
504 if (!common_flags) {
505 dev_warn(icd->parent,
506 "Flags incompatible: camera 0x%x, host 0x%x\n",
507 cfg.flags, CSI_BUS_FLAGS);
508 return -EINVAL;
510 } else if (ret != -ENOIOCTLCMD) {
511 return ret;
512 } else {
513 common_flags = CSI_BUS_FLAGS;
516 /* Make choises, based on platform choice */
517 if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
518 (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
519 if (!pcdev->pdata ||
520 pcdev->pdata->flags & MX1_CAMERA_VSYNC_HIGH)
521 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
522 else
523 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
526 if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
527 (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
528 if (!pcdev->pdata ||
529 pcdev->pdata->flags & MX1_CAMERA_PCLK_RISING)
530 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
531 else
532 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
535 if ((common_flags & V4L2_MBUS_DATA_ACTIVE_HIGH) &&
536 (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)) {
537 if (!pcdev->pdata ||
538 pcdev->pdata->flags & MX1_CAMERA_DATA_HIGH)
539 common_flags &= ~V4L2_MBUS_DATA_ACTIVE_LOW;
540 else
541 common_flags &= ~V4L2_MBUS_DATA_ACTIVE_HIGH;
544 cfg.flags = common_flags;
545 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
546 if (ret < 0 && ret != -ENOIOCTLCMD) {
547 dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n",
548 common_flags, ret);
549 return ret;
552 csicr1 = __raw_readl(pcdev->base + CSICR1);
554 if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
555 csicr1 |= CSICR1_REDGE;
556 if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
557 csicr1 |= CSICR1_SOF_POL;
558 if (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)
559 csicr1 |= CSICR1_DATA_POL;
561 __raw_writel(csicr1, pcdev->base + CSICR1);
563 return 0;
566 static int mx1_camera_set_fmt(struct soc_camera_device *icd,
567 struct v4l2_format *f)
569 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
570 const struct soc_camera_format_xlate *xlate;
571 struct v4l2_pix_format *pix = &f->fmt.pix;
572 struct v4l2_mbus_framefmt mf;
573 int ret, buswidth;
575 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
576 if (!xlate) {
577 dev_warn(icd->parent, "Format %x not found\n",
578 pix->pixelformat);
579 return -EINVAL;
582 buswidth = xlate->host_fmt->bits_per_sample;
583 if (buswidth > 8) {
584 dev_warn(icd->parent,
585 "bits-per-sample %d for format %x unsupported\n",
586 buswidth, pix->pixelformat);
587 return -EINVAL;
590 mf.width = pix->width;
591 mf.height = pix->height;
592 mf.field = pix->field;
593 mf.colorspace = pix->colorspace;
594 mf.code = xlate->code;
596 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
597 if (ret < 0)
598 return ret;
600 if (mf.code != xlate->code)
601 return -EINVAL;
603 pix->width = mf.width;
604 pix->height = mf.height;
605 pix->field = mf.field;
606 pix->colorspace = mf.colorspace;
607 icd->current_fmt = xlate;
609 return ret;
612 static int mx1_camera_try_fmt(struct soc_camera_device *icd,
613 struct v4l2_format *f)
615 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
616 const struct soc_camera_format_xlate *xlate;
617 struct v4l2_pix_format *pix = &f->fmt.pix;
618 struct v4l2_mbus_framefmt mf;
619 int ret;
620 /* TODO: limit to mx1 hardware capabilities */
622 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
623 if (!xlate) {
624 dev_warn(icd->parent, "Format %x not found\n",
625 pix->pixelformat);
626 return -EINVAL;
629 mf.width = pix->width;
630 mf.height = pix->height;
631 mf.field = pix->field;
632 mf.colorspace = pix->colorspace;
633 mf.code = xlate->code;
635 /* limit to sensor capabilities */
636 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
637 if (ret < 0)
638 return ret;
640 pix->width = mf.width;
641 pix->height = mf.height;
642 pix->field = mf.field;
643 pix->colorspace = mf.colorspace;
645 return 0;
648 static int mx1_camera_reqbufs(struct soc_camera_device *icd,
649 struct v4l2_requestbuffers *p)
651 int i;
654 * This is for locking debugging only. I removed spinlocks and now I
655 * check whether .prepare is ever called on a linked buffer, or whether
656 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
657 * it hadn't triggered
659 for (i = 0; i < p->count; i++) {
660 struct mx1_buffer *buf = container_of(icd->vb_vidq.bufs[i],
661 struct mx1_buffer, vb);
662 buf->inwork = 0;
663 INIT_LIST_HEAD(&buf->vb.queue);
666 return 0;
669 static unsigned int mx1_camera_poll(struct file *file, poll_table *pt)
671 struct soc_camera_device *icd = file->private_data;
672 struct mx1_buffer *buf;
674 buf = list_entry(icd->vb_vidq.stream.next, struct mx1_buffer,
675 vb.stream);
677 poll_wait(file, &buf->vb.done, pt);
679 if (buf->vb.state == VIDEOBUF_DONE ||
680 buf->vb.state == VIDEOBUF_ERROR)
681 return POLLIN | POLLRDNORM;
683 return 0;
686 static int mx1_camera_querycap(struct soc_camera_host *ici,
687 struct v4l2_capability *cap)
689 /* cap->name is set by the friendly caller:-> */
690 strlcpy(cap->card, "i.MX1/i.MXL Camera", sizeof(cap->card));
691 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
693 return 0;
696 static struct soc_camera_host_ops mx1_soc_camera_host_ops = {
697 .owner = THIS_MODULE,
698 .add = mx1_camera_add_device,
699 .remove = mx1_camera_remove_device,
700 .set_bus_param = mx1_camera_set_bus_param,
701 .set_crop = mx1_camera_set_crop,
702 .set_fmt = mx1_camera_set_fmt,
703 .try_fmt = mx1_camera_try_fmt,
704 .init_videobuf = mx1_camera_init_videobuf,
705 .reqbufs = mx1_camera_reqbufs,
706 .poll = mx1_camera_poll,
707 .querycap = mx1_camera_querycap,
710 static struct fiq_handler fh = {
711 .name = "csi_sof"
714 static int __init mx1_camera_probe(struct platform_device *pdev)
716 struct mx1_camera_dev *pcdev;
717 struct resource *res;
718 struct pt_regs regs;
719 struct clk *clk;
720 void __iomem *base;
721 unsigned int irq;
722 int err = 0;
724 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
725 irq = platform_get_irq(pdev, 0);
726 if (!res || (int)irq <= 0) {
727 err = -ENODEV;
728 goto exit;
731 clk = clk_get(&pdev->dev, "csi_clk");
732 if (IS_ERR(clk)) {
733 err = PTR_ERR(clk);
734 goto exit;
737 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
738 if (!pcdev) {
739 dev_err(&pdev->dev, "Could not allocate pcdev\n");
740 err = -ENOMEM;
741 goto exit_put_clk;
744 pcdev->res = res;
745 pcdev->clk = clk;
747 pcdev->pdata = pdev->dev.platform_data;
749 if (pcdev->pdata)
750 pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
752 if (!pcdev->mclk) {
753 dev_warn(&pdev->dev,
754 "mclk_10khz == 0! Please, fix your platform data. "
755 "Using default 20MHz\n");
756 pcdev->mclk = 20000000;
759 INIT_LIST_HEAD(&pcdev->capture);
760 spin_lock_init(&pcdev->lock);
763 * Request the regions.
765 if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME)) {
766 err = -EBUSY;
767 goto exit_kfree;
770 base = ioremap(res->start, resource_size(res));
771 if (!base) {
772 err = -ENOMEM;
773 goto exit_release;
775 pcdev->irq = irq;
776 pcdev->base = base;
778 /* request dma */
779 pcdev->dma_chan = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_HIGH);
780 if (pcdev->dma_chan < 0) {
781 dev_err(&pdev->dev, "Can't request DMA for MX1 CSI\n");
782 err = -EBUSY;
783 goto exit_iounmap;
785 dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chan);
787 imx_dma_setup_handlers(pcdev->dma_chan, mx1_camera_dma_irq, NULL,
788 pcdev);
790 imx_dma_config_channel(pcdev->dma_chan, IMX_DMA_TYPE_FIFO,
791 IMX_DMA_MEMSIZE_32, MX1_DMA_REQ_CSI_R, 0);
792 /* burst length : 16 words = 64 bytes */
793 imx_dma_config_burstlen(pcdev->dma_chan, 0);
795 /* request irq */
796 err = claim_fiq(&fh);
797 if (err) {
798 dev_err(&pdev->dev, "Camera interrupt register failed \n");
799 goto exit_free_dma;
802 set_fiq_handler(&mx1_camera_sof_fiq_start, &mx1_camera_sof_fiq_end -
803 &mx1_camera_sof_fiq_start);
805 regs.ARM_r8 = (long)MX1_DMA_DIMR;
806 regs.ARM_r9 = (long)MX1_DMA_CCR(pcdev->dma_chan);
807 regs.ARM_r10 = (long)pcdev->base + CSICR1;
808 regs.ARM_fp = (long)pcdev->base + CSISR;
809 regs.ARM_sp = 1 << pcdev->dma_chan;
810 set_fiq_regs(&regs);
812 mxc_set_irq_fiq(irq, 1);
813 enable_fiq(irq);
815 pcdev->soc_host.drv_name = DRIVER_NAME;
816 pcdev->soc_host.ops = &mx1_soc_camera_host_ops;
817 pcdev->soc_host.priv = pcdev;
818 pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
819 pcdev->soc_host.nr = pdev->id;
820 err = soc_camera_host_register(&pcdev->soc_host);
821 if (err)
822 goto exit_free_irq;
824 dev_info(&pdev->dev, "MX1 Camera driver loaded\n");
826 return 0;
828 exit_free_irq:
829 disable_fiq(irq);
830 mxc_set_irq_fiq(irq, 0);
831 release_fiq(&fh);
832 exit_free_dma:
833 imx_dma_free(pcdev->dma_chan);
834 exit_iounmap:
835 iounmap(base);
836 exit_release:
837 release_mem_region(res->start, resource_size(res));
838 exit_kfree:
839 kfree(pcdev);
840 exit_put_clk:
841 clk_put(clk);
842 exit:
843 return err;
846 static int __exit mx1_camera_remove(struct platform_device *pdev)
848 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
849 struct mx1_camera_dev *pcdev = container_of(soc_host,
850 struct mx1_camera_dev, soc_host);
851 struct resource *res;
853 imx_dma_free(pcdev->dma_chan);
854 disable_fiq(pcdev->irq);
855 mxc_set_irq_fiq(pcdev->irq, 0);
856 release_fiq(&fh);
858 clk_put(pcdev->clk);
860 soc_camera_host_unregister(soc_host);
862 iounmap(pcdev->base);
864 res = pcdev->res;
865 release_mem_region(res->start, resource_size(res));
867 kfree(pcdev);
869 dev_info(&pdev->dev, "MX1 Camera driver unloaded\n");
871 return 0;
874 static struct platform_driver mx1_camera_driver = {
875 .driver = {
876 .name = DRIVER_NAME,
878 .remove = __exit_p(mx1_camera_remove),
881 static int __init mx1_camera_init(void)
883 return platform_driver_probe(&mx1_camera_driver, mx1_camera_probe);
886 static void __exit mx1_camera_exit(void)
888 return platform_driver_unregister(&mx1_camera_driver);
891 module_init(mx1_camera_init);
892 module_exit(mx1_camera_exit);
894 MODULE_DESCRIPTION("i.MX1/i.MXL SoC Camera Host driver");
895 MODULE_AUTHOR("Paulius Zaleckas <paulius.zaleckas@teltonika.lt>");
896 MODULE_LICENSE("GPL v2");
897 MODULE_VERSION(DRIVER_VERSION);
898 MODULE_ALIAS("platform:" DRIVER_NAME);