blob: 75e7fa7b11216e9f99f47618f0c80c184bc407d4 [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
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/moduleparam.h>
21#include <linux/init.h>
Steven Tothe47f30b2008-01-10 04:25:59 -030022
23#include "cx23885.h"
24
25static 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)\
35 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
36 } while (0)
Steven Tothe47f30b2008-01-10 04:25:59 -030037
38/* ------------------------------------------------------------------ */
39
Steven Toth4f9c4142011-10-10 11:09:54 -030040#define VBI_LINE_LENGTH 1440
Hans Verkuil453afdd2014-08-14 06:43:01 -030041#define VBI_NTSC_LINE_COUNT 12
42#define VBI_PAL_LINE_COUNT 18
Steven Toth4f9c4142011-10-10 11:09:54 -030043
44
Steven Tothe47f30b2008-01-10 04:25:59 -030045int cx23885_vbi_fmt(struct file *file, void *priv,
46 struct v4l2_format *f)
47{
Hans Verkuil568f44a2014-04-23 08:43:51 -030048 struct cx23885_dev *dev = video_drvdata(file);
Steven Tothe47f30b2008-01-10 04:25:59 -030049
Hans Verkuil453afdd2014-08-14 06:43:01 -030050 f->fmt.vbi.sampling_rate = 27000000;
51 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
52 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
53 f->fmt.vbi.offset = 0;
54 f->fmt.vbi.flags = 0;
Steven Tothe47f30b2008-01-10 04:25:59 -030055 if (dev->tvnorm & V4L2_STD_525_60) {
56 /* ntsc */
Hans Verkuil420b2172014-09-20 10:19:32 -030057 f->fmt.vbi.start[0] = V4L2_VBI_ITU_525_F1_START + 9;
58 f->fmt.vbi.start[1] = V4L2_VBI_ITU_525_F2_START + 9;
Hans Verkuil453afdd2014-08-14 06:43:01 -030059 f->fmt.vbi.count[0] = VBI_NTSC_LINE_COUNT;
60 f->fmt.vbi.count[1] = VBI_NTSC_LINE_COUNT;
Steven Tothe47f30b2008-01-10 04:25:59 -030061 } else if (dev->tvnorm & V4L2_STD_625_50) {
62 /* pal */
Hans Verkuil420b2172014-09-20 10:19:32 -030063 f->fmt.vbi.start[0] = V4L2_VBI_ITU_625_F1_START + 5;
64 f->fmt.vbi.start[1] = V4L2_VBI_ITU_625_F2_START + 5;
Hans Verkuil453afdd2014-08-14 06:43:01 -030065 f->fmt.vbi.count[0] = VBI_PAL_LINE_COUNT;
66 f->fmt.vbi.count[1] = VBI_PAL_LINE_COUNT;
Steven Tothe47f30b2008-01-10 04:25:59 -030067 }
Steven Toth4f9c4142011-10-10 11:09:54 -030068
Steven Tothe47f30b2008-01-10 04:25:59 -030069 return 0;
70}
71
Steven Tothb5f74052011-10-10 11:09:54 -030072/* We're given the Video Interrupt status register.
73 * The cx23885_video_irq() func has already validated
74 * the potential error bits, we just need to
75 * deal with vbi payload and return indication if
76 * we actually processed any payload.
77 */
78int cx23885_vbi_irq(struct cx23885_dev *dev, u32 status)
79{
80 u32 count;
81 int handled = 0;
82
83 if (status & VID_BC_MSK_VBI_RISCI1) {
84 dprintk(1, "%s() VID_BC_MSK_VBI_RISCI1\n", __func__);
85 spin_lock(&dev->slock);
Hans Verkuilecc2fe22015-11-30 14:47:00 -020086 count = cx_read(VBI_A_GPCNT);
Steven Tothb5f74052011-10-10 11:09:54 -030087 cx23885_video_wakeup(dev, &dev->vbiq, count);
88 spin_unlock(&dev->slock);
89 handled++;
90 }
91
Steven Tothb5f74052011-10-10 11:09:54 -030092 return handled;
93}
94
Steven Tothe47f30b2008-01-10 04:25:59 -030095static int cx23885_start_vbi_dma(struct cx23885_dev *dev,
96 struct cx23885_dmaqueue *q,
97 struct cx23885_buffer *buf)
98{
Steven Tothb5f74052011-10-10 11:09:54 -030099 dprintk(1, "%s()\n", __func__);
100
Steven Tothe47f30b2008-01-10 04:25:59 -0300101 /* setup fifo + format */
102 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH02],
Hans Verkuil453afdd2014-08-14 06:43:01 -0300103 VBI_LINE_LENGTH, buf->risc.dma);
Steven Tothe47f30b2008-01-10 04:25:59 -0300104
105 /* reset counter */
Steven Toth4f9c4142011-10-10 11:09:54 -0300106 cx_write(VID_A_VBI_CTRL, 3);
107 cx_write(VBI_A_GPCNT_CTL, 3);
Hans Verkuil453afdd2014-08-14 06:43:01 -0300108 q->count = 0;
Steven Tothe47f30b2008-01-10 04:25:59 -0300109
Steven Tothb5f74052011-10-10 11:09:54 -0300110 /* enable irq */
Andy Wallsdbe83a32010-07-19 01:19:43 -0300111 cx23885_irq_add_enable(dev, 0x01);
Steven Tothe47f30b2008-01-10 04:25:59 -0300112 cx_set(VID_A_INT_MSK, 0x000022);
113
114 /* start dma */
115 cx_set(DEV_CNTRL2, (1<<5));
Steven Tothb5f74052011-10-10 11:09:54 -0300116 cx_set(VID_A_DMA_CTL, 0x22); /* FIFO and RISC enable */
Steven Tothe47f30b2008-01-10 04:25:59 -0300117
118 return 0;
119}
120
Steven Tothe47f30b2008-01-10 04:25:59 -0300121/* ------------------------------------------------------------------ */
Steven Tothe47f30b2008-01-10 04:25:59 -0300122
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200123static int queue_setup(struct vb2_queue *q,
Hans Verkuil453afdd2014-08-14 06:43:01 -0300124 unsigned int *num_buffers, unsigned int *num_planes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300125 unsigned int sizes[], struct device *alloc_devs[])
Steven Tothe47f30b2008-01-10 04:25:59 -0300126{
Hans Verkuil453afdd2014-08-14 06:43:01 -0300127 struct cx23885_dev *dev = q->drv_priv;
128 unsigned lines = VBI_PAL_LINE_COUNT;
129
130 if (dev->tvnorm & V4L2_STD_525_60)
131 lines = VBI_NTSC_LINE_COUNT;
132 *num_planes = 1;
133 sizes[0] = lines * VBI_LINE_LENGTH * 2;
Steven Tothe47f30b2008-01-10 04:25:59 -0300134 return 0;
135}
136
Hans Verkuil453afdd2014-08-14 06:43:01 -0300137static int buffer_prepare(struct vb2_buffer *vb)
Steven Tothe47f30b2008-01-10 04:25:59 -0300138{
Junghak Sung2d700712015-09-22 10:30:30 -0300139 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuil453afdd2014-08-14 06:43:01 -0300140 struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
Junghak Sung2d700712015-09-22 10:30:30 -0300141 struct cx23885_buffer *buf = container_of(vbuf,
Steven Tothe47f30b2008-01-10 04:25:59 -0300142 struct cx23885_buffer, vb);
Hans Verkuil453afdd2014-08-14 06:43:01 -0300143 struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
144 unsigned lines = VBI_PAL_LINE_COUNT;
Steven Tothe47f30b2008-01-10 04:25:59 -0300145
Hans Verkuil453afdd2014-08-14 06:43:01 -0300146 if (dev->tvnorm & V4L2_STD_525_60)
147 lines = VBI_NTSC_LINE_COUNT;
148
149 if (vb2_plane_size(vb, 0) < lines * VBI_LINE_LENGTH * 2)
Steven Tothe47f30b2008-01-10 04:25:59 -0300150 return -EINVAL;
Hans Verkuil453afdd2014-08-14 06:43:01 -0300151 vb2_set_plane_payload(vb, 0, lines * VBI_LINE_LENGTH * 2);
Steven Tothe47f30b2008-01-10 04:25:59 -0300152
Hans Verkuil453afdd2014-08-14 06:43:01 -0300153 cx23885_risc_vbibuffer(dev->pci, &buf->risc,
154 sgt->sgl,
155 0, VBI_LINE_LENGTH * lines,
156 VBI_LINE_LENGTH, 0,
157 lines);
Steven Tothe47f30b2008-01-10 04:25:59 -0300158 return 0;
Steven Tothe47f30b2008-01-10 04:25:59 -0300159}
160
Hans Verkuil453afdd2014-08-14 06:43:01 -0300161static void buffer_finish(struct vb2_buffer *vb)
Steven Tothe47f30b2008-01-10 04:25:59 -0300162{
Junghak Sung2d700712015-09-22 10:30:30 -0300163 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
164 struct cx23885_buffer *buf = container_of(vbuf,
Hans Verkuil453afdd2014-08-14 06:43:01 -0300165 struct cx23885_buffer, vb);
Steven Tothe47f30b2008-01-10 04:25:59 -0300166
Hans Verkuil453afdd2014-08-14 06:43:01 -0300167 cx23885_free_buffer(vb->vb2_queue->drv_priv, buf);
Hans Verkuil453afdd2014-08-14 06:43:01 -0300168}
169
170/*
171 * The risc program for each buffer works as follows: it starts with a simple
172 * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the
173 * buffer follows and at the end we have a JUMP back to the start + 12 (skipping
174 * the initial JUMP).
175 *
176 * This is the risc program of the first buffer to be queued if the active list
177 * is empty and it just keeps DMAing this buffer without generating any
178 * interrupts.
179 *
180 * If a new buffer is added then the initial JUMP in the code for that buffer
181 * will generate an interrupt which signals that the previous buffer has been
182 * DMAed successfully and that it can be returned to userspace.
183 *
184 * It also sets the final jump of the previous buffer to the start of the new
185 * buffer, thus chaining the new buffer into the DMA chain. This is a single
186 * atomic u32 write, so there is no race condition.
187 *
188 * The end-result of all this that you only get an interrupt when a buffer
189 * is ready, so the control flow is very easy.
190 */
191static void buffer_queue(struct vb2_buffer *vb)
192{
Junghak Sung2d700712015-09-22 10:30:30 -0300193 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuil453afdd2014-08-14 06:43:01 -0300194 struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
Junghak Sung2d700712015-09-22 10:30:30 -0300195 struct cx23885_buffer *buf = container_of(vbuf,
196 struct cx23885_buffer, vb);
Hans Verkuil453afdd2014-08-14 06:43:01 -0300197 struct cx23885_buffer *prev;
198 struct cx23885_dmaqueue *q = &dev->vbiq;
199 unsigned long flags;
200
201 buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
202 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
203 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
Steven Tothe47f30b2008-01-10 04:25:59 -0300204 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
205
206 if (list_empty(&q->active)) {
Hans Verkuil453afdd2014-08-14 06:43:01 -0300207 spin_lock_irqsave(&dev->slock, flags);
208 list_add_tail(&buf->queue, &q->active);
209 spin_unlock_irqrestore(&dev->slock, flags);
Steven Tothe47f30b2008-01-10 04:25:59 -0300210 dprintk(2, "[%p/%d] vbi_queue - first active\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300211 buf, buf->vb.vb2_buf.index);
Steven Tothe47f30b2008-01-10 04:25:59 -0300212
213 } else {
Hans Verkuil453afdd2014-08-14 06:43:01 -0300214 buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
Steven Tothe47f30b2008-01-10 04:25:59 -0300215 prev = list_entry(q->active.prev, struct cx23885_buffer,
Hans Verkuil453afdd2014-08-14 06:43:01 -0300216 queue);
217 spin_lock_irqsave(&dev->slock, flags);
218 list_add_tail(&buf->queue, &q->active);
219 spin_unlock_irqrestore(&dev->slock, flags);
Steven Tothe47f30b2008-01-10 04:25:59 -0300220 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
Steven Tothe47f30b2008-01-10 04:25:59 -0300221 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300222 buf, buf->vb.vb2_buf.index);
Steven Tothe47f30b2008-01-10 04:25:59 -0300223 }
224}
225
Hans Verkuil453afdd2014-08-14 06:43:01 -0300226static int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
Steven Tothe47f30b2008-01-10 04:25:59 -0300227{
Hans Verkuil453afdd2014-08-14 06:43:01 -0300228 struct cx23885_dev *dev = q->drv_priv;
229 struct cx23885_dmaqueue *dmaq = &dev->vbiq;
230 struct cx23885_buffer *buf = list_entry(dmaq->active.next,
231 struct cx23885_buffer, queue);
Steven Tothe47f30b2008-01-10 04:25:59 -0300232
Hans Verkuil453afdd2014-08-14 06:43:01 -0300233 cx23885_start_vbi_dma(dev, dmaq, buf);
234 return 0;
Steven Tothe47f30b2008-01-10 04:25:59 -0300235}
236
Hans Verkuil453afdd2014-08-14 06:43:01 -0300237static void cx23885_stop_streaming(struct vb2_queue *q)
238{
239 struct cx23885_dev *dev = q->drv_priv;
240 struct cx23885_dmaqueue *dmaq = &dev->vbiq;
241 unsigned long flags;
Steven Tothe47f30b2008-01-10 04:25:59 -0300242
Hans Verkuil453afdd2014-08-14 06:43:01 -0300243 cx_clear(VID_A_DMA_CTL, 0x22); /* FIFO and RISC enable */
244 spin_lock_irqsave(&dev->slock, flags);
245 while (!list_empty(&dmaq->active)) {
246 struct cx23885_buffer *buf = list_entry(dmaq->active.next,
247 struct cx23885_buffer, queue);
248
249 list_del(&buf->queue);
Junghak Sung2d700712015-09-22 10:30:30 -0300250 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Hans Verkuil453afdd2014-08-14 06:43:01 -0300251 }
252 spin_unlock_irqrestore(&dev->slock, flags);
253}
254
255
256struct vb2_ops cx23885_vbi_qops = {
257 .queue_setup = queue_setup,
258 .buf_prepare = buffer_prepare,
259 .buf_finish = buffer_finish,
260 .buf_queue = buffer_queue,
261 .wait_prepare = vb2_ops_wait_prepare,
262 .wait_finish = vb2_ops_wait_finish,
263 .start_streaming = cx23885_start_streaming,
264 .stop_streaming = cx23885_stop_streaming,
265};