Free Electrons

Embedded Linux Experts

   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
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
/*
 * drivers/media/video/omap24xxcam.c
 *
 * OMAP 2 camera block driver.
 *
 * Copyright (C) 2004 MontaVista Software, Inc.
 * Copyright (C) 2004 Texas Instruments.
 * Copyright (C) 2007-2008 Nokia Corporation.
 *
 * Contact: Sakari Ailus <sakari.ailus@nokia.com>
 *
 * Based on code from Andy Lowe <source@mvista.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/videodev2.h>
#include <linux/pci.h>		/* needed for videobufs */
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/module.h>

#include <media/v4l2-common.h>
#include <media/v4l2-ioctl.h>

#include "omap24xxcam.h"

#define OMAP24XXCAM_VERSION "0.0.1"

#define RESET_TIMEOUT_NS 10000

static void omap24xxcam_reset(struct omap24xxcam_device *cam);
static int omap24xxcam_sensor_if_enable(struct omap24xxcam_device *cam);
static void omap24xxcam_device_unregister(struct v4l2_int_device *s);
static int omap24xxcam_remove(struct platform_device *pdev);

/* module parameters */
static int video_nr = -1;	/* video device minor (-1 ==> auto assign) */
/*
 * Maximum amount of memory to use for capture buffers.
 * Default is 4800KB, enough to double-buffer SXGA.
 */
static int capture_mem = 1280 * 960 * 2 * 2;

static struct v4l2_int_device omap24xxcam;

/*
 *
 * Clocks.
 *
 */

static void omap24xxcam_clock_put(struct omap24xxcam_device *cam)
{
	if (cam->ick != NULL && !IS_ERR(cam->ick))
		clk_put(cam->ick);
	if (cam->fck != NULL && !IS_ERR(cam->fck))
		clk_put(cam->fck);

	cam->ick = cam->fck = NULL;
}

static int omap24xxcam_clock_get(struct omap24xxcam_device *cam)
{
	int rval = 0;

	cam->fck = clk_get(cam->dev, "fck");
	if (IS_ERR(cam->fck)) {
		dev_err(cam->dev, "can't get camera fck");
		rval = PTR_ERR(cam->fck);
		omap24xxcam_clock_put(cam);
		return rval;
	}

	cam->ick = clk_get(cam->dev, "ick");
	if (IS_ERR(cam->ick)) {
		dev_err(cam->dev, "can't get camera ick");
		rval = PTR_ERR(cam->ick);
		omap24xxcam_clock_put(cam);
	}

	return rval;
}

static void omap24xxcam_clock_on(struct omap24xxcam_device *cam)
{
	clk_enable(cam->fck);
	clk_enable(cam->ick);
}

static void omap24xxcam_clock_off(struct omap24xxcam_device *cam)
{
	clk_disable(cam->fck);
	clk_disable(cam->ick);
}

/*
 *
 * Camera core
 *
 */

/*
 * Set xclk.
 *
 * To disable xclk, use value zero.
 */
static void omap24xxcam_core_xclk_set(const struct omap24xxcam_device *cam,
				      u32 xclk)
{
	if (xclk) {
		u32 divisor = CAM_MCLK / xclk;

		if (divisor == 1)
			omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET,
					    CC_CTRL_XCLK,
					    CC_CTRL_XCLK_DIV_BYPASS);
		else
			omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET,
					    CC_CTRL_XCLK, divisor);
	} else
		omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET,
				    CC_CTRL_XCLK, CC_CTRL_XCLK_DIV_STABLE_LOW);
}

static void omap24xxcam_core_hwinit(const struct omap24xxcam_device *cam)
{
	/*
	 * Setting the camera core AUTOIDLE bit causes problems with frame
	 * synchronization, so we will clear the AUTOIDLE bit instead.
	 */
	omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_SYSCONFIG,
			    CC_SYSCONFIG_AUTOIDLE);

	/* program the camera interface DMA packet size */
	omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL_DMA,
			    CC_CTRL_DMA_EN | (DMA_THRESHOLD / 4 - 1));

	/* enable camera core error interrupts */
	omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_IRQENABLE,
			    CC_IRQENABLE_FW_ERR_IRQ
			    | CC_IRQENABLE_FSC_ERR_IRQ
			    | CC_IRQENABLE_SSC_ERR_IRQ
			    | CC_IRQENABLE_FIFO_OF_IRQ);
}

/*
 * Enable the camera core.
 *
 * Data transfer to the camera DMA starts from next starting frame.
 */
static void omap24xxcam_core_enable(const struct omap24xxcam_device *cam)
{

	omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL,
			    cam->cc_ctrl);
}

/*
 * Disable camera core.
 *
 * The data transfer will be stopped immediately (CC_CTRL_CC_RST). The
 * core internal state machines will be reset. Use
 * CC_CTRL_CC_FRAME_TRIG instead if you want to transfer the current
 * frame completely.
 */
static void omap24xxcam_core_disable(const struct omap24xxcam_device *cam)
{
	omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL,
			    CC_CTRL_CC_RST);
}

/* Interrupt service routine for camera core interrupts. */
static void omap24xxcam_core_isr(struct omap24xxcam_device *cam)
{
	u32 cc_irqstatus;
	const u32 cc_irqstatus_err =
		CC_IRQSTATUS_FW_ERR_IRQ
		| CC_IRQSTATUS_FSC_ERR_IRQ
		| CC_IRQSTATUS_SSC_ERR_IRQ
		| CC_IRQSTATUS_FIFO_UF_IRQ
		| CC_IRQSTATUS_FIFO_OF_IRQ;

	cc_irqstatus = omap24xxcam_reg_in(cam->mmio_base + CC_REG_OFFSET,
					  CC_IRQSTATUS);
	omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_IRQSTATUS,
			    cc_irqstatus);

	if (cc_irqstatus & cc_irqstatus_err
	    && !atomic_read(&cam->in_reset)) {
		dev_dbg(cam->dev, "resetting camera, cc_irqstatus 0x%x\n",
			cc_irqstatus);
		omap24xxcam_reset(cam);
	}
}

/*
 *
 * videobuf_buffer handling.
 *
 * Memory for mmapped videobuf_buffers is not allocated
 * conventionally, but by several kmalloc allocations and then
 * creating the scatterlist on our own. User-space buffers are handled
 * normally.
 *
 */

/*
 * Free the memory-mapped buffer memory allocated for a
 * videobuf_buffer and the associated scatterlist.
 */
static void omap24xxcam_vbq_free_mmap_buffer(struct videobuf_buffer *vb)
{
	struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
	size_t alloc_size;
	struct page *page;
	int i;

	if (dma->sglist == NULL)
		return;

	i = dma->sglen;
	while (i) {
		i--;
		alloc_size = sg_dma_len(&dma->sglist[i]);
		page = sg_page(&dma->sglist[i]);
		do {
			ClearPageReserved(page++);
		} while (alloc_size -= PAGE_SIZE);
		__free_pages(sg_page(&dma->sglist[i]),
			     get_order(sg_dma_len(&dma->sglist[i])));
	}

	kfree(dma->sglist);
	dma->sglist = NULL;
}

