Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sta2x11_vip.c
Go to the documentation of this file.
1 /*
2  * This is the driver for the STA2x11 Video Input Port.
3  *
4  * Copyright (C) 2010 WindRiver Systems, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * The full GNU General Public License is included in this distribution in
20  * the file called "COPYING".
21  *
22  * Author: Andreas Kies <[email protected]>
23  * Vlad Lungu <[email protected]>
24  *
25  */
26 
27 #include <linux/types.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/vmalloc.h>
32 
33 #include <linux/videodev2.h>
34 
35 #include <linux/kmod.h>
36 
37 #include <linux/pci.h>
38 #include <linux/interrupt.h>
39 #include <linux/mutex.h>
40 #include <linux/io.h>
41 #include <linux/gpio.h>
42 #include <linux/i2c.h>
43 #include <linux/delay.h>
44 #include <media/v4l2-common.h>
45 #include <media/v4l2-device.h>
46 #include <media/v4l2-ioctl.h>
48 
49 #include "sta2x11_vip.h"
50 
51 #define DRV_NAME "sta2x11_vip"
52 #define DRV_VERSION "1.3"
53 
54 #ifndef PCI_DEVICE_ID_STMICRO_VIP
55 #define PCI_DEVICE_ID_STMICRO_VIP 0xCC0D
56 #endif
57 
58 #define MAX_FRAMES 4
59 
60 /*Register offsets*/
61 #define DVP_CTL 0x00
62 #define DVP_TFO 0x04
63 #define DVP_TFS 0x08
64 #define DVP_BFO 0x0C
65 #define DVP_BFS 0x10
66 #define DVP_VTP 0x14
67 #define DVP_VBP 0x18
68 #define DVP_VMP 0x1C
69 #define DVP_ITM 0x98
70 #define DVP_ITS 0x9C
71 #define DVP_STA 0xA0
72 #define DVP_HLFLN 0xA8
73 #define DVP_RGB 0xC0
74 #define DVP_PKZ 0xF0
75 
76 /*Register fields*/
77 #define DVP_CTL_ENA 0x00000001
78 #define DVP_CTL_RST 0x80000000
79 #define DVP_CTL_DIS (~0x00040001)
80 
81 #define DVP_IT_VSB 0x00000008
82 #define DVP_IT_VST 0x00000010
83 #define DVP_IT_FIFO 0x00000020
84 
85 #define DVP_HLFLN_SD 0x00000001
86 
87 #define REG_WRITE(vip, reg, value) iowrite32((value), (vip->iomem)+(reg))
88 #define REG_READ(vip, reg) ioread32((vip->iomem)+(reg))
89 
90 #define SAVE_COUNT 8
91 #define AUX_COUNT 3
92 #define IRQ_COUNT 1
93 
125 struct sta2x11_vip {
128  struct pci_dev *pdev;
134  unsigned int input;
135  int users;
136  int disabled;
137  struct mutex mutex; /* exclusive access during open */
138  spinlock_t slock; /* spin lock for hardware and queue access */
143  int overflow;
144  void *mem_spare;
146  void *iomem;
148 };
149 
150 static const unsigned int registers_to_save[AUX_COUNT] = {
152 };
153 
154 static struct v4l2_pix_format formats_50[] = {
155  { /*PAL interlaced */
156  .width = 720,
157  .height = 576,
158  .pixelformat = V4L2_PIX_FMT_UYVY,
159  .field = V4L2_FIELD_INTERLACED,
160  .bytesperline = 720 * 2,
161  .sizeimage = 720 * 2 * 576,
162  .colorspace = V4L2_COLORSPACE_SMPTE170M},
163  { /*PAL top */
164  .width = 720,
165  .height = 288,
166  .pixelformat = V4L2_PIX_FMT_UYVY,
167  .field = V4L2_FIELD_TOP,
168  .bytesperline = 720 * 2,
169  .sizeimage = 720 * 2 * 288,
170  .colorspace = V4L2_COLORSPACE_SMPTE170M},
171  { /*PAL bottom */
172  .width = 720,
173  .height = 288,
174  .pixelformat = V4L2_PIX_FMT_UYVY,
175  .field = V4L2_FIELD_BOTTOM,
176  .bytesperline = 720 * 2,
177  .sizeimage = 720 * 2 * 288,
178  .colorspace = V4L2_COLORSPACE_SMPTE170M},
179 
180 };
181 
182 static struct v4l2_pix_format formats_60[] = {
183  { /*NTSC interlaced */
184  .width = 720,
185  .height = 480,
186  .pixelformat = V4L2_PIX_FMT_UYVY,
187  .field = V4L2_FIELD_INTERLACED,
188  .bytesperline = 720 * 2,
189  .sizeimage = 720 * 2 * 480,
190  .colorspace = V4L2_COLORSPACE_SMPTE170M},
191  { /*NTSC top */
192  .width = 720,
193  .height = 240,
194  .pixelformat = V4L2_PIX_FMT_UYVY,
195  .field = V4L2_FIELD_TOP,
196  .bytesperline = 720 * 2,
197  .sizeimage = 720 * 2 * 240,
198  .colorspace = V4L2_COLORSPACE_SMPTE170M},
199  { /*NTSC bottom */
200  .width = 720,
201  .height = 240,
202  .pixelformat = V4L2_PIX_FMT_UYVY,
203  .field = V4L2_FIELD_BOTTOM,
204  .bytesperline = 720 * 2,
205  .sizeimage = 720 * 2 * 240,
206  .colorspace = V4L2_COLORSPACE_SMPTE170M},
207 };
208 
220 static int buf_setup(struct videobuf_queue *vq, unsigned int *count,
221  unsigned int *size)
222 {
223  struct sta2x11_vip *vip = vq->priv_data;
224 
225  *size = vip->format.width * vip->format.height * 2;
226  if (0 == *count || MAX_FRAMES < *count)
227  *count = MAX_FRAMES;
228  return 0;
229 };
230 
244 static int buf_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
245  enum v4l2_field field)
246 {
247  struct sta2x11_vip *vip = vq->priv_data;
248  int ret;
249 
250  vb->size = vip->format.width * vip->format.height * 2;
251  if ((0 != vb->baddr) && (vb->bsize < vb->size))
252  return -EINVAL;
253  vb->width = vip->format.width;
254  vb->height = vip->format.height;
255  vb->field = field;
256 
257  if (VIDEOBUF_NEEDS_INIT == vb->state) {
258  ret = videobuf_iolock(vq, vb, NULL);
259  if (ret)
260  goto fail;
261  }
262  vb->state = VIDEOBUF_PREPARED;
263  return 0;
264 fail:
265  videobuf_dma_contig_free(vq, vb);
267  return ret;
268 }
269 
278 static void buf_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
279 {
280  struct sta2x11_vip *vip = vq->priv_data;
281  u32 dma;
282 
283  vb->state = VIDEOBUF_QUEUED;
284 
285  if (vip->active) {
286  list_add_tail(&vb->queue, &vip->capture);
287  return;
288  }
289 
290  vip->started = 1;
291  vip->tcount = 0;
292  vip->bcount = 0;
293  vip->active = vb;
294  vb->state = VIDEOBUF_ACTIVE;
295 
296  dma = videobuf_to_dma_contig(vb);
297 
298  REG_WRITE(vip, DVP_TFO, (0 << 16) | (0));
299  /* despite of interlace mode, upper and lower frames start at zero */
300  REG_WRITE(vip, DVP_BFO, (0 << 16) | (0));
301 
302  switch (vip->format.field) {
304  REG_WRITE(vip, DVP_TFS,
305  ((vip->format.height / 2 - 1) << 16) |
306  (2 * vip->format.width - 1));
307  REG_WRITE(vip, DVP_BFS, ((vip->format.height / 2 - 1) << 16) |
308  (2 * vip->format.width - 1));
309  REG_WRITE(vip, DVP_VTP, dma);
310  REG_WRITE(vip, DVP_VBP, dma + vip->format.width * 2);
311  REG_WRITE(vip, DVP_VMP, 4 * vip->format.width);
312  break;
313  case V4L2_FIELD_TOP:
314  REG_WRITE(vip, DVP_TFS,
315  ((vip->format.height - 1) << 16) |
316  (2 * vip->format.width - 1));
317  REG_WRITE(vip, DVP_BFS, ((0) << 16) |
318  (2 * vip->format.width - 1));
319  REG_WRITE(vip, DVP_VTP, dma);
320  REG_WRITE(vip, DVP_VBP, dma);
321  REG_WRITE(vip, DVP_VMP, 2 * vip->format.width);
322  break;
323  case V4L2_FIELD_BOTTOM:
324  REG_WRITE(vip, DVP_TFS, ((0) << 16) |
325  (2 * vip->format.width - 1));
326  REG_WRITE(vip, DVP_BFS,
327  ((vip->format.height) << 16) |
328  (2 * vip->format.width - 1));
329  REG_WRITE(vip, DVP_VTP, dma);
330  REG_WRITE(vip, DVP_VBP, dma);
331  REG_WRITE(vip, DVP_VMP, 2 * vip->format.width);
332  break;
333 
334  default:
335  pr_warning("VIP: unknown field format\n");
336  return;
337  }
338 
340 }
341 
349 static void buf_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
350 {
351 
352  videobuf_dma_contig_free(vq, vb);
354 }
355 
356 static struct videobuf_queue_ops vip_qops = {
357  .buf_setup = buf_setup,
358  .buf_prepare = buf_prepare,
359  .buf_queue = buf_queue,
360  .buf_release = buf_release,
361 };
362 
374 static int vip_open(struct file *file)
375 {
376  struct video_device *dev = video_devdata(file);
377  struct sta2x11_vip *vip = video_get_drvdata(dev);
378 
379  mutex_lock(&vip->mutex);
380  vip->users++;
381 
382  if (vip->users > 1) {
383  vip->users--;
384  mutex_unlock(&vip->mutex);
385  return -EBUSY;
386  }
387 
388  file->private_data = dev;
389  vip->overflow = 0;
390  vip->started = 0;
391  vip->closing = 0;
392  vip->active = NULL;
393 
394  INIT_LIST_HEAD(&vip->capture);
395  vip->mem_spare = dma_alloc_coherent(&vip->pdev->dev, 64,
396  &vip->dma_spare, GFP_KERNEL);
397  if (!vip->mem_spare) {
398  vip->users--;
399  mutex_unlock(&vip->mutex);
400  return -ENOMEM;
401  }
402 
403  mutex_unlock(&vip->mutex);
405  &vip_qops,
406  &vip->pdev->dev,
407  &vip->slock,
410  sizeof(struct videobuf_buffer),
411  vip, NULL);
412  REG_READ(vip, DVP_ITS);
416  REG_WRITE(vip, DVP_CTL, 0);
417  REG_READ(vip, DVP_ITS);
418  return 0;
419 }
420 
431 static int vip_close(struct file *file)
432 {
433  struct video_device *dev = video_devdata(file);
434  struct sta2x11_vip *vip = video_get_drvdata(dev);
435 
436  vip->closing = 1;
437  if (vip->active)
438  videobuf_waiton(&vip->vb_vidq, vip->active, 0, 0);
439  spin_lock_irq(&vip->slock);
440 
441  REG_WRITE(vip, DVP_ITM, 0);
443  REG_WRITE(vip, DVP_CTL, 0);
444  REG_READ(vip, DVP_ITS);
445 
446  vip->started = 0;
447  vip->active = NULL;
448 
449  spin_unlock_irq(&vip->slock);
450 
451  videobuf_stop(&vip->vb_vidq);
453 
454  dma_free_coherent(&vip->pdev->dev, 64, vip->mem_spare, vip->dma_spare);
455  file->private_data = NULL;
456  mutex_lock(&vip->mutex);
457  vip->users--;
458  mutex_unlock(&vip->mutex);
459  return 0;
460 }
461 
473 static ssize_t vip_read(struct file *file, char __user *data,
474  size_t count, loff_t *ppos)
475 {
476  struct video_device *dev = file->private_data;
477  struct sta2x11_vip *vip = video_get_drvdata(dev);
478 
479  return videobuf_read_stream(&vip->vb_vidq, data, count, ppos, 0,
480  file->f_flags & O_NONBLOCK);
481 }
482 
492 static int vip_mmap(struct file *file, struct vm_area_struct *vma)
493 {
494  struct video_device *dev = file->private_data;
495  struct sta2x11_vip *vip = video_get_drvdata(dev);
496 
497  return videobuf_mmap_mapper(&vip->vb_vidq, vma);
498 }
499 
509 static unsigned int vip_poll(struct file *file, struct poll_table_struct *wait)
510 {
511  struct video_device *dev = file->private_data;
512  struct sta2x11_vip *vip = video_get_drvdata(dev);
513 
514  return videobuf_poll_stream(file, &vip->vb_vidq, wait);
515 }
516 
527 static int vidioc_querycap(struct file *file, void *priv,
528  struct v4l2_capability *cap)
529 {
530  struct video_device *dev = priv;
531  struct sta2x11_vip *vip = video_get_drvdata(dev);
532 
533  memset(cap, 0, sizeof(struct v4l2_capability));
534  strcpy(cap->driver, DRV_NAME);
535  strcpy(cap->card, DRV_NAME);
536  cap->version = 0;
537  snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
538  pci_name(vip->pdev));
541 
542  return 0;
543 }
544 
559 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *std)
560 {
561  struct video_device *dev = priv;
562  struct sta2x11_vip *vip = video_get_drvdata(dev);
563  v4l2_std_id oldstd = vip->std, newstd;
564  int status;
565 
566  if (V4L2_STD_ALL == *std) {
567  v4l2_subdev_call(vip->decoder, core, s_std, *std);
568  ssleep(2);
569  v4l2_subdev_call(vip->decoder, video, querystd, &newstd);
570  v4l2_subdev_call(vip->decoder, video, g_input_status, &status);
571  if (status & V4L2_IN_ST_NO_SIGNAL)
572  return -EIO;
573  *std = vip->std = newstd;
574  if (oldstd != *std) {
575  if (V4L2_STD_525_60 & (*std))
576  vip->format = formats_60[0];
577  else
578  vip->format = formats_50[0];
579  }
580  return 0;
581  }
582 
583  if (oldstd != *std) {
584  if (V4L2_STD_525_60 & (*std))
585  vip->format = formats_60[0];
586  else
587  vip->format = formats_50[0];
588  }
589 
590  return v4l2_subdev_call(vip->decoder, core, s_std, *std);
591 }
592 
603 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
604 {
605  struct video_device *dev = priv;
606  struct sta2x11_vip *vip = video_get_drvdata(dev);
607 
608  *std = vip->std;
609  return 0;
610 }
611 
622 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
623 {
624  struct video_device *dev = priv;
625  struct sta2x11_vip *vip = video_get_drvdata(dev);
626 
627  return v4l2_subdev_call(vip->decoder, video, querystd, std);
628 
629 }
630 
640 static int vidioc_queryctrl(struct file *file, void *priv,
641  struct v4l2_queryctrl *ctrl)
642 {
643  struct video_device *dev = priv;
644  struct sta2x11_vip *vip = video_get_drvdata(dev);
645 
646  return v4l2_subdev_call(vip->decoder, core, queryctrl, ctrl);
647 }
648 
658 static int vidioc_g_ctrl(struct file *file, void *priv,
659  struct v4l2_control *ctrl)
660 {
661  struct video_device *dev = priv;
662  struct sta2x11_vip *vip = video_get_drvdata(dev);
663 
664  return v4l2_subdev_call(vip->decoder, core, g_ctrl, ctrl);
665 }
666 
676 static int vidioc_s_ctrl(struct file *file, void *priv,
677  struct v4l2_control *ctrl)
678 {
679  struct video_device *dev = priv;
680  struct sta2x11_vip *vip = video_get_drvdata(dev);
681 
682  return v4l2_subdev_call(vip->decoder, core, s_ctrl, ctrl);
683 }
684 
697 static int vidioc_enum_input(struct file *file, void *priv,
698  struct v4l2_input *inp)
699 {
700  if (inp->index > 1)
701  return -EINVAL;
702 
704  inp->std = V4L2_STD_ALL;
705  sprintf(inp->name, "Camera %u", inp->index);
706 
707  return 0;
708 }
709 
722 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
723 {
724  struct video_device *dev = priv;
725  struct sta2x11_vip *vip = video_get_drvdata(dev);
726  int ret;
727 
728  if (i > 1)
729  return -EINVAL;
730  ret = v4l2_subdev_call(vip->decoder, video, s_routing, i, 0, 0);
731 
732  if (!ret)
733  vip->input = i;
734 
735  return 0;
736 }
737 
748 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
749 {
750  struct video_device *dev = priv;
751  struct sta2x11_vip *vip = video_get_drvdata(dev);
752 
753  *i = vip->input;
754  return 0;
755 }
756 
768 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
769  struct v4l2_fmtdesc *f)
770 {
771 
772  if (f->index != 0)
773  return -EINVAL;
774 
775  strcpy(f->description, "4:2:2, packed, UYVY");
777  f->flags = 0;
778  return 0;
779 }
780 
797 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
798  struct v4l2_format *f)
799 {
800  struct video_device *dev = priv;
801  struct sta2x11_vip *vip = video_get_drvdata(dev);
802  int interlace_lim;
803 
804  if (V4L2_PIX_FMT_UYVY != f->fmt.pix.pixelformat)
805  return -EINVAL;
806 
807  if (V4L2_STD_525_60 & vip->std)
808  interlace_lim = 240;
809  else
810  interlace_lim = 288;
811 
812  switch (f->fmt.pix.field) {
813  case V4L2_FIELD_ANY:
814  if (interlace_lim < f->fmt.pix.height)
815  f->fmt.pix.field = V4L2_FIELD_INTERLACED;
816  else
817  f->fmt.pix.field = V4L2_FIELD_BOTTOM;
818  break;
819  case V4L2_FIELD_TOP:
820  case V4L2_FIELD_BOTTOM:
821  if (interlace_lim < f->fmt.pix.height)
822  f->fmt.pix.height = interlace_lim;
823  break;
825  break;
826  default:
827  return -EINVAL;
828  }
829 
830  f->fmt.pix.height &= ~1;
831  if (2 * interlace_lim < f->fmt.pix.height)
832  f->fmt.pix.height = 2 * interlace_lim;
833  if (200 > f->fmt.pix.height)
834  f->fmt.pix.height = 200;
835  f->fmt.pix.width = 720;
836  f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
837  f->fmt.pix.sizeimage = f->fmt.pix.width * 2 * f->fmt.pix.height;
838  f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
839  f->fmt.pix.priv = 0;
840  return 0;
841 }
842 
854 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
855  struct v4l2_format *f)
856 {
857  struct video_device *dev = priv;
858  struct sta2x11_vip *vip = video_get_drvdata(dev);
859  int ret;
860 
861  ret = vidioc_try_fmt_vid_cap(file, priv, f);
862  if (ret)
863  return ret;
864 
865  memcpy(&vip->format, &f->fmt.pix, sizeof(struct v4l2_pix_format));
866  return 0;
867 }
868 
879 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
880  struct v4l2_format *f)
881 {
882  struct video_device *dev = priv;
883  struct sta2x11_vip *vip = video_get_drvdata(dev);
884 
885  memcpy(&f->fmt.pix, &vip->format, sizeof(struct v4l2_pix_format));
886  return 0;
887 }
888 
897 static int vidioc_reqbufs(struct file *file, void *priv,
898  struct v4l2_requestbuffers *p)
899 {
900  struct video_device *dev = priv;
901  struct sta2x11_vip *vip = video_get_drvdata(dev);
902 
903  return videobuf_reqbufs(&vip->vb_vidq, p);
904 }
905 
915 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
916 {
917  struct video_device *dev = priv;
918  struct sta2x11_vip *vip = video_get_drvdata(dev);
919 
920  return videobuf_querybuf(&vip->vb_vidq, p);
921 }
922 
931 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
932 {
933  struct video_device *dev = priv;
934  struct sta2x11_vip *vip = video_get_drvdata(dev);
935 
936  return videobuf_qbuf(&vip->vb_vidq, p);
937 }
938 
947 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
948 {
949  struct video_device *dev = priv;
950  struct sta2x11_vip *vip = video_get_drvdata(dev);
951 
952  return videobuf_dqbuf(&vip->vb_vidq, p, file->f_flags & O_NONBLOCK);
953 }
954 
964 static int vidioc_streamon(struct file *file, void *priv,
965  enum v4l2_buf_type type)
966 {
967  struct video_device *dev = priv;
968  struct sta2x11_vip *vip = video_get_drvdata(dev);
969 
970  return videobuf_streamon(&vip->vb_vidq);
971 }
972 
982 static int vidioc_streamoff(struct file *file, void *priv,
983  enum v4l2_buf_type type)
984 {
985  struct video_device *dev = priv;
986  struct sta2x11_vip *vip = video_get_drvdata(dev);
987 
988  return videobuf_streamoff(&vip->vb_vidq);
989 }
990 
991 static const struct v4l2_file_operations vip_fops = {
992  .owner = THIS_MODULE,
993  .open = vip_open,
994  .release = vip_close,
995  .ioctl = video_ioctl2,
996  .read = vip_read,
997  .mmap = vip_mmap,
998  .poll = vip_poll
999 };
1000 
1001 static const struct v4l2_ioctl_ops vip_ioctl_ops = {
1002  .vidioc_querycap = vidioc_querycap,
1003  .vidioc_s_std = vidioc_s_std,
1004  .vidioc_g_std = vidioc_g_std,
1005  .vidioc_querystd = vidioc_querystd,
1006  .vidioc_queryctrl = vidioc_queryctrl,
1007  .vidioc_g_ctrl = vidioc_g_ctrl,
1008  .vidioc_s_ctrl = vidioc_s_ctrl,
1009  .vidioc_enum_input = vidioc_enum_input,
1010  .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1011  .vidioc_s_input = vidioc_s_input,
1012  .vidioc_g_input = vidioc_g_input,
1013  .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1014  .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1015  .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1016  .vidioc_reqbufs = vidioc_reqbufs,
1017  .vidioc_querybuf = vidioc_querybuf,
1018  .vidioc_qbuf = vidioc_qbuf,
1019  .vidioc_dqbuf = vidioc_dqbuf,
1020  .vidioc_streamon = vidioc_streamon,
1021  .vidioc_streamoff = vidioc_streamoff,
1022 };
1023 
1024 static struct video_device video_dev_template = {
1025  .name = DRV_NAME,
1026  .release = video_device_release,
1027  .fops = &vip_fops,
1028  .ioctl_ops = &vip_ioctl_ops,
1029  .tvnorms = V4L2_STD_ALL,
1030 };
1031 
1047 static irqreturn_t vip_irq(int irq, struct sta2x11_vip *vip)
1048 {
1049  u32 status, dma;
1050  unsigned long flags;
1051  struct videobuf_buffer *vb;
1052 
1053  status = REG_READ(vip, DVP_ITS);
1054 
1055  if (!status) {
1056  pr_debug("VIP: irq ignored\n");
1057  return IRQ_NONE;
1058  }
1059 
1060  if (!vip->started)
1061  return IRQ_HANDLED;
1062 
1063  if (status & DVP_IT_VSB)
1064  vip->bcount++;
1065 
1066  if (status & DVP_IT_VST)
1067  vip->tcount++;
1068 
1069  if ((DVP_IT_VSB | DVP_IT_VST) == (status & (DVP_IT_VST | DVP_IT_VSB))) {
1070  /* this is bad, we are too slow, hope the condition is gone
1071  * on the next frame */
1072  pr_info("VIP: both irqs\n");
1073  return IRQ_HANDLED;
1074  }
1075 
1076  if (status & DVP_IT_FIFO) {
1077  if (5 > vip->overflow++)
1078  pr_info("VIP: fifo overflow\n");
1079  }
1080 
1081  if (2 > vip->tcount)
1082  return IRQ_HANDLED;
1083 
1084  if (status & DVP_IT_VSB)
1085  return IRQ_HANDLED;
1086 
1087  spin_lock_irqsave(&vip->slock, flags);
1088 
1089  REG_WRITE(vip, DVP_CTL, REG_READ(vip, DVP_CTL) & ~DVP_CTL_ENA);
1090  if (vip->active) {
1091  do_gettimeofday(&vip->active->ts);
1092  vip->active->field_count++;
1093  vip->active->state = VIDEOBUF_DONE;
1094  wake_up(&vip->active->done);
1095  vip->active = NULL;
1096  }
1097  if (!vip->closing) {
1098  if (list_empty(&vip->capture))
1099  goto done;
1100 
1101  vb = list_first_entry(&vip->capture, struct videobuf_buffer,
1102  queue);
1103  if (NULL == vb) {
1104  pr_info("VIP: no buffer\n");
1105  goto done;
1106  }
1107  vb->state = VIDEOBUF_ACTIVE;
1108  list_del(&vb->queue);
1109  vip->active = vb;
1110  dma = videobuf_to_dma_contig(vb);
1111  switch (vip->format.field) {
1112  case V4L2_FIELD_INTERLACED:
1113  REG_WRITE(vip, DVP_VTP, dma);
1114  REG_WRITE(vip, DVP_VBP, dma + vip->format.width * 2);
1115  break;
1116  case V4L2_FIELD_TOP:
1117  case V4L2_FIELD_BOTTOM:
1118  REG_WRITE(vip, DVP_VTP, dma);
1119  REG_WRITE(vip, DVP_VBP, dma);
1120  break;
1121  default:
1122  pr_warning("VIP: unknown field format\n");
1123  goto done;
1124  break;
1125  }
1126  REG_WRITE(vip, DVP_CTL, REG_READ(vip, DVP_CTL) | DVP_CTL_ENA);
1127  }
1128 done:
1129  spin_unlock_irqrestore(&vip->slock, flags);
1130  return IRQ_HANDLED;
1131 }
1132 
1141 static int vip_gpio_reserve(struct device *dev, int pin, int dir,
1142  const char *name)
1143 {
1144  int ret;
1145 
1146  if (pin == -1)
1147  return 0;
1148 
1149  ret = gpio_request(pin, name);
1150  if (ret) {
1151  dev_err(dev, "Failed to allocate pin %d (%s)\n", pin, name);
1152  return ret;
1153  }
1154 
1155  ret = gpio_direction_output(pin, dir);
1156  if (ret) {
1157  dev_err(dev, "Failed to set direction for pin %d (%s)\n",
1158  pin, name);
1159  gpio_free(pin);
1160  return ret;
1161  }
1162 
1163  ret = gpio_export(pin, false);
1164  if (ret) {
1165  dev_err(dev, "Failed to export pin %d (%s)\n", pin, name);
1166  gpio_free(pin);
1167  return ret;
1168  }
1169 
1170  return 0;
1171 }
1172 
1180 static void vip_gpio_release(struct device *dev, int pin, const char *name)
1181 {
1182  if (pin != -1) {
1183  dev_dbg(dev, "releasing pin %d (%s)\n", pin, name);
1184  gpio_unexport(pin);
1185  gpio_free(pin);
1186  }
1187 }
1188 
1208 static int __devinit sta2x11_vip_init_one(struct pci_dev *pdev,
1209  const struct pci_device_id *ent)
1210 {
1211  int ret;
1212  struct sta2x11_vip *vip;
1213  struct vip_config *config;
1214 
1215  ret = pci_enable_device(pdev);
1216  if (ret)
1217  return ret;
1218 
1219  config = dev_get_platdata(&pdev->dev);
1220  if (!config) {
1221  dev_info(&pdev->dev, "VIP slot disabled\n");
1222  ret = -EINVAL;
1223  goto disable;
1224  }
1225 
1226  ret = vip_gpio_reserve(&pdev->dev, config->pwr_pin, 0,
1227  config->pwr_name);
1228  if (ret)
1229  goto disable;
1230 
1231  if (config->reset_pin >= 0) {
1232  ret = vip_gpio_reserve(&pdev->dev, config->reset_pin, 0,
1233  config->reset_name);
1234  if (ret) {
1235  vip_gpio_release(&pdev->dev, config->pwr_pin,
1236  config->pwr_name);
1237  goto disable;
1238  }
1239  }
1240 
1241  if (config->pwr_pin != -1) {
1242  /* Datasheet says 5ms between PWR and RST */
1243  usleep_range(5000, 25000);
1244  ret = gpio_direction_output(config->pwr_pin, 1);
1245  }
1246 
1247  if (config->reset_pin != -1) {
1248  /* Datasheet says 5ms between PWR and RST */
1249  usleep_range(5000, 25000);
1250  ret = gpio_direction_output(config->reset_pin, 1);
1251  }
1252  usleep_range(5000, 25000);
1253 
1254  vip = kzalloc(sizeof(struct sta2x11_vip), GFP_KERNEL);
1255  if (!vip) {
1256  ret = -ENOMEM;
1257  goto release_gpios;
1258  }
1259 
1260  vip->pdev = pdev;
1261  vip->std = V4L2_STD_PAL;
1262  vip->format = formats_50[0];
1263  vip->config = config;
1264 
1265  if (v4l2_device_register(&pdev->dev, &vip->v4l2_dev))
1266  goto free_mem;
1267 
1268  dev_dbg(&pdev->dev, "BAR #0 at 0x%lx 0x%lx irq %d\n",
1269  (unsigned long)pci_resource_start(pdev, 0),
1270  (unsigned long)pci_resource_len(pdev, 0), pdev->irq);
1271 
1272  pci_set_master(pdev);
1273 
1274  ret = pci_request_regions(pdev, DRV_NAME);
1275  if (ret)
1276  goto unreg;
1277 
1278  vip->iomem = pci_iomap(pdev, 0, 0x100);
1279  if (!vip->iomem) {
1280  ret = -ENOMEM; /* FIXME */
1281  goto release;
1282  }
1283 
1284  pci_enable_msi(pdev);
1285 
1286  INIT_LIST_HEAD(&vip->capture);
1287  spin_lock_init(&vip->slock);
1288  mutex_init(&vip->mutex);
1289  vip->started = 0;
1290  vip->disabled = 0;
1291 
1292  ret = request_irq(pdev->irq,
1293  (irq_handler_t) vip_irq,
1294  IRQF_SHARED, DRV_NAME, vip);
1295  if (ret) {
1296  dev_err(&pdev->dev, "request_irq failed\n");
1297  ret = -ENODEV;
1298  goto unmap;
1299  }
1300 
1301  vip->video_dev = video_device_alloc();
1302  if (!vip->video_dev) {
1303  ret = -ENOMEM;
1304  goto release_irq;
1305  }
1306 
1307  *(vip->video_dev) = video_dev_template;
1308  video_set_drvdata(vip->video_dev, vip);
1309 
1310  ret = video_register_device(vip->video_dev, VFL_TYPE_GRABBER, -1);
1311  if (ret)
1312  goto vrelease;
1313 
1314  vip->adapter = i2c_get_adapter(vip->config->i2c_id);
1315  if (!vip->adapter) {
1316  ret = -ENODEV;
1317  dev_err(&pdev->dev, "no I2C adapter found\n");
1318  goto vunreg;
1319  }
1320 
1321  vip->decoder = v4l2_i2c_new_subdev(&vip->v4l2_dev, vip->adapter,
1322  "adv7180", vip->config->i2c_addr,
1323  NULL);
1324  if (!vip->decoder) {
1325  ret = -ENODEV;
1326  dev_err(&pdev->dev, "no decoder found\n");
1327  goto vunreg;
1328  }
1329 
1330  i2c_put_adapter(vip->adapter);
1331 
1332  v4l2_subdev_call(vip->decoder, core, init, 0);
1333 
1334  pr_info("STA2X11 Video Input Port (VIP) loaded\n");
1335  return 0;
1336 
1337 vunreg:
1338  video_set_drvdata(vip->video_dev, NULL);
1339 vrelease:
1340  if (video_is_registered(vip->video_dev))
1342  else
1344 release_irq:
1345  free_irq(pdev->irq, vip);
1346  pci_disable_msi(pdev);
1347 unmap:
1348  pci_iounmap(pdev, vip->iomem);
1349  mutex_destroy(&vip->mutex);
1350 release:
1351  pci_release_regions(pdev);
1352 unreg:
1354 free_mem:
1355  kfree(vip);
1356 release_gpios:
1357  vip_gpio_release(&pdev->dev, config->reset_pin, config->reset_name);
1358  vip_gpio_release(&pdev->dev, config->pwr_pin, config->pwr_name);
1359 disable:
1360  /*
1361  * do not call pci_disable_device on sta2x11 because it break all
1362  * other Bus masters on this EP
1363  */
1364  return ret;
1365 }
1366 
1379 static void __devexit sta2x11_vip_remove_one(struct pci_dev *pdev)
1380 {
1381  struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1382  struct sta2x11_vip *vip =
1383  container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1384 
1385  video_set_drvdata(vip->video_dev, NULL);
1387  /*do not call video_device_release() here, is already done */
1388  free_irq(pdev->irq, vip);
1389  pci_disable_msi(pdev);
1390  pci_iounmap(pdev, vip->iomem);
1391  pci_release_regions(pdev);
1392 
1394  mutex_destroy(&vip->mutex);
1395 
1396  vip_gpio_release(&pdev->dev, vip->config->pwr_pin,
1397  vip->config->pwr_name);
1398  vip_gpio_release(&pdev->dev, vip->config->reset_pin,
1399  vip->config->reset_name);
1400 
1401  kfree(vip);
1402  /*
1403  * do not call pci_disable_device on sta2x11 because it break all
1404  * other Bus masters on this EP
1405  */
1406 }
1407 
1408 #ifdef CONFIG_PM
1409 
1423 static int sta2x11_vip_suspend(struct pci_dev *pdev, pm_message_t state)
1424 {
1425  struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1426  struct sta2x11_vip *vip =
1427  container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1428  unsigned long flags;
1429  int i;
1430 
1431  spin_lock_irqsave(&vip->slock, flags);
1432  vip->register_save_area[0] = REG_READ(vip, DVP_CTL);
1435  REG_WRITE(vip, DVP_ITM, 0);
1436  for (i = 1; i < SAVE_COUNT; i++)
1437  vip->register_save_area[i] = REG_READ(vip, 4 * i);
1438  for (i = 0; i < AUX_COUNT; i++)
1439  vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i] =
1440  REG_READ(vip, registers_to_save[i]);
1441  spin_unlock_irqrestore(&vip->slock, flags);
1442  /* save pci state */
1443  pci_save_state(pdev);
1444  if (pci_set_power_state(pdev, pci_choose_state(pdev, state))) {
1445  /*
1446  * do not call pci_disable_device on sta2x11 because it
1447  * break all other Bus masters on this EP
1448  */
1449  vip->disabled = 1;
1450  }
1451 
1452  pr_info("VIP: suspend\n");
1453  return 0;
1454 }
1455 
1467 static int sta2x11_vip_resume(struct pci_dev *pdev)
1468 {
1469  struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1470  struct sta2x11_vip *vip =
1471  container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1472  unsigned long flags;
1473  int ret, i;
1474 
1475  pr_info("VIP: resume\n");
1476  /* restore pci state */
1477  if (vip->disabled) {
1478  ret = pci_enable_device(pdev);
1479  if (ret) {
1480  pr_warning("VIP: Can't enable device.\n");
1481  return ret;
1482  }
1483  vip->disabled = 0;
1484  }
1485  ret = pci_set_power_state(pdev, PCI_D0);
1486  if (ret) {
1487  /*
1488  * do not call pci_disable_device on sta2x11 because it
1489  * break all other Bus masters on this EP
1490  */
1491  pr_warning("VIP: Can't enable device.\n");
1492  vip->disabled = 1;
1493  return ret;
1494  }
1495 
1496  pci_restore_state(pdev);
1497 
1498  spin_lock_irqsave(&vip->slock, flags);
1499  for (i = 1; i < SAVE_COUNT; i++)
1500  REG_WRITE(vip, 4 * i, vip->register_save_area[i]);
1501  for (i = 0; i < AUX_COUNT; i++)
1502  REG_WRITE(vip, registers_to_save[i],
1503  vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i]);
1504  REG_WRITE(vip, DVP_CTL, vip->register_save_area[0]);
1505  REG_WRITE(vip, DVP_ITM, vip->register_save_area[SAVE_COUNT]);
1506  spin_unlock_irqrestore(&vip->slock, flags);
1507  return 0;
1508 }
1509 
1510 #endif
1511 
1512 static DEFINE_PCI_DEVICE_TABLE(sta2x11_vip_pci_tbl) = {
1514  {0,}
1515 };
1516 
1517 static struct pci_driver sta2x11_vip_driver = {
1518  .name = DRV_NAME,
1519  .probe = sta2x11_vip_init_one,
1520  .remove = __devexit_p(sta2x11_vip_remove_one),
1521  .id_table = sta2x11_vip_pci_tbl,
1522 #ifdef CONFIG_PM
1523  .suspend = sta2x11_vip_suspend,
1524  .resume = sta2x11_vip_resume,
1525 #endif
1526 };
1527 
1528 static int __init sta2x11_vip_init_module(void)
1529 {
1530  return pci_register_driver(&sta2x11_vip_driver);
1531 }
1532 
1533 static void __exit sta2x11_vip_exit_module(void)
1534 {
1535  pci_unregister_driver(&sta2x11_vip_driver);
1536 }
1537 
1538 #ifdef MODULE
1539 module_init(sta2x11_vip_init_module);
1540 module_exit(sta2x11_vip_exit_module);
1541 #else
1542 late_initcall_sync(sta2x11_vip_init_module);
1543 #endif
1544 
1545 MODULE_DESCRIPTION("STA2X11 Video Input Port driver");
1546 MODULE_AUTHOR("Wind River");
1547 MODULE_LICENSE("GPL v2");
1548 MODULE_SUPPORTED_DEVICE("sta2x11 video input");
1550 MODULE_DEVICE_TABLE(pci, sta2x11_vip_pci_tbl);