Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (C) 2020 NVIDIA CORPORATION.  All rights reserved.
 */

#include <linux/bitmap.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/host1x.h>
#include <linux/lcm.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_graph.h>
#include <linux/platform_device.h>
#include <linux/regulator/consumer.h>
#include <linux/pm_runtime.h>
#include <linux/slab.h>

#include <media/v4l2-event.h>
#include <media/v4l2-fh.h>
#include <media/v4l2-fwnode.h>
#include <media/v4l2-ioctl.h>
#include <media/videobuf2-dma-contig.h>

#include <soc/tegra/pmc.h>

#include "vi.h"
#include "video.h"

#define SURFACE_ALIGN_BYTES		64
#define MAX_CID_CONTROLS		1

static const struct tegra_video_format tegra_default_format = {
	.img_dt = TEGRA_IMAGE_DT_RAW10,
	.bit_width = 10,
	.code = MEDIA_BUS_FMT_SRGGB10_1X10,
	.bpp = 2,
	.img_fmt = TEGRA_IMAGE_FORMAT_DEF,
	.fourcc = V4L2_PIX_FMT_SRGGB10,
};

static inline struct tegra_vi *
host1x_client_to_vi(struct host1x_client *client)
{
	return container_of(client, struct tegra_vi, client);
}

static inline struct tegra_channel_buffer *
to_tegra_channel_buffer(struct vb2_v4l2_buffer *vb)
{
	return container_of(vb, struct tegra_channel_buffer, buf);
}

static inline struct tegra_vi_graph_entity *
to_tegra_vi_graph_entity(struct v4l2_async_subdev *asd)
{
	return container_of(asd, struct tegra_vi_graph_entity, asd);
}

static int tegra_get_format_idx_by_code(struct tegra_vi *vi,
					unsigned int code,
					unsigned int offset)
{
	unsigned int i;

	for (i = offset; i < vi->soc->nformats; ++i) {
		if (vi->soc->video_formats[i].code == code)
			return i;
	}

	return -1;
}

static u32 tegra_get_format_fourcc_by_idx(struct tegra_vi *vi,
					  unsigned int index)
{
	if (index >= vi->soc->nformats)
		return -EINVAL;

	return vi->soc->video_formats[index].fourcc;
}

static const struct tegra_video_format *
tegra_get_format_by_fourcc(struct tegra_vi *vi, u32 fourcc)
{
	unsigned int i;

	for (i = 0; i < vi->soc->nformats; ++i) {
		if (vi->soc->video_formats[i].fourcc == fourcc)
			return &vi->soc->video_formats[i];
	}

	return NULL;
}

/*
 * videobuf2 queue operations
 */
static int tegra_channel_queue_setup(struct vb2_queue *vq,
				     unsigned int *nbuffers,
				     unsigned int *nplanes,
				     unsigned int sizes[],
				     struct device *alloc_devs[])
{
	struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);

	if (*nplanes)
		return sizes[0] < chan->format.sizeimage ? -EINVAL : 0;

	*nplanes = 1;
	sizes[0] = chan->format.sizeimage;
	alloc_devs[0] = chan->vi->dev;

	return 0;
}

static int tegra_channel_buffer_prepare(struct vb2_buffer *vb)
{
	struct tegra_vi_channel *chan = vb2_get_drv_priv(vb->vb2_queue);
	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
	struct tegra_channel_buffer *buf = to_tegra_channel_buffer(vbuf);
	unsigned long size = chan->format.sizeimage;

	if (vb2_plane_size(vb, 0) < size) {
		v4l2_err(chan->video.v4l2_dev,
			 "buffer too small (%lu < %lu)\n",
			 vb2_plane_size(vb, 0), size);
		return -EINVAL;
	}

	vb2_set_plane_payload(vb, 0, size);
	buf->chan = chan;
	buf->addr = vb2_dma_contig_plane_dma_addr(vb, 0);

	return 0;
}

static void tegra_channel_buffer_queue(struct vb2_buffer *vb)
{
	struct tegra_vi_channel *chan = vb2_get_drv_priv(vb->vb2_queue);
	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
	struct tegra_channel_buffer *buf = to_tegra_channel_buffer(vbuf);

	/* put buffer into the capture queue */
	spin_lock(&chan->start_lock);
	list_add_tail(&buf->queue, &chan->capture);
	spin_unlock(&chan->start_lock);

	/* wait up kthread for capture */
	wake_up_interruptible(&chan->start_wait);
}

struct v4l2_subdev *
tegra_channel_get_remote_csi_subdev(struct tegra_vi_channel *chan)
{
	struct media_pad *pad;

	pad = media_entity_remote_pad(&chan->pad);
	if (!pad)
		return NULL;

	return media_entity_to_v4l2_subdev(pad->entity);
}

struct v4l2_subdev *
tegra_channel_get_remote_source_subdev(struct tegra_vi_channel *chan)
{
	struct media_pad *pad;
	struct v4l2_subdev *subdev;
	struct media_entity *entity;

	subdev = tegra_channel_get_remote_csi_subdev(chan);
	if (!subdev)
		return NULL;

	pad = &subdev->entity.pads[0];
	while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
		pad = media_entity_remote_pad(pad);
		if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
			break;
		entity = pad->entity;
		pad = &entity->pads[0];
		subdev = media_entity_to_v4l2_subdev(entity);
	}

	return subdev;
}

