Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / drivers / media / video / mx2_camera.c
blobec2410c0c8069cddda1d19986d2235072bccbfbd
1 /*
2 * V4L2 Driver for i.MX27/i.MX25 camera host
4 * Copyright (C) 2008, Sascha Hauer, Pengutronix
5 * Copyright (C) 2010, Baruch Siach, Orex Computed Radiography
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/io.h>
16 #include <linux/delay.h>
17 #include <linux/slab.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/errno.h>
20 #include <linux/fs.h>
21 #include <linux/interrupt.h>
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/moduleparam.h>
25 #include <linux/time.h>
26 #include <linux/device.h>
27 #include <linux/platform_device.h>
28 #include <linux/mutex.h>
29 #include <linux/clk.h>
31 #include <media/v4l2-common.h>
32 #include <media/v4l2-dev.h>
33 #include <media/videobuf-core.h>
34 #include <media/videobuf-dma-contig.h>
35 #include <media/soc_camera.h>
36 #include <media/soc_mediabus.h>
38 #include <linux/videodev2.h>
40 #include <mach/mx2_cam.h>
41 #ifdef CONFIG_MACH_MX27
42 #include <mach/dma-mx1-mx2.h>
43 #endif
44 #include <mach/hardware.h>
46 #include <asm/dma.h>
48 #define MX2_CAM_DRV_NAME "mx2-camera"
49 #define MX2_CAM_VERSION "0.0.6"
50 #define MX2_CAM_DRIVER_DESCRIPTION "i.MX2x_Camera"
52 /* reset values */
53 #define CSICR1_RESET_VAL 0x40000800
54 #define CSICR2_RESET_VAL 0x0
55 #define CSICR3_RESET_VAL 0x0
57 /* csi control reg 1 */
58 #define CSICR1_SWAP16_EN (1 << 31)
59 #define CSICR1_EXT_VSYNC (1 << 30)
60 #define CSICR1_EOF_INTEN (1 << 29)
61 #define CSICR1_PRP_IF_EN (1 << 28)
62 #define CSICR1_CCIR_MODE (1 << 27)
63 #define CSICR1_COF_INTEN (1 << 26)
64 #define CSICR1_SF_OR_INTEN (1 << 25)
65 #define CSICR1_RF_OR_INTEN (1 << 24)
66 #define CSICR1_STATFF_LEVEL (3 << 22)
67 #define CSICR1_STATFF_INTEN (1 << 21)
68 #define CSICR1_RXFF_LEVEL(l) (((l) & 3) << 19) /* MX27 */
69 #define CSICR1_FB2_DMA_INTEN (1 << 20) /* MX25 */
70 #define CSICR1_FB1_DMA_INTEN (1 << 19) /* MX25 */
71 #define CSICR1_RXFF_INTEN (1 << 18)
72 #define CSICR1_SOF_POL (1 << 17)
73 #define CSICR1_SOF_INTEN (1 << 16)
74 #define CSICR1_MCLKDIV(d) (((d) & 0xF) << 12)
75 #define CSICR1_HSYNC_POL (1 << 11)
76 #define CSICR1_CCIR_EN (1 << 10)
77 #define CSICR1_MCLKEN (1 << 9)
78 #define CSICR1_FCC (1 << 8)
79 #define CSICR1_PACK_DIR (1 << 7)
80 #define CSICR1_CLR_STATFIFO (1 << 6)
81 #define CSICR1_CLR_RXFIFO (1 << 5)
82 #define CSICR1_GCLK_MODE (1 << 4)
83 #define CSICR1_INV_DATA (1 << 3)
84 #define CSICR1_INV_PCLK (1 << 2)
85 #define CSICR1_REDGE (1 << 1)
87 #define SHIFT_STATFF_LEVEL 22
88 #define SHIFT_RXFF_LEVEL 19
89 #define SHIFT_MCLKDIV 12
91 /* control reg 3 */
92 #define CSICR3_FRMCNT (0xFFFF << 16)
93 #define CSICR3_FRMCNT_RST (1 << 15)
94 #define CSICR3_DMA_REFLASH_RFF (1 << 14)
95 #define CSICR3_DMA_REFLASH_SFF (1 << 13)
96 #define CSICR3_DMA_REQ_EN_RFF (1 << 12)
97 #define CSICR3_DMA_REQ_EN_SFF (1 << 11)
98 #define CSICR3_RXFF_LEVEL(l) (((l) & 7) << 4) /* MX25 */
99 #define CSICR3_CSI_SUP (1 << 3)
100 #define CSICR3_ZERO_PACK_EN (1 << 2)
101 #define CSICR3_ECC_INT_EN (1 << 1)
102 #define CSICR3_ECC_AUTO_EN (1 << 0)
104 #define SHIFT_FRMCNT 16
106 /* csi status reg */
107 #define CSISR_SFF_OR_INT (1 << 25)
108 #define CSISR_RFF_OR_INT (1 << 24)
109 #define CSISR_STATFF_INT (1 << 21)
110 #define CSISR_DMA_TSF_FB2_INT (1 << 20) /* MX25 */
111 #define CSISR_DMA_TSF_FB1_INT (1 << 19) /* MX25 */
112 #define CSISR_RXFF_INT (1 << 18)
113 #define CSISR_EOF_INT (1 << 17)
114 #define CSISR_SOF_INT (1 << 16)
115 #define CSISR_F2_INT (1 << 15)
116 #define CSISR_F1_INT (1 << 14)
117 #define CSISR_COF_INT (1 << 13)
118 #define CSISR_ECC_INT (1 << 1)
119 #define CSISR_DRDY (1 << 0)
121 #define CSICR1 0x00
122 #define CSICR2 0x04
123 #define CSISR (cpu_is_mx27() ? 0x08 : 0x18)
124 #define CSISTATFIFO 0x0c
125 #define CSIRFIFO 0x10
126 #define CSIRXCNT 0x14
127 #define CSICR3 (cpu_is_mx27() ? 0x1C : 0x08)
128 #define CSIDMASA_STATFIFO 0x20
129 #define CSIDMATA_STATFIFO 0x24
130 #define CSIDMASA_FB1 0x28
131 #define CSIDMASA_FB2 0x2c
132 #define CSIFBUF_PARA 0x30
133 #define CSIIMAG_PARA 0x34
135 /* EMMA PrP */
136 #define PRP_CNTL 0x00
137 #define PRP_INTR_CNTL 0x04
138 #define PRP_INTRSTATUS 0x08
139 #define PRP_SOURCE_Y_PTR 0x0c
140 #define PRP_SOURCE_CB_PTR 0x10
141 #define PRP_SOURCE_CR_PTR 0x14
142 #define PRP_DEST_RGB1_PTR 0x18
143 #define PRP_DEST_RGB2_PTR 0x1c
144 #define PRP_DEST_Y_PTR 0x20
145 #define PRP_DEST_CB_PTR 0x24
146 #define PRP_DEST_CR_PTR 0x28
147 #define PRP_SRC_FRAME_SIZE 0x2c
148 #define PRP_DEST_CH1_LINE_STRIDE 0x30
149 #define PRP_SRC_PIXEL_FORMAT_CNTL 0x34
150 #define PRP_CH1_PIXEL_FORMAT_CNTL 0x38
151 #define PRP_CH1_OUT_IMAGE_SIZE 0x3c
152 #define PRP_CH2_OUT_IMAGE_SIZE 0x40
153 #define PRP_SRC_LINE_STRIDE 0x44
154 #define PRP_CSC_COEF_012 0x48
155 #define PRP_CSC_COEF_345 0x4c
156 #define PRP_CSC_COEF_678 0x50
157 #define PRP_CH1_RZ_HORI_COEF1 0x54
158 #define PRP_CH1_RZ_HORI_COEF2 0x58
159 #define PRP_CH1_RZ_HORI_VALID 0x5c
160 #define PRP_CH1_RZ_VERT_COEF1 0x60
161 #define PRP_CH1_RZ_VERT_COEF2 0x64
162 #define PRP_CH1_RZ_VERT_VALID 0x68
163 #define PRP_CH2_RZ_HORI_COEF1 0x6c
164 #define PRP_CH2_RZ_HORI_COEF2 0x70
165 #define PRP_CH2_RZ_HORI_VALID 0x74
166 #define PRP_CH2_RZ_VERT_COEF1 0x78
167 #define PRP_CH2_RZ_VERT_COEF2 0x7c
168 #define PRP_CH2_RZ_VERT_VALID 0x80
170 #define PRP_CNTL_CH1EN (1 << 0)
171 #define PRP_CNTL_CH2EN (1 << 1)
172 #define PRP_CNTL_CSIEN (1 << 2)
173 #define PRP_CNTL_DATA_IN_YUV420 (0 << 3)
174 #define PRP_CNTL_DATA_IN_YUV422 (1 << 3)
175 #define PRP_CNTL_DATA_IN_RGB16 (2 << 3)
176 #define PRP_CNTL_DATA_IN_RGB32 (3 << 3)
177 #define PRP_CNTL_CH1_OUT_RGB8 (0 << 5)
178 #define PRP_CNTL_CH1_OUT_RGB16 (1 << 5)
179 #define PRP_CNTL_CH1_OUT_RGB32 (2 << 5)
180 #define PRP_CNTL_CH1_OUT_YUV422 (3 << 5)
181 #define PRP_CNTL_CH2_OUT_YUV420 (0 << 7)
182 #define PRP_CNTL_CH2_OUT_YUV422 (1 << 7)
183 #define PRP_CNTL_CH2_OUT_YUV444 (2 << 7)
184 #define PRP_CNTL_CH1_LEN (1 << 9)
185 #define PRP_CNTL_CH2_LEN (1 << 10)
186 #define PRP_CNTL_SKIP_FRAME (1 << 11)
187 #define PRP_CNTL_SWRST (1 << 12)
188 #define PRP_CNTL_CLKEN (1 << 13)
189 #define PRP_CNTL_WEN (1 << 14)
190 #define PRP_CNTL_CH1BYP (1 << 15)
191 #define PRP_CNTL_IN_TSKIP(x) ((x) << 16)
192 #define PRP_CNTL_CH1_TSKIP(x) ((x) << 19)
193 #define PRP_CNTL_CH2_TSKIP(x) ((x) << 22)
194 #define PRP_CNTL_INPUT_FIFO_LEVEL(x) ((x) << 25)
195 #define PRP_CNTL_RZ_FIFO_LEVEL(x) ((x) << 27)
196 #define PRP_CNTL_CH2B1EN (1 << 29)
197 #define PRP_CNTL_CH2B2EN (1 << 30)
198 #define PRP_CNTL_CH2FEN (1 << 31)
200 /* IRQ Enable and status register */
201 #define PRP_INTR_RDERR (1 << 0)
202 #define PRP_INTR_CH1WERR (1 << 1)
203 #define PRP_INTR_CH2WERR (1 << 2)
204 #define PRP_INTR_CH1FC (1 << 3)
205 #define PRP_INTR_CH2FC (1 << 5)
206 #define PRP_INTR_LBOVF (1 << 7)
207 #define PRP_INTR_CH2OVF (1 << 8)
209 #define mx27_camera_emma(pcdev) (cpu_is_mx27() && pcdev->use_emma)
211 #define MAX_VIDEO_MEM 16
213 struct mx2_camera_dev {
214 struct device *dev;
215 struct soc_camera_host soc_host;
216 struct soc_camera_device *icd;
217 struct clk *clk_csi, *clk_emma;
219 unsigned int irq_csi, irq_emma;
220 void __iomem *base_csi, *base_emma;
221 unsigned long base_dma;
223 struct mx2_camera_platform_data *pdata;
224 struct resource *res_csi, *res_emma;
225 unsigned long platform_flags;
227 struct list_head capture;
228 struct list_head active_bufs;
230 spinlock_t lock;
232 int dma;
233 struct mx2_buffer *active;
234 struct mx2_buffer *fb1_active;
235 struct mx2_buffer *fb2_active;
237 int use_emma;
239 u32 csicr1;
241 void *discard_buffer;
242 dma_addr_t discard_buffer_dma;
243 size_t discard_size;
246 /* buffer for one video frame */
247 struct mx2_buffer {
248 /* common v4l buffer stuff -- must be first */
249 struct videobuf_buffer vb;
251 enum v4l2_mbus_pixelcode code;
253 int bufnum;
256 static void mx2_camera_deactivate(struct mx2_camera_dev *pcdev)
258 unsigned long flags;
260 clk_disable(pcdev->clk_csi);
261 writel(0, pcdev->base_csi + CSICR1);
262 if (mx27_camera_emma(pcdev)) {
263 writel(0, pcdev->base_emma + PRP_CNTL);
264 } else if (cpu_is_mx25()) {
265 spin_lock_irqsave(&pcdev->lock, flags);
266 pcdev->fb1_active = NULL;
267 pcdev->fb2_active = NULL;
268 writel(0, pcdev->base_csi + CSIDMASA_FB1);
269 writel(0, pcdev->base_csi + CSIDMASA_FB2);
270 spin_unlock_irqrestore(&pcdev->lock, flags);
275 * The following two functions absolutely depend on the fact, that
276 * there can be only one camera on mx2 camera sensor interface
278 static int mx2_camera_add_device(struct soc_camera_device *icd)
280 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
281 struct mx2_camera_dev *pcdev = ici->priv;
282 int ret;
283 u32 csicr1;
285 if (pcdev->icd)
286 return -EBUSY;
288 ret = clk_enable(pcdev->clk_csi);
289 if (ret < 0)
290 return ret;
292 csicr1 = CSICR1_MCLKEN;
294 if (mx27_camera_emma(pcdev)) {
295 csicr1 |= CSICR1_PRP_IF_EN | CSICR1_FCC |
296 CSICR1_RXFF_LEVEL(0);
297 } else if (cpu_is_mx27())
298 csicr1 |= CSICR1_SOF_INTEN | CSICR1_RXFF_LEVEL(2);
300 pcdev->csicr1 = csicr1;
301 writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
303 pcdev->icd = icd;
305 dev_info(icd->parent, "Camera driver attached to camera %d\n",
306 icd->devnum);
308 return 0;
311 static void mx2_camera_remove_device(struct soc_camera_device *icd)
313 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
314 struct mx2_camera_dev *pcdev = ici->priv;
316 BUG_ON(icd != pcdev->icd);
318 dev_info(icd->parent, "Camera driver detached from camera %d\n",
319 icd->devnum);
321 mx2_camera_deactivate(pcdev);
323 if (pcdev->discard_buffer) {
324 dma_free_coherent(ici->v4l2_dev.dev, pcdev->discard_size,
325 pcdev->discard_buffer,
326 pcdev->discard_buffer_dma);
327 pcdev->discard_buffer = NULL;
330 pcdev->icd = NULL;
333 #ifdef CONFIG_MACH_MX27
334 static void mx27_camera_dma_enable(struct mx2_camera_dev *pcdev)
336 u32 tmp;
338 imx_dma_enable(pcdev->dma);
340 tmp = readl(pcdev->base_csi + CSICR1);
341 tmp |= CSICR1_RF_OR_INTEN;
342 writel(tmp, pcdev->base_csi + CSICR1);
345 static irqreturn_t mx27_camera_irq(int irq_csi, void *data)
347 struct mx2_camera_dev *pcdev = data;
348 u32 status = readl(pcdev->base_csi + CSISR);
350 if (status & CSISR_SOF_INT && pcdev->active) {
351 u32 tmp;
353 tmp = readl(pcdev->base_csi + CSICR1);
354 writel(tmp | CSICR1_CLR_RXFIFO, pcdev->base_csi + CSICR1);
355 mx27_camera_dma_enable(pcdev);
358 writel(CSISR_SOF_INT | CSISR_RFF_OR_INT, pcdev->base_csi + CSISR);
360 return IRQ_HANDLED;
362 #else
363 static irqreturn_t mx27_camera_irq(int irq_csi, void *data)
365 return IRQ_NONE;
367 #endif /* CONFIG_MACH_MX27 */
369 static void mx25_camera_frame_done(struct mx2_camera_dev *pcdev, int fb,
370 int state)
372 struct videobuf_buffer *vb;
373 struct mx2_buffer *buf;
374 struct mx2_buffer **fb_active = fb == 1 ? &pcdev->fb1_active :
375 &pcdev->fb2_active;
376 u32 fb_reg = fb == 1 ? CSIDMASA_FB1 : CSIDMASA_FB2;
377 unsigned long flags;
379 spin_lock_irqsave(&pcdev->lock, flags);
381 if (*fb_active == NULL)
382 goto out;
384 vb = &(*fb_active)->vb;
385 dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
386 vb, vb->baddr, vb->bsize);
388 vb->state = state;
389 do_gettimeofday(&vb->ts);
390 vb->field_count++;
392 wake_up(&vb->done);
394 if (list_empty(&pcdev->capture)) {
395 buf = NULL;
396 writel(0, pcdev->base_csi + fb_reg);
397 } else {
398 buf = list_entry(pcdev->capture.next, struct mx2_buffer,
399 vb.queue);
400 vb = &buf->vb;
401 list_del(&vb->queue);
402 vb->state = VIDEOBUF_ACTIVE;
403 writel(videobuf_to_dma_contig(vb), pcdev->base_csi + fb_reg);
406 *fb_active = buf;
408 out:
409 spin_unlock_irqrestore(&pcdev->lock, flags);
412 static irqreturn_t mx25_camera_irq(int irq_csi, void *data)
414 struct mx2_camera_dev *pcdev = data;
415 u32 status = readl(pcdev->base_csi + CSISR);
417 if (status & CSISR_DMA_TSF_FB1_INT)
418 mx25_camera_frame_done(pcdev, 1, VIDEOBUF_DONE);
419 else if (status & CSISR_DMA_TSF_FB2_INT)
420 mx25_camera_frame_done(pcdev, 2, VIDEOBUF_DONE);
422 /* FIXME: handle CSISR_RFF_OR_INT */
424 writel(status, pcdev->base_csi + CSISR);
426 return IRQ_HANDLED;
430 * Videobuf operations
432 static int mx2_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
433 unsigned int *size)
435 struct soc_camera_device *icd = vq->priv_data;
436 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
437 icd->current_fmt->host_fmt);
439 dev_dbg(icd->parent, "count=%d, size=%d\n", *count, *size);
441 if (bytes_per_line < 0)
442 return bytes_per_line;
444 *size = bytes_per_line * icd->user_height;
446 if (0 == *count)
447 *count = 32;
448 if (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
449 *count = (MAX_VIDEO_MEM * 1024 * 1024) / *size;
451 return 0;
454 static void free_buffer(struct videobuf_queue *vq, struct mx2_buffer *buf)
456 struct soc_camera_device *icd = vq->priv_data;
457 struct videobuf_buffer *vb = &buf->vb;
459 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
460 vb, vb->baddr, vb->bsize);
463 * This waits until this buffer is out of danger, i.e., until it is no
464 * longer in state VIDEOBUF_QUEUED or VIDEOBUF_ACTIVE
466 videobuf_waiton(vq, vb, 0, 0);
468 videobuf_dma_contig_free(vq, vb);
469 dev_dbg(icd->parent, "%s freed\n", __func__);
471 vb->state = VIDEOBUF_NEEDS_INIT;
474 static int mx2_videobuf_prepare(struct videobuf_queue *vq,
475 struct videobuf_buffer *vb, enum v4l2_field field)
477 struct soc_camera_device *icd = vq->priv_data;
478 struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb);
479 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
480 icd->current_fmt->host_fmt);
481 int ret = 0;
483 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
484 vb, vb->baddr, vb->bsize);
486 if (bytes_per_line < 0)
487 return bytes_per_line;
489 #ifdef DEBUG
491 * This can be useful if you want to see if we actually fill
492 * the buffer with something
494 memset((void *)vb->baddr, 0xaa, vb->bsize);
495 #endif
497 if (buf->code != icd->current_fmt->code ||
498 vb->width != icd->user_width ||
499 vb->height != icd->user_height ||
500 vb->field != field) {
501 buf->code = icd->current_fmt->code;
502 vb->width = icd->user_width;
503 vb->height = icd->user_height;
504 vb->field = field;
505 vb->state = VIDEOBUF_NEEDS_INIT;
508 vb->size = bytes_per_line * vb->height;
509 if (vb->baddr && vb->bsize < vb->size) {
510 ret = -EINVAL;
511 goto out;
514 if (vb->state == VIDEOBUF_NEEDS_INIT) {
515 ret = videobuf_iolock(vq, vb, NULL);
516 if (ret)
517 goto fail;
519 vb->state = VIDEOBUF_PREPARED;
522 return 0;
524 fail:
525 free_buffer(vq, buf);
526 out:
527 return ret;
530 static void mx2_videobuf_queue(struct videobuf_queue *vq,
531 struct videobuf_buffer *vb)
533 struct soc_camera_device *icd = vq->priv_data;
534 struct soc_camera_host *ici =
535 to_soc_camera_host(icd->parent);
536 struct mx2_camera_dev *pcdev = ici->priv;
537 struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb);
538 unsigned long flags;
540 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
541 vb, vb->baddr, vb->bsize);
543 spin_lock_irqsave(&pcdev->lock, flags);
545 vb->state = VIDEOBUF_QUEUED;
546 list_add_tail(&vb->queue, &pcdev->capture);
548 if (mx27_camera_emma(pcdev)) {
549 goto out;
550 #ifdef CONFIG_MACH_MX27
551 } else if (cpu_is_mx27()) {
552 int ret;
554 if (pcdev->active == NULL) {
555 ret = imx_dma_setup_single(pcdev->dma,
556 videobuf_to_dma_contig(vb), vb->size,
557 (u32)pcdev->base_dma + 0x10,
558 DMA_MODE_READ);
559 if (ret) {
560 vb->state = VIDEOBUF_ERROR;
561 wake_up(&vb->done);
562 goto out;
565 vb->state = VIDEOBUF_ACTIVE;
566 pcdev->active = buf;
568 #endif
569 } else { /* cpu_is_mx25() */
570 u32 csicr3, dma_inten = 0;
572 if (pcdev->fb1_active == NULL) {
573 writel(videobuf_to_dma_contig(vb),
574 pcdev->base_csi + CSIDMASA_FB1);
575 pcdev->fb1_active = buf;
576 dma_inten = CSICR1_FB1_DMA_INTEN;
577 } else if (pcdev->fb2_active == NULL) {
578 writel(videobuf_to_dma_contig(vb),
579 pcdev->base_csi + CSIDMASA_FB2);
580 pcdev->fb2_active = buf;
581 dma_inten = CSICR1_FB2_DMA_INTEN;
584 if (dma_inten) {
585 list_del(&vb->queue);
586 vb->state = VIDEOBUF_ACTIVE;
588 csicr3 = readl(pcdev->base_csi + CSICR3);
590 /* Reflash DMA */
591 writel(csicr3 | CSICR3_DMA_REFLASH_RFF,
592 pcdev->base_csi + CSICR3);
594 /* clear & enable interrupts */
595 writel(dma_inten, pcdev->base_csi + CSISR);
596 pcdev->csicr1 |= dma_inten;
597 writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
599 /* enable DMA */
600 csicr3 |= CSICR3_DMA_REQ_EN_RFF | CSICR3_RXFF_LEVEL(1);
601 writel(csicr3, pcdev->base_csi + CSICR3);
605 out:
606 spin_unlock_irqrestore(&pcdev->lock, flags);
609 static void mx2_videobuf_release(struct videobuf_queue *vq,
610 struct videobuf_buffer *vb)
612 struct soc_camera_device *icd = vq->priv_data;
613 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
614 struct mx2_camera_dev *pcdev = ici->priv;
615 struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb);
616 unsigned long flags;
618 #ifdef DEBUG
619 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
620 vb, vb->baddr, vb->bsize);
622 switch (vb->state) {
623 case VIDEOBUF_ACTIVE:
624 dev_info(icd->parent, "%s (active)\n", __func__);
625 break;
626 case VIDEOBUF_QUEUED:
627 dev_info(icd->parent, "%s (queued)\n", __func__);
628 break;
629 case VIDEOBUF_PREPARED:
630 dev_info(icd->parent, "%s (prepared)\n", __func__);
631 break;
632 default:
633 dev_info(icd->parent, "%s (unknown) %d\n", __func__,
634 vb->state);
635 break;
637 #endif
640 * Terminate only queued but inactive buffers. Active buffers are
641 * released when they become inactive after videobuf_waiton().
643 * FIXME: implement forced termination of active buffers for mx27 and
644 * mx27 eMMA, so that the user won't get stuck in an uninterruptible
645 * state. This requires a specific handling for each of the these DMA
646 * types.
648 spin_lock_irqsave(&pcdev->lock, flags);
649 if (vb->state == VIDEOBUF_QUEUED) {
650 list_del(&vb->queue);
651 vb->state = VIDEOBUF_ERROR;
652 } else if (cpu_is_mx25() && vb->state == VIDEOBUF_ACTIVE) {
653 if (pcdev->fb1_active == buf) {
654 pcdev->csicr1 &= ~CSICR1_FB1_DMA_INTEN;
655 writel(0, pcdev->base_csi + CSIDMASA_FB1);
656 pcdev->fb1_active = NULL;
657 } else if (pcdev->fb2_active == buf) {
658 pcdev->csicr1 &= ~CSICR1_FB2_DMA_INTEN;
659 writel(0, pcdev->base_csi + CSIDMASA_FB2);
660 pcdev->fb2_active = NULL;
662 writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
663 vb->state = VIDEOBUF_ERROR;
665 spin_unlock_irqrestore(&pcdev->lock, flags);
667 free_buffer(vq, buf);
670 static struct videobuf_queue_ops mx2_videobuf_ops = {
671 .buf_setup = mx2_videobuf_setup,
672 .buf_prepare = mx2_videobuf_prepare,
673 .buf_queue = mx2_videobuf_queue,
674 .buf_release = mx2_videobuf_release,
677 static void mx2_camera_init_videobuf(struct videobuf_queue *q,
678 struct soc_camera_device *icd)
680 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
681 struct mx2_camera_dev *pcdev = ici->priv;
683 videobuf_queue_dma_contig_init(q, &mx2_videobuf_ops, pcdev->dev,
684 &pcdev->lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
685 V4L2_FIELD_NONE, sizeof(struct mx2_buffer),
686 icd, &icd->video_lock);
689 #define MX2_BUS_FLAGS (SOCAM_DATAWIDTH_8 | \
690 SOCAM_MASTER | \
691 SOCAM_VSYNC_ACTIVE_HIGH | \
692 SOCAM_VSYNC_ACTIVE_LOW | \
693 SOCAM_HSYNC_ACTIVE_HIGH | \
694 SOCAM_HSYNC_ACTIVE_LOW | \
695 SOCAM_PCLK_SAMPLE_RISING | \
696 SOCAM_PCLK_SAMPLE_FALLING | \
697 SOCAM_DATA_ACTIVE_HIGH | \
698 SOCAM_DATA_ACTIVE_LOW)
700 static int mx27_camera_emma_prp_reset(struct mx2_camera_dev *pcdev)
702 u32 cntl;
703 int count = 0;
705 cntl = readl(pcdev->base_emma + PRP_CNTL);
706 writel(PRP_CNTL_SWRST, pcdev->base_emma + PRP_CNTL);
707 while (count++ < 100) {
708 if (!(readl(pcdev->base_emma + PRP_CNTL) & PRP_CNTL_SWRST))
709 return 0;
710 barrier();
711 udelay(1);
714 return -ETIMEDOUT;
717 static void mx27_camera_emma_buf_init(struct soc_camera_device *icd,
718 int bytesperline)
720 struct soc_camera_host *ici =
721 to_soc_camera_host(icd->parent);
722 struct mx2_camera_dev *pcdev = ici->priv;
724 writel(pcdev->discard_buffer_dma,
725 pcdev->base_emma + PRP_DEST_RGB1_PTR);
726 writel(pcdev->discard_buffer_dma,
727 pcdev->base_emma + PRP_DEST_RGB2_PTR);
730 * We only use the EMMA engine to get rid of the broken
731 * DMA Engine. No color space consversion at the moment.
732 * We set the incomming and outgoing pixelformat to an
733 * 16 Bit wide format and adjust the bytesperline
734 * accordingly. With this configuration the inputdata
735 * will not be changed by the emma and could be any type
736 * of 16 Bit Pixelformat.
738 writel(PRP_CNTL_CH1EN |
739 PRP_CNTL_CSIEN |
740 PRP_CNTL_DATA_IN_RGB16 |
741 PRP_CNTL_CH1_OUT_RGB16 |
742 PRP_CNTL_CH1_LEN |
743 PRP_CNTL_CH1BYP |
744 PRP_CNTL_CH1_TSKIP(0) |
745 PRP_CNTL_IN_TSKIP(0),
746 pcdev->base_emma + PRP_CNTL);
748 writel(((bytesperline >> 1) << 16) | icd->user_height,
749 pcdev->base_emma + PRP_SRC_FRAME_SIZE);
750 writel(((bytesperline >> 1) << 16) | icd->user_height,
751 pcdev->base_emma + PRP_CH1_OUT_IMAGE_SIZE);
752 writel(bytesperline,
753 pcdev->base_emma + PRP_DEST_CH1_LINE_STRIDE);
754 writel(0x2ca00565, /* RGB565 */
755 pcdev->base_emma + PRP_SRC_PIXEL_FORMAT_CNTL);
756 writel(0x2ca00565, /* RGB565 */
757 pcdev->base_emma + PRP_CH1_PIXEL_FORMAT_CNTL);
759 /* Enable interrupts */
760 writel(PRP_INTR_RDERR |
761 PRP_INTR_CH1WERR |
762 PRP_INTR_CH2WERR |
763 PRP_INTR_CH1FC |
764 PRP_INTR_CH2FC |
765 PRP_INTR_LBOVF |
766 PRP_INTR_CH2OVF,
767 pcdev->base_emma + PRP_INTR_CNTL);
770 static int mx2_camera_set_bus_param(struct soc_camera_device *icd,
771 __u32 pixfmt)
773 struct soc_camera_host *ici =
774 to_soc_camera_host(icd->parent);
775 struct mx2_camera_dev *pcdev = ici->priv;
776 unsigned long camera_flags, common_flags;
777 int ret = 0;
778 int bytesperline;
779 u32 csicr1 = pcdev->csicr1;
781 camera_flags = icd->ops->query_bus_param(icd);
783 common_flags = soc_camera_bus_param_compatible(camera_flags,
784 MX2_BUS_FLAGS);
785 if (!common_flags)
786 return -EINVAL;
788 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
789 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
790 if (pcdev->platform_flags & MX2_CAMERA_HSYNC_HIGH)
791 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
792 else
793 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
796 if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
797 (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
798 if (pcdev->platform_flags & MX2_CAMERA_PCLK_SAMPLE_RISING)
799 common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
800 else
801 common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
804 ret = icd->ops->set_bus_param(icd, common_flags);
805 if (ret < 0)
806 return ret;
808 if (common_flags & SOCAM_PCLK_SAMPLE_RISING)
809 csicr1 |= CSICR1_REDGE;
810 if (common_flags & SOCAM_VSYNC_ACTIVE_HIGH)
811 csicr1 |= CSICR1_SOF_POL;
812 if (common_flags & SOCAM_HSYNC_ACTIVE_HIGH)
813 csicr1 |= CSICR1_HSYNC_POL;
814 if (pcdev->platform_flags & MX2_CAMERA_SWAP16)
815 csicr1 |= CSICR1_SWAP16_EN;
816 if (pcdev->platform_flags & MX2_CAMERA_EXT_VSYNC)
817 csicr1 |= CSICR1_EXT_VSYNC;
818 if (pcdev->platform_flags & MX2_CAMERA_CCIR)
819 csicr1 |= CSICR1_CCIR_EN;
820 if (pcdev->platform_flags & MX2_CAMERA_CCIR_INTERLACE)
821 csicr1 |= CSICR1_CCIR_MODE;
822 if (pcdev->platform_flags & MX2_CAMERA_GATED_CLOCK)
823 csicr1 |= CSICR1_GCLK_MODE;
824 if (pcdev->platform_flags & MX2_CAMERA_INV_DATA)
825 csicr1 |= CSICR1_INV_DATA;
826 if (pcdev->platform_flags & MX2_CAMERA_PACK_DIR_MSB)
827 csicr1 |= CSICR1_PACK_DIR;
829 pcdev->csicr1 = csicr1;
831 bytesperline = soc_mbus_bytes_per_line(icd->user_width,
832 icd->current_fmt->host_fmt);
833 if (bytesperline < 0)
834 return bytesperline;
836 if (mx27_camera_emma(pcdev)) {
837 ret = mx27_camera_emma_prp_reset(pcdev);
838 if (ret)
839 return ret;
841 if (pcdev->discard_buffer)
842 dma_free_coherent(ici->v4l2_dev.dev,
843 pcdev->discard_size, pcdev->discard_buffer,
844 pcdev->discard_buffer_dma);
847 * I didn't manage to properly enable/disable the prp
848 * on a per frame basis during running transfers,
849 * thus we allocate a buffer here and use it to
850 * discard frames when no buffer is available.
851 * Feel free to work on this ;)
853 pcdev->discard_size = icd->user_height * bytesperline;
854 pcdev->discard_buffer = dma_alloc_coherent(ici->v4l2_dev.dev,
855 pcdev->discard_size, &pcdev->discard_buffer_dma,
856 GFP_KERNEL);
857 if (!pcdev->discard_buffer)
858 return -ENOMEM;
860 mx27_camera_emma_buf_init(icd, bytesperline);
861 } else if (cpu_is_mx25()) {
862 writel((bytesperline * icd->user_height) >> 2,
863 pcdev->base_csi + CSIRXCNT);
864 writel((bytesperline << 16) | icd->user_height,
865 pcdev->base_csi + CSIIMAG_PARA);
868 writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
870 return 0;
873 static int mx2_camera_set_crop(struct soc_camera_device *icd,
874 struct v4l2_crop *a)
876 struct v4l2_rect *rect = &a->c;
877 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
878 struct v4l2_mbus_framefmt mf;
879 int ret;
881 soc_camera_limit_side(&rect->left, &rect->width, 0, 2, 4096);
882 soc_camera_limit_side(&rect->top, &rect->height, 0, 2, 4096);
884 ret = v4l2_subdev_call(sd, video, s_crop, a);
885 if (ret < 0)
886 return ret;
888 /* The capture device might have changed its output */
889 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
890 if (ret < 0)
891 return ret;
893 dev_dbg(icd->parent, "Sensor cropped %dx%d\n",
894 mf.width, mf.height);
896 icd->user_width = mf.width;
897 icd->user_height = mf.height;
899 return ret;
902 static int mx2_camera_set_fmt(struct soc_camera_device *icd,
903 struct v4l2_format *f)
905 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
906 const struct soc_camera_format_xlate *xlate;
907 struct v4l2_pix_format *pix = &f->fmt.pix;
908 struct v4l2_mbus_framefmt mf;
909 int ret;
911 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
912 if (!xlate) {
913 dev_warn(icd->parent, "Format %x not found\n",
914 pix->pixelformat);
915 return -EINVAL;
918 mf.width = pix->width;
919 mf.height = pix->height;
920 mf.field = pix->field;
921 mf.colorspace = pix->colorspace;
922 mf.code = xlate->code;
924 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
925 if (ret < 0 && ret != -ENOIOCTLCMD)
926 return ret;
928 if (mf.code != xlate->code)
929 return -EINVAL;
931 pix->width = mf.width;
932 pix->height = mf.height;
933 pix->field = mf.field;
934 pix->colorspace = mf.colorspace;
935 icd->current_fmt = xlate;
937 return 0;
940 static int mx2_camera_try_fmt(struct soc_camera_device *icd,
941 struct v4l2_format *f)
943 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
944 const struct soc_camera_format_xlate *xlate;
945 struct v4l2_pix_format *pix = &f->fmt.pix;
946 struct v4l2_mbus_framefmt mf;
947 __u32 pixfmt = pix->pixelformat;
948 unsigned int width_limit;
949 int ret;
951 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
952 if (pixfmt && !xlate) {
953 dev_warn(icd->parent, "Format %x not found\n", pixfmt);
954 return -EINVAL;
957 /* FIXME: implement MX27 limits */
959 /* limit to MX25 hardware capabilities */
960 if (cpu_is_mx25()) {
961 if (xlate->host_fmt->bits_per_sample <= 8)
962 width_limit = 0xffff * 4;
963 else
964 width_limit = 0xffff * 2;
965 /* CSIIMAG_PARA limit */
966 if (pix->width > width_limit)
967 pix->width = width_limit;
968 if (pix->height > 0xffff)
969 pix->height = 0xffff;
971 pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
972 xlate->host_fmt);
973 if (pix->bytesperline < 0)
974 return pix->bytesperline;
975 pix->sizeimage = pix->height * pix->bytesperline;
976 /* Check against the CSIRXCNT limit */
977 if (pix->sizeimage > 4 * 0x3ffff) {
978 /* Adjust geometry, preserve aspect ratio */
979 unsigned int new_height = int_sqrt(4 * 0x3ffff *
980 pix->height / pix->bytesperline);
981 pix->width = new_height * pix->width / pix->height;
982 pix->height = new_height;
983 pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
984 xlate->host_fmt);
985 BUG_ON(pix->bytesperline < 0);
989 /* limit to sensor capabilities */
990 mf.width = pix->width;
991 mf.height = pix->height;
992 mf.field = pix->field;
993 mf.colorspace = pix->colorspace;
994 mf.code = xlate->code;
996 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
997 if (ret < 0)
998 return ret;
1000 if (mf.field == V4L2_FIELD_ANY)
1001 mf.field = V4L2_FIELD_NONE;
1002 if (mf.field != V4L2_FIELD_NONE) {
1003 dev_err(icd->parent, "Field type %d unsupported.\n",
1004 mf.field);
1005 return -EINVAL;
1008 pix->width = mf.width;
1009 pix->height = mf.height;
1010 pix->field = mf.field;
1011 pix->colorspace = mf.colorspace;
1013 return 0;
1016 static int mx2_camera_querycap(struct soc_camera_host *ici,
1017 struct v4l2_capability *cap)
1019 /* cap->name is set by the friendly caller:-> */
1020 strlcpy(cap->card, MX2_CAM_DRIVER_DESCRIPTION, sizeof(cap->card));
1021 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1023 return 0;
1026 static int mx2_camera_reqbufs(struct soc_camera_device *icd,
1027 struct v4l2_requestbuffers *p)
1029 int i;
1031 for (i = 0; i < p->count; i++) {
1032 struct mx2_buffer *buf = container_of(icd->vb_vidq.bufs[i],
1033 struct mx2_buffer, vb);
1034 INIT_LIST_HEAD(&buf->vb.queue);
1037 return 0;
1040 #ifdef CONFIG_MACH_MX27
1041 static void mx27_camera_frame_done(struct mx2_camera_dev *pcdev, int state)
1043 struct videobuf_buffer *vb;
1044 struct mx2_buffer *buf;
1045 unsigned long flags;
1046 int ret;
1048 spin_lock_irqsave(&pcdev->lock, flags);
1050 if (!pcdev->active) {
1051 dev_err(pcdev->dev, "%s called with no active buffer!\n",
1052 __func__);
1053 goto out;
1056 vb = &pcdev->active->vb;
1057 buf = container_of(vb, struct mx2_buffer, vb);
1058 WARN_ON(list_empty(&vb->queue));
1059 dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
1060 vb, vb->baddr, vb->bsize);
1062 /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
1063 list_del_init(&vb->queue);
1064 vb->state = state;
1065 do_gettimeofday(&vb->ts);
1066 vb->field_count++;
1068 wake_up(&vb->done);
1070 if (list_empty(&pcdev->capture)) {
1071 pcdev->active = NULL;
1072 goto out;
1075 pcdev->active = list_entry(pcdev->capture.next,
1076 struct mx2_buffer, vb.queue);
1078 vb = &pcdev->active->vb;
1079 vb->state = VIDEOBUF_ACTIVE;
1081 ret = imx_dma_setup_single(pcdev->dma, videobuf_to_dma_contig(vb),
1082 vb->size, (u32)pcdev->base_dma + 0x10, DMA_MODE_READ);
1084 if (ret) {
1085 vb->state = VIDEOBUF_ERROR;
1086 pcdev->active = NULL;
1087 wake_up(&vb->done);
1090 out:
1091 spin_unlock_irqrestore(&pcdev->lock, flags);
1094 static void mx27_camera_dma_err_callback(int channel, void *data, int err)
1096 struct mx2_camera_dev *pcdev = data;
1098 mx27_camera_frame_done(pcdev, VIDEOBUF_ERROR);
1101 static void mx27_camera_dma_callback(int channel, void *data)
1103 struct mx2_camera_dev *pcdev = data;
1105 mx27_camera_frame_done(pcdev, VIDEOBUF_DONE);
1108 #define DMA_REQ_CSI_RX 31 /* FIXME: Add this to a resource */
1110 static int __devinit mx27_camera_dma_init(struct platform_device *pdev,
1111 struct mx2_camera_dev *pcdev)
1113 int err;
1115 pcdev->dma = imx_dma_request_by_prio("CSI RX DMA", DMA_PRIO_HIGH);
1116 if (pcdev->dma < 0) {
1117 dev_err(&pdev->dev, "%s failed to request DMA channel\n",
1118 __func__);
1119 return pcdev->dma;
1122 err = imx_dma_setup_handlers(pcdev->dma, mx27_camera_dma_callback,
1123 mx27_camera_dma_err_callback, pcdev);
1124 if (err) {
1125 dev_err(&pdev->dev, "%s failed to set DMA callback\n",
1126 __func__);
1127 goto err_out;
1130 err = imx_dma_config_channel(pcdev->dma,
1131 IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_FIFO,
1132 IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR,
1133 DMA_REQ_CSI_RX, 1);
1134 if (err) {
1135 dev_err(&pdev->dev, "%s failed to config DMA channel\n",
1136 __func__);
1137 goto err_out;
1140 imx_dma_config_burstlen(pcdev->dma, 64);
1142 return 0;
1144 err_out:
1145 imx_dma_free(pcdev->dma);
1147 return err;
1149 #endif /* CONFIG_MACH_MX27 */
1151 static unsigned int mx2_camera_poll(struct file *file, poll_table *pt)
1153 struct soc_camera_device *icd = file->private_data;
1155 return videobuf_poll_stream(file, &icd->vb_vidq, pt);
1158 static struct soc_camera_host_ops mx2_soc_camera_host_ops = {
1159 .owner = THIS_MODULE,
1160 .add = mx2_camera_add_device,
1161 .remove = mx2_camera_remove_device,
1162 .set_fmt = mx2_camera_set_fmt,
1163 .set_crop = mx2_camera_set_crop,
1164 .try_fmt = mx2_camera_try_fmt,
1165 .init_videobuf = mx2_camera_init_videobuf,
1166 .reqbufs = mx2_camera_reqbufs,
1167 .poll = mx2_camera_poll,
1168 .querycap = mx2_camera_querycap,
1169 .set_bus_param = mx2_camera_set_bus_param,
1172 static void mx27_camera_frame_done_emma(struct mx2_camera_dev *pcdev,
1173 int bufnum, int state)
1175 struct mx2_buffer *buf;
1176 struct videobuf_buffer *vb;
1177 unsigned long phys;
1179 if (!list_empty(&pcdev->active_bufs)) {
1180 buf = list_entry(pcdev->active_bufs.next,
1181 struct mx2_buffer, vb.queue);
1183 BUG_ON(buf->bufnum != bufnum);
1185 vb = &buf->vb;
1186 #ifdef DEBUG
1187 phys = videobuf_to_dma_contig(vb);
1188 if (readl(pcdev->base_emma + PRP_DEST_RGB1_PTR + 4 * bufnum)
1189 != phys) {
1190 dev_err(pcdev->dev, "%p != %p\n", phys,
1191 readl(pcdev->base_emma +
1192 PRP_DEST_RGB1_PTR +
1193 4 * bufnum));
1195 #endif
1196 dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__, vb,
1197 vb->baddr, vb->bsize);
1199 list_del(&vb->queue);
1200 vb->state = state;
1201 do_gettimeofday(&vb->ts);
1202 vb->field_count++;
1204 wake_up(&vb->done);
1207 if (list_empty(&pcdev->capture)) {
1208 writel(pcdev->discard_buffer_dma, pcdev->base_emma +
1209 PRP_DEST_RGB1_PTR + 4 * bufnum);
1210 return;
1213 buf = list_entry(pcdev->capture.next,
1214 struct mx2_buffer, vb.queue);
1216 buf->bufnum = !bufnum;
1218 list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
1220 vb = &buf->vb;
1221 vb->state = VIDEOBUF_ACTIVE;
1223 phys = videobuf_to_dma_contig(vb);
1224 writel(phys, pcdev->base_emma + PRP_DEST_RGB1_PTR + 4 * bufnum);
1227 static irqreturn_t mx27_camera_emma_irq(int irq_emma, void *data)
1229 struct mx2_camera_dev *pcdev = data;
1230 unsigned int status = readl(pcdev->base_emma + PRP_INTRSTATUS);
1231 struct mx2_buffer *buf;
1233 if (status & (1 << 7)) { /* overflow */
1234 u32 cntl;
1236 * We only disable channel 1 here since this is the only
1237 * enabled channel
1239 * FIXME: the correct DMA overflow handling should be resetting
1240 * the buffer, returning an error frame, and continuing with
1241 * the next one.
1243 cntl = readl(pcdev->base_emma + PRP_CNTL);
1244 writel(cntl & ~PRP_CNTL_CH1EN, pcdev->base_emma + PRP_CNTL);
1245 writel(cntl, pcdev->base_emma + PRP_CNTL);
1247 if ((status & (3 << 5)) == (3 << 5)
1248 && !list_empty(&pcdev->active_bufs)) {
1250 * Both buffers have triggered, process the one we're expecting
1251 * to first
1253 buf = list_entry(pcdev->active_bufs.next,
1254 struct mx2_buffer, vb.queue);
1255 mx27_camera_frame_done_emma(pcdev, buf->bufnum, VIDEOBUF_DONE);
1256 status &= ~(1 << (6 - buf->bufnum)); /* mark processed */
1258 if (status & (1 << 6))
1259 mx27_camera_frame_done_emma(pcdev, 0, VIDEOBUF_DONE);
1260 if (status & (1 << 5))
1261 mx27_camera_frame_done_emma(pcdev, 1, VIDEOBUF_DONE);
1263 writel(status, pcdev->base_emma + PRP_INTRSTATUS);
1265 return IRQ_HANDLED;
1268 static int __devinit mx27_camera_emma_init(struct mx2_camera_dev *pcdev)
1270 struct resource *res_emma = pcdev->res_emma;
1271 int err = 0;
1273 if (!request_mem_region(res_emma->start, resource_size(res_emma),
1274 MX2_CAM_DRV_NAME)) {
1275 err = -EBUSY;
1276 goto out;
1279 pcdev->base_emma = ioremap(res_emma->start, resource_size(res_emma));
1280 if (!pcdev->base_emma) {
1281 err = -ENOMEM;
1282 goto exit_release;
1285 err = request_irq(pcdev->irq_emma, mx27_camera_emma_irq, 0,
1286 MX2_CAM_DRV_NAME, pcdev);
1287 if (err) {
1288 dev_err(pcdev->dev, "Camera EMMA interrupt register failed \n");
1289 goto exit_iounmap;
1292 pcdev->clk_emma = clk_get(NULL, "emma");
1293 if (IS_ERR(pcdev->clk_emma)) {
1294 err = PTR_ERR(pcdev->clk_emma);
1295 goto exit_free_irq;
1298 clk_enable(pcdev->clk_emma);
1300 err = mx27_camera_emma_prp_reset(pcdev);
1301 if (err)
1302 goto exit_clk_emma_put;
1304 return err;
1306 exit_clk_emma_put:
1307 clk_disable(pcdev->clk_emma);
1308 clk_put(pcdev->clk_emma);
1309 exit_free_irq:
1310 free_irq(pcdev->irq_emma, pcdev);
1311 exit_iounmap:
1312 iounmap(pcdev->base_emma);
1313 exit_release:
1314 release_mem_region(res_emma->start, resource_size(res_emma));
1315 out:
1316 return err;
1319 static int __devinit mx2_camera_probe(struct platform_device *pdev)
1321 struct mx2_camera_dev *pcdev;
1322 struct resource *res_csi, *res_emma;
1323 void __iomem *base_csi;
1324 int irq_csi, irq_emma;
1325 irq_handler_t mx2_cam_irq_handler = cpu_is_mx25() ? mx25_camera_irq
1326 : mx27_camera_irq;
1327 int err = 0;
1329 dev_dbg(&pdev->dev, "initialising\n");
1331 res_csi = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1332 irq_csi = platform_get_irq(pdev, 0);
1333 if (res_csi == NULL || irq_csi < 0) {
1334 dev_err(&pdev->dev, "Missing platform resources data\n");
1335 err = -ENODEV;
1336 goto exit;
1339 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1340 if (!pcdev) {
1341 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1342 err = -ENOMEM;
1343 goto exit;
1346 pcdev->clk_csi = clk_get(&pdev->dev, NULL);
1347 if (IS_ERR(pcdev->clk_csi)) {
1348 err = PTR_ERR(pcdev->clk_csi);
1349 goto exit_kfree;
1352 dev_dbg(&pdev->dev, "Camera clock frequency: %ld\n",
1353 clk_get_rate(pcdev->clk_csi));
1355 /* Initialize DMA */
1356 #ifdef CONFIG_MACH_MX27
1357 if (cpu_is_mx27()) {
1358 err = mx27_camera_dma_init(pdev, pcdev);
1359 if (err)
1360 goto exit_clk_put;
1362 #endif /* CONFIG_MACH_MX27 */
1364 pcdev->res_csi = res_csi;
1365 pcdev->pdata = pdev->dev.platform_data;
1366 if (pcdev->pdata) {
1367 long rate;
1369 pcdev->platform_flags = pcdev->pdata->flags;
1371 rate = clk_round_rate(pcdev->clk_csi, pcdev->pdata->clk * 2);
1372 if (rate <= 0) {
1373 err = -ENODEV;
1374 goto exit_dma_free;
1376 err = clk_set_rate(pcdev->clk_csi, rate);
1377 if (err < 0)
1378 goto exit_dma_free;
1381 INIT_LIST_HEAD(&pcdev->capture);
1382 INIT_LIST_HEAD(&pcdev->active_bufs);
1383 spin_lock_init(&pcdev->lock);
1386 * Request the regions.
1388 if (!request_mem_region(res_csi->start, resource_size(res_csi),
1389 MX2_CAM_DRV_NAME)) {
1390 err = -EBUSY;
1391 goto exit_dma_free;
1394 base_csi = ioremap(res_csi->start, resource_size(res_csi));
1395 if (!base_csi) {
1396 err = -ENOMEM;
1397 goto exit_release;
1399 pcdev->irq_csi = irq_csi;
1400 pcdev->base_csi = base_csi;
1401 pcdev->base_dma = res_csi->start;
1402 pcdev->dev = &pdev->dev;
1404 err = request_irq(pcdev->irq_csi, mx2_cam_irq_handler, 0,
1405 MX2_CAM_DRV_NAME, pcdev);
1406 if (err) {
1407 dev_err(pcdev->dev, "Camera interrupt register failed \n");
1408 goto exit_iounmap;
1411 if (cpu_is_mx27()) {
1412 /* EMMA support */
1413 res_emma = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1414 irq_emma = platform_get_irq(pdev, 1);
1416 if (res_emma && irq_emma >= 0) {
1417 dev_info(&pdev->dev, "Using EMMA\n");
1418 pcdev->use_emma = 1;
1419 pcdev->res_emma = res_emma;
1420 pcdev->irq_emma = irq_emma;
1421 if (mx27_camera_emma_init(pcdev))
1422 goto exit_free_irq;
1426 pcdev->soc_host.drv_name = MX2_CAM_DRV_NAME,
1427 pcdev->soc_host.ops = &mx2_soc_camera_host_ops,
1428 pcdev->soc_host.priv = pcdev;
1429 pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
1430 pcdev->soc_host.nr = pdev->id;
1431 err = soc_camera_host_register(&pcdev->soc_host);
1432 if (err)
1433 goto exit_free_emma;
1435 dev_info(&pdev->dev, "MX2 Camera (CSI) driver probed, clock frequency: %ld\n",
1436 clk_get_rate(pcdev->clk_csi));
1438 return 0;
1440 exit_free_emma:
1441 if (mx27_camera_emma(pcdev)) {
1442 free_irq(pcdev->irq_emma, pcdev);
1443 clk_disable(pcdev->clk_emma);
1444 clk_put(pcdev->clk_emma);
1445 iounmap(pcdev->base_emma);
1446 release_mem_region(res_emma->start, resource_size(res_emma));
1448 exit_free_irq:
1449 free_irq(pcdev->irq_csi, pcdev);
1450 exit_iounmap:
1451 iounmap(base_csi);
1452 exit_release:
1453 release_mem_region(res_csi->start, resource_size(res_csi));
1454 exit_dma_free:
1455 #ifdef CONFIG_MACH_MX27
1456 if (cpu_is_mx27())
1457 imx_dma_free(pcdev->dma);
1458 exit_clk_put:
1459 clk_put(pcdev->clk_csi);
1460 #endif /* CONFIG_MACH_MX27 */
1461 exit_kfree:
1462 kfree(pcdev);
1463 exit:
1464 return err;
1467 static int __devexit mx2_camera_remove(struct platform_device *pdev)
1469 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1470 struct mx2_camera_dev *pcdev = container_of(soc_host,
1471 struct mx2_camera_dev, soc_host);
1472 struct resource *res;
1474 clk_put(pcdev->clk_csi);
1475 #ifdef CONFIG_MACH_MX27
1476 if (cpu_is_mx27())
1477 imx_dma_free(pcdev->dma);
1478 #endif /* CONFIG_MACH_MX27 */
1479 free_irq(pcdev->irq_csi, pcdev);
1480 if (mx27_camera_emma(pcdev))
1481 free_irq(pcdev->irq_emma, pcdev);
1483 soc_camera_host_unregister(&pcdev->soc_host);
1485 iounmap(pcdev->base_csi);
1487 if (mx27_camera_emma(pcdev)) {
1488 clk_disable(pcdev->clk_emma);
1489 clk_put(pcdev->clk_emma);
1490 iounmap(pcdev->base_emma);
1491 res = pcdev->res_emma;
1492 release_mem_region(res->start, resource_size(res));
1495 res = pcdev->res_csi;
1496 release_mem_region(res->start, resource_size(res));
1498 kfree(pcdev);
1500 dev_info(&pdev->dev, "MX2 Camera driver unloaded\n");
1502 return 0;
1505 static struct platform_driver mx2_camera_driver = {
1506 .driver = {
1507 .name = MX2_CAM_DRV_NAME,
1509 .remove = __devexit_p(mx2_camera_remove),
1513 static int __init mx2_camera_init(void)
1515 return platform_driver_probe(&mx2_camera_driver, &mx2_camera_probe);
1518 static void __exit mx2_camera_exit(void)
1520 return platform_driver_unregister(&mx2_camera_driver);
1523 module_init(mx2_camera_init);
1524 module_exit(mx2_camera_exit);
1526 MODULE_DESCRIPTION("i.MX27/i.MX25 SoC Camera Host driver");
1527 MODULE_AUTHOR("Sascha Hauer <sha@pengutronix.de>");
1528 MODULE_LICENSE("GPL");
1529 MODULE_VERSION(MX2_CAM_VERSION);