Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2020 NVIDIA CORPORATION.  All rights reserved.
0004  */
0005 
0006 #include <linux/bitmap.h>
0007 #include <linux/clk.h>
0008 #include <linux/delay.h>
0009 #include <linux/host1x.h>
0010 #include <linux/lcm.h>
0011 #include <linux/list.h>
0012 #include <linux/module.h>
0013 #include <linux/of.h>
0014 #include <linux/of_device.h>
0015 #include <linux/of_graph.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/regulator/consumer.h>
0018 #include <linux/pm_runtime.h>
0019 #include <linux/slab.h>
0020 
0021 #include <media/v4l2-dv-timings.h>
0022 #include <media/v4l2-event.h>
0023 #include <media/v4l2-fh.h>
0024 #include <media/v4l2-fwnode.h>
0025 #include <media/v4l2-ioctl.h>
0026 #include <media/videobuf2-dma-contig.h>
0027 
0028 #include <soc/tegra/pmc.h>
0029 
0030 #include "vi.h"
0031 #include "video.h"
0032 
0033 #define MAX_CID_CONTROLS        1
0034 
0035 static const struct tegra_video_format tegra_default_format = {
0036     .img_dt = TEGRA_IMAGE_DT_RAW10,
0037     .bit_width = 10,
0038     .code = MEDIA_BUS_FMT_SRGGB10_1X10,
0039     .bpp = 2,
0040     .img_fmt = TEGRA_IMAGE_FORMAT_DEF,
0041     .fourcc = V4L2_PIX_FMT_SRGGB10,
0042 };
0043 
0044 static inline struct tegra_vi *
0045 host1x_client_to_vi(struct host1x_client *client)
0046 {
0047     return container_of(client, struct tegra_vi, client);
0048 }
0049 
0050 static inline struct tegra_channel_buffer *
0051 to_tegra_channel_buffer(struct vb2_v4l2_buffer *vb)
0052 {
0053     return container_of(vb, struct tegra_channel_buffer, buf);
0054 }
0055 
0056 static inline struct tegra_vi_graph_entity *
0057 to_tegra_vi_graph_entity(struct v4l2_async_subdev *asd)
0058 {
0059     return container_of(asd, struct tegra_vi_graph_entity, asd);
0060 }
0061 
0062 static int tegra_get_format_idx_by_code(struct tegra_vi *vi,
0063                     unsigned int code,
0064                     unsigned int offset)
0065 {
0066     unsigned int i;
0067 
0068     for (i = offset; i < vi->soc->nformats; ++i) {
0069         if (vi->soc->video_formats[i].code == code)
0070             return i;
0071     }
0072 
0073     return -1;
0074 }
0075 
0076 static u32 tegra_get_format_fourcc_by_idx(struct tegra_vi *vi,
0077                       unsigned int index)
0078 {
0079     if (index >= vi->soc->nformats)
0080         return -EINVAL;
0081 
0082     return vi->soc->video_formats[index].fourcc;
0083 }
0084 
0085 static const struct tegra_video_format *
0086 tegra_get_format_by_fourcc(struct tegra_vi *vi, u32 fourcc)
0087 {
0088     unsigned int i;
0089 
0090     for (i = 0; i < vi->soc->nformats; ++i) {
0091         if (vi->soc->video_formats[i].fourcc == fourcc)
0092             return &vi->soc->video_formats[i];
0093     }
0094 
0095     return NULL;
0096 }
0097 
0098 /*
0099  * videobuf2 queue operations
0100  */
0101 static int tegra_channel_queue_setup(struct vb2_queue *vq,
0102                      unsigned int *nbuffers,
0103                      unsigned int *nplanes,
0104                      unsigned int sizes[],
0105                      struct device *alloc_devs[])
0106 {
0107     struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
0108 
0109     if (*nplanes)
0110         return sizes[0] < chan->format.sizeimage ? -EINVAL : 0;
0111 
0112     *nplanes = 1;
0113     sizes[0] = chan->format.sizeimage;
0114     alloc_devs[0] = chan->vi->dev;
0115 
0116     return 0;
0117 }
0118 
0119 static int tegra_channel_buffer_prepare(struct vb2_buffer *vb)
0120 {
0121     struct tegra_vi_channel *chan = vb2_get_drv_priv(vb->vb2_queue);
0122     struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0123     struct tegra_channel_buffer *buf = to_tegra_channel_buffer(vbuf);
0124     unsigned long size = chan->format.sizeimage;
0125 
0126     if (vb2_plane_size(vb, 0) < size) {
0127         v4l2_err(chan->video.v4l2_dev,
0128              "buffer too small (%lu < %lu)\n",
0129              vb2_plane_size(vb, 0), size);
0130         return -EINVAL;
0131     }
0132 
0133     vb2_set_plane_payload(vb, 0, size);
0134     buf->chan = chan;
0135     buf->addr = vb2_dma_contig_plane_dma_addr(vb, 0);
0136 
0137     return 0;
0138 }
0139 
0140 static void tegra_channel_buffer_queue(struct vb2_buffer *vb)
0141 {
0142     struct tegra_vi_channel *chan = vb2_get_drv_priv(vb->vb2_queue);
0143     struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0144     struct tegra_channel_buffer *buf = to_tegra_channel_buffer(vbuf);
0145 
0146     /* put buffer into the capture queue */
0147     spin_lock(&chan->start_lock);
0148     list_add_tail(&buf->queue, &chan->capture);
0149     spin_unlock(&chan->start_lock);
0150 
0151     /* wait up kthread for capture */
0152     wake_up_interruptible(&chan->start_wait);
0153 }
0154 
0155 struct v4l2_subdev *
0156 tegra_channel_get_remote_csi_subdev(struct tegra_vi_channel *chan)
0157 {
0158     struct media_pad *pad;
0159 
0160     pad = media_pad_remote_pad_first(&chan->pad);
0161     if (!pad)
0162         return NULL;
0163 
0164     return media_entity_to_v4l2_subdev(pad->entity);
0165 }
0166 
0167 struct v4l2_subdev *
0168 tegra_channel_get_remote_source_subdev(struct tegra_vi_channel *chan)
0169 {
0170     struct media_pad *pad;
0171     struct v4l2_subdev *subdev;
0172     struct media_entity *entity;
0173 
0174     subdev = tegra_channel_get_remote_csi_subdev(chan);
0175     if (!subdev)
0176         return NULL;
0177 
0178     pad = &subdev->entity.pads[0];
0179     while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
0180         pad = media_pad_remote_pad_first(pad);
0181         if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
0182             break;
0183         entity = pad->entity;
0184         pad = &entity->pads[0];
0185         subdev = media_entity_to_v4l2_subdev(entity);
0186     }
0187 
0188     return subdev;
0189 }
0190 
0191 static int tegra_channel_enable_stream(struct tegra_vi_channel *chan)
0192 {
0193     struct v4l2_subdev *csi_subdev, *src_subdev;
0194     struct tegra_csi_channel *csi_chan;
0195     int ret, err;
0196 
0197     /*
0198      * Tegra CSI receiver can detect the first LP to HS transition.
0199      * So, start the CSI stream-on prior to sensor stream-on and
0200      * vice-versa for stream-off.
0201      */
0202     csi_subdev = tegra_channel_get_remote_csi_subdev(chan);
0203     ret = v4l2_subdev_call(csi_subdev, video, s_stream, true);
0204     if (ret < 0 && ret != -ENOIOCTLCMD)
0205         return ret;
0206 
0207     if (IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
0208         return 0;
0209 
0210     csi_chan = v4l2_get_subdevdata(csi_subdev);
0211     /*
0212      * TRM has incorrectly documented to wait for done status from
0213      * calibration logic after CSI interface power on.
0214      * As per the design, calibration results are latched and applied
0215      * to the pads only when the link is in LP11 state which will happen
0216      * during the sensor stream-on.
0217      * CSI subdev stream-on triggers start of MIPI pads calibration.
0218      * Wait for calibration to finish here after sensor subdev stream-on.
0219      */
0220     src_subdev = tegra_channel_get_remote_source_subdev(chan);
0221     ret = v4l2_subdev_call(src_subdev, video, s_stream, true);
0222     err = tegra_mipi_finish_calibration(csi_chan->mipi);
0223 
0224     if (ret < 0 && ret != -ENOIOCTLCMD)
0225         goto err_disable_csi_stream;
0226 
0227     if (err < 0)
0228         dev_warn(csi_chan->csi->dev,
0229              "MIPI calibration failed: %d\n", err);
0230 
0231     return 0;
0232 
0233 err_disable_csi_stream:
0234     v4l2_subdev_call(csi_subdev, video, s_stream, false);
0235     return ret;
0236 }
0237 
0238 static int tegra_channel_disable_stream(struct tegra_vi_channel *chan)
0239 {
0240     struct v4l2_subdev *subdev;
0241     int ret;
0242 
0243     /*
0244      * Stream-off subdevices in reverse order to stream-on.
0245      * Remote source subdev in TPG mode is same as CSI subdev.
0246      */
0247     subdev = tegra_channel_get_remote_source_subdev(chan);
0248     ret = v4l2_subdev_call(subdev, video, s_stream, false);
0249     if (ret < 0 && ret != -ENOIOCTLCMD)
0250         return ret;
0251 
0252     if (IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
0253         return 0;
0254 
0255     subdev = tegra_channel_get_remote_csi_subdev(chan);
0256     ret = v4l2_subdev_call(subdev, video, s_stream, false);
0257     if (ret < 0 && ret != -ENOIOCTLCMD)
0258         return ret;
0259 
0260     return 0;
0261 }
0262 
0263 int tegra_channel_set_stream(struct tegra_vi_channel *chan, bool on)
0264 {
0265     int ret;
0266 
0267     if (on)
0268         ret = tegra_channel_enable_stream(chan);
0269     else
0270         ret = tegra_channel_disable_stream(chan);
0271 
0272     return ret;
0273 }
0274 
0275 void tegra_channel_release_buffers(struct tegra_vi_channel *chan,
0276                    enum vb2_buffer_state state)
0277 {
0278     struct tegra_channel_buffer *buf, *nbuf;
0279 
0280     spin_lock(&chan->start_lock);
0281     list_for_each_entry_safe(buf, nbuf, &chan->capture, queue) {
0282         vb2_buffer_done(&buf->buf.vb2_buf, state);
0283         list_del(&buf->queue);
0284     }
0285     spin_unlock(&chan->start_lock);
0286 
0287     spin_lock(&chan->done_lock);
0288     list_for_each_entry_safe(buf, nbuf, &chan->done, queue) {
0289         vb2_buffer_done(&buf->buf.vb2_buf, state);
0290         list_del(&buf->queue);
0291     }
0292     spin_unlock(&chan->done_lock);
0293 }
0294 
0295 static int tegra_channel_start_streaming(struct vb2_queue *vq, u32 count)
0296 {
0297     struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
0298     int ret;
0299 
0300     ret = pm_runtime_resume_and_get(chan->vi->dev);
0301     if (ret < 0) {
0302         dev_err(chan->vi->dev, "failed to get runtime PM: %d\n", ret);
0303         return ret;
0304     }
0305 
0306     ret = chan->vi->ops->vi_start_streaming(vq, count);
0307     if (ret < 0)
0308         pm_runtime_put(chan->vi->dev);
0309 
0310     return ret;
0311 }
0312 
0313 static void tegra_channel_stop_streaming(struct vb2_queue *vq)
0314 {
0315     struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
0316 
0317     chan->vi->ops->vi_stop_streaming(vq);
0318     pm_runtime_put(chan->vi->dev);
0319 }
0320 
0321 static const struct vb2_ops tegra_channel_queue_qops = {
0322     .queue_setup = tegra_channel_queue_setup,
0323     .buf_prepare = tegra_channel_buffer_prepare,
0324     .buf_queue = tegra_channel_buffer_queue,
0325     .wait_prepare = vb2_ops_wait_prepare,
0326     .wait_finish = vb2_ops_wait_finish,
0327     .start_streaming = tegra_channel_start_streaming,
0328     .stop_streaming = tegra_channel_stop_streaming,
0329 };
0330 
0331 /*
0332  * V4L2 ioctl operations
0333  */
0334 static int tegra_channel_querycap(struct file *file, void *fh,
0335                   struct v4l2_capability *cap)
0336 {
0337     struct tegra_vi_channel *chan = video_drvdata(file);
0338 
0339     strscpy(cap->driver, "tegra-video", sizeof(cap->driver));
0340     strscpy(cap->card, chan->video.name, sizeof(cap->card));
0341     snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
0342          dev_name(chan->vi->dev));
0343 
0344     return 0;
0345 }
0346 
0347 static int tegra_channel_g_parm(struct file *file, void *fh,
0348                 struct v4l2_streamparm *a)
0349 {
0350     struct tegra_vi_channel *chan = video_drvdata(file);
0351     struct v4l2_subdev *subdev;
0352 
0353     subdev = tegra_channel_get_remote_source_subdev(chan);
0354     return v4l2_g_parm_cap(&chan->video, subdev, a);
0355 }
0356 
0357 static int tegra_channel_s_parm(struct file *file, void *fh,
0358                 struct v4l2_streamparm *a)
0359 {
0360     struct tegra_vi_channel *chan = video_drvdata(file);
0361     struct v4l2_subdev *subdev;
0362 
0363     subdev = tegra_channel_get_remote_source_subdev(chan);
0364     return v4l2_s_parm_cap(&chan->video, subdev, a);
0365 }
0366 
0367 static int tegra_channel_enum_framesizes(struct file *file, void *fh,
0368                      struct v4l2_frmsizeenum *sizes)
0369 {
0370     int ret;
0371     struct tegra_vi_channel *chan = video_drvdata(file);
0372     struct v4l2_subdev *subdev;
0373     const struct tegra_video_format *fmtinfo;
0374     struct v4l2_subdev_frame_size_enum fse = {
0375         .index = sizes->index,
0376         .which = V4L2_SUBDEV_FORMAT_ACTIVE,
0377     };
0378 
0379     fmtinfo = tegra_get_format_by_fourcc(chan->vi, sizes->pixel_format);
0380     if (!fmtinfo)
0381         return -EINVAL;
0382 
0383     fse.code = fmtinfo->code;
0384 
0385     subdev = tegra_channel_get_remote_source_subdev(chan);
0386     ret = v4l2_subdev_call(subdev, pad, enum_frame_size, NULL, &fse);
0387     if (ret)
0388         return ret;
0389 
0390     sizes->type = V4L2_FRMSIZE_TYPE_DISCRETE;
0391     sizes->discrete.width = fse.max_width;
0392     sizes->discrete.height = fse.max_height;
0393 
0394     return 0;
0395 }
0396 
0397 static int tegra_channel_enum_frameintervals(struct file *file, void *fh,
0398                          struct v4l2_frmivalenum *ivals)
0399 {
0400     int ret;
0401     struct tegra_vi_channel *chan = video_drvdata(file);
0402     struct v4l2_subdev *subdev;
0403     const struct tegra_video_format *fmtinfo;
0404     struct v4l2_subdev_frame_interval_enum fie = {
0405         .index = ivals->index,
0406         .width = ivals->width,
0407         .height = ivals->height,
0408         .which = V4L2_SUBDEV_FORMAT_ACTIVE,
0409     };
0410 
0411     fmtinfo = tegra_get_format_by_fourcc(chan->vi, ivals->pixel_format);
0412     if (!fmtinfo)
0413         return -EINVAL;
0414 
0415     fie.code = fmtinfo->code;
0416 
0417     subdev = tegra_channel_get_remote_source_subdev(chan);
0418     ret = v4l2_subdev_call(subdev, pad, enum_frame_interval, NULL, &fie);
0419     if (ret)
0420         return ret;
0421 
0422     ivals->type = V4L2_FRMIVAL_TYPE_DISCRETE;
0423     ivals->discrete.numerator = fie.interval.numerator;
0424     ivals->discrete.denominator = fie.interval.denominator;
0425 
0426     return 0;
0427 }
0428 
0429 static int tegra_channel_enum_format(struct file *file, void *fh,
0430                      struct v4l2_fmtdesc *f)
0431 {
0432     struct tegra_vi_channel *chan = video_drvdata(file);
0433     unsigned int index = 0, i;
0434     unsigned long *fmts_bitmap = chan->tpg_fmts_bitmap;
0435 
0436     if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
0437         fmts_bitmap = chan->fmts_bitmap;
0438 
0439     if (f->index >= bitmap_weight(fmts_bitmap, MAX_FORMAT_NUM))
0440         return -EINVAL;
0441 
0442     for (i = 0; i < f->index + 1; i++, index++)
0443         index = find_next_bit(fmts_bitmap, MAX_FORMAT_NUM, index);
0444 
0445     f->pixelformat = tegra_get_format_fourcc_by_idx(chan->vi, index - 1);
0446 
0447     return 0;
0448 }
0449 
0450 static int tegra_channel_get_format(struct file *file, void *fh,
0451                     struct v4l2_format *format)
0452 {
0453     struct tegra_vi_channel *chan = video_drvdata(file);
0454 
0455     format->fmt.pix = chan->format;
0456 
0457     return 0;
0458 }
0459 
0460 static void tegra_channel_fmt_align(struct tegra_vi_channel *chan,
0461                     struct v4l2_pix_format *pix,
0462                     unsigned int bpp)
0463 {
0464     unsigned int min_bpl;
0465     unsigned int max_bpl;
0466     unsigned int bpl;
0467 
0468     /*
0469      * The transfer alignment requirements are expressed in bytes.
0470      * Clamp the requested width and height to the limits.
0471      */
0472     pix->width = clamp(pix->width, TEGRA_MIN_WIDTH, TEGRA_MAX_WIDTH);
0473     pix->height = clamp(pix->height, TEGRA_MIN_HEIGHT, TEGRA_MAX_HEIGHT);
0474 
0475     /* Clamp the requested bytes per line value. If the maximum bytes per
0476      * line value is zero, the module doesn't support user configurable
0477      * line sizes. Override the requested value with the minimum in that
0478      * case.
0479      */
0480     min_bpl = pix->width * bpp;
0481     max_bpl = rounddown(TEGRA_MAX_WIDTH, SURFACE_ALIGN_BYTES);
0482     bpl = roundup(pix->bytesperline, SURFACE_ALIGN_BYTES);
0483 
0484     pix->bytesperline = clamp(bpl, min_bpl, max_bpl);
0485     pix->sizeimage = pix->bytesperline * pix->height;
0486     if (pix->pixelformat == V4L2_PIX_FMT_NV16)
0487         pix->sizeimage *= 2;
0488 }
0489 
0490 static int __tegra_channel_try_format(struct tegra_vi_channel *chan,
0491                       struct v4l2_pix_format *pix)
0492 {
0493     const struct tegra_video_format *fmtinfo;
0494     static struct lock_class_key key;
0495     struct v4l2_subdev *subdev;
0496     struct v4l2_subdev_format fmt;
0497     struct v4l2_subdev_state *sd_state;
0498     struct v4l2_subdev_frame_size_enum fse = {
0499         .which = V4L2_SUBDEV_FORMAT_TRY,
0500     };
0501     struct v4l2_subdev_selection sdsel = {
0502         .which = V4L2_SUBDEV_FORMAT_ACTIVE,
0503         .target = V4L2_SEL_TGT_CROP_BOUNDS,
0504     };
0505     int ret;
0506 
0507     subdev = tegra_channel_get_remote_source_subdev(chan);
0508     if (!subdev)
0509         return -ENODEV;
0510 
0511     /*
0512      * FIXME: Drop this call, drivers are not supposed to use
0513      * __v4l2_subdev_state_alloc().
0514      */
0515     sd_state = __v4l2_subdev_state_alloc(subdev, "tegra:state->lock",
0516                          &key);
0517     if (IS_ERR(sd_state))
0518         return PTR_ERR(sd_state);
0519     /*
0520      * Retrieve the format information and if requested format isn't
0521      * supported, keep the current format.
0522      */
0523     fmtinfo = tegra_get_format_by_fourcc(chan->vi, pix->pixelformat);
0524     if (!fmtinfo) {
0525         pix->pixelformat = chan->format.pixelformat;
0526         pix->colorspace = chan->format.colorspace;
0527         fmtinfo = tegra_get_format_by_fourcc(chan->vi,
0528                              pix->pixelformat);
0529     }
0530 
0531     pix->field = V4L2_FIELD_NONE;
0532     fmt.which = V4L2_SUBDEV_FORMAT_TRY;
0533     fmt.pad = 0;
0534     v4l2_fill_mbus_format(&fmt.format, pix, fmtinfo->code);
0535 
0536     /*
0537      * Attempt to obtain the format size from subdev.
0538      * If not available, try to get crop boundary from subdev.
0539      */
0540     fse.code = fmtinfo->code;
0541     ret = v4l2_subdev_call(subdev, pad, enum_frame_size, sd_state, &fse);
0542     if (ret) {
0543         if (!v4l2_subdev_has_op(subdev, pad, get_selection)) {
0544             sd_state->pads->try_crop.width = 0;
0545             sd_state->pads->try_crop.height = 0;
0546         } else {
0547             ret = v4l2_subdev_call(subdev, pad, get_selection,
0548                            NULL, &sdsel);
0549             if (ret)
0550                 return -EINVAL;
0551 
0552             sd_state->pads->try_crop.width = sdsel.r.width;
0553             sd_state->pads->try_crop.height = sdsel.r.height;
0554         }
0555     } else {
0556         sd_state->pads->try_crop.width = fse.max_width;
0557         sd_state->pads->try_crop.height = fse.max_height;
0558     }
0559 
0560     ret = v4l2_subdev_call(subdev, pad, set_fmt, sd_state, &fmt);
0561     if (ret < 0)
0562         return ret;
0563 
0564     v4l2_fill_pix_format(pix, &fmt.format);
0565     tegra_channel_fmt_align(chan, pix, fmtinfo->bpp);
0566 
0567     __v4l2_subdev_state_free(sd_state);
0568 
0569     return 0;
0570 }
0571 
0572 static int tegra_channel_try_format(struct file *file, void *fh,
0573                     struct v4l2_format *format)
0574 {
0575     struct tegra_vi_channel *chan = video_drvdata(file);
0576 
0577     return __tegra_channel_try_format(chan, &format->fmt.pix);
0578 }
0579 
0580 static void tegra_channel_update_gangports(struct tegra_vi_channel *chan)
0581 {
0582     if (chan->format.width <= 1920)
0583         chan->numgangports = 1;
0584     else
0585         chan->numgangports = chan->totalports;
0586 }
0587 
0588 static int tegra_channel_set_format(struct file *file, void *fh,
0589                     struct v4l2_format *format)
0590 {
0591     struct tegra_vi_channel *chan = video_drvdata(file);
0592     const struct tegra_video_format *fmtinfo;
0593     struct v4l2_subdev_format fmt;
0594     struct v4l2_subdev *subdev;
0595     struct v4l2_pix_format *pix = &format->fmt.pix;
0596     int ret;
0597 
0598     if (vb2_is_busy(&chan->queue))
0599         return -EBUSY;
0600 
0601     /* get supported format by try_fmt */
0602     ret = __tegra_channel_try_format(chan, pix);
0603     if (ret)
0604         return ret;
0605 
0606     fmtinfo = tegra_get_format_by_fourcc(chan->vi, pix->pixelformat);
0607 
0608     fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
0609     fmt.pad = 0;
0610     v4l2_fill_mbus_format(&fmt.format, pix, fmtinfo->code);
0611     subdev = tegra_channel_get_remote_source_subdev(chan);
0612     ret = v4l2_subdev_call(subdev, pad, set_fmt, NULL, &fmt);
0613     if (ret < 0)
0614         return ret;
0615 
0616     v4l2_fill_pix_format(pix, &fmt.format);
0617     tegra_channel_fmt_align(chan, pix, fmtinfo->bpp);
0618 
0619     chan->format = *pix;
0620     chan->fmtinfo = fmtinfo;
0621     tegra_channel_update_gangports(chan);
0622 
0623     return 0;
0624 }
0625 
0626 static int tegra_channel_set_subdev_active_fmt(struct tegra_vi_channel *chan)
0627 {
0628     int ret, index;
0629     struct v4l2_subdev *subdev;
0630     struct v4l2_subdev_format fmt = {
0631         .which = V4L2_SUBDEV_FORMAT_ACTIVE,
0632     };
0633 
0634     /*
0635      * Initialize channel format to the sub-device active format if there
0636      * is corresponding match in the Tegra supported video formats.
0637      */
0638     subdev = tegra_channel_get_remote_source_subdev(chan);
0639     ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
0640     if (ret)
0641         return ret;
0642 
0643     index = tegra_get_format_idx_by_code(chan->vi, fmt.format.code, 0);
0644     if (index < 0)
0645         return -EINVAL;
0646 
0647     chan->fmtinfo = &chan->vi->soc->video_formats[index];
0648     v4l2_fill_pix_format(&chan->format, &fmt.format);
0649     chan->format.pixelformat = chan->fmtinfo->fourcc;
0650     chan->format.bytesperline = chan->format.width * chan->fmtinfo->bpp;
0651     chan->format.sizeimage = chan->format.bytesperline *
0652                  chan->format.height;
0653     tegra_channel_fmt_align(chan, &chan->format, chan->fmtinfo->bpp);
0654     tegra_channel_update_gangports(chan);
0655 
0656     return 0;
0657 }
0658 
0659 static int
0660 tegra_channel_subscribe_event(struct v4l2_fh *fh,
0661                   const struct v4l2_event_subscription *sub)
0662 {
0663     switch (sub->type) {
0664     case V4L2_EVENT_SOURCE_CHANGE:
0665         return v4l2_event_subscribe(fh, sub, 4, NULL);
0666     }
0667 
0668     return v4l2_ctrl_subscribe_event(fh, sub);
0669 }
0670 
0671 static int tegra_channel_g_selection(struct file *file, void *priv,
0672                      struct v4l2_selection *sel)
0673 {
0674     struct tegra_vi_channel *chan = video_drvdata(file);
0675     struct v4l2_subdev *subdev;
0676     struct v4l2_subdev_format fmt = {
0677         .which = V4L2_SUBDEV_FORMAT_ACTIVE,
0678     };
0679     struct v4l2_subdev_selection sdsel = {
0680         .which = V4L2_SUBDEV_FORMAT_ACTIVE,
0681         .target = sel->target,
0682     };
0683     int ret;
0684 
0685     subdev = tegra_channel_get_remote_source_subdev(chan);
0686     if (!v4l2_subdev_has_op(subdev, pad, get_selection))
0687         return -ENOTTY;
0688 
0689     if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
0690         return -EINVAL;
0691     /*
0692      * Try the get selection operation and fallback to get format if not
0693      * implemented.
0694      */
0695     ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
0696     if (!ret)
0697         sel->r = sdsel.r;
0698     if (ret != -ENOIOCTLCMD)
0699         return ret;
0700 
0701     ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
0702     if (ret < 0)
0703         return ret;
0704 
0705     sel->r.left = 0;
0706     sel->r.top = 0;
0707     sel->r.width = fmt.format.width;
0708     sel->r.height = fmt.format.height;
0709 
0710     return 0;
0711 }
0712 
0713 static int tegra_channel_s_selection(struct file *file, void *fh,
0714                      struct v4l2_selection *sel)
0715 {
0716     struct tegra_vi_channel *chan = video_drvdata(file);
0717     struct v4l2_subdev *subdev;
0718     int ret;
0719     struct v4l2_subdev_selection sdsel = {
0720         .which = V4L2_SUBDEV_FORMAT_ACTIVE,
0721         .target = sel->target,
0722         .flags = sel->flags,
0723         .r = sel->r,
0724     };
0725 
0726     subdev = tegra_channel_get_remote_source_subdev(chan);
0727     if (!v4l2_subdev_has_op(subdev, pad, set_selection))
0728         return -ENOTTY;
0729 
0730     if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
0731         return -EINVAL;
0732 
0733     if (vb2_is_busy(&chan->queue))
0734         return -EBUSY;
0735 
0736     ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel);
0737     if (!ret) {
0738         sel->r = sdsel.r;
0739         /*
0740          * Subdev active format resolution may have changed during
0741          * set selection operation. So, update channel format to
0742          * the sub-device active format.
0743          */
0744         return tegra_channel_set_subdev_active_fmt(chan);
0745     }
0746 
0747     return ret;
0748 }
0749 
0750 static int tegra_channel_g_edid(struct file *file, void *fh,
0751                 struct v4l2_edid *edid)
0752 {
0753     struct tegra_vi_channel *chan = video_drvdata(file);
0754     struct v4l2_subdev *subdev;
0755 
0756     subdev = tegra_channel_get_remote_source_subdev(chan);
0757     if (!v4l2_subdev_has_op(subdev, pad, get_edid))
0758         return -ENOTTY;
0759 
0760     return v4l2_subdev_call(subdev, pad, get_edid, edid);
0761 }
0762 
0763 static int tegra_channel_s_edid(struct file *file, void *fh,
0764                 struct v4l2_edid *edid)
0765 {
0766     struct tegra_vi_channel *chan = video_drvdata(file);
0767     struct v4l2_subdev *subdev;
0768 
0769     subdev = tegra_channel_get_remote_source_subdev(chan);
0770     if (!v4l2_subdev_has_op(subdev, pad, set_edid))
0771         return -ENOTTY;
0772 
0773     return v4l2_subdev_call(subdev, pad, set_edid, edid);
0774 }
0775 
0776 static int tegra_channel_g_dv_timings(struct file *file, void *fh,
0777                       struct v4l2_dv_timings *timings)
0778 {
0779     struct tegra_vi_channel *chan = video_drvdata(file);
0780     struct v4l2_subdev *subdev;
0781 
0782     subdev = tegra_channel_get_remote_source_subdev(chan);
0783     if (!v4l2_subdev_has_op(subdev, video, g_dv_timings))
0784         return -ENOTTY;
0785 
0786     return v4l2_device_call_until_err(chan->video.v4l2_dev, 0,
0787                       video, g_dv_timings, timings);
0788 }
0789 
0790 static int tegra_channel_s_dv_timings(struct file *file, void *fh,
0791                       struct v4l2_dv_timings *timings)
0792 {
0793     struct tegra_vi_channel *chan = video_drvdata(file);
0794     struct v4l2_subdev *subdev;
0795     struct v4l2_bt_timings *bt = &timings->bt;
0796     struct v4l2_dv_timings curr_timings;
0797     int ret;
0798 
0799     subdev = tegra_channel_get_remote_source_subdev(chan);
0800     if (!v4l2_subdev_has_op(subdev, video, s_dv_timings))
0801         return -ENOTTY;
0802 
0803     ret = tegra_channel_g_dv_timings(file, fh, &curr_timings);
0804     if (ret)
0805         return ret;
0806 
0807     if (v4l2_match_dv_timings(timings, &curr_timings, 0, false))
0808         return 0;
0809 
0810     if (vb2_is_busy(&chan->queue))
0811         return -EBUSY;
0812 
0813     ret = v4l2_device_call_until_err(chan->video.v4l2_dev, 0,
0814                      video, s_dv_timings, timings);
0815     if (ret)
0816         return ret;
0817 
0818     chan->format.width = bt->width;
0819     chan->format.height = bt->height;
0820     chan->format.bytesperline = bt->width * chan->fmtinfo->bpp;
0821     chan->format.sizeimage = chan->format.bytesperline * bt->height;
0822     tegra_channel_fmt_align(chan, &chan->format, chan->fmtinfo->bpp);
0823     tegra_channel_update_gangports(chan);
0824 
0825     return 0;
0826 }
0827 
0828 static int tegra_channel_query_dv_timings(struct file *file, void *fh,
0829                       struct v4l2_dv_timings *timings)
0830 {
0831     struct tegra_vi_channel *chan = video_drvdata(file);
0832     struct v4l2_subdev *subdev;
0833 
0834     subdev = tegra_channel_get_remote_source_subdev(chan);
0835     if (!v4l2_subdev_has_op(subdev, video, query_dv_timings))
0836         return -ENOTTY;
0837 
0838     return v4l2_device_call_until_err(chan->video.v4l2_dev, 0,
0839                       video, query_dv_timings, timings);
0840 }
0841 
0842 static int tegra_channel_enum_dv_timings(struct file *file, void *fh,
0843                      struct v4l2_enum_dv_timings *timings)
0844 {
0845     struct tegra_vi_channel *chan = video_drvdata(file);
0846     struct v4l2_subdev *subdev;
0847 
0848     subdev = tegra_channel_get_remote_source_subdev(chan);
0849     if (!v4l2_subdev_has_op(subdev, pad, enum_dv_timings))
0850         return -ENOTTY;
0851 
0852     return v4l2_subdev_call(subdev, pad, enum_dv_timings, timings);
0853 }
0854 
0855 static int tegra_channel_dv_timings_cap(struct file *file, void *fh,
0856                     struct v4l2_dv_timings_cap *cap)
0857 {
0858     struct tegra_vi_channel *chan = video_drvdata(file);
0859     struct v4l2_subdev *subdev;
0860 
0861     subdev = tegra_channel_get_remote_source_subdev(chan);
0862     if (!v4l2_subdev_has_op(subdev, pad, dv_timings_cap))
0863         return -ENOTTY;
0864 
0865     return v4l2_subdev_call(subdev, pad, dv_timings_cap, cap);
0866 }
0867 
0868 static int tegra_channel_log_status(struct file *file, void *fh)
0869 {
0870     struct tegra_vi_channel *chan = video_drvdata(file);
0871 
0872     v4l2_device_call_all(chan->video.v4l2_dev, 0, core, log_status);
0873 
0874     return 0;
0875 }
0876 
0877 static int tegra_channel_enum_input(struct file *file, void *fh,
0878                     struct v4l2_input *inp)
0879 {
0880     struct tegra_vi_channel *chan = video_drvdata(file);
0881     struct v4l2_subdev *subdev;
0882 
0883     if (inp->index)
0884         return -EINVAL;
0885 
0886     inp->type = V4L2_INPUT_TYPE_CAMERA;
0887     subdev = tegra_channel_get_remote_source_subdev(chan);
0888     strscpy(inp->name, subdev->name, sizeof(inp->name));
0889     if (v4l2_subdev_has_op(subdev, pad, dv_timings_cap))
0890         inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
0891 
0892     return 0;
0893 }
0894 
0895 static int tegra_channel_g_input(struct file *file, void *priv,
0896                  unsigned int *i)
0897 {
0898     *i = 0;
0899 
0900     return 0;
0901 }
0902 
0903 static int tegra_channel_s_input(struct file *file, void *priv,
0904                  unsigned int input)
0905 {
0906     if (input > 0)
0907         return -EINVAL;
0908 
0909     return 0;
0910 }
0911 
0912 static const struct v4l2_ioctl_ops tegra_channel_ioctl_ops = {
0913     .vidioc_querycap        = tegra_channel_querycap,
0914     .vidioc_g_parm          = tegra_channel_g_parm,
0915     .vidioc_s_parm          = tegra_channel_s_parm,
0916     .vidioc_enum_framesizes     = tegra_channel_enum_framesizes,
0917     .vidioc_enum_frameintervals = tegra_channel_enum_frameintervals,
0918     .vidioc_enum_fmt_vid_cap    = tegra_channel_enum_format,
0919     .vidioc_g_fmt_vid_cap       = tegra_channel_get_format,
0920     .vidioc_s_fmt_vid_cap       = tegra_channel_set_format,
0921     .vidioc_try_fmt_vid_cap     = tegra_channel_try_format,
0922     .vidioc_enum_input      = tegra_channel_enum_input,
0923     .vidioc_g_input         = tegra_channel_g_input,
0924     .vidioc_s_input         = tegra_channel_s_input,
0925     .vidioc_reqbufs         = vb2_ioctl_reqbufs,
0926     .vidioc_prepare_buf     = vb2_ioctl_prepare_buf,
0927     .vidioc_querybuf        = vb2_ioctl_querybuf,
0928     .vidioc_qbuf            = vb2_ioctl_qbuf,
0929     .vidioc_dqbuf           = vb2_ioctl_dqbuf,
0930     .vidioc_create_bufs     = vb2_ioctl_create_bufs,
0931     .vidioc_expbuf          = vb2_ioctl_expbuf,
0932     .vidioc_streamon        = vb2_ioctl_streamon,
0933     .vidioc_streamoff       = vb2_ioctl_streamoff,
0934     .vidioc_subscribe_event     = tegra_channel_subscribe_event,
0935     .vidioc_unsubscribe_event   = v4l2_event_unsubscribe,
0936     .vidioc_g_selection     = tegra_channel_g_selection,
0937     .vidioc_s_selection     = tegra_channel_s_selection,
0938     .vidioc_g_edid          = tegra_channel_g_edid,
0939     .vidioc_s_edid          = tegra_channel_s_edid,
0940     .vidioc_g_dv_timings        = tegra_channel_g_dv_timings,
0941     .vidioc_s_dv_timings        = tegra_channel_s_dv_timings,
0942     .vidioc_query_dv_timings    = tegra_channel_query_dv_timings,
0943     .vidioc_enum_dv_timings     = tegra_channel_enum_dv_timings,
0944     .vidioc_dv_timings_cap      = tegra_channel_dv_timings_cap,
0945     .vidioc_log_status      = tegra_channel_log_status,
0946 };
0947 
0948 /*
0949  * V4L2 file operations
0950  */
0951 static const struct v4l2_file_operations tegra_channel_fops = {
0952     .owner      = THIS_MODULE,
0953     .unlocked_ioctl = video_ioctl2,
0954     .open       = v4l2_fh_open,
0955     .release    = vb2_fop_release,
0956     .read       = vb2_fop_read,
0957     .poll       = vb2_fop_poll,
0958     .mmap       = vb2_fop_mmap,
0959 };
0960 
0961 /*
0962  * V4L2 control operations
0963  */
0964 static int vi_s_ctrl(struct v4l2_ctrl *ctrl)
0965 {
0966     struct tegra_vi_channel *chan = container_of(ctrl->handler,
0967                              struct tegra_vi_channel,
0968                              ctrl_handler);
0969 
0970     switch (ctrl->id) {
0971     case V4L2_CID_TEST_PATTERN:
0972         /* pattern change takes effect on next stream */
0973         chan->pg_mode = ctrl->val + 1;
0974         break;
0975     case V4L2_CID_TEGRA_SYNCPT_TIMEOUT_RETRY:
0976         chan->syncpt_timeout_retry = ctrl->val;
0977         break;
0978     default:
0979         return -EINVAL;
0980     }
0981 
0982     return 0;
0983 }
0984 
0985 static const struct v4l2_ctrl_ops vi_ctrl_ops = {
0986     .s_ctrl = vi_s_ctrl,
0987 };
0988 
0989 #if IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)
0990 static const char *const vi_pattern_strings[] = {
0991     "Black/White Direct Mode",
0992     "Color Patch Mode",
0993 };
0994 #else
0995 static const struct v4l2_ctrl_config syncpt_timeout_ctrl = {
0996     .ops = &vi_ctrl_ops,
0997     .id = V4L2_CID_TEGRA_SYNCPT_TIMEOUT_RETRY,
0998     .name = "Syncpt timeout retry",
0999     .type = V4L2_CTRL_TYPE_INTEGER,
1000     .min = 1,
1001     .max = 10000,
1002     .step = 1,
1003     .def = 5,
1004 };
1005 #endif
1006 
1007 static int tegra_channel_setup_ctrl_handler(struct tegra_vi_channel *chan)
1008 {
1009     int ret;
1010 
1011 #if IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)
1012     /* add test pattern control handler to v4l2 device */
1013     v4l2_ctrl_new_std_menu_items(&chan->ctrl_handler, &vi_ctrl_ops,
1014                      V4L2_CID_TEST_PATTERN,
1015                      ARRAY_SIZE(vi_pattern_strings) - 1,
1016                      0, 0, vi_pattern_strings);
1017     if (chan->ctrl_handler.error) {
1018         dev_err(chan->vi->dev, "failed to add TPG ctrl handler: %d\n",
1019             chan->ctrl_handler.error);
1020         v4l2_ctrl_handler_free(&chan->ctrl_handler);
1021         return chan->ctrl_handler.error;
1022     }
1023 #else
1024     struct v4l2_subdev *subdev;
1025 
1026     /* custom control */
1027     v4l2_ctrl_new_custom(&chan->ctrl_handler, &syncpt_timeout_ctrl, NULL);
1028     if (chan->ctrl_handler.error) {
1029         dev_err(chan->vi->dev, "failed to add %s ctrl handler: %d\n",
1030             syncpt_timeout_ctrl.name,
1031             chan->ctrl_handler.error);
1032         v4l2_ctrl_handler_free(&chan->ctrl_handler);
1033         return chan->ctrl_handler.error;
1034     }
1035 
1036     subdev = tegra_channel_get_remote_source_subdev(chan);
1037     if (!subdev)
1038         return -ENODEV;
1039 
1040     ret = v4l2_ctrl_add_handler(&chan->ctrl_handler, subdev->ctrl_handler,
1041                     NULL, true);
1042     if (ret < 0) {
1043         dev_err(chan->vi->dev,
1044             "failed to add subdev %s ctrl handler: %d\n",
1045             subdev->name, ret);
1046         v4l2_ctrl_handler_free(&chan->ctrl_handler);
1047         return ret;
1048     }
1049 #endif
1050 
1051     /* setup the controls */
1052     ret = v4l2_ctrl_handler_setup(&chan->ctrl_handler);
1053     if (ret < 0) {
1054         dev_err(chan->vi->dev,
1055             "failed to setup v4l2 ctrl handler: %d\n", ret);
1056         return ret;
1057     }
1058 
1059     return 0;
1060 }
1061 
1062 /* VI only support 2 formats in TPG mode */
1063 static void vi_tpg_fmts_bitmap_init(struct tegra_vi_channel *chan)
1064 {
1065     int index;
1066 
1067     bitmap_zero(chan->tpg_fmts_bitmap, MAX_FORMAT_NUM);
1068 
1069     index = tegra_get_format_idx_by_code(chan->vi,
1070                          MEDIA_BUS_FMT_SRGGB10_1X10, 0);
1071     bitmap_set(chan->tpg_fmts_bitmap, index, 1);
1072 
1073     index = tegra_get_format_idx_by_code(chan->vi,
1074                          MEDIA_BUS_FMT_RGB888_1X32_PADHI,
1075                          0);
1076     bitmap_set(chan->tpg_fmts_bitmap, index, 1);
1077 }
1078 
1079 static int vi_fmts_bitmap_init(struct tegra_vi_channel *chan)
1080 {
1081     int index, ret, match_code = 0;
1082     struct v4l2_subdev *subdev;
1083     struct v4l2_subdev_mbus_code_enum code = {
1084         .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1085     };
1086 
1087     bitmap_zero(chan->fmts_bitmap, MAX_FORMAT_NUM);
1088 
1089     /*
1090      * Set the bitmap bits based on all the matched formats between the
1091      * available media bus formats of sub-device and the pre-defined Tegra
1092      * supported video formats.
1093      */
1094     subdev = tegra_channel_get_remote_source_subdev(chan);
1095     while (1) {
1096         ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
1097                        NULL, &code);
1098         if (ret < 0)
1099             break;
1100 
1101         index = tegra_get_format_idx_by_code(chan->vi, code.code, 0);
1102         while (index >= 0) {
1103             bitmap_set(chan->fmts_bitmap, index, 1);
1104             if (!match_code)
1105                 match_code = code.code;
1106             /* look for other formats with same mbus code */
1107             index = tegra_get_format_idx_by_code(chan->vi,
1108                                  code.code,
1109                                  index + 1);
1110         }
1111 
1112         code.index++;
1113     }
1114 
1115     /*
1116      * Set the bitmap bit corresponding to default tegra video format if
1117      * there are no matched formats.
1118      */
1119     if (!match_code) {
1120         match_code = tegra_default_format.code;
1121         index = tegra_get_format_idx_by_code(chan->vi, match_code, 0);
1122         if (WARN_ON(index < 0))
1123             return -EINVAL;
1124 
1125         bitmap_set(chan->fmts_bitmap, index, 1);
1126     }
1127 
1128     /* initialize channel format to the sub-device active format */
1129     tegra_channel_set_subdev_active_fmt(chan);
1130 
1131     return 0;
1132 }
1133 
1134 static void tegra_channel_host1x_syncpts_free(struct tegra_vi_channel *chan)
1135 {
1136     int i;
1137 
1138     for (i = 0; i < chan->numgangports; i++) {
1139         host1x_syncpt_put(chan->mw_ack_sp[i]);
1140         host1x_syncpt_put(chan->frame_start_sp[i]);
1141     }
1142 }
1143 
1144 static void tegra_channel_cleanup(struct tegra_vi_channel *chan)
1145 {
1146     v4l2_ctrl_handler_free(&chan->ctrl_handler);
1147     media_entity_cleanup(&chan->video.entity);
1148     tegra_channel_host1x_syncpts_free(chan);
1149     mutex_destroy(&chan->video_lock);
1150 }
1151 
1152 void tegra_channels_cleanup(struct tegra_vi *vi)
1153 {
1154     struct tegra_vi_channel *chan, *tmp;
1155 
1156     if (!vi)
1157         return;
1158 
1159     list_for_each_entry_safe(chan, tmp, &vi->vi_chans, list) {
1160         tegra_channel_cleanup(chan);
1161         list_del(&chan->list);
1162         kfree(chan);
1163     }
1164 }
1165 
1166 static int tegra_channel_host1x_syncpt_init(struct tegra_vi_channel *chan)
1167 {
1168     struct tegra_vi *vi = chan->vi;
1169     unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
1170     struct host1x_syncpt *fs_sp;
1171     struct host1x_syncpt *mw_sp;
1172     int ret, i;
1173 
1174     for (i = 0; i < chan->numgangports; i++) {
1175         fs_sp = host1x_syncpt_request(&vi->client, flags);
1176         if (!fs_sp) {
1177             dev_err(vi->dev, "failed to request frame start syncpoint\n");
1178             ret = -ENOMEM;
1179             goto free_syncpts;
1180         }
1181 
1182         mw_sp = host1x_syncpt_request(&vi->client, flags);
1183         if (!mw_sp) {
1184             dev_err(vi->dev, "failed to request memory ack syncpoint\n");
1185             host1x_syncpt_put(fs_sp);
1186             ret = -ENOMEM;
1187             goto free_syncpts;
1188         }
1189 
1190         chan->frame_start_sp[i] = fs_sp;
1191         chan->mw_ack_sp[i] = mw_sp;
1192         spin_lock_init(&chan->sp_incr_lock[i]);
1193     }
1194 
1195     return 0;
1196 
1197 free_syncpts:
1198     tegra_channel_host1x_syncpts_free(chan);
1199     return ret;
1200 }
1201 
1202 static int tegra_channel_init(struct tegra_vi_channel *chan)
1203 {
1204     struct tegra_vi *vi = chan->vi;
1205     struct tegra_video_device *vid = dev_get_drvdata(vi->client.host);
1206     int ret;
1207 
1208     mutex_init(&chan->video_lock);
1209     INIT_LIST_HEAD(&chan->capture);
1210     INIT_LIST_HEAD(&chan->done);
1211     spin_lock_init(&chan->start_lock);
1212     spin_lock_init(&chan->done_lock);
1213     init_waitqueue_head(&chan->start_wait);
1214     init_waitqueue_head(&chan->done_wait);
1215 
1216     /* initialize the video format */
1217     chan->fmtinfo = &tegra_default_format;
1218     chan->format.pixelformat = chan->fmtinfo->fourcc;
1219     chan->format.colorspace = V4L2_COLORSPACE_SRGB;
1220     chan->format.field = V4L2_FIELD_NONE;
1221     chan->format.width = TEGRA_DEF_WIDTH;
1222     chan->format.height = TEGRA_DEF_HEIGHT;
1223     chan->format.bytesperline = TEGRA_DEF_WIDTH * chan->fmtinfo->bpp;
1224     chan->format.sizeimage = chan->format.bytesperline * TEGRA_DEF_HEIGHT;
1225     tegra_channel_fmt_align(chan, &chan->format, chan->fmtinfo->bpp);
1226 
1227     ret = tegra_channel_host1x_syncpt_init(chan);
1228     if (ret)
1229         return ret;
1230 
1231     /* initialize the media entity */
1232     chan->pad.flags = MEDIA_PAD_FL_SINK;
1233     ret = media_entity_pads_init(&chan->video.entity, 1, &chan->pad);
1234     if (ret < 0) {
1235         dev_err(vi->dev,
1236             "failed to initialize media entity: %d\n", ret);
1237         goto free_syncpts;
1238     }
1239 
1240     ret = v4l2_ctrl_handler_init(&chan->ctrl_handler, MAX_CID_CONTROLS);
1241     if (chan->ctrl_handler.error) {
1242         dev_err(vi->dev,
1243             "failed to initialize v4l2 ctrl handler: %d\n", ret);
1244         goto cleanup_media;
1245     }
1246 
1247     /* initialize the video_device */
1248     chan->video.fops = &tegra_channel_fops;
1249     chan->video.v4l2_dev = &vid->v4l2_dev;
1250     chan->video.release = video_device_release_empty;
1251     chan->video.queue = &chan->queue;
1252     snprintf(chan->video.name, sizeof(chan->video.name), "%s-%s-%u",
1253          dev_name(vi->dev), "output", chan->portnos[0]);
1254     chan->video.vfl_type = VFL_TYPE_VIDEO;
1255     chan->video.vfl_dir = VFL_DIR_RX;
1256     chan->video.ioctl_ops = &tegra_channel_ioctl_ops;
1257     chan->video.ctrl_handler = &chan->ctrl_handler;
1258     chan->video.lock = &chan->video_lock;
1259     chan->video.device_caps = V4L2_CAP_VIDEO_CAPTURE |
1260                   V4L2_CAP_STREAMING |
1261                   V4L2_CAP_READWRITE;
1262     video_set_drvdata(&chan->video, chan);
1263 
1264     chan->queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1265     chan->queue.io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
1266     chan->queue.lock = &chan->video_lock;
1267     chan->queue.drv_priv = chan;
1268     chan->queue.buf_struct_size = sizeof(struct tegra_channel_buffer);
1269     chan->queue.ops = &tegra_channel_queue_qops;
1270     chan->queue.mem_ops = &vb2_dma_contig_memops;
1271     chan->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1272     chan->queue.min_buffers_needed = 2;
1273     chan->queue.dev = vi->dev;
1274     ret = vb2_queue_init(&chan->queue);
1275     if (ret < 0) {
1276         dev_err(vi->dev, "failed to initialize vb2 queue: %d\n", ret);
1277         goto free_v4l2_ctrl_hdl;
1278     }
1279 
1280     if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
1281         v4l2_async_nf_init(&chan->notifier);
1282 
1283     return 0;
1284 
1285 free_v4l2_ctrl_hdl:
1286     v4l2_ctrl_handler_free(&chan->ctrl_handler);
1287 cleanup_media:
1288     media_entity_cleanup(&chan->video.entity);
1289 free_syncpts:
1290     tegra_channel_host1x_syncpts_free(chan);
1291     return ret;
1292 }
1293 
1294 static int tegra_vi_channel_alloc(struct tegra_vi *vi, unsigned int port_num,
1295                   struct device_node *node, unsigned int lanes)
1296 {
1297     struct tegra_vi_channel *chan;
1298     unsigned int i;
1299 
1300     /*
1301      * Do not use devm_kzalloc as memory is freed immediately
1302      * when device instance is unbound but application might still
1303      * be holding the device node open. Channel memory allocated
1304      * with kzalloc is freed during video device release callback.
1305      */
1306     chan = kzalloc(sizeof(*chan), GFP_KERNEL);
1307     if (!chan)
1308         return -ENOMEM;
1309 
1310     chan->vi = vi;
1311     chan->portnos[0] = port_num;
1312     /*
1313      * For data lanes more than maximum csi lanes per brick, multiple of
1314      * x4 ports are used simultaneously for capture.
1315      */
1316     if (lanes <= CSI_LANES_PER_BRICK)
1317         chan->totalports = 1;
1318     else
1319         chan->totalports = lanes / CSI_LANES_PER_BRICK;
1320     chan->numgangports = chan->totalports;
1321 
1322     for (i = 1; i < chan->totalports; i++)
1323         chan->portnos[i] = chan->portnos[0] + i * CSI_PORTS_PER_BRICK;
1324 
1325     chan->of_node = node;
1326     list_add_tail(&chan->list, &vi->vi_chans);
1327 
1328     return 0;
1329 }
1330 
1331 static int tegra_vi_tpg_channels_alloc(struct tegra_vi *vi)
1332 {
1333     unsigned int port_num;
1334     unsigned int nchannels = vi->soc->vi_max_channels;
1335     int ret;
1336 
1337     for (port_num = 0; port_num < nchannels; port_num++) {
1338         ret = tegra_vi_channel_alloc(vi, port_num,
1339                          vi->dev->of_node, 2);
1340         if (ret < 0)
1341             return ret;
1342     }
1343 
1344     return 0;
1345 }
1346 
1347 static int tegra_vi_channels_alloc(struct tegra_vi *vi)
1348 {
1349     struct device_node *node = vi->dev->of_node;
1350     struct device_node *ep = NULL;
1351     struct device_node *ports;
1352     struct device_node *port;
1353     unsigned int port_num;
1354     struct device_node *parent;
1355     struct v4l2_fwnode_endpoint v4l2_ep = { .bus_type = 0 };
1356     unsigned int lanes;
1357     int ret = 0;
1358 
1359     ports = of_get_child_by_name(node, "ports");
1360     if (!ports)
1361         return -ENODEV;
1362 
1363     for_each_child_of_node(ports, port) {
1364         if (!of_node_name_eq(port, "port"))
1365             continue;
1366 
1367         ret = of_property_read_u32(port, "reg", &port_num);
1368         if (ret < 0)
1369             continue;
1370 
1371         if (port_num > vi->soc->vi_max_channels) {
1372             dev_err(vi->dev, "invalid port num %d for %pOF\n",
1373                 port_num, port);
1374             ret = -EINVAL;
1375             of_node_put(port);
1376             goto cleanup;
1377         }
1378 
1379         ep = of_get_child_by_name(port, "endpoint");
1380         if (!ep)
1381             continue;
1382 
1383         parent = of_graph_get_remote_port_parent(ep);
1384         of_node_put(ep);
1385         if (!parent)
1386             continue;
1387 
1388         ep = of_graph_get_endpoint_by_regs(parent, 0, 0);
1389         of_node_put(parent);
1390         ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep),
1391                          &v4l2_ep);
1392         of_node_put(ep);
1393         if (ret)
1394             continue;
1395 
1396         lanes = v4l2_ep.bus.mipi_csi2.num_data_lanes;
1397         ret = tegra_vi_channel_alloc(vi, port_num, port, lanes);
1398         if (ret < 0) {
1399             of_node_put(port);
1400             goto cleanup;
1401         }
1402     }
1403 
1404 cleanup:
1405     of_node_put(ports);
1406     return ret;
1407 }
1408 
1409 static int tegra_vi_channels_init(struct tegra_vi *vi)
1410 {
1411     struct tegra_vi_channel *chan;
1412     int ret;
1413 
1414     list_for_each_entry(chan, &vi->vi_chans, list) {
1415         ret = tegra_channel_init(chan);
1416         if (ret < 0) {
1417             dev_err(vi->dev,
1418                 "failed to initialize channel-%d: %d\n",
1419                 chan->portnos[0], ret);
1420             goto cleanup;
1421         }
1422     }
1423 
1424     return 0;
1425 
1426 cleanup:
1427     list_for_each_entry_continue_reverse(chan, &vi->vi_chans, list)
1428         tegra_channel_cleanup(chan);
1429 
1430     return ret;
1431 }
1432 
1433 void tegra_v4l2_nodes_cleanup_tpg(struct tegra_video_device *vid)
1434 {
1435     struct tegra_vi *vi = vid->vi;
1436     struct tegra_csi *csi = vid->csi;
1437     struct tegra_csi_channel *csi_chan;
1438     struct tegra_vi_channel *chan;
1439 
1440     list_for_each_entry(chan, &vi->vi_chans, list)
1441         vb2_video_unregister_device(&chan->video);
1442 
1443     list_for_each_entry(csi_chan, &csi->csi_chans, list)
1444         v4l2_device_unregister_subdev(&csi_chan->subdev);
1445 }
1446 
1447 int tegra_v4l2_nodes_setup_tpg(struct tegra_video_device *vid)
1448 {
1449     struct tegra_vi *vi = vid->vi;
1450     struct tegra_csi *csi = vid->csi;
1451     struct tegra_vi_channel *vi_chan;
1452     struct tegra_csi_channel *csi_chan;
1453     u32 link_flags = MEDIA_LNK_FL_ENABLED;
1454     int ret;
1455 
1456     if (!vi || !csi)
1457         return -ENODEV;
1458 
1459     csi_chan = list_first_entry(&csi->csi_chans,
1460                     struct tegra_csi_channel, list);
1461 
1462     list_for_each_entry(vi_chan, &vi->vi_chans, list) {
1463         struct media_entity *source = &csi_chan->subdev.entity;
1464         struct media_entity *sink = &vi_chan->video.entity;
1465         struct media_pad *source_pad = csi_chan->pads;
1466         struct media_pad *sink_pad = &vi_chan->pad;
1467 
1468         ret = v4l2_device_register_subdev(&vid->v4l2_dev,
1469                           &csi_chan->subdev);
1470         if (ret) {
1471             dev_err(vi->dev,
1472                 "failed to register subdev: %d\n", ret);
1473             goto cleanup;
1474         }
1475 
1476         ret = video_register_device(&vi_chan->video,
1477                         VFL_TYPE_VIDEO, -1);
1478         if (ret < 0) {
1479             dev_err(vi->dev,
1480                 "failed to register video device: %d\n", ret);
1481             goto cleanup;
1482         }
1483 
1484         dev_dbg(vi->dev, "creating %s:%u -> %s:%u link\n",
1485             source->name, source_pad->index,
1486             sink->name, sink_pad->index);
1487 
1488         ret = media_create_pad_link(source, source_pad->index,
1489                         sink, sink_pad->index,
1490                         link_flags);
1491         if (ret < 0) {
1492             dev_err(vi->dev,
1493                 "failed to create %s:%u -> %s:%u link: %d\n",
1494                 source->name, source_pad->index,
1495                 sink->name, sink_pad->index, ret);
1496             goto cleanup;
1497         }
1498 
1499         ret = tegra_channel_setup_ctrl_handler(vi_chan);
1500         if (ret < 0)
1501             goto cleanup;
1502 
1503         v4l2_set_subdev_hostdata(&csi_chan->subdev, vi_chan);
1504         vi_tpg_fmts_bitmap_init(vi_chan);
1505         csi_chan = list_next_entry(csi_chan, list);
1506     }
1507 
1508     return 0;
1509 
1510 cleanup:
1511     tegra_v4l2_nodes_cleanup_tpg(vid);
1512     return ret;
1513 }
1514 
1515 static int __maybe_unused vi_runtime_resume(struct device *dev)
1516 {
1517     struct tegra_vi *vi = dev_get_drvdata(dev);
1518     int ret;
1519 
1520     ret = regulator_enable(vi->vdd);
1521     if (ret) {
1522         dev_err(dev, "failed to enable VDD supply: %d\n", ret);
1523         return ret;
1524     }
1525 
1526     ret = clk_set_rate(vi->clk, vi->soc->vi_max_clk_hz);
1527     if (ret) {
1528         dev_err(dev, "failed to set vi clock rate: %d\n", ret);
1529         goto disable_vdd;
1530     }
1531 
1532     ret = clk_prepare_enable(vi->clk);
1533     if (ret) {
1534         dev_err(dev, "failed to enable vi clock: %d\n", ret);
1535         goto disable_vdd;
1536     }
1537 
1538     return 0;
1539 
1540 disable_vdd:
1541     regulator_disable(vi->vdd);
1542     return ret;
1543 }
1544 
1545 static int __maybe_unused vi_runtime_suspend(struct device *dev)
1546 {
1547     struct tegra_vi *vi = dev_get_drvdata(dev);
1548 
1549     clk_disable_unprepare(vi->clk);
1550 
1551     regulator_disable(vi->vdd);
1552 
1553     return 0;
1554 }
1555 
1556 /*
1557  * Graph Management
1558  */
1559 static struct tegra_vi_graph_entity *
1560 tegra_vi_graph_find_entity(struct tegra_vi_channel *chan,
1561                const struct fwnode_handle *fwnode)
1562 {
1563     struct tegra_vi_graph_entity *entity;
1564     struct v4l2_async_subdev *asd;
1565 
1566     list_for_each_entry(asd, &chan->notifier.asd_list, asd_list) {
1567         entity = to_tegra_vi_graph_entity(asd);
1568         if (entity->asd.match.fwnode == fwnode)
1569             return entity;
1570     }
1571 
1572     return NULL;
1573 }
1574 
1575 static int tegra_vi_graph_build(struct tegra_vi_channel *chan,
1576                 struct tegra_vi_graph_entity *entity)
1577 {
1578     struct tegra_vi *vi = chan->vi;
1579     struct tegra_vi_graph_entity *ent;
1580     struct fwnode_handle *ep = NULL;
1581     struct v4l2_fwnode_link link;
1582     struct media_entity *local = entity->entity;
1583     struct media_entity *remote;
1584     struct media_pad *local_pad;
1585     struct media_pad *remote_pad;
1586     u32 link_flags = MEDIA_LNK_FL_ENABLED;
1587     int ret = 0;
1588 
1589     dev_dbg(vi->dev, "creating links for entity %s\n", local->name);
1590 
1591     while (1) {
1592         ep = fwnode_graph_get_next_endpoint(entity->asd.match.fwnode,
1593                             ep);
1594         if (!ep)
1595             break;
1596 
1597         ret = v4l2_fwnode_parse_link(ep, &link);
1598         if (ret < 0) {
1599             dev_err(vi->dev, "failed to parse link for %pOF: %d\n",
1600                 to_of_node(ep), ret);
1601             continue;
1602         }
1603 
1604         if (link.local_port >= local->num_pads) {
1605             dev_err(vi->dev, "invalid port number %u on %pOF\n",
1606                 link.local_port, to_of_node(link.local_node));
1607             v4l2_fwnode_put_link(&link);
1608             ret = -EINVAL;
1609             break;
1610         }
1611 
1612         local_pad = &local->pads[link.local_port];
1613         /* Remote node is vi node. So use channel video entity and pad
1614          * as remote/sink.
1615          */
1616         if (link.remote_node == of_fwnode_handle(vi->dev->of_node)) {
1617             remote = &chan->video.entity;
1618             remote_pad = &chan->pad;
1619             goto create_link;
1620         }
1621 
1622         /*
1623          * Skip sink ports, they will be processed from the other end
1624          * of the link.
1625          */
1626         if (local_pad->flags & MEDIA_PAD_FL_SINK) {
1627             dev_dbg(vi->dev, "skipping sink port %pOF:%u\n",
1628                 to_of_node(link.local_node), link.local_port);
1629             v4l2_fwnode_put_link(&link);
1630             continue;
1631         }
1632 
1633         /* find the remote entity from notifier list */
1634         ent = tegra_vi_graph_find_entity(chan, link.remote_node);
1635         if (!ent) {
1636             dev_err(vi->dev, "no entity found for %pOF\n",
1637                 to_of_node(link.remote_node));
1638             v4l2_fwnode_put_link(&link);
1639             ret = -ENODEV;
1640             break;
1641         }
1642 
1643         remote = ent->entity;
1644         if (link.remote_port >= remote->num_pads) {
1645             dev_err(vi->dev, "invalid port number %u on %pOF\n",
1646                 link.remote_port,
1647                 to_of_node(link.remote_node));
1648             v4l2_fwnode_put_link(&link);
1649             ret = -EINVAL;
1650             break;
1651         }
1652 
1653         remote_pad = &remote->pads[link.remote_port];
1654 
1655 create_link:
1656         dev_dbg(vi->dev, "creating %s:%u -> %s:%u link\n",
1657             local->name, local_pad->index,
1658             remote->name, remote_pad->index);
1659 
1660         ret = media_create_pad_link(local, local_pad->index,
1661                         remote, remote_pad->index,
1662                         link_flags);
1663         v4l2_fwnode_put_link(&link);
1664         if (ret < 0) {
1665             dev_err(vi->dev,
1666                 "failed to create %s:%u -> %s:%u link: %d\n",
1667                 local->name, local_pad->index,
1668                 remote->name, remote_pad->index, ret);
1669             break;
1670         }
1671     }
1672 
1673     fwnode_handle_put(ep);
1674     return ret;
1675 }
1676 
1677 static int tegra_vi_graph_notify_complete(struct v4l2_async_notifier *notifier)
1678 {
1679     struct tegra_vi_graph_entity *entity;
1680     struct v4l2_async_subdev *asd;
1681     struct v4l2_subdev *subdev;
1682     struct tegra_vi_channel *chan;
1683     struct tegra_vi *vi;
1684     int ret;
1685 
1686     chan = container_of(notifier, struct tegra_vi_channel, notifier);
1687     vi = chan->vi;
1688 
1689     dev_dbg(vi->dev, "notify complete, all subdevs registered\n");
1690 
1691     /*
1692      * Video device node should be created at the end of all the device
1693      * related initialization/setup.
1694      * Current video_register_device() does both initialize and register
1695      * video device in same API.
1696      *
1697      * TODO: Update v4l2-dev driver to split initialize and register into
1698      * separate APIs and then update Tegra video driver to do video device
1699      * initialize followed by all video device related setup and then
1700      * register the video device.
1701      */
1702     ret = video_register_device(&chan->video, VFL_TYPE_VIDEO, -1);
1703     if (ret < 0) {
1704         dev_err(vi->dev,
1705             "failed to register video device: %d\n", ret);
1706         goto unregister_video;
1707     }
1708 
1709     /* create links between the entities */
1710     list_for_each_entry(asd, &chan->notifier.asd_list, asd_list) {
1711         entity = to_tegra_vi_graph_entity(asd);
1712         ret = tegra_vi_graph_build(chan, entity);
1713         if (ret < 0)
1714             goto unregister_video;
1715     }
1716 
1717     ret = tegra_channel_setup_ctrl_handler(chan);
1718     if (ret < 0) {
1719         dev_err(vi->dev,
1720             "failed to setup channel controls: %d\n", ret);
1721         goto unregister_video;
1722     }
1723 
1724     ret = vi_fmts_bitmap_init(chan);
1725     if (ret < 0) {
1726         dev_err(vi->dev,
1727             "failed to initialize formats bitmap: %d\n", ret);
1728         goto unregister_video;
1729     }
1730 
1731     subdev = tegra_channel_get_remote_csi_subdev(chan);
1732     if (!subdev) {
1733         ret = -ENODEV;
1734         dev_err(vi->dev,
1735             "failed to get remote csi subdev: %d\n", ret);
1736         goto unregister_video;
1737     }
1738 
1739     v4l2_set_subdev_hostdata(subdev, chan);
1740 
1741     subdev = tegra_channel_get_remote_source_subdev(chan);
1742     v4l2_set_subdev_hostdata(subdev, chan);
1743 
1744     return 0;
1745 
1746 unregister_video:
1747     vb2_video_unregister_device(&chan->video);
1748     return ret;
1749 }
1750 
1751 static int tegra_vi_graph_notify_bound(struct v4l2_async_notifier *notifier,
1752                        struct v4l2_subdev *subdev,
1753                        struct v4l2_async_subdev *asd)
1754 {
1755     struct tegra_vi_graph_entity *entity;
1756     struct tegra_vi *vi;
1757     struct tegra_vi_channel *chan;
1758 
1759     chan = container_of(notifier, struct tegra_vi_channel, notifier);
1760     vi = chan->vi;
1761 
1762     /*
1763      * Locate the entity corresponding to the bound subdev and store the
1764      * subdev pointer.
1765      */
1766     entity = tegra_vi_graph_find_entity(chan, subdev->fwnode);
1767     if (!entity) {
1768         dev_err(vi->dev, "no entity for subdev %s\n", subdev->name);
1769         return -EINVAL;
1770     }
1771 
1772     if (entity->subdev) {
1773         dev_err(vi->dev, "duplicate subdev for node %pOF\n",
1774             to_of_node(entity->asd.match.fwnode));
1775         return -EINVAL;
1776     }
1777 
1778     dev_dbg(vi->dev, "subdev %s bound\n", subdev->name);
1779     entity->entity = &subdev->entity;
1780     entity->subdev = subdev;
1781 
1782     return 0;
1783 }
1784 
1785 static const struct v4l2_async_notifier_operations tegra_vi_async_ops = {
1786     .bound = tegra_vi_graph_notify_bound,
1787     .complete = tegra_vi_graph_notify_complete,
1788 };
1789 
1790 static int tegra_vi_graph_parse_one(struct tegra_vi_channel *chan,
1791                     struct fwnode_handle *fwnode)
1792 {
1793     struct tegra_vi *vi = chan->vi;
1794     struct fwnode_handle *ep = NULL;
1795     struct fwnode_handle *remote = NULL;
1796     struct tegra_vi_graph_entity *tvge;
1797     struct device_node *node = NULL;
1798     int ret;
1799 
1800     dev_dbg(vi->dev, "parsing node %pOF\n", to_of_node(fwnode));
1801 
1802     /* parse all the remote entities and put them into the list */
1803     for_each_endpoint_of_node(to_of_node(fwnode), node) {
1804         ep = of_fwnode_handle(node);
1805         remote = fwnode_graph_get_remote_port_parent(ep);
1806         if (!remote) {
1807             dev_err(vi->dev,
1808                 "remote device at %pOF not found\n", node);
1809             ret = -EINVAL;
1810             goto cleanup;
1811         }
1812 
1813         /* skip entities that are already processed */
1814         if (remote == dev_fwnode(vi->dev) ||
1815             tegra_vi_graph_find_entity(chan, remote)) {
1816             fwnode_handle_put(remote);
1817             continue;
1818         }
1819 
1820         tvge = v4l2_async_nf_add_fwnode(&chan->notifier, remote,
1821                         struct tegra_vi_graph_entity);
1822         if (IS_ERR(tvge)) {
1823             ret = PTR_ERR(tvge);
1824             dev_err(vi->dev,
1825                 "failed to add subdev to notifier: %d\n", ret);
1826             fwnode_handle_put(remote);
1827             goto cleanup;
1828         }
1829 
1830         ret = tegra_vi_graph_parse_one(chan, remote);
1831         if (ret < 0) {
1832             fwnode_handle_put(remote);
1833             goto cleanup;
1834         }
1835 
1836         fwnode_handle_put(remote);
1837     }
1838 
1839     return 0;
1840 
1841 cleanup:
1842     dev_err(vi->dev, "failed parsing the graph: %d\n", ret);
1843     v4l2_async_nf_cleanup(&chan->notifier);
1844     of_node_put(node);
1845     return ret;
1846 }
1847 
1848 static int tegra_vi_graph_init(struct tegra_vi *vi)
1849 {
1850     struct tegra_video_device *vid = dev_get_drvdata(vi->client.host);
1851     struct tegra_vi_channel *chan;
1852     struct fwnode_handle *fwnode = dev_fwnode(vi->dev);
1853     int ret;
1854 
1855     /*
1856      * Walk the links to parse the full graph. Each channel will have
1857      * one endpoint of the composite node. Start by parsing the
1858      * composite node and parse the remote entities in turn.
1859      * Each channel will register v4l2 async notifier to make the graph
1860      * independent between the channels so we can the current channel
1861      * in case of something wrong during graph parsing and continue with
1862      * next channels.
1863      */
1864     list_for_each_entry(chan, &vi->vi_chans, list) {
1865         struct fwnode_handle *ep, *remote;
1866 
1867         ep = fwnode_graph_get_endpoint_by_id(fwnode,
1868                              chan->portnos[0], 0, 0);
1869         if (!ep)
1870             continue;
1871 
1872         remote = fwnode_graph_get_remote_port_parent(ep);
1873         fwnode_handle_put(ep);
1874 
1875         ret = tegra_vi_graph_parse_one(chan, remote);
1876         fwnode_handle_put(remote);
1877         if (ret < 0 || list_empty(&chan->notifier.asd_list))
1878             continue;
1879 
1880         chan->notifier.ops = &tegra_vi_async_ops;
1881         ret = v4l2_async_nf_register(&vid->v4l2_dev, &chan->notifier);
1882         if (ret < 0) {
1883             dev_err(vi->dev,
1884                 "failed to register channel %d notifier: %d\n",
1885                 chan->portnos[0], ret);
1886             v4l2_async_nf_cleanup(&chan->notifier);
1887         }
1888     }
1889 
1890     return 0;
1891 }
1892 
1893 static void tegra_vi_graph_cleanup(struct tegra_vi *vi)
1894 {
1895     struct tegra_vi_channel *chan;
1896 
1897     list_for_each_entry(chan, &vi->vi_chans, list) {
1898         vb2_video_unregister_device(&chan->video);
1899         v4l2_async_nf_unregister(&chan->notifier);
1900         v4l2_async_nf_cleanup(&chan->notifier);
1901     }
1902 }
1903 
1904 static int tegra_vi_init(struct host1x_client *client)
1905 {
1906     struct tegra_video_device *vid = dev_get_drvdata(client->host);
1907     struct tegra_vi *vi = host1x_client_to_vi(client);
1908     struct tegra_vi_channel *chan, *tmp;
1909     int ret;
1910 
1911     vid->media_dev.hw_revision = vi->soc->hw_revision;
1912     snprintf(vid->media_dev.bus_info, sizeof(vid->media_dev.bus_info),
1913          "platform:%s", dev_name(vi->dev));
1914 
1915     INIT_LIST_HEAD(&vi->vi_chans);
1916 
1917     if (IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
1918         ret = tegra_vi_tpg_channels_alloc(vi);
1919     else
1920         ret = tegra_vi_channels_alloc(vi);
1921     if (ret < 0) {
1922         dev_err(vi->dev,
1923             "failed to allocate vi channels: %d\n", ret);
1924         goto free_chans;
1925     }
1926 
1927     ret = tegra_vi_channels_init(vi);
1928     if (ret < 0)
1929         goto free_chans;
1930 
1931     vid->vi = vi;
1932 
1933     if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)) {
1934         ret = tegra_vi_graph_init(vi);
1935         if (ret < 0)
1936             goto cleanup_chans;
1937     }
1938 
1939     return 0;
1940 
1941 cleanup_chans:
1942     list_for_each_entry(chan, &vi->vi_chans, list)
1943         tegra_channel_cleanup(chan);
1944 free_chans:
1945     list_for_each_entry_safe(chan, tmp, &vi->vi_chans, list) {
1946         list_del(&chan->list);
1947         kfree(chan);
1948     }
1949 
1950     return ret;
1951 }
1952 
1953 static int tegra_vi_exit(struct host1x_client *client)
1954 {
1955     struct tegra_vi *vi = host1x_client_to_vi(client);
1956 
1957     /*
1958      * Do not cleanup the channels here as application might still be
1959      * holding video device nodes. Channels cleanup will happen during
1960      * v4l2_device release callback which gets called after all video
1961      * device nodes are released.
1962      */
1963 
1964     if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
1965         tegra_vi_graph_cleanup(vi);
1966 
1967     return 0;
1968 }
1969 
1970 static const struct host1x_client_ops vi_client_ops = {
1971     .init = tegra_vi_init,
1972     .exit = tegra_vi_exit,
1973 };
1974 
1975 static int tegra_vi_probe(struct platform_device *pdev)
1976 {
1977     struct tegra_vi *vi;
1978     int ret;
1979 
1980     vi = devm_kzalloc(&pdev->dev, sizeof(*vi), GFP_KERNEL);
1981     if (!vi)
1982         return -ENOMEM;
1983 
1984     vi->iomem = devm_platform_ioremap_resource(pdev, 0);
1985     if (IS_ERR(vi->iomem))
1986         return PTR_ERR(vi->iomem);
1987 
1988     vi->soc = of_device_get_match_data(&pdev->dev);
1989 
1990     vi->clk = devm_clk_get(&pdev->dev, NULL);
1991     if (IS_ERR(vi->clk)) {
1992         ret = PTR_ERR(vi->clk);
1993         dev_err(&pdev->dev, "failed to get vi clock: %d\n", ret);
1994         return ret;
1995     }
1996 
1997     vi->vdd = devm_regulator_get(&pdev->dev, "avdd-dsi-csi");
1998     if (IS_ERR(vi->vdd)) {
1999         ret = PTR_ERR(vi->vdd);
2000         dev_err(&pdev->dev, "failed to get VDD supply: %d\n", ret);
2001         return ret;
2002     }
2003 
2004     if (!pdev->dev.pm_domain) {
2005         ret = -ENOENT;
2006         dev_warn(&pdev->dev, "PM domain is not attached: %d\n", ret);
2007         return ret;
2008     }
2009 
2010     ret = devm_of_platform_populate(&pdev->dev);
2011     if (ret < 0) {
2012         dev_err(&pdev->dev,
2013             "failed to populate vi child device: %d\n", ret);
2014         return ret;
2015     }
2016 
2017     vi->dev = &pdev->dev;
2018     vi->ops = vi->soc->ops;
2019     platform_set_drvdata(pdev, vi);
2020     pm_runtime_enable(&pdev->dev);
2021 
2022     /* initialize host1x interface */
2023     INIT_LIST_HEAD(&vi->client.list);
2024     vi->client.ops = &vi_client_ops;
2025     vi->client.dev = &pdev->dev;
2026 
2027     ret = host1x_client_register(&vi->client);
2028     if (ret < 0) {
2029         dev_err(&pdev->dev,
2030             "failed to register host1x client: %d\n", ret);
2031         goto rpm_disable;
2032     }
2033 
2034     return 0;
2035 
2036 rpm_disable:
2037     pm_runtime_disable(&pdev->dev);
2038     return ret;
2039 }
2040 
2041 static int tegra_vi_remove(struct platform_device *pdev)
2042 {
2043     struct tegra_vi *vi = platform_get_drvdata(pdev);
2044     int err;
2045 
2046     err = host1x_client_unregister(&vi->client);
2047     if (err < 0) {
2048         dev_err(&pdev->dev,
2049             "failed to unregister host1x client: %d\n", err);
2050         return err;
2051     }
2052 
2053     pm_runtime_disable(&pdev->dev);
2054 
2055     return 0;
2056 }
2057 
2058 static const struct of_device_id tegra_vi_of_id_table[] = {
2059 #if defined(CONFIG_ARCH_TEGRA_210_SOC)
2060     { .compatible = "nvidia,tegra210-vi", .data = &tegra210_vi_soc },
2061 #endif
2062     { }
2063 };
2064 MODULE_DEVICE_TABLE(of, tegra_vi_of_id_table);
2065 
2066 static const struct dev_pm_ops tegra_vi_pm_ops = {
2067     SET_RUNTIME_PM_OPS(vi_runtime_suspend, vi_runtime_resume, NULL)
2068 };
2069 
2070 struct platform_driver tegra_vi_driver = {
2071     .driver = {
2072         .name = "tegra-vi",
2073         .of_match_table = tegra_vi_of_id_table,
2074         .pm = &tegra_vi_pm_ops,
2075     },
2076     .probe = tegra_vi_probe,
2077     .remove = tegra_vi_remove,
2078 };