1
2
3
4
5
6
7
8
9
10
11
12#include <linux/types.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/videodev2.h>
17#include <linux/kmod.h>
18#include <linux/pci.h>
19#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/gpio.h>
22#include <linux/i2c.h>
23#include <linux/delay.h>
24#include <media/v4l2-common.h>
25#include <media/v4l2-device.h>
26#include <media/v4l2-ctrls.h>
27#include <media/v4l2-ioctl.h>
28#include <media/v4l2-fh.h>
29#include <media/v4l2-event.h>
30#include <media/videobuf2-dma-contig.h>
31
32#include "sta2x11_vip.h"
33
34#define DRV_VERSION "1.3"
35
36#ifndef PCI_DEVICE_ID_STMICRO_VIP
37#define PCI_DEVICE_ID_STMICRO_VIP 0xCC0D
38#endif
39
40#define MAX_FRAMES 4
41
42
43#define DVP_CTL 0x00
44#define DVP_TFO 0x04
45#define DVP_TFS 0x08
46#define DVP_BFO 0x0C
47#define DVP_BFS 0x10
48#define DVP_VTP 0x14
49#define DVP_VBP 0x18
50#define DVP_VMP 0x1C
51#define DVP_ITM 0x98
52#define DVP_ITS 0x9C
53#define DVP_STA 0xA0
54#define DVP_HLFLN 0xA8
55#define DVP_RGB 0xC0
56#define DVP_PKZ 0xF0
57
58
59#define DVP_CTL_ENA 0x00000001
60#define DVP_CTL_RST 0x80000000
61#define DVP_CTL_DIS (~0x00040001)
62
63#define DVP_IT_VSB 0x00000008
64#define DVP_IT_VST 0x00000010
65#define DVP_IT_FIFO 0x00000020
66
67#define DVP_HLFLN_SD 0x00000001
68
69#define SAVE_COUNT 8
70#define AUX_COUNT 3
71#define IRQ_COUNT 1
72
73
74struct vip_buffer {
75 struct vb2_v4l2_buffer vb;
76 struct list_head list;
77 dma_addr_t dma;
78};
79static inline struct vip_buffer *to_vip_buffer(struct vb2_v4l2_buffer *vb2)
80{
81 return container_of(vb2, struct vip_buffer, vb);
82}
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112struct sta2x11_vip {
113 struct v4l2_device v4l2_dev;
114 struct video_device video_dev;
115 struct pci_dev *pdev;
116 struct i2c_adapter *adapter;
117 unsigned int register_save_area[IRQ_COUNT + SAVE_COUNT + AUX_COUNT];
118 struct v4l2_subdev *decoder;
119 struct v4l2_ctrl_handler ctrl_hdl;
120
121
122 struct v4l2_pix_format format;
123 v4l2_std_id std;
124 unsigned int input;
125 int disabled;
126 spinlock_t slock;
127
128 struct vb2_queue vb_vidq;
129 struct list_head buffer_list;
130 unsigned int sequence;
131 struct vip_buffer *active;
132 spinlock_t lock;
133 struct mutex v4l_lock;
134
135
136 int tcount, bcount;
137 int overflow;
138
139 void __iomem *iomem;
140 struct vip_config *config;
141};
142
143static const unsigned int registers_to_save[AUX_COUNT] = {
144 DVP_HLFLN, DVP_RGB, DVP_PKZ
145};
146
147static struct v4l2_pix_format formats_50[] = {
148 {
149 .width = 720,
150 .height = 576,
151 .pixelformat = V4L2_PIX_FMT_UYVY,
152 .field = V4L2_FIELD_INTERLACED,
153 .bytesperline = 720 * 2,
154 .sizeimage = 720 * 2 * 576,
155 .colorspace = V4L2_COLORSPACE_SMPTE170M},
156 {
157 .width = 720,
158 .height = 288,
159 .pixelformat = V4L2_PIX_FMT_UYVY,
160 .field = V4L2_FIELD_TOP,
161 .bytesperline = 720 * 2,
162 .sizeimage = 720 * 2 * 288,
163 .colorspace = V4L2_COLORSPACE_SMPTE170M},
164 {
165 .width = 720,
166 .height = 288,
167 .pixelformat = V4L2_PIX_FMT_UYVY,
168 .field = V4L2_FIELD_BOTTOM,
169 .bytesperline = 720 * 2,
170 .sizeimage = 720 * 2 * 288,
171 .colorspace = V4L2_COLORSPACE_SMPTE170M},
172
173};
174
175static struct v4l2_pix_format formats_60[] = {
176 {
177 .width = 720,
178 .height = 480,
179 .pixelformat = V4L2_PIX_FMT_UYVY,
180 .field = V4L2_FIELD_INTERLACED,
181 .bytesperline = 720 * 2,
182 .sizeimage = 720 * 2 * 480,
183 .colorspace = V4L2_COLORSPACE_SMPTE170M},
184 {
185 .width = 720,
186 .height = 240,
187 .pixelformat = V4L2_PIX_FMT_UYVY,
188 .field = V4L2_FIELD_TOP,
189 .bytesperline = 720 * 2,
190 .sizeimage = 720 * 2 * 240,
191 .colorspace = V4L2_COLORSPACE_SMPTE170M},
192 {
193 .width = 720,
194 .height = 240,
195 .pixelformat = V4L2_PIX_FMT_UYVY,
196 .field = V4L2_FIELD_BOTTOM,
197 .bytesperline = 720 * 2,
198 .sizeimage = 720 * 2 * 240,
199 .colorspace = V4L2_COLORSPACE_SMPTE170M},
200};
201
202
203static inline void reg_write(struct sta2x11_vip *vip, unsigned int reg, u32 val)
204{
205 iowrite32((val), (vip->iomem)+(reg));
206}
207
208static inline u32 reg_read(struct sta2x11_vip *vip, unsigned int reg)
209{
210 return ioread32((vip->iomem)+(reg));
211}
212
213static void start_dma(struct sta2x11_vip *vip, struct vip_buffer *vip_buf)
214{
215 unsigned long offset = 0;
216
217 if (vip->format.field == V4L2_FIELD_INTERLACED)
218 offset = vip->format.width * 2;
219
220 spin_lock_irq(&vip->slock);
221
222 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) | DVP_CTL_ENA);
223
224 reg_write(vip, DVP_VTP, (u32)vip_buf->dma);
225 reg_write(vip, DVP_VBP, (u32)vip_buf->dma + offset);
226 spin_unlock_irq(&vip->slock);
227}
228
229
230static void vip_active_buf_next(struct sta2x11_vip *vip)
231{
232
233 spin_lock(&vip->lock);
234 if (list_empty(&vip->buffer_list)) {
235 spin_unlock(&vip->lock);
236 return;
237 }
238 vip->active = list_first_entry(&vip->buffer_list,
239 struct vip_buffer,
240 list);
241
242 vip->tcount = 0;
243 vip->bcount = 0;
244 spin_unlock(&vip->lock);
245 if (vb2_is_streaming(&vip->vb_vidq)) {
246 start_dma(vip, vip->active);
247 }
248}
249
250
251
252static int queue_setup(struct vb2_queue *vq,
253 unsigned int *nbuffers, unsigned int *nplanes,
254 unsigned int sizes[], struct device *alloc_devs[])
255{
256 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
257
258 if (!(*nbuffers) || *nbuffers < MAX_FRAMES)
259 *nbuffers = MAX_FRAMES;
260
261 *nplanes = 1;
262 sizes[0] = vip->format.sizeimage;
263
264 vip->sequence = 0;
265 vip->active = NULL;
266 vip->tcount = 0;
267 vip->bcount = 0;
268
269 return 0;
270};
271static int buffer_init(struct vb2_buffer *vb)
272{
273 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
274 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
275
276 vip_buf->dma = vb2_dma_contig_plane_dma_addr(vb, 0);
277 INIT_LIST_HEAD(&vip_buf->list);
278 return 0;
279}
280
281static int buffer_prepare(struct vb2_buffer *vb)
282{
283 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
284 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
285 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
286 unsigned long size;
287
288 size = vip->format.sizeimage;
289 if (vb2_plane_size(vb, 0) < size) {
290 v4l2_err(&vip->v4l2_dev, "buffer too small (%lu < %lu)\n",
291 vb2_plane_size(vb, 0), size);
292 return -EINVAL;
293 }
294
295 vb2_set_plane_payload(&vip_buf->vb.vb2_buf, 0, size);
296
297 return 0;
298}
299static void buffer_queue(struct vb2_buffer *vb)
300{
301 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
302 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
303 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
304
305 spin_lock(&vip->lock);
306 list_add_tail(&vip_buf->list, &vip->buffer_list);
307 if (!vip->active) {
308 vip->active = list_first_entry(&vip->buffer_list,
309 struct vip_buffer,
310 list);
311 if (vb2_is_streaming(&vip->vb_vidq))
312 start_dma(vip, vip_buf);
313 }
314 spin_unlock(&vip->lock);
315}
316static void buffer_finish(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
323 spin_lock(&vip->lock);
324 list_del_init(&vip_buf->list);
325 spin_unlock(&vip->lock);
326
327 if (vb2_is_streaming(vb->vb2_queue))
328 vip_active_buf_next(vip);
329}
330
331static int start_streaming(struct vb2_queue *vq, unsigned int count)
332{
333 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
334
335 spin_lock_irq(&vip->slock);
336
337 reg_write(vip, DVP_ITM, DVP_IT_VSB | DVP_IT_VST);
338 spin_unlock_irq(&vip->slock);
339
340 if (count)
341 start_dma(vip, vip->active);
342
343 return 0;
344}
345
346
347static void stop_streaming(struct vb2_queue *vq)
348{
349 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
350 struct vip_buffer *vip_buf, *node;
351
352
353 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
354
355 reg_write(vip, DVP_ITM, 0);
356
357
358 spin_lock(&vip->lock);
359 list_for_each_entry_safe(vip_buf, node, &vip->buffer_list, list) {
360 vb2_buffer_done(&vip_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
361 list_del(&vip_buf->list);
362 }
363 spin_unlock(&vip->lock);
364}
365
366static const struct vb2_ops vip_video_qops = {
367 .queue_setup = queue_setup,
368 .buf_init = buffer_init,
369 .buf_prepare = buffer_prepare,
370 .buf_finish = buffer_finish,
371 .buf_queue = buffer_queue,
372 .start_streaming = start_streaming,
373 .stop_streaming = stop_streaming,
374 .wait_prepare = vb2_ops_wait_prepare,
375 .wait_finish = vb2_ops_wait_finish,
376};
377
378
379
380static const struct v4l2_file_operations vip_fops = {
381 .owner = THIS_MODULE,
382 .open = v4l2_fh_open,
383 .release = vb2_fop_release,
384 .unlocked_ioctl = video_ioctl2,
385 .read = vb2_fop_read,
386 .mmap = vb2_fop_mmap,
387 .poll = vb2_fop_poll
388};
389
390
391
392
393
394
395
396
397
398
399
400
401static int vidioc_querycap(struct file *file, void *priv,
402 struct v4l2_capability *cap)
403{
404 struct sta2x11_vip *vip = video_drvdata(file);
405
406 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
407 strscpy(cap->card, KBUILD_MODNAME, sizeof(cap->card));
408 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
409 pci_name(vip->pdev));
410 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
411 V4L2_CAP_STREAMING;
412 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
413
414 return 0;
415}
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id std)
432{
433 struct sta2x11_vip *vip = video_drvdata(file);
434
435
436
437
438
439 if (std == V4L2_STD_ALL) {
440 v4l2_subdev_call(vip->decoder, video, querystd, &std);
441 if (std == V4L2_STD_UNKNOWN)
442 return -EIO;
443 }
444
445 if (vip->std != std) {
446 vip->std = std;
447 if (V4L2_STD_525_60 & std)
448 vip->format = formats_60[0];
449 else
450 vip->format = formats_50[0];
451 }
452
453 return v4l2_subdev_call(vip->decoder, video, s_std, std);
454}
455
456
457
458
459
460
461
462
463
464
465
466static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
467{
468 struct sta2x11_vip *vip = video_drvdata(file);
469
470 *std = vip->std;
471 return 0;
472}
473
474
475
476
477
478
479
480
481
482
483
484static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
485{
486 struct sta2x11_vip *vip = video_drvdata(file);
487
488 return v4l2_subdev_call(vip->decoder, video, querystd, std);
489}
490
491static int vidioc_enum_input(struct file *file, void *priv,
492 struct v4l2_input *inp)
493{
494 if (inp->index > 1)
495 return -EINVAL;
496
497 inp->type = V4L2_INPUT_TYPE_CAMERA;
498 inp->std = V4L2_STD_ALL;
499 sprintf(inp->name, "Camera %u", inp->index);
500
501 return 0;
502}
503
504
505
506
507
508
509
510
511
512
513
514
515
516static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
517{
518 struct sta2x11_vip *vip = video_drvdata(file);
519 int ret;
520
521 if (i > 1)
522 return -EINVAL;
523 ret = v4l2_subdev_call(vip->decoder, video, s_routing, i, 0, 0);
524
525 if (!ret)
526 vip->input = i;
527
528 return 0;
529}
530
531
532
533
534
535
536
537
538
539
540
541static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
542{
543 struct sta2x11_vip *vip = video_drvdata(file);
544
545 *i = vip->input;
546 return 0;
547}
548
549
550
551
552
553
554
555
556
557
558
559
560static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
561 struct v4l2_fmtdesc *f)
562{
563
564 if (f->index != 0)
565 return -EINVAL;
566
567 strscpy(f->description, "4:2:2, packed, UYVY", sizeof(f->description));
568 f->pixelformat = V4L2_PIX_FMT_UYVY;
569 f->flags = 0;
570 return 0;
571}
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
590 struct v4l2_format *f)
591{
592 struct sta2x11_vip *vip = video_drvdata(file);
593 int interlace_lim;
594
595 if (V4L2_PIX_FMT_UYVY != f->fmt.pix.pixelformat) {
596 v4l2_warn(&vip->v4l2_dev, "Invalid format, only UYVY supported\n");
597 return -EINVAL;
598 }
599
600 if (V4L2_STD_525_60 & vip->std)
601 interlace_lim = 240;
602 else
603 interlace_lim = 288;
604
605 switch (f->fmt.pix.field) {
606 default:
607 case V4L2_FIELD_ANY:
608 if (interlace_lim < f->fmt.pix.height)
609 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
610 else
611 f->fmt.pix.field = V4L2_FIELD_BOTTOM;
612 break;
613 case V4L2_FIELD_TOP:
614 case V4L2_FIELD_BOTTOM:
615 if (interlace_lim < f->fmt.pix.height)
616 f->fmt.pix.height = interlace_lim;
617 break;
618 case V4L2_FIELD_INTERLACED:
619 break;
620 }
621
622
623 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
624 f->fmt.pix.height &= ~1;
625 if (2 * interlace_lim < f->fmt.pix.height)
626 f->fmt.pix.height = 2 * interlace_lim;
627 if (200 > f->fmt.pix.height)
628 f->fmt.pix.height = 200;
629 f->fmt.pix.width = 720;
630 f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
631 f->fmt.pix.sizeimage = f->fmt.pix.width * 2 * f->fmt.pix.height;
632 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
633 return 0;
634}
635
636
637
638
639
640
641
642
643
644
645
646
647static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
648 struct v4l2_format *f)
649{
650 struct sta2x11_vip *vip = video_drvdata(file);
651 unsigned int t_stop, b_stop, pitch;
652 int ret;
653
654 ret = vidioc_try_fmt_vid_cap(file, priv, f);
655 if (ret)
656 return ret;
657
658 if (vb2_is_busy(&vip->vb_vidq)) {
659
660 v4l2_err(&vip->v4l2_dev, "device busy\n");
661 return -EBUSY;
662 }
663 vip->format = f->fmt.pix;
664 switch (vip->format.field) {
665 case V4L2_FIELD_INTERLACED:
666 t_stop = ((vip->format.height / 2 - 1) << 16) |
667 (2 * vip->format.width - 1);
668 b_stop = t_stop;
669 pitch = 4 * vip->format.width;
670 break;
671 case V4L2_FIELD_TOP:
672 t_stop = ((vip->format.height - 1) << 16) |
673 (2 * vip->format.width - 1);
674 b_stop = (0 << 16) | (2 * vip->format.width - 1);
675 pitch = 2 * vip->format.width;
676 break;
677 case V4L2_FIELD_BOTTOM:
678 t_stop = (0 << 16) | (2 * vip->format.width - 1);
679 b_stop = (vip->format.height << 16) |
680 (2 * vip->format.width - 1);
681 pitch = 2 * vip->format.width;
682 break;
683 default:
684 v4l2_err(&vip->v4l2_dev, "unknown field format\n");
685 return -EINVAL;
686 }
687
688 spin_lock_irq(&vip->slock);
689
690 reg_write(vip, DVP_TFO, 0);
691
692 reg_write(vip, DVP_BFO, 0);
693
694 reg_write(vip, DVP_TFS, t_stop);
695
696 reg_write(vip, DVP_BFS, b_stop);
697
698 reg_write(vip, DVP_VMP, pitch);
699 spin_unlock_irq(&vip->slock);
700
701 return 0;
702}
703
704
705
706
707
708
709
710
711
712
713
714static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
715 struct v4l2_format *f)
716{
717 struct sta2x11_vip *vip = video_drvdata(file);
718
719 f->fmt.pix = vip->format;
720
721 return 0;
722}
723
724static const struct v4l2_ioctl_ops vip_ioctl_ops = {
725 .vidioc_querycap = vidioc_querycap,
726
727 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
728 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
729 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
730 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
731
732 .vidioc_create_bufs = vb2_ioctl_create_bufs,
733 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
734 .vidioc_reqbufs = vb2_ioctl_reqbufs,
735 .vidioc_querybuf = vb2_ioctl_querybuf,
736 .vidioc_qbuf = vb2_ioctl_qbuf,
737 .vidioc_dqbuf = vb2_ioctl_dqbuf,
738
739 .vidioc_streamon = vb2_ioctl_streamon,
740 .vidioc_streamoff = vb2_ioctl_streamoff,
741
742 .vidioc_g_std = vidioc_g_std,
743 .vidioc_s_std = vidioc_s_std,
744 .vidioc_querystd = vidioc_querystd,
745
746 .vidioc_enum_input = vidioc_enum_input,
747 .vidioc_g_input = vidioc_g_input,
748 .vidioc_s_input = vidioc_s_input,
749
750 .vidioc_log_status = v4l2_ctrl_log_status,
751
752 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
753 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
754};
755
756static const struct video_device video_dev_template = {
757 .name = KBUILD_MODNAME,
758 .release = video_device_release_empty,
759 .fops = &vip_fops,
760 .ioctl_ops = &vip_ioctl_ops,
761 .tvnorms = V4L2_STD_ALL,
762};
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777static irqreturn_t vip_irq(int irq, struct sta2x11_vip *vip)
778{
779 unsigned int status;
780
781 status = reg_read(vip, DVP_ITS);
782
783 if (!status)
784 return IRQ_NONE;
785
786 if (status & DVP_IT_FIFO)
787 if (vip->overflow++ > 5)
788 pr_info("VIP: fifo overflow\n");
789
790 if ((status & DVP_IT_VST) && (status & DVP_IT_VSB)) {
791
792
793 return IRQ_HANDLED;
794 }
795
796 if (status & DVP_IT_VST)
797 if ((++vip->tcount) < 2)
798 return IRQ_HANDLED;
799 if (status & DVP_IT_VSB) {
800 vip->bcount++;
801 return IRQ_HANDLED;
802 }
803
804 if (vip->active) {
805
806 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
807
808 vip->active->vb.vb2_buf.timestamp = ktime_get_ns();
809 vip->active->vb.sequence = vip->sequence++;
810 vb2_buffer_done(&vip->active->vb.vb2_buf, VB2_BUF_STATE_DONE);
811 }
812
813 return IRQ_HANDLED;
814}
815
816static void sta2x11_vip_init_register(struct sta2x11_vip *vip)
817{
818
819 spin_lock_irq(&vip->slock);
820
821 reg_read(vip, DVP_ITS);
822
823 reg_write(vip, DVP_HLFLN, DVP_HLFLN_SD);
824
825 reg_write(vip, DVP_CTL, DVP_CTL_RST);
826
827 reg_write(vip, DVP_CTL, 0);
828 spin_unlock_irq(&vip->slock);
829}
830static void sta2x11_vip_clear_register(struct sta2x11_vip *vip)
831{
832 spin_lock_irq(&vip->slock);
833
834 reg_write(vip, DVP_ITM, 0);
835
836 reg_write(vip, DVP_CTL, DVP_CTL_RST);
837
838 reg_write(vip, DVP_CTL, 0);
839
840 reg_read(vip, DVP_ITS);
841 spin_unlock_irq(&vip->slock);
842}
843static int sta2x11_vip_init_buffer(struct sta2x11_vip *vip)
844{
845 int err;
846
847 err = dma_set_coherent_mask(&vip->pdev->dev, DMA_BIT_MASK(29));
848 if (err) {
849 v4l2_err(&vip->v4l2_dev, "Cannot configure coherent mask");
850 return err;
851 }
852 memset(&vip->vb_vidq, 0, sizeof(struct vb2_queue));
853 vip->vb_vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
854 vip->vb_vidq.io_modes = VB2_MMAP | VB2_READ;
855 vip->vb_vidq.drv_priv = vip;
856 vip->vb_vidq.buf_struct_size = sizeof(struct vip_buffer);
857 vip->vb_vidq.ops = &vip_video_qops;
858 vip->vb_vidq.mem_ops = &vb2_dma_contig_memops;
859 vip->vb_vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
860 vip->vb_vidq.dev = &vip->pdev->dev;
861 vip->vb_vidq.lock = &vip->v4l_lock;
862 err = vb2_queue_init(&vip->vb_vidq);
863 if (err)
864 return err;
865 INIT_LIST_HEAD(&vip->buffer_list);
866 spin_lock_init(&vip->lock);
867 return 0;
868}
869
870static int sta2x11_vip_init_controls(struct sta2x11_vip *vip)
871{
872
873
874
875
876 v4l2_ctrl_handler_init(&vip->ctrl_hdl, 0);
877
878 vip->v4l2_dev.ctrl_handler = &vip->ctrl_hdl;
879 if (vip->ctrl_hdl.error) {
880 int err = vip->ctrl_hdl.error;
881
882 v4l2_ctrl_handler_free(&vip->ctrl_hdl);
883 return err;
884 }
885
886 return 0;
887}
888
889
890
891
892
893
894
895
896
897static int vip_gpio_reserve(struct device *dev, int pin, int dir,
898 const char *name)
899{
900 int ret = -ENODEV;
901
902 if (!gpio_is_valid(pin))
903 return ret;
904
905 ret = gpio_request(pin, name);
906 if (ret) {
907 dev_err(dev, "Failed to allocate pin %d (%s)\n", pin, name);
908 return ret;
909 }
910
911 ret = gpio_direction_output(pin, dir);
912 if (ret) {
913 dev_err(dev, "Failed to set direction for pin %d (%s)\n",
914 pin, name);
915 gpio_free(pin);
916 return ret;
917 }
918
919 ret = gpio_export(pin, false);
920 if (ret) {
921 dev_err(dev, "Failed to export pin %d (%s)\n", pin, name);
922 gpio_free(pin);
923 return ret;
924 }
925
926 return 0;
927}
928
929
930
931
932
933
934
935
936static void vip_gpio_release(struct device *dev, int pin, const char *name)
937{
938 if (gpio_is_valid(pin)) {
939 dev_dbg(dev, "releasing pin %d (%s)\n", pin, name);
940 gpio_unexport(pin);
941 gpio_free(pin);
942 }
943}
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964static int sta2x11_vip_init_one(struct pci_dev *pdev,
965 const struct pci_device_id *ent)
966{
967 int ret;
968 struct sta2x11_vip *vip;
969 struct vip_config *config;
970
971
972 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(26))) {
973 dev_err(&pdev->dev, "26-bit DMA addressing not available\n");
974 return -EINVAL;
975 }
976
977 ret = pci_enable_device(pdev);
978 if (ret)
979 return ret;
980
981
982 config = dev_get_platdata(&pdev->dev);
983 if (!config) {
984 dev_info(&pdev->dev, "VIP slot disabled\n");
985 ret = -EINVAL;
986 goto disable;
987 }
988
989
990 ret = vip_gpio_reserve(&pdev->dev, config->pwr_pin, 0,
991 config->pwr_name);
992 if (ret)
993 goto disable;
994
995 ret = vip_gpio_reserve(&pdev->dev, config->reset_pin, 0,
996 config->reset_name);
997 if (ret) {
998 vip_gpio_release(&pdev->dev, config->pwr_pin,
999 config->pwr_name);
1000 goto disable;
1001 }
1002
1003 if (gpio_is_valid(config->pwr_pin)) {
1004
1005 usleep_range(5000, 25000);
1006 gpio_direction_output(config->pwr_pin, 1);
1007 }
1008
1009 if (gpio_is_valid(config->reset_pin)) {
1010
1011 usleep_range(5000, 25000);
1012 gpio_direction_output(config->reset_pin, 1);
1013 }
1014 usleep_range(5000, 25000);
1015
1016
1017 vip = kzalloc(sizeof(struct sta2x11_vip), GFP_KERNEL);
1018 if (!vip) {
1019 ret = -ENOMEM;
1020 goto release_gpios;
1021 }
1022 vip->pdev = pdev;
1023 vip->std = V4L2_STD_PAL;
1024 vip->format = formats_50[0];
1025 vip->config = config;
1026 mutex_init(&vip->v4l_lock);
1027
1028 ret = sta2x11_vip_init_controls(vip);
1029 if (ret)
1030 goto free_mem;
1031 ret = v4l2_device_register(&pdev->dev, &vip->v4l2_dev);
1032 if (ret)
1033 goto free_mem;
1034
1035 dev_dbg(&pdev->dev, "BAR #0 at 0x%lx 0x%lx irq %d\n",
1036 (unsigned long)pci_resource_start(pdev, 0),
1037 (unsigned long)pci_resource_len(pdev, 0), pdev->irq);
1038
1039 pci_set_master(pdev);
1040
1041 ret = pci_request_regions(pdev, KBUILD_MODNAME);
1042 if (ret)
1043 goto unreg;
1044
1045 vip->iomem = pci_iomap(pdev, 0, 0x100);
1046 if (!vip->iomem) {
1047 ret = -ENOMEM;
1048 goto release;
1049 }
1050
1051 pci_enable_msi(pdev);
1052
1053
1054 ret = sta2x11_vip_init_buffer(vip);
1055 if (ret)
1056 goto unmap;
1057
1058 spin_lock_init(&vip->slock);
1059
1060 ret = request_irq(pdev->irq,
1061 (irq_handler_t) vip_irq,
1062 IRQF_SHARED, KBUILD_MODNAME, vip);
1063 if (ret) {
1064 dev_err(&pdev->dev, "request_irq failed\n");
1065 ret = -ENODEV;
1066 goto release_buf;
1067 }
1068
1069
1070 vip->video_dev = video_dev_template;
1071 vip->video_dev.v4l2_dev = &vip->v4l2_dev;
1072 vip->video_dev.queue = &vip->vb_vidq;
1073 vip->video_dev.lock = &vip->v4l_lock;
1074 video_set_drvdata(&vip->video_dev, vip);
1075
1076 ret = video_register_device(&vip->video_dev, VFL_TYPE_GRABBER, -1);
1077 if (ret)
1078 goto vrelease;
1079
1080
1081 vip->adapter = i2c_get_adapter(vip->config->i2c_id);
1082 if (!vip->adapter) {
1083 ret = -ENODEV;
1084 dev_err(&pdev->dev, "no I2C adapter found\n");
1085 goto vunreg;
1086 }
1087
1088 vip->decoder = v4l2_i2c_new_subdev(&vip->v4l2_dev, vip->adapter,
1089 "adv7180", vip->config->i2c_addr,
1090 NULL);
1091 if (!vip->decoder) {
1092 ret = -ENODEV;
1093 dev_err(&pdev->dev, "no decoder found\n");
1094 goto vunreg;
1095 }
1096
1097 i2c_put_adapter(vip->adapter);
1098 v4l2_subdev_call(vip->decoder, core, init, 0);
1099
1100 sta2x11_vip_init_register(vip);
1101
1102 dev_info(&pdev->dev, "STA2X11 Video Input Port (VIP) loaded\n");
1103 return 0;
1104
1105vunreg:
1106 video_set_drvdata(&vip->video_dev, NULL);
1107vrelease:
1108 video_unregister_device(&vip->video_dev);
1109 free_irq(pdev->irq, vip);
1110release_buf:
1111 pci_disable_msi(pdev);
1112unmap:
1113 vb2_queue_release(&vip->vb_vidq);
1114 pci_iounmap(pdev, vip->iomem);
1115release:
1116 pci_release_regions(pdev);
1117unreg:
1118 v4l2_device_unregister(&vip->v4l2_dev);
1119free_mem:
1120 kfree(vip);
1121release_gpios:
1122 vip_gpio_release(&pdev->dev, config->reset_pin, config->reset_name);
1123 vip_gpio_release(&pdev->dev, config->pwr_pin, config->pwr_name);
1124disable:
1125
1126
1127
1128
1129 return ret;
1130}
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144static void sta2x11_vip_remove_one(struct pci_dev *pdev)
1145{
1146 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1147 struct sta2x11_vip *vip =
1148 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1149
1150 sta2x11_vip_clear_register(vip);
1151
1152 video_set_drvdata(&vip->video_dev, NULL);
1153 video_unregister_device(&vip->video_dev);
1154 free_irq(pdev->irq, vip);
1155 pci_disable_msi(pdev);
1156 vb2_queue_release(&vip->vb_vidq);
1157 pci_iounmap(pdev, vip->iomem);
1158 pci_release_regions(pdev);
1159
1160 v4l2_device_unregister(&vip->v4l2_dev);
1161
1162 vip_gpio_release(&pdev->dev, vip->config->pwr_pin,
1163 vip->config->pwr_name);
1164 vip_gpio_release(&pdev->dev, vip->config->reset_pin,
1165 vip->config->reset_name);
1166
1167 kfree(vip);
1168
1169
1170
1171
1172}
1173
1174#ifdef CONFIG_PM
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186static int sta2x11_vip_suspend(struct pci_dev *pdev, pm_message_t state)
1187{
1188 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1189 struct sta2x11_vip *vip =
1190 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1191 unsigned long flags;
1192 int i;
1193
1194 spin_lock_irqsave(&vip->slock, flags);
1195 vip->register_save_area[0] = reg_read(vip, DVP_CTL);
1196 reg_write(vip, DVP_CTL, vip->register_save_area[0] & DVP_CTL_DIS);
1197 vip->register_save_area[SAVE_COUNT] = reg_read(vip, DVP_ITM);
1198 reg_write(vip, DVP_ITM, 0);
1199 for (i = 1; i < SAVE_COUNT; i++)
1200 vip->register_save_area[i] = reg_read(vip, 4 * i);
1201 for (i = 0; i < AUX_COUNT; i++)
1202 vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i] =
1203 reg_read(vip, registers_to_save[i]);
1204 spin_unlock_irqrestore(&vip->slock, flags);
1205
1206 pci_save_state(pdev);
1207 if (pci_set_power_state(pdev, pci_choose_state(pdev, state))) {
1208
1209
1210
1211
1212 vip->disabled = 1;
1213 }
1214
1215 pr_info("VIP: suspend\n");
1216 return 0;
1217}
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230static int sta2x11_vip_resume(struct pci_dev *pdev)
1231{
1232 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1233 struct sta2x11_vip *vip =
1234 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1235 unsigned long flags;
1236 int ret, i;
1237
1238 pr_info("VIP: resume\n");
1239
1240 if (vip->disabled) {
1241 ret = pci_enable_device(pdev);
1242 if (ret) {
1243 pr_warn("VIP: Can't enable device.\n");
1244 return ret;
1245 }
1246 vip->disabled = 0;
1247 }
1248 ret = pci_set_power_state(pdev, PCI_D0);
1249 if (ret) {
1250
1251
1252
1253
1254 pr_warn("VIP: Can't enable device.\n");
1255 vip->disabled = 1;
1256 return ret;
1257 }
1258
1259 pci_restore_state(pdev);
1260
1261 spin_lock_irqsave(&vip->slock, flags);
1262 for (i = 1; i < SAVE_COUNT; i++)
1263 reg_write(vip, 4 * i, vip->register_save_area[i]);
1264 for (i = 0; i < AUX_COUNT; i++)
1265 reg_write(vip, registers_to_save[i],
1266 vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i]);
1267 reg_write(vip, DVP_CTL, vip->register_save_area[0]);
1268 reg_write(vip, DVP_ITM, vip->register_save_area[SAVE_COUNT]);
1269 spin_unlock_irqrestore(&vip->slock, flags);
1270 return 0;
1271}
1272
1273#endif
1274
1275static const struct pci_device_id sta2x11_vip_pci_tbl[] = {
1276 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_VIP)},
1277 {0,}
1278};
1279
1280static struct pci_driver sta2x11_vip_driver = {
1281 .name = KBUILD_MODNAME,
1282 .probe = sta2x11_vip_init_one,
1283 .remove = sta2x11_vip_remove_one,
1284 .id_table = sta2x11_vip_pci_tbl,
1285#ifdef CONFIG_PM
1286 .suspend = sta2x11_vip_suspend,
1287 .resume = sta2x11_vip_resume,
1288#endif
1289};
1290
1291static int __init sta2x11_vip_init_module(void)
1292{
1293 return pci_register_driver(&sta2x11_vip_driver);
1294}
1295
1296static void __exit sta2x11_vip_exit_module(void)
1297{
1298 pci_unregister_driver(&sta2x11_vip_driver);
1299}
1300
1301#ifdef MODULE
1302module_init(sta2x11_vip_init_module);
1303module_exit(sta2x11_vip_exit_module);
1304#else
1305late_initcall_sync(sta2x11_vip_init_module);
1306#endif
1307
1308MODULE_DESCRIPTION("STA2X11 Video Input Port driver");
1309MODULE_AUTHOR("Wind River");
1310MODULE_LICENSE("GPL v2");
1311MODULE_SUPPORTED_DEVICE("sta2x11 video input");
1312MODULE_VERSION(DRV_VERSION);
1313MODULE_DEVICE_TABLE(pci, sta2x11_vip_pci_tbl);
1314