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