Version:  2.0.40 2.2.26 2.4.37 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7

Linux/drivers/media/pci/sta2x11/sta2x11_vip.c

  1 /*
  2  * This is the driver for the STA2x11 Video Input Port.
  3  *
  4  * Copyright (C) 2012       ST Microelectronics
  5  *     author: Federico Vaga <federico.vaga@gmail.com>
  6  * Copyright (C) 2010       WindRiver Systems, Inc.
  7  *     authors: Andreas Kies <andreas.kies@windriver.com>
  8  *              Vlad Lungu   <vlad.lungu@windriver.com>
  9  *
 10  * This program is free software; you can redistribute it and/or modify it
 11  * under the terms and conditions of the GNU General Public License,
 12  * version 2, as published by the Free Software Foundation.
 13  *
 14  * This program is distributed in the hope it will be useful, but WITHOUT
 15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 17  * more details.
 18  *
 19  * You should have received a copy of the GNU General Public License along with
 20  * this program; if not, write to the Free Software Foundation, Inc.,
 21  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 22  *
 23  * The full GNU General Public License is included in this distribution in
 24  * the file called "COPYING".
 25  *
 26  */
 27 
 28 #include <linux/types.h>
 29 #include <linux/kernel.h>
 30 #include <linux/module.h>
 31 #include <linux/init.h>
 32 #include <linux/videodev2.h>
 33 #include <linux/kmod.h>
 34 #include <linux/pci.h>
 35 #include <linux/interrupt.h>
 36 #include <linux/io.h>
 37 #include <linux/gpio.h>
 38 #include <linux/i2c.h>
 39 #include <linux/delay.h>
 40 #include <media/v4l2-common.h>
 41 #include <media/v4l2-device.h>
 42 #include <media/v4l2-ctrls.h>
 43 #include <media/v4l2-ioctl.h>
 44 #include <media/v4l2-fh.h>
 45 #include <media/v4l2-event.h>
 46 #include <media/videobuf2-dma-contig.h>
 47 
 48 #include "sta2x11_vip.h"
 49 
 50 #define DRV_VERSION "1.3"
 51 
 52 #ifndef PCI_DEVICE_ID_STMICRO_VIP
 53 #define PCI_DEVICE_ID_STMICRO_VIP 0xCC0D
 54 #endif
 55 
 56 #define MAX_FRAMES 4
 57 
 58 /*Register offsets*/
 59 #define DVP_CTL         0x00
 60 #define DVP_TFO         0x04
 61 #define DVP_TFS         0x08
 62 #define DVP_BFO         0x0C
 63 #define DVP_BFS         0x10
 64 #define DVP_VTP         0x14
 65 #define DVP_VBP         0x18
 66 #define DVP_VMP         0x1C
 67 #define DVP_ITM         0x98
 68 #define DVP_ITS         0x9C
 69 #define DVP_STA         0xA0
 70 #define DVP_HLFLN       0xA8
 71 #define DVP_RGB         0xC0
 72 #define DVP_PKZ         0xF0
 73 
 74 /*Register fields*/
 75 #define DVP_CTL_ENA     0x00000001
 76 #define DVP_CTL_RST     0x80000000
 77 #define DVP_CTL_DIS     (~0x00040001)
 78 
 79 #define DVP_IT_VSB      0x00000008
 80 #define DVP_IT_VST      0x00000010
 81 #define DVP_IT_FIFO     0x00000020
 82 
 83 #define DVP_HLFLN_SD    0x00000001
 84 
 85 #define SAVE_COUNT 8
 86 #define AUX_COUNT 3
 87 #define IRQ_COUNT 1
 88 
 89 
 90 struct vip_buffer {
 91         struct vb2_v4l2_buffer vb;
 92         struct list_head        list;
 93         dma_addr_t              dma;
 94 };
 95 static inline struct vip_buffer *to_vip_buffer(struct vb2_v4l2_buffer *vb2)
 96 {
 97         return container_of(vb2, struct vip_buffer, vb);
 98 }
 99 
