s390/ptrace: get rid of long longs in psw_bits
[linux/fpc-iii.git] / drivers / media / pci / sta2x11 / sta2x11_vip.c
blob6367b455a7e7ea4f2b053b222b12d6f67b1589bd
1 /*
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
17 * more details.
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>
36 #include <linux/io.h>
37 #include <linux/gpio.h>
38 #include <linux/i2c.h>
39 #include <linux/delay.h>
40 #include <media/v4l2-common.h>
41 #include <media/v4l2-device.h>
42 #include <media/v4l2-ctrls.h>
43 #include <media/v4l2-ioctl.h>
44 #include <media/v4l2-fh.h>
45 #include <media/v4l2-event.h>
46 #include <media/videobuf2-dma-contig.h>
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
54 #endif
56 #define MAX_FRAMES 4
58 /*Register offsets*/
59 #define DVP_CTL 0x00
60 #define DVP_TFO 0x04
61 #define DVP_TFS 0x08
62 #define DVP_BFO 0x0C
63 #define DVP_BFS 0x10
64 #define DVP_VTP 0x14
65 #define DVP_VBP 0x18
66 #define DVP_VMP 0x1C
67 #define DVP_ITM 0x98
68 #define DVP_ITS 0x9C
69 #define DVP_STA 0xA0
70 #define DVP_HLFLN 0xA8
71 #define DVP_RGB 0xC0
72 #define DVP_PKZ 0xF0
74 /*Register fields*/
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
85 #define SAVE_COUNT 8
86 #define AUX_COUNT 3
87 #define IRQ_COUNT 1
90 struct vip_buffer {
91 struct vb2_v4l2_buffer vb;
92 struct list_head list;
93 dma_addr_t dma;
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
104 * @pdev: PCI 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 * @alloc_ctx: context for videobuf2
115 * @vb_vidq: queue maintained by videobuf2 layer
116 * @buffer_list: list of buffer in use
117 * @sequence: sequence number of acquired buffer
118 * @active: current active buffer
119 * @lock: used in videobuf2 callback
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.
128 struct sta2x11_vip {
129 struct v4l2_device v4l2_dev;
130 struct video_device video_dev;
131 struct pci_dev *pdev;
132 struct i2c_adapter *adapter;
133 unsigned int register_save_area[IRQ_COUNT + SAVE_COUNT + AUX_COUNT];
134 struct v4l2_subdev *decoder;
135 struct v4l2_ctrl_handler ctrl_hdl;
138 struct v4l2_pix_format format;
139 v4l2_std_id std;
140 unsigned int input;
141 int disabled;
142 spinlock_t slock;
144 struct vb2_alloc_ctx *alloc_ctx;
145 struct vb2_queue vb_vidq;
146 struct list_head buffer_list;
147 unsigned int sequence;
148 struct vip_buffer *active; /* current active buffer */
149 spinlock_t lock; /* Used in videobuf2 callback */
151 /* Interrupt counters */
152 int tcount, bcount;
153 int overflow;
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 */
165 .width = 720,
166 .height = 576,
167 .pixelformat = V4L2_PIX_FMT_UYVY,
168 .field = V4L2_FIELD_INTERLACED,
169 .bytesperline = 720 * 2,
170 .sizeimage = 720 * 2 * 576,
171 .colorspace = V4L2_COLORSPACE_SMPTE170M},
172 { /*PAL top */
173 .width = 720,
174 .height = 288,
175 .pixelformat = V4L2_PIX_FMT_UYVY,
176 .field = V4L2_FIELD_TOP,
177 .bytesperline = 720 * 2,
178 .sizeimage = 720 * 2 * 288,
179 .colorspace = V4L2_COLORSPACE_SMPTE170M},
180 { /*PAL bottom */
181 .width = 720,
182 .height = 288,
183 .pixelformat = V4L2_PIX_FMT_UYVY,
184 .field = V4L2_FIELD_BOTTOM,
185 .bytesperline = 720 * 2,
186 .sizeimage = 720 * 2 * 288,
187 .colorspace = V4L2_COLORSPACE_SMPTE170M},
191 static struct v4l2_pix_format formats_60[] = {
192 { /*NTSC interlaced */
193 .width = 720,
194 .height = 480,
195 .pixelformat = V4L2_PIX_FMT_UYVY,
196 .field = V4L2_FIELD_INTERLACED,
197 .bytesperline = 720 * 2,
198 .sizeimage = 720 * 2 * 480,
199 .colorspace = V4L2_COLORSPACE_SMPTE170M},
200 { /*NTSC top */
201 .width = 720,
202 .height = 240,
203 .pixelformat = V4L2_PIX_FMT_UYVY,
204 .field = V4L2_FIELD_TOP,
205 .bytesperline = 720 * 2,
206 .sizeimage = 720 * 2 * 240,
207 .colorspace = V4L2_COLORSPACE_SMPTE170M},
208 { /*NTSC bottom */
209 .width = 720,
210 .height = 240,
211 .pixelformat = V4L2_PIX_FMT_UYVY,
212 .field = V4L2_FIELD_BOTTOM,
213 .bytesperline = 720 * 2,
214 .sizeimage = 720 * 2 * 240,
215 .colorspace = V4L2_COLORSPACE_SMPTE170M},
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);
252 return;
254 vip->active = list_first_entry(&vip->buffer_list,
255 struct vip_buffer,
256 list);
257 /* Reset Top and Bottom counter */
258 vip->tcount = 0;
259 vip->bcount = 0;
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, const void *parg,
269 unsigned int *nbuffers, unsigned int *nplanes,
270 unsigned int sizes[], void *alloc_ctxs[])
272 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
274 if (!(*nbuffers) || *nbuffers < MAX_FRAMES)
275 *nbuffers = MAX_FRAMES;
277 *nplanes = 1;
278 sizes[0] = vip->format.sizeimage;
279 alloc_ctxs[0] = vip->alloc_ctx;
281 vip->sequence = 0;
282 vip->active = NULL;
283 vip->tcount = 0;
284 vip->bcount = 0;
286 return 0;
288 static int buffer_init(struct vb2_buffer *vb)
290 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
291 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
293 vip_buf->dma = vb2_dma_contig_plane_dma_addr(vb, 0);
294 INIT_LIST_HEAD(&vip_buf->list);
295 return 0;
298 static int buffer_prepare(struct vb2_buffer *vb)
300 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
301 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
302 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
303 unsigned long size;
305 size = vip->format.sizeimage;
306 if (vb2_plane_size(vb, 0) < size) {
307 v4l2_err(&vip->v4l2_dev, "buffer too small (%lu < %lu)\n",
308 vb2_plane_size(vb, 0), size);
309 return -EINVAL;
312 vb2_set_plane_payload(&vip_buf->vb.vb2_buf, 0, size);
314 return 0;
316 static void buffer_queue(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 spin_lock(&vip->lock);
323 list_add_tail(&vip_buf->list, &vip->buffer_list);
324 if (!vip->active) { /* No active buffer, active the first one */
325 vip->active = list_first_entry(&vip->buffer_list,
326 struct vip_buffer,
327 list);
328 if (vb2_is_streaming(&vip->vb_vidq)) /* streaming is on */
329 start_dma(vip, vip_buf); /* start dma capture */
331 spin_unlock(&vip->lock);
333 static void buffer_finish(struct vb2_buffer *vb)
335 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
336 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
337 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
339 /* Buffer handled, remove it from the list */
340 spin_lock(&vip->lock);
341 list_del_init(&vip_buf->list);
342 spin_unlock(&vip->lock);
344 if (vb2_is_streaming(vb->vb2_queue))
345 vip_active_buf_next(vip);
348 static int start_streaming(struct vb2_queue *vq, unsigned int count)
350 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
352 spin_lock_irq(&vip->slock);
353 /* Enable interrupt VSYNC Top and Bottom*/
354 reg_write(vip, DVP_ITM, DVP_IT_VSB | DVP_IT_VST);
355 spin_unlock_irq(&vip->slock);
357 if (count)
358 start_dma(vip, vip->active);
360 return 0;
363 /* abort streaming and wait for last buffer */
364 static void stop_streaming(struct vb2_queue *vq)
366 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
367 struct vip_buffer *vip_buf, *node;
369 /* Disable acquisition */
370 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
371 /* Disable all interrupts */
372 reg_write(vip, DVP_ITM, 0);
374 /* Release all active buffers */
375 spin_lock(&vip->lock);
376 list_for_each_entry_safe(vip_buf, node, &vip->buffer_list, list) {
377 vb2_buffer_done(&vip_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
378 list_del(&vip_buf->list);
380 spin_unlock(&vip->lock);
383 static struct vb2_ops vip_video_qops = {
384 .queue_setup = queue_setup,
385 .buf_init = buffer_init,
386 .buf_prepare = buffer_prepare,
387 .buf_finish = buffer_finish,
388 .buf_queue = buffer_queue,
389 .start_streaming = start_streaming,
390 .stop_streaming = stop_streaming,
394 /* File Operations */
395 static const struct v4l2_file_operations vip_fops = {
396 .owner = THIS_MODULE,
397 .open = v4l2_fh_open,
398 .release = vb2_fop_release,
399 .unlocked_ioctl = video_ioctl2,
400 .read = vb2_fop_read,
401 .mmap = vb2_fop_mmap,
402 .poll = vb2_fop_poll
407 * vidioc_querycap - return capabilities of device
408 * @file: descriptor of device
409 * @cap: contains return values
411 * the capabilities of the device are returned
413 * return value: 0, no error.
415 static int vidioc_querycap(struct file *file, void *priv,
416 struct v4l2_capability *cap)
418 struct sta2x11_vip *vip = video_drvdata(file);
420 strcpy(cap->driver, KBUILD_MODNAME);
421 strcpy(cap->card, KBUILD_MODNAME);
422 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
423 pci_name(vip->pdev));
424 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
425 V4L2_CAP_STREAMING;
426 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
428 return 0;
432 * vidioc_s_std - set video standard
433 * @file: descriptor of device
434 * @std: contains standard to be set
436 * the video standard is set
438 * return value: 0, no error.
440 * -EIO, no input signal detected
442 * other, returned from video DAC.
444 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id std)
446 struct sta2x11_vip *vip = video_drvdata(file);
447 v4l2_std_id oldstd = vip->std, newstd;
448 int status;
450 if (V4L2_STD_ALL == std) {
451 v4l2_subdev_call(vip->decoder, video, s_std, std);
452 ssleep(2);
453 v4l2_subdev_call(vip->decoder, video, querystd, &newstd);
454 v4l2_subdev_call(vip->decoder, video, g_input_status, &status);
455 if (status & V4L2_IN_ST_NO_SIGNAL)
456 return -EIO;
457 std = vip->std = newstd;
458 if (oldstd != std) {
459 if (V4L2_STD_525_60 & std)
460 vip->format = formats_60[0];
461 else
462 vip->format = formats_50[0];
464 return 0;
467 if (oldstd != std) {
468 if (V4L2_STD_525_60 & std)
469 vip->format = formats_60[0];
470 else
471 vip->format = formats_50[0];
474 return v4l2_subdev_call(vip->decoder, video, s_std, std);
478 * vidioc_g_std - get video standard
479 * @file: descriptor of device
480 * @std: contains return values
482 * the current video standard is returned
484 * return value: 0, no error.
486 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
488 struct sta2x11_vip *vip = video_drvdata(file);
490 *std = vip->std;
491 return 0;
495 * vidioc_querystd - get possible video standards
496 * @file: descriptor of device
497 * @std: contains return values
499 * all possible video standards are returned
501 * return value: delivered by video DAC routine.
503 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
505 struct sta2x11_vip *vip = video_drvdata(file);
507 return v4l2_subdev_call(vip->decoder, video, querystd, std);
510 static int vidioc_enum_input(struct file *file, void *priv,
511 struct v4l2_input *inp)
513 if (inp->index > 1)
514 return -EINVAL;
516 inp->type = V4L2_INPUT_TYPE_CAMERA;
517 inp->std = V4L2_STD_ALL;
518 sprintf(inp->name, "Camera %u", inp->index);
520 return 0;
524 * vidioc_s_input - set input line
525 * @file: descriptor of device
526 * @i: new input line number
528 * the current active input line is set
530 * return value: 0, no error.
532 * -EINVAL, line number out of range
534 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
536 struct sta2x11_vip *vip = video_drvdata(file);
537 int ret;
539 if (i > 1)
540 return -EINVAL;
541 ret = v4l2_subdev_call(vip->decoder, video, s_routing, i, 0, 0);
543 if (!ret)
544 vip->input = i;
546 return 0;
550 * vidioc_g_input - return input line
551 * @file: descriptor of device
552 * @i: returned input line number
554 * the current active input line is returned
556 * return value: always 0.
558 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
560 struct sta2x11_vip *vip = video_drvdata(file);
562 *i = vip->input;
563 return 0;
567 * vidioc_enum_fmt_vid_cap - return video capture format
568 * @f: returned format information
570 * returns name and format of video capture
571 * Only UYVY is supported by hardware.
573 * return value: always 0.
575 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
576 struct v4l2_fmtdesc *f)
579 if (f->index != 0)
580 return -EINVAL;
582 strcpy(f->description, "4:2:2, packed, UYVY");
583 f->pixelformat = V4L2_PIX_FMT_UYVY;
584 f->flags = 0;
585 return 0;
589 * vidioc_try_fmt_vid_cap - set video capture format
590 * @file: descriptor of device
591 * @f: new format
593 * new video format is set which includes width and
594 * field type. width is fixed to 720, no scaling.
595 * Only UYVY is supported by this hardware.
596 * the minimum height is 200, the maximum is 576 (PAL)
598 * return value: 0, no error
600 * -EINVAL, pixel or field format not supported
603 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
604 struct v4l2_format *f)
606 struct sta2x11_vip *vip = video_drvdata(file);
607 int interlace_lim;
609 if (V4L2_PIX_FMT_UYVY != f->fmt.pix.pixelformat) {
610 v4l2_warn(&vip->v4l2_dev, "Invalid format, only UYVY supported\n");
611 return -EINVAL;
614 if (V4L2_STD_525_60 & vip->std)
615 interlace_lim = 240;
616 else
617 interlace_lim = 288;
619 switch (f->fmt.pix.field) {
620 default:
621 case V4L2_FIELD_ANY:
622 if (interlace_lim < f->fmt.pix.height)
623 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
624 else
625 f->fmt.pix.field = V4L2_FIELD_BOTTOM;
626 break;
627 case V4L2_FIELD_TOP:
628 case V4L2_FIELD_BOTTOM:
629 if (interlace_lim < f->fmt.pix.height)
630 f->fmt.pix.height = interlace_lim;
631 break;
632 case V4L2_FIELD_INTERLACED:
633 break;
636 /* It is the only supported format */
637 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
638 f->fmt.pix.height &= ~1;
639 if (2 * interlace_lim < f->fmt.pix.height)
640 f->fmt.pix.height = 2 * interlace_lim;
641 if (200 > f->fmt.pix.height)
642 f->fmt.pix.height = 200;
643 f->fmt.pix.width = 720;
644 f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
645 f->fmt.pix.sizeimage = f->fmt.pix.width * 2 * f->fmt.pix.height;
646 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
647 return 0;
651 * vidioc_s_fmt_vid_cap - set current video format parameters
652 * @file: descriptor of device
653 * @f: returned format information
655 * set new capture format
656 * return value: 0, no error
658 * other, delivered by video DAC routine.
660 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
661 struct v4l2_format *f)
663 struct sta2x11_vip *vip = video_drvdata(file);
664 unsigned int t_stop, b_stop, pitch;
665 int ret;
667 ret = vidioc_try_fmt_vid_cap(file, priv, f);
668 if (ret)
669 return ret;
671 if (vb2_is_busy(&vip->vb_vidq)) {
672 /* Can't change format during acquisition */
673 v4l2_err(&vip->v4l2_dev, "device busy\n");
674 return -EBUSY;
676 vip->format = f->fmt.pix;
677 switch (vip->format.field) {
678 case V4L2_FIELD_INTERLACED:
679 t_stop = ((vip->format.height / 2 - 1) << 16) |
680 (2 * vip->format.width - 1);
681 b_stop = t_stop;
682 pitch = 4 * vip->format.width;
683 break;
684 case V4L2_FIELD_TOP:
685 t_stop = ((vip->format.height - 1) << 16) |
686 (2 * vip->format.width - 1);
687 b_stop = (0 << 16) | (2 * vip->format.width - 1);
688 pitch = 2 * vip->format.width;
689 break;
690 case V4L2_FIELD_BOTTOM:
691 t_stop = (0 << 16) | (2 * vip->format.width - 1);
692 b_stop = (vip->format.height << 16) |
693 (2 * vip->format.width - 1);
694 pitch = 2 * vip->format.width;
695 break;
696 default:
697 v4l2_err(&vip->v4l2_dev, "unknown field format\n");
698 return -EINVAL;
701 spin_lock_irq(&vip->slock);
702 /* Y-X Top Field Offset */
703 reg_write(vip, DVP_TFO, 0);
704 /* Y-X Bottom Field Offset */
705 reg_write(vip, DVP_BFO, 0);
706 /* Y-X Top Field Stop*/
707 reg_write(vip, DVP_TFS, t_stop);
708 /* Y-X Bottom Field Stop */
709 reg_write(vip, DVP_BFS, b_stop);
710 /* Video Memory Pitch */
711 reg_write(vip, DVP_VMP, pitch);
712 spin_unlock_irq(&vip->slock);
714 return 0;
718 * vidioc_g_fmt_vid_cap - get current video format parameters
719 * @file: descriptor of device
720 * @f: contains format information
722 * returns current video format parameters
724 * return value: 0, always successful
726 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
727 struct v4l2_format *f)
729 struct sta2x11_vip *vip = video_drvdata(file);
731 f->fmt.pix = vip->format;
733 return 0;
736 static const struct v4l2_ioctl_ops vip_ioctl_ops = {
737 .vidioc_querycap = vidioc_querycap,
738 /* FMT handling */
739 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
740 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
741 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
742 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
743 /* Buffer handlers */
744 .vidioc_create_bufs = vb2_ioctl_create_bufs,
745 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
746 .vidioc_reqbufs = vb2_ioctl_reqbufs,
747 .vidioc_querybuf = vb2_ioctl_querybuf,
748 .vidioc_qbuf = vb2_ioctl_qbuf,
749 .vidioc_dqbuf = vb2_ioctl_dqbuf,
750 /* Stream on/off */
751 .vidioc_streamon = vb2_ioctl_streamon,
752 .vidioc_streamoff = vb2_ioctl_streamoff,
753 /* Standard handling */
754 .vidioc_g_std = vidioc_g_std,
755 .vidioc_s_std = vidioc_s_std,
756 .vidioc_querystd = vidioc_querystd,
757 /* Input handling */
758 .vidioc_enum_input = vidioc_enum_input,
759 .vidioc_g_input = vidioc_g_input,
760 .vidioc_s_input = vidioc_s_input,
761 /* Log status ioctl */
762 .vidioc_log_status = v4l2_ctrl_log_status,
763 /* Event handling */
764 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
765 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
768 static struct video_device video_dev_template = {
769 .name = KBUILD_MODNAME,
770 .release = video_device_release_empty,
771 .fops = &vip_fops,
772 .ioctl_ops = &vip_ioctl_ops,
773 .tvnorms = V4L2_STD_ALL,
777 * vip_irq - interrupt routine
778 * @irq: Number of interrupt ( not used, correct number is assumed )
779 * @vip: local data structure containing all information
781 * check for both frame interrupts set ( top and bottom ).
782 * check FIFO overflow, but limit number of log messages after open.
783 * signal a complete buffer if done
785 * return value: IRQ_NONE, interrupt was not generated by VIP
787 * IRQ_HANDLED, interrupt done.
789 static irqreturn_t vip_irq(int irq, struct sta2x11_vip *vip)
791 unsigned int status;
793 status = reg_read(vip, DVP_ITS);
795 if (!status) /* No interrupt to handle */
796 return IRQ_NONE;
798 if (status & DVP_IT_FIFO)
799 if (vip->overflow++ > 5)
800 pr_info("VIP: fifo overflow\n");
802 if ((status & DVP_IT_VST) && (status & DVP_IT_VSB)) {
803 /* this is bad, we are too slow, hope the condition is gone
804 * on the next frame */
805 return IRQ_HANDLED;
808 if (status & DVP_IT_VST)
809 if ((++vip->tcount) < 2)
810 return IRQ_HANDLED;
811 if (status & DVP_IT_VSB) {
812 vip->bcount++;
813 return IRQ_HANDLED;
816 if (vip->active) { /* Acquisition is over on this buffer */
817 /* Disable acquisition */
818 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
819 /* Remove the active buffer from the list */
820 v4l2_get_timestamp(&vip->active->vb.timestamp);
821 vip->active->vb.sequence = vip->sequence++;
822 vb2_buffer_done(&vip->active->vb.vb2_buf, VB2_BUF_STATE_DONE);
825 return IRQ_HANDLED;
828 static void sta2x11_vip_init_register(struct sta2x11_vip *vip)
830 /* Register initialization */
831 spin_lock_irq(&vip->slock);
832 /* Clean interrupt */
833 reg_read(vip, DVP_ITS);
834 /* Enable Half Line per vertical */
835 reg_write(vip, DVP_HLFLN, DVP_HLFLN_SD);
836 /* Reset VIP control */
837 reg_write(vip, DVP_CTL, DVP_CTL_RST);
838 /* Clear VIP control */
839 reg_write(vip, DVP_CTL, 0);
840 spin_unlock_irq(&vip->slock);
842 static void sta2x11_vip_clear_register(struct sta2x11_vip *vip)
844 spin_lock_irq(&vip->slock);
845 /* Disable interrupt */
846 reg_write(vip, DVP_ITM, 0);
847 /* Reset VIP Control */
848 reg_write(vip, DVP_CTL, DVP_CTL_RST);
849 /* Clear VIP Control */
850 reg_write(vip, DVP_CTL, 0);
851 /* Clean VIP Interrupt */
852 reg_read(vip, DVP_ITS);
853 spin_unlock_irq(&vip->slock);
855 static int sta2x11_vip_init_buffer(struct sta2x11_vip *vip)
857 int err;
859 err = dma_set_coherent_mask(&vip->pdev->dev, DMA_BIT_MASK(29));
860 if (err) {
861 v4l2_err(&vip->v4l2_dev, "Cannot configure coherent mask");
862 return err;
864 memset(&vip->vb_vidq, 0, sizeof(struct vb2_queue));
865 vip->vb_vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
866 vip->vb_vidq.io_modes = VB2_MMAP | VB2_READ;
867 vip->vb_vidq.drv_priv = vip;
868 vip->vb_vidq.buf_struct_size = sizeof(struct vip_buffer);
869 vip->vb_vidq.ops = &vip_video_qops;
870 vip->vb_vidq.mem_ops = &vb2_dma_contig_memops;
871 vip->vb_vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
872 err = vb2_queue_init(&vip->vb_vidq);
873 if (err)
874 return err;
875 INIT_LIST_HEAD(&vip->buffer_list);
876 spin_lock_init(&vip->lock);
879 vip->alloc_ctx = vb2_dma_contig_init_ctx(&vip->pdev->dev);
880 if (IS_ERR(vip->alloc_ctx)) {
881 v4l2_err(&vip->v4l2_dev, "Can't allocate buffer context");
882 return PTR_ERR(vip->alloc_ctx);
885 return 0;
887 static void sta2x11_vip_release_buffer(struct sta2x11_vip *vip)
889 vb2_dma_contig_cleanup_ctx(vip->alloc_ctx);
891 static int sta2x11_vip_init_controls(struct sta2x11_vip *vip)
894 * Inititialize an empty control so VIP can inerithing controls
895 * from ADV7180
897 v4l2_ctrl_handler_init(&vip->ctrl_hdl, 0);
899 vip->v4l2_dev.ctrl_handler = &vip->ctrl_hdl;
900 if (vip->ctrl_hdl.error) {
901 int err = vip->ctrl_hdl.error;
903 v4l2_ctrl_handler_free(&vip->ctrl_hdl);
904 return err;
907 return 0;
911 * vip_gpio_reserve - reserve gpio pin
912 * @dev: device
913 * @pin: GPIO pin number
914 * @dir: direction, input or output
915 * @name: GPIO pin name
918 static int vip_gpio_reserve(struct device *dev, int pin, int dir,
919 const char *name)
921 int ret;
923 if (pin == -1)
924 return 0;
926 ret = gpio_request(pin, name);
927 if (ret) {
928 dev_err(dev, "Failed to allocate pin %d (%s)\n", pin, name);
929 return ret;
932 ret = gpio_direction_output(pin, dir);
933 if (ret) {
934 dev_err(dev, "Failed to set direction for pin %d (%s)\n",
935 pin, name);
936 gpio_free(pin);
937 return ret;
940 ret = gpio_export(pin, false);
941 if (ret) {
942 dev_err(dev, "Failed to export pin %d (%s)\n", pin, name);
943 gpio_free(pin);
944 return ret;
947 return 0;
951 * vip_gpio_release - release gpio pin
952 * @dev: device
953 * @pin: GPIO pin number
954 * @name: GPIO pin name
957 static void vip_gpio_release(struct device *dev, int pin, const char *name)
959 if (pin != -1) {
960 dev_dbg(dev, "releasing pin %d (%s)\n", pin, name);
961 gpio_unexport(pin);
962 gpio_free(pin);
967 * sta2x11_vip_init_one - init one instance of video device
968 * @pdev: PCI device
969 * @ent: (not used)
971 * allocate reset pins for DAC.
972 * Reset video DAC, this is done via reset line.
973 * allocate memory for managing device
974 * request interrupt
975 * map IO region
976 * register device
977 * find and initialize video DAC
979 * return value: 0, no error
981 * -ENOMEM, no memory
983 * -ENODEV, device could not be detected or registered
985 static int sta2x11_vip_init_one(struct pci_dev *pdev,
986 const struct pci_device_id *ent)
988 int ret;
989 struct sta2x11_vip *vip;
990 struct vip_config *config;
992 /* Check if hardware support 26-bit DMA */
993 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(26))) {
994 dev_err(&pdev->dev, "26-bit DMA addressing not available\n");
995 return -EINVAL;
997 /* Enable PCI */
998 ret = pci_enable_device(pdev);
999 if (ret)
1000 return ret;
1002 /* Get VIP platform data */
1003 config = dev_get_platdata(&pdev->dev);
1004 if (!config) {
1005 dev_info(&pdev->dev, "VIP slot disabled\n");
1006 ret = -EINVAL;
1007 goto disable;
1010 /* Power configuration */
1011 ret = vip_gpio_reserve(&pdev->dev, config->pwr_pin, 0,
1012 config->pwr_name);
1013 if (ret)
1014 goto disable;
1016 if (config->reset_pin >= 0) {
1017 ret = vip_gpio_reserve(&pdev->dev, config->reset_pin, 0,
1018 config->reset_name);
1019 if (ret) {
1020 vip_gpio_release(&pdev->dev, config->pwr_pin,
1021 config->pwr_name);
1022 goto disable;
1025 if (config->pwr_pin != -1) {
1026 /* Datasheet says 5ms between PWR and RST */
1027 usleep_range(5000, 25000);
1028 ret = gpio_direction_output(config->pwr_pin, 1);
1031 if (config->reset_pin != -1) {
1032 /* Datasheet says 5ms between PWR and RST */
1033 usleep_range(5000, 25000);
1034 ret = gpio_direction_output(config->reset_pin, 1);
1036 usleep_range(5000, 25000);
1038 /* Allocate a new VIP instance */
1039 vip = kzalloc(sizeof(struct sta2x11_vip), GFP_KERNEL);
1040 if (!vip) {
1041 ret = -ENOMEM;
1042 goto release_gpios;
1044 vip->pdev = pdev;
1045 vip->std = V4L2_STD_PAL;
1046 vip->format = formats_50[0];
1047 vip->config = config;
1049 ret = sta2x11_vip_init_controls(vip);
1050 if (ret)
1051 goto free_mem;
1052 ret = v4l2_device_register(&pdev->dev, &vip->v4l2_dev);
1053 if (ret)
1054 goto free_mem;
1056 dev_dbg(&pdev->dev, "BAR #0 at 0x%lx 0x%lx irq %d\n",
1057 (unsigned long)pci_resource_start(pdev, 0),
1058 (unsigned long)pci_resource_len(pdev, 0), pdev->irq);
1060 pci_set_master(pdev);
1062 ret = pci_request_regions(pdev, KBUILD_MODNAME);
1063 if (ret)
1064 goto unreg;
1066 vip->iomem = pci_iomap(pdev, 0, 0x100);
1067 if (!vip->iomem) {
1068 ret = -ENOMEM;
1069 goto release;
1072 pci_enable_msi(pdev);
1074 /* Initialize buffer */
1075 ret = sta2x11_vip_init_buffer(vip);
1076 if (ret)
1077 goto unmap;
1079 spin_lock_init(&vip->slock);
1081 ret = request_irq(pdev->irq,
1082 (irq_handler_t) vip_irq,
1083 IRQF_SHARED, KBUILD_MODNAME, vip);
1084 if (ret) {
1085 dev_err(&pdev->dev, "request_irq failed\n");
1086 ret = -ENODEV;
1087 goto release_buf;
1090 /* Initialize and register video device */
1091 vip->video_dev = video_dev_template;
1092 vip->video_dev.v4l2_dev = &vip->v4l2_dev;
1093 vip->video_dev.queue = &vip->vb_vidq;
1094 video_set_drvdata(&vip->video_dev, vip);
1096 ret = video_register_device(&vip->video_dev, VFL_TYPE_GRABBER, -1);
1097 if (ret)
1098 goto vrelease;
1100 /* Get ADV7180 subdevice */
1101 vip->adapter = i2c_get_adapter(vip->config->i2c_id);
1102 if (!vip->adapter) {
1103 ret = -ENODEV;
1104 dev_err(&pdev->dev, "no I2C adapter found\n");
1105 goto vunreg;
1108 vip->decoder = v4l2_i2c_new_subdev(&vip->v4l2_dev, vip->adapter,
1109 "adv7180", vip->config->i2c_addr,
1110 NULL);
1111 if (!vip->decoder) {
1112 ret = -ENODEV;
1113 dev_err(&pdev->dev, "no decoder found\n");
1114 goto vunreg;
1117 i2c_put_adapter(vip->adapter);
1118 v4l2_subdev_call(vip->decoder, core, init, 0);
1120 sta2x11_vip_init_register(vip);
1122 dev_info(&pdev->dev, "STA2X11 Video Input Port (VIP) loaded\n");
1123 return 0;
1125 vunreg:
1126 video_set_drvdata(&vip->video_dev, NULL);
1127 vrelease:
1128 video_unregister_device(&vip->video_dev);
1129 free_irq(pdev->irq, vip);
1130 release_buf:
1131 sta2x11_vip_release_buffer(vip);
1132 pci_disable_msi(pdev);
1133 unmap:
1134 vb2_queue_release(&vip->vb_vidq);
1135 pci_iounmap(pdev, vip->iomem);
1136 release:
1137 pci_release_regions(pdev);
1138 unreg:
1139 v4l2_device_unregister(&vip->v4l2_dev);
1140 free_mem:
1141 kfree(vip);
1142 release_gpios:
1143 vip_gpio_release(&pdev->dev, config->reset_pin, config->reset_name);
1144 vip_gpio_release(&pdev->dev, config->pwr_pin, config->pwr_name);
1145 disable:
1147 * do not call pci_disable_device on sta2x11 because it break all
1148 * other Bus masters on this EP
1150 return ret;
1154 * sta2x11_vip_remove_one - release device
1155 * @pdev: PCI device
1157 * Undo everything done in .._init_one
1159 * unregister video device
1160 * free interrupt
1161 * unmap ioadresses
1162 * free memory
1163 * free GPIO pins
1165 static void sta2x11_vip_remove_one(struct pci_dev *pdev)
1167 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1168 struct sta2x11_vip *vip =
1169 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1171 sta2x11_vip_clear_register(vip);
1173 video_set_drvdata(&vip->video_dev, NULL);
1174 video_unregister_device(&vip->video_dev);
1175 free_irq(pdev->irq, vip);
1176 pci_disable_msi(pdev);
1177 vb2_queue_release(&vip->vb_vidq);
1178 pci_iounmap(pdev, vip->iomem);
1179 pci_release_regions(pdev);
1181 v4l2_device_unregister(&vip->v4l2_dev);
1183 vip_gpio_release(&pdev->dev, vip->config->pwr_pin,
1184 vip->config->pwr_name);
1185 vip_gpio_release(&pdev->dev, vip->config->reset_pin,
1186 vip->config->reset_name);
1188 kfree(vip);
1190 * do not call pci_disable_device on sta2x11 because it break all
1191 * other Bus masters on this EP
1195 #ifdef CONFIG_PM
1198 * sta2x11_vip_suspend - set device into power save mode
1199 * @pdev: PCI device
1200 * @state: new state of device
1202 * all relevant registers are saved and an attempt to set a new state is made.
1204 * return value: 0 always indicate success,
1205 * even if device could not be disabled. (workaround for hardware problem)
1207 static int sta2x11_vip_suspend(struct pci_dev *pdev, pm_message_t state)
1209 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1210 struct sta2x11_vip *vip =
1211 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1212 unsigned long flags;
1213 int i;
1215 spin_lock_irqsave(&vip->slock, flags);
1216 vip->register_save_area[0] = reg_read(vip, DVP_CTL);
1217 reg_write(vip, DVP_CTL, vip->register_save_area[0] & DVP_CTL_DIS);
1218 vip->register_save_area[SAVE_COUNT] = reg_read(vip, DVP_ITM);
1219 reg_write(vip, DVP_ITM, 0);
1220 for (i = 1; i < SAVE_COUNT; i++)
1221 vip->register_save_area[i] = reg_read(vip, 4 * i);
1222 for (i = 0; i < AUX_COUNT; i++)
1223 vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i] =
1224 reg_read(vip, registers_to_save[i]);
1225 spin_unlock_irqrestore(&vip->slock, flags);
1226 /* save pci state */
1227 pci_save_state(pdev);
1228 if (pci_set_power_state(pdev, pci_choose_state(pdev, state))) {
1230 * do not call pci_disable_device on sta2x11 because it
1231 * break all other Bus masters on this EP
1233 vip->disabled = 1;
1236 pr_info("VIP: suspend\n");
1237 return 0;
1241 * sta2x11_vip_resume - resume device operation
1242 * @pdev : PCI device
1244 * re-enable device, set PCI state to powered and restore registers.
1245 * resume normal device operation afterwards.
1247 * return value: 0, no error.
1249 * other, could not set device to power on state.
1251 static int sta2x11_vip_resume(struct pci_dev *pdev)
1253 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1254 struct sta2x11_vip *vip =
1255 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1256 unsigned long flags;
1257 int ret, i;
1259 pr_info("VIP: resume\n");
1260 /* restore pci state */
1261 if (vip->disabled) {
1262 ret = pci_enable_device(pdev);
1263 if (ret) {
1264 pr_warn("VIP: Can't enable device.\n");
1265 return ret;
1267 vip->disabled = 0;
1269 ret = pci_set_power_state(pdev, PCI_D0);
1270 if (ret) {
1272 * do not call pci_disable_device on sta2x11 because it
1273 * break all other Bus masters on this EP
1275 pr_warn("VIP: Can't enable device.\n");
1276 vip->disabled = 1;
1277 return ret;
1280 pci_restore_state(pdev);
1282 spin_lock_irqsave(&vip->slock, flags);
1283 for (i = 1; i < SAVE_COUNT; i++)
1284 reg_write(vip, 4 * i, vip->register_save_area[i]);
1285 for (i = 0; i < AUX_COUNT; i++)
1286 reg_write(vip, registers_to_save[i],
1287 vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i]);
1288 reg_write(vip, DVP_CTL, vip->register_save_area[0]);
1289 reg_write(vip, DVP_ITM, vip->register_save_area[SAVE_COUNT]);
1290 spin_unlock_irqrestore(&vip->slock, flags);
1291 return 0;
1294 #endif
1296 static const struct pci_device_id sta2x11_vip_pci_tbl[] = {
1297 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_VIP)},
1298 {0,}
1301 static struct pci_driver sta2x11_vip_driver = {
1302 .name = KBUILD_MODNAME,
1303 .probe = sta2x11_vip_init_one,
1304 .remove = sta2x11_vip_remove_one,
1305 .id_table = sta2x11_vip_pci_tbl,
1306 #ifdef CONFIG_PM
1307 .suspend = sta2x11_vip_suspend,
1308 .resume = sta2x11_vip_resume,
1309 #endif
1312 static int __init sta2x11_vip_init_module(void)
1314 return pci_register_driver(&sta2x11_vip_driver);
1317 static void __exit sta2x11_vip_exit_module(void)
1319 pci_unregister_driver(&sta2x11_vip_driver);
1322 #ifdef MODULE
1323 module_init(sta2x11_vip_init_module);
1324 module_exit(sta2x11_vip_exit_module);
1325 #else
1326 late_initcall_sync(sta2x11_vip_init_module);
1327 #endif
1329 MODULE_DESCRIPTION("STA2X11 Video Input Port driver");
1330 MODULE_AUTHOR("Wind River");
1331 MODULE_LICENSE("GPL v2");
1332 MODULE_SUPPORTED_DEVICE("sta2x11 video input");
1333 MODULE_VERSION(DRV_VERSION);
1334 MODULE_DEVICE_TABLE(pci, sta2x11_vip_pci_tbl);