/* Release all memory related to the videobuf_queue. */
static void omap24xxcam_vbq_free_mmap_buffers(struct videobuf_queue *vbq)
{
	int i;

	mutex_lock(&vbq->vb_lock);

	for (i = 0; i < VIDEO_MAX_FRAME; i++) {
		if (NULL == vbq->bufs[i])
			continue;
		if (V4L2_MEMORY_MMAP != vbq->bufs[i]->memory)
			continue;
		vbq->ops->buf_release(vbq, vbq->bufs[i]);
		omap24xxcam_vbq_free_mmap_buffer(vbq->bufs[i]);
		kfree(vbq->bufs[i]);
		vbq->bufs[i] = NULL;
	}

	mutex_unlock(&vbq->vb_lock);

	videobuf_mmap_free(vbq);
}

/*
 * Allocate physically as contiguous as possible buffer for video
 * frame and allocate and build DMA scatter-gather list for it.
 */
static int omap24xxcam_vbq_alloc_mmap_buffer(struct videobuf_buffer *vb)
{
	unsigned int order;
	size_t alloc_size, size = vb->bsize; /* vb->bsize is page aligned */
	struct page *page;
	int max_pages, err = 0, i = 0;
	struct videobuf_dmabuf *dma = videobuf_to_dma(vb);

	/*
	 * allocate maximum size scatter-gather list. Note this is
	 * overhead. We may not use as many entries as we allocate
	 */
	max_pages = vb->bsize >> PAGE_SHIFT;
	dma->sglist = kcalloc(max_pages, sizeof(*dma->sglist), GFP_KERNEL);
	if (dma->sglist == NULL) {
		err = -ENOMEM;
		goto out;
	}

	while (size) {
		order = get_order(size);
		/*
		 * do not over-allocate even if we would get larger
		 * contiguous chunk that way
		 */
		if ((PAGE_SIZE << order) > size)
			order--;

		/* try to allocate as many contiguous pages as possible */
		page = alloc_pages(GFP_KERNEL, order);
		/* if allocation fails, try to allocate smaller amount */
		while (page == NULL) {
			order--;
			page = alloc_pages(GFP_KERNEL, order);
			if (page == NULL && !order) {
				err = -ENOMEM;
				goto out;
			}
		}
		size -= (PAGE_SIZE << order);

		/* append allocated chunk of pages into scatter-gather list */
		sg_set_page(&dma->sglist[i], page, PAGE_SIZE << order, 0);
		dma->sglen++;
		i++;

		alloc_size = (PAGE_SIZE << order);

		/* clear pages before giving them to user space */
		memset(page_address(page), 0, alloc_size);

		/* mark allocated pages reserved */
		do {
			SetPageReserved(page++);
		} while (alloc_size -= PAGE_SIZE);
	}
	/*
	 * REVISIT: not fully correct to assign nr_pages == sglen but
	 * video-buf is passing nr_pages for e.g. unmap_sg calls
	 */
	dma->nr_pages = dma->sglen;
	dma->direction = PCI_DMA_FROMDEVICE;

	return 0;

out:
	omap24xxcam_vbq_free_mmap_buffer(vb);
	return err;
}

static int omap24xxcam_vbq_alloc_mmap_buffers(struct videobuf_queue *vbq,
					      unsigned int count)
{
	int i, err = 0;
	struct omap24xxcam_fh *fh =
		container_of(vbq, struct omap24xxcam_fh, vbq);

	mutex_lock(&vbq->vb_lock);

	for (i = 0; i < count; i++) {
		err = omap24xxcam_vbq_alloc_mmap_buffer(vbq->bufs[i]);
		if (err)
			goto out;
		dev_dbg(fh->cam->dev, "sglen is %d for buffer %d\n",
			videobuf_to_dma(vbq->bufs[i])->sglen, i);
	}

	mutex_unlock(&vbq->vb_lock);

	return 0;
out:
	while (i) {
		i--;
		omap24xxcam_vbq_free_mmap_buffer(vbq->bufs[i]);
	}

	mutex_unlock(&vbq->vb_lock);

	return err;
}

/*
 * This routine is called from interrupt context when a scatter-gather DMA
 * transfer of a videobuf_buffer completes.
 */
static void omap24xxcam_vbq_complete(struct omap24xxcam_sgdma *sgdma,
				     u32 csr, void *arg)
{
	struct omap24xxcam_device *cam =
		container_of(sgdma, struct omap24xxcam_device, sgdma);
	struct omap24xxcam_fh *fh = cam->streaming->private_data;
	struct videobuf_buffer *vb = (struct videobuf_buffer *)arg;
	const u32 csr_error = CAMDMA_CSR_MISALIGNED_ERR
		| CAMDMA_CSR_SUPERVISOR_ERR | CAMDMA_CSR_SECURE_ERR
		| CAMDMA_CSR_TRANS_ERR | CAMDMA_CSR_DROP;
	unsigned long flags;

	spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
	if (--cam->sgdma_in_queue == 0)
		omap24xxcam_core_disable(cam);
	spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);

	do_gettimeofday(&vb->ts);
	vb->field_count = atomic_add_return(2, &fh->field_count);
	if (csr & csr_error) {
		vb->state = VIDEOBUF_ERROR;
		if (!atomic_read(&fh->cam->in_reset)) {
			dev_dbg(cam->dev, "resetting camera, csr 0x%x\n", csr);
			omap24xxcam_reset(cam);
		}
	} else
		vb->state = VIDEOBUF_DONE;
	wake_up(&vb->done);
}

static void omap24xxcam_vbq_release(struct videobuf_queue *vbq,
				    struct videobuf_buffer *vb)
{
	struct videobuf_dmabuf *dma = videobuf_to_dma(vb);

	/* wait for buffer, especially to get out of the sgdma queue */
	videobuf_waiton(vbq, vb, 0, 0);
	if (vb->memory == V4L2_MEMORY_MMAP) {
		dma_unmap_sg(vbq->dev, dma->sglist, dma->sglen,
			     dma->direction);
		dma->direction = DMA_NONE;
	} else {
		videobuf_dma_unmap(vbq->dev, videobuf_to_dma(vb));
		videobuf_dma_free(videobuf_to_dma(vb));
	}

	vb->state = VIDEOBUF_NEEDS_INIT;
}

/*
 * Limit the number of available kernel image capture buffers based on the
 * number requested, the currently selected image size, and the maximum
 * amount of memory permitted for kernel capture buffers.
 */
static int omap24xxcam_vbq_setup(struct videobuf_queue *vbq, unsigned int *cnt,
				 unsigned int *size)
{
	struct omap24xxcam_fh *fh = vbq->priv_data;

	if (*cnt <= 0)
		*cnt = VIDEO_MAX_FRAME;	/* supply a default number of buffers */

	if (*cnt > VIDEO_MAX_FRAME)
		*cnt = VIDEO_MAX_FRAME;

	*size = fh->pix.sizeimage;

	/* accessing fh->cam->capture_mem is ok, it's constant */
	if (*size * *cnt > fh->cam->capture_mem)
		*cnt = fh->cam->capture_mem / *size;

	return 0;
}

