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.h>
22 #include <linux/i2c.h>
23 #include <linux/delay.h>
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-ioctl.h>
28 #include <media/v4l2-fh.h>
29 #include <media/v4l2-event.h>
30 #include <media/videobuf2-dma-contig.h>
32 #include "sta2x11_vip.h"
34 #define DRV_VERSION "1.3"
36 #ifndef PCI_DEVICE_ID_STMICRO_VIP
37 #define PCI_DEVICE_ID_STMICRO_VIP 0xCC0D
54 #define DVP_HLFLN 0xA8
59 #define DVP_CTL_ENA 0x00000001
60 #define DVP_CTL_RST 0x80000000
61 #define DVP_CTL_DIS (~0x00040001)
63 #define DVP_IT_VSB 0x00000008
64 #define DVP_IT_VST 0x00000010
65 #define DVP_IT_FIFO 0x00000020
67 #define DVP_HLFLN_SD 0x00000001
75 struct vb2_v4l2_buffer vb;
76 struct list_head list;
79 static inline struct vip_buffer *to_vip_buffer(struct vb2_v4l2_buffer *vb2)
81 return container_of(vb2, struct vip_buffer, vb);
85 * struct sta2x11_vip - All internal data for one instance of device
86 * @v4l2_dev: device registered in v4l layer
87 * @video_dev: properties of our device
89 * @adapter: contains I2C adapter information
90 * @register_save_area: All relevant register are saved here during suspend
91 * @decoder: contains information about video DAC
92 * @ctrl_hdl: handler for control framework
93 * @format: pixel format, fixed UYVY
94 * @std: video standard (e.g. PAL/NTSC)
95 * @input: input line for video signal ( 0 or 1 )
96 * @disabled: Device is in power down state
97 * @slock: for excluse access of registers
98 * @vb_vidq: queue maintained by videobuf2 layer
99 * @buffer_list: list of buffer in use
100 * @sequence: sequence number of acquired buffer
101 * @active: current active buffer
102 * @lock: used in videobuf2 callback
103 * @v4l_lock: serialize its video4linux ioctls
104 * @tcount: Number of top frames
105 * @bcount: Number of bottom frames
106 * @overflow: Number of FIFO overflows
107 * @iomem: hardware base address
108 * @config: I2C and gpio config from platform
110 * All non-local data is accessed via this structure.
113 struct v4l2_device v4l2_dev;
114 struct video_device video_dev;
115 struct pci_dev *pdev;
116 struct i2c_adapter *adapter;
117 unsigned int register_save_area[IRQ_COUNT + SAVE_COUNT + AUX_COUNT];
118 struct v4l2_subdev *decoder;
119 struct v4l2_ctrl_handler ctrl_hdl;
122 struct v4l2_pix_format format;
128 struct vb2_queue vb_vidq;
129 struct list_head buffer_list;
130 unsigned int sequence;
131 struct vip_buffer *active; /* current active buffer */
132 spinlock_t lock; /* Used in videobuf2 callback */
133 struct mutex v4l_lock;
135 /* Interrupt counters */
139 void __iomem *iomem; /* I/O Memory */
140 struct vip_config *config;
143 static const unsigned int registers_to_save[AUX_COUNT] = {
144 DVP_HLFLN, DVP_RGB, DVP_PKZ
147 static struct v4l2_pix_format formats_50[] = {
148 { /*PAL interlaced */
151 .pixelformat = V4L2_PIX_FMT_UYVY,
152 .field = V4L2_FIELD_INTERLACED,
153 .bytesperline = 720 * 2,
154 .sizeimage = 720 * 2 * 576,
155 .colorspace = V4L2_COLORSPACE_SMPTE170M},
159 .pixelformat = V4L2_PIX_FMT_UYVY,
160 .field = V4L2_FIELD_TOP,
161 .bytesperline = 720 * 2,
162 .sizeimage = 720 * 2 * 288,
163 .colorspace = V4L2_COLORSPACE_SMPTE170M},
167 .pixelformat = V4L2_PIX_FMT_UYVY,
168 .field = V4L2_FIELD_BOTTOM,
169 .bytesperline = 720 * 2,
170 .sizeimage = 720 * 2 * 288,
171 .colorspace = V4L2_COLORSPACE_SMPTE170M},
175 static struct v4l2_pix_format formats_60[] = {
176 { /*NTSC interlaced */
179 .pixelformat = V4L2_PIX_FMT_UYVY,
180 .field = V4L2_FIELD_INTERLACED,
181 .bytesperline = 720 * 2,
182 .sizeimage = 720 * 2 * 480,
183 .colorspace = V4L2_COLORSPACE_SMPTE170M},
187 .pixelformat = V4L2_PIX_FMT_UYVY,
188 .field = V4L2_FIELD_TOP,
189 .bytesperline = 720 * 2,
190 .sizeimage = 720 * 2 * 240,
191 .colorspace = V4L2_COLORSPACE_SMPTE170M},
195 .pixelformat = V4L2_PIX_FMT_UYVY,
196 .field = V4L2_FIELD_BOTTOM,
197 .bytesperline = 720 * 2,
198 .sizeimage = 720 * 2 * 240,
199 .colorspace = V4L2_COLORSPACE_SMPTE170M},
202 /* Write VIP register */
203 static inline void reg_write(struct sta2x11_vip *vip, unsigned int reg, u32 val)
205 iowrite32((val), (vip->iomem)+(reg));
207 /* Read VIP register */
208 static inline u32 reg_read(struct sta2x11_vip *vip, unsigned int reg)
210 return ioread32((vip->iomem)+(reg));
212 /* Start DMA acquisition */
213 static void start_dma(struct sta2x11_vip *vip, struct vip_buffer *vip_buf)
215 unsigned long offset = 0;
217 if (vip->format.field == V4L2_FIELD_INTERLACED)
218 offset = vip->format.width * 2;
220 spin_lock_irq(&vip->slock);
221 /* Enable acquisition */
222 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) | DVP_CTL_ENA);
223 /* Set Top and Bottom Field memory address */
224 reg_write(vip, DVP_VTP, (u32)vip_buf->dma);
225 reg_write(vip, DVP_VBP, (u32)vip_buf->dma + offset);
226 spin_unlock_irq(&vip->slock);
229 /* Fetch the next buffer to activate */
230 static void vip_active_buf_next(struct sta2x11_vip *vip)
232 /* Get the next buffer */
233 spin_lock(&vip->lock);
234 if (list_empty(&vip->buffer_list)) {/* No available buffer */
235 spin_unlock(&vip->lock);
238 vip->active = list_first_entry(&vip->buffer_list,
241 /* Reset Top and Bottom counter */
244 spin_unlock(&vip->lock);
245 if (vb2_is_streaming(&vip->vb_vidq)) { /* streaming is on */
246 start_dma(vip, vip->active); /* start dma capture */
251 /* Videobuf2 Operations */
252 static int queue_setup(struct vb2_queue *vq,
253 unsigned int *nbuffers, unsigned int *nplanes,
254 unsigned int sizes[], struct device *alloc_devs[])
256 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
258 if (!(*nbuffers) || *nbuffers < MAX_FRAMES)
259 *nbuffers = MAX_FRAMES;
262 sizes[0] = vip->format.sizeimage;
271 static int buffer_init(struct vb2_buffer *vb)
273 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
274 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
276 vip_buf->dma = vb2_dma_contig_plane_dma_addr(vb, 0);
277 INIT_LIST_HEAD(&vip_buf->list);
281 static int buffer_prepare(struct vb2_buffer *vb)
283 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
284 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
285 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
288 size = vip->format.sizeimage;
289 if (vb2_plane_size(vb, 0) < size) {
290 v4l2_err(&vip->v4l2_dev, "buffer too small (%lu < %lu)\n",
291 vb2_plane_size(vb, 0), size);
295 vb2_set_plane_payload(&vip_buf->vb.vb2_buf, 0, size);
299 static void buffer_queue(struct vb2_buffer *vb)
301 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
302 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
303 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
305 spin_lock(&vip->lock);
306 list_add_tail(&vip_buf->list, &vip->buffer_list);
307 if (!vip->active) { /* No active buffer, active the first one */
308 vip->active = list_first_entry(&vip->buffer_list,
311 if (vb2_is_streaming(&vip->vb_vidq)) /* streaming is on */
312 start_dma(vip, vip_buf); /* start dma capture */
314 spin_unlock(&vip->lock);
316 static void buffer_finish(struct vb2_buffer *vb)
318 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
319 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
320 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
322 /* Buffer handled, remove it from the list */
323 spin_lock(&vip->lock);
324 list_del_init(&vip_buf->list);
325 spin_unlock(&vip->lock);
327 if (vb2_is_streaming(vb->vb2_queue))
328 vip_active_buf_next(vip);
331 static int start_streaming(struct vb2_queue *vq, unsigned int count)
333 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
335 spin_lock_irq(&vip->slock);
336 /* Enable interrupt VSYNC Top and Bottom*/
337 reg_write(vip, DVP_ITM, DVP_IT_VSB | DVP_IT_VST);
338 spin_unlock_irq(&vip->slock);
341 start_dma(vip, vip->active);
346 /* abort streaming and wait for last buffer */
347 static void stop_streaming(struct vb2_queue *vq)
349 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
350 struct vip_buffer *vip_buf, *node;
352 /* Disable acquisition */
353 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
354 /* Disable all interrupts */
355 reg_write(vip, DVP_ITM, 0);
357 /* Release all active buffers */
358 spin_lock(&vip->lock);
359 list_for_each_entry_safe(vip_buf, node, &vip->buffer_list, list) {
360 vb2_buffer_done(&vip_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
361 list_del(&vip_buf->list);
363 spin_unlock(&vip->lock);
366 static const struct vb2_ops vip_video_qops = {
367 .queue_setup = queue_setup,
368 .buf_init = buffer_init,
369 .buf_prepare = buffer_prepare,
370 .buf_finish = buffer_finish,
371 .buf_queue = buffer_queue,
372 .start_streaming = start_streaming,
373 .stop_streaming = stop_streaming,
374 .wait_prepare = vb2_ops_wait_prepare,
375 .wait_finish = vb2_ops_wait_finish,
379 /* File Operations */
380 static const struct v4l2_file_operations vip_fops = {
381 .owner = THIS_MODULE,
382 .open = v4l2_fh_open,
383 .release = vb2_fop_release,
384 .unlocked_ioctl = video_ioctl2,
385 .read = vb2_fop_read,
386 .mmap = vb2_fop_mmap,
392 * vidioc_querycap - return capabilities of device
393 * @file: descriptor of device
394 * @cap: contains return values
397 * the capabilities of the device are returned
399 * return value: 0, no error.
401 static int vidioc_querycap(struct file *file, void *priv,
402 struct v4l2_capability *cap)
404 struct sta2x11_vip *vip = video_drvdata(file);
406 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
407 strscpy(cap->card, KBUILD_MODNAME, sizeof(cap->card));
408 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
409 pci_name(vip->pdev));
414 * vidioc_s_std - set video standard
415 * @file: descriptor of device
416 * @std: contains standard to be set
419 * the video standard is set
421 * return value: 0, no error.
423 * -EIO, no input signal detected
425 * other, returned from video DAC.
427 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id std)
429 struct sta2x11_vip *vip = video_drvdata(file);
432 * This is here for backwards compatibility only.
433 * The use of V4L2_STD_ALL to trigger a querystd is non-standard.
435 if (std == V4L2_STD_ALL) {
436 v4l2_subdev_call(vip->decoder, video, querystd, &std);
437 if (std == V4L2_STD_UNKNOWN)
441 if (vip->std != std) {
443 if (V4L2_STD_525_60 & std)
444 vip->format = formats_60[0];
446 vip->format = formats_50[0];
449 return v4l2_subdev_call(vip->decoder, video, s_std, std);
453 * vidioc_g_std - get video standard
454 * @file: descriptor of device
456 * @std: contains return values
458 * the current video standard is returned
460 * return value: 0, no error.
462 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
464 struct sta2x11_vip *vip = video_drvdata(file);
471 * vidioc_querystd - get possible video standards
472 * @file: descriptor of device
474 * @std: contains return values
476 * all possible video standards are returned
478 * return value: delivered by video DAC routine.
480 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
482 struct sta2x11_vip *vip = video_drvdata(file);
484 return v4l2_subdev_call(vip->decoder, video, querystd, std);
487 static int vidioc_enum_input(struct file *file, void *priv,
488 struct v4l2_input *inp)
493 inp->type = V4L2_INPUT_TYPE_CAMERA;
494 inp->std = V4L2_STD_ALL;
495 sprintf(inp->name, "Camera %u", inp->index);
501 * vidioc_s_input - set input line
502 * @file: descriptor of device
504 * @i: new input line number
506 * the current active input line is set
508 * return value: 0, no error.
510 * -EINVAL, line number out of range
512 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
514 struct sta2x11_vip *vip = video_drvdata(file);
519 ret = v4l2_subdev_call(vip->decoder, video, s_routing, i, 0, 0);
528 * vidioc_g_input - return input line
529 * @file: descriptor of device
531 * @i: returned input line number
533 * the current active input line is returned
535 * return value: always 0.
537 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
539 struct sta2x11_vip *vip = video_drvdata(file);
546 * vidioc_enum_fmt_vid_cap - return video capture format
547 * @file: descriptor of device
549 * @f: returned format information
551 * returns name and format of video capture
552 * Only UYVY is supported by hardware.
554 * return value: always 0.
556 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
557 struct v4l2_fmtdesc *f)
563 strscpy(f->description, "4:2:2, packed, UYVY", sizeof(f->description));
564 f->pixelformat = V4L2_PIX_FMT_UYVY;
570 * vidioc_try_fmt_vid_cap - set video capture format
571 * @file: descriptor of device
575 * new video format is set which includes width and
576 * field type. width is fixed to 720, no scaling.
577 * Only UYVY is supported by this hardware.
578 * the minimum height is 200, the maximum is 576 (PAL)
580 * return value: 0, no error
582 * -EINVAL, pixel or field format not supported
585 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
586 struct v4l2_format *f)
588 struct sta2x11_vip *vip = video_drvdata(file);
591 if (V4L2_PIX_FMT_UYVY != f->fmt.pix.pixelformat) {
592 v4l2_warn(&vip->v4l2_dev, "Invalid format, only UYVY supported\n");
596 if (V4L2_STD_525_60 & vip->std)
601 switch (f->fmt.pix.field) {
604 if (interlace_lim < f->fmt.pix.height)
605 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
607 f->fmt.pix.field = V4L2_FIELD_BOTTOM;
610 case V4L2_FIELD_BOTTOM:
611 if (interlace_lim < f->fmt.pix.height)
612 f->fmt.pix.height = interlace_lim;
614 case V4L2_FIELD_INTERLACED:
618 /* It is the only supported format */
619 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
620 f->fmt.pix.height &= ~1;
621 if (2 * interlace_lim < f->fmt.pix.height)
622 f->fmt.pix.height = 2 * interlace_lim;
623 if (200 > f->fmt.pix.height)
624 f->fmt.pix.height = 200;
625 f->fmt.pix.width = 720;
626 f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
627 f->fmt.pix.sizeimage = f->fmt.pix.width * 2 * f->fmt.pix.height;
628 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
633 * vidioc_s_fmt_vid_cap - set current video format parameters
634 * @file: descriptor of device
636 * @f: returned format information
638 * set new capture format
639 * return value: 0, no error
641 * other, delivered by video DAC routine.
643 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
644 struct v4l2_format *f)
646 struct sta2x11_vip *vip = video_drvdata(file);
647 unsigned int t_stop, b_stop, pitch;
650 ret = vidioc_try_fmt_vid_cap(file, priv, f);
654 if (vb2_is_busy(&vip->vb_vidq)) {
655 /* Can't change format during acquisition */
656 v4l2_err(&vip->v4l2_dev, "device busy\n");
659 vip->format = f->fmt.pix;
660 switch (vip->format.field) {
661 case V4L2_FIELD_INTERLACED:
662 t_stop = ((vip->format.height / 2 - 1) << 16) |
663 (2 * vip->format.width - 1);
665 pitch = 4 * vip->format.width;
668 t_stop = ((vip->format.height - 1) << 16) |
669 (2 * vip->format.width - 1);
670 b_stop = (0 << 16) | (2 * vip->format.width - 1);
671 pitch = 2 * vip->format.width;
673 case V4L2_FIELD_BOTTOM:
674 t_stop = (0 << 16) | (2 * vip->format.width - 1);
675 b_stop = (vip->format.height << 16) |
676 (2 * vip->format.width - 1);
677 pitch = 2 * vip->format.width;
680 v4l2_err(&vip->v4l2_dev, "unknown field format\n");
684 spin_lock_irq(&vip->slock);
685 /* Y-X Top Field Offset */
686 reg_write(vip, DVP_TFO, 0);
687 /* Y-X Bottom Field Offset */
688 reg_write(vip, DVP_BFO, 0);
689 /* Y-X Top Field Stop*/
690 reg_write(vip, DVP_TFS, t_stop);
691 /* Y-X Bottom Field Stop */
692 reg_write(vip, DVP_BFS, b_stop);
693 /* Video Memory Pitch */
694 reg_write(vip, DVP_VMP, pitch);
695 spin_unlock_irq(&vip->slock);
701 * vidioc_g_fmt_vid_cap - get current video format parameters
702 * @file: descriptor of device
704 * @f: contains format information
706 * returns current video format parameters
708 * return value: 0, always successful
710 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
711 struct v4l2_format *f)
713 struct sta2x11_vip *vip = video_drvdata(file);
715 f->fmt.pix = vip->format;
720 static const struct v4l2_ioctl_ops vip_ioctl_ops = {
721 .vidioc_querycap = vidioc_querycap,
723 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
724 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
725 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
726 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
727 /* Buffer handlers */
728 .vidioc_create_bufs = vb2_ioctl_create_bufs,
729 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
730 .vidioc_reqbufs = vb2_ioctl_reqbufs,
731 .vidioc_querybuf = vb2_ioctl_querybuf,
732 .vidioc_qbuf = vb2_ioctl_qbuf,
733 .vidioc_dqbuf = vb2_ioctl_dqbuf,
735 .vidioc_streamon = vb2_ioctl_streamon,
736 .vidioc_streamoff = vb2_ioctl_streamoff,
737 /* Standard handling */
738 .vidioc_g_std = vidioc_g_std,
739 .vidioc_s_std = vidioc_s_std,
740 .vidioc_querystd = vidioc_querystd,
742 .vidioc_enum_input = vidioc_enum_input,
743 .vidioc_g_input = vidioc_g_input,
744 .vidioc_s_input = vidioc_s_input,
745 /* Log status ioctl */
746 .vidioc_log_status = v4l2_ctrl_log_status,
748 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
749 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
752 static const struct video_device video_dev_template = {
753 .name = KBUILD_MODNAME,
754 .release = video_device_release_empty,
756 .ioctl_ops = &vip_ioctl_ops,
757 .tvnorms = V4L2_STD_ALL,
758 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
763 * vip_irq - interrupt routine
764 * @irq: Number of interrupt ( not used, correct number is assumed )
765 * @vip: local data structure containing all information
767 * check for both frame interrupts set ( top and bottom ).
768 * check FIFO overflow, but limit number of log messages after open.
769 * signal a complete buffer if done
771 * return value: IRQ_NONE, interrupt was not generated by VIP
773 * IRQ_HANDLED, interrupt done.
775 static irqreturn_t vip_irq(int irq, struct sta2x11_vip *vip)
779 status = reg_read(vip, DVP_ITS);
781 if (!status) /* No interrupt to handle */
784 if (status & DVP_IT_FIFO)
785 if (vip->overflow++ > 5)
786 pr_info("VIP: fifo overflow\n");
788 if ((status & DVP_IT_VST) && (status & DVP_IT_VSB)) {
789 /* this is bad, we are too slow, hope the condition is gone
790 * on the next frame */
794 if (status & DVP_IT_VST)
795 if ((++vip->tcount) < 2)
797 if (status & DVP_IT_VSB) {
802 if (vip->active) { /* Acquisition is over on this buffer */
803 /* Disable acquisition */
804 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
805 /* Remove the active buffer from the list */
806 vip->active->vb.vb2_buf.timestamp = ktime_get_ns();
807 vip->active->vb.sequence = vip->sequence++;
808 vb2_buffer_done(&vip->active->vb.vb2_buf, VB2_BUF_STATE_DONE);
814 static void sta2x11_vip_init_register(struct sta2x11_vip *vip)
816 /* Register initialization */
817 spin_lock_irq(&vip->slock);
818 /* Clean interrupt */
819 reg_read(vip, DVP_ITS);
820 /* Enable Half Line per vertical */
821 reg_write(vip, DVP_HLFLN, DVP_HLFLN_SD);
822 /* Reset VIP control */
823 reg_write(vip, DVP_CTL, DVP_CTL_RST);
824 /* Clear VIP control */
825 reg_write(vip, DVP_CTL, 0);
826 spin_unlock_irq(&vip->slock);
828 static void sta2x11_vip_clear_register(struct sta2x11_vip *vip)
830 spin_lock_irq(&vip->slock);
831 /* Disable interrupt */
832 reg_write(vip, DVP_ITM, 0);
833 /* Reset VIP Control */
834 reg_write(vip, DVP_CTL, DVP_CTL_RST);
835 /* Clear VIP Control */
836 reg_write(vip, DVP_CTL, 0);
837 /* Clean VIP Interrupt */
838 reg_read(vip, DVP_ITS);
839 spin_unlock_irq(&vip->slock);
841 static int sta2x11_vip_init_buffer(struct sta2x11_vip *vip)
845 err = dma_set_coherent_mask(&vip->pdev->dev, DMA_BIT_MASK(29));
847 v4l2_err(&vip->v4l2_dev, "Cannot configure coherent mask");
850 memset(&vip->vb_vidq, 0, sizeof(struct vb2_queue));
851 vip->vb_vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
852 vip->vb_vidq.io_modes = VB2_MMAP | VB2_READ;
853 vip->vb_vidq.drv_priv = vip;
854 vip->vb_vidq.buf_struct_size = sizeof(struct vip_buffer);
855 vip->vb_vidq.ops = &vip_video_qops;
856 vip->vb_vidq.mem_ops = &vb2_dma_contig_memops;
857 vip->vb_vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
858 vip->vb_vidq.dev = &vip->pdev->dev;
859 vip->vb_vidq.lock = &vip->v4l_lock;
860 err = vb2_queue_init(&vip->vb_vidq);
863 INIT_LIST_HEAD(&vip->buffer_list);
864 spin_lock_init(&vip->lock);
868 static int sta2x11_vip_init_controls(struct sta2x11_vip *vip)
871 * Inititialize an empty control so VIP can inerithing controls
874 v4l2_ctrl_handler_init(&vip->ctrl_hdl, 0);
876 vip->v4l2_dev.ctrl_handler = &vip->ctrl_hdl;
877 if (vip->ctrl_hdl.error) {
878 int err = vip->ctrl_hdl.error;
880 v4l2_ctrl_handler_free(&vip->ctrl_hdl);
888 * vip_gpio_reserve - reserve gpio pin
890 * @pin: GPIO pin number
891 * @dir: direction, input or output
892 * @name: GPIO pin name
895 static int vip_gpio_reserve(struct device *dev, int pin, int dir,
900 if (!gpio_is_valid(pin))
903 ret = gpio_request(pin, name);
905 dev_err(dev, "Failed to allocate pin %d (%s)\n", pin, name);
909 ret = gpio_direction_output(pin, dir);
911 dev_err(dev, "Failed to set direction for pin %d (%s)\n",
917 ret = gpio_export(pin, false);
919 dev_err(dev, "Failed to export pin %d (%s)\n", pin, name);
928 * vip_gpio_release - release gpio pin
930 * @pin: GPIO pin number
931 * @name: GPIO pin name
934 static void vip_gpio_release(struct device *dev, int pin, const char *name)
936 if (gpio_is_valid(pin)) {
937 dev_dbg(dev, "releasing pin %d (%s)\n", pin, name);
944 * sta2x11_vip_init_one - init one instance of video device
948 * allocate reset pins for DAC.
949 * Reset video DAC, this is done via reset line.
950 * allocate memory for managing device
954 * find and initialize video DAC
956 * return value: 0, no error
960 * -ENODEV, device could not be detected or registered
962 static int sta2x11_vip_init_one(struct pci_dev *pdev,
963 const struct pci_device_id *ent)
966 struct sta2x11_vip *vip;
967 struct vip_config *config;
969 /* Check if hardware support 26-bit DMA */
970 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(26))) {
971 dev_err(&pdev->dev, "26-bit DMA addressing not available\n");
975 ret = pci_enable_device(pdev);
979 /* Get VIP platform data */
980 config = dev_get_platdata(&pdev->dev);
982 dev_info(&pdev->dev, "VIP slot disabled\n");
987 /* Power configuration */
988 ret = vip_gpio_reserve(&pdev->dev, config->pwr_pin, 0,
993 ret = vip_gpio_reserve(&pdev->dev, config->reset_pin, 0,
996 vip_gpio_release(&pdev->dev, config->pwr_pin,
1001 if (gpio_is_valid(config->pwr_pin)) {
1002 /* Datasheet says 5ms between PWR and RST */
1003 usleep_range(5000, 25000);
1004 gpio_direction_output(config->pwr_pin, 1);
1007 if (gpio_is_valid(config->reset_pin)) {
1008 /* Datasheet says 5ms between PWR and RST */
1009 usleep_range(5000, 25000);
1010 gpio_direction_output(config->reset_pin, 1);
1012 usleep_range(5000, 25000);
1014 /* Allocate a new VIP instance */
1015 vip = kzalloc(sizeof(struct sta2x11_vip), GFP_KERNEL);
1021 vip->std = V4L2_STD_PAL;
1022 vip->format = formats_50[0];
1023 vip->config = config;
1024 mutex_init(&vip->v4l_lock);
1026 ret = sta2x11_vip_init_controls(vip);
1029 ret = v4l2_device_register(&pdev->dev, &vip->v4l2_dev);
1033 dev_dbg(&pdev->dev, "BAR #0 at 0x%lx 0x%lx irq %d\n",
1034 (unsigned long)pci_resource_start(pdev, 0),
1035 (unsigned long)pci_resource_len(pdev, 0), pdev->irq);
1037 pci_set_master(pdev);
1039 ret = pci_request_regions(pdev, KBUILD_MODNAME);
1043 vip->iomem = pci_iomap(pdev, 0, 0x100);
1049 pci_enable_msi(pdev);
1051 /* Initialize buffer */
1052 ret = sta2x11_vip_init_buffer(vip);
1056 spin_lock_init(&vip->slock);
1058 ret = request_irq(pdev->irq,
1059 (irq_handler_t) vip_irq,
1060 IRQF_SHARED, KBUILD_MODNAME, vip);
1062 dev_err(&pdev->dev, "request_irq failed\n");
1067 /* Initialize and register video device */
1068 vip->video_dev = video_dev_template;
1069 vip->video_dev.v4l2_dev = &vip->v4l2_dev;
1070 vip->video_dev.queue = &vip->vb_vidq;
1071 vip->video_dev.lock = &vip->v4l_lock;
1072 video_set_drvdata(&vip->video_dev, vip);
1074 ret = video_register_device(&vip->video_dev, VFL_TYPE_GRABBER, -1);
1078 /* Get ADV7180 subdevice */
1079 vip->adapter = i2c_get_adapter(vip->config->i2c_id);
1080 if (!vip->adapter) {
1082 dev_err(&pdev->dev, "no I2C adapter found\n");
1086 vip->decoder = v4l2_i2c_new_subdev(&vip->v4l2_dev, vip->adapter,
1087 "adv7180", vip->config->i2c_addr,
1089 if (!vip->decoder) {
1091 dev_err(&pdev->dev, "no decoder found\n");
1095 i2c_put_adapter(vip->adapter);
1096 v4l2_subdev_call(vip->decoder, core, init, 0);
1098 sta2x11_vip_init_register(vip);
1100 dev_info(&pdev->dev, "STA2X11 Video Input Port (VIP) loaded\n");
1104 video_set_drvdata(&vip->video_dev, NULL);
1106 video_unregister_device(&vip->video_dev);
1107 free_irq(pdev->irq, vip);
1109 pci_disable_msi(pdev);
1111 vb2_queue_release(&vip->vb_vidq);
1112 pci_iounmap(pdev, vip->iomem);
1114 pci_release_regions(pdev);
1116 v4l2_device_unregister(&vip->v4l2_dev);
1120 vip_gpio_release(&pdev->dev, config->reset_pin, config->reset_name);
1121 vip_gpio_release(&pdev->dev, config->pwr_pin, config->pwr_name);
1124 * do not call pci_disable_device on sta2x11 because it break all
1125 * other Bus masters on this EP
1131 * sta2x11_vip_remove_one - release device
1134 * Undo everything done in .._init_one
1136 * unregister video device
1142 static void sta2x11_vip_remove_one(struct pci_dev *pdev)
1144 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1145 struct sta2x11_vip *vip =
1146 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1148 sta2x11_vip_clear_register(vip);
1150 video_set_drvdata(&vip->video_dev, NULL);
1151 video_unregister_device(&vip->video_dev);
1152 free_irq(pdev->irq, vip);
1153 pci_disable_msi(pdev);
1154 vb2_queue_release(&vip->vb_vidq);
1155 pci_iounmap(pdev, vip->iomem);
1156 pci_release_regions(pdev);
1158 v4l2_device_unregister(&vip->v4l2_dev);
1160 vip_gpio_release(&pdev->dev, vip->config->pwr_pin,
1161 vip->config->pwr_name);
1162 vip_gpio_release(&pdev->dev, vip->config->reset_pin,
1163 vip->config->reset_name);
1167 * do not call pci_disable_device on sta2x11 because it break all
1168 * other Bus masters on this EP
1175 * sta2x11_vip_suspend - set device into power save mode
1177 * @state: new state of device
1179 * all relevant registers are saved and an attempt to set a new state is made.
1181 * return value: 0 always indicate success,
1182 * even if device could not be disabled. (workaround for hardware problem)
1184 static int sta2x11_vip_suspend(struct pci_dev *pdev, pm_message_t state)
1186 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1187 struct sta2x11_vip *vip =
1188 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1189 unsigned long flags;
1192 spin_lock_irqsave(&vip->slock, flags);
1193 vip->register_save_area[0] = reg_read(vip, DVP_CTL);
1194 reg_write(vip, DVP_CTL, vip->register_save_area[0] & DVP_CTL_DIS);
1195 vip->register_save_area[SAVE_COUNT] = reg_read(vip, DVP_ITM);
1196 reg_write(vip, DVP_ITM, 0);
1197 for (i = 1; i < SAVE_COUNT; i++)
1198 vip->register_save_area[i] = reg_read(vip, 4 * i);
1199 for (i = 0; i < AUX_COUNT; i++)
1200 vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i] =
1201 reg_read(vip, registers_to_save[i]);
1202 spin_unlock_irqrestore(&vip->slock, flags);
1203 /* save pci state */
1204 pci_save_state(pdev);
1205 if (pci_set_power_state(pdev, pci_choose_state(pdev, state))) {
1207 * do not call pci_disable_device on sta2x11 because it
1208 * break all other Bus masters on this EP
1213 pr_info("VIP: suspend\n");
1218 * sta2x11_vip_resume - resume device operation
1219 * @pdev : PCI device
1221 * re-enable device, set PCI state to powered and restore registers.
1222 * resume normal device operation afterwards.
1224 * return value: 0, no error.
1226 * other, could not set device to power on state.
1228 static int sta2x11_vip_resume(struct pci_dev *pdev)
1230 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1231 struct sta2x11_vip *vip =
1232 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1233 unsigned long flags;
1236 pr_info("VIP: resume\n");
1237 /* restore pci state */
1238 if (vip->disabled) {
1239 ret = pci_enable_device(pdev);
1241 pr_warn("VIP: Can't enable device.\n");
1246 ret = pci_set_power_state(pdev, PCI_D0);
1249 * do not call pci_disable_device on sta2x11 because it
1250 * break all other Bus masters on this EP
1252 pr_warn("VIP: Can't enable device.\n");
1257 pci_restore_state(pdev);
1259 spin_lock_irqsave(&vip->slock, flags);
1260 for (i = 1; i < SAVE_COUNT; i++)
1261 reg_write(vip, 4 * i, vip->register_save_area[i]);
1262 for (i = 0; i < AUX_COUNT; i++)
1263 reg_write(vip, registers_to_save[i],
1264 vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i]);
1265 reg_write(vip, DVP_CTL, vip->register_save_area[0]);
1266 reg_write(vip, DVP_ITM, vip->register_save_area[SAVE_COUNT]);
1267 spin_unlock_irqrestore(&vip->slock, flags);
1273 static const struct pci_device_id sta2x11_vip_pci_tbl[] = {
1274 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_VIP)},
1278 static struct pci_driver sta2x11_vip_driver = {
1279 .name = KBUILD_MODNAME,
1280 .probe = sta2x11_vip_init_one,
1281 .remove = sta2x11_vip_remove_one,
1282 .id_table = sta2x11_vip_pci_tbl,
1284 .suspend = sta2x11_vip_suspend,
1285 .resume = sta2x11_vip_resume,
1289 static int __init sta2x11_vip_init_module(void)
1291 return pci_register_driver(&sta2x11_vip_driver);
1294 static void __exit sta2x11_vip_exit_module(void)
1296 pci_unregister_driver(&sta2x11_vip_driver);
1300 module_init(sta2x11_vip_init_module);
1301 module_exit(sta2x11_vip_exit_module);
1303 late_initcall_sync(sta2x11_vip_init_module);
1306 MODULE_DESCRIPTION("STA2X11 Video Input Port driver");
1307 MODULE_AUTHOR("Wind River");
1308 MODULE_LICENSE("GPL v2");
1309 MODULE_SUPPORTED_DEVICE("sta2x11 video input");
1310 MODULE_VERSION(DRV_VERSION);
1311 MODULE_DEVICE_TABLE(pci, sta2x11_vip_pci_tbl);