static int tegra_channel_enable_stream(struct tegra_vi_channel *chan)
{
	struct v4l2_subdev *csi_subdev, *src_subdev;
	struct tegra_csi_channel *csi_chan;
	int ret, err;

	/*
	 * Tegra CSI receiver can detect the first LP to HS transition.
	 * So, start the CSI stream-on prior to sensor stream-on and
	 * vice-versa for stream-off.
	 */
	csi_subdev = tegra_channel_get_remote_csi_subdev(chan);
	ret = v4l2_subdev_call(csi_subdev, video, s_stream, true);
	if (ret < 0 && ret != -ENOIOCTLCMD)
		return ret;

	if (IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
		return 0;

	csi_chan = v4l2_get_subdevdata(csi_subdev);
	/*
	 * TRM has incorrectly documented to wait for done status from
	 * calibration logic after CSI interface power on.
	 * As per the design, calibration results are latched and applied
	 * to the pads only when the link is in LP11 state which will happen
	 * during the sensor stream-on.
	 * CSI subdev stream-on triggers start of MIPI pads calibration.
	 * Wait for calibration to finish here after sensor subdev stream-on.
	 */
	src_subdev = tegra_channel_get_remote_source_subdev(chan);
	ret = v4l2_subdev_call(src_subdev, video, s_stream, true);
	err = tegra_mipi_finish_calibration(csi_chan->mipi);

	if (ret < 0 && ret != -ENOIOCTLCMD)
		goto err_disable_csi_stream;

	if (err < 0)
		dev_warn(csi_chan->csi->dev,
			 "MIPI calibration failed: %d\n", err);

	return 0;

err_disable_csi_stream:
	v4l2_subdev_call(csi_subdev, video, s_stream, false);
	return ret;
}

static int tegra_channel_disable_stream(struct tegra_vi_channel *chan)
{
	struct v4l2_subdev *subdev;
	int ret;

	/*
	 * Stream-off subdevices in reverse order to stream-on.
	 * Remote source subdev in TPG mode is same as CSI subdev.
	 */
	subdev = tegra_channel_get_remote_source_subdev(chan);
	ret = v4l2_subdev_call(subdev, video, s_stream, false);
	if (ret < 0 && ret != -ENOIOCTLCMD)
		return ret;

	if (IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
		return 0;

	subdev = tegra_channel_get_remote_csi_subdev(chan);
	ret = v4l2_subdev_call(subdev, video, s_stream, false);
	if (ret < 0 && ret != -ENOIOCTLCMD)
		return ret;

	return 0;
}

int tegra_channel_set_stream(struct tegra_vi_channel *chan, bool on)
{
	int ret;

	if (on)
		ret = tegra_channel_enable_stream(chan);
	else
		ret = tegra_channel_disable_stream(chan);

	return ret;
}

void tegra_channel_release_buffers(struct tegra_vi_channel *chan,
				   enum vb2_buffer_state state)
{
	struct tegra_channel_buffer *buf, *nbuf;

	spin_lock(&chan->start_lock);
	list_for_each_entry_safe(buf, nbuf, &chan->capture, queue) {
		vb2_buffer_done(&buf->buf.vb2_buf, state);
		list_del(&buf->queue);
	}
	spin_unlock(&chan->start_lock);

	spin_lock(&chan->done_lock);
	list_for_each_entry_safe(buf, nbuf, &chan->done, queue) {
		vb2_buffer_done(&buf->buf.vb2_buf, state);
		list_del(&buf->queue);
	}
	spin_unlock(&chan->done_lock);
}

static int tegra_channel_start_streaming(struct vb2_queue *vq, u32 count)
{
	struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
	int ret;

	ret = pm_runtime_get_sync(chan->vi->dev);
	if (ret < 0) {
		dev_err(chan->vi->dev, "failed to get runtime PM: %d\n", ret);
		pm_runtime_put_noidle(chan->vi->dev);
		return ret;
	}

	ret = chan->vi->ops->vi_start_streaming(vq, count);
	if (ret < 0)
		pm_runtime_put(chan->vi->dev);

	return ret;
}

static void tegra_channel_stop_streaming(struct vb2_queue *vq)
{
	struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);

	chan->vi->ops->vi_stop_streaming(vq);
	pm_runtime_put(chan->vi->dev);
}

static const struct vb2_ops tegra_channel_queue_qops = {
	.queue_setup = tegra_channel_queue_setup,
	.buf_prepare = tegra_channel_buffer_prepare,
	.buf_queue = tegra_channel_buffer_queue,
	.wait_prepare = vb2_ops_wait_prepare,
	.wait_finish = vb2_ops_wait_finish,
	.start_streaming = tegra_channel_start_streaming,
	.stop_streaming = tegra_channel_stop_streaming,
};

/*
 * V4L2 ioctl operations
 */
static int tegra_channel_querycap(struct file *file, void *fh,
				  struct v4l2_capability *cap)
{
	struct tegra_vi_channel *chan = video_drvdata(file);

	strscpy(cap->driver, "tegra-video", sizeof(cap->driver));
	strscpy(cap->card, chan->video.name, sizeof(cap->card));
	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
		 dev_name(chan->vi->dev));

	return 0;
}

static int tegra_channel_g_parm(struct file *file, void *fh,
				struct v4l2_streamparm *a)
{
	struct tegra_vi_channel *chan = video_drvdata(file);
	struct v4l2_subdev *subdev;

	subdev = tegra_channel_get_remote_source_subdev(chan);
	return v4l2_g_parm_cap(&chan->video, subdev, a);
}

static int tegra_channel_s_parm(struct file *file, void *fh,
				struct v4l2_streamparm *a)
{
	struct tegra_vi_channel *chan = video_drvdata(file);
	struct v4l2_subdev *subdev;

	subdev = tegra_channel_get_remote_source_subdev(chan);
	return v4l2_s_parm_cap(&chan->video, subdev, a);
}

static int tegra_channel_enum_framesizes(struct file *file, void *fh,
					 struct v4l2_frmsizeenum *sizes)
{
	int ret;
	struct tegra_vi_channel *chan = video_drvdata(file);
	struct v4l2_subdev *subdev;
	const struct tegra_video_format *fmtinfo;
	struct v4l2_subdev_frame_size_enum fse = {
		.index = sizes->index,
		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
	};

	fmtinfo = tegra_get_format_by_fourcc(chan->vi, sizes->pixel_format);
	if (!fmtinfo)
		return -EINVAL;

	fse.code = fmtinfo->code;

	subdev = tegra_channel_get_remote_source_subdev(chan);
	ret = v4l2_subdev_call(subdev, pad, enum_frame_size, NULL, &fse);
	if (ret)
		return ret;

	sizes->type = V4L2_FRMSIZE_TYPE_DISCRETE;
	sizes->discrete.width = fse.max_width;
	sizes->discrete.height = fse.max_height;

	return 0;
}

static int tegra_channel_enum_frameintervals(struct file *file, void *fh,
					     struct v4l2_frmivalenum *ivals)
{
	int ret;
	struct tegra_vi_channel *chan = video_drvdata(file);
	struct v4l2_subdev *subdev;
	const struct tegra_video_format *fmtinfo;
	struct v4l2_subdev_frame_interval_enum fie = {
		.index = ivals->index,
		.width = ivals->width,
		.height = ivals->height,
		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
	};

	fmtinfo = tegra_get_format_by_fourcc(chan->vi, ivals->pixel_format);
	if (!fmtinfo)
		return -EINVAL;

	fie.code = fmtinfo->code;

	subdev = tegra_channel_get_remote_source_subdev(chan);
	ret = v4l2_subdev_call(subdev, pad, enum_frame_interval, NULL, &fie);
	if (ret)
		return ret;

	ivals->type = V4L2_FRMIVAL_TYPE_DISCRETE;
	ivals->discrete.numerator = fie.interval.numerator;
	ivals->discrete.denominator = fie.interval.denominator;

	return 0;
}

static int tegra_channel_enum_format(struct file *file, void *fh,
				     struct v4l2_fmtdesc *f)
{
	struct tegra_vi_channel *chan = video_drvdata(file);
	unsigned int index = 0, i;
	unsigned long *fmts_bitmap = chan->tpg_fmts_bitmap;

	if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
		fmts_bitmap = chan->fmts_bitmap;

	if (f->index >= bitmap_weight(fmts_bitmap, MAX_FORMAT_NUM))
		return -EINVAL;

	for (i = 0; i < f->index + 1; i++, index++)
		index = find_next_bit(fmts_bitmap, MAX_FORMAT_NUM, index);

	f->pixelformat = tegra_get_format_fourcc_by_idx(chan->vi, index - 1);

	return 0;
}

static int tegra_channel_get_format(struct file *file, void *fh,
				    struct v4l2_format *format)
{
	struct tegra_vi_channel *chan = video_drvdata(file);

	format->fmt.pix = chan->format;

	return 0;
}

