1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 NVIDIA CORPORATION. All rights reserved.
6 #include <linux/bitmap.h>
8 #include <linux/delay.h>
9 #include <linux/host1x.h>
10 #include <linux/lcm.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/of_graph.h>
16 #include <linux/platform_device.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/slab.h>
21 #include <media/v4l2-event.h>
22 #include <media/v4l2-fh.h>
23 #include <media/v4l2-fwnode.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/videobuf2-dma-contig.h>
27 #include <soc/tegra/pmc.h>
32 #define SURFACE_ALIGN_BYTES 64
33 #define MAX_CID_CONTROLS 1
35 static const struct tegra_video_format tegra_default_format = {
36 .img_dt = TEGRA_IMAGE_DT_RAW10,
38 .code = MEDIA_BUS_FMT_SRGGB10_1X10,
40 .img_fmt = TEGRA_IMAGE_FORMAT_DEF,
41 .fourcc = V4L2_PIX_FMT_SRGGB10,
44 static inline struct tegra_vi *
45 host1x_client_to_vi(struct host1x_client *client)
47 return container_of(client, struct tegra_vi, client);
50 static inline struct tegra_channel_buffer *
51 to_tegra_channel_buffer(struct vb2_v4l2_buffer *vb)
53 return container_of(vb, struct tegra_channel_buffer, buf);
56 static inline struct tegra_vi_graph_entity *
57 to_tegra_vi_graph_entity(struct v4l2_async_subdev *asd)
59 return container_of(asd, struct tegra_vi_graph_entity, asd);
62 static int tegra_get_format_idx_by_code(struct tegra_vi *vi,
68 for (i = offset; i < vi->soc->nformats; ++i) {
69 if (vi->soc->video_formats[i].code == code)
76 static u32 tegra_get_format_fourcc_by_idx(struct tegra_vi *vi,
79 if (index >= vi->soc->nformats)
82 return vi->soc->video_formats[index].fourcc;
85 static const struct tegra_video_format *
86 tegra_get_format_by_fourcc(struct tegra_vi *vi, u32 fourcc)
90 for (i = 0; i < vi->soc->nformats; ++i) {
91 if (vi->soc->video_formats[i].fourcc == fourcc)
92 return &vi->soc->video_formats[i];
99 * videobuf2 queue operations
101 static int tegra_channel_queue_setup(struct vb2_queue *vq,
102 unsigned int *nbuffers,
103 unsigned int *nplanes,
104 unsigned int sizes[],
105 struct device *alloc_devs[])
107 struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
110 return sizes[0] < chan->format.sizeimage ? -EINVAL : 0;
113 sizes[0] = chan->format.sizeimage;
114 alloc_devs[0] = chan->vi->dev;
119 static int tegra_channel_buffer_prepare(struct vb2_buffer *vb)
121 struct tegra_vi_channel *chan = vb2_get_drv_priv(vb->vb2_queue);
122 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
123 struct tegra_channel_buffer *buf = to_tegra_channel_buffer(vbuf);
124 unsigned long size = chan->format.sizeimage;
126 if (vb2_plane_size(vb, 0) < size) {
127 v4l2_err(chan->video.v4l2_dev,
128 "buffer too small (%lu < %lu)\n",
129 vb2_plane_size(vb, 0), size);
133 vb2_set_plane_payload(vb, 0, size);
135 buf->addr = vb2_dma_contig_plane_dma_addr(vb, 0);
140 static void tegra_channel_buffer_queue(struct vb2_buffer *vb)
142 struct tegra_vi_channel *chan = vb2_get_drv_priv(vb->vb2_queue);
143 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
144 struct tegra_channel_buffer *buf = to_tegra_channel_buffer(vbuf);
146 /* put buffer into the capture queue */
147 spin_lock(&chan->start_lock);
148 list_add_tail(&buf->queue, &chan->capture);
149 spin_unlock(&chan->start_lock);
151 /* wait up kthread for capture */
152 wake_up_interruptible(&chan->start_wait);
156 tegra_channel_get_remote_csi_subdev(struct tegra_vi_channel *chan)
158 struct media_pad *pad;
160 pad = media_entity_remote_pad(&chan->pad);
164 return media_entity_to_v4l2_subdev(pad->entity);
168 tegra_channel_get_remote_source_subdev(struct tegra_vi_channel *chan)
170 struct media_pad *pad;
171 struct v4l2_subdev *subdev;
172 struct media_entity *entity;
174 subdev = tegra_channel_get_remote_csi_subdev(chan);
178 pad = &subdev->entity.pads[0];
179 while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
180 pad = media_entity_remote_pad(pad);
181 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
183 entity = pad->entity;
184 pad = &entity->pads[0];
185 subdev = media_entity_to_v4l2_subdev(entity);
191 static int tegra_channel_enable_stream(struct tegra_vi_channel *chan)
193 struct v4l2_subdev *csi_subdev, *src_subdev;
194 struct tegra_csi_channel *csi_chan;
198 * Tegra CSI receiver can detect the first LP to HS transition.
199 * So, start the CSI stream-on prior to sensor stream-on and
200 * vice-versa for stream-off.
202 csi_subdev = tegra_channel_get_remote_csi_subdev(chan);
203 ret = v4l2_subdev_call(csi_subdev, video, s_stream, true);
204 if (ret < 0 && ret != -ENOIOCTLCMD)
207 if (IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
210 csi_chan = v4l2_get_subdevdata(csi_subdev);
212 * TRM has incorrectly documented to wait for done status from
213 * calibration logic after CSI interface power on.
214 * As per the design, calibration results are latched and applied
215 * to the pads only when the link is in LP11 state which will happen
216 * during the sensor stream-on.
217 * CSI subdev stream-on triggers start of MIPI pads calibration.
218 * Wait for calibration to finish here after sensor subdev stream-on.
220 src_subdev = tegra_channel_get_remote_source_subdev(chan);
221 ret = v4l2_subdev_call(src_subdev, video, s_stream, true);
222 err = tegra_mipi_finish_calibration(csi_chan->mipi);
224 if (ret < 0 && ret != -ENOIOCTLCMD)
225 goto err_disable_csi_stream;
228 dev_warn(csi_chan->csi->dev,
229 "MIPI calibration failed: %d\n", err);
233 err_disable_csi_stream:
234 v4l2_subdev_call(csi_subdev, video, s_stream, false);
238 static int tegra_channel_disable_stream(struct tegra_vi_channel *chan)
240 struct v4l2_subdev *subdev;
244 * Stream-off subdevices in reverse order to stream-on.
245 * Remote source subdev in TPG mode is same as CSI subdev.
247 subdev = tegra_channel_get_remote_source_subdev(chan);
248 ret = v4l2_subdev_call(subdev, video, s_stream, false);
249 if (ret < 0 && ret != -ENOIOCTLCMD)
252 if (IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
255 subdev = tegra_channel_get_remote_csi_subdev(chan);
256 ret = v4l2_subdev_call(subdev, video, s_stream, false);
257 if (ret < 0 && ret != -ENOIOCTLCMD)
263 int tegra_channel_set_stream(struct tegra_vi_channel *chan, bool on)
268 ret = tegra_channel_enable_stream(chan);
270 ret = tegra_channel_disable_stream(chan);
275 void tegra_channel_release_buffers(struct tegra_vi_channel *chan,
276 enum vb2_buffer_state state)
278 struct tegra_channel_buffer *buf, *nbuf;
280 spin_lock(&chan->start_lock);
281 list_for_each_entry_safe(buf, nbuf, &chan->capture, queue) {
282 vb2_buffer_done(&buf->buf.vb2_buf, state);
283 list_del(&buf->queue);
285 spin_unlock(&chan->start_lock);
287 spin_lock(&chan->done_lock);
288 list_for_each_entry_safe(buf, nbuf, &chan->done, queue) {
289 vb2_buffer_done(&buf->buf.vb2_buf, state);
290 list_del(&buf->queue);
292 spin_unlock(&chan->done_lock);
295 static int tegra_channel_start_streaming(struct vb2_queue *vq, u32 count)
297 struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
300 ret = pm_runtime_get_sync(chan->vi->dev);
302 dev_err(chan->vi->dev, "failed to get runtime PM: %d\n", ret);
303 pm_runtime_put_noidle(chan->vi->dev);
307 ret = chan->vi->ops->vi_start_streaming(vq, count);
309 pm_runtime_put(chan->vi->dev);
314 static void tegra_channel_stop_streaming(struct vb2_queue *vq)
316 struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
318 chan->vi->ops->vi_stop_streaming(vq);
319 pm_runtime_put(chan->vi->dev);
322 static const struct vb2_ops tegra_channel_queue_qops = {
323 .queue_setup = tegra_channel_queue_setup,
324 .buf_prepare = tegra_channel_buffer_prepare,
325 .buf_queue = tegra_channel_buffer_queue,
326 .wait_prepare = vb2_ops_wait_prepare,
327 .wait_finish = vb2_ops_wait_finish,
328 .start_streaming = tegra_channel_start_streaming,
329 .stop_streaming = tegra_channel_stop_streaming,
333 * V4L2 ioctl operations
335 static int tegra_channel_querycap(struct file *file, void *fh,
336 struct v4l2_capability *cap)
338 struct tegra_vi_channel *chan = video_drvdata(file);
340 strscpy(cap->driver, "tegra-video", sizeof(cap->driver));
341 strscpy(cap->card, chan->video.name, sizeof(cap->card));
342 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
343 dev_name(chan->vi->dev));
348 static int tegra_channel_g_parm(struct file *file, void *fh,
349 struct v4l2_streamparm *a)
351 struct tegra_vi_channel *chan = video_drvdata(file);
352 struct v4l2_subdev *subdev;
354 subdev = tegra_channel_get_remote_source_subdev(chan);
355 return v4l2_g_parm_cap(&chan->video, subdev, a);
358 static int tegra_channel_s_parm(struct file *file, void *fh,
359 struct v4l2_streamparm *a)
361 struct tegra_vi_channel *chan = video_drvdata(file);
362 struct v4l2_subdev *subdev;
364 subdev = tegra_channel_get_remote_source_subdev(chan);
365 return v4l2_s_parm_cap(&chan->video, subdev, a);
368 static int tegra_channel_enum_framesizes(struct file *file, void *fh,
369 struct v4l2_frmsizeenum *sizes)
372 struct tegra_vi_channel *chan = video_drvdata(file);
373 struct v4l2_subdev *subdev;
374 const struct tegra_video_format *fmtinfo;
375 struct v4l2_subdev_frame_size_enum fse = {
376 .index = sizes->index,
377 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
380 fmtinfo = tegra_get_format_by_fourcc(chan->vi, sizes->pixel_format);
384 fse.code = fmtinfo->code;
386 subdev = tegra_channel_get_remote_source_subdev(chan);
387 ret = v4l2_subdev_call(subdev, pad, enum_frame_size, NULL, &fse);
391 sizes->type = V4L2_FRMSIZE_TYPE_DISCRETE;
392 sizes->discrete.width = fse.max_width;
393 sizes->discrete.height = fse.max_height;
398 static int tegra_channel_enum_frameintervals(struct file *file, void *fh,
399 struct v4l2_frmivalenum *ivals)
402 struct tegra_vi_channel *chan = video_drvdata(file);
403 struct v4l2_subdev *subdev;
404 const struct tegra_video_format *fmtinfo;
405 struct v4l2_subdev_frame_interval_enum fie = {
406 .index = ivals->index,
407 .width = ivals->width,
408 .height = ivals->height,
409 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
412 fmtinfo = tegra_get_format_by_fourcc(chan->vi, ivals->pixel_format);
416 fie.code = fmtinfo->code;
418 subdev = tegra_channel_get_remote_source_subdev(chan);
419 ret = v4l2_subdev_call(subdev, pad, enum_frame_interval, NULL, &fie);
423 ivals->type = V4L2_FRMIVAL_TYPE_DISCRETE;
424 ivals->discrete.numerator = fie.interval.numerator;
425 ivals->discrete.denominator = fie.interval.denominator;
430 static int tegra_channel_enum_format(struct file *file, void *fh,
431 struct v4l2_fmtdesc *f)
433 struct tegra_vi_channel *chan = video_drvdata(file);
434 unsigned int index = 0, i;
435 unsigned long *fmts_bitmap = chan->tpg_fmts_bitmap;
437 if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
438 fmts_bitmap = chan->fmts_bitmap;
440 if (f->index >= bitmap_weight(fmts_bitmap, MAX_FORMAT_NUM))
443 for (i = 0; i < f->index + 1; i++, index++)
444 index = find_next_bit(fmts_bitmap, MAX_FORMAT_NUM, index);
446 f->pixelformat = tegra_get_format_fourcc_by_idx(chan->vi, index - 1);
451 static int tegra_channel_get_format(struct file *file, void *fh,
452 struct v4l2_format *format)
454 struct tegra_vi_channel *chan = video_drvdata(file);
456 format->fmt.pix = chan->format;
461 static void tegra_channel_fmt_align(struct tegra_vi_channel *chan,
462 struct v4l2_pix_format *pix,
465 unsigned int min_bpl;
466 unsigned int max_bpl;
470 * The transfer alignment requirements are expressed in bytes.
471 * Clamp the requested width and height to the limits.
473 pix->width = clamp(pix->width, TEGRA_MIN_WIDTH, TEGRA_MAX_WIDTH);
474 pix->height = clamp(pix->height, TEGRA_MIN_HEIGHT, TEGRA_MAX_HEIGHT);
476 /* Clamp the requested bytes per line value. If the maximum bytes per
477 * line value is zero, the module doesn't support user configurable
478 * line sizes. Override the requested value with the minimum in that
481 min_bpl = pix->width * bpp;
482 max_bpl = rounddown(TEGRA_MAX_WIDTH, SURFACE_ALIGN_BYTES);
483 bpl = roundup(pix->bytesperline, SURFACE_ALIGN_BYTES);
485 pix->bytesperline = clamp(bpl, min_bpl, max_bpl);
486 pix->sizeimage = pix->bytesperline * pix->height;
489 static int __tegra_channel_try_format(struct tegra_vi_channel *chan,
490 struct v4l2_pix_format *pix)
492 const struct tegra_video_format *fmtinfo;
493 struct v4l2_subdev *subdev;
494 struct v4l2_subdev_format fmt;
495 struct v4l2_subdev_pad_config *pad_cfg;
496 struct v4l2_subdev_frame_size_enum fse = {
497 .which = V4L2_SUBDEV_FORMAT_TRY,
499 struct v4l2_subdev_selection sdsel = {
500 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
501 .target = V4L2_SEL_TGT_CROP_BOUNDS,
505 subdev = tegra_channel_get_remote_source_subdev(chan);
509 pad_cfg = v4l2_subdev_alloc_pad_config(subdev);
513 * Retrieve the format information and if requested format isn't
514 * supported, keep the current format.
516 fmtinfo = tegra_get_format_by_fourcc(chan->vi, pix->pixelformat);
518 pix->pixelformat = chan->format.pixelformat;
519 pix->colorspace = chan->format.colorspace;
520 fmtinfo = tegra_get_format_by_fourcc(chan->vi,
524 pix->field = V4L2_FIELD_NONE;
525 fmt.which = V4L2_SUBDEV_FORMAT_TRY;
527 v4l2_fill_mbus_format(&fmt.format, pix, fmtinfo->code);
530 * Attempt to obtain the format size from subdev.
531 * If not available, try to get crop boundary from subdev.
533 fse.code = fmtinfo->code;
534 ret = v4l2_subdev_call(subdev, pad, enum_frame_size, pad_cfg, &fse);
536 ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
539 pad_cfg->try_crop.width = sdsel.r.width;
540 pad_cfg->try_crop.height = sdsel.r.height;
542 pad_cfg->try_crop.width = fse.max_width;
543 pad_cfg->try_crop.height = fse.max_height;
546 ret = v4l2_subdev_call(subdev, pad, set_fmt, pad_cfg, &fmt);
550 v4l2_fill_pix_format(pix, &fmt.format);
551 tegra_channel_fmt_align(chan, pix, fmtinfo->bpp);
553 v4l2_subdev_free_pad_config(pad_cfg);
558 static int tegra_channel_try_format(struct file *file, void *fh,
559 struct v4l2_format *format)
561 struct tegra_vi_channel *chan = video_drvdata(file);
563 return __tegra_channel_try_format(chan, &format->fmt.pix);
566 static int tegra_channel_set_format(struct file *file, void *fh,
567 struct v4l2_format *format)
569 struct tegra_vi_channel *chan = video_drvdata(file);
570 const struct tegra_video_format *fmtinfo;
571 struct v4l2_subdev_format fmt;
572 struct v4l2_subdev *subdev;
573 struct v4l2_pix_format *pix = &format->fmt.pix;
576 if (vb2_is_busy(&chan->queue))
579 /* get supported format by try_fmt */
580 ret = __tegra_channel_try_format(chan, pix);
584 fmtinfo = tegra_get_format_by_fourcc(chan->vi, pix->pixelformat);
586 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
588 v4l2_fill_mbus_format(&fmt.format, pix, fmtinfo->code);
589 subdev = tegra_channel_get_remote_source_subdev(chan);
590 ret = v4l2_subdev_call(subdev, pad, set_fmt, NULL, &fmt);
594 v4l2_fill_pix_format(pix, &fmt.format);
595 tegra_channel_fmt_align(chan, pix, fmtinfo->bpp);
598 chan->fmtinfo = fmtinfo;
603 static int tegra_channel_set_subdev_active_fmt(struct tegra_vi_channel *chan)
606 struct v4l2_subdev *subdev;
607 struct v4l2_subdev_format fmt = {
608 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
612 * Initialize channel format to the sub-device active format if there
613 * is corresponding match in the Tegra supported video formats.
615 subdev = tegra_channel_get_remote_source_subdev(chan);
616 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
620 index = tegra_get_format_idx_by_code(chan->vi, fmt.format.code, 0);
624 chan->fmtinfo = &chan->vi->soc->video_formats[index];
625 v4l2_fill_pix_format(&chan->format, &fmt.format);
626 chan->format.pixelformat = chan->fmtinfo->fourcc;
627 chan->format.bytesperline = chan->format.width * chan->fmtinfo->bpp;
628 chan->format.sizeimage = chan->format.bytesperline *
630 tegra_channel_fmt_align(chan, &chan->format, chan->fmtinfo->bpp);
635 static int tegra_channel_g_selection(struct file *file, void *priv,
636 struct v4l2_selection *sel)
638 struct tegra_vi_channel *chan = video_drvdata(file);
639 struct v4l2_subdev *subdev;
640 struct v4l2_subdev_format fmt = {
641 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
643 struct v4l2_subdev_selection sdsel = {
644 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
645 .target = sel->target,
649 subdev = tegra_channel_get_remote_source_subdev(chan);
650 if (!v4l2_subdev_has_op(subdev, pad, get_selection))
653 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
656 * Try the get selection operation and fallback to get format if not
659 ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
662 if (ret != -ENOIOCTLCMD)
665 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
671 sel->r.width = fmt.format.width;
672 sel->r.height = fmt.format.height;
677 static int tegra_channel_s_selection(struct file *file, void *fh,
678 struct v4l2_selection *sel)
680 struct tegra_vi_channel *chan = video_drvdata(file);
681 struct v4l2_subdev *subdev;
683 struct v4l2_subdev_selection sdsel = {
684 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
685 .target = sel->target,
690 subdev = tegra_channel_get_remote_source_subdev(chan);
691 if (!v4l2_subdev_has_op(subdev, pad, set_selection))
694 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
697 if (vb2_is_busy(&chan->queue))
700 ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel);
704 * Subdev active format resolution may have changed during
705 * set selection operation. So, update channel format to
706 * the sub-device active format.
708 return tegra_channel_set_subdev_active_fmt(chan);
714 static int tegra_channel_enum_input(struct file *file, void *fh,
715 struct v4l2_input *inp)
717 struct tegra_vi_channel *chan = video_drvdata(file);
718 struct v4l2_subdev *subdev;
723 inp->type = V4L2_INPUT_TYPE_CAMERA;
724 subdev = tegra_channel_get_remote_source_subdev(chan);
725 strscpy(inp->name, subdev->name, sizeof(inp->name));
730 static int tegra_channel_g_input(struct file *file, void *priv,
738 static int tegra_channel_s_input(struct file *file, void *priv,
747 static const struct v4l2_ioctl_ops tegra_channel_ioctl_ops = {
748 .vidioc_querycap = tegra_channel_querycap,
749 .vidioc_g_parm = tegra_channel_g_parm,
750 .vidioc_s_parm = tegra_channel_s_parm,
751 .vidioc_enum_framesizes = tegra_channel_enum_framesizes,
752 .vidioc_enum_frameintervals = tegra_channel_enum_frameintervals,
753 .vidioc_enum_fmt_vid_cap = tegra_channel_enum_format,
754 .vidioc_g_fmt_vid_cap = tegra_channel_get_format,
755 .vidioc_s_fmt_vid_cap = tegra_channel_set_format,
756 .vidioc_try_fmt_vid_cap = tegra_channel_try_format,
757 .vidioc_enum_input = tegra_channel_enum_input,
758 .vidioc_g_input = tegra_channel_g_input,
759 .vidioc_s_input = tegra_channel_s_input,
760 .vidioc_reqbufs = vb2_ioctl_reqbufs,
761 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
762 .vidioc_querybuf = vb2_ioctl_querybuf,
763 .vidioc_qbuf = vb2_ioctl_qbuf,
764 .vidioc_dqbuf = vb2_ioctl_dqbuf,
765 .vidioc_create_bufs = vb2_ioctl_create_bufs,
766 .vidioc_expbuf = vb2_ioctl_expbuf,
767 .vidioc_streamon = vb2_ioctl_streamon,
768 .vidioc_streamoff = vb2_ioctl_streamoff,
769 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
770 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
771 .vidioc_g_selection = tegra_channel_g_selection,
772 .vidioc_s_selection = tegra_channel_s_selection,
776 * V4L2 file operations
778 static const struct v4l2_file_operations tegra_channel_fops = {
779 .owner = THIS_MODULE,
780 .unlocked_ioctl = video_ioctl2,
781 .open = v4l2_fh_open,
782 .release = vb2_fop_release,
783 .read = vb2_fop_read,
784 .poll = vb2_fop_poll,
785 .mmap = vb2_fop_mmap,
789 * V4L2 control operations
791 #if IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)
792 static int vi_s_ctrl(struct v4l2_ctrl *ctrl)
794 struct tegra_vi_channel *chan = container_of(ctrl->handler,
795 struct tegra_vi_channel,
799 case V4L2_CID_TEST_PATTERN:
800 /* pattern change takes effect on next stream */
801 chan->pg_mode = ctrl->val + 1;
810 static const struct v4l2_ctrl_ops vi_ctrl_ops = {
814 static const char *const vi_pattern_strings[] = {
815 "Black/White Direct Mode",
820 static int tegra_channel_setup_ctrl_handler(struct tegra_vi_channel *chan)
824 #if IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)
825 /* add test pattern control handler to v4l2 device */
826 v4l2_ctrl_new_std_menu_items(&chan->ctrl_handler, &vi_ctrl_ops,
827 V4L2_CID_TEST_PATTERN,
828 ARRAY_SIZE(vi_pattern_strings) - 1,
829 0, 0, vi_pattern_strings);
830 if (chan->ctrl_handler.error) {
831 dev_err(chan->vi->dev, "failed to add TPG ctrl handler: %d\n",
832 chan->ctrl_handler.error);
833 v4l2_ctrl_handler_free(&chan->ctrl_handler);
834 return chan->ctrl_handler.error;
837 struct v4l2_subdev *subdev;
839 subdev = tegra_channel_get_remote_source_subdev(chan);
843 ret = v4l2_ctrl_add_handler(&chan->ctrl_handler, subdev->ctrl_handler,
846 dev_err(chan->vi->dev,
847 "failed to add subdev %s ctrl handler: %d\n",
849 v4l2_ctrl_handler_free(&chan->ctrl_handler);
854 /* setup the controls */
855 ret = v4l2_ctrl_handler_setup(&chan->ctrl_handler);
857 dev_err(chan->vi->dev,
858 "failed to setup v4l2 ctrl handler: %d\n", ret);
865 /* VI only support 2 formats in TPG mode */
866 static void vi_tpg_fmts_bitmap_init(struct tegra_vi_channel *chan)
870 bitmap_zero(chan->tpg_fmts_bitmap, MAX_FORMAT_NUM);
872 index = tegra_get_format_idx_by_code(chan->vi,
873 MEDIA_BUS_FMT_SRGGB10_1X10, 0);
874 bitmap_set(chan->tpg_fmts_bitmap, index, 1);
876 index = tegra_get_format_idx_by_code(chan->vi,
877 MEDIA_BUS_FMT_RGB888_1X32_PADHI,
879 bitmap_set(chan->tpg_fmts_bitmap, index, 1);
882 static int vi_fmts_bitmap_init(struct tegra_vi_channel *chan)
884 int index, ret, match_code = 0;
885 struct v4l2_subdev *subdev;
886 struct v4l2_subdev_mbus_code_enum code = {
887 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
890 bitmap_zero(chan->fmts_bitmap, MAX_FORMAT_NUM);
893 * Set the bitmap bits based on all the matched formats between the
894 * available media bus formats of sub-device and the pre-defined Tegra
895 * supported video formats.
897 subdev = tegra_channel_get_remote_source_subdev(chan);
899 ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
904 index = tegra_get_format_idx_by_code(chan->vi, code.code, 0);
906 bitmap_set(chan->fmts_bitmap, index, 1);
908 match_code = code.code;
909 /* look for other formats with same mbus code */
910 index = tegra_get_format_idx_by_code(chan->vi,
919 * Set the bitmap bit corresponding to default tegra video format if
920 * there are no matched formats.
923 match_code = tegra_default_format.code;
924 index = tegra_get_format_idx_by_code(chan->vi, match_code, 0);
925 if (WARN_ON(index < 0))
928 bitmap_set(chan->fmts_bitmap, index, 1);
931 /* initialize channel format to the sub-device active format */
932 tegra_channel_set_subdev_active_fmt(chan);
937 static void tegra_channel_cleanup(struct tegra_vi_channel *chan)
939 v4l2_ctrl_handler_free(&chan->ctrl_handler);
940 media_entity_cleanup(&chan->video.entity);
941 host1x_syncpt_free(chan->mw_ack_sp);
942 host1x_syncpt_free(chan->frame_start_sp);
943 mutex_destroy(&chan->video_lock);
946 void tegra_channels_cleanup(struct tegra_vi *vi)
948 struct tegra_vi_channel *chan, *tmp;
953 list_for_each_entry_safe(chan, tmp, &vi->vi_chans, list) {
954 tegra_channel_cleanup(chan);
955 list_del(&chan->list);
960 static int tegra_channel_init(struct tegra_vi_channel *chan)
962 struct tegra_vi *vi = chan->vi;
963 struct tegra_video_device *vid = dev_get_drvdata(vi->client.host);
964 unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
967 mutex_init(&chan->video_lock);
968 INIT_LIST_HEAD(&chan->capture);
969 INIT_LIST_HEAD(&chan->done);
970 spin_lock_init(&chan->start_lock);
971 spin_lock_init(&chan->done_lock);
972 spin_lock_init(&chan->sp_incr_lock);
973 init_waitqueue_head(&chan->start_wait);
974 init_waitqueue_head(&chan->done_wait);
976 /* initialize the video format */
977 chan->fmtinfo = &tegra_default_format;
978 chan->format.pixelformat = chan->fmtinfo->fourcc;
979 chan->format.colorspace = V4L2_COLORSPACE_SRGB;
980 chan->format.field = V4L2_FIELD_NONE;
981 chan->format.width = TEGRA_DEF_WIDTH;
982 chan->format.height = TEGRA_DEF_HEIGHT;
983 chan->format.bytesperline = TEGRA_DEF_WIDTH * chan->fmtinfo->bpp;
984 chan->format.sizeimage = chan->format.bytesperline * TEGRA_DEF_HEIGHT;
985 tegra_channel_fmt_align(chan, &chan->format, chan->fmtinfo->bpp);
987 chan->frame_start_sp = host1x_syncpt_request(&vi->client, flags);
988 if (!chan->frame_start_sp) {
989 dev_err(vi->dev, "failed to request frame start syncpoint\n");
993 chan->mw_ack_sp = host1x_syncpt_request(&vi->client, flags);
994 if (!chan->mw_ack_sp) {
995 dev_err(vi->dev, "failed to request memory ack syncpoint\n");
1000 /* initialize the media entity */
1001 chan->pad.flags = MEDIA_PAD_FL_SINK;
1002 ret = media_entity_pads_init(&chan->video.entity, 1, &chan->pad);
1005 "failed to initialize media entity: %d\n", ret);
1006 goto free_mw_ack_syncpt;
1009 ret = v4l2_ctrl_handler_init(&chan->ctrl_handler, MAX_CID_CONTROLS);
1010 if (chan->ctrl_handler.error) {
1012 "failed to initialize v4l2 ctrl handler: %d\n", ret);
1016 /* initialize the video_device */
1017 chan->video.fops = &tegra_channel_fops;
1018 chan->video.v4l2_dev = &vid->v4l2_dev;
1019 chan->video.release = video_device_release_empty;
1020 chan->video.queue = &chan->queue;
1021 snprintf(chan->video.name, sizeof(chan->video.name), "%s-%s-%u",
1022 dev_name(vi->dev), "output", chan->portno);
1023 chan->video.vfl_type = VFL_TYPE_VIDEO;
1024 chan->video.vfl_dir = VFL_DIR_RX;
1025 chan->video.ioctl_ops = &tegra_channel_ioctl_ops;
1026 chan->video.ctrl_handler = &chan->ctrl_handler;
1027 chan->video.lock = &chan->video_lock;
1028 chan->video.device_caps = V4L2_CAP_VIDEO_CAPTURE |
1029 V4L2_CAP_STREAMING |
1031 video_set_drvdata(&chan->video, chan);
1033 chan->queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1034 chan->queue.io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
1035 chan->queue.lock = &chan->video_lock;
1036 chan->queue.drv_priv = chan;
1037 chan->queue.buf_struct_size = sizeof(struct tegra_channel_buffer);
1038 chan->queue.ops = &tegra_channel_queue_qops;
1039 chan->queue.mem_ops = &vb2_dma_contig_memops;
1040 chan->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1041 chan->queue.min_buffers_needed = 2;
1042 chan->queue.dev = vi->dev;
1043 ret = vb2_queue_init(&chan->queue);
1045 dev_err(vi->dev, "failed to initialize vb2 queue: %d\n", ret);
1046 goto free_v4l2_ctrl_hdl;
1049 if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
1050 v4l2_async_notifier_init(&chan->notifier);
1055 v4l2_ctrl_handler_free(&chan->ctrl_handler);
1057 media_entity_cleanup(&chan->video.entity);
1059 host1x_syncpt_free(chan->mw_ack_sp);
1061 host1x_syncpt_free(chan->frame_start_sp);
1065 static int tegra_vi_channel_alloc(struct tegra_vi *vi, unsigned int port_num,
1066 struct device_node *node)
1068 struct tegra_vi_channel *chan;
1071 * Do not use devm_kzalloc as memory is freed immediately
1072 * when device instance is unbound but application might still
1073 * be holding the device node open. Channel memory allocated
1074 * with kzalloc is freed during video device release callback.
1076 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
1081 chan->portno = port_num;
1082 chan->of_node = node;
1083 list_add_tail(&chan->list, &vi->vi_chans);
1088 static int tegra_vi_tpg_channels_alloc(struct tegra_vi *vi)
1090 unsigned int port_num;
1091 unsigned int nchannels = vi->soc->vi_max_channels;
1094 for (port_num = 0; port_num < nchannels; port_num++) {
1095 ret = tegra_vi_channel_alloc(vi, port_num, vi->dev->of_node);
1103 static int tegra_vi_channels_alloc(struct tegra_vi *vi)
1105 struct device_node *node = vi->dev->of_node;
1106 struct device_node *ep = NULL;
1107 struct device_node *ports;
1108 struct device_node *port;
1109 unsigned int port_num;
1112 ports = of_get_child_by_name(node, "ports");
1116 for_each_child_of_node(ports, port) {
1117 if (!of_node_name_eq(port, "port"))
1120 ret = of_property_read_u32(port, "reg", &port_num);
1124 if (port_num > vi->soc->vi_max_channels) {
1125 dev_err(vi->dev, "invalid port num %d for %pOF\n",
1132 ep = of_get_child_by_name(port, "endpoint");
1137 ret = tegra_vi_channel_alloc(vi, port_num, port);
1149 static int tegra_vi_channels_init(struct tegra_vi *vi)
1151 struct tegra_vi_channel *chan;
1154 list_for_each_entry(chan, &vi->vi_chans, list) {
1155 ret = tegra_channel_init(chan);
1158 "failed to initialize channel-%d: %d\n",
1167 list_for_each_entry_continue_reverse(chan, &vi->vi_chans, list)
1168 tegra_channel_cleanup(chan);
1173 void tegra_v4l2_nodes_cleanup_tpg(struct tegra_video_device *vid)
1175 struct tegra_vi *vi = vid->vi;
1176 struct tegra_csi *csi = vid->csi;
1177 struct tegra_csi_channel *csi_chan;
1178 struct tegra_vi_channel *chan;
1180 list_for_each_entry(chan, &vi->vi_chans, list)
1181 vb2_video_unregister_device(&chan->video);
1183 list_for_each_entry(csi_chan, &csi->csi_chans, list)
1184 v4l2_device_unregister_subdev(&csi_chan->subdev);
1187 int tegra_v4l2_nodes_setup_tpg(struct tegra_video_device *vid)
1189 struct tegra_vi *vi = vid->vi;
1190 struct tegra_csi *csi = vid->csi;
1191 struct tegra_vi_channel *vi_chan;
1192 struct tegra_csi_channel *csi_chan;
1193 u32 link_flags = MEDIA_LNK_FL_ENABLED;
1199 csi_chan = list_first_entry(&csi->csi_chans,
1200 struct tegra_csi_channel, list);
1202 list_for_each_entry(vi_chan, &vi->vi_chans, list) {
1203 struct media_entity *source = &csi_chan->subdev.entity;
1204 struct media_entity *sink = &vi_chan->video.entity;
1205 struct media_pad *source_pad = csi_chan->pads;
1206 struct media_pad *sink_pad = &vi_chan->pad;
1208 ret = v4l2_device_register_subdev(&vid->v4l2_dev,
1212 "failed to register subdev: %d\n", ret);
1216 ret = video_register_device(&vi_chan->video,
1217 VFL_TYPE_VIDEO, -1);
1220 "failed to register video device: %d\n", ret);
1224 dev_dbg(vi->dev, "creating %s:%u -> %s:%u link\n",
1225 source->name, source_pad->index,
1226 sink->name, sink_pad->index);
1228 ret = media_create_pad_link(source, source_pad->index,
1229 sink, sink_pad->index,
1233 "failed to create %s:%u -> %s:%u link: %d\n",
1234 source->name, source_pad->index,
1235 sink->name, sink_pad->index, ret);
1239 ret = tegra_channel_setup_ctrl_handler(vi_chan);
1243 v4l2_set_subdev_hostdata(&csi_chan->subdev, vi_chan);
1244 vi_tpg_fmts_bitmap_init(vi_chan);
1245 csi_chan = list_next_entry(csi_chan, list);
1251 tegra_v4l2_nodes_cleanup_tpg(vid);
1255 static int __maybe_unused vi_runtime_resume(struct device *dev)
1257 struct tegra_vi *vi = dev_get_drvdata(dev);
1260 ret = regulator_enable(vi->vdd);
1262 dev_err(dev, "failed to enable VDD supply: %d\n", ret);
1266 ret = clk_set_rate(vi->clk, vi->soc->vi_max_clk_hz);
1268 dev_err(dev, "failed to set vi clock rate: %d\n", ret);
1272 ret = clk_prepare_enable(vi->clk);
1274 dev_err(dev, "failed to enable vi clock: %d\n", ret);
1281 regulator_disable(vi->vdd);
1285 static int __maybe_unused vi_runtime_suspend(struct device *dev)
1287 struct tegra_vi *vi = dev_get_drvdata(dev);
1289 clk_disable_unprepare(vi->clk);
1291 regulator_disable(vi->vdd);
1299 static struct tegra_vi_graph_entity *
1300 tegra_vi_graph_find_entity(struct tegra_vi_channel *chan,
1301 const struct fwnode_handle *fwnode)
1303 struct tegra_vi_graph_entity *entity;
1304 struct v4l2_async_subdev *asd;
1306 list_for_each_entry(asd, &chan->notifier.asd_list, asd_list) {
1307 entity = to_tegra_vi_graph_entity(asd);
1308 if (entity->asd.match.fwnode == fwnode)
1315 static int tegra_vi_graph_build(struct tegra_vi_channel *chan,
1316 struct tegra_vi_graph_entity *entity)
1318 struct tegra_vi *vi = chan->vi;
1319 struct tegra_vi_graph_entity *ent;
1320 struct fwnode_handle *ep = NULL;
1321 struct v4l2_fwnode_link link;
1322 struct media_entity *local = entity->entity;
1323 struct media_entity *remote;
1324 struct media_pad *local_pad;
1325 struct media_pad *remote_pad;
1326 u32 link_flags = MEDIA_LNK_FL_ENABLED;
1329 dev_dbg(vi->dev, "creating links for entity %s\n", local->name);
1332 ep = fwnode_graph_get_next_endpoint(entity->asd.match.fwnode,
1337 ret = v4l2_fwnode_parse_link(ep, &link);
1339 dev_err(vi->dev, "failed to parse link for %pOF: %d\n",
1340 to_of_node(ep), ret);
1344 if (link.local_port >= local->num_pads) {
1345 dev_err(vi->dev, "invalid port number %u on %pOF\n",
1346 link.local_port, to_of_node(link.local_node));
1347 v4l2_fwnode_put_link(&link);
1352 local_pad = &local->pads[link.local_port];
1353 /* Remote node is vi node. So use channel video entity and pad
1356 if (link.remote_node == of_fwnode_handle(vi->dev->of_node)) {
1357 remote = &chan->video.entity;
1358 remote_pad = &chan->pad;
1363 * Skip sink ports, they will be processed from the other end
1366 if (local_pad->flags & MEDIA_PAD_FL_SINK) {
1367 dev_dbg(vi->dev, "skipping sink port %pOF:%u\n",
1368 to_of_node(link.local_node), link.local_port);
1369 v4l2_fwnode_put_link(&link);
1373 /* find the remote entity from notifier list */
1374 ent = tegra_vi_graph_find_entity(chan, link.remote_node);
1376 dev_err(vi->dev, "no entity found for %pOF\n",
1377 to_of_node(link.remote_node));
1378 v4l2_fwnode_put_link(&link);
1383 remote = ent->entity;
1384 if (link.remote_port >= remote->num_pads) {
1385 dev_err(vi->dev, "invalid port number %u on %pOF\n",
1387 to_of_node(link.remote_node));
1388 v4l2_fwnode_put_link(&link);
1393 remote_pad = &remote->pads[link.remote_port];
1396 dev_dbg(vi->dev, "creating %s:%u -> %s:%u link\n",
1397 local->name, local_pad->index,
1398 remote->name, remote_pad->index);
1400 ret = media_create_pad_link(local, local_pad->index,
1401 remote, remote_pad->index,
1403 v4l2_fwnode_put_link(&link);
1406 "failed to create %s:%u -> %s:%u link: %d\n",
1407 local->name, local_pad->index,
1408 remote->name, remote_pad->index, ret);
1413 fwnode_handle_put(ep);
1417 static int tegra_vi_graph_notify_complete(struct v4l2_async_notifier *notifier)
1419 struct tegra_vi_graph_entity *entity;
1420 struct v4l2_async_subdev *asd;
1421 struct v4l2_subdev *subdev;
1422 struct tegra_vi_channel *chan;
1423 struct tegra_vi *vi;
1426 chan = container_of(notifier, struct tegra_vi_channel, notifier);
1429 dev_dbg(vi->dev, "notify complete, all subdevs registered\n");
1432 * Video device node should be created at the end of all the device
1433 * related initialization/setup.
1434 * Current video_register_device() does both initialize and register
1435 * video device in same API.
1437 * TODO: Update v4l2-dev driver to split initialize and register into
1438 * separate APIs and then update Tegra video driver to do video device
1439 * initialize followed by all video device related setup and then
1440 * register the video device.
1442 ret = video_register_device(&chan->video, VFL_TYPE_VIDEO, -1);
1445 "failed to register video device: %d\n", ret);
1446 goto unregister_video;
1449 /* create links between the entities */
1450 list_for_each_entry(asd, &chan->notifier.asd_list, asd_list) {
1451 entity = to_tegra_vi_graph_entity(asd);
1452 ret = tegra_vi_graph_build(chan, entity);
1454 goto unregister_video;
1457 ret = tegra_channel_setup_ctrl_handler(chan);
1460 "failed to setup channel controls: %d\n", ret);
1461 goto unregister_video;
1464 ret = vi_fmts_bitmap_init(chan);
1467 "failed to initialize formats bitmap: %d\n", ret);
1468 goto unregister_video;
1471 subdev = tegra_channel_get_remote_csi_subdev(chan);
1475 "failed to get remote csi subdev: %d\n", ret);
1476 goto unregister_video;
1479 v4l2_set_subdev_hostdata(subdev, chan);
1484 vb2_video_unregister_device(&chan->video);
1488 static int tegra_vi_graph_notify_bound(struct v4l2_async_notifier *notifier,
1489 struct v4l2_subdev *subdev,
1490 struct v4l2_async_subdev *asd)
1492 struct tegra_vi_graph_entity *entity;
1493 struct tegra_vi *vi;
1494 struct tegra_vi_channel *chan;
1496 chan = container_of(notifier, struct tegra_vi_channel, notifier);
1500 * Locate the entity corresponding to the bound subdev and store the
1503 entity = tegra_vi_graph_find_entity(chan, subdev->fwnode);
1505 dev_err(vi->dev, "no entity for subdev %s\n", subdev->name);
1509 if (entity->subdev) {
1510 dev_err(vi->dev, "duplicate subdev for node %pOF\n",
1511 to_of_node(entity->asd.match.fwnode));
1515 dev_dbg(vi->dev, "subdev %s bound\n", subdev->name);
1516 entity->entity = &subdev->entity;
1517 entity->subdev = subdev;
1522 static const struct v4l2_async_notifier_operations tegra_vi_async_ops = {
1523 .bound = tegra_vi_graph_notify_bound,
1524 .complete = tegra_vi_graph_notify_complete,
1527 static int tegra_vi_graph_parse_one(struct tegra_vi_channel *chan,
1528 struct fwnode_handle *fwnode)
1530 struct tegra_vi *vi = chan->vi;
1531 struct fwnode_handle *ep = NULL;
1532 struct fwnode_handle *remote = NULL;
1533 struct v4l2_async_subdev *asd;
1534 struct device_node *node = NULL;
1537 dev_dbg(vi->dev, "parsing node %pOF\n", to_of_node(fwnode));
1539 /* parse all the remote entities and put them into the list */
1540 for_each_endpoint_of_node(to_of_node(fwnode), node) {
1541 ep = of_fwnode_handle(node);
1542 remote = fwnode_graph_get_remote_port_parent(ep);
1545 "remote device at %pOF not found\n", node);
1550 /* skip entities that are already processed */
1551 if (remote == dev_fwnode(vi->dev) ||
1552 tegra_vi_graph_find_entity(chan, remote)) {
1553 fwnode_handle_put(remote);
1557 asd = v4l2_async_notifier_add_fwnode_subdev(&chan->notifier,
1558 remote, sizeof(struct tegra_vi_graph_entity));
1562 "failed to add subdev to notifier: %d\n", ret);
1563 fwnode_handle_put(remote);
1567 ret = tegra_vi_graph_parse_one(chan, remote);
1569 fwnode_handle_put(remote);
1573 fwnode_handle_put(remote);
1579 dev_err(vi->dev, "failed parsing the graph: %d\n", ret);
1580 v4l2_async_notifier_cleanup(&chan->notifier);
1585 static int tegra_vi_graph_init(struct tegra_vi *vi)
1587 struct tegra_video_device *vid = dev_get_drvdata(vi->client.host);
1588 struct tegra_vi_channel *chan;
1589 struct fwnode_handle *fwnode = dev_fwnode(vi->dev);
1591 struct fwnode_handle *remote = NULL;
1594 * Walk the links to parse the full graph. Each channel will have
1595 * one endpoint of the composite node. Start by parsing the
1596 * composite node and parse the remote entities in turn.
1597 * Each channel will register v4l2 async notifier to make the graph
1598 * independent between the channels so we can the current channel
1599 * in case of something wrong during graph parsing and continue with
1602 list_for_each_entry(chan, &vi->vi_chans, list) {
1603 remote = fwnode_graph_get_remote_node(fwnode, chan->portno, 0);
1607 ret = tegra_vi_graph_parse_one(chan, remote);
1608 fwnode_handle_put(remote);
1609 if (ret < 0 || list_empty(&chan->notifier.asd_list))
1612 chan->notifier.ops = &tegra_vi_async_ops;
1613 ret = v4l2_async_notifier_register(&vid->v4l2_dev,
1617 "failed to register channel %d notifier: %d\n",
1619 v4l2_async_notifier_cleanup(&chan->notifier);
1626 static void tegra_vi_graph_cleanup(struct tegra_vi *vi)
1628 struct tegra_vi_channel *chan;
1630 list_for_each_entry(chan, &vi->vi_chans, list) {
1631 vb2_video_unregister_device(&chan->video);
1632 v4l2_async_notifier_unregister(&chan->notifier);
1633 v4l2_async_notifier_cleanup(&chan->notifier);
1637 static int tegra_vi_init(struct host1x_client *client)
1639 struct tegra_video_device *vid = dev_get_drvdata(client->host);
1640 struct tegra_vi *vi = host1x_client_to_vi(client);
1641 struct tegra_vi_channel *chan, *tmp;
1644 vid->media_dev.hw_revision = vi->soc->hw_revision;
1645 snprintf(vid->media_dev.bus_info, sizeof(vid->media_dev.bus_info),
1646 "platform:%s", dev_name(vi->dev));
1648 INIT_LIST_HEAD(&vi->vi_chans);
1650 if (IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
1651 ret = tegra_vi_tpg_channels_alloc(vi);
1653 ret = tegra_vi_channels_alloc(vi);
1656 "failed to allocate vi channels: %d\n", ret);
1660 ret = tegra_vi_channels_init(vi);
1666 if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)) {
1667 ret = tegra_vi_graph_init(vi);
1675 list_for_each_entry_safe(chan, tmp, &vi->vi_chans, list) {
1676 list_del(&chan->list);
1683 static int tegra_vi_exit(struct host1x_client *client)
1685 struct tegra_vi *vi = host1x_client_to_vi(client);
1688 * Do not cleanup the channels here as application might still be
1689 * holding video device nodes. Channels cleanup will happen during
1690 * v4l2_device release callback which gets called after all video
1691 * device nodes are released.
1694 if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
1695 tegra_vi_graph_cleanup(vi);
1700 static const struct host1x_client_ops vi_client_ops = {
1701 .init = tegra_vi_init,
1702 .exit = tegra_vi_exit,
1705 static int tegra_vi_probe(struct platform_device *pdev)
1707 struct tegra_vi *vi;
1710 vi = devm_kzalloc(&pdev->dev, sizeof(*vi), GFP_KERNEL);
1714 vi->iomem = devm_platform_ioremap_resource(pdev, 0);
1715 if (IS_ERR(vi->iomem))
1716 return PTR_ERR(vi->iomem);
1718 vi->soc = of_device_get_match_data(&pdev->dev);
1720 vi->clk = devm_clk_get(&pdev->dev, NULL);
1721 if (IS_ERR(vi->clk)) {
1722 ret = PTR_ERR(vi->clk);
1723 dev_err(&pdev->dev, "failed to get vi clock: %d\n", ret);
1727 vi->vdd = devm_regulator_get(&pdev->dev, "avdd-dsi-csi");
1728 if (IS_ERR(vi->vdd)) {
1729 ret = PTR_ERR(vi->vdd);
1730 dev_err(&pdev->dev, "failed to get VDD supply: %d\n", ret);
1734 if (!pdev->dev.pm_domain) {
1736 dev_warn(&pdev->dev, "PM domain is not attached: %d\n", ret);
1740 ret = devm_of_platform_populate(&pdev->dev);
1743 "failed to populate vi child device: %d\n", ret);
1747 vi->dev = &pdev->dev;
1748 vi->ops = vi->soc->ops;
1749 platform_set_drvdata(pdev, vi);
1750 pm_runtime_enable(&pdev->dev);
1752 /* initialize host1x interface */
1753 INIT_LIST_HEAD(&vi->client.list);
1754 vi->client.ops = &vi_client_ops;
1755 vi->client.dev = &pdev->dev;
1757 ret = host1x_client_register(&vi->client);
1760 "failed to register host1x client: %d\n", ret);
1767 pm_runtime_disable(&pdev->dev);
1771 static int tegra_vi_remove(struct platform_device *pdev)
1773 struct tegra_vi *vi = platform_get_drvdata(pdev);
1776 err = host1x_client_unregister(&vi->client);
1779 "failed to unregister host1x client: %d\n", err);
1783 pm_runtime_disable(&pdev->dev);
1788 static const struct of_device_id tegra_vi_of_id_table[] = {
1789 #if defined(CONFIG_ARCH_TEGRA_210_SOC)
1790 { .compatible = "nvidia,tegra210-vi", .data = &tegra210_vi_soc },
1794 MODULE_DEVICE_TABLE(of, tegra_vi_of_id_table);
1796 static const struct dev_pm_ops tegra_vi_pm_ops = {
1797 SET_RUNTIME_PM_OPS(vi_runtime_suspend, vi_runtime_resume, NULL)
1800 struct platform_driver tegra_vi_driver = {
1803 .of_match_table = tegra_vi_of_id_table,
1804 .pm = &tegra_vi_pm_ops,
1806 .probe = tegra_vi_probe,
1807 .remove = tegra_vi_remove,