Version:  2.0.40 2.2.26 2.4.37 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16

Linux/drivers/media/platform/soc_camera/mx1_camera.c

  1 /*
  2  * V4L2 Driver for i.MXL/i.MXL camera (CSI) host
  3  *
  4  * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
  5  * Copyright (C) 2009, Darius Augulis <augulis.darius@gmail.com>
  6  *
  7  * Based on PXA SoC camera driver
  8  * Copyright (C) 2006, Sascha Hauer, Pengutronix
  9  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
 10  *
 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.
 14  */
 15 
 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>
 34 
 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>
 40 
 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>
 47 
 48 /*
 49  * CSI registers
 50  */
 51 #define CSICR1          0x00                    /* CSI Control Register 1 */
 52 #define CSISR           0x08                    /* CSI Status Register */
 53 #define CSIRXR          0x10                    /* CSI RxFIFO Register */
 54 
 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)
 67 
 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)
 74 
 75 #define DRIVER_VERSION "0.0.2"
 76 #define DRIVER_NAME "mx1-camera"
 77 
 78 #define CSI_IRQ_MASK    (CSISR_SFF_OR_INT | CSISR_RFF_OR_INT | \
 79                         CSISR_STATFF_INT | CSISR_RXFF_INT | CSISR_SOF_INT)
 80 
 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)
 85 
 86 #define MAX_VIDEO_MEM 16        /* Video memory limit in megabytes */
 87 
 88 /*
 89  * Structures
 90  */
 91 
 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;
 98 };
 99 
