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