static void tegra_channel_fmt_align(struct tegra_vi_channel *chan,
				    struct v4l2_pix_format *pix,
				    unsigned int bpp)
{
	unsigned int min_bpl;
	unsigned int max_bpl;
	unsigned int bpl;

	/*
	 * The transfer alignment requirements are expressed in bytes.
	 * Clamp the requested width and height to the limits.
	 */
	pix->width = clamp(pix->width, TEGRA_MIN_WIDTH, TEGRA_MAX_WIDTH);
	pix->height = clamp(pix->height, TEGRA_MIN_HEIGHT, TEGRA_MAX_HEIGHT);

	/* Clamp the requested bytes per line value. If the maximum bytes per
	 * line value is zero, the module doesn't support user configurable
	 * line sizes. Override the requested value with the minimum in that
	 * case.
	 */
	min_bpl = pix->width * bpp;
	max_bpl = rounddown(TEGRA_MAX_WIDTH, SURFACE_ALIGN_BYTES);
	bpl = roundup(pix->bytesperline, SURFACE_ALIGN_BYTES);

	pix->bytesperline = clamp(bpl, min_bpl, max_bpl);
	pix->sizeimage = pix->bytesperline * pix->height;
}

static int __tegra_channel_try_format(struct tegra_vi_channel *chan,
				      struct v4l2_pix_format *pix)
{
	const struct tegra_video_format *fmtinfo;
	struct v4l2_subdev *subdev;
	struct v4l2_subdev_format fmt;
	struct v4l2_subdev_pad_config *pad_cfg;
	struct v4l2_subdev_frame_size_enum fse = {
		.which = V4L2_SUBDEV_FORMAT_TRY,
	};
	struct v4l2_subdev_selection sdsel = {
		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
		.target = V4L2_SEL_TGT_CROP_BOUNDS,
	};
	int ret;

	subdev = tegra_channel_get_remote_source_subdev(chan);
	if (!subdev)
		return -ENODEV;

	pad_cfg = v4l2_subdev_alloc_pad_config(subdev);
	if (!pad_cfg)
		return -ENOMEM;
	/*
	 * Retrieve the format information and if requested format isn't
	 * supported, keep the current format.
	 */
	fmtinfo = tegra_get_format_by_fourcc(chan->vi, pix->pixelformat);
	if (!fmtinfo) {
		pix->pixelformat = chan->format.pixelformat;
		pix->colorspace = chan->format.colorspace;
		fmtinfo = tegra_get_format_by_fourcc(chan->vi,
						     pix->pixelformat);
	}

	pix->field = V4L2_FIELD_NONE;
	fmt.which = V4L2_SUBDEV_FORMAT_TRY;
	fmt.pad = 0;
	v4l2_fill_mbus_format(&fmt.format, pix, fmtinfo->code);

	/*
	 * Attempt to obtain the format size from subdev.
	 * If not available, try to get crop boundary from subdev.
	 */
	fse.code = fmtinfo->code;
	ret = v4l2_subdev_call(subdev, pad, enum_frame_size, pad_cfg, &fse);
	if (ret) {
		ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
		if (ret)
			return -EINVAL;
		pad_cfg->try_crop.width = sdsel.r.width;
		pad_cfg->try_crop.height = sdsel.r.height;
	} else {
		pad_cfg->try_crop.width = fse.max_width;
		pad_cfg->try_crop.height = fse.max_height;
	}

	ret = v4l2_subdev_call(subdev, pad, set_fmt, pad_cfg, &fmt);
	if (ret < 0)
		return ret;

	v4l2_fill_pix_format(pix, &fmt.format);
	tegra_channel_fmt_align(chan, pix, fmtinfo->bpp);

	v4l2_subdev_free_pad_config(pad_cfg);

	return 0;
}

static int tegra_channel_try_format(struct file *file, void *fh,
				    struct v4l2_format *format)
{
	struct tegra_vi_channel *chan = video_drvdata(file);

	return __tegra_channel_try_format(chan, &format->fmt.pix);
}

static int tegra_channel_set_format(struct file *file, void *fh,
				    struct v4l2_format *format)
{
	struct tegra_vi_channel *chan = video_drvdata(file);
	const struct tegra_video_format *fmtinfo;
	struct v4l2_subdev_format fmt;
	struct v4l2_subdev *subdev;
	struct v4l2_pix_format *pix = &format->fmt.pix;
	int ret;

	if (vb2_is_busy(&chan->queue))
		return -EBUSY;

	/* get supported format by try_fmt */
	ret = __tegra_channel_try_format(chan, pix);
	if (ret)
		return ret;

	fmtinfo = tegra_get_format_by_fourcc(chan->vi, pix->pixelformat);

	fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
	fmt.pad = 0;
	v4l2_fill_mbus_format(&fmt.format, pix, fmtinfo->code);
	subdev = tegra_channel_get_remote_source_subdev(chan);
	ret = v4l2_subdev_call(subdev, pad, set_fmt, NULL, &fmt);
	if (ret < 0)
		return ret;

	v4l2_fill_pix_format(pix, &fmt.format);
	tegra_channel_fmt_align(chan, pix, fmtinfo->bpp);

	chan->format = *pix;
	chan->fmtinfo = fmtinfo;

	return 0;
}

static int tegra_channel_set_subdev_active_fmt(struct tegra_vi_channel *chan)
{
	int ret, index;
	struct v4l2_subdev *subdev;
	struct v4l2_subdev_format fmt = {
		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
	};

	/*
	 * Initialize channel format to the sub-device active format if there
	 * is corresponding match in the Tegra supported video formats.
	 */
	subdev = tegra_channel_get_remote_source_subdev(chan);
	ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
	if (ret)
		return ret;

	index = tegra_get_format_idx_by_code(chan->vi, fmt.format.code, 0);
	if (index < 0)
		return -EINVAL;

	chan->fmtinfo = &chan->vi->soc->video_formats[index];
	v4l2_fill_pix_format(&chan->format, &fmt.format);
	chan->format.pixelformat = chan->fmtinfo->fourcc;
	chan->format.bytesperline = chan->format.width * chan->fmtinfo->bpp;
	chan->format.sizeimage = chan->format.bytesperline *
				 chan->format.height;
	tegra_channel_fmt_align(chan, &chan->format, chan->fmtinfo->bpp);

	return 0;
}

static int tegra_channel_g_selection(struct file *file, void *priv,
				     struct v4l2_selection *sel)
{
	struct tegra_vi_channel *chan = video_drvdata(file);
	struct v4l2_subdev *subdev;
	struct v4l2_subdev_format fmt = {
		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
	};
	struct v4l2_subdev_selection sdsel = {
		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
		.target = sel->target,
	};
	int ret;

	subdev = tegra_channel_get_remote_source_subdev(chan);
	if (!v4l2_subdev_has_op(subdev, pad, get_selection))
		return -ENOTTY;

	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;
	/*
	 * Try the get selection operation and fallback to get format if not
	 * implemented.
	 */
	ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
	if (!ret)
		sel->r = sdsel.r;
	if (ret != -ENOIOCTLCMD)
		return ret;

	ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
	if (ret < 0)
		return ret;

	sel->r.left = 0;
	sel->r.top = 0;
	sel->r.width = fmt.format.width;
	sel->r.height = fmt.format.height;

	return 0;
}

