saa7134-vbi.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. *
  3. * device driver for philips saa7134 based TV cards
  4. * video4linux video interface
  5. *
  6. * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include "saa7134.h"
  23. #include "saa7134-reg.h"
  24. #include <linux/init.h>
  25. #include <linux/list.h>
  26. #include <linux/module.h>
  27. #include <linux/kernel.h>
  28. /* ------------------------------------------------------------------ */
  29. static unsigned int vbi_debug;
  30. module_param(vbi_debug, int, 0644);
  31. MODULE_PARM_DESC(vbi_debug,"enable debug messages [vbi]");
  32. static unsigned int vbibufs = 4;
  33. module_param(vbibufs, int, 0444);
  34. MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32");
  35. #define vbi_dbg(fmt, arg...) do { \
  36. if (vbi_debug) \
  37. printk(KERN_DEBUG pr_fmt("vbi: " fmt), ## arg); \
  38. } while (0)
  39. /* ------------------------------------------------------------------ */
  40. #define VBI_LINE_COUNT 17
  41. #define VBI_LINE_LENGTH 2048
  42. #define VBI_SCALE 0x200
  43. static void task_init(struct saa7134_dev *dev, struct saa7134_buf *buf,
  44. int task)
  45. {
  46. struct saa7134_tvnorm *norm = dev->tvnorm;
  47. /* setup video scaler */
  48. saa_writeb(SAA7134_VBI_H_START1(task), norm->h_start & 0xff);
  49. saa_writeb(SAA7134_VBI_H_START2(task), norm->h_start >> 8);
  50. saa_writeb(SAA7134_VBI_H_STOP1(task), norm->h_stop & 0xff);
  51. saa_writeb(SAA7134_VBI_H_STOP2(task), norm->h_stop >> 8);
  52. saa_writeb(SAA7134_VBI_V_START1(task), norm->vbi_v_start_0 & 0xff);
  53. saa_writeb(SAA7134_VBI_V_START2(task), norm->vbi_v_start_0 >> 8);
  54. saa_writeb(SAA7134_VBI_V_STOP1(task), norm->vbi_v_stop_0 & 0xff);
  55. saa_writeb(SAA7134_VBI_V_STOP2(task), norm->vbi_v_stop_0 >> 8);
  56. saa_writeb(SAA7134_VBI_H_SCALE_INC1(task), VBI_SCALE & 0xff);
  57. saa_writeb(SAA7134_VBI_H_SCALE_INC2(task), VBI_SCALE >> 8);
  58. saa_writeb(SAA7134_VBI_PHASE_OFFSET_LUMA(task), 0x00);
  59. saa_writeb(SAA7134_VBI_PHASE_OFFSET_CHROMA(task), 0x00);
  60. saa_writeb(SAA7134_VBI_H_LEN1(task), dev->vbi_hlen & 0xff);
  61. saa_writeb(SAA7134_VBI_H_LEN2(task), dev->vbi_hlen >> 8);
  62. saa_writeb(SAA7134_VBI_V_LEN1(task), dev->vbi_vlen & 0xff);
  63. saa_writeb(SAA7134_VBI_V_LEN2(task), dev->vbi_vlen >> 8);
  64. saa_andorb(SAA7134_DATA_PATH(task), 0xc0, 0x00);
  65. }
  66. /* ------------------------------------------------------------------ */
  67. static int buffer_activate(struct saa7134_dev *dev,
  68. struct saa7134_buf *buf,
  69. struct saa7134_buf *next)
  70. {
  71. struct saa7134_dmaqueue *dmaq = buf->vb2.vb2_buf.vb2_queue->drv_priv;
  72. unsigned long control, base;
  73. vbi_dbg("buffer_activate [%p]\n", buf);
  74. buf->top_seen = 0;
  75. task_init(dev, buf, TASK_A);
  76. task_init(dev, buf, TASK_B);
  77. saa_writeb(SAA7134_OFMT_DATA_A, 0x06);
  78. saa_writeb(SAA7134_OFMT_DATA_B, 0x06);
  79. /* DMA: setup channel 2+3 (= VBI Task A+B) */
  80. base = saa7134_buffer_base(buf);
  81. control = SAA7134_RS_CONTROL_BURST_16 |
  82. SAA7134_RS_CONTROL_ME |
  83. (dmaq->pt.dma >> 12);
  84. saa_writel(SAA7134_RS_BA1(2), base);
  85. saa_writel(SAA7134_RS_BA2(2), base + dev->vbi_hlen * dev->vbi_vlen);
  86. saa_writel(SAA7134_RS_PITCH(2), dev->vbi_hlen);
  87. saa_writel(SAA7134_RS_CONTROL(2), control);
  88. saa_writel(SAA7134_RS_BA1(3), base);
  89. saa_writel(SAA7134_RS_BA2(3), base + dev->vbi_hlen * dev->vbi_vlen);
  90. saa_writel(SAA7134_RS_PITCH(3), dev->vbi_hlen);
  91. saa_writel(SAA7134_RS_CONTROL(3), control);
  92. /* start DMA */
  93. saa7134_set_dmabits(dev);
  94. mod_timer(&dmaq->timeout, jiffies + BUFFER_TIMEOUT);
  95. return 0;
  96. }
  97. static int buffer_prepare(struct vb2_buffer *vb2)
  98. {
  99. struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
  100. struct saa7134_dev *dev = dmaq->dev;
  101. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
  102. struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
  103. struct sg_table *dma = vb2_dma_sg_plane_desc(vb2, 0);
  104. unsigned int size;
  105. if (dma->sgl->offset) {
  106. pr_err("The buffer is not page-aligned\n");
  107. return -EINVAL;
  108. }
  109. size = dev->vbi_hlen * dev->vbi_vlen * 2;
  110. if (vb2_plane_size(vb2, 0) < size)
  111. return -EINVAL;
  112. vb2_set_plane_payload(vb2, 0, size);
  113. return saa7134_pgtable_build(dev->pci, &dmaq->pt, dma->sgl, dma->nents,
  114. saa7134_buffer_startpage(buf));
  115. }
  116. static int queue_setup(struct vb2_queue *q, const void *parg,
  117. unsigned int *nbuffers, unsigned int *nplanes,
  118. unsigned int sizes[], void *alloc_ctxs[])
  119. {
  120. struct saa7134_dmaqueue *dmaq = q->drv_priv;
  121. struct saa7134_dev *dev = dmaq->dev;
  122. unsigned int size;
  123. dev->vbi_vlen = dev->tvnorm->vbi_v_stop_0 - dev->tvnorm->vbi_v_start_0 + 1;
  124. if (dev->vbi_vlen > VBI_LINE_COUNT)
  125. dev->vbi_vlen = VBI_LINE_COUNT;
  126. dev->vbi_hlen = VBI_LINE_LENGTH;
  127. size = dev->vbi_hlen * dev->vbi_vlen * 2;
  128. *nbuffers = saa7134_buffer_count(size, *nbuffers);
  129. *nplanes = 1;
  130. sizes[0] = size;
  131. alloc_ctxs[0] = dev->alloc_ctx;
  132. return 0;
  133. }
  134. static int buffer_init(struct vb2_buffer *vb2)
  135. {
  136. struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
  137. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
  138. struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
  139. dmaq->curr = NULL;
  140. buf->activate = buffer_activate;
  141. return 0;
  142. }
  143. struct vb2_ops saa7134_vbi_qops = {
  144. .queue_setup = queue_setup,
  145. .buf_init = buffer_init,
  146. .buf_prepare = buffer_prepare,
  147. .buf_queue = saa7134_vb2_buffer_queue,
  148. .wait_prepare = vb2_ops_wait_prepare,
  149. .wait_finish = vb2_ops_wait_finish,
  150. .start_streaming = saa7134_vb2_start_streaming,
  151. .stop_streaming = saa7134_vb2_stop_streaming,
  152. };
  153. /* ------------------------------------------------------------------ */
  154. int saa7134_vbi_init1(struct saa7134_dev *dev)
  155. {
  156. INIT_LIST_HEAD(&dev->vbi_q.queue);
  157. init_timer(&dev->vbi_q.timeout);
  158. dev->vbi_q.timeout.function = saa7134_buffer_timeout;
  159. dev->vbi_q.timeout.data = (unsigned long)(&dev->vbi_q);
  160. dev->vbi_q.dev = dev;
  161. if (vbibufs < 2)
  162. vbibufs = 2;
  163. if (vbibufs > VIDEO_MAX_FRAME)
  164. vbibufs = VIDEO_MAX_FRAME;
  165. return 0;
  166. }
  167. int saa7134_vbi_fini(struct saa7134_dev *dev)
  168. {
  169. /* nothing */
  170. return 0;
  171. }
  172. void saa7134_irq_vbi_done(struct saa7134_dev *dev, unsigned long status)
  173. {
  174. spin_lock(&dev->slock);
  175. if (dev->vbi_q.curr) {
  176. /* make sure we have seen both fields */
  177. if ((status & 0x10) == 0x00) {
  178. dev->vbi_q.curr->top_seen = 1;
  179. goto done;
  180. }
  181. if (!dev->vbi_q.curr->top_seen)
  182. goto done;
  183. saa7134_buffer_finish(dev, &dev->vbi_q, VB2_BUF_STATE_DONE);
  184. }
  185. saa7134_buffer_next(dev, &dev->vbi_q);
  186. done:
  187. spin_unlock(&dev->slock);
  188. }