1 /*
2  * This is the driver for the STA2x11 Video Input Port.
3  *
4  * Copyright (C) 2010       WindRiver Systems, Inc.
5  *
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.
9  *
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
13  * more details.
14  *
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.
18  *
19  * The full GNU General Public License is included in this distribution in
20  * the file called "COPYING".
21  *
22  * Author: Andreas Kies <andreas.kies@windriver.com>
23  *		Vlad Lungu <vlad.lungu@windriver.com>
24  *
25  */
26 
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>
32 
33 #include <linux/videodev2.h>
34 
35 #include <linux/kmod.h>
36 
37 #include <linux/pci.h>
38 #include <linux/interrupt.h>
39 #include <linux/mutex.h>
40 #include <linux/io.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>
48 
49 #include "sta2x11_vip.h"
50 
51 #define DRV_NAME "sta2x11_vip"
52 #define DRV_VERSION "1.3"
53 
54 #ifndef PCI_DEVICE_ID_STMICRO_VIP
55 #define PCI_DEVICE_ID_STMICRO_VIP 0xCC0D
56 #endif
57 
58 #define MAX_FRAMES 4
59 
60 /*Register offsets*/
61 #define DVP_CTL		0x00
62 #define DVP_TFO		0x04
63 #define DVP_TFS		0x08
64 #define DVP_BFO		0x0C
65 #define DVP_BFS		0x10
66 #define DVP_VTP         0x14
67 #define DVP_VBP         0x18
68 #define DVP_VMP		0x1C
69 #define DVP_ITM		0x98
70 #define DVP_ITS		0x9C
71 #define DVP_STA		0xA0
72 #define DVP_HLFLN	0xA8
73 #define DVP_RGB		0xC0
74 #define DVP_PKZ		0xF0
75 
76 /*Register fields*/
77 #define DVP_CTL_ENA	0x00000001
78 #define DVP_CTL_RST	0x80000000
79 #define DVP_CTL_DIS	(~0x00040001)
80 
81 #define DVP_IT_VSB	0x00000008
82 #define DVP_IT_VST	0x00000010
83 #define DVP_IT_FIFO	0x00000020
84 
85 #define DVP_HLFLN_SD	0x00000001
86 
87 #define REG_WRITE(vip, reg, value) iowrite32((value), (vip->iomem)+(reg))
88 #define REG_READ(vip, reg) ioread32((vip->iomem)+(reg))
89 
90 #define SAVE_COUNT 8
91 #define AUX_COUNT 3
92 #define IRQ_COUNT 1
93 
94 /**
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
98  * @pdev: PCI 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
121  *
122  * All non-local data is accessed via this structure.
123  */
124 
125 struct sta2x11_vip {
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;
133 	v4l2_std_id std;
134 	unsigned int input;
135 	int users;
136 	int disabled;
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;
143 	int overflow;
144 	void *mem_spare;
145 	dma_addr_t dma_spare;
146 	void *iomem;
147 	struct vip_config *config;
148 };
149 
150 static const unsigned int registers_to_save[AUX_COUNT] = {
151 	DVP_HLFLN, DVP_RGB, DVP_PKZ
152 };
153 
154 static struct v4l2_pix_format formats_50[] = {
155 	{			/*PAL interlaced */
156 	 .width = 720,
157 	 .height = 576,
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},
163 	{			/*PAL top */
164 	 .width = 720,
165 	 .height = 288,
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},
171 	{			/*PAL bottom */
172 	 .width = 720,
173 	 .height = 288,
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},
179 
180 };
181 
182 static struct v4l2_pix_format formats_60[] = {
183 	{			/*NTSC interlaced */
184 	 .width = 720,
185 	 .height = 480,
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},
191 	{			/*NTSC top */
192 	 .width = 720,
193 	 .height = 240,
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},
199 	{			/*NTSC bottom */
200 	 .width = 720,
201 	 .height = 240,
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},
207 };
208 
209 /**
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
215  *
216  * returns size and number of buffers
217  * a preset value of 0 returns the default number.
218  * return value: 0, always succesfull.
219  */
220 static int buf_setup(struct videobuf_queue *vq, unsigned int *count,
221 		     unsigned int *size)
222 {
223 	struct sta2x11_vip *vip = vq->priv_data;
224 
225 	*size = vip->format.width * vip->format.height * 2;
226 	if (0 == *count || MAX_FRAMES < *count)
227 		*count = MAX_FRAMES;
228 	return 0;
229 };
230 
231 /**
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)
236  *
237  * Allocate or realloc buffer
238  * return value: 0, successful.
239  *
240  * -EINVAL, supplied buffer is too small.
241  *
242  *  other, buffer could not be locked.
243  */
244 static int buf_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
245 		       enum v4l2_field field)
246 {
247 	struct sta2x11_vip *vip = vq->priv_data;
248 	int ret;
249 
250 	vb->size = vip->format.width * vip->format.height * 2;
251 	if ((0 != vb->baddr) && (vb->bsize < vb->size))
252 		return -EINVAL;
253 	vb->width = vip->format.width;
254 	vb->height = vip->format.height;
255 	vb->field = field;
256 
257 	if (VIDEOBUF_NEEDS_INIT == vb->state) {
258 		ret = videobuf_iolock(vq, vb, NULL);
259 		if (ret)
260 			goto fail;
261 	}
262 	vb->state = VIDEOBUF_PREPARED;
263 	return 0;
264 fail:
265 	videobuf_dma_contig_free(vq, vb);
266 	vb->state = VIDEOBUF_NEEDS_INIT;
267 	return ret;
268 }
269 
270 /**
271  * buf_queu - queue buffer for filling
272  * @vq: queue in videobuf layer
273  * @vb: buffer to be queued
274  *
275  * if capturing is already running, the buffer will be queued. Otherwise
276  * capture is started and the buffer is used directly.
277  */
278 static void buf_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
279 {
280 	struct sta2x11_vip *vip = vq->priv_data;
281 	u32 dma;
282 
283 	vb->state = VIDEOBUF_QUEUED;
284 
285 	if (vip->active) {
286 		list_add_tail(&vb->queue, &vip->capture);
287 		return;
288 	}
289 
290 	vip->started = 1;
291 	vip->tcount = 0;
292 	vip->bcount = 0;
293 	vip->active = vb;
294 	vb->state = VIDEOBUF_ACTIVE;
295 
296 	dma = videobuf_to_dma_contig(vb);
297 
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));
301 
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);
312 		break;
313 	case V4L2_FIELD_TOP:
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);
322 		break;
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);
332 		break;
333 
334 	default:
335 		pr_warning("VIP: unknown field format\n");
336 		return;
337 	}
338 
339 	REG_WRITE(vip, DVP_CTL, DVP_CTL_ENA);
340 }
341 
342 /**
343  * buff_release - release buffer
344  * @vq: queue in videobuf layer
345  * @vb: buffer to be released
346  *
347  * release buffer in videobuf layer
348  */
349 static void buf_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
350 {
351 
352 	videobuf_dma_contig_free(vq, vb);
353 	vb->state = VIDEOBUF_NEEDS_INIT;
354 }
355 
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,
361 };
362 
363 /**
364  * vip_open - open video device
365  * @file: descriptor of device
366  *
367  * open device, make sure it is only opened once.
368  * return value: 0, no error.
369  *
370  * -EBUSY, device is already opened
371  *
372  * -ENOMEM, no memory for auxiliary DMA buffer
373  */
374 static int vip_open(struct file *file)
375 {
376 	struct video_device *dev = video_devdata(file);
377 	struct sta2x11_vip *vip = video_get_drvdata(dev);
378 
379 	mutex_lock(&vip->mutex);
380 	vip->users++;
381 
382 	if (vip->users > 1) {
383 		vip->users--;
384 		mutex_unlock(&vip->mutex);
385 		return -EBUSY;
386 	}
387 
388 	file->private_data = dev;
389 	vip->overflow = 0;
390 	vip->started = 0;
391 	vip->closing = 0;
392 	vip->active = NULL;
393 
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) {
398 		vip->users--;
399 		mutex_unlock(&vip->mutex);
400 		return -ENOMEM;
401 	}
402 
403 	mutex_unlock(&vip->mutex);
404 	videobuf_queue_dma_contig_init_cached(&vip->vb_vidq,
405 					      &vip_qops,
406 					      &vip->pdev->dev,
407 					      &vip->slock,
408 					      V4L2_BUF_TYPE_VIDEO_CAPTURE,
409 					      V4L2_FIELD_INTERLACED,
410 					      sizeof(struct videobuf_buffer),
411 					      vip, NULL);
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);
418 	return 0;
419 }
420 
421 /**
422  * vip_close - close video device
423  * @file: descriptor of device
424  *
425  * close video device, wait until all pending operations are finished
426  * ( maximum FRAME_MAX buffers pending )
427  * Turn off interrupts.
428  *
429  * return value: 0, always succesful.
430  */
431 static int vip_close(struct file *file)
432 {
433 	struct video_device *dev = video_devdata(file);
434 	struct sta2x11_vip *vip = video_get_drvdata(dev);
435 
436 	vip->closing = 1;
437 	if (vip->active)
438 		videobuf_waiton(&vip->vb_vidq, vip->active, 0, 0);
439 	spin_lock_irq(&vip->slock);
440 
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);
445 
446 	vip->started = 0;
447 	vip->active = NULL;
448 
449 	spin_unlock_irq(&vip->slock);
450 
451 	videobuf_stop(&vip->vb_vidq);
452 	videobuf_mmap_free(&vip->vb_vidq);
453 
454 	dma_free_coherent(&vip->pdev->dev, 64, vip->mem_spare, vip->dma_spare);
455 	file->private_data = NULL;
456 	mutex_lock(&vip->mutex);
457 	vip->users--;
458 	mutex_unlock(&vip->mutex);
459 	return 0;
460 }
461 
462 /**
463  * vip_read - read from video input
464  * @file: descriptor of device
465  * @data: user buffer
466  * @count: number of bytes to be read
467  * @ppos: position within stream
468  *
469  * read video data from video device.
470  * handling is done in generic videobuf layer
471  * return value: provided by videobuf layer
472  */
473 static ssize_t vip_read(struct file *file, char __user *data,
474 			size_t count, loff_t *ppos)
475 {
476 	struct video_device *dev = file->private_data;
477 	struct sta2x11_vip *vip = video_get_drvdata(dev);
478 
479 	return videobuf_read_stream(&vip->vb_vidq, data, count, ppos, 0,
480 				    file->f_flags & O_NONBLOCK);
481 }
482 
483 /**
484  * vip_mmap - map user buffer
485  * @file: descriptor of device
486  * @vma: user buffer
487  *
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
491  */
492 static int vip_mmap(struct file *file, struct vm_area_struct *vma)
493 {
494 	struct video_device *dev = file->private_data;
495 	struct sta2x11_vip *vip = video_get_drvdata(dev);
496 
497 	return videobuf_mmap_mapper(&vip->vb_vidq, vma);
498 }
499 
500 /**
501  * vip_poll - poll for event
502  * @file: descriptor of device
503  * @wait: contains events to be waited for
504  *
505  * wait for event related to video device.
506  * handling is done in generic videobuf layer.
507  * return value: provided by videobuf layer
508  */
509 static unsigned int vip_poll(struct file *file, struct poll_table_struct *wait)
510 {
511 	struct video_device *dev = file->private_data;
512 	struct sta2x11_vip *vip = video_get_drvdata(dev);
513 
514 	return videobuf_poll_stream(file, &vip->vb_vidq, wait);
515 }
516 
517 /**
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
522  *
523  * the capabilities of the device are returned
524  *
525  * return value: 0, no error.
526  */
527 static int vidioc_querycap(struct file *file, void *priv,
528 			   struct v4l2_capability *cap)
529 {
530 	struct video_device *dev = priv;
531 	struct sta2x11_vip *vip = video_get_drvdata(dev);
532 
533 	memset(cap, 0, sizeof(struct v4l2_capability));
534 	strcpy(cap->driver, DRV_NAME);
535 	strcpy(cap->card, DRV_NAME);
536 	cap->version = 0;
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 |
540 	    V4L2_CAP_STREAMING;
541 
542 	return 0;
543 }
544 
545 /**
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
550  *
551  * the video standard is set
552  *
553  * return value: 0, no error.
554  *
555  * -EIO, no input signal detected
556  *
557  * other, returned from video DAC.
558  */
559 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *std)
560 {
561 	struct video_device *dev = priv;
562 	struct sta2x11_vip *vip = video_get_drvdata(dev);
563 	v4l2_std_id oldstd = vip->std, newstd;
564 	int status;
565 
566 	if (V4L2_STD_ALL == *std) {
567 		v4l2_subdev_call(vip->decoder, core, s_std, *std);
568 		ssleep(2);
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)
572 			return -EIO;
573 		*std = vip->std = newstd;
574 		if (oldstd != *std) {
575 			if (V4L2_STD_525_60 & (*std))
576 				vip->format = formats_60[0];
577 			else
578 				vip->format = formats_50[0];
579 		}
580 		return 0;
581 	}
582 
583 	if (oldstd != *std) {
584 		if (V4L2_STD_525_60 & (*std))
585 			vip->format = formats_60[0];
586 		else
587 			vip->format = formats_50[0];
588 	}
589 
590 	return v4l2_subdev_call(vip->decoder, core, s_std, *std);
591 }
592 
593 /**
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
598  *
599  * the current video standard is returned
600  *
601  * return value: 0, no error.
602  */
603 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
604 {
605 	struct video_device *dev = priv;
606 	struct sta2x11_vip *vip = video_get_drvdata(dev);
607 
608 	*std = vip->std;
609 	return 0;
610 }
611 
612 /**
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
617  *
618  * all possible video standards are returned
619  *
620  * return value: delivered by video DAC routine.
621  */
622 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
623 {
624 	struct video_device *dev = priv;
625 	struct sta2x11_vip *vip = video_get_drvdata(dev);
626 
627 	return v4l2_subdev_call(vip->decoder, video, querystd, std);
628 
629 }
630 
631 /**
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
636  *
637  * return possible values for a control
638  * return value: delivered by video DAC routine.
639  */
640 static int vidioc_queryctrl(struct file *file, void *priv,
641 			    struct v4l2_queryctrl *ctrl)
642 {
643 	struct video_device *dev = priv;
644 	struct sta2x11_vip *vip = video_get_drvdata(dev);
645 
646 	return v4l2_subdev_call(vip->decoder, core, queryctrl, ctrl);
647 }
648 
649 /**
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
654  *
655  * return setting for a control value
656  * return value: delivered by video DAC routine.
657  */
658 static int vidioc_g_ctrl(struct file *file, void *priv,
659 			 struct v4l2_control *ctrl)
660 {
661 	struct video_device *dev = priv;
662 	struct sta2x11_vip *vip = video_get_drvdata(dev);
663 
664 	return v4l2_subdev_call(vip->decoder, core, g_ctrl, ctrl);
665 }
666 
667 /**
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
672  *
673  * set value for a specific control
674  * return value: delivered by video DAC routine.
675  */
676 static int vidioc_s_ctrl(struct file *file, void *priv,
677 			 struct v4l2_control *ctrl)
678 {
679 	struct video_device *dev = priv;
680 	struct sta2x11_vip *vip = video_get_drvdata(dev);
681 
682 	return v4l2_subdev_call(vip->decoder, core, s_ctrl, ctrl);
683 }
684 
685 /**
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
690  *
691  * the user friendly name of the input line is returned
692  *
693  * return value: 0, no error.
694  *
695  * -EINVAL, input line number out of range
696  */
697 static int vidioc_enum_input(struct file *file, void *priv,
698 			     struct v4l2_input *inp)
699 {
700 	if (inp->index > 1)
701 		return -EINVAL;
702 
703 	inp->type = V4L2_INPUT_TYPE_CAMERA;
704 	inp->std = V4L2_STD_ALL;
705 	sprintf(inp->name, "Camera %u", inp->index);
706 
707 	return 0;
708 }
709 
710 /**
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
715  *
716  * the current active input line is set
717  *
718  * return value: 0, no error.
719  *
720  * -EINVAL, line number out of range
721  */
722 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
723 {
724 	struct video_device *dev = priv;
725 	struct sta2x11_vip *vip = video_get_drvdata(dev);
726 	int ret;
727 
728 	if (i > 1)
729 		return -EINVAL;
730 	ret = v4l2_subdev_call(vip->decoder, video, s_routing, i, 0, 0);
731 
732 	if (!ret)
733 		vip->input = i;
734 
735 	return 0;
736 }
737 
738 /**
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
743  *
744  * the current active input line is returned
745  *
746  * return value: always 0.
747  */
748 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
749 {
750 	struct video_device *dev = priv;
751 	struct sta2x11_vip *vip = video_get_drvdata(dev);
752 
753 	*i = vip->input;
754 	return 0;
755 }
756 
757 /**
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
762  *
763  * returns name and format of video capture
764  * Only UYVY is supported by hardware.
765  *
766  * return value: always 0.
767  */
768 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
769 				   struct v4l2_fmtdesc *f)
770 {
771 
772 	if (f->index != 0)
773 		return -EINVAL;
774 
775 	strcpy(f->description, "4:2:2, packed, UYVY");
776 	f->pixelformat = V4L2_PIX_FMT_UYVY;
777 	f->flags = 0;
778 	return 0;
779 }
780 
781 /**
782  * vidioc_try_fmt_vid_cap - set video capture format
783  * @file: descriptor of device ( not used)
784  * @priv: points to current videodevice
785  * @f: new format
786  *
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)
791  *
792  * return value: 0, no error
793  *
794  * -EINVAL, pixel or field format not supported
795  *
796  */
797 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
798 				  struct v4l2_format *f)
799 {
800 	struct video_device *dev = priv;
801 	struct sta2x11_vip *vip = video_get_drvdata(dev);
802 	int interlace_lim;
803 
804 	if (V4L2_PIX_FMT_UYVY != f->fmt.pix.pixelformat)
805 		return -EINVAL;
806 
807 	if (V4L2_STD_525_60 & vip->std)
808 		interlace_lim = 240;
809 	else
810 		interlace_lim = 288;
811 
812 	switch (f->fmt.pix.field) {
813 	case V4L2_FIELD_ANY:
814 		if (interlace_lim < f->fmt.pix.height)
815 			f->fmt.pix.field = V4L2_FIELD_INTERLACED;
816 		else
817 			f->fmt.pix.field = V4L2_FIELD_BOTTOM;
818 		break;
819 	case V4L2_FIELD_TOP:
820 	case V4L2_FIELD_BOTTOM:
821 		if (interlace_lim < f->fmt.pix.height)
822 			f->fmt.pix.height = interlace_lim;
823 		break;
824 	case V4L2_FIELD_INTERLACED:
825 		break;
826 	default:
827 		return -EINVAL;
828 	}
829 
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;
839 	f->fmt.pix.priv = 0;
840 	return 0;
841 }
842 
843 /**
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
848  *
849  * set new capture format
850  * return value: 0, no error
851  *
852  * other, delivered by video DAC routine.
853  */
854 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
855 				struct v4l2_format *f)
856 {
857 	struct video_device *dev = priv;
858 	struct sta2x11_vip *vip = video_get_drvdata(dev);
859 	int ret;
860 
861 	ret = vidioc_try_fmt_vid_cap(file, priv, f);
862 	if (ret)
863 		return ret;
864 
865 	memcpy(&vip->format, &f->fmt.pix, sizeof(struct v4l2_pix_format));
866 	return 0;
867 }
868 
869 /**
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
874  *
875  * returns current video format parameters
876  *
877  * return value: 0, always successful
878  */
879 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
880 				struct v4l2_format *f)
881 {
882 	struct video_device *dev = priv;
883 	struct sta2x11_vip *vip = video_get_drvdata(dev);
884 
885 	memcpy(&f->fmt.pix, &vip->format, sizeof(struct v4l2_pix_format));
886 	return 0;
887 }
888 
889 /**
890  * vidioc_reqfs - request buffer
891  * @file: descriptor of device ( not used)
892  * @priv: points to current videodevice
893  * @p: video buffer
894  *
895  * Handling is done in generic videobuf layer.
896  */
897 static int vidioc_reqbufs(struct file *file, void *priv,
898 			  struct v4l2_requestbuffers *p)
899 {
900 	struct video_device *dev = priv;
901 	struct sta2x11_vip *vip = video_get_drvdata(dev);
902 
903 	return videobuf_reqbufs(&vip->vb_vidq, p);
904 }
905 
906 /**
907  * vidioc_querybuf - query buffer
908  * @file: descriptor of device ( not used)
909  * @priv: points to current videodevice
910  * @p: video buffer
911  *
912  * query buffer state.
913  * Handling is done in generic videobuf layer.
914  */
915 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
916 {
917 	struct video_device *dev = priv;
918 	struct sta2x11_vip *vip = video_get_drvdata(dev);
919 
920 	return videobuf_querybuf(&vip->vb_vidq, p);
921 }
922 
923 /**
924  * vidioc_qbuf - queue a buffer
925  * @file: descriptor of device ( not used)
926  * @priv: points to current videodevice
927  * @p: video buffer
928  *
929  * Handling is done in generic videobuf layer.
930  */
931 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
932 {
933 	struct video_device *dev = priv;
934 	struct sta2x11_vip *vip = video_get_drvdata(dev);
935 
936 	return videobuf_qbuf(&vip->vb_vidq, p);
937 }
938 
939 /**
940  * vidioc_dqbuf - dequeue a buffer
941  * @file: descriptor of device ( not used)
942  * @priv: points to current videodevice
943  * @p: video buffer
944  *
945  * Handling is done in generic videobuf layer.
946  */
947 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
948 {
949 	struct video_device *dev = priv;
950 	struct sta2x11_vip *vip = video_get_drvdata(dev);
951 
952 	return videobuf_dqbuf(&vip->vb_vidq, p, file->f_flags & O_NONBLOCK);
953 }
954 
955 /**
956  * vidioc_streamon - turn on streaming
957  * @file: descriptor of device ( not used)
958  * @priv: points to current videodevice
959  * @type: type of capture
960  *
961  * turn on streaming.
962  * Handling is done in generic videobuf layer.
963  */
964 static int vidioc_streamon(struct file *file, void *priv,
965 			   enum v4l2_buf_type type)
966 {
967 	struct video_device *dev = priv;
968 	struct sta2x11_vip *vip = video_get_drvdata(dev);
969 
970 	return videobuf_streamon(&vip->vb_vidq);
971 }
972 
973 /**
974  * vidioc_streamoff - turn off streaming
975  * @file: descriptor of device ( not used)
976  * @priv: points to current videodevice
977  * @type: type of capture
978  *
979  * turn off streaming.
980  * Handling is done in generic videobuf layer.
981  */
982 static int vidioc_streamoff(struct file *file, void *priv,
983 			    enum v4l2_buf_type type)
984 {
985 	struct video_device *dev = priv;
986 	struct sta2x11_vip *vip = video_get_drvdata(dev);
987 
988 	return videobuf_streamoff(&vip->vb_vidq);
989 }
990 
991 static const struct v4l2_file_operations vip_fops = {
992 	.owner = THIS_MODULE,
993 	.open = vip_open,
994 	.release = vip_close,
995 	.ioctl = video_ioctl2,
996 	.read = vip_read,
997 	.mmap = vip_mmap,
998 	.poll = vip_poll
999 };
1000 
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,
1022 };
1023 
1024 static struct video_device video_dev_template = {
1025 	.name = DRV_NAME,
1026 	.release = video_device_release,
1027 	.fops = &vip_fops,
1028 	.ioctl_ops = &vip_ioctl_ops,
1029 	.tvnorms = V4L2_STD_ALL,
1030 };
1031 
1032 /**
1033  * vip_irq - interrupt routine
1034  * @irq: Number of interrupt ( not used, correct number is assumed )
1035  * @vip: local data structure containing all information
1036  *
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.
1042  *
1043  * return value: IRQ_NONE, interrupt was not generated by VIP
1044  *
1045  * IRQ_HANDLED, interrupt done.
1046  */
1047 static irqreturn_t vip_irq(int irq, struct sta2x11_vip *vip)
1048 {
1049 	u32 status, dma;
1050 	unsigned long flags;
1051 	struct videobuf_buffer *vb;
1052 
1053 	status = REG_READ(vip, DVP_ITS);
1054 
1055 	if (!status) {
1056 		pr_debug("VIP: irq ignored\n");
1057 		return IRQ_NONE;
1058 	}
1059 
1060 	if (!vip->started)
1061 		return IRQ_HANDLED;
1062 
1063 	if (status & DVP_IT_VSB)
1064 		vip->bcount++;
1065 
1066 	if (status & DVP_IT_VST)
1067 		vip->tcount++;
1068 
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");
1073 		return IRQ_HANDLED;
1074 	}
1075 
1076 	if (status & DVP_IT_FIFO) {
1077 		if (5 > vip->overflow++)
1078 			pr_info("VIP: fifo overflow\n");
1079 	}
1080 
1081 	if (2 > vip->tcount)
1082 		return IRQ_HANDLED;
1083 
1084 	if (status & DVP_IT_VSB)
1085 		return IRQ_HANDLED;
1086 
1087 	spin_lock_irqsave(&vip->slock, flags);
1088 
1089 	REG_WRITE(vip, DVP_CTL, REG_READ(vip, DVP_CTL) & ~DVP_CTL_ENA);
1090 	if (vip->active) {
1091 		do_gettimeofday(&vip->active->ts);
1092 		vip->active->field_count++;
1093 		vip->active->state = VIDEOBUF_DONE;
1094 		wake_up(&vip->active->done);
1095 		vip->active = NULL;
1096 	}
1097 	if (!vip->closing) {
1098 		if (list_empty(&vip->capture))
1099 			goto done;
1100 
1101 		vb = list_first_entry(&vip->capture, struct videobuf_buffer,
1102 				      queue);
1103 		if (NULL == vb) {
1104 			pr_info("VIP: no buffer\n");
1105 			goto done;
1106 		}
1107 		vb->state = VIDEOBUF_ACTIVE;
1108 		list_del(&vb->queue);
1109 		vip->active = vb;
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);
1115 			break;
1116 		case V4L2_FIELD_TOP:
1117 		case V4L2_FIELD_BOTTOM:
1118 			REG_WRITE(vip, DVP_VTP, dma);
1119 			REG_WRITE(vip, DVP_VBP, dma);
1120 			break;
1121 		default:
1122 			pr_warning("VIP: unknown field format\n");
1123 			goto done;
1124 			break;
1125 		}
1126 		REG_WRITE(vip, DVP_CTL, REG_READ(vip, DVP_CTL) | DVP_CTL_ENA);
1127 	}
1128 done:
1129 	spin_unlock_irqrestore(&vip->slock, flags);
1130 	return IRQ_HANDLED;
1131 }
1132 
1133 /**
1134  * vip_gpio_reserve - reserve gpio pin
1135  * @dev: device
1136  * @pin: GPIO pin number
1137  * @dir: direction, input or output
1138  * @name: GPIO pin name
1139  *
1140  */
1141 static int vip_gpio_reserve(struct device *dev, int pin, int dir,
1142 			    const char *name)
1143 {
1144 	int ret;
1145 
1146 	if (pin == -1)
1147 		return 0;
1148 
1149 	ret = gpio_request(pin, name);
1150 	if (ret) {
1151 		dev_err(dev, "Failed to allocate pin %d (%s)\n", pin, name);
1152 		return ret;
1153 	}
1154 
1155 	ret = gpio_direction_output(pin, dir);
1156 	if (ret) {
1157 		dev_err(dev, "Failed to set direction for pin %d (%s)\n",
1158 			pin, name);
1159 		gpio_free(pin);
1160 		return ret;
1161 	}
1162 
1163 	ret = gpio_export(pin, false);
1164 	if (ret) {
1165 		dev_err(dev, "Failed to export pin %d (%s)\n", pin, name);
1166 		gpio_free(pin);
1167 		return ret;
1168 	}
1169 
1170 	return 0;
1171 }
1172 
1173 /**
1174  * vip_gpio_release - release gpio pin
1175  * @dev: device
1176  * @pin: GPIO pin number
1177  * @name: GPIO pin name
1178  *
1179  */
1180 static void vip_gpio_release(struct device *dev, int pin, const char *name)
1181 {
1182 	if (pin != -1) {
1183 		dev_dbg(dev, "releasing pin %d (%s)\n",	pin, name);
1184 		gpio_unexport(pin);
1185 		gpio_free(pin);
1186 	}
1187 }
1188 
1189 /**
1190  * sta2x11_vip_init_one - init one instance of video device
1191  * @pdev: PCI device
1192  * @ent: (not used)
1193  *
1194  * allocate reset pins for DAC.
1195  * Reset video DAC, this is done via reset line.
1196  * allocate memory for managing device
1197  * request interrupt
1198  * map IO region
1199  * register device
1200  * find and initialize video DAC
1201  *
1202  * return value: 0, no error
1203  *
1204  * -ENOMEM, no memory
1205  *
1206  * -ENODEV, device could not be detected or registered
1207  */
1208 static int sta2x11_vip_init_one(struct pci_dev *pdev,
1209 				const struct pci_device_id *ent)
1210 {
1211 	int ret;
1212 	struct sta2x11_vip *vip;
1213 	struct vip_config *config;
1214 
1215 	ret = pci_enable_device(pdev);
1216 	if (ret)
1217 		return ret;
1218 
1219 	config = dev_get_platdata(&pdev->dev);
1220 	if (!config) {
1221 		dev_info(&pdev->dev, "VIP slot disabled\n");
1222 		ret = -EINVAL;
1223 		goto disable;
1224 	}
1225 
1226 	ret = vip_gpio_reserve(&pdev->dev, config->pwr_pin, 0,
1227 			       config->pwr_name);
1228 	if (ret)
1229 		goto disable;
1230 
1231 	if (config->reset_pin >= 0) {
1232 		ret = vip_gpio_reserve(&pdev->dev, config->reset_pin, 0,
1233 				       config->reset_name);
1234 		if (ret) {
1235 			vip_gpio_release(&pdev->dev, config->pwr_pin,
1236 					 config->pwr_name);
1237 			goto disable;
1238 		}
1239 	}
1240 
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);
1245 	}
1246 
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);
1251 	}
1252 	usleep_range(5000, 25000);
1253 
1254 	vip = kzalloc(sizeof(struct sta2x11_vip), GFP_KERNEL);
1255 	if (!vip) {
1256 		ret = -ENOMEM;
1257 		goto release_gpios;
1258 	}
1259 
1260 	vip->pdev = pdev;
1261 	vip->std = V4L2_STD_PAL;
1262 	vip->format = formats_50[0];
1263 	vip->config = config;
1264 
1265 	if (v4l2_device_register(&pdev->dev, &vip->v4l2_dev))
1266 		goto free_mem;
1267 
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);
1271 
1272 	pci_set_master(pdev);
1273 
1274 	ret = pci_request_regions(pdev, DRV_NAME);
1275 	if (ret)
1276 		goto unreg;
1277 
1278 	vip->iomem = pci_iomap(pdev, 0, 0x100);
1279 	if (!vip->iomem) {
1280 		ret = -ENOMEM; /* FIXME */
1281 		goto release;
1282 	}
1283 
1284 	pci_enable_msi(pdev);
1285 
1286 	INIT_LIST_HEAD(&vip->capture);
1287 	spin_lock_init(&vip->slock);
1288 	mutex_init(&vip->mutex);
1289 	vip->started = 0;
1290 	vip->disabled = 0;
1291 
1292 	ret = request_irq(pdev->irq,
1293 			  (irq_handler_t) vip_irq,
1294 			  IRQF_SHARED, DRV_NAME, vip);
1295 	if (ret) {
1296 		dev_err(&pdev->dev, "request_irq failed\n");
1297 		ret = -ENODEV;
1298 		goto unmap;
1299 	}
1300 
1301 	vip->video_dev = video_device_alloc();
1302 	if (!vip->video_dev) {
1303 		ret = -ENOMEM;
1304 		goto release_irq;
1305 	}
1306 
1307 	*(vip->video_dev) = video_dev_template;
1308 	video_set_drvdata(vip->video_dev, vip);
1309 
1310 	ret = video_register_device(vip->video_dev, VFL_TYPE_GRABBER, -1);
1311 	if (ret)
1312 		goto vrelease;
1313 
1314 	vip->adapter = i2c_get_adapter(vip->config->i2c_id);
1315 	if (!vip->adapter) {
1316 		ret = -ENODEV;
1317 		dev_err(&pdev->dev, "no I2C adapter found\n");
1318 		goto vunreg;
1319 	}
1320 
1321 	vip->decoder = v4l2_i2c_new_subdev(&vip->v4l2_dev, vip->adapter,
1322 					   "adv7180", vip->config->i2c_addr,
1323 					   NULL);
1324 	if (!vip->decoder) {
1325 		ret = -ENODEV;
1326 		dev_err(&pdev->dev, "no decoder found\n");
1327 		goto vunreg;
1328 	}
1329 
1330 	i2c_put_adapter(vip->adapter);
1331 
1332 	v4l2_subdev_call(vip->decoder, core, init, 0);
1333 
1334 	pr_info("STA2X11 Video Input Port (VIP) loaded\n");
1335 	return 0;
1336 
1337 vunreg:
1338 	video_set_drvdata(vip->video_dev, NULL);
1339 vrelease:
1340 	if (video_is_registered(vip->video_dev))
1341 		video_unregister_device(vip->video_dev);
1342 	else
1343 		video_device_release(vip->video_dev);
1344 release_irq:
1345 	free_irq(pdev->irq, vip);
1346 	pci_disable_msi(pdev);
1347 unmap:
1348 	pci_iounmap(pdev, vip->iomem);
1349 	mutex_destroy(&vip->mutex);
1350 release:
1351 	pci_release_regions(pdev);
1352 unreg:
1353 	v4l2_device_unregister(&vip->v4l2_dev);
1354 free_mem:
1355 	kfree(vip);
1356 release_gpios:
1357 	vip_gpio_release(&pdev->dev, config->reset_pin, config->reset_name);
1358 	vip_gpio_release(&pdev->dev, config->pwr_pin, config->pwr_name);
1359 disable:
1360 	/*
1361 	 * do not call pci_disable_device on sta2x11 because it break all
1362 	 * other Bus masters on this EP
1363 	 */
1364 	return ret;
1365 }
1366 
1367 /**
1368  * sta2x11_vip_remove_one - release device
1369  * @pdev: PCI device
1370  *
1371  * Undo everything done in .._init_one
1372  *
1373  * unregister video device
1374  * free interrupt
1375  * unmap ioadresses
1376  * free memory
1377  * free GPIO pins
1378  */
1379 static void sta2x11_vip_remove_one(struct pci_dev *pdev)
1380 {
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);
1384 
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);
1392 
1393 	v4l2_device_unregister(&vip->v4l2_dev);
1394 	mutex_destroy(&vip->mutex);
1395 
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);
1400 
1401 	kfree(vip);
1402 	/*
1403 	 * do not call pci_disable_device on sta2x11 because it break all
1404 	 * other Bus masters on this EP
1405 	 */
1406 }
1407 
1408 #ifdef CONFIG_PM
1409 
1410 /**
1411  * sta2x11_vip_suspend - set device into power save mode
1412  * @pdev: PCI device
1413  * @state: new state of device
1414  *
1415  * all relevant registers are saved and an attempt to set a new state is made.
1416  *
1417  * return value: 0 always indicate success,
1418  * even if device could not be disabled. (workaround for hardware problem)
1419  *
1420  * reurn value : 0, always succesful, even if hardware does not not support
1421  * power down mode.
1422  */
1423 static int sta2x11_vip_suspend(struct pci_dev *pdev, pm_message_t state)
1424 {
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;
1429 	int i;
1430 
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))) {
1445 		/*
1446 		 * do not call pci_disable_device on sta2x11 because it
1447 		 * break all other Bus masters on this EP
1448 		 */
1449 		vip->disabled = 1;
1450 	}
1451 
1452 	pr_info("VIP: suspend\n");
1453 	return 0;
1454 }
1455 
1456 /**
1457  * sta2x11_vip_resume - resume device operation
1458  * @pdev : PCI device
1459  *
1460  * re-enable device, set PCI state to powered and restore registers.
1461  * resume normal device operation afterwards.
1462  *
1463  * return value: 0, no error.
1464  *
1465  * other, could not set device to power on state.
1466  */
1467 static int sta2x11_vip_resume(struct pci_dev *pdev)
1468 {
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;
1473 	int ret, i;
1474 
1475 	pr_info("VIP: resume\n");
1476 	/* restore pci state */
1477 	if (vip->disabled) {
1478 		ret = pci_enable_device(pdev);
1479 		if (ret) {
1480 			pr_warning("VIP: Can't enable device.\n");
1481 			return ret;
1482 		}
1483 		vip->disabled = 0;
1484 	}
1485 	ret = pci_set_power_state(pdev, PCI_D0);
1486 	if (ret) {
1487 		/*
1488 		 * do not call pci_disable_device on sta2x11 because it
1489 		 * break all other Bus masters on this EP
1490 		 */
1491 		pr_warning("VIP: Can't enable device.\n");
1492 		vip->disabled = 1;
1493 		return ret;
1494 	}
1495 
1496 	pci_restore_state(pdev);
1497 
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);
1507 	return 0;
1508 }
1509 
1510 #endif
1511 
1512 static DEFINE_PCI_DEVICE_TABLE(sta2x11_vip_pci_tbl) = {
1513 	{PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_VIP)},
1514 	{0,}
1515 };
1516 
1517 static struct pci_driver sta2x11_vip_driver = {
1518 	.name = DRV_NAME,
1519 	.probe = sta2x11_vip_init_one,
1520 	.remove = sta2x11_vip_remove_one,
1521 	.id_table = sta2x11_vip_pci_tbl,
1522 #ifdef CONFIG_PM
1523 	.suspend = sta2x11_vip_suspend,
1524 	.resume = sta2x11_vip_resume,
1525 #endif
1526 };
1527 
1528 static int __init sta2x11_vip_init_module(void)
1529 {
1530 	return pci_register_driver(&sta2x11_vip_driver);
1531 }
1532 
1533 static void __exit sta2x11_vip_exit_module(void)
1534 {
1535 	pci_unregister_driver(&sta2x11_vip_driver);
1536 }
1537 
1538 #ifdef MODULE
1539 module_init(sta2x11_vip_init_module);
1540 module_exit(sta2x11_vip_exit_module);
1541 #else
1542 late_initcall_sync(sta2x11_vip_init_module);
1543 #endif
1544 
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);
1551