static int tegra_channel_s_selection(struct file *file, void *fh,
				     struct v4l2_selection *sel)
{
	struct tegra_vi_channel *chan = video_drvdata(file);
	struct v4l2_subdev *subdev;
	int ret;
	struct v4l2_subdev_selection sdsel = {
		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
		.target = sel->target,
		.flags = sel->flags,
		.r = sel->r,
	};

	subdev = tegra_channel_get_remote_source_subdev(chan);
	if (!v4l2_subdev_has_op(subdev, pad, set_selection))
		return -ENOTTY;

	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

	if (vb2_is_busy(&chan->queue))
		return -EBUSY;

	ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel);
	if (!ret) {
		sel->r = sdsel.r;
		/*
		 * Subdev active format resolution may have changed during
		 * set selection operation. So, update channel format to
		 * the sub-device active format.
		 */
		return tegra_channel_set_subdev_active_fmt(chan);
	}

	return ret;
}

static int tegra_channel_enum_input(struct file *file, void *fh,
				    struct v4l2_input *inp)
{
	struct tegra_vi_channel *chan = video_drvdata(file);
	struct v4l2_subdev *subdev;

	if (inp->index)
		return -EINVAL;

	inp->type = V4L2_INPUT_TYPE_CAMERA;
	subdev = tegra_channel_get_remote_source_subdev(chan);
	strscpy(inp->name, subdev->name, sizeof(inp->name));

	return 0;
}

static int tegra_channel_g_input(struct file *file, void *priv,
				 unsigned int *i)
{
	*i = 0;

	return 0;
}

static int tegra_channel_s_input(struct file *file, void *priv,
				 unsigned int input)
{
	if (input > 0)
		return -EINVAL;

	return 0;
}

static const struct v4l2_ioctl_ops tegra_channel_ioctl_ops = {
	.vidioc_querycap		= tegra_channel_querycap,
	.vidioc_g_parm			= tegra_channel_g_parm,
	.vidioc_s_parm			= tegra_channel_s_parm,
	.vidioc_enum_framesizes		= tegra_channel_enum_framesizes,
	.vidioc_enum_frameintervals	= tegra_channel_enum_frameintervals,
	.vidioc_enum_fmt_vid_cap	= tegra_channel_enum_format,
	.vidioc_g_fmt_vid_cap		= tegra_channel_get_format,
	.vidioc_s_fmt_vid_cap		= tegra_channel_set_format,
	.vidioc_try_fmt_vid_cap		= tegra_channel_try_format,
	.vidioc_enum_input		= tegra_channel_enum_input,
	.vidioc_g_input			= tegra_channel_g_input,
	.vidioc_s_input			= tegra_channel_s_input,
	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
	.vidioc_querybuf		= vb2_ioctl_querybuf,
	.vidioc_qbuf			= vb2_ioctl_qbuf,
	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
	.vidioc_expbuf			= vb2_ioctl_expbuf,
	.vidioc_streamon		= vb2_ioctl_streamon,
	.vidioc_streamoff		= vb2_ioctl_streamoff,
	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
	.vidioc_g_selection		= tegra_channel_g_selection,
	.vidioc_s_selection		= tegra_channel_s_selection,
};

/*
 * V4L2 file operations
 */
static const struct v4l2_file_operations tegra_channel_fops = {
	.owner		= THIS_MODULE,
	.unlocked_ioctl	= video_ioctl2,
	.open		= v4l2_fh_open,
	.release	= vb2_fop_release,
	.read		= vb2_fop_read,
	.poll		= vb2_fop_poll,
	.mmap		= vb2_fop_mmap,
};

/*
 * V4L2 control operations
 */
