1 // SPDX-License-Identifier: GPL-2.0-only
2 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4 #include <media/drv-intf/saa7146_vv.h>
5 #include <linux/module.h>
7 /****************************************************************************/
8 /* resource management functions, shamelessly stolen from saa7134 driver */
10 int saa7146_res_get(struct saa7146_dev
*dev
, unsigned int bit
)
12 struct saa7146_vv
*vv
= dev
->vv_data
;
14 if (vv
->resources
& bit
) {
15 DEB_D("already allocated! want: 0x%02x, cur:0x%02x\n",
17 /* have it already allocated */
22 if (vv
->resources
& bit
) {
23 DEB_D("locked! vv->resources:0x%02x, we want:0x%02x\n",
25 /* no, someone else uses it */
28 /* it's free, grab it */
30 DEB_D("res: get 0x%02x, cur:0x%02x\n", bit
, vv
->resources
);
34 void saa7146_res_free(struct saa7146_dev
*dev
, unsigned int bits
)
36 struct saa7146_vv
*vv
= dev
->vv_data
;
38 WARN_ON((vv
->resources
& bits
) != bits
);
40 vv
->resources
&= ~bits
;
41 DEB_D("res: put 0x%02x, cur:0x%02x\n", bits
, vv
->resources
);
45 /********************************************************************************/
46 /* common buffer functions */
48 int saa7146_buffer_queue(struct saa7146_dev
*dev
,
49 struct saa7146_dmaqueue
*q
,
50 struct saa7146_buf
*buf
)
52 assert_spin_locked(&dev
->slock
);
53 DEB_EE("dev:%p, dmaq:%p, buf:%p\n", dev
, q
, buf
);
58 if (NULL
== q
->curr
) {
60 DEB_D("immediately activating buffer %p\n", buf
);
61 buf
->activate(dev
,buf
,NULL
);
63 list_add_tail(&buf
->list
, &q
->queue
);
64 DEB_D("adding buffer %p to queue. (active buffer present)\n",
70 void saa7146_buffer_finish(struct saa7146_dev
*dev
,
71 struct saa7146_dmaqueue
*q
,
74 struct saa7146_vv
*vv
= dev
->vv_data
;
75 struct saa7146_buf
*buf
= q
->curr
;
77 assert_spin_locked(&dev
->slock
);
78 DEB_EE("dev:%p, dmaq:%p, state:%d\n", dev
, q
, state
);
79 DEB_EE("q->curr:%p\n", q
->curr
);
81 /* finish current buffer */
83 DEB_D("aiii. no current buffer\n");
88 buf
->vb
.vb2_buf
.timestamp
= ktime_get_ns();
89 if (vv
->video_fmt
.field
== V4L2_FIELD_ALTERNATE
)
90 buf
->vb
.field
= vv
->last_field
;
91 else if (vv
->video_fmt
.field
== V4L2_FIELD_ANY
)
92 buf
->vb
.field
= (vv
->video_fmt
.height
> vv
->standard
->v_max_out
/ 2)
93 ? V4L2_FIELD_INTERLACED
96 buf
->vb
.field
= vv
->video_fmt
.field
;
97 buf
->vb
.sequence
= vv
->seqnr
++;
98 vb2_buffer_done(&buf
->vb
.vb2_buf
, state
);
101 void saa7146_buffer_next(struct saa7146_dev
*dev
,
102 struct saa7146_dmaqueue
*q
, int vbi
)
104 struct saa7146_buf
*buf
,*next
= NULL
;
109 DEB_INT("dev:%p, dmaq:%p, vbi:%d\n", dev
, q
, vbi
);
111 assert_spin_locked(&dev
->slock
);
112 if (!list_empty(&q
->queue
)) {
113 /* activate next one from queue */
114 buf
= list_entry(q
->queue
.next
, struct saa7146_buf
, list
);
115 list_del(&buf
->list
);
116 if (!list_empty(&q
->queue
))
117 next
= list_entry(q
->queue
.next
, struct saa7146_buf
, list
);
119 DEB_INT("next buffer: buf:%p, prev:%p, next:%p\n",
120 buf
, q
->queue
.prev
, q
->queue
.next
);
121 buf
->activate(dev
,buf
,next
);
123 DEB_INT("no next buffer. stopping.\n");
125 /* turn off video-dma3 */
126 saa7146_write(dev
,MC1
, MASK_20
);
128 /* nothing to do -- just prevent next video-dma1 transfer
129 by lowering the protection address */
131 // fixme: fix this for vflip != 0
133 saa7146_write(dev
, PROT_ADDR1
, 0);
134 saa7146_write(dev
, MC2
, (MASK_02
|MASK_18
));
136 /* write the address of the rps-program */
137 saa7146_write(dev
, RPS_ADDR0
, dev
->d_rps0
.dma_handle
);
139 saa7146_write(dev
, MC1
, (MASK_12
| MASK_28
));
142 printk("vdma%d.base_even: 0x%08x\n", 1,saa7146_read(dev,BASE_EVEN1));
143 printk("vdma%d.base_odd: 0x%08x\n", 1,saa7146_read(dev,BASE_ODD1));
144 printk("vdma%d.prot_addr: 0x%08x\n", 1,saa7146_read(dev,PROT_ADDR1));
145 printk("vdma%d.base_page: 0x%08x\n", 1,saa7146_read(dev,BASE_PAGE1));
146 printk("vdma%d.pitch: 0x%08x\n", 1,saa7146_read(dev,PITCH1));
147 printk("vdma%d.num_line_byte: 0x%08x\n", 1,saa7146_read(dev,NUM_LINE_BYTE1));
150 del_timer(&q
->timeout
);
154 void saa7146_buffer_timeout(struct timer_list
*t
)
156 struct saa7146_dmaqueue
*q
= from_timer(q
, t
, timeout
);
157 struct saa7146_dev
*dev
= q
->dev
;
160 DEB_EE("dev:%p, dmaq:%p\n", dev
, q
);
162 spin_lock_irqsave(&dev
->slock
,flags
);
164 DEB_D("timeout on %p\n", q
->curr
);
165 saa7146_buffer_finish(dev
, q
, VB2_BUF_STATE_ERROR
);
168 /* we don't restart the transfer here like other drivers do. when
169 a streaming capture is disabled, the timeout function will be
170 called for the current buffer. if we activate the next buffer now,
171 we mess up our capture logic. if a timeout occurs on another buffer,
172 then something is seriously broken before, so no need to buffer the
173 next capture IMHO... */
175 saa7146_buffer_next(dev
, q
, 0);
177 spin_unlock_irqrestore(&dev
->slock
,flags
);
180 /********************************************************************************/
181 /* file operations */
183 static ssize_t
fops_write(struct file
*file
, const char __user
*data
, size_t count
, loff_t
*ppos
)
185 struct video_device
*vdev
= video_devdata(file
);
186 struct saa7146_dev
*dev
= video_drvdata(file
);
189 if (vdev
->vfl_type
!= VFL_TYPE_VBI
|| !dev
->ext_vv_data
->vbi_fops
.write
)
191 if (mutex_lock_interruptible(vdev
->lock
))
193 ret
= dev
->ext_vv_data
->vbi_fops
.write(file
, data
, count
, ppos
);
194 mutex_unlock(vdev
->lock
);
198 static const struct v4l2_file_operations video_fops
=
200 .owner
= THIS_MODULE
,
201 .open
= v4l2_fh_open
,
202 .release
= vb2_fop_release
,
203 .read
= vb2_fop_read
,
205 .poll
= vb2_fop_poll
,
206 .mmap
= vb2_fop_mmap
,
207 .unlocked_ioctl
= video_ioctl2
,
210 static void vv_callback(struct saa7146_dev
*dev
, unsigned long status
)
214 DEB_INT("dev:%p, isr:0x%08x\n", dev
, (u32
)status
);
216 if (0 != (isr
& (MASK_27
))) {
217 DEB_INT("irq: RPS0 (0x%08x)\n", isr
);
218 saa7146_video_uops
.irq_done(dev
,isr
);
221 if (0 != (isr
& (MASK_28
))) {
222 u32 mc2
= saa7146_read(dev
, MC2
);
223 if( 0 != (mc2
& MASK_15
)) {
224 DEB_INT("irq: RPS1 vbi workaround (0x%08x)\n", isr
);
225 wake_up(&dev
->vv_data
->vbi_wq
);
226 saa7146_write(dev
,MC2
, MASK_31
);
229 DEB_INT("irq: RPS1 (0x%08x)\n", isr
);
230 saa7146_vbi_uops
.irq_done(dev
,isr
);
234 static const struct v4l2_ctrl_ops saa7146_ctrl_ops
= {
235 .s_ctrl
= saa7146_s_ctrl
,
238 int saa7146_vv_init(struct saa7146_dev
* dev
, struct saa7146_ext_vv
*ext_vv
)
240 struct v4l2_ctrl_handler
*hdl
= &dev
->ctrl_handler
;
241 struct v4l2_pix_format
*fmt
;
242 struct v4l2_vbi_format
*vbi
;
243 struct saa7146_vv
*vv
;
246 err
= v4l2_device_register(&dev
->pci
->dev
, &dev
->v4l2_dev
);
250 v4l2_ctrl_handler_init(hdl
, 6);
251 v4l2_ctrl_new_std(hdl
, &saa7146_ctrl_ops
,
252 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 128);
253 v4l2_ctrl_new_std(hdl
, &saa7146_ctrl_ops
,
254 V4L2_CID_CONTRAST
, 0, 127, 1, 64);
255 v4l2_ctrl_new_std(hdl
, &saa7146_ctrl_ops
,
256 V4L2_CID_SATURATION
, 0, 127, 1, 64);
257 v4l2_ctrl_new_std(hdl
, &saa7146_ctrl_ops
,
258 V4L2_CID_VFLIP
, 0, 1, 1, 0);
259 v4l2_ctrl_new_std(hdl
, &saa7146_ctrl_ops
,
260 V4L2_CID_HFLIP
, 0, 1, 1, 0);
263 v4l2_ctrl_handler_free(hdl
);
264 v4l2_device_unregister(&dev
->v4l2_dev
);
267 dev
->v4l2_dev
.ctrl_handler
= hdl
;
269 vv
= kzalloc(sizeof(struct saa7146_vv
), GFP_KERNEL
);
271 ERR("out of memory. aborting.\n");
272 v4l2_ctrl_handler_free(hdl
);
273 v4l2_device_unregister(&dev
->v4l2_dev
);
276 ext_vv
->vid_ops
= saa7146_video_ioctl_ops
;
277 ext_vv
->vbi_ops
= saa7146_vbi_ioctl_ops
;
278 ext_vv
->core_ops
= &saa7146_video_ioctl_ops
;
280 DEB_EE("dev:%p\n", dev
);
282 /* set default values for video parts of the saa7146 */
283 saa7146_write(dev
, BCS_CTRL
, 0x80400040);
285 /* enable video-port pins */
286 saa7146_write(dev
, MC1
, (MASK_10
| MASK_26
));
288 /* save per-device extension data (one extension can
289 handle different devices that might need different
290 configuration data) */
291 dev
->ext_vv_data
= ext_vv
;
293 saa7146_video_uops
.init(dev
,vv
);
294 if (dev
->ext_vv_data
->capabilities
& V4L2_CAP_VBI_CAPTURE
)
295 saa7146_vbi_uops
.init(dev
,vv
);
297 fmt
= &vv
->video_fmt
;
300 fmt
->pixelformat
= V4L2_PIX_FMT_BGR24
;
301 fmt
->field
= V4L2_FIELD_INTERLACED
;
302 fmt
->colorspace
= V4L2_COLORSPACE_SMPTE170M
;
303 fmt
->bytesperline
= 3 * fmt
->width
;
304 fmt
->sizeimage
= fmt
->bytesperline
* fmt
->height
;
307 vbi
->sampling_rate
= 27000000;
308 vbi
->offset
= 248; /* todo */
309 vbi
->samples_per_line
= 720 * 2;
310 vbi
->sample_format
= V4L2_PIX_FMT_GREY
;
312 /* fixme: this only works for PAL */
318 timer_setup(&vv
->vbi_read_timeout
, NULL
, 0);
321 dev
->vv_callback
= &vv_callback
;
325 EXPORT_SYMBOL_GPL(saa7146_vv_init
);
327 int saa7146_vv_release(struct saa7146_dev
* dev
)
329 struct saa7146_vv
*vv
= dev
->vv_data
;
331 DEB_EE("dev:%p\n", dev
);
333 v4l2_device_unregister(&dev
->v4l2_dev
);
334 v4l2_ctrl_handler_free(&dev
->ctrl_handler
);
337 dev
->vv_callback
= NULL
;
341 EXPORT_SYMBOL_GPL(saa7146_vv_release
);
343 int saa7146_register_device(struct video_device
*vfd
, struct saa7146_dev
*dev
,
344 char *name
, int type
)
350 DEB_EE("dev:%p, name:'%s', type:%d\n", dev
, name
, type
);
352 vfd
->fops
= &video_fops
;
353 if (type
== VFL_TYPE_VIDEO
) {
354 vfd
->ioctl_ops
= &dev
->ext_vv_data
->vid_ops
;
355 q
= &dev
->vv_data
->video_dmaq
.q
;
357 vfd
->ioctl_ops
= &dev
->ext_vv_data
->vbi_ops
;
358 q
= &dev
->vv_data
->vbi_dmaq
.q
;
360 vfd
->release
= video_device_release_empty
;
361 vfd
->lock
= &dev
->v4l2_lock
;
362 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
364 for (i
= 0; i
< dev
->ext_vv_data
->num_stds
; i
++)
365 vfd
->tvnorms
|= dev
->ext_vv_data
->stds
[i
].id
;
366 strscpy(vfd
->name
, name
, sizeof(vfd
->name
));
367 vfd
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
|
368 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
369 vfd
->device_caps
|= dev
->ext_vv_data
->capabilities
;
370 if (type
== VFL_TYPE_VIDEO
) {
372 ~(V4L2_CAP_VBI_CAPTURE
| V4L2_CAP_SLICED_VBI_OUTPUT
);
373 } else if (vfd
->device_caps
& V4L2_CAP_SLICED_VBI_OUTPUT
) {
374 vfd
->vfl_dir
= VFL_DIR_TX
;
375 vfd
->device_caps
&= ~(V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
|
376 V4L2_CAP_AUDIO
| V4L2_CAP_TUNER
);
378 vfd
->device_caps
&= ~V4L2_CAP_VIDEO_CAPTURE
;
381 q
->type
= type
== VFL_TYPE_VIDEO
? V4L2_BUF_TYPE_VIDEO_CAPTURE
: V4L2_BUF_TYPE_VBI_CAPTURE
;
382 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
383 q
->io_modes
= VB2_MMAP
| VB2_READ
| VB2_DMABUF
;
384 q
->ops
= type
== VFL_TYPE_VIDEO
? &video_qops
: &vbi_qops
;
385 q
->mem_ops
= &vb2_dma_sg_memops
;
387 q
->gfp_flags
= __GFP_DMA32
;
388 q
->buf_struct_size
= sizeof(struct saa7146_buf
);
389 q
->lock
= &dev
->v4l2_lock
;
390 q
->min_queued_buffers
= 2;
391 q
->dev
= &dev
->pci
->dev
;
392 err
= vb2_queue_init(q
);
397 video_set_drvdata(vfd
, dev
);
399 err
= video_register_device(vfd
, type
, -1);
401 ERR("cannot register v4l2 device. skipping.\n");
405 pr_info("%s: registered device %s [v4l2]\n",
406 dev
->name
, video_device_node_name(vfd
));
409 EXPORT_SYMBOL_GPL(saa7146_register_device
);
411 int saa7146_unregister_device(struct video_device
*vfd
, struct saa7146_dev
*dev
)
413 DEB_EE("dev:%p\n", dev
);
415 video_unregister_device(vfd
);
418 EXPORT_SYMBOL_GPL(saa7146_unregister_device
);
420 static int __init
saa7146_vv_init_module(void)
426 static void __exit
saa7146_vv_cleanup_module(void)
430 module_init(saa7146_vv_init_module
);
431 module_exit(saa7146_vv_cleanup_module
);
433 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
434 MODULE_DESCRIPTION("video4linux driver for saa7146-based hardware");
435 MODULE_LICENSE("GPL");