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_graph.h>
15 #include <linux/of_platform.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 3
36 * struct tegra_vi_graph_entity - Entity in the video graph
38 * @asd: subdev asynchronous registration information
39 * @entity: media entity from the corresponding V4L2 subdev
40 * @subdev: V4L2 subdev
42 struct tegra_vi_graph_entity {
43 struct v4l2_async_connection asd;
44 struct media_entity *entity;
45 struct v4l2_subdev *subdev;
48 static inline struct tegra_vi *
49 host1x_client_to_vi(struct host1x_client *client)
51 return container_of(client, struct tegra_vi, client);
54 static inline struct tegra_channel_buffer *
55 to_tegra_channel_buffer(struct vb2_v4l2_buffer *vb)
57 return container_of(vb, struct tegra_channel_buffer, buf);
60 static inline struct tegra_vi_graph_entity *
61 to_tegra_vi_graph_entity(struct v4l2_async_connection *asd)
63 return container_of(asd, struct tegra_vi_graph_entity, asd);
66 static int tegra_get_format_idx_by_code(struct tegra_vi *vi,
72 for (i = offset; i < vi->soc->nformats; ++i) {
73 if (vi->soc->video_formats[i].code == code)
80 static u32 tegra_get_format_fourcc_by_idx(struct tegra_vi *vi,
83 if (index >= vi->soc->nformats)
86 return vi->soc->video_formats[index].fourcc;
89 static const struct tegra_video_format *
90 tegra_get_format_by_fourcc(struct tegra_vi *vi, u32 fourcc)
94 for (i = 0; i < vi->soc->nformats; ++i) {
95 if (vi->soc->video_formats[i].fourcc == fourcc)
96 return &vi->soc->video_formats[i];
103 * videobuf2 queue operations
106 static int tegra_channel_queue_setup(struct vb2_queue *vq,
107 unsigned int *nbuffers,
108 unsigned int *nplanes,
109 unsigned int sizes[],
110 struct device *alloc_devs[])
112 struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
115 return sizes[0] < chan->format.sizeimage ? -EINVAL : 0;
118 sizes[0] = chan->format.sizeimage;
119 alloc_devs[0] = chan->vi->dev;
121 if (chan->vi->ops->channel_queue_setup)
122 chan->vi->ops->channel_queue_setup(chan);
127 static int tegra_channel_buffer_prepare(struct vb2_buffer *vb)
129 struct tegra_vi_channel *chan = vb2_get_drv_priv(vb->vb2_queue);
130 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
131 struct tegra_channel_buffer *buf = to_tegra_channel_buffer(vbuf);
132 unsigned long size = chan->format.sizeimage;
134 if (vb2_plane_size(vb, 0) < size) {
135 v4l2_err(chan->video.v4l2_dev,
136 "buffer too small (%lu < %lu)\n",
137 vb2_plane_size(vb, 0), size);
141 vb2_set_plane_payload(vb, 0, size);
143 buf->addr = vb2_dma_contig_plane_dma_addr(vb, 0);
148 static void tegra_channel_buffer_queue(struct vb2_buffer *vb)
150 struct tegra_vi_channel *chan = vb2_get_drv_priv(vb->vb2_queue);
151 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
152 struct tegra_channel_buffer *buf = to_tegra_channel_buffer(vbuf);
154 /* put buffer into the capture queue */
155 spin_lock(&chan->start_lock);
156 list_add_tail(&buf->queue, &chan->capture);
157 spin_unlock(&chan->start_lock);
159 /* wait up kthread for capture */
160 wake_up_interruptible(&chan->start_wait);
164 tegra_channel_get_remote_csi_subdev(struct tegra_vi_channel *chan)
166 struct media_pad *pad;
168 pad = media_pad_remote_pad_first(&chan->pad);
172 return media_entity_to_v4l2_subdev(pad->entity);
176 * Walk up the chain until the initial source (e.g. image sensor)
179 tegra_channel_get_remote_source_subdev(struct tegra_vi_channel *chan)
181 struct media_pad *pad;
182 struct v4l2_subdev *subdev;
183 struct media_entity *entity;
185 subdev = tegra_channel_get_remote_csi_subdev(chan);
189 pad = &subdev->entity.pads[0];
190 while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
191 pad = media_pad_remote_pad_first(pad);
192 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
194 entity = pad->entity;
195 pad = &entity->pads[0];
196 subdev = media_entity_to_v4l2_subdev(entity);
202 static int tegra_channel_enable_stream(struct tegra_vi_channel *chan)
204 struct v4l2_subdev *subdev;
207 subdev = tegra_channel_get_remote_csi_subdev(chan);
208 ret = v4l2_subdev_call(subdev, video, s_stream, true);
209 if (ret < 0 && ret != -ENOIOCTLCMD)
215 static int tegra_channel_disable_stream(struct tegra_vi_channel *chan)
217 struct v4l2_subdev *subdev;
220 subdev = tegra_channel_get_remote_csi_subdev(chan);
221 ret = v4l2_subdev_call(subdev, video, s_stream, false);
222 if (ret < 0 && ret != -ENOIOCTLCMD)
228 int tegra_channel_set_stream(struct tegra_vi_channel *chan, bool on)
233 ret = tegra_channel_enable_stream(chan);
235 ret = tegra_channel_disable_stream(chan);
240 void tegra_channel_release_buffers(struct tegra_vi_channel *chan,
241 enum vb2_buffer_state state)
243 struct tegra_channel_buffer *buf, *nbuf;
245 spin_lock(&chan->start_lock);
246 list_for_each_entry_safe(buf, nbuf, &chan->capture, queue) {
247 vb2_buffer_done(&buf->buf.vb2_buf, state);
248 list_del(&buf->queue);
250 spin_unlock(&chan->start_lock);
252 spin_lock(&chan->done_lock);
253 list_for_each_entry_safe(buf, nbuf, &chan->done, queue) {
254 vb2_buffer_done(&buf->buf.vb2_buf, state);
255 list_del(&buf->queue);
257 spin_unlock(&chan->done_lock);
260 static int tegra_channel_start_streaming(struct vb2_queue *vq, u32 count)
262 struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
265 ret = pm_runtime_resume_and_get(chan->vi->dev);
267 dev_err(chan->vi->dev, "failed to get runtime PM: %d\n", ret);
271 ret = chan->vi->ops->vi_start_streaming(vq, count);
273 pm_runtime_put(chan->vi->dev);
278 static void tegra_channel_stop_streaming(struct vb2_queue *vq)
280 struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
282 chan->vi->ops->vi_stop_streaming(vq);
283 pm_runtime_put(chan->vi->dev);
286 static const struct vb2_ops tegra_channel_queue_qops = {
287 .queue_setup = tegra_channel_queue_setup,
288 .buf_prepare = tegra_channel_buffer_prepare,
289 .buf_queue = tegra_channel_buffer_queue,
290 .wait_prepare = vb2_ops_wait_prepare,
291 .wait_finish = vb2_ops_wait_finish,
292 .start_streaming = tegra_channel_start_streaming,
293 .stop_streaming = tegra_channel_stop_streaming,
297 * V4L2 ioctl operations
299 static int tegra_channel_querycap(struct file *file, void *fh,
300 struct v4l2_capability *cap)
302 struct tegra_vi_channel *chan = video_drvdata(file);
304 strscpy(cap->driver, "tegra-video", sizeof(cap->driver));
305 strscpy(cap->card, chan->video.name, sizeof(cap->card));
306 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
307 dev_name(chan->vi->dev));
312 static int tegra_channel_g_parm(struct file *file, void *fh,
313 struct v4l2_streamparm *a)
315 struct tegra_vi_channel *chan = video_drvdata(file);
316 struct v4l2_subdev *subdev;
318 subdev = tegra_channel_get_remote_source_subdev(chan);
319 return v4l2_g_parm_cap(&chan->video, subdev, a);
322 static int tegra_channel_s_parm(struct file *file, void *fh,
323 struct v4l2_streamparm *a)
325 struct tegra_vi_channel *chan = video_drvdata(file);
326 struct v4l2_subdev *subdev;
328 subdev = tegra_channel_get_remote_source_subdev(chan);
329 return v4l2_s_parm_cap(&chan->video, subdev, a);
332 static int tegra_channel_enum_framesizes(struct file *file, void *fh,
333 struct v4l2_frmsizeenum *sizes)
336 struct tegra_vi_channel *chan = video_drvdata(file);
337 struct v4l2_subdev *subdev;
338 const struct tegra_video_format *fmtinfo;
339 struct v4l2_subdev_frame_size_enum fse = {
340 .index = sizes->index,
341 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
344 fmtinfo = tegra_get_format_by_fourcc(chan->vi, sizes->pixel_format);
348 fse.code = fmtinfo->code;
350 subdev = tegra_channel_get_remote_source_subdev(chan);
351 ret = v4l2_subdev_call(subdev, pad, enum_frame_size, NULL, &fse);
355 sizes->type = V4L2_FRMSIZE_TYPE_DISCRETE;
356 sizes->discrete.width = fse.max_width;
357 sizes->discrete.height = fse.max_height;
362 static int tegra_channel_enum_frameintervals(struct file *file, void *fh,
363 struct v4l2_frmivalenum *ivals)
366 struct tegra_vi_channel *chan = video_drvdata(file);
367 struct v4l2_subdev *subdev;
368 const struct tegra_video_format *fmtinfo;
369 struct v4l2_subdev_frame_interval_enum fie = {
370 .index = ivals->index,
371 .width = ivals->width,
372 .height = ivals->height,
373 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
376 fmtinfo = tegra_get_format_by_fourcc(chan->vi, ivals->pixel_format);
380 fie.code = fmtinfo->code;
382 subdev = tegra_channel_get_remote_source_subdev(chan);
383 ret = v4l2_subdev_call(subdev, pad, enum_frame_interval, NULL, &fie);
387 ivals->type = V4L2_FRMIVAL_TYPE_DISCRETE;
388 ivals->discrete.numerator = fie.interval.numerator;
389 ivals->discrete.denominator = fie.interval.denominator;
394 static int tegra_channel_enum_format(struct file *file, void *fh,
395 struct v4l2_fmtdesc *f)
397 struct tegra_vi_channel *chan = video_drvdata(file);
398 unsigned int index = 0, i;
399 unsigned long *fmts_bitmap = chan->tpg_fmts_bitmap;
401 if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
402 fmts_bitmap = chan->fmts_bitmap;
404 if (f->index >= bitmap_weight(fmts_bitmap, MAX_FORMAT_NUM))
407 for (i = 0; i < f->index + 1; i++, index++)
408 index = find_next_bit(fmts_bitmap, MAX_FORMAT_NUM, index);
410 f->pixelformat = tegra_get_format_fourcc_by_idx(chan->vi, index - 1);
415 static int tegra_channel_get_format(struct file *file, void *fh,
416 struct v4l2_format *format)
418 struct tegra_vi_channel *chan = video_drvdata(file);
420 format->fmt.pix = chan->format;
425 static int __tegra_channel_try_format(struct tegra_vi_channel *chan,
426 struct v4l2_pix_format *pix)
428 const struct tegra_video_format *fmtinfo;
429 static struct lock_class_key key;
430 struct v4l2_subdev *subdev;
431 struct v4l2_subdev_format fmt = {
432 .which = V4L2_SUBDEV_FORMAT_TRY,
434 struct v4l2_subdev_state *sd_state;
435 struct v4l2_subdev_frame_size_enum fse = {
436 .which = V4L2_SUBDEV_FORMAT_TRY,
438 struct v4l2_subdev_selection sdsel = {
439 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
440 .target = V4L2_SEL_TGT_CROP_BOUNDS,
444 subdev = tegra_channel_get_remote_source_subdev(chan);
449 * FIXME: Drop this call, drivers are not supposed to use
450 * __v4l2_subdev_state_alloc().
452 sd_state = __v4l2_subdev_state_alloc(subdev, "tegra:state->lock",
454 if (IS_ERR(sd_state))
455 return PTR_ERR(sd_state);
457 * Retrieve the format information and if requested format isn't
458 * supported, keep the current format.
460 fmtinfo = tegra_get_format_by_fourcc(chan->vi, pix->pixelformat);
462 pix->pixelformat = chan->format.pixelformat;
463 pix->colorspace = chan->format.colorspace;
464 fmtinfo = tegra_get_format_by_fourcc(chan->vi,
468 pix->field = V4L2_FIELD_NONE;
470 v4l2_fill_mbus_format(&fmt.format, pix, fmtinfo->code);
473 * Attempt to obtain the format size from subdev.
474 * If not available, try to get crop boundary from subdev.
476 fse.code = fmtinfo->code;
477 ret = v4l2_subdev_call(subdev, pad, enum_frame_size, sd_state, &fse);
479 if (!v4l2_subdev_has_op(subdev, pad, get_selection)) {
480 sd_state->pads->try_crop.width = 0;
481 sd_state->pads->try_crop.height = 0;
483 ret = v4l2_subdev_call(subdev, pad, get_selection,
488 sd_state->pads->try_crop.width = sdsel.r.width;
489 sd_state->pads->try_crop.height = sdsel.r.height;
492 sd_state->pads->try_crop.width = fse.max_width;
493 sd_state->pads->try_crop.height = fse.max_height;
496 ret = v4l2_subdev_call(subdev, pad, set_fmt, sd_state, &fmt);
500 v4l2_fill_pix_format(pix, &fmt.format);
501 chan->vi->ops->vi_fmt_align(pix, fmtinfo->bpp);
503 __v4l2_subdev_state_free(sd_state);
508 static int tegra_channel_try_format(struct file *file, void *fh,
509 struct v4l2_format *format)
511 struct tegra_vi_channel *chan = video_drvdata(file);
513 return __tegra_channel_try_format(chan, &format->fmt.pix);
516 static void tegra_channel_update_gangports(struct tegra_vi_channel *chan)
518 if (chan->format.width <= 1920)
519 chan->numgangports = 1;
521 chan->numgangports = chan->totalports;
524 static int tegra_channel_set_format(struct file *file, void *fh,
525 struct v4l2_format *format)
527 struct tegra_vi_channel *chan = video_drvdata(file);
528 const struct tegra_video_format *fmtinfo;
529 struct v4l2_subdev_format fmt = {
530 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
532 struct v4l2_subdev *subdev;
533 struct v4l2_pix_format *pix = &format->fmt.pix;
536 if (vb2_is_busy(&chan->queue))
539 /* get supported format by try_fmt */
540 ret = __tegra_channel_try_format(chan, pix);
544 fmtinfo = tegra_get_format_by_fourcc(chan->vi, pix->pixelformat);
547 v4l2_fill_mbus_format(&fmt.format, pix, fmtinfo->code);
548 subdev = tegra_channel_get_remote_source_subdev(chan);
549 ret = v4l2_subdev_call(subdev, pad, set_fmt, NULL, &fmt);
553 v4l2_fill_pix_format(pix, &fmt.format);
554 chan->vi->ops->vi_fmt_align(pix, fmtinfo->bpp);
557 chan->fmtinfo = fmtinfo;
558 tegra_channel_update_gangports(chan);
563 static int tegra_channel_set_subdev_active_fmt(struct tegra_vi_channel *chan)
566 struct v4l2_subdev *subdev;
567 struct v4l2_subdev_format fmt = {
568 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
572 * Initialize channel format to the sub-device active format if there
573 * is corresponding match in the Tegra supported video formats.
575 subdev = tegra_channel_get_remote_source_subdev(chan);
576 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
580 index = tegra_get_format_idx_by_code(chan->vi, fmt.format.code, 0);
584 chan->fmtinfo = &chan->vi->soc->video_formats[index];
585 v4l2_fill_pix_format(&chan->format, &fmt.format);
586 chan->format.pixelformat = chan->fmtinfo->fourcc;
587 chan->format.bytesperline = chan->format.width * chan->fmtinfo->bpp;
588 chan->format.sizeimage = chan->format.bytesperline *
590 chan->vi->ops->vi_fmt_align(&chan->format, chan->fmtinfo->bpp);
591 tegra_channel_update_gangports(chan);
597 tegra_channel_subscribe_event(struct v4l2_fh *fh,
598 const struct v4l2_event_subscription *sub)
601 case V4L2_EVENT_SOURCE_CHANGE:
602 return v4l2_event_subscribe(fh, sub, 4, NULL);
605 return v4l2_ctrl_subscribe_event(fh, sub);
608 static int tegra_channel_g_selection(struct file *file, void *priv,
609 struct v4l2_selection *sel)
611 struct tegra_vi_channel *chan = video_drvdata(file);
612 struct v4l2_subdev *subdev;
613 struct v4l2_subdev_format fmt = {
614 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
616 struct v4l2_subdev_selection sdsel = {
617 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
618 .target = sel->target,
622 subdev = tegra_channel_get_remote_source_subdev(chan);
623 if (!v4l2_subdev_has_op(subdev, pad, get_selection))
626 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
629 * Try the get selection operation and fallback to get format if not
632 ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
635 if (ret != -ENOIOCTLCMD)
638 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
644 sel->r.width = fmt.format.width;
645 sel->r.height = fmt.format.height;
650 static int tegra_channel_s_selection(struct file *file, void *fh,
651 struct v4l2_selection *sel)
653 struct tegra_vi_channel *chan = video_drvdata(file);
654 struct v4l2_subdev *subdev;
656 struct v4l2_subdev_selection sdsel = {
657 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
658 .target = sel->target,
663 subdev = tegra_channel_get_remote_source_subdev(chan);
664 if (!v4l2_subdev_has_op(subdev, pad, set_selection))
667 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
670 if (vb2_is_busy(&chan->queue))
673 ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel);
677 * Subdev active format resolution may have changed during
678 * set selection operation. So, update channel format to
679 * the sub-device active format.
681 return tegra_channel_set_subdev_active_fmt(chan);
687 static int tegra_channel_g_edid(struct file *file, void *fh,
688 struct v4l2_edid *edid)
690 struct tegra_vi_channel *chan = video_drvdata(file);
691 struct v4l2_subdev *subdev;
693 subdev = tegra_channel_get_remote_source_subdev(chan);
694 if (!v4l2_subdev_has_op(subdev, pad, get_edid))
697 return v4l2_subdev_call(subdev, pad, get_edid, edid);
700 static int tegra_channel_s_edid(struct file *file, void *fh,
701 struct v4l2_edid *edid)
703 struct tegra_vi_channel *chan = video_drvdata(file);
704 struct v4l2_subdev *subdev;
706 subdev = tegra_channel_get_remote_source_subdev(chan);
707 if (!v4l2_subdev_has_op(subdev, pad, set_edid))
710 return v4l2_subdev_call(subdev, pad, set_edid, edid);
713 static int tegra_channel_g_dv_timings(struct file *file, void *fh,
714 struct v4l2_dv_timings *timings)
716 struct tegra_vi_channel *chan = video_drvdata(file);
717 struct v4l2_subdev *subdev;
719 subdev = tegra_channel_get_remote_source_subdev(chan);
720 if (!v4l2_subdev_has_op(subdev, video, g_dv_timings))
723 return v4l2_device_call_until_err(chan->video.v4l2_dev, 0,
724 video, g_dv_timings, timings);
727 static int tegra_channel_s_dv_timings(struct file *file, void *fh,
728 struct v4l2_dv_timings *timings)
730 struct tegra_vi_channel *chan = video_drvdata(file);
731 struct v4l2_subdev *subdev;
732 struct v4l2_bt_timings *bt = &timings->bt;
733 struct v4l2_dv_timings curr_timings;
736 subdev = tegra_channel_get_remote_source_subdev(chan);
737 if (!v4l2_subdev_has_op(subdev, video, s_dv_timings))
740 ret = tegra_channel_g_dv_timings(file, fh, &curr_timings);
744 if (v4l2_match_dv_timings(timings, &curr_timings, 0, false))
747 if (vb2_is_busy(&chan->queue))
750 ret = v4l2_device_call_until_err(chan->video.v4l2_dev, 0,
751 video, s_dv_timings, timings);
755 chan->format.width = bt->width;
756 chan->format.height = bt->height;
757 chan->format.bytesperline = bt->width * chan->fmtinfo->bpp;
758 chan->format.sizeimage = chan->format.bytesperline * bt->height;
759 chan->vi->ops->vi_fmt_align(&chan->format, chan->fmtinfo->bpp);
760 tegra_channel_update_gangports(chan);
765 static int tegra_channel_query_dv_timings(struct file *file, void *fh,
766 struct v4l2_dv_timings *timings)
768 struct tegra_vi_channel *chan = video_drvdata(file);
769 struct v4l2_subdev *subdev;
771 subdev = tegra_channel_get_remote_source_subdev(chan);
772 if (!v4l2_subdev_has_op(subdev, video, query_dv_timings))
775 return v4l2_device_call_until_err(chan->video.v4l2_dev, 0,
776 video, query_dv_timings, timings);
779 static int tegra_channel_enum_dv_timings(struct file *file, void *fh,
780 struct v4l2_enum_dv_timings *timings)
782 struct tegra_vi_channel *chan = video_drvdata(file);
783 struct v4l2_subdev *subdev;
785 subdev = tegra_channel_get_remote_source_subdev(chan);
786 if (!v4l2_subdev_has_op(subdev, pad, enum_dv_timings))
789 return v4l2_subdev_call(subdev, pad, enum_dv_timings, timings);
792 static int tegra_channel_dv_timings_cap(struct file *file, void *fh,
793 struct v4l2_dv_timings_cap *cap)
795 struct tegra_vi_channel *chan = video_drvdata(file);
796 struct v4l2_subdev *subdev;
798 subdev = tegra_channel_get_remote_source_subdev(chan);
799 if (!v4l2_subdev_has_op(subdev, pad, dv_timings_cap))
802 return v4l2_subdev_call(subdev, pad, dv_timings_cap, cap);
805 static int tegra_channel_log_status(struct file *file, void *fh)
807 struct tegra_vi_channel *chan = video_drvdata(file);
809 v4l2_device_call_all(chan->video.v4l2_dev, 0, core, log_status);
814 static int tegra_channel_enum_input(struct file *file, void *fh,
815 struct v4l2_input *inp)
817 struct tegra_vi_channel *chan = video_drvdata(file);
818 struct v4l2_subdev *subdev;
823 inp->type = V4L2_INPUT_TYPE_CAMERA;
824 subdev = tegra_channel_get_remote_source_subdev(chan);
825 strscpy(inp->name, subdev->name, sizeof(inp->name));
826 if (v4l2_subdev_has_op(subdev, pad, dv_timings_cap))
827 inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
832 static int tegra_channel_g_input(struct file *file, void *priv,
840 static int tegra_channel_s_input(struct file *file, void *priv,
849 static const struct v4l2_ioctl_ops tegra_channel_ioctl_ops = {
850 .vidioc_querycap = tegra_channel_querycap,
851 .vidioc_g_parm = tegra_channel_g_parm,
852 .vidioc_s_parm = tegra_channel_s_parm,
853 .vidioc_enum_framesizes = tegra_channel_enum_framesizes,
854 .vidioc_enum_frameintervals = tegra_channel_enum_frameintervals,
855 .vidioc_enum_fmt_vid_cap = tegra_channel_enum_format,
856 .vidioc_g_fmt_vid_cap = tegra_channel_get_format,
857 .vidioc_s_fmt_vid_cap = tegra_channel_set_format,
858 .vidioc_try_fmt_vid_cap = tegra_channel_try_format,
859 .vidioc_enum_input = tegra_channel_enum_input,
860 .vidioc_g_input = tegra_channel_g_input,
861 .vidioc_s_input = tegra_channel_s_input,
862 .vidioc_reqbufs = vb2_ioctl_reqbufs,
863 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
864 .vidioc_querybuf = vb2_ioctl_querybuf,
865 .vidioc_qbuf = vb2_ioctl_qbuf,
866 .vidioc_dqbuf = vb2_ioctl_dqbuf,
867 .vidioc_create_bufs = vb2_ioctl_create_bufs,
868 .vidioc_expbuf = vb2_ioctl_expbuf,
869 .vidioc_streamon = vb2_ioctl_streamon,
870 .vidioc_streamoff = vb2_ioctl_streamoff,
871 .vidioc_subscribe_event = tegra_channel_subscribe_event,
872 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
873 .vidioc_g_selection = tegra_channel_g_selection,
874 .vidioc_s_selection = tegra_channel_s_selection,
875 .vidioc_g_edid = tegra_channel_g_edid,
876 .vidioc_s_edid = tegra_channel_s_edid,
877 .vidioc_g_dv_timings = tegra_channel_g_dv_timings,
878 .vidioc_s_dv_timings = tegra_channel_s_dv_timings,
879 .vidioc_query_dv_timings = tegra_channel_query_dv_timings,
880 .vidioc_enum_dv_timings = tegra_channel_enum_dv_timings,
881 .vidioc_dv_timings_cap = tegra_channel_dv_timings_cap,
882 .vidioc_log_status = tegra_channel_log_status,
886 * V4L2 file operations
888 static const struct v4l2_file_operations tegra_channel_fops = {
889 .owner = THIS_MODULE,
890 .unlocked_ioctl = video_ioctl2,
891 .open = v4l2_fh_open,
892 .release = vb2_fop_release,
893 .read = vb2_fop_read,
894 .poll = vb2_fop_poll,
895 .mmap = vb2_fop_mmap,
899 * V4L2 control operations
901 static int vi_s_ctrl(struct v4l2_ctrl *ctrl)
903 struct tegra_vi_channel *chan = container_of(ctrl->handler,
904 struct tegra_vi_channel,
908 case V4L2_CID_TEST_PATTERN:
909 /* pattern change takes effect on next stream */
910 chan->pg_mode = ctrl->val + 1;
912 case V4L2_CID_TEGRA_SYNCPT_TIMEOUT_RETRY:
913 chan->syncpt_timeout_retry = ctrl->val;
916 chan->hflip = ctrl->val;
919 chan->vflip = ctrl->val;
928 static const struct v4l2_ctrl_ops vi_ctrl_ops = {
932 #if IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)
933 static const char *const vi_pattern_strings[] = {
934 "Black/White Direct Mode",
938 static const struct v4l2_ctrl_config syncpt_timeout_ctrl = {
940 .id = V4L2_CID_TEGRA_SYNCPT_TIMEOUT_RETRY,
941 .name = "Syncpt timeout retry",
942 .type = V4L2_CTRL_TYPE_INTEGER,
950 static int tegra_channel_setup_ctrl_handler(struct tegra_vi_channel *chan)
954 #if IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)
955 /* add test pattern control handler to v4l2 device */
956 v4l2_ctrl_new_std_menu_items(&chan->ctrl_handler, &vi_ctrl_ops,
957 V4L2_CID_TEST_PATTERN,
958 ARRAY_SIZE(vi_pattern_strings) - 1,
959 0, 0, vi_pattern_strings);
960 if (chan->ctrl_handler.error) {
961 dev_err(chan->vi->dev, "failed to add TPG ctrl handler: %d\n",
962 chan->ctrl_handler.error);
963 v4l2_ctrl_handler_free(&chan->ctrl_handler);
964 return chan->ctrl_handler.error;
967 struct v4l2_subdev *subdev;
970 v4l2_ctrl_new_custom(&chan->ctrl_handler, &syncpt_timeout_ctrl, NULL);
971 if (chan->ctrl_handler.error) {
972 dev_err(chan->vi->dev, "failed to add %s ctrl handler: %d\n",
973 syncpt_timeout_ctrl.name,
974 chan->ctrl_handler.error);
975 v4l2_ctrl_handler_free(&chan->ctrl_handler);
976 return chan->ctrl_handler.error;
979 subdev = tegra_channel_get_remote_source_subdev(chan);
983 ret = v4l2_ctrl_add_handler(&chan->ctrl_handler, subdev->ctrl_handler,
986 dev_err(chan->vi->dev,
987 "failed to add subdev %s ctrl handler: %d\n",
989 v4l2_ctrl_handler_free(&chan->ctrl_handler);
993 if (chan->vi->soc->has_h_v_flip) {
994 v4l2_ctrl_new_std(&chan->ctrl_handler, &vi_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
995 v4l2_ctrl_new_std(&chan->ctrl_handler, &vi_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
1000 /* setup the controls */
1001 ret = v4l2_ctrl_handler_setup(&chan->ctrl_handler);
1003 dev_err(chan->vi->dev,
1004 "failed to setup v4l2 ctrl handler: %d\n", ret);
1011 /* VI only support 2 formats in TPG mode */
1012 static void vi_tpg_fmts_bitmap_init(struct tegra_vi_channel *chan)
1016 bitmap_zero(chan->tpg_fmts_bitmap, MAX_FORMAT_NUM);
1018 index = tegra_get_format_idx_by_code(chan->vi,
1019 MEDIA_BUS_FMT_SRGGB10_1X10, 0);
1020 bitmap_set(chan->tpg_fmts_bitmap, index, 1);
1022 index = tegra_get_format_idx_by_code(chan->vi,
1023 MEDIA_BUS_FMT_RGB888_1X32_PADHI,
1025 bitmap_set(chan->tpg_fmts_bitmap, index, 1);
1028 static int vi_fmts_bitmap_init(struct tegra_vi_channel *chan)
1030 int index, ret, match_code = 0;
1031 struct v4l2_subdev *subdev;
1032 struct v4l2_subdev_mbus_code_enum code = {
1033 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1036 bitmap_zero(chan->fmts_bitmap, MAX_FORMAT_NUM);
1039 * Set the bitmap bits based on all the matched formats between the
1040 * available media bus formats of sub-device and the pre-defined Tegra
1041 * supported video formats.
1043 subdev = tegra_channel_get_remote_source_subdev(chan);
1045 ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
1050 index = tegra_get_format_idx_by_code(chan->vi, code.code, 0);
1051 while (index >= 0) {
1052 bitmap_set(chan->fmts_bitmap, index, 1);
1054 match_code = code.code;
1055 /* look for other formats with same mbus code */
1056 index = tegra_get_format_idx_by_code(chan->vi,
1065 * Set the bitmap bit corresponding to default tegra video format if
1066 * there are no matched formats.
1069 match_code = chan->vi->soc->default_video_format->code;
1070 index = tegra_get_format_idx_by_code(chan->vi, match_code, 0);
1071 if (WARN_ON(index < 0))
1074 bitmap_set(chan->fmts_bitmap, index, 1);
1077 /* initialize channel format to the sub-device active format */
1078 tegra_channel_set_subdev_active_fmt(chan);
1083 static void tegra_channel_cleanup(struct tegra_vi_channel *chan)
1085 v4l2_ctrl_handler_free(&chan->ctrl_handler);
1086 media_entity_cleanup(&chan->video.entity);
1087 chan->vi->ops->channel_host1x_syncpt_free(chan);
1088 mutex_destroy(&chan->video_lock);
1091 void tegra_channels_cleanup(struct tegra_vi *vi)
1093 struct tegra_vi_channel *chan, *tmp;
1098 list_for_each_entry_safe(chan, tmp, &vi->vi_chans, list) {
1099 tegra_channel_cleanup(chan);
1100 list_del(&chan->list);
1105 static int tegra_channel_init(struct tegra_vi_channel *chan)
1107 struct tegra_vi *vi = chan->vi;
1108 struct tegra_video_device *vid = dev_get_drvdata(vi->client.host);
1111 mutex_init(&chan->video_lock);
1112 INIT_LIST_HEAD(&chan->capture);
1113 INIT_LIST_HEAD(&chan->done);
1114 spin_lock_init(&chan->start_lock);
1115 spin_lock_init(&chan->done_lock);
1116 init_waitqueue_head(&chan->start_wait);
1117 init_waitqueue_head(&chan->done_wait);
1119 /* initialize the video format */
1120 chan->fmtinfo = chan->vi->soc->default_video_format;
1121 chan->format.pixelformat = chan->fmtinfo->fourcc;
1122 chan->format.colorspace = V4L2_COLORSPACE_SRGB;
1123 chan->format.field = V4L2_FIELD_NONE;
1124 chan->format.width = TEGRA_DEF_WIDTH;
1125 chan->format.height = TEGRA_DEF_HEIGHT;
1126 chan->format.bytesperline = TEGRA_DEF_WIDTH * chan->fmtinfo->bpp;
1127 chan->format.sizeimage = chan->format.bytesperline * TEGRA_DEF_HEIGHT;
1128 vi->ops->vi_fmt_align(&chan->format, chan->fmtinfo->bpp);
1130 ret = vi->ops->channel_host1x_syncpt_init(chan);
1134 /* initialize the media entity */
1135 chan->pad.flags = MEDIA_PAD_FL_SINK;
1136 ret = media_entity_pads_init(&chan->video.entity, 1, &chan->pad);
1139 "failed to initialize media entity: %d\n", ret);
1143 ret = v4l2_ctrl_handler_init(&chan->ctrl_handler, MAX_CID_CONTROLS);
1144 if (chan->ctrl_handler.error) {
1146 "failed to initialize v4l2 ctrl handler: %d\n", ret);
1150 /* initialize the video_device */
1151 chan->video.fops = &tegra_channel_fops;
1152 chan->video.v4l2_dev = &vid->v4l2_dev;
1153 chan->video.release = video_device_release_empty;
1154 chan->video.queue = &chan->queue;
1155 snprintf(chan->video.name, sizeof(chan->video.name), "%s-%s-%u",
1156 dev_name(vi->dev), "output", chan->portnos[0]);
1157 chan->video.vfl_type = VFL_TYPE_VIDEO;
1158 chan->video.vfl_dir = VFL_DIR_RX;
1159 chan->video.ioctl_ops = &tegra_channel_ioctl_ops;
1160 chan->video.ctrl_handler = &chan->ctrl_handler;
1161 chan->video.lock = &chan->video_lock;
1162 chan->video.device_caps = V4L2_CAP_VIDEO_CAPTURE |
1163 V4L2_CAP_STREAMING |
1165 video_set_drvdata(&chan->video, chan);
1167 chan->queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1168 chan->queue.io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
1169 chan->queue.lock = &chan->video_lock;
1170 chan->queue.drv_priv = chan;
1171 chan->queue.buf_struct_size = sizeof(struct tegra_channel_buffer);
1172 chan->queue.ops = &tegra_channel_queue_qops;
1173 chan->queue.mem_ops = &vb2_dma_contig_memops;
1174 chan->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1175 chan->queue.min_buffers_needed = 2;
1176 chan->queue.dev = vi->dev;
1177 ret = vb2_queue_init(&chan->queue);
1179 dev_err(vi->dev, "failed to initialize vb2 queue: %d\n", ret);
1180 goto free_v4l2_ctrl_hdl;
1183 if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
1184 v4l2_async_nf_init(&chan->notifier, &vid->v4l2_dev);
1189 v4l2_ctrl_handler_free(&chan->ctrl_handler);
1191 media_entity_cleanup(&chan->video.entity);
1193 vi->ops->channel_host1x_syncpt_free(chan);
1197 static int tegra_vi_channel_alloc(struct tegra_vi *vi, unsigned int port_num,
1198 struct device_node *node, unsigned int lanes)
1200 struct tegra_vi_channel *chan;
1204 * Do not use devm_kzalloc as memory is freed immediately
1205 * when device instance is unbound but application might still
1206 * be holding the device node open. Channel memory allocated
1207 * with kzalloc is freed during video device release callback.
1209 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
1214 chan->portnos[0] = port_num;
1216 * For data lanes more than maximum csi lanes per brick, multiple of
1217 * x4 ports are used simultaneously for capture.
1219 if (lanes <= CSI_LANES_PER_BRICK)
1220 chan->totalports = 1;
1222 chan->totalports = lanes / CSI_LANES_PER_BRICK;
1223 chan->numgangports = chan->totalports;
1225 for (i = 1; i < chan->totalports; i++)
1226 chan->portnos[i] = chan->portnos[0] + i * CSI_PORTS_PER_BRICK;
1228 chan->of_node = node;
1229 list_add_tail(&chan->list, &vi->vi_chans);
1234 static int tegra_vi_tpg_channels_alloc(struct tegra_vi *vi)
1236 unsigned int port_num;
1237 unsigned int nchannels = vi->soc->vi_max_channels;
1240 for (port_num = 0; port_num < nchannels; port_num++) {
1241 ret = tegra_vi_channel_alloc(vi, port_num,
1242 vi->dev->of_node, 2);
1250 static int tegra_vi_channels_alloc(struct tegra_vi *vi)
1252 struct device_node *node = vi->dev->of_node;
1253 struct device_node *ep = NULL;
1254 struct device_node *ports;
1255 struct device_node *port = NULL;
1256 unsigned int port_num;
1257 struct device_node *parent;
1258 struct v4l2_fwnode_endpoint v4l2_ep = { .bus_type = 0 };
1262 ports = of_get_child_by_name(node, "ports");
1264 return dev_err_probe(vi->dev, -ENODEV, "%pOF: missing 'ports' node\n", node);
1266 for_each_child_of_node(ports, port) {
1267 if (!of_node_name_eq(port, "port"))
1270 ret = of_property_read_u32(port, "reg", &port_num);
1274 if (port_num > vi->soc->vi_max_channels) {
1275 dev_err(vi->dev, "invalid port num %d for %pOF\n",
1281 ep = of_get_child_by_name(port, "endpoint");
1285 parent = of_graph_get_remote_port_parent(ep);
1290 ep = of_graph_get_endpoint_by_regs(parent, 0, 0);
1291 of_node_put(parent);
1292 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep),
1298 lanes = v4l2_ep.bus.mipi_csi2.num_data_lanes;
1299 ret = tegra_vi_channel_alloc(vi, port_num, port, lanes);
1310 static int tegra_vi_channels_init(struct tegra_vi *vi)
1312 struct tegra_vi_channel *chan;
1315 list_for_each_entry(chan, &vi->vi_chans, list) {
1316 ret = tegra_channel_init(chan);
1319 "failed to initialize channel-%d: %d\n",
1320 chan->portnos[0], ret);
1328 list_for_each_entry_continue_reverse(chan, &vi->vi_chans, list)
1329 tegra_channel_cleanup(chan);
1334 void tegra_v4l2_nodes_cleanup_tpg(struct tegra_video_device *vid)
1336 struct tegra_vi *vi = vid->vi;
1337 struct tegra_csi *csi = vid->csi;
1338 struct tegra_csi_channel *csi_chan;
1339 struct tegra_vi_channel *chan;
1341 list_for_each_entry(chan, &vi->vi_chans, list)
1342 vb2_video_unregister_device(&chan->video);
1344 list_for_each_entry(csi_chan, &csi->csi_chans, list)
1345 v4l2_device_unregister_subdev(&csi_chan->subdev);
1348 int tegra_v4l2_nodes_setup_tpg(struct tegra_video_device *vid)
1350 struct tegra_vi *vi = vid->vi;
1351 struct tegra_csi *csi = vid->csi;
1352 struct tegra_vi_channel *vi_chan;
1353 struct tegra_csi_channel *csi_chan;
1354 u32 link_flags = MEDIA_LNK_FL_ENABLED;
1360 csi_chan = list_first_entry(&csi->csi_chans,
1361 struct tegra_csi_channel, list);
1363 list_for_each_entry(vi_chan, &vi->vi_chans, list) {
1364 struct media_entity *source = &csi_chan->subdev.entity;
1365 struct media_entity *sink = &vi_chan->video.entity;
1366 struct media_pad *source_pad = csi_chan->pads;
1367 struct media_pad *sink_pad = &vi_chan->pad;
1369 ret = v4l2_device_register_subdev(&vid->v4l2_dev,
1373 "failed to register subdev: %d\n", ret);
1377 ret = video_register_device(&vi_chan->video,
1378 VFL_TYPE_VIDEO, -1);
1381 "failed to register video device: %d\n", ret);
1385 dev_dbg(vi->dev, "creating %s:%u -> %s:%u link\n",
1386 source->name, source_pad->index,
1387 sink->name, sink_pad->index);
1389 ret = media_create_pad_link(source, source_pad->index,
1390 sink, sink_pad->index,
1394 "failed to create %s:%u -> %s:%u link: %d\n",
1395 source->name, source_pad->index,
1396 sink->name, sink_pad->index, ret);
1400 ret = tegra_channel_setup_ctrl_handler(vi_chan);
1404 v4l2_set_subdev_hostdata(&csi_chan->subdev, vi_chan);
1405 vi_tpg_fmts_bitmap_init(vi_chan);
1406 csi_chan = list_next_entry(csi_chan, list);
1412 tegra_v4l2_nodes_cleanup_tpg(vid);
1416 static int __maybe_unused vi_runtime_resume(struct device *dev)
1418 struct tegra_vi *vi = dev_get_drvdata(dev);
1421 ret = regulator_enable(vi->vdd);
1423 dev_err(dev, "failed to enable VDD supply: %d\n", ret);
1427 ret = clk_set_rate(vi->clk, vi->soc->vi_max_clk_hz);
1429 dev_err(dev, "failed to set vi clock rate: %d\n", ret);
1433 ret = clk_prepare_enable(vi->clk);
1435 dev_err(dev, "failed to enable vi clock: %d\n", ret);
1442 regulator_disable(vi->vdd);
1446 static int __maybe_unused vi_runtime_suspend(struct device *dev)
1448 struct tegra_vi *vi = dev_get_drvdata(dev);
1450 clk_disable_unprepare(vi->clk);
1452 regulator_disable(vi->vdd);
1458 * Find the entity matching a given fwnode in an v4l2_async_notifier list
1460 static struct tegra_vi_graph_entity *
1461 tegra_vi_graph_find_entity(struct list_head *list,
1462 const struct fwnode_handle *fwnode)
1464 struct tegra_vi_graph_entity *entity;
1465 struct v4l2_async_connection *asd;
1467 list_for_each_entry(asd, list, asc_entry) {
1468 entity = to_tegra_vi_graph_entity(asd);
1470 if (entity->asd.match.fwnode == fwnode)
1477 static int tegra_vi_graph_build(struct tegra_vi_channel *chan,
1478 struct tegra_vi_graph_entity *entity)
1480 struct tegra_vi *vi = chan->vi;
1481 struct tegra_vi_graph_entity *ent;
1482 struct fwnode_handle *ep = NULL;
1483 struct v4l2_fwnode_link link;
1484 struct media_entity *local = entity->entity;
1485 struct media_entity *remote;
1486 struct media_pad *local_pad;
1487 struct media_pad *remote_pad;
1488 u32 link_flags = MEDIA_LNK_FL_ENABLED;
1491 dev_dbg(vi->dev, "creating links for entity %s\n", local->name);
1494 ep = fwnode_graph_get_next_endpoint(entity->asd.match.fwnode,
1499 ret = v4l2_fwnode_parse_link(ep, &link);
1501 dev_err(vi->dev, "failed to parse link for %pOF: %d\n",
1502 to_of_node(ep), ret);
1506 if (link.local_port >= local->num_pads) {
1507 dev_err(vi->dev, "invalid port number %u on %pOF\n",
1508 link.local_port, to_of_node(link.local_node));
1509 v4l2_fwnode_put_link(&link);
1514 local_pad = &local->pads[link.local_port];
1515 /* Remote node is vi node. So use channel video entity and pad
1518 if (link.remote_node == of_fwnode_handle(vi->dev->of_node)) {
1519 remote = &chan->video.entity;
1520 remote_pad = &chan->pad;
1525 * Skip sink ports, they will be processed from the other end
1528 if (local_pad->flags & MEDIA_PAD_FL_SINK) {
1529 dev_dbg(vi->dev, "skipping sink port %pOF:%u\n",
1530 to_of_node(link.local_node), link.local_port);
1531 v4l2_fwnode_put_link(&link);
1535 /* find the remote entity from notifier list */
1536 ent = tegra_vi_graph_find_entity(&chan->notifier.done_list,
1539 dev_err(vi->dev, "no entity found for %pOF\n",
1540 to_of_node(link.remote_node));
1541 v4l2_fwnode_put_link(&link);
1546 remote = ent->entity;
1547 if (link.remote_port >= remote->num_pads) {
1548 dev_err(vi->dev, "invalid port number %u on %pOF\n",
1550 to_of_node(link.remote_node));
1551 v4l2_fwnode_put_link(&link);
1556 remote_pad = &remote->pads[link.remote_port];
1559 dev_dbg(vi->dev, "creating %s:%u -> %s:%u link\n",
1560 local->name, local_pad->index,
1561 remote->name, remote_pad->index);
1563 ret = media_create_pad_link(local, local_pad->index,
1564 remote, remote_pad->index,
1566 v4l2_fwnode_put_link(&link);
1569 "failed to create %s:%u -> %s:%u link: %d\n",
1570 local->name, local_pad->index,
1571 remote->name, remote_pad->index, ret);
1576 fwnode_handle_put(ep);
1580 static int tegra_vi_graph_notify_complete(struct v4l2_async_notifier *notifier)
1582 struct tegra_vi_graph_entity *entity;
1583 struct v4l2_async_connection *asd;
1584 struct v4l2_subdev *subdev;
1585 struct tegra_vi_channel *chan;
1586 struct tegra_vi *vi;
1589 chan = container_of(notifier, struct tegra_vi_channel, notifier);
1592 dev_dbg(vi->dev, "notify complete, all subdevs registered\n");
1595 * Video device node should be created at the end of all the device
1596 * related initialization/setup.
1597 * Current video_register_device() does both initialize and register
1598 * video device in same API.
1600 * TODO: Update v4l2-dev driver to split initialize and register into
1601 * separate APIs and then update Tegra video driver to do video device
1602 * initialize followed by all video device related setup and then
1603 * register the video device.
1605 ret = video_register_device(&chan->video, VFL_TYPE_VIDEO, -1);
1608 "failed to register video device: %d\n", ret);
1609 goto unregister_video;
1612 /* create links between the entities */
1613 list_for_each_entry(asd, &chan->notifier.done_list, asc_entry) {
1614 entity = to_tegra_vi_graph_entity(asd);
1615 ret = tegra_vi_graph_build(chan, entity);
1617 goto unregister_video;
1620 ret = tegra_channel_setup_ctrl_handler(chan);
1623 "failed to setup channel controls: %d\n", ret);
1624 goto unregister_video;
1627 ret = vi_fmts_bitmap_init(chan);
1630 "failed to initialize formats bitmap: %d\n", ret);
1631 goto unregister_video;
1634 subdev = tegra_channel_get_remote_csi_subdev(chan);
1638 "failed to get remote csi subdev: %d\n", ret);
1639 goto unregister_video;
1642 v4l2_set_subdev_hostdata(subdev, chan);
1644 subdev = tegra_channel_get_remote_source_subdev(chan);
1645 v4l2_set_subdev_hostdata(subdev, chan);
1650 vb2_video_unregister_device(&chan->video);
1654 static int tegra_vi_graph_notify_bound(struct v4l2_async_notifier *notifier,
1655 struct v4l2_subdev *subdev,
1656 struct v4l2_async_connection *asd)
1658 struct tegra_vi_graph_entity *entity;
1659 struct tegra_vi *vi;
1660 struct tegra_vi_channel *chan;
1662 chan = container_of(notifier, struct tegra_vi_channel, notifier);
1666 * Locate the entity corresponding to the bound subdev and store the
1669 entity = tegra_vi_graph_find_entity(&chan->notifier.waiting_list,
1672 dev_err(vi->dev, "no entity for subdev %s\n", subdev->name);
1676 if (entity->subdev) {
1677 dev_err(vi->dev, "duplicate subdev for node %pOF\n",
1678 to_of_node(entity->asd.match.fwnode));
1682 dev_dbg(vi->dev, "subdev %s bound\n", subdev->name);
1683 entity->entity = &subdev->entity;
1684 entity->subdev = subdev;
1689 static const struct v4l2_async_notifier_operations tegra_vi_async_ops = {
1690 .bound = tegra_vi_graph_notify_bound,
1691 .complete = tegra_vi_graph_notify_complete,
1694 static int tegra_vi_graph_parse_one(struct tegra_vi_channel *chan,
1695 struct fwnode_handle *fwnode)
1697 struct tegra_vi *vi = chan->vi;
1698 struct fwnode_handle *ep = NULL;
1699 struct fwnode_handle *remote = NULL;
1700 struct tegra_vi_graph_entity *tvge;
1701 struct device_node *node = NULL;
1704 dev_dbg(vi->dev, "parsing node %pOF\n", to_of_node(fwnode));
1706 /* parse all the remote entities and put them into the list */
1707 for_each_endpoint_of_node(to_of_node(fwnode), node) {
1708 ep = of_fwnode_handle(node);
1709 remote = fwnode_graph_get_remote_port_parent(ep);
1712 "remote device at %pOF not found\n", node);
1717 /* skip entities that are already processed */
1718 if (device_match_fwnode(vi->dev, remote) ||
1719 tegra_vi_graph_find_entity(&chan->notifier.waiting_list,
1721 fwnode_handle_put(remote);
1725 tvge = v4l2_async_nf_add_fwnode(&chan->notifier, remote,
1726 struct tegra_vi_graph_entity);
1728 ret = PTR_ERR(tvge);
1730 "failed to add subdev to notifier: %d\n", ret);
1731 fwnode_handle_put(remote);
1735 ret = tegra_vi_graph_parse_one(chan, remote);
1737 fwnode_handle_put(remote);
1741 fwnode_handle_put(remote);
1747 dev_err(vi->dev, "failed parsing the graph: %d\n", ret);
1748 v4l2_async_nf_cleanup(&chan->notifier);
1753 static int tegra_vi_graph_init(struct tegra_vi *vi)
1755 struct tegra_vi_channel *chan;
1756 struct fwnode_handle *fwnode = dev_fwnode(vi->dev);
1760 * Walk the links to parse the full graph. Each channel will have
1761 * one endpoint of the composite node. Start by parsing the
1762 * composite node and parse the remote entities in turn.
1763 * Each channel will register a v4l2 async notifier to make the graph
1764 * independent between the channels so we can skip the current channel
1765 * in case of something wrong during graph parsing and continue with
1766 * the next channels.
1768 list_for_each_entry(chan, &vi->vi_chans, list) {
1769 struct fwnode_handle *ep, *remote;
1771 ep = fwnode_graph_get_endpoint_by_id(fwnode,
1772 chan->portnos[0], 0, 0);
1776 remote = fwnode_graph_get_remote_port_parent(ep);
1777 fwnode_handle_put(ep);
1779 ret = tegra_vi_graph_parse_one(chan, remote);
1780 fwnode_handle_put(remote);
1781 if (ret < 0 || list_empty(&chan->notifier.waiting_list))
1784 chan->notifier.ops = &tegra_vi_async_ops;
1785 ret = v4l2_async_nf_register(&chan->notifier);
1788 "failed to register channel %d notifier: %d\n",
1789 chan->portnos[0], ret);
1790 v4l2_async_nf_cleanup(&chan->notifier);
1797 static void tegra_vi_graph_cleanup(struct tegra_vi *vi)
1799 struct tegra_vi_channel *chan;
1801 list_for_each_entry(chan, &vi->vi_chans, list) {
1802 vb2_video_unregister_device(&chan->video);
1803 v4l2_async_nf_unregister(&chan->notifier);
1804 v4l2_async_nf_cleanup(&chan->notifier);
1808 static int tegra_vi_init(struct host1x_client *client)
1810 struct tegra_video_device *vid = dev_get_drvdata(client->host);
1811 struct tegra_vi *vi = host1x_client_to_vi(client);
1812 struct tegra_vi_channel *chan, *tmp;
1815 vid->media_dev.hw_revision = vi->soc->hw_revision;
1816 snprintf(vid->media_dev.bus_info, sizeof(vid->media_dev.bus_info),
1817 "platform:%s", dev_name(vi->dev));
1819 INIT_LIST_HEAD(&vi->vi_chans);
1821 if (IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
1822 ret = tegra_vi_tpg_channels_alloc(vi);
1824 ret = tegra_vi_channels_alloc(vi);
1828 ret = tegra_vi_channels_init(vi);
1834 if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)) {
1835 ret = tegra_vi_graph_init(vi);
1843 list_for_each_entry(chan, &vi->vi_chans, list)
1844 tegra_channel_cleanup(chan);
1846 list_for_each_entry_safe(chan, tmp, &vi->vi_chans, list) {
1847 list_del(&chan->list);
1854 static int tegra_vi_exit(struct host1x_client *client)
1856 struct tegra_vi *vi = host1x_client_to_vi(client);
1859 * Do not cleanup the channels here as application might still be
1860 * holding video device nodes. Channels cleanup will happen during
1861 * v4l2_device release callback which gets called after all video
1862 * device nodes are released.
1865 if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
1866 tegra_vi_graph_cleanup(vi);
1871 static const struct host1x_client_ops vi_client_ops = {
1872 .init = tegra_vi_init,
1873 .exit = tegra_vi_exit,
1876 static int tegra_vi_probe(struct platform_device *pdev)
1878 struct tegra_vi *vi;
1881 vi = devm_kzalloc(&pdev->dev, sizeof(*vi), GFP_KERNEL);
1885 vi->iomem = devm_platform_ioremap_resource(pdev, 0);
1886 if (IS_ERR(vi->iomem))
1887 return PTR_ERR(vi->iomem);
1889 vi->soc = of_device_get_match_data(&pdev->dev);
1891 vi->clk = devm_clk_get(&pdev->dev, NULL);
1892 if (IS_ERR(vi->clk)) {
1893 ret = PTR_ERR(vi->clk);
1894 dev_err(&pdev->dev, "failed to get vi clock: %d\n", ret);
1898 vi->vdd = devm_regulator_get(&pdev->dev, "avdd-dsi-csi");
1899 if (IS_ERR(vi->vdd)) {
1900 ret = PTR_ERR(vi->vdd);
1901 dev_err(&pdev->dev, "failed to get VDD supply: %d\n", ret);
1905 if (!pdev->dev.pm_domain) {
1907 dev_warn(&pdev->dev, "PM domain is not attached: %d\n", ret);
1911 ret = devm_of_platform_populate(&pdev->dev);
1914 "failed to populate vi child device: %d\n", ret);
1918 vi->dev = &pdev->dev;
1919 vi->ops = vi->soc->ops;
1920 platform_set_drvdata(pdev, vi);
1921 pm_runtime_enable(&pdev->dev);
1923 /* initialize host1x interface */
1924 INIT_LIST_HEAD(&vi->client.list);
1925 vi->client.ops = &vi_client_ops;
1926 vi->client.dev = &pdev->dev;
1928 if (vi->ops->vi_enable)
1929 vi->ops->vi_enable(vi, true);
1931 ret = host1x_client_register(&vi->client);
1934 "failed to register host1x client: %d\n", ret);
1941 if (vi->ops->vi_enable)
1942 vi->ops->vi_enable(vi, false);
1943 pm_runtime_disable(&pdev->dev);
1947 static int tegra_vi_remove(struct platform_device *pdev)
1949 struct tegra_vi *vi = platform_get_drvdata(pdev);
1951 host1x_client_unregister(&vi->client);
1953 if (vi->ops->vi_enable)
1954 vi->ops->vi_enable(vi, false);
1955 pm_runtime_disable(&pdev->dev);
1960 static const struct of_device_id tegra_vi_of_id_table[] = {
1961 #if defined(CONFIG_ARCH_TEGRA_2x_SOC)
1962 { .compatible = "nvidia,tegra20-vi", .data = &tegra20_vi_soc },
1964 #if defined(CONFIG_ARCH_TEGRA_210_SOC)
1965 { .compatible = "nvidia,tegra210-vi", .data = &tegra210_vi_soc },
1969 MODULE_DEVICE_TABLE(of, tegra_vi_of_id_table);
1971 static const struct dev_pm_ops tegra_vi_pm_ops = {
1972 SET_RUNTIME_PM_OPS(vi_runtime_suspend, vi_runtime_resume, NULL)
1975 struct platform_driver tegra_vi_driver = {
1978 .of_match_table = tegra_vi_of_id_table,
1979 .pm = &tegra_vi_pm_ops,
1981 .probe = tegra_vi_probe,
1982 .remove = tegra_vi_remove,