static int omap24xxcam_dma_iolock(struct videobuf_queue *vbq,
				  struct videobuf_dmabuf *dma)
{
	int err = 0;

	dma->direction = PCI_DMA_FROMDEVICE;
	if (!dma_map_sg(vbq->dev, dma->sglist, dma->sglen, dma->direction)) {
		kfree(dma->sglist);
		dma->sglist = NULL;
		dma->sglen = 0;
		err = -EIO;
	}

	return err;
}

static int omap24xxcam_vbq_prepare(struct videobuf_queue *vbq,
				   struct videobuf_buffer *vb,
				   enum v4l2_field field)
{
	struct omap24xxcam_fh *fh = vbq->priv_data;
	int err = 0;

	/*
	 * Accessing pix here is okay since it's constant while
	 * streaming is on (and we only get called then).
	 */
	if (vb->baddr) {
		/* This is a userspace buffer. */
		if (fh->pix.sizeimage > vb->bsize) {
			/* The buffer isn't big enough. */
			err = -EINVAL;
		} else
			vb->size = fh->pix.sizeimage;
	} else {
		if (vb->state != VIDEOBUF_NEEDS_INIT) {
			/*
			 * We have a kernel bounce buffer that has
			 * already been allocated.
			 */
			if (fh->pix.sizeimage > vb->size) {
				/*
				 * The image size has been changed to
				 * a larger size since this buffer was
				 * allocated, so we need to free and
				 * reallocate it.
				 */
				omap24xxcam_vbq_release(vbq, vb);
				vb->size = fh->pix.sizeimage;
			}
		} else {
			/* We need to allocate a new kernel bounce buffer. */
			vb->size = fh->pix.sizeimage;
		}
	}

	if (err)
		return err;

	vb->width = fh->pix.width;
	vb->height = fh->pix.height;
	vb->field = field;

	if (vb->state == VIDEOBUF_NEEDS_INIT) {
		if (vb->memory == V4L2_MEMORY_MMAP)
			/*
			 * we have built the scatter-gather list by ourself so
			 * do the scatter-gather mapping as well
			 */
			err = omap24xxcam_dma_iolock(vbq, videobuf_to_dma(vb));
		else
			err = videobuf_iolock(vbq, vb, NULL);
	}

	if (!err)
		vb->state = VIDEOBUF_PREPARED;
	else
		omap24xxcam_vbq_release(vbq, vb);

	return err;
}

static void omap24xxcam_vbq_queue(struct videobuf_queue *vbq,
				  struct videobuf_buffer *vb)
{
	struct omap24xxcam_fh *fh = vbq->priv_data;
	struct omap24xxcam_device *cam = fh->cam;
	enum videobuf_state state = vb->state;
	unsigned long flags;
	int err;

	/*
	 * FIXME: We're marking the buffer active since we have no
	 * pretty way of marking it active exactly when the
	 * scatter-gather transfer starts.
	 */
	vb->state = VIDEOBUF_ACTIVE;

	err = omap24xxcam_sgdma_queue(&fh->cam->sgdma,
				      videobuf_to_dma(vb)->sglist,
				      videobuf_to_dma(vb)->sglen, vb->size,
				      omap24xxcam_vbq_complete, vb);

	if (!err) {
		spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
		if (++cam->sgdma_in_queue == 1
		    && !atomic_read(&cam->in_reset))
			omap24xxcam_core_enable(cam);
		spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
	} else {
		/*
		 * Oops. We're not supposed to get any errors here.
		 * The only way we could get an error is if we ran out
		 * of scatter-gather DMA slots, but we are supposed to
		 * have at least as many scatter-gather DMA slots as
		 * video buffers so that can't happen.
		 */
		dev_err(cam->dev, "failed to queue a video buffer for dma!\n");
		dev_err(cam->dev, "likely a bug in the driver!\n");
		vb->state = state;
	}
}

static struct videobuf_queue_ops omap24xxcam_vbq_ops = {
	.buf_setup   = omap24xxcam_vbq_setup,
	.buf_prepare = omap24xxcam_vbq_prepare,
	.buf_queue   = omap24xxcam_vbq_queue,
	.buf_release = omap24xxcam_vbq_release,
};

/*
 *
 * OMAP main camera system
 *
 */

/*
 * Reset camera block to power-on state.
 */
static void omap24xxcam_poweron_reset(struct omap24xxcam_device *cam)
{
	int max_loop = RESET_TIMEOUT_NS;

	/* Reset whole camera subsystem */
	omap24xxcam_reg_out(cam->mmio_base,
			    CAM_SYSCONFIG,
			    CAM_SYSCONFIG_SOFTRESET);

	/* Wait till it's finished */
	while (!(omap24xxcam_reg_in(cam->mmio_base, CAM_SYSSTATUS)
		 & CAM_SYSSTATUS_RESETDONE)
	       && --max_loop) {
		ndelay(1);
	}

	if (!(omap24xxcam_reg_in(cam->mmio_base, CAM_SYSSTATUS)
	      & CAM_SYSSTATUS_RESETDONE))
		dev_err(cam->dev, "camera soft reset timeout\n");
}

/*
 * (Re)initialise the camera block.
 */
static void omap24xxcam_hwinit(struct omap24xxcam_device *cam)
{
	omap24xxcam_poweron_reset(cam);

	/* set the camera subsystem autoidle bit */
	omap24xxcam_reg_out(cam->mmio_base, CAM_SYSCONFIG,
			    CAM_SYSCONFIG_AUTOIDLE);

	/* set the camera MMU autoidle bit */
	omap24xxcam_reg_out(cam->mmio_base,
			    CAMMMU_REG_OFFSET + CAMMMU_SYSCONFIG,
			    CAMMMU_SYSCONFIG_AUTOIDLE);

	omap24xxcam_core_hwinit(cam);

	omap24xxcam_dma_hwinit(&cam->sgdma.dma);
}

/*
 * Callback for dma transfer stalling.
 */
static void omap24xxcam_stalled_dma_reset(unsigned long data)
{
	struct omap24xxcam_device *cam = (struct omap24xxcam_device *)data;

	if (!atomic_read(&cam->in_reset)) {
		dev_dbg(cam->dev, "dma stalled, resetting camera\n");
		omap24xxcam_reset(cam);
	}
}

/*
 * Stop capture. Mark we're doing a reset, stop DMA transfers and
 * core. (No new scatter-gather transfers will be queued whilst
 * in_reset is non-zero.)
 *
 * If omap24xxcam_capture_stop is called from several places at
 * once, only the first call will have an effect. Similarly, the last
 * call omap24xxcam_streaming_cont will have effect.
 *
 * Serialisation is ensured by using cam->core_enable_disable_lock.
 */
static void omap24xxcam_capture_stop(struct omap24xxcam_device *cam)
{
	unsigned long flags;

	spin_lock_irqsave(&cam->core_enable_disable_lock, flags);

	if (atomic_inc_return(&cam->in_reset) != 1) {
		spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
		return;
	}

	omap24xxcam_core_disable(cam);

	spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);

	omap24xxcam_sgdma_sync(&cam->sgdma);
}

