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