blob: 3f0b0933eed69e9bbffbcc5f822d95b775191965 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * device driver for philips saa7134 based TV cards
5 * video4linux video interface
6 *
7 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Mauro Carvalho Chehab9a12ccf2015-04-30 06:44:56 -030010#include "saa7134.h"
11#include "saa7134-reg.h"
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/init.h>
14#include <linux/list.h>
15#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018/* ------------------------------------------------------------------ */
19
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030020static unsigned int vbi_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021module_param(vbi_debug, int, 0644);
22MODULE_PARM_DESC(vbi_debug,"enable debug messages [vbi]");
23
24static unsigned int vbibufs = 4;
25module_param(vbibufs, int, 0444);
26MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32");
27
Mauro Carvalho Chehab45f38cb2015-05-13 14:09:25 -030028#define vbi_dbg(fmt, arg...) do { \
29 if (vbi_debug) \
30 printk(KERN_DEBUG pr_fmt("vbi: " fmt), ## arg); \
31 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/* ------------------------------------------------------------------ */
34
Hans Verkuil033d0082014-09-21 06:38:55 -030035#define VBI_LINE_COUNT 17
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define VBI_LINE_LENGTH 2048
37#define VBI_SCALE 0x200
38
39static void task_init(struct saa7134_dev *dev, struct saa7134_buf *buf,
40 int task)
41{
42 struct saa7134_tvnorm *norm = dev->tvnorm;
43
44 /* setup video scaler */
45 saa_writeb(SAA7134_VBI_H_START1(task), norm->h_start & 0xff);
46 saa_writeb(SAA7134_VBI_H_START2(task), norm->h_start >> 8);
47 saa_writeb(SAA7134_VBI_H_STOP1(task), norm->h_stop & 0xff);
48 saa_writeb(SAA7134_VBI_H_STOP2(task), norm->h_stop >> 8);
Michael Schimekf246a812005-06-23 22:04:47 -070049 saa_writeb(SAA7134_VBI_V_START1(task), norm->vbi_v_start_0 & 0xff);
50 saa_writeb(SAA7134_VBI_V_START2(task), norm->vbi_v_start_0 >> 8);
51 saa_writeb(SAA7134_VBI_V_STOP1(task), norm->vbi_v_stop_0 & 0xff);
52 saa_writeb(SAA7134_VBI_V_STOP2(task), norm->vbi_v_stop_0 >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 saa_writeb(SAA7134_VBI_H_SCALE_INC1(task), VBI_SCALE & 0xff);
55 saa_writeb(SAA7134_VBI_H_SCALE_INC2(task), VBI_SCALE >> 8);
56 saa_writeb(SAA7134_VBI_PHASE_OFFSET_LUMA(task), 0x00);
57 saa_writeb(SAA7134_VBI_PHASE_OFFSET_CHROMA(task), 0x00);
58
Hans Verkuil01c3a842014-04-17 04:55:43 -030059 saa_writeb(SAA7134_VBI_H_LEN1(task), dev->vbi_hlen & 0xff);
60 saa_writeb(SAA7134_VBI_H_LEN2(task), dev->vbi_hlen >> 8);
61 saa_writeb(SAA7134_VBI_V_LEN1(task), dev->vbi_vlen & 0xff);
62 saa_writeb(SAA7134_VBI_V_LEN2(task), dev->vbi_vlen >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 saa_andorb(SAA7134_DATA_PATH(task), 0xc0, 0x00);
65}
66
67/* ------------------------------------------------------------------ */
68
69static int buffer_activate(struct saa7134_dev *dev,
70 struct saa7134_buf *buf,
71 struct saa7134_buf *next)
72{
Junghak Sung2d700712015-09-22 10:30:30 -030073 struct saa7134_dmaqueue *dmaq = buf->vb2.vb2_buf.vb2_queue->drv_priv;
Hans Verkuile72936d2014-04-17 03:48:50 -030074 unsigned long control, base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Mauro Carvalho Chehab630983b2015-04-30 08:52:35 -030076 vbi_dbg("buffer_activate [%p]\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 buf->top_seen = 0;
78
Hans Verkuile72936d2014-04-17 03:48:50 -030079 task_init(dev, buf, TASK_A);
80 task_init(dev, buf, TASK_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 saa_writeb(SAA7134_OFMT_DATA_A, 0x06);
82 saa_writeb(SAA7134_OFMT_DATA_B, 0x06);
83
84 /* DMA: setup channel 2+3 (= VBI Task A+B) */
85 base = saa7134_buffer_base(buf);
86 control = SAA7134_RS_CONTROL_BURST_16 |
87 SAA7134_RS_CONTROL_ME |
Hans Verkuil2ada8152014-04-17 07:30:53 -030088 (dmaq->pt.dma >> 12);
Hans Verkuile72936d2014-04-17 03:48:50 -030089 saa_writel(SAA7134_RS_BA1(2), base);
Hans Verkuil01c3a842014-04-17 04:55:43 -030090 saa_writel(SAA7134_RS_BA2(2), base + dev->vbi_hlen * dev->vbi_vlen);
91 saa_writel(SAA7134_RS_PITCH(2), dev->vbi_hlen);
Hans Verkuile72936d2014-04-17 03:48:50 -030092 saa_writel(SAA7134_RS_CONTROL(2), control);
93 saa_writel(SAA7134_RS_BA1(3), base);
Hans Verkuil01c3a842014-04-17 04:55:43 -030094 saa_writel(SAA7134_RS_BA2(3), base + dev->vbi_hlen * dev->vbi_vlen);
95 saa_writel(SAA7134_RS_PITCH(3), dev->vbi_hlen);
Hans Verkuile72936d2014-04-17 03:48:50 -030096 saa_writel(SAA7134_RS_CONTROL(3), control);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 /* start DMA */
99 saa7134_set_dmabits(dev);
Hans Verkuil2ada8152014-04-17 07:30:53 -0300100 mod_timer(&dmaq->timeout, jiffies + BUFFER_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 return 0;
103}
104
Hans Verkuil2ada8152014-04-17 07:30:53 -0300105static int buffer_prepare(struct vb2_buffer *vb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Hans Verkuil2ada8152014-04-17 07:30:53 -0300107 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
Hans Verkuila00e6882014-04-17 06:06:06 -0300108 struct saa7134_dev *dev = dmaq->dev;
Junghak Sung2d700712015-09-22 10:30:30 -0300109 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
110 struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
111 struct sg_table *dma = vb2_dma_sg_plane_desc(vb2, 0);
Hans Verkuil01c3a842014-04-17 04:55:43 -0300112 unsigned int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Hans Verkuila3f415a2014-04-17 06:44:31 -0300114 if (dma->sgl->offset) {
115 pr_err("The buffer is not page-aligned\n");
116 return -EINVAL;
117 }
Hans Verkuil01c3a842014-04-17 04:55:43 -0300118 size = dev->vbi_hlen * dev->vbi_vlen * 2;
Hans Verkuil2ada8152014-04-17 07:30:53 -0300119 if (vb2_plane_size(vb2, 0) < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return -EINVAL;
121
Hans Verkuil2ada8152014-04-17 07:30:53 -0300122 vb2_set_plane_payload(vb2, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Hans Verkuil2ada8152014-04-17 07:30:53 -0300124 return saa7134_pgtable_build(dev->pci, &dmaq->pt, dma->sgl, dma->nents,
125 saa7134_buffer_startpage(buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200128static int queue_setup(struct vb2_queue *q,
Hans Verkuil2ada8152014-04-17 07:30:53 -0300129 unsigned int *nbuffers, unsigned int *nplanes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300130 unsigned int sizes[], struct device *alloc_devs[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Hans Verkuil2ada8152014-04-17 07:30:53 -0300132 struct saa7134_dmaqueue *dmaq = q->drv_priv;
Hans Verkuila00e6882014-04-17 06:06:06 -0300133 struct saa7134_dev *dev = dmaq->dev;
Hans Verkuil2ada8152014-04-17 07:30:53 -0300134 unsigned int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Hans Verkuil01c3a842014-04-17 04:55:43 -0300136 dev->vbi_vlen = dev->tvnorm->vbi_v_stop_0 - dev->tvnorm->vbi_v_start_0 + 1;
137 if (dev->vbi_vlen > VBI_LINE_COUNT)
138 dev->vbi_vlen = VBI_LINE_COUNT;
139 dev->vbi_hlen = VBI_LINE_LENGTH;
Hans Verkuil2ada8152014-04-17 07:30:53 -0300140 size = dev->vbi_hlen * dev->vbi_vlen * 2;
141
142 *nbuffers = saa7134_buffer_count(size, *nbuffers);
143 *nplanes = 1;
144 sizes[0] = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return 0;
146}
147
Hans Verkuil2ada8152014-04-17 07:30:53 -0300148static int buffer_init(struct vb2_buffer *vb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Hans Verkuil2ada8152014-04-17 07:30:53 -0300150 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
Junghak Sung2d700712015-09-22 10:30:30 -0300151 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
152 struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
Hans Verkuil2ada8152014-04-17 07:30:53 -0300153
154 dmaq->curr = NULL;
155 buf->activate = buffer_activate;
156 return 0;
157}
158
Bhumika Goyal0e2d9a92017-09-27 18:59:06 +0200159const struct vb2_ops saa7134_vbi_qops = {
Hans Verkuil2ada8152014-04-17 07:30:53 -0300160 .queue_setup = queue_setup,
161 .buf_init = buffer_init,
162 .buf_prepare = buffer_prepare,
Hans Verkuil2ada8152014-04-17 07:30:53 -0300163 .buf_queue = saa7134_vb2_buffer_queue,
164 .wait_prepare = vb2_ops_wait_prepare,
165 .wait_finish = vb2_ops_wait_finish,
166 .start_streaming = saa7134_vb2_start_streaming,
167 .stop_streaming = saa7134_vb2_stop_streaming,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168};
169
170/* ------------------------------------------------------------------ */
171
172int saa7134_vbi_init1(struct saa7134_dev *dev)
173{
174 INIT_LIST_HEAD(&dev->vbi_q.queue);
Kees Cook1e7126b2017-10-16 19:11:30 -0400175 timer_setup(&dev->vbi_q.timeout, saa7134_buffer_timeout, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 dev->vbi_q.dev = dev;
177
178 if (vbibufs < 2)
179 vbibufs = 2;
180 if (vbibufs > VIDEO_MAX_FRAME)
181 vbibufs = VIDEO_MAX_FRAME;
182 return 0;
183}
184
185int saa7134_vbi_fini(struct saa7134_dev *dev)
186{
187 /* nothing */
188 return 0;
189}
190
191void saa7134_irq_vbi_done(struct saa7134_dev *dev, unsigned long status)
192{
193 spin_lock(&dev->slock);
194 if (dev->vbi_q.curr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /* make sure we have seen both fields */
196 if ((status & 0x10) == 0x00) {
197 dev->vbi_q.curr->top_seen = 1;
198 goto done;
199 }
200 if (!dev->vbi_q.curr->top_seen)
201 goto done;
202
Hans Verkuil2ada8152014-04-17 07:30:53 -0300203 saa7134_buffer_finish(dev, &dev->vbi_q, VB2_BUF_STATE_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
Hans Verkuile72936d2014-04-17 03:48:50 -0300205 saa7134_buffer_next(dev, &dev->vbi_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 done:
208 spin_unlock(&dev->slock);
209}