1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011-2018 Magewell Electronics Co., Ltd. (Nanjing)
5 * Author: Yong Deng <yong.deng@magewell.com>
10 #include <media/v4l2-device.h>
11 #include <media/v4l2-event.h>
12 #include <media/v4l2-ioctl.h>
13 #include <media/v4l2-mc.h>
14 #include <media/videobuf2-dma-contig.h>
15 #include <media/videobuf2-v4l2.h>
17 #include "sun6i_csi.h"
18 #include "sun6i_video.h"
20 /* This is got from BSP sources. */
21 #define MIN_WIDTH (32)
22 #define MIN_HEIGHT (32)
23 #define MAX_WIDTH (4800)
24 #define MAX_HEIGHT (4800)
26 struct sun6i_csi_buffer {
27 struct vb2_v4l2_buffer v4l2_buffer;
28 struct list_head list;
36 static struct v4l2_subdev *
37 sun6i_video_remote_subdev(struct sun6i_video *video, u32 *pad)
39 struct media_pad *remote;
41 remote = media_pad_remote_pad_first(&video->pad);
43 if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
49 return media_entity_to_v4l2_subdev(remote->entity);
54 static const u32 sun6i_video_formats[] = {
71 V4L2_PIX_FMT_NV12_16L16,
84 static bool sun6i_video_format_check(u32 format)
88 for (i = 0; i < ARRAY_SIZE(sun6i_video_formats); i++)
89 if (sun6i_video_formats[i] == format)
97 static void sun6i_video_buffer_configure(struct sun6i_csi_device *csi_dev,
98 struct sun6i_csi_buffer *csi_buffer)
100 csi_buffer->queued_to_csi = true;
101 sun6i_csi_update_buf_addr(csi_dev, csi_buffer->dma_addr);
104 static void sun6i_video_configure(struct sun6i_csi_device *csi_dev)
106 struct sun6i_video *video = &csi_dev->video;
107 struct sun6i_csi_config config = { 0 };
109 config.pixelformat = video->format.fmt.pix.pixelformat;
110 config.code = video->mbus_code;
111 config.field = video->format.fmt.pix.field;
112 config.width = video->format.fmt.pix.width;
113 config.height = video->format.fmt.pix.height;
115 sun6i_csi_update_config(csi_dev, &config);
120 static int sun6i_video_queue_setup(struct vb2_queue *queue,
121 unsigned int *buffers_count,
122 unsigned int *planes_count,
123 unsigned int sizes[],
124 struct device *alloc_devs[])
126 struct sun6i_csi_device *csi_dev = vb2_get_drv_priv(queue);
127 struct sun6i_video *video = &csi_dev->video;
128 unsigned int size = video->format.fmt.pix.sizeimage;
131 return sizes[0] < size ? -EINVAL : 0;
139 static int sun6i_video_buffer_prepare(struct vb2_buffer *buffer)
141 struct sun6i_csi_device *csi_dev = vb2_get_drv_priv(buffer->vb2_queue);
142 struct sun6i_video *video = &csi_dev->video;
143 struct v4l2_device *v4l2_dev = &csi_dev->v4l2.v4l2_dev;
144 struct vb2_v4l2_buffer *v4l2_buffer = to_vb2_v4l2_buffer(buffer);
145 struct sun6i_csi_buffer *csi_buffer =
146 container_of(v4l2_buffer, struct sun6i_csi_buffer, v4l2_buffer);
147 unsigned long size = video->format.fmt.pix.sizeimage;
149 if (vb2_plane_size(buffer, 0) < size) {
150 v4l2_err(v4l2_dev, "buffer too small (%lu < %lu)\n",
151 vb2_plane_size(buffer, 0), size);
155 vb2_set_plane_payload(buffer, 0, size);
157 csi_buffer->dma_addr = vb2_dma_contig_plane_dma_addr(buffer, 0);
158 v4l2_buffer->field = video->format.fmt.pix.field;
163 static void sun6i_video_buffer_queue(struct vb2_buffer *buffer)
165 struct sun6i_csi_device *csi_dev = vb2_get_drv_priv(buffer->vb2_queue);
166 struct sun6i_video *video = &csi_dev->video;
167 struct vb2_v4l2_buffer *v4l2_buffer = to_vb2_v4l2_buffer(buffer);
168 struct sun6i_csi_buffer *csi_buffer =
169 container_of(v4l2_buffer, struct sun6i_csi_buffer, v4l2_buffer);
172 spin_lock_irqsave(&video->dma_queue_lock, flags);
173 csi_buffer->queued_to_csi = false;
174 list_add_tail(&csi_buffer->list, &video->dma_queue);
175 spin_unlock_irqrestore(&video->dma_queue_lock, flags);
178 static int sun6i_video_start_streaming(struct vb2_queue *queue,
181 struct sun6i_csi_device *csi_dev = vb2_get_drv_priv(queue);
182 struct sun6i_video *video = &csi_dev->video;
183 struct video_device *video_dev = &video->video_dev;
184 struct sun6i_csi_buffer *buf;
185 struct sun6i_csi_buffer *next_buf;
186 struct v4l2_subdev *subdev;
192 ret = video_device_pipeline_alloc_start(video_dev);
194 goto error_dma_queue_flush;
196 if (video->mbus_code == 0) {
198 goto error_media_pipeline;
201 subdev = sun6i_video_remote_subdev(video, NULL);
204 goto error_media_pipeline;
207 sun6i_video_configure(csi_dev);
209 spin_lock_irqsave(&video->dma_queue_lock, flags);
211 buf = list_first_entry(&video->dma_queue,
212 struct sun6i_csi_buffer, list);
213 sun6i_video_buffer_configure(csi_dev, buf);
215 sun6i_csi_set_stream(csi_dev, true);
218 * CSI will lookup the next dma buffer for next frame before the
219 * current frame done IRQ triggered. This is not documented
220 * but reported by Ondřej Jirman.
221 * The BSP code has workaround for this too. It skip to mark the
222 * first buffer as frame done for VB2 and pass the second buffer
223 * to CSI in the first frame done ISR call. Then in second frame
224 * done ISR call, it mark the first buffer as frame done for VB2
225 * and pass the third buffer to CSI. And so on. The bad thing is
226 * that the first buffer will be written twice and the first frame
227 * is dropped even the queued buffer is sufficient.
228 * So, I make some improvement here. Pass the next buffer to CSI
229 * just follow starting the CSI. In this case, the first frame
230 * will be stored in first buffer, second frame in second buffer.
231 * This method is used to avoid dropping the first frame, it
232 * would also drop frame when lacking of queued buffer.
234 next_buf = list_next_entry(buf, list);
235 sun6i_video_buffer_configure(csi_dev, next_buf);
237 spin_unlock_irqrestore(&video->dma_queue_lock, flags);
239 ret = v4l2_subdev_call(subdev, video, s_stream, 1);
240 if (ret && ret != -ENOIOCTLCMD)
246 sun6i_csi_set_stream(csi_dev, false);
248 error_media_pipeline:
249 video_device_pipeline_stop(video_dev);
251 error_dma_queue_flush:
252 spin_lock_irqsave(&video->dma_queue_lock, flags);
253 list_for_each_entry(buf, &video->dma_queue, list)
254 vb2_buffer_done(&buf->v4l2_buffer.vb2_buf,
255 VB2_BUF_STATE_QUEUED);
256 INIT_LIST_HEAD(&video->dma_queue);
257 spin_unlock_irqrestore(&video->dma_queue_lock, flags);
262 static void sun6i_video_stop_streaming(struct vb2_queue *queue)
264 struct sun6i_csi_device *csi_dev = vb2_get_drv_priv(queue);
265 struct sun6i_video *video = &csi_dev->video;
266 struct v4l2_subdev *subdev;
268 struct sun6i_csi_buffer *buf;
270 subdev = sun6i_video_remote_subdev(video, NULL);
272 v4l2_subdev_call(subdev, video, s_stream, 0);
274 sun6i_csi_set_stream(csi_dev, false);
276 video_device_pipeline_stop(&video->video_dev);
278 /* Release all active buffers */
279 spin_lock_irqsave(&video->dma_queue_lock, flags);
280 list_for_each_entry(buf, &video->dma_queue, list)
281 vb2_buffer_done(&buf->v4l2_buffer.vb2_buf, VB2_BUF_STATE_ERROR);
282 INIT_LIST_HEAD(&video->dma_queue);
283 spin_unlock_irqrestore(&video->dma_queue_lock, flags);
286 void sun6i_video_frame_done(struct sun6i_csi_device *csi_dev)
288 struct sun6i_video *video = &csi_dev->video;
289 struct sun6i_csi_buffer *buf;
290 struct sun6i_csi_buffer *next_buf;
291 struct vb2_v4l2_buffer *v4l2_buffer;
293 spin_lock(&video->dma_queue_lock);
295 buf = list_first_entry(&video->dma_queue,
296 struct sun6i_csi_buffer, list);
297 if (list_is_last(&buf->list, &video->dma_queue)) {
298 dev_dbg(csi_dev->dev, "Frame dropped!\n");
302 next_buf = list_next_entry(buf, list);
303 /* If a new buffer (#next_buf) had not been queued to CSI, the old
304 * buffer (#buf) is still holding by CSI for storing the next
305 * frame. So, we queue a new buffer (#next_buf) to CSI then wait
308 if (!next_buf->queued_to_csi) {
309 sun6i_video_buffer_configure(csi_dev, next_buf);
310 dev_dbg(csi_dev->dev, "Frame dropped!\n");
314 list_del(&buf->list);
315 v4l2_buffer = &buf->v4l2_buffer;
316 v4l2_buffer->vb2_buf.timestamp = ktime_get_ns();
317 v4l2_buffer->sequence = video->sequence;
318 vb2_buffer_done(&v4l2_buffer->vb2_buf, VB2_BUF_STATE_DONE);
320 /* Prepare buffer for next frame but one. */
321 if (!list_is_last(&next_buf->list, &video->dma_queue)) {
322 next_buf = list_next_entry(next_buf, list);
323 sun6i_video_buffer_configure(csi_dev, next_buf);
325 dev_dbg(csi_dev->dev, "Next frame will be dropped!\n");
330 spin_unlock(&video->dma_queue_lock);
333 static const struct vb2_ops sun6i_video_queue_ops = {
334 .queue_setup = sun6i_video_queue_setup,
335 .buf_prepare = sun6i_video_buffer_prepare,
336 .buf_queue = sun6i_video_buffer_queue,
337 .start_streaming = sun6i_video_start_streaming,
338 .stop_streaming = sun6i_video_stop_streaming,
339 .wait_prepare = vb2_ops_wait_prepare,
340 .wait_finish = vb2_ops_wait_finish,
345 static int sun6i_video_querycap(struct file *file, void *private,
346 struct v4l2_capability *capability)
348 struct sun6i_csi_device *csi_dev = video_drvdata(file);
349 struct video_device *video_dev = &csi_dev->video.video_dev;
351 strscpy(capability->driver, SUN6I_CSI_NAME, sizeof(capability->driver));
352 strscpy(capability->card, video_dev->name, sizeof(capability->card));
353 snprintf(capability->bus_info, sizeof(capability->bus_info),
354 "platform:%s", dev_name(csi_dev->dev));
359 static int sun6i_video_enum_fmt(struct file *file, void *private,
360 struct v4l2_fmtdesc *fmtdesc)
362 u32 index = fmtdesc->index;
364 if (index >= ARRAY_SIZE(sun6i_video_formats))
367 fmtdesc->pixelformat = sun6i_video_formats[index];
372 static int sun6i_video_g_fmt(struct file *file, void *private,
373 struct v4l2_format *format)
375 struct sun6i_csi_device *csi_dev = video_drvdata(file);
376 struct sun6i_video *video = &csi_dev->video;
378 *format = video->format;
383 static int sun6i_video_format_try(struct sun6i_video *video,
384 struct v4l2_format *format)
386 struct v4l2_pix_format *pix_format = &format->fmt.pix;
389 if (!sun6i_video_format_check(pix_format->pixelformat))
390 pix_format->pixelformat = sun6i_video_formats[0];
392 v4l_bound_align_image(&pix_format->width, MIN_WIDTH, MAX_WIDTH, 1,
393 &pix_format->height, MIN_HEIGHT, MAX_WIDTH, 1, 1);
395 bpp = sun6i_csi_get_bpp(pix_format->pixelformat);
396 pix_format->bytesperline = (pix_format->width * bpp) >> 3;
397 pix_format->sizeimage = pix_format->bytesperline * pix_format->height;
399 if (pix_format->field == V4L2_FIELD_ANY)
400 pix_format->field = V4L2_FIELD_NONE;
402 if (pix_format->pixelformat == V4L2_PIX_FMT_JPEG)
403 pix_format->colorspace = V4L2_COLORSPACE_JPEG;
405 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
407 pix_format->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
408 pix_format->quantization = V4L2_QUANTIZATION_DEFAULT;
409 pix_format->xfer_func = V4L2_XFER_FUNC_DEFAULT;
414 static int sun6i_video_format_set(struct sun6i_video *video,
415 struct v4l2_format *format)
419 ret = sun6i_video_format_try(video, format);
423 video->format = *format;
428 static int sun6i_video_s_fmt(struct file *file, void *private,
429 struct v4l2_format *format)
431 struct sun6i_csi_device *csi_dev = video_drvdata(file);
432 struct sun6i_video *video = &csi_dev->video;
434 if (vb2_is_busy(&video->queue))
437 return sun6i_video_format_set(video, format);
440 static int sun6i_video_try_fmt(struct file *file, void *private,
441 struct v4l2_format *format)
443 struct sun6i_csi_device *csi_dev = video_drvdata(file);
444 struct sun6i_video *video = &csi_dev->video;
446 return sun6i_video_format_try(video, format);
449 static int sun6i_video_enum_input(struct file *file, void *private,
450 struct v4l2_input *input)
452 if (input->index != 0)
455 input->type = V4L2_INPUT_TYPE_CAMERA;
456 strscpy(input->name, "Camera", sizeof(input->name));
461 static int sun6i_video_g_input(struct file *file, void *private,
469 static int sun6i_video_s_input(struct file *file, void *private,
478 static const struct v4l2_ioctl_ops sun6i_video_ioctl_ops = {
479 .vidioc_querycap = sun6i_video_querycap,
481 .vidioc_enum_fmt_vid_cap = sun6i_video_enum_fmt,
482 .vidioc_g_fmt_vid_cap = sun6i_video_g_fmt,
483 .vidioc_s_fmt_vid_cap = sun6i_video_s_fmt,
484 .vidioc_try_fmt_vid_cap = sun6i_video_try_fmt,
486 .vidioc_enum_input = sun6i_video_enum_input,
487 .vidioc_g_input = sun6i_video_g_input,
488 .vidioc_s_input = sun6i_video_s_input,
490 .vidioc_create_bufs = vb2_ioctl_create_bufs,
491 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
492 .vidioc_reqbufs = vb2_ioctl_reqbufs,
493 .vidioc_querybuf = vb2_ioctl_querybuf,
494 .vidioc_expbuf = vb2_ioctl_expbuf,
495 .vidioc_qbuf = vb2_ioctl_qbuf,
496 .vidioc_dqbuf = vb2_ioctl_dqbuf,
497 .vidioc_streamon = vb2_ioctl_streamon,
498 .vidioc_streamoff = vb2_ioctl_streamoff,
503 static int sun6i_video_open(struct file *file)
505 struct sun6i_csi_device *csi_dev = video_drvdata(file);
506 struct sun6i_video *video = &csi_dev->video;
509 if (mutex_lock_interruptible(&video->lock))
512 ret = v4l2_fh_open(file);
516 ret = v4l2_pipeline_pm_get(&video->video_dev.entity);
520 /* Power on at first open. */
521 if (v4l2_fh_is_singular_file(file)) {
522 ret = sun6i_csi_set_power(csi_dev, true);
527 mutex_unlock(&video->lock);
532 v4l2_fh_release(file);
535 mutex_unlock(&video->lock);
540 static int sun6i_video_close(struct file *file)
542 struct sun6i_csi_device *csi_dev = video_drvdata(file);
543 struct sun6i_video *video = &csi_dev->video;
546 mutex_lock(&video->lock);
548 last_close = v4l2_fh_is_singular_file(file);
550 _vb2_fop_release(file, NULL);
551 v4l2_pipeline_pm_put(&video->video_dev.entity);
553 /* Power off at last close. */
555 sun6i_csi_set_power(csi_dev, false);
557 mutex_unlock(&video->lock);
562 static const struct v4l2_file_operations sun6i_video_fops = {
563 .owner = THIS_MODULE,
564 .open = sun6i_video_open,
565 .release = sun6i_video_close,
566 .unlocked_ioctl = video_ioctl2,
567 .mmap = vb2_fop_mmap,
573 static int sun6i_video_link_validate_get_format(struct media_pad *pad,
574 struct v4l2_subdev_format *fmt)
576 if (is_media_entity_v4l2_subdev(pad->entity)) {
577 struct v4l2_subdev *sd =
578 media_entity_to_v4l2_subdev(pad->entity);
580 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
581 fmt->pad = pad->index;
582 return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
588 static int sun6i_video_link_validate(struct media_link *link)
590 struct video_device *vdev = container_of(link->sink->entity,
591 struct video_device, entity);
592 struct sun6i_csi_device *csi_dev = video_get_drvdata(vdev);
593 struct sun6i_video *video = &csi_dev->video;
594 struct v4l2_subdev_format source_fmt;
597 video->mbus_code = 0;
599 if (!media_pad_remote_pad_first(link->sink->entity->pads)) {
600 dev_info(csi_dev->dev, "video node %s pad not connected\n",
605 ret = sun6i_video_link_validate_get_format(link->source, &source_fmt);
609 if (!sun6i_csi_is_format_supported(csi_dev,
610 video->format.fmt.pix.pixelformat,
611 source_fmt.format.code)) {
612 dev_err(csi_dev->dev,
613 "Unsupported pixformat: 0x%x with mbus code: 0x%x!\n",
614 video->format.fmt.pix.pixelformat,
615 source_fmt.format.code);
619 if (source_fmt.format.width != video->format.fmt.pix.width ||
620 source_fmt.format.height != video->format.fmt.pix.height) {
621 dev_err(csi_dev->dev,
622 "Wrong width or height %ux%u (%ux%u expected)\n",
623 video->format.fmt.pix.width, video->format.fmt.pix.height,
624 source_fmt.format.width, source_fmt.format.height);
628 video->mbus_code = source_fmt.format.code;
633 static const struct media_entity_operations sun6i_video_media_ops = {
634 .link_validate = sun6i_video_link_validate
639 int sun6i_video_setup(struct sun6i_csi_device *csi_dev)
641 struct sun6i_video *video = &csi_dev->video;
642 struct v4l2_device *v4l2_dev = &csi_dev->v4l2.v4l2_dev;
643 struct video_device *video_dev = &video->video_dev;
644 struct vb2_queue *queue = &video->queue;
645 struct media_pad *pad = &video->pad;
646 struct v4l2_format format = { 0 };
647 struct v4l2_pix_format *pix_format = &format.fmt.pix;
652 video_dev->entity.ops = &sun6i_video_media_ops;
656 pad->flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
658 ret = media_entity_pads_init(&video_dev->entity, 1, pad);
664 INIT_LIST_HEAD(&video->dma_queue);
665 spin_lock_init(&video->dma_queue_lock);
671 mutex_init(&video->lock);
673 queue->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
674 queue->io_modes = VB2_MMAP | VB2_DMABUF;
675 queue->buf_struct_size = sizeof(struct sun6i_csi_buffer);
676 queue->ops = &sun6i_video_queue_ops;
677 queue->mem_ops = &vb2_dma_contig_memops;
678 queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
679 queue->lock = &video->lock;
680 queue->dev = csi_dev->dev;
681 queue->drv_priv = csi_dev;
683 /* Make sure non-dropped frame. */
684 queue->min_buffers_needed = 3;
686 ret = vb2_queue_init(queue);
688 v4l2_err(v4l2_dev, "failed to initialize vb2 queue: %d\n", ret);
689 goto error_media_entity;
694 format.type = queue->type;
695 pix_format->pixelformat = sun6i_video_formats[0];
696 pix_format->width = 1280;
697 pix_format->height = 720;
698 pix_format->field = V4L2_FIELD_NONE;
700 sun6i_video_format_set(video, &format);
704 strscpy(video_dev->name, SUN6I_CSI_NAME, sizeof(video_dev->name));
705 video_dev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
706 video_dev->vfl_dir = VFL_DIR_RX;
707 video_dev->release = video_device_release_empty;
708 video_dev->fops = &sun6i_video_fops;
709 video_dev->ioctl_ops = &sun6i_video_ioctl_ops;
710 video_dev->v4l2_dev = v4l2_dev;
711 video_dev->queue = queue;
712 video_dev->lock = &video->lock;
714 video_set_drvdata(video_dev, csi_dev);
716 ret = video_register_device(video_dev, VFL_TYPE_VIDEO, -1);
718 v4l2_err(v4l2_dev, "failed to register video device: %d\n",
720 goto error_media_entity;
726 media_entity_cleanup(&video_dev->entity);
728 mutex_destroy(&video->lock);
733 void sun6i_video_cleanup(struct sun6i_csi_device *csi_dev)
735 struct sun6i_video *video = &csi_dev->video;
736 struct video_device *video_dev = &video->video_dev;
738 vb2_video_unregister_device(video_dev);
739 media_entity_cleanup(&video_dev->entity);
740 mutex_destroy(&video->lock);