Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / media / common / saa7146 / saa7146_vbi.c
bloba1854b3dd004b90c3f449df085c28b336bc3abbb
1 // SPDX-License-Identifier: GPL-2.0
2 #include <media/drv-intf/saa7146_vv.h>
4 static int vbi_pixel_to_capture = 720 * 2;
6 static int vbi_workaround(struct saa7146_dev *dev)
8 struct saa7146_vv *vv = dev->vv_data;
10 u32 *cpu;
11 dma_addr_t dma_addr;
13 int count = 0;
14 int i;
16 DECLARE_WAITQUEUE(wait, current);
18 DEB_VBI("dev:%p\n", dev);
20 /* once again, a bug in the saa7146: the brs acquisition
21 is buggy and especially the BXO-counter does not work
22 as specified. there is this workaround, but please
23 don't let me explain it. ;-) */
25 cpu = dma_alloc_coherent(&dev->pci->dev, 4096, &dma_addr, GFP_KERNEL);
26 if (NULL == cpu)
27 return -ENOMEM;
29 /* setup some basic programming, just for the workaround */
30 saa7146_write(dev, BASE_EVEN3, dma_addr);
31 saa7146_write(dev, BASE_ODD3, dma_addr+vbi_pixel_to_capture);
32 saa7146_write(dev, PROT_ADDR3, dma_addr+4096);
33 saa7146_write(dev, PITCH3, vbi_pixel_to_capture);
34 saa7146_write(dev, BASE_PAGE3, 0x0);
35 saa7146_write(dev, NUM_LINE_BYTE3, (2<<16)|((vbi_pixel_to_capture)<<0));
36 saa7146_write(dev, MC2, MASK_04|MASK_20);
38 /* load brs-control register */
39 WRITE_RPS1(CMD_WR_REG | (1 << 8) | (BRS_CTRL/4));
40 /* BXO = 1h, BRS to outbound */
41 WRITE_RPS1(0xc000008c);
42 /* wait for vbi_a or vbi_b*/
43 if ( 0 != (SAA7146_USE_PORT_B_FOR_VBI & dev->ext_vv_data->flags)) {
44 DEB_D("...using port b\n");
45 WRITE_RPS1(CMD_PAUSE | CMD_OAN | CMD_SIG1 | CMD_E_FID_B);
46 WRITE_RPS1(CMD_PAUSE | CMD_OAN | CMD_SIG1 | CMD_O_FID_B);
48 WRITE_RPS1(CMD_PAUSE | MASK_09);
50 } else {
51 DEB_D("...using port a\n");
52 WRITE_RPS1(CMD_PAUSE | MASK_10);
54 /* upload brs */
55 WRITE_RPS1(CMD_UPLOAD | MASK_08);
56 /* load brs-control register */
57 WRITE_RPS1(CMD_WR_REG | (1 << 8) | (BRS_CTRL/4));
58 /* BYO = 1, BXO = NQBIL (=1728 for PAL, for NTSC this is 858*2) - NumByte3 (=1440) = 288 */
59 WRITE_RPS1(((1728-(vbi_pixel_to_capture)) << 7) | MASK_19);
60 /* wait for brs_done */
61 WRITE_RPS1(CMD_PAUSE | MASK_08);
62 /* upload brs */
63 WRITE_RPS1(CMD_UPLOAD | MASK_08);
64 /* load video-dma3 NumLines3 and NumBytes3 */
65 WRITE_RPS1(CMD_WR_REG | (1 << 8) | (NUM_LINE_BYTE3/4));
66 /* dev->vbi_count*2 lines, 720 pixel (= 1440 Bytes) */
67 WRITE_RPS1((2 << 16) | (vbi_pixel_to_capture));
68 /* load brs-control register */
69 WRITE_RPS1(CMD_WR_REG | (1 << 8) | (BRS_CTRL/4));
70 /* Set BRS right: note: this is an experimental value for BXO (=> PAL!) */
71 WRITE_RPS1((540 << 7) | (5 << 19)); // 5 == vbi_start
72 /* wait for brs_done */
73 WRITE_RPS1(CMD_PAUSE | MASK_08);
74 /* upload brs and video-dma3*/
75 WRITE_RPS1(CMD_UPLOAD | MASK_08 | MASK_04);
76 /* load mc2 register: enable dma3 */
77 WRITE_RPS1(CMD_WR_REG | (1 << 8) | (MC1/4));
78 WRITE_RPS1(MASK_20 | MASK_04);
79 /* generate interrupt */
80 WRITE_RPS1(CMD_INTERRUPT);
81 /* stop rps1 */
82 WRITE_RPS1(CMD_STOP);
84 /* we have to do the workaround twice to be sure that
85 everything is ok */
86 for(i = 0; i < 2; i++) {
88 /* indicate to the irq handler that we do the workaround */
89 saa7146_write(dev, MC2, MASK_31|MASK_15);
91 saa7146_write(dev, NUM_LINE_BYTE3, (1<<16)|(2<<0));
92 saa7146_write(dev, MC2, MASK_04|MASK_20);
94 /* enable rps1 irqs */
95 SAA7146_IER_ENABLE(dev,MASK_28);
97 /* prepare to wait to be woken up by the irq-handler */
98 add_wait_queue(&vv->vbi_wq, &wait);
99 set_current_state(TASK_INTERRUPTIBLE);
101 /* start rps1 to enable workaround */
102 saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
103 saa7146_write(dev, MC1, (MASK_13 | MASK_29));
105 schedule();
107 DEB_VBI("brs bug workaround %d/1\n", i);
109 remove_wait_queue(&vv->vbi_wq, &wait);
110 __set_current_state(TASK_RUNNING);
112 /* disable rps1 irqs */
113 SAA7146_IER_DISABLE(dev,MASK_28);
115 /* stop video-dma3 */
116 saa7146_write(dev, MC1, MASK_20);
118 if(signal_pending(current)) {
120 DEB_VBI("aborted (rps:0x%08x)\n",
121 saa7146_read(dev, RPS_ADDR1));
123 /* stop rps1 for sure */
124 saa7146_write(dev, MC1, MASK_29);
126 dma_free_coherent(&dev->pci->dev, 4096, cpu, dma_addr);
127 return -EINTR;
131 dma_free_coherent(&dev->pci->dev, 4096, cpu, dma_addr);
132 return 0;
135 static void saa7146_set_vbi_capture(struct saa7146_dev *dev, struct saa7146_buf *buf, struct saa7146_buf *next)
137 struct saa7146_vv *vv = dev->vv_data;
139 struct saa7146_video_dma vdma3;
141 int count = 0;
142 unsigned long e_wait = vv->current_hps_sync == SAA7146_HPS_SYNC_PORT_A ? CMD_E_FID_A : CMD_E_FID_B;
143 unsigned long o_wait = vv->current_hps_sync == SAA7146_HPS_SYNC_PORT_A ? CMD_O_FID_A : CMD_O_FID_B;
146 vdma3.base_even = 0xc8000000+2560*70;
147 vdma3.base_odd = 0xc8000000;
148 vdma3.prot_addr = 0xc8000000+2560*164;
149 vdma3.pitch = 2560;
150 vdma3.base_page = 0;
151 vdma3.num_line_byte = (64<<16)|((vbi_pixel_to_capture)<<0); // set above!
153 vdma3.base_even = buf->pt[2].offset;
154 vdma3.base_odd = buf->pt[2].offset + 16 * vbi_pixel_to_capture;
155 vdma3.prot_addr = buf->pt[2].offset + 16 * 2 * vbi_pixel_to_capture;
156 vdma3.pitch = vbi_pixel_to_capture;
157 vdma3.base_page = buf->pt[2].dma | ME1;
158 vdma3.num_line_byte = (16 << 16) | vbi_pixel_to_capture;
160 saa7146_write_out_dma(dev, 3, &vdma3);
162 /* write beginning of rps-program */
163 count = 0;
165 /* wait for o_fid_a/b / e_fid_a/b toggle only if bit 1 is not set */
167 /* we don't wait here for the first field anymore. this is different from the video
168 capture and might cause that the first buffer is only half filled (with only
169 one field). but since this is some sort of streaming data, this is not that negative.
170 but by doing this, we can use the whole engine from videobuf-dma-sg.c... */
173 WRITE_RPS1(CMD_PAUSE | CMD_OAN | CMD_SIG1 | e_wait);
174 WRITE_RPS1(CMD_PAUSE | CMD_OAN | CMD_SIG1 | o_wait);
176 /* set bit 1 */
177 WRITE_RPS1(CMD_WR_REG | (1 << 8) | (MC2/4));
178 WRITE_RPS1(MASK_28 | MASK_12);
180 /* turn on video-dma3 */
181 WRITE_RPS1(CMD_WR_REG_MASK | (MC1/4));
182 WRITE_RPS1(MASK_04 | MASK_20); /* => mask */
183 WRITE_RPS1(MASK_04 | MASK_20); /* => values */
185 /* wait for o_fid_a/b / e_fid_a/b toggle */
186 WRITE_RPS1(CMD_PAUSE | o_wait);
187 WRITE_RPS1(CMD_PAUSE | e_wait);
189 /* generate interrupt */
190 WRITE_RPS1(CMD_INTERRUPT);
192 /* stop */
193 WRITE_RPS1(CMD_STOP);
195 /* enable rps1 irqs */
196 SAA7146_IER_ENABLE(dev, MASK_28);
198 /* write the address of the rps-program */
199 saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
201 /* turn on rps */
202 saa7146_write(dev, MC1, (MASK_13 | MASK_29));
205 static int buffer_activate(struct saa7146_dev *dev,
206 struct saa7146_buf *buf,
207 struct saa7146_buf *next)
209 struct saa7146_vv *vv = dev->vv_data;
211 DEB_VBI("dev:%p, buf:%p, next:%p\n", dev, buf, next);
212 saa7146_set_vbi_capture(dev,buf,next);
214 mod_timer(&vv->vbi_dmaq.timeout, jiffies+BUFFER_TIMEOUT);
215 return 0;
218 /* ------------------------------------------------------------------ */
220 static int queue_setup(struct vb2_queue *q,
221 unsigned int *num_buffers, unsigned int *num_planes,
222 unsigned int sizes[], struct device *alloc_devs[])
224 unsigned int size = 16 * 2 * vbi_pixel_to_capture;
226 if (*num_planes)
227 return sizes[0] < size ? -EINVAL : 0;
228 *num_planes = 1;
229 sizes[0] = size;
231 return 0;
234 static void buf_queue(struct vb2_buffer *vb)
236 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
237 struct vb2_queue *vq = vb->vb2_queue;
238 struct saa7146_dev *dev = vb2_get_drv_priv(vq);
239 struct saa7146_buf *buf = container_of(vbuf, struct saa7146_buf, vb);
240 unsigned long flags;
242 spin_lock_irqsave(&dev->slock, flags);
244 saa7146_buffer_queue(dev, &dev->vv_data->vbi_dmaq, buf);
245 spin_unlock_irqrestore(&dev->slock, flags);
248 static int buf_init(struct vb2_buffer *vb)
250 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
251 struct saa7146_buf *buf = container_of(vbuf, struct saa7146_buf, vb);
252 struct sg_table *sgt = vb2_dma_sg_plane_desc(&buf->vb.vb2_buf, 0);
253 struct scatterlist *list = sgt->sgl;
254 int length = sgt->nents;
255 struct vb2_queue *vq = vb->vb2_queue;
256 struct saa7146_dev *dev = vb2_get_drv_priv(vq);
257 int ret;
259 buf->activate = buffer_activate;
261 saa7146_pgtable_alloc(dev->pci, &buf->pt[2]);
263 ret = saa7146_pgtable_build_single(dev->pci, &buf->pt[2],
264 list, length);
265 if (ret)
266 saa7146_pgtable_free(dev->pci, &buf->pt[2]);
267 return ret;
270 static int buf_prepare(struct vb2_buffer *vb)
272 unsigned int size = 16 * 2 * vbi_pixel_to_capture;
274 if (vb2_plane_size(vb, 0) < size)
275 return -EINVAL;
276 vb2_set_plane_payload(vb, 0, size);
277 return 0;
280 static void buf_cleanup(struct vb2_buffer *vb)
282 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
283 struct saa7146_buf *buf = container_of(vbuf, struct saa7146_buf, vb);
284 struct vb2_queue *vq = vb->vb2_queue;
285 struct saa7146_dev *dev = vb2_get_drv_priv(vq);
287 saa7146_pgtable_free(dev->pci, &buf->pt[2]);
290 static void return_buffers(struct vb2_queue *q, int state)
292 struct saa7146_dev *dev = vb2_get_drv_priv(q);
293 struct saa7146_dmaqueue *dq = &dev->vv_data->vbi_dmaq;
294 struct saa7146_buf *buf;
296 if (dq->curr) {
297 buf = dq->curr;
298 dq->curr = NULL;
299 vb2_buffer_done(&buf->vb.vb2_buf, state);
301 while (!list_empty(&dq->queue)) {
302 buf = list_entry(dq->queue.next, struct saa7146_buf, list);
303 list_del(&buf->list);
304 vb2_buffer_done(&buf->vb.vb2_buf, state);
308 static void vbi_stop(struct saa7146_dev *dev)
310 struct saa7146_vv *vv = dev->vv_data;
311 unsigned long flags;
312 DEB_VBI("dev:%p\n", dev);
314 spin_lock_irqsave(&dev->slock,flags);
316 /* disable rps1 */
317 saa7146_write(dev, MC1, MASK_29);
319 /* disable rps1 irqs */
320 SAA7146_IER_DISABLE(dev, MASK_28);
322 /* shut down dma 3 transfers */
323 saa7146_write(dev, MC1, MASK_20);
325 del_timer(&vv->vbi_dmaq.timeout);
326 del_timer(&vv->vbi_read_timeout);
328 spin_unlock_irqrestore(&dev->slock, flags);
331 static void vbi_read_timeout(struct timer_list *t)
333 struct saa7146_vv *vv = from_timer(vv, t, vbi_read_timeout);
334 struct saa7146_dev *dev = vv->vbi_dmaq.dev;
336 DEB_VBI("dev:%p\n", dev);
338 vbi_stop(dev);
341 static int vbi_begin(struct saa7146_dev *dev)
343 struct saa7146_vv *vv = dev->vv_data;
344 u32 arbtr_ctrl = saa7146_read(dev, PCI_BT_V1);
345 int ret = 0;
347 DEB_VBI("dev:%p\n", dev);
349 ret = saa7146_res_get(dev, RESOURCE_DMA3_BRS);
350 if (0 == ret) {
351 DEB_S("cannot get vbi RESOURCE_DMA3_BRS resource\n");
352 return -EBUSY;
355 /* adjust arbitrition control for video dma 3 */
356 arbtr_ctrl &= ~0x1f0000;
357 arbtr_ctrl |= 0x1d0000;
358 saa7146_write(dev, PCI_BT_V1, arbtr_ctrl);
359 saa7146_write(dev, MC2, (MASK_04|MASK_20));
361 vv->vbi_read_timeout.function = vbi_read_timeout;
363 /* initialize the brs */
364 if ( 0 != (SAA7146_USE_PORT_B_FOR_VBI & dev->ext_vv_data->flags)) {
365 saa7146_write(dev, BRS_CTRL, MASK_30|MASK_29 | (7 << 19));
366 } else {
367 saa7146_write(dev, BRS_CTRL, 0x00000001);
369 if (0 != (ret = vbi_workaround(dev))) {
370 DEB_VBI("vbi workaround failed!\n");
371 /* return ret;*/
375 /* upload brs register */
376 saa7146_write(dev, MC2, (MASK_08|MASK_24));
377 return 0;
380 static int start_streaming(struct vb2_queue *q, unsigned int count)
382 struct saa7146_dev *dev = vb2_get_drv_priv(q);
383 int ret;
385 if (!vb2_is_streaming(&dev->vv_data->vbi_dmaq.q))
386 dev->vv_data->seqnr = 0;
387 ret = vbi_begin(dev);
388 if (ret)
389 return_buffers(q, VB2_BUF_STATE_QUEUED);
390 return ret;
393 static void stop_streaming(struct vb2_queue *q)
395 struct saa7146_dev *dev = vb2_get_drv_priv(q);
397 vbi_stop(dev);
398 return_buffers(q, VB2_BUF_STATE_ERROR);
399 saa7146_res_free(dev, RESOURCE_DMA3_BRS);
402 const struct vb2_ops vbi_qops = {
403 .queue_setup = queue_setup,
404 .buf_queue = buf_queue,
405 .buf_init = buf_init,
406 .buf_prepare = buf_prepare,
407 .buf_cleanup = buf_cleanup,
408 .start_streaming = start_streaming,
409 .stop_streaming = stop_streaming,
412 /* ------------------------------------------------------------------ */
414 static void vbi_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
416 DEB_VBI("dev:%p\n", dev);
418 INIT_LIST_HEAD(&vv->vbi_dmaq.queue);
420 timer_setup(&vv->vbi_dmaq.timeout, saa7146_buffer_timeout, 0);
421 vv->vbi_dmaq.dev = dev;
423 init_waitqueue_head(&vv->vbi_wq);
426 static void vbi_irq_done(struct saa7146_dev *dev, unsigned long status)
428 struct saa7146_vv *vv = dev->vv_data;
429 spin_lock(&dev->slock);
431 if (vv->vbi_dmaq.curr) {
432 DEB_VBI("dev:%p, curr:%p\n", dev, vv->vbi_dmaq.curr);
433 saa7146_buffer_finish(dev, &vv->vbi_dmaq, VB2_BUF_STATE_DONE);
434 } else {
435 DEB_VBI("dev:%p\n", dev);
437 saa7146_buffer_next(dev, &vv->vbi_dmaq, 1);
439 spin_unlock(&dev->slock);
442 const struct saa7146_use_ops saa7146_vbi_uops = {
443 .init = vbi_init,
444 .irq_done = vbi_irq_done,