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