#if IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)
static int vi_s_ctrl(struct v4l2_ctrl *ctrl)
{
	struct tegra_vi_channel *chan = container_of(ctrl->handler,
						     struct tegra_vi_channel,
						     ctrl_handler);

	switch (ctrl->id) {
	case V4L2_CID_TEST_PATTERN:
		/* pattern change takes effect on next stream */
		chan->pg_mode = ctrl->val + 1;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static const struct v4l2_ctrl_ops vi_ctrl_ops = {
	.s_ctrl	= vi_s_ctrl,
};

static const char *const vi_pattern_strings[] = {
	"Black/White Direct Mode",
	"Color Patch Mode",
};
#endif

static int tegra_channel_setup_ctrl_handler(struct tegra_vi_channel *chan)
{
	int ret;

#if IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)
	/* add test pattern control handler to v4l2 device */
	v4l2_ctrl_new_std_menu_items(&chan->ctrl_handler, &vi_ctrl_ops,
				     V4L2_CID_TEST_PATTERN,
				     ARRAY_SIZE(vi_pattern_strings) - 1,
				     0, 0, vi_pattern_strings);
	if (chan->ctrl_handler.error) {
		dev_err(chan->vi->dev, "failed to add TPG ctrl handler: %d\n",
			chan->ctrl_handler.error);
		v4l2_ctrl_handler_free(&chan->ctrl_handler);
		return chan->ctrl_handler.error;
	}
#else
	struct v4l2_subdev *subdev;

	subdev = tegra_channel_get_remote_source_subdev(chan);
	if (!subdev)
		return -ENODEV;

	ret = v4l2_ctrl_add_handler(&chan->ctrl_handler, subdev->ctrl_handler,
				    NULL, true);
	if (ret < 0) {
		dev_err(chan->vi->dev,
			"failed to add subdev %s ctrl handler: %d\n",
			subdev->name, ret);
		v4l2_ctrl_handler_free(&chan->ctrl_handler);
		return ret;
	}
#endif

	/* setup the controls */
	ret = v4l2_ctrl_handler_setup(&chan->ctrl_handler);
	if (ret < 0) {
		dev_err(chan->vi->dev,
			"failed to setup v4l2 ctrl handler: %d\n", ret);
		return ret;
	}

	return 0;
}

/* VI only support 2 formats in TPG mode */
static void vi_tpg_fmts_bitmap_init(struct tegra_vi_channel *chan)
{
	int index;

	bitmap_zero(chan->tpg_fmts_bitmap, MAX_FORMAT_NUM);

	index = tegra_get_format_idx_by_code(chan->vi,
					     MEDIA_BUS_FMT_SRGGB10_1X10, 0);
	bitmap_set(chan->tpg_fmts_bitmap, index, 1);

	index = tegra_get_format_idx_by_code(chan->vi,
					     MEDIA_BUS_FMT_RGB888_1X32_PADHI,
					     0);
	bitmap_set(chan->tpg_fmts_bitmap, index, 1);
}

static int vi_fmts_bitmap_init(struct tegra_vi_channel *chan)
{
	int index, ret, match_code = 0;
	struct v4l2_subdev *subdev;
	struct v4l2_subdev_mbus_code_enum code = {
		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
	};

	bitmap_zero(chan->fmts_bitmap, MAX_FORMAT_NUM);

	/*
	 * Set the bitmap bits based on all the matched formats between the
	 * available media bus formats of sub-device and the pre-defined Tegra
	 * supported video formats.
	 */
	subdev = tegra_channel_get_remote_source_subdev(chan);
	while (1) {
		ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
				       NULL, &code);
		if (ret < 0)
			break;

		index = tegra_get_format_idx_by_code(chan->vi, code.code, 0);
		while (index >= 0) {
			bitmap_set(chan->fmts_bitmap, index, 1);
			if (!match_code)
				match_code = code.code;
			/* look for other formats with same mbus code */
			index = tegra_get_format_idx_by_code(chan->vi,
							     code.code,
							     index + 1);
		}

		code.index++;
	}

	/*
	 * Set the bitmap bit corresponding to default tegra video format if
	 * there are no matched formats.
	 */
	if (!match_code) {
		match_code = tegra_default_format.code;
		index = tegra_get_format_idx_by_code(chan->vi, match_code, 0);
		if (WARN_ON(index < 0))
			return -EINVAL;

		bitmap_set(chan->fmts_bitmap, index, 1);
	}

	/* initialize channel format to the sub-device active format */
	tegra_channel_set_subdev_active_fmt(chan);

	return 0;
}

static void tegra_channel_cleanup(struct tegra_vi_channel *chan)
{
	v4l2_ctrl_handler_free(&chan->ctrl_handler);
	media_entity_cleanup(&chan->video.entity);
	host1x_syncpt_free(chan->mw_ack_sp);
	host1x_syncpt_free(chan->frame_start_sp);
	mutex_destroy(&chan->video_lock);
}

void tegra_channels_cleanup(struct tegra_vi *vi)
{
	struct tegra_vi_channel *chan, *tmp;

	if (!vi)
		return;

	list_for_each_entry_safe(chan, tmp, &vi->vi_chans, list) {
		tegra_channel_cleanup(chan);
		list_del(&chan->list);
		kfree(chan);
	}
}

static int tegra_channel_init(struct tegra_vi_channel *chan)
{
	struct tegra_vi *vi = chan->vi;
	struct tegra_video_device *vid = dev_get_drvdata(vi->client.host);
	unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
	int ret;

	mutex_init(&chan->video_lock);
	INIT_LIST_HEAD(&chan->capture);
	INIT_LIST_HEAD(&chan->done);
	spin_lock_init(&chan->start_lock);
	spin_lock_init(&chan->done_lock);
	spin_lock_init(&chan->sp_incr_lock);
	init_waitqueue_head(&chan->start_wait);
	init_waitqueue_head(&chan->done_wait);

	/* initialize the video format */
	chan->fmtinfo = &tegra_default_format;
	chan->format.pixelformat = chan->fmtinfo->fourcc;
	chan->format.colorspace = V4L2_COLORSPACE_SRGB;
	chan->format.field = V4L2_FIELD_NONE;
	chan->format.width = TEGRA_DEF_WIDTH;
	chan->format.height = TEGRA_DEF_HEIGHT;
	chan->format.bytesperline = TEGRA_DEF_WIDTH * chan->fmtinfo->bpp;
	chan->format.sizeimage = chan->format.bytesperline * TEGRA_DEF_HEIGHT;
	tegra_channel_fmt_align(chan, &chan->format, chan->fmtinfo->bpp);

	chan->frame_start_sp = host1x_syncpt_request(&vi->client, flags);
	if (!chan->frame_start_sp) {
		dev_err(vi->dev, "failed to request frame start syncpoint\n");
		return -ENOMEM;
	}

	chan->mw_ack_sp = host1x_syncpt_request(&vi->client, flags);
	if (!chan->mw_ack_sp) {
		dev_err(vi->dev, "failed to request memory ack syncpoint\n");
		ret = -ENOMEM;
		goto free_fs_syncpt;
	}

	/* initialize the media entity */
	chan->pad.flags = MEDIA_PAD_FL_SINK;
	ret = media_entity_pads_init(&chan->video.entity, 1, &chan->pad);
	if (ret < 0) {
		dev_err(vi->dev,
			"failed to initialize media entity: %d\n", ret);
		goto free_mw_ack_syncpt;
	}

	ret = v4l2_ctrl_handler_init(&chan->ctrl_handler, MAX_CID_CONTROLS);
	if (chan->ctrl_handler.error) {
		dev_err(vi->dev,
			"failed to initialize v4l2 ctrl handler: %d\n", ret);
		goto cleanup_media;
	}

	/* initialize the video_device */
	chan->video.fops = &tegra_channel_fops;
	chan->video.v4l2_dev = &vid->v4l2_dev;
	chan->video.release = video_device_release_empty;
	chan->video.queue = &chan->queue;
	snprintf(chan->video.name, sizeof(chan->video.name), "%s-%s-%u",
		 dev_name(vi->dev), "output", chan->portno);
	chan->video.vfl_type = VFL_TYPE_VIDEO;
	chan->video.vfl_dir = VFL_DIR_RX;
	chan->video.ioctl_ops = &tegra_channel_ioctl_ops;
	chan->video.ctrl_handler = &chan->ctrl_handler;
	chan->video.lock = &chan->video_lock;
	chan->video.device_caps = V4L2_CAP_VIDEO_CAPTURE |
				  V4L2_CAP_STREAMING |
				  V4L2_CAP_READWRITE;
	video_set_drvdata(&chan->video, chan);

	chan->queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	chan->queue.io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
	chan->queue.lock = &chan->video_lock;
	chan->queue.drv_priv = chan;
	chan->queue.buf_struct_size = sizeof(struct tegra_channel_buffer);
	chan->queue.ops = &tegra_channel_queue_qops;
	chan->queue.mem_ops = &vb2_dma_contig_memops;
	chan->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
	chan->queue.min_buffers_needed = 2;
	chan->queue.dev = vi->dev;
	ret = vb2_queue_init(&chan->queue);
	if (ret < 0) {
		dev_err(vi->dev, "failed to initialize vb2 queue: %d\n", ret);
		goto free_v4l2_ctrl_hdl;
	}

	if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
		v4l2_async_notifier_init(&chan->notifier);

	return 0;

free_v4l2_ctrl_hdl:
	v4l2_ctrl_handler_free(&chan->ctrl_handler);
cleanup_media:
	media_entity_cleanup(&chan->video.entity);
free_mw_ack_syncpt:
	host1x_syncpt_free(chan->mw_ack_sp);
free_fs_syncpt:
	host1x_syncpt_free(chan->frame_start_sp);
	return ret;
}

static int tegra_vi_channel_alloc(struct tegra_vi *vi, unsigned int port_num,
				  struct device_node *node)
{
	struct tegra_vi_channel *chan;

	/*
	 * Do not use devm_kzalloc as memory is freed immediately
	 * when device instance is unbound but application might still
	 * be holding the device node open. Channel memory allocated
	 * with kzalloc is freed during video device release callback.
	 */
	chan = kzalloc(sizeof(*chan), GFP_KERNEL);
	if (!chan)
		return -ENOMEM;

	chan->vi = vi;
	chan->portno = port_num;
	chan->of_node = node;
	list_add_tail(&chan->list, &vi->vi_chans);

	return 0;
}

static int tegra_vi_tpg_channels_alloc(struct tegra_vi *vi)
{
	unsigned int port_num;
	unsigned int nchannels = vi->soc->vi_max_channels;
	int ret;

	for (port_num = 0; port_num < nchannels; port_num++) {
		ret = tegra_vi_channel_alloc(vi, port_num, vi->dev->of_node);
		if (ret < 0)
			return ret;
	}

	return 0;
}

static int tegra_vi_channels_alloc(struct tegra_vi *vi)
{
	struct device_node *node = vi->dev->of_node;
	struct device_node *ep = NULL;
	struct device_node *ports;
	struct device_node *port;
	unsigned int port_num;
	int ret = 0;

	ports = of_get_child_by_name(node, "ports");
	if (!ports)
		return -ENODEV;

	for_each_child_of_node(ports, port) {
		if (!of_node_name_eq(port, "port"))
			continue;

		ret = of_property_read_u32(port, "reg", &port_num);
		if (ret < 0)
			continue;

		if (port_num > vi->soc->vi_max_channels) {
			dev_err(vi->dev, "invalid port num %d for %pOF\n",
				port_num, port);
			ret = -EINVAL;
			of_node_put(port);
			goto cleanup;
		}

		ep = of_get_child_by_name(port, "endpoint");
		if (!ep)
			continue;

		of_node_put(ep);
		ret = tegra_vi_channel_alloc(vi, port_num, port);
		if (ret < 0) {
			of_node_put(port);
			goto cleanup;
		}
	}

cleanup:
	of_node_put(ports);
	return ret;
}

static int tegra_vi_channels_init(struct tegra_vi *vi)
{
	struct tegra_vi_channel *chan;
	int ret;

	list_for_each_entry(chan, &vi->vi_chans, list) {
		ret = tegra_channel_init(chan);
		if (ret < 0) {
			dev_err(vi->dev,
				"failed to initialize channel-%d: %d\n",
				chan->portno, ret);
			goto cleanup;
		}
	}

	return 0;

cleanup:
	list_for_each_entry_continue_reverse(chan, &vi->vi_chans, list)
		tegra_channel_cleanup(chan);

	return ret;
}

void tegra_v4l2_nodes_cleanup_tpg(struct tegra_video_device *vid)
{
	struct tegra_vi *vi = vid->vi;
	struct tegra_csi *csi = vid->csi;
	struct tegra_csi_channel *csi_chan;
	struct tegra_vi_channel *chan;

	list_for_each_entry(chan, &vi->vi_chans, list)
		vb2_video_unregister_device(&chan->video);

	list_for_each_entry(csi_chan, &csi->csi_chans, list)
		v4l2_device_unregister_subdev(&csi_chan->subdev);
}

int tegra_v4l2_nodes_setup_tpg(struct tegra_video_device *vid)
{
	struct tegra_vi *vi = vid->vi;
	struct tegra_csi *csi = vid->csi;
	struct tegra_vi_channel *vi_chan;
	struct tegra_csi_channel *csi_chan;
	u32 link_flags = MEDIA_LNK_FL_ENABLED;
	int ret;

	if (!vi || !csi)
		return -ENODEV;

	csi_chan = list_first_entry(&csi->csi_chans,
				    struct tegra_csi_channel, list);

	list_for_each_entry(vi_chan, &vi->vi_chans, list) {
		struct media_entity *source = &csi_chan->subdev.entity;
		struct media_entity *sink = &vi_chan->video.entity;
		struct media_pad *source_pad = csi_chan->pads;
		struct media_pad *sink_pad = &vi_chan->pad;

		ret = v4l2_device_register_subdev(&vid->v4l2_dev,
						  &csi_chan->subdev);
		if (ret) {
			dev_err(vi->dev,
				"failed to register subdev: %d\n", ret);
			goto cleanup;
		}

		ret = video_register_device(&vi_chan->video,
					    VFL_TYPE_VIDEO, -1);
		if (ret < 0) {
			dev_err(vi->dev,
				"failed to register video device: %d\n", ret);
			goto cleanup;
		}

		dev_dbg(vi->dev, "creating %s:%u -> %s:%u link\n",
			source->name, source_pad->index,
			sink->name, sink_pad->index);

		ret = media_create_pad_link(source, source_pad->index,
					    sink, sink_pad->index,
					    link_flags);
		if (ret < 0) {
			dev_err(vi->dev,
				"failed to create %s:%u -> %s:%u link: %d\n",
				source->name, source_pad->index,
				sink->name, sink_pad->index, ret);
			goto cleanup;
		}

		ret = tegra_channel_setup_ctrl_handler(vi_chan);
		if (ret < 0)
			goto cleanup;

		v4l2_set_subdev_hostdata(&csi_chan->subdev, vi_chan);
		vi_tpg_fmts_bitmap_init(vi_chan);
		csi_chan = list_next_entry(csi_chan, list);
	}

	return 0;

cleanup:
	tegra_v4l2_nodes_cleanup_tpg(vid);
	return ret;
}

static int __maybe_unused vi_runtime_resume(struct device *dev)
{
	struct tegra_vi *vi = dev_get_drvdata(dev);
	int ret;

	ret = regulator_enable(vi->vdd);
	if (ret) {
		dev_err(dev, "failed to enable VDD supply: %d\n", ret);
		return ret;
	}

	ret = clk_set_rate(vi->clk, vi->soc->vi_max_clk_hz);
	if (ret) {
		dev_err(dev, "failed to set vi clock rate: %d\n", ret);
		goto disable_vdd;
	}

	ret = clk_prepare_enable(vi->clk);
	if (ret) {
		dev_err(dev, "failed to enable vi clock: %d\n", ret);
		goto disable_vdd;
	}

	return 0;

disable_vdd:
	regulator_disable(vi->vdd);
	return ret;
}

static int __maybe_unused vi_runtime_suspend(struct device *dev)
{
	struct tegra_vi *vi = dev_get_drvdata(dev);

	clk_disable_unprepare(vi->clk);

	regulator_disable(vi->vdd);

	return 0;
}

/*
 * Graph Management
 */
static struct tegra_vi_graph_entity *
tegra_vi_graph_find_entity(struct tegra_vi_channel *chan,
			   const struct fwnode_handle *fwnode)
{
	struct tegra_vi_graph_entity *entity;
	struct v4l2_async_subdev *asd;

	list_for_each_entry(asd, &chan->notifier.asd_list, asd_list) {
		entity = to_tegra_vi_graph_entity(asd);
		if (entity->asd.match.fwnode == fwnode)
			return entity;
	}

	return NULL;
}

static int tegra_vi_graph_build(struct tegra_vi_channel *chan,
				struct tegra_vi_graph_entity *entity)
{
	struct tegra_vi *vi = chan->vi;
	struct tegra_vi_graph_entity *ent;
	struct fwnode_handle *ep = NULL;
	struct v4l2_fwnode_link link;
	struct media_entity *local = entity->entity;
	struct media_entity *remote;
	struct media_pad *local_pad;
	struct media_pad *remote_pad;
	u32 link_flags = MEDIA_LNK_FL_ENABLED;
	int ret = 0;

	dev_dbg(vi->dev, "creating links for entity %s\n", local->name);

	while (1) {
		ep = fwnode_graph_get_next_endpoint(entity->asd.match.fwnode,
						    ep);
		if (!ep)
			break;

		ret = v4l2_fwnode_parse_link(ep, &link);
		if (ret < 0) {
			dev_err(vi->dev, "failed to parse link for %pOF: %d\n",
				to_of_node(ep), ret);
			continue;
		}

		if (link.local_port >= local->num_pads) {
			dev_err(vi->dev, "invalid port number %u on %pOF\n",
				link.local_port, to_of_node(link.local_node));
			v4l2_fwnode_put_link(&link);
			ret = -EINVAL;
			break;
		}

		local_pad = &local->pads[link.local_port];
		/* Remote node is vi node. So use channel video entity and pad
		 * as remote/sink.
		 */
		if (link.remote_node == of_fwnode_handle(vi->dev->of_node)) {
			remote = &chan->video.entity;
			remote_pad = &chan->pad;
			goto create_link;
		}

		/*
		 * Skip sink ports, they will be processed from the other end
		 * of the link.
		 */
		if (local_pad->flags & MEDIA_PAD_FL_SINK) {
			dev_dbg(vi->dev, "skipping sink port %pOF:%u\n",
				to_of_node(link.local_node), link.local_port);
			v4l2_fwnode_put_link(&link);
			continue;
		}

		/* find the remote entity from notifier list */
		ent = tegra_vi_graph_find_entity(chan, link.remote_node);
		if (!ent) {
			dev_err(vi->dev, "no entity found for %pOF\n",
				to_of_node(link.remote_node));
			v4l2_fwnode_put_link(&link);
			ret = -ENODEV;
			break;
		}

		remote = ent->entity;
		if (link.remote_port >= remote->num_pads) {
			dev_err(vi->dev, "invalid port number %u on %pOF\n",
				link.remote_port,
				to_of_node(link.remote_node));
			v4l2_fwnode_put_link(&link);
			ret = -EINVAL;
			break;
		}

		remote_pad = &remote->pads[link.remote_port];

create_link:
		dev_dbg(vi->dev, "creating %s:%u -> %s:%u link\n",
			local->name, local_pad->index,
			remote->name, remote_pad->index);

		ret = media_create_pad_link(local, local_pad->index,
					    remote, remote_pad->index,
					    link_flags);
		v4l2_fwnode_put_link(&link);
		if (ret < 0) {
			dev_err(vi->dev,
				"failed to create %s:%u -> %s:%u link: %d\n",
				local->name, local_pad->index,
				remote->name, remote_pad->index, ret);
			break;
		}
	}

	fwnode_handle_put(ep);
	return ret;
}

static int tegra_vi_graph_notify_complete(struct v4l2_async_notifier *notifier)
{
	struct tegra_vi_graph_entity *entity;
	struct v4l2_async_subdev *asd;
	struct v4l2_subdev *subdev;
	struct tegra_vi_channel *chan;
	struct tegra_vi *vi;
	int ret;

	chan = container_of(notifier, struct tegra_vi_channel, notifier);
	vi = chan->vi;

	dev_dbg(vi->dev, "notify complete, all subdevs registered\n");

	/*
	 * Video device node should be created at the end of all the device
	 * related initialization/setup.
	 * Current video_register_device() does both initialize and register
	 * video device in same API.
	 *
	 * TODO: Update v4l2-dev driver to split initialize and register into
	 * separate APIs and then update Tegra video driver to do video device
	 * initialize followed by all video device related setup and then
	 * register the video device.
	 */
	ret = video_register_device(&chan->video, VFL_TYPE_VIDEO, -1);
	if (ret < 0) {
		dev_err(vi->dev,
			"failed to register video device: %d\n", ret);
		goto unregister_video;
	}

	/* create links between the entities */
	list_for_each_entry(asd, &chan->notifier.asd_list, asd_list) {
		entity = to_tegra_vi_graph_entity(asd);
		ret = tegra_vi_graph_build(chan, entity);
		if (ret < 0)
			goto unregister_video;
	}

	ret = tegra_channel_setup_ctrl_handler(chan);
	if (ret < 0) {
		dev_err(vi->dev,
			"failed to setup channel controls: %d\n", ret);
		goto unregister_video;
	}

	ret = vi_fmts_bitmap_init(chan);
	if (ret < 0) {
		dev_err(vi->dev,
			"failed to initialize formats bitmap: %d\n", ret);
		goto unregister_video;
	}

	subdev = tegra_channel_get_remote_csi_subdev(chan);
	if (!subdev) {
		ret = -ENODEV;
		dev_err(vi->dev,
			"failed to get remote csi subdev: %d\n", ret);
		goto unregister_video;
	}

	v4l2_set_subdev_hostdata(subdev, chan);

	return 0;

unregister_video:
	vb2_video_unregister_device(&chan->video);
	return ret;
}

static int tegra_vi_graph_notify_bound(struct v4l2_async_notifier *notifier,
				       struct v4l2_subdev *subdev,
				       struct v4l2_async_subdev *asd)
{
	struct tegra_vi_graph_entity *entity;
	struct tegra_vi *vi;
	struct tegra_vi_channel *chan;

	chan = container_of(notifier, struct tegra_vi_channel, notifier);
	vi = chan->vi;

	/*
	 * Locate the entity corresponding to the bound subdev and store the
	 * subdev pointer.
	 */
	entity = tegra_vi_graph_find_entity(chan, subdev->fwnode);
	if (!entity) {
		dev_err(vi->dev, "no entity for subdev %s\n", subdev->name);
		return -EINVAL;
	}

	if (entity->subdev) {
		dev_err(vi->dev, "duplicate subdev for node %pOF\n",
			to_of_node(entity->asd.match.fwnode));
		return -EINVAL;
	}

	dev_dbg(vi->dev, "subdev %s bound\n", subdev->name);
	entity->entity = &subdev->entity;
	entity->subdev = subdev;

	return 0;
}

static const struct v4l2_async_notifier_operations tegra_vi_async_ops = {
	.bound = tegra_vi_graph_notify_bound,
	.complete = tegra_vi_graph_notify_complete,
};

static int tegra_vi_graph_parse_one(struct tegra_vi_channel *chan,
				    struct fwnode_handle *fwnode)
{
	struct tegra_vi *vi = chan->vi;
	struct fwnode_handle *ep = NULL;
	struct fwnode_handle *remote = NULL;
	struct v4l2_async_subdev *asd;
	struct device_node *node = NULL;
	int ret;

	dev_dbg(vi->dev, "parsing node %pOF\n", to_of_node(fwnode));

	/* parse all the remote entities and put them into the list */
	for_each_endpoint_of_node(to_of_node(fwnode), node) {
		ep = of_fwnode_handle(node);
		remote = fwnode_graph_get_remote_port_parent(ep);
		if (!remote) {
			dev_err(vi->dev,
				"remote device at %pOF not found\n", node);
			ret = -EINVAL;
			goto cleanup;
		}

		/* skip entities that are already processed */
		if (remote == dev_fwnode(vi->dev) ||
		    tegra_vi_graph_find_entity(chan, remote)) {
			fwnode_handle_put(remote);
			continue;
		}

		asd = v4l2_async_notifier_add_fwnode_subdev(&chan->notifier,
				remote, sizeof(struct tegra_vi_graph_entity));
		if (IS_ERR(asd)) {
			ret = PTR_ERR(asd);
			dev_err(vi->dev,
				"failed to add subdev to notifier: %d\n", ret);
			fwnode_handle_put(remote);
			goto cleanup;
		}

		ret = tegra_vi_graph_parse_one(chan, remote);
		if (ret < 0) {
			fwnode_handle_put(remote);
			goto cleanup;
		}

		fwnode_handle_put(remote);
	}

	return 0;

cleanup:
	dev_err(vi->dev, "failed parsing the graph: %d\n", ret);
	v4l2_async_notifier_cleanup(&chan->notifier);
	of_node_put(node);
	return ret;
}

static int tegra_vi_graph_init(struct tegra_vi *vi)
{
	struct tegra_video_device *vid = dev_get_drvdata(vi->client.host);
	struct tegra_vi_channel *chan;
	struct fwnode_handle *fwnode = dev_fwnode(vi->dev);
	int ret;
	struct fwnode_handle *remote = NULL;

	/*
	 * Walk the links to parse the full graph. Each channel will have
	 * one endpoint of the composite node. Start by parsing the
	 * composite node and parse the remote entities in turn.
	 * Each channel will register v4l2 async notifier to make the graph
	 * independent between the channels so we can the current channel
	 * in case of something wrong during graph parsing and continue with
	 * next channels.
	 */
	list_for_each_entry(chan, &vi->vi_chans, list) {
		remote = fwnode_graph_get_remote_node(fwnode, chan->portno, 0);
		if (!remote)
			continue;

		ret = tegra_vi_graph_parse_one(chan, remote);
		fwnode_handle_put(remote);
		if (ret < 0 || list_empty(&chan->notifier.asd_list))
			continue;

		chan->notifier.ops = &tegra_vi_async_ops;
		ret = v4l2_async_notifier_register(&vid->v4l2_dev,
						   &chan->notifier);
		if (ret < 0) {
			dev_err(vi->dev,
				"failed to register channel %d notifier: %d\n",
				chan->portno, ret);
			v4l2_async_notifier_cleanup(&chan->notifier);
		}
	}

	return 0;
}

static void tegra_vi_graph_cleanup(struct tegra_vi *vi)
{
	struct tegra_vi_channel *chan;

	list_for_each_entry(chan, &vi->vi_chans, list) {
		vb2_video_unregister_device(&chan->video);
		v4l2_async_notifier_unregister(&chan->notifier);
		v4l2_async_notifier_cleanup(&chan->notifier);
	}
}

static int tegra_vi_init(struct host1x_client *client)
{
	struct tegra_video_device *vid = dev_get_drvdata(client->host);
	struct tegra_vi *vi = host1x_client_to_vi(client);
	struct tegra_vi_channel *chan, *tmp;
	int ret;

	vid->media_dev.hw_revision = vi->soc->hw_revision;
	snprintf(vid->media_dev.bus_info, sizeof(vid->media_dev.bus_info),
		 "platform:%s", dev_name(vi->dev));

	INIT_LIST_HEAD(&vi->vi_chans);

	if (IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
		ret = tegra_vi_tpg_channels_alloc(vi);
	else
		ret = tegra_vi_channels_alloc(vi);
	if (ret < 0) {
		dev_err(vi->dev,
			"failed to allocate vi channels: %d\n", ret);
		goto free_chans;
	}

	ret = tegra_vi_channels_init(vi);
	if (ret < 0)
		goto free_chans;

	vid->vi = vi;

	if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)) {
		ret = tegra_vi_graph_init(vi);
		if (ret < 0)
			goto free_chans;
	}

	return 0;

free_chans:
	list_for_each_entry_safe(chan, tmp, &vi->vi_chans, list) {
		list_del(&chan->list);
		kfree(chan);
	}

	return ret;
}

