[media] media: videobuf2: Restructure vb2_buffer
[firefly-linux-kernel-4.4.55.git] / drivers / media / platform / soc_camera / mx3_camera.c
1 /*
2  * V4L2 Driver for i.MX3x camera host
3  *
4  * Copyright (C) 2008
5  * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.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 version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/videodev2.h>
15 #include <linux/platform_device.h>
16 #include <linux/clk.h>
17 #include <linux/vmalloc.h>
18 #include <linux/interrupt.h>
19 #include <linux/sched.h>
20 #include <linux/dma/ipu-dma.h>
21
22 #include <media/v4l2-common.h>
23 #include <media/v4l2-dev.h>
24 #include <media/videobuf2-dma-contig.h>
25 #include <media/soc_camera.h>
26 #include <media/soc_mediabus.h>
27
28 #include <linux/platform_data/camera-mx3.h>
29 #include <linux/platform_data/dma-imx.h>
30
31 #define MX3_CAM_DRV_NAME "mx3-camera"
32
33 /* CMOS Sensor Interface Registers */
34 #define CSI_REG_START           0x60
35
36 #define CSI_SENS_CONF           (0x60 - CSI_REG_START)
37 #define CSI_SENS_FRM_SIZE       (0x64 - CSI_REG_START)
38 #define CSI_ACT_FRM_SIZE        (0x68 - CSI_REG_START)
39 #define CSI_OUT_FRM_CTRL        (0x6C - CSI_REG_START)
40 #define CSI_TST_CTRL            (0x70 - CSI_REG_START)
41 #define CSI_CCIR_CODE_1         (0x74 - CSI_REG_START)
42 #define CSI_CCIR_CODE_2         (0x78 - CSI_REG_START)
43 #define CSI_CCIR_CODE_3         (0x7C - CSI_REG_START)
44 #define CSI_FLASH_STROBE_1      (0x80 - CSI_REG_START)
45 #define CSI_FLASH_STROBE_2      (0x84 - CSI_REG_START)
46
47 #define CSI_SENS_CONF_VSYNC_POL_SHIFT           0
48 #define CSI_SENS_CONF_HSYNC_POL_SHIFT           1
49 #define CSI_SENS_CONF_DATA_POL_SHIFT            2
50 #define CSI_SENS_CONF_PIX_CLK_POL_SHIFT         3
51 #define CSI_SENS_CONF_SENS_PRTCL_SHIFT          4
52 #define CSI_SENS_CONF_SENS_CLKSRC_SHIFT         7
53 #define CSI_SENS_CONF_DATA_FMT_SHIFT            8
54 #define CSI_SENS_CONF_DATA_WIDTH_SHIFT          10
55 #define CSI_SENS_CONF_EXT_VSYNC_SHIFT           15
56 #define CSI_SENS_CONF_DIVRATIO_SHIFT            16
57
58 #define CSI_SENS_CONF_DATA_FMT_RGB_YUV444       (0UL << CSI_SENS_CONF_DATA_FMT_SHIFT)
59 #define CSI_SENS_CONF_DATA_FMT_YUV422           (2UL << CSI_SENS_CONF_DATA_FMT_SHIFT)
60 #define CSI_SENS_CONF_DATA_FMT_BAYER            (3UL << CSI_SENS_CONF_DATA_FMT_SHIFT)
61
62 #define MAX_VIDEO_MEM 16
63
64 struct mx3_camera_buffer {
65         /* common v4l buffer stuff -- must be first */
66         struct vb2_v4l2_buffer vb;
67         struct list_head                        queue;
68
69         /* One descriptot per scatterlist (per frame) */
70         struct dma_async_tx_descriptor          *txd;
71
72         /* We have to "build" a scatterlist ourselves - one element per frame */
73         struct scatterlist                      sg;
74 };
75
76 /**
77  * struct mx3_camera_dev - i.MX3x camera (CSI) object
78  * @dev:                camera device, to which the coherent buffer is attached
79  * @icd:                currently attached camera sensor
80  * @clk:                pointer to clock
81  * @base:               remapped register base address
82  * @pdata:              platform data
83  * @platform_flags:     platform flags
84  * @mclk:               master clock frequency in Hz
85  * @capture:            list of capture videobuffers
86  * @lock:               protects video buffer lists
87  * @active:             active video buffer
88  * @idmac_channel:      array of pointers to IPU DMAC DMA channels
89  * @soc_host:           embedded soc_host object
90  */
91 struct mx3_camera_dev {
92         /*
93          * i.MX3x is only supposed to handle one camera on its Camera Sensor
94          * Interface. If anyone ever builds hardware to enable more than one
95          * camera _simultaneously_, they will have to modify this driver too
96          */
97         struct clk              *clk;
98
99         void __iomem            *base;
100
101         struct mx3_camera_pdata *pdata;
102
103         unsigned long           platform_flags;
104         unsigned long           mclk;
105         u16                     width_flags;    /* max 15 bits */
106
107         struct list_head        capture;
108         spinlock_t              lock;           /* Protects video buffer lists */
109         struct mx3_camera_buffer *active;
110         size_t                  buf_total;
111         struct vb2_alloc_ctx    *alloc_ctx;
112         enum v4l2_field         field;
113         int                     sequence;
114
115         /* IDMAC / dmaengine interface */
116         struct idmac_channel    *idmac_channel[1];      /* We need one channel */
117
118         struct soc_camera_host  soc_host;
119 };
120
121 struct dma_chan_request {
122         struct mx3_camera_dev   *mx3_cam;
123         enum ipu_channel        id;
124 };
125
126 static u32 csi_reg_read(struct mx3_camera_dev *mx3, off_t reg)
127 {
128         return __raw_readl(mx3->base + reg);
129 }
130
131 static void csi_reg_write(struct mx3_camera_dev *mx3, u32 value, off_t reg)
132 {
133         __raw_writel(value, mx3->base + reg);
134 }
135
136 static struct mx3_camera_buffer *to_mx3_vb(struct vb2_v4l2_buffer *vb)
137 {
138         return container_of(vb, struct mx3_camera_buffer, vb);
139 }
140
141 /* Called from the IPU IDMAC ISR */
142 static void mx3_cam_dma_done(void *arg)
143 {
144         struct idmac_tx_desc *desc = to_tx_desc(arg);
145         struct dma_chan *chan = desc->txd.chan;
146         struct idmac_channel *ichannel = to_idmac_chan(chan);
147         struct mx3_camera_dev *mx3_cam = ichannel->client;
148
149         dev_dbg(chan->device->dev, "callback cookie %d, active DMA 0x%08x\n",
150                 desc->txd.cookie, mx3_cam->active ? sg_dma_address(&mx3_cam->active->sg) : 0);
151
152         spin_lock(&mx3_cam->lock);
153         if (mx3_cam->active) {
154                 struct vb2_v4l2_buffer *vb = &mx3_cam->active->vb;
155                 struct mx3_camera_buffer *buf = to_mx3_vb(vb);
156
157                 list_del_init(&buf->queue);
158                 v4l2_get_timestamp(&vb->timestamp);
159                 vb->field = mx3_cam->field;
160                 vb->sequence = mx3_cam->sequence++;
161                 vb2_buffer_done(&vb->vb2_buf, VB2_BUF_STATE_DONE);
162         }
163
164         if (list_empty(&mx3_cam->capture)) {
165                 mx3_cam->active = NULL;
166                 spin_unlock(&mx3_cam->lock);
167
168                 /*
169                  * stop capture - without further buffers IPU_CHA_BUF0_RDY will
170                  * not get updated
171                  */
172                 return;
173         }
174
175         mx3_cam->active = list_entry(mx3_cam->capture.next,
176                                      struct mx3_camera_buffer, queue);
177         spin_unlock(&mx3_cam->lock);
178 }
179
180 /*
181  * Videobuf operations
182  */
183
184 /*
185  * Calculate the __buffer__ (not data) size and number of buffers.
186  */
187 static int mx3_videobuf_setup(struct vb2_queue *vq,
188                         const struct v4l2_format *fmt,
189                         unsigned int *count, unsigned int *num_planes,
190                         unsigned int sizes[], void *alloc_ctxs[])
191 {
192         struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
193         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
194         struct mx3_camera_dev *mx3_cam = ici->priv;
195
196         if (!mx3_cam->idmac_channel[0])
197                 return -EINVAL;
198
199         if (fmt) {
200                 const struct soc_camera_format_xlate *xlate = soc_camera_xlate_by_fourcc(icd,
201                                                                 fmt->fmt.pix.pixelformat);
202                 unsigned int bytes_per_line;
203                 int ret;
204
205                 if (!xlate)
206                         return -EINVAL;
207
208                 ret = soc_mbus_bytes_per_line(fmt->fmt.pix.width,
209                                               xlate->host_fmt);
210                 if (ret < 0)
211                         return ret;
212
213                 bytes_per_line = max_t(u32, fmt->fmt.pix.bytesperline, ret);
214
215                 ret = soc_mbus_image_size(xlate->host_fmt, bytes_per_line,
216                                           fmt->fmt.pix.height);
217                 if (ret < 0)
218                         return ret;
219
220                 sizes[0] = max_t(u32, fmt->fmt.pix.sizeimage, ret);
221         } else {
222                 /* Called from VIDIOC_REQBUFS or in compatibility mode */
223                 sizes[0] = icd->sizeimage;
224         }
225
226         alloc_ctxs[0] = mx3_cam->alloc_ctx;
227
228         if (!vq->num_buffers)
229                 mx3_cam->sequence = 0;
230
231         if (!*count)
232                 *count = 2;
233
234         /* If *num_planes != 0, we have already verified *count. */
235         if (!*num_planes &&
236             sizes[0] * *count + mx3_cam->buf_total > MAX_VIDEO_MEM * 1024 * 1024)
237                 *count = (MAX_VIDEO_MEM * 1024 * 1024 - mx3_cam->buf_total) /
238                         sizes[0];
239
240         *num_planes = 1;
241
242         return 0;
243 }
244
245 static enum pixel_fmt fourcc_to_ipu_pix(__u32 fourcc)
246 {
247         /* Add more formats as need arises and test possibilities appear... */
248         switch (fourcc) {
249         case V4L2_PIX_FMT_RGB24:
250                 return IPU_PIX_FMT_RGB24;
251         case V4L2_PIX_FMT_UYVY:
252         case V4L2_PIX_FMT_RGB565:
253         default:
254                 return IPU_PIX_FMT_GENERIC;
255         }
256 }
257
258 static void mx3_videobuf_queue(struct vb2_buffer *vb)
259 {
260         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
261         struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
262         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
263         struct mx3_camera_dev *mx3_cam = ici->priv;
264         struct mx3_camera_buffer *buf = to_mx3_vb(vbuf);
265         struct scatterlist *sg = &buf->sg;
266         struct dma_async_tx_descriptor *txd;
267         struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
268         struct idmac_video_param *video = &ichan->params.video;
269         const struct soc_mbus_pixelfmt *host_fmt = icd->current_fmt->host_fmt;
270         dma_cookie_t cookie;
271         size_t new_size;
272
273         new_size = icd->sizeimage;
274
275         if (vb2_plane_size(vb, 0) < new_size) {
276                 dev_err(icd->parent, "Buffer #%d too small (%lu < %zu)\n",
277                         vbuf->vb2_buf.index, vb2_plane_size(vb, 0), new_size);
278                 goto error;
279         }
280
281         if (!buf->txd) {
282                 sg_dma_address(sg)      = vb2_dma_contig_plane_dma_addr(vb, 0);
283                 sg_dma_len(sg)          = new_size;
284
285                 txd = dmaengine_prep_slave_sg(
286                         &ichan->dma_chan, sg, 1, DMA_DEV_TO_MEM,
287                         DMA_PREP_INTERRUPT);
288                 if (!txd)
289                         goto error;
290
291                 txd->callback_param     = txd;
292                 txd->callback           = mx3_cam_dma_done;
293
294                 buf->txd                = txd;
295         } else {
296                 txd = buf->txd;
297         }
298
299         vb2_set_plane_payload(vb, 0, new_size);
300
301         /* This is the configuration of one sg-element */
302         video->out_pixel_fmt = fourcc_to_ipu_pix(host_fmt->fourcc);
303
304         if (video->out_pixel_fmt == IPU_PIX_FMT_GENERIC) {
305                 /*
306                  * If the IPU DMA channel is configured to transfer generic
307                  * 8-bit data, we have to set up the geometry parameters
308                  * correctly, according to the current pixel format. The DMA
309                  * horizontal parameters in this case are expressed in bytes,
310                  * not in pixels.
311                  */
312                 video->out_width        = icd->bytesperline;
313                 video->out_height       = icd->user_height;
314                 video->out_stride       = icd->bytesperline;
315         } else {
316                 /*
317                  * For IPU known formats the pixel unit will be managed
318                  * successfully by the IPU code
319                  */
320                 video->out_width        = icd->user_width;
321                 video->out_height       = icd->user_height;
322                 video->out_stride       = icd->user_width;
323         }
324
325 #ifdef DEBUG
326         /* helps to see what DMA actually has written */
327         if (vb2_plane_vaddr(vb, 0))
328                 memset(vb2_plane_vaddr(vb, 0), 0xaa, vb2_get_plane_payload(vb, 0));
329 #endif
330
331         spin_lock_irq(&mx3_cam->lock);
332         list_add_tail(&buf->queue, &mx3_cam->capture);
333
334         if (!mx3_cam->active)
335                 mx3_cam->active = buf;
336
337         spin_unlock_irq(&mx3_cam->lock);
338
339         cookie = txd->tx_submit(txd);
340         dev_dbg(icd->parent, "Submitted cookie %d DMA 0x%08x\n",
341                 cookie, sg_dma_address(&buf->sg));
342
343         if (cookie >= 0)
344                 return;
345
346         spin_lock_irq(&mx3_cam->lock);
347
348         /* Submit error */
349         list_del_init(&buf->queue);
350
351         if (mx3_cam->active == buf)
352                 mx3_cam->active = NULL;
353
354         spin_unlock_irq(&mx3_cam->lock);
355 error:
356         vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
357 }
358
359 static void mx3_videobuf_release(struct vb2_buffer *vb)
360 {
361         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
362         struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
363         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
364         struct mx3_camera_dev *mx3_cam = ici->priv;
365         struct mx3_camera_buffer *buf = to_mx3_vb(vbuf);
366         struct dma_async_tx_descriptor *txd = buf->txd;
367         unsigned long flags;
368
369         dev_dbg(icd->parent,
370                 "Release%s DMA 0x%08x, queue %sempty\n",
371                 mx3_cam->active == buf ? " active" : "", sg_dma_address(&buf->sg),
372                 list_empty(&buf->queue) ? "" : "not ");
373
374         spin_lock_irqsave(&mx3_cam->lock, flags);
375
376         if (mx3_cam->active == buf)
377                 mx3_cam->active = NULL;
378
379         /* Doesn't hurt also if the list is empty */
380         list_del_init(&buf->queue);
381
382         if (txd) {
383                 buf->txd = NULL;
384                 if (mx3_cam->idmac_channel[0])
385                         async_tx_ack(txd);
386         }
387
388         spin_unlock_irqrestore(&mx3_cam->lock, flags);
389
390         mx3_cam->buf_total -= vb2_plane_size(vb, 0);
391 }
392
393 static int mx3_videobuf_init(struct vb2_buffer *vb)
394 {
395         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
396         struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
397         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
398         struct mx3_camera_dev *mx3_cam = ici->priv;
399         struct mx3_camera_buffer *buf = to_mx3_vb(vbuf);
400
401         if (!buf->txd) {
402                 /* This is for locking debugging only */
403                 INIT_LIST_HEAD(&buf->queue);
404                 sg_init_table(&buf->sg, 1);
405
406                 mx3_cam->buf_total += vb2_plane_size(vb, 0);
407         }
408
409         return 0;
410 }
411
412 static void mx3_stop_streaming(struct vb2_queue *q)
413 {
414         struct soc_camera_device *icd = soc_camera_from_vb2q(q);
415         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
416         struct mx3_camera_dev *mx3_cam = ici->priv;
417         struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
418         struct mx3_camera_buffer *buf, *tmp;
419         unsigned long flags;
420
421         if (ichan)
422                 dmaengine_pause(&ichan->dma_chan);
423
424         spin_lock_irqsave(&mx3_cam->lock, flags);
425
426         mx3_cam->active = NULL;
427
428         list_for_each_entry_safe(buf, tmp, &mx3_cam->capture, queue) {
429                 list_del_init(&buf->queue);
430                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
431         }
432
433         spin_unlock_irqrestore(&mx3_cam->lock, flags);
434 }
435
436 static struct vb2_ops mx3_videobuf_ops = {
437         .queue_setup    = mx3_videobuf_setup,
438         .buf_queue      = mx3_videobuf_queue,
439         .buf_cleanup    = mx3_videobuf_release,
440         .buf_init       = mx3_videobuf_init,
441         .wait_prepare   = vb2_ops_wait_prepare,
442         .wait_finish    = vb2_ops_wait_finish,
443         .stop_streaming = mx3_stop_streaming,
444 };
445
446 static int mx3_camera_init_videobuf(struct vb2_queue *q,
447                                      struct soc_camera_device *icd)
448 {
449         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
450
451         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
452         q->io_modes = VB2_MMAP | VB2_USERPTR;
453         q->drv_priv = icd;
454         q->ops = &mx3_videobuf_ops;
455         q->mem_ops = &vb2_dma_contig_memops;
456         q->buf_struct_size = sizeof(struct mx3_camera_buffer);
457         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
458         q->lock = &ici->host_lock;
459
460         return vb2_queue_init(q);
461 }
462
463 /* First part of ipu_csi_init_interface() */
464 static void mx3_camera_activate(struct mx3_camera_dev *mx3_cam)
465 {
466         u32 conf;
467         long rate;
468
469         /* Set default size: ipu_csi_set_window_size() */
470         csi_reg_write(mx3_cam, (640 - 1) | ((480 - 1) << 16), CSI_ACT_FRM_SIZE);
471         /* ...and position to 0:0: ipu_csi_set_window_pos() */
472         conf = csi_reg_read(mx3_cam, CSI_OUT_FRM_CTRL) & 0xffff0000;
473         csi_reg_write(mx3_cam, conf, CSI_OUT_FRM_CTRL);
474
475         /* We use only gated clock synchronisation mode so far */
476         conf = 0 << CSI_SENS_CONF_SENS_PRTCL_SHIFT;
477
478         /* Set generic data, platform-biggest bus-width */
479         conf |= CSI_SENS_CONF_DATA_FMT_BAYER;
480
481         if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_15)
482                 conf |= 3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
483         else if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_10)
484                 conf |= 2 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
485         else if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_8)
486                 conf |= 1 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
487         else/* if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_4)*/
488                 conf |= 0 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
489
490         if (mx3_cam->platform_flags & MX3_CAMERA_CLK_SRC)
491                 conf |= 1 << CSI_SENS_CONF_SENS_CLKSRC_SHIFT;
492         if (mx3_cam->platform_flags & MX3_CAMERA_EXT_VSYNC)
493                 conf |= 1 << CSI_SENS_CONF_EXT_VSYNC_SHIFT;
494         if (mx3_cam->platform_flags & MX3_CAMERA_DP)
495                 conf |= 1 << CSI_SENS_CONF_DATA_POL_SHIFT;
496         if (mx3_cam->platform_flags & MX3_CAMERA_PCP)
497                 conf |= 1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT;
498         if (mx3_cam->platform_flags & MX3_CAMERA_HSP)
499                 conf |= 1 << CSI_SENS_CONF_HSYNC_POL_SHIFT;
500         if (mx3_cam->platform_flags & MX3_CAMERA_VSP)
501                 conf |= 1 << CSI_SENS_CONF_VSYNC_POL_SHIFT;
502
503         /* ipu_csi_init_interface() */
504         csi_reg_write(mx3_cam, conf, CSI_SENS_CONF);
505
506         clk_prepare_enable(mx3_cam->clk);
507         rate = clk_round_rate(mx3_cam->clk, mx3_cam->mclk);
508         dev_dbg(mx3_cam->soc_host.v4l2_dev.dev, "Set SENS_CONF to %x, rate %ld\n", conf, rate);
509         if (rate)
510                 clk_set_rate(mx3_cam->clk, rate);
511 }
512
513 static int mx3_camera_add_device(struct soc_camera_device *icd)
514 {
515         dev_info(icd->parent, "MX3 Camera driver attached to camera %d\n",
516                  icd->devnum);
517
518         return 0;
519 }
520
521 static void mx3_camera_remove_device(struct soc_camera_device *icd)
522 {
523         dev_info(icd->parent, "MX3 Camera driver detached from camera %d\n",
524                  icd->devnum);
525 }
526
527 /* Called with .host_lock held */
528 static int mx3_camera_clock_start(struct soc_camera_host *ici)
529 {
530         struct mx3_camera_dev *mx3_cam = ici->priv;
531
532         mx3_camera_activate(mx3_cam);
533
534         mx3_cam->buf_total = 0;
535
536         return 0;
537 }
538
539 /* Called with .host_lock held */
540 static void mx3_camera_clock_stop(struct soc_camera_host *ici)
541 {
542         struct mx3_camera_dev *mx3_cam = ici->priv;
543         struct idmac_channel **ichan = &mx3_cam->idmac_channel[0];
544
545         if (*ichan) {
546                 dma_release_channel(&(*ichan)->dma_chan);
547                 *ichan = NULL;
548         }
549
550         clk_disable_unprepare(mx3_cam->clk);
551 }
552
553 static int test_platform_param(struct mx3_camera_dev *mx3_cam,
554                                unsigned char buswidth, unsigned long *flags)
555 {
556         /*
557          * If requested data width is supported by the platform, use it or any
558          * possible lower value - i.MX31 is smart enough to shift bits
559          */
560         if (buswidth > fls(mx3_cam->width_flags))
561                 return -EINVAL;
562
563         /*
564          * Platform specified synchronization and pixel clock polarities are
565          * only a recommendation and are only used during probing. MX3x
566          * camera interface only works in master mode, i.e., uses HSYNC and
567          * VSYNC signals from the sensor
568          */
569         *flags = V4L2_MBUS_MASTER |
570                 V4L2_MBUS_HSYNC_ACTIVE_HIGH |
571                 V4L2_MBUS_HSYNC_ACTIVE_LOW |
572                 V4L2_MBUS_VSYNC_ACTIVE_HIGH |
573                 V4L2_MBUS_VSYNC_ACTIVE_LOW |
574                 V4L2_MBUS_PCLK_SAMPLE_RISING |
575                 V4L2_MBUS_PCLK_SAMPLE_FALLING |
576                 V4L2_MBUS_DATA_ACTIVE_HIGH |
577                 V4L2_MBUS_DATA_ACTIVE_LOW;
578
579         return 0;
580 }
581
582 static int mx3_camera_try_bus_param(struct soc_camera_device *icd,
583                                     const unsigned int depth)
584 {
585         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
586         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
587         struct mx3_camera_dev *mx3_cam = ici->priv;
588         struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
589         unsigned long bus_flags, common_flags;
590         int ret = test_platform_param(mx3_cam, depth, &bus_flags);
591
592         dev_dbg(icd->parent, "request bus width %d bit: %d\n", depth, ret);
593
594         if (ret < 0)
595                 return ret;
596
597         ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
598         if (!ret) {
599                 common_flags = soc_mbus_config_compatible(&cfg,
600                                                           bus_flags);
601                 if (!common_flags) {
602                         dev_warn(icd->parent,
603                                  "Flags incompatible: camera 0x%x, host 0x%lx\n",
604                                  cfg.flags, bus_flags);
605                         return -EINVAL;
606                 }
607         } else if (ret != -ENOIOCTLCMD) {
608                 return ret;
609         }
610
611         return 0;
612 }
613
614 static bool chan_filter(struct dma_chan *chan, void *arg)
615 {
616         struct dma_chan_request *rq = arg;
617         struct mx3_camera_pdata *pdata;
618
619         if (!imx_dma_is_ipu(chan))
620                 return false;
621
622         if (!rq)
623                 return false;
624
625         pdata = rq->mx3_cam->soc_host.v4l2_dev.dev->platform_data;
626
627         return rq->id == chan->chan_id &&
628                 pdata->dma_dev == chan->device->dev;
629 }
630
631 static const struct soc_mbus_pixelfmt mx3_camera_formats[] = {
632         {
633                 .fourcc                 = V4L2_PIX_FMT_SBGGR8,
634                 .name                   = "Bayer BGGR (sRGB) 8 bit",
635                 .bits_per_sample        = 8,
636                 .packing                = SOC_MBUS_PACKING_NONE,
637                 .order                  = SOC_MBUS_ORDER_LE,
638                 .layout                 = SOC_MBUS_LAYOUT_PACKED,
639         }, {
640                 .fourcc                 = V4L2_PIX_FMT_GREY,
641                 .name                   = "Monochrome 8 bit",
642                 .bits_per_sample        = 8,
643                 .packing                = SOC_MBUS_PACKING_NONE,
644                 .order                  = SOC_MBUS_ORDER_LE,
645                 .layout                 = SOC_MBUS_LAYOUT_PACKED,
646         },
647 };
648
649 /* This will be corrected as we get more formats */
650 static bool mx3_camera_packing_supported(const struct soc_mbus_pixelfmt *fmt)
651 {
652         return  fmt->packing == SOC_MBUS_PACKING_NONE ||
653                 (fmt->bits_per_sample == 8 &&
654                  fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
655                 (fmt->bits_per_sample > 8 &&
656                  fmt->packing == SOC_MBUS_PACKING_EXTEND16);
657 }
658
659 static int mx3_camera_get_formats(struct soc_camera_device *icd, unsigned int idx,
660                                   struct soc_camera_format_xlate *xlate)
661 {
662         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
663         struct device *dev = icd->parent;
664         int formats = 0, ret;
665         struct v4l2_subdev_mbus_code_enum code = {
666                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
667                 .index = idx,
668         };
669         const struct soc_mbus_pixelfmt *fmt;
670
671         ret = v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code);
672         if (ret < 0)
673                 /* No more formats */
674                 return 0;
675
676         fmt = soc_mbus_get_fmtdesc(code.code);
677         if (!fmt) {
678                 dev_warn(icd->parent,
679                          "Unsupported format code #%u: 0x%x\n", idx, code.code);
680                 return 0;
681         }
682
683         /* This also checks support for the requested bits-per-sample */
684         ret = mx3_camera_try_bus_param(icd, fmt->bits_per_sample);
685         if (ret < 0)
686                 return 0;
687
688         switch (code.code) {
689         case MEDIA_BUS_FMT_SBGGR10_1X10:
690                 formats++;
691                 if (xlate) {
692                         xlate->host_fmt = &mx3_camera_formats[0];
693                         xlate->code     = code.code;
694                         xlate++;
695                         dev_dbg(dev, "Providing format %s using code 0x%x\n",
696                                 mx3_camera_formats[0].name, code.code);
697                 }
698                 break;
699         case MEDIA_BUS_FMT_Y10_1X10:
700                 formats++;
701                 if (xlate) {
702                         xlate->host_fmt = &mx3_camera_formats[1];
703                         xlate->code     = code.code;
704                         xlate++;
705                         dev_dbg(dev, "Providing format %s using code 0x%x\n",
706                                 mx3_camera_formats[1].name, code.code);
707                 }
708                 break;
709         default:
710                 if (!mx3_camera_packing_supported(fmt))
711                         return 0;
712         }
713
714         /* Generic pass-through */
715         formats++;
716         if (xlate) {
717                 xlate->host_fmt = fmt;
718                 xlate->code     = code.code;
719                 dev_dbg(dev, "Providing format %c%c%c%c in pass-through mode\n",
720                         (fmt->fourcc >> (0*8)) & 0xFF,
721                         (fmt->fourcc >> (1*8)) & 0xFF,
722                         (fmt->fourcc >> (2*8)) & 0xFF,
723                         (fmt->fourcc >> (3*8)) & 0xFF);
724                 xlate++;
725         }
726
727         return formats;
728 }
729
730 static void configure_geometry(struct mx3_camera_dev *mx3_cam,
731                                unsigned int width, unsigned int height,
732                                const struct soc_mbus_pixelfmt *fmt)
733 {
734         u32 ctrl, width_field, height_field;
735
736         if (fourcc_to_ipu_pix(fmt->fourcc) == IPU_PIX_FMT_GENERIC) {
737                 /*
738                  * As the CSI will be configured to output BAYER, here
739                  * the width parameter count the number of samples to
740                  * capture to complete the whole image width.
741                  */
742                 unsigned int num, den;
743                 int ret = soc_mbus_samples_per_pixel(fmt, &num, &den);
744                 BUG_ON(ret < 0);
745                 width = width * num / den;
746         }
747
748         /* Setup frame size - this cannot be changed on-the-fly... */
749         width_field = width - 1;
750         height_field = height - 1;
751         csi_reg_write(mx3_cam, width_field | (height_field << 16), CSI_SENS_FRM_SIZE);
752
753         csi_reg_write(mx3_cam, width_field << 16, CSI_FLASH_STROBE_1);
754         csi_reg_write(mx3_cam, (height_field << 16) | 0x22, CSI_FLASH_STROBE_2);
755
756         csi_reg_write(mx3_cam, width_field | (height_field << 16), CSI_ACT_FRM_SIZE);
757
758         /* ...and position */
759         ctrl = csi_reg_read(mx3_cam, CSI_OUT_FRM_CTRL) & 0xffff0000;
760         /* Sensor does the cropping */
761         csi_reg_write(mx3_cam, ctrl | 0 | (0 << 8), CSI_OUT_FRM_CTRL);
762 }
763
764 static int acquire_dma_channel(struct mx3_camera_dev *mx3_cam)
765 {
766         dma_cap_mask_t mask;
767         struct dma_chan *chan;
768         struct idmac_channel **ichan = &mx3_cam->idmac_channel[0];
769         /* We have to use IDMAC_IC_7 for Bayer / generic data */
770         struct dma_chan_request rq = {.mx3_cam = mx3_cam,
771                                       .id = IDMAC_IC_7};
772
773         dma_cap_zero(mask);
774         dma_cap_set(DMA_SLAVE, mask);
775         dma_cap_set(DMA_PRIVATE, mask);
776         chan = dma_request_channel(mask, chan_filter, &rq);
777         if (!chan)
778                 return -EBUSY;
779
780         *ichan = to_idmac_chan(chan);
781         (*ichan)->client = mx3_cam;
782
783         return 0;
784 }
785
786 /*
787  * FIXME: learn to use stride != width, then we can keep stride properly aligned
788  * and support arbitrary (even) widths.
789  */
790 static inline void stride_align(__u32 *width)
791 {
792         if (ALIGN(*width, 8) < 4096)
793                 *width = ALIGN(*width, 8);
794         else
795                 *width = *width &  ~7;
796 }
797
798 /*
799  * As long as we don't implement host-side cropping and scaling, we can use
800  * default g_crop and cropcap from soc_camera.c
801  */
802 static int mx3_camera_set_crop(struct soc_camera_device *icd,
803                                const struct v4l2_crop *a)
804 {
805         struct v4l2_crop a_writable = *a;
806         struct v4l2_rect *rect = &a_writable.c;
807         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
808         struct mx3_camera_dev *mx3_cam = ici->priv;
809         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
810         struct v4l2_subdev_format fmt = {
811                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
812         };
813         struct v4l2_mbus_framefmt *mf = &fmt.format;
814         int ret;
815
816         soc_camera_limit_side(&rect->left, &rect->width, 0, 2, 4096);
817         soc_camera_limit_side(&rect->top, &rect->height, 0, 2, 4096);
818
819         ret = v4l2_subdev_call(sd, video, s_crop, a);
820         if (ret < 0)
821                 return ret;
822
823         /* The capture device might have changed its output sizes */
824         ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt);
825         if (ret < 0)
826                 return ret;
827
828         if (mf->code != icd->current_fmt->code)
829                 return -EINVAL;
830
831         if (mf->width & 7) {
832                 /* Ouch! We can only handle 8-byte aligned width... */
833                 stride_align(&mf->width);
834                 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &fmt);
835                 if (ret < 0)
836                         return ret;
837         }
838
839         if (mf->width != icd->user_width || mf->height != icd->user_height)
840                 configure_geometry(mx3_cam, mf->width, mf->height,
841                                    icd->current_fmt->host_fmt);
842
843         dev_dbg(icd->parent, "Sensor cropped %dx%d\n",
844                 mf->width, mf->height);
845
846         icd->user_width         = mf->width;
847         icd->user_height        = mf->height;
848
849         return ret;
850 }
851
852 static int mx3_camera_set_fmt(struct soc_camera_device *icd,
853                               struct v4l2_format *f)
854 {
855         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
856         struct mx3_camera_dev *mx3_cam = ici->priv;
857         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
858         const struct soc_camera_format_xlate *xlate;
859         struct v4l2_pix_format *pix = &f->fmt.pix;
860         struct v4l2_subdev_format format = {
861                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
862         };
863         struct v4l2_mbus_framefmt *mf = &format.format;
864         int ret;
865
866         xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
867         if (!xlate) {
868                 dev_warn(icd->parent, "Format %x not found\n",
869                          pix->pixelformat);
870                 return -EINVAL;
871         }
872
873         stride_align(&pix->width);
874         dev_dbg(icd->parent, "Set format %dx%d\n", pix->width, pix->height);
875
876         /*
877          * Might have to perform a complete interface initialisation like in
878          * ipu_csi_init_interface() in mxc_v4l2_s_param(). Also consider
879          * mxc_v4l2_s_fmt()
880          */
881
882         configure_geometry(mx3_cam, pix->width, pix->height, xlate->host_fmt);
883
884         mf->width       = pix->width;
885         mf->height      = pix->height;
886         mf->field       = pix->field;
887         mf->colorspace  = pix->colorspace;
888         mf->code        = xlate->code;
889
890         ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &format);
891         if (ret < 0)
892                 return ret;
893
894         if (mf->code != xlate->code)
895                 return -EINVAL;
896
897         if (!mx3_cam->idmac_channel[0]) {
898                 ret = acquire_dma_channel(mx3_cam);
899                 if (ret < 0)
900                         return ret;
901         }
902
903         pix->width              = mf->width;
904         pix->height             = mf->height;
905         pix->field              = mf->field;
906         mx3_cam->field          = mf->field;
907         pix->colorspace         = mf->colorspace;
908         icd->current_fmt        = xlate;
909
910         dev_dbg(icd->parent, "Sensor set %dx%d\n", pix->width, pix->height);
911
912         return ret;
913 }
914
915 static int mx3_camera_try_fmt(struct soc_camera_device *icd,
916                               struct v4l2_format *f)
917 {
918         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
919         const struct soc_camera_format_xlate *xlate;
920         struct v4l2_pix_format *pix = &f->fmt.pix;
921         struct v4l2_subdev_pad_config pad_cfg;
922         struct v4l2_subdev_format format = {
923                 .which = V4L2_SUBDEV_FORMAT_TRY,
924         };
925         struct v4l2_mbus_framefmt *mf = &format.format;
926         __u32 pixfmt = pix->pixelformat;
927         int ret;
928
929         xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
930         if (pixfmt && !xlate) {
931                 dev_warn(icd->parent, "Format %x not found\n", pixfmt);
932                 return -EINVAL;
933         }
934
935         /* limit to MX3 hardware capabilities */
936         if (pix->height > 4096)
937                 pix->height = 4096;
938         if (pix->width > 4096)
939                 pix->width = 4096;
940
941         /* limit to sensor capabilities */
942         mf->width       = pix->width;
943         mf->height      = pix->height;
944         mf->field       = pix->field;
945         mf->colorspace  = pix->colorspace;
946         mf->code        = xlate->code;
947
948         ret = v4l2_subdev_call(sd, pad, set_fmt, &pad_cfg, &format);
949         if (ret < 0)
950                 return ret;
951
952         pix->width      = mf->width;
953         pix->height     = mf->height;
954         pix->colorspace = mf->colorspace;
955
956         switch (mf->field) {
957         case V4L2_FIELD_ANY:
958                 pix->field = V4L2_FIELD_NONE;
959                 break;
960         case V4L2_FIELD_NONE:
961                 break;
962         default:
963                 dev_err(icd->parent, "Field type %d unsupported.\n",
964                         mf->field);
965                 ret = -EINVAL;
966         }
967
968         return ret;
969 }
970
971 static int mx3_camera_reqbufs(struct soc_camera_device *icd,
972                               struct v4l2_requestbuffers *p)
973 {
974         return 0;
975 }
976
977 static unsigned int mx3_camera_poll(struct file *file, poll_table *pt)
978 {
979         struct soc_camera_device *icd = file->private_data;
980
981         return vb2_poll(&icd->vb2_vidq, file, pt);
982 }
983
984 static int mx3_camera_querycap(struct soc_camera_host *ici,
985                                struct v4l2_capability *cap)
986 {
987         /* cap->name is set by the firendly caller:-> */
988         strlcpy(cap->card, "i.MX3x Camera", sizeof(cap->card));
989         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
990         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
991
992         return 0;
993 }
994
995 static int mx3_camera_set_bus_param(struct soc_camera_device *icd)
996 {
997         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
998         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
999         struct mx3_camera_dev *mx3_cam = ici->priv;
1000         struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
1001         u32 pixfmt = icd->current_fmt->host_fmt->fourcc;
1002         unsigned long bus_flags, common_flags;
1003         u32 dw, sens_conf;
1004         const struct soc_mbus_pixelfmt *fmt;
1005         int buswidth;
1006         int ret;
1007         const struct soc_camera_format_xlate *xlate;
1008         struct device *dev = icd->parent;
1009
1010         fmt = soc_mbus_get_fmtdesc(icd->current_fmt->code);
1011         if (!fmt)
1012                 return -EINVAL;
1013
1014         xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1015         if (!xlate) {
1016                 dev_warn(dev, "Format %x not found\n", pixfmt);
1017                 return -EINVAL;
1018         }
1019
1020         buswidth = fmt->bits_per_sample;
1021         ret = test_platform_param(mx3_cam, buswidth, &bus_flags);
1022
1023         dev_dbg(dev, "requested bus width %d bit: %d\n", buswidth, ret);
1024
1025         if (ret < 0)
1026                 return ret;
1027
1028         ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
1029         if (!ret) {
1030                 common_flags = soc_mbus_config_compatible(&cfg,
1031                                                           bus_flags);
1032                 if (!common_flags) {
1033                         dev_warn(icd->parent,
1034                                  "Flags incompatible: camera 0x%x, host 0x%lx\n",
1035                                  cfg.flags, bus_flags);
1036                         return -EINVAL;
1037                 }
1038         } else if (ret != -ENOIOCTLCMD) {
1039                 return ret;
1040         } else {
1041                 common_flags = bus_flags;
1042         }
1043
1044         dev_dbg(dev, "Flags cam: 0x%x host: 0x%lx common: 0x%lx\n",
1045                 cfg.flags, bus_flags, common_flags);
1046
1047         /* Make choices, based on platform preferences */
1048         if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
1049             (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
1050                 if (mx3_cam->platform_flags & MX3_CAMERA_HSP)
1051                         common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
1052                 else
1053                         common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
1054         }
1055
1056         if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
1057             (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
1058                 if (mx3_cam->platform_flags & MX3_CAMERA_VSP)
1059                         common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
1060                 else
1061                         common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
1062         }
1063
1064         if ((common_flags & V4L2_MBUS_DATA_ACTIVE_HIGH) &&
1065             (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)) {
1066                 if (mx3_cam->platform_flags & MX3_CAMERA_DP)
1067                         common_flags &= ~V4L2_MBUS_DATA_ACTIVE_HIGH;
1068                 else
1069                         common_flags &= ~V4L2_MBUS_DATA_ACTIVE_LOW;
1070         }
1071
1072         if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
1073             (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
1074                 if (mx3_cam->platform_flags & MX3_CAMERA_PCP)
1075                         common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
1076                 else
1077                         common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
1078         }
1079
1080         cfg.flags = common_flags;
1081         ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
1082         if (ret < 0 && ret != -ENOIOCTLCMD) {
1083                 dev_dbg(dev, "camera s_mbus_config(0x%lx) returned %d\n",
1084                         common_flags, ret);
1085                 return ret;
1086         }
1087
1088         /*
1089          * So far only gated clock mode is supported. Add a line
1090          *      (3 << CSI_SENS_CONF_SENS_PRTCL_SHIFT) |
1091          * below and select the required mode when supporting other
1092          * synchronisation protocols.
1093          */
1094         sens_conf = csi_reg_read(mx3_cam, CSI_SENS_CONF) &
1095                 ~((1 << CSI_SENS_CONF_VSYNC_POL_SHIFT) |
1096                   (1 << CSI_SENS_CONF_HSYNC_POL_SHIFT) |
1097                   (1 << CSI_SENS_CONF_DATA_POL_SHIFT) |
1098                   (1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT) |
1099                   (3 << CSI_SENS_CONF_DATA_FMT_SHIFT) |
1100                   (3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT));
1101
1102         /* TODO: Support RGB and YUV formats */
1103
1104         /* This has been set in mx3_camera_activate(), but we clear it above */
1105         sens_conf |= CSI_SENS_CONF_DATA_FMT_BAYER;
1106
1107         if (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
1108                 sens_conf |= 1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT;
1109         if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
1110                 sens_conf |= 1 << CSI_SENS_CONF_HSYNC_POL_SHIFT;
1111         if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1112                 sens_conf |= 1 << CSI_SENS_CONF_VSYNC_POL_SHIFT;
1113         if (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)
1114                 sens_conf |= 1 << CSI_SENS_CONF_DATA_POL_SHIFT;
1115
1116         /* Just do what we're asked to do */
1117         switch (xlate->host_fmt->bits_per_sample) {
1118         case 4:
1119                 dw = 0 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1120                 break;
1121         case 8:
1122                 dw = 1 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1123                 break;
1124         case 10:
1125                 dw = 2 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1126                 break;
1127         default:
1128                 /*
1129                  * Actually it can only be 15 now, default is just to silence
1130                  * compiler warnings
1131                  */
1132         case 15:
1133                 dw = 3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1134         }
1135
1136         csi_reg_write(mx3_cam, sens_conf | dw, CSI_SENS_CONF);
1137
1138         dev_dbg(dev, "Set SENS_CONF to %x\n", sens_conf | dw);
1139
1140         return 0;
1141 }
1142
1143 static struct soc_camera_host_ops mx3_soc_camera_host_ops = {
1144         .owner          = THIS_MODULE,
1145         .add            = mx3_camera_add_device,
1146         .remove         = mx3_camera_remove_device,
1147         .clock_start    = mx3_camera_clock_start,
1148         .clock_stop     = mx3_camera_clock_stop,
1149         .set_crop       = mx3_camera_set_crop,
1150         .set_fmt        = mx3_camera_set_fmt,
1151         .try_fmt        = mx3_camera_try_fmt,
1152         .get_formats    = mx3_camera_get_formats,
1153         .init_videobuf2 = mx3_camera_init_videobuf,
1154         .reqbufs        = mx3_camera_reqbufs,
1155         .poll           = mx3_camera_poll,
1156         .querycap       = mx3_camera_querycap,
1157         .set_bus_param  = mx3_camera_set_bus_param,
1158 };
1159
1160 static int mx3_camera_probe(struct platform_device *pdev)
1161 {
1162         struct mx3_camera_pdata *pdata = pdev->dev.platform_data;
1163         struct mx3_camera_dev *mx3_cam;
1164         struct resource *res;
1165         void __iomem *base;
1166         int err = 0;
1167         struct soc_camera_host *soc_host;
1168
1169         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1170         base = devm_ioremap_resource(&pdev->dev, res);
1171         if (IS_ERR(base))
1172                 return PTR_ERR(base);
1173
1174         if (!pdata)
1175                 return -EINVAL;
1176
1177         mx3_cam = devm_kzalloc(&pdev->dev, sizeof(*mx3_cam), GFP_KERNEL);
1178         if (!mx3_cam) {
1179                 dev_err(&pdev->dev, "Could not allocate mx3 camera object\n");
1180                 return -ENOMEM;
1181         }
1182
1183         mx3_cam->clk = devm_clk_get(&pdev->dev, NULL);
1184         if (IS_ERR(mx3_cam->clk))
1185                 return PTR_ERR(mx3_cam->clk);
1186
1187         mx3_cam->pdata = pdata;
1188         mx3_cam->platform_flags = pdata->flags;
1189         if (!(mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_MASK)) {
1190                 /*
1191                  * Platform hasn't set available data widths. This is bad.
1192                  * Warn and use a default.
1193                  */
1194                 dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
1195                          "data widths, using default 8 bit\n");
1196                 mx3_cam->platform_flags |= MX3_CAMERA_DATAWIDTH_8;
1197         }
1198         if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_4)
1199                 mx3_cam->width_flags = 1 << 3;
1200         if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_8)
1201                 mx3_cam->width_flags |= 1 << 7;
1202         if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_10)
1203                 mx3_cam->width_flags |= 1 << 9;
1204         if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_15)
1205                 mx3_cam->width_flags |= 1 << 14;
1206
1207         mx3_cam->mclk = pdata->mclk_10khz * 10000;
1208         if (!mx3_cam->mclk) {
1209                 dev_warn(&pdev->dev,
1210                          "mclk_10khz == 0! Please, fix your platform data. "
1211                          "Using default 20MHz\n");
1212                 mx3_cam->mclk = 20000000;
1213         }
1214
1215         /* list of video-buffers */
1216         INIT_LIST_HEAD(&mx3_cam->capture);
1217         spin_lock_init(&mx3_cam->lock);
1218
1219         mx3_cam->base   = base;
1220
1221         soc_host                = &mx3_cam->soc_host;
1222         soc_host->drv_name      = MX3_CAM_DRV_NAME;
1223         soc_host->ops           = &mx3_soc_camera_host_ops;
1224         soc_host->priv          = mx3_cam;
1225         soc_host->v4l2_dev.dev  = &pdev->dev;
1226         soc_host->nr            = pdev->id;
1227
1228         mx3_cam->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1229         if (IS_ERR(mx3_cam->alloc_ctx))
1230                 return PTR_ERR(mx3_cam->alloc_ctx);
1231
1232         if (pdata->asd_sizes) {
1233                 soc_host->asd = pdata->asd;
1234                 soc_host->asd_sizes = pdata->asd_sizes;
1235         }
1236
1237         err = soc_camera_host_register(soc_host);
1238         if (err)
1239                 goto ecamhostreg;
1240
1241         /* IDMAC interface */
1242         dmaengine_get();
1243
1244         return 0;
1245
1246 ecamhostreg:
1247         vb2_dma_contig_cleanup_ctx(mx3_cam->alloc_ctx);
1248         return err;
1249 }
1250
1251 static int mx3_camera_remove(struct platform_device *pdev)
1252 {
1253         struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1254         struct mx3_camera_dev *mx3_cam = container_of(soc_host,
1255                                         struct mx3_camera_dev, soc_host);
1256
1257         soc_camera_host_unregister(soc_host);
1258
1259         /*
1260          * The channel has either not been allocated,
1261          * or should have been released
1262          */
1263         if (WARN_ON(mx3_cam->idmac_channel[0]))
1264                 dma_release_channel(&mx3_cam->idmac_channel[0]->dma_chan);
1265
1266         vb2_dma_contig_cleanup_ctx(mx3_cam->alloc_ctx);
1267
1268         dmaengine_put();
1269
1270         return 0;
1271 }
1272
1273 static struct platform_driver mx3_camera_driver = {
1274         .driver         = {
1275                 .name   = MX3_CAM_DRV_NAME,
1276         },
1277         .probe          = mx3_camera_probe,
1278         .remove         = mx3_camera_remove,
1279 };
1280
1281 module_platform_driver(mx3_camera_driver);
1282
1283 MODULE_DESCRIPTION("i.MX3x SoC Camera Host driver");
1284 MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
1285 MODULE_LICENSE("GPL v2");
1286 MODULE_VERSION("0.2.3");
1287 MODULE_ALIAS("platform:" MX3_CAM_DRV_NAME);