100 /**
101  * struct sta2x11_vip - All internal data for one instance of device
102  * @v4l2_dev: device registered in v4l layer
103  * @video_dev: properties of our device
104  * @pdev: PCI device
105  * @adapter: contains I2C adapter information
106  * @register_save_area: All relevant register are saved here during suspend
107  * @decoder: contains information about video DAC
108  * @ctrl_hdl: handler for control framework
109  * @format: pixel format, fixed UYVY
110  * @std: video standard (e.g. PAL/NTSC)
111  * @input: input line for video signal ( 0 or 1 )
112  * @disabled: Device is in power down state
113  * @slock: for excluse acces of registers
114  * @alloc_ctx: context for videobuf2
115  * @vb_vidq: queue maintained by videobuf2 layer
116  * @buffer_list: list of buffer in use
117  * @sequence: sequence number of acquired buffer
118  * @active: current active buffer
119  * @lock: used in videobuf2 callback
120  * @tcount: Number of top frames
121  * @bcount: Number of bottom frames
122  * @overflow: Number of FIFO overflows
123  * @iomem: hardware base address
124  * @config: I2C and gpio config from platform
125  *
126  * All non-local data is accessed via this structure.
127  */
128 struct sta2x11_vip {
129         struct v4l2_device v4l2_dev;
130         struct video_device video_dev;
131         struct pci_dev *pdev;
132         struct i2c_adapter *adapter;
133         unsigned int register_save_area[IRQ_COUNT + SAVE_COUNT + AUX_COUNT];
134         struct v4l2_subdev *decoder;
135         struct v4l2_ctrl_handler ctrl_hdl;
136 
137 
138         struct v4l2_pix_format format;
139         v4l2_std_id std;
140         unsigned int input;
141         int disabled;
142         spinlock_t slock;
143 
144         struct vb2_alloc_ctx *alloc_ctx;
145         struct vb2_queue vb_vidq;
146         struct list_head buffer_list;
147         unsigned int sequence;
148         struct vip_buffer *active; /* current active buffer */
149         spinlock_t lock; /* Used in videobuf2 callback */
150 
151         /* Interrupt counters */
152         int tcount, bcount;
153         int overflow;
154 
155         void __iomem *iomem;    /* I/O Memory */
156         struct vip_config *config;
157 };
158 
159 static const unsigned int registers_to_save[AUX_COUNT] = {
160         DVP_HLFLN, DVP_RGB, DVP_PKZ
161 };
162 
163 static struct v4l2_pix_format formats_50[] = {
164         {                       /*PAL interlaced */
165          .width = 720,
166          .height = 576,
167          .pixelformat = V4L2_PIX_FMT_UYVY,
168          .field = V4L2_FIELD_INTERLACED,
169          .bytesperline = 720 * 2,
170          .sizeimage = 720 * 2 * 576,
171          .colorspace = V4L2_COLORSPACE_SMPTE170M},
172         {                       /*PAL top */
173          .width = 720,
174          .height = 288,
175          .pixelformat = V4L2_PIX_FMT_UYVY,
176          .field = V4L2_FIELD_TOP,
177          .bytesperline = 720 * 2,
178          .sizeimage = 720 * 2 * 288,
179          .colorspace = V4L2_COLORSPACE_SMPTE170M},
180         {                       /*PAL bottom */
181          .width = 720,
182          .height = 288,
183          .pixelformat = V4L2_PIX_FMT_UYVY,
184          .field = V4L2_FIELD_BOTTOM,
185          .bytesperline = 720 * 2,
186          .sizeimage = 720 * 2 * 288,
187          .colorspace = V4L2_COLORSPACE_SMPTE170M},
188 
189 };
190 
191 static struct v4l2_pix_format formats_60[] = {
192         {                       /*NTSC interlaced */
193          .width = 720,
194          .height = 480,
195          .pixelformat = V4L2_PIX_FMT_UYVY,
196          .field = V4L2_FIELD_INTERLACED,
197          .bytesperline = 720 * 2,
198          .sizeimage = 720 * 2 * 480,
199          .colorspace = V4L2_COLORSPACE_SMPTE170M},
200         {                       /*NTSC top */
201          .width = 720,
202          .height = 240,
203          .pixelformat = V4L2_PIX_FMT_UYVY,
204          .field = V4L2_FIELD_TOP,
205          .bytesperline = 720 * 2,
206          .sizeimage = 720 * 2 * 240,
207          .colorspace = V4L2_COLORSPACE_SMPTE170M},
208         {                       /*NTSC bottom */
209          .width = 720,
210          .height = 240,
211          .pixelformat = V4L2_PIX_FMT_UYVY,
212          .field = V4L2_FIELD_BOTTOM,
213          .bytesperline = 720 * 2,
214          .sizeimage = 720 * 2 * 240,
215          .colorspace = V4L2_COLORSPACE_SMPTE170M},
216 };
217 
218 /* Write VIP register */
219 static inline void reg_write(struct sta2x11_vip *vip, unsigned int reg, u32 val)
220 {
221         iowrite32((val), (vip->iomem)+(reg));
222 }
223 /* Read VIP register */
224 static inline u32 reg_read(struct sta2x11_vip *vip, unsigned int reg)
225 {
226         return  ioread32((vip->iomem)+(reg));
227 }
228 /* Start DMA acquisition */
229 static void start_dma(struct sta2x11_vip *vip, struct vip_buffer *vip_buf)
230 {
231         unsigned long offset = 0;
232 
233         if (vip->format.field == V4L2_FIELD_INTERLACED)
234                 offset = vip->format.width * 2;
235 
236         spin_lock_irq(&vip->slock);
237         /* Enable acquisition */
238         reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) | DVP_CTL_ENA);
239         /* Set Top and Bottom Field memory address */
240         reg_write(vip, DVP_VTP, (u32)vip_buf->dma);
241         reg_write(vip, DVP_VBP, (u32)vip_buf->dma + offset);
242         spin_unlock_irq(&vip->slock);
243 }
244 
245 /* Fetch the next buffer to activate */
246 static void vip_active_buf_next(struct sta2x11_vip *vip)
247 {
248         /* Get the next buffer */
249         spin_lock(&vip->lock);
250         if (list_empty(&vip->buffer_list)) {/* No available buffer */
251                 spin_unlock(&vip->lock);
252                 return;
253         }
254         vip->active = list_first_entry(&vip->buffer_list,
255                                        struct vip_buffer,
256                                        list);
257         /* Reset Top and Bottom counter */
258         vip->tcount = 0;
259         vip->bcount = 0;
260         spin_unlock(&vip->lock);
261         if (vb2_is_streaming(&vip->vb_vidq)) {  /* streaming is on */
262                 start_dma(vip, vip->active);    /* start dma capture */
263         }
264 }
265 
266 
267 /* Videobuf2 Operations */
268 static int queue_setup(struct vb2_queue *vq,
269                        unsigned int *nbuffers, unsigned int *nplanes,
270                        unsigned int sizes[], void *alloc_ctxs[])
271 {
272         struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
273 
274         if (!(*nbuffers) || *nbuffers < MAX_FRAMES)
275                 *nbuffers = MAX_FRAMES;
276 
277         *nplanes = 1;
278         sizes[0] = vip->format.sizeimage;
279         alloc_ctxs[0] = vip->alloc_ctx;
280 
281         vip->sequence = 0;
282         vip->active = NULL;
283         vip->tcount = 0;
284         vip->bcount = 0;
285 
286         return 0;
287 };
288 static int buffer_init(struct vb2_buffer *vb)
289 {
290         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
291         struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
292 
293         vip_buf->dma = vb2_dma_contig_plane_dma_addr(vb, 0);
294         INIT_LIST_HEAD(&vip_buf->list);
295         return 0;
296 }
297 
298 static int buffer_prepare(struct vb2_buffer *vb)
299 {
300         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
301         struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
302         struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
303         unsigned long size;
304 
305         size = vip->format.sizeimage;
306         if (vb2_plane_size(vb, 0) < size) {
307                 v4l2_err(&vip->v4l2_dev, "buffer too small (%lu < %lu)\n",
308                          vb2_plane_size(vb, 0), size);
309                 return -EINVAL;
310         }
311 
312         vb2_set_plane_payload(&vip_buf->vb.vb2_buf, 0, size);
313 
314         return 0;
315 }
316 static void buffer_queue(struct vb2_buffer *vb)
317 {
318         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
319         struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
320         struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
321 
322         spin_lock(&vip->lock);
323         list_add_tail(&vip_buf->list, &vip->buffer_list);
324         if (!vip->active) {     /* No active buffer, active the first one */
325                 vip->active = list_first_entry(&vip->buffer_list,
326                                                struct vip_buffer,
327                                                list);
328                 if (vb2_is_streaming(&vip->vb_vidq))    /* streaming is on */
329                         start_dma(vip, vip_buf);        /* start dma capture */
330         }
331         spin_unlock(&vip->lock);
332 }
333 static void buffer_finish(struct vb2_buffer *vb)
334 {
335         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
336         struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
337         struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
338 
339         /* Buffer handled, remove it from the list */
340         spin_lock(&vip->lock);
341         list_del_init(&vip_buf->list);
342         spin_unlock(&vip->lock);
343 
344         if (vb2_is_streaming(vb->vb2_queue))
345                 vip_active_buf_next(vip);
346 }
347 
348 static int start_streaming(struct vb2_queue *vq, unsigned int count)
349 {
350         struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
351 
352         spin_lock_irq(&vip->slock);
353         /* Enable interrupt VSYNC Top and Bottom*/
354         reg_write(vip, DVP_ITM, DVP_IT_VSB | DVP_IT_VST);
355         spin_unlock_irq(&vip->slock);
356 
357         if (count)
358                 start_dma(vip, vip->active);
359 
360         return 0;
361 }
362 
363 /* abort streaming and wait for last buffer */
364 static void stop_streaming(struct vb2_queue *vq)
365 {
366         struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
367         struct vip_buffer *vip_buf, *node;
368 
369         /* Disable acquisition */
370         reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
371         /* Disable all interrupts */
372         reg_write(vip, DVP_ITM, 0);
373 
374         /* Release all active buffers */
375         spin_lock(&vip->lock);
376         list_for_each_entry_safe(vip_buf, node, &vip->buffer_list, list) {
377                 vb2_buffer_done(&vip_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
378                 list_del(&vip_buf->list);
379         }
380         spin_unlock(&vip->lock);
381 }
382 
383 static struct vb2_ops vip_video_qops = {
384         .queue_setup            = queue_setup,
385         .buf_init               = buffer_init,
386         .buf_prepare            = buffer_prepare,
387         .buf_finish             = buffer_finish,
388         .buf_queue              = buffer_queue,
389         .start_streaming        = start_streaming,
390         .stop_streaming         = stop_streaming,
391 };
392 
393 
394 /* File Operations */
395 static const struct v4l2_file_operations vip_fops = {
396         .owner = THIS_MODULE,
397         .open = v4l2_fh_open,
398         .release = vb2_fop_release,
399         .unlocked_ioctl = video_ioctl2,
400         .read = vb2_fop_read,
401         .mmap = vb2_fop_mmap,
402         .poll = vb2_fop_poll
403 };
404 
405 
406 /**
407  * vidioc_querycap - return capabilities of device
408  * @file: descriptor of device
409  * @cap: contains return values
410  *
411  * the capabilities of the device are returned
412  *
413  * return value: 0, no error.
414  */
415 static int vidioc_querycap(struct file *file, void *priv,
416                            struct v4l2_capability *cap)
417 {
418         struct sta2x11_vip *vip = video_drvdata(file);
419 
420         strcpy(cap->driver, KBUILD_MODNAME);
421         strcpy(cap->card, KBUILD_MODNAME);
422         snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
423                  pci_name(vip->pdev));
424         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
425                            V4L2_CAP_STREAMING;
426         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
427 
428         return 0;
429 }
430 
431 /**
432  * vidioc_s_std - set video standard
433  * @file: descriptor of device
434  * @std: contains standard to be set
435  *
436  * the video standard is set
437  *
438  * return value: 0, no error.
439  *
440  * -EIO, no input signal detected
441  *
442  * other, returned from video DAC.
443  */
444 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id std)
445 {
446         struct sta2x11_vip *vip = video_drvdata(file);
447 
448         /*
449          * This is here for backwards compatibility only.
450          * The use of V4L2_STD_ALL to trigger a querystd is non-standard.
451          */
452         if (std == V4L2_STD_ALL) {
453                 v4l2_subdev_call(vip->decoder, video, querystd, &std);
454                 if (std == V4L2_STD_UNKNOWN)
455                         return -EIO;
456         }
457 
458         if (vip->std != std) {
459                 vip->std = std;
460                 if (V4L2_STD_525_60 & std)
461                         vip->format = formats_60[0];
462                 else
463                         vip->format = formats_50[0];
464         }
465 
466         return v4l2_subdev_call(vip->decoder, video, s_std, std);
467 }
468 
469 /**
470  * vidioc_g_std - get video standard
471  * @file: descriptor of device
472  * @std: contains return values
473  *
474  * the current video standard is returned
475  *
476  * return value: 0, no error.
477  */
478 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
479 {
480         struct sta2x11_vip *vip = video_drvdata(file);
481 
482         *std = vip->std;
483         return 0;
484 }
485 
486 /**
487  * vidioc_querystd - get possible video standards
488  * @file: descriptor of device
489  * @std: contains return values
490  *
491  * all possible video standards are returned
492  *
493  * return value: delivered by video DAC routine.
494  */
495 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
496 {
497         struct sta2x11_vip *vip = video_drvdata(file);
498 
499         return v4l2_subdev_call(vip->decoder, video, querystd, std);
500 }
501 
502 static int vidioc_enum_input(struct file *file, void *priv,
503                              struct v4l2_input *inp)
504 {
505         if (inp->index > 1)
506                 return -EINVAL;
507 
508         inp->type = V4L2_INPUT_TYPE_CAMERA;
509         inp->std = V4L2_STD_ALL;
510         sprintf(inp->name, "Camera %u", inp->index);
511 
512         return 0;
513 }
514 
515 /**
516  * vidioc_s_input - set input line
517  * @file: descriptor of device
518  * @i: new input line number
519  *
520  * the current active input line is set
521  *
522  * return value: 0, no error.
523  *
524  * -EINVAL, line number out of range
525  */
526 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
527 {
528         struct sta2x11_vip *vip = video_drvdata(file);
529         int ret;
530 
531         if (i > 1)
532                 return -EINVAL;
533         ret = v4l2_subdev_call(vip->decoder, video, s_routing, i, 0, 0);
534 
535         if (!ret)
536                 vip->input = i;
537 
538         return 0;
539 }
540 
541 /**
542  * vidioc_g_input - return input line
543  * @file: descriptor of device
544  * @i: returned input line number
545  *
546  * the current active input line is returned
547  *
548  * return value: always 0.
549  */
550 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
551 {
552         struct sta2x11_vip *vip = video_drvdata(file);
553 
554         *i = vip->input;
555         return 0;
556 }
557 
558 /**
559  * vidioc_enum_fmt_vid_cap - return video capture format
560  * @f: returned format information
561  *
562  * returns name and format of video capture
563  * Only UYVY is supported by hardware.
564  *
565  * return value: always 0.
566  */
567 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
568                                    struct v4l2_fmtdesc *f)
569 {
570 
571         if (f->index != 0)
572                 return -EINVAL;
573 
574         strcpy(f->description, "4:2:2, packed, UYVY");
575         f->pixelformat = V4L2_PIX_FMT_UYVY;
576         f->flags = 0;
577         return 0;
578 }
579 
580 /**
581  * vidioc_try_fmt_vid_cap - set video capture format
582  * @file: descriptor of device
583  * @f: new format
584  *
585  * new video format is set which includes width and
586  * field type. width is fixed to 720, no scaling.
587  * Only UYVY is supported by this hardware.
588  * the minimum height is 200, the maximum is 576 (PAL)
589  *
590  * return value: 0, no error
591  *
592  * -EINVAL, pixel or field format not supported
593  *
594  */
595 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
596                                   struct v4l2_format *f)
597 {
598         struct sta2x11_vip *vip = video_drvdata(file);
599         int interlace_lim;
600 
601         if (V4L2_PIX_FMT_UYVY != f->fmt.pix.pixelformat) {
602                 v4l2_warn(&vip->v4l2_dev, "Invalid format, only UYVY supported\n");
603                 return -EINVAL;
604         }
605 
606         if (V4L2_STD_525_60 & vip->std)
607                 interlace_lim = 240;
608         else
609                 interlace_lim = 288;
610 
611         switch (f->fmt.pix.field) {
612         default:
613         case V4L2_FIELD_ANY:
614                 if (interlace_lim < f->fmt.pix.height)
615                         f->fmt.pix.field = V4L2_FIELD_INTERLACED;
616                 else
617                         f->fmt.pix.field = V4L2_FIELD_BOTTOM;
618                 break;
619         case V4L2_FIELD_TOP:
620         case V4L2_FIELD_BOTTOM:
621                 if (interlace_lim < f->fmt.pix.height)
622                         f->fmt.pix.height = interlace_lim;
623                 break;
624         case V4L2_FIELD_INTERLACED:
625                 break;
626         }
627 
628         /* It is the only supported format */
629         f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
630         f->fmt.pix.height &= ~1;
631         if (2 * interlace_lim < f->fmt.pix.height)
632                 f->fmt.pix.height = 2 * interlace_lim;
633         if (200 > f->fmt.pix.height)
634                 f->fmt.pix.height = 200;
635         f->fmt.pix.width = 720;
636         f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
637         f->fmt.pix.sizeimage = f->fmt.pix.width * 2 * f->fmt.pix.height;
638         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
639         return 0;
640 }
641 
642 /**
643  * vidioc_s_fmt_vid_cap - set current video format parameters
644  * @file: descriptor of device
645  * @f: returned format information
646  *
647  * set new capture format
648  * return value: 0, no error
649  *
650  * other, delivered by video DAC routine.
651  */
652 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
653                                 struct v4l2_format *f)
654 {
655         struct sta2x11_vip *vip = video_drvdata(file);
656         unsigned int t_stop, b_stop, pitch;
657         int ret;
658 
659         ret = vidioc_try_fmt_vid_cap(file, priv, f);
660         if (ret)
661                 return ret;
662 
663         if (vb2_is_busy(&vip->vb_vidq)) {
664                 /* Can't change format during acquisition */
665                 v4l2_err(&vip->v4l2_dev, "device busy\n");
666                 return -EBUSY;
667         }
668         vip->format = f->fmt.pix;
669         switch (vip->format.field) {
670         case V4L2_FIELD_INTERLACED:
671                 t_stop = ((vip->format.height / 2 - 1) << 16) |
672                          (2 * vip->format.width - 1);
673                 b_stop = t_stop;
674                 pitch = 4 * vip->format.width;
675                 break;
676         case V4L2_FIELD_TOP:
677                 t_stop = ((vip->format.height - 1) << 16) |
678                          (2 * vip->format.width - 1);
679                 b_stop = (0 << 16) | (2 * vip->format.width - 1);
680                 pitch = 2 * vip->format.width;
681                 break;
682         case V4L2_FIELD_BOTTOM:
683                 t_stop = (0 << 16) | (2 * vip->format.width - 1);
684                 b_stop = (vip->format.height << 16) |
685                          (2 * vip->format.width - 1);
686                 pitch = 2 * vip->format.width;
687                 break;
688         default:
689                 v4l2_err(&vip->v4l2_dev, "unknown field format\n");
690                 return -EINVAL;
691         }
692 
693         spin_lock_irq(&vip->slock);
694         /* Y-X Top Field Offset */
695         reg_write(vip, DVP_TFO, 0);
696         /* Y-X Bottom Field Offset */
697         reg_write(vip, DVP_BFO, 0);
698         /* Y-X Top Field Stop*/
699         reg_write(vip, DVP_TFS, t_stop);
700         /* Y-X Bottom Field Stop */
701         reg_write(vip, DVP_BFS, b_stop);
702         /* Video Memory Pitch */
703         reg_write(vip, DVP_VMP, pitch);
704         spin_unlock_irq(&vip->slock);
705 
706         return 0;
707 }
708 
709 /**
710  * vidioc_g_fmt_vid_cap - get current video format parameters
711  * @file: descriptor of device
712  * @f: contains format information
713  *
714  * returns current video format parameters
715  *
716  * return value: 0, always successful
717  */
718 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
719                                 struct v4l2_format *f)
720 {
721         struct sta2x11_vip *vip = video_drvdata(file);
722 
723         f->fmt.pix = vip->format;
724 
725         return 0;
726 }
727 
728 static const struct v4l2_ioctl_ops vip_ioctl_ops = {
729         .vidioc_querycap = vidioc_querycap,
730         /* FMT handling */
731         .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
732         .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
733         .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
734         .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
735         /* Buffer handlers */
736         .vidioc_create_bufs = vb2_ioctl_create_bufs,
737         .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
738         .vidioc_reqbufs = vb2_ioctl_reqbufs,
739         .vidioc_querybuf = vb2_ioctl_querybuf,
740         .vidioc_qbuf = vb2_ioctl_qbuf,
741         .vidioc_dqbuf = vb2_ioctl_dqbuf,
742         /* Stream on/off */
743         .vidioc_streamon = vb2_ioctl_streamon,
744         .vidioc_streamoff = vb2_ioctl_streamoff,
745         /* Standard handling */
746         .vidioc_g_std = vidioc_g_std,
747         .vidioc_s_std = vidioc_s_std,
748         .vidioc_querystd = vidioc_querystd,
749         /* Input handling */
750         .vidioc_enum_input = vidioc_enum_input,
751         .vidioc_g_input = vidioc_g_input,
752         .vidioc_s_input = vidioc_s_input,
753         /* Log status ioctl */
754         .vidioc_log_status = v4l2_ctrl_log_status,
755         /* Event handling */
756         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
757         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
758 };
759 
760 static struct video_device video_dev_template = {
761         .name = KBUILD_MODNAME,
762         .release = video_device_release_empty,
763         .fops = &vip_fops,
764         .ioctl_ops = &vip_ioctl_ops,
765         .tvnorms = V4L2_STD_ALL,
766 };
767 
768 /**
769  * vip_irq - interrupt routine
770  * @irq: Number of interrupt ( not used, correct number is assumed )
771  * @vip: local data structure containing all information
772  *
773  * check for both frame interrupts set ( top and bottom ).
774  * check FIFO overflow, but limit number of log messages after open.
775  * signal a complete buffer if done
776  *
777  * return value: IRQ_NONE, interrupt was not generated by VIP
778  *
779  * IRQ_HANDLED, interrupt done.
780  */
781 static irqreturn_t vip_irq(int irq, struct sta2x11_vip *vip)
782 {
783         unsigned int status;
784 
785         status = reg_read(vip, DVP_ITS);
786 
787         if (!status)            /* No interrupt to handle */
788                 return IRQ_NONE;
789 
790         if (status & DVP_IT_FIFO)
791                 if (vip->overflow++ > 5)
792                         pr_info("VIP: fifo overflow\n");
793 
794         if ((status & DVP_IT_VST) && (status & DVP_IT_VSB)) {
795                 /* this is bad, we are too slow, hope the condition is gone
796                  * on the next frame */
797                 return IRQ_HANDLED;
798         }
799 
800         if (status & DVP_IT_VST)
801                 if ((++vip->tcount) < 2)
802                         return IRQ_HANDLED;
803         if (status & DVP_IT_VSB) {
804                 vip->bcount++;
805                 return IRQ_HANDLED;
806         }
807 
808         if (vip->active) { /* Acquisition is over on this buffer */
809                 /* Disable acquisition */
810                 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
811                 /* Remove the active buffer from the list */
812                 vip->active->vb.vb2_buf.timestamp = ktime_get_ns();
813                 vip->active->vb.sequence = vip->sequence++;
814                 vb2_buffer_done(&vip->active->vb.vb2_buf, VB2_BUF_STATE_DONE);
815         }
816 
817         return IRQ_HANDLED;
818 }
819 
820 static void sta2x11_vip_init_register(struct sta2x11_vip *vip)
821 {
822         /* Register initialization */
823         spin_lock_irq(&vip->slock);
824         /* Clean interrupt */
825         reg_read(vip, DVP_ITS);
826         /* Enable Half Line per vertical */
827         reg_write(vip, DVP_HLFLN, DVP_HLFLN_SD);
828         /* Reset VIP control */
829         reg_write(vip, DVP_CTL, DVP_CTL_RST);
830         /* Clear VIP control */
831         reg_write(vip, DVP_CTL, 0);
832         spin_unlock_irq(&vip->slock);
833 }
834 static void sta2x11_vip_clear_register(struct sta2x11_vip *vip)
835 {
836         spin_lock_irq(&vip->slock);
837         /* Disable interrupt */
838         reg_write(vip, DVP_ITM, 0);
839         /* Reset VIP Control */
840         reg_write(vip, DVP_CTL, DVP_CTL_RST);
841         /* Clear VIP Control */
842         reg_write(vip, DVP_CTL, 0);
843         /* Clean VIP Interrupt */
844         reg_read(vip, DVP_ITS);
845         spin_unlock_irq(&vip->slock);
846 }
847 static int sta2x11_vip_init_buffer(struct sta2x11_vip *vip)
848 {
849         int err;
850 
851         err = dma_set_coherent_mask(&vip->pdev->dev, DMA_BIT_MASK(29));
852         if (err) {
853                 v4l2_err(&vip->v4l2_dev, "Cannot configure coherent mask");
854                 return err;
855         }
856         memset(&vip->vb_vidq, 0, sizeof(struct vb2_queue));