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-dv-timings.h>
22 #include <media/v4l2-event.h>
23 #include <media/v4l2-fh.h>
24 #include <media/v4l2-fwnode.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/videobuf2-dma-contig.h>
28 #include <soc/tegra/pmc.h>
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_resume_and_get(chan->vi->dev);
302 dev_err(chan->vi->dev, "failed to get runtime PM: %d\n", ret);
306 ret = chan->vi->ops->vi_start_streaming(vq, count);
308 pm_runtime_put(chan->vi->dev);
313 static void tegra_channel_stop_streaming(struct vb2_queue *vq)
315 struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
317 chan->vi->ops->vi_stop_streaming(vq);
318 pm_runtime_put(chan->vi->dev);
321 static const struct vb2_ops tegra_channel_queue_qops = {
322 .queue_setup = tegra_channel_queue_setup,
323 .buf_prepare = tegra_channel_buffer_prepare,
324 .buf_queue = tegra_channel_buffer_queue,
325 .wait_prepare = vb2_ops_wait_prepare,
326 .wait_finish = vb2_ops_wait_finish,
327 .start_streaming = tegra_channel_start_streaming,
328 .stop_streaming = tegra_channel_stop_streaming,
332 * V4L2 ioctl operations
334 static int tegra_channel_querycap(struct file *file, void *fh,
335 struct v4l2_capability *cap)
337 struct tegra_vi_channel *chan = video_drvdata(file);
339 strscpy(cap->driver, "tegra-video", sizeof(cap->driver));
340 strscpy(cap->card, chan->video.name, sizeof(cap->card));
341 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
342 dev_name(chan->vi->dev));
347 static int tegra_channel_g_parm(struct file *file, void *fh,
348 struct v4l2_streamparm *a)
350 struct tegra_vi_channel *chan = video_drvdata(file);
351 struct v4l2_subdev *subdev;
353 subdev = tegra_channel_get_remote_source_subdev(chan);
354 return v4l2_g_parm_cap(&chan->video, subdev, a);
357 static int tegra_channel_s_parm(struct file *file, void *fh,
358 struct v4l2_streamparm *a)
360 struct tegra_vi_channel *chan = video_drvdata(file);
361 struct v4l2_subdev *subdev;
363 subdev = tegra_channel_get_remote_source_subdev(chan);
364 return v4l2_s_parm_cap(&chan->video, subdev, a);
367 static int tegra_channel_enum_framesizes(struct file *file, void *fh,
368 struct v4l2_frmsizeenum *sizes)
371 struct tegra_vi_channel *chan = video_drvdata(file);
372 struct v4l2_subdev *subdev;
373 const struct tegra_video_format *fmtinfo;
374 struct v4l2_subdev_frame_size_enum fse = {
375 .index = sizes->index,
376 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
379 fmtinfo = tegra_get_format_by_fourcc(chan->vi, sizes->pixel_format);
383 fse.code = fmtinfo->code;
385 subdev = tegra_channel_get_remote_source_subdev(chan);
386 ret = v4l2_subdev_call(subdev, pad, enum_frame_size, NULL, &fse);
390 sizes->type = V4L2_FRMSIZE_TYPE_DISCRETE;
391 sizes->discrete.width = fse.max_width;
392 sizes->discrete.height = fse.max_height;
397 static int tegra_channel_enum_frameintervals(struct file *file, void *fh,
398 struct v4l2_frmivalenum *ivals)
401 struct tegra_vi_channel *chan = video_drvdata(file);
402 struct v4l2_subdev *subdev;
403 const struct tegra_video_format *fmtinfo;
404 struct v4l2_subdev_frame_interval_enum fie = {
405 .index = ivals->index,
406 .width = ivals->width,
407 .height = ivals->height,
408 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
411 fmtinfo = tegra_get_format_by_fourcc(chan->vi, ivals->pixel_format);
415 fie.code = fmtinfo->code;
417 subdev = tegra_channel_get_remote_source_subdev(chan);
418 ret = v4l2_subdev_call(subdev, pad, enum_frame_interval, NULL, &fie);
422 ivals->type = V4L2_FRMIVAL_TYPE_DISCRETE;
423 ivals->discrete.numerator = fie.interval.numerator;
424 ivals->discrete.denominator = fie.interval.denominator;
429 static int tegra_channel_enum_format(struct file *file, void *fh,
430 struct v4l2_fmtdesc *f)
432 struct tegra_vi_channel *chan = video_drvdata(file);
433 unsigned int index = 0, i;
434 unsigned long *fmts_bitmap = chan->tpg_fmts_bitmap;
436 if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
437 fmts_bitmap = chan->fmts_bitmap;
439 if (f->index >= bitmap_weight(fmts_bitmap, MAX_FORMAT_NUM))
442 for (i = 0; i < f->index + 1; i++, index++)
443 index = find_next_bit(fmts_bitmap, MAX_FORMAT_NUM, index);
445 f->pixelformat = tegra_get_format_fourcc_by_idx(chan->vi, index - 1);
450 static int tegra_channel_get_format(struct file *file, void *fh,
451 struct v4l2_format *format)
453 struct tegra_vi_channel *chan = video_drvdata(file);
455 format->fmt.pix = chan->format;
460 static void tegra_channel_fmt_align(struct tegra_vi_channel *chan,
461 struct v4l2_pix_format *pix,
464 unsigned int min_bpl;
465 unsigned int max_bpl;
469 * The transfer alignment requirements are expressed in bytes.
470 * Clamp the requested width and height to the limits.
472 pix->width = clamp(pix->width, TEGRA_MIN_WIDTH, TEGRA_MAX_WIDTH);
473 pix->height = clamp(pix->height, TEGRA_MIN_HEIGHT, TEGRA_MAX_HEIGHT);
475 /* Clamp the requested bytes per line value. If the maximum bytes per
476 * line value is zero, the module doesn't support user configurable
477 * line sizes. Override the requested value with the minimum in that
480 min_bpl = pix->width * bpp;
481 max_bpl = rounddown(TEGRA_MAX_WIDTH, SURFACE_ALIGN_BYTES);
482 bpl = roundup(pix->bytesperline, SURFACE_ALIGN_BYTES);
484 pix->bytesperline = clamp(bpl, min_bpl, max_bpl);
485 pix->sizeimage = pix->bytesperline * pix->height;
486 if (pix->pixelformat == V4L2_PIX_FMT_NV16)
490 static int __tegra_channel_try_format(struct tegra_vi_channel *chan,
491 struct v4l2_pix_format *pix)
493 const struct tegra_video_format *fmtinfo;
494 struct v4l2_subdev *subdev;
495 struct v4l2_subdev_format fmt;
496 struct v4l2_subdev_state *sd_state;
497 struct v4l2_subdev_frame_size_enum fse = {
498 .which = V4L2_SUBDEV_FORMAT_TRY,
500 struct v4l2_subdev_selection sdsel = {
501 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
502 .target = V4L2_SEL_TGT_CROP_BOUNDS,
506 subdev = tegra_channel_get_remote_source_subdev(chan);
510 sd_state = v4l2_subdev_alloc_state(subdev);
514 * Retrieve the format information and if requested format isn't
515 * supported, keep the current format.
517 fmtinfo = tegra_get_format_by_fourcc(chan->vi, pix->pixelformat);
519 pix->pixelformat = chan->format.pixelformat;
520 pix->colorspace = chan->format.colorspace;
521 fmtinfo = tegra_get_format_by_fourcc(chan->vi,
525 pix->field = V4L2_FIELD_NONE;
526 fmt.which = V4L2_SUBDEV_FORMAT_TRY;
528 v4l2_fill_mbus_format(&fmt.format, pix, fmtinfo->code);
531 * Attempt to obtain the format size from subdev.
532 * If not available, try to get crop boundary from subdev.
534 fse.code = fmtinfo->code;
535 ret = v4l2_subdev_call(subdev, pad, enum_frame_size, sd_state, &fse);
537 if (!v4l2_subdev_has_op(subdev, pad, get_selection)) {
538 sd_state->pads->try_crop.width = 0;
539 sd_state->pads->try_crop.height = 0;
541 ret = v4l2_subdev_call(subdev, pad, get_selection,
546 sd_state->pads->try_crop.width = sdsel.r.width;
547 sd_state->pads->try_crop.height = sdsel.r.height;
550 sd_state->pads->try_crop.width = fse.max_width;
551 sd_state->pads->try_crop.height = fse.max_height;
554 ret = v4l2_subdev_call(subdev, pad, set_fmt, sd_state, &fmt);
558 v4l2_fill_pix_format(pix, &fmt.format);
559 tegra_channel_fmt_align(chan, pix, fmtinfo->bpp);
561 v4l2_subdev_free_state(sd_state);
566 static int tegra_channel_try_format(struct file *file, void *fh,
567 struct v4l2_format *format)
569 struct tegra_vi_channel *chan = video_drvdata(file);
571 return __tegra_channel_try_format(chan, &format->fmt.pix);
574 static void tegra_channel_update_gangports(struct tegra_vi_channel *chan)
576 if (chan->format.width <= 1920)
577 chan->numgangports = 1;
579 chan->numgangports = chan->totalports;
582 static int tegra_channel_set_format(struct file *file, void *fh,
583 struct v4l2_format *format)
585 struct tegra_vi_channel *chan = video_drvdata(file);
586 const struct tegra_video_format *fmtinfo;
587 struct v4l2_subdev_format fmt;
588 struct v4l2_subdev *subdev;
589 struct v4l2_pix_format *pix = &format->fmt.pix;
592 if (vb2_is_busy(&chan->queue))
595 /* get supported format by try_fmt */
596 ret = __tegra_channel_try_format(chan, pix);
600 fmtinfo = tegra_get_format_by_fourcc(chan->vi, pix->pixelformat);
602 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
604 v4l2_fill_mbus_format(&fmt.format, pix, fmtinfo->code);
605 subdev = tegra_channel_get_remote_source_subdev(chan);
606 ret = v4l2_subdev_call(subdev, pad, set_fmt, NULL, &fmt);
610 v4l2_fill_pix_format(pix, &fmt.format);
611 tegra_channel_fmt_align(chan, pix, fmtinfo->bpp);
614 chan->fmtinfo = fmtinfo;
615 tegra_channel_update_gangports(chan);
620 static int tegra_channel_set_subdev_active_fmt(struct tegra_vi_channel *chan)
623 struct v4l2_subdev *subdev;
624 struct v4l2_subdev_format fmt = {
625 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
629 * Initialize channel format to the sub-device active format if there
630 * is corresponding match in the Tegra supported video formats.
632 subdev = tegra_channel_get_remote_source_subdev(chan);
633 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
637 index = tegra_get_format_idx_by_code(chan->vi, fmt.format.code, 0);
641 chan->fmtinfo = &chan->vi->soc->video_formats[index];
642 v4l2_fill_pix_format(&chan->format, &fmt.format);
643 chan->format.pixelformat = chan->fmtinfo->fourcc;
644 chan->format.bytesperline = chan->format.width * chan->fmtinfo->bpp;
645 chan->format.sizeimage = chan->format.bytesperline *
647 tegra_channel_fmt_align(chan, &chan->format, chan->fmtinfo->bpp);
648 tegra_channel_update_gangports(chan);
654 tegra_channel_subscribe_event(struct v4l2_fh *fh,
655 const struct v4l2_event_subscription *sub)
658 case V4L2_EVENT_SOURCE_CHANGE:
659 return v4l2_event_subscribe(fh, sub, 4, NULL);
662 return v4l2_ctrl_subscribe_event(fh, sub);
665 static int tegra_channel_g_selection(struct file *file, void *priv,
666 struct v4l2_selection *sel)
668 struct tegra_vi_channel *chan = video_drvdata(file);
669 struct v4l2_subdev *subdev;
670 struct v4l2_subdev_format fmt = {
671 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
673 struct v4l2_subdev_selection sdsel = {
674 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
675 .target = sel->target,
679 subdev = tegra_channel_get_remote_source_subdev(chan);
680 if (!v4l2_subdev_has_op(subdev, pad, get_selection))
683 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
686 * Try the get selection operation and fallback to get format if not
689 ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
692 if (ret != -ENOIOCTLCMD)
695 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
701 sel->r.width = fmt.format.width;
702 sel->r.height = fmt.format.height;
707 static int tegra_channel_s_selection(struct file *file, void *fh,
708 struct v4l2_selection *sel)
710 struct tegra_vi_channel *chan = video_drvdata(file);
711 struct v4l2_subdev *subdev;
713 struct v4l2_subdev_selection sdsel = {
714 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
715 .target = sel->target,
720 subdev = tegra_channel_get_remote_source_subdev(chan);
721 if (!v4l2_subdev_has_op(subdev, pad, set_selection))
724 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
727 if (vb2_is_busy(&chan->queue))
730 ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel);
734 * Subdev active format resolution may have changed during
735 * set selection operation. So, update channel format to
736 * the sub-device active format.
738 return tegra_channel_set_subdev_active_fmt(chan);
744 static int tegra_channel_g_edid(struct file *file, void *fh,
745 struct v4l2_edid *edid)
747 struct tegra_vi_channel *chan = video_drvdata(file);
748 struct v4l2_subdev *subdev;
750 subdev = tegra_channel_get_remote_source_subdev(chan);
751 if (!v4l2_subdev_has_op(subdev, pad, get_edid))
754 return v4l2_subdev_call(subdev, pad, get_edid, edid);
757 static int tegra_channel_s_edid(struct file *file, void *fh,
758 struct v4l2_edid *edid)
760 struct tegra_vi_channel *chan = video_drvdata(file);
761 struct v4l2_subdev *subdev;
763 subdev = tegra_channel_get_remote_source_subdev(chan);
764 if (!v4l2_subdev_has_op(subdev, pad, set_edid))
767 return v4l2_subdev_call(subdev, pad, set_edid, edid);
770 static int tegra_channel_g_dv_timings(struct file *file, void *fh,
771 struct v4l2_dv_timings *timings)
773 struct tegra_vi_channel *chan = video_drvdata(file);
774 struct v4l2_subdev *subdev;
776 subdev = tegra_channel_get_remote_source_subdev(chan);
777 if (!v4l2_subdev_has_op(subdev, video, g_dv_timings))
780 return v4l2_device_call_until_err(chan->video.v4l2_dev, 0,
781 video, g_dv_timings, timings);
784 static int tegra_channel_s_dv_timings(struct file *file, void *fh,
785 struct v4l2_dv_timings *timings)
787 struct tegra_vi_channel *chan = video_drvdata(file);
788 struct v4l2_subdev *subdev;
789 struct v4l2_bt_timings *bt = &timings->bt;
790 struct v4l2_dv_timings curr_timings;
793 subdev = tegra_channel_get_remote_source_subdev(chan);
794 if (!v4l2_subdev_has_op(subdev, video, s_dv_timings))
797 ret = tegra_channel_g_dv_timings(file, fh, &curr_timings);
801 if (v4l2_match_dv_timings(timings, &curr_timings, 0, false))
804 if (vb2_is_busy(&chan->queue))
807 ret = v4l2_device_call_until_err(chan->video.v4l2_dev, 0,
808 video, s_dv_timings, timings);
812 chan->format.width = bt->width;
813 chan->format.height = bt->height;
814 chan->format.bytesperline = bt->width * chan->fmtinfo->bpp;
815 chan->format.sizeimage = chan->format.bytesperline * bt->height;
816 tegra_channel_fmt_align(chan, &chan->format, chan->fmtinfo->bpp);
817 tegra_channel_update_gangports(chan);
822 static int tegra_channel_query_dv_timings(struct file *file, void *fh,
823 struct v4l2_dv_timings *timings)
825 struct tegra_vi_channel *chan = video_drvdata(file);
826 struct v4l2_subdev *subdev;
828 subdev = tegra_channel_get_remote_source_subdev(chan);
829 if (!v4l2_subdev_has_op(subdev, video, query_dv_timings))
832 return v4l2_device_call_until_err(chan->video.v4l2_dev, 0,
833 video, query_dv_timings, timings);
836 static int tegra_channel_enum_dv_timings(struct file *file, void *fh,
837 struct v4l2_enum_dv_timings *timings)
839 struct tegra_vi_channel *chan = video_drvdata(file);
840 struct v4l2_subdev *subdev;
842 subdev = tegra_channel_get_remote_source_subdev(chan);
843 if (!v4l2_subdev_has_op(subdev, pad, enum_dv_timings))
846 return v4l2_subdev_call(subdev, pad, enum_dv_timings, timings);
849 static int tegra_channel_dv_timings_cap(struct file *file, void *fh,
850 struct v4l2_dv_timings_cap *cap)
852 struct tegra_vi_channel *chan = video_drvdata(file);
853 struct v4l2_subdev *subdev;
855 subdev = tegra_channel_get_remote_source_subdev(chan);
856 if (!v4l2_subdev_has_op(subdev, pad, dv_timings_cap))
859 return v4l2_subdev_call(subdev, pad, dv_timings_cap, cap);
862 static int tegra_channel_log_status(struct file *file, void *fh)
864 struct tegra_vi_channel *chan = video_drvdata(file);
866 v4l2_device_call_all(chan->video.v4l2_dev, 0, core, log_status);
871 static int tegra_channel_enum_input(struct file *file, void *fh,
872 struct v4l2_input *inp)
874 struct tegra_vi_channel *chan = video_drvdata(file);
875 struct v4l2_subdev *subdev;
880 inp->type = V4L2_INPUT_TYPE_CAMERA;
881 subdev = tegra_channel_get_remote_source_subdev(chan);
882 strscpy(inp->name, subdev->name, sizeof(inp->name));
883 if (v4l2_subdev_has_op(subdev, pad, dv_timings_cap))
884 inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
889 static int tegra_channel_g_input(struct file *file, void *priv,
897 static int tegra_channel_s_input(struct file *file, void *priv,
906 static const struct v4l2_ioctl_ops tegra_channel_ioctl_ops = {
907 .vidioc_querycap = tegra_channel_querycap,
908 .vidioc_g_parm = tegra_channel_g_parm,
909 .vidioc_s_parm = tegra_channel_s_parm,
910 .vidioc_enum_framesizes = tegra_channel_enum_framesizes,
911 .vidioc_enum_frameintervals = tegra_channel_enum_frameintervals,
912 .vidioc_enum_fmt_vid_cap = tegra_channel_enum_format,
913 .vidioc_g_fmt_vid_cap = tegra_channel_get_format,
914 .vidioc_s_fmt_vid_cap = tegra_channel_set_format,
915 .vidioc_try_fmt_vid_cap = tegra_channel_try_format,
916 .vidioc_enum_input = tegra_channel_enum_input,
917 .vidioc_g_input = tegra_channel_g_input,
918 .vidioc_s_input = tegra_channel_s_input,
919 .vidioc_reqbufs = vb2_ioctl_reqbufs,
920 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
921 .vidioc_querybuf = vb2_ioctl_querybuf,
922 .vidioc_qbuf = vb2_ioctl_qbuf,
923 .vidioc_dqbuf = vb2_ioctl_dqbuf,
924 .vidioc_create_bufs = vb2_ioctl_create_bufs,
925 .vidioc_expbuf = vb2_ioctl_expbuf,
926 .vidioc_streamon = vb2_ioctl_streamon,
927 .vidioc_streamoff = vb2_ioctl_streamoff,
928 .vidioc_subscribe_event = tegra_channel_subscribe_event,
929 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
930 .vidioc_g_selection = tegra_channel_g_selection,
931 .vidioc_s_selection = tegra_channel_s_selection,
932 .vidioc_g_edid = tegra_channel_g_edid,
933 .vidioc_s_edid = tegra_channel_s_edid,
934 .vidioc_g_dv_timings = tegra_channel_g_dv_timings,
935 .vidioc_s_dv_timings = tegra_channel_s_dv_timings,
936 .vidioc_query_dv_timings = tegra_channel_query_dv_timings,
937 .vidioc_enum_dv_timings = tegra_channel_enum_dv_timings,
938 .vidioc_dv_timings_cap = tegra_channel_dv_timings_cap,
939 .vidioc_log_status = tegra_channel_log_status,
943 * V4L2 file operations
945 static const struct v4l2_file_operations tegra_channel_fops = {
946 .owner = THIS_MODULE,
947 .unlocked_ioctl = video_ioctl2,
948 .open = v4l2_fh_open,
949 .release = vb2_fop_release,
950 .read = vb2_fop_read,
951 .poll = vb2_fop_poll,
952 .mmap = vb2_fop_mmap,
956 * V4L2 control operations
958 static int vi_s_ctrl(struct v4l2_ctrl *ctrl)
960 struct tegra_vi_channel *chan = container_of(ctrl->handler,
961 struct tegra_vi_channel,
965 case V4L2_CID_TEST_PATTERN:
966 /* pattern change takes effect on next stream */
967 chan->pg_mode = ctrl->val + 1;
969 case V4L2_CID_TEGRA_SYNCPT_TIMEOUT_RETRY:
970 chan->syncpt_timeout_retry = ctrl->val;
979 static const struct v4l2_ctrl_ops vi_ctrl_ops = {
983 #if IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)
984 static const char *const vi_pattern_strings[] = {
985 "Black/White Direct Mode",
989 static const struct v4l2_ctrl_config syncpt_timeout_ctrl = {
991 .id = V4L2_CID_TEGRA_SYNCPT_TIMEOUT_RETRY,
992 .name = "Syncpt timeout retry",
993 .type = V4L2_CTRL_TYPE_INTEGER,
1001 static int tegra_channel_setup_ctrl_handler(struct tegra_vi_channel *chan)
1005 #if IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)
1006 /* add test pattern control handler to v4l2 device */
1007 v4l2_ctrl_new_std_menu_items(&chan->ctrl_handler, &vi_ctrl_ops,
1008 V4L2_CID_TEST_PATTERN,
1009 ARRAY_SIZE(vi_pattern_strings) - 1,
1010 0, 0, vi_pattern_strings);
1011 if (chan->ctrl_handler.error) {
1012 dev_err(chan->vi->dev, "failed to add TPG ctrl handler: %d\n",
1013 chan->ctrl_handler.error);
1014 v4l2_ctrl_handler_free(&chan->ctrl_handler);
1015 return chan->ctrl_handler.error;
1018 struct v4l2_subdev *subdev;
1020 /* custom control */
1021 v4l2_ctrl_new_custom(&chan->ctrl_handler, &syncpt_timeout_ctrl, NULL);
1022 if (chan->ctrl_handler.error) {
1023 dev_err(chan->vi->dev, "failed to add %s ctrl handler: %d\n",
1024 syncpt_timeout_ctrl.name,
1025 chan->ctrl_handler.error);
1026 v4l2_ctrl_handler_free(&chan->ctrl_handler);
1027 return chan->ctrl_handler.error;
1030 subdev = tegra_channel_get_remote_source_subdev(chan);
1034 ret = v4l2_ctrl_add_handler(&chan->ctrl_handler, subdev->ctrl_handler,
1037 dev_err(chan->vi->dev,
1038 "failed to add subdev %s ctrl handler: %d\n",
1040 v4l2_ctrl_handler_free(&chan->ctrl_handler);
1045 /* setup the controls */
1046 ret = v4l2_ctrl_handler_setup(&chan->ctrl_handler);
1048 dev_err(chan->vi->dev,
1049 "failed to setup v4l2 ctrl handler: %d\n", ret);
1056 /* VI only support 2 formats in TPG mode */
1057 static void vi_tpg_fmts_bitmap_init(struct tegra_vi_channel *chan)
1061 bitmap_zero(chan->tpg_fmts_bitmap, MAX_FORMAT_NUM);
1063 index = tegra_get_format_idx_by_code(chan->vi,
1064 MEDIA_BUS_FMT_SRGGB10_1X10, 0);
1065 bitmap_set(chan->tpg_fmts_bitmap, index, 1);
1067 index = tegra_get_format_idx_by_code(chan->vi,
1068 MEDIA_BUS_FMT_RGB888_1X32_PADHI,
1070 bitmap_set(chan->tpg_fmts_bitmap, index, 1);
1073 static int vi_fmts_bitmap_init(struct tegra_vi_channel *chan)
1075 int index, ret, match_code = 0;
1076 struct v4l2_subdev *subdev;
1077 struct v4l2_subdev_mbus_code_enum code = {
1078 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1081 bitmap_zero(chan->fmts_bitmap, MAX_FORMAT_NUM);
1084 * Set the bitmap bits based on all the matched formats between the
1085 * available media bus formats of sub-device and the pre-defined Tegra
1086 * supported video formats.
1088 subdev = tegra_channel_get_remote_source_subdev(chan);
1090 ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
1095 index = tegra_get_format_idx_by_code(chan->vi, code.code, 0);
1096 while (index >= 0) {
1097 bitmap_set(chan->fmts_bitmap, index, 1);
1099 match_code = code.code;
1100 /* look for other formats with same mbus code */
1101 index = tegra_get_format_idx_by_code(chan->vi,
1110 * Set the bitmap bit corresponding to default tegra video format if
1111 * there are no matched formats.
1114 match_code = tegra_default_format.code;
1115 index = tegra_get_format_idx_by_code(chan->vi, match_code, 0);
1116 if (WARN_ON(index < 0))
1119 bitmap_set(chan->fmts_bitmap, index, 1);
1122 /* initialize channel format to the sub-device active format */
1123 tegra_channel_set_subdev_active_fmt(chan);
1128 static void tegra_channel_host1x_syncpts_free(struct tegra_vi_channel *chan)
1132 for (i = 0; i < chan->numgangports; i++) {
1133 host1x_syncpt_put(chan->mw_ack_sp[i]);
1134 host1x_syncpt_put(chan->frame_start_sp[i]);
1138 static void tegra_channel_cleanup(struct tegra_vi_channel *chan)
1140 v4l2_ctrl_handler_free(&chan->ctrl_handler);
1141 media_entity_cleanup(&chan->video.entity);
1142 tegra_channel_host1x_syncpts_free(chan);
1143 mutex_destroy(&chan->video_lock);
1146 void tegra_channels_cleanup(struct tegra_vi *vi)
1148 struct tegra_vi_channel *chan, *tmp;
1153 list_for_each_entry_safe(chan, tmp, &vi->vi_chans, list) {
1154 tegra_channel_cleanup(chan);
1155 list_del(&chan->list);
1160 static int tegra_channel_host1x_syncpt_init(struct tegra_vi_channel *chan)
1162 struct tegra_vi *vi = chan->vi;
1163 unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
1164 struct host1x_syncpt *fs_sp;
1165 struct host1x_syncpt *mw_sp;
1168 for (i = 0; i < chan->numgangports; i++) {
1169 fs_sp = host1x_syncpt_request(&vi->client, flags);
1171 dev_err(vi->dev, "failed to request frame start syncpoint\n");
1176 mw_sp = host1x_syncpt_request(&vi->client, flags);
1178 dev_err(vi->dev, "failed to request memory ack syncpoint\n");
1179 host1x_syncpt_put(fs_sp);
1184 chan->frame_start_sp[i] = fs_sp;
1185 chan->mw_ack_sp[i] = mw_sp;
1186 spin_lock_init(&chan->sp_incr_lock[i]);
1192 tegra_channel_host1x_syncpts_free(chan);
1196 static int tegra_channel_init(struct tegra_vi_channel *chan)
1198 struct tegra_vi *vi = chan->vi;
1199 struct tegra_video_device *vid = dev_get_drvdata(vi->client.host);
1202 mutex_init(&chan->video_lock);
1203 INIT_LIST_HEAD(&chan->capture);
1204 INIT_LIST_HEAD(&chan->done);
1205 spin_lock_init(&chan->start_lock);
1206 spin_lock_init(&chan->done_lock);
1207 init_waitqueue_head(&chan->start_wait);
1208 init_waitqueue_head(&chan->done_wait);
1210 /* initialize the video format */
1211 chan->fmtinfo = &tegra_default_format;
1212 chan->format.pixelformat = chan->fmtinfo->fourcc;
1213 chan->format.colorspace = V4L2_COLORSPACE_SRGB;
1214 chan->format.field = V4L2_FIELD_NONE;
1215 chan->format.width = TEGRA_DEF_WIDTH;
1216 chan->format.height = TEGRA_DEF_HEIGHT;
1217 chan->format.bytesperline = TEGRA_DEF_WIDTH * chan->fmtinfo->bpp;
1218 chan->format.sizeimage = chan->format.bytesperline * TEGRA_DEF_HEIGHT;
1219 tegra_channel_fmt_align(chan, &chan->format, chan->fmtinfo->bpp);
1221 ret = tegra_channel_host1x_syncpt_init(chan);
1225 /* initialize the media entity */
1226 chan->pad.flags = MEDIA_PAD_FL_SINK;
1227 ret = media_entity_pads_init(&chan->video.entity, 1, &chan->pad);
1230 "failed to initialize media entity: %d\n", ret);
1234 ret = v4l2_ctrl_handler_init(&chan->ctrl_handler, MAX_CID_CONTROLS);
1235 if (chan->ctrl_handler.error) {
1237 "failed to initialize v4l2 ctrl handler: %d\n", ret);
1241 /* initialize the video_device */
1242 chan->video.fops = &tegra_channel_fops;
1243 chan->video.v4l2_dev = &vid->v4l2_dev;
1244 chan->video.release = video_device_release_empty;
1245 chan->video.queue = &chan->queue;
1246 snprintf(chan->video.name, sizeof(chan->video.name), "%s-%s-%u",
1247 dev_name(vi->dev), "output", chan->portnos[0]);
1248 chan->video.vfl_type = VFL_TYPE_VIDEO;
1249 chan->video.vfl_dir = VFL_DIR_RX;
1250 chan->video.ioctl_ops = &tegra_channel_ioctl_ops;
1251 chan->video.ctrl_handler = &chan->ctrl_handler;
1252 chan->video.lock = &chan->video_lock;
1253 chan->video.device_caps = V4L2_CAP_VIDEO_CAPTURE |
1254 V4L2_CAP_STREAMING |
1256 video_set_drvdata(&chan->video, chan);
1258 chan->queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1259 chan->queue.io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
1260 chan->queue.lock = &chan->video_lock;
1261 chan->queue.drv_priv = chan;
1262 chan->queue.buf_struct_size = sizeof(struct tegra_channel_buffer);
1263 chan->queue.ops = &tegra_channel_queue_qops;
1264 chan->queue.mem_ops = &vb2_dma_contig_memops;
1265 chan->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1266 chan->queue.min_buffers_needed = 2;
1267 chan->queue.dev = vi->dev;
1268 ret = vb2_queue_init(&chan->queue);
1270 dev_err(vi->dev, "failed to initialize vb2 queue: %d\n", ret);
1271 goto free_v4l2_ctrl_hdl;
1274 if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
1275 v4l2_async_notifier_init(&chan->notifier);
1280 v4l2_ctrl_handler_free(&chan->ctrl_handler);
1282 media_entity_cleanup(&chan->video.entity);
1284 tegra_channel_host1x_syncpts_free(chan);
1288 static int tegra_vi_channel_alloc(struct tegra_vi *vi, unsigned int port_num,
1289 struct device_node *node, unsigned int lanes)
1291 struct tegra_vi_channel *chan;
1295 * Do not use devm_kzalloc as memory is freed immediately
1296 * when device instance is unbound but application might still
1297 * be holding the device node open. Channel memory allocated
1298 * with kzalloc is freed during video device release callback.
1300 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
1305 chan->portnos[0] = port_num;
1307 * For data lanes more than maximum csi lanes per brick, multiple of
1308 * x4 ports are used simultaneously for capture.
1310 if (lanes <= CSI_LANES_PER_BRICK)
1311 chan->totalports = 1;
1313 chan->totalports = lanes / CSI_LANES_PER_BRICK;
1314 chan->numgangports = chan->totalports;
1316 for (i = 1; i < chan->totalports; i++)
1317 chan->portnos[i] = chan->portnos[0] + i * CSI_PORTS_PER_BRICK;
1319 chan->of_node = node;
1320 list_add_tail(&chan->list, &vi->vi_chans);
1325 static int tegra_vi_tpg_channels_alloc(struct tegra_vi *vi)
1327 unsigned int port_num;
1328 unsigned int nchannels = vi->soc->vi_max_channels;
1331 for (port_num = 0; port_num < nchannels; port_num++) {
1332 ret = tegra_vi_channel_alloc(vi, port_num,
1333 vi->dev->of_node, 2);
1341 static int tegra_vi_channels_alloc(struct tegra_vi *vi)
1343 struct device_node *node = vi->dev->of_node;
1344 struct device_node *ep = NULL;
1345 struct device_node *ports;
1346 struct device_node *port;
1347 unsigned int port_num;
1348 struct device_node *parent;
1349 struct v4l2_fwnode_endpoint v4l2_ep = { .bus_type = 0 };
1353 ports = of_get_child_by_name(node, "ports");
1357 for_each_child_of_node(ports, port) {
1358 if (!of_node_name_eq(port, "port"))
1361 ret = of_property_read_u32(port, "reg", &port_num);
1365 if (port_num > vi->soc->vi_max_channels) {
1366 dev_err(vi->dev, "invalid port num %d for %pOF\n",
1373 ep = of_get_child_by_name(port, "endpoint");
1377 parent = of_graph_get_remote_port_parent(ep);
1382 ep = of_graph_get_endpoint_by_regs(parent, 0, 0);
1383 of_node_put(parent);
1384 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep),
1390 lanes = v4l2_ep.bus.mipi_csi2.num_data_lanes;
1391 ret = tegra_vi_channel_alloc(vi, port_num, port, lanes);
1403 static int tegra_vi_channels_init(struct tegra_vi *vi)
1405 struct tegra_vi_channel *chan;
1408 list_for_each_entry(chan, &vi->vi_chans, list) {
1409 ret = tegra_channel_init(chan);
1412 "failed to initialize channel-%d: %d\n",
1413 chan->portnos[0], ret);
1421 list_for_each_entry_continue_reverse(chan, &vi->vi_chans, list)
1422 tegra_channel_cleanup(chan);
1427 void tegra_v4l2_nodes_cleanup_tpg(struct tegra_video_device *vid)
1429 struct tegra_vi *vi = vid->vi;
1430 struct tegra_csi *csi = vid->csi;
1431 struct tegra_csi_channel *csi_chan;
1432 struct tegra_vi_channel *chan;
1434 list_for_each_entry(chan, &vi->vi_chans, list)
1435 vb2_video_unregister_device(&chan->video);
1437 list_for_each_entry(csi_chan, &csi->csi_chans, list)
1438 v4l2_device_unregister_subdev(&csi_chan->subdev);
1441 int tegra_v4l2_nodes_setup_tpg(struct tegra_video_device *vid)
1443 struct tegra_vi *vi = vid->vi;
1444 struct tegra_csi *csi = vid->csi;
1445 struct tegra_vi_channel *vi_chan;
1446 struct tegra_csi_channel *csi_chan;
1447 u32 link_flags = MEDIA_LNK_FL_ENABLED;
1453 csi_chan = list_first_entry(&csi->csi_chans,
1454 struct tegra_csi_channel, list);
1456 list_for_each_entry(vi_chan, &vi->vi_chans, list) {
1457 struct media_entity *source = &csi_chan->subdev.entity;
1458 struct media_entity *sink = &vi_chan->video.entity;
1459 struct media_pad *source_pad = csi_chan->pads;
1460 struct media_pad *sink_pad = &vi_chan->pad;
1462 ret = v4l2_device_register_subdev(&vid->v4l2_dev,
1466 "failed to register subdev: %d\n", ret);
1470 ret = video_register_device(&vi_chan->video,
1471 VFL_TYPE_VIDEO, -1);
1474 "failed to register video device: %d\n", ret);
1478 dev_dbg(vi->dev, "creating %s:%u -> %s:%u link\n",
1479 source->name, source_pad->index,
1480 sink->name, sink_pad->index);
1482 ret = media_create_pad_link(source, source_pad->index,
1483 sink, sink_pad->index,
1487 "failed to create %s:%u -> %s:%u link: %d\n",
1488 source->name, source_pad->index,
1489 sink->name, sink_pad->index, ret);
1493 ret = tegra_channel_setup_ctrl_handler(vi_chan);
1497 v4l2_set_subdev_hostdata(&csi_chan->subdev, vi_chan);
1498 vi_tpg_fmts_bitmap_init(vi_chan);
1499 csi_chan = list_next_entry(csi_chan, list);
1505 tegra_v4l2_nodes_cleanup_tpg(vid);
1509 static int __maybe_unused vi_runtime_resume(struct device *dev)
1511 struct tegra_vi *vi = dev_get_drvdata(dev);
1514 ret = regulator_enable(vi->vdd);
1516 dev_err(dev, "failed to enable VDD supply: %d\n", ret);
1520 ret = clk_set_rate(vi->clk, vi->soc->vi_max_clk_hz);
1522 dev_err(dev, "failed to set vi clock rate: %d\n", ret);
1526 ret = clk_prepare_enable(vi->clk);
1528 dev_err(dev, "failed to enable vi clock: %d\n", ret);
1535 regulator_disable(vi->vdd);
1539 static int __maybe_unused vi_runtime_suspend(struct device *dev)
1541 struct tegra_vi *vi = dev_get_drvdata(dev);
1543 clk_disable_unprepare(vi->clk);
1545 regulator_disable(vi->vdd);
1553 static struct tegra_vi_graph_entity *
1554 tegra_vi_graph_find_entity(struct tegra_vi_channel *chan,
1555 const struct fwnode_handle *fwnode)
1557 struct tegra_vi_graph_entity *entity;
1558 struct v4l2_async_subdev *asd;
1560 list_for_each_entry(asd, &chan->notifier.asd_list, asd_list) {
1561 entity = to_tegra_vi_graph_entity(asd);
1562 if (entity->asd.match.fwnode == fwnode)
1569 static int tegra_vi_graph_build(struct tegra_vi_channel *chan,
1570 struct tegra_vi_graph_entity *entity)
1572 struct tegra_vi *vi = chan->vi;
1573 struct tegra_vi_graph_entity *ent;
1574 struct fwnode_handle *ep = NULL;
1575 struct v4l2_fwnode_link link;
1576 struct media_entity *local = entity->entity;
1577 struct media_entity *remote;
1578 struct media_pad *local_pad;
1579 struct media_pad *remote_pad;
1580 u32 link_flags = MEDIA_LNK_FL_ENABLED;
1583 dev_dbg(vi->dev, "creating links for entity %s\n", local->name);
1586 ep = fwnode_graph_get_next_endpoint(entity->asd.match.fwnode,
1591 ret = v4l2_fwnode_parse_link(ep, &link);
1593 dev_err(vi->dev, "failed to parse link for %pOF: %d\n",
1594 to_of_node(ep), ret);
1598 if (link.local_port >= local->num_pads) {
1599 dev_err(vi->dev, "invalid port number %u on %pOF\n",
1600 link.local_port, to_of_node(link.local_node));
1601 v4l2_fwnode_put_link(&link);
1606 local_pad = &local->pads[link.local_port];
1607 /* Remote node is vi node. So use channel video entity and pad
1610 if (link.remote_node == of_fwnode_handle(vi->dev->of_node)) {
1611 remote = &chan->video.entity;
1612 remote_pad = &chan->pad;
1617 * Skip sink ports, they will be processed from the other end
1620 if (local_pad->flags & MEDIA_PAD_FL_SINK) {
1621 dev_dbg(vi->dev, "skipping sink port %pOF:%u\n",
1622 to_of_node(link.local_node), link.local_port);
1623 v4l2_fwnode_put_link(&link);
1627 /* find the remote entity from notifier list */
1628 ent = tegra_vi_graph_find_entity(chan, link.remote_node);
1630 dev_err(vi->dev, "no entity found for %pOF\n",
1631 to_of_node(link.remote_node));
1632 v4l2_fwnode_put_link(&link);
1637 remote = ent->entity;
1638 if (link.remote_port >= remote->num_pads) {
1639 dev_err(vi->dev, "invalid port number %u on %pOF\n",
1641 to_of_node(link.remote_node));
1642 v4l2_fwnode_put_link(&link);
1647 remote_pad = &remote->pads[link.remote_port];
1650 dev_dbg(vi->dev, "creating %s:%u -> %s:%u link\n",
1651 local->name, local_pad->index,
1652 remote->name, remote_pad->index);
1654 ret = media_create_pad_link(local, local_pad->index,
1655 remote, remote_pad->index,
1657 v4l2_fwnode_put_link(&link);
1660 "failed to create %s:%u -> %s:%u link: %d\n",
1661 local->name, local_pad->index,
1662 remote->name, remote_pad->index, ret);
1667 fwnode_handle_put(ep);
1671 static int tegra_vi_graph_notify_complete(struct v4l2_async_notifier *notifier)
1673 struct tegra_vi_graph_entity *entity;
1674 struct v4l2_async_subdev *asd;
1675 struct v4l2_subdev *subdev;
1676 struct tegra_vi_channel *chan;
1677 struct tegra_vi *vi;
1680 chan = container_of(notifier, struct tegra_vi_channel, notifier);
1683 dev_dbg(vi->dev, "notify complete, all subdevs registered\n");
1686 * Video device node should be created at the end of all the device
1687 * related initialization/setup.
1688 * Current video_register_device() does both initialize and register
1689 * video device in same API.
1691 * TODO: Update v4l2-dev driver to split initialize and register into
1692 * separate APIs and then update Tegra video driver to do video device
1693 * initialize followed by all video device related setup and then
1694 * register the video device.
1696 ret = video_register_device(&chan->video, VFL_TYPE_VIDEO, -1);
1699 "failed to register video device: %d\n", ret);
1700 goto unregister_video;
1703 /* create links between the entities */
1704 list_for_each_entry(asd, &chan->notifier.asd_list, asd_list) {
1705 entity = to_tegra_vi_graph_entity(asd);
1706 ret = tegra_vi_graph_build(chan, entity);
1708 goto unregister_video;
1711 ret = tegra_channel_setup_ctrl_handler(chan);
1714 "failed to setup channel controls: %d\n", ret);
1715 goto unregister_video;
1718 ret = vi_fmts_bitmap_init(chan);
1721 "failed to initialize formats bitmap: %d\n", ret);
1722 goto unregister_video;
1725 subdev = tegra_channel_get_remote_csi_subdev(chan);
1729 "failed to get remote csi subdev: %d\n", ret);
1730 goto unregister_video;
1733 v4l2_set_subdev_hostdata(subdev, chan);
1735 subdev = tegra_channel_get_remote_source_subdev(chan);
1736 v4l2_set_subdev_hostdata(subdev, chan);
1741 vb2_video_unregister_device(&chan->video);
1745 static int tegra_vi_graph_notify_bound(struct v4l2_async_notifier *notifier,
1746 struct v4l2_subdev *subdev,
1747 struct v4l2_async_subdev *asd)
1749 struct tegra_vi_graph_entity *entity;
1750 struct tegra_vi *vi;
1751 struct tegra_vi_channel *chan;
1753 chan = container_of(notifier, struct tegra_vi_channel, notifier);
1757 * Locate the entity corresponding to the bound subdev and store the
1760 entity = tegra_vi_graph_find_entity(chan, subdev->fwnode);
1762 dev_err(vi->dev, "no entity for subdev %s\n", subdev->name);
1766 if (entity->subdev) {
1767 dev_err(vi->dev, "duplicate subdev for node %pOF\n",
1768 to_of_node(entity->asd.match.fwnode));
1772 dev_dbg(vi->dev, "subdev %s bound\n", subdev->name);
1773 entity->entity = &subdev->entity;
1774 entity->subdev = subdev;
1779 static const struct v4l2_async_notifier_operations tegra_vi_async_ops = {
1780 .bound = tegra_vi_graph_notify_bound,
1781 .complete = tegra_vi_graph_notify_complete,
1784 static int tegra_vi_graph_parse_one(struct tegra_vi_channel *chan,
1785 struct fwnode_handle *fwnode)
1787 struct tegra_vi *vi = chan->vi;
1788 struct fwnode_handle *ep = NULL;
1789 struct fwnode_handle *remote = NULL;
1790 struct tegra_vi_graph_entity *tvge;
1791 struct device_node *node = NULL;
1794 dev_dbg(vi->dev, "parsing node %pOF\n", to_of_node(fwnode));
1796 /* parse all the remote entities and put them into the list */
1797 for_each_endpoint_of_node(to_of_node(fwnode), node) {
1798 ep = of_fwnode_handle(node);
1799 remote = fwnode_graph_get_remote_port_parent(ep);
1802 "remote device at %pOF not found\n", node);
1807 /* skip entities that are already processed */
1808 if (remote == dev_fwnode(vi->dev) ||
1809 tegra_vi_graph_find_entity(chan, remote)) {
1810 fwnode_handle_put(remote);
1814 tvge = v4l2_async_notifier_add_fwnode_subdev(&chan->notifier, remote,
1815 struct tegra_vi_graph_entity);
1817 ret = PTR_ERR(tvge);
1819 "failed to add subdev to notifier: %d\n", ret);
1820 fwnode_handle_put(remote);
1824 ret = tegra_vi_graph_parse_one(chan, remote);
1826 fwnode_handle_put(remote);
1830 fwnode_handle_put(remote);
1836 dev_err(vi->dev, "failed parsing the graph: %d\n", ret);
1837 v4l2_async_notifier_cleanup(&chan->notifier);
1842 static int tegra_vi_graph_init(struct tegra_vi *vi)
1844 struct tegra_video_device *vid = dev_get_drvdata(vi->client.host);
1845 struct tegra_vi_channel *chan;
1846 struct fwnode_handle *fwnode = dev_fwnode(vi->dev);
1848 struct fwnode_handle *remote = NULL;
1851 * Walk the links to parse the full graph. Each channel will have
1852 * one endpoint of the composite node. Start by parsing the
1853 * composite node and parse the remote entities in turn.
1854 * Each channel will register v4l2 async notifier to make the graph
1855 * independent between the channels so we can the current channel
1856 * in case of something wrong during graph parsing and continue with
1859 list_for_each_entry(chan, &vi->vi_chans, list) {
1860 remote = fwnode_graph_get_remote_node(fwnode, chan->portnos[0],
1865 ret = tegra_vi_graph_parse_one(chan, remote);
1866 fwnode_handle_put(remote);
1867 if (ret < 0 || list_empty(&chan->notifier.asd_list))
1870 chan->notifier.ops = &tegra_vi_async_ops;
1871 ret = v4l2_async_notifier_register(&vid->v4l2_dev,
1875 "failed to register channel %d notifier: %d\n",
1876 chan->portnos[0], ret);
1877 v4l2_async_notifier_cleanup(&chan->notifier);
1884 static void tegra_vi_graph_cleanup(struct tegra_vi *vi)
1886 struct tegra_vi_channel *chan;
1888 list_for_each_entry(chan, &vi->vi_chans, list) {
1889 vb2_video_unregister_device(&chan->video);
1890 v4l2_async_notifier_unregister(&chan->notifier);
1891 v4l2_async_notifier_cleanup(&chan->notifier);
1895 static int tegra_vi_init(struct host1x_client *client)
1897 struct tegra_video_device *vid = dev_get_drvdata(client->host);
1898 struct tegra_vi *vi = host1x_client_to_vi(client);
1899 struct tegra_vi_channel *chan, *tmp;
1902 vid->media_dev.hw_revision = vi->soc->hw_revision;
1903 snprintf(vid->media_dev.bus_info, sizeof(vid->media_dev.bus_info),
1904 "platform:%s", dev_name(vi->dev));
1906 INIT_LIST_HEAD(&vi->vi_chans);
1908 if (IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
1909 ret = tegra_vi_tpg_channels_alloc(vi);
1911 ret = tegra_vi_channels_alloc(vi);
1914 "failed to allocate vi channels: %d\n", ret);
1918 ret = tegra_vi_channels_init(vi);
1924 if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)) {
1925 ret = tegra_vi_graph_init(vi);
1933 list_for_each_entry(chan, &vi->vi_chans, list)
1934 tegra_channel_cleanup(chan);
1936 list_for_each_entry_safe(chan, tmp, &vi->vi_chans, list) {
1937 list_del(&chan->list);
1944 static int tegra_vi_exit(struct host1x_client *client)
1946 struct tegra_vi *vi = host1x_client_to_vi(client);
1949 * Do not cleanup the channels here as application might still be
1950 * holding video device nodes. Channels cleanup will happen during
1951 * v4l2_device release callback which gets called after all video
1952 * device nodes are released.
1955 if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
1956 tegra_vi_graph_cleanup(vi);
1961 static const struct host1x_client_ops vi_client_ops = {
1962 .init = tegra_vi_init,
1963 .exit = tegra_vi_exit,
1966 static int tegra_vi_probe(struct platform_device *pdev)
1968 struct tegra_vi *vi;
1971 vi = devm_kzalloc(&pdev->dev, sizeof(*vi), GFP_KERNEL);
1975 vi->iomem = devm_platform_ioremap_resource(pdev, 0);
1976 if (IS_ERR(vi->iomem))
1977 return PTR_ERR(vi->iomem);
1979 vi->soc = of_device_get_match_data(&pdev->dev);
1981 vi->clk = devm_clk_get(&pdev->dev, NULL);
1982 if (IS_ERR(vi->clk)) {
1983 ret = PTR_ERR(vi->clk);
1984 dev_err(&pdev->dev, "failed to get vi clock: %d\n", ret);
1988 vi->vdd = devm_regulator_get(&pdev->dev, "avdd-dsi-csi");
1989 if (IS_ERR(vi->vdd)) {
1990 ret = PTR_ERR(vi->vdd);
1991 dev_err(&pdev->dev, "failed to get VDD supply: %d\n", ret);
1995 if (!pdev->dev.pm_domain) {
1997 dev_warn(&pdev->dev, "PM domain is not attached: %d\n", ret);
2001 ret = devm_of_platform_populate(&pdev->dev);
2004 "failed to populate vi child device: %d\n", ret);
2008 vi->dev = &pdev->dev;
2009 vi->ops = vi->soc->ops;
2010 platform_set_drvdata(pdev, vi);
2011 pm_runtime_enable(&pdev->dev);
2013 /* initialize host1x interface */
2014 INIT_LIST_HEAD(&vi->client.list);
2015 vi->client.ops = &vi_client_ops;
2016 vi->client.dev = &pdev->dev;
2018 ret = host1x_client_register(&vi->client);
2021 "failed to register host1x client: %d\n", ret);
2028 pm_runtime_disable(&pdev->dev);
2032 static int tegra_vi_remove(struct platform_device *pdev)
2034 struct tegra_vi *vi = platform_get_drvdata(pdev);
2037 err = host1x_client_unregister(&vi->client);
2040 "failed to unregister host1x client: %d\n", err);
2044 pm_runtime_disable(&pdev->dev);
2049 static const struct of_device_id tegra_vi_of_id_table[] = {
2050 #if defined(CONFIG_ARCH_TEGRA_210_SOC)
2051 { .compatible = "nvidia,tegra210-vi", .data = &tegra210_vi_soc },
2055 MODULE_DEVICE_TABLE(of, tegra_vi_of_id_table);
2057 static const struct dev_pm_ops tegra_vi_pm_ops = {
2058 SET_RUNTIME_PM_OPS(vi_runtime_suspend, vi_runtime_resume, NULL)
2061 struct platform_driver tegra_vi_driver = {
2064 .of_match_table = tegra_vi_of_id_table,
2065 .pm = &tegra_vi_pm_ops,
2067 .probe = tegra_vi_probe,
2068 .remove = tegra_vi_remove,