omap_vout: fix compiler warning
[pandora-kernel.git] / drivers / media / video / 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/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>
35
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>
41
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>
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 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;
113
114         void __iomem                    *base;
115         int                             dma_chan;
116         unsigned int                    irq;
117         unsigned long                   mclk;
118
119         spinlock_t                      lock;
120 };
121
122 /*
123  *  Videobuf operations
124  */
125 static int mx1_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
126                               unsigned int *size)
127 {
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);
131
132         if (bytes_per_line < 0)
133                 return bytes_per_line;
134
135         *size = bytes_per_line * icd->user_height;
136
137         if (!*count)
138                 *count = 32;
139
140         if (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
141                 *count = (MAX_VIDEO_MEM * 1024 * 1024) / *size;
142
143         dev_dbg(icd->parent, "count=%d, size=%d\n", *count, *size);
144
145         return 0;
146 }
147
148 static void free_buffer(struct videobuf_queue *vq, struct mx1_buffer *buf)
149 {
150         struct soc_camera_device *icd = vq->priv_data;
151         struct videobuf_buffer *vb = &buf->vb;
152
153         BUG_ON(in_interrupt());
154
155         dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
156                 vb, vb->baddr, vb->bsize);
157
158         /*
159          * This waits until this buffer is out of danger, i.e., until it is no
160          * longer in STATE_QUEUED or STATE_ACTIVE
161          */
162         videobuf_waiton(vq, vb, 0, 0);
163         videobuf_dma_contig_free(vq, vb);
164
165         vb->state = VIDEOBUF_NEEDS_INIT;
166 }
167
168 static int mx1_videobuf_prepare(struct videobuf_queue *vq,
169                 struct videobuf_buffer *vb, enum v4l2_field field)
170 {
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);
176
177         if (bytes_per_line < 0)
178                 return bytes_per_line;
179
180         dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
181                 vb, vb->baddr, vb->bsize);
182
183         /* Added list head initialization on alloc */
184         WARN_ON(!list_empty(&vb->queue));
185
186         BUG_ON(NULL == icd->current_fmt);
187
188         /*
189          * I think, in buf_prepare you only have to protect global data,
190          * the actual buffer is yours
191          */
192         buf->inwork = 1;
193
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;
203         }
204
205         vb->size = bytes_per_line * vb->height;
206         if (0 != vb->baddr && vb->bsize < vb->size) {
207                 ret = -EINVAL;
208                 goto out;
209         }
210
211         if (vb->state == VIDEOBUF_NEEDS_INIT) {
212                 ret = videobuf_iolock(vq, vb, NULL);
213                 if (ret)
214                         goto fail;
215
216                 vb->state = VIDEOBUF_PREPARED;
217         }
218
219         buf->inwork = 0;
220
221         return 0;
222
223 fail:
224         free_buffer(vq, buf);
225 out:
226         buf->inwork = 0;
227         return ret;
228 }
229
230 static int mx1_camera_setup_dma(struct mx1_camera_dev *pcdev)
231 {
232         struct videobuf_buffer *vbuf = &pcdev->active->vb;
233         struct device *dev = pcdev->icd->parent;
234         int ret;
235
236         if (unlikely(!pcdev->active)) {
237                 dev_err(dev, "DMA End IRQ with no active buffer\n");
238                 return -EFAULT;
239         }
240
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");
248
249         return ret;
250 }
251
252 /* Called under spinlock_irqsave(&pcdev->lock, ...) */
253 static void mx1_videobuf_queue(struct videobuf_queue *vq,
254                                                 struct videobuf_buffer *vb)
255 {
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);
260
261         dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
262                 vb, vb->baddr, vb->bsize);
263
264         list_add_tail(&vb->queue, &pcdev->capture);
265
266         vb->state = VIDEOBUF_ACTIVE;
267
268         if (!pcdev->active) {
269                 pcdev->active = buf;
270
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);
278                 }
279         }
280 }
281
282 static void mx1_videobuf_release(struct videobuf_queue *vq,
283                                  struct videobuf_buffer *vb)
284 {
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;
289
290         dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
291                 vb, vb->baddr, vb->bsize);
292
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;
306         }
307 #endif
308
309         free_buffer(vq, buf);
310 }
311
312 static void mx1_camera_wakeup(struct mx1_camera_dev *pcdev,
313                               struct videobuf_buffer *vb,
314                               struct mx1_buffer *buf)
315 {
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);
322
323         if (list_empty(&pcdev->capture)) {
324                 pcdev->active = NULL;
325                 return;
326         }
327
328         pcdev->active = list_entry(pcdev->capture.next,
329                                    struct mx1_buffer, vb.queue);
330
331         /* setup sg list for future DMA */
332         if (likely(!mx1_camera_setup_dma(pcdev))) {
333                 unsigned int temp;
334
335                 /* enable SOF irq */
336                 temp = __raw_readl(pcdev->base + CSICR1) | CSICR1_SOF_INTEN;
337                 __raw_writel(temp, pcdev->base + CSICR1);
338         }
339 }
340
341 static void mx1_camera_dma_irq(int channel, void *data)
342 {
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;
348
349         spin_lock_irqsave(&pcdev->lock, flags);
350
351         imx_dma_disable(channel);
352
353         if (unlikely(!pcdev->active)) {
354                 dev_err(dev, "DMA End IRQ with no active buffer\n");
355                 goto out;
356         }
357
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);
363
364         mx1_camera_wakeup(pcdev, vb, buf);
365 out:
366         spin_unlock_irqrestore(&pcdev->lock, flags);
367 }
368
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,
374 };
375
376 static void mx1_camera_init_videobuf(struct videobuf_queue *q,
377                                      struct soc_camera_device *icd)
378 {
379         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
380         struct mx1_camera_dev *pcdev = ici->priv;
381
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);
386 }
387
388 static int mclk_get_divisor(struct mx1_camera_dev *pcdev)
389 {
390         unsigned int mclk = pcdev->mclk;
391         unsigned long div;
392         unsigned long lcdclk;
393
394         lcdclk = clk_get_rate(pcdev->clk);
395
396         /*
397          * We verify platform_mclk_10khz != 0, so if anyone breaks it, here
398          * they get a nice Oops
399          */
400         div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
401
402         dev_dbg(pcdev->icd->parent,
403                 "System clock %lukHz, target freq %dkHz, divisor %lu\n",
404                 lcdclk / 1000, mclk / 1000, div);
405
406         return div;
407 }
408
409 static void mx1_camera_activate(struct mx1_camera_dev *pcdev)
410 {
411         unsigned int csicr1 = CSICR1_EN;
412
413         dev_dbg(pcdev->icd->parent, "Activate device\n");
414
415         clk_enable(pcdev->clk);
416
417         /* enable CSI before doing anything else */
418         __raw_writel(csicr1, pcdev->base + CSICR1);
419
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 */
423
424         __raw_writel(csicr1, pcdev->base + CSICR1);
425 }
426
427 static void mx1_camera_deactivate(struct mx1_camera_dev *pcdev)
428 {
429         dev_dbg(pcdev->icd->parent, "Deactivate device\n");
430
431         /* Disable all CSI interface */
432         __raw_writel(0x00, pcdev->base + CSICR1);
433
434         clk_disable(pcdev->clk);
435 }
436
437 /*
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
440  */
441 static int mx1_camera_add_device(struct soc_camera_device *icd)
442 {
443         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
444         struct mx1_camera_dev *pcdev = ici->priv;
445
446         if (pcdev->icd)
447                 return -EBUSY;
448
449         dev_info(icd->parent, "MX1 Camera driver attached to camera %d\n",
450                  icd->devnum);
451
452         mx1_camera_activate(pcdev);
453
454         pcdev->icd = icd;
455
456         return 0;
457 }
458
459 static void mx1_camera_remove_device(struct soc_camera_device *icd)
460 {
461         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
462         struct mx1_camera_dev *pcdev = ici->priv;
463         unsigned int csicr1;
464
465         BUG_ON(icd != pcdev->icd);
466
467         /* disable interrupts */
468         csicr1 = __raw_readl(pcdev->base + CSICR1) & ~CSI_IRQ_MASK;
469         __raw_writel(csicr1, pcdev->base + CSICR1);
470
471         /* Stop DMA engine */
472         imx_dma_disable(pcdev->dma_chan);
473
474         dev_info(icd->parent, "MX1 Camera driver detached from camera %d\n",
475                  icd->devnum);
476
477         mx1_camera_deactivate(pcdev);
478
479         pcdev->icd = NULL;
480 }
481
482 static int mx1_camera_set_crop(struct soc_camera_device *icd,
483                                struct v4l2_crop *a)
484 {
485         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
486
487         return v4l2_subdev_call(sd, video, s_crop, a);
488 }
489
490 static int mx1_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
491 {
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;
499
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;
509                 }
510         } else if (ret != -ENOIOCTLCMD) {
511                 return ret;
512         } else {
513                 common_flags = CSI_BUS_FLAGS;
514         }
515
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;
524         }
525
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;
533         }
534
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;
542         }
543
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;
550         }
551
552         csicr1 = __raw_readl(pcdev->base + CSICR1);
553
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;
560
561         __raw_writel(csicr1, pcdev->base + CSICR1);
562
563         return 0;
564 }
565
566 static int mx1_camera_set_fmt(struct soc_camera_device *icd,
567                               struct v4l2_format *f)
568 {
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;
574
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;
580         }
581
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;
588         }
589
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;
595
596         ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
597         if (ret < 0)
598                 return ret;
599
600         if (mf.code != xlate->code)
601                 return -EINVAL;
602
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;
608
609         return ret;
610 }
611
612 static int mx1_camera_try_fmt(struct soc_camera_device *icd,
613                               struct v4l2_format *f)
614 {
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 */
621
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;
627         }
628
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;
634
635         /* limit to sensor capabilities */
636         ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
637         if (ret < 0)
638                 return ret;
639
640         pix->width      = mf.width;
641         pix->height     = mf.height;
642         pix->field      = mf.field;
643         pix->colorspace = mf.colorspace;
644
645         return 0;
646 }
647
648 static int mx1_camera_reqbufs(struct soc_camera_device *icd,
649                               struct v4l2_requestbuffers *p)
650 {
651         int i;
652
653         /*
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
658          */
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);
664         }
665
666         return 0;
667 }
668
669 static unsigned int mx1_camera_poll(struct file *file, poll_table *pt)
670 {
671         struct soc_camera_device *icd = file->private_data;
672         struct mx1_buffer *buf;
673
674         buf = list_entry(icd->vb_vidq.stream.next, struct mx1_buffer,
675                          vb.stream);
676
677         poll_wait(file, &buf->vb.done, pt);
678
679         if (buf->vb.state == VIDEOBUF_DONE ||
680             buf->vb.state == VIDEOBUF_ERROR)
681                 return POLLIN | POLLRDNORM;
682
683         return 0;
684 }
685
686 static int mx1_camera_querycap(struct soc_camera_host *ici,
687                                struct v4l2_capability *cap)
688 {
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;
692
693         return 0;
694 }
695
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,
708 };
709
710 static struct fiq_handler fh = {
711         .name           = "csi_sof"
712 };
713
714 static int __init mx1_camera_probe(struct platform_device *pdev)
715 {
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;
723
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;
729         }
730
731         clk = clk_get(&pdev->dev, "csi_clk");
732         if (IS_ERR(clk)) {
733                 err = PTR_ERR(clk);
734                 goto exit;
735         }
736
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;
742         }
743
744         pcdev->res = res;
745         pcdev->clk = clk;
746
747         pcdev->pdata = pdev->dev.platform_data;
748
749         if (pcdev->pdata)
750                 pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
751
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;
757         }
758
759         INIT_LIST_HEAD(&pcdev->capture);
760         spin_lock_init(&pcdev->lock);
761
762         /*
763          * Request the regions.
764          */
765         if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME)) {
766                 err = -EBUSY;
767                 goto exit_kfree;
768         }
769
770         base = ioremap(res->start, resource_size(res));
771         if (!base) {
772                 err = -ENOMEM;
773                 goto exit_release;
774         }
775         pcdev->irq = irq;
776         pcdev->base = base;
777
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;
784         }
785         dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chan);
786
787         imx_dma_setup_handlers(pcdev->dma_chan, mx1_camera_dma_irq, NULL,
788                                pcdev);
789
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);
794
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;
800         }
801
802         set_fiq_handler(&mx1_camera_sof_fiq_start, &mx1_camera_sof_fiq_end -
803                                                    &mx1_camera_sof_fiq_start);
804
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);
811
812         mxc_set_irq_fiq(irq, 1);
813         enable_fiq(irq);
814
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;
823
824         dev_info(&pdev->dev, "MX1 Camera driver loaded\n");
825
826         return 0;
827
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;
844 }
845
846 static int __exit mx1_camera_remove(struct platform_device *pdev)
847 {
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;
852
853         imx_dma_free(pcdev->dma_chan);
854         disable_fiq(pcdev->irq);
855         mxc_set_irq_fiq(pcdev->irq, 0);
856         release_fiq(&fh);
857
858         clk_put(pcdev->clk);
859
860         soc_camera_host_unregister(soc_host);
861
862         iounmap(pcdev->base);
863
864         res = pcdev->res;
865         release_mem_region(res->start, resource_size(res));
866
867         kfree(pcdev);
868
869         dev_info(&pdev->dev, "MX1 Camera driver unloaded\n");
870
871         return 0;
872 }
873
874 static struct platform_driver mx1_camera_driver = {
875         .driver         = {
876                 .name   = DRIVER_NAME,
877         },
878         .remove         = __exit_p(mx1_camera_remove),
879 };
880
881 static int __init mx1_camera_init(void)
882 {
883         return platform_driver_probe(&mx1_camera_driver, mx1_camera_probe);
884 }
885
886 static void __exit mx1_camera_exit(void)
887 {
888         return platform_driver_unregister(&mx1_camera_driver);
889 }
890
891 module_init(mx1_camera_init);
892 module_exit(mx1_camera_exit);
893
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);