100 /*
101  * i.MX1/i.MXL is only supposed to handle one camera on its Camera Sensor
102  * Interface. If anyone ever builds hardware to enable more than
103  * one camera, they will have to modify this driver too
104  */
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;
112 
113         void __iomem                    *base;
114         int                             dma_chan;
115         unsigned int                    irq;
116         unsigned long                   mclk;
117 
118         spinlock_t                      lock;
119 };
120 
121 /*
122  *  Videobuf operations
123  */
124 static int mx1_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
125                               unsigned int *size)
126 {
127         struct soc_camera_device *icd = vq->priv_data;
128 
129         *size = icd->sizeimage;
130 
131         if (!*count)
132                 *count = 32;
133 
134         if (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
135                 *count = (MAX_VIDEO_MEM * 1024 * 1024) / *size;
136 
137         dev_dbg(icd->parent, "count=%d, size=%d\n", *count, *size);
138 
139         return 0;
140 }
141 
142 static void free_buffer(struct videobuf_queue *vq, struct mx1_buffer *buf)
143 {
144         struct soc_camera_device *icd = vq->priv_data;
145         struct videobuf_buffer *vb = &buf->vb;
146 
147         BUG_ON(in_interrupt());
148 
149         dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
150                 vb, vb->baddr, vb->bsize);
151 
152         /*
153          * This waits until this buffer is out of danger, i.e., until it is no
154          * longer in STATE_QUEUED or STATE_ACTIVE
155          */
156         videobuf_waiton(vq, vb, 0, 0);
157         videobuf_dma_contig_free(vq, vb);
158 
159         vb->state = VIDEOBUF_NEEDS_INIT;
160 }
161 
162 static int mx1_videobuf_prepare(struct videobuf_queue *vq,
163                 struct videobuf_buffer *vb, enum v4l2_field field)
164 {
165         struct soc_camera_device *icd = vq->priv_data;
166         struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
167         int ret;
168 
169         dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
170                 vb, vb->baddr, vb->bsize);
171 
172         /* Added list head initialization on alloc */
173         WARN_ON(!list_empty(&vb->queue));
174 
175         BUG_ON(NULL == icd->current_fmt);
176 
177         /*
178          * I think, in buf_prepare you only have to protect global data,
179          * the actual buffer is yours
180          */
181         buf->inwork = 1;
182 
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;
192         }
193 
194         vb->size = icd->sizeimage;
195         if (0 != vb->baddr && vb->bsize < vb->size) {
196                 ret = -EINVAL;
197                 goto out;
198         }
199 
200         if (vb->state == VIDEOBUF_NEEDS_INIT) {
201                 ret = videobuf_iolock(vq, vb, NULL);
202                 if (ret)
203                         goto fail;
204 
205                 vb->state = VIDEOBUF_PREPARED;
206         }
207 
208         buf->inwork = 0;
209 
210         return 0;
211 
212 fail:
213         free_buffer(vq, buf);
214 out:
215         buf->inwork = 0;
216         return ret;
217 }
218 
219 static int mx1_camera_setup_dma(struct mx1_camera_dev *pcdev)
220 {
221         struct videobuf_buffer *vbuf = &pcdev->active->vb;
222         struct device *dev = pcdev->soc_host.icd->parent;
223         int ret;
224 
225         if (unlikely(!pcdev->active)) {
226                 dev_err(dev, "DMA End IRQ with no active buffer\n");
227                 return -EFAULT;
228         }
229 
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");
237 
238         return ret;
239 }
240 
241 /* Called under spinlock_irqsave(&pcdev->lock, ...) */
242 static void mx1_videobuf_queue(struct videobuf_queue *vq,
243                                                 struct videobuf_buffer *vb)
244 {
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);
249 
250         dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
251                 vb, vb->baddr, vb->bsize);
252 
253         list_add_tail(&vb->queue, &pcdev->capture);
254 
255         vb->state = VIDEOBUF_ACTIVE;
256 
257         if (!pcdev->active) {
258                 pcdev->active = buf;
259 
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);
267                 }
268         }
269 }
270 
271 static void mx1_videobuf_release(struct videobuf_queue *vq,
272                                  struct videobuf_buffer *vb)
273 {
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;
278 
279         dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
280                 vb, vb->baddr, vb->bsize);
281 
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;
295         }
296 #endif
297 
298         free_buffer(vq, buf);
299 }
300 
301 static void mx1_camera_wakeup(struct mx1_camera_dev *pcdev,
302                               struct videobuf_buffer *vb,
303                               struct mx1_buffer *buf)
304 {
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);
311 
312         if (list_empty(&pcdev->capture)) {
313                 pcdev->active = NULL;
314                 return;
315         }
316 
317         pcdev->active = list_entry(pcdev->capture.next,
318                                    struct mx1_buffer, vb.queue);
319 
320         /* setup sg list for future DMA */
321         if (likely(!mx1_camera_setup_dma(pcdev))) {
322                 unsigned int temp;
323 
324                 /* enable SOF irq */
325                 temp = __raw_readl(pcdev->base + CSICR1) | CSICR1_SOF_INTEN;
326                 __raw_writel(temp, pcdev->base + CSICR1);
327         }
328 }
329 
330 static void mx1_camera_dma_irq(int channel, void *data)
331 {
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;
337 
338         spin_lock_irqsave(&pcdev->lock, flags);
339 
340         imx_dma_disable(channel);
341 
342         if (unlikely(!pcdev->active)) {
343                 dev_err(dev, "DMA End IRQ with no active buffer\n");
344                 goto out;
345         }
346 
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);
352 
353         mx1_camera_wakeup(pcdev, vb, buf);
354 out:
355         spin_unlock_irqrestore(&pcdev->lock, flags);
356 }
357 
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,
363 };
364 
365 static void mx1_camera_init_videobuf(struct videobuf_queue *q,
366                                      struct soc_camera_device *icd)
367 {
368         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
369         struct mx1_camera_dev *pcdev = ici->priv;
370 
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);
375 }
376 
377 static int mclk_get_divisor(struct mx1_camera_dev *pcdev)
378 {
379         unsigned int mclk = pcdev->mclk;
380         unsigned long div;
381         unsigned long lcdclk;
382 
383         lcdclk = clk_get_rate(pcdev->clk);
384 
385         /*
386          * We verify platform_mclk_10khz != 0, so if anyone breaks it, here
387          * they get a nice Oops
388          */
389         div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
390 
391         dev_dbg(pcdev->soc_host.icd->parent,
392                 "System clock %lukHz, target freq %dkHz, divisor %lu\n",
393                 lcdclk / 1000, mclk / 1000, div);
394 
395         return div;
396 }
397 
398 static void mx1_camera_activate(struct mx1_camera_dev *pcdev)
399 {
400         unsigned int csicr1 = CSICR1_EN;
401 
402         dev_dbg(pcdev->soc_host.v4l2_dev.dev, "Activate device\n");
403 
404         clk_prepare_enable(pcdev->clk);
405 
406         /* enable CSI before doing anything else */
407         __raw_writel(csicr1, pcdev->base + CSICR1);
408 
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 */
412 
413         __raw_writel(csicr1, pcdev->base + CSICR1);
414 }
415 
416 static void mx1_camera_deactivate(struct mx1_camera_dev *pcdev)
417 {
418         dev_dbg(pcdev->soc_host.v4l2_dev.dev, "Deactivate device\n");
419 
420         /* Disable all CSI interface */
421         __raw_writel(0x00, pcdev->base + CSICR1);
422 
423         clk_disable_unprepare(pcdev->clk);
424 }
425 
426 static int mx1_camera_add_device(struct soc_camera_device *icd)
427 {
428         dev_info(icd->parent, "MX1 Camera driver attached to camera %d\n",
429                  icd->devnum);
430 
431         return 0;
432 }
433 
434 static void mx1_camera_remove_device(struct soc_camera_device *icd)
435 {
436         dev_info(icd->parent, "MX1 Camera driver detached from camera %d\n",
437                  icd->devnum);
438 }
439 
440 /*
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
443  */
444 static int mx1_camera_clock_start(struct soc_camera_host *ici)
445 {
446         struct mx1_camera_dev *pcdev = ici->priv;
447 
448         mx1_camera_activate(pcdev);
449 
450         return 0;
451 }
452 
453 static void mx1_camera_clock_stop(struct soc_camera_host *ici)
454 {
455         struct mx1_camera_dev *pcdev = ici->priv;
456         unsigned int csicr1;
457 
458         /* disable interrupts */
459         csicr1 = __raw_readl(pcdev->base + CSICR1) & ~CSI_IRQ_MASK;
460         __raw_writel(csicr1, pcdev->base + CSICR1);
461 
462         /* Stop DMA engine */
463         imx_dma_disable(pcdev->dma_chan);
464 
465         mx1_camera_deactivate(pcdev);
466 }
467 
468 static int mx1_camera_set_bus_param(struct soc_camera_device *icd)
469 {
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;
477 
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;
487                 }
488         } else if (ret != -ENOIOCTLCMD) {
489                 return ret;
490         } else {
491                 common_flags = CSI_BUS_FLAGS;
492         }
493 
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;
502         }
503 
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;
511         }
512 
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;
520         }
521 
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;
528         }
529 
530         csicr1 = __raw_readl(pcdev->base + CSICR1);
531 
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;
538 
539         __raw_writel(csicr1, pcdev->base + CSICR1);
540 
541         return 0;
542 }
543 
544 static int mx1_camera_set_fmt(struct soc_camera_device *icd,
545                               struct v4l2_format *f)
546 {
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;
552 
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;
558         }
559 
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;
566         }
567 
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;
573 
574         ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
575         if (ret < 0)
576                 return ret;
577 
578         if (mf.code != xlate->code)
579                 return -EINVAL;
580 
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;
586 
587         return ret;
588 }
589 
590 static int mx1_camera_try_fmt(struct soc_camera_device *icd,
591                               struct v4l2_format *f)
592 {
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 */
599 
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;
605         }
606 
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;
612 
613         /* limit to sensor capabilities */
614         ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
615         if (ret < 0)
616                 return ret;
617 
618         pix->width      = mf.width;
619         pix->height     = mf.height;
620         pix->field      = mf.field;
621         pix->colorspace = mf.colorspace;
622 
623         return 0;
624 }
625 
626 static int mx1_camera_reqbufs(struct soc_camera_device *icd,
627                               struct v4l2_requestbuffers *p)
628 {
629         int i;
630 
631         /*
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
636          */
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);
642         }
643 
644         return 0;
645 }
646 
647 static unsigned int mx1_camera_poll(struct file *file, poll_table *pt)
648 {
649         struct soc_camera_device *icd = file->private_data;
650         struct mx1_buffer *buf;
651 
652         buf = list_entry(icd->vb_vidq.stream.next, struct mx1_buffer,
653                          vb.stream);
654 
655         poll_wait(file, &buf->vb.done, pt);
656 
657         if (buf->vb.state == VIDEOBUF_DONE ||
658             buf->vb.state == VIDEOBUF_ERROR)
659                 return POLLIN | POLLRDNORM;
660 
661         return 0;
662 }
663 
664 static int mx1_camera_querycap(struct soc_camera_host *ici,
665                                struct v4l2_capability *cap)
666 {
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;
670 
671         return 0;
672 }
673 
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,
687 };
688 
689 static struct fiq_handler fh = {
690         .name           = "csi_sof"
691 };
692 
693 static int __init mx1_camera_probe(struct platform_device *pdev)
694 {
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;
702 
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;
708         }
709 
710         clk = clk_get(&pdev->dev, "csi_clk");
711         if (IS_ERR(clk)) {
712                 err = PTR_ERR(clk);
713                 goto exit;
714         }
715 
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;
721         }
722 
723         pcdev->res = res;
724         pcdev->clk = clk;
725 
726         pcdev->pdata = pdev->dev.platform_data;
727 
728         if (pcdev->pdata)
729                 pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
730 
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;
736         }
737 
738         INIT_LIST_HEAD(&pcdev->capture);
739         spin_lock_init(&pcdev->lock);
740 
741         /*
742          * Request the regions.
743          */
744         if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME)) {
745                 err = -EBUSY;
746                 goto exit_kfree;
747         }
748 
749         base = ioremap(res->start, resource_size(res));
750         if (!base) {
751                 err = -ENOMEM;
752                 goto exit_release;
753         }
754         pcdev->irq = irq;
755         pcdev->base = base;
756 
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;
763         }
764         dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chan);
765 
766         imx_dma_setup_handlers(pcdev->dma_chan, mx1_camera_dma_irq, NULL,
767                                pcdev);
768 
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);
773 
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;
779         }
780 
781         set_fiq_handler(&mx1_camera_sof_fiq_start, &mx1_camera_sof_fiq_end -
782                                                    &mx1_camera_sof_fiq_start);
783 
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);
790 
791         mxc_set_irq_fiq(irq, 1);
792         enable_fiq(irq);
793 
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;
802 
803         dev_info(&pdev->dev, "MX1 Camera driver loaded\n");
804 
805         return 0;
806 
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;
823 }
824 
825 static int __exit mx1_camera_remove(struct platform_device *pdev)
826 {
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;
831 
832         imx_dma_free(pcdev->dma_chan);
833         disable_fiq(pcdev->irq);
834         mxc_set_irq_fiq(pcdev->irq, 0);
835         release_fiq(&fh);
836 
837         clk_put(pcdev->clk);
838 
839         soc_camera_host_unregister(soc_host);
840 
841         iounmap(pcdev->base);
842 
843         res = pcdev->res;
844         release_mem_region(res->start, resource_size(res));
845 
846         kfree(pcdev);
847 
848         dev_info(&pdev->dev, "MX1 Camera driver unloaded\n");
849 
850         return 0;
851 }
852 
853 static struct platform_driver mx1_camera_driver = {
854         .driver         = {
855                 .name   = DRIVER_NAME,
856         },
857         .remove         = __exit_p(mx1_camera_remove),
858 };
859 
860 module_platform_driver_probe(mx1_camera_driver, mx1_camera_probe);
861 
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);
867 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us