/*
 * Reset and continue streaming.
 *
 * Note: Resetting the camera FIFO via the CC_RST bit in the CC_CTRL
 * register is supposed to be sufficient to recover from a camera
 * interface error, but it doesn't seem to be enough. If we only do
 * that then subsequent image captures are out of sync by either one
 * or two times DMA_THRESHOLD bytes. Resetting and re-initializing the
 * entire camera subsystem prevents the problem with frame
 * synchronization.
 */
static void omap24xxcam_capture_cont(struct omap24xxcam_device *cam)
{
	unsigned long flags;

	spin_lock_irqsave(&cam->core_enable_disable_lock, flags);

	if (atomic_read(&cam->in_reset) != 1)
		goto out;

	omap24xxcam_hwinit(cam);

	omap24xxcam_sensor_if_enable(cam);

	omap24xxcam_sgdma_process(&cam->sgdma);

	if (cam->sgdma_in_queue)
		omap24xxcam_core_enable(cam);

out:
	atomic_dec(&cam->in_reset);
	spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
}

static ssize_t
omap24xxcam_streaming_show(struct device *dev, struct device_attribute *attr,
		char *buf)
{
	struct omap24xxcam_device *cam = dev_get_drvdata(dev);

	return sprintf(buf, "%s\n", cam->streaming ?  "active" : "inactive");
}
static DEVICE_ATTR(streaming, S_IRUGO, omap24xxcam_streaming_show, NULL);

/*
 * Stop capture and restart it. I.e. reset the camera during use.
 */
static void omap24xxcam_reset(struct omap24xxcam_device *cam)
{
	omap24xxcam_capture_stop(cam);
	omap24xxcam_capture_cont(cam);
}

/*
 * The main interrupt handler.
 */
static irqreturn_t omap24xxcam_isr(int irq, void *arg)
{
	struct omap24xxcam_device *cam = (struct omap24xxcam_device *)arg;
	u32 irqstatus;
	unsigned int irqhandled = 0;

	irqstatus = omap24xxcam_reg_in(cam->mmio_base, CAM_IRQSTATUS);

	if (irqstatus &
	    (CAM_IRQSTATUS_DMA_IRQ2 | CAM_IRQSTATUS_DMA_IRQ1
	     | CAM_IRQSTATUS_DMA_IRQ0)) {
		omap24xxcam_dma_isr(&cam->sgdma.dma);
		irqhandled = 1;
	}
	if (irqstatus & CAM_IRQSTATUS_CC_IRQ) {
		omap24xxcam_core_isr(cam);
		irqhandled = 1;
	}
	if (irqstatus & CAM_IRQSTATUS_MMU_IRQ)
		dev_err(cam->dev, "unhandled camera MMU interrupt!\n");

	return IRQ_RETVAL(irqhandled);
}

/*
 *
 * Sensor handling.
 *
 */

/*
 * Enable the external sensor interface. Try to negotiate interface
 * parameters with the sensor and start using the new ones. The calls
 * to sensor_if_enable and sensor_if_disable need not to be balanced.
 */
static int omap24xxcam_sensor_if_enable(struct omap24xxcam_device *cam)
{
	int rval;
	struct v4l2_ifparm p;

	rval = vidioc_int_g_ifparm(cam->sdev, &p);
	if (rval) {
		dev_err(cam->dev, "vidioc_int_g_ifparm failed with %d\n", rval);
		return rval;
	}

	cam->if_type = p.if_type;

	cam->cc_ctrl = CC_CTRL_CC_EN;

	switch (p.if_type) {
	case V4L2_IF_TYPE_BT656:
		if (p.u.bt656.frame_start_on_rising_vs)
			cam->cc_ctrl |= CC_CTRL_NOBT_SYNCHRO;
		if (p.u.bt656.bt_sync_correct)
			cam->cc_ctrl |= CC_CTRL_BT_CORRECT;
		if (p.u.bt656.swap)
			cam->cc_ctrl |= CC_CTRL_PAR_ORDERCAM;
		if (p.u.bt656.latch_clk_inv)
			cam->cc_ctrl |= CC_CTRL_PAR_CLK_POL;
		if (p.u.bt656.nobt_hs_inv)
			cam->cc_ctrl |= CC_CTRL_NOBT_HS_POL;
		if (p.u.bt656.nobt_vs_inv)
			cam->cc_ctrl |= CC_CTRL_NOBT_VS_POL;

		switch (p.u.bt656.mode) {
		case V4L2_IF_TYPE_BT656_MODE_NOBT_8BIT:
			cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT8;
			break;
		case V4L2_IF_TYPE_BT656_MODE_NOBT_10BIT:
			cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT10;
			break;
		case V4L2_IF_TYPE_BT656_MODE_NOBT_12BIT:
			cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT12;
			break;
		case V4L2_IF_TYPE_BT656_MODE_BT_8BIT:
			cam->cc_ctrl |= CC_CTRL_PAR_MODE_BT8;
			break;
		case V4L2_IF_TYPE_BT656_MODE_BT_10BIT:
			cam->cc_ctrl |= CC_CTRL_PAR_MODE_BT10;
			break;
		default:
			dev_err(cam->dev,
				"bt656 interface mode %d not supported\n",
				p.u.bt656.mode);
			return -EINVAL;
		}
		/*
		 * The clock rate that the sensor wants has changed.
		 * We have to adjust the xclk from OMAP 2 side to
		 * match the sensor's wish as closely as possible.
		 */
		if (p.u.bt656.clock_curr != cam->if_u.bt656.xclk) {
			u32 xclk = p.u.bt656.clock_curr;
			u32 divisor;

			if (xclk == 0)
				return -EINVAL;

			if (xclk > CAM_MCLK)
				xclk = CAM_MCLK;

			divisor = CAM_MCLK / xclk;
			if (divisor * xclk < CAM_MCLK)
				divisor++;
			if (CAM_MCLK / divisor < p.u.bt656.clock_min
			    && divisor > 1)
				divisor--;
			if (divisor > 30)
				divisor = 30;

			xclk = CAM_MCLK / divisor;

			if (xclk < p.u.bt656.clock_min
			    || xclk > p.u.bt656.clock_max)
				return -EINVAL;

			cam->if_u.bt656.xclk = xclk;
		}
		omap24xxcam_core_xclk_set(cam, cam->if_u.bt656.xclk);
		break;
	default:
		/* FIXME: how about other interfaces? */
		dev_err(cam->dev, "interface type %d not supported\n",
			p.if_type);
		return -EINVAL;
	}

	return 0;
}

static void omap24xxcam_sensor_if_disable(const struct omap24xxcam_device *cam)
{
	switch (cam->if_type) {
	case V4L2_IF_TYPE_BT656:
		omap24xxcam_core_xclk_set(cam, 0);
		break;
	}
}

/*
 * Initialise the sensor hardware.
 */
