2 * This is the driver for the STA2x11 Video Input Port.
4 * Copyright (C) 2010 WindRiver Systems, Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
22 * Author: Andreas Kies <andreas.kies@windriver.com>
23 * Vlad Lungu <vlad.lungu@windriver.com>
27 #include <linux/types.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/vmalloc.h>
33 #include <linux/videodev2.h>
35 #include <linux/kmod.h>
37 #include <linux/pci.h>
38 #include <linux/interrupt.h>
39 #include <linux/mutex.h>
41 #include <linux/gpio.h>
42 #include <linux/i2c.h>
43 #include <linux/delay.h>
44 #include <media/v4l2-common.h>
45 #include <media/v4l2-device.h>
46 #include <media/v4l2-ioctl.h>
47 #include <media/videobuf-dma-contig.h>
49 #include "sta2x11_vip.h"
51 #define DRV_NAME "sta2x11_vip"
52 #define DRV_VERSION "1.3"
54 #ifndef PCI_DEVICE_ID_STMICRO_VIP
55 #define PCI_DEVICE_ID_STMICRO_VIP 0xCC0D
72 #define DVP_HLFLN 0xA8
77 #define DVP_CTL_ENA 0x00000001
78 #define DVP_CTL_RST 0x80000000
79 #define DVP_CTL_DIS (~0x00040001)
81 #define DVP_IT_VSB 0x00000008
82 #define DVP_IT_VST 0x00000010
83 #define DVP_IT_FIFO 0x00000020
85 #define DVP_HLFLN_SD 0x00000001
87 #define REG_WRITE(vip, reg, value) iowrite32((value), (vip->iomem)+(reg))
88 #define REG_READ(vip, reg) ioread32((vip->iomem)+(reg))
95 * struct sta2x11_vip - All internal data for one instance of device
96 * @v4l2_dev: device registered in v4l layer
97 * @video_dev: properties of our device
99 * @adapter: contains I2C adapter information
100 * @register_save_area: All relevant register are saved here during suspend
101 * @decoder: contains information about video DAC
102 * @format: pixel format, fixed UYVY
103 * @std: video standard (e.g. PAL/NTSC)
104 * @input: input line for video signal ( 0 or 1 )
105 * @users: Number of open of device ( max. 1 )
106 * @disabled: Device is in power down state
107 * @mutex: ensures exclusive opening of device
108 * @slock: for excluse acces of registers
109 * @vb_vidq: queue maintained by videobuf layer
110 * @capture: linked list of capture buffer
111 * @active: struct videobuf_buffer currently beingg filled
112 * @started: device is ready to capture frame
113 * @closing: device will be shut down
114 * @tcount: Number of top frames
115 * @bcount: Number of bottom frames
116 * @overflow: Number of FIFO overflows
117 * @mem_spare: small buffer of unused frame
118 * @dma_spare: dma addres of mem_spare
119 * @iomem: hardware base address
120 * @config: I2C and gpio config from platform
122 * All non-local data is accessed via this structure.
126 struct v4l2_device v4l2_dev
;
127 struct video_device
*video_dev
;
128 struct pci_dev
*pdev
;
129 struct i2c_adapter
*adapter
;
130 unsigned int register_save_area
[IRQ_COUNT
+ SAVE_COUNT
+ AUX_COUNT
];
131 struct v4l2_subdev
*decoder
;
132 struct v4l2_pix_format format
;
137 struct mutex mutex
; /* exclusive access during open */
138 spinlock_t slock
; /* spin lock for hardware and queue access */
139 struct videobuf_queue vb_vidq
;
140 struct list_head capture
;
141 struct videobuf_buffer
*active
;
142 int started
, closing
, tcount
, bcount
;
145 dma_addr_t dma_spare
;
147 struct vip_config
*config
;
150 static const unsigned int registers_to_save
[AUX_COUNT
] = {
151 DVP_HLFLN
, DVP_RGB
, DVP_PKZ
154 static struct v4l2_pix_format formats_50
[] = {
155 { /*PAL interlaced */
158 .pixelformat
= V4L2_PIX_FMT_UYVY
,
159 .field
= V4L2_FIELD_INTERLACED
,
160 .bytesperline
= 720 * 2,
161 .sizeimage
= 720 * 2 * 576,
162 .colorspace
= V4L2_COLORSPACE_SMPTE170M
},
166 .pixelformat
= V4L2_PIX_FMT_UYVY
,
167 .field
= V4L2_FIELD_TOP
,
168 .bytesperline
= 720 * 2,
169 .sizeimage
= 720 * 2 * 288,
170 .colorspace
= V4L2_COLORSPACE_SMPTE170M
},
174 .pixelformat
= V4L2_PIX_FMT_UYVY
,
175 .field
= V4L2_FIELD_BOTTOM
,
176 .bytesperline
= 720 * 2,
177 .sizeimage
= 720 * 2 * 288,
178 .colorspace
= V4L2_COLORSPACE_SMPTE170M
},
182 static struct v4l2_pix_format formats_60
[] = {
183 { /*NTSC interlaced */
186 .pixelformat
= V4L2_PIX_FMT_UYVY
,
187 .field
= V4L2_FIELD_INTERLACED
,
188 .bytesperline
= 720 * 2,
189 .sizeimage
= 720 * 2 * 480,
190 .colorspace
= V4L2_COLORSPACE_SMPTE170M
},
194 .pixelformat
= V4L2_PIX_FMT_UYVY
,
195 .field
= V4L2_FIELD_TOP
,
196 .bytesperline
= 720 * 2,
197 .sizeimage
= 720 * 2 * 240,
198 .colorspace
= V4L2_COLORSPACE_SMPTE170M
},
202 .pixelformat
= V4L2_PIX_FMT_UYVY
,
203 .field
= V4L2_FIELD_BOTTOM
,
204 .bytesperline
= 720 * 2,
205 .sizeimage
= 720 * 2 * 240,
206 .colorspace
= V4L2_COLORSPACE_SMPTE170M
},
210 * buf_setup - Get size and number of video buffer
211 * @vq: queue in videobuf
212 * @count: Number of buffers (1..MAX_FRAMES).
213 * 0 use default value.
214 * @size: size of buffer in bytes
216 * returns size and number of buffers
217 * a preset value of 0 returns the default number.
218 * return value: 0, always succesfull.
220 static int buf_setup(struct videobuf_queue
*vq
, unsigned int *count
,
223 struct sta2x11_vip
*vip
= vq
->priv_data
;
225 *size
= vip
->format
.width
* vip
->format
.height
* 2;
226 if (0 == *count
|| MAX_FRAMES
< *count
)
232 * buf_prepare - prepare buffer for usage
233 * @vq: queue in videobuf layer
234 * @vb: buffer to be prepared
235 * @field: type of video data (interlaced/non-interlaced)
237 * Allocate or realloc buffer
238 * return value: 0, successful.
240 * -EINVAL, supplied buffer is too small.
242 * other, buffer could not be locked.
244 static int buf_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
245 enum v4l2_field field
)
247 struct sta2x11_vip
*vip
= vq
->priv_data
;
250 vb
->size
= vip
->format
.width
* vip
->format
.height
* 2;
251 if ((0 != vb
->baddr
) && (vb
->bsize
< vb
->size
))
253 vb
->width
= vip
->format
.width
;
254 vb
->height
= vip
->format
.height
;
257 if (VIDEOBUF_NEEDS_INIT
== vb
->state
) {
258 ret
= videobuf_iolock(vq
, vb
, NULL
);
262 vb
->state
= VIDEOBUF_PREPARED
;
265 videobuf_dma_contig_free(vq
, vb
);
266 vb
->state
= VIDEOBUF_NEEDS_INIT
;
271 * buf_queu - queue buffer for filling
272 * @vq: queue in videobuf layer
273 * @vb: buffer to be queued
275 * if capturing is already running, the buffer will be queued. Otherwise
276 * capture is started and the buffer is used directly.
278 static void buf_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
280 struct sta2x11_vip
*vip
= vq
->priv_data
;
283 vb
->state
= VIDEOBUF_QUEUED
;
286 list_add_tail(&vb
->queue
, &vip
->capture
);
294 vb
->state
= VIDEOBUF_ACTIVE
;
296 dma
= videobuf_to_dma_contig(vb
);
298 REG_WRITE(vip
, DVP_TFO
, (0 << 16) | (0));
299 /* despite of interlace mode, upper and lower frames start at zero */
300 REG_WRITE(vip
, DVP_BFO
, (0 << 16) | (0));
302 switch (vip
->format
.field
) {
303 case V4L2_FIELD_INTERLACED
:
304 REG_WRITE(vip
, DVP_TFS
,
305 ((vip
->format
.height
/ 2 - 1) << 16) |
306 (2 * vip
->format
.width
- 1));
307 REG_WRITE(vip
, DVP_BFS
, ((vip
->format
.height
/ 2 - 1) << 16) |
308 (2 * vip
->format
.width
- 1));
309 REG_WRITE(vip
, DVP_VTP
, dma
);
310 REG_WRITE(vip
, DVP_VBP
, dma
+ vip
->format
.width
* 2);
311 REG_WRITE(vip
, DVP_VMP
, 4 * vip
->format
.width
);
314 REG_WRITE(vip
, DVP_TFS
,
315 ((vip
->format
.height
- 1) << 16) |
316 (2 * vip
->format
.width
- 1));
317 REG_WRITE(vip
, DVP_BFS
, ((0) << 16) |
318 (2 * vip
->format
.width
- 1));
319 REG_WRITE(vip
, DVP_VTP
, dma
);
320 REG_WRITE(vip
, DVP_VBP
, dma
);
321 REG_WRITE(vip
, DVP_VMP
, 2 * vip
->format
.width
);
323 case V4L2_FIELD_BOTTOM
:
324 REG_WRITE(vip
, DVP_TFS
, ((0) << 16) |
325 (2 * vip
->format
.width
- 1));
326 REG_WRITE(vip
, DVP_BFS
,
327 ((vip
->format
.height
) << 16) |
328 (2 * vip
->format
.width
- 1));
329 REG_WRITE(vip
, DVP_VTP
, dma
);
330 REG_WRITE(vip
, DVP_VBP
, dma
);
331 REG_WRITE(vip
, DVP_VMP
, 2 * vip
->format
.width
);
335 pr_warning("VIP: unknown field format\n");
339 REG_WRITE(vip
, DVP_CTL
, DVP_CTL_ENA
);
343 * buff_release - release buffer
344 * @vq: queue in videobuf layer
345 * @vb: buffer to be released
347 * release buffer in videobuf layer
349 static void buf_release(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
352 videobuf_dma_contig_free(vq
, vb
);
353 vb
->state
= VIDEOBUF_NEEDS_INIT
;
356 static struct videobuf_queue_ops vip_qops
= {
357 .buf_setup
= buf_setup
,
358 .buf_prepare
= buf_prepare
,
359 .buf_queue
= buf_queue
,
360 .buf_release
= buf_release
,
364 * vip_open - open video device
365 * @file: descriptor of device
367 * open device, make sure it is only opened once.
368 * return value: 0, no error.
370 * -EBUSY, device is already opened
372 * -ENOMEM, no memory for auxiliary DMA buffer
374 static int vip_open(struct file
*file
)
376 struct video_device
*dev
= video_devdata(file
);
377 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
379 mutex_lock(&vip
->mutex
);
382 if (vip
->users
> 1) {
384 mutex_unlock(&vip
->mutex
);
388 file
->private_data
= dev
;
394 INIT_LIST_HEAD(&vip
->capture
);
395 vip
->mem_spare
= dma_alloc_coherent(&vip
->pdev
->dev
, 64,
396 &vip
->dma_spare
, GFP_KERNEL
);
397 if (!vip
->mem_spare
) {
399 mutex_unlock(&vip
->mutex
);
403 mutex_unlock(&vip
->mutex
);
404 videobuf_queue_dma_contig_init_cached(&vip
->vb_vidq
,
408 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
409 V4L2_FIELD_INTERLACED
,
410 sizeof(struct videobuf_buffer
),
412 REG_READ(vip
, DVP_ITS
);
413 REG_WRITE(vip
, DVP_HLFLN
, DVP_HLFLN_SD
);
414 REG_WRITE(vip
, DVP_ITM
, DVP_IT_VSB
| DVP_IT_VST
);
415 REG_WRITE(vip
, DVP_CTL
, DVP_CTL_RST
);
416 REG_WRITE(vip
, DVP_CTL
, 0);
417 REG_READ(vip
, DVP_ITS
);
422 * vip_close - close video device
423 * @file: descriptor of device
425 * close video device, wait until all pending operations are finished
426 * ( maximum FRAME_MAX buffers pending )
427 * Turn off interrupts.
429 * return value: 0, always succesful.
431 static int vip_close(struct file
*file
)
433 struct video_device
*dev
= video_devdata(file
);
434 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
438 videobuf_waiton(&vip
->vb_vidq
, vip
->active
, 0, 0);
439 spin_lock_irq(&vip
->slock
);
441 REG_WRITE(vip
, DVP_ITM
, 0);
442 REG_WRITE(vip
, DVP_CTL
, DVP_CTL_RST
);
443 REG_WRITE(vip
, DVP_CTL
, 0);
444 REG_READ(vip
, DVP_ITS
);
449 spin_unlock_irq(&vip
->slock
);
451 videobuf_stop(&vip
->vb_vidq
);
452 videobuf_mmap_free(&vip
->vb_vidq
);
454 dma_free_coherent(&vip
->pdev
->dev
, 64, vip
->mem_spare
, vip
->dma_spare
);
455 file
->private_data
= NULL
;
456 mutex_lock(&vip
->mutex
);
458 mutex_unlock(&vip
->mutex
);
463 * vip_read - read from video input
464 * @file: descriptor of device
466 * @count: number of bytes to be read
467 * @ppos: position within stream
469 * read video data from video device.
470 * handling is done in generic videobuf layer
471 * return value: provided by videobuf layer
473 static ssize_t
vip_read(struct file
*file
, char __user
*data
,
474 size_t count
, loff_t
*ppos
)
476 struct video_device
*dev
= file
->private_data
;
477 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
479 return videobuf_read_stream(&vip
->vb_vidq
, data
, count
, ppos
, 0,
480 file
->f_flags
& O_NONBLOCK
);
484 * vip_mmap - map user buffer
485 * @file: descriptor of device
488 * map user space buffer into kernel mode, including DMA address.
489 * handling is done in generic videobuf layer.
490 * return value: provided by videobuf layer
492 static int vip_mmap(struct file
*file
, struct vm_area_struct
*vma
)
494 struct video_device
*dev
= file
->private_data
;
495 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
497 return videobuf_mmap_mapper(&vip
->vb_vidq
, vma
);
501 * vip_poll - poll for event
502 * @file: descriptor of device
503 * @wait: contains events to be waited for
505 * wait for event related to video device.
506 * handling is done in generic videobuf layer.
507 * return value: provided by videobuf layer
509 static unsigned int vip_poll(struct file
*file
, struct poll_table_struct
*wait
)
511 struct video_device
*dev
= file
->private_data
;
512 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
514 return videobuf_poll_stream(file
, &vip
->vb_vidq
, wait
);
518 * vidioc_querycap - return capabilities of device
519 * @file: descriptor of device (not used)
520 * @priv: points to current videodevice
521 * @cap: contains return values
523 * the capabilities of the device are returned
525 * return value: 0, no error.
527 static int vidioc_querycap(struct file
*file
, void *priv
,
528 struct v4l2_capability
*cap
)
530 struct video_device
*dev
= priv
;
531 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
533 memset(cap
, 0, sizeof(struct v4l2_capability
));
534 strcpy(cap
->driver
, DRV_NAME
);
535 strcpy(cap
->card
, DRV_NAME
);
537 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
), "PCI:%s",
538 pci_name(vip
->pdev
));
539 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
|
546 * vidioc_s_std - set video standard
547 * @file: descriptor of device (not used)
548 * @priv: points to current videodevice
549 * @std: contains standard to be set
551 * the video standard is set
553 * return value: 0, no error.
555 * -EIO, no input signal detected
557 * other, returned from video DAC.
559 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
561 struct video_device
*dev
= priv
;
562 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
563 v4l2_std_id oldstd
= vip
->std
, newstd
;
566 if (V4L2_STD_ALL
== *std
) {
567 v4l2_subdev_call(vip
->decoder
, core
, s_std
, *std
);
569 v4l2_subdev_call(vip
->decoder
, video
, querystd
, &newstd
);
570 v4l2_subdev_call(vip
->decoder
, video
, g_input_status
, &status
);
571 if (status
& V4L2_IN_ST_NO_SIGNAL
)
573 *std
= vip
->std
= newstd
;
574 if (oldstd
!= *std
) {
575 if (V4L2_STD_525_60
& (*std
))
576 vip
->format
= formats_60
[0];
578 vip
->format
= formats_50
[0];
583 if (oldstd
!= *std
) {
584 if (V4L2_STD_525_60
& (*std
))
585 vip
->format
= formats_60
[0];
587 vip
->format
= formats_50
[0];
590 return v4l2_subdev_call(vip
->decoder
, core
, s_std
, *std
);
594 * vidioc_g_std - get video standard
595 * @file: descriptor of device (not used)
596 * @priv: points to current videodevice
597 * @std: contains return values
599 * the current video standard is returned
601 * return value: 0, no error.
603 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
605 struct video_device
*dev
= priv
;
606 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
613 * vidioc_querystd - get possible video standards
614 * @file: descriptor of device (not used)
615 * @priv: points to current videodevice
616 * @std: contains return values
618 * all possible video standards are returned
620 * return value: delivered by video DAC routine.
622 static int vidioc_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std
)
624 struct video_device
*dev
= priv
;
625 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
627 return v4l2_subdev_call(vip
->decoder
, video
, querystd
, std
);
632 * vidioc_queryctl - get possible control settings
633 * @file: descriptor of device (not used)
634 * @priv: points to current videodevice
635 * @ctrl: contains return values
637 * return possible values for a control
638 * return value: delivered by video DAC routine.
640 static int vidioc_queryctrl(struct file
*file
, void *priv
,
641 struct v4l2_queryctrl
*ctrl
)
643 struct video_device
*dev
= priv
;
644 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
646 return v4l2_subdev_call(vip
->decoder
, core
, queryctrl
, ctrl
);
650 * vidioc_g_ctl - get control value
651 * @file: descriptor of device (not used)
652 * @priv: points to current videodevice
653 * @ctrl: contains return values
655 * return setting for a control value
656 * return value: delivered by video DAC routine.
658 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
659 struct v4l2_control
*ctrl
)
661 struct video_device
*dev
= priv
;
662 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
664 return v4l2_subdev_call(vip
->decoder
, core
, g_ctrl
, ctrl
);
668 * vidioc_s_ctl - set control value
669 * @file: descriptor of device (not used)
670 * @priv: points to current videodevice
671 * @ctrl: contains value to be set
673 * set value for a specific control
674 * return value: delivered by video DAC routine.
676 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
677 struct v4l2_control
*ctrl
)
679 struct video_device
*dev
= priv
;
680 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
682 return v4l2_subdev_call(vip
->decoder
, core
, s_ctrl
, ctrl
);
686 * vidioc_enum_input - return name of input line
687 * @file: descriptor of device (not used)
688 * @priv: points to current videodevice
689 * @inp: contains return values
691 * the user friendly name of the input line is returned
693 * return value: 0, no error.
695 * -EINVAL, input line number out of range
697 static int vidioc_enum_input(struct file
*file
, void *priv
,
698 struct v4l2_input
*inp
)
703 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
704 inp
->std
= V4L2_STD_ALL
;
705 sprintf(inp
->name
, "Camera %u", inp
->index
);
711 * vidioc_s_input - set input line
712 * @file: descriptor of device ( not used)
713 * @priv: points to current videodevice
714 * @i: new input line number
716 * the current active input line is set
718 * return value: 0, no error.
720 * -EINVAL, line number out of range
722 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
724 struct video_device
*dev
= priv
;
725 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
730 ret
= v4l2_subdev_call(vip
->decoder
, video
, s_routing
, i
, 0, 0);
739 * vidioc_g_input - return input line
740 * @file: descriptor of device ( not used)
741 * @priv: points to current videodevice
742 * @i: returned input line number
744 * the current active input line is returned
746 * return value: always 0.
748 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
750 struct video_device
*dev
= priv
;
751 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
758 * vidioc_enum_fmt_vid_cap - return video capture format
759 * @file: descriptor of device ( not used)
760 * @priv: points to current videodevice
761 * @f: returned format information
763 * returns name and format of video capture
764 * Only UYVY is supported by hardware.
766 * return value: always 0.
768 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
769 struct v4l2_fmtdesc
*f
)
775 strcpy(f
->description
, "4:2:2, packed, UYVY");
776 f
->pixelformat
= V4L2_PIX_FMT_UYVY
;
782 * vidioc_try_fmt_vid_cap - set video capture format
783 * @file: descriptor of device ( not used)
784 * @priv: points to current videodevice
787 * new video format is set which includes width and
788 * field type. width is fixed to 720, no scaling.
789 * Only UYVY is supported by this hardware.
790 * the minimum height is 200, the maximum is 576 (PAL)
792 * return value: 0, no error
794 * -EINVAL, pixel or field format not supported
797 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
798 struct v4l2_format
*f
)
800 struct video_device
*dev
= priv
;
801 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
804 if (V4L2_PIX_FMT_UYVY
!= f
->fmt
.pix
.pixelformat
)
807 if (V4L2_STD_525_60
& vip
->std
)
812 switch (f
->fmt
.pix
.field
) {
814 if (interlace_lim
< f
->fmt
.pix
.height
)
815 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
817 f
->fmt
.pix
.field
= V4L2_FIELD_BOTTOM
;
820 case V4L2_FIELD_BOTTOM
:
821 if (interlace_lim
< f
->fmt
.pix
.height
)
822 f
->fmt
.pix
.height
= interlace_lim
;
824 case V4L2_FIELD_INTERLACED
:
830 f
->fmt
.pix
.height
&= ~1;
831 if (2 * interlace_lim
< f
->fmt
.pix
.height
)
832 f
->fmt
.pix
.height
= 2 * interlace_lim
;
833 if (200 > f
->fmt
.pix
.height
)
834 f
->fmt
.pix
.height
= 200;
835 f
->fmt
.pix
.width
= 720;
836 f
->fmt
.pix
.bytesperline
= f
->fmt
.pix
.width
* 2;
837 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.width
* 2 * f
->fmt
.pix
.height
;
838 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
844 * vidioc_s_fmt_vid_cap - set current video format parameters
845 * @file: descriptor of device ( not used)
846 * @priv: points to current videodevice
847 * @f: returned format information
849 * set new capture format
850 * return value: 0, no error
852 * other, delivered by video DAC routine.
854 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
855 struct v4l2_format
*f
)
857 struct video_device
*dev
= priv
;
858 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
861 ret
= vidioc_try_fmt_vid_cap(file
, priv
, f
);
865 memcpy(&vip
->format
, &f
->fmt
.pix
, sizeof(struct v4l2_pix_format
));
870 * vidioc_g_fmt_vid_cap - get current video format parameters
871 * @file: descriptor of device ( not used)
872 * @priv: points to current videodevice
873 * @f: contains format information
875 * returns current video format parameters
877 * return value: 0, always successful
879 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
880 struct v4l2_format
*f
)
882 struct video_device
*dev
= priv
;
883 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
885 memcpy(&f
->fmt
.pix
, &vip
->format
, sizeof(struct v4l2_pix_format
));
890 * vidioc_reqfs - request buffer
891 * @file: descriptor of device ( not used)
892 * @priv: points to current videodevice
895 * Handling is done in generic videobuf layer.
897 static int vidioc_reqbufs(struct file
*file
, void *priv
,
898 struct v4l2_requestbuffers
*p
)
900 struct video_device
*dev
= priv
;
901 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
903 return videobuf_reqbufs(&vip
->vb_vidq
, p
);
907 * vidioc_querybuf - query buffer
908 * @file: descriptor of device ( not used)
909 * @priv: points to current videodevice
912 * query buffer state.
913 * Handling is done in generic videobuf layer.
915 static int vidioc_querybuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
917 struct video_device
*dev
= priv
;
918 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
920 return videobuf_querybuf(&vip
->vb_vidq
, p
);
924 * vidioc_qbuf - queue a buffer
925 * @file: descriptor of device ( not used)
926 * @priv: points to current videodevice
929 * Handling is done in generic videobuf layer.
931 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
933 struct video_device
*dev
= priv
;
934 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
936 return videobuf_qbuf(&vip
->vb_vidq
, p
);
940 * vidioc_dqbuf - dequeue a buffer
941 * @file: descriptor of device ( not used)
942 * @priv: points to current videodevice
945 * Handling is done in generic videobuf layer.
947 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
949 struct video_device
*dev
= priv
;
950 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
952 return videobuf_dqbuf(&vip
->vb_vidq
, p
, file
->f_flags
& O_NONBLOCK
);
956 * vidioc_streamon - turn on streaming
957 * @file: descriptor of device ( not used)
958 * @priv: points to current videodevice
959 * @type: type of capture
962 * Handling is done in generic videobuf layer.
964 static int vidioc_streamon(struct file
*file
, void *priv
,
965 enum v4l2_buf_type type
)
967 struct video_device
*dev
= priv
;
968 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
970 return videobuf_streamon(&vip
->vb_vidq
);
974 * vidioc_streamoff - turn off streaming
975 * @file: descriptor of device ( not used)
976 * @priv: points to current videodevice
977 * @type: type of capture
979 * turn off streaming.
980 * Handling is done in generic videobuf layer.
982 static int vidioc_streamoff(struct file
*file
, void *priv
,
983 enum v4l2_buf_type type
)
985 struct video_device
*dev
= priv
;
986 struct sta2x11_vip
*vip
= video_get_drvdata(dev
);
988 return videobuf_streamoff(&vip
->vb_vidq
);
991 static const struct v4l2_file_operations vip_fops
= {
992 .owner
= THIS_MODULE
,
994 .release
= vip_close
,
995 .ioctl
= video_ioctl2
,
1001 static const struct v4l2_ioctl_ops vip_ioctl_ops
= {
1002 .vidioc_querycap
= vidioc_querycap
,
1003 .vidioc_s_std
= vidioc_s_std
,
1004 .vidioc_g_std
= vidioc_g_std
,
1005 .vidioc_querystd
= vidioc_querystd
,
1006 .vidioc_queryctrl
= vidioc_queryctrl
,
1007 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1008 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1009 .vidioc_enum_input
= vidioc_enum_input
,
1010 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1011 .vidioc_s_input
= vidioc_s_input
,
1012 .vidioc_g_input
= vidioc_g_input
,
1013 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1014 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1015 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1016 .vidioc_reqbufs
= vidioc_reqbufs
,
1017 .vidioc_querybuf
= vidioc_querybuf
,
1018 .vidioc_qbuf
= vidioc_qbuf
,
1019 .vidioc_dqbuf
= vidioc_dqbuf
,
1020 .vidioc_streamon
= vidioc_streamon
,
1021 .vidioc_streamoff
= vidioc_streamoff
,
1024 static struct video_device video_dev_template
= {
1026 .release
= video_device_release
,
1028 .ioctl_ops
= &vip_ioctl_ops
,
1029 .tvnorms
= V4L2_STD_ALL
,
1033 * vip_irq - interrupt routine
1034 * @irq: Number of interrupt ( not used, correct number is assumed )
1035 * @vip: local data structure containing all information
1037 * check for both frame interrupts set ( top and bottom ).
1038 * check FIFO overflow, but limit number of log messages after open.
1039 * signal a complete buffer if done.
1040 * dequeue a new buffer if available.
1041 * disable VIP if no buffer available.
1043 * return value: IRQ_NONE, interrupt was not generated by VIP
1045 * IRQ_HANDLED, interrupt done.
1047 static irqreturn_t
vip_irq(int irq
, struct sta2x11_vip
*vip
)
1050 unsigned long flags
;
1051 struct videobuf_buffer
*vb
;
1053 status
= REG_READ(vip
, DVP_ITS
);
1056 pr_debug("VIP: irq ignored\n");
1063 if (status
& DVP_IT_VSB
)
1066 if (status
& DVP_IT_VST
)
1069 if ((DVP_IT_VSB
| DVP_IT_VST
) == (status
& (DVP_IT_VST
| DVP_IT_VSB
))) {
1070 /* this is bad, we are too slow, hope the condition is gone
1071 * on the next frame */
1072 pr_info("VIP: both irqs\n");
1076 if (status
& DVP_IT_FIFO
) {
1077 if (5 > vip
->overflow
++)
1078 pr_info("VIP: fifo overflow\n");
1081 if (2 > vip
->tcount
)
1084 if (status
& DVP_IT_VSB
)
1087 spin_lock_irqsave(&vip
->slock
, flags
);
1089 REG_WRITE(vip
, DVP_CTL
, REG_READ(vip
, DVP_CTL
) & ~DVP_CTL_ENA
);
1091 do_gettimeofday(&vip
->active
->ts
);
1092 vip
->active
->field_count
++;
1093 vip
->active
->state
= VIDEOBUF_DONE
;
1094 wake_up(&vip
->active
->done
);
1097 if (!vip
->closing
) {
1098 if (list_empty(&vip
->capture
))
1101 vb
= list_first_entry(&vip
->capture
, struct videobuf_buffer
,
1104 pr_info("VIP: no buffer\n");
1107 vb
->state
= VIDEOBUF_ACTIVE
;
1108 list_del(&vb
->queue
);
1110 dma
= videobuf_to_dma_contig(vb
);
1111 switch (vip
->format
.field
) {
1112 case V4L2_FIELD_INTERLACED
:
1113 REG_WRITE(vip
, DVP_VTP
, dma
);
1114 REG_WRITE(vip
, DVP_VBP
, dma
+ vip
->format
.width
* 2);
1116 case V4L2_FIELD_TOP
:
1117 case V4L2_FIELD_BOTTOM
:
1118 REG_WRITE(vip
, DVP_VTP
, dma
);
1119 REG_WRITE(vip
, DVP_VBP
, dma
);
1122 pr_warning("VIP: unknown field format\n");
1126 REG_WRITE(vip
, DVP_CTL
, REG_READ(vip
, DVP_CTL
) | DVP_CTL_ENA
);
1129 spin_unlock_irqrestore(&vip
->slock
, flags
);
1134 * vip_gpio_reserve - reserve gpio pin
1136 * @pin: GPIO pin number
1137 * @dir: direction, input or output
1138 * @name: GPIO pin name
1141 static int vip_gpio_reserve(struct device
*dev
, int pin
, int dir
,
1149 ret
= gpio_request(pin
, name
);
1151 dev_err(dev
, "Failed to allocate pin %d (%s)\n", pin
, name
);
1155 ret
= gpio_direction_output(pin
, dir
);
1157 dev_err(dev
, "Failed to set direction for pin %d (%s)\n",
1163 ret
= gpio_export(pin
, false);
1165 dev_err(dev
, "Failed to export pin %d (%s)\n", pin
, name
);
1174 * vip_gpio_release - release gpio pin
1176 * @pin: GPIO pin number
1177 * @name: GPIO pin name
1180 static void vip_gpio_release(struct device
*dev
, int pin
, const char *name
)
1183 dev_dbg(dev
, "releasing pin %d (%s)\n", pin
, name
);
1190 * sta2x11_vip_init_one - init one instance of video device
1194 * allocate reset pins for DAC.
1195 * Reset video DAC, this is done via reset line.
1196 * allocate memory for managing device
1200 * find and initialize video DAC
1202 * return value: 0, no error
1204 * -ENOMEM, no memory
1206 * -ENODEV, device could not be detected or registered
1208 static int __devinit
sta2x11_vip_init_one(struct pci_dev
*pdev
,
1209 const struct pci_device_id
*ent
)
1212 struct sta2x11_vip
*vip
;
1213 struct vip_config
*config
;
1215 ret
= pci_enable_device(pdev
);
1219 config
= dev_get_platdata(&pdev
->dev
);
1221 dev_info(&pdev
->dev
, "VIP slot disabled\n");
1226 ret
= vip_gpio_reserve(&pdev
->dev
, config
->pwr_pin
, 0,
1231 if (config
->reset_pin
>= 0) {
1232 ret
= vip_gpio_reserve(&pdev
->dev
, config
->reset_pin
, 0,
1233 config
->reset_name
);
1235 vip_gpio_release(&pdev
->dev
, config
->pwr_pin
,
1241 if (config
->pwr_pin
!= -1) {
1242 /* Datasheet says 5ms between PWR and RST */
1243 usleep_range(5000, 25000);
1244 ret
= gpio_direction_output(config
->pwr_pin
, 1);
1247 if (config
->reset_pin
!= -1) {
1248 /* Datasheet says 5ms between PWR and RST */
1249 usleep_range(5000, 25000);
1250 ret
= gpio_direction_output(config
->reset_pin
, 1);
1252 usleep_range(5000, 25000);
1254 vip
= kzalloc(sizeof(struct sta2x11_vip
), GFP_KERNEL
);
1261 vip
->std
= V4L2_STD_PAL
;
1262 vip
->format
= formats_50
[0];
1263 vip
->config
= config
;
1265 if (v4l2_device_register(&pdev
->dev
, &vip
->v4l2_dev
))
1268 dev_dbg(&pdev
->dev
, "BAR #0 at 0x%lx 0x%lx irq %d\n",
1269 (unsigned long)pci_resource_start(pdev
, 0),
1270 (unsigned long)pci_resource_len(pdev
, 0), pdev
->irq
);
1272 pci_set_master(pdev
);
1274 ret
= pci_request_regions(pdev
, DRV_NAME
);
1278 vip
->iomem
= pci_iomap(pdev
, 0, 0x100);
1280 ret
= -ENOMEM
; /* FIXME */
1284 pci_enable_msi(pdev
);
1286 INIT_LIST_HEAD(&vip
->capture
);
1287 spin_lock_init(&vip
->slock
);
1288 mutex_init(&vip
->mutex
);
1292 ret
= request_irq(pdev
->irq
,
1293 (irq_handler_t
) vip_irq
,
1294 IRQF_SHARED
, DRV_NAME
, vip
);
1296 dev_err(&pdev
->dev
, "request_irq failed\n");
1301 vip
->video_dev
= video_device_alloc();
1302 if (!vip
->video_dev
) {
1307 *(vip
->video_dev
) = video_dev_template
;
1308 video_set_drvdata(vip
->video_dev
, vip
);
1310 ret
= video_register_device(vip
->video_dev
, VFL_TYPE_GRABBER
, -1);
1314 vip
->adapter
= i2c_get_adapter(vip
->config
->i2c_id
);
1315 if (!vip
->adapter
) {
1317 dev_err(&pdev
->dev
, "no I2C adapter found\n");
1321 vip
->decoder
= v4l2_i2c_new_subdev(&vip
->v4l2_dev
, vip
->adapter
,
1322 "adv7180", vip
->config
->i2c_addr
,
1324 if (!vip
->decoder
) {
1326 dev_err(&pdev
->dev
, "no decoder found\n");
1330 i2c_put_adapter(vip
->adapter
);
1332 v4l2_subdev_call(vip
->decoder
, core
, init
, 0);
1334 pr_info("STA2X11 Video Input Port (VIP) loaded\n");
1338 video_set_drvdata(vip
->video_dev
, NULL
);
1340 if (video_is_registered(vip
->video_dev
))
1341 video_unregister_device(vip
->video_dev
);
1343 video_device_release(vip
->video_dev
);
1345 free_irq(pdev
->irq
, vip
);
1346 pci_disable_msi(pdev
);
1348 pci_iounmap(pdev
, vip
->iomem
);
1349 mutex_destroy(&vip
->mutex
);
1351 pci_release_regions(pdev
);
1353 v4l2_device_unregister(&vip
->v4l2_dev
);
1357 vip_gpio_release(&pdev
->dev
, config
->reset_pin
, config
->reset_name
);
1358 vip_gpio_release(&pdev
->dev
, config
->pwr_pin
, config
->pwr_name
);
1361 * do not call pci_disable_device on sta2x11 because it break all
1362 * other Bus masters on this EP
1368 * sta2x11_vip_remove_one - release device
1371 * Undo everything done in .._init_one
1373 * unregister video device
1379 static void __devexit
sta2x11_vip_remove_one(struct pci_dev
*pdev
)
1381 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pdev
);
1382 struct sta2x11_vip
*vip
=
1383 container_of(v4l2_dev
, struct sta2x11_vip
, v4l2_dev
);
1385 video_set_drvdata(vip
->video_dev
, NULL
);
1386 video_unregister_device(vip
->video_dev
);
1387 /*do not call video_device_release() here, is already done */
1388 free_irq(pdev
->irq
, vip
);
1389 pci_disable_msi(pdev
);
1390 pci_iounmap(pdev
, vip
->iomem
);
1391 pci_release_regions(pdev
);
1393 v4l2_device_unregister(&vip
->v4l2_dev
);
1394 mutex_destroy(&vip
->mutex
);
1396 vip_gpio_release(&pdev
->dev
, vip
->config
->pwr_pin
,
1397 vip
->config
->pwr_name
);
1398 vip_gpio_release(&pdev
->dev
, vip
->config
->reset_pin
,
1399 vip
->config
->reset_name
);
1403 * do not call pci_disable_device on sta2x11 because it break all
1404 * other Bus masters on this EP
1411 * sta2x11_vip_suspend - set device into power save mode
1413 * @state: new state of device
1415 * all relevant registers are saved and an attempt to set a new state is made.
1417 * return value: 0 always indicate success,
1418 * even if device could not be disabled. (workaround for hardware problem)
1420 * reurn value : 0, always succesful, even if hardware does not not support
1423 static int sta2x11_vip_suspend(struct pci_dev
*pdev
, pm_message_t state
)
1425 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pdev
);
1426 struct sta2x11_vip
*vip
=
1427 container_of(v4l2_dev
, struct sta2x11_vip
, v4l2_dev
);
1428 unsigned long flags
;
1431 spin_lock_irqsave(&vip
->slock
, flags
);
1432 vip
->register_save_area
[0] = REG_READ(vip
, DVP_CTL
);
1433 REG_WRITE(vip
, DVP_CTL
, vip
->register_save_area
[0] & DVP_CTL_DIS
);
1434 vip
->register_save_area
[SAVE_COUNT
] = REG_READ(vip
, DVP_ITM
);
1435 REG_WRITE(vip
, DVP_ITM
, 0);
1436 for (i
= 1; i
< SAVE_COUNT
; i
++)
1437 vip
->register_save_area
[i
] = REG_READ(vip
, 4 * i
);
1438 for (i
= 0; i
< AUX_COUNT
; i
++)
1439 vip
->register_save_area
[SAVE_COUNT
+ IRQ_COUNT
+ i
] =
1440 REG_READ(vip
, registers_to_save
[i
]);
1441 spin_unlock_irqrestore(&vip
->slock
, flags
);
1442 /* save pci state */
1443 pci_save_state(pdev
);
1444 if (pci_set_power_state(pdev
, pci_choose_state(pdev
, state
))) {
1446 * do not call pci_disable_device on sta2x11 because it
1447 * break all other Bus masters on this EP
1452 pr_info("VIP: suspend\n");
1457 * sta2x11_vip_resume - resume device operation
1458 * @pdev : PCI device
1460 * re-enable device, set PCI state to powered and restore registers.
1461 * resume normal device operation afterwards.
1463 * return value: 0, no error.
1465 * other, could not set device to power on state.
1467 static int sta2x11_vip_resume(struct pci_dev
*pdev
)
1469 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pdev
);
1470 struct sta2x11_vip
*vip
=
1471 container_of(v4l2_dev
, struct sta2x11_vip
, v4l2_dev
);
1472 unsigned long flags
;
1475 pr_info("VIP: resume\n");
1476 /* restore pci state */
1477 if (vip
->disabled
) {
1478 ret
= pci_enable_device(pdev
);
1480 pr_warning("VIP: Can't enable device.\n");
1485 ret
= pci_set_power_state(pdev
, PCI_D0
);
1488 * do not call pci_disable_device on sta2x11 because it
1489 * break all other Bus masters on this EP
1491 pr_warning("VIP: Can't enable device.\n");
1496 pci_restore_state(pdev
);
1498 spin_lock_irqsave(&vip
->slock
, flags
);
1499 for (i
= 1; i
< SAVE_COUNT
; i
++)
1500 REG_WRITE(vip
, 4 * i
, vip
->register_save_area
[i
]);
1501 for (i
= 0; i
< AUX_COUNT
; i
++)
1502 REG_WRITE(vip
, registers_to_save
[i
],
1503 vip
->register_save_area
[SAVE_COUNT
+ IRQ_COUNT
+ i
]);
1504 REG_WRITE(vip
, DVP_CTL
, vip
->register_save_area
[0]);
1505 REG_WRITE(vip
, DVP_ITM
, vip
->register_save_area
[SAVE_COUNT
]);
1506 spin_unlock_irqrestore(&vip
->slock
, flags
);
1512 static DEFINE_PCI_DEVICE_TABLE(sta2x11_vip_pci_tbl
) = {
1513 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO
, PCI_DEVICE_ID_STMICRO_VIP
)},
1517 static struct pci_driver sta2x11_vip_driver
= {
1519 .probe
= sta2x11_vip_init_one
,
1520 .remove
= __devexit_p(sta2x11_vip_remove_one
),
1521 .id_table
= sta2x11_vip_pci_tbl
,
1523 .suspend
= sta2x11_vip_suspend
,
1524 .resume
= sta2x11_vip_resume
,
1528 static int __init
sta2x11_vip_init_module(void)
1530 return pci_register_driver(&sta2x11_vip_driver
);
1533 static void __exit
sta2x11_vip_exit_module(void)
1535 pci_unregister_driver(&sta2x11_vip_driver
);
1539 module_init(sta2x11_vip_init_module
);
1540 module_exit(sta2x11_vip_exit_module
);
1542 late_initcall_sync(sta2x11_vip_init_module
);
1545 MODULE_DESCRIPTION("STA2X11 Video Input Port driver");
1546 MODULE_AUTHOR("Wind River");
1547 MODULE_LICENSE("GPL v2");
1548 MODULE_SUPPORTED_DEVICE("sta2x11 video input");
1549 MODULE_VERSION(DRV_VERSION
);
1550 MODULE_DEVICE_TABLE(pci
, sta2x11_vip_pci_tbl
);