]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/pxa_camera.c
07f792b659d9ae0e68525f9d02a70c83084a1335
[linux-2.6-omap-h63xx.git] / drivers / media / video / pxa_camera.c
1 /*
2  * V4L2 Driver for PXA camera host
3  *
4  * Copyright (C) 2006, Sascha Hauer, Pengutronix
5  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
6  *
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.
11  */
12
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/io.h>
16 #include <linux/delay.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/errno.h>
19 #include <linux/fs.h>
20 #include <linux/interrupt.h>
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/moduleparam.h>
24 #include <linux/time.h>
25 #include <linux/version.h>
26 #include <linux/device.h>
27 #include <linux/platform_device.h>
28 #include <linux/clk.h>
29
30 #include <media/v4l2-common.h>
31 #include <media/v4l2-dev.h>
32 #include <media/videobuf-dma-sg.h>
33 #include <media/soc_camera.h>
34
35 #include <linux/videodev2.h>
36
37 #include <mach/dma.h>
38 #include <mach/camera.h>
39
40 #define PXA_CAM_VERSION_CODE KERNEL_VERSION(0, 0, 5)
41 #define PXA_CAM_DRV_NAME "pxa27x-camera"
42
43 /* Camera Interface */
44 #define CICR0           0x0000
45 #define CICR1           0x0004
46 #define CICR2           0x0008
47 #define CICR3           0x000C
48 #define CICR4           0x0010
49 #define CISR            0x0014
50 #define CIFR            0x0018
51 #define CITOR           0x001C
52 #define CIBR0           0x0028
53 #define CIBR1           0x0030
54 #define CIBR2           0x0038
55
56 #define CICR0_DMAEN     (1 << 31)       /* DMA request enable */
57 #define CICR0_PAR_EN    (1 << 30)       /* Parity enable */
58 #define CICR0_SL_CAP_EN (1 << 29)       /* Capture enable for slave mode */
59 #define CICR0_ENB       (1 << 28)       /* Camera interface enable */
60 #define CICR0_DIS       (1 << 27)       /* Camera interface disable */
61 #define CICR0_SIM       (0x7 << 24)     /* Sensor interface mode mask */
62 #define CICR0_TOM       (1 << 9)        /* Time-out mask */
63 #define CICR0_RDAVM     (1 << 8)        /* Receive-data-available mask */
64 #define CICR0_FEM       (1 << 7)        /* FIFO-empty mask */
65 #define CICR0_EOLM      (1 << 6)        /* End-of-line mask */
66 #define CICR0_PERRM     (1 << 5)        /* Parity-error mask */
67 #define CICR0_QDM       (1 << 4)        /* Quick-disable mask */
68 #define CICR0_CDM       (1 << 3)        /* Disable-done mask */
69 #define CICR0_SOFM      (1 << 2)        /* Start-of-frame mask */
70 #define CICR0_EOFM      (1 << 1)        /* End-of-frame mask */
71 #define CICR0_FOM       (1 << 0)        /* FIFO-overrun mask */
72
73 #define CICR1_TBIT      (1 << 31)       /* Transparency bit */
74 #define CICR1_RGBT_CONV (0x3 << 29)     /* RGBT conversion mask */
75 #define CICR1_PPL       (0x7ff << 15)   /* Pixels per line mask */
76 #define CICR1_RGB_CONV  (0x7 << 12)     /* RGB conversion mask */
77 #define CICR1_RGB_F     (1 << 11)       /* RGB format */
78 #define CICR1_YCBCR_F   (1 << 10)       /* YCbCr format */
79 #define CICR1_RGB_BPP   (0x7 << 7)      /* RGB bis per pixel mask */
80 #define CICR1_RAW_BPP   (0x3 << 5)      /* Raw bis per pixel mask */
81 #define CICR1_COLOR_SP  (0x3 << 3)      /* Color space mask */
82 #define CICR1_DW        (0x7 << 0)      /* Data width mask */
83
84 #define CICR2_BLW       (0xff << 24)    /* Beginning-of-line pixel clock
85                                            wait count mask */
86 #define CICR2_ELW       (0xff << 16)    /* End-of-line pixel clock
87                                            wait count mask */
88 #define CICR2_HSW       (0x3f << 10)    /* Horizontal sync pulse width mask */
89 #define CICR2_BFPW      (0x3f << 3)     /* Beginning-of-frame pixel clock
90                                            wait count mask */
91 #define CICR2_FSW       (0x7 << 0)      /* Frame stabilization
92                                            wait count mask */
93
94 #define CICR3_BFW       (0xff << 24)    /* Beginning-of-frame line clock
95                                            wait count mask */
96 #define CICR3_EFW       (0xff << 16)    /* End-of-frame line clock
97                                            wait count mask */
98 #define CICR3_VSW       (0x3f << 10)    /* Vertical sync pulse width mask */
99 #define CICR3_BFPW      (0x3f << 3)     /* Beginning-of-frame pixel clock
100                                            wait count mask */
101 #define CICR3_LPF       (0x7ff << 0)    /* Lines per frame mask */
102
103 #define CICR4_MCLK_DLY  (0x3 << 24)     /* MCLK Data Capture Delay mask */
104 #define CICR4_PCLK_EN   (1 << 23)       /* Pixel clock enable */
105 #define CICR4_PCP       (1 << 22)       /* Pixel clock polarity */
106 #define CICR4_HSP       (1 << 21)       /* Horizontal sync polarity */
107 #define CICR4_VSP       (1 << 20)       /* Vertical sync polarity */
108 #define CICR4_MCLK_EN   (1 << 19)       /* MCLK enable */
109 #define CICR4_FR_RATE   (0x7 << 8)      /* Frame rate mask */
110 #define CICR4_DIV       (0xff << 0)     /* Clock divisor mask */
111
112 #define CISR_FTO        (1 << 15)       /* FIFO time-out */
113 #define CISR_RDAV_2     (1 << 14)       /* Channel 2 receive data available */
114 #define CISR_RDAV_1     (1 << 13)       /* Channel 1 receive data available */
115 #define CISR_RDAV_0     (1 << 12)       /* Channel 0 receive data available */
116 #define CISR_FEMPTY_2   (1 << 11)       /* Channel 2 FIFO empty */
117 #define CISR_FEMPTY_1   (1 << 10)       /* Channel 1 FIFO empty */
118 #define CISR_FEMPTY_0   (1 << 9)        /* Channel 0 FIFO empty */
119 #define CISR_EOL        (1 << 8)        /* End of line */
120 #define CISR_PAR_ERR    (1 << 7)        /* Parity error */
121 #define CISR_CQD        (1 << 6)        /* Camera interface quick disable */
122 #define CISR_CDD        (1 << 5)        /* Camera interface disable done */
123 #define CISR_SOF        (1 << 4)        /* Start of frame */
124 #define CISR_EOF        (1 << 3)        /* End of frame */
125 #define CISR_IFO_2      (1 << 2)        /* FIFO overrun for Channel 2 */
126 #define CISR_IFO_1      (1 << 1)        /* FIFO overrun for Channel 1 */
127 #define CISR_IFO_0      (1 << 0)        /* FIFO overrun for Channel 0 */
128
129 #define CIFR_FLVL2      (0x7f << 23)    /* FIFO 2 level mask */
130 #define CIFR_FLVL1      (0x7f << 16)    /* FIFO 1 level mask */
131 #define CIFR_FLVL0      (0xff << 8)     /* FIFO 0 level mask */
132 #define CIFR_THL_0      (0x3 << 4)      /* Threshold Level for Channel 0 FIFO */
133 #define CIFR_RESET_F    (1 << 3)        /* Reset input FIFOs */
134 #define CIFR_FEN2       (1 << 2)        /* FIFO enable for channel 2 */
135 #define CIFR_FEN1       (1 << 1)        /* FIFO enable for channel 1 */
136 #define CIFR_FEN0       (1 << 0)        /* FIFO enable for channel 0 */
137
138 #define CICR0_SIM_MP    (0 << 24)
139 #define CICR0_SIM_SP    (1 << 24)
140 #define CICR0_SIM_MS    (2 << 24)
141 #define CICR0_SIM_EP    (3 << 24)
142 #define CICR0_SIM_ES    (4 << 24)
143
144 #define CICR1_DW_VAL(x)   ((x) & CICR1_DW)          /* Data bus width */
145 #define CICR1_PPL_VAL(x)  (((x) << 15) & CICR1_PPL) /* Pixels per line */
146 #define CICR1_COLOR_SP_VAL(x)   (((x) << 3) & CICR1_COLOR_SP)   /* color space */
147 #define CICR1_RGB_BPP_VAL(x)    (((x) << 7) & CICR1_RGB_BPP)    /* bpp for rgb */
148 #define CICR1_RGBT_CONV_VAL(x)  (((x) << 29) & CICR1_RGBT_CONV) /* rgbt conv */
149
150 #define CICR2_BLW_VAL(x)  (((x) << 24) & CICR2_BLW) /* Beginning-of-line pixel clock wait count */
151 #define CICR2_ELW_VAL(x)  (((x) << 16) & CICR2_ELW) /* End-of-line pixel clock wait count */
152 #define CICR2_HSW_VAL(x)  (((x) << 10) & CICR2_HSW) /* Horizontal sync pulse width */
153 #define CICR2_BFPW_VAL(x) (((x) << 3) & CICR2_BFPW) /* Beginning-of-frame pixel clock wait count */
154 #define CICR2_FSW_VAL(x)  (((x) << 0) & CICR2_FSW)  /* Frame stabilization wait count */
155
156 #define CICR3_BFW_VAL(x)  (((x) << 24) & CICR3_BFW) /* Beginning-of-frame line clock wait count  */
157 #define CICR3_EFW_VAL(x)  (((x) << 16) & CICR3_EFW) /* End-of-frame line clock wait count */
158 #define CICR3_VSW_VAL(x)  (((x) << 11) & CICR3_VSW) /* Vertical sync pulse width */
159 #define CICR3_LPF_VAL(x)  (((x) << 0) & CICR3_LPF)  /* Lines per frame */
160
161 #define CICR0_IRQ_MASK (CICR0_TOM | CICR0_RDAVM | CICR0_FEM | CICR0_EOLM | \
162                         CICR0_PERRM | CICR0_QDM | CICR0_CDM | CICR0_SOFM | \
163                         CICR0_EOFM | CICR0_FOM)
164
165 /*
166  * YUV422P picture size should be a multiple of 16, so the heuristic aligns
167  * height, width on 4 byte boundaries to reach the 16 multiple for the size.
168  */
169 #define YUV422P_X_Y_ALIGN 4
170 #define YUV422P_SIZE_ALIGN YUV422P_X_Y_ALIGN * YUV422P_X_Y_ALIGN
171
172 /*
173  * Structures
174  */
175 enum pxa_camera_active_dma {
176         DMA_Y = 0x1,
177         DMA_U = 0x2,
178         DMA_V = 0x4,
179 };
180
181 /* descriptor needed for the PXA DMA engine */
182 struct pxa_cam_dma {
183         dma_addr_t              sg_dma;
184         struct pxa_dma_desc     *sg_cpu;
185         size_t                  sg_size;
186         int                     sglen;
187 };
188
189 /* buffer for one video frame */
190 struct pxa_buffer {
191         /* common v4l buffer stuff -- must be first */
192         struct videobuf_buffer vb;
193
194         const struct soc_camera_data_format        *fmt;
195
196         /* our descriptor lists for Y, U and V channels */
197         struct pxa_cam_dma dmas[3];
198
199         int                     inwork;
200
201         enum pxa_camera_active_dma active_dma;
202 };
203
204 struct pxa_camera_dev {
205         struct device           *dev;
206         /* PXA27x is only supposed to handle one camera on its Quick Capture
207          * interface. If anyone ever builds hardware to enable more than
208          * one camera, they will have to modify this driver too */
209         struct soc_camera_device *icd;
210         struct clk              *clk;
211
212         unsigned int            irq;
213         void __iomem            *base;
214
215         int                     channels;
216         unsigned int            dma_chans[3];
217
218         struct pxacamera_platform_data *pdata;
219         struct resource         *res;
220         unsigned long           platform_flags;
221         unsigned long           ciclk;
222         unsigned long           mclk;
223         u32                     mclk_divisor;
224
225         struct list_head        capture;
226
227         spinlock_t              lock;
228
229         struct pxa_buffer       *active;
230         struct pxa_dma_desc     *sg_tail[3];
231
232         u32                     save_cicr[5];
233 };
234
235 static const char *pxa_cam_driver_description = "PXA_Camera";
236
237 static unsigned int vid_limit = 16;     /* Video memory limit, in Mb */
238
239 /*
240  *  Videobuf operations
241  */
242 static int pxa_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
243                               unsigned int *size)
244 {
245         struct soc_camera_device *icd = vq->priv_data;
246
247         dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
248
249         *size = roundup(icd->width * icd->height *
250                         ((icd->current_fmt->depth + 7) >> 3), 8);
251
252         if (0 == *count)
253                 *count = 32;
254         while (*size * *count > vid_limit * 1024 * 1024)
255                 (*count)--;
256
257         return 0;
258 }
259
260 static void free_buffer(struct videobuf_queue *vq, struct pxa_buffer *buf)
261 {
262         struct soc_camera_device *icd = vq->priv_data;
263         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
264         struct pxa_camera_dev *pcdev = ici->priv;
265         struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
266         int i;
267
268         BUG_ON(in_interrupt());
269
270         dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
271                 &buf->vb, buf->vb.baddr, buf->vb.bsize);
272
273         /* This waits until this buffer is out of danger, i.e., until it is no
274          * longer in STATE_QUEUED or STATE_ACTIVE */
275         videobuf_waiton(&buf->vb, 0, 0);
276         videobuf_dma_unmap(vq, dma);
277         videobuf_dma_free(dma);
278
279         for (i = 0; i < ARRAY_SIZE(buf->dmas); i++) {
280                 if (buf->dmas[i].sg_cpu)
281                         dma_free_coherent(pcdev->dev, buf->dmas[i].sg_size,
282                                           buf->dmas[i].sg_cpu,
283                                           buf->dmas[i].sg_dma);
284                 buf->dmas[i].sg_cpu = NULL;
285         }
286
287         buf->vb.state = VIDEOBUF_NEEDS_INIT;
288 }
289
290 static int calculate_dma_sglen(struct scatterlist *sglist, int sglen,
291                                int sg_first_ofs, int size)
292 {
293         int i, offset, dma_len, xfer_len;
294         struct scatterlist *sg;
295
296         offset = sg_first_ofs;
297         for_each_sg(sglist, sg, sglen, i) {
298                 dma_len = sg_dma_len(sg);
299
300                 /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
301                 xfer_len = roundup(min(dma_len - offset, size), 8);
302
303                 size = max(0, size - xfer_len);
304                 offset = 0;
305                 if (size == 0)
306                         break;
307         }
308
309         BUG_ON(size != 0);
310         return i + 1;
311 }
312
313 /**
314  * pxa_init_dma_channel - init dma descriptors
315  * @pcdev: pxa camera device
316  * @buf: pxa buffer to find pxa dma channel
317  * @dma: dma video buffer
318  * @channel: dma channel (0 => 'Y', 1 => 'U', 2 => 'V')
319  * @cibr: camera Receive Buffer Register
320  * @size: bytes to transfer
321  * @sg_first: first element of sg_list
322  * @sg_first_ofs: offset in first element of sg_list
323  *
324  * Prepares the pxa dma descriptors to transfer one camera channel.
325  * Beware sg_first and sg_first_ofs are both input and output parameters.
326  *
327  * Returns 0 or -ENOMEM if no coherent memory is available
328  */
329 static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev,
330                                 struct pxa_buffer *buf,
331                                 struct videobuf_dmabuf *dma, int channel,
332                                 int cibr, int size,
333                                 struct scatterlist **sg_first, int *sg_first_ofs)
334 {
335         struct pxa_cam_dma *pxa_dma = &buf->dmas[channel];
336         struct scatterlist *sg;
337         int i, offset, sglen;
338         int dma_len = 0, xfer_len = 0;
339
340         if (pxa_dma->sg_cpu)
341                 dma_free_coherent(pcdev->dev, pxa_dma->sg_size,
342                                   pxa_dma->sg_cpu, pxa_dma->sg_dma);
343
344         sglen = calculate_dma_sglen(*sg_first, dma->sglen,
345                                     *sg_first_ofs, size);
346
347         pxa_dma->sg_size = (sglen + 1) * sizeof(struct pxa_dma_desc);
348         pxa_dma->sg_cpu = dma_alloc_coherent(pcdev->dev, pxa_dma->sg_size,
349                                              &pxa_dma->sg_dma, GFP_KERNEL);
350         if (!pxa_dma->sg_cpu)
351                 return -ENOMEM;
352
353         pxa_dma->sglen = sglen;
354         offset = *sg_first_ofs;
355
356         dev_dbg(pcdev->dev, "DMA: sg_first=%p, sglen=%d, ofs=%d, dma.desc=%x\n",
357                 *sg_first, sglen, *sg_first_ofs, pxa_dma->sg_dma);
358
359
360         for_each_sg(*sg_first, sg, sglen, i) {
361                 dma_len = sg_dma_len(sg);
362
363                 /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
364                 xfer_len = roundup(min(dma_len - offset, size), 8);
365
366                 size = max(0, size - xfer_len);
367
368                 pxa_dma->sg_cpu[i].dsadr = pcdev->res->start + cibr;
369                 pxa_dma->sg_cpu[i].dtadr = sg_dma_address(sg) + offset;
370                 pxa_dma->sg_cpu[i].dcmd =
371                         DCMD_FLOWSRC | DCMD_BURST8 | DCMD_INCTRGADDR | xfer_len;
372                 pxa_dma->sg_cpu[i].ddadr =
373                         pxa_dma->sg_dma + (i + 1) * sizeof(struct pxa_dma_desc);
374
375                 dev_vdbg(pcdev->dev, "DMA: desc.%08x->@phys=0x%08x, len=%d\n",
376                          pxa_dma->sg_dma + i * sizeof(struct pxa_dma_desc),
377                          sg_dma_address(sg) + offset, xfer_len);
378                 offset = 0;
379
380                 if (size == 0)
381                         break;
382         }
383
384         pxa_dma->sg_cpu[sglen - 1].ddadr = DDADR_STOP;
385         pxa_dma->sg_cpu[sglen - 1].dcmd |= DCMD_ENDIRQEN;
386
387         /*
388          * Handle 1 special case :
389          *  - in 3 planes (YUV422P format), we might finish with xfer_len equal
390          *    to dma_len (end on PAGE boundary). In this case, the sg element
391          *    for next plane should be the next after the last used to store the
392          *    last scatter gather RAM page
393          */
394         if (xfer_len >= dma_len) {
395                 *sg_first_ofs = xfer_len - dma_len;
396                 *sg_first = sg_next(sg);
397         } else {
398                 *sg_first_ofs = xfer_len;
399                 *sg_first = sg;
400         }
401
402         return 0;
403 }
404
405 static int pxa_videobuf_prepare(struct videobuf_queue *vq,
406                 struct videobuf_buffer *vb, enum v4l2_field field)
407 {
408         struct soc_camera_device *icd = vq->priv_data;
409         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
410         struct pxa_camera_dev *pcdev = ici->priv;
411         struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
412         int ret;
413         int size_y, size_u = 0, size_v = 0;
414
415         dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
416                 vb, vb->baddr, vb->bsize);
417
418         /* Added list head initialization on alloc */
419         WARN_ON(!list_empty(&vb->queue));
420
421 #ifdef DEBUG
422         /* This can be useful if you want to see if we actually fill
423          * the buffer with something */
424         memset((void *)vb->baddr, 0xaa, vb->bsize);
425 #endif
426
427         BUG_ON(NULL == icd->current_fmt);
428
429         /* I think, in buf_prepare you only have to protect global data,
430          * the actual buffer is yours */
431         buf->inwork = 1;
432
433         if (buf->fmt    != icd->current_fmt ||
434             vb->width   != icd->width ||
435             vb->height  != icd->height ||
436             vb->field   != field) {
437                 buf->fmt        = icd->current_fmt;
438                 vb->width       = icd->width;
439                 vb->height      = icd->height;
440                 vb->field       = field;
441                 vb->state       = VIDEOBUF_NEEDS_INIT;
442         }
443
444         vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
445         if (0 != vb->baddr && vb->bsize < vb->size) {
446                 ret = -EINVAL;
447                 goto out;
448         }
449
450         if (vb->state == VIDEOBUF_NEEDS_INIT) {
451                 int size = vb->size;
452                 int next_ofs = 0;
453                 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
454                 struct scatterlist *sg;
455
456                 ret = videobuf_iolock(vq, vb, NULL);
457                 if (ret)
458                         goto fail;
459
460                 if (pcdev->channels == 3) {
461                         size_y = size / 2;
462                         size_u = size_v = size / 4;
463                 } else {
464                         size_y = size;
465                 }
466
467                 sg = dma->sglist;
468
469                 /* init DMA for Y channel */
470                 ret = pxa_init_dma_channel(pcdev, buf, dma, 0, CIBR0, size_y,
471                                            &sg, &next_ofs);
472                 if (ret) {
473                         dev_err(pcdev->dev,
474                                 "DMA initialization for Y/RGB failed\n");
475                         goto fail;
476                 }
477
478                 /* init DMA for U channel */
479                 if (size_u)
480                         ret = pxa_init_dma_channel(pcdev, buf, dma, 1, CIBR1,
481                                                    size_u, &sg, &next_ofs);
482                 if (ret) {
483                         dev_err(pcdev->dev,
484                                 "DMA initialization for U failed\n");
485                         goto fail_u;
486                 }
487
488                 /* init DMA for V channel */
489                 if (size_v)
490                         ret = pxa_init_dma_channel(pcdev, buf, dma, 2, CIBR2,
491                                                    size_v, &sg, &next_ofs);
492                 if (ret) {
493                         dev_err(pcdev->dev,
494                                 "DMA initialization for V failed\n");
495                         goto fail_v;
496                 }
497
498                 vb->state = VIDEOBUF_PREPARED;
499         }
500
501         buf->inwork = 0;
502         buf->active_dma = DMA_Y;
503         if (pcdev->channels == 3)
504                 buf->active_dma |= DMA_U | DMA_V;
505
506         return 0;
507
508 fail_v:
509         dma_free_coherent(pcdev->dev, buf->dmas[1].sg_size,
510                           buf->dmas[1].sg_cpu, buf->dmas[1].sg_dma);
511 fail_u:
512         dma_free_coherent(pcdev->dev, buf->dmas[0].sg_size,
513                           buf->dmas[0].sg_cpu, buf->dmas[0].sg_dma);
514 fail:
515         free_buffer(vq, buf);
516 out:
517         buf->inwork = 0;
518         return ret;
519 }
520
521 static void pxa_videobuf_queue(struct videobuf_queue *vq,
522                                struct videobuf_buffer *vb)
523 {
524         struct soc_camera_device *icd = vq->priv_data;
525         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
526         struct pxa_camera_dev *pcdev = ici->priv;
527         struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
528         struct pxa_buffer *active;
529         unsigned long flags;
530         int i;
531
532         dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
533                 vb, vb->baddr, vb->bsize);
534         spin_lock_irqsave(&pcdev->lock, flags);
535
536         list_add_tail(&vb->queue, &pcdev->capture);
537
538         vb->state = VIDEOBUF_ACTIVE;
539         active = pcdev->active;
540
541         if (!active) {
542                 unsigned long cifr, cicr0;
543
544                 cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
545                 __raw_writel(cifr, pcdev->base + CIFR);
546
547                 for (i = 0; i < pcdev->channels; i++) {
548                         DDADR(pcdev->dma_chans[i]) = buf->dmas[i].sg_dma;
549                         DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
550                         pcdev->sg_tail[i] = buf->dmas[i].sg_cpu + buf->dmas[i].sglen - 1;
551                 }
552
553                 pcdev->active = buf;
554
555                 cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_ENB;
556                 __raw_writel(cicr0, pcdev->base + CICR0);
557         } else {
558                 struct pxa_cam_dma *buf_dma;
559                 struct pxa_cam_dma *act_dma;
560                 int nents;
561
562                 for (i = 0; i < pcdev->channels; i++) {
563                         buf_dma = &buf->dmas[i];
564                         act_dma = &active->dmas[i];
565                         nents = buf_dma->sglen;
566
567                         /* Stop DMA engine */
568                         DCSR(pcdev->dma_chans[i]) = 0;
569
570                         /* Add the descriptors we just initialized to
571                            the currently running chain */
572                         pcdev->sg_tail[i]->ddadr = buf_dma->sg_dma;
573                         pcdev->sg_tail[i] = buf_dma->sg_cpu + buf_dma->sglen - 1;
574
575                         /* Setup a dummy descriptor with the DMA engines current
576                          * state
577                          */
578                         buf_dma->sg_cpu[nents].dsadr =
579                                 pcdev->res->start + 0x28 + i*8; /* CIBRx */
580                         buf_dma->sg_cpu[nents].dtadr =
581                                 DTADR(pcdev->dma_chans[i]);
582                         buf_dma->sg_cpu[nents].dcmd =
583                                 DCMD(pcdev->dma_chans[i]);
584
585                         if (DDADR(pcdev->dma_chans[i]) == DDADR_STOP) {
586                                 /* The DMA engine is on the last
587                                    descriptor, set the next descriptors
588                                    address to the descriptors we just
589                                    initialized */
590                                 buf_dma->sg_cpu[nents].ddadr = buf_dma->sg_dma;
591                         } else {
592                                 buf_dma->sg_cpu[nents].ddadr =
593                                         DDADR(pcdev->dma_chans[i]);
594                         }
595
596                         /* The next descriptor is the dummy descriptor */
597                         DDADR(pcdev->dma_chans[i]) = buf_dma->sg_dma + nents *
598                                 sizeof(struct pxa_dma_desc);
599
600                         DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
601                 }
602         }
603
604         spin_unlock_irqrestore(&pcdev->lock, flags);
605 }
606
607 static void pxa_videobuf_release(struct videobuf_queue *vq,
608                                  struct videobuf_buffer *vb)
609 {
610         struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
611 #ifdef DEBUG
612         struct soc_camera_device *icd = vq->priv_data;
613
614         dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
615                 vb, vb->baddr, vb->bsize);
616
617         switch (vb->state) {
618         case VIDEOBUF_ACTIVE:
619                 dev_dbg(&icd->dev, "%s (active)\n", __func__);
620                 break;
621         case VIDEOBUF_QUEUED:
622                 dev_dbg(&icd->dev, "%s (queued)\n", __func__);
623                 break;
624         case VIDEOBUF_PREPARED:
625                 dev_dbg(&icd->dev, "%s (prepared)\n", __func__);
626                 break;
627         default:
628                 dev_dbg(&icd->dev, "%s (unknown)\n", __func__);
629                 break;
630         }
631 #endif
632
633         free_buffer(vq, buf);
634 }
635
636 static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev,
637                               struct videobuf_buffer *vb,
638                               struct pxa_buffer *buf)
639 {
640         unsigned long cicr0;
641
642         /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
643         list_del_init(&vb->queue);
644         vb->state = VIDEOBUF_DONE;
645         do_gettimeofday(&vb->ts);
646         vb->field_count++;
647         wake_up(&vb->done);
648
649         if (list_empty(&pcdev->capture)) {
650                 pcdev->active = NULL;
651                 DCSR(pcdev->dma_chans[0]) = 0;
652                 DCSR(pcdev->dma_chans[1]) = 0;
653                 DCSR(pcdev->dma_chans[2]) = 0;
654
655                 cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB;
656                 __raw_writel(cicr0, pcdev->base + CICR0);
657                 return;
658         }
659
660         pcdev->active = list_entry(pcdev->capture.next,
661                                    struct pxa_buffer, vb.queue);
662 }
663
664 static void pxa_camera_dma_irq(int channel, struct pxa_camera_dev *pcdev,
665                                enum pxa_camera_active_dma act_dma)
666 {
667         struct pxa_buffer *buf;
668         unsigned long flags;
669         u32 status, camera_status, overrun;
670         struct videobuf_buffer *vb;
671         unsigned long cifr, cicr0;
672
673         spin_lock_irqsave(&pcdev->lock, flags);
674
675         status = DCSR(channel);
676         DCSR(channel) = status | DCSR_ENDINTR;
677
678         if (status & DCSR_BUSERR) {
679                 dev_err(pcdev->dev, "DMA Bus Error IRQ!\n");
680                 goto out;
681         }
682
683         if (!(status & DCSR_ENDINTR)) {
684                 dev_err(pcdev->dev, "Unknown DMA IRQ source, "
685                         "status: 0x%08x\n", status);
686                 goto out;
687         }
688
689         if (!pcdev->active) {
690                 dev_err(pcdev->dev, "DMA End IRQ with no active buffer!\n");
691                 goto out;
692         }
693
694         camera_status = __raw_readl(pcdev->base + CISR);
695         overrun = CISR_IFO_0;
696         if (pcdev->channels == 3)
697                 overrun |= CISR_IFO_1 | CISR_IFO_2;
698         if (camera_status & overrun) {
699                 dev_dbg(pcdev->dev, "FIFO overrun! CISR: %x\n", camera_status);
700                 /* Stop the Capture Interface */
701                 cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB;
702                 __raw_writel(cicr0, pcdev->base + CICR0);
703
704                 /* Stop DMA */
705                 DCSR(channel) = 0;
706                 /* Reset the FIFOs */
707                 cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
708                 __raw_writel(cifr, pcdev->base + CIFR);
709                 /* Enable End-Of-Frame Interrupt */
710                 cicr0 &= ~CICR0_EOFM;
711                 __raw_writel(cicr0, pcdev->base + CICR0);
712                 /* Restart the Capture Interface */
713                 __raw_writel(cicr0 | CICR0_ENB, pcdev->base + CICR0);
714                 goto out;
715         }
716
717         vb = &pcdev->active->vb;
718         buf = container_of(vb, struct pxa_buffer, vb);
719         WARN_ON(buf->inwork || list_empty(&vb->queue));
720         dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
721                 vb, vb->baddr, vb->bsize);
722
723         buf->active_dma &= ~act_dma;
724         if (!buf->active_dma)
725                 pxa_camera_wakeup(pcdev, vb, buf);
726
727 out:
728         spin_unlock_irqrestore(&pcdev->lock, flags);
729 }
730
731 static void pxa_camera_dma_irq_y(int channel, void *data)
732 {
733         struct pxa_camera_dev *pcdev = data;
734         pxa_camera_dma_irq(channel, pcdev, DMA_Y);
735 }
736
737 static void pxa_camera_dma_irq_u(int channel, void *data)
738 {
739         struct pxa_camera_dev *pcdev = data;
740         pxa_camera_dma_irq(channel, pcdev, DMA_U);
741 }
742
743 static void pxa_camera_dma_irq_v(int channel, void *data)
744 {
745         struct pxa_camera_dev *pcdev = data;
746         pxa_camera_dma_irq(channel, pcdev, DMA_V);
747 }
748
749 static struct videobuf_queue_ops pxa_videobuf_ops = {
750         .buf_setup      = pxa_videobuf_setup,
751         .buf_prepare    = pxa_videobuf_prepare,
752         .buf_queue      = pxa_videobuf_queue,
753         .buf_release    = pxa_videobuf_release,
754 };
755
756 static void pxa_camera_init_videobuf(struct videobuf_queue *q,
757                               struct soc_camera_device *icd)
758 {
759         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
760         struct pxa_camera_dev *pcdev = ici->priv;
761
762         /* We must pass NULL as dev pointer, then all pci_* dma operations
763          * transform to normal dma_* ones. */
764         videobuf_queue_sg_init(q, &pxa_videobuf_ops, NULL, &pcdev->lock,
765                                 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
766                                 sizeof(struct pxa_buffer), icd);
767 }
768
769 static u32 mclk_get_divisor(struct pxa_camera_dev *pcdev)
770 {
771         unsigned long mclk = pcdev->mclk;
772         u32 div;
773         unsigned long lcdclk;
774
775         lcdclk = clk_get_rate(pcdev->clk);
776         pcdev->ciclk = lcdclk;
777
778         /* mclk <= ciclk / 4 (27.4.2) */
779         if (mclk > lcdclk / 4) {
780                 mclk = lcdclk / 4;
781                 dev_warn(pcdev->dev, "Limiting master clock to %lu\n", mclk);
782         }
783
784         /* We verify mclk != 0, so if anyone breaks it, here comes their Oops */
785         div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
786
787         /* If we're not supplying MCLK, leave it at 0 */
788         if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
789                 pcdev->mclk = lcdclk / (2 * (div + 1));
790
791         dev_dbg(pcdev->dev, "LCD clock %luHz, target freq %luHz, "
792                 "divisor %u\n", lcdclk, mclk, div);
793
794         return div;
795 }
796
797 static void recalculate_fifo_timeout(struct pxa_camera_dev *pcdev,
798                                      unsigned long pclk)
799 {
800         /* We want a timeout > 1 pixel time, not ">=" */
801         u32 ciclk_per_pixel = pcdev->ciclk / pclk + 1;
802
803         __raw_writel(ciclk_per_pixel, pcdev->base + CITOR);
804 }
805
806 static void pxa_camera_activate(struct pxa_camera_dev *pcdev)
807 {
808         struct pxacamera_platform_data *pdata = pcdev->pdata;
809         u32 cicr4 = 0;
810
811         dev_dbg(pcdev->dev, "Registered platform device at %p data %p\n",
812                 pcdev, pdata);
813
814         if (pdata && pdata->init) {
815                 dev_dbg(pcdev->dev, "%s: Init gpios\n", __func__);
816                 pdata->init(pcdev->dev);
817         }
818
819         /* disable all interrupts */
820         __raw_writel(0x3ff, pcdev->base + CICR0);
821
822         if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
823                 cicr4 |= CICR4_PCLK_EN;
824         if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
825                 cicr4 |= CICR4_MCLK_EN;
826         if (pcdev->platform_flags & PXA_CAMERA_PCP)
827                 cicr4 |= CICR4_PCP;
828         if (pcdev->platform_flags & PXA_CAMERA_HSP)
829                 cicr4 |= CICR4_HSP;
830         if (pcdev->platform_flags & PXA_CAMERA_VSP)
831                 cicr4 |= CICR4_VSP;
832
833         __raw_writel(pcdev->mclk_divisor | cicr4, pcdev->base + CICR4);
834
835         if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
836                 /* Initialise the timeout under the assumption pclk = mclk */
837                 recalculate_fifo_timeout(pcdev, pcdev->mclk);
838         else
839                 /* "Safe default" - 13MHz */
840                 recalculate_fifo_timeout(pcdev, 13000000);
841
842         clk_enable(pcdev->clk);
843 }
844
845 static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev)
846 {
847         clk_disable(pcdev->clk);
848 }
849
850 static irqreturn_t pxa_camera_irq(int irq, void *data)
851 {
852         struct pxa_camera_dev *pcdev = data;
853         unsigned long status, cicr0;
854
855         status = __raw_readl(pcdev->base + CISR);
856         dev_dbg(pcdev->dev, "Camera interrupt status 0x%lx\n", status);
857
858         if (!status)
859                 return IRQ_NONE;
860
861         __raw_writel(status, pcdev->base + CISR);
862
863         if (status & CISR_EOF) {
864                 int i;
865                 for (i = 0; i < pcdev->channels; i++) {
866                         DDADR(pcdev->dma_chans[i]) =
867                                 pcdev->active->dmas[i].sg_dma;
868                         DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
869                 }
870                 cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_EOFM;
871                 __raw_writel(cicr0, pcdev->base + CICR0);
872         }
873
874         return IRQ_HANDLED;
875 }
876
877 /*
878  * The following two functions absolutely depend on the fact, that
879  * there can be only one camera on PXA quick capture interface
880  * Called with .video_lock held
881  */
882 static int pxa_camera_add_device(struct soc_camera_device *icd)
883 {
884         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
885         struct pxa_camera_dev *pcdev = ici->priv;
886         int ret;
887
888         if (pcdev->icd) {
889                 ret = -EBUSY;
890                 goto ebusy;
891         }
892
893         dev_info(&icd->dev, "PXA Camera driver attached to camera %d\n",
894                  icd->devnum);
895
896         pxa_camera_activate(pcdev);
897         ret = icd->ops->init(icd);
898
899         if (!ret)
900                 pcdev->icd = icd;
901
902 ebusy:
903         return ret;
904 }
905
906 /* Called with .video_lock held */
907 static void pxa_camera_remove_device(struct soc_camera_device *icd)
908 {
909         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
910         struct pxa_camera_dev *pcdev = ici->priv;
911
912         BUG_ON(icd != pcdev->icd);
913
914         dev_info(&icd->dev, "PXA Camera driver detached from camera %d\n",
915                  icd->devnum);
916
917         /* disable capture, disable interrupts */
918         __raw_writel(0x3ff, pcdev->base + CICR0);
919
920         /* Stop DMA engine */
921         DCSR(pcdev->dma_chans[0]) = 0;
922         DCSR(pcdev->dma_chans[1]) = 0;
923         DCSR(pcdev->dma_chans[2]) = 0;
924
925         icd->ops->release(icd);
926
927         pxa_camera_deactivate(pcdev);
928
929         pcdev->icd = NULL;
930 }
931
932 static int test_platform_param(struct pxa_camera_dev *pcdev,
933                                unsigned char buswidth, unsigned long *flags)
934 {
935         /*
936          * Platform specified synchronization and pixel clock polarities are
937          * only a recommendation and are only used during probing. The PXA270
938          * quick capture interface supports both.
939          */
940         *flags = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
941                   SOCAM_MASTER : SOCAM_SLAVE) |
942                 SOCAM_HSYNC_ACTIVE_HIGH |
943                 SOCAM_HSYNC_ACTIVE_LOW |
944                 SOCAM_VSYNC_ACTIVE_HIGH |
945                 SOCAM_VSYNC_ACTIVE_LOW |
946                 SOCAM_DATA_ACTIVE_HIGH |
947                 SOCAM_PCLK_SAMPLE_RISING |
948                 SOCAM_PCLK_SAMPLE_FALLING;
949
950         /* If requested data width is supported by the platform, use it */
951         switch (buswidth) {
952         case 10:
953                 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10))
954                         return -EINVAL;
955                 *flags |= SOCAM_DATAWIDTH_10;
956                 break;
957         case 9:
958                 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9))
959                         return -EINVAL;
960                 *flags |= SOCAM_DATAWIDTH_9;
961                 break;
962         case 8:
963                 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8))
964                         return -EINVAL;
965                 *flags |= SOCAM_DATAWIDTH_8;
966                 break;
967         default:
968                 return -EINVAL;
969         }
970
971         return 0;
972 }
973
974 static int pxa_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
975 {
976         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
977         struct pxa_camera_dev *pcdev = ici->priv;
978         unsigned long dw, bpp, bus_flags, camera_flags, common_flags;
979         u32 cicr0, cicr1, cicr2, cicr3, cicr4 = 0;
980         int ret = test_platform_param(pcdev, icd->buswidth, &bus_flags);
981
982         if (ret < 0)
983                 return ret;
984
985         camera_flags = icd->ops->query_bus_param(icd);
986
987         common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
988         if (!common_flags)
989                 return -EINVAL;
990
991         pcdev->channels = 1;
992
993         /* Make choises, based on platform preferences */
994         if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
995             (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
996                 if (pcdev->platform_flags & PXA_CAMERA_HSP)
997                         common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
998                 else
999                         common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
1000         }
1001
1002         if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
1003             (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
1004                 if (pcdev->platform_flags & PXA_CAMERA_VSP)
1005                         common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
1006                 else
1007                         common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
1008         }
1009
1010         if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
1011             (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
1012                 if (pcdev->platform_flags & PXA_CAMERA_PCP)
1013                         common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
1014                 else
1015                         common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
1016         }
1017
1018         ret = icd->ops->set_bus_param(icd, common_flags);
1019         if (ret < 0)
1020                 return ret;
1021
1022         /* Datawidth is now guaranteed to be equal to one of the three values.
1023          * We fix bit-per-pixel equal to data-width... */
1024         switch (common_flags & SOCAM_DATAWIDTH_MASK) {
1025         case SOCAM_DATAWIDTH_10:
1026                 dw = 4;
1027                 bpp = 0x40;
1028                 break;
1029         case SOCAM_DATAWIDTH_9:
1030                 dw = 3;
1031                 bpp = 0x20;
1032                 break;
1033         default:
1034                 /* Actually it can only be 8 now,
1035                  * default is just to silence compiler warnings */
1036         case SOCAM_DATAWIDTH_8:
1037                 dw = 2;
1038                 bpp = 0;
1039         }
1040
1041         if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
1042                 cicr4 |= CICR4_PCLK_EN;
1043         if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
1044                 cicr4 |= CICR4_MCLK_EN;
1045         if (common_flags & SOCAM_PCLK_SAMPLE_FALLING)
1046                 cicr4 |= CICR4_PCP;
1047         if (common_flags & SOCAM_HSYNC_ACTIVE_LOW)
1048                 cicr4 |= CICR4_HSP;
1049         if (common_flags & SOCAM_VSYNC_ACTIVE_LOW)
1050                 cicr4 |= CICR4_VSP;
1051
1052         cicr0 = __raw_readl(pcdev->base + CICR0);
1053         if (cicr0 & CICR0_ENB)
1054                 __raw_writel(cicr0 & ~CICR0_ENB, pcdev->base + CICR0);
1055
1056         cicr1 = CICR1_PPL_VAL(icd->width - 1) | bpp | dw;
1057
1058         switch (pixfmt) {
1059         case V4L2_PIX_FMT_YUV422P:
1060                 pcdev->channels = 3;
1061                 cicr1 |= CICR1_YCBCR_F;
1062                 /*
1063                  * Normally, pxa bus wants as input UYVY format. We allow all
1064                  * reorderings of the YUV422 format, as no processing is done,
1065                  * and the YUV stream is just passed through without any
1066                  * transformation. Note that UYVY is the only format that
1067                  * should be used if pxa framebuffer Overlay2 is used.
1068                  */
1069         case V4L2_PIX_FMT_UYVY:
1070         case V4L2_PIX_FMT_VYUY:
1071         case V4L2_PIX_FMT_YUYV:
1072         case V4L2_PIX_FMT_YVYU:
1073                 cicr1 |= CICR1_COLOR_SP_VAL(2);
1074                 break;
1075         case V4L2_PIX_FMT_RGB555:
1076                 cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) |
1077                         CICR1_TBIT | CICR1_COLOR_SP_VAL(1);
1078                 break;
1079         case V4L2_PIX_FMT_RGB565:
1080                 cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2);
1081                 break;
1082         }
1083
1084         cicr2 = 0;
1085         cicr3 = CICR3_LPF_VAL(icd->height - 1) |
1086                 CICR3_BFW_VAL(min((unsigned short)255, icd->y_skip_top));
1087         cicr4 |= pcdev->mclk_divisor;
1088
1089         __raw_writel(cicr1, pcdev->base + CICR1);
1090         __raw_writel(cicr2, pcdev->base + CICR2);
1091         __raw_writel(cicr3, pcdev->base + CICR3);
1092         __raw_writel(cicr4, pcdev->base + CICR4);
1093
1094         /* CIF interrupts are not used, only DMA */
1095         cicr0 = (cicr0 & CICR0_ENB) | (pcdev->platform_flags & PXA_CAMERA_MASTER ?
1096                 CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP));
1097         cicr0 |= CICR0_DMAEN | CICR0_IRQ_MASK;
1098         __raw_writel(cicr0, pcdev->base + CICR0);
1099
1100         return 0;
1101 }
1102
1103 static int pxa_camera_try_bus_param(struct soc_camera_device *icd,
1104                                     unsigned char buswidth)
1105 {
1106         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1107         struct pxa_camera_dev *pcdev = ici->priv;
1108         unsigned long bus_flags, camera_flags;
1109         int ret = test_platform_param(pcdev, buswidth, &bus_flags);
1110
1111         if (ret < 0)
1112                 return ret;
1113
1114         camera_flags = icd->ops->query_bus_param(icd);
1115
1116         return soc_camera_bus_param_compatible(camera_flags, bus_flags) ? 0 : -EINVAL;
1117 }
1118
1119 static const struct soc_camera_data_format pxa_camera_formats[] = {
1120         {
1121                 .name           = "Planar YUV422 16 bit",
1122                 .depth          = 16,
1123                 .fourcc         = V4L2_PIX_FMT_YUV422P,
1124                 .colorspace     = V4L2_COLORSPACE_JPEG,
1125         },
1126 };
1127
1128 static bool buswidth_supported(struct soc_camera_device *icd, int depth)
1129 {
1130         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1131         struct pxa_camera_dev *pcdev = ici->priv;
1132
1133         switch (depth) {
1134         case 8:
1135                 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8);
1136         case 9:
1137                 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9);
1138         case 10:
1139                 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10);
1140         }
1141         return false;
1142 }
1143
1144 static int required_buswidth(const struct soc_camera_data_format *fmt)
1145 {
1146         switch (fmt->fourcc) {
1147         case V4L2_PIX_FMT_UYVY:
1148         case V4L2_PIX_FMT_VYUY:
1149         case V4L2_PIX_FMT_YUYV:
1150         case V4L2_PIX_FMT_YVYU:
1151         case V4L2_PIX_FMT_RGB565:
1152         case V4L2_PIX_FMT_RGB555:
1153                 return 8;
1154         default:
1155                 return fmt->depth;
1156         }
1157 }
1158
1159 static int pxa_camera_get_formats(struct soc_camera_device *icd, int idx,
1160                                   struct soc_camera_format_xlate *xlate)
1161 {
1162         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1163         int formats = 0, buswidth, ret;
1164
1165         buswidth = required_buswidth(icd->formats + idx);
1166
1167         if (!buswidth_supported(icd, buswidth))
1168                 return 0;
1169
1170         ret = pxa_camera_try_bus_param(icd, buswidth);
1171         if (ret < 0)
1172                 return 0;
1173
1174         switch (icd->formats[idx].fourcc) {
1175         case V4L2_PIX_FMT_UYVY:
1176                 formats++;
1177                 if (xlate) {
1178                         xlate->host_fmt = &pxa_camera_formats[0];
1179                         xlate->cam_fmt = icd->formats + idx;
1180                         xlate->buswidth = buswidth;
1181                         xlate++;
1182                         dev_dbg(&ici->dev, "Providing format %s using %s\n",
1183                                 pxa_camera_formats[0].name,
1184                                 icd->formats[idx].name);
1185                 }
1186         case V4L2_PIX_FMT_VYUY:
1187         case V4L2_PIX_FMT_YUYV:
1188         case V4L2_PIX_FMT_YVYU:
1189         case V4L2_PIX_FMT_RGB565:
1190         case V4L2_PIX_FMT_RGB555:
1191                 formats++;
1192                 if (xlate) {
1193                         xlate->host_fmt = icd->formats + idx;
1194                         xlate->cam_fmt = icd->formats + idx;
1195                         xlate->buswidth = buswidth;
1196                         xlate++;
1197                         dev_dbg(&ici->dev, "Providing format %s packed\n",
1198                                 icd->formats[idx].name);
1199                 }
1200                 break;
1201         default:
1202                 /* Generic pass-through */
1203                 formats++;
1204                 if (xlate) {
1205                         xlate->host_fmt = icd->formats + idx;
1206                         xlate->cam_fmt = icd->formats + idx;
1207                         xlate->buswidth = icd->formats[idx].depth;
1208                         xlate++;
1209                         dev_dbg(&ici->dev,
1210                                 "Providing format %s in pass-through mode\n",
1211                                 icd->formats[idx].name);
1212                 }
1213         }
1214
1215         return formats;
1216 }
1217
1218 static int pxa_camera_set_crop(struct soc_camera_device *icd,
1219                                struct v4l2_rect *rect)
1220 {
1221         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1222         struct pxa_camera_dev *pcdev = ici->priv;
1223         struct soc_camera_sense sense = {
1224                 .master_clock = pcdev->mclk,
1225                 .pixel_clock_max = pcdev->ciclk / 4,
1226         };
1227         int ret;
1228
1229         /* If PCLK is used to latch data from the sensor, check sense */
1230         if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
1231                 icd->sense = &sense;
1232
1233         ret = icd->ops->set_crop(icd, rect);
1234
1235         icd->sense = NULL;
1236
1237         if (ret < 0) {
1238                 dev_warn(&ici->dev, "Failed to crop to %ux%u@%u:%u\n",
1239                          rect->width, rect->height, rect->left, rect->top);
1240         } else if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) {
1241                 if (sense.pixel_clock > sense.pixel_clock_max) {
1242                         dev_err(&ici->dev,
1243                                 "pixel clock %lu set by the camera too high!",
1244                                 sense.pixel_clock);
1245                         return -EIO;
1246                 }
1247                 recalculate_fifo_timeout(pcdev, sense.pixel_clock);
1248         }
1249
1250         return ret;
1251 }
1252
1253 static int pxa_camera_set_fmt(struct soc_camera_device *icd,
1254                               struct v4l2_format *f)
1255 {
1256         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1257         struct pxa_camera_dev *pcdev = ici->priv;
1258         const struct soc_camera_data_format *cam_fmt = NULL;
1259         const struct soc_camera_format_xlate *xlate = NULL;
1260         struct soc_camera_sense sense = {
1261                 .master_clock = pcdev->mclk,
1262                 .pixel_clock_max = pcdev->ciclk / 4,
1263         };
1264         struct v4l2_pix_format *pix = &f->fmt.pix;
1265         struct v4l2_format cam_f = *f;
1266         int ret;
1267
1268         xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
1269         if (!xlate) {
1270                 dev_warn(&ici->dev, "Format %x not found\n", pix->pixelformat);
1271                 return -EINVAL;
1272         }
1273
1274         cam_fmt = xlate->cam_fmt;
1275
1276         /* If PCLK is used to latch data from the sensor, check sense */
1277         if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
1278                 icd->sense = &sense;
1279
1280         cam_f.fmt.pix.pixelformat = cam_fmt->fourcc;
1281         ret = icd->ops->set_fmt(icd, &cam_f);
1282
1283         icd->sense = NULL;
1284
1285         if (ret < 0) {
1286                 dev_warn(&ici->dev, "Failed to configure for format %x\n",
1287                          pix->pixelformat);
1288         } else if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) {
1289                 if (sense.pixel_clock > sense.pixel_clock_max) {
1290                         dev_err(&ici->dev,
1291                                 "pixel clock %lu set by the camera too high!",
1292                                 sense.pixel_clock);
1293                         return -EIO;
1294                 }
1295                 recalculate_fifo_timeout(pcdev, sense.pixel_clock);
1296         }
1297
1298         if (!ret) {
1299                 icd->buswidth = xlate->buswidth;
1300                 icd->current_fmt = xlate->host_fmt;
1301         }
1302
1303         return ret;
1304 }
1305
1306 static int pxa_camera_try_fmt(struct soc_camera_device *icd,
1307                               struct v4l2_format *f)
1308 {
1309         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1310         const struct soc_camera_format_xlate *xlate;
1311         struct v4l2_pix_format *pix = &f->fmt.pix;
1312         __u32 pixfmt = pix->pixelformat;
1313         enum v4l2_field field;
1314         int ret;
1315
1316         xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1317         if (!xlate) {
1318                 dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
1319                 return -EINVAL;
1320         }
1321
1322         /* limit to pxa hardware capabilities */
1323         if (pix->height < 32)
1324                 pix->height = 32;
1325         if (pix->height > 2048)
1326                 pix->height = 2048;
1327         if (pix->width < 48)
1328                 pix->width = 48;
1329         if (pix->width > 2048)
1330                 pix->width = 2048;
1331         pix->width &= ~0x01;
1332
1333         /*
1334          * YUV422P planar format requires images size to be a 16 bytes
1335          * multiple. If not, zeros will be inserted between Y and U planes, and
1336          * U and V planes, and YUV422P standard would be violated.
1337          */
1338         if (xlate->host_fmt->fourcc == V4L2_PIX_FMT_YUV422P) {
1339                 if (!IS_ALIGNED(pix->width * pix->height, YUV422P_SIZE_ALIGN))
1340                         pix->height = ALIGN(pix->height, YUV422P_X_Y_ALIGN);
1341                 if (!IS_ALIGNED(pix->width * pix->height, YUV422P_SIZE_ALIGN))
1342                         pix->width = ALIGN(pix->width, YUV422P_X_Y_ALIGN);
1343         }
1344
1345         pix->bytesperline = pix->width *
1346                 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
1347         pix->sizeimage = pix->height * pix->bytesperline;
1348
1349         /* camera has to see its format, but the user the original one */
1350         pix->pixelformat = xlate->cam_fmt->fourcc;
1351         /* limit to sensor capabilities */
1352         ret = icd->ops->try_fmt(icd, f);
1353         pix->pixelformat = xlate->host_fmt->fourcc;
1354
1355         field = pix->field;
1356
1357         if (field == V4L2_FIELD_ANY) {
1358                 pix->field = V4L2_FIELD_NONE;
1359         } else if (field != V4L2_FIELD_NONE) {
1360                 dev_err(&icd->dev, "Field type %d unsupported.\n", field);
1361                 return -EINVAL;
1362         }
1363
1364         return ret;
1365 }
1366
1367 static int pxa_camera_reqbufs(struct soc_camera_file *icf,
1368                               struct v4l2_requestbuffers *p)
1369 {
1370         int i;
1371
1372         /* This is for locking debugging only. I removed spinlocks and now I
1373          * check whether .prepare is ever called on a linked buffer, or whether
1374          * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1375          * it hadn't triggered */
1376         for (i = 0; i < p->count; i++) {
1377                 struct pxa_buffer *buf = container_of(icf->vb_vidq.bufs[i],
1378                                                       struct pxa_buffer, vb);
1379                 buf->inwork = 0;
1380                 INIT_LIST_HEAD(&buf->vb.queue);
1381         }
1382
1383         return 0;
1384 }
1385
1386 static unsigned int pxa_camera_poll(struct file *file, poll_table *pt)
1387 {
1388         struct soc_camera_file *icf = file->private_data;
1389         struct pxa_buffer *buf;
1390
1391         buf = list_entry(icf->vb_vidq.stream.next, struct pxa_buffer,
1392                          vb.stream);
1393
1394         poll_wait(file, &buf->vb.done, pt);
1395
1396         if (buf->vb.state == VIDEOBUF_DONE ||
1397             buf->vb.state == VIDEOBUF_ERROR)
1398                 return POLLIN|POLLRDNORM;
1399
1400         return 0;
1401 }
1402
1403 static int pxa_camera_querycap(struct soc_camera_host *ici,
1404                                struct v4l2_capability *cap)
1405 {
1406         /* cap->name is set by the firendly caller:-> */
1407         strlcpy(cap->card, pxa_cam_driver_description, sizeof(cap->card));
1408         cap->version = PXA_CAM_VERSION_CODE;
1409         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1410
1411         return 0;
1412 }
1413
1414 static int pxa_camera_suspend(struct soc_camera_device *icd, pm_message_t state)
1415 {
1416         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1417         struct pxa_camera_dev *pcdev = ici->priv;
1418         int i = 0, ret = 0;
1419
1420         pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR0);
1421         pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR1);
1422         pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR2);
1423         pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR3);
1424         pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR4);
1425
1426         if ((pcdev->icd) && (pcdev->icd->ops->suspend))
1427                 ret = pcdev->icd->ops->suspend(pcdev->icd, state);
1428
1429         return ret;
1430 }
1431
1432 static int pxa_camera_resume(struct soc_camera_device *icd)
1433 {
1434         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1435         struct pxa_camera_dev *pcdev = ici->priv;
1436         int i = 0, ret = 0;
1437
1438         DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
1439         DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
1440         DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
1441
1442         __raw_writel(pcdev->save_cicr[i++] & ~CICR0_ENB, pcdev->base + CICR0);
1443         __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR1);
1444         __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR2);
1445         __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR3);
1446         __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR4);
1447
1448         if ((pcdev->icd) && (pcdev->icd->ops->resume))
1449                 ret = pcdev->icd->ops->resume(pcdev->icd);
1450
1451         /* Restart frame capture if active buffer exists */
1452         if (!ret && pcdev->active) {
1453                 unsigned long cifr, cicr0;
1454
1455                 /* Reset the FIFOs */
1456                 cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
1457                 __raw_writel(cifr, pcdev->base + CIFR);
1458
1459                 cicr0 = __raw_readl(pcdev->base + CICR0);
1460                 cicr0 &= ~CICR0_EOFM;   /* Enable End-Of-Frame Interrupt */
1461                 cicr0 |= CICR0_ENB;     /* Restart the Capture Interface */
1462                 __raw_writel(cicr0, pcdev->base + CICR0);
1463         }
1464
1465         return ret;
1466 }
1467
1468 static struct soc_camera_host_ops pxa_soc_camera_host_ops = {
1469         .owner          = THIS_MODULE,
1470         .add            = pxa_camera_add_device,
1471         .remove         = pxa_camera_remove_device,
1472         .suspend        = pxa_camera_suspend,
1473         .resume         = pxa_camera_resume,
1474         .set_crop       = pxa_camera_set_crop,
1475         .get_formats    = pxa_camera_get_formats,
1476         .set_fmt        = pxa_camera_set_fmt,
1477         .try_fmt        = pxa_camera_try_fmt,
1478         .init_videobuf  = pxa_camera_init_videobuf,
1479         .reqbufs        = pxa_camera_reqbufs,
1480         .poll           = pxa_camera_poll,
1481         .querycap       = pxa_camera_querycap,
1482         .set_bus_param  = pxa_camera_set_bus_param,
1483 };
1484
1485 /* Should be allocated dynamically too, but we have only one. */
1486 static struct soc_camera_host pxa_soc_camera_host = {
1487         .drv_name               = PXA_CAM_DRV_NAME,
1488         .ops                    = &pxa_soc_camera_host_ops,
1489 };
1490
1491 static int pxa_camera_probe(struct platform_device *pdev)
1492 {
1493         struct pxa_camera_dev *pcdev;
1494         struct resource *res;
1495         void __iomem *base;
1496         int irq;
1497         int err = 0;
1498
1499         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1500         irq = platform_get_irq(pdev, 0);
1501         if (!res || irq < 0) {
1502                 err = -ENODEV;
1503                 goto exit;
1504         }
1505
1506         pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1507         if (!pcdev) {
1508                 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1509                 err = -ENOMEM;
1510                 goto exit;
1511         }
1512
1513         pcdev->clk = clk_get(&pdev->dev, NULL);
1514         if (IS_ERR(pcdev->clk)) {
1515                 err = PTR_ERR(pcdev->clk);
1516                 goto exit_kfree;
1517         }
1518
1519         dev_set_drvdata(&pdev->dev, pcdev);
1520         pcdev->res = res;
1521
1522         pcdev->pdata = pdev->dev.platform_data;
1523         pcdev->platform_flags = pcdev->pdata->flags;
1524         if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
1525                         PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) {
1526                 /* Platform hasn't set available data widths. This is bad.
1527                  * Warn and use a default. */
1528                 dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
1529                          "data widths, using default 10 bit\n");
1530                 pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
1531         }
1532         pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
1533         if (!pcdev->mclk) {
1534                 dev_warn(&pdev->dev,
1535                          "mclk == 0! Please, fix your platform data. "
1536                          "Using default 20MHz\n");
1537                 pcdev->mclk = 20000000;
1538         }
1539
1540         pcdev->dev = &pdev->dev;
1541         pcdev->mclk_divisor = mclk_get_divisor(pcdev);
1542
1543         INIT_LIST_HEAD(&pcdev->capture);
1544         spin_lock_init(&pcdev->lock);
1545
1546         /*
1547          * Request the regions.
1548          */
1549         if (!request_mem_region(res->start, res->end - res->start + 1,
1550                                 PXA_CAM_DRV_NAME)) {
1551                 err = -EBUSY;
1552                 goto exit_clk;
1553         }
1554
1555         base = ioremap(res->start, res->end - res->start + 1);
1556         if (!base) {
1557                 err = -ENOMEM;
1558                 goto exit_release;
1559         }
1560         pcdev->irq = irq;
1561         pcdev->base = base;
1562
1563         /* request dma */
1564         err = pxa_request_dma("CI_Y", DMA_PRIO_HIGH,
1565                               pxa_camera_dma_irq_y, pcdev);
1566         if (err < 0) {
1567                 dev_err(pcdev->dev, "Can't request DMA for Y\n");
1568                 goto exit_iounmap;
1569         }
1570         pcdev->dma_chans[0] = err;
1571         dev_dbg(pcdev->dev, "got DMA channel %d\n", pcdev->dma_chans[0]);
1572
1573         err = pxa_request_dma("CI_U", DMA_PRIO_HIGH,
1574                               pxa_camera_dma_irq_u, pcdev);
1575         if (err < 0) {
1576                 dev_err(pcdev->dev, "Can't request DMA for U\n");
1577                 goto exit_free_dma_y;
1578         }
1579         pcdev->dma_chans[1] = err;
1580         dev_dbg(pcdev->dev, "got DMA channel (U) %d\n", pcdev->dma_chans[1]);
1581
1582         err = pxa_request_dma("CI_V", DMA_PRIO_HIGH,
1583                               pxa_camera_dma_irq_v, pcdev);
1584         if (err < 0) {
1585                 dev_err(pcdev->dev, "Can't request DMA for V\n");
1586                 goto exit_free_dma_u;
1587         }
1588         pcdev->dma_chans[2] = err;
1589         dev_dbg(pcdev->dev, "got DMA channel (V) %d\n", pcdev->dma_chans[2]);
1590
1591         DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
1592         DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
1593         DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
1594
1595         /* request irq */
1596         err = request_irq(pcdev->irq, pxa_camera_irq, 0, PXA_CAM_DRV_NAME,
1597                           pcdev);
1598         if (err) {
1599                 dev_err(pcdev->dev, "Camera interrupt register failed \n");
1600                 goto exit_free_dma;
1601         }
1602
1603         pxa_soc_camera_host.priv        = pcdev;
1604         pxa_soc_camera_host.dev.parent  = &pdev->dev;
1605         pxa_soc_camera_host.nr          = pdev->id;
1606         err = soc_camera_host_register(&pxa_soc_camera_host);
1607         if (err)
1608                 goto exit_free_irq;
1609
1610         return 0;
1611
1612 exit_free_irq:
1613         free_irq(pcdev->irq, pcdev);
1614 exit_free_dma:
1615         pxa_free_dma(pcdev->dma_chans[2]);
1616 exit_free_dma_u:
1617         pxa_free_dma(pcdev->dma_chans[1]);
1618 exit_free_dma_y:
1619         pxa_free_dma(pcdev->dma_chans[0]);
1620 exit_iounmap:
1621         iounmap(base);
1622 exit_release:
1623         release_mem_region(res->start, res->end - res->start + 1);
1624 exit_clk:
1625         clk_put(pcdev->clk);
1626 exit_kfree:
1627         kfree(pcdev);
1628 exit:
1629         return err;
1630 }
1631
1632 static int __devexit pxa_camera_remove(struct platform_device *pdev)
1633 {
1634         struct pxa_camera_dev *pcdev = platform_get_drvdata(pdev);
1635         struct resource *res;
1636
1637         clk_put(pcdev->clk);
1638
1639         pxa_free_dma(pcdev->dma_chans[0]);
1640         pxa_free_dma(pcdev->dma_chans[1]);
1641         pxa_free_dma(pcdev->dma_chans[2]);
1642         free_irq(pcdev->irq, pcdev);
1643
1644         soc_camera_host_unregister(&pxa_soc_camera_host);
1645
1646         iounmap(pcdev->base);
1647
1648         res = pcdev->res;
1649         release_mem_region(res->start, res->end - res->start + 1);
1650
1651         kfree(pcdev);
1652
1653         dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
1654
1655         return 0;
1656 }
1657
1658 static struct platform_driver pxa_camera_driver = {
1659         .driver         = {
1660                 .name   = PXA_CAM_DRV_NAME,
1661         },
1662         .probe          = pxa_camera_probe,
1663         .remove         = __exit_p(pxa_camera_remove),
1664 };
1665
1666
1667 static int __devinit pxa_camera_init(void)
1668 {
1669         return platform_driver_register(&pxa_camera_driver);
1670 }
1671
1672 static void __exit pxa_camera_exit(void)
1673 {
1674         platform_driver_unregister(&pxa_camera_driver);
1675 }
1676
1677 module_init(pxa_camera_init);
1678 module_exit(pxa_camera_exit);
1679
1680 MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
1681 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1682 MODULE_LICENSE("GPL");