2 * This is the driver for the STA2x11 Video Input Port.
4 * Copyright (C) 2012 ST Microelectronics
5 * author: Federico Vaga <federico.vaga@gmail.com>
6 * Copyright (C) 2010 WindRiver Systems, Inc.
7 * authors: Andreas Kies <andreas.kies@windriver.com>
8 * Vlad Lungu <vlad.lungu@windriver.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms and conditions of the GNU General Public License,
12 * version 2, as published by the Free Software Foundation.
14 * This program is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * You should have received a copy of the GNU General Public License along with
20 * this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
23 * The full GNU General Public License is included in this distribution in
24 * the file called "COPYING".
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>
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>
48 #include "sta2x11_vip.h"
50 #define DRV_VERSION "1.3"
52 #ifndef PCI_DEVICE_ID_STMICRO_VIP
53 #define PCI_DEVICE_ID_STMICRO_VIP 0xCC0D
70 #define DVP_HLFLN 0xA8
75 #define DVP_CTL_ENA 0x00000001
76 #define DVP_CTL_RST 0x80000000
77 #define DVP_CTL_DIS (~0x00040001)
79 #define DVP_IT_VSB 0x00000008
80 #define DVP_IT_VST 0x00000010
81 #define DVP_IT_FIFO 0x00000020
83 #define DVP_HLFLN_SD 0x00000001
91 struct vb2_v4l2_buffer vb
;
92 struct list_head list
;
95 static inline struct vip_buffer
*to_vip_buffer(struct vb2_v4l2_buffer
*vb2
)
97 return container_of(vb2
, struct vip_buffer
, vb
);
101 * struct sta2x11_vip - All internal data for one instance of device
102 * @v4l2_dev: device registered in v4l layer
103 * @video_dev: properties of our device
105 * @adapter: contains I2C adapter information
106 * @register_save_area: All relevant register are saved here during suspend
107 * @decoder: contains information about video DAC
108 * @ctrl_hdl: handler for control framework
109 * @format: pixel format, fixed UYVY
110 * @std: video standard (e.g. PAL/NTSC)
111 * @input: input line for video signal ( 0 or 1 )
112 * @disabled: Device is in power down state
113 * @slock: for excluse acces of registers
114 * @vb_vidq: queue maintained by videobuf2 layer
115 * @buffer_list: list of buffer in use
116 * @sequence: sequence number of acquired buffer
117 * @active: current active buffer
118 * @lock: used in videobuf2 callback
119 * @v4l_lock: serialize its video4linux ioctls
120 * @tcount: Number of top frames
121 * @bcount: Number of bottom frames
122 * @overflow: Number of FIFO overflows
123 * @iomem: hardware base address
124 * @config: I2C and gpio config from platform
126 * All non-local data is accessed via this structure.
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
;
138 struct v4l2_pix_format format
;
144 struct vb2_queue vb_vidq
;
145 struct list_head buffer_list
;
146 unsigned int sequence
;
147 struct vip_buffer
*active
; /* current active buffer */
148 spinlock_t lock
; /* Used in videobuf2 callback */
149 struct mutex v4l_lock
;
151 /* Interrupt counters */
155 void __iomem
*iomem
; /* I/O Memory */
156 struct vip_config
*config
;
159 static const unsigned int registers_to_save
[AUX_COUNT
] = {
160 DVP_HLFLN
, DVP_RGB
, DVP_PKZ
163 static struct v4l2_pix_format formats_50
[] = {
164 { /*PAL interlaced */
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
},
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
},
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
},
191 static struct v4l2_pix_format formats_60
[] = {
192 { /*NTSC interlaced */
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
},
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
},
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
},
218 /* Write VIP register */
219 static inline void reg_write(struct sta2x11_vip
*vip
, unsigned int reg
, u32 val
)
221 iowrite32((val
), (vip
->iomem
)+(reg
));
223 /* Read VIP register */
224 static inline u32
reg_read(struct sta2x11_vip
*vip
, unsigned int reg
)
226 return ioread32((vip
->iomem
)+(reg
));
228 /* Start DMA acquisition */
229 static void start_dma(struct sta2x11_vip
*vip
, struct vip_buffer
*vip_buf
)
231 unsigned long offset
= 0;
233 if (vip
->format
.field
== V4L2_FIELD_INTERLACED
)
234 offset
= vip
->format
.width
* 2;
236 spin_lock_irq(&vip
->slock
);
237 /* Enable acquisition */
238 reg_write(vip
, DVP_CTL
, reg_read(vip
, DVP_CTL
) | DVP_CTL_ENA
);
239 /* Set Top and Bottom Field memory address */
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
);
245 /* Fetch the next buffer to activate */
246 static void vip_active_buf_next(struct sta2x11_vip
*vip
)
248 /* Get the next buffer */
249 spin_lock(&vip
->lock
);
250 if (list_empty(&vip
->buffer_list
)) {/* No available buffer */
251 spin_unlock(&vip
->lock
);
254 vip
->active
= list_first_entry(&vip
->buffer_list
,
257 /* Reset Top and Bottom counter */
260 spin_unlock(&vip
->lock
);
261 if (vb2_is_streaming(&vip
->vb_vidq
)) { /* streaming is on */
262 start_dma(vip
, vip
->active
); /* start dma capture */
267 /* Videobuf2 Operations */
268 static int queue_setup(struct vb2_queue
*vq
,
269 unsigned int *nbuffers
, unsigned int *nplanes
,
270 unsigned int sizes
[], struct device
*alloc_devs
[])
272 struct sta2x11_vip
*vip
= vb2_get_drv_priv(vq
);
274 if (!(*nbuffers
) || *nbuffers
< MAX_FRAMES
)
275 *nbuffers
= MAX_FRAMES
;
278 sizes
[0] = vip
->format
.sizeimage
;
287 static int buffer_init(struct vb2_buffer
*vb
)
289 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
290 struct vip_buffer
*vip_buf
= to_vip_buffer(vbuf
);
292 vip_buf
->dma
= vb2_dma_contig_plane_dma_addr(vb
, 0);
293 INIT_LIST_HEAD(&vip_buf
->list
);
297 static int buffer_prepare(struct vb2_buffer
*vb
)
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
);
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
);
311 vb2_set_plane_payload(&vip_buf
->vb
.vb2_buf
, 0, size
);
315 static void buffer_queue(struct vb2_buffer
*vb
)
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
);
321 spin_lock(&vip
->lock
);
322 list_add_tail(&vip_buf
->list
, &vip
->buffer_list
);
323 if (!vip
->active
) { /* No active buffer, active the first one */
324 vip
->active
= list_first_entry(&vip
->buffer_list
,
327 if (vb2_is_streaming(&vip
->vb_vidq
)) /* streaming is on */
328 start_dma(vip
, vip_buf
); /* start dma capture */
330 spin_unlock(&vip
->lock
);
332 static void buffer_finish(struct vb2_buffer
*vb
)
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
);
338 /* Buffer handled, remove it from the list */
339 spin_lock(&vip
->lock
);
340 list_del_init(&vip_buf
->list
);
341 spin_unlock(&vip
->lock
);
343 if (vb2_is_streaming(vb
->vb2_queue
))
344 vip_active_buf_next(vip
);
347 static int start_streaming(struct vb2_queue
*vq
, unsigned int count
)
349 struct sta2x11_vip
*vip
= vb2_get_drv_priv(vq
);
351 spin_lock_irq(&vip
->slock
);
352 /* Enable interrupt VSYNC Top and Bottom*/
353 reg_write(vip
, DVP_ITM
, DVP_IT_VSB
| DVP_IT_VST
);
354 spin_unlock_irq(&vip
->slock
);
357 start_dma(vip
, vip
->active
);
362 /* abort streaming and wait for last buffer */
363 static void stop_streaming(struct vb2_queue
*vq
)
365 struct sta2x11_vip
*vip
= vb2_get_drv_priv(vq
);
366 struct vip_buffer
*vip_buf
, *node
;
368 /* Disable acquisition */
369 reg_write(vip
, DVP_CTL
, reg_read(vip
, DVP_CTL
) & ~DVP_CTL_ENA
);
370 /* Disable all interrupts */
371 reg_write(vip
, DVP_ITM
, 0);
373 /* Release all active buffers */
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
);
379 spin_unlock(&vip
->lock
);
382 static 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
,
395 /* File Operations */
396 static 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
,
408 * vidioc_querycap - return capabilities of device
409 * @file: descriptor of device
410 * @cap: contains return values
413 * the capabilities of the device are returned
415 * return value: 0, no error.
417 static int vidioc_querycap(struct file
*file
, void *priv
,
418 struct v4l2_capability
*cap
)
420 struct sta2x11_vip
*vip
= video_drvdata(file
);
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
|
428 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
434 * vidioc_s_std - set video standard
435 * @file: descriptor of device
436 * @std: contains standard to be set
439 * the video standard is set
441 * return value: 0, no error.
443 * -EIO, no input signal detected
445 * other, returned from video DAC.
447 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id std
)
449 struct sta2x11_vip
*vip
= video_drvdata(file
);
452 * This is here for backwards compatibility only.
453 * The use of V4L2_STD_ALL to trigger a querystd is non-standard.
455 if (std
== V4L2_STD_ALL
) {
456 v4l2_subdev_call(vip
->decoder
, video
, querystd
, &std
);
457 if (std
== V4L2_STD_UNKNOWN
)
461 if (vip
->std
!= std
) {
463 if (V4L2_STD_525_60
& std
)
464 vip
->format
= formats_60
[0];
466 vip
->format
= formats_50
[0];
469 return v4l2_subdev_call(vip
->decoder
, video
, s_std
, std
);
473 * vidioc_g_std - get video standard
474 * @file: descriptor of device
476 * @std: contains return values
478 * the current video standard is returned
480 * return value: 0, no error.
482 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
484 struct sta2x11_vip
*vip
= video_drvdata(file
);
491 * vidioc_querystd - get possible video standards
492 * @file: descriptor of device
494 * @std: contains return values
496 * all possible video standards are returned
498 * return value: delivered by video DAC routine.
500 static int vidioc_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std
)
502 struct sta2x11_vip
*vip
= video_drvdata(file
);
504 return v4l2_subdev_call(vip
->decoder
, video
, querystd
, std
);
507 static int vidioc_enum_input(struct file
*file
, void *priv
,
508 struct v4l2_input
*inp
)
513 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
514 inp
->std
= V4L2_STD_ALL
;
515 sprintf(inp
->name
, "Camera %u", inp
->index
);
521 * vidioc_s_input - set input line
522 * @file: descriptor of device
524 * @i: new input line number
526 * the current active input line is set
528 * return value: 0, no error.
530 * -EINVAL, line number out of range
532 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
534 struct sta2x11_vip
*vip
= video_drvdata(file
);
539 ret
= v4l2_subdev_call(vip
->decoder
, video
, s_routing
, i
, 0, 0);
548 * vidioc_g_input - return input line
549 * @file: descriptor of device
551 * @i: returned input line number
553 * the current active input line is returned
555 * return value: always 0.
557 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
559 struct sta2x11_vip
*vip
= video_drvdata(file
);
566 * vidioc_enum_fmt_vid_cap - return video capture format
567 * @file: descriptor of device
569 * @f: returned format information
571 * returns name and format of video capture
572 * Only UYVY is supported by hardware.
574 * return value: always 0.
576 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
577 struct v4l2_fmtdesc
*f
)
583 strcpy(f
->description
, "4:2:2, packed, UYVY");
584 f
->pixelformat
= V4L2_PIX_FMT_UYVY
;
590 * vidioc_try_fmt_vid_cap - set video capture format
591 * @file: descriptor of device
595 * new video format is set which includes width and
596 * field type. width is fixed to 720, no scaling.
597 * Only UYVY is supported by this hardware.
598 * the minimum height is 200, the maximum is 576 (PAL)
600 * return value: 0, no error
602 * -EINVAL, pixel or field format not supported
605 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
606 struct v4l2_format
*f
)
608 struct sta2x11_vip
*vip
= video_drvdata(file
);
611 if (V4L2_PIX_FMT_UYVY
!= f
->fmt
.pix
.pixelformat
) {
612 v4l2_warn(&vip
->v4l2_dev
, "Invalid format, only UYVY supported\n");
616 if (V4L2_STD_525_60
& vip
->std
)
621 switch (f
->fmt
.pix
.field
) {
624 if (interlace_lim
< f
->fmt
.pix
.height
)
625 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
627 f
->fmt
.pix
.field
= V4L2_FIELD_BOTTOM
;
630 case V4L2_FIELD_BOTTOM
:
631 if (interlace_lim
< f
->fmt
.pix
.height
)
632 f
->fmt
.pix
.height
= interlace_lim
;
634 case V4L2_FIELD_INTERLACED
:
638 /* It is the only supported format */
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
;
653 * vidioc_s_fmt_vid_cap - set current video format parameters
654 * @file: descriptor of device
656 * @f: returned format information
658 * set new capture format
659 * return value: 0, no error
661 * other, delivered by video DAC routine.
663 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
664 struct v4l2_format
*f
)
666 struct sta2x11_vip
*vip
= video_drvdata(file
);
667 unsigned int t_stop
, b_stop
, pitch
;
670 ret
= vidioc_try_fmt_vid_cap(file
, priv
, f
);
674 if (vb2_is_busy(&vip
->vb_vidq
)) {
675 /* Can't change format during acquisition */
676 v4l2_err(&vip
->v4l2_dev
, "device busy\n");
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);
685 pitch
= 4 * vip
->format
.width
;
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
;
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
;
700 v4l2_err(&vip
->v4l2_dev
, "unknown field format\n");
704 spin_lock_irq(&vip
->slock
);
705 /* Y-X Top Field Offset */
706 reg_write(vip
, DVP_TFO
, 0);
707 /* Y-X Bottom Field Offset */
708 reg_write(vip
, DVP_BFO
, 0);
709 /* Y-X Top Field Stop*/
710 reg_write(vip
, DVP_TFS
, t_stop
);
711 /* Y-X Bottom Field Stop */
712 reg_write(vip
, DVP_BFS
, b_stop
);
713 /* Video Memory Pitch */
714 reg_write(vip
, DVP_VMP
, pitch
);
715 spin_unlock_irq(&vip
->slock
);
721 * vidioc_g_fmt_vid_cap - get current video format parameters
722 * @file: descriptor of device
724 * @f: contains format information
726 * returns current video format parameters
728 * return value: 0, always successful
730 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
731 struct v4l2_format
*f
)
733 struct sta2x11_vip
*vip
= video_drvdata(file
);
735 f
->fmt
.pix
= vip
->format
;
740 static const struct v4l2_ioctl_ops vip_ioctl_ops
= {
741 .vidioc_querycap
= vidioc_querycap
,
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 /* Buffer handlers */
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
,
755 .vidioc_streamon
= vb2_ioctl_streamon
,
756 .vidioc_streamoff
= vb2_ioctl_streamoff
,
757 /* Standard handling */
758 .vidioc_g_std
= vidioc_g_std
,
759 .vidioc_s_std
= vidioc_s_std
,
760 .vidioc_querystd
= vidioc_querystd
,
762 .vidioc_enum_input
= vidioc_enum_input
,
763 .vidioc_g_input
= vidioc_g_input
,
764 .vidioc_s_input
= vidioc_s_input
,
765 /* Log status ioctl */
766 .vidioc_log_status
= v4l2_ctrl_log_status
,
768 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
769 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
772 static const struct video_device video_dev_template
= {
773 .name
= KBUILD_MODNAME
,
774 .release
= video_device_release_empty
,
776 .ioctl_ops
= &vip_ioctl_ops
,
777 .tvnorms
= V4L2_STD_ALL
,
781 * vip_irq - interrupt routine
782 * @irq: Number of interrupt ( not used, correct number is assumed )
783 * @vip: local data structure containing all information
785 * check for both frame interrupts set ( top and bottom ).
786 * check FIFO overflow, but limit number of log messages after open.
787 * signal a complete buffer if done
789 * return value: IRQ_NONE, interrupt was not generated by VIP
791 * IRQ_HANDLED, interrupt done.
793 static irqreturn_t
vip_irq(int irq
, struct sta2x11_vip
*vip
)
797 status
= reg_read(vip
, DVP_ITS
);
799 if (!status
) /* No interrupt to handle */
802 if (status
& DVP_IT_FIFO
)
803 if (vip
->overflow
++ > 5)
804 pr_info("VIP: fifo overflow\n");
806 if ((status
& DVP_IT_VST
) && (status
& DVP_IT_VSB
)) {
807 /* this is bad, we are too slow, hope the condition is gone
808 * on the next frame */
812 if (status
& DVP_IT_VST
)
813 if ((++vip
->tcount
) < 2)
815 if (status
& DVP_IT_VSB
) {
820 if (vip
->active
) { /* Acquisition is over on this buffer */
821 /* Disable acquisition */
822 reg_write(vip
, DVP_CTL
, reg_read(vip
, DVP_CTL
) & ~DVP_CTL_ENA
);
823 /* Remove the active buffer from the list */
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
);
832 static void sta2x11_vip_init_register(struct sta2x11_vip
*vip
)
834 /* Register initialization */
835 spin_lock_irq(&vip
->slock
);
836 /* Clean interrupt */
837 reg_read(vip
, DVP_ITS
);
838 /* Enable Half Line per vertical */
839 reg_write(vip
, DVP_HLFLN
, DVP_HLFLN_SD
);
840 /* Reset VIP control */
841 reg_write(vip
, DVP_CTL
, DVP_CTL_RST
);
842 /* Clear VIP control */
843 reg_write(vip
, DVP_CTL
, 0);
844 spin_unlock_irq(&vip
->slock
);
846 static void sta2x11_vip_clear_register(struct sta2x11_vip
*vip
)
848 spin_lock_irq(&vip
->slock
);
849 /* Disable interrupt */
850 reg_write(vip
, DVP_ITM
, 0);
851 /* Reset VIP Control */
852 reg_write(vip
, DVP_CTL
, DVP_CTL_RST
);
853 /* Clear VIP Control */
854 reg_write(vip
, DVP_CTL
, 0);
855 /* Clean VIP Interrupt */
856 reg_read(vip
, DVP_ITS
);
857 spin_unlock_irq(&vip
->slock
);
859 static int sta2x11_vip_init_buffer(struct sta2x11_vip
*vip
)
863 err
= dma_set_coherent_mask(&vip
->pdev
->dev
, DMA_BIT_MASK(29));
865 v4l2_err(&vip
->v4l2_dev
, "Cannot configure coherent mask");
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
);
881 INIT_LIST_HEAD(&vip
->buffer_list
);
882 spin_lock_init(&vip
->lock
);
886 static int sta2x11_vip_init_controls(struct sta2x11_vip
*vip
)
889 * Inititialize an empty control so VIP can inerithing controls
892 v4l2_ctrl_handler_init(&vip
->ctrl_hdl
, 0);
894 vip
->v4l2_dev
.ctrl_handler
= &vip
->ctrl_hdl
;
895 if (vip
->ctrl_hdl
.error
) {
896 int err
= vip
->ctrl_hdl
.error
;
898 v4l2_ctrl_handler_free(&vip
->ctrl_hdl
);
906 * vip_gpio_reserve - reserve gpio pin
908 * @pin: GPIO pin number
909 * @dir: direction, input or output
910 * @name: GPIO pin name
913 static int vip_gpio_reserve(struct device
*dev
, int pin
, int dir
,
918 if (!gpio_is_valid(pin
))
921 ret
= gpio_request(pin
, name
);
923 dev_err(dev
, "Failed to allocate pin %d (%s)\n", pin
, name
);
927 ret
= gpio_direction_output(pin
, dir
);
929 dev_err(dev
, "Failed to set direction for pin %d (%s)\n",
935 ret
= gpio_export(pin
, false);
937 dev_err(dev
, "Failed to export pin %d (%s)\n", pin
, name
);
946 * vip_gpio_release - release gpio pin
948 * @pin: GPIO pin number
949 * @name: GPIO pin name
952 static void vip_gpio_release(struct device
*dev
, int pin
, const char *name
)
954 if (gpio_is_valid(pin
)) {
955 dev_dbg(dev
, "releasing pin %d (%s)\n", pin
, name
);
962 * sta2x11_vip_init_one - init one instance of video device
966 * allocate reset pins for DAC.
967 * Reset video DAC, this is done via reset line.
968 * allocate memory for managing device
972 * find and initialize video DAC
974 * return value: 0, no error
978 * -ENODEV, device could not be detected or registered
980 static int sta2x11_vip_init_one(struct pci_dev
*pdev
,
981 const struct pci_device_id
*ent
)
984 struct sta2x11_vip
*vip
;
985 struct vip_config
*config
;
987 /* Check if hardware support 26-bit DMA */
988 if (dma_set_mask(&pdev
->dev
, DMA_BIT_MASK(26))) {
989 dev_err(&pdev
->dev
, "26-bit DMA addressing not available\n");
993 ret
= pci_enable_device(pdev
);
997 /* Get VIP platform data */
998 config
= dev_get_platdata(&pdev
->dev
);
1000 dev_info(&pdev
->dev
, "VIP slot disabled\n");
1005 /* Power configuration */
1006 ret
= vip_gpio_reserve(&pdev
->dev
, config
->pwr_pin
, 0,
1011 ret
= vip_gpio_reserve(&pdev
->dev
, config
->reset_pin
, 0,
1012 config
->reset_name
);
1014 vip_gpio_release(&pdev
->dev
, config
->pwr_pin
,
1019 if (gpio_is_valid(config
->pwr_pin
)) {
1020 /* Datasheet says 5ms between PWR and RST */
1021 usleep_range(5000, 25000);
1022 gpio_direction_output(config
->pwr_pin
, 1);
1025 if (gpio_is_valid(config
->reset_pin
)) {
1026 /* Datasheet says 5ms between PWR and RST */
1027 usleep_range(5000, 25000);
1028 gpio_direction_output(config
->reset_pin
, 1);
1030 usleep_range(5000, 25000);
1032 /* Allocate a new VIP instance */
1033 vip
= kzalloc(sizeof(struct sta2x11_vip
), GFP_KERNEL
);
1039 vip
->std
= V4L2_STD_PAL
;
1040 vip
->format
= formats_50
[0];
1041 vip
->config
= config
;
1042 mutex_init(&vip
->v4l_lock
);
1044 ret
= sta2x11_vip_init_controls(vip
);
1047 ret
= v4l2_device_register(&pdev
->dev
, &vip
->v4l2_dev
);
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
);
1055 pci_set_master(pdev
);
1057 ret
= pci_request_regions(pdev
, KBUILD_MODNAME
);
1061 vip
->iomem
= pci_iomap(pdev
, 0, 0x100);
1067 pci_enable_msi(pdev
);
1069 /* Initialize buffer */
1070 ret
= sta2x11_vip_init_buffer(vip
);
1074 spin_lock_init(&vip
->slock
);
1076 ret
= request_irq(pdev
->irq
,
1077 (irq_handler_t
) vip_irq
,
1078 IRQF_SHARED
, KBUILD_MODNAME
, vip
);
1080 dev_err(&pdev
->dev
, "request_irq failed\n");
1085 /* Initialize and register video device */
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
);
1092 ret
= video_register_device(&vip
->video_dev
, VFL_TYPE_GRABBER
, -1);
1096 /* Get ADV7180 subdevice */
1097 vip
->adapter
= i2c_get_adapter(vip
->config
->i2c_id
);
1098 if (!vip
->adapter
) {
1100 dev_err(&pdev
->dev
, "no I2C adapter found\n");
1104 vip
->decoder
= v4l2_i2c_new_subdev(&vip
->v4l2_dev
, vip
->adapter
,
1105 "adv7180", vip
->config
->i2c_addr
,
1107 if (!vip
->decoder
) {
1109 dev_err(&pdev
->dev
, "no decoder found\n");
1113 i2c_put_adapter(vip
->adapter
);
1114 v4l2_subdev_call(vip
->decoder
, core
, init
, 0);
1116 sta2x11_vip_init_register(vip
);
1118 dev_info(&pdev
->dev
, "STA2X11 Video Input Port (VIP) loaded\n");
1122 video_set_drvdata(&vip
->video_dev
, NULL
);
1124 video_unregister_device(&vip
->video_dev
);
1125 free_irq(pdev
->irq
, vip
);
1127 pci_disable_msi(pdev
);
1129 vb2_queue_release(&vip
->vb_vidq
);
1130 pci_iounmap(pdev
, vip
->iomem
);
1132 pci_release_regions(pdev
);
1134 v4l2_device_unregister(&vip
->v4l2_dev
);
1138 vip_gpio_release(&pdev
->dev
, config
->reset_pin
, config
->reset_name
);
1139 vip_gpio_release(&pdev
->dev
, config
->pwr_pin
, config
->pwr_name
);
1142 * do not call pci_disable_device on sta2x11 because it break all
1143 * other Bus masters on this EP
1149 * sta2x11_vip_remove_one - release device
1152 * Undo everything done in .._init_one
1154 * unregister video device
1160 static void sta2x11_vip_remove_one(struct pci_dev
*pdev
)
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
);
1166 sta2x11_vip_clear_register(vip
);
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
);
1176 v4l2_device_unregister(&vip
->v4l2_dev
);
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
);
1185 * do not call pci_disable_device on sta2x11 because it break all
1186 * other Bus masters on this EP
1193 * sta2x11_vip_suspend - set device into power save mode
1195 * @state: new state of device
1197 * all relevant registers are saved and an attempt to set a new state is made.
1199 * return value: 0 always indicate success,
1200 * even if device could not be disabled. (workaround for hardware problem)
1202 static int sta2x11_vip_suspend(struct pci_dev
*pdev
, pm_message_t state
)
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
;
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 /* save pci state */
1222 pci_save_state(pdev
);
1223 if (pci_set_power_state(pdev
, pci_choose_state(pdev
, state
))) {
1225 * do not call pci_disable_device on sta2x11 because it
1226 * break all other Bus masters on this EP
1231 pr_info("VIP: suspend\n");
1236 * sta2x11_vip_resume - resume device operation
1237 * @pdev : PCI device
1239 * re-enable device, set PCI state to powered and restore registers.
1240 * resume normal device operation afterwards.
1242 * return value: 0, no error.
1244 * other, could not set device to power on state.
1246 static int sta2x11_vip_resume(struct pci_dev
*pdev
)
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
;
1254 pr_info("VIP: resume\n");
1255 /* restore pci state */
1256 if (vip
->disabled
) {
1257 ret
= pci_enable_device(pdev
);
1259 pr_warn("VIP: Can't enable device.\n");
1264 ret
= pci_set_power_state(pdev
, PCI_D0
);
1267 * do not call pci_disable_device on sta2x11 because it
1268 * break all other Bus masters on this EP
1270 pr_warn("VIP: Can't enable device.\n");
1275 pci_restore_state(pdev
);
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
);
1291 static const struct pci_device_id sta2x11_vip_pci_tbl
[] = {
1292 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO
, PCI_DEVICE_ID_STMICRO_VIP
)},
1296 static 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
,
1302 .suspend
= sta2x11_vip_suspend
,
1303 .resume
= sta2x11_vip_resume
,
1307 static int __init
sta2x11_vip_init_module(void)
1309 return pci_register_driver(&sta2x11_vip_driver
);
1312 static void __exit
sta2x11_vip_exit_module(void)
1314 pci_unregister_driver(&sta2x11_vip_driver
);
1318 module_init(sta2x11_vip_init_module
);
1319 module_exit(sta2x11_vip_exit_module
);
1321 late_initcall_sync(sta2x11_vip_init_module
);
1324 MODULE_DESCRIPTION("STA2X11 Video Input Port driver");
1325 MODULE_AUTHOR("Wind River");
1326 MODULE_LICENSE("GPL v2");
1327 MODULE_SUPPORTED_DEVICE("sta2x11 video input");
1328 MODULE_VERSION(DRV_VERSION
);
1329 MODULE_DEVICE_TABLE(pci
, sta2x11_vip_pci_tbl
);