blob: 369e545cac0418124a7c6e23f1b3756a3337f644 [file] [log] [blame]
Steven Tothe47f30b2008-01-10 04:25:59 -03001/*
2 * Driver for the Conexant CX23885 PCIe bridge
3 *
Steven Toth6d897612008-09-03 17:12:12 -03004 * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
Steven Tothe47f30b2008-01-10 04:25:59 -03005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
Steven Tothe47f30b2008-01-10 04:25:59 -030016 */
17
Mauro Carvalho Chehabe39682b2016-11-13 09:46:11 -020018#include "cx23885.h"
19
Steven Tothe47f30b2008-01-10 04:25:59 -030020#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/moduleparam.h>
23#include <linux/init.h>
Steven Tothe47f30b2008-01-10 04:25:59 -030024
Steven Tothe47f30b2008-01-10 04:25:59 -030025static unsigned int vbibufs = 4;
26module_param(vbibufs, int, 0644);
27MODULE_PARM_DESC(vbibufs, "number of vbi buffers, range 2-32");
28
Steven Toth4513fc692008-01-12 11:36:36 -030029static unsigned int vbi_debug;
Steven Tothe47f30b2008-01-10 04:25:59 -030030module_param(vbi_debug, int, 0644);
31MODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]");
32
Steven Toth4513fc692008-01-12 11:36:36 -030033#define dprintk(level, fmt, arg...)\
34 do { if (vbi_debug >= level)\
Mauro Carvalho Chehabe39682b2016-11-13 09:46:11 -020035 printk(KERN_DEBUG pr_fmt("%s: vbi:" fmt), \
36 __func__, ##arg); \
Steven Toth4513fc692008-01-12 11:36:36 -030037 } while (0)
Steven Tothe47f30b2008-01-10 04:25:59 -030038
39/* ------------------------------------------------------------------ */
40
Steven Toth4f9c4142011-10-10 11:09:54 -030041#define VBI_LINE_LENGTH 1440
Hans Verkuil453afdd2014-08-14 06:43:01 -030042#define VBI_NTSC_LINE_COUNT 12
43#define VBI_PAL_LINE_COUNT 18
Steven Toth4f9c4142011-10-10 11:09:54 -030044
45
Steven Tothe47f30b2008-01-10 04:25:59 -030046int cx23885_vbi_fmt(struct file *file, void *priv,
47 struct v4l2_format *f)
48{
Hans Verkuil568f44a2014-04-23 08:43:51 -030049 struct cx23885_dev *dev = video_drvdata(file);
Steven Tothe47f30b2008-01-10 04:25:59 -030050
Hans Verkuil453afdd2014-08-14 06:43:01 -030051 f->fmt.vbi.sampling_rate = 27000000;
52 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
53 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
54 f->fmt.vbi.offset = 0;
55 f->fmt.vbi.flags = 0;
Steven Tothe47f30b2008-01-10 04:25:59 -030056 if (dev->tvnorm & V4L2_STD_525_60) {
57 /* ntsc */
Hans Verkuil420b2172014-09-20 10:19:32 -030058 f->fmt.vbi.start[0] = V4L2_VBI_ITU_525_F1_START + 9;
59 f->fmt.vbi.start[1] = V4L2_VBI_ITU_525_F2_START + 9;
Hans Verkuil453afdd2014-08-14 06:43:01 -030060 f->fmt.vbi.count[0] = VBI_NTSC_LINE_COUNT;
61 f->fmt.vbi.count[1] = VBI_NTSC_LINE_COUNT;
Steven Tothe47f30b2008-01-10 04:25:59 -030062 } else if (dev->tvnorm & V4L2_STD_625_50) {
63 /* pal */
Hans Verkuil420b2172014-09-20 10:19:32 -030064 f->fmt.vbi.start[0] = V4L2_VBI_ITU_625_F1_START + 5;
65 f->fmt.vbi.start[1] = V4L2_VBI_ITU_625_F2_START + 5;
Hans Verkuil453afdd2014-08-14 06:43:01 -030066 f->fmt.vbi.count[0] = VBI_PAL_LINE_COUNT;
67 f->fmt.vbi.count[1] = VBI_PAL_LINE_COUNT;
Steven Tothe47f30b2008-01-10 04:25:59 -030068 }
Steven Toth4f9c4142011-10-10 11:09:54 -030069
Steven Tothe47f30b2008-01-10 04:25:59 -030070 return 0;
71}
72
Steven Tothb5f74052011-10-10 11:09:54 -030073/* We're given the Video Interrupt status register.
74 * The cx23885_video_irq() func has already validated
75 * the potential error bits, we just need to
76 * deal with vbi payload and return indication if
77 * we actually processed any payload.
78 */
79int cx23885_vbi_irq(struct cx23885_dev *dev, u32 status)
80{
81 u32 count;
82 int handled = 0;
83
84 if (status & VID_BC_MSK_VBI_RISCI1) {
85 dprintk(1, "%s() VID_BC_MSK_VBI_RISCI1\n", __func__);
86 spin_lock(&dev->slock);
Hans Verkuilecc2fe22015-11-30 14:47:00 -020087 count = cx_read(VBI_A_GPCNT);
Steven Tothb5f74052011-10-10 11:09:54 -030088 cx23885_video_wakeup(dev, &dev->vbiq, count);
89 spin_unlock(&dev->slock);
90 handled++;
91 }
92
Steven Tothb5f74052011-10-10 11:09:54 -030093 return handled;
94}
95
Steven Tothe47f30b2008-01-10 04:25:59 -030096static int cx23885_start_vbi_dma(struct cx23885_dev *dev,
97 struct cx23885_dmaqueue *q,
98 struct cx23885_buffer *buf)
99{
Steven Tothb5f74052011-10-10 11:09:54 -0300100 dprintk(1, "%s()\n", __func__);
101
Steven Tothe47f30b2008-01-10 04:25:59 -0300102 /* setup fifo + format */
103 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH02],
Hans Verkuil453afdd2014-08-14 06:43:01 -0300104 VBI_LINE_LENGTH, buf->risc.dma);
Steven Tothe47f30b2008-01-10 04:25:59 -0300105
106 /* reset counter */
Steven Toth4f9c4142011-10-10 11:09:54 -0300107 cx_write(VID_A_VBI_CTRL, 3);
108 cx_write(VBI_A_GPCNT_CTL, 3);
Hans Verkuil453afdd2014-08-14 06:43:01 -0300109 q->count = 0;
Steven Tothe47f30b2008-01-10 04:25:59 -0300110
Steven Tothb5f74052011-10-10 11:09:54 -0300111 /* enable irq */
Andy Wallsdbe83a32010-07-19 01:19:43 -0300112 cx23885_irq_add_enable(dev, 0x01);
Steven Tothe47f30b2008-01-10 04:25:59 -0300113 cx_set(VID_A_INT_MSK, 0x000022);
114
115 /* start dma */
116 cx_set(DEV_CNTRL2, (1<<5));
Steven Tothb5f74052011-10-10 11:09:54 -0300117 cx_set(VID_A_DMA_CTL, 0x22); /* FIFO and RISC enable */
Steven Tothe47f30b2008-01-10 04:25:59 -0300118
119 return 0;
120}
121
Steven Tothe47f30b2008-01-10 04:25:59 -0300122/* ------------------------------------------------------------------ */
Steven Tothe47f30b2008-01-10 04:25:59 -0300123
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200124static int queue_setup(struct vb2_queue *q,
Hans Verkuil453afdd2014-08-14 06:43:01 -0300125 unsigned int *num_buffers, unsigned int *num_planes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300126 unsigned int sizes[], struct device *alloc_devs[])
Steven Tothe47f30b2008-01-10 04:25:59 -0300127{
Hans Verkuil453afdd2014-08-14 06:43:01 -0300128 struct cx23885_dev *dev = q->drv_priv;
129 unsigned lines = VBI_PAL_LINE_COUNT;
130
131 if (dev->tvnorm & V4L2_STD_525_60)
132 lines = VBI_NTSC_LINE_COUNT;
133 *num_planes = 1;
134 sizes[0] = lines * VBI_LINE_LENGTH * 2;
Steven Tothe47f30b2008-01-10 04:25:59 -0300135 return 0;
136}
137
Hans Verkuil453afdd2014-08-14 06:43:01 -0300138static int buffer_prepare(struct vb2_buffer *vb)
Steven Tothe47f30b2008-01-10 04:25:59 -0300139{
Junghak Sung2d700712015-09-22 10:30:30 -0300140 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuil453afdd2014-08-14 06:43:01 -0300141 struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
Junghak Sung2d700712015-09-22 10:30:30 -0300142 struct cx23885_buffer *buf = container_of(vbuf,
Steven Tothe47f30b2008-01-10 04:25:59 -0300143 struct cx23885_buffer, vb);
Hans Verkuil453afdd2014-08-14 06:43:01 -0300144 struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
145 unsigned lines = VBI_PAL_LINE_COUNT;
Steven Tothe47f30b2008-01-10 04:25:59 -0300146
Hans Verkuil453afdd2014-08-14 06:43:01 -0300147 if (dev->tvnorm & V4L2_STD_525_60)
148 lines = VBI_NTSC_LINE_COUNT;
149
150 if (vb2_plane_size(vb, 0) < lines * VBI_LINE_LENGTH * 2)
Steven Tothe47f30b2008-01-10 04:25:59 -0300151 return -EINVAL;
Hans Verkuil453afdd2014-08-14 06:43:01 -0300152 vb2_set_plane_payload(vb, 0, lines * VBI_LINE_LENGTH * 2);
Steven Tothe47f30b2008-01-10 04:25:59 -0300153
Hans Verkuil453afdd2014-08-14 06:43:01 -0300154 cx23885_risc_vbibuffer(dev->pci, &buf->risc,
155 sgt->sgl,
156 0, VBI_LINE_LENGTH * lines,
157 VBI_LINE_LENGTH, 0,
158 lines);
Steven Tothe47f30b2008-01-10 04:25:59 -0300159 return 0;
Steven Tothe47f30b2008-01-10 04:25:59 -0300160}
161
Hans Verkuil453afdd2014-08-14 06:43:01 -0300162static void buffer_finish(struct vb2_buffer *vb)
Steven Tothe47f30b2008-01-10 04:25:59 -0300163{
Junghak Sung2d700712015-09-22 10:30:30 -0300164 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
165 struct cx23885_buffer *buf = container_of(vbuf,
Hans Verkuil453afdd2014-08-14 06:43:01 -0300166 struct cx23885_buffer, vb);
Steven Tothe47f30b2008-01-10 04:25:59 -0300167
Hans Verkuil453afdd2014-08-14 06:43:01 -0300168 cx23885_free_buffer(vb->vb2_queue->drv_priv, buf);
Hans Verkuil453afdd2014-08-14 06:43:01 -0300169}
170
171/*
172 * The risc program for each buffer works as follows: it starts with a simple
173 * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the
174 * buffer follows and at the end we have a JUMP back to the start + 12 (skipping
175 * the initial JUMP).
176 *
177 * This is the risc program of the first buffer to be queued if the active list
178 * is empty and it just keeps DMAing this buffer without generating any
179 * interrupts.
180 *
181 * If a new buffer is added then the initial JUMP in the code for that buffer
182 * will generate an interrupt which signals that the previous buffer has been
183 * DMAed successfully and that it can be returned to userspace.
184 *
185 * It also sets the final jump of the previous buffer to the start of the new
186 * buffer, thus chaining the new buffer into the DMA chain. This is a single
187 * atomic u32 write, so there is no race condition.
188 *
189 * The end-result of all this that you only get an interrupt when a buffer
190 * is ready, so the control flow is very easy.
191 */
192static void buffer_queue(struct vb2_buffer *vb)
193{
Junghak Sung2d700712015-09-22 10:30:30 -0300194 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuil453afdd2014-08-14 06:43:01 -0300195 struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
Junghak Sung2d700712015-09-22 10:30:30 -0300196 struct cx23885_buffer *buf = container_of(vbuf,
197 struct cx23885_buffer, vb);
Hans Verkuil453afdd2014-08-14 06:43:01 -0300198 struct cx23885_buffer *prev;
199 struct cx23885_dmaqueue *q = &dev->vbiq;
200 unsigned long flags;
201
202 buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
203 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
204 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
Steven Tothe47f30b2008-01-10 04:25:59 -0300205 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
206
207 if (list_empty(&q->active)) {
Hans Verkuil453afdd2014-08-14 06:43:01 -0300208 spin_lock_irqsave(&dev->slock, flags);
209 list_add_tail(&buf->queue, &q->active);
210 spin_unlock_irqrestore(&dev->slock, flags);
Steven Tothe47f30b2008-01-10 04:25:59 -0300211 dprintk(2, "[%p/%d] vbi_queue - first active\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300212 buf, buf->vb.vb2_buf.index);
Steven Tothe47f30b2008-01-10 04:25:59 -0300213
214 } else {
Hans Verkuil453afdd2014-08-14 06:43:01 -0300215 buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
Steven Tothe47f30b2008-01-10 04:25:59 -0300216 prev = list_entry(q->active.prev, struct cx23885_buffer,
Hans Verkuil453afdd2014-08-14 06:43:01 -0300217 queue);
218 spin_lock_irqsave(&dev->slock, flags);
219 list_add_tail(&buf->queue, &q->active);
220 spin_unlock_irqrestore(&dev->slock, flags);
Steven Tothe47f30b2008-01-10 04:25:59 -0300221 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
Steven Tothe47f30b2008-01-10 04:25:59 -0300222 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300223 buf, buf->vb.vb2_buf.index);
Steven Tothe47f30b2008-01-10 04:25:59 -0300224 }
225}
226
Hans Verkuil453afdd2014-08-14 06:43:01 -0300227static int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
Steven Tothe47f30b2008-01-10 04:25:59 -0300228{
Hans Verkuil453afdd2014-08-14 06:43:01 -0300229 struct cx23885_dev *dev = q->drv_priv;
230 struct cx23885_dmaqueue *dmaq = &dev->vbiq;
231 struct cx23885_buffer *buf = list_entry(dmaq->active.next,
232 struct cx23885_buffer, queue);
Steven Tothe47f30b2008-01-10 04:25:59 -0300233
Hans Verkuil453afdd2014-08-14 06:43:01 -0300234 cx23885_start_vbi_dma(dev, dmaq, buf);
235 return 0;
Steven Tothe47f30b2008-01-10 04:25:59 -0300236}
237
Hans Verkuil453afdd2014-08-14 06:43:01 -0300238static void cx23885_stop_streaming(struct vb2_queue *q)
239{
240 struct cx23885_dev *dev = q->drv_priv;
241 struct cx23885_dmaqueue *dmaq = &dev->vbiq;
242 unsigned long flags;
Steven Tothe47f30b2008-01-10 04:25:59 -0300243
Hans Verkuil453afdd2014-08-14 06:43:01 -0300244 cx_clear(VID_A_DMA_CTL, 0x22); /* FIFO and RISC enable */
245 spin_lock_irqsave(&dev->slock, flags);
246 while (!list_empty(&dmaq->active)) {
247 struct cx23885_buffer *buf = list_entry(dmaq->active.next,
248 struct cx23885_buffer, queue);
249
250 list_del(&buf->queue);
Junghak Sung2d700712015-09-22 10:30:30 -0300251 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Hans Verkuil453afdd2014-08-14 06:43:01 -0300252 }
253 spin_unlock_irqrestore(&dev->slock, flags);
254}
255
256
257struct vb2_ops cx23885_vbi_qops = {
258 .queue_setup = queue_setup,
259 .buf_prepare = buffer_prepare,
260 .buf_finish = buffer_finish,
261 .buf_queue = buffer_queue,
262 .wait_prepare = vb2_ops_wait_prepare,
263 .wait_finish = vb2_ops_wait_finish,
264 .start_streaming = cx23885_start_streaming,
265 .stop_streaming = cx23885_stop_streaming,
266};