]> rtime.felk.cvut.cz Git - can-eth-gw-linux.git/blob - drivers/media/video/atmel-isi.c
[media] media: vb2: change queue initialization order
[can-eth-gw-linux.git] / drivers / media / video / atmel-isi.c
1 /*
2  * Copyright (c) 2011 Atmel Corporation
3  * Josh Wu, <josh.wu@atmel.com>
4  *
5  * Based on previous work by Lars Haring, <lars.haring@atmel.com>
6  * and Sedji Gaouaou
7  * Based on the bttv driver for Bt848 with respective copyright holders
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include <linux/clk.h>
15 #include <linux/completion.h>
16 #include <linux/delay.h>
17 #include <linux/fs.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24
25 #include <media/atmel-isi.h>
26 #include <media/soc_camera.h>
27 #include <media/soc_mediabus.h>
28 #include <media/videobuf2-dma-contig.h>
29
30 #define MAX_BUFFER_NUM                  32
31 #define MAX_SUPPORT_WIDTH               2048
32 #define MAX_SUPPORT_HEIGHT              2048
33 #define VID_LIMIT_BYTES                 (16 * 1024 * 1024)
34 #define MIN_FRAME_RATE                  15
35 #define FRAME_INTERVAL_MILLI_SEC        (1000 / MIN_FRAME_RATE)
36
37 /* ISI states */
38 enum {
39         ISI_STATE_IDLE = 0,
40         ISI_STATE_READY,
41         ISI_STATE_WAIT_SOF,
42 };
43
44 /* Frame buffer descriptor */
45 struct fbd {
46         /* Physical address of the frame buffer */
47         u32 fb_address;
48         /* DMA Control Register(only in HISI2) */
49         u32 dma_ctrl;
50         /* Physical address of the next fbd */
51         u32 next_fbd_address;
52 };
53
54 static void set_dma_ctrl(struct fbd *fb_desc, u32 ctrl)
55 {
56         fb_desc->dma_ctrl = ctrl;
57 }
58
59 struct isi_dma_desc {
60         struct list_head list;
61         struct fbd *p_fbd;
62         u32 fbd_phys;
63 };
64
65 /* Frame buffer data */
66 struct frame_buffer {
67         struct vb2_buffer vb;
68         struct isi_dma_desc *p_dma_desc;
69         struct list_head list;
70 };
71
72 struct atmel_isi {
73         /* Protects the access of variables shared with the ISR */
74         spinlock_t                      lock;
75         void __iomem                    *regs;
76
77         int                             sequence;
78         /* State of the ISI module in capturing mode */
79         int                             state;
80
81         /* Wait queue for waiting for SOF */
82         wait_queue_head_t               vsync_wq;
83
84         struct vb2_alloc_ctx            *alloc_ctx;
85
86         /* Allocate descriptors for dma buffer use */
87         struct fbd                      *p_fb_descriptors;
88         u32                             fb_descriptors_phys;
89         struct                          list_head dma_desc_head;
90         struct isi_dma_desc             dma_desc[MAX_BUFFER_NUM];
91
92         struct completion               complete;
93         struct clk                      *pclk;
94         unsigned int                    irq;
95
96         struct isi_platform_data        *pdata;
97
98         struct list_head                video_buffer_list;
99         struct frame_buffer             *active;
100
101         struct soc_camera_device        *icd;
102         struct soc_camera_host          soc_host;
103 };
104
105 static void isi_writel(struct atmel_isi *isi, u32 reg, u32 val)
106 {
107         writel(val, isi->regs + reg);
108 }
109 static u32 isi_readl(struct atmel_isi *isi, u32 reg)
110 {
111         return readl(isi->regs + reg);
112 }
113
114 static int configure_geometry(struct atmel_isi *isi, u32 width,
115                         u32 height, enum v4l2_mbus_pixelcode code)
116 {
117         u32 cfg2, cr;
118
119         switch (code) {
120         /* YUV, including grey */
121         case V4L2_MBUS_FMT_Y8_1X8:
122                 cr = ISI_CFG2_GRAYSCALE;
123                 break;
124         case V4L2_MBUS_FMT_UYVY8_2X8:
125                 cr = ISI_CFG2_YCC_SWAP_MODE_3;
126                 break;
127         case V4L2_MBUS_FMT_VYUY8_2X8:
128                 cr = ISI_CFG2_YCC_SWAP_MODE_2;
129                 break;
130         case V4L2_MBUS_FMT_YUYV8_2X8:
131                 cr = ISI_CFG2_YCC_SWAP_MODE_1;
132                 break;
133         case V4L2_MBUS_FMT_YVYU8_2X8:
134                 cr = ISI_CFG2_YCC_SWAP_DEFAULT;
135                 break;
136         /* RGB, TODO */
137         default:
138                 return -EINVAL;
139         }
140
141         isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
142
143         cfg2 = isi_readl(isi, ISI_CFG2);
144         cfg2 |= cr;
145         /* Set width */
146         cfg2 &= ~(ISI_CFG2_IM_HSIZE_MASK);
147         cfg2 |= ((width - 1) << ISI_CFG2_IM_HSIZE_OFFSET) &
148                         ISI_CFG2_IM_HSIZE_MASK;
149         /* Set height */
150         cfg2 &= ~(ISI_CFG2_IM_VSIZE_MASK);
151         cfg2 |= ((height - 1) << ISI_CFG2_IM_VSIZE_OFFSET)
152                         & ISI_CFG2_IM_VSIZE_MASK;
153         isi_writel(isi, ISI_CFG2, cfg2);
154
155         return 0;
156 }
157
158 static irqreturn_t atmel_isi_handle_streaming(struct atmel_isi *isi)
159 {
160         if (isi->active) {
161                 struct vb2_buffer *vb = &isi->active->vb;
162                 struct frame_buffer *buf = isi->active;
163
164                 list_del_init(&buf->list);
165                 do_gettimeofday(&vb->v4l2_buf.timestamp);
166                 vb->v4l2_buf.sequence = isi->sequence++;
167                 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
168         }
169
170         if (list_empty(&isi->video_buffer_list)) {
171                 isi->active = NULL;
172         } else {
173                 /* start next dma frame. */
174                 isi->active = list_entry(isi->video_buffer_list.next,
175                                         struct frame_buffer, list);
176                 isi_writel(isi, ISI_DMA_C_DSCR,
177                         isi->active->p_dma_desc->fbd_phys);
178                 isi_writel(isi, ISI_DMA_C_CTRL,
179                         ISI_DMA_CTRL_FETCH | ISI_DMA_CTRL_DONE);
180                 isi_writel(isi, ISI_DMA_CHER, ISI_DMA_CHSR_C_CH);
181         }
182         return IRQ_HANDLED;
183 }
184
185 /* ISI interrupt service routine */
186 static irqreturn_t isi_interrupt(int irq, void *dev_id)
187 {
188         struct atmel_isi *isi = dev_id;
189         u32 status, mask, pending;
190         irqreturn_t ret = IRQ_NONE;
191
192         spin_lock(&isi->lock);
193
194         status = isi_readl(isi, ISI_STATUS);
195         mask = isi_readl(isi, ISI_INTMASK);
196         pending = status & mask;
197
198         if (pending & ISI_CTRL_SRST) {
199                 complete(&isi->complete);
200                 isi_writel(isi, ISI_INTDIS, ISI_CTRL_SRST);
201                 ret = IRQ_HANDLED;
202         } else if (pending & ISI_CTRL_DIS) {
203                 complete(&isi->complete);
204                 isi_writel(isi, ISI_INTDIS, ISI_CTRL_DIS);
205                 ret = IRQ_HANDLED;
206         } else {
207                 if ((pending & ISI_SR_VSYNC) &&
208                                 (isi->state == ISI_STATE_IDLE)) {
209                         isi->state = ISI_STATE_READY;
210                         wake_up_interruptible(&isi->vsync_wq);
211                         ret = IRQ_HANDLED;
212                 }
213                 if (likely(pending & ISI_SR_CXFR_DONE))
214                         ret = atmel_isi_handle_streaming(isi);
215         }
216
217         spin_unlock(&isi->lock);
218         return ret;
219 }
220
221 #define WAIT_ISI_RESET          1
222 #define WAIT_ISI_DISABLE        0
223 static int atmel_isi_wait_status(struct atmel_isi *isi, int wait_reset)
224 {
225         unsigned long timeout;
226         /*
227          * The reset or disable will only succeed if we have a
228          * pixel clock from the camera.
229          */
230         init_completion(&isi->complete);
231
232         if (wait_reset) {
233                 isi_writel(isi, ISI_INTEN, ISI_CTRL_SRST);
234                 isi_writel(isi, ISI_CTRL, ISI_CTRL_SRST);
235         } else {
236                 isi_writel(isi, ISI_INTEN, ISI_CTRL_DIS);
237                 isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
238         }
239
240         timeout = wait_for_completion_timeout(&isi->complete,
241                         msecs_to_jiffies(100));
242         if (timeout == 0)
243                 return -ETIMEDOUT;
244
245         return 0;
246 }
247
248 /* ------------------------------------------------------------------
249         Videobuf operations
250    ------------------------------------------------------------------*/
251 static int queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
252                                 unsigned int *nplanes, unsigned int sizes[],
253                                 void *alloc_ctxs[])
254 {
255         struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
256         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
257         struct atmel_isi *isi = ici->priv;
258         unsigned long size;
259         int ret, bytes_per_line;
260
261         /* Reset ISI */
262         ret = atmel_isi_wait_status(isi, WAIT_ISI_RESET);
263         if (ret < 0) {
264                 dev_err(icd->parent, "Reset ISI timed out\n");
265                 return ret;
266         }
267         /* Disable all interrupts */
268         isi_writel(isi, ISI_INTDIS, ~0UL);
269
270         bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
271                                                 icd->current_fmt->host_fmt);
272
273         if (bytes_per_line < 0)
274                 return bytes_per_line;
275
276         size = bytes_per_line * icd->user_height;
277
278         if (!*nbuffers || *nbuffers > MAX_BUFFER_NUM)
279                 *nbuffers = MAX_BUFFER_NUM;
280
281         if (size * *nbuffers > VID_LIMIT_BYTES)
282                 *nbuffers = VID_LIMIT_BYTES / size;
283
284         *nplanes = 1;
285         sizes[0] = size;
286         alloc_ctxs[0] = isi->alloc_ctx;
287
288         isi->sequence = 0;
289         isi->active = NULL;
290
291         dev_dbg(icd->parent, "%s, count=%d, size=%ld\n", __func__,
292                 *nbuffers, size);
293
294         return 0;
295 }
296
297 static int buffer_init(struct vb2_buffer *vb)
298 {
299         struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
300
301         buf->p_dma_desc = NULL;
302         INIT_LIST_HEAD(&buf->list);
303
304         return 0;
305 }
306
307 static int buffer_prepare(struct vb2_buffer *vb)
308 {
309         struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
310         struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
311         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
312         struct atmel_isi *isi = ici->priv;
313         unsigned long size;
314         struct isi_dma_desc *desc;
315         int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
316                                                 icd->current_fmt->host_fmt);
317
318         if (bytes_per_line < 0)
319                 return bytes_per_line;
320
321         size = bytes_per_line * icd->user_height;
322
323         if (vb2_plane_size(vb, 0) < size) {
324                 dev_err(icd->parent, "%s data will not fit into plane (%lu < %lu)\n",
325                                 __func__, vb2_plane_size(vb, 0), size);
326                 return -EINVAL;
327         }
328
329         vb2_set_plane_payload(&buf->vb, 0, size);
330
331         if (!buf->p_dma_desc) {
332                 if (list_empty(&isi->dma_desc_head)) {
333                         dev_err(icd->parent, "Not enough dma descriptors.\n");
334                         return -EINVAL;
335                 } else {
336                         /* Get an available descriptor */
337                         desc = list_entry(isi->dma_desc_head.next,
338                                                 struct isi_dma_desc, list);
339                         /* Delete the descriptor since now it is used */
340                         list_del_init(&desc->list);
341
342                         /* Initialize the dma descriptor */
343                         desc->p_fbd->fb_address =
344                                         vb2_dma_contig_plane_dma_addr(vb, 0);
345                         desc->p_fbd->next_fbd_address = 0;
346                         set_dma_ctrl(desc->p_fbd, ISI_DMA_CTRL_WB);
347
348                         buf->p_dma_desc = desc;
349                 }
350         }
351         return 0;
352 }
353
354 static void buffer_cleanup(struct vb2_buffer *vb)
355 {
356         struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
357         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
358         struct atmel_isi *isi = ici->priv;
359         struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
360
361         /* This descriptor is available now and we add to head list */
362         if (buf->p_dma_desc)
363                 list_add(&buf->p_dma_desc->list, &isi->dma_desc_head);
364 }
365
366 static void start_dma(struct atmel_isi *isi, struct frame_buffer *buffer)
367 {
368         u32 ctrl, cfg1;
369
370         cfg1 = isi_readl(isi, ISI_CFG1);
371         /* Enable irq: cxfr for the codec path, pxfr for the preview path */
372         isi_writel(isi, ISI_INTEN,
373                         ISI_SR_CXFR_DONE | ISI_SR_PXFR_DONE);
374
375         /* Check if already in a frame */
376         if (isi_readl(isi, ISI_STATUS) & ISI_CTRL_CDC) {
377                 dev_err(isi->icd->parent, "Already in frame handling.\n");
378                 return;
379         }
380
381         isi_writel(isi, ISI_DMA_C_DSCR, buffer->p_dma_desc->fbd_phys);
382         isi_writel(isi, ISI_DMA_C_CTRL, ISI_DMA_CTRL_FETCH | ISI_DMA_CTRL_DONE);
383         isi_writel(isi, ISI_DMA_CHER, ISI_DMA_CHSR_C_CH);
384
385         /* Enable linked list */
386         cfg1 |= isi->pdata->frate | ISI_CFG1_DISCR;
387
388         /* Enable codec path and ISI */
389         ctrl = ISI_CTRL_CDC | ISI_CTRL_EN;
390         isi_writel(isi, ISI_CTRL, ctrl);
391         isi_writel(isi, ISI_CFG1, cfg1);
392 }
393
394 static void buffer_queue(struct vb2_buffer *vb)
395 {
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 atmel_isi *isi = ici->priv;
399         struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
400         unsigned long flags = 0;
401
402         spin_lock_irqsave(&isi->lock, flags);
403         list_add_tail(&buf->list, &isi->video_buffer_list);
404
405         if (isi->active == NULL) {
406                 isi->active = buf;
407                 if (vb2_is_streaming(vb->vb2_queue))
408                         start_dma(isi, buf);
409         }
410         spin_unlock_irqrestore(&isi->lock, flags);
411 }
412
413 static int start_streaming(struct vb2_queue *vq, unsigned int count)
414 {
415         struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
416         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
417         struct atmel_isi *isi = ici->priv;
418
419         u32 sr = 0;
420         int ret;
421
422         spin_lock_irq(&isi->lock);
423         isi->state = ISI_STATE_IDLE;
424         /* Clear any pending SOF interrupt */
425         sr = isi_readl(isi, ISI_STATUS);
426         /* Enable VSYNC interrupt for SOF */
427         isi_writel(isi, ISI_INTEN, ISI_SR_VSYNC);
428         isi_writel(isi, ISI_CTRL, ISI_CTRL_EN);
429         spin_unlock_irq(&isi->lock);
430
431         dev_dbg(icd->parent, "Waiting for SOF\n");
432         ret = wait_event_interruptible(isi->vsync_wq,
433                                        isi->state != ISI_STATE_IDLE);
434         if (ret)
435                 goto err;
436
437         if (isi->state != ISI_STATE_READY) {
438                 ret = -EIO;
439                 goto err;
440         }
441
442         spin_lock_irq(&isi->lock);
443         isi->state = ISI_STATE_WAIT_SOF;
444         isi_writel(isi, ISI_INTDIS, ISI_SR_VSYNC);
445         if (count)
446                 start_dma(isi, isi->active);
447         spin_unlock_irq(&isi->lock);
448
449         return 0;
450 err:
451         isi->active = NULL;
452         isi->sequence = 0;
453         INIT_LIST_HEAD(&isi->video_buffer_list);
454         return ret;
455 }
456
457 /* abort streaming and wait for last buffer */
458 static int stop_streaming(struct vb2_queue *vq)
459 {
460         struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
461         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
462         struct atmel_isi *isi = ici->priv;
463         struct frame_buffer *buf, *node;
464         int ret = 0;
465         unsigned long timeout;
466
467         spin_lock_irq(&isi->lock);
468         isi->active = NULL;
469         /* Release all active buffers */
470         list_for_each_entry_safe(buf, node, &isi->video_buffer_list, list) {
471                 list_del_init(&buf->list);
472                 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
473         }
474         spin_unlock_irq(&isi->lock);
475
476         timeout = jiffies + FRAME_INTERVAL_MILLI_SEC * HZ;
477         /* Wait until the end of the current frame. */
478         while ((isi_readl(isi, ISI_STATUS) & ISI_CTRL_CDC) &&
479                         time_before(jiffies, timeout))
480                 msleep(1);
481
482         if (time_after(jiffies, timeout)) {
483                 dev_err(icd->parent,
484                         "Timeout waiting for finishing codec request\n");
485                 return -ETIMEDOUT;
486         }
487
488         /* Disable interrupts */
489         isi_writel(isi, ISI_INTDIS,
490                         ISI_SR_CXFR_DONE | ISI_SR_PXFR_DONE);
491
492         /* Disable ISI and wait for it is done */
493         ret = atmel_isi_wait_status(isi, WAIT_ISI_DISABLE);
494         if (ret < 0)
495                 dev_err(icd->parent, "Disable ISI timed out\n");
496
497         return ret;
498 }
499
500 static struct vb2_ops isi_video_qops = {
501         .queue_setup            = queue_setup,
502         .buf_init               = buffer_init,
503         .buf_prepare            = buffer_prepare,
504         .buf_cleanup            = buffer_cleanup,
505         .buf_queue              = buffer_queue,
506         .start_streaming        = start_streaming,
507         .stop_streaming         = stop_streaming,
508         .wait_prepare           = soc_camera_unlock,
509         .wait_finish            = soc_camera_lock,
510 };
511
512 /* ------------------------------------------------------------------
513         SOC camera operations for the device
514    ------------------------------------------------------------------*/
515 static int isi_camera_init_videobuf(struct vb2_queue *q,
516                                      struct soc_camera_device *icd)
517 {
518         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
519         q->io_modes = VB2_MMAP;
520         q->drv_priv = icd;
521         q->buf_struct_size = sizeof(struct frame_buffer);
522         q->ops = &isi_video_qops;
523         q->mem_ops = &vb2_dma_contig_memops;
524
525         return vb2_queue_init(q);
526 }
527
528 static int isi_camera_set_fmt(struct soc_camera_device *icd,
529                               struct v4l2_format *f)
530 {
531         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
532         struct atmel_isi *isi = ici->priv;
533         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
534         const struct soc_camera_format_xlate *xlate;
535         struct v4l2_pix_format *pix = &f->fmt.pix;
536         struct v4l2_mbus_framefmt mf;
537         int ret;
538
539         xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
540         if (!xlate) {
541                 dev_warn(icd->parent, "Format %x not found\n",
542                          pix->pixelformat);
543                 return -EINVAL;
544         }
545
546         dev_dbg(icd->parent, "Plan to set format %dx%d\n",
547                         pix->width, pix->height);
548
549         mf.width        = pix->width;
550         mf.height       = pix->height;
551         mf.field        = pix->field;
552         mf.colorspace   = pix->colorspace;
553         mf.code         = xlate->code;
554
555         ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
556         if (ret < 0)
557                 return ret;
558
559         if (mf.code != xlate->code)
560                 return -EINVAL;
561
562         ret = configure_geometry(isi, pix->width, pix->height, xlate->code);
563         if (ret < 0)
564                 return ret;
565
566         pix->width              = mf.width;
567         pix->height             = mf.height;
568         pix->field              = mf.field;
569         pix->colorspace         = mf.colorspace;
570         icd->current_fmt        = xlate;
571
572         dev_dbg(icd->parent, "Finally set format %dx%d\n",
573                 pix->width, pix->height);
574
575         return ret;
576 }
577
578 static int isi_camera_try_fmt(struct soc_camera_device *icd,
579                               struct v4l2_format *f)
580 {
581         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
582         const struct soc_camera_format_xlate *xlate;
583         struct v4l2_pix_format *pix = &f->fmt.pix;
584         struct v4l2_mbus_framefmt mf;
585         u32 pixfmt = pix->pixelformat;
586         int ret;
587
588         xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
589         if (pixfmt && !xlate) {
590                 dev_warn(icd->parent, "Format %x not found\n", pixfmt);
591                 return -EINVAL;
592         }
593
594         /* limit to Atmel ISI hardware capabilities */
595         if (pix->height > MAX_SUPPORT_HEIGHT)
596                 pix->height = MAX_SUPPORT_HEIGHT;
597         if (pix->width > MAX_SUPPORT_WIDTH)
598                 pix->width = MAX_SUPPORT_WIDTH;
599
600         /* limit to sensor capabilities */
601         mf.width        = pix->width;
602         mf.height       = pix->height;
603         mf.field        = pix->field;
604         mf.colorspace   = pix->colorspace;
605         mf.code         = xlate->code;
606
607         ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
608         if (ret < 0)
609                 return ret;
610
611         pix->width      = mf.width;
612         pix->height     = mf.height;
613         pix->colorspace = mf.colorspace;
614
615         switch (mf.field) {
616         case V4L2_FIELD_ANY:
617                 pix->field = V4L2_FIELD_NONE;
618                 break;
619         case V4L2_FIELD_NONE:
620                 break;
621         default:
622                 dev_err(icd->parent, "Field type %d unsupported.\n",
623                         mf.field);
624                 ret = -EINVAL;
625         }
626
627         return ret;
628 }
629
630 static const struct soc_mbus_pixelfmt isi_camera_formats[] = {
631         {
632                 .fourcc                 = V4L2_PIX_FMT_YUYV,
633                 .name                   = "Packed YUV422 16 bit",
634                 .bits_per_sample        = 8,
635                 .packing                = SOC_MBUS_PACKING_2X8_PADHI,
636                 .order                  = SOC_MBUS_ORDER_LE,
637         },
638 };
639
640 /* This will be corrected as we get more formats */
641 static bool isi_camera_packing_supported(const struct soc_mbus_pixelfmt *fmt)
642 {
643         return  fmt->packing == SOC_MBUS_PACKING_NONE ||
644                 (fmt->bits_per_sample == 8 &&
645                  fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
646                 (fmt->bits_per_sample > 8 &&
647                  fmt->packing == SOC_MBUS_PACKING_EXTEND16);
648 }
649
650 static unsigned long make_bus_param(struct atmel_isi *isi)
651 {
652         unsigned long flags;
653         /*
654          * Platform specified synchronization and pixel clock polarities are
655          * only a recommendation and are only used during probing. Atmel ISI
656          * camera interface only works in master mode, i.e., uses HSYNC and
657          * VSYNC signals from the sensor
658          */
659         flags = SOCAM_MASTER |
660                 SOCAM_HSYNC_ACTIVE_HIGH |
661                 SOCAM_HSYNC_ACTIVE_LOW |
662                 SOCAM_VSYNC_ACTIVE_HIGH |
663                 SOCAM_VSYNC_ACTIVE_LOW |
664                 SOCAM_PCLK_SAMPLE_RISING |
665                 SOCAM_PCLK_SAMPLE_FALLING |
666                 SOCAM_DATA_ACTIVE_HIGH;
667
668         if (isi->pdata->data_width_flags & ISI_DATAWIDTH_10)
669                 flags |= SOCAM_DATAWIDTH_10;
670
671         if (isi->pdata->data_width_flags & ISI_DATAWIDTH_8)
672                 flags |= SOCAM_DATAWIDTH_8;
673
674         if (flags & SOCAM_DATAWIDTH_MASK)
675                 return flags;
676
677         return 0;
678 }
679
680 static int isi_camera_try_bus_param(struct soc_camera_device *icd,
681                                     unsigned char buswidth)
682 {
683         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
684         struct atmel_isi *isi = ici->priv;
685         unsigned long camera_flags;
686         int ret;
687
688         camera_flags = icd->ops->query_bus_param(icd);
689         ret = soc_camera_bus_param_compatible(camera_flags,
690                                         make_bus_param(isi));
691         if (!ret)
692                 return -EINVAL;
693         return 0;
694 }
695
696
697 static int isi_camera_get_formats(struct soc_camera_device *icd,
698                                   unsigned int idx,
699                                   struct soc_camera_format_xlate *xlate)
700 {
701         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
702         int formats = 0, ret;
703         /* sensor format */
704         enum v4l2_mbus_pixelcode code;
705         /* soc camera host format */
706         const struct soc_mbus_pixelfmt *fmt;
707
708         ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
709         if (ret < 0)
710                 /* No more formats */
711                 return 0;
712
713         fmt = soc_mbus_get_fmtdesc(code);
714         if (!fmt) {
715                 dev_err(icd->parent,
716                         "Invalid format code #%u: %d\n", idx, code);
717                 return 0;
718         }
719
720         /* This also checks support for the requested bits-per-sample */
721         ret = isi_camera_try_bus_param(icd, fmt->bits_per_sample);
722         if (ret < 0) {
723                 dev_err(icd->parent,
724                         "Fail to try the bus parameters.\n");
725                 return 0;
726         }
727
728         switch (code) {
729         case V4L2_MBUS_FMT_UYVY8_2X8:
730         case V4L2_MBUS_FMT_VYUY8_2X8:
731         case V4L2_MBUS_FMT_YUYV8_2X8:
732         case V4L2_MBUS_FMT_YVYU8_2X8:
733                 formats++;
734                 if (xlate) {
735                         xlate->host_fmt = &isi_camera_formats[0];
736                         xlate->code     = code;
737                         xlate++;
738                         dev_dbg(icd->parent, "Providing format %s using code %d\n",
739                                 isi_camera_formats[0].name, code);
740                 }
741                 break;
742         default:
743                 if (!isi_camera_packing_supported(fmt))
744                         return 0;
745                 if (xlate)
746                         dev_dbg(icd->parent,
747                                 "Providing format %s in pass-through mode\n",
748                                 fmt->name);
749         }
750
751         /* Generic pass-through */
752         formats++;
753         if (xlate) {
754                 xlate->host_fmt = fmt;
755                 xlate->code     = code;
756                 xlate++;
757         }
758
759         return formats;
760 }
761
762 /* Called with .video_lock held */
763 static int isi_camera_add_device(struct soc_camera_device *icd)
764 {
765         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
766         struct atmel_isi *isi = ici->priv;
767         int ret;
768
769         if (isi->icd)
770                 return -EBUSY;
771
772         ret = clk_enable(isi->pclk);
773         if (ret)
774                 return ret;
775
776         isi->icd = icd;
777         dev_dbg(icd->parent, "Atmel ISI Camera driver attached to camera %d\n",
778                  icd->devnum);
779         return 0;
780 }
781 /* Called with .video_lock held */
782 static void isi_camera_remove_device(struct soc_camera_device *icd)
783 {
784         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
785         struct atmel_isi *isi = ici->priv;
786
787         BUG_ON(icd != isi->icd);
788
789         clk_disable(isi->pclk);
790         isi->icd = NULL;
791
792         dev_dbg(icd->parent, "Atmel ISI Camera driver detached from camera %d\n",
793                  icd->devnum);
794 }
795
796 static unsigned int isi_camera_poll(struct file *file, poll_table *pt)
797 {
798         struct soc_camera_device *icd = file->private_data;
799
800         return vb2_poll(&icd->vb2_vidq, file, pt);
801 }
802
803 static int isi_camera_querycap(struct soc_camera_host *ici,
804                                struct v4l2_capability *cap)
805 {
806         strcpy(cap->driver, "atmel-isi");
807         strcpy(cap->card, "Atmel Image Sensor Interface");
808         cap->capabilities = (V4L2_CAP_VIDEO_CAPTURE |
809                                 V4L2_CAP_STREAMING);
810         return 0;
811 }
812
813 static int isi_camera_set_bus_param(struct soc_camera_device *icd, u32 pixfmt)
814 {
815         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
816         struct atmel_isi *isi = ici->priv;
817         unsigned long bus_flags, camera_flags, common_flags;
818         int ret;
819         u32 cfg1 = 0;
820
821         camera_flags = icd->ops->query_bus_param(icd);
822
823         bus_flags = make_bus_param(isi);
824         common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
825         dev_dbg(icd->parent, "Flags cam: 0x%lx host: 0x%lx common: 0x%lx\n",
826                 camera_flags, bus_flags, common_flags);
827         if (!common_flags)
828                 return -EINVAL;
829
830         /* Make choises, based on platform preferences */
831         if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
832             (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
833                 if (isi->pdata->hsync_act_low)
834                         common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
835                 else
836                         common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
837         }
838
839         if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
840             (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
841                 if (isi->pdata->vsync_act_low)
842                         common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
843                 else
844                         common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
845         }
846
847         if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
848             (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
849                 if (isi->pdata->pclk_act_falling)
850                         common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
851                 else
852                         common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
853         }
854
855         ret = icd->ops->set_bus_param(icd, common_flags);
856         if (ret < 0) {
857                 dev_dbg(icd->parent, "Camera set_bus_param(%lx) returned %d\n",
858                         common_flags, ret);
859                 return ret;
860         }
861
862         /* set bus param for ISI */
863         if (common_flags & SOCAM_HSYNC_ACTIVE_LOW)
864                 cfg1 |= ISI_CFG1_HSYNC_POL_ACTIVE_LOW;
865         if (common_flags & SOCAM_VSYNC_ACTIVE_LOW)
866                 cfg1 |= ISI_CFG1_VSYNC_POL_ACTIVE_LOW;
867         if (common_flags & SOCAM_PCLK_SAMPLE_FALLING)
868                 cfg1 |= ISI_CFG1_PIXCLK_POL_ACTIVE_FALLING;
869
870         if (isi->pdata->has_emb_sync)
871                 cfg1 |= ISI_CFG1_EMB_SYNC;
872         if (isi->pdata->isi_full_mode)
873                 cfg1 |= ISI_CFG1_FULL_MODE;
874
875         isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
876         isi_writel(isi, ISI_CFG1, cfg1);
877
878         return 0;
879 }
880
881 static struct soc_camera_host_ops isi_soc_camera_host_ops = {
882         .owner          = THIS_MODULE,
883         .add            = isi_camera_add_device,
884         .remove         = isi_camera_remove_device,
885         .set_fmt        = isi_camera_set_fmt,
886         .try_fmt        = isi_camera_try_fmt,
887         .get_formats    = isi_camera_get_formats,
888         .init_videobuf2 = isi_camera_init_videobuf,
889         .poll           = isi_camera_poll,
890         .querycap       = isi_camera_querycap,
891         .set_bus_param  = isi_camera_set_bus_param,
892 };
893
894 /* -----------------------------------------------------------------------*/
895 static int __devexit atmel_isi_remove(struct platform_device *pdev)
896 {
897         struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
898         struct atmel_isi *isi = container_of(soc_host,
899                                         struct atmel_isi, soc_host);
900
901         free_irq(isi->irq, isi);
902         soc_camera_host_unregister(soc_host);
903         vb2_dma_contig_cleanup_ctx(isi->alloc_ctx);
904         dma_free_coherent(&pdev->dev,
905                         sizeof(struct fbd) * MAX_BUFFER_NUM,
906                         isi->p_fb_descriptors,
907                         isi->fb_descriptors_phys);
908
909         iounmap(isi->regs);
910         clk_put(isi->pclk);
911         kfree(isi);
912
913         return 0;
914 }
915
916 static int __devinit atmel_isi_probe(struct platform_device *pdev)
917 {
918         unsigned int irq;
919         struct atmel_isi *isi;
920         struct clk *pclk;
921         struct resource *regs;
922         int ret, i;
923         struct device *dev = &pdev->dev;
924         struct soc_camera_host *soc_host;
925         struct isi_platform_data *pdata;
926
927         pdata = dev->platform_data;
928         if (!pdata || !pdata->data_width_flags) {
929                 dev_err(&pdev->dev,
930                         "No config available for Atmel ISI\n");
931                 return -EINVAL;
932         }
933
934         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
935         if (!regs)
936                 return -ENXIO;
937
938         pclk = clk_get(&pdev->dev, "isi_clk");
939         if (IS_ERR(pclk))
940                 return PTR_ERR(pclk);
941
942         isi = kzalloc(sizeof(struct atmel_isi), GFP_KERNEL);
943         if (!isi) {
944                 ret = -ENOMEM;
945                 dev_err(&pdev->dev, "Can't allocate interface!\n");
946                 goto err_alloc_isi;
947         }
948
949         isi->pclk = pclk;
950         isi->pdata = pdata;
951         isi->active = NULL;
952         spin_lock_init(&isi->lock);
953         init_waitqueue_head(&isi->vsync_wq);
954         INIT_LIST_HEAD(&isi->video_buffer_list);
955         INIT_LIST_HEAD(&isi->dma_desc_head);
956
957         isi->p_fb_descriptors = dma_alloc_coherent(&pdev->dev,
958                                 sizeof(struct fbd) * MAX_BUFFER_NUM,
959                                 &isi->fb_descriptors_phys,
960                                 GFP_KERNEL);
961         if (!isi->p_fb_descriptors) {
962                 ret = -ENOMEM;
963                 dev_err(&pdev->dev, "Can't allocate descriptors!\n");
964                 goto err_alloc_descriptors;
965         }
966
967         for (i = 0; i < MAX_BUFFER_NUM; i++) {
968                 isi->dma_desc[i].p_fbd = isi->p_fb_descriptors + i;
969                 isi->dma_desc[i].fbd_phys = isi->fb_descriptors_phys +
970                                         i * sizeof(struct fbd);
971                 list_add(&isi->dma_desc[i].list, &isi->dma_desc_head);
972         }
973
974         isi->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
975         if (IS_ERR(isi->alloc_ctx)) {
976                 ret = PTR_ERR(isi->alloc_ctx);
977                 goto err_alloc_ctx;
978         }
979
980         isi->regs = ioremap(regs->start, resource_size(regs));
981         if (!isi->regs) {
982                 ret = -ENOMEM;
983                 goto err_ioremap;
984         }
985
986         isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
987
988         irq = platform_get_irq(pdev, 0);
989         if (irq < 0) {
990                 ret = irq;
991                 goto err_req_irq;
992         }
993
994         ret = request_irq(irq, isi_interrupt, 0, "isi", isi);
995         if (ret) {
996                 dev_err(&pdev->dev, "Unable to request irq %d\n", irq);
997                 goto err_req_irq;
998         }
999         isi->irq = irq;
1000
1001         soc_host                = &isi->soc_host;
1002         soc_host->drv_name      = "isi-camera";
1003         soc_host->ops           = &isi_soc_camera_host_ops;
1004         soc_host->priv          = isi;
1005         soc_host->v4l2_dev.dev  = &pdev->dev;
1006         soc_host->nr            = pdev->id;
1007
1008         ret = soc_camera_host_register(soc_host);
1009         if (ret) {
1010                 dev_err(&pdev->dev, "Unable to register soc camera host\n");
1011                 goto err_register_soc_camera_host;
1012         }
1013         return 0;
1014
1015 err_register_soc_camera_host:
1016         free_irq(isi->irq, isi);
1017 err_req_irq:
1018         iounmap(isi->regs);
1019 err_ioremap:
1020         vb2_dma_contig_cleanup_ctx(isi->alloc_ctx);
1021 err_alloc_ctx:
1022         dma_free_coherent(&pdev->dev,
1023                         sizeof(struct fbd) * MAX_BUFFER_NUM,
1024                         isi->p_fb_descriptors,
1025                         isi->fb_descriptors_phys);
1026 err_alloc_descriptors:
1027         kfree(isi);
1028 err_alloc_isi:
1029         clk_put(isi->pclk);
1030
1031         return ret;
1032 }
1033
1034 static struct platform_driver atmel_isi_driver = {
1035         .probe          = atmel_isi_probe,
1036         .remove         = __devexit_p(atmel_isi_remove),
1037         .driver         = {
1038                 .name = "atmel_isi",
1039                 .owner = THIS_MODULE,
1040         },
1041 };
1042
1043 static int __init atmel_isi_init_module(void)
1044 {
1045         return  platform_driver_probe(&atmel_isi_driver, &atmel_isi_probe);
1046 }
1047
1048 static void __exit atmel_isi_exit(void)
1049 {
1050         platform_driver_unregister(&atmel_isi_driver);
1051 }
1052 module_init(atmel_isi_init_module);
1053 module_exit(atmel_isi_exit);
1054
1055 MODULE_AUTHOR("Josh Wu <josh.wu@atmel.com>");
1056 MODULE_DESCRIPTION("The V4L2 driver for Atmel Linux");
1057 MODULE_LICENSE("GPL");
1058 MODULE_SUPPORTED_DEVICE("video");