xref: /linux/drivers/media/pci/sta2x11/sta2x11_vip.c (revision cbdb1f163af2bb90d01be1f0263df1d8d5c9d9d3)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This is the driver for the STA2x11 Video Input Port.
4  *
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>
10  */
11 
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>
20 #include <linux/io.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>
31 
32 #include "sta2x11_vip.h"
33 
34 #define DRV_VERSION "1.3"
35 
36 #ifndef PCI_DEVICE_ID_STMICRO_VIP
37 #define PCI_DEVICE_ID_STMICRO_VIP 0xCC0D
38 #endif
39 
40 #define MAX_FRAMES 4
41 
42 /*Register offsets*/
43 #define DVP_CTL		0x00
44 #define DVP_TFO		0x04
45 #define DVP_TFS		0x08
46 #define DVP_BFO		0x0C
47 #define DVP_BFS		0x10
48 #define DVP_VTP		0x14
49 #define DVP_VBP		0x18
50 #define DVP_VMP		0x1C
51 #define DVP_ITM		0x98
52 #define DVP_ITS		0x9C
53 #define DVP_STA		0xA0
54 #define DVP_HLFLN	0xA8
55 #define DVP_RGB		0xC0
56 #define DVP_PKZ		0xF0
57 
58 /*Register fields*/
59 #define DVP_CTL_ENA	0x00000001
60 #define DVP_CTL_RST	0x80000000
61 #define DVP_CTL_DIS	(~0x00040001)
62 
63 #define DVP_IT_VSB	0x00000008
64 #define DVP_IT_VST	0x00000010
65 #define DVP_IT_FIFO	0x00000020
66 
67 #define DVP_HLFLN_SD	0x00000001
68 
69 #define SAVE_COUNT 8
70 #define AUX_COUNT 3
71 #define IRQ_COUNT 1
72 
73 
74 struct vip_buffer {
75 	struct vb2_v4l2_buffer vb;
76 	struct list_head	list;
77 	dma_addr_t		dma;
78 };
79 static inline struct vip_buffer *to_vip_buffer(struct vb2_v4l2_buffer *vb2)
80 {
81 	return container_of(vb2, struct vip_buffer, vb);
82 }
83 
84 /**
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
88  * @pdev: PCI 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
109  *
110  * All non-local data is accessed via this structure.
111  */
112 struct sta2x11_vip {
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;
120 
121 
122 	struct v4l2_pix_format format;
123 	v4l2_std_id std;
124 	unsigned int input;
125 	int disabled;
126 	spinlock_t slock;
127 
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;
134 
135 	/* Interrupt counters */
136 	int tcount, bcount;
137 	int overflow;
138 
139 	void __iomem *iomem;	/* I/O Memory */
140 	struct vip_config *config;
141 };
142 
143 static const unsigned int registers_to_save[AUX_COUNT] = {
144 	DVP_HLFLN, DVP_RGB, DVP_PKZ
145 };
146 
147 static struct v4l2_pix_format formats_50[] = {
148 	{			/*PAL interlaced */
149 	 .width = 720,
150 	 .height = 576,
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},
156 	{			/*PAL top */
157 	 .width = 720,
158 	 .height = 288,
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},
164 	{			/*PAL bottom */
165 	 .width = 720,
166 	 .height = 288,
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},
172 
173 };
174 
175 static struct v4l2_pix_format formats_60[] = {
176 	{			/*NTSC interlaced */
177 	 .width = 720,
178 	 .height = 480,
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},
184 	{			/*NTSC top */
185 	 .width = 720,
186 	 .height = 240,
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},
192 	{			/*NTSC bottom */
193 	 .width = 720,
194 	 .height = 240,
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},
200 };
201 
202 /* Write VIP register */
203 static inline void reg_write(struct sta2x11_vip *vip, unsigned int reg, u32 val)
204 {
205 	iowrite32((val), (vip->iomem)+(reg));
206 }
207 /* Read VIP register */
208 static inline u32 reg_read(struct sta2x11_vip *vip, unsigned int reg)
209 {
210 	return  ioread32((vip->iomem)+(reg));
211 }
212 /* Start DMA acquisition */
213 static void start_dma(struct sta2x11_vip *vip, struct vip_buffer *vip_buf)
214 {
215 	unsigned long offset = 0;
216 
217 	if (vip->format.field == V4L2_FIELD_INTERLACED)
218 		offset = vip->format.width * 2;
219 
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);
227 }
228 
229 /* Fetch the next buffer to activate */
230 static void vip_active_buf_next(struct sta2x11_vip *vip)
231 {
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);
236 		return;
237 	}
238 	vip->active = list_first_entry(&vip->buffer_list,
239 				       struct vip_buffer,
240 				       list);
241 	/* Reset Top and Bottom counter */
242 	vip->tcount = 0;
243 	vip->bcount = 0;
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 */
247 	}
248 }
249 
250 
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[])
255 {
256 	struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
257 
258 	if (!(*nbuffers) || *nbuffers < MAX_FRAMES)
259 		*nbuffers = MAX_FRAMES;
260 
261 	*nplanes = 1;
262 	sizes[0] = vip->format.sizeimage;
263 
264 	vip->sequence = 0;
265 	vip->active = NULL;
266 	vip->tcount = 0;
267 	vip->bcount = 0;
268 
269 	return 0;
270 };
271 static int buffer_init(struct vb2_buffer *vb)
272 {
273 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
274 	struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
275 
276 	vip_buf->dma = vb2_dma_contig_plane_dma_addr(vb, 0);
277 	INIT_LIST_HEAD(&vip_buf->list);
278 	return 0;
279 }
280 
281 static int buffer_prepare(struct vb2_buffer *vb)
282 {
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);
286 	unsigned long size;
287 
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);
292 		return -EINVAL;
293 	}
294 
295 	vb2_set_plane_payload(&vip_buf->vb.vb2_buf, 0, size);
296 
297 	return 0;
298 }
299 static void buffer_queue(struct vb2_buffer *vb)
300 {
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);
304 
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,
309 					       struct vip_buffer,
310 					       list);
311 		if (vb2_is_streaming(&vip->vb_vidq))	/* streaming is on */
312 			start_dma(vip, vip_buf);	/* start dma capture */
313 	}
314 	spin_unlock(&vip->lock);
315 }
316 static void buffer_finish(struct vb2_buffer *vb)
317 {
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);
321 
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);
326 
327 	if (vb2_is_streaming(vb->vb2_queue))
328 		vip_active_buf_next(vip);
329 }
330 
331 static int start_streaming(struct vb2_queue *vq, unsigned int count)
332 {
333 	struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
334 
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);
339 
340 	if (count)
341 		start_dma(vip, vip->active);
342 
343 	return 0;
344 }
345 
346 /* abort streaming and wait for last buffer */
347 static void stop_streaming(struct vb2_queue *vq)
348 {
349 	struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
350 	struct vip_buffer *vip_buf, *node;
351 
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);
356 
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);
362 	}
363 	spin_unlock(&vip->lock);
364 }
365 
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,
376 };
377 
378 
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,
387 	.poll = vb2_fop_poll
388 };
389 
390 
391 /**
392  * vidioc_querycap - return capabilities of device
393  * @file: descriptor of device
394  * @cap: contains return values
395  * @priv: unused
396  *
397  * the capabilities of the device are returned
398  *
399  * return value: 0, no error.
400  */
401 static int vidioc_querycap(struct file *file, void *priv,
402 			   struct v4l2_capability *cap)
403 {
404 	strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
405 	strscpy(cap->card, KBUILD_MODNAME, sizeof(cap->card));
406 	return 0;
407 }
408 
409 /**
410  * vidioc_s_std - set video standard
411  * @file: descriptor of device
412  * @std: contains standard to be set
413  * @priv: unused
414  *
415  * the video standard is set
416  *
417  * return value: 0, no error.
418  *
419  * -EIO, no input signal detected
420  *
421  * other, returned from video DAC.
422  */
423 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id std)
424 {
425 	struct sta2x11_vip *vip = video_drvdata(file);
426 
427 	/*
428 	 * This is here for backwards compatibility only.
429 	 * The use of V4L2_STD_ALL to trigger a querystd is non-standard.
430 	 */
431 	if (std == V4L2_STD_ALL) {
432 		v4l2_subdev_call(vip->decoder, video, querystd, &std);
433 		if (std == V4L2_STD_UNKNOWN)
434 			return -EIO;
435 	}
436 
437 	if (vip->std != std) {
438 		vip->std = std;
439 		if (V4L2_STD_525_60 & std)
440 			vip->format = formats_60[0];
441 		else
442 			vip->format = formats_50[0];
443 	}
444 
445 	return v4l2_subdev_call(vip->decoder, video, s_std, std);
446 }
447 
448 /**
449  * vidioc_g_std - get video standard
450  * @file: descriptor of device
451  * @priv: unused
452  * @std: contains return values
453  *
454  * the current video standard is returned
455  *
456  * return value: 0, no error.
457  */
458 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
459 {
460 	struct sta2x11_vip *vip = video_drvdata(file);
461 
462 	*std = vip->std;
463 	return 0;
464 }
465 
466 /**
467  * vidioc_querystd - get possible video standards
468  * @file: descriptor of device
469  * @priv: unused
470  * @std: contains return values
471  *
472  * all possible video standards are returned
473  *
474  * return value: delivered by video DAC routine.
475  */
476 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
477 {
478 	struct sta2x11_vip *vip = video_drvdata(file);
479 
480 	return v4l2_subdev_call(vip->decoder, video, querystd, std);
481 }
482 
483 static int vidioc_enum_input(struct file *file, void *priv,
484 			     struct v4l2_input *inp)
485 {
486 	if (inp->index > 1)
487 		return -EINVAL;
488 
489 	inp->type = V4L2_INPUT_TYPE_CAMERA;
490 	inp->std = V4L2_STD_ALL;
491 	sprintf(inp->name, "Camera %u", inp->index);
492 
493 	return 0;
494 }
495 
496 /**
497  * vidioc_s_input - set input line
498  * @file: descriptor of device
499  * @priv: unused
500  * @i: new input line number
501  *
502  * the current active input line is set
503  *
504  * return value: 0, no error.
505  *
506  * -EINVAL, line number out of range
507  */
508 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
509 {
510 	struct sta2x11_vip *vip = video_drvdata(file);
511 	int ret;
512 
513 	if (i > 1)
514 		return -EINVAL;
515 	ret = v4l2_subdev_call(vip->decoder, video, s_routing, i, 0, 0);
516 
517 	if (!ret)
518 		vip->input = i;
519 
520 	return 0;
521 }
522 
523 /**
524  * vidioc_g_input - return input line
525  * @file: descriptor of device
526  * @priv: unused
527  * @i: returned input line number
528  *
529  * the current active input line is returned
530  *
531  * return value: always 0.
532  */
533 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
534 {
535 	struct sta2x11_vip *vip = video_drvdata(file);
536 
537 	*i = vip->input;
538 	return 0;
539 }
540 
541 /**
542  * vidioc_enum_fmt_vid_cap - return video capture format
543  * @file: descriptor of device
544  * @priv: unused
545  * @f: returned format information
546  *
547  * returns name and format of video capture
548  * Only UYVY is supported by hardware.
549  *
550  * return value: always 0.
551  */
552 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
553 				   struct v4l2_fmtdesc *f)
554 {
555 
556 	if (f->index != 0)
557 		return -EINVAL;
558 
559 	f->pixelformat = V4L2_PIX_FMT_UYVY;
560 	return 0;
561 }
562 
563 /**
564  * vidioc_try_fmt_vid_cap - set video capture format
565  * @file: descriptor of device
566  * @priv: unused
567  * @f: new format
568  *
569  * new video format is set which includes width and
570  * field type. width is fixed to 720, no scaling.
571  * Only UYVY is supported by this hardware.
572  * the minimum height is 200, the maximum is 576 (PAL)
573  *
574  * return value: 0, no error
575  *
576  * -EINVAL, pixel or field format not supported
577  *
578  */
579 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
580 				  struct v4l2_format *f)
581 {
582 	struct sta2x11_vip *vip = video_drvdata(file);
583 	int interlace_lim;
584 
585 	if (V4L2_PIX_FMT_UYVY != f->fmt.pix.pixelformat) {
586 		v4l2_warn(&vip->v4l2_dev, "Invalid format, only UYVY supported\n");
587 		return -EINVAL;
588 	}
589 
590 	if (V4L2_STD_525_60 & vip->std)
591 		interlace_lim = 240;
592 	else
593 		interlace_lim = 288;
594 
595 	switch (f->fmt.pix.field) {
596 	default:
597 	case V4L2_FIELD_ANY:
598 		if (interlace_lim < f->fmt.pix.height)
599 			f->fmt.pix.field = V4L2_FIELD_INTERLACED;
600 		else
601 			f->fmt.pix.field = V4L2_FIELD_BOTTOM;
602 		break;
603 	case V4L2_FIELD_TOP:
604 	case V4L2_FIELD_BOTTOM:
605 		if (interlace_lim < f->fmt.pix.height)
606 			f->fmt.pix.height = interlace_lim;
607 		break;
608 	case V4L2_FIELD_INTERLACED:
609 		break;
610 	}
611 
612 	/* It is the only supported format */
613 	f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
614 	f->fmt.pix.height &= ~1;
615 	if (2 * interlace_lim < f->fmt.pix.height)
616 		f->fmt.pix.height = 2 * interlace_lim;
617 	if (200 > f->fmt.pix.height)
618 		f->fmt.pix.height = 200;
619 	f->fmt.pix.width = 720;
620 	f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
621 	f->fmt.pix.sizeimage = f->fmt.pix.width * 2 * f->fmt.pix.height;
622 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
623 	return 0;
624 }
625 
626 /**
627  * vidioc_s_fmt_vid_cap - set current video format parameters
628  * @file: descriptor of device
629  * @priv: unused
630  * @f: returned format information
631  *
632  * set new capture format
633  * return value: 0, no error
634  *
635  * other, delivered by video DAC routine.
636  */
637 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
638 				struct v4l2_format *f)
639 {
640 	struct sta2x11_vip *vip = video_drvdata(file);
641 	unsigned int t_stop, b_stop, pitch;
642 	int ret;
643 
644 	ret = vidioc_try_fmt_vid_cap(file, priv, f);
645 	if (ret)
646 		return ret;
647 
648 	if (vb2_is_busy(&vip->vb_vidq)) {
649 		/* Can't change format during acquisition */
650 		v4l2_err(&vip->v4l2_dev, "device busy\n");
651 		return -EBUSY;
652 	}
653 	vip->format = f->fmt.pix;
654 	switch (vip->format.field) {
655 	case V4L2_FIELD_INTERLACED:
656 		t_stop = ((vip->format.height / 2 - 1) << 16) |
657 			 (2 * vip->format.width - 1);
658 		b_stop = t_stop;
659 		pitch = 4 * vip->format.width;
660 		break;
661 	case V4L2_FIELD_TOP:
662 		t_stop = ((vip->format.height - 1) << 16) |
663 			 (2 * vip->format.width - 1);
664 		b_stop = (0 << 16) | (2 * vip->format.width - 1);
665 		pitch = 2 * vip->format.width;
666 		break;
667 	case V4L2_FIELD_BOTTOM:
668 		t_stop = (0 << 16) | (2 * vip->format.width - 1);
669 		b_stop = (vip->format.height << 16) |
670 			 (2 * vip->format.width - 1);
671 		pitch = 2 * vip->format.width;
672 		break;
673 	default:
674 		v4l2_err(&vip->v4l2_dev, "unknown field format\n");
675 		return -EINVAL;
676 	}
677 
678 	spin_lock_irq(&vip->slock);
679 	/* Y-X Top Field Offset */
680 	reg_write(vip, DVP_TFO, 0);
681 	/* Y-X Bottom Field Offset */
682 	reg_write(vip, DVP_BFO, 0);
683 	/* Y-X Top Field Stop*/
684 	reg_write(vip, DVP_TFS, t_stop);
685 	/* Y-X Bottom Field Stop */
686 	reg_write(vip, DVP_BFS, b_stop);
687 	/* Video Memory Pitch */
688 	reg_write(vip, DVP_VMP, pitch);
689 	spin_unlock_irq(&vip->slock);
690 
691 	return 0;
692 }
693 
694 /**
695  * vidioc_g_fmt_vid_cap - get current video format parameters
696  * @file: descriptor of device
697  * @priv: unused
698  * @f: contains format information
699  *
700  * returns current video format parameters
701  *
702  * return value: 0, always successful
703  */
704 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
705 				struct v4l2_format *f)
706 {
707 	struct sta2x11_vip *vip = video_drvdata(file);
708 
709 	f->fmt.pix = vip->format;
710 
711 	return 0;
712 }
713 
714 static const struct v4l2_ioctl_ops vip_ioctl_ops = {
715 	.vidioc_querycap = vidioc_querycap,
716 	/* FMT handling */
717 	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
718 	.vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
719 	.vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
720 	.vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
721 	/* Buffer handlers */
722 	.vidioc_create_bufs = vb2_ioctl_create_bufs,
723 	.vidioc_prepare_buf = vb2_ioctl_prepare_buf,
724 	.vidioc_reqbufs = vb2_ioctl_reqbufs,
725 	.vidioc_querybuf = vb2_ioctl_querybuf,
726 	.vidioc_qbuf = vb2_ioctl_qbuf,
727 	.vidioc_dqbuf = vb2_ioctl_dqbuf,
728 	/* Stream on/off */
729 	.vidioc_streamon = vb2_ioctl_streamon,
730 	.vidioc_streamoff = vb2_ioctl_streamoff,
731 	/* Standard handling */
732 	.vidioc_g_std = vidioc_g_std,
733 	.vidioc_s_std = vidioc_s_std,
734 	.vidioc_querystd = vidioc_querystd,
735 	/* Input handling */
736 	.vidioc_enum_input = vidioc_enum_input,
737 	.vidioc_g_input = vidioc_g_input,
738 	.vidioc_s_input = vidioc_s_input,
739 	/* Log status ioctl */
740 	.vidioc_log_status = v4l2_ctrl_log_status,
741 	/* Event handling */
742 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
743 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
744 };
745 
746 static const struct video_device video_dev_template = {
747 	.name = KBUILD_MODNAME,
748 	.release = video_device_release_empty,
749 	.fops = &vip_fops,
750 	.ioctl_ops = &vip_ioctl_ops,
751 	.tvnorms = V4L2_STD_ALL,
752 	.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
753 		       V4L2_CAP_STREAMING,
754 };
755 
756 /**
757  * vip_irq - interrupt routine
758  * @irq: Number of interrupt ( not used, correct number is assumed )
759  * @vip: local data structure containing all information
760  *
761  * check for both frame interrupts set ( top and bottom ).
762  * check FIFO overflow, but limit number of log messages after open.
763  * signal a complete buffer if done
764  *
765  * return value: IRQ_NONE, interrupt was not generated by VIP
766  *
767  * IRQ_HANDLED, interrupt done.
768  */
769 static irqreturn_t vip_irq(int irq, struct sta2x11_vip *vip)
770 {
771 	unsigned int status;
772 
773 	status = reg_read(vip, DVP_ITS);
774 
775 	if (!status)		/* No interrupt to handle */
776 		return IRQ_NONE;
777 
778 	if (status & DVP_IT_FIFO)
779 		if (vip->overflow++ > 5)
780 			pr_info("VIP: fifo overflow\n");
781 
782 	if ((status & DVP_IT_VST) && (status & DVP_IT_VSB)) {
783 		/* this is bad, we are too slow, hope the condition is gone
784 		 * on the next frame */
785 		return IRQ_HANDLED;
786 	}
787 
788 	if (status & DVP_IT_VST)
789 		if ((++vip->tcount) < 2)
790 			return IRQ_HANDLED;
791 	if (status & DVP_IT_VSB) {
792 		vip->bcount++;
793 		return IRQ_HANDLED;
794 	}
795 
796 	if (vip->active) { /* Acquisition is over on this buffer */
797 		/* Disable acquisition */
798 		reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
799 		/* Remove the active buffer from the list */
800 		vip->active->vb.vb2_buf.timestamp = ktime_get_ns();
801 		vip->active->vb.sequence = vip->sequence++;
802 		vb2_buffer_done(&vip->active->vb.vb2_buf, VB2_BUF_STATE_DONE);
803 	}
804 
805 	return IRQ_HANDLED;
806 }
807 
808 static void sta2x11_vip_init_register(struct sta2x11_vip *vip)
809 {
810 	/* Register initialization */
811 	spin_lock_irq(&vip->slock);
812 	/* Clean interrupt */
813 	reg_read(vip, DVP_ITS);
814 	/* Enable Half Line per vertical */
815 	reg_write(vip, DVP_HLFLN, DVP_HLFLN_SD);
816 	/* Reset VIP control */
817 	reg_write(vip, DVP_CTL, DVP_CTL_RST);
818 	/* Clear VIP control */
819 	reg_write(vip, DVP_CTL, 0);
820 	spin_unlock_irq(&vip->slock);
821 }
822 static void sta2x11_vip_clear_register(struct sta2x11_vip *vip)
823 {
824 	spin_lock_irq(&vip->slock);
825 	/* Disable interrupt */
826 	reg_write(vip, DVP_ITM, 0);
827 	/* Reset VIP Control */
828 	reg_write(vip, DVP_CTL, DVP_CTL_RST);
829 	/* Clear VIP Control */
830 	reg_write(vip, DVP_CTL, 0);
831 	/* Clean VIP Interrupt */
832 	reg_read(vip, DVP_ITS);
833 	spin_unlock_irq(&vip->slock);
834 }
835 static int sta2x11_vip_init_buffer(struct sta2x11_vip *vip)
836 {
837 	int err;
838 
839 	err = dma_set_coherent_mask(&vip->pdev->dev, DMA_BIT_MASK(29));
840 	if (err) {
841 		v4l2_err(&vip->v4l2_dev, "Cannot configure coherent mask");
842 		return err;
843 	}
844 	memset(&vip->vb_vidq, 0, sizeof(struct vb2_queue));
845 	vip->vb_vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
846 	vip->vb_vidq.io_modes = VB2_MMAP | VB2_READ;
847 	vip->vb_vidq.drv_priv = vip;
848 	vip->vb_vidq.buf_struct_size = sizeof(struct vip_buffer);
849 	vip->vb_vidq.ops = &vip_video_qops;
850 	vip->vb_vidq.mem_ops = &vb2_dma_contig_memops;
851 	vip->vb_vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
852 	vip->vb_vidq.dev = &vip->pdev->dev;
853 	vip->vb_vidq.lock = &vip->v4l_lock;
854 	err = vb2_queue_init(&vip->vb_vidq);
855 	if (err)
856 		return err;
857 	INIT_LIST_HEAD(&vip->buffer_list);
858 	spin_lock_init(&vip->lock);
859 	return 0;
860 }
861 
862 static int sta2x11_vip_init_controls(struct sta2x11_vip *vip)
863 {
864 	/*
865 	 * Inititialize an empty control so VIP can inerithing controls
866 	 * from ADV7180
867 	 */
868 	v4l2_ctrl_handler_init(&vip->ctrl_hdl, 0);
869 
870 	vip->v4l2_dev.ctrl_handler = &vip->ctrl_hdl;
871 	if (vip->ctrl_hdl.error) {
872 		int err = vip->ctrl_hdl.error;
873 
874 		v4l2_ctrl_handler_free(&vip->ctrl_hdl);
875 		return err;
876 	}
877 
878 	return 0;
879 }
880 
881 /**
882  * vip_gpio_reserve - reserve gpio pin
883  * @dev: device
884  * @pin: GPIO pin number
885  * @dir: direction, input or output
886  * @name: GPIO pin name
887  *
888  */
889 static int vip_gpio_reserve(struct device *dev, int pin, int dir,
890 			    const char *name)
891 {
892 	int ret = -ENODEV;
893 
894 	if (!gpio_is_valid(pin))
895 		return ret;
896 
897 	ret = gpio_request(pin, name);
898 	if (ret) {
899 		dev_err(dev, "Failed to allocate pin %d (%s)\n", pin, name);
900 		return ret;
901 	}
902 
903 	ret = gpio_direction_output(pin, dir);
904 	if (ret) {
905 		dev_err(dev, "Failed to set direction for pin %d (%s)\n",
906 			pin, name);
907 		gpio_free(pin);
908 		return ret;
909 	}
910 
911 	ret = gpio_export(pin, false);
912 	if (ret) {
913 		dev_err(dev, "Failed to export pin %d (%s)\n", pin, name);
914 		gpio_free(pin);
915 		return ret;
916 	}
917 
918 	return 0;
919 }
920 
921 /**
922  * vip_gpio_release - release gpio pin
923  * @dev: device
924  * @pin: GPIO pin number
925  * @name: GPIO pin name
926  *
927  */
928 static void vip_gpio_release(struct device *dev, int pin, const char *name)
929 {
930 	if (gpio_is_valid(pin)) {
931 		dev_dbg(dev, "releasing pin %d (%s)\n",	pin, name);
932 		gpio_unexport(pin);
933 		gpio_free(pin);
934 	}
935 }
936 
937 /**
938  * sta2x11_vip_init_one - init one instance of video device
939  * @pdev: PCI device
940  * @ent: (not used)
941  *
942  * allocate reset pins for DAC.
943  * Reset video DAC, this is done via reset line.
944  * allocate memory for managing device
945  * request interrupt
946  * map IO region
947  * register device
948  * find and initialize video DAC
949  *
950  * return value: 0, no error
951  *
952  * -ENOMEM, no memory
953  *
954  * -ENODEV, device could not be detected or registered
955  */
956 static int sta2x11_vip_init_one(struct pci_dev *pdev,
957 				const struct pci_device_id *ent)
958 {
959 	int ret;
960 	struct sta2x11_vip *vip;
961 	struct vip_config *config;
962 
963 	/* Check if hardware support 26-bit DMA */
964 	if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(26))) {
965 		dev_err(&pdev->dev, "26-bit DMA addressing not available\n");
966 		return -EINVAL;
967 	}
968 	/* Enable PCI */
969 	ret = pci_enable_device(pdev);
970 	if (ret)
971 		return ret;
972 
973 	/* Get VIP platform data */
974 	config = dev_get_platdata(&pdev->dev);
975 	if (!config) {
976 		dev_info(&pdev->dev, "VIP slot disabled\n");
977 		ret = -EINVAL;
978 		goto disable;
979 	}
980 
981 	/* Power configuration */
982 	ret = vip_gpio_reserve(&pdev->dev, config->pwr_pin, 0,
983 			       config->pwr_name);
984 	if (ret)
985 		goto disable;
986 
987 	ret = vip_gpio_reserve(&pdev->dev, config->reset_pin, 0,
988 			       config->reset_name);
989 	if (ret) {
990 		vip_gpio_release(&pdev->dev, config->pwr_pin,
991 				 config->pwr_name);
992 		goto disable;
993 	}
994 
995 	if (gpio_is_valid(config->pwr_pin)) {
996 		/* Datasheet says 5ms between PWR and RST */
997 		usleep_range(5000, 25000);
998 		gpio_direction_output(config->pwr_pin, 1);
999 	}
1000 
1001 	if (gpio_is_valid(config->reset_pin)) {
1002 		/* Datasheet says 5ms between PWR and RST */
1003 		usleep_range(5000, 25000);
1004 		gpio_direction_output(config->reset_pin, 1);
1005 	}
1006 	usleep_range(5000, 25000);
1007 
1008 	/* Allocate a new VIP instance */
1009 	vip = kzalloc(sizeof(struct sta2x11_vip), GFP_KERNEL);
1010 	if (!vip) {
1011 		ret = -ENOMEM;
1012 		goto release_gpios;
1013 	}
1014 	vip->pdev = pdev;
1015 	vip->std = V4L2_STD_PAL;
1016 	vip->format = formats_50[0];
1017 	vip->config = config;
1018 	mutex_init(&vip->v4l_lock);
1019 
1020 	ret = sta2x11_vip_init_controls(vip);
1021 	if (ret)
1022 		goto free_mem;
1023 	ret = v4l2_device_register(&pdev->dev, &vip->v4l2_dev);
1024 	if (ret)
1025 		goto free_mem;
1026 
1027 	dev_dbg(&pdev->dev, "BAR #0 at 0x%lx 0x%lx irq %d\n",
1028 		(unsigned long)pci_resource_start(pdev, 0),
1029 		(unsigned long)pci_resource_len(pdev, 0), pdev->irq);
1030 
1031 	pci_set_master(pdev);
1032 
1033 	ret = pci_request_regions(pdev, KBUILD_MODNAME);
1034 	if (ret)
1035 		goto unreg;
1036 
1037 	vip->iomem = pci_iomap(pdev, 0, 0x100);
1038 	if (!vip->iomem) {
1039 		ret = -ENOMEM;
1040 		goto release;
1041 	}
1042 
1043 	pci_enable_msi(pdev);
1044 
1045 	/* Initialize buffer */
1046 	ret = sta2x11_vip_init_buffer(vip);
1047 	if (ret)
1048 		goto unmap;
1049 
1050 	spin_lock_init(&vip->slock);
1051 
1052 	ret = request_irq(pdev->irq,
1053 			  (irq_handler_t) vip_irq,
1054 			  IRQF_SHARED, KBUILD_MODNAME, vip);
1055 	if (ret) {
1056 		dev_err(&pdev->dev, "request_irq failed\n");
1057 		ret = -ENODEV;
1058 		goto release_buf;
1059 	}
1060 
1061 	/* Initialize and register video device */
1062 	vip->video_dev = video_dev_template;
1063 	vip->video_dev.v4l2_dev = &vip->v4l2_dev;
1064 	vip->video_dev.queue = &vip->vb_vidq;
1065 	vip->video_dev.lock = &vip->v4l_lock;
1066 	video_set_drvdata(&vip->video_dev, vip);
1067 
1068 	ret = video_register_device(&vip->video_dev, VFL_TYPE_VIDEO, -1);
1069 	if (ret)
1070 		goto vrelease;
1071 
1072 	/* Get ADV7180 subdevice */
1073 	vip->adapter = i2c_get_adapter(vip->config->i2c_id);
1074 	if (!vip->adapter) {
1075 		ret = -ENODEV;
1076 		dev_err(&pdev->dev, "no I2C adapter found\n");
1077 		goto vunreg;
1078 	}
1079 
1080 	vip->decoder = v4l2_i2c_new_subdev(&vip->v4l2_dev, vip->adapter,
1081 					   "adv7180", vip->config->i2c_addr,
1082 					   NULL);
1083 	if (!vip->decoder) {
1084 		ret = -ENODEV;
1085 		dev_err(&pdev->dev, "no decoder found\n");
1086 		goto vunreg;
1087 	}
1088 
1089 	i2c_put_adapter(vip->adapter);
1090 	v4l2_subdev_call(vip->decoder, core, init, 0);
1091 
1092 	sta2x11_vip_init_register(vip);
1093 
1094 	dev_info(&pdev->dev, "STA2X11 Video Input Port (VIP) loaded\n");
1095 	return 0;
1096 
1097 vunreg:
1098 	video_set_drvdata(&vip->video_dev, NULL);
1099 vrelease:
1100 	vb2_video_unregister_device(&vip->video_dev);
1101 	free_irq(pdev->irq, vip);
1102 release_buf:
1103 	pci_disable_msi(pdev);
1104 unmap:
1105 	pci_iounmap(pdev, vip->iomem);
1106 release:
1107 	pci_release_regions(pdev);
1108 unreg:
1109 	v4l2_device_unregister(&vip->v4l2_dev);
1110 free_mem:
1111 	kfree(vip);
1112 release_gpios:
1113 	vip_gpio_release(&pdev->dev, config->reset_pin, config->reset_name);
1114 	vip_gpio_release(&pdev->dev, config->pwr_pin, config->pwr_name);
1115 disable:
1116 	/*
1117 	 * do not call pci_disable_device on sta2x11 because it break all
1118 	 * other Bus masters on this EP
1119 	 */
1120 	return ret;
1121 }
1122 
1123 /**
1124  * sta2x11_vip_remove_one - release device
1125  * @pdev: PCI device
1126  *
1127  * Undo everything done in .._init_one
1128  *
1129  * unregister video device
1130  * free interrupt
1131  * unmap ioadresses
1132  * free memory
1133  * free GPIO pins
1134  */
1135 static void sta2x11_vip_remove_one(struct pci_dev *pdev)
1136 {
1137 	struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1138 	struct sta2x11_vip *vip =
1139 	    container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1140 
1141 	sta2x11_vip_clear_register(vip);
1142 
1143 	video_set_drvdata(&vip->video_dev, NULL);
1144 	vb2_video_unregister_device(&vip->video_dev);
1145 	free_irq(pdev->irq, vip);
1146 	pci_disable_msi(pdev);
1147 	pci_iounmap(pdev, vip->iomem);
1148 	pci_release_regions(pdev);
1149 
1150 	v4l2_device_unregister(&vip->v4l2_dev);
1151 
1152 	vip_gpio_release(&pdev->dev, vip->config->pwr_pin,
1153 			 vip->config->pwr_name);
1154 	vip_gpio_release(&pdev->dev, vip->config->reset_pin,
1155 			 vip->config->reset_name);
1156 
1157 	kfree(vip);
1158 	/*
1159 	 * do not call pci_disable_device on sta2x11 because it break all
1160 	 * other Bus masters on this EP
1161 	 */
1162 }
1163 
1164 /**
1165  * sta2x11_vip_suspend - set device into power save mode
1166  * @dev_d: PCI device
1167  *
1168  * all relevant registers are saved and an attempt to set a new state is made.
1169  *
1170  * return value: 0 always indicate success,
1171  * even if device could not be disabled. (workaround for hardware problem)
1172  */
1173 static int __maybe_unused sta2x11_vip_suspend(struct device *dev_d)
1174 {
1175 	struct v4l2_device *v4l2_dev = dev_get_drvdata(dev_d);
1176 	struct sta2x11_vip *vip =
1177 	    container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1178 	unsigned long flags;
1179 	int i;
1180 
1181 	spin_lock_irqsave(&vip->slock, flags);
1182 	vip->register_save_area[0] = reg_read(vip, DVP_CTL);
1183 	reg_write(vip, DVP_CTL, vip->register_save_area[0] & DVP_CTL_DIS);
1184 	vip->register_save_area[SAVE_COUNT] = reg_read(vip, DVP_ITM);
1185 	reg_write(vip, DVP_ITM, 0);
1186 	for (i = 1; i < SAVE_COUNT; i++)
1187 		vip->register_save_area[i] = reg_read(vip, 4 * i);
1188 	for (i = 0; i < AUX_COUNT; i++)
1189 		vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i] =
1190 		    reg_read(vip, registers_to_save[i]);
1191 	spin_unlock_irqrestore(&vip->slock, flags);
1192 
1193 	vip->disabled = 1;
1194 
1195 	pr_info("VIP: suspend\n");
1196 	return 0;
1197 }
1198 
1199 /**
1200  * sta2x11_vip_resume - resume device operation
1201  * @dev_d : PCI device
1202  *
1203  * return value: 0, no error.
1204  *
1205  * other, could not set device to power on state.
1206  */
1207 static int __maybe_unused sta2x11_vip_resume(struct device *dev_d)
1208 {
1209 	struct v4l2_device *v4l2_dev = dev_get_drvdata(dev_d);
1210 	struct sta2x11_vip *vip =
1211 	    container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1212 	unsigned long flags;
1213 	int i;
1214 
1215 	pr_info("VIP: resume\n");
1216 
1217 	vip->disabled = 0;
1218 
1219 	spin_lock_irqsave(&vip->slock, flags);
1220 	for (i = 1; i < SAVE_COUNT; i++)
1221 		reg_write(vip, 4 * i, vip->register_save_area[i]);
1222 	for (i = 0; i < AUX_COUNT; i++)
1223 		reg_write(vip, registers_to_save[i],
1224 			  vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i]);
1225 	reg_write(vip, DVP_CTL, vip->register_save_area[0]);
1226 	reg_write(vip, DVP_ITM, vip->register_save_area[SAVE_COUNT]);
1227 	spin_unlock_irqrestore(&vip->slock, flags);
1228 	return 0;
1229 }
1230 
1231 static const struct pci_device_id sta2x11_vip_pci_tbl[] = {
1232 	{PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_VIP)},
1233 	{0,}
1234 };
1235 
1236 static SIMPLE_DEV_PM_OPS(sta2x11_vip_pm_ops,
1237 			 sta2x11_vip_suspend,
1238 			 sta2x11_vip_resume);
1239 
1240 static struct pci_driver sta2x11_vip_driver = {
1241 	.name = KBUILD_MODNAME,
1242 	.probe = sta2x11_vip_init_one,
1243 	.remove = sta2x11_vip_remove_one,
1244 	.id_table = sta2x11_vip_pci_tbl,
1245 	.driver.pm = &sta2x11_vip_pm_ops,
1246 };
1247 
1248 static int __init sta2x11_vip_init_module(void)
1249 {
1250 	return pci_register_driver(&sta2x11_vip_driver);
1251 }
1252 
1253 static void __exit sta2x11_vip_exit_module(void)
1254 {
1255 	pci_unregister_driver(&sta2x11_vip_driver);
1256 }
1257 
1258 #ifdef MODULE
1259 module_init(sta2x11_vip_init_module);
1260 module_exit(sta2x11_vip_exit_module);
1261 #else
1262 late_initcall_sync(sta2x11_vip_init_module);
1263 #endif
1264 
1265 MODULE_DESCRIPTION("STA2X11 Video Input Port driver");
1266 MODULE_AUTHOR("Wind River");
1267 MODULE_LICENSE("GPL v2");
1268 MODULE_VERSION(DRV_VERSION);
1269 MODULE_DEVICE_TABLE(pci, sta2x11_vip_pci_tbl);
1270