static int omap24xxcam_sensor_init(struct omap24xxcam_device *cam)
{
	int err = 0;
	struct v4l2_int_device *sdev = cam->sdev;

	omap24xxcam_clock_on(cam);
	err = omap24xxcam_sensor_if_enable(cam);
	if (err) {
		dev_err(cam->dev, "sensor interface could not be enabled at "
			"initialisation, %d\n", err);
		cam->sdev = NULL;
		goto out;
	}

	/* power up sensor during sensor initialization */
	vidioc_int_s_power(sdev, 1);

	err = vidioc_int_dev_init(sdev);
	if (err) {
		dev_err(cam->dev, "cannot initialize sensor, error %d\n", err);
		/* Sensor init failed --- it's nonexistent to us! */
		cam->sdev = NULL;
		goto out;
	}

	dev_info(cam->dev, "sensor is %s\n", sdev->name);

out:
	omap24xxcam_sensor_if_disable(cam);
	omap24xxcam_clock_off(cam);

	vidioc_int_s_power(sdev, 0);

	return err;
}

static void omap24xxcam_sensor_exit(struct omap24xxcam_device *cam)
{
	if (cam->sdev)
		vidioc_int_dev_exit(cam->sdev);
}

static void omap24xxcam_sensor_disable(struct omap24xxcam_device *cam)
{
	omap24xxcam_sensor_if_disable(cam);
	omap24xxcam_clock_off(cam);
	vidioc_int_s_power(cam->sdev, 0);
}

/*
 * Power-up and configure camera sensor. It's ready for capturing now.
 */
static int omap24xxcam_sensor_enable(struct omap24xxcam_device *cam)
{
	int rval;

	omap24xxcam_clock_on(cam);

	omap24xxcam_sensor_if_enable(cam);

	rval = vidioc_int_s_power(cam->sdev, 1);
	if (rval)
		goto out;

	rval = vidioc_int_init(cam->sdev);
	if (rval)
		goto out;

	return 0;

out:
	omap24xxcam_sensor_disable(cam);

	return rval;
}

static void omap24xxcam_sensor_reset_work(struct work_struct *work)
{
	struct omap24xxcam_device *cam =
		container_of(work, struct omap24xxcam_device,
			     sensor_reset_work);

	if (atomic_read(&cam->reset_disable))
		return;

	omap24xxcam_capture_stop(cam);

	if (vidioc_int_reset(cam->sdev) == 0) {
		vidioc_int_init(cam->sdev);
	} else {
		/* Can't reset it by vidioc_int_reset. */
		omap24xxcam_sensor_disable(cam);
		omap24xxcam_sensor_enable(cam);
	}

	omap24xxcam_capture_cont(cam);
}

/*
 *
 * IOCTL interface.
 *
 */

static int vidioc_querycap(struct file *file, void *fh,
			   struct v4l2_capability *cap)
{
	struct omap24xxcam_fh *ofh = fh;
	struct omap24xxcam_device *cam = ofh->cam;

	strlcpy(cap->driver, CAM_NAME, sizeof(cap->driver));
	strlcpy(cap->card, cam->vfd->name, sizeof(cap->card));
	cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;

	return 0;
}

static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh,
				   struct v4l2_fmtdesc *f)
{
	struct omap24xxcam_fh *ofh = fh;
	struct omap24xxcam_device *cam = ofh->cam;
	int rval;

	rval = vidioc_int_enum_fmt_cap(cam->sdev, f);

	return rval;
}

static int vidioc_g_fmt_vid_cap(struct file *file, void *fh,
				struct v4l2_format *f)
{
	struct omap24xxcam_fh *ofh = fh;
	struct omap24xxcam_device *cam = ofh->cam;
	int rval;

	mutex_lock(&cam->mutex);
	rval = vidioc_int_g_fmt_cap(cam->sdev, f);
	mutex_unlock(&cam->mutex);

	return rval;
}

static int vidioc_s_fmt_vid_cap(struct file *file, void *fh,
				struct v4l2_format *f)
{
	struct omap24xxcam_fh *ofh = fh;
	struct omap24xxcam_device *cam = ofh->cam;
	int rval;

	mutex_lock(&cam->mutex);
	if (cam->streaming) {
		rval = -EBUSY;
		goto out;
	}

	rval = vidioc_int_s_fmt_cap(cam->sdev, f);

out:
	mutex_unlock(&cam->mutex);

	if (!rval) {
		mutex_lock(&ofh->vbq.vb_lock);
		ofh->pix = f->fmt.pix;
		mutex_unlock(&ofh->vbq.vb_lock);
	}

	memset(f, 0, sizeof(*f));
	vidioc_g_fmt_vid_cap(file, fh, f);

	return rval;
}

static int vidioc_try_fmt_vid_cap(struct file *file, void *fh,
				  struct v4l2_format *f)
{
	struct omap24xxcam_fh *ofh = fh;
	struct omap24xxcam_device *cam = ofh->cam;
	int rval;

	mutex_lock(&cam->mutex);
	rval = vidioc_int_try_fmt_cap(cam->sdev, f);
	mutex_unlock(&cam->mutex);

	return rval;
}

static int vidioc_reqbufs(struct file *file, void *fh,
			  struct v4l2_requestbuffers *b)
{
	struct omap24xxcam_fh *ofh = fh;
	struct omap24xxcam_device *cam = ofh->cam;
	int rval;

	mutex_lock(&cam->mutex);
	if (cam->streaming) {
		mutex_unlock(&cam->mutex);
		return -EBUSY;
	}

	omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq);
	mutex_unlock(&cam->mutex);

	rval = videobuf_reqbufs(&ofh->vbq, b);

	/*
	 * Either videobuf_reqbufs failed or the buffers are not
	 * memory-mapped (which would need special attention).
	 */
	if (rval < 0 || b->memory != V4L2_MEMORY_MMAP)
		goto out;

	rval = omap24xxcam_vbq_alloc_mmap_buffers(&ofh->vbq, rval);
	if (rval)
		omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq);

out:
	return rval;
}

static int vidioc_querybuf(struct file *file, void *fh,
			   struct v4l2_buffer *b)
{
	struct omap24xxcam_fh *ofh = fh;

	return videobuf_querybuf(&ofh->vbq, b);
}

static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
{
	struct omap24xxcam_fh *ofh = fh;

	return videobuf_qbuf(&ofh->vbq, b);
}

static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
{
	struct omap24xxcam_fh *ofh = fh;
	struct omap24xxcam_device *cam = ofh->cam;
	struct videobuf_buffer *vb;
	int rval;

videobuf_dqbuf_again:
	rval = videobuf_dqbuf(&ofh->vbq, b, file->f_flags & O_NONBLOCK);
	if (rval)
		goto out;

	vb = ofh->vbq.bufs[b->index];

	mutex_lock(&cam->mutex);
	/* _needs_reset returns -EIO if reset is required. */
	rval = vidioc_int_g_needs_reset(cam->sdev, (void *)vb->baddr);
	mutex_unlock(&cam->mutex);
	if (rval == -EIO)
		schedule_work(&cam->sensor_reset_work);
	else
		rval = 0;

out:
	/*
	 * This is a hack. We don't want to show -EIO to the user
	 * space. Requeue the buffer and try again if we're not doing
	 * this in non-blocking mode.
	 */
	if (rval == -EIO) {
		videobuf_qbuf(&ofh->vbq, b);
		if (!(file->f_flags & O_NONBLOCK))
			goto videobuf_dqbuf_again;
		/*
		 * We don't have a videobuf_buffer now --- maybe next
		 * time...
		 */
		rval = -EAGAIN;
	}

	return rval;
}