static int tegra_vi_exit(struct host1x_client *client)
{
	struct tegra_vi *vi = host1x_client_to_vi(client);

	/*
	 * Do not cleanup the channels here as application might still be
	 * holding video device nodes. Channels cleanup will happen during
	 * v4l2_device release callback which gets called after all video
	 * device nodes are released.
	 */

	if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
		tegra_vi_graph_cleanup(vi);

	return 0;
}

static const struct host1x_client_ops vi_client_ops = {
	.init = tegra_vi_init,
	.exit = tegra_vi_exit,
};

static int tegra_vi_probe(struct platform_device *pdev)
{
	struct tegra_vi *vi;
	int ret;

	vi = devm_kzalloc(&pdev->dev, sizeof(*vi), GFP_KERNEL);
	if (!vi)
		return -ENOMEM;

	vi->iomem = devm_platform_ioremap_resource(pdev, 0);
	if (IS_ERR(vi->iomem))
		return PTR_ERR(vi->iomem);

	vi->soc = of_device_get_match_data(&pdev->dev);

	vi->clk = devm_clk_get(&pdev->dev, NULL);
	if (IS_ERR(vi->clk)) {
		ret = PTR_ERR(vi->clk);
		dev_err(&pdev->dev, "failed to get vi clock: %d\n", ret);
		return ret;
	}

	vi->vdd = devm_regulator_get(&pdev->dev, "avdd-dsi-csi");
	if (IS_ERR(vi->vdd)) {
		ret = PTR_ERR(vi->vdd);
		dev_err(&pdev->dev, "failed to get VDD supply: %d\n", ret);
		return ret;
	}

	if (!pdev->dev.pm_domain) {
		ret = -ENOENT;
		dev_warn(&pdev->dev, "PM domain is not attached: %d\n", ret);
		return ret;
	}

	ret = devm_of_platform_populate(&pdev->dev);
	if (ret < 0) {
		dev_err(&pdev->dev,
			"failed to populate vi child device: %d\n", ret);
		return ret;
	}

	vi->dev = &pdev->dev;
	vi->ops = vi->soc->ops;
	platform_set_drvdata(pdev, vi);
	pm_runtime_enable(&pdev->dev);

	/* initialize host1x interface */
	INIT_LIST_HEAD(&vi->client.list);
	vi->client.ops = &vi_client_ops;
	vi->client.dev = &pdev->dev;

	ret = host1x_client_register(&vi->client);
	if (ret < 0) {
		dev_err(&pdev->dev,
			"failed to register host1x client: %d\n", ret);
		goto rpm_disable;
	}

	return 0;

rpm_disable:
	pm_runtime_disable(&pdev->dev);
	return ret;
}

static int tegra_vi_remove(struct platform_device *pdev)
{
	struct tegra_vi *vi = platform_get_drvdata(pdev);
	int err;

	err = host1x_client_unregister(&vi->client);
	if (err < 0) {
		dev_err(&pdev->dev,
			"failed to unregister host1x client: %d\n", err);
		return err;
	}

	pm_runtime_disable(&pdev->dev);

	return 0;
}

static const struct of_device_id tegra_vi_of_id_table[] = {
#if defined(CONFIG_ARCH_TEGRA_210_SOC)
	{ .compatible = "nvidia,tegra210-vi", .data = &tegra210_vi_soc },
#endif
	{ }
};
MODULE_DEVICE_TABLE(of, tegra_vi_of_id_table);

static const struct dev_pm_ops tegra_vi_pm_ops = {
	SET_RUNTIME_PM_OPS(vi_runtime_suspend, vi_runtime_resume, NULL)
};

struct platform_driver tegra_vi_driver = {
	.driver = {
		.name = "tegra-vi",
		.of_match_table = tegra_vi_of_id_table,
		.pm = &tegra_vi_pm_ops,
	},
	.probe = tegra_vi_probe,
	.remove = tegra_vi_remove,
};