Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Mauro Carvalho Chehab | 9a12ccf | 2015-04-30 06:44:56 -0300 | [diff] [blame] | 10 | #include "saa7134.h" |
| 11 | #include "saa7134-reg.h" |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/init.h> |
| 14 | #include <linux/list.h> |
| 15 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | /* ------------------------------------------------------------------ */ |
| 19 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 20 | static unsigned int vbi_debug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | module_param(vbi_debug, int, 0644); |
| 22 | MODULE_PARM_DESC(vbi_debug,"enable debug messages [vbi]"); |
| 23 | |
| 24 | static unsigned int vbibufs = 4; |
| 25 | module_param(vbibufs, int, 0444); |
| 26 | MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32"); |
| 27 | |
Mauro Carvalho Chehab | 45f38cb | 2015-05-13 14:09:25 -0300 | [diff] [blame] | 28 | #define vbi_dbg(fmt, arg...) do { \ |
| 29 | if (vbi_debug) \ |
| 30 | printk(KERN_DEBUG pr_fmt("vbi: " fmt), ## arg); \ |
| 31 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | /* ------------------------------------------------------------------ */ |
| 34 | |
Hans Verkuil | 033d008 | 2014-09-21 06:38:55 -0300 | [diff] [blame] | 35 | #define VBI_LINE_COUNT 17 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #define VBI_LINE_LENGTH 2048 |
| 37 | #define VBI_SCALE 0x200 |
| 38 | |
| 39 | static 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 Schimek | f246a81 | 2005-06-23 22:04:47 -0700 | [diff] [blame] | 49 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 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 Verkuil | 01c3a84 | 2014-04-17 04:55:43 -0300 | [diff] [blame] | 59 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | saa_andorb(SAA7134_DATA_PATH(task), 0xc0, 0x00); |
| 65 | } |
| 66 | |
| 67 | /* ------------------------------------------------------------------ */ |
| 68 | |
| 69 | static int buffer_activate(struct saa7134_dev *dev, |
| 70 | struct saa7134_buf *buf, |
| 71 | struct saa7134_buf *next) |
| 72 | { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 73 | struct saa7134_dmaqueue *dmaq = buf->vb2.vb2_buf.vb2_queue->drv_priv; |
Hans Verkuil | e72936d | 2014-04-17 03:48:50 -0300 | [diff] [blame] | 74 | unsigned long control, base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Mauro Carvalho Chehab | 630983b | 2015-04-30 08:52:35 -0300 | [diff] [blame] | 76 | vbi_dbg("buffer_activate [%p]\n", buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | buf->top_seen = 0; |
| 78 | |
Hans Verkuil | e72936d | 2014-04-17 03:48:50 -0300 | [diff] [blame] | 79 | task_init(dev, buf, TASK_A); |
| 80 | task_init(dev, buf, TASK_B); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | 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 Verkuil | 2ada815 | 2014-04-17 07:30:53 -0300 | [diff] [blame] | 88 | (dmaq->pt.dma >> 12); |
Hans Verkuil | e72936d | 2014-04-17 03:48:50 -0300 | [diff] [blame] | 89 | saa_writel(SAA7134_RS_BA1(2), base); |
Hans Verkuil | 01c3a84 | 2014-04-17 04:55:43 -0300 | [diff] [blame] | 90 | saa_writel(SAA7134_RS_BA2(2), base + dev->vbi_hlen * dev->vbi_vlen); |
| 91 | saa_writel(SAA7134_RS_PITCH(2), dev->vbi_hlen); |
Hans Verkuil | e72936d | 2014-04-17 03:48:50 -0300 | [diff] [blame] | 92 | saa_writel(SAA7134_RS_CONTROL(2), control); |
| 93 | saa_writel(SAA7134_RS_BA1(3), base); |
Hans Verkuil | 01c3a84 | 2014-04-17 04:55:43 -0300 | [diff] [blame] | 94 | saa_writel(SAA7134_RS_BA2(3), base + dev->vbi_hlen * dev->vbi_vlen); |
| 95 | saa_writel(SAA7134_RS_PITCH(3), dev->vbi_hlen); |
Hans Verkuil | e72936d | 2014-04-17 03:48:50 -0300 | [diff] [blame] | 96 | saa_writel(SAA7134_RS_CONTROL(3), control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | /* start DMA */ |
| 99 | saa7134_set_dmabits(dev); |
Hans Verkuil | 2ada815 | 2014-04-17 07:30:53 -0300 | [diff] [blame] | 100 | mod_timer(&dmaq->timeout, jiffies + BUFFER_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
Hans Verkuil | 2ada815 | 2014-04-17 07:30:53 -0300 | [diff] [blame] | 105 | static int buffer_prepare(struct vb2_buffer *vb2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
Hans Verkuil | 2ada815 | 2014-04-17 07:30:53 -0300 | [diff] [blame] | 107 | struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv; |
Hans Verkuil | a00e688 | 2014-04-17 06:06:06 -0300 | [diff] [blame] | 108 | struct saa7134_dev *dev = dmaq->dev; |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 109 | 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 Verkuil | 01c3a84 | 2014-04-17 04:55:43 -0300 | [diff] [blame] | 112 | unsigned int size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Hans Verkuil | a3f415a | 2014-04-17 06:44:31 -0300 | [diff] [blame] | 114 | if (dma->sgl->offset) { |
| 115 | pr_err("The buffer is not page-aligned\n"); |
| 116 | return -EINVAL; |
| 117 | } |
Hans Verkuil | 01c3a84 | 2014-04-17 04:55:43 -0300 | [diff] [blame] | 118 | size = dev->vbi_hlen * dev->vbi_vlen * 2; |
Hans Verkuil | 2ada815 | 2014-04-17 07:30:53 -0300 | [diff] [blame] | 119 | if (vb2_plane_size(vb2, 0) < size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return -EINVAL; |
| 121 | |
Hans Verkuil | 2ada815 | 2014-04-17 07:30:53 -0300 | [diff] [blame] | 122 | vb2_set_plane_payload(vb2, 0, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
Hans Verkuil | 2ada815 | 2014-04-17 07:30:53 -0300 | [diff] [blame] | 124 | return saa7134_pgtable_build(dev->pci, &dmaq->pt, dma->sgl, dma->nents, |
| 125 | saa7134_buffer_startpage(buf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Hans Verkuil | df9ecb0c | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 128 | static int queue_setup(struct vb2_queue *q, |
Hans Verkuil | 2ada815 | 2014-04-17 07:30:53 -0300 | [diff] [blame] | 129 | unsigned int *nbuffers, unsigned int *nplanes, |
Hans Verkuil | 36c0f8b | 2016-04-15 09:15:05 -0300 | [diff] [blame] | 130 | unsigned int sizes[], struct device *alloc_devs[]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
Hans Verkuil | 2ada815 | 2014-04-17 07:30:53 -0300 | [diff] [blame] | 132 | struct saa7134_dmaqueue *dmaq = q->drv_priv; |
Hans Verkuil | a00e688 | 2014-04-17 06:06:06 -0300 | [diff] [blame] | 133 | struct saa7134_dev *dev = dmaq->dev; |
Hans Verkuil | 2ada815 | 2014-04-17 07:30:53 -0300 | [diff] [blame] | 134 | unsigned int size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Hans Verkuil | 01c3a84 | 2014-04-17 04:55:43 -0300 | [diff] [blame] | 136 | 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 Verkuil | 2ada815 | 2014-04-17 07:30:53 -0300 | [diff] [blame] | 140 | size = dev->vbi_hlen * dev->vbi_vlen * 2; |
| 141 | |
| 142 | *nbuffers = saa7134_buffer_count(size, *nbuffers); |
| 143 | *nplanes = 1; |
| 144 | sizes[0] = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
Hans Verkuil | 2ada815 | 2014-04-17 07:30:53 -0300 | [diff] [blame] | 148 | static int buffer_init(struct vb2_buffer *vb2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Hans Verkuil | 2ada815 | 2014-04-17 07:30:53 -0300 | [diff] [blame] | 150 | struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv; |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 151 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2); |
| 152 | struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2); |
Hans Verkuil | 2ada815 | 2014-04-17 07:30:53 -0300 | [diff] [blame] | 153 | |
| 154 | dmaq->curr = NULL; |
| 155 | buf->activate = buffer_activate; |
| 156 | return 0; |
| 157 | } |
| 158 | |
Bhumika Goyal | 0e2d9a9 | 2017-09-27 18:59:06 +0200 | [diff] [blame] | 159 | const struct vb2_ops saa7134_vbi_qops = { |
Hans Verkuil | 2ada815 | 2014-04-17 07:30:53 -0300 | [diff] [blame] | 160 | .queue_setup = queue_setup, |
| 161 | .buf_init = buffer_init, |
| 162 | .buf_prepare = buffer_prepare, |
Hans Verkuil | 2ada815 | 2014-04-17 07:30:53 -0300 | [diff] [blame] | 163 | .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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | /* ------------------------------------------------------------------ */ |
| 171 | |
| 172 | int saa7134_vbi_init1(struct saa7134_dev *dev) |
| 173 | { |
| 174 | INIT_LIST_HEAD(&dev->vbi_q.queue); |
Kees Cook | 1e7126b | 2017-10-16 19:11:30 -0400 | [diff] [blame] | 175 | timer_setup(&dev->vbi_q.timeout, saa7134_buffer_timeout, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | 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 | |
| 185 | int saa7134_vbi_fini(struct saa7134_dev *dev) |
| 186 | { |
| 187 | /* nothing */ |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | void saa7134_irq_vbi_done(struct saa7134_dev *dev, unsigned long status) |
| 192 | { |
| 193 | spin_lock(&dev->slock); |
| 194 | if (dev->vbi_q.curr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | /* 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 Verkuil | 2ada815 | 2014-04-17 07:30:53 -0300 | [diff] [blame] | 203 | saa7134_buffer_finish(dev, &dev->vbi_q, VB2_BUF_STATE_DONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
Hans Verkuil | e72936d | 2014-04-17 03:48:50 -0300 | [diff] [blame] | 205 | saa7134_buffer_next(dev, &dev->vbi_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
| 207 | done: |
| 208 | spin_unlock(&dev->slock); |
| 209 | } |