static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
{
	struct omap24xxcam_fh *ofh = fh;
	struct omap24xxcam_device *cam = ofh->cam;
	int rval;

	mutex_lock(&cam->mutex);
	if (cam->streaming) {
		rval = -EBUSY;
		goto out;
	}

	rval = omap24xxcam_sensor_if_enable(cam);
	if (rval) {
		dev_dbg(cam->dev, "vidioc_int_g_ifparm failed\n");
		goto out;
	}

	rval = videobuf_streamon(&ofh->vbq);
	if (!rval) {
		cam->streaming = file;
		sysfs_notify(&cam->dev->kobj, NULL, "streaming");
	}

out:
	mutex_unlock(&cam->mutex);

	return rval;
}

static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
{
	struct omap24xxcam_fh *ofh = fh;
	struct omap24xxcam_device *cam = ofh->cam;
	struct videobuf_queue *q = &ofh->vbq;
	int rval;

	atomic_inc(&cam->reset_disable);

	flush_work_sync(&cam->sensor_reset_work);

	rval = videobuf_streamoff(q);
	if (!rval) {
		mutex_lock(&cam->mutex);
		cam->streaming = NULL;
		mutex_unlock(&cam->mutex);
		sysfs_notify(&cam->dev->kobj, NULL, "streaming");
	}

	atomic_dec(&cam->reset_disable);

	return rval;
}

static int vidioc_enum_input(struct file *file, void *fh,
			     struct v4l2_input *inp)
{
	if (inp->index > 0)
		return -EINVAL;

	strlcpy(inp->name, "camera", sizeof(inp->name));
	inp->type = V4L2_INPUT_TYPE_CAMERA;

	return 0;
}

static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
{
	*i = 0;

	return 0;
}

static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
{
	if (i > 0)
		return -EINVAL;

	return 0;
}

static int vidioc_queryctrl(struct file *file, void *fh,
			    struct v4l2_queryctrl *a)
{
	struct omap24xxcam_fh *ofh = fh;
	struct omap24xxcam_device *cam = ofh->cam;
	int rval;

	rval = vidioc_int_queryctrl(cam->sdev, a);

	return rval;
}

static int vidioc_g_ctrl(struct file *file, void *fh,
			 struct v4l2_control *a)
{
	struct omap24xxcam_fh *ofh = fh;
	struct omap24xxcam_device *cam = ofh->cam;
	int rval;

	mutex_lock(&cam->mutex);
	rval = vidioc_int_g_ctrl(cam->sdev, a);
	mutex_unlock(&cam->mutex);

	return rval;
}

static int vidioc_s_ctrl(struct file *file, void *fh,
			 struct v4l2_control *a)
{
	struct omap24xxcam_fh *ofh = fh;
	struct omap24xxcam_device *cam = ofh->cam;
	int rval;

	mutex_lock(&cam->mutex);
	rval = vidioc_int_s_ctrl(cam->sdev, a);
	mutex_unlock(&cam->mutex);

	return rval;
}

static int vidioc_g_parm(struct file *file, void *fh,
			 struct v4l2_streamparm *a) {
	struct omap24xxcam_fh *ofh = fh;
	struct omap24xxcam_device *cam = ofh->cam;
	int rval;

	mutex_lock(&cam->mutex);
	rval = vidioc_int_g_parm(cam->sdev, a);
	mutex_unlock(&cam->mutex);

	return rval;
}

static int vidioc_s_parm(struct file *file, void *fh,
			 struct v4l2_streamparm *a)
{
	struct omap24xxcam_fh *ofh = fh;
	struct omap24xxcam_device *cam = ofh->cam;
	struct v4l2_streamparm old_streamparm;
	int rval;

	mutex_lock(&cam->mutex);
	if (cam->streaming) {
		rval = -EBUSY;
		goto out;
	}

	old_streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	rval = vidioc_int_g_parm(cam->sdev, &old_streamparm);
	if (rval)
		goto out;

	rval = vidioc_int_s_parm(cam->sdev, a);
	if (rval)
		goto out;

	rval = omap24xxcam_sensor_if_enable(cam);
	/*
	 * Revert to old streaming parameters if enabling sensor
	 * interface with the new ones failed.
	 */
	if (rval)
		vidioc_int_s_parm(cam->sdev, &old_streamparm);

out:
	mutex_unlock(&cam->mutex);

	return rval;
}

/*
 *
 * File operations.
 *
 */

static unsigned int omap24xxcam_poll(struct file *file,
				     struct poll_table_struct *wait)
{
	struct omap24xxcam_fh *fh = file->private_data;
	struct omap24xxcam_device *cam = fh->cam;
	struct videobuf_buffer *vb;

	mutex_lock(&cam->mutex);
	if (cam->streaming != file) {
		mutex_unlock(&cam->mutex);
		return POLLERR;
	}
	mutex_unlock(&cam->mutex);

	mutex_lock(&fh->vbq.vb_lock);
	if (list_empty(&fh->vbq.stream)) {
		mutex_unlock(&fh->vbq.vb_lock);
		return POLLERR;
	}
	vb = list_entry(fh->vbq.stream.next, struct videobuf_buffer, stream);
	mutex_unlock(&fh->vbq.vb_lock);

	poll_wait(file, &vb->done, wait);

	if (vb->state == VIDEOBUF_DONE || vb->state == VIDEOBUF_ERROR)
		return POLLIN | POLLRDNORM;

	return 0;
}

static int omap24xxcam_mmap_buffers(struct file *file,
				    struct vm_area_struct *vma)
{
	struct omap24xxcam_fh *fh = file->private_data;
	struct omap24xxcam_device *cam = fh->cam;
	struct videobuf_queue *vbq = &fh->vbq;
	unsigned int first, last, size, i, j;
	int err = 0;

	mutex_lock(&cam->mutex);
	if (cam->streaming) {
		mutex_unlock(&cam->mutex);
		return -EBUSY;
	}
	mutex_unlock(&cam->mutex);
	mutex_lock(&vbq->vb_lock);

	/* look for first buffer to map */
	for (first = 0; first < VIDEO_MAX_FRAME; first++) {
		if (NULL == vbq->bufs[first])
			continue;
		if (V4L2_MEMORY_MMAP != vbq->bufs[first]->memory)
			continue;
		if (vbq->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
			break;
	}

	/* look for last buffer to map */
	for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
		if (NULL == vbq->bufs[last])
			continue;
		if (V4L2_MEMORY_MMAP != vbq->bufs[last]->memory)
			continue;
		size += vbq->bufs[last]->bsize;
		if (size == (vma->vm_end - vma->vm_start))
			break;
	}

