0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/types.h>
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/init.h>
0016 #include <linux/videodev2.h>
0017 #include <linux/kmod.h>
0018 #include <linux/pci.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/io.h>
0021 #include <linux/gpio.h>
0022 #include <linux/i2c.h>
0023 #include <linux/delay.h>
0024 #include <media/v4l2-common.h>
0025 #include <media/v4l2-device.h>
0026 #include <media/v4l2-ctrls.h>
0027 #include <media/v4l2-ioctl.h>
0028 #include <media/v4l2-fh.h>
0029 #include <media/v4l2-event.h>
0030 #include <media/videobuf2-dma-contig.h>
0031
0032 #include "sta2x11_vip.h"
0033
0034 #define DRV_VERSION "1.3"
0035
0036 #ifndef PCI_DEVICE_ID_STMICRO_VIP
0037 #define PCI_DEVICE_ID_STMICRO_VIP 0xCC0D
0038 #endif
0039
0040 #define MAX_FRAMES 4
0041
0042
0043 #define DVP_CTL 0x00
0044 #define DVP_TFO 0x04
0045 #define DVP_TFS 0x08
0046 #define DVP_BFO 0x0C
0047 #define DVP_BFS 0x10
0048 #define DVP_VTP 0x14
0049 #define DVP_VBP 0x18
0050 #define DVP_VMP 0x1C
0051 #define DVP_ITM 0x98
0052 #define DVP_ITS 0x9C
0053 #define DVP_STA 0xA0
0054 #define DVP_HLFLN 0xA8
0055 #define DVP_RGB 0xC0
0056 #define DVP_PKZ 0xF0
0057
0058
0059 #define DVP_CTL_ENA 0x00000001
0060 #define DVP_CTL_RST 0x80000000
0061 #define DVP_CTL_DIS (~0x00040001)
0062
0063 #define DVP_IT_VSB 0x00000008
0064 #define DVP_IT_VST 0x00000010
0065 #define DVP_IT_FIFO 0x00000020
0066
0067 #define DVP_HLFLN_SD 0x00000001
0068
0069 #define SAVE_COUNT 8
0070 #define AUX_COUNT 3
0071 #define IRQ_COUNT 1
0072
0073
0074 struct vip_buffer {
0075 struct vb2_v4l2_buffer vb;
0076 struct list_head list;
0077 dma_addr_t dma;
0078 };
0079 static inline struct vip_buffer *to_vip_buffer(struct vb2_v4l2_buffer *vb2)
0080 {
0081 return container_of(vb2, struct vip_buffer, vb);
0082 }
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112 struct sta2x11_vip {
0113 struct v4l2_device v4l2_dev;
0114 struct video_device video_dev;
0115 struct pci_dev *pdev;
0116 struct i2c_adapter *adapter;
0117 unsigned int register_save_area[IRQ_COUNT + SAVE_COUNT + AUX_COUNT];
0118 struct v4l2_subdev *decoder;
0119 struct v4l2_ctrl_handler ctrl_hdl;
0120
0121
0122 struct v4l2_pix_format format;
0123 v4l2_std_id std;
0124 unsigned int input;
0125 int disabled;
0126 spinlock_t slock;
0127
0128 struct vb2_queue vb_vidq;
0129 struct list_head buffer_list;
0130 unsigned int sequence;
0131 struct vip_buffer *active;
0132 spinlock_t lock;
0133 struct mutex v4l_lock;
0134
0135
0136 int tcount, bcount;
0137 int overflow;
0138
0139 void __iomem *iomem;
0140 struct vip_config *config;
0141 };
0142
0143 static const unsigned int registers_to_save[AUX_COUNT] = {
0144 DVP_HLFLN, DVP_RGB, DVP_PKZ
0145 };
0146
0147 static struct v4l2_pix_format formats_50[] = {
0148 {
0149 .width = 720,
0150 .height = 576,
0151 .pixelformat = V4L2_PIX_FMT_UYVY,
0152 .field = V4L2_FIELD_INTERLACED,
0153 .bytesperline = 720 * 2,
0154 .sizeimage = 720 * 2 * 576,
0155 .colorspace = V4L2_COLORSPACE_SMPTE170M},
0156 {
0157 .width = 720,
0158 .height = 288,
0159 .pixelformat = V4L2_PIX_FMT_UYVY,
0160 .field = V4L2_FIELD_TOP,
0161 .bytesperline = 720 * 2,
0162 .sizeimage = 720 * 2 * 288,
0163 .colorspace = V4L2_COLORSPACE_SMPTE170M},
0164 {
0165 .width = 720,
0166 .height = 288,
0167 .pixelformat = V4L2_PIX_FMT_UYVY,
0168 .field = V4L2_FIELD_BOTTOM,
0169 .bytesperline = 720 * 2,
0170 .sizeimage = 720 * 2 * 288,
0171 .colorspace = V4L2_COLORSPACE_SMPTE170M},
0172
0173 };
0174
0175 static struct v4l2_pix_format formats_60[] = {
0176 {
0177 .width = 720,
0178 .height = 480,
0179 .pixelformat = V4L2_PIX_FMT_UYVY,
0180 .field = V4L2_FIELD_INTERLACED,
0181 .bytesperline = 720 * 2,
0182 .sizeimage = 720 * 2 * 480,
0183 .colorspace = V4L2_COLORSPACE_SMPTE170M},
0184 {
0185 .width = 720,
0186 .height = 240,
0187 .pixelformat = V4L2_PIX_FMT_UYVY,
0188 .field = V4L2_FIELD_TOP,
0189 .bytesperline = 720 * 2,
0190 .sizeimage = 720 * 2 * 240,
0191 .colorspace = V4L2_COLORSPACE_SMPTE170M},
0192 {
0193 .width = 720,
0194 .height = 240,
0195 .pixelformat = V4L2_PIX_FMT_UYVY,
0196 .field = V4L2_FIELD_BOTTOM,
0197 .bytesperline = 720 * 2,
0198 .sizeimage = 720 * 2 * 240,
0199 .colorspace = V4L2_COLORSPACE_SMPTE170M},
0200 };
0201
0202
0203 static inline void reg_write(struct sta2x11_vip *vip, unsigned int reg, u32 val)
0204 {
0205 iowrite32((val), (vip->iomem)+(reg));
0206 }
0207
0208 static inline u32 reg_read(struct sta2x11_vip *vip, unsigned int reg)
0209 {
0210 return ioread32((vip->iomem)+(reg));
0211 }
0212
0213 static void start_dma(struct sta2x11_vip *vip, struct vip_buffer *vip_buf)
0214 {
0215 unsigned long offset = 0;
0216
0217 if (vip->format.field == V4L2_FIELD_INTERLACED)
0218 offset = vip->format.width * 2;
0219
0220 spin_lock_irq(&vip->slock);
0221
0222 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) | DVP_CTL_ENA);
0223
0224 reg_write(vip, DVP_VTP, (u32)vip_buf->dma);
0225 reg_write(vip, DVP_VBP, (u32)vip_buf->dma + offset);
0226 spin_unlock_irq(&vip->slock);
0227 }
0228
0229
0230 static void vip_active_buf_next(struct sta2x11_vip *vip)
0231 {
0232
0233 spin_lock(&vip->lock);
0234 if (list_empty(&vip->buffer_list)) {
0235 spin_unlock(&vip->lock);
0236 return;
0237 }
0238 vip->active = list_first_entry(&vip->buffer_list,
0239 struct vip_buffer,
0240 list);
0241
0242 vip->tcount = 0;
0243 vip->bcount = 0;
0244 spin_unlock(&vip->lock);
0245 if (vb2_is_streaming(&vip->vb_vidq)) {
0246 start_dma(vip, vip->active);
0247 }
0248 }
0249
0250
0251
0252 static int queue_setup(struct vb2_queue *vq,
0253 unsigned int *nbuffers, unsigned int *nplanes,
0254 unsigned int sizes[], struct device *alloc_devs[])
0255 {
0256 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
0257
0258 if (!(*nbuffers) || *nbuffers < MAX_FRAMES)
0259 *nbuffers = MAX_FRAMES;
0260
0261 *nplanes = 1;
0262 sizes[0] = vip->format.sizeimage;
0263
0264 vip->sequence = 0;
0265 vip->active = NULL;
0266 vip->tcount = 0;
0267 vip->bcount = 0;
0268
0269 return 0;
0270 };
0271 static int buffer_init(struct vb2_buffer *vb)
0272 {
0273 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0274 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
0275
0276 vip_buf->dma = vb2_dma_contig_plane_dma_addr(vb, 0);
0277 INIT_LIST_HEAD(&vip_buf->list);
0278 return 0;
0279 }
0280
0281 static int buffer_prepare(struct vb2_buffer *vb)
0282 {
0283 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0284 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
0285 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
0286 unsigned long size;
0287
0288 size = vip->format.sizeimage;
0289 if (vb2_plane_size(vb, 0) < size) {
0290 v4l2_err(&vip->v4l2_dev, "buffer too small (%lu < %lu)\n",
0291 vb2_plane_size(vb, 0), size);
0292 return -EINVAL;
0293 }
0294
0295 vb2_set_plane_payload(&vip_buf->vb.vb2_buf, 0, size);
0296
0297 return 0;
0298 }
0299 static void buffer_queue(struct vb2_buffer *vb)
0300 {
0301 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0302 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
0303 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
0304
0305 spin_lock(&vip->lock);
0306 list_add_tail(&vip_buf->list, &vip->buffer_list);
0307 if (!vip->active) {
0308 vip->active = list_first_entry(&vip->buffer_list,
0309 struct vip_buffer,
0310 list);
0311 if (vb2_is_streaming(&vip->vb_vidq))
0312 start_dma(vip, vip_buf);
0313 }
0314 spin_unlock(&vip->lock);
0315 }
0316 static void buffer_finish(struct vb2_buffer *vb)
0317 {
0318 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0319 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
0320 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
0321
0322
0323 spin_lock(&vip->lock);
0324 list_del_init(&vip_buf->list);
0325 spin_unlock(&vip->lock);
0326
0327 if (vb2_is_streaming(vb->vb2_queue))
0328 vip_active_buf_next(vip);
0329 }
0330
0331 static int start_streaming(struct vb2_queue *vq, unsigned int count)
0332 {
0333 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
0334
0335 spin_lock_irq(&vip->slock);
0336
0337 reg_write(vip, DVP_ITM, DVP_IT_VSB | DVP_IT_VST);
0338 spin_unlock_irq(&vip->slock);
0339
0340 if (count)
0341 start_dma(vip, vip->active);
0342
0343 return 0;
0344 }
0345
0346
0347 static void stop_streaming(struct vb2_queue *vq)
0348 {
0349 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
0350 struct vip_buffer *vip_buf, *node;
0351
0352
0353 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
0354
0355 reg_write(vip, DVP_ITM, 0);
0356
0357
0358 spin_lock(&vip->lock);
0359 list_for_each_entry_safe(vip_buf, node, &vip->buffer_list, list) {
0360 vb2_buffer_done(&vip_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
0361 list_del(&vip_buf->list);
0362 }
0363 spin_unlock(&vip->lock);
0364 }
0365
0366 static const struct vb2_ops vip_video_qops = {
0367 .queue_setup = queue_setup,
0368 .buf_init = buffer_init,
0369 .buf_prepare = buffer_prepare,
0370 .buf_finish = buffer_finish,
0371 .buf_queue = buffer_queue,
0372 .start_streaming = start_streaming,
0373 .stop_streaming = stop_streaming,
0374 .wait_prepare = vb2_ops_wait_prepare,
0375 .wait_finish = vb2_ops_wait_finish,
0376 };
0377
0378
0379
0380 static const struct v4l2_file_operations vip_fops = {
0381 .owner = THIS_MODULE,
0382 .open = v4l2_fh_open,
0383 .release = vb2_fop_release,
0384 .unlocked_ioctl = video_ioctl2,
0385 .read = vb2_fop_read,
0386 .mmap = vb2_fop_mmap,
0387 .poll = vb2_fop_poll
0388 };
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401 static int vidioc_querycap(struct file *file, void *priv,
0402 struct v4l2_capability *cap)
0403 {
0404 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
0405 strscpy(cap->card, KBUILD_MODNAME, sizeof(cap->card));
0406 return 0;
0407 }
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id std)
0424 {
0425 struct sta2x11_vip *vip = video_drvdata(file);
0426
0427
0428
0429
0430
0431 if (std == V4L2_STD_ALL) {
0432 v4l2_subdev_call(vip->decoder, video, querystd, &std);
0433 if (std == V4L2_STD_UNKNOWN)
0434 return -EIO;
0435 }
0436
0437 if (vip->std != std) {
0438 vip->std = std;
0439 if (V4L2_STD_525_60 & std)
0440 vip->format = formats_60[0];
0441 else
0442 vip->format = formats_50[0];
0443 }
0444
0445 return v4l2_subdev_call(vip->decoder, video, s_std, std);
0446 }
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
0459 {
0460 struct sta2x11_vip *vip = video_drvdata(file);
0461
0462 *std = vip->std;
0463 return 0;
0464 }
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
0477 {
0478 struct sta2x11_vip *vip = video_drvdata(file);
0479
0480 return v4l2_subdev_call(vip->decoder, video, querystd, std);
0481 }
0482
0483 static int vidioc_enum_input(struct file *file, void *priv,
0484 struct v4l2_input *inp)
0485 {
0486 if (inp->index > 1)
0487 return -EINVAL;
0488
0489 inp->type = V4L2_INPUT_TYPE_CAMERA;
0490 inp->std = V4L2_STD_ALL;
0491 sprintf(inp->name, "Camera %u", inp->index);
0492
0493 return 0;
0494 }
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
0509 {
0510 struct sta2x11_vip *vip = video_drvdata(file);
0511 int ret;
0512
0513 if (i > 1)
0514 return -EINVAL;
0515 ret = v4l2_subdev_call(vip->decoder, video, s_routing, i, 0, 0);
0516
0517 if (!ret)
0518 vip->input = i;
0519
0520 return 0;
0521 }
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
0534 {
0535 struct sta2x11_vip *vip = video_drvdata(file);
0536
0537 *i = vip->input;
0538 return 0;
0539 }
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
0553 struct v4l2_fmtdesc *f)
0554 {
0555
0556 if (f->index != 0)
0557 return -EINVAL;
0558
0559 f->pixelformat = V4L2_PIX_FMT_UYVY;
0560 return 0;
0561 }
0562
0563
0564
0565
0566
0567
0568
0569
0570
0571
0572
0573
0574
0575
0576
0577
0578
0579 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
0580 struct v4l2_format *f)
0581 {
0582 struct sta2x11_vip *vip = video_drvdata(file);
0583 int interlace_lim;
0584
0585 if (V4L2_PIX_FMT_UYVY != f->fmt.pix.pixelformat) {
0586 v4l2_warn(&vip->v4l2_dev, "Invalid format, only UYVY supported\n");
0587 return -EINVAL;
0588 }
0589
0590 if (V4L2_STD_525_60 & vip->std)
0591 interlace_lim = 240;
0592 else
0593 interlace_lim = 288;
0594
0595 switch (f->fmt.pix.field) {
0596 default:
0597 case V4L2_FIELD_ANY:
0598 if (interlace_lim < f->fmt.pix.height)
0599 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
0600 else
0601 f->fmt.pix.field = V4L2_FIELD_BOTTOM;
0602 break;
0603 case V4L2_FIELD_TOP:
0604 case V4L2_FIELD_BOTTOM:
0605 if (interlace_lim < f->fmt.pix.height)
0606 f->fmt.pix.height = interlace_lim;
0607 break;
0608 case V4L2_FIELD_INTERLACED:
0609 break;
0610 }
0611
0612
0613 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
0614 f->fmt.pix.height &= ~1;
0615 if (2 * interlace_lim < f->fmt.pix.height)
0616 f->fmt.pix.height = 2 * interlace_lim;
0617 if (200 > f->fmt.pix.height)
0618 f->fmt.pix.height = 200;
0619 f->fmt.pix.width = 720;
0620 f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
0621 f->fmt.pix.sizeimage = f->fmt.pix.width * 2 * f->fmt.pix.height;
0622 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
0623 return 0;
0624 }
0625
0626
0627
0628
0629
0630
0631
0632
0633
0634
0635
0636
0637 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
0638 struct v4l2_format *f)
0639 {
0640 struct sta2x11_vip *vip = video_drvdata(file);
0641 unsigned int t_stop, b_stop, pitch;
0642 int ret;
0643
0644 ret = vidioc_try_fmt_vid_cap(file, priv, f);
0645 if (ret)
0646 return ret;
0647
0648 if (vb2_is_busy(&vip->vb_vidq)) {
0649
0650 v4l2_err(&vip->v4l2_dev, "device busy\n");
0651 return -EBUSY;
0652 }
0653 vip->format = f->fmt.pix;
0654 switch (vip->format.field) {
0655 case V4L2_FIELD_INTERLACED:
0656 t_stop = ((vip->format.height / 2 - 1) << 16) |
0657 (2 * vip->format.width - 1);
0658 b_stop = t_stop;
0659 pitch = 4 * vip->format.width;
0660 break;
0661 case V4L2_FIELD_TOP:
0662 t_stop = ((vip->format.height - 1) << 16) |
0663 (2 * vip->format.width - 1);
0664 b_stop = (0 << 16) | (2 * vip->format.width - 1);
0665 pitch = 2 * vip->format.width;
0666 break;
0667 case V4L2_FIELD_BOTTOM:
0668 t_stop = (0 << 16) | (2 * vip->format.width - 1);
0669 b_stop = (vip->format.height << 16) |
0670 (2 * vip->format.width - 1);
0671 pitch = 2 * vip->format.width;
0672 break;
0673 default:
0674 v4l2_err(&vip->v4l2_dev, "unknown field format\n");
0675 return -EINVAL;
0676 }
0677
0678 spin_lock_irq(&vip->slock);
0679
0680 reg_write(vip, DVP_TFO, 0);
0681
0682 reg_write(vip, DVP_BFO, 0);
0683
0684 reg_write(vip, DVP_TFS, t_stop);
0685
0686 reg_write(vip, DVP_BFS, b_stop);
0687
0688 reg_write(vip, DVP_VMP, pitch);
0689 spin_unlock_irq(&vip->slock);
0690
0691 return 0;
0692 }
0693
0694
0695
0696
0697
0698
0699
0700
0701
0702
0703
0704 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
0705 struct v4l2_format *f)
0706 {
0707 struct sta2x11_vip *vip = video_drvdata(file);
0708
0709 f->fmt.pix = vip->format;
0710
0711 return 0;
0712 }
0713
0714 static const struct v4l2_ioctl_ops vip_ioctl_ops = {
0715 .vidioc_querycap = vidioc_querycap,
0716
0717 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
0718 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
0719 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
0720 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
0721
0722 .vidioc_create_bufs = vb2_ioctl_create_bufs,
0723 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
0724 .vidioc_reqbufs = vb2_ioctl_reqbufs,
0725 .vidioc_querybuf = vb2_ioctl_querybuf,
0726 .vidioc_qbuf = vb2_ioctl_qbuf,
0727 .vidioc_dqbuf = vb2_ioctl_dqbuf,
0728
0729 .vidioc_streamon = vb2_ioctl_streamon,
0730 .vidioc_streamoff = vb2_ioctl_streamoff,
0731
0732 .vidioc_g_std = vidioc_g_std,
0733 .vidioc_s_std = vidioc_s_std,
0734 .vidioc_querystd = vidioc_querystd,
0735
0736 .vidioc_enum_input = vidioc_enum_input,
0737 .vidioc_g_input = vidioc_g_input,
0738 .vidioc_s_input = vidioc_s_input,
0739
0740 .vidioc_log_status = v4l2_ctrl_log_status,
0741
0742 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
0743 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
0744 };
0745
0746 static const struct video_device video_dev_template = {
0747 .name = KBUILD_MODNAME,
0748 .release = video_device_release_empty,
0749 .fops = &vip_fops,
0750 .ioctl_ops = &vip_ioctl_ops,
0751 .tvnorms = V4L2_STD_ALL,
0752 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
0753 V4L2_CAP_STREAMING,
0754 };
0755
0756
0757
0758
0759
0760
0761
0762
0763
0764
0765
0766
0767
0768
0769 static irqreturn_t vip_irq(int irq, struct sta2x11_vip *vip)
0770 {
0771 unsigned int status;
0772
0773 status = reg_read(vip, DVP_ITS);
0774
0775 if (!status)
0776 return IRQ_NONE;
0777
0778 if (status & DVP_IT_FIFO)
0779 if (vip->overflow++ > 5)
0780 pr_info("VIP: fifo overflow\n");
0781
0782 if ((status & DVP_IT_VST) && (status & DVP_IT_VSB)) {
0783
0784
0785 return IRQ_HANDLED;
0786 }
0787
0788 if (status & DVP_IT_VST)
0789 if ((++vip->tcount) < 2)
0790 return IRQ_HANDLED;
0791 if (status & DVP_IT_VSB) {
0792 vip->bcount++;
0793 return IRQ_HANDLED;
0794 }
0795
0796 if (vip->active) {
0797
0798 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
0799
0800 vip->active->vb.vb2_buf.timestamp = ktime_get_ns();
0801 vip->active->vb.sequence = vip->sequence++;
0802 vb2_buffer_done(&vip->active->vb.vb2_buf, VB2_BUF_STATE_DONE);
0803 }
0804
0805 return IRQ_HANDLED;
0806 }
0807
0808 static void sta2x11_vip_init_register(struct sta2x11_vip *vip)
0809 {
0810
0811 spin_lock_irq(&vip->slock);
0812
0813 reg_read(vip, DVP_ITS);
0814
0815 reg_write(vip, DVP_HLFLN, DVP_HLFLN_SD);
0816
0817 reg_write(vip, DVP_CTL, DVP_CTL_RST);
0818
0819 reg_write(vip, DVP_CTL, 0);
0820 spin_unlock_irq(&vip->slock);
0821 }
0822 static void sta2x11_vip_clear_register(struct sta2x11_vip *vip)
0823 {
0824 spin_lock_irq(&vip->slock);
0825
0826 reg_write(vip, DVP_ITM, 0);
0827
0828 reg_write(vip, DVP_CTL, DVP_CTL_RST);
0829
0830 reg_write(vip, DVP_CTL, 0);
0831
0832 reg_read(vip, DVP_ITS);
0833 spin_unlock_irq(&vip->slock);
0834 }
0835 static int sta2x11_vip_init_buffer(struct sta2x11_vip *vip)
0836 {
0837 int err;
0838
0839 err = dma_set_coherent_mask(&vip->pdev->dev, DMA_BIT_MASK(29));
0840 if (err) {
0841 v4l2_err(&vip->v4l2_dev, "Cannot configure coherent mask");
0842 return err;
0843 }
0844 memset(&vip->vb_vidq, 0, sizeof(struct vb2_queue));
0845 vip->vb_vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
0846 vip->vb_vidq.io_modes = VB2_MMAP | VB2_READ;
0847 vip->vb_vidq.drv_priv = vip;
0848 vip->vb_vidq.buf_struct_size = sizeof(struct vip_buffer);
0849 vip->vb_vidq.ops = &vip_video_qops;
0850 vip->vb_vidq.mem_ops = &vb2_dma_contig_memops;
0851 vip->vb_vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
0852 vip->vb_vidq.dev = &vip->pdev->dev;
0853 vip->vb_vidq.lock = &vip->v4l_lock;
0854 err = vb2_queue_init(&vip->vb_vidq);
0855 if (err)
0856 return err;
0857 INIT_LIST_HEAD(&vip->buffer_list);
0858 spin_lock_init(&vip->lock);
0859 return 0;
0860 }
0861
0862 static int sta2x11_vip_init_controls(struct sta2x11_vip *vip)
0863 {
0864
0865
0866
0867
0868 v4l2_ctrl_handler_init(&vip->ctrl_hdl, 0);
0869
0870 vip->v4l2_dev.ctrl_handler = &vip->ctrl_hdl;
0871 if (vip->ctrl_hdl.error) {
0872 int err = vip->ctrl_hdl.error;
0873
0874 v4l2_ctrl_handler_free(&vip->ctrl_hdl);
0875 return err;
0876 }
0877
0878 return 0;
0879 }
0880
0881
0882
0883
0884
0885
0886
0887
0888
0889 static int vip_gpio_reserve(struct device *dev, int pin, int dir,
0890 const char *name)
0891 {
0892 int ret = -ENODEV;
0893
0894 if (!gpio_is_valid(pin))
0895 return ret;
0896
0897 ret = gpio_request(pin, name);
0898 if (ret) {
0899 dev_err(dev, "Failed to allocate pin %d (%s)\n", pin, name);
0900 return ret;
0901 }
0902
0903 ret = gpio_direction_output(pin, dir);
0904 if (ret) {
0905 dev_err(dev, "Failed to set direction for pin %d (%s)\n",
0906 pin, name);
0907 gpio_free(pin);
0908 return ret;
0909 }
0910
0911 ret = gpio_export(pin, false);
0912 if (ret) {
0913 dev_err(dev, "Failed to export pin %d (%s)\n", pin, name);
0914 gpio_free(pin);
0915 return ret;
0916 }
0917
0918 return 0;
0919 }
0920
0921
0922
0923
0924
0925
0926
0927
0928 static void vip_gpio_release(struct device *dev, int pin, const char *name)
0929 {
0930 if (gpio_is_valid(pin)) {
0931 dev_dbg(dev, "releasing pin %d (%s)\n", pin, name);
0932 gpio_unexport(pin);
0933 gpio_free(pin);
0934 }
0935 }
0936
0937
0938
0939
0940
0941
0942
0943
0944
0945
0946
0947
0948
0949
0950
0951
0952
0953
0954
0955
0956 static int sta2x11_vip_init_one(struct pci_dev *pdev,
0957 const struct pci_device_id *ent)
0958 {
0959 int ret;
0960 struct sta2x11_vip *vip;
0961 struct vip_config *config;
0962
0963
0964 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(26))) {
0965 dev_err(&pdev->dev, "26-bit DMA addressing not available\n");
0966 return -EINVAL;
0967 }
0968
0969 ret = pci_enable_device(pdev);
0970 if (ret)
0971 return ret;
0972
0973
0974 config = dev_get_platdata(&pdev->dev);
0975 if (!config) {
0976 dev_info(&pdev->dev, "VIP slot disabled\n");
0977 ret = -EINVAL;
0978 goto disable;
0979 }
0980
0981
0982 ret = vip_gpio_reserve(&pdev->dev, config->pwr_pin, 0,
0983 config->pwr_name);
0984 if (ret)
0985 goto disable;
0986
0987 ret = vip_gpio_reserve(&pdev->dev, config->reset_pin, 0,
0988 config->reset_name);
0989 if (ret) {
0990 vip_gpio_release(&pdev->dev, config->pwr_pin,
0991 config->pwr_name);
0992 goto disable;
0993 }
0994
0995 if (gpio_is_valid(config->pwr_pin)) {
0996
0997 usleep_range(5000, 25000);
0998 gpio_direction_output(config->pwr_pin, 1);
0999 }
1000
1001 if (gpio_is_valid(config->reset_pin)) {
1002
1003 usleep_range(5000, 25000);
1004 gpio_direction_output(config->reset_pin, 1);
1005 }
1006 usleep_range(5000, 25000);
1007
1008
1009 vip = kzalloc(sizeof(struct sta2x11_vip), GFP_KERNEL);
1010 if (!vip) {
1011 ret = -ENOMEM;
1012 goto release_gpios;
1013 }
1014 vip->pdev = pdev;
1015 vip->std = V4L2_STD_PAL;
1016 vip->format = formats_50[0];
1017 vip->config = config;
1018 mutex_init(&vip->v4l_lock);
1019
1020 ret = sta2x11_vip_init_controls(vip);
1021 if (ret)
1022 goto free_mem;
1023 ret = v4l2_device_register(&pdev->dev, &vip->v4l2_dev);
1024 if (ret)
1025 goto free_mem;
1026
1027 dev_dbg(&pdev->dev, "BAR #0 at 0x%lx 0x%lx irq %d\n",
1028 (unsigned long)pci_resource_start(pdev, 0),
1029 (unsigned long)pci_resource_len(pdev, 0), pdev->irq);
1030
1031 pci_set_master(pdev);
1032
1033 ret = pci_request_regions(pdev, KBUILD_MODNAME);
1034 if (ret)
1035 goto unreg;
1036
1037 vip->iomem = pci_iomap(pdev, 0, 0x100);
1038 if (!vip->iomem) {
1039 ret = -ENOMEM;
1040 goto release;
1041 }
1042
1043 pci_enable_msi(pdev);
1044
1045
1046 ret = sta2x11_vip_init_buffer(vip);
1047 if (ret)
1048 goto unmap;
1049
1050 spin_lock_init(&vip->slock);
1051
1052 ret = request_irq(pdev->irq,
1053 (irq_handler_t) vip_irq,
1054 IRQF_SHARED, KBUILD_MODNAME, vip);
1055 if (ret) {
1056 dev_err(&pdev->dev, "request_irq failed\n");
1057 ret = -ENODEV;
1058 goto release_buf;
1059 }
1060
1061
1062 vip->video_dev = video_dev_template;
1063 vip->video_dev.v4l2_dev = &vip->v4l2_dev;
1064 vip->video_dev.queue = &vip->vb_vidq;
1065 vip->video_dev.lock = &vip->v4l_lock;
1066 video_set_drvdata(&vip->video_dev, vip);
1067
1068 ret = video_register_device(&vip->video_dev, VFL_TYPE_VIDEO, -1);
1069 if (ret)
1070 goto vrelease;
1071
1072
1073 vip->adapter = i2c_get_adapter(vip->config->i2c_id);
1074 if (!vip->adapter) {
1075 ret = -ENODEV;
1076 dev_err(&pdev->dev, "no I2C adapter found\n");
1077 goto vunreg;
1078 }
1079
1080 vip->decoder = v4l2_i2c_new_subdev(&vip->v4l2_dev, vip->adapter,
1081 "adv7180", vip->config->i2c_addr,
1082 NULL);
1083 if (!vip->decoder) {
1084 ret = -ENODEV;
1085 dev_err(&pdev->dev, "no decoder found\n");
1086 goto vunreg;
1087 }
1088
1089 i2c_put_adapter(vip->adapter);
1090 v4l2_subdev_call(vip->decoder, core, init, 0);
1091
1092 sta2x11_vip_init_register(vip);
1093
1094 dev_info(&pdev->dev, "STA2X11 Video Input Port (VIP) loaded\n");
1095 return 0;
1096
1097 vunreg:
1098 video_set_drvdata(&vip->video_dev, NULL);
1099 vrelease:
1100 vb2_video_unregister_device(&vip->video_dev);
1101 free_irq(pdev->irq, vip);
1102 release_buf:
1103 pci_disable_msi(pdev);
1104 unmap:
1105 pci_iounmap(pdev, vip->iomem);
1106 release:
1107 pci_release_regions(pdev);
1108 unreg:
1109 v4l2_device_unregister(&vip->v4l2_dev);
1110 free_mem:
1111 kfree(vip);
1112 release_gpios:
1113 vip_gpio_release(&pdev->dev, config->reset_pin, config->reset_name);
1114 vip_gpio_release(&pdev->dev, config->pwr_pin, config->pwr_name);
1115 disable:
1116
1117
1118
1119
1120 return ret;
1121 }
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135 static void sta2x11_vip_remove_one(struct pci_dev *pdev)
1136 {
1137 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1138 struct sta2x11_vip *vip =
1139 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1140
1141 sta2x11_vip_clear_register(vip);
1142
1143 video_set_drvdata(&vip->video_dev, NULL);
1144 vb2_video_unregister_device(&vip->video_dev);
1145 free_irq(pdev->irq, vip);
1146 pci_disable_msi(pdev);
1147 pci_iounmap(pdev, vip->iomem);
1148 pci_release_regions(pdev);
1149
1150 v4l2_device_unregister(&vip->v4l2_dev);
1151
1152 vip_gpio_release(&pdev->dev, vip->config->pwr_pin,
1153 vip->config->pwr_name);
1154 vip_gpio_release(&pdev->dev, vip->config->reset_pin,
1155 vip->config->reset_name);
1156
1157 kfree(vip);
1158
1159
1160
1161
1162 }
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173 static int __maybe_unused sta2x11_vip_suspend(struct device *dev_d)
1174 {
1175 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev_d);
1176 struct sta2x11_vip *vip =
1177 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1178 unsigned long flags;
1179 int i;
1180
1181 spin_lock_irqsave(&vip->slock, flags);
1182 vip->register_save_area[0] = reg_read(vip, DVP_CTL);
1183 reg_write(vip, DVP_CTL, vip->register_save_area[0] & DVP_CTL_DIS);
1184 vip->register_save_area[SAVE_COUNT] = reg_read(vip, DVP_ITM);
1185 reg_write(vip, DVP_ITM, 0);
1186 for (i = 1; i < SAVE_COUNT; i++)
1187 vip->register_save_area[i] = reg_read(vip, 4 * i);
1188 for (i = 0; i < AUX_COUNT; i++)
1189 vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i] =
1190 reg_read(vip, registers_to_save[i]);
1191 spin_unlock_irqrestore(&vip->slock, flags);
1192
1193 vip->disabled = 1;
1194
1195 pr_info("VIP: suspend\n");
1196 return 0;
1197 }
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207 static int __maybe_unused sta2x11_vip_resume(struct device *dev_d)
1208 {
1209 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev_d);
1210 struct sta2x11_vip *vip =
1211 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1212 unsigned long flags;
1213 int i;
1214
1215 pr_info("VIP: resume\n");
1216
1217 vip->disabled = 0;
1218
1219 spin_lock_irqsave(&vip->slock, flags);
1220 for (i = 1; i < SAVE_COUNT; i++)
1221 reg_write(vip, 4 * i, vip->register_save_area[i]);
1222 for (i = 0; i < AUX_COUNT; i++)
1223 reg_write(vip, registers_to_save[i],
1224 vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i]);
1225 reg_write(vip, DVP_CTL, vip->register_save_area[0]);
1226 reg_write(vip, DVP_ITM, vip->register_save_area[SAVE_COUNT]);
1227 spin_unlock_irqrestore(&vip->slock, flags);
1228 return 0;
1229 }
1230
1231 static const struct pci_device_id sta2x11_vip_pci_tbl[] = {
1232 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_VIP)},
1233 {0,}
1234 };
1235
1236 static SIMPLE_DEV_PM_OPS(sta2x11_vip_pm_ops,
1237 sta2x11_vip_suspend,
1238 sta2x11_vip_resume);
1239
1240 static struct pci_driver sta2x11_vip_driver = {
1241 .name = KBUILD_MODNAME,
1242 .probe = sta2x11_vip_init_one,
1243 .remove = sta2x11_vip_remove_one,
1244 .id_table = sta2x11_vip_pci_tbl,
1245 .driver.pm = &sta2x11_vip_pm_ops,
1246 };
1247
1248 static int __init sta2x11_vip_init_module(void)
1249 {
1250 return pci_register_driver(&sta2x11_vip_driver);
1251 }
1252
1253 static void __exit sta2x11_vip_exit_module(void)
1254 {
1255 pci_unregister_driver(&sta2x11_vip_driver);
1256 }
1257
1258 #ifdef MODULE
1259 module_init(sta2x11_vip_init_module);
1260 module_exit(sta2x11_vip_exit_module);
1261 #else
1262 late_initcall_sync(sta2x11_vip_init_module);
1263 #endif
1264
1265 MODULE_DESCRIPTION("STA2X11 Video Input Port driver");
1266 MODULE_AUTHOR("Wind River");
1267 MODULE_LICENSE("GPL v2");
1268 MODULE_VERSION(DRV_VERSION);
1269 MODULE_DEVICE_TABLE(pci, sta2x11_vip_pci_tbl);