1 // SPDX-License-Identifier: GPL-2.0-only
3 * This is the driver for the STA2x11 Video Input Port.
5 * Copyright (C) 2012 ST Microelectronics
6 * author: Federico Vaga <federico.vaga@gmail.com>
7 * Copyright (C) 2010 WindRiver Systems, Inc.
8 * authors: Andreas Kies <andreas.kies@windriver.com>
9 * Vlad Lungu <vlad.lungu@windriver.com>
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>
21 #include <linux/gpio/consumer.h>
22 #include <linux/gpio.h>
23 #include <linux/i2c.h>
24 #include <linux/delay.h>
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/v4l2-fh.h>
30 #include <media/v4l2-event.h>
31 #include <media/videobuf2-dma-contig.h>
33 #include "sta2x11_vip.h"
35 #define DRV_VERSION "1.3"
37 #ifndef PCI_DEVICE_ID_STMICRO_VIP
38 #define PCI_DEVICE_ID_STMICRO_VIP 0xCC0D
55 #define DVP_HLFLN 0xA8
60 #define DVP_CTL_ENA 0x00000001
61 #define DVP_CTL_RST 0x80000000
62 #define DVP_CTL_DIS (~0x00040001)
64 #define DVP_IT_VSB 0x00000008
65 #define DVP_IT_VST 0x00000010
66 #define DVP_IT_FIFO 0x00000020
68 #define DVP_HLFLN_SD 0x00000001
76 struct vb2_v4l2_buffer vb
;
77 struct list_head list
;
80 static inline struct vip_buffer
*to_vip_buffer(struct vb2_v4l2_buffer
*vb2
)
82 return container_of(vb2
, struct vip_buffer
, vb
);
86 * struct sta2x11_vip - All internal data for one instance of device
87 * @v4l2_dev: device registered in v4l layer
88 * @video_dev: properties of our device
90 * @adapter: contains I2C adapter information
91 * @register_save_area: All relevant register are saved here during suspend
92 * @decoder: contains information about video DAC
93 * @ctrl_hdl: handler for control framework
94 * @format: pixel format, fixed UYVY
95 * @std: video standard (e.g. PAL/NTSC)
96 * @input: input line for video signal ( 0 or 1 )
97 * @disabled: Device is in power down state
98 * @slock: for excluse access of registers
99 * @vb_vidq: queue maintained by videobuf2 layer
100 * @buffer_list: list of buffer in use
101 * @sequence: sequence number of acquired buffer
102 * @active: current active buffer
103 * @lock: used in videobuf2 callback
104 * @v4l_lock: serialize its video4linux ioctls
105 * @tcount: Number of top frames
106 * @bcount: Number of bottom frames
107 * @overflow: Number of FIFO overflows
108 * @iomem: hardware base address
109 * @config: I2C and gpio config from platform
111 * All non-local data is accessed via this structure.
114 struct v4l2_device v4l2_dev
;
115 struct video_device video_dev
;
116 struct pci_dev
*pdev
;
117 struct i2c_adapter
*adapter
;
118 unsigned int register_save_area
[IRQ_COUNT
+ SAVE_COUNT
+ AUX_COUNT
];
119 struct v4l2_subdev
*decoder
;
120 struct v4l2_ctrl_handler ctrl_hdl
;
123 struct v4l2_pix_format format
;
129 struct vb2_queue vb_vidq
;
130 struct list_head buffer_list
;
131 unsigned int sequence
;
132 struct vip_buffer
*active
; /* current active buffer */
133 spinlock_t lock
; /* Used in videobuf2 callback */
134 struct mutex v4l_lock
;
136 /* Interrupt counters */
140 void __iomem
*iomem
; /* I/O Memory */
141 struct vip_config
*config
;
144 static const unsigned int registers_to_save
[AUX_COUNT
] = {
145 DVP_HLFLN
, DVP_RGB
, DVP_PKZ
148 static struct v4l2_pix_format formats_50
[] = {
149 { /*PAL interlaced */
152 .pixelformat
= V4L2_PIX_FMT_UYVY
,
153 .field
= V4L2_FIELD_INTERLACED
,
154 .bytesperline
= 720 * 2,
155 .sizeimage
= 720 * 2 * 576,
156 .colorspace
= V4L2_COLORSPACE_SMPTE170M
},
160 .pixelformat
= V4L2_PIX_FMT_UYVY
,
161 .field
= V4L2_FIELD_TOP
,
162 .bytesperline
= 720 * 2,
163 .sizeimage
= 720 * 2 * 288,
164 .colorspace
= V4L2_COLORSPACE_SMPTE170M
},
168 .pixelformat
= V4L2_PIX_FMT_UYVY
,
169 .field
= V4L2_FIELD_BOTTOM
,
170 .bytesperline
= 720 * 2,
171 .sizeimage
= 720 * 2 * 288,
172 .colorspace
= V4L2_COLORSPACE_SMPTE170M
},
176 static struct v4l2_pix_format formats_60
[] = {
177 { /*NTSC interlaced */
180 .pixelformat
= V4L2_PIX_FMT_UYVY
,
181 .field
= V4L2_FIELD_INTERLACED
,
182 .bytesperline
= 720 * 2,
183 .sizeimage
= 720 * 2 * 480,
184 .colorspace
= V4L2_COLORSPACE_SMPTE170M
},
188 .pixelformat
= V4L2_PIX_FMT_UYVY
,
189 .field
= V4L2_FIELD_TOP
,
190 .bytesperline
= 720 * 2,
191 .sizeimage
= 720 * 2 * 240,
192 .colorspace
= V4L2_COLORSPACE_SMPTE170M
},
196 .pixelformat
= V4L2_PIX_FMT_UYVY
,
197 .field
= V4L2_FIELD_BOTTOM
,
198 .bytesperline
= 720 * 2,
199 .sizeimage
= 720 * 2 * 240,
200 .colorspace
= V4L2_COLORSPACE_SMPTE170M
},
203 /* Write VIP register */
204 static inline void reg_write(struct sta2x11_vip
*vip
, unsigned int reg
, u32 val
)
206 iowrite32((val
), (vip
->iomem
)+(reg
));
208 /* Read VIP register */
209 static inline u32
reg_read(struct sta2x11_vip
*vip
, unsigned int reg
)
211 return ioread32((vip
->iomem
)+(reg
));
213 /* Start DMA acquisition */
214 static void start_dma(struct sta2x11_vip
*vip
, struct vip_buffer
*vip_buf
)
216 unsigned long offset
= 0;
218 if (vip
->format
.field
== V4L2_FIELD_INTERLACED
)
219 offset
= vip
->format
.width
* 2;
221 spin_lock_irq(&vip
->slock
);
222 /* Enable acquisition */
223 reg_write(vip
, DVP_CTL
, reg_read(vip
, DVP_CTL
) | DVP_CTL_ENA
);
224 /* Set Top and Bottom Field memory address */
225 reg_write(vip
, DVP_VTP
, (u32
)vip_buf
->dma
);
226 reg_write(vip
, DVP_VBP
, (u32
)vip_buf
->dma
+ offset
);
227 spin_unlock_irq(&vip
->slock
);
230 /* Fetch the next buffer to activate */
231 static void vip_active_buf_next(struct sta2x11_vip
*vip
)
233 /* Get the next buffer */
234 spin_lock(&vip
->lock
);
235 if (list_empty(&vip
->buffer_list
)) {/* No available buffer */
236 spin_unlock(&vip
->lock
);
239 vip
->active
= list_first_entry(&vip
->buffer_list
,
242 /* Reset Top and Bottom counter */
245 spin_unlock(&vip
->lock
);
246 if (vb2_is_streaming(&vip
->vb_vidq
)) { /* streaming is on */
247 start_dma(vip
, vip
->active
); /* start dma capture */
252 /* Videobuf2 Operations */
253 static int queue_setup(struct vb2_queue
*vq
,
254 unsigned int *nbuffers
, unsigned int *nplanes
,
255 unsigned int sizes
[], struct device
*alloc_devs
[])
257 struct sta2x11_vip
*vip
= vb2_get_drv_priv(vq
);
259 if (!(*nbuffers
) || *nbuffers
< MAX_FRAMES
)
260 *nbuffers
= MAX_FRAMES
;
263 sizes
[0] = vip
->format
.sizeimage
;
272 static int buffer_init(struct vb2_buffer
*vb
)
274 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
275 struct vip_buffer
*vip_buf
= to_vip_buffer(vbuf
);
277 vip_buf
->dma
= vb2_dma_contig_plane_dma_addr(vb
, 0);
278 INIT_LIST_HEAD(&vip_buf
->list
);
282 static int buffer_prepare(struct vb2_buffer
*vb
)
284 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
285 struct sta2x11_vip
*vip
= vb2_get_drv_priv(vb
->vb2_queue
);
286 struct vip_buffer
*vip_buf
= to_vip_buffer(vbuf
);
289 size
= vip
->format
.sizeimage
;
290 if (vb2_plane_size(vb
, 0) < size
) {
291 v4l2_err(&vip
->v4l2_dev
, "buffer too small (%lu < %lu)\n",
292 vb2_plane_size(vb
, 0), size
);
296 vb2_set_plane_payload(&vip_buf
->vb
.vb2_buf
, 0, size
);
300 static void buffer_queue(struct vb2_buffer
*vb
)
302 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
303 struct sta2x11_vip
*vip
= vb2_get_drv_priv(vb
->vb2_queue
);
304 struct vip_buffer
*vip_buf
= to_vip_buffer(vbuf
);
306 spin_lock(&vip
->lock
);
307 list_add_tail(&vip_buf
->list
, &vip
->buffer_list
);
308 if (!vip
->active
) { /* No active buffer, active the first one */
309 vip
->active
= list_first_entry(&vip
->buffer_list
,
312 if (vb2_is_streaming(&vip
->vb_vidq
)) /* streaming is on */
313 start_dma(vip
, vip_buf
); /* start dma capture */
315 spin_unlock(&vip
->lock
);
317 static void buffer_finish(struct vb2_buffer
*vb
)
319 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
320 struct sta2x11_vip
*vip
= vb2_get_drv_priv(vb
->vb2_queue
);
321 struct vip_buffer
*vip_buf
= to_vip_buffer(vbuf
);
323 /* Buffer handled, remove it from the list */
324 spin_lock(&vip
->lock
);
325 list_del_init(&vip_buf
->list
);
326 spin_unlock(&vip
->lock
);
328 if (vb2_is_streaming(vb
->vb2_queue
))
329 vip_active_buf_next(vip
);
332 static int start_streaming(struct vb2_queue
*vq
, unsigned int count
)
334 struct sta2x11_vip
*vip
= vb2_get_drv_priv(vq
);
336 spin_lock_irq(&vip
->slock
);
337 /* Enable interrupt VSYNC Top and Bottom*/
338 reg_write(vip
, DVP_ITM
, DVP_IT_VSB
| DVP_IT_VST
);
339 spin_unlock_irq(&vip
->slock
);
342 start_dma(vip
, vip
->active
);
347 /* abort streaming and wait for last buffer */
348 static void stop_streaming(struct vb2_queue
*vq
)
350 struct sta2x11_vip
*vip
= vb2_get_drv_priv(vq
);
351 struct vip_buffer
*vip_buf
, *node
;
353 /* Disable acquisition */
354 reg_write(vip
, DVP_CTL
, reg_read(vip
, DVP_CTL
) & ~DVP_CTL_ENA
);
355 /* Disable all interrupts */
356 reg_write(vip
, DVP_ITM
, 0);
358 /* Release all active buffers */
359 spin_lock(&vip
->lock
);
360 list_for_each_entry_safe(vip_buf
, node
, &vip
->buffer_list
, list
) {
361 vb2_buffer_done(&vip_buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
362 list_del(&vip_buf
->list
);
364 spin_unlock(&vip
->lock
);
367 static const struct vb2_ops vip_video_qops
= {
368 .queue_setup
= queue_setup
,
369 .buf_init
= buffer_init
,
370 .buf_prepare
= buffer_prepare
,
371 .buf_finish
= buffer_finish
,
372 .buf_queue
= buffer_queue
,
373 .start_streaming
= start_streaming
,
374 .stop_streaming
= stop_streaming
,
378 /* File Operations */
379 static const struct v4l2_file_operations vip_fops
= {
380 .owner
= THIS_MODULE
,
381 .open
= v4l2_fh_open
,
382 .release
= vb2_fop_release
,
383 .unlocked_ioctl
= video_ioctl2
,
384 .read
= vb2_fop_read
,
385 .mmap
= vb2_fop_mmap
,
391 * vidioc_querycap - return capabilities of device
392 * @file: descriptor of device
393 * @cap: contains return values
396 * the capabilities of the device are returned
398 * return value: 0, no error.
400 static int vidioc_querycap(struct file
*file
, void *priv
,
401 struct v4l2_capability
*cap
)
403 strscpy(cap
->driver
, KBUILD_MODNAME
, sizeof(cap
->driver
));
404 strscpy(cap
->card
, KBUILD_MODNAME
, sizeof(cap
->card
));
409 * vidioc_s_std - set video standard
410 * @file: descriptor of device
411 * @std: contains standard to be set
414 * the video standard is set
416 * return value: 0, no error.
418 * -EIO, no input signal detected
420 * other, returned from video DAC.
422 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id std
)
424 struct sta2x11_vip
*vip
= video_drvdata(file
);
427 * This is here for backwards compatibility only.
428 * The use of V4L2_STD_ALL to trigger a querystd is non-standard.
430 if (std
== V4L2_STD_ALL
) {
431 v4l2_subdev_call(vip
->decoder
, video
, querystd
, &std
);
432 if (std
== V4L2_STD_UNKNOWN
)
436 if (vip
->std
!= std
) {
438 if (V4L2_STD_525_60
& std
)
439 vip
->format
= formats_60
[0];
441 vip
->format
= formats_50
[0];
444 return v4l2_subdev_call(vip
->decoder
, video
, s_std
, std
);
448 * vidioc_g_std - get video standard
449 * @file: descriptor of device
451 * @std: contains return values
453 * the current video standard is returned
455 * return value: 0, no error.
457 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
459 struct sta2x11_vip
*vip
= video_drvdata(file
);
466 * vidioc_querystd - get possible video standards
467 * @file: descriptor of device
469 * @std: contains return values
471 * all possible video standards are returned
473 * return value: delivered by video DAC routine.
475 static int vidioc_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std
)
477 struct sta2x11_vip
*vip
= video_drvdata(file
);
479 return v4l2_subdev_call(vip
->decoder
, video
, querystd
, std
);
482 static int vidioc_enum_input(struct file
*file
, void *priv
,
483 struct v4l2_input
*inp
)
488 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
489 inp
->std
= V4L2_STD_ALL
;
490 sprintf(inp
->name
, "Camera %u", inp
->index
);
496 * vidioc_s_input - set input line
497 * @file: descriptor of device
499 * @i: new input line number
501 * the current active input line is set
503 * return value: 0, no error.
505 * -EINVAL, line number out of range
507 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
509 struct sta2x11_vip
*vip
= video_drvdata(file
);
514 ret
= v4l2_subdev_call(vip
->decoder
, video
, s_routing
, i
, 0, 0);
523 * vidioc_g_input - return input line
524 * @file: descriptor of device
526 * @i: returned input line number
528 * the current active input line is returned
530 * return value: always 0.
532 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
534 struct sta2x11_vip
*vip
= video_drvdata(file
);
541 * vidioc_enum_fmt_vid_cap - return video capture format
542 * @file: descriptor of device
544 * @f: returned format information
546 * returns name and format of video capture
547 * Only UYVY is supported by hardware.
549 * return value: always 0.
551 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
552 struct v4l2_fmtdesc
*f
)
558 f
->pixelformat
= V4L2_PIX_FMT_UYVY
;
563 * vidioc_try_fmt_vid_cap - set video capture format
564 * @file: descriptor of device
568 * new video format is set which includes width and
569 * field type. width is fixed to 720, no scaling.
570 * Only UYVY is supported by this hardware.
571 * the minimum height is 200, the maximum is 576 (PAL)
573 * return value: 0, no error
575 * -EINVAL, pixel or field format not supported
578 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
579 struct v4l2_format
*f
)
581 struct sta2x11_vip
*vip
= video_drvdata(file
);
584 if (V4L2_PIX_FMT_UYVY
!= f
->fmt
.pix
.pixelformat
) {
585 v4l2_warn(&vip
->v4l2_dev
, "Invalid format, only UYVY supported\n");
589 if (V4L2_STD_525_60
& vip
->std
)
594 switch (f
->fmt
.pix
.field
) {
597 if (interlace_lim
< f
->fmt
.pix
.height
)
598 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
600 f
->fmt
.pix
.field
= V4L2_FIELD_BOTTOM
;
603 case V4L2_FIELD_BOTTOM
:
604 if (interlace_lim
< f
->fmt
.pix
.height
)
605 f
->fmt
.pix
.height
= interlace_lim
;
607 case V4L2_FIELD_INTERLACED
:
611 /* It is the only supported format */
612 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_UYVY
;
613 f
->fmt
.pix
.height
&= ~1;
614 if (2 * interlace_lim
< f
->fmt
.pix
.height
)
615 f
->fmt
.pix
.height
= 2 * interlace_lim
;
616 if (200 > f
->fmt
.pix
.height
)
617 f
->fmt
.pix
.height
= 200;
618 f
->fmt
.pix
.width
= 720;
619 f
->fmt
.pix
.bytesperline
= f
->fmt
.pix
.width
* 2;
620 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.width
* 2 * f
->fmt
.pix
.height
;
621 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
626 * vidioc_s_fmt_vid_cap - set current video format parameters
627 * @file: descriptor of device
629 * @f: returned format information
631 * set new capture format
632 * return value: 0, no error
634 * other, delivered by video DAC routine.
636 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
637 struct v4l2_format
*f
)
639 struct sta2x11_vip
*vip
= video_drvdata(file
);
640 unsigned int t_stop
, b_stop
, pitch
;
643 ret
= vidioc_try_fmt_vid_cap(file
, priv
, f
);
647 if (vb2_is_busy(&vip
->vb_vidq
)) {
648 /* Can't change format during acquisition */
649 v4l2_err(&vip
->v4l2_dev
, "device busy\n");
652 vip
->format
= f
->fmt
.pix
;
653 switch (vip
->format
.field
) {
654 case V4L2_FIELD_INTERLACED
:
655 t_stop
= ((vip
->format
.height
/ 2 - 1) << 16) |
656 (2 * vip
->format
.width
- 1);
658 pitch
= 4 * vip
->format
.width
;
661 t_stop
= ((vip
->format
.height
- 1) << 16) |
662 (2 * vip
->format
.width
- 1);
663 b_stop
= (0 << 16) | (2 * vip
->format
.width
- 1);
664 pitch
= 2 * vip
->format
.width
;
666 case V4L2_FIELD_BOTTOM
:
667 t_stop
= (0 << 16) | (2 * vip
->format
.width
- 1);
668 b_stop
= (vip
->format
.height
<< 16) |
669 (2 * vip
->format
.width
- 1);
670 pitch
= 2 * vip
->format
.width
;
673 v4l2_err(&vip
->v4l2_dev
, "unknown field format\n");
677 spin_lock_irq(&vip
->slock
);
678 /* Y-X Top Field Offset */
679 reg_write(vip
, DVP_TFO
, 0);
680 /* Y-X Bottom Field Offset */
681 reg_write(vip
, DVP_BFO
, 0);
682 /* Y-X Top Field Stop*/
683 reg_write(vip
, DVP_TFS
, t_stop
);
684 /* Y-X Bottom Field Stop */
685 reg_write(vip
, DVP_BFS
, b_stop
);
686 /* Video Memory Pitch */
687 reg_write(vip
, DVP_VMP
, pitch
);
688 spin_unlock_irq(&vip
->slock
);
694 * vidioc_g_fmt_vid_cap - get current video format parameters
695 * @file: descriptor of device
697 * @f: contains format information
699 * returns current video format parameters
701 * return value: 0, always successful
703 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
704 struct v4l2_format
*f
)
706 struct sta2x11_vip
*vip
= video_drvdata(file
);
708 f
->fmt
.pix
= vip
->format
;
713 static const struct v4l2_ioctl_ops vip_ioctl_ops
= {
714 .vidioc_querycap
= vidioc_querycap
,
716 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
717 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
718 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
719 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
720 /* Buffer handlers */
721 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
722 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
723 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
724 .vidioc_querybuf
= vb2_ioctl_querybuf
,
725 .vidioc_qbuf
= vb2_ioctl_qbuf
,
726 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
728 .vidioc_streamon
= vb2_ioctl_streamon
,
729 .vidioc_streamoff
= vb2_ioctl_streamoff
,
730 /* Standard handling */
731 .vidioc_g_std
= vidioc_g_std
,
732 .vidioc_s_std
= vidioc_s_std
,
733 .vidioc_querystd
= vidioc_querystd
,
735 .vidioc_enum_input
= vidioc_enum_input
,
736 .vidioc_g_input
= vidioc_g_input
,
737 .vidioc_s_input
= vidioc_s_input
,
738 /* Log status ioctl */
739 .vidioc_log_status
= v4l2_ctrl_log_status
,
741 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
742 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
745 static const struct video_device video_dev_template
= {
746 .name
= KBUILD_MODNAME
,
747 .release
= video_device_release_empty
,
749 .ioctl_ops
= &vip_ioctl_ops
,
750 .tvnorms
= V4L2_STD_ALL
,
751 .device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
|
756 * vip_irq - interrupt routine
757 * @irq: Number of interrupt ( not used, correct number is assumed )
758 * @data: local data structure containing all information
760 * check for both frame interrupts set ( top and bottom ).
761 * check FIFO overflow, but limit number of log messages after open.
762 * signal a complete buffer if done
764 * return value: IRQ_NONE, interrupt was not generated by VIP
766 * IRQ_HANDLED, interrupt done.
768 static irqreturn_t
vip_irq(int irq
, void *data
)
770 struct sta2x11_vip
*vip
= data
;
773 status
= reg_read(vip
, DVP_ITS
);
775 if (!status
) /* No interrupt to handle */
778 if (status
& DVP_IT_FIFO
)
779 if (vip
->overflow
++ > 5)
780 pr_info("VIP: fifo overflow\n");
782 if ((status
& DVP_IT_VST
) && (status
& DVP_IT_VSB
)) {
783 /* this is bad, we are too slow, hope the condition is gone
784 * on the next frame */
788 if (status
& DVP_IT_VST
)
789 if ((++vip
->tcount
) < 2)
791 if (status
& DVP_IT_VSB
) {
796 if (vip
->active
) { /* Acquisition is over on this buffer */
797 /* Disable acquisition */
798 reg_write(vip
, DVP_CTL
, reg_read(vip
, DVP_CTL
) & ~DVP_CTL_ENA
);
799 /* Remove the active buffer from the list */
800 vip
->active
->vb
.vb2_buf
.timestamp
= ktime_get_ns();
801 vip
->active
->vb
.sequence
= vip
->sequence
++;
802 vb2_buffer_done(&vip
->active
->vb
.vb2_buf
, VB2_BUF_STATE_DONE
);
808 static void sta2x11_vip_init_register(struct sta2x11_vip
*vip
)
810 /* Register initialization */
811 spin_lock_irq(&vip
->slock
);
812 /* Clean interrupt */
813 reg_read(vip
, DVP_ITS
);
814 /* Enable Half Line per vertical */
815 reg_write(vip
, DVP_HLFLN
, DVP_HLFLN_SD
);
816 /* Reset VIP control */
817 reg_write(vip
, DVP_CTL
, DVP_CTL_RST
);
818 /* Clear VIP control */
819 reg_write(vip
, DVP_CTL
, 0);
820 spin_unlock_irq(&vip
->slock
);
822 static void sta2x11_vip_clear_register(struct sta2x11_vip
*vip
)
824 spin_lock_irq(&vip
->slock
);
825 /* Disable interrupt */
826 reg_write(vip
, DVP_ITM
, 0);
827 /* Reset VIP Control */
828 reg_write(vip
, DVP_CTL
, DVP_CTL_RST
);
829 /* Clear VIP Control */
830 reg_write(vip
, DVP_CTL
, 0);
831 /* Clean VIP Interrupt */
832 reg_read(vip
, DVP_ITS
);
833 spin_unlock_irq(&vip
->slock
);
835 static int sta2x11_vip_init_buffer(struct sta2x11_vip
*vip
)
839 err
= dma_set_coherent_mask(&vip
->pdev
->dev
, DMA_BIT_MASK(29));
841 v4l2_err(&vip
->v4l2_dev
, "Cannot configure coherent mask");
844 memset(&vip
->vb_vidq
, 0, sizeof(struct vb2_queue
));
845 vip
->vb_vidq
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
846 vip
->vb_vidq
.io_modes
= VB2_MMAP
| VB2_READ
;
847 vip
->vb_vidq
.drv_priv
= vip
;
848 vip
->vb_vidq
.buf_struct_size
= sizeof(struct vip_buffer
);
849 vip
->vb_vidq
.ops
= &vip_video_qops
;
850 vip
->vb_vidq
.mem_ops
= &vb2_dma_contig_memops
;
851 vip
->vb_vidq
.timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
852 vip
->vb_vidq
.dev
= &vip
->pdev
->dev
;
853 vip
->vb_vidq
.lock
= &vip
->v4l_lock
;
854 err
= vb2_queue_init(&vip
->vb_vidq
);
857 INIT_LIST_HEAD(&vip
->buffer_list
);
858 spin_lock_init(&vip
->lock
);
862 static int sta2x11_vip_init_controls(struct sta2x11_vip
*vip
)
865 * Inititialize an empty control so VIP can inerithing controls
868 v4l2_ctrl_handler_init(&vip
->ctrl_hdl
, 0);
870 vip
->v4l2_dev
.ctrl_handler
= &vip
->ctrl_hdl
;
871 if (vip
->ctrl_hdl
.error
) {
872 int err
= vip
->ctrl_hdl
.error
;
874 v4l2_ctrl_handler_free(&vip
->ctrl_hdl
);
882 * vip_gpio_reserve - reserve gpio pin
884 * @pin: GPIO pin number
885 * @dir: direction, input or output
886 * @name: GPIO pin name
889 static int vip_gpio_reserve(struct device
*dev
, int pin
, int dir
,
892 struct gpio_desc
*desc
= gpio_to_desc(pin
);
895 if (!gpio_is_valid(pin
))
898 ret
= gpio_request(pin
, name
);
900 dev_err(dev
, "Failed to allocate pin %d (%s)\n", pin
, name
);
904 ret
= gpiod_direction_output(desc
, dir
);
906 dev_err(dev
, "Failed to set direction for pin %d (%s)\n",
912 ret
= gpiod_export(desc
, false);
914 dev_err(dev
, "Failed to export pin %d (%s)\n", pin
, name
);
923 * vip_gpio_release - release gpio pin
925 * @pin: GPIO pin number
926 * @name: GPIO pin name
929 static void vip_gpio_release(struct device
*dev
, int pin
, const char *name
)
931 if (gpio_is_valid(pin
)) {
932 struct gpio_desc
*desc
= gpio_to_desc(pin
);
934 dev_dbg(dev
, "releasing pin %d (%s)\n", pin
, name
);
935 gpiod_unexport(desc
);
941 * sta2x11_vip_init_one - init one instance of video device
945 * allocate reset pins for DAC.
946 * Reset video DAC, this is done via reset line.
947 * allocate memory for managing device
951 * find and initialize video DAC
953 * return value: 0, no error
957 * -ENODEV, device could not be detected or registered
959 static int sta2x11_vip_init_one(struct pci_dev
*pdev
,
960 const struct pci_device_id
*ent
)
963 struct sta2x11_vip
*vip
;
964 struct vip_config
*config
;
966 /* Check if hardware support 26-bit DMA */
967 if (dma_set_mask(&pdev
->dev
, DMA_BIT_MASK(26))) {
968 dev_err(&pdev
->dev
, "26-bit DMA addressing not available\n");
972 ret
= pci_enable_device(pdev
);
976 /* Get VIP platform data */
977 config
= dev_get_platdata(&pdev
->dev
);
979 dev_info(&pdev
->dev
, "VIP slot disabled\n");
984 /* Power configuration */
985 ret
= vip_gpio_reserve(&pdev
->dev
, config
->pwr_pin
, 0,
990 ret
= vip_gpio_reserve(&pdev
->dev
, config
->reset_pin
, 0,
993 vip_gpio_release(&pdev
->dev
, config
->pwr_pin
,
998 if (gpio_is_valid(config
->pwr_pin
)) {
999 /* Datasheet says 5ms between PWR and RST */
1000 usleep_range(5000, 25000);
1001 gpio_direction_output(config
->pwr_pin
, 1);
1004 if (gpio_is_valid(config
->reset_pin
)) {
1005 /* Datasheet says 5ms between PWR and RST */
1006 usleep_range(5000, 25000);
1007 gpio_direction_output(config
->reset_pin
, 1);
1009 usleep_range(5000, 25000);
1011 /* Allocate a new VIP instance */
1012 vip
= kzalloc(sizeof(struct sta2x11_vip
), GFP_KERNEL
);
1018 vip
->std
= V4L2_STD_PAL
;
1019 vip
->format
= formats_50
[0];
1020 vip
->config
= config
;
1021 mutex_init(&vip
->v4l_lock
);
1023 ret
= sta2x11_vip_init_controls(vip
);
1026 ret
= v4l2_device_register(&pdev
->dev
, &vip
->v4l2_dev
);
1030 dev_dbg(&pdev
->dev
, "BAR #0 at 0x%lx 0x%lx irq %d\n",
1031 (unsigned long)pci_resource_start(pdev
, 0),
1032 (unsigned long)pci_resource_len(pdev
, 0), pdev
->irq
);
1034 pci_set_master(pdev
);
1036 ret
= pci_request_regions(pdev
, KBUILD_MODNAME
);
1040 vip
->iomem
= pci_iomap(pdev
, 0, 0x100);
1046 pci_enable_msi(pdev
);
1048 /* Initialize buffer */
1049 ret
= sta2x11_vip_init_buffer(vip
);
1053 spin_lock_init(&vip
->slock
);
1055 ret
= request_irq(pdev
->irq
, vip_irq
, IRQF_SHARED
, KBUILD_MODNAME
, vip
);
1057 dev_err(&pdev
->dev
, "request_irq failed\n");
1062 /* Initialize and register video device */
1063 vip
->video_dev
= video_dev_template
;
1064 vip
->video_dev
.v4l2_dev
= &vip
->v4l2_dev
;
1065 vip
->video_dev
.queue
= &vip
->vb_vidq
;
1066 vip
->video_dev
.lock
= &vip
->v4l_lock
;
1067 video_set_drvdata(&vip
->video_dev
, vip
);
1069 ret
= video_register_device(&vip
->video_dev
, VFL_TYPE_VIDEO
, -1);
1073 /* Get ADV7180 subdevice */
1074 vip
->adapter
= i2c_get_adapter(vip
->config
->i2c_id
);
1075 if (!vip
->adapter
) {
1077 dev_err(&pdev
->dev
, "no I2C adapter found\n");
1081 vip
->decoder
= v4l2_i2c_new_subdev(&vip
->v4l2_dev
, vip
->adapter
,
1082 "adv7180", vip
->config
->i2c_addr
,
1084 if (!vip
->decoder
) {
1086 dev_err(&pdev
->dev
, "no decoder found\n");
1090 i2c_put_adapter(vip
->adapter
);
1091 v4l2_subdev_call(vip
->decoder
, core
, init
, 0);
1093 sta2x11_vip_init_register(vip
);
1095 dev_info(&pdev
->dev
, "STA2X11 Video Input Port (VIP) loaded\n");
1099 video_set_drvdata(&vip
->video_dev
, NULL
);
1101 vb2_video_unregister_device(&vip
->video_dev
);
1102 free_irq(pdev
->irq
, vip
);
1104 pci_disable_msi(pdev
);
1106 pci_iounmap(pdev
, vip
->iomem
);
1108 pci_release_regions(pdev
);
1110 v4l2_device_unregister(&vip
->v4l2_dev
);
1114 vip_gpio_release(&pdev
->dev
, config
->reset_pin
, config
->reset_name
);
1115 vip_gpio_release(&pdev
->dev
, config
->pwr_pin
, config
->pwr_name
);
1118 * do not call pci_disable_device on sta2x11 because it break all
1119 * other Bus masters on this EP
1125 * sta2x11_vip_remove_one - release device
1128 * Undo everything done in .._init_one
1130 * unregister video device
1136 static void sta2x11_vip_remove_one(struct pci_dev
*pdev
)
1138 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pdev
);
1139 struct sta2x11_vip
*vip
=
1140 container_of(v4l2_dev
, struct sta2x11_vip
, v4l2_dev
);
1142 sta2x11_vip_clear_register(vip
);
1144 video_set_drvdata(&vip
->video_dev
, NULL
);
1145 vb2_video_unregister_device(&vip
->video_dev
);
1146 free_irq(pdev
->irq
, vip
);
1147 pci_disable_msi(pdev
);
1148 pci_iounmap(pdev
, vip
->iomem
);
1149 pci_release_regions(pdev
);
1151 v4l2_device_unregister(&vip
->v4l2_dev
);
1153 vip_gpio_release(&pdev
->dev
, vip
->config
->pwr_pin
,
1154 vip
->config
->pwr_name
);
1155 vip_gpio_release(&pdev
->dev
, vip
->config
->reset_pin
,
1156 vip
->config
->reset_name
);
1160 * do not call pci_disable_device on sta2x11 because it break all
1161 * other Bus masters on this EP
1166 * sta2x11_vip_suspend - set device into power save mode
1167 * @dev_d: PCI device
1169 * all relevant registers are saved and an attempt to set a new state is made.
1171 * return value: 0 always indicate success,
1172 * even if device could not be disabled. (workaround for hardware problem)
1174 static int __maybe_unused
sta2x11_vip_suspend(struct device
*dev_d
)
1176 struct v4l2_device
*v4l2_dev
= dev_get_drvdata(dev_d
);
1177 struct sta2x11_vip
*vip
=
1178 container_of(v4l2_dev
, struct sta2x11_vip
, v4l2_dev
);
1179 unsigned long flags
;
1182 spin_lock_irqsave(&vip
->slock
, flags
);
1183 vip
->register_save_area
[0] = reg_read(vip
, DVP_CTL
);
1184 reg_write(vip
, DVP_CTL
, vip
->register_save_area
[0] & DVP_CTL_DIS
);
1185 vip
->register_save_area
[SAVE_COUNT
] = reg_read(vip
, DVP_ITM
);
1186 reg_write(vip
, DVP_ITM
, 0);
1187 for (i
= 1; i
< SAVE_COUNT
; i
++)
1188 vip
->register_save_area
[i
] = reg_read(vip
, 4 * i
);
1189 for (i
= 0; i
< AUX_COUNT
; i
++)
1190 vip
->register_save_area
[SAVE_COUNT
+ IRQ_COUNT
+ i
] =
1191 reg_read(vip
, registers_to_save
[i
]);
1192 spin_unlock_irqrestore(&vip
->slock
, flags
);
1196 pr_info("VIP: suspend\n");
1201 * sta2x11_vip_resume - resume device operation
1202 * @dev_d : PCI device
1204 * return value: 0, no error.
1206 * other, could not set device to power on state.
1208 static int __maybe_unused
sta2x11_vip_resume(struct device
*dev_d
)
1210 struct v4l2_device
*v4l2_dev
= dev_get_drvdata(dev_d
);
1211 struct sta2x11_vip
*vip
=
1212 container_of(v4l2_dev
, struct sta2x11_vip
, v4l2_dev
);
1213 unsigned long flags
;
1216 pr_info("VIP: resume\n");
1220 spin_lock_irqsave(&vip
->slock
, flags
);
1221 for (i
= 1; i
< SAVE_COUNT
; i
++)
1222 reg_write(vip
, 4 * i
, vip
->register_save_area
[i
]);
1223 for (i
= 0; i
< AUX_COUNT
; i
++)
1224 reg_write(vip
, registers_to_save
[i
],
1225 vip
->register_save_area
[SAVE_COUNT
+ IRQ_COUNT
+ i
]);
1226 reg_write(vip
, DVP_CTL
, vip
->register_save_area
[0]);
1227 reg_write(vip
, DVP_ITM
, vip
->register_save_area
[SAVE_COUNT
]);
1228 spin_unlock_irqrestore(&vip
->slock
, flags
);
1232 static const struct pci_device_id sta2x11_vip_pci_tbl
[] = {
1233 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO
, PCI_DEVICE_ID_STMICRO_VIP
)},
1237 static SIMPLE_DEV_PM_OPS(sta2x11_vip_pm_ops
,
1238 sta2x11_vip_suspend
,
1239 sta2x11_vip_resume
);
1241 static struct pci_driver sta2x11_vip_driver
= {
1242 .name
= KBUILD_MODNAME
,
1243 .probe
= sta2x11_vip_init_one
,
1244 .remove
= sta2x11_vip_remove_one
,
1245 .id_table
= sta2x11_vip_pci_tbl
,
1246 .driver
.pm
= &sta2x11_vip_pm_ops
,
1249 static int __init
sta2x11_vip_init_module(void)
1251 return pci_register_driver(&sta2x11_vip_driver
);
1254 static void __exit
sta2x11_vip_exit_module(void)
1256 pci_unregister_driver(&sta2x11_vip_driver
);
1260 module_init(sta2x11_vip_init_module
);
1261 module_exit(sta2x11_vip_exit_module
);
1263 late_initcall_sync(sta2x11_vip_init_module
);
1266 MODULE_DESCRIPTION("STA2X11 Video Input Port driver");
1267 MODULE_AUTHOR("Wind River");
1268 MODULE_LICENSE("GPL v2");
1269 MODULE_VERSION(DRV_VERSION
);
1270 MODULE_DEVICE_TABLE(pci
, sta2x11_vip_pci_tbl
);