	size = 0;
	for (i = first; i <= last && i < VIDEO_MAX_FRAME; i++) {
		struct videobuf_dmabuf *dma = videobuf_to_dma(vbq->bufs[i]);

		for (j = 0; j < dma->sglen; j++) {
			err = remap_pfn_range(
				vma, vma->vm_start + size,
				page_to_pfn(sg_page(&dma->sglist[j])),
				sg_dma_len(&dma->sglist[j]), vma->vm_page_prot);
			if (err)
				goto out;
			size += sg_dma_len(&dma->sglist[j]);
		}
	}

out:
	mutex_unlock(&vbq->vb_lock);

	return err;
}

static int omap24xxcam_mmap(struct file *file, struct vm_area_struct *vma)
{
	struct omap24xxcam_fh *fh = file->private_data;
	int rval;

	/* let the video-buf mapper check arguments and set-up structures */
	rval = videobuf_mmap_mapper(&fh->vbq, vma);
	if (rval)
		return rval;

	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);

	/* do mapping to our allocated buffers */
	rval = omap24xxcam_mmap_buffers(file, vma);
	/*
	 * In case of error, free vma->vm_private_data allocated by
	 * videobuf_mmap_mapper.
	 */
	if (rval)
		kfree(vma->vm_private_data);

	return rval;
}

static int omap24xxcam_open(struct file *file)
{
	struct omap24xxcam_device *cam = omap24xxcam.priv;
	struct omap24xxcam_fh *fh;
	struct v4l2_format format;

	if (!cam || !cam->vfd)
		return -ENODEV;

	fh = kzalloc(sizeof(*fh), GFP_KERNEL);
	if (fh == NULL)
		return -ENOMEM;

	mutex_lock(&cam->mutex);
	if (cam->sdev == NULL || !try_module_get(cam->sdev->module)) {
		mutex_unlock(&cam->mutex);
		goto out_try_module_get;
	}

	if (atomic_inc_return(&cam->users) == 1) {
		omap24xxcam_hwinit(cam);
		if (omap24xxcam_sensor_enable(cam)) {
			mutex_unlock(&cam->mutex);
			goto out_omap24xxcam_sensor_enable;
		}
	}
	mutex_unlock(&cam->mutex);

	fh->cam = cam;
	mutex_lock(&cam->mutex);
	vidioc_int_g_fmt_cap(cam->sdev, &format);
	mutex_unlock(&cam->mutex);
	/* FIXME: how about fh->pix when there are more users? */
	fh->pix = format.fmt.pix;

	file->private_data = fh;

	spin_lock_init(&fh->vbq_lock);

	videobuf_queue_sg_init(&fh->vbq, &omap24xxcam_vbq_ops, NULL,
				&fh->vbq_lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
				V4L2_FIELD_NONE,
				sizeof(struct videobuf_buffer), fh, NULL);

	return 0;

out_omap24xxcam_sensor_enable:
	omap24xxcam_poweron_reset(cam);
	module_put(cam->sdev->module);

out_try_module_get:
	kfree(fh);

	return -ENODEV;
}

static int omap24xxcam_release(struct file *file)
{
	struct omap24xxcam_fh *fh = file->private_data;
	struct omap24xxcam_device *cam = fh->cam;

	atomic_inc(&cam->reset_disable);

	flush_work_sync(&cam->sensor_reset_work);

	/* stop streaming capture */
	videobuf_streamoff(&fh->vbq);

	mutex_lock(&cam->mutex);
	if (cam->streaming == file) {
		cam->streaming = NULL;
		mutex_unlock(&cam->mutex);
		sysfs_notify(&cam->dev->kobj, NULL, "streaming");
	} else {
		mutex_unlock(&cam->mutex);
	}

	atomic_dec(&cam->reset_disable);

	omap24xxcam_vbq_free_mmap_buffers(&fh->vbq);

	/*
	 * Make sure the reset work we might have scheduled is not
	 * pending! It may be run *only* if we have users. (And it may
	 * not be scheduled anymore since streaming is already
	 * disabled.)
	 */
	flush_work_sync(&cam->sensor_reset_work);

	mutex_lock(&cam->mutex);
	if (atomic_dec_return(&cam->users) == 0) {
		omap24xxcam_sensor_disable(cam);
		omap24xxcam_poweron_reset(cam);
	}
	mutex_unlock(&cam->mutex);

	file->private_data = NULL;

	module_put(cam->sdev->module);
	kfree(fh);

	return 0;
}

static struct v4l2_file_operations omap24xxcam_fops = {
	.ioctl	 = video_ioctl2,
	.poll	 = omap24xxcam_poll,
	.mmap	 = omap24xxcam_mmap,
	.open	 = omap24xxcam_open,
	.release = omap24xxcam_release,
};

/*
 *
 * Power management.
 *
 */

#ifdef CONFIG_PM
static int omap24xxcam_suspend(struct platform_device *pdev, pm_message_t state)
{
	struct omap24xxcam_device *cam = platform_get_drvdata(pdev);

	if (atomic_read(&cam->users) == 0)
		return 0;

	if (!atomic_read(&cam->reset_disable))
		omap24xxcam_capture_stop(cam);

	omap24xxcam_sensor_disable(cam);
	omap24xxcam_poweron_reset(cam);

	return 0;
}

static int omap24xxcam_resume(struct platform_device *pdev)
{
	struct omap24xxcam_device *cam = platform_get_drvdata(pdev);

	if (atomic_read(&cam->users) == 0)
		return 0;

	omap24xxcam_hwinit(cam);
	omap24xxcam_sensor_enable(cam);

	if (!atomic_read(&cam->reset_disable))
		omap24xxcam_capture_cont(cam);

	return 0;
}
#endif /* CONFIG_PM */

static const struct v4l2_ioctl_ops omap24xxcam_ioctl_fops = {
	.vidioc_querycap	= vidioc_querycap,
	.vidioc_enum_fmt_vid_cap	= vidioc_enum_fmt_vid_cap,
	.vidioc_g_fmt_vid_cap	= vidioc_g_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap	= vidioc_s_fmt_vid_cap,
	.vidioc_try_fmt_vid_cap	= vidioc_try_fmt_vid_cap,
	.vidioc_reqbufs		= vidioc_reqbufs,
	.vidioc_querybuf	= vidioc_querybuf,
	.vidioc_qbuf		= vidioc_qbuf,
	.vidioc_dqbuf		= vidioc_dqbuf,
	.vidioc_streamon	= vidioc_streamon,
	.vidioc_streamoff	= vidioc_streamoff,
	.vidioc_enum_input	= vidioc_enum_input,
	.vidioc_g_input		= vidioc_g_input,
	.vidioc_s_input		= vidioc_s_input,
	.vidioc_queryctrl	= vidioc_queryctrl,
	.vidioc_g_ctrl		= vidioc_g_ctrl,
	.vidioc_s_ctrl		= vidioc_s_ctrl,
	.vidioc_g_parm		= vidioc_g_parm,
	.vidioc_s_parm		= vidioc_s_parm,
};

/*
 *
 * Camera device (i.e. /dev/video).
 *
 */

static int omap24xxcam_device_register(struct v4l2_int_device *s)
{
	struct omap24xxcam_device *cam = s->u.slave->master->priv;
	struct video_device *vfd;
	int rval;

	/* We already have a slave. */
	if (cam->sdev)
		return -EBUSY;

	cam->sdev = s;

	if (device_create_file(cam->dev, &dev_attr_streaming) != 0) {
		dev_err(cam->dev, "could not register sysfs entry\n");
		rval = -EBUSY;
		goto err;
	}

	/* initialize the video_device struct */
	vfd = cam->vfd = video_device_alloc();
	if (!vfd) {
		dev_err(cam->dev, "could not allocate video device struct\n");
		rval = -ENOMEM;
		goto err;
	}
	vfd->release = video_device_release;

	vfd->parent = cam->dev;

	strlcpy(vfd->name, CAM_NAME, sizeof(vfd->name));
	vfd->fops		 = &omap24xxcam_fops;
	vfd->ioctl_ops		 = &omap24xxcam_ioctl_fops;

	omap24xxcam_hwinit(cam);

	rval = omap24xxcam_sensor_init(cam);
	if (rval)
		goto err;

	if (video_register_device(vfd, VFL_TYPE_GRABBER, video_nr) < 0) {
		dev_err(cam->dev, "could not register V4L device\n");
		rval = -EBUSY;
		goto err;
	}

	omap24xxcam_poweron_reset(cam);

	dev_info(cam->dev, "registered device %s\n",
		 video_device_node_name(vfd));

	return 0;

err:
	omap24xxcam_device_unregister(s);

	return rval;
}

static void omap24xxcam_device_unregister(struct v4l2_int_device *s)
{
	struct omap24xxcam_device *cam = s->u.slave->master->priv;

	omap24xxcam_sensor_exit(cam);

	if (cam->vfd) {
		if (!video_is_registered(cam->vfd)) {
			/*
			 * The device was never registered, so release the
			 * video_device struct directly.
			 */
			video_device_release(cam->vfd);
		} else {
			/*
			 * The unregister function will release the
			 * video_device struct as well as
			 * unregistering it.
			 */
			video_unregister_device(cam->vfd);
		}
		cam->vfd = NULL;
	}

	device_remove_file(cam->dev, &dev_attr_streaming);

	cam->sdev = NULL;
}

static struct v4l2_int_master omap24xxcam_master = {
	.attach = omap24xxcam_device_register,
	.detach = omap24xxcam_device_unregister,
};

static struct v4l2_int_device omap24xxcam = {
	.module	= THIS_MODULE,
	.name	= CAM_NAME,
	.type	= v4l2_int_type_master,
	.u	= {
		.master = &omap24xxcam_master
	},
};

/*
 *
 * Driver initialisation and deinitialisation.
 *
 */

static int __devinit omap24xxcam_probe(struct platform_device *pdev)
{
	struct omap24xxcam_device *cam;
	struct resource *mem;
	int irq;

	cam = kzalloc(sizeof(*cam), GFP_KERNEL);
	if (!cam) {
		dev_err(&pdev->dev, "could not allocate memory\n");
		goto err;
	}

	platform_set_drvdata(pdev, cam);

	cam->dev = &pdev->dev;

	/*
	 * Impose a lower limit on the amount of memory allocated for
	 * capture. We require at least enough memory to double-buffer
	 * QVGA (300KB).
	 */
	if (capture_mem < 320 * 240 * 2 * 2)
		capture_mem = 320 * 240 * 2 * 2;
	cam->capture_mem = capture_mem;

	/* request the mem region for the camera registers */
	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!mem) {
		dev_err(cam->dev, "no mem resource?\n");
		goto err;
	}
	if (!request_mem_region(mem->start, resource_size(mem), pdev->name)) {
		dev_err(cam->dev,
			"cannot reserve camera register I/O region\n");
		goto err;
	}
	cam->mmio_base_phys = mem->start;
	cam->mmio_size = resource_size(mem);

	/* map the region */
	cam->mmio_base = (unsigned long)
		ioremap_nocache(cam->mmio_base_phys, cam->mmio_size);
	if (!cam->mmio_base) {
		dev_err(cam->dev, "cannot map camera register I/O region\n");
		goto err;
	}

	irq = platform_get_irq(pdev, 0);
	if (irq <= 0) {
		dev_err(cam->dev, "no irq for camera?\n");
		goto err;
	}

	/* install the interrupt service routine */
	if (request_irq(irq, omap24xxcam_isr, 0, CAM_NAME, cam)) {
		dev_err(cam->dev,
			"could not install interrupt service routine\n");
		goto err;
	}
	cam->irq = irq;

	if (omap24xxcam_clock_get(cam))
		goto err;

	INIT_WORK(&cam->sensor_reset_work, omap24xxcam_sensor_reset_work);

	mutex_init(&cam->mutex);
	spin_lock_init(&cam->core_enable_disable_lock);

	omap24xxcam_sgdma_init(&cam->sgdma,
			       cam->mmio_base + CAMDMA_REG_OFFSET,
			       omap24xxcam_stalled_dma_reset,
			       (unsigned long)cam);

	omap24xxcam.priv = cam;

	if (v4l2_int_device_register(&omap24xxcam))
		goto err;

	return 0;

err:
	omap24xxcam_remove(pdev);
	return -ENODEV;
}

static int omap24xxcam_remove(struct platform_device *pdev)
{
	struct omap24xxcam_device *cam = platform_get_drvdata(pdev);

	if (!cam)
		return 0;

	if (omap24xxcam.priv != NULL)
		v4l2_int_device_unregister(&omap24xxcam);
	omap24xxcam.priv = NULL;

	omap24xxcam_clock_put(cam);

	if (cam->irq) {
		free_irq(cam->irq, cam);
		cam->irq = 0;
	}

	if (cam->mmio_base) {
		iounmap((void *)cam->mmio_base);
		cam->mmio_base = 0;
	}

	if (cam->mmio_base_phys) {
		release_mem_region(cam->mmio_base_phys, cam->mmio_size);
		cam->mmio_base_phys = 0;
	}

	kfree(cam);

	return 0;
}

static struct platform_driver omap24xxcam_driver = {
	.probe	 = omap24xxcam_probe,
	.remove	 = omap24xxcam_remove,
#ifdef CONFIG_PM
	.suspend = omap24xxcam_suspend,
	.resume	 = omap24xxcam_resume,
#endif
	.driver	 = {
		.name = CAM_NAME,
		.owner = THIS_MODULE,
	},
};

module_platform_driver(omap24xxcam_driver);

MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
MODULE_DESCRIPTION("OMAP24xx Video for Linux camera driver");
MODULE_LICENSE("GPL");
MODULE_VERSION(OMAP24XXCAM_VERSION);
module_param(video_nr, int, 0);
MODULE_PARM_DESC(video_nr,
		 "Minor number for video device (-1 ==> auto assign)");
module_param(capture_mem, int, 0);
MODULE_PARM_DESC(capture_mem, "Maximum amount of memory for capture "
		 "buffers (default 4800kiB)");