linux/drivers/staging/media/tegra-video/vi.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (C) 2020 NVIDIA CORPORATION.  All rights reserved.
   4 */
   5
   6#include <linux/bitmap.h>
   7#include <linux/clk.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>
  13#include <linux/of.h>
  14#include <linux/of_device.h>
  15#include <linux/of_graph.h>
  16#include <linux/platform_device.h>
  17#include <linux/regulator/consumer.h>
  18#include <linux/pm_runtime.h>
  19#include <linux/slab.h>
  20
  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>
  27
  28#include <soc/tegra/pmc.h>
  29
  30#include "vi.h"
  31#include "video.h"
  32
  33#define MAX_CID_CONTROLS                1
  34
  35static const struct tegra_video_format tegra_default_format = {
  36        .img_dt = TEGRA_IMAGE_DT_RAW10,
  37        .bit_width = 10,
  38        .code = MEDIA_BUS_FMT_SRGGB10_1X10,
  39        .bpp = 2,
  40        .img_fmt = TEGRA_IMAGE_FORMAT_DEF,
  41        .fourcc = V4L2_PIX_FMT_SRGGB10,
  42};
  43
  44static inline struct tegra_vi *
  45host1x_client_to_vi(struct host1x_client *client)
  46{
  47        return container_of(client, struct tegra_vi, client);
  48}
  49
  50static inline struct tegra_channel_buffer *
  51to_tegra_channel_buffer(struct vb2_v4l2_buffer *vb)
  52{
  53        return container_of(vb, struct tegra_channel_buffer, buf);
  54}
  55
  56static inline struct tegra_vi_graph_entity *
  57to_tegra_vi_graph_entity(struct v4l2_async_subdev *asd)
  58{
  59        return container_of(asd, struct tegra_vi_graph_entity, asd);
  60}
  61
  62static int tegra_get_format_idx_by_code(struct tegra_vi *vi,
  63                                        unsigned int code,
  64                                        unsigned int offset)
  65{
  66        unsigned int i;
  67
  68        for (i = offset; i < vi->soc->nformats; ++i) {
  69                if (vi->soc->video_formats[i].code == code)
  70                        return i;
  71        }
  72
  73        return -1;
  74}
  75
  76static u32 tegra_get_format_fourcc_by_idx(struct tegra_vi *vi,
  77                                          unsigned int index)
  78{
  79        if (index >= vi->soc->nformats)
  80                return -EINVAL;
  81
  82        return vi->soc->video_formats[index].fourcc;
  83}
  84
  85static const struct tegra_video_format *
  86tegra_get_format_by_fourcc(struct tegra_vi *vi, u32 fourcc)
  87{
  88        unsigned int i;
  89
  90        for (i = 0; i < vi->soc->nformats; ++i) {
  91                if (vi->soc->video_formats[i].fourcc == fourcc)
  92                        return &vi->soc->video_formats[i];
  93        }
  94
  95        return NULL;
  96}
  97
  98/*
  99 * videobuf2 queue operations
 100 */
 101static int tegra_channel_queue_setup(struct vb2_queue *vq,
 102                                     unsigned int *nbuffers,
 103                                     unsigned int *nplanes,
 104                                     unsigned int sizes[],
 105                                     struct device *alloc_devs[])
 106{
 107        struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
 108
 109        if (*nplanes)
 110                return sizes[0] < chan->format.sizeimage ? -EINVAL : 0;
 111
 112        *nplanes = 1;
 113        sizes[0] = chan->format.sizeimage;
 114        alloc_devs[0] = chan->vi->dev;
 115
 116        return 0;
 117}
 118
 119static int tegra_channel_buffer_prepare(struct vb2_buffer *vb)
 120{
 121        struct tegra_vi_channel *chan = vb2_get_drv_priv(vb->vb2_queue);
 122        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 123        struct tegra_channel_buffer *buf = to_tegra_channel_buffer(vbuf);
 124        unsigned long size = chan->format.sizeimage;
 125
 126        if (vb2_plane_size(vb, 0) < size) {
 127                v4l2_err(chan->video.v4l2_dev,
 128                         "buffer too small (%lu < %lu)\n",
 129                         vb2_plane_size(vb, 0), size);
 130                return -EINVAL;
 131        }
 132
 133        vb2_set_plane_payload(vb, 0, size);
 134        buf->chan = chan;
 135        buf->addr = vb2_dma_contig_plane_dma_addr(vb, 0);
 136
 137        return 0;
 138}
 139
 140static void tegra_channel_buffer_queue(struct vb2_buffer *vb)
 141{
 142        struct tegra_vi_channel *chan = vb2_get_drv_priv(vb->vb2_queue);
 143        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 144        struct tegra_channel_buffer *buf = to_tegra_channel_buffer(vbuf);
 145
 146        /* put buffer into the capture queue */
 147        spin_lock(&chan->start_lock);
 148        list_add_tail(&buf->queue, &chan->capture);
 149        spin_unlock(&chan->start_lock);
 150
 151        /* wait up kthread for capture */
 152        wake_up_interruptible(&chan->start_wait);
 153}
 154
 155struct v4l2_subdev *
 156tegra_channel_get_remote_csi_subdev(struct tegra_vi_channel *chan)
 157{
 158        struct media_pad *pad;
 159
 160        pad = media_entity_remote_pad(&chan->pad);
 161        if (!pad)
 162                return NULL;
 163
 164        return media_entity_to_v4l2_subdev(pad->entity);
 165}
 166
 167struct v4l2_subdev *
 168tegra_channel_get_remote_source_subdev(struct tegra_vi_channel *chan)
 169{
 170        struct media_pad *pad;
 171        struct v4l2_subdev *subdev;
 172        struct media_entity *entity;
 173
 174        subdev = tegra_channel_get_remote_csi_subdev(chan);
 175        if (!subdev)
 176                return NULL;
 177
 178        pad = &subdev->entity.pads[0];
 179        while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
 180                pad = media_entity_remote_pad(pad);
 181                if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
 182                        break;
 183                entity = pad->entity;
 184                pad = &entity->pads[0];
 185                subdev = media_entity_to_v4l2_subdev(entity);
 186        }
 187
 188        return subdev;
 189}
 190
 191static int tegra_channel_enable_stream(struct tegra_vi_channel *chan)
 192{
 193        struct v4l2_subdev *csi_subdev, *src_subdev;
 194        struct tegra_csi_channel *csi_chan;
 195        int ret, err;
 196
 197        /*
 198         * Tegra CSI receiver can detect the first LP to HS transition.
 199         * So, start the CSI stream-on prior to sensor stream-on and
 200         * vice-versa for stream-off.
 201         */
 202        csi_subdev = tegra_channel_get_remote_csi_subdev(chan);
 203        ret = v4l2_subdev_call(csi_subdev, video, s_stream, true);
 204        if (ret < 0 && ret != -ENOIOCTLCMD)
 205                return ret;
 206
 207        if (IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
 208                return 0;
 209
 210        csi_chan = v4l2_get_subdevdata(csi_subdev);
 211        /*
 212         * TRM has incorrectly documented to wait for done status from
 213         * calibration logic after CSI interface power on.
 214         * As per the design, calibration results are latched and applied
 215         * to the pads only when the link is in LP11 state which will happen
 216         * during the sensor stream-on.
 217         * CSI subdev stream-on triggers start of MIPI pads calibration.
 218         * Wait for calibration to finish here after sensor subdev stream-on.
 219         */
 220        src_subdev = tegra_channel_get_remote_source_subdev(chan);
 221        ret = v4l2_subdev_call(src_subdev, video, s_stream, true);
 222        err = tegra_mipi_finish_calibration(csi_chan->mipi);
 223
 224        if (ret < 0 && ret != -ENOIOCTLCMD)
 225                goto err_disable_csi_stream;
 226
 227        if (err < 0)
 228                dev_warn(csi_chan->csi->dev,
 229                         "MIPI calibration failed: %d\n", err);
 230
 231        return 0;
 232
 233err_disable_csi_stream:
 234        v4l2_subdev_call(csi_subdev, video, s_stream, false);
 235        return ret;
 236}
 237
 238static int tegra_channel_disable_stream(struct tegra_vi_channel *chan)
 239{
 240        struct v4l2_subdev *subdev;
 241        int ret;
 242
 243        /*
 244         * Stream-off subdevices in reverse order to stream-on.
 245         * Remote source subdev in TPG mode is same as CSI subdev.
 246         */
 247        subdev = tegra_channel_get_remote_source_subdev(chan);
 248        ret = v4l2_subdev_call(subdev, video, s_stream, false);
 249        if (ret < 0 && ret != -ENOIOCTLCMD)
 250                return ret;
 251
 252        if (IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
 253                return 0;
 254
 255        subdev = tegra_channel_get_remote_csi_subdev(chan);
 256        ret = v4l2_subdev_call(subdev, video, s_stream, false);
 257        if (ret < 0 && ret != -ENOIOCTLCMD)
 258                return ret;
 259
 260        return 0;
 261}
 262
 263int tegra_channel_set_stream(struct tegra_vi_channel *chan, bool on)
 264{
 265        int ret;
 266
 267        if (on)
 268                ret = tegra_channel_enable_stream(chan);
 269        else
 270                ret = tegra_channel_disable_stream(chan);
 271
 272        return ret;
 273}
 274
 275void tegra_channel_release_buffers(struct tegra_vi_channel *chan,
 276                                   enum vb2_buffer_state state)
 277{
 278        struct tegra_channel_buffer *buf, *nbuf;
 279
 280        spin_lock(&chan->start_lock);
 281        list_for_each_entry_safe(buf, nbuf, &chan->capture, queue) {
 282                vb2_buffer_done(&buf->buf.vb2_buf, state);
 283                list_del(&buf->queue);
 284        }
 285        spin_unlock(&chan->start_lock);
 286
 287        spin_lock(&chan->done_lock);
 288        list_for_each_entry_safe(buf, nbuf, &chan->done, queue) {
 289                vb2_buffer_done(&buf->buf.vb2_buf, state);
 290                list_del(&buf->queue);
 291        }
 292        spin_unlock(&chan->done_lock);
 293}
 294
 295static int tegra_channel_start_streaming(struct vb2_queue *vq, u32 count)
 296{
 297        struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
 298        int ret;
 299
 300        ret = pm_runtime_resume_and_get(chan->vi->dev);
 301        if (ret < 0) {
 302                dev_err(chan->vi->dev, "failed to get runtime PM: %d\n", ret);
 303                return ret;
 304        }
 305
 306        ret = chan->vi->ops->vi_start_streaming(vq, count);
 307        if (ret < 0)
 308                pm_runtime_put(chan->vi->dev);
 309
 310        return ret;
 311}
 312
 313static void tegra_channel_stop_streaming(struct vb2_queue *vq)
 314{
 315        struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
 316
 317        chan->vi->ops->vi_stop_streaming(vq);
 318        pm_runtime_put(chan->vi->dev);
 319}
 320
 321static const struct vb2_ops tegra_channel_queue_qops = {
 322        .queue_setup = tegra_channel_queue_setup,
 323        .buf_prepare = tegra_channel_buffer_prepare,
 324        .buf_queue = tegra_channel_buffer_queue,
 325        .wait_prepare = vb2_ops_wait_prepare,
 326        .wait_finish = vb2_ops_wait_finish,
 327        .start_streaming = tegra_channel_start_streaming,
 328        .stop_streaming = tegra_channel_stop_streaming,
 329};
 330
 331/*
 332 * V4L2 ioctl operations
 333 */
 334static int tegra_channel_querycap(struct file *file, void *fh,
 335                                  struct v4l2_capability *cap)
 336{
 337        struct tegra_vi_channel *chan = video_drvdata(file);
 338
 339        strscpy(cap->driver, "tegra-video", sizeof(cap->driver));
 340        strscpy(cap->card, chan->video.name, sizeof(cap->card));
 341        snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
 342                 dev_name(chan->vi->dev));
 343
 344        return 0;
 345}
 346
 347static int tegra_channel_g_parm(struct file *file, void *fh,
 348                                struct v4l2_streamparm *a)
 349{
 350        struct tegra_vi_channel *chan = video_drvdata(file);
 351        struct v4l2_subdev *subdev;
 352
 353        subdev = tegra_channel_get_remote_source_subdev(chan);
 354        return v4l2_g_parm_cap(&chan->video, subdev, a);
 355}
 356
 357static int tegra_channel_s_parm(struct file *file, void *fh,
 358                                struct v4l2_streamparm *a)
 359{
 360        struct tegra_vi_channel *chan = video_drvdata(file);
 361        struct v4l2_subdev *subdev;
 362
 363        subdev = tegra_channel_get_remote_source_subdev(chan);
 364        return v4l2_s_parm_cap(&chan->video, subdev, a);
 365}
 366
 367static int tegra_channel_enum_framesizes(struct file *file, void *fh,
 368                                         struct v4l2_frmsizeenum *sizes)
 369{
 370        int ret;
 371        struct tegra_vi_channel *chan = video_drvdata(file);
 372        struct v4l2_subdev *subdev;
 373        const struct tegra_video_format *fmtinfo;
 374        struct v4l2_subdev_frame_size_enum fse = {
 375                .index = sizes->index,
 376                .which = V4L2_SUBDEV_FORMAT_ACTIVE,
 377        };
 378
 379        fmtinfo = tegra_get_format_by_fourcc(chan->vi, sizes->pixel_format);
 380        if (!fmtinfo)
 381                return -EINVAL;
 382
 383        fse.code = fmtinfo->code;
 384
 385        subdev = tegra_channel_get_remote_source_subdev(chan);
 386        ret = v4l2_subdev_call(subdev, pad, enum_frame_size, NULL, &fse);
 387        if (ret)
 388                return ret;
 389
 390        sizes->type = V4L2_FRMSIZE_TYPE_DISCRETE;
 391        sizes->discrete.width = fse.max_width;
 392        sizes->discrete.height = fse.max_height;
 393
 394        return 0;
 395}
 396
 397static int tegra_channel_enum_frameintervals(struct file *file, void *fh,
 398                                             struct v4l2_frmivalenum *ivals)
 399{
 400        int ret;
 401        struct tegra_vi_channel *chan = video_drvdata(file);
 402        struct v4l2_subdev *subdev;
 403        const struct tegra_video_format *fmtinfo;
 404        struct v4l2_subdev_frame_interval_enum fie = {
 405                .index = ivals->index,
 406                .width = ivals->width,
 407                .height = ivals->height,
 408                .which = V4L2_SUBDEV_FORMAT_ACTIVE,
 409        };
 410
 411        fmtinfo = tegra_get_format_by_fourcc(chan->vi, ivals->pixel_format);
 412        if (!fmtinfo)
 413                return -EINVAL;
 414
 415        fie.code = fmtinfo->code;
 416
 417        subdev = tegra_channel_get_remote_source_subdev(chan);
 418        ret = v4l2_subdev_call(subdev, pad, enum_frame_interval, NULL, &fie);
 419        if (ret)
 420                return ret;
 421
 422        ivals->type = V4L2_FRMIVAL_TYPE_DISCRETE;
 423        ivals->discrete.numerator = fie.interval.numerator;
 424        ivals->discrete.denominator = fie.interval.denominator;
 425
 426        return 0;
 427}
 428
 429static int tegra_channel_enum_format(struct file *file, void *fh,
 430                                     struct v4l2_fmtdesc *f)
 431{
 432        struct tegra_vi_channel *chan = video_drvdata(file);
 433        unsigned int index = 0, i;
 434        unsigned long *fmts_bitmap = chan->tpg_fmts_bitmap;
 435
 436        if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
 437                fmts_bitmap = chan->fmts_bitmap;
 438
 439        if (f->index >= bitmap_weight(fmts_bitmap, MAX_FORMAT_NUM))
 440                return -EINVAL;
 441
 442        for (i = 0; i < f->index + 1; i++, index++)
 443                index = find_next_bit(fmts_bitmap, MAX_FORMAT_NUM, index);
 444
 445        f->pixelformat = tegra_get_format_fourcc_by_idx(chan->vi, index - 1);
 446
 447        return 0;
 448}
 449
 450static int tegra_channel_get_format(struct file *file, void *fh,
 451                                    struct v4l2_format *format)
 452{
 453        struct tegra_vi_channel *chan = video_drvdata(file);
 454
 455        format->fmt.pix = chan->format;
 456
 457        return 0;
 458}
 459
 460static void tegra_channel_fmt_align(struct tegra_vi_channel *chan,
 461                                    struct v4l2_pix_format *pix,
 462                                    unsigned int bpp)
 463{
 464        unsigned int min_bpl;
 465        unsigned int max_bpl;
 466        unsigned int bpl;
 467
 468        /*
 469         * The transfer alignment requirements are expressed in bytes.
 470         * Clamp the requested width and height to the limits.
 471         */
 472        pix->width = clamp(pix->width, TEGRA_MIN_WIDTH, TEGRA_MAX_WIDTH);
 473        pix->height = clamp(pix->height, TEGRA_MIN_HEIGHT, TEGRA_MAX_HEIGHT);
 474
 475        /* Clamp the requested bytes per line value. If the maximum bytes per
 476         * line value is zero, the module doesn't support user configurable
 477         * line sizes. Override the requested value with the minimum in that
 478         * case.
 479         */
 480        min_bpl = pix->width * bpp;
 481        max_bpl = rounddown(TEGRA_MAX_WIDTH, SURFACE_ALIGN_BYTES);
 482        bpl = roundup(pix->bytesperline, SURFACE_ALIGN_BYTES);
 483
 484        pix->bytesperline = clamp(bpl, min_bpl, max_bpl);
 485        pix->sizeimage = pix->bytesperline * pix->height;
 486        if (pix->pixelformat == V4L2_PIX_FMT_NV16)
 487                pix->sizeimage *= 2;
 488}
 489
 490static int __tegra_channel_try_format(struct tegra_vi_channel *chan,
 491                                      struct v4l2_pix_format *pix)
 492{
 493        const struct tegra_video_format *fmtinfo;
 494        struct v4l2_subdev *subdev;
 495        struct v4l2_subdev_format fmt;
 496        struct v4l2_subdev_state *sd_state;
 497        struct v4l2_subdev_frame_size_enum fse = {
 498                .which = V4L2_SUBDEV_FORMAT_TRY,
 499        };
 500        struct v4l2_subdev_selection sdsel = {
 501                .which = V4L2_SUBDEV_FORMAT_ACTIVE,
 502                .target = V4L2_SEL_TGT_CROP_BOUNDS,
 503        };
 504        int ret;
 505
 506        subdev = tegra_channel_get_remote_source_subdev(chan);
 507        if (!subdev)
 508                return -ENODEV;
 509
 510        sd_state = v4l2_subdev_alloc_state(subdev);
 511        if (IS_ERR(sd_state))
 512                return PTR_ERR(sd_state);
 513        /*
 514         * Retrieve the format information and if requested format isn't
 515         * supported, keep the current format.
 516         */
 517        fmtinfo = tegra_get_format_by_fourcc(chan->vi, pix->pixelformat);
 518        if (!fmtinfo) {
 519                pix->pixelformat = chan->format.pixelformat;
 520                pix->colorspace = chan->format.colorspace;
 521                fmtinfo = tegra_get_format_by_fourcc(chan->vi,
 522                                                     pix->pixelformat);
 523        }
 524
 525        pix->field = V4L2_FIELD_NONE;
 526        fmt.which = V4L2_SUBDEV_FORMAT_TRY;
 527        fmt.pad = 0;
 528        v4l2_fill_mbus_format(&fmt.format, pix, fmtinfo->code);
 529
 530        /*
 531         * Attempt to obtain the format size from subdev.
 532         * If not available, try to get crop boundary from subdev.
 533         */
 534        fse.code = fmtinfo->code;
 535        ret = v4l2_subdev_call(subdev, pad, enum_frame_size, sd_state, &fse);
 536        if (ret) {
 537                if (!v4l2_subdev_has_op(subdev, pad, get_selection)) {
 538                        sd_state->pads->try_crop.width = 0;
 539                        sd_state->pads->try_crop.height = 0;
 540                } else {
 541                        ret = v4l2_subdev_call(subdev, pad, get_selection,
 542                                               NULL, &sdsel);
 543                        if (ret)
 544                                return -EINVAL;
 545
 546                        sd_state->pads->try_crop.width = sdsel.r.width;
 547                        sd_state->pads->try_crop.height = sdsel.r.height;
 548                }
 549        } else {
 550                sd_state->pads->try_crop.width = fse.max_width;
 551                sd_state->pads->try_crop.height = fse.max_height;
 552        }
 553
 554        ret = v4l2_subdev_call(subdev, pad, set_fmt, sd_state, &fmt);
 555        if (ret < 0)
 556                return ret;
 557
 558        v4l2_fill_pix_format(pix, &fmt.format);
 559        tegra_channel_fmt_align(chan, pix, fmtinfo->bpp);
 560
 561        v4l2_subdev_free_state(sd_state);
 562
 563        return 0;
 564}
 565
 566static int tegra_channel_try_format(struct file *file, void *fh,
 567                                    struct v4l2_format *format)
 568{
 569        struct tegra_vi_channel *chan = video_drvdata(file);
 570
 571        return __tegra_channel_try_format(chan, &format->fmt.pix);
 572}
 573
 574static void tegra_channel_update_gangports(struct tegra_vi_channel *chan)
 575{
 576        if (chan->format.width <= 1920)
 577                chan->numgangports = 1;
 578        else
 579                chan->numgangports = chan->totalports;
 580}
 581
 582static int tegra_channel_set_format(struct file *file, void *fh,
 583                                    struct v4l2_format *format)
 584{
 585        struct tegra_vi_channel *chan = video_drvdata(file);
 586        const struct tegra_video_format *fmtinfo;
 587        struct v4l2_subdev_format fmt;
 588        struct v4l2_subdev *subdev;
 589        struct v4l2_pix_format *pix = &format->fmt.pix;
 590        int ret;
 591
 592        if (vb2_is_busy(&chan->queue))
 593                return -EBUSY;
 594
 595        /* get supported format by try_fmt */
 596        ret = __tegra_channel_try_format(chan, pix);
 597        if (ret)
 598                return ret;
 599
 600        fmtinfo = tegra_get_format_by_fourcc(chan->vi, pix->pixelformat);
 601
 602        fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
 603        fmt.pad = 0;
 604        v4l2_fill_mbus_format(&fmt.format, pix, fmtinfo->code);
 605        subdev = tegra_channel_get_remote_source_subdev(chan);
 606        ret = v4l2_subdev_call(subdev, pad, set_fmt, NULL, &fmt);
 607        if (ret < 0)
 608                return ret;
 609
 610        v4l2_fill_pix_format(pix, &fmt.format);
 611        tegra_channel_fmt_align(chan, pix, fmtinfo->bpp);
 612
 613        chan->format = *pix;
 614        chan->fmtinfo = fmtinfo;
 615        tegra_channel_update_gangports(chan);
 616
 617        return 0;
 618}
 619
 620static int tegra_channel_set_subdev_active_fmt(struct tegra_vi_channel *chan)
 621{
 622        int ret, index;
 623        struct v4l2_subdev *subdev;
 624        struct v4l2_subdev_format fmt = {
 625                .which = V4L2_SUBDEV_FORMAT_ACTIVE,
 626        };
 627
 628        /*
 629         * Initialize channel format to the sub-device active format if there
 630         * is corresponding match in the Tegra supported video formats.
 631         */
 632        subdev = tegra_channel_get_remote_source_subdev(chan);
 633        ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
 634        if (ret)
 635                return ret;
 636
 637        index = tegra_get_format_idx_by_code(chan->vi, fmt.format.code, 0);
 638        if (index < 0)
 639                return -EINVAL;
 640
 641        chan->fmtinfo = &chan->vi->soc->video_formats[index];
 642        v4l2_fill_pix_format(&chan->format, &fmt.format);
 643        chan->format.pixelformat = chan->fmtinfo->fourcc;
 644        chan->format.bytesperline = chan->format.width * chan->fmtinfo->bpp;
 645        chan->format.sizeimage = chan->format.bytesperline *
 646                                 chan->format.height;
 647        tegra_channel_fmt_align(chan, &chan->format, chan->fmtinfo->bpp);
 648        tegra_channel_update_gangports(chan);
 649
 650        return 0;
 651}
 652
 653static int
 654tegra_channel_subscribe_event(struct v4l2_fh *fh,
 655                              const struct v4l2_event_subscription *sub)
 656{
 657        switch (sub->type) {
 658        case V4L2_EVENT_SOURCE_CHANGE:
 659                return v4l2_event_subscribe(fh, sub, 4, NULL);
 660        }
 661
 662        return v4l2_ctrl_subscribe_event(fh, sub);
 663}
 664
 665static int tegra_channel_g_selection(struct file *file, void *priv,
 666                                     struct v4l2_selection *sel)
 667{
 668        struct tegra_vi_channel *chan = video_drvdata(file);
 669        struct v4l2_subdev *subdev;
 670        struct v4l2_subdev_format fmt = {
 671                .which = V4L2_SUBDEV_FORMAT_ACTIVE,
 672        };
 673        struct v4l2_subdev_selection sdsel = {
 674                .which = V4L2_SUBDEV_FORMAT_ACTIVE,
 675                .target = sel->target,
 676        };
 677        int ret;
 678
 679        subdev = tegra_channel_get_remote_source_subdev(chan);
 680        if (!v4l2_subdev_has_op(subdev, pad, get_selection))
 681                return -ENOTTY;
 682
 683        if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 684                return -EINVAL;
 685        /*
 686         * Try the get selection operation and fallback to get format if not
 687         * implemented.
 688         */
 689        ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
 690        if (!ret)
 691                sel->r = sdsel.r;
 692        if (ret != -ENOIOCTLCMD)
 693                return ret;
 694
 695        ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
 696        if (ret < 0)
 697                return ret;
 698
 699        sel->r.left = 0;
 700        sel->r.top = 0;
 701        sel->r.width = fmt.format.width;
 702        sel->r.height = fmt.format.height;
 703
 704        return 0;
 705}
 706
 707static int tegra_channel_s_selection(struct file *file, void *fh,
 708                                     struct v4l2_selection *sel)
 709{
 710        struct tegra_vi_channel *chan = video_drvdata(file);
 711        struct v4l2_subdev *subdev;
 712        int ret;
 713        struct v4l2_subdev_selection sdsel = {
 714                .which = V4L2_SUBDEV_FORMAT_ACTIVE,
 715                .target = sel->target,
 716                .flags = sel->flags,
 717                .r = sel->r,
 718        };
 719
 720        subdev = tegra_channel_get_remote_source_subdev(chan);
 721        if (!v4l2_subdev_has_op(subdev, pad, set_selection))
 722                return -ENOTTY;
 723
 724        if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 725                return -EINVAL;
 726
 727        if (vb2_is_busy(&chan->queue))
 728                return -EBUSY;
 729
 730        ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel);
 731        if (!ret) {
 732                sel->r = sdsel.r;
 733                /*
 734                 * Subdev active format resolution may have changed during
 735                 * set selection operation. So, update channel format to
 736                 * the sub-device active format.
 737                 */
 738                return tegra_channel_set_subdev_active_fmt(chan);
 739        }
 740
 741        return ret;
 742}
 743
 744static int tegra_channel_g_edid(struct file *file, void *fh,
 745                                struct v4l2_edid *edid)
 746{
 747        struct tegra_vi_channel *chan = video_drvdata(file);
 748        struct v4l2_subdev *subdev;
 749
 750        subdev = tegra_channel_get_remote_source_subdev(chan);
 751        if (!v4l2_subdev_has_op(subdev, pad, get_edid))
 752                return -ENOTTY;
 753
 754        return v4l2_subdev_call(subdev, pad, get_edid, edid);
 755}
 756
 757static int tegra_channel_s_edid(struct file *file, void *fh,
 758                                struct v4l2_edid *edid)
 759{
 760        struct tegra_vi_channel *chan = video_drvdata(file);
 761        struct v4l2_subdev *subdev;
 762
 763        subdev = tegra_channel_get_remote_source_subdev(chan);
 764        if (!v4l2_subdev_has_op(subdev, pad, set_edid))
 765                return -ENOTTY;
 766
 767        return v4l2_subdev_call(subdev, pad, set_edid, edid);
 768}
 769
 770static int tegra_channel_g_dv_timings(struct file *file, void *fh,
 771                                      struct v4l2_dv_timings *timings)
 772{
 773        struct tegra_vi_channel *chan = video_drvdata(file);
 774        struct v4l2_subdev *subdev;
 775
 776        subdev = tegra_channel_get_remote_source_subdev(chan);
 777        if (!v4l2_subdev_has_op(subdev, video, g_dv_timings))
 778                return -ENOTTY;
 779
 780        return v4l2_device_call_until_err(chan->video.v4l2_dev, 0,
 781                                          video, g_dv_timings, timings);
 782}
 783
 784static int tegra_channel_s_dv_timings(struct file *file, void *fh,
 785                                      struct v4l2_dv_timings *timings)
 786{
 787        struct tegra_vi_channel *chan = video_drvdata(file);
 788        struct v4l2_subdev *subdev;
 789        struct v4l2_bt_timings *bt = &timings->bt;
 790        struct v4l2_dv_timings curr_timings;
 791        int ret;
 792
 793        subdev = tegra_channel_get_remote_source_subdev(chan);
 794        if (!v4l2_subdev_has_op(subdev, video, s_dv_timings))
 795                return -ENOTTY;
 796
 797        ret = tegra_channel_g_dv_timings(file, fh, &curr_timings);
 798        if (ret)
 799                return ret;
 800
 801        if (v4l2_match_dv_timings(timings, &curr_timings, 0, false))
 802                return 0;
 803
 804        if (vb2_is_busy(&chan->queue))
 805                return -EBUSY;
 806
 807        ret = v4l2_device_call_until_err(chan->video.v4l2_dev, 0,
 808                                         video, s_dv_timings, timings);
 809        if (ret)
 810                return ret;
 811
 812        chan->format.width = bt->width;
 813        chan->format.height = bt->height;
 814        chan->format.bytesperline = bt->width * chan->fmtinfo->bpp;
 815        chan->format.sizeimage = chan->format.bytesperline * bt->height;
 816        tegra_channel_fmt_align(chan, &chan->format, chan->fmtinfo->bpp);
 817        tegra_channel_update_gangports(chan);
 818
 819        return 0;
 820}
 821
 822static int tegra_channel_query_dv_timings(struct file *file, void *fh,
 823                                          struct v4l2_dv_timings *timings)
 824{
 825        struct tegra_vi_channel *chan = video_drvdata(file);
 826        struct v4l2_subdev *subdev;
 827
 828        subdev = tegra_channel_get_remote_source_subdev(chan);
 829        if (!v4l2_subdev_has_op(subdev, video, query_dv_timings))
 830                return -ENOTTY;
 831
 832        return v4l2_device_call_until_err(chan->video.v4l2_dev, 0,
 833                                          video, query_dv_timings, timings);
 834}
 835
 836static int tegra_channel_enum_dv_timings(struct file *file, void *fh,
 837                                         struct v4l2_enum_dv_timings *timings)
 838{
 839        struct tegra_vi_channel *chan = video_drvdata(file);
 840        struct v4l2_subdev *subdev;
 841
 842        subdev = tegra_channel_get_remote_source_subdev(chan);
 843        if (!v4l2_subdev_has_op(subdev, pad, enum_dv_timings))
 844                return -ENOTTY;
 845
 846        return v4l2_subdev_call(subdev, pad, enum_dv_timings, timings);
 847}
 848
 849static int tegra_channel_dv_timings_cap(struct file *file, void *fh,
 850                                        struct v4l2_dv_timings_cap *cap)
 851{
 852        struct tegra_vi_channel *chan = video_drvdata(file);
 853        struct v4l2_subdev *subdev;
 854
 855        subdev = tegra_channel_get_remote_source_subdev(chan);
 856        if (!v4l2_subdev_has_op(subdev, pad, dv_timings_cap))
 857                return -ENOTTY;
 858
 859        return v4l2_subdev_call(subdev, pad, dv_timings_cap, cap);
 860}
 861
 862static int tegra_channel_log_status(struct file *file, void *fh)
 863{
 864        struct tegra_vi_channel *chan = video_drvdata(file);
 865
 866        v4l2_device_call_all(chan->video.v4l2_dev, 0, core, log_status);
 867
 868        return 0;
 869}
 870
 871static int tegra_channel_enum_input(struct file *file, void *fh,
 872                                    struct v4l2_input *inp)
 873{
 874        struct tegra_vi_channel *chan = video_drvdata(file);
 875        struct v4l2_subdev *subdev;
 876
 877        if (inp->index)
 878                return -EINVAL;
 879
 880        inp->type = V4L2_INPUT_TYPE_CAMERA;
 881        subdev = tegra_channel_get_remote_source_subdev(chan);
 882        strscpy(inp->name, subdev->name, sizeof(inp->name));
 883        if (v4l2_subdev_has_op(subdev, pad, dv_timings_cap))
 884                inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
 885
 886        return 0;
 887}
 888
 889static int tegra_channel_g_input(struct file *file, void *priv,
 890                                 unsigned int *i)
 891{
 892        *i = 0;
 893
 894        return 0;
 895}
 896
 897static int tegra_channel_s_input(struct file *file, void *priv,
 898                                 unsigned int input)
 899{
 900        if (input > 0)
 901                return -EINVAL;
 902
 903        return 0;
 904}
 905
 906static const struct v4l2_ioctl_ops tegra_channel_ioctl_ops = {
 907        .vidioc_querycap                = tegra_channel_querycap,
 908        .vidioc_g_parm                  = tegra_channel_g_parm,
 909        .vidioc_s_parm                  = tegra_channel_s_parm,
 910        .vidioc_enum_framesizes         = tegra_channel_enum_framesizes,
 911        .vidioc_enum_frameintervals     = tegra_channel_enum_frameintervals,
 912        .vidioc_enum_fmt_vid_cap        = tegra_channel_enum_format,
 913        .vidioc_g_fmt_vid_cap           = tegra_channel_get_format,
 914        .vidioc_s_fmt_vid_cap           = tegra_channel_set_format,
 915        .vidioc_try_fmt_vid_cap         = tegra_channel_try_format,
 916        .vidioc_enum_input              = tegra_channel_enum_input,
 917        .vidioc_g_input                 = tegra_channel_g_input,
 918        .vidioc_s_input                 = tegra_channel_s_input,
 919        .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
 920        .vidioc_prepare_buf             = vb2_ioctl_prepare_buf,
 921        .vidioc_querybuf                = vb2_ioctl_querybuf,
 922        .vidioc_qbuf                    = vb2_ioctl_qbuf,
 923        .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
 924        .vidioc_create_bufs             = vb2_ioctl_create_bufs,
 925        .vidioc_expbuf                  = vb2_ioctl_expbuf,
 926        .vidioc_streamon                = vb2_ioctl_streamon,
 927        .vidioc_streamoff               = vb2_ioctl_streamoff,
 928        .vidioc_subscribe_event         = tegra_channel_subscribe_event,
 929        .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
 930        .vidioc_g_selection             = tegra_channel_g_selection,
 931        .vidioc_s_selection             = tegra_channel_s_selection,
 932        .vidioc_g_edid                  = tegra_channel_g_edid,
 933        .vidioc_s_edid                  = tegra_channel_s_edid,
 934        .vidioc_g_dv_timings            = tegra_channel_g_dv_timings,
 935        .vidioc_s_dv_timings            = tegra_channel_s_dv_timings,
 936        .vidioc_query_dv_timings        = tegra_channel_query_dv_timings,
 937        .vidioc_enum_dv_timings         = tegra_channel_enum_dv_timings,
 938        .vidioc_dv_timings_cap          = tegra_channel_dv_timings_cap,
 939        .vidioc_log_status              = tegra_channel_log_status,
 940};
 941
 942/*
 943 * V4L2 file operations
 944 */
 945static const struct v4l2_file_operations tegra_channel_fops = {
 946        .owner          = THIS_MODULE,
 947        .unlocked_ioctl = video_ioctl2,
 948        .open           = v4l2_fh_open,
 949        .release        = vb2_fop_release,
 950        .read           = vb2_fop_read,
 951        .poll           = vb2_fop_poll,
 952        .mmap           = vb2_fop_mmap,
 953};
 954
 955/*
 956 * V4L2 control operations
 957 */
 958static int vi_s_ctrl(struct v4l2_ctrl *ctrl)
 959{
 960        struct tegra_vi_channel *chan = container_of(ctrl->handler,
 961                                                     struct tegra_vi_channel,
 962                                                     ctrl_handler);
 963
 964        switch (ctrl->id) {
 965        case V4L2_CID_TEST_PATTERN:
 966                /* pattern change takes effect on next stream */
 967                chan->pg_mode = ctrl->val + 1;
 968                break;
 969        case V4L2_CID_TEGRA_SYNCPT_TIMEOUT_RETRY:
 970                chan->syncpt_timeout_retry = ctrl->val;
 971                break;
 972        default:
 973                return -EINVAL;
 974        }
 975
 976        return 0;
 977}
 978
 979static const struct v4l2_ctrl_ops vi_ctrl_ops = {
 980        .s_ctrl = vi_s_ctrl,
 981};
 982
 983#if IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)
 984static const char *const vi_pattern_strings[] = {
 985        "Black/White Direct Mode",
 986        "Color Patch Mode",
 987};
 988#else
 989static const struct v4l2_ctrl_config syncpt_timeout_ctrl = {
 990        .ops = &vi_ctrl_ops,
 991        .id = V4L2_CID_TEGRA_SYNCPT_TIMEOUT_RETRY,
 992        .name = "Syncpt timeout retry",
 993        .type = V4L2_CTRL_TYPE_INTEGER,
 994        .min = 1,
 995        .max = 10000,
 996        .step = 1,
 997        .def = 5,
 998};
 999#endif
1000
1001static int tegra_channel_setup_ctrl_handler(struct tegra_vi_channel *chan)
1002{
1003        int ret;
1004
1005#if IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)
1006        /* add test pattern control handler to v4l2 device */
1007        v4l2_ctrl_new_std_menu_items(&chan->ctrl_handler, &vi_ctrl_ops,
1008                                     V4L2_CID_TEST_PATTERN,
1009                                     ARRAY_SIZE(vi_pattern_strings) - 1,
1010                                     0, 0, vi_pattern_strings);
1011        if (chan->ctrl_handler.error) {
1012                dev_err(chan->vi->dev, "failed to add TPG ctrl handler: %d\n",
1013                        chan->ctrl_handler.error);
1014                v4l2_ctrl_handler_free(&chan->ctrl_handler);
1015                return chan->ctrl_handler.error;
1016        }
1017#else
1018        struct v4l2_subdev *subdev;
1019
1020        /* custom control */
1021        v4l2_ctrl_new_custom(&chan->ctrl_handler, &syncpt_timeout_ctrl, NULL);
1022        if (chan->ctrl_handler.error) {
1023                dev_err(chan->vi->dev, "failed to add %s ctrl handler: %d\n",
1024                        syncpt_timeout_ctrl.name,
1025                        chan->ctrl_handler.error);
1026                v4l2_ctrl_handler_free(&chan->ctrl_handler);
1027                return chan->ctrl_handler.error;
1028        }
1029
1030        subdev = tegra_channel_get_remote_source_subdev(chan);
1031        if (!subdev)
1032                return -ENODEV;
1033
1034        ret = v4l2_ctrl_add_handler(&chan->ctrl_handler, subdev->ctrl_handler,
1035                                    NULL, true);
1036        if (ret < 0) {
1037                dev_err(chan->vi->dev,
1038                        "failed to add subdev %s ctrl handler: %d\n",
1039                        subdev->name, ret);
1040                v4l2_ctrl_handler_free(&chan->ctrl_handler);
1041                return ret;
1042        }
1043#endif
1044
1045        /* setup the controls */
1046        ret = v4l2_ctrl_handler_setup(&chan->ctrl_handler);
1047        if (ret < 0) {
1048                dev_err(chan->vi->dev,
1049                        "failed to setup v4l2 ctrl handler: %d\n", ret);
1050                return ret;
1051        }
1052
1053        return 0;
1054}
1055
1056/* VI only support 2 formats in TPG mode */
1057static void vi_tpg_fmts_bitmap_init(struct tegra_vi_channel *chan)
1058{
1059        int index;
1060
1061        bitmap_zero(chan->tpg_fmts_bitmap, MAX_FORMAT_NUM);
1062
1063        index = tegra_get_format_idx_by_code(chan->vi,
1064                                             MEDIA_BUS_FMT_SRGGB10_1X10, 0);
1065        bitmap_set(chan->tpg_fmts_bitmap, index, 1);
1066
1067        index = tegra_get_format_idx_by_code(chan->vi,
1068                                             MEDIA_BUS_FMT_RGB888_1X32_PADHI,
1069                                             0);
1070        bitmap_set(chan->tpg_fmts_bitmap, index, 1);
1071}
1072
1073static int vi_fmts_bitmap_init(struct tegra_vi_channel *chan)
1074{
1075        int index, ret, match_code = 0;
1076        struct v4l2_subdev *subdev;
1077        struct v4l2_subdev_mbus_code_enum code = {
1078                .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1079        };
1080
1081        bitmap_zero(chan->fmts_bitmap, MAX_FORMAT_NUM);
1082
1083        /*
1084         * Set the bitmap bits based on all the matched formats between the
1085         * available media bus formats of sub-device and the pre-defined Tegra
1086         * supported video formats.
1087         */
1088        subdev = tegra_channel_get_remote_source_subdev(chan);
1089        while (1) {
1090                ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
1091                                       NULL, &code);
1092                if (ret < 0)
1093                        break;
1094
1095                index = tegra_get_format_idx_by_code(chan->vi, code.code, 0);
1096                while (index >= 0) {
1097                        bitmap_set(chan->fmts_bitmap, index, 1);
1098                        if (!match_code)
1099                                match_code = code.code;
1100                        /* look for other formats with same mbus code */
1101                        index = tegra_get_format_idx_by_code(chan->vi,
1102                                                             code.code,
1103                                                             index + 1);
1104                }
1105
1106                code.index++;
1107        }
1108
1109        /*
1110         * Set the bitmap bit corresponding to default tegra video format if
1111         * there are no matched formats.
1112         */
1113        if (!match_code) {
1114                match_code = tegra_default_format.code;
1115                index = tegra_get_format_idx_by_code(chan->vi, match_code, 0);
1116                if (WARN_ON(index < 0))
1117                        return -EINVAL;
1118
1119                bitmap_set(chan->fmts_bitmap, index, 1);
1120        }
1121
1122        /* initialize channel format to the sub-device active format */
1123        tegra_channel_set_subdev_active_fmt(chan);
1124
1125        return 0;
1126}
1127
1128static void tegra_channel_host1x_syncpts_free(struct tegra_vi_channel *chan)
1129{
1130        int i;
1131
1132        for (i = 0; i < chan->numgangports; i++) {
1133                host1x_syncpt_put(chan->mw_ack_sp[i]);
1134                host1x_syncpt_put(chan->frame_start_sp[i]);
1135        }
1136}
1137
1138static void tegra_channel_cleanup(struct tegra_vi_channel *chan)
1139{
1140        v4l2_ctrl_handler_free(&chan->ctrl_handler);
1141        media_entity_cleanup(&chan->video.entity);
1142        tegra_channel_host1x_syncpts_free(chan);
1143        mutex_destroy(&chan->video_lock);
1144}
1145
1146void tegra_channels_cleanup(struct tegra_vi *vi)
1147{
1148        struct tegra_vi_channel *chan, *tmp;
1149
1150        if (!vi)
1151                return;
1152
1153        list_for_each_entry_safe(chan, tmp, &vi->vi_chans, list) {
1154                tegra_channel_cleanup(chan);
1155                list_del(&chan->list);
1156                kfree(chan);
1157        }
1158}
1159
1160static int tegra_channel_host1x_syncpt_init(struct tegra_vi_channel *chan)
1161{
1162        struct tegra_vi *vi = chan->vi;
1163        unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
1164        struct host1x_syncpt *fs_sp;
1165        struct host1x_syncpt *mw_sp;
1166        int ret, i;
1167
1168        for (i = 0; i < chan->numgangports; i++) {
1169                fs_sp = host1x_syncpt_request(&vi->client, flags);
1170                if (!fs_sp) {
1171                        dev_err(vi->dev, "failed to request frame start syncpoint\n");
1172                        ret = -ENOMEM;
1173                        goto free_syncpts;
1174                }
1175
1176                mw_sp = host1x_syncpt_request(&vi->client, flags);
1177                if (!mw_sp) {
1178                        dev_err(vi->dev, "failed to request memory ack syncpoint\n");
1179                        host1x_syncpt_put(fs_sp);
1180                        ret = -ENOMEM;
1181                        goto free_syncpts;
1182                }
1183
1184                chan->frame_start_sp[i] = fs_sp;
1185                chan->mw_ack_sp[i] = mw_sp;
1186                spin_lock_init(&chan->sp_incr_lock[i]);
1187        }
1188
1189        return 0;
1190
1191free_syncpts:
1192        tegra_channel_host1x_syncpts_free(chan);
1193        return ret;
1194}
1195
1196static int tegra_channel_init(struct tegra_vi_channel *chan)
1197{
1198        struct tegra_vi *vi = chan->vi;
1199        struct tegra_video_device *vid = dev_get_drvdata(vi->client.host);
1200        int ret;
1201
1202        mutex_init(&chan->video_lock);
1203        INIT_LIST_HEAD(&chan->capture);
1204        INIT_LIST_HEAD(&chan->done);
1205        spin_lock_init(&chan->start_lock);
1206        spin_lock_init(&chan->done_lock);
1207        init_waitqueue_head(&chan->start_wait);
1208        init_waitqueue_head(&chan->done_wait);
1209
1210        /* initialize the video format */
1211        chan->fmtinfo = &tegra_default_format;
1212        chan->format.pixelformat = chan->fmtinfo->fourcc;
1213        chan->format.colorspace = V4L2_COLORSPACE_SRGB;
1214        chan->format.field = V4L2_FIELD_NONE;
1215        chan->format.width = TEGRA_DEF_WIDTH;
1216        chan->format.height = TEGRA_DEF_HEIGHT;
1217        chan->format.bytesperline = TEGRA_DEF_WIDTH * chan->fmtinfo->bpp;
1218        chan->format.sizeimage = chan->format.bytesperline * TEGRA_DEF_HEIGHT;
1219        tegra_channel_fmt_align(chan, &chan->format, chan->fmtinfo->bpp);
1220
1221        ret = tegra_channel_host1x_syncpt_init(chan);
1222        if (ret)
1223                return ret;
1224
1225        /* initialize the media entity */
1226        chan->pad.flags = MEDIA_PAD_FL_SINK;
1227        ret = media_entity_pads_init(&chan->video.entity, 1, &chan->pad);
1228        if (ret < 0) {
1229                dev_err(vi->dev,
1230                        "failed to initialize media entity: %d\n", ret);
1231                goto free_syncpts;
1232        }
1233
1234        ret = v4l2_ctrl_handler_init(&chan->ctrl_handler, MAX_CID_CONTROLS);
1235        if (chan->ctrl_handler.error) {
1236                dev_err(vi->dev,
1237                        "failed to initialize v4l2 ctrl handler: %d\n", ret);
1238                goto cleanup_media;
1239        }
1240
1241        /* initialize the video_device */
1242        chan->video.fops = &tegra_channel_fops;
1243        chan->video.v4l2_dev = &vid->v4l2_dev;
1244        chan->video.release = video_device_release_empty;
1245        chan->video.queue = &chan->queue;
1246        snprintf(chan->video.name, sizeof(chan->video.name), "%s-%s-%u",
1247                 dev_name(vi->dev), "output", chan->portnos[0]);
1248        chan->video.vfl_type = VFL_TYPE_VIDEO;
1249        chan->video.vfl_dir = VFL_DIR_RX;
1250        chan->video.ioctl_ops = &tegra_channel_ioctl_ops;
1251        chan->video.ctrl_handler = &chan->ctrl_handler;
1252        chan->video.lock = &chan->video_lock;
1253        chan->video.device_caps = V4L2_CAP_VIDEO_CAPTURE |
1254                                  V4L2_CAP_STREAMING |
1255                                  V4L2_CAP_READWRITE;
1256        video_set_drvdata(&chan->video, chan);
1257
1258        chan->queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1259        chan->queue.io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
1260        chan->queue.lock = &chan->video_lock;
1261        chan->queue.drv_priv = chan;
1262        chan->queue.buf_struct_size = sizeof(struct tegra_channel_buffer);
1263        chan->queue.ops = &tegra_channel_queue_qops;
1264        chan->queue.mem_ops = &vb2_dma_contig_memops;
1265        chan->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1266        chan->queue.min_buffers_needed = 2;
1267        chan->queue.dev = vi->dev;
1268        ret = vb2_queue_init(&chan->queue);
1269        if (ret < 0) {
1270                dev_err(vi->dev, "failed to initialize vb2 queue: %d\n", ret);
1271                goto free_v4l2_ctrl_hdl;
1272        }
1273
1274        if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
1275                v4l2_async_notifier_init(&chan->notifier);
1276
1277        return 0;
1278
1279free_v4l2_ctrl_hdl:
1280        v4l2_ctrl_handler_free(&chan->ctrl_handler);
1281cleanup_media:
1282        media_entity_cleanup(&chan->video.entity);
1283free_syncpts:
1284        tegra_channel_host1x_syncpts_free(chan);
1285        return ret;
1286}
1287
1288static int tegra_vi_channel_alloc(struct tegra_vi *vi, unsigned int port_num,
1289                                  struct device_node *node, unsigned int lanes)
1290{
1291        struct tegra_vi_channel *chan;
1292        unsigned int i;
1293
1294        /*
1295         * Do not use devm_kzalloc as memory is freed immediately
1296         * when device instance is unbound but application might still
1297         * be holding the device node open. Channel memory allocated
1298         * with kzalloc is freed during video device release callback.
1299         */
1300        chan = kzalloc(sizeof(*chan), GFP_KERNEL);
1301        if (!chan)
1302                return -ENOMEM;
1303
1304        chan->vi = vi;
1305        chan->portnos[0] = port_num;
1306        /*
1307         * For data lanes more than maximum csi lanes per brick, multiple of
1308         * x4 ports are used simultaneously for capture.
1309         */
1310        if (lanes <= CSI_LANES_PER_BRICK)
1311                chan->totalports = 1;
1312        else
1313                chan->totalports = lanes / CSI_LANES_PER_BRICK;
1314        chan->numgangports = chan->totalports;
1315
1316        for (i = 1; i < chan->totalports; i++)
1317                chan->portnos[i] = chan->portnos[0] + i * CSI_PORTS_PER_BRICK;
1318
1319        chan->of_node = node;
1320        list_add_tail(&chan->list, &vi->vi_chans);
1321
1322        return 0;
1323}
1324
1325static int tegra_vi_tpg_channels_alloc(struct tegra_vi *vi)
1326{
1327        unsigned int port_num;
1328        unsigned int nchannels = vi->soc->vi_max_channels;
1329        int ret;
1330
1331        for (port_num = 0; port_num < nchannels; port_num++) {
1332                ret = tegra_vi_channel_alloc(vi, port_num,
1333                                             vi->dev->of_node, 2);
1334                if (ret < 0)
1335                        return ret;
1336        }
1337
1338        return 0;
1339}
1340
1341static int tegra_vi_channels_alloc(struct tegra_vi *vi)
1342{
1343        struct device_node *node = vi->dev->of_node;
1344        struct device_node *ep = NULL;
1345        struct device_node *ports;
1346        struct device_node *port;
1347        unsigned int port_num;
1348        struct device_node *parent;
1349        struct v4l2_fwnode_endpoint v4l2_ep = { .bus_type = 0 };
1350        unsigned int lanes;
1351        int ret = 0;
1352
1353        ports = of_get_child_by_name(node, "ports");
1354        if (!ports)
1355                return -ENODEV;
1356
1357        for_each_child_of_node(ports, port) {
1358                if (!of_node_name_eq(port, "port"))
1359                        continue;
1360
1361                ret = of_property_read_u32(port, "reg", &port_num);
1362                if (ret < 0)
1363                        continue;
1364
1365                if (port_num > vi->soc->vi_max_channels) {
1366                        dev_err(vi->dev, "invalid port num %d for %pOF\n",
1367                                port_num, port);
1368                        ret = -EINVAL;
1369                        of_node_put(port);
1370                        goto cleanup;
1371                }
1372
1373                ep = of_get_child_by_name(port, "endpoint");
1374                if (!ep)
1375                        continue;
1376
1377                parent = of_graph_get_remote_port_parent(ep);
1378                of_node_put(ep);
1379                if (!parent)
1380                        continue;
1381
1382                ep = of_graph_get_endpoint_by_regs(parent, 0, 0);
1383                of_node_put(parent);
1384                ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep),
1385                                                 &v4l2_ep);
1386                of_node_put(ep);
1387                if (ret)
1388                        continue;
1389
1390                lanes = v4l2_ep.bus.mipi_csi2.num_data_lanes;
1391                ret = tegra_vi_channel_alloc(vi, port_num, port, lanes);
1392                if (ret < 0) {
1393                        of_node_put(port);
1394                        goto cleanup;
1395                }
1396        }
1397
1398cleanup:
1399        of_node_put(ports);
1400        return ret;
1401}
1402
1403static int tegra_vi_channels_init(struct tegra_vi *vi)
1404{
1405        struct tegra_vi_channel *chan;
1406        int ret;
1407
1408        list_for_each_entry(chan, &vi->vi_chans, list) {
1409                ret = tegra_channel_init(chan);
1410                if (ret < 0) {
1411                        dev_err(vi->dev,
1412                                "failed to initialize channel-%d: %d\n",
1413                                chan->portnos[0], ret);
1414                        goto cleanup;
1415                }
1416        }
1417
1418        return 0;
1419
1420cleanup:
1421        list_for_each_entry_continue_reverse(chan, &vi->vi_chans, list)
1422                tegra_channel_cleanup(chan);
1423
1424        return ret;
1425}
1426
1427void tegra_v4l2_nodes_cleanup_tpg(struct tegra_video_device *vid)
1428{
1429        struct tegra_vi *vi = vid->vi;
1430        struct tegra_csi *csi = vid->csi;
1431        struct tegra_csi_channel *csi_chan;
1432        struct tegra_vi_channel *chan;
1433
1434        list_for_each_entry(chan, &vi->vi_chans, list)
1435                vb2_video_unregister_device(&chan->video);
1436
1437        list_for_each_entry(csi_chan, &csi->csi_chans, list)
1438                v4l2_device_unregister_subdev(&csi_chan->subdev);
1439}
1440
1441int tegra_v4l2_nodes_setup_tpg(struct tegra_video_device *vid)
1442{
1443        struct tegra_vi *vi = vid->vi;
1444        struct tegra_csi *csi = vid->csi;
1445        struct tegra_vi_channel *vi_chan;
1446        struct tegra_csi_channel *csi_chan;
1447        u32 link_flags = MEDIA_LNK_FL_ENABLED;
1448        int ret;
1449
1450        if (!vi || !csi)
1451                return -ENODEV;
1452
1453        csi_chan = list_first_entry(&csi->csi_chans,
1454                                    struct tegra_csi_channel, list);
1455
1456        list_for_each_entry(vi_chan, &vi->vi_chans, list) {
1457                struct media_entity *source = &csi_chan->subdev.entity;
1458                struct media_entity *sink = &vi_chan->video.entity;
1459                struct media_pad *source_pad = csi_chan->pads;
1460                struct media_pad *sink_pad = &vi_chan->pad;
1461
1462                ret = v4l2_device_register_subdev(&vid->v4l2_dev,
1463                                                  &csi_chan->subdev);
1464                if (ret) {
1465                        dev_err(vi->dev,
1466                                "failed to register subdev: %d\n", ret);
1467                        goto cleanup;
1468                }
1469
1470                ret = video_register_device(&vi_chan->video,
1471                                            VFL_TYPE_VIDEO, -1);
1472                if (ret < 0) {
1473                        dev_err(vi->dev,
1474                                "failed to register video device: %d\n", ret);
1475                        goto cleanup;
1476                }
1477
1478                dev_dbg(vi->dev, "creating %s:%u -> %s:%u link\n",
1479                        source->name, source_pad->index,
1480                        sink->name, sink_pad->index);
1481
1482                ret = media_create_pad_link(source, source_pad->index,
1483                                            sink, sink_pad->index,
1484                                            link_flags);
1485                if (ret < 0) {
1486                        dev_err(vi->dev,
1487                                "failed to create %s:%u -> %s:%u link: %d\n",
1488                                source->name, source_pad->index,
1489                                sink->name, sink_pad->index, ret);
1490                        goto cleanup;
1491                }
1492
1493                ret = tegra_channel_setup_ctrl_handler(vi_chan);
1494                if (ret < 0)
1495                        goto cleanup;
1496
1497                v4l2_set_subdev_hostdata(&csi_chan->subdev, vi_chan);
1498                vi_tpg_fmts_bitmap_init(vi_chan);
1499                csi_chan = list_next_entry(csi_chan, list);
1500        }
1501
1502        return 0;
1503
1504cleanup:
1505        tegra_v4l2_nodes_cleanup_tpg(vid);
1506        return ret;
1507}
1508
1509static int __maybe_unused vi_runtime_resume(struct device *dev)
1510{
1511        struct tegra_vi *vi = dev_get_drvdata(dev);
1512        int ret;
1513
1514        ret = regulator_enable(vi->vdd);
1515        if (ret) {
1516                dev_err(dev, "failed to enable VDD supply: %d\n", ret);
1517                return ret;
1518        }
1519
1520        ret = clk_set_rate(vi->clk, vi->soc->vi_max_clk_hz);
1521        if (ret) {
1522                dev_err(dev, "failed to set vi clock rate: %d\n", ret);
1523                goto disable_vdd;
1524        }
1525
1526        ret = clk_prepare_enable(vi->clk);
1527        if (ret) {
1528                dev_err(dev, "failed to enable vi clock: %d\n", ret);
1529                goto disable_vdd;
1530        }
1531
1532        return 0;
1533
1534disable_vdd:
1535        regulator_disable(vi->vdd);
1536        return ret;
1537}
1538
1539static int __maybe_unused vi_runtime_suspend(struct device *dev)
1540{
1541        struct tegra_vi *vi = dev_get_drvdata(dev);
1542
1543        clk_disable_unprepare(vi->clk);
1544
1545        regulator_disable(vi->vdd);
1546
1547        return 0;
1548}
1549
1550/*
1551 * Graph Management
1552 */
1553static struct tegra_vi_graph_entity *
1554tegra_vi_graph_find_entity(struct tegra_vi_channel *chan,
1555                           const struct fwnode_handle *fwnode)
1556{
1557        struct tegra_vi_graph_entity *entity;
1558        struct v4l2_async_subdev *asd;
1559
1560        list_for_each_entry(asd, &chan->notifier.asd_list, asd_list) {
1561                entity = to_tegra_vi_graph_entity(asd);
1562                if (entity->asd.match.fwnode == fwnode)
1563                        return entity;
1564        }
1565
1566        return NULL;
1567}
1568
1569static int tegra_vi_graph_build(struct tegra_vi_channel *chan,
1570                                struct tegra_vi_graph_entity *entity)
1571{
1572        struct tegra_vi *vi = chan->vi;
1573        struct tegra_vi_graph_entity *ent;
1574        struct fwnode_handle *ep = NULL;
1575        struct v4l2_fwnode_link link;
1576        struct media_entity *local = entity->entity;
1577        struct media_entity *remote;
1578        struct media_pad *local_pad;
1579        struct media_pad *remote_pad;
1580        u32 link_flags = MEDIA_LNK_FL_ENABLED;
1581        int ret = 0;
1582
1583        dev_dbg(vi->dev, "creating links for entity %s\n", local->name);
1584
1585        while (1) {
1586                ep = fwnode_graph_get_next_endpoint(entity->asd.match.fwnode,
1587                                                    ep);
1588                if (!ep)
1589                        break;
1590
1591                ret = v4l2_fwnode_parse_link(ep, &link);
1592                if (ret < 0) {
1593                        dev_err(vi->dev, "failed to parse link for %pOF: %d\n",
1594                                to_of_node(ep), ret);
1595                        continue;
1596                }
1597
1598                if (link.local_port >= local->num_pads) {
1599                        dev_err(vi->dev, "invalid port number %u on %pOF\n",
1600                                link.local_port, to_of_node(link.local_node));
1601                        v4l2_fwnode_put_link(&link);
1602                        ret = -EINVAL;
1603                        break;
1604                }
1605
1606                local_pad = &local->pads[link.local_port];
1607                /* Remote node is vi node. So use channel video entity and pad
1608                 * as remote/sink.
1609                 */
1610                if (link.remote_node == of_fwnode_handle(vi->dev->of_node)) {
1611                        remote = &chan->video.entity;
1612                        remote_pad = &chan->pad;
1613                        goto create_link;
1614                }
1615
1616                /*
1617                 * Skip sink ports, they will be processed from the other end
1618                 * of the link.
1619                 */
1620                if (local_pad->flags & MEDIA_PAD_FL_SINK) {
1621                        dev_dbg(vi->dev, "skipping sink port %pOF:%u\n",
1622                                to_of_node(link.local_node), link.local_port);
1623                        v4l2_fwnode_put_link(&link);
1624                        continue;
1625                }
1626
1627                /* find the remote entity from notifier list */
1628                ent = tegra_vi_graph_find_entity(chan, link.remote_node);
1629                if (!ent) {
1630                        dev_err(vi->dev, "no entity found for %pOF\n",
1631                                to_of_node(link.remote_node));
1632                        v4l2_fwnode_put_link(&link);
1633                        ret = -ENODEV;
1634                        break;
1635                }
1636
1637                remote = ent->entity;
1638                if (link.remote_port >= remote->num_pads) {
1639                        dev_err(vi->dev, "invalid port number %u on %pOF\n",
1640                                link.remote_port,
1641                                to_of_node(link.remote_node));
1642                        v4l2_fwnode_put_link(&link);
1643                        ret = -EINVAL;
1644                        break;
1645                }
1646
1647                remote_pad = &remote->pads[link.remote_port];
1648
1649create_link:
1650                dev_dbg(vi->dev, "creating %s:%u -> %s:%u link\n",
1651                        local->name, local_pad->index,
1652                        remote->name, remote_pad->index);
1653
1654                ret = media_create_pad_link(local, local_pad->index,
1655                                            remote, remote_pad->index,
1656                                            link_flags);
1657                v4l2_fwnode_put_link(&link);
1658                if (ret < 0) {
1659                        dev_err(vi->dev,
1660                                "failed to create %s:%u -> %s:%u link: %d\n",
1661                                local->name, local_pad->index,
1662                                remote->name, remote_pad->index, ret);
1663                        break;
1664                }
1665        }
1666
1667        fwnode_handle_put(ep);
1668        return ret;
1669}
1670
1671static int tegra_vi_graph_notify_complete(struct v4l2_async_notifier *notifier)
1672{
1673        struct tegra_vi_graph_entity *entity;
1674        struct v4l2_async_subdev *asd;
1675        struct v4l2_subdev *subdev;
1676        struct tegra_vi_channel *chan;
1677        struct tegra_vi *vi;
1678        int ret;
1679
1680        chan = container_of(notifier, struct tegra_vi_channel, notifier);
1681        vi = chan->vi;
1682
1683        dev_dbg(vi->dev, "notify complete, all subdevs registered\n");
1684
1685        /*
1686         * Video device node should be created at the end of all the device
1687         * related initialization/setup.
1688         * Current video_register_device() does both initialize and register
1689         * video device in same API.
1690         *
1691         * TODO: Update v4l2-dev driver to split initialize and register into
1692         * separate APIs and then update Tegra video driver to do video device
1693         * initialize followed by all video device related setup and then
1694         * register the video device.
1695         */
1696        ret = video_register_device(&chan->video, VFL_TYPE_VIDEO, -1);
1697        if (ret < 0) {
1698                dev_err(vi->dev,
1699                        "failed to register video device: %d\n", ret);
1700                goto unregister_video;
1701        }
1702
1703        /* create links between the entities */
1704        list_for_each_entry(asd, &chan->notifier.asd_list, asd_list) {
1705                entity = to_tegra_vi_graph_entity(asd);
1706                ret = tegra_vi_graph_build(chan, entity);
1707                if (ret < 0)
1708                        goto unregister_video;
1709        }
1710
1711        ret = tegra_channel_setup_ctrl_handler(chan);
1712        if (ret < 0) {
1713                dev_err(vi->dev,
1714                        "failed to setup channel controls: %d\n", ret);
1715                goto unregister_video;
1716        }
1717
1718        ret = vi_fmts_bitmap_init(chan);
1719        if (ret < 0) {
1720                dev_err(vi->dev,
1721                        "failed to initialize formats bitmap: %d\n", ret);
1722                goto unregister_video;
1723        }
1724
1725        subdev = tegra_channel_get_remote_csi_subdev(chan);
1726        if (!subdev) {
1727                ret = -ENODEV;
1728                dev_err(vi->dev,
1729                        "failed to get remote csi subdev: %d\n", ret);
1730                goto unregister_video;
1731        }
1732
1733        v4l2_set_subdev_hostdata(subdev, chan);
1734
1735        subdev = tegra_channel_get_remote_source_subdev(chan);
1736        v4l2_set_subdev_hostdata(subdev, chan);
1737
1738        return 0;
1739
1740unregister_video:
1741        vb2_video_unregister_device(&chan->video);
1742        return ret;
1743}
1744
1745static int tegra_vi_graph_notify_bound(struct v4l2_async_notifier *notifier,
1746                                       struct v4l2_subdev *subdev,
1747                                       struct v4l2_async_subdev *asd)
1748{
1749        struct tegra_vi_graph_entity *entity;
1750        struct tegra_vi *vi;
1751        struct tegra_vi_channel *chan;
1752
1753        chan = container_of(notifier, struct tegra_vi_channel, notifier);
1754        vi = chan->vi;
1755
1756        /*
1757         * Locate the entity corresponding to the bound subdev and store the
1758         * subdev pointer.
1759         */
1760        entity = tegra_vi_graph_find_entity(chan, subdev->fwnode);
1761        if (!entity) {
1762                dev_err(vi->dev, "no entity for subdev %s\n", subdev->name);
1763                return -EINVAL;
1764        }
1765
1766        if (entity->subdev) {
1767                dev_err(vi->dev, "duplicate subdev for node %pOF\n",
1768                        to_of_node(entity->asd.match.fwnode));
1769                return -EINVAL;
1770        }
1771
1772        dev_dbg(vi->dev, "subdev %s bound\n", subdev->name);
1773        entity->entity = &subdev->entity;
1774        entity->subdev = subdev;
1775
1776        return 0;
1777}
1778
1779static const struct v4l2_async_notifier_operations tegra_vi_async_ops = {
1780        .bound = tegra_vi_graph_notify_bound,
1781        .complete = tegra_vi_graph_notify_complete,
1782};
1783
1784static int tegra_vi_graph_parse_one(struct tegra_vi_channel *chan,
1785                                    struct fwnode_handle *fwnode)
1786{
1787        struct tegra_vi *vi = chan->vi;
1788        struct fwnode_handle *ep = NULL;
1789        struct fwnode_handle *remote = NULL;
1790        struct tegra_vi_graph_entity *tvge;
1791        struct device_node *node = NULL;
1792        int ret;
1793
1794        dev_dbg(vi->dev, "parsing node %pOF\n", to_of_node(fwnode));
1795
1796        /* parse all the remote entities and put them into the list */
1797        for_each_endpoint_of_node(to_of_node(fwnode), node) {
1798                ep = of_fwnode_handle(node);
1799                remote = fwnode_graph_get_remote_port_parent(ep);
1800                if (!remote) {
1801                        dev_err(vi->dev,
1802                                "remote device at %pOF not found\n", node);
1803                        ret = -EINVAL;
1804                        goto cleanup;
1805                }
1806
1807                /* skip entities that are already processed */
1808                if (remote == dev_fwnode(vi->dev) ||
1809                    tegra_vi_graph_find_entity(chan, remote)) {
1810                        fwnode_handle_put(remote);
1811                        continue;
1812                }
1813
1814                tvge = v4l2_async_notifier_add_fwnode_subdev(&chan->notifier, remote,
1815                                                             struct tegra_vi_graph_entity);
1816                if (IS_ERR(tvge)) {
1817                        ret = PTR_ERR(tvge);
1818                        dev_err(vi->dev,
1819                                "failed to add subdev to notifier: %d\n", ret);
1820                        fwnode_handle_put(remote);
1821                        goto cleanup;
1822                }
1823
1824                ret = tegra_vi_graph_parse_one(chan, remote);
1825                if (ret < 0) {
1826                        fwnode_handle_put(remote);
1827                        goto cleanup;
1828                }
1829
1830                fwnode_handle_put(remote);
1831        }
1832
1833        return 0;
1834
1835cleanup:
1836        dev_err(vi->dev, "failed parsing the graph: %d\n", ret);
1837        v4l2_async_notifier_cleanup(&chan->notifier);
1838        of_node_put(node);
1839        return ret;
1840}
1841
1842static int tegra_vi_graph_init(struct tegra_vi *vi)
1843{
1844        struct tegra_video_device *vid = dev_get_drvdata(vi->client.host);
1845        struct tegra_vi_channel *chan;
1846        struct fwnode_handle *fwnode = dev_fwnode(vi->dev);
1847        int ret;
1848        struct fwnode_handle *remote = NULL;
1849
1850        /*
1851         * Walk the links to parse the full graph. Each channel will have
1852         * one endpoint of the composite node. Start by parsing the
1853         * composite node and parse the remote entities in turn.
1854         * Each channel will register v4l2 async notifier to make the graph
1855         * independent between the channels so we can the current channel
1856         * in case of something wrong during graph parsing and continue with
1857         * next channels.
1858         */
1859        list_for_each_entry(chan, &vi->vi_chans, list) {
1860                remote = fwnode_graph_get_remote_node(fwnode, chan->portnos[0],
1861                                                      0);
1862                if (!remote)
1863                        continue;
1864
1865                ret = tegra_vi_graph_parse_one(chan, remote);
1866                fwnode_handle_put(remote);
1867                if (ret < 0 || list_empty(&chan->notifier.asd_list))
1868                        continue;
1869
1870                chan->notifier.ops = &tegra_vi_async_ops;
1871                ret = v4l2_async_notifier_register(&vid->v4l2_dev,
1872                                                   &chan->notifier);
1873                if (ret < 0) {
1874                        dev_err(vi->dev,
1875                                "failed to register channel %d notifier: %d\n",
1876                                chan->portnos[0], ret);
1877                        v4l2_async_notifier_cleanup(&chan->notifier);
1878                }
1879        }
1880
1881        return 0;
1882}
1883
1884static void tegra_vi_graph_cleanup(struct tegra_vi *vi)
1885{
1886        struct tegra_vi_channel *chan;
1887
1888        list_for_each_entry(chan, &vi->vi_chans, list) {
1889                vb2_video_unregister_device(&chan->video);
1890                v4l2_async_notifier_unregister(&chan->notifier);
1891                v4l2_async_notifier_cleanup(&chan->notifier);
1892        }
1893}
1894
1895static int tegra_vi_init(struct host1x_client *client)
1896{
1897        struct tegra_video_device *vid = dev_get_drvdata(client->host);
1898        struct tegra_vi *vi = host1x_client_to_vi(client);
1899        struct tegra_vi_channel *chan, *tmp;
1900        int ret;
1901
1902        vid->media_dev.hw_revision = vi->soc->hw_revision;
1903        snprintf(vid->media_dev.bus_info, sizeof(vid->media_dev.bus_info),
1904                 "platform:%s", dev_name(vi->dev));
1905
1906        INIT_LIST_HEAD(&vi->vi_chans);
1907
1908        if (IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
1909                ret = tegra_vi_tpg_channels_alloc(vi);
1910        else
1911                ret = tegra_vi_channels_alloc(vi);
1912        if (ret < 0) {
1913                dev_err(vi->dev,
1914                        "failed to allocate vi channels: %d\n", ret);
1915                goto free_chans;
1916        }
1917
1918        ret = tegra_vi_channels_init(vi);
1919        if (ret < 0)
1920                goto free_chans;
1921
1922        vid->vi = vi;
1923
1924        if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)) {
1925                ret = tegra_vi_graph_init(vi);
1926                if (ret < 0)
1927                        goto cleanup_chans;
1928        }
1929
1930        return 0;
1931
1932cleanup_chans:
1933        list_for_each_entry(chan, &vi->vi_chans, list)
1934                tegra_channel_cleanup(chan);
1935free_chans:
1936        list_for_each_entry_safe(chan, tmp, &vi->vi_chans, list) {
1937                list_del(&chan->list);
1938                kfree(chan);
1939        }
1940
1941        return ret;
1942}
1943
1944static int tegra_vi_exit(struct host1x_client *client)
1945{
1946        struct tegra_vi *vi = host1x_client_to_vi(client);
1947
1948        /*
1949         * Do not cleanup the channels here as application might still be
1950         * holding video device nodes. Channels cleanup will happen during
1951         * v4l2_device release callback which gets called after all video
1952         * device nodes are released.
1953         */
1954
1955        if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
1956                tegra_vi_graph_cleanup(vi);
1957
1958        return 0;
1959}
1960
1961static const struct host1x_client_ops vi_client_ops = {
1962        .init = tegra_vi_init,
1963        .exit = tegra_vi_exit,
1964};
1965
1966static int tegra_vi_probe(struct platform_device *pdev)
1967{
1968        struct tegra_vi *vi;
1969        int ret;
1970
1971        vi = devm_kzalloc(&pdev->dev, sizeof(*vi), GFP_KERNEL);
1972        if (!vi)
1973                return -ENOMEM;
1974
1975        vi->iomem = devm_platform_ioremap_resource(pdev, 0);
1976        if (IS_ERR(vi->iomem))
1977                return PTR_ERR(vi->iomem);
1978
1979        vi->soc = of_device_get_match_data(&pdev->dev);
1980
1981        vi->clk = devm_clk_get(&pdev->dev, NULL);
1982        if (IS_ERR(vi->clk)) {
1983                ret = PTR_ERR(vi->clk);
1984                dev_err(&pdev->dev, "failed to get vi clock: %d\n", ret);
1985                return ret;
1986        }
1987
1988        vi->vdd = devm_regulator_get(&pdev->dev, "avdd-dsi-csi");
1989        if (IS_ERR(vi->vdd)) {
1990                ret = PTR_ERR(vi->vdd);
1991                dev_err(&pdev->dev, "failed to get VDD supply: %d\n", ret);
1992                return ret;
1993        }
1994
1995        if (!pdev->dev.pm_domain) {
1996                ret = -ENOENT;
1997                dev_warn(&pdev->dev, "PM domain is not attached: %d\n", ret);
1998                return ret;
1999        }
2000
2001        ret = devm_of_platform_populate(&pdev->dev);
2002        if (ret < 0) {
2003                dev_err(&pdev->dev,
2004                        "failed to populate vi child device: %d\n", ret);
2005                return ret;
2006        }
2007
2008        vi->dev = &pdev->dev;
2009        vi->ops = vi->soc->ops;
2010        platform_set_drvdata(pdev, vi);
2011        pm_runtime_enable(&pdev->dev);
2012
2013        /* initialize host1x interface */
2014        INIT_LIST_HEAD(&vi->client.list);
2015        vi->client.ops = &vi_client_ops;
2016        vi->client.dev = &pdev->dev;
2017
2018        ret = host1x_client_register(&vi->client);
2019        if (ret < 0) {
2020                dev_err(&pdev->dev,
2021                        "failed to register host1x client: %d\n", ret);
2022                goto rpm_disable;
2023        }
2024
2025        return 0;
2026
2027rpm_disable:
2028        pm_runtime_disable(&pdev->dev);
2029        return ret;
2030}
2031
2032static int tegra_vi_remove(struct platform_device *pdev)
2033{
2034        struct tegra_vi *vi = platform_get_drvdata(pdev);
2035        int err;
2036
2037        err = host1x_client_unregister(&vi->client);
2038        if (err < 0) {
2039                dev_err(&pdev->dev,
2040                        "failed to unregister host1x client: %d\n", err);
2041                return err;
2042        }
2043
2044        pm_runtime_disable(&pdev->dev);
2045
2046        return 0;
2047}
2048
2049static const struct of_device_id tegra_vi_of_id_table[] = {
2050#if defined(CONFIG_ARCH_TEGRA_210_SOC)
2051        { .compatible = "nvidia,tegra210-vi", .data = &tegra210_vi_soc },
2052#endif
2053        { }
2054};
2055MODULE_DEVICE_TABLE(of, tegra_vi_of_id_table);
2056
2057static const struct dev_pm_ops tegra_vi_pm_ops = {
2058        SET_RUNTIME_PM_OPS(vi_runtime_suspend, vi_runtime_resume, NULL)
2059};
2060
2061struct platform_driver tegra_vi_driver = {
2062        .driver = {
2063                .name = "tegra-vi",
2064                .of_match_table = tegra_vi_of_id_table,
2065                .pm = &tegra_vi_pm_ops,
2066        },
2067        .probe = tegra_vi_probe,
2068        .remove = tegra_vi_remove,
2069};
2070