xref: /OK3568_Linux_fs/kernel/drivers/media/pci/cx23885/cx23885-vbi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  Driver for the Conexant CX23885 PCIe bridge
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *  Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include "cx23885.h"
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/moduleparam.h>
13*4882a593Smuzhiyun #include <linux/init.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun static unsigned int vbibufs = 4;
16*4882a593Smuzhiyun module_param(vbibufs, int, 0644);
17*4882a593Smuzhiyun MODULE_PARM_DESC(vbibufs, "number of vbi buffers, range 2-32");
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun static unsigned int vbi_debug;
20*4882a593Smuzhiyun module_param(vbi_debug, int, 0644);
21*4882a593Smuzhiyun MODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]");
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #define dprintk(level, fmt, arg...)\
24*4882a593Smuzhiyun 	do { if (vbi_debug >= level)\
25*4882a593Smuzhiyun 		printk(KERN_DEBUG pr_fmt("%s: vbi:" fmt), \
26*4882a593Smuzhiyun 			__func__, ##arg); \
27*4882a593Smuzhiyun 	} while (0)
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /* ------------------------------------------------------------------ */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #define VBI_LINE_LENGTH 1440
32*4882a593Smuzhiyun #define VBI_NTSC_LINE_COUNT 12
33*4882a593Smuzhiyun #define VBI_PAL_LINE_COUNT 18
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 
cx23885_vbi_fmt(struct file * file,void * priv,struct v4l2_format * f)36*4882a593Smuzhiyun int cx23885_vbi_fmt(struct file *file, void *priv,
37*4882a593Smuzhiyun 	struct v4l2_format *f)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun 	struct cx23885_dev *dev = video_drvdata(file);
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	f->fmt.vbi.sampling_rate = 27000000;
42*4882a593Smuzhiyun 	f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
43*4882a593Smuzhiyun 	f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
44*4882a593Smuzhiyun 	f->fmt.vbi.offset = 0;
45*4882a593Smuzhiyun 	f->fmt.vbi.flags = 0;
46*4882a593Smuzhiyun 	if (dev->tvnorm & V4L2_STD_525_60) {
47*4882a593Smuzhiyun 		/* ntsc */
48*4882a593Smuzhiyun 		f->fmt.vbi.start[0] = V4L2_VBI_ITU_525_F1_START + 9;
49*4882a593Smuzhiyun 		f->fmt.vbi.start[1] = V4L2_VBI_ITU_525_F2_START + 9;
50*4882a593Smuzhiyun 		f->fmt.vbi.count[0] = VBI_NTSC_LINE_COUNT;
51*4882a593Smuzhiyun 		f->fmt.vbi.count[1] = VBI_NTSC_LINE_COUNT;
52*4882a593Smuzhiyun 	} else if (dev->tvnorm & V4L2_STD_625_50) {
53*4882a593Smuzhiyun 		/* pal */
54*4882a593Smuzhiyun 		f->fmt.vbi.start[0] = V4L2_VBI_ITU_625_F1_START + 5;
55*4882a593Smuzhiyun 		f->fmt.vbi.start[1] = V4L2_VBI_ITU_625_F2_START + 5;
56*4882a593Smuzhiyun 		f->fmt.vbi.count[0] = VBI_PAL_LINE_COUNT;
57*4882a593Smuzhiyun 		f->fmt.vbi.count[1] = VBI_PAL_LINE_COUNT;
58*4882a593Smuzhiyun 	}
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	return 0;
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun /* We're given the Video Interrupt status register.
64*4882a593Smuzhiyun  * The cx23885_video_irq() func has already validated
65*4882a593Smuzhiyun  * the potential error bits, we just need to
66*4882a593Smuzhiyun  * deal with vbi payload and return indication if
67*4882a593Smuzhiyun  * we actually processed any payload.
68*4882a593Smuzhiyun  */
cx23885_vbi_irq(struct cx23885_dev * dev,u32 status)69*4882a593Smuzhiyun int cx23885_vbi_irq(struct cx23885_dev *dev, u32 status)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	u32 count;
72*4882a593Smuzhiyun 	int handled = 0;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	if (status & VID_BC_MSK_VBI_RISCI1) {
75*4882a593Smuzhiyun 		dprintk(1, "%s() VID_BC_MSK_VBI_RISCI1\n", __func__);
76*4882a593Smuzhiyun 		spin_lock(&dev->slock);
77*4882a593Smuzhiyun 		count = cx_read(VBI_A_GPCNT);
78*4882a593Smuzhiyun 		cx23885_video_wakeup(dev, &dev->vbiq, count);
79*4882a593Smuzhiyun 		spin_unlock(&dev->slock);
80*4882a593Smuzhiyun 		handled++;
81*4882a593Smuzhiyun 	}
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	return handled;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
cx23885_start_vbi_dma(struct cx23885_dev * dev,struct cx23885_dmaqueue * q,struct cx23885_buffer * buf)86*4882a593Smuzhiyun static int cx23885_start_vbi_dma(struct cx23885_dev    *dev,
87*4882a593Smuzhiyun 			 struct cx23885_dmaqueue *q,
88*4882a593Smuzhiyun 			 struct cx23885_buffer   *buf)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	dprintk(1, "%s()\n", __func__);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	/* setup fifo + format */
93*4882a593Smuzhiyun 	cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH02],
94*4882a593Smuzhiyun 				VBI_LINE_LENGTH, buf->risc.dma);
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	/* reset counter */
97*4882a593Smuzhiyun 	cx_write(VID_A_VBI_CTRL, 3);
98*4882a593Smuzhiyun 	cx_write(VBI_A_GPCNT_CTL, 3);
99*4882a593Smuzhiyun 	q->count = 0;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	/* enable irq */
102*4882a593Smuzhiyun 	cx23885_irq_add_enable(dev, 0x01);
103*4882a593Smuzhiyun 	cx_set(VID_A_INT_MSK, 0x000022);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	/* start dma */
106*4882a593Smuzhiyun 	cx_set(DEV_CNTRL2, (1<<5));
107*4882a593Smuzhiyun 	cx_set(VID_A_DMA_CTL, 0x22); /* FIFO and RISC enable */
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	return 0;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun /* ------------------------------------------------------------------ */
113*4882a593Smuzhiyun 
queue_setup(struct vb2_queue * q,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])114*4882a593Smuzhiyun static int queue_setup(struct vb2_queue *q,
115*4882a593Smuzhiyun 			   unsigned int *num_buffers, unsigned int *num_planes,
116*4882a593Smuzhiyun 			   unsigned int sizes[], struct device *alloc_devs[])
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	struct cx23885_dev *dev = q->drv_priv;
119*4882a593Smuzhiyun 	unsigned lines = VBI_PAL_LINE_COUNT;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	if (dev->tvnorm & V4L2_STD_525_60)
122*4882a593Smuzhiyun 		lines = VBI_NTSC_LINE_COUNT;
123*4882a593Smuzhiyun 	*num_planes = 1;
124*4882a593Smuzhiyun 	sizes[0] = lines * VBI_LINE_LENGTH * 2;
125*4882a593Smuzhiyun 	return 0;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun 
buffer_prepare(struct vb2_buffer * vb)128*4882a593Smuzhiyun static int buffer_prepare(struct vb2_buffer *vb)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
131*4882a593Smuzhiyun 	struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
132*4882a593Smuzhiyun 	struct cx23885_buffer *buf = container_of(vbuf,
133*4882a593Smuzhiyun 		struct cx23885_buffer, vb);
134*4882a593Smuzhiyun 	struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
135*4882a593Smuzhiyun 	unsigned lines = VBI_PAL_LINE_COUNT;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	if (dev->tvnorm & V4L2_STD_525_60)
138*4882a593Smuzhiyun 		lines = VBI_NTSC_LINE_COUNT;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	if (vb2_plane_size(vb, 0) < lines * VBI_LINE_LENGTH * 2)
141*4882a593Smuzhiyun 		return -EINVAL;
142*4882a593Smuzhiyun 	vb2_set_plane_payload(vb, 0, lines * VBI_LINE_LENGTH * 2);
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	cx23885_risc_vbibuffer(dev->pci, &buf->risc,
145*4882a593Smuzhiyun 			 sgt->sgl,
146*4882a593Smuzhiyun 			 0, VBI_LINE_LENGTH * lines,
147*4882a593Smuzhiyun 			 VBI_LINE_LENGTH, 0,
148*4882a593Smuzhiyun 			 lines);
149*4882a593Smuzhiyun 	return 0;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun 
buffer_finish(struct vb2_buffer * vb)152*4882a593Smuzhiyun static void buffer_finish(struct vb2_buffer *vb)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
155*4882a593Smuzhiyun 	struct cx23885_buffer *buf = container_of(vbuf,
156*4882a593Smuzhiyun 		struct cx23885_buffer, vb);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	cx23885_free_buffer(vb->vb2_queue->drv_priv, buf);
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun /*
162*4882a593Smuzhiyun  * The risc program for each buffer works as follows: it starts with a simple
163*4882a593Smuzhiyun  * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the
164*4882a593Smuzhiyun  * buffer follows and at the end we have a JUMP back to the start + 12 (skipping
165*4882a593Smuzhiyun  * the initial JUMP).
166*4882a593Smuzhiyun  *
167*4882a593Smuzhiyun  * This is the risc program of the first buffer to be queued if the active list
168*4882a593Smuzhiyun  * is empty and it just keeps DMAing this buffer without generating any
169*4882a593Smuzhiyun  * interrupts.
170*4882a593Smuzhiyun  *
171*4882a593Smuzhiyun  * If a new buffer is added then the initial JUMP in the code for that buffer
172*4882a593Smuzhiyun  * will generate an interrupt which signals that the previous buffer has been
173*4882a593Smuzhiyun  * DMAed successfully and that it can be returned to userspace.
174*4882a593Smuzhiyun  *
175*4882a593Smuzhiyun  * It also sets the final jump of the previous buffer to the start of the new
176*4882a593Smuzhiyun  * buffer, thus chaining the new buffer into the DMA chain. This is a single
177*4882a593Smuzhiyun  * atomic u32 write, so there is no race condition.
178*4882a593Smuzhiyun  *
179*4882a593Smuzhiyun  * The end-result of all this that you only get an interrupt when a buffer
180*4882a593Smuzhiyun  * is ready, so the control flow is very easy.
181*4882a593Smuzhiyun  */
buffer_queue(struct vb2_buffer * vb)182*4882a593Smuzhiyun static void buffer_queue(struct vb2_buffer *vb)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
185*4882a593Smuzhiyun 	struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
186*4882a593Smuzhiyun 	struct cx23885_buffer *buf = container_of(vbuf,
187*4882a593Smuzhiyun 			struct cx23885_buffer, vb);
188*4882a593Smuzhiyun 	struct cx23885_buffer *prev;
189*4882a593Smuzhiyun 	struct cx23885_dmaqueue *q = &dev->vbiq;
190*4882a593Smuzhiyun 	unsigned long flags;
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
193*4882a593Smuzhiyun 	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
194*4882a593Smuzhiyun 	buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
195*4882a593Smuzhiyun 	buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	if (list_empty(&q->active)) {
198*4882a593Smuzhiyun 		spin_lock_irqsave(&dev->slock, flags);
199*4882a593Smuzhiyun 		list_add_tail(&buf->queue, &q->active);
200*4882a593Smuzhiyun 		spin_unlock_irqrestore(&dev->slock, flags);
201*4882a593Smuzhiyun 		dprintk(2, "[%p/%d] vbi_queue - first active\n",
202*4882a593Smuzhiyun 			buf, buf->vb.vb2_buf.index);
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	} else {
205*4882a593Smuzhiyun 		buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
206*4882a593Smuzhiyun 		prev = list_entry(q->active.prev, struct cx23885_buffer,
207*4882a593Smuzhiyun 			queue);
208*4882a593Smuzhiyun 		spin_lock_irqsave(&dev->slock, flags);
209*4882a593Smuzhiyun 		list_add_tail(&buf->queue, &q->active);
210*4882a593Smuzhiyun 		spin_unlock_irqrestore(&dev->slock, flags);
211*4882a593Smuzhiyun 		prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
212*4882a593Smuzhiyun 		dprintk(2, "[%p/%d] buffer_queue - append to active\n",
213*4882a593Smuzhiyun 			buf, buf->vb.vb2_buf.index);
214*4882a593Smuzhiyun 	}
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
cx23885_start_streaming(struct vb2_queue * q,unsigned int count)217*4882a593Smuzhiyun static int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun 	struct cx23885_dev *dev = q->drv_priv;
220*4882a593Smuzhiyun 	struct cx23885_dmaqueue *dmaq = &dev->vbiq;
221*4882a593Smuzhiyun 	struct cx23885_buffer *buf = list_entry(dmaq->active.next,
222*4882a593Smuzhiyun 			struct cx23885_buffer, queue);
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	cx23885_start_vbi_dma(dev, dmaq, buf);
225*4882a593Smuzhiyun 	return 0;
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun 
cx23885_stop_streaming(struct vb2_queue * q)228*4882a593Smuzhiyun static void cx23885_stop_streaming(struct vb2_queue *q)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun 	struct cx23885_dev *dev = q->drv_priv;
231*4882a593Smuzhiyun 	struct cx23885_dmaqueue *dmaq = &dev->vbiq;
232*4882a593Smuzhiyun 	unsigned long flags;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	cx_clear(VID_A_DMA_CTL, 0x22); /* FIFO and RISC enable */
235*4882a593Smuzhiyun 	spin_lock_irqsave(&dev->slock, flags);
236*4882a593Smuzhiyun 	while (!list_empty(&dmaq->active)) {
237*4882a593Smuzhiyun 		struct cx23885_buffer *buf = list_entry(dmaq->active.next,
238*4882a593Smuzhiyun 			struct cx23885_buffer, queue);
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 		list_del(&buf->queue);
241*4882a593Smuzhiyun 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
242*4882a593Smuzhiyun 	}
243*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dev->slock, flags);
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun const struct vb2_ops cx23885_vbi_qops = {
248*4882a593Smuzhiyun 	.queue_setup    = queue_setup,
249*4882a593Smuzhiyun 	.buf_prepare  = buffer_prepare,
250*4882a593Smuzhiyun 	.buf_finish = buffer_finish,
251*4882a593Smuzhiyun 	.buf_queue    = buffer_queue,
252*4882a593Smuzhiyun 	.wait_prepare = vb2_ops_wait_prepare,
253*4882a593Smuzhiyun 	.wait_finish = vb2_ops_wait_finish,
254*4882a593Smuzhiyun 	.start_streaming = cx23885_start_streaming,
255*4882a593Smuzhiyun 	.stop_streaming = cx23885_stop_streaming,
256*4882a593Smuzhiyun };
257