18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Driver for the Conexant CX23885 PCIe bridge 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include "cx23885.h" 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/kernel.h> 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/moduleparam.h> 138c2ecf20Sopenharmony_ci#include <linux/init.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_cistatic unsigned int vbibufs = 4; 168c2ecf20Sopenharmony_cimodule_param(vbibufs, int, 0644); 178c2ecf20Sopenharmony_ciMODULE_PARM_DESC(vbibufs, "number of vbi buffers, range 2-32"); 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_cistatic unsigned int vbi_debug; 208c2ecf20Sopenharmony_cimodule_param(vbi_debug, int, 0644); 218c2ecf20Sopenharmony_ciMODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]"); 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#define dprintk(level, fmt, arg...)\ 248c2ecf20Sopenharmony_ci do { if (vbi_debug >= level)\ 258c2ecf20Sopenharmony_ci printk(KERN_DEBUG pr_fmt("%s: vbi:" fmt), \ 268c2ecf20Sopenharmony_ci __func__, ##arg); \ 278c2ecf20Sopenharmony_ci } while (0) 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------ */ 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#define VBI_LINE_LENGTH 1440 328c2ecf20Sopenharmony_ci#define VBI_NTSC_LINE_COUNT 12 338c2ecf20Sopenharmony_ci#define VBI_PAL_LINE_COUNT 18 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ciint cx23885_vbi_fmt(struct file *file, void *priv, 378c2ecf20Sopenharmony_ci struct v4l2_format *f) 388c2ecf20Sopenharmony_ci{ 398c2ecf20Sopenharmony_ci struct cx23885_dev *dev = video_drvdata(file); 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci f->fmt.vbi.sampling_rate = 27000000; 428c2ecf20Sopenharmony_ci f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH; 438c2ecf20Sopenharmony_ci f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY; 448c2ecf20Sopenharmony_ci f->fmt.vbi.offset = 0; 458c2ecf20Sopenharmony_ci f->fmt.vbi.flags = 0; 468c2ecf20Sopenharmony_ci if (dev->tvnorm & V4L2_STD_525_60) { 478c2ecf20Sopenharmony_ci /* ntsc */ 488c2ecf20Sopenharmony_ci f->fmt.vbi.start[0] = V4L2_VBI_ITU_525_F1_START + 9; 498c2ecf20Sopenharmony_ci f->fmt.vbi.start[1] = V4L2_VBI_ITU_525_F2_START + 9; 508c2ecf20Sopenharmony_ci f->fmt.vbi.count[0] = VBI_NTSC_LINE_COUNT; 518c2ecf20Sopenharmony_ci f->fmt.vbi.count[1] = VBI_NTSC_LINE_COUNT; 528c2ecf20Sopenharmony_ci } else if (dev->tvnorm & V4L2_STD_625_50) { 538c2ecf20Sopenharmony_ci /* pal */ 548c2ecf20Sopenharmony_ci f->fmt.vbi.start[0] = V4L2_VBI_ITU_625_F1_START + 5; 558c2ecf20Sopenharmony_ci f->fmt.vbi.start[1] = V4L2_VBI_ITU_625_F2_START + 5; 568c2ecf20Sopenharmony_ci f->fmt.vbi.count[0] = VBI_PAL_LINE_COUNT; 578c2ecf20Sopenharmony_ci f->fmt.vbi.count[1] = VBI_PAL_LINE_COUNT; 588c2ecf20Sopenharmony_ci } 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci return 0; 618c2ecf20Sopenharmony_ci} 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci/* We're given the Video Interrupt status register. 648c2ecf20Sopenharmony_ci * The cx23885_video_irq() func has already validated 658c2ecf20Sopenharmony_ci * the potential error bits, we just need to 668c2ecf20Sopenharmony_ci * deal with vbi payload and return indication if 678c2ecf20Sopenharmony_ci * we actually processed any payload. 688c2ecf20Sopenharmony_ci */ 698c2ecf20Sopenharmony_ciint cx23885_vbi_irq(struct cx23885_dev *dev, u32 status) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci u32 count; 728c2ecf20Sopenharmony_ci int handled = 0; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci if (status & VID_BC_MSK_VBI_RISCI1) { 758c2ecf20Sopenharmony_ci dprintk(1, "%s() VID_BC_MSK_VBI_RISCI1\n", __func__); 768c2ecf20Sopenharmony_ci spin_lock(&dev->slock); 778c2ecf20Sopenharmony_ci count = cx_read(VBI_A_GPCNT); 788c2ecf20Sopenharmony_ci cx23885_video_wakeup(dev, &dev->vbiq, count); 798c2ecf20Sopenharmony_ci spin_unlock(&dev->slock); 808c2ecf20Sopenharmony_ci handled++; 818c2ecf20Sopenharmony_ci } 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci return handled; 848c2ecf20Sopenharmony_ci} 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_cistatic int cx23885_start_vbi_dma(struct cx23885_dev *dev, 878c2ecf20Sopenharmony_ci struct cx23885_dmaqueue *q, 888c2ecf20Sopenharmony_ci struct cx23885_buffer *buf) 898c2ecf20Sopenharmony_ci{ 908c2ecf20Sopenharmony_ci dprintk(1, "%s()\n", __func__); 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci /* setup fifo + format */ 938c2ecf20Sopenharmony_ci cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH02], 948c2ecf20Sopenharmony_ci VBI_LINE_LENGTH, buf->risc.dma); 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci /* reset counter */ 978c2ecf20Sopenharmony_ci cx_write(VID_A_VBI_CTRL, 3); 988c2ecf20Sopenharmony_ci cx_write(VBI_A_GPCNT_CTL, 3); 998c2ecf20Sopenharmony_ci q->count = 0; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci /* enable irq */ 1028c2ecf20Sopenharmony_ci cx23885_irq_add_enable(dev, 0x01); 1038c2ecf20Sopenharmony_ci cx_set(VID_A_INT_MSK, 0x000022); 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci /* start dma */ 1068c2ecf20Sopenharmony_ci cx_set(DEV_CNTRL2, (1<<5)); 1078c2ecf20Sopenharmony_ci cx_set(VID_A_DMA_CTL, 0x22); /* FIFO and RISC enable */ 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci return 0; 1108c2ecf20Sopenharmony_ci} 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------ */ 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_cistatic int queue_setup(struct vb2_queue *q, 1158c2ecf20Sopenharmony_ci unsigned int *num_buffers, unsigned int *num_planes, 1168c2ecf20Sopenharmony_ci unsigned int sizes[], struct device *alloc_devs[]) 1178c2ecf20Sopenharmony_ci{ 1188c2ecf20Sopenharmony_ci struct cx23885_dev *dev = q->drv_priv; 1198c2ecf20Sopenharmony_ci unsigned lines = VBI_PAL_LINE_COUNT; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci if (dev->tvnorm & V4L2_STD_525_60) 1228c2ecf20Sopenharmony_ci lines = VBI_NTSC_LINE_COUNT; 1238c2ecf20Sopenharmony_ci *num_planes = 1; 1248c2ecf20Sopenharmony_ci sizes[0] = lines * VBI_LINE_LENGTH * 2; 1258c2ecf20Sopenharmony_ci return 0; 1268c2ecf20Sopenharmony_ci} 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_cistatic int buffer_prepare(struct vb2_buffer *vb) 1298c2ecf20Sopenharmony_ci{ 1308c2ecf20Sopenharmony_ci struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1318c2ecf20Sopenharmony_ci struct cx23885_dev *dev = vb->vb2_queue->drv_priv; 1328c2ecf20Sopenharmony_ci struct cx23885_buffer *buf = container_of(vbuf, 1338c2ecf20Sopenharmony_ci struct cx23885_buffer, vb); 1348c2ecf20Sopenharmony_ci struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0); 1358c2ecf20Sopenharmony_ci unsigned lines = VBI_PAL_LINE_COUNT; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci if (dev->tvnorm & V4L2_STD_525_60) 1388c2ecf20Sopenharmony_ci lines = VBI_NTSC_LINE_COUNT; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci if (vb2_plane_size(vb, 0) < lines * VBI_LINE_LENGTH * 2) 1418c2ecf20Sopenharmony_ci return -EINVAL; 1428c2ecf20Sopenharmony_ci vb2_set_plane_payload(vb, 0, lines * VBI_LINE_LENGTH * 2); 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci cx23885_risc_vbibuffer(dev->pci, &buf->risc, 1458c2ecf20Sopenharmony_ci sgt->sgl, 1468c2ecf20Sopenharmony_ci 0, VBI_LINE_LENGTH * lines, 1478c2ecf20Sopenharmony_ci VBI_LINE_LENGTH, 0, 1488c2ecf20Sopenharmony_ci lines); 1498c2ecf20Sopenharmony_ci return 0; 1508c2ecf20Sopenharmony_ci} 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_cistatic void buffer_finish(struct vb2_buffer *vb) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1558c2ecf20Sopenharmony_ci struct cx23885_buffer *buf = container_of(vbuf, 1568c2ecf20Sopenharmony_ci struct cx23885_buffer, vb); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci cx23885_free_buffer(vb->vb2_queue->drv_priv, buf); 1598c2ecf20Sopenharmony_ci} 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci/* 1628c2ecf20Sopenharmony_ci * The risc program for each buffer works as follows: it starts with a simple 1638c2ecf20Sopenharmony_ci * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the 1648c2ecf20Sopenharmony_ci * buffer follows and at the end we have a JUMP back to the start + 12 (skipping 1658c2ecf20Sopenharmony_ci * the initial JUMP). 1668c2ecf20Sopenharmony_ci * 1678c2ecf20Sopenharmony_ci * This is the risc program of the first buffer to be queued if the active list 1688c2ecf20Sopenharmony_ci * is empty and it just keeps DMAing this buffer without generating any 1698c2ecf20Sopenharmony_ci * interrupts. 1708c2ecf20Sopenharmony_ci * 1718c2ecf20Sopenharmony_ci * If a new buffer is added then the initial JUMP in the code for that buffer 1728c2ecf20Sopenharmony_ci * will generate an interrupt which signals that the previous buffer has been 1738c2ecf20Sopenharmony_ci * DMAed successfully and that it can be returned to userspace. 1748c2ecf20Sopenharmony_ci * 1758c2ecf20Sopenharmony_ci * It also sets the final jump of the previous buffer to the start of the new 1768c2ecf20Sopenharmony_ci * buffer, thus chaining the new buffer into the DMA chain. This is a single 1778c2ecf20Sopenharmony_ci * atomic u32 write, so there is no race condition. 1788c2ecf20Sopenharmony_ci * 1798c2ecf20Sopenharmony_ci * The end-result of all this that you only get an interrupt when a buffer 1808c2ecf20Sopenharmony_ci * is ready, so the control flow is very easy. 1818c2ecf20Sopenharmony_ci */ 1828c2ecf20Sopenharmony_cistatic void buffer_queue(struct vb2_buffer *vb) 1838c2ecf20Sopenharmony_ci{ 1848c2ecf20Sopenharmony_ci struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1858c2ecf20Sopenharmony_ci struct cx23885_dev *dev = vb->vb2_queue->drv_priv; 1868c2ecf20Sopenharmony_ci struct cx23885_buffer *buf = container_of(vbuf, 1878c2ecf20Sopenharmony_ci struct cx23885_buffer, vb); 1888c2ecf20Sopenharmony_ci struct cx23885_buffer *prev; 1898c2ecf20Sopenharmony_ci struct cx23885_dmaqueue *q = &dev->vbiq; 1908c2ecf20Sopenharmony_ci unsigned long flags; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12); 1938c2ecf20Sopenharmony_ci buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC); 1948c2ecf20Sopenharmony_ci buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12); 1958c2ecf20Sopenharmony_ci buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */ 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci if (list_empty(&q->active)) { 1988c2ecf20Sopenharmony_ci spin_lock_irqsave(&dev->slock, flags); 1998c2ecf20Sopenharmony_ci list_add_tail(&buf->queue, &q->active); 2008c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&dev->slock, flags); 2018c2ecf20Sopenharmony_ci dprintk(2, "[%p/%d] vbi_queue - first active\n", 2028c2ecf20Sopenharmony_ci buf, buf->vb.vb2_buf.index); 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci } else { 2058c2ecf20Sopenharmony_ci buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1); 2068c2ecf20Sopenharmony_ci prev = list_entry(q->active.prev, struct cx23885_buffer, 2078c2ecf20Sopenharmony_ci queue); 2088c2ecf20Sopenharmony_ci spin_lock_irqsave(&dev->slock, flags); 2098c2ecf20Sopenharmony_ci list_add_tail(&buf->queue, &q->active); 2108c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&dev->slock, flags); 2118c2ecf20Sopenharmony_ci prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma); 2128c2ecf20Sopenharmony_ci dprintk(2, "[%p/%d] buffer_queue - append to active\n", 2138c2ecf20Sopenharmony_ci buf, buf->vb.vb2_buf.index); 2148c2ecf20Sopenharmony_ci } 2158c2ecf20Sopenharmony_ci} 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_cistatic int cx23885_start_streaming(struct vb2_queue *q, unsigned int count) 2188c2ecf20Sopenharmony_ci{ 2198c2ecf20Sopenharmony_ci struct cx23885_dev *dev = q->drv_priv; 2208c2ecf20Sopenharmony_ci struct cx23885_dmaqueue *dmaq = &dev->vbiq; 2218c2ecf20Sopenharmony_ci struct cx23885_buffer *buf = list_entry(dmaq->active.next, 2228c2ecf20Sopenharmony_ci struct cx23885_buffer, queue); 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci cx23885_start_vbi_dma(dev, dmaq, buf); 2258c2ecf20Sopenharmony_ci return 0; 2268c2ecf20Sopenharmony_ci} 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_cistatic void cx23885_stop_streaming(struct vb2_queue *q) 2298c2ecf20Sopenharmony_ci{ 2308c2ecf20Sopenharmony_ci struct cx23885_dev *dev = q->drv_priv; 2318c2ecf20Sopenharmony_ci struct cx23885_dmaqueue *dmaq = &dev->vbiq; 2328c2ecf20Sopenharmony_ci unsigned long flags; 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci cx_clear(VID_A_DMA_CTL, 0x22); /* FIFO and RISC enable */ 2358c2ecf20Sopenharmony_ci spin_lock_irqsave(&dev->slock, flags); 2368c2ecf20Sopenharmony_ci while (!list_empty(&dmaq->active)) { 2378c2ecf20Sopenharmony_ci struct cx23885_buffer *buf = list_entry(dmaq->active.next, 2388c2ecf20Sopenharmony_ci struct cx23885_buffer, queue); 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci list_del(&buf->queue); 2418c2ecf20Sopenharmony_ci vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 2428c2ecf20Sopenharmony_ci } 2438c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&dev->slock, flags); 2448c2ecf20Sopenharmony_ci} 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ciconst struct vb2_ops cx23885_vbi_qops = { 2488c2ecf20Sopenharmony_ci .queue_setup = queue_setup, 2498c2ecf20Sopenharmony_ci .buf_prepare = buffer_prepare, 2508c2ecf20Sopenharmony_ci .buf_finish = buffer_finish, 2518c2ecf20Sopenharmony_ci .buf_queue = buffer_queue, 2528c2ecf20Sopenharmony_ci .wait_prepare = vb2_ops_wait_prepare, 2538c2ecf20Sopenharmony_ci .wait_finish = vb2_ops_wait_finish, 2548c2ecf20Sopenharmony_ci .start_streaming = cx23885_start_streaming, 2558c2ecf20Sopenharmony_ci .stop_streaming = cx23885_stop_streaming, 2568c2ecf20Sopenharmony_ci}; 257