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