Lines Matching +full:field +full:- +full:even +full:- +full:active

1 // SPDX-License-Identifier: GPL-2.0-only
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/v4l2-fh.h>
30 #include <media/v4l2-event.h>
31 #include <media/videobuf2-dma-contig.h>
86 * struct sta2x11_vip - All internal data for one instance of device
102 * @active: current active buffer
111 * All non-local data is accessed via this structure.
132 struct vip_buffer *active; /* current active buffer */ member
153 .field = V4L2_FIELD_INTERLACED,
161 .field = V4L2_FIELD_TOP,
169 .field = V4L2_FIELD_BOTTOM,
181 .field = V4L2_FIELD_INTERLACED,
189 .field = V4L2_FIELD_TOP,
197 .field = V4L2_FIELD_BOTTOM,
206 iowrite32((val), (vip->iomem)+(reg)); in reg_write()
211 return ioread32((vip->iomem)+(reg)); in reg_read()
218 if (vip->format.field == V4L2_FIELD_INTERLACED) in start_dma()
219 offset = vip->format.width * 2; in start_dma()
221 spin_lock_irq(&vip->slock); in start_dma()
224 /* Set Top and Bottom Field memory address */ in start_dma()
225 reg_write(vip, DVP_VTP, (u32)vip_buf->dma); in start_dma()
226 reg_write(vip, DVP_VBP, (u32)vip_buf->dma + offset); in start_dma()
227 spin_unlock_irq(&vip->slock); in start_dma()
234 spin_lock(&vip->lock); in vip_active_buf_next()
235 if (list_empty(&vip->buffer_list)) {/* No available buffer */ in vip_active_buf_next()
236 spin_unlock(&vip->lock); in vip_active_buf_next()
239 vip->active = list_first_entry(&vip->buffer_list, in vip_active_buf_next()
243 vip->tcount = 0; in vip_active_buf_next()
244 vip->bcount = 0; in vip_active_buf_next()
245 spin_unlock(&vip->lock); in vip_active_buf_next()
246 if (vb2_is_streaming(&vip->vb_vidq)) { /* streaming is on */ in vip_active_buf_next()
247 start_dma(vip, vip->active); /* start dma capture */ in vip_active_buf_next()
263 sizes[0] = vip->format.sizeimage; in queue_setup()
265 vip->sequence = 0; in queue_setup()
266 vip->active = NULL; in queue_setup()
267 vip->tcount = 0; in queue_setup()
268 vip->bcount = 0; in queue_setup()
277 vip_buf->dma = vb2_dma_contig_plane_dma_addr(vb, 0); in buffer_init()
278 INIT_LIST_HEAD(&vip_buf->list); in buffer_init()
285 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue); in buffer_prepare()
289 size = vip->format.sizeimage; in buffer_prepare()
291 v4l2_err(&vip->v4l2_dev, "buffer too small (%lu < %lu)\n", in buffer_prepare()
293 return -EINVAL; in buffer_prepare()
296 vb2_set_plane_payload(&vip_buf->vb.vb2_buf, 0, size); in buffer_prepare()
303 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue); in buffer_queue()
306 spin_lock(&vip->lock); in buffer_queue()
307 list_add_tail(&vip_buf->list, &vip->buffer_list); in buffer_queue()
308 if (!vip->active) { /* No active buffer, active the first one */ in buffer_queue()
309 vip->active = list_first_entry(&vip->buffer_list, in buffer_queue()
312 if (vb2_is_streaming(&vip->vb_vidq)) /* streaming is on */ in buffer_queue()
315 spin_unlock(&vip->lock); in buffer_queue()
320 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue); in buffer_finish()
324 spin_lock(&vip->lock); in buffer_finish()
325 list_del_init(&vip_buf->list); in buffer_finish()
326 spin_unlock(&vip->lock); in buffer_finish()
328 if (vb2_is_streaming(vb->vb2_queue)) in buffer_finish()
336 spin_lock_irq(&vip->slock); in start_streaming()
339 spin_unlock_irq(&vip->slock); in start_streaming()
342 start_dma(vip, vip->active); in start_streaming()
358 /* Release all active buffers */ in stop_streaming()
359 spin_lock(&vip->lock); in stop_streaming()
360 list_for_each_entry_safe(vip_buf, node, &vip->buffer_list, list) { in stop_streaming()
361 vb2_buffer_done(&vip_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); in stop_streaming()
362 list_del(&vip_buf->list); in stop_streaming()
364 spin_unlock(&vip->lock); in stop_streaming()
393 * vidioc_querycap - return capabilities of device
405 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); in vidioc_querycap()
406 strscpy(cap->card, KBUILD_MODNAME, sizeof(cap->card)); in vidioc_querycap()
411 * vidioc_s_std - set video standard
420 * -EIO, no input signal detected
430 * The use of V4L2_STD_ALL to trigger a querystd is non-standard. in vidioc_s_std()
433 v4l2_subdev_call(vip->decoder, video, querystd, &std); in vidioc_s_std()
435 return -EIO; in vidioc_s_std()
438 if (vip->std != std) { in vidioc_s_std()
439 vip->std = std; in vidioc_s_std()
441 vip->format = formats_60[0]; in vidioc_s_std()
443 vip->format = formats_50[0]; in vidioc_s_std()
446 return v4l2_subdev_call(vip->decoder, video, s_std, std); in vidioc_s_std()
450 * vidioc_g_std - get video standard
463 *std = vip->std; in vidioc_g_std()
468 * vidioc_querystd - get possible video standards
481 return v4l2_subdev_call(vip->decoder, video, querystd, std); in vidioc_querystd()
487 if (inp->index > 1) in vidioc_enum_input()
488 return -EINVAL; in vidioc_enum_input()
490 inp->type = V4L2_INPUT_TYPE_CAMERA; in vidioc_enum_input()
491 inp->std = V4L2_STD_ALL; in vidioc_enum_input()
492 sprintf(inp->name, "Camera %u", inp->index); in vidioc_enum_input()
498 * vidioc_s_input - set input line
503 * the current active input line is set
507 * -EINVAL, line number out of range
515 return -EINVAL; in vidioc_s_input()
516 ret = v4l2_subdev_call(vip->decoder, video, s_routing, i, 0, 0); in vidioc_s_input()
519 vip->input = i; in vidioc_s_input()
525 * vidioc_g_input - return input line
530 * the current active input line is returned
538 *i = vip->input; in vidioc_g_input()
543 * vidioc_enum_fmt_vid_cap - return video capture format
557 if (f->index != 0) in vidioc_enum_fmt_vid_cap()
558 return -EINVAL; in vidioc_enum_fmt_vid_cap()
560 f->pixelformat = V4L2_PIX_FMT_UYVY; in vidioc_enum_fmt_vid_cap()
565 * vidioc_try_fmt_vid_cap - set video capture format
571 * field type. width is fixed to 720, no scaling.
577 * -EINVAL, pixel or field format not supported
586 if (V4L2_PIX_FMT_UYVY != f->fmt.pix.pixelformat) { in vidioc_try_fmt_vid_cap()
587 v4l2_warn(&vip->v4l2_dev, "Invalid format, only UYVY supported\n"); in vidioc_try_fmt_vid_cap()
588 return -EINVAL; in vidioc_try_fmt_vid_cap()
591 if (V4L2_STD_525_60 & vip->std) in vidioc_try_fmt_vid_cap()
596 switch (f->fmt.pix.field) { in vidioc_try_fmt_vid_cap()
599 if (interlace_lim < f->fmt.pix.height) in vidioc_try_fmt_vid_cap()
600 f->fmt.pix.field = V4L2_FIELD_INTERLACED; in vidioc_try_fmt_vid_cap()
602 f->fmt.pix.field = V4L2_FIELD_BOTTOM; in vidioc_try_fmt_vid_cap()
606 if (interlace_lim < f->fmt.pix.height) in vidioc_try_fmt_vid_cap()
607 f->fmt.pix.height = interlace_lim; in vidioc_try_fmt_vid_cap()
614 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY; in vidioc_try_fmt_vid_cap()
615 f->fmt.pix.height &= ~1; in vidioc_try_fmt_vid_cap()
616 if (2 * interlace_lim < f->fmt.pix.height) in vidioc_try_fmt_vid_cap()
617 f->fmt.pix.height = 2 * interlace_lim; in vidioc_try_fmt_vid_cap()
618 if (200 > f->fmt.pix.height) in vidioc_try_fmt_vid_cap()
619 f->fmt.pix.height = 200; in vidioc_try_fmt_vid_cap()
620 f->fmt.pix.width = 720; in vidioc_try_fmt_vid_cap()
621 f->fmt.pix.bytesperline = f->fmt.pix.width * 2; in vidioc_try_fmt_vid_cap()
622 f->fmt.pix.sizeimage = f->fmt.pix.width * 2 * f->fmt.pix.height; in vidioc_try_fmt_vid_cap()
623 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; in vidioc_try_fmt_vid_cap()
628 * vidioc_s_fmt_vid_cap - set current video format parameters
649 if (vb2_is_busy(&vip->vb_vidq)) { in vidioc_s_fmt_vid_cap()
651 v4l2_err(&vip->v4l2_dev, "device busy\n"); in vidioc_s_fmt_vid_cap()
652 return -EBUSY; in vidioc_s_fmt_vid_cap()
654 vip->format = f->fmt.pix; in vidioc_s_fmt_vid_cap()
655 switch (vip->format.field) { in vidioc_s_fmt_vid_cap()
657 t_stop = ((vip->format.height / 2 - 1) << 16) | in vidioc_s_fmt_vid_cap()
658 (2 * vip->format.width - 1); in vidioc_s_fmt_vid_cap()
660 pitch = 4 * vip->format.width; in vidioc_s_fmt_vid_cap()
663 t_stop = ((vip->format.height - 1) << 16) | in vidioc_s_fmt_vid_cap()
664 (2 * vip->format.width - 1); in vidioc_s_fmt_vid_cap()
665 b_stop = (0 << 16) | (2 * vip->format.width - 1); in vidioc_s_fmt_vid_cap()
666 pitch = 2 * vip->format.width; in vidioc_s_fmt_vid_cap()
669 t_stop = (0 << 16) | (2 * vip->format.width - 1); in vidioc_s_fmt_vid_cap()
670 b_stop = (vip->format.height << 16) | in vidioc_s_fmt_vid_cap()
671 (2 * vip->format.width - 1); in vidioc_s_fmt_vid_cap()
672 pitch = 2 * vip->format.width; in vidioc_s_fmt_vid_cap()
675 v4l2_err(&vip->v4l2_dev, "unknown field format\n"); in vidioc_s_fmt_vid_cap()
676 return -EINVAL; in vidioc_s_fmt_vid_cap()
679 spin_lock_irq(&vip->slock); in vidioc_s_fmt_vid_cap()
680 /* Y-X Top Field Offset */ in vidioc_s_fmt_vid_cap()
682 /* Y-X Bottom Field Offset */ in vidioc_s_fmt_vid_cap()
684 /* Y-X Top Field Stop*/ in vidioc_s_fmt_vid_cap()
686 /* Y-X Bottom Field Stop */ in vidioc_s_fmt_vid_cap()
690 spin_unlock_irq(&vip->slock); in vidioc_s_fmt_vid_cap()
696 * vidioc_g_fmt_vid_cap - get current video format parameters
710 f->fmt.pix = vip->format; in vidioc_g_fmt_vid_cap()
758 * vip_irq - interrupt routine
781 if (vip->overflow++ > 5) in vip_irq()
791 if ((++vip->tcount) < 2) in vip_irq()
794 vip->bcount++; in vip_irq()
798 if (vip->active) { /* Acquisition is over on this buffer */ in vip_irq()
801 /* Remove the active buffer from the list */ in vip_irq()
802 vip->active->vb.vb2_buf.timestamp = ktime_get_ns(); in vip_irq()
803 vip->active->vb.sequence = vip->sequence++; in vip_irq()
804 vb2_buffer_done(&vip->active->vb.vb2_buf, VB2_BUF_STATE_DONE); in vip_irq()
813 spin_lock_irq(&vip->slock); in sta2x11_vip_init_register()
822 spin_unlock_irq(&vip->slock); in sta2x11_vip_init_register()
826 spin_lock_irq(&vip->slock); in sta2x11_vip_clear_register()
835 spin_unlock_irq(&vip->slock); in sta2x11_vip_clear_register()
841 err = dma_set_coherent_mask(&vip->pdev->dev, DMA_BIT_MASK(29)); in sta2x11_vip_init_buffer()
843 v4l2_err(&vip->v4l2_dev, "Cannot configure coherent mask"); in sta2x11_vip_init_buffer()
846 memset(&vip->vb_vidq, 0, sizeof(struct vb2_queue)); in sta2x11_vip_init_buffer()
847 vip->vb_vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; in sta2x11_vip_init_buffer()
848 vip->vb_vidq.io_modes = VB2_MMAP | VB2_READ; in sta2x11_vip_init_buffer()
849 vip->vb_vidq.drv_priv = vip; in sta2x11_vip_init_buffer()
850 vip->vb_vidq.buf_struct_size = sizeof(struct vip_buffer); in sta2x11_vip_init_buffer()
851 vip->vb_vidq.ops = &vip_video_qops; in sta2x11_vip_init_buffer()
852 vip->vb_vidq.mem_ops = &vb2_dma_contig_memops; in sta2x11_vip_init_buffer()
853 vip->vb_vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; in sta2x11_vip_init_buffer()
854 vip->vb_vidq.dev = &vip->pdev->dev; in sta2x11_vip_init_buffer()
855 vip->vb_vidq.lock = &vip->v4l_lock; in sta2x11_vip_init_buffer()
856 err = vb2_queue_init(&vip->vb_vidq); in sta2x11_vip_init_buffer()
859 INIT_LIST_HEAD(&vip->buffer_list); in sta2x11_vip_init_buffer()
860 spin_lock_init(&vip->lock); in sta2x11_vip_init_buffer()
870 v4l2_ctrl_handler_init(&vip->ctrl_hdl, 0); in sta2x11_vip_init_controls()
872 vip->v4l2_dev.ctrl_handler = &vip->ctrl_hdl; in sta2x11_vip_init_controls()
873 if (vip->ctrl_hdl.error) { in sta2x11_vip_init_controls()
874 int err = vip->ctrl_hdl.error; in sta2x11_vip_init_controls()
876 v4l2_ctrl_handler_free(&vip->ctrl_hdl); in sta2x11_vip_init_controls()
884 * vip_gpio_reserve - reserve gpio pin
895 int ret = -ENODEV; in vip_gpio_reserve()
925 * vip_gpio_release - release gpio pin
943 * sta2x11_vip_init_one - init one instance of video device
957 * -ENOMEM, no memory
959 * -ENODEV, device could not be detected or registered
968 /* Check if hardware support 26-bit DMA */ in sta2x11_vip_init_one()
969 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(26))) { in sta2x11_vip_init_one()
970 dev_err(&pdev->dev, "26-bit DMA addressing not available\n"); in sta2x11_vip_init_one()
971 return -EINVAL; in sta2x11_vip_init_one()
979 config = dev_get_platdata(&pdev->dev); in sta2x11_vip_init_one()
981 dev_info(&pdev->dev, "VIP slot disabled\n"); in sta2x11_vip_init_one()
982 ret = -EINVAL; in sta2x11_vip_init_one()
987 ret = vip_gpio_reserve(&pdev->dev, config->pwr_pin, 0, in sta2x11_vip_init_one()
988 config->pwr_name); in sta2x11_vip_init_one()
992 ret = vip_gpio_reserve(&pdev->dev, config->reset_pin, 0, in sta2x11_vip_init_one()
993 config->reset_name); in sta2x11_vip_init_one()
995 vip_gpio_release(&pdev->dev, config->pwr_pin, in sta2x11_vip_init_one()
996 config->pwr_name); in sta2x11_vip_init_one()
1000 if (gpio_is_valid(config->pwr_pin)) { in sta2x11_vip_init_one()
1003 gpio_direction_output(config->pwr_pin, 1); in sta2x11_vip_init_one()
1006 if (gpio_is_valid(config->reset_pin)) { in sta2x11_vip_init_one()
1009 gpio_direction_output(config->reset_pin, 1); in sta2x11_vip_init_one()
1016 ret = -ENOMEM; in sta2x11_vip_init_one()
1019 vip->pdev = pdev; in sta2x11_vip_init_one()
1020 vip->std = V4L2_STD_PAL; in sta2x11_vip_init_one()
1021 vip->format = formats_50[0]; in sta2x11_vip_init_one()
1022 vip->config = config; in sta2x11_vip_init_one()
1023 mutex_init(&vip->v4l_lock); in sta2x11_vip_init_one()
1028 ret = v4l2_device_register(&pdev->dev, &vip->v4l2_dev); in sta2x11_vip_init_one()
1032 dev_dbg(&pdev->dev, "BAR #0 at 0x%lx 0x%lx irq %d\n", in sta2x11_vip_init_one()
1034 (unsigned long)pci_resource_len(pdev, 0), pdev->irq); in sta2x11_vip_init_one()
1042 vip->iomem = pci_iomap(pdev, 0, 0x100); in sta2x11_vip_init_one()
1043 if (!vip->iomem) { in sta2x11_vip_init_one()
1044 ret = -ENOMEM; in sta2x11_vip_init_one()
1055 spin_lock_init(&vip->slock); in sta2x11_vip_init_one()
1057 ret = request_irq(pdev->irq, vip_irq, IRQF_SHARED, KBUILD_MODNAME, vip); in sta2x11_vip_init_one()
1059 dev_err(&pdev->dev, "request_irq failed\n"); in sta2x11_vip_init_one()
1060 ret = -ENODEV; in sta2x11_vip_init_one()
1065 vip->video_dev = video_dev_template; in sta2x11_vip_init_one()
1066 vip->video_dev.v4l2_dev = &vip->v4l2_dev; in sta2x11_vip_init_one()
1067 vip->video_dev.queue = &vip->vb_vidq; in sta2x11_vip_init_one()
1068 vip->video_dev.lock = &vip->v4l_lock; in sta2x11_vip_init_one()
1069 video_set_drvdata(&vip->video_dev, vip); in sta2x11_vip_init_one()
1071 ret = video_register_device(&vip->video_dev, VFL_TYPE_VIDEO, -1); in sta2x11_vip_init_one()
1076 vip->adapter = i2c_get_adapter(vip->config->i2c_id); in sta2x11_vip_init_one()
1077 if (!vip->adapter) { in sta2x11_vip_init_one()
1078 ret = -ENODEV; in sta2x11_vip_init_one()
1079 dev_err(&pdev->dev, "no I2C adapter found\n"); in sta2x11_vip_init_one()
1083 vip->decoder = v4l2_i2c_new_subdev(&vip->v4l2_dev, vip->adapter, in sta2x11_vip_init_one()
1084 "adv7180", vip->config->i2c_addr, in sta2x11_vip_init_one()
1086 if (!vip->decoder) { in sta2x11_vip_init_one()
1087 ret = -ENODEV; in sta2x11_vip_init_one()
1088 dev_err(&pdev->dev, "no decoder found\n"); in sta2x11_vip_init_one()
1092 i2c_put_adapter(vip->adapter); in sta2x11_vip_init_one()
1093 v4l2_subdev_call(vip->decoder, core, init, 0); in sta2x11_vip_init_one()
1097 dev_info(&pdev->dev, "STA2X11 Video Input Port (VIP) loaded\n"); in sta2x11_vip_init_one()
1101 video_set_drvdata(&vip->video_dev, NULL); in sta2x11_vip_init_one()
1103 vb2_video_unregister_device(&vip->video_dev); in sta2x11_vip_init_one()
1104 free_irq(pdev->irq, vip); in sta2x11_vip_init_one()
1108 pci_iounmap(pdev, vip->iomem); in sta2x11_vip_init_one()
1112 v4l2_device_unregister(&vip->v4l2_dev); in sta2x11_vip_init_one()
1116 vip_gpio_release(&pdev->dev, config->reset_pin, config->reset_name); in sta2x11_vip_init_one()
1117 vip_gpio_release(&pdev->dev, config->pwr_pin, config->pwr_name); in sta2x11_vip_init_one()
1127 * sta2x11_vip_remove_one - release device
1146 video_set_drvdata(&vip->video_dev, NULL); in sta2x11_vip_remove_one()
1147 vb2_video_unregister_device(&vip->video_dev); in sta2x11_vip_remove_one()
1148 free_irq(pdev->irq, vip); in sta2x11_vip_remove_one()
1150 pci_iounmap(pdev, vip->iomem); in sta2x11_vip_remove_one()
1153 v4l2_device_unregister(&vip->v4l2_dev); in sta2x11_vip_remove_one()
1155 vip_gpio_release(&pdev->dev, vip->config->pwr_pin, in sta2x11_vip_remove_one()
1156 vip->config->pwr_name); in sta2x11_vip_remove_one()
1157 vip_gpio_release(&pdev->dev, vip->config->reset_pin, in sta2x11_vip_remove_one()
1158 vip->config->reset_name); in sta2x11_vip_remove_one()
1168 * sta2x11_vip_suspend - set device into power save mode
1174 * even if device could not be disabled. (workaround for hardware problem)
1184 spin_lock_irqsave(&vip->slock, flags); in sta2x11_vip_suspend()
1185 vip->register_save_area[0] = reg_read(vip, DVP_CTL); in sta2x11_vip_suspend()
1186 reg_write(vip, DVP_CTL, vip->register_save_area[0] & DVP_CTL_DIS); in sta2x11_vip_suspend()
1187 vip->register_save_area[SAVE_COUNT] = reg_read(vip, DVP_ITM); in sta2x11_vip_suspend()
1190 vip->register_save_area[i] = reg_read(vip, 4 * i); in sta2x11_vip_suspend()
1192 vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i] = in sta2x11_vip_suspend()
1194 spin_unlock_irqrestore(&vip->slock, flags); in sta2x11_vip_suspend()
1196 vip->disabled = 1; in sta2x11_vip_suspend()
1203 * sta2x11_vip_resume - resume device operation
1220 vip->disabled = 0; in sta2x11_vip_resume()
1222 spin_lock_irqsave(&vip->slock, flags); in sta2x11_vip_resume()
1224 reg_write(vip, 4 * i, vip->register_save_area[i]); in sta2x11_vip_resume()
1227 vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i]); in sta2x11_vip_resume()
1228 reg_write(vip, DVP_CTL, vip->register_save_area[0]); in sta2x11_vip_resume()
1229 reg_write(vip, DVP_ITM, vip->register_save_area[SAVE_COUNT]); in sta2x11_vip_resume()
1230 spin_unlock_irqrestore(&vip->slock, flags); in sta2x11_vip_resume()