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 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 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 | // SPDX-License-Identifier: GPL-2.0 /* * Driver for STM32 Digital Camera Memory Interface * * Copyright (C) STMicroelectronics SA 2017 * Authors: Yannick Fertre <yannick.fertre@st.com> * Hugues Fruchet <hugues.fruchet@st.com> * for STMicroelectronics. * * This driver is based on atmel_isi.c * */ #include <linux/clk.h> #include <linux/completion.h> #include <linux/delay.h> #include <linux/dmaengine.h> #include <linux/init.h> #include <linux/interrupt.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/of.h> #include <linux/of_device.h> #include <linux/of_graph.h> #include <linux/pinctrl/consumer.h> #include <linux/platform_device.h> #include <linux/pm_runtime.h> #include <linux/reset.h> #include <linux/videodev2.h> #include <media/v4l2-ctrls.h> #include <media/v4l2-dev.h> #include <media/v4l2-device.h> #include <media/v4l2-event.h> #include <media/v4l2-fwnode.h> #include <media/v4l2-image-sizes.h> #include <media/v4l2-ioctl.h> #include <media/v4l2-rect.h> #include <media/videobuf2-dma-contig.h> #define DRV_NAME "stm32-dcmi" /* Registers offset for DCMI */ #define DCMI_CR 0x00 /* Control Register */ #define DCMI_SR 0x04 /* Status Register */ #define DCMI_RIS 0x08 /* Raw Interrupt Status register */ #define DCMI_IER 0x0C /* Interrupt Enable Register */ #define DCMI_MIS 0x10 /* Masked Interrupt Status register */ #define DCMI_ICR 0x14 /* Interrupt Clear Register */ #define DCMI_ESCR 0x18 /* Embedded Synchronization Code Register */ #define DCMI_ESUR 0x1C /* Embedded Synchronization Unmask Register */ #define DCMI_CWSTRT 0x20 /* Crop Window STaRT */ #define DCMI_CWSIZE 0x24 /* Crop Window SIZE */ #define DCMI_DR 0x28 /* Data Register */ #define DCMI_IDR 0x2C /* IDentifier Register */ /* Bits definition for control register (DCMI_CR) */ #define CR_CAPTURE BIT(0) #define CR_CM BIT(1) #define CR_CROP BIT(2) #define CR_JPEG BIT(3) #define CR_ESS BIT(4) #define CR_PCKPOL BIT(5) #define CR_HSPOL BIT(6) #define CR_VSPOL BIT(7) #define CR_FCRC_0 BIT(8) #define CR_FCRC_1 BIT(9) #define CR_EDM_0 BIT(10) #define CR_EDM_1 BIT(11) #define CR_ENABLE BIT(14) /* Bits definition for status register (DCMI_SR) */ #define SR_HSYNC BIT(0) #define SR_VSYNC BIT(1) #define SR_FNE BIT(2) /* * Bits definition for interrupt registers * (DCMI_RIS, DCMI_IER, DCMI_MIS, DCMI_ICR) */ #define IT_FRAME BIT(0) #define IT_OVR BIT(1) #define IT_ERR BIT(2) #define IT_VSYNC BIT(3) #define IT_LINE BIT(4) enum state { STOPPED = 0, WAIT_FOR_BUFFER, RUNNING, }; #define MIN_WIDTH 16U #define MAX_WIDTH 2592U #define MIN_HEIGHT 16U #define MAX_HEIGHT 2592U #define TIMEOUT_MS 1000 #define OVERRUN_ERROR_THRESHOLD 3 struct dcmi_format { u32 fourcc; u32 mbus_code; u8 bpp; }; struct dcmi_framesize { u32 width; u32 height; }; struct dcmi_buf { struct vb2_v4l2_buffer vb; bool prepared; struct sg_table sgt; size_t size; struct list_head list; }; struct stm32_dcmi { /* Protects the access of variables shared within the interrupt */ spinlock_t irqlock; struct device *dev; void __iomem *regs; struct resource *res; struct reset_control *rstc; int sequence; struct list_head buffers; struct dcmi_buf *active; int irq; struct v4l2_device v4l2_dev; struct video_device *vdev; struct v4l2_async_notifier notifier; struct v4l2_subdev *source; struct v4l2_format fmt; struct v4l2_rect crop; bool do_crop; const struct dcmi_format **sd_formats; unsigned int num_of_sd_formats; const struct dcmi_format *sd_format; struct dcmi_framesize *sd_framesizes; unsigned int num_of_sd_framesizes; struct dcmi_framesize sd_framesize; struct v4l2_rect sd_bounds; /* Protect this data structure */ struct mutex lock; struct vb2_queue queue; struct v4l2_mbus_config_parallel bus; enum v4l2_mbus_type bus_type; struct completion complete; struct clk *mclk; enum state state; struct dma_chan *dma_chan; dma_cookie_t dma_cookie; u32 dma_max_burst; u32 misr; int errors_count; int overrun_count; int buffers_count; /* Ensure DMA operations atomicity */ struct mutex dma_lock; struct media_device mdev; struct media_pad vid_cap_pad; struct media_pipeline pipeline; }; static inline struct stm32_dcmi *notifier_to_dcmi(struct v4l2_async_notifier *n) { return container_of(n, struct stm32_dcmi, notifier); } static inline u32 reg_read(void __iomem *base, u32 reg) { return readl_relaxed(base + reg); } static inline void reg_write(void __iomem *base, u32 reg, u32 val) { writel_relaxed(val, base + reg); } static inline void reg_set(void __iomem *base, u32 reg, u32 mask) { reg_write(base, reg, reg_read(base, reg) | mask); } static inline void reg_clear(void __iomem *base, u32 reg, u32 mask) { reg_write(base, reg, reg_read(base, reg) & ~mask); } static int dcmi_start_capture(struct stm32_dcmi *dcmi, struct dcmi_buf *buf); static void dcmi_buffer_done(struct stm32_dcmi *dcmi, struct dcmi_buf *buf, size_t bytesused, int err) { struct vb2_v4l2_buffer *vbuf; if (!buf) return; list_del_init(&buf->list); vbuf = &buf->vb; vbuf->sequence = dcmi->sequence++; vbuf->field = V4L2_FIELD_NONE; vbuf->vb2_buf.timestamp = ktime_get_ns(); vb2_set_plane_payload(&vbuf->vb2_buf, 0, bytesused); vb2_buffer_done(&vbuf->vb2_buf, err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); dev_dbg(dcmi->dev, "buffer[%d] done seq=%d, bytesused=%zu\n", vbuf->vb2_buf.index, vbuf->sequence, bytesused); dcmi->buffers_count++; dcmi->active = NULL; } static int dcmi_restart_capture(struct stm32_dcmi *dcmi) { struct dcmi_buf *buf; spin_lock_irq(&dcmi->irqlock); if (dcmi->state != RUNNING) { spin_unlock_irq(&dcmi->irqlock); return -EINVAL; } /* Restart a new DMA transfer with next buffer */ if (list_empty(&dcmi->buffers)) { dev_dbg(dcmi->dev, "Capture restart is deferred to next buffer queueing\n"); dcmi->state = WAIT_FOR_BUFFER; spin_unlock_irq(&dcmi->irqlock); return 0; } buf = list_entry(dcmi->buffers.next, struct dcmi_buf, list); dcmi->active = buf; spin_unlock_irq(&dcmi->irqlock); return dcmi_start_capture(dcmi, buf); } static void dcmi_dma_callback(void *param) { struct stm32_dcmi *dcmi = (struct stm32_dcmi *)param; struct dma_tx_state state; enum dma_status status; struct dcmi_buf *buf = dcmi->active; spin_lock_irq(&dcmi->irqlock); /* Check DMA status */ status = dmaengine_tx_status(dcmi->dma_chan, dcmi->dma_cookie, &state); switch (status) { case DMA_IN_PROGRESS: dev_dbg(dcmi->dev, "%s: Received DMA_IN_PROGRESS\n", __func__); break; case DMA_PAUSED: dev_err(dcmi->dev, "%s: Received DMA_PAUSED\n", __func__); break; case DMA_ERROR: dev_err(dcmi->dev, "%s: Received DMA_ERROR\n", __func__); /* Return buffer to V4L2 in error state */ dcmi_buffer_done(dcmi, buf, 0, -EIO); break; case DMA_COMPLETE: dev_dbg(dcmi->dev, "%s: Received DMA_COMPLETE\n", __func__); /* Return buffer to V4L2 */ dcmi_buffer_done(dcmi, buf, buf->size, 0); spin_unlock_irq(&dcmi->irqlock); /* Restart capture */ if (dcmi_restart_capture(dcmi)) dev_err(dcmi->dev, "%s: Cannot restart capture on DMA complete\n", __func__); return; default: dev_err(dcmi->dev, "%s: Received unknown status\n", __func__); break; } spin_unlock_irq(&dcmi->irqlock); } static int dcmi_start_dma(struct stm32_dcmi *dcmi, struct dcmi_buf *buf) { struct dma_async_tx_descriptor *desc = NULL; struct dma_slave_config config; int ret; memset(&config, 0, sizeof(config)); config.src_addr = (dma_addr_t)dcmi->res->start + DCMI_DR; config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; config.dst_maxburst = 4; /* Configure DMA channel */ ret = dmaengine_slave_config(dcmi->dma_chan, &config); if (ret < 0) { dev_err(dcmi->dev, "%s: DMA channel config failed (%d)\n", __func__, ret); return ret; } /* * Avoid call of dmaengine_terminate_sync() between * dmaengine_prep_slave_single() and dmaengine_submit() * by locking the whole DMA submission sequence */ mutex_lock(&dcmi->dma_lock); /* Prepare a DMA transaction */ desc = dmaengine_prep_slave_sg(dcmi->dma_chan, buf->sgt.sgl, buf->sgt.nents, DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT); if (!desc) { dev_err(dcmi->dev, "%s: DMA dmaengine_prep_slave_sg failed\n", __func__); mutex_unlock(&dcmi->dma_lock); return -EINVAL; } /* Set completion callback routine for notification */ desc->callback = dcmi_dma_callback; desc->callback_param = dcmi; /* Push current DMA transaction in the pending queue */ dcmi->dma_cookie = dmaengine_submit(desc); if (dma_submit_error(dcmi->dma_cookie)) { dev_err(dcmi->dev, "%s: DMA submission failed\n", __func__); mutex_unlock(&dcmi->dma_lock); return -ENXIO; } mutex_unlock(&dcmi->dma_lock); dma_async_issue_pending(dcmi->dma_chan); return 0; } static int dcmi_start_capture(struct stm32_dcmi *dcmi, struct dcmi_buf *buf) { int ret; if (!buf) return -EINVAL; ret = dcmi_start_dma(dcmi, buf); if (ret) { dcmi->errors_count++; return ret; } /* Enable capture */ reg_set(dcmi->regs, DCMI_CR, CR_CAPTURE); return 0; } static void dcmi_set_crop(struct stm32_dcmi *dcmi) { u32 size, start; /* Crop resolution */ size = ((dcmi->crop.height - 1) << 16) | ((dcmi->crop.width << 1) - 1); reg_write(dcmi->regs, DCMI_CWSIZE, size); /* Crop start point */ start = ((dcmi->crop.top) << 16) | ((dcmi->crop.left << 1)); reg_write(dcmi->regs, DCMI_CWSTRT, start); dev_dbg(dcmi->dev, "Cropping to %ux%u@%u:%u\n", dcmi->crop.width, dcmi->crop.height, dcmi->crop.left, dcmi->crop.top); /* Enable crop */ reg_set(dcmi->regs, DCMI_CR, CR_CROP); } static void dcmi_process_jpeg(struct stm32_dcmi *dcmi) { struct dma_tx_state state; enum dma_status status; struct dcmi_buf *buf = dcmi->active; if (!buf) return; /* * Because of variable JPEG buffer size sent by sensor, * DMA transfer never completes due to transfer size never reached. * In order to ensure that all the JPEG data are transferred * in active buffer memory, DMA is drained. * Then DMA tx status gives the amount of data transferred * to memory, which is then returned to V4L2 through the active * buffer payload. */ /* Drain DMA */ dmaengine_synchronize(dcmi->dma_chan); /* Get DMA residue to get JPEG size */ status = dmaengine_tx_status(dcmi->dma_chan, dcmi->dma_cookie, &state); if (status != DMA_ERROR && state.residue < buf->size) { /* Return JPEG buffer to V4L2 with received JPEG buffer size */ dcmi_buffer_done(dcmi, buf, buf->size - state.residue, 0); } else { dcmi->errors_count++; dev_err(dcmi->dev, "%s: Cannot get JPEG size from DMA\n", __func__); /* Return JPEG buffer to V4L2 in ERROR state */ dcmi_buffer_done(dcmi, buf, 0, -EIO); } /* Abort DMA operation */ dmaengine_terminate_sync(dcmi->dma_chan); /* Restart capture */ if (dcmi_restart_capture(dcmi)) dev_err(dcmi->dev, "%s: Cannot restart capture on JPEG received\n", __func__); } static irqreturn_t dcmi_irq_thread(int irq, void *arg) { struct stm32_dcmi *dcmi = arg; spin_lock_irq(&dcmi->irqlock); if (dcmi->misr & IT_OVR) { dcmi->overrun_count++; if (dcmi->overrun_count > OVERRUN_ERROR_THRESHOLD) dcmi->errors_count++; } if (dcmi->misr & IT_ERR) dcmi->errors_count++; if (dcmi->sd_format->fourcc == V4L2_PIX_FMT_JPEG && dcmi->misr & IT_FRAME) { /* JPEG received */ spin_unlock_irq(&dcmi->irqlock); dcmi_process_jpeg(dcmi); return IRQ_HANDLED; } spin_unlock_irq(&dcmi->irqlock); return IRQ_HANDLED; } static irqreturn_t dcmi_irq_callback(int irq, void *arg) { struct stm32_dcmi *dcmi = arg; unsigned long flags; spin_lock_irqsave(&dcmi->irqlock, flags); dcmi->misr = reg_read(dcmi->regs, DCMI_MIS); /* Clear interrupt */ reg_set(dcmi->regs, DCMI_ICR, IT_FRAME | IT_OVR | IT_ERR); spin_unlock_irqrestore(&dcmi->irqlock, flags); return IRQ_WAKE_THREAD; } static int dcmi_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers, unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[]) { struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq); unsigned int size; size = dcmi->fmt.fmt.pix.sizeimage; /* Make sure the image size is large enough */ if (*nplanes) return sizes[0] < size ? -EINVAL : 0; *nplanes = 1; sizes[0] = size; dev_dbg(dcmi->dev, "Setup queue, count=%d, size=%d\n", *nbuffers, size); return 0; } static int dcmi_buf_init(struct vb2_buffer *vb) { struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); struct dcmi_buf *buf = container_of(vbuf, struct dcmi_buf, vb); INIT_LIST_HEAD(&buf->list); return 0; } static int dcmi_buf_prepare(struct vb2_buffer *vb) { struct stm32_dcmi *dcmi = vb2_get_drv_priv(vb->vb2_queue); struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); struct dcmi_buf *buf = container_of(vbuf, struct dcmi_buf, vb); unsigned long size; unsigned int num_sgs = 1; dma_addr_t dma_buf; struct scatterlist *sg; int i, ret; size = dcmi->fmt.fmt.pix.sizeimage; if (vb2_plane_size(vb, 0) < size) { dev_err(dcmi->dev, "%s data will not fit into plane (%lu < %lu)\n", __func__, vb2_plane_size(vb, 0), size); return -EINVAL; } vb2_set_plane_payload(vb, 0, size); if (!buf->prepared) { /* Get memory addresses */ buf->size = vb2_plane_size(&buf->vb.vb2_buf, 0); if (buf->size > dcmi->dma_max_burst) num_sgs = DIV_ROUND_UP(buf->size, dcmi->dma_max_burst); ret = sg_alloc_table(&buf->sgt, num_sgs, GFP_ATOMIC); if (ret) { dev_err(dcmi->dev, "sg table alloc failed\n"); return ret; } dma_buf = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0); dev_dbg(dcmi->dev, "buffer[%d] phy=%pad size=%zu\n", vb->index, &dma_buf, buf->size); for_each_sg(buf->sgt.sgl, sg, num_sgs, i) { size_t bytes = min_t(size_t, size, dcmi->dma_max_burst); sg_dma_address(sg) = dma_buf; sg_dma_len(sg) = bytes; dma_buf += bytes; size -= bytes; } buf->prepared = true; vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->size); } return 0; } static void dcmi_buf_queue(struct vb2_buffer *vb) { struct stm32_dcmi *dcmi = vb2_get_drv_priv(vb->vb2_queue); struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); struct dcmi_buf *buf = container_of(vbuf, struct dcmi_buf, vb); spin_lock_irq(&dcmi->irqlock); /* Enqueue to video buffers list */ list_add_tail(&buf->list, &dcmi->buffers); if (dcmi->state == WAIT_FOR_BUFFER) { dcmi->state = RUNNING; dcmi->active = buf; dev_dbg(dcmi->dev, "Starting capture on buffer[%d] queued\n", buf->vb.vb2_buf.index); spin_unlock_irq(&dcmi->irqlock); if (dcmi_start_capture(dcmi, buf)) dev_err(dcmi->dev, "%s: Cannot restart capture on overflow or error\n", __func__); return; } spin_unlock_irq(&dcmi->irqlock); } static struct media_entity *dcmi_find_source(struct stm32_dcmi *dcmi) { struct media_entity *entity = &dcmi->vdev->entity; struct media_pad *pad; /* Walk searching for entity having no sink */ while (1) { pad = &entity->pads[0]; if (!(pad->flags & MEDIA_PAD_FL_SINK)) break; pad = media_pad_remote_pad_first(pad); if (!pad || !is_media_entity_v4l2_subdev(pad->entity)) break; entity = pad->entity; } return entity; } static int dcmi_pipeline_s_fmt(struct stm32_dcmi *dcmi, struct v4l2_subdev_format *format) { struct media_entity *entity = &dcmi->source->entity; struct v4l2_subdev *subdev; struct media_pad *sink_pad = NULL; struct media_pad *src_pad = NULL; struct media_pad *pad = NULL; struct v4l2_subdev_format fmt = *format; bool found = false; int ret; /* * Starting from sensor subdevice, walk within * pipeline and set format on each subdevice */ while (1) { unsigned int i; /* Search if current entity has a source pad */ for (i = 0; i < entity->num_pads; i++) { pad = &entity->pads[i]; if (pad->flags & MEDIA_PAD_FL_SOURCE) { src_pad = pad; found = true; break; } } if (!found) break; subdev = media_entity_to_v4l2_subdev(entity); /* Propagate format on sink pad if any, otherwise source pad */ if (sink_pad) pad = sink_pad; dev_dbg(dcmi->dev, "\"%s\":%d pad format set to 0x%x %ux%u\n", subdev->name, pad->index, format->format.code, format->format.width, format->format.height); fmt.pad = pad->index; ret = v4l2_subdev_call(subdev, pad, set_fmt, NULL, &fmt); if (ret < 0) { dev_err(dcmi->dev, "%s: Failed to set format 0x%x %ux%u on \"%s\":%d pad (%d)\n", __func__, format->format.code, format->format.width, format->format.height, subdev->name, pad->index, ret); return ret; } if (fmt.format.code != format->format.code || fmt.format.width != format->format.width || fmt.format.height != format->format.height) { dev_dbg(dcmi->dev, "\"%s\":%d pad format has been changed to 0x%x %ux%u\n", subdev->name, pad->index, fmt.format.code, fmt.format.width, fmt.format.height); } /* Walk to next entity */ sink_pad = media_pad_remote_pad_first(src_pad); if (!sink_pad || !is_media_entity_v4l2_subdev(sink_pad->entity)) break; entity = sink_pad->entity; } *format = fmt; return 0; } static int dcmi_pipeline_s_stream(struct stm32_dcmi *dcmi, int state) { struct media_entity *entity = &dcmi->vdev->entity; struct v4l2_subdev *subdev; struct media_pad *pad; int ret; /* Start/stop all entities within pipeline */ while (1) { pad = &entity->pads[0]; if (!(pad->flags & MEDIA_PAD_FL_SINK)) break; pad = media_pad_remote_pad_first(pad); if (!pad || !is_media_entity_v4l2_subdev(pad->entity)) break; entity = pad->entity; subdev = media_entity_to_v4l2_subdev(entity); ret = v4l2_subdev_call(subdev, video, s_stream, state); if (ret < 0 && ret != -ENOIOCTLCMD) { dev_err(dcmi->dev, "%s: \"%s\" failed to %s streaming (%d)\n", __func__, subdev->name, state ? "start" : "stop", ret); return ret; } dev_dbg(dcmi->dev, "\"%s\" is %s\n", subdev->name, state ? "started" : "stopped"); } return 0; } static int dcmi_pipeline_start(struct stm32_dcmi *dcmi) { return dcmi_pipeline_s_stream(dcmi, 1); } static void dcmi_pipeline_stop(struct stm32_dcmi *dcmi) { dcmi_pipeline_s_stream(dcmi, 0); } static int dcmi_start_streaming(struct vb2_queue *vq, unsigned int count) { struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq); struct dcmi_buf *buf, *node; u32 val = 0; int ret; ret = pm_runtime_resume_and_get(dcmi->dev); if (ret < 0) { dev_err(dcmi->dev, "%s: Failed to start streaming, cannot get sync (%d)\n", __func__, ret); goto err_unlocked; } ret = video_device_pipeline_start(dcmi->vdev, &dcmi->pipeline); if (ret < 0) { dev_err(dcmi->dev, "%s: Failed to start streaming, media pipeline start error (%d)\n", __func__, ret); goto err_pm_put; } ret = dcmi_pipeline_start(dcmi); if (ret) goto err_media_pipeline_stop; spin_lock_irq(&dcmi->irqlock); /* Set bus width */ switch (dcmi->bus.bus_width) { case 14: val |= CR_EDM_0 | CR_EDM_1; break; case 12: val |= CR_EDM_1; break; case 10: val |= CR_EDM_0; break; default: /* Set bus width to 8 bits by default */ break; } /* Set vertical synchronization polarity */ if (dcmi->bus.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) val |= CR_VSPOL; /* Set horizontal synchronization polarity */ if (dcmi->bus.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) val |= CR_HSPOL; /* Set pixel clock polarity */ if (dcmi->bus.flags & V4L2_MBUS_PCLK_SAMPLE_RISING) val |= CR_PCKPOL; /* * BT656 embedded synchronisation bus mode. * * Default SAV/EAV mode is supported here with default codes * SAV=0xff000080 & EAV=0xff00009d. * With DCMI this means LSC=SAV=0x80 & LEC=EAV=0x9d. */ if (dcmi->bus_type == V4L2_MBUS_BT656) { val |= CR_ESS; /* Unmask all codes */ reg_write(dcmi->regs, DCMI_ESUR, 0xffffffff);/* FEC:LEC:LSC:FSC */ /* Trig on LSC=0x80 & LEC=0x9d codes, ignore FSC and FEC */ reg_write(dcmi->regs, DCMI_ESCR, 0xff9d80ff);/* FEC:LEC:LSC:FSC */ } reg_write(dcmi->regs, DCMI_CR, val); /* Set crop */ if (dcmi->do_crop) dcmi_set_crop(dcmi); /* Enable jpeg capture */ if (dcmi->sd_format->fourcc == V4L2_PIX_FMT_JPEG) reg_set(dcmi->regs, DCMI_CR, CR_CM);/* Snapshot mode */ /* Enable dcmi */ reg_set(dcmi->regs, DCMI_CR, CR_ENABLE); dcmi->sequence = 0; dcmi->errors_count = 0; dcmi->overrun_count = 0; dcmi->buffers_count = 0; /* * Start transfer if at least one buffer has been queued, * otherwise transfer is deferred at buffer queueing */ if (list_empty(&dcmi->buffers)) { dev_dbg(dcmi->dev, "Start streaming is deferred to next buffer queueing\n"); dcmi->state = WAIT_FOR_BUFFER; spin_unlock_irq(&dcmi->irqlock); return 0; } buf = list_entry(dcmi->buffers.next, struct dcmi_buf, list); dcmi->active = buf; dcmi->state = RUNNING; dev_dbg(dcmi->dev, "Start streaming, starting capture\n"); spin_unlock_irq(&dcmi->irqlock); ret = dcmi_start_capture(dcmi, buf); if (ret) { dev_err(dcmi->dev, "%s: Start streaming failed, cannot start capture\n", __func__); goto err_pipeline_stop; } /* Enable interruptions */ if (dcmi->sd_format->fourcc == V4L2_PIX_FMT_JPEG) reg_set(dcmi->regs, DCMI_IER, IT_FRAME | IT_OVR | IT_ERR); else reg_set(dcmi->regs, DCMI_IER, IT_OVR | IT_ERR); return 0; err_pipeline_stop: dcmi_pipeline_stop(dcmi); err_media_pipeline_stop: video_device_pipeline_stop(dcmi->vdev); err_pm_put: pm_runtime_put(dcmi->dev); err_unlocked: spin_lock_irq(&dcmi->irqlock); /* * Return all buffers to vb2 in QUEUED state. * This will give ownership back to userspace */ list_for_each_entry_safe(buf, node, &dcmi->buffers, list) { list_del_init(&buf->list); vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED); } dcmi->active = NULL; spin_unlock_irq(&dcmi->irqlock); return ret; } static void dcmi_stop_streaming(struct vb2_queue *vq) { struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq); struct dcmi_buf *buf, *node; dcmi_pipeline_stop(dcmi); video_device_pipeline_stop(dcmi->vdev); spin_lock_irq(&dcmi->irqlock); /* Disable interruptions */ reg_clear(dcmi->regs, DCMI_IER, IT_FRAME | IT_OVR | IT_ERR); /* Disable DCMI */ reg_clear(dcmi->regs, DCMI_CR, CR_ENABLE); /* Return all queued buffers to vb2 in ERROR state */ list_for_each_entry_safe(buf, node, &dcmi->buffers, list) { list_del_init(&buf->list); vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); } dcmi->active = NULL; dcmi->state = STOPPED; spin_unlock_irq(&dcmi->irqlock); /* Stop all pending DMA operations */ mutex_lock(&dcmi->dma_lock); dmaengine_terminate_sync(dcmi->dma_chan); mutex_unlock(&dcmi->dma_lock); pm_runtime_put(dcmi->dev); if (dcmi->errors_count) dev_warn(dcmi->dev, "Some errors found while streaming: errors=%d (overrun=%d), buffers=%d\n", dcmi->errors_count, dcmi->overrun_count, dcmi->buffers_count); dev_dbg(dcmi->dev, "Stop streaming, errors=%d (overrun=%d), buffers=%d\n", dcmi->errors_count, dcmi->overrun_count, dcmi->buffers_count); } static const struct vb2_ops dcmi_video_qops = { .queue_setup = dcmi_queue_setup, .buf_init = dcmi_buf_init, .buf_prepare = dcmi_buf_prepare, .buf_queue = dcmi_buf_queue, .start_streaming = dcmi_start_streaming, .stop_streaming = dcmi_stop_streaming, .wait_prepare = vb2_ops_wait_prepare, .wait_finish = vb2_ops_wait_finish, }; static int dcmi_g_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *fmt) { struct stm32_dcmi *dcmi = video_drvdata(file); *fmt = dcmi->fmt; return 0; } static const struct dcmi_format *find_format_by_fourcc(struct stm32_dcmi *dcmi, unsigned int fourcc) { unsigned int num_formats = dcmi->num_of_sd_formats; const struct dcmi_format *fmt; unsigned int i; for (i = 0; i < num_formats; i++) { fmt = dcmi->sd_formats[i]; if (fmt->fourcc == fourcc) return fmt; } return NULL; } static void __find_outer_frame_size(struct stm32_dcmi *dcmi, struct v4l2_pix_format *pix, struct dcmi_framesize *framesize) { struct dcmi_framesize *match = NULL; unsigned int i; unsigned int min_err = UINT_MAX; for (i = 0; i < dcmi->num_of_sd_framesizes; i++) { struct dcmi_framesize *fsize = &dcmi->sd_framesizes[i]; int w_err = (fsize->width - pix->width); int h_err = (fsize->height - pix->height); int err = w_err + h_err; if (w_err >= 0 && h_err >= 0 && err < min_err) { min_err = err; match = fsize; } } if (!match) match = &dcmi->sd_framesizes[0]; *framesize = *match; } static int dcmi_try_fmt(struct stm32_dcmi *dcmi, struct v4l2_format *f, const struct dcmi_format **sd_format, struct dcmi_framesize *sd_framesize) { const struct dcmi_format *sd_fmt; struct dcmi_framesize sd_fsize; struct v4l2_pix_format *pix = &f->fmt.pix; struct v4l2_subdev_format format = { .which = V4L2_SUBDEV_FORMAT_TRY, }; bool do_crop; int ret; sd_fmt = find_format_by_fourcc(dcmi, pix->pixelformat); if (!sd_fmt) { if (!dcmi->num_of_sd_formats) return -ENODATA; sd_fmt = dcmi->sd_formats[dcmi->num_of_sd_formats - 1]; pix->pixelformat = sd_fmt->fourcc; } /* Limit to hardware capabilities */ pix->width = clamp(pix->width, MIN_WIDTH, MAX_WIDTH); pix->height = clamp(pix->height, MIN_HEIGHT, MAX_HEIGHT); /* No crop if JPEG is requested */ do_crop = dcmi->do_crop && (pix->pixelformat != V4L2_PIX_FMT_JPEG); if (do_crop && dcmi->num_of_sd_framesizes) { struct dcmi_framesize outer_sd_fsize; /* * If crop is requested and sensor have discrete frame sizes, * select the frame size that is just larger than request */ __find_outer_frame_size(dcmi, pix, &outer_sd_fsize); pix->width = outer_sd_fsize.width; pix->height = outer_sd_fsize.height; } v4l2_fill_mbus_format(&format.format, pix, sd_fmt->mbus_code); ret = v4l2_subdev_call_state_try(dcmi->source, pad, set_fmt, &format); if (ret < 0) return ret; /* Update pix regarding to what sensor can do */ v4l2_fill_pix_format(pix, &format.format); /* Save resolution that sensor can actually do */ sd_fsize.width = pix->width; sd_fsize.height = pix->height; if (do_crop) { struct v4l2_rect c = dcmi->crop; struct v4l2_rect max_rect; /* * Adjust crop by making the intersection between * format resolution request and crop request */ max_rect.top = 0; max_rect.left = 0; max_rect.width = pix->width; max_rect.height = pix->height; v4l2_rect_map_inside(&c, &max_rect); c.top = clamp_t(s32, c.top, 0, pix->height - c.height); c.left = clamp_t(s32, c.left, 0, pix->width - c.width); dcmi->crop = c; /* Adjust format resolution request to crop */ pix->width = dcmi->crop.width; pix->height = dcmi->crop.height; } pix->field = V4L2_FIELD_NONE; pix->bytesperline = pix->width * sd_fmt->bpp; pix->sizeimage = pix->bytesperline * pix->height; if (sd_format) *sd_format = sd_fmt; if (sd_framesize) *sd_framesize = sd_fsize; return 0; } static int dcmi_set_fmt(struct stm32_dcmi *dcmi, struct v4l2_format *f) { struct v4l2_subdev_format format = { .which = V4L2_SUBDEV_FORMAT_ACTIVE, }; const struct dcmi_format *sd_format; struct dcmi_framesize sd_framesize; struct v4l2_mbus_framefmt *mf = &format.format; struct v4l2_pix_format *pix = &f->fmt.pix; int ret; /* * Try format, fmt.width/height could have been changed * to match sensor capability or crop request * sd_format & sd_framesize will contain what subdev * can do for this request. */ ret = dcmi_try_fmt(dcmi, f, &sd_format, &sd_framesize); if (ret) return ret; /* Disable crop if JPEG is requested or BT656 bus is selected */ if (pix->pixelformat == V4L2_PIX_FMT_JPEG && dcmi->bus_type != V4L2_MBUS_BT656) dcmi->do_crop = false; /* pix to mbus format */ v4l2_fill_mbus_format(mf, pix, sd_format->mbus_code); mf->width = sd_framesize.width; mf->height = sd_framesize.height; ret = dcmi_pipeline_s_fmt(dcmi, &format); if (ret < 0) return ret; dev_dbg(dcmi->dev, "Sensor format set to 0x%x %ux%u\n", mf->code, mf->width, mf->height); dev_dbg(dcmi->dev, "Buffer format set to %4.4s %ux%u\n", (char *)&pix->pixelformat, pix->width, pix->height); dcmi->fmt = *f; dcmi->sd_format = sd_format; dcmi->sd_framesize = sd_framesize; return 0; } static int dcmi_s_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f) { struct stm32_dcmi *dcmi = video_drvdata(file); if (vb2_is_streaming(&dcmi->queue)) return -EBUSY; return dcmi_set_fmt(dcmi, f); } static int dcmi_try_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f) { struct stm32_dcmi *dcmi = video_drvdata(file); return dcmi_try_fmt(dcmi, f, NULL, NULL); } static int dcmi_enum_fmt_vid_cap(struct file *file, void *priv, struct v4l2_fmtdesc *f) { struct stm32_dcmi *dcmi = video_drvdata(file); if (f->index >= dcmi->num_of_sd_formats) return -EINVAL; f->pixelformat = dcmi->sd_formats[f->index]->fourcc; return 0; } static int dcmi_get_sensor_format(struct stm32_dcmi *dcmi, struct v4l2_pix_format *pix) { struct v4l2_subdev_format fmt = { .which = V4L2_SUBDEV_FORMAT_ACTIVE, }; int ret; ret = v4l2_subdev_call(dcmi->source, pad, get_fmt, NULL, &fmt); if (ret) return ret; v4l2_fill_pix_format(pix, &fmt.format); return 0; } static int dcmi_set_sensor_format(struct stm32_dcmi *dcmi, struct v4l2_pix_format *pix) { const struct dcmi_format *sd_fmt; struct v4l2_subdev_format format = { .which = V4L2_SUBDEV_FORMAT_TRY, }; int ret; sd_fmt = find_format_by_fourcc(dcmi, pix->pixelformat); if (!sd_fmt) { if (!dcmi->num_of_sd_formats) return -ENODATA; sd_fmt = dcmi->sd_formats[dcmi->num_of_sd_formats - 1]; pix->pixelformat = sd_fmt->fourcc; } v4l2_fill_mbus_format(&format.format, pix, sd_fmt->mbus_code); ret = v4l2_subdev_call_state_try(dcmi->source, pad, set_fmt, &format); if (ret < 0) return ret; return 0; } static int dcmi_get_sensor_bounds(struct stm32_dcmi *dcmi, struct v4l2_rect *r) { struct v4l2_subdev_selection bounds = { .which = V4L2_SUBDEV_FORMAT_ACTIVE, .target = V4L2_SEL_TGT_CROP_BOUNDS, }; unsigned int max_width, max_height, max_pixsize; struct v4l2_pix_format pix; unsigned int i; int ret; /* * Get sensor bounds first */ ret = v4l2_subdev_call(dcmi->source, pad, get_selection, NULL, &bounds); if (!ret) *r = bounds.r; if (ret != -ENOIOCTLCMD) return ret; /* * If selection is not implemented, * fallback by enumerating sensor frame sizes * and take the largest one */ max_width = 0; max_height = 0; max_pixsize = 0; for (i = 0; i < dcmi->num_of_sd_framesizes; i++) { struct dcmi_framesize *fsize = &dcmi->sd_framesizes[i]; unsigned int pixsize = fsize->width * fsize->height; if (pixsize > max_pixsize) { max_pixsize = pixsize; max_width = fsize->width; max_height = fsize->height; } } if (max_pixsize > 0) { r->top = 0; r->left = 0; r->width = max_width; r->height = max_height; return 0; } /* * If frame sizes enumeration is not implemented, * fallback by getting current sensor frame size */ ret = dcmi_get_sensor_format(dcmi, &pix); if (ret) return ret; r->top = 0; r->left = 0; r->width = pix.width; r->height = pix.height; return 0; } static int dcmi_g_selection(struct file *file, void *fh, struct v4l2_selection *s) { struct stm32_dcmi *dcmi = video_drvdata(file); if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) return -EINVAL; switch (s->target) { case V4L2_SEL_TGT_CROP_DEFAULT: case V4L2_SEL_TGT_CROP_BOUNDS: s->r = dcmi->sd_bounds; return 0; case V4L2_SEL_TGT_CROP: if (dcmi->do_crop) { s->r = dcmi->crop; } else { s->r.top = 0; s->r.left = 0; s->r.width = dcmi->fmt.fmt.pix.width; s->r.height = dcmi->fmt.fmt.pix.height; } break; default: return -EINVAL; } return 0; } static int dcmi_s_selection(struct file *file, void *priv, struct v4l2_selection *s) { struct stm32_dcmi *dcmi = video_drvdata(file); struct v4l2_rect r = s->r; struct v4l2_rect max_rect; struct v4l2_pix_format pix; if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || s->target != V4L2_SEL_TGT_CROP) return -EINVAL; /* Reset sensor resolution to max resolution */ pix.pixelformat = dcmi->fmt.fmt.pix.pixelformat; pix.width = dcmi->sd_bounds.width; pix.height = dcmi->sd_bounds.height; dcmi_set_sensor_format(dcmi, &pix); /* * Make the intersection between * sensor resolution * and crop request */ max_rect.top = 0; max_rect.left = 0; max_rect.width = pix.width; max_rect.height = pix.height; v4l2_rect_map_inside(&r, &max_rect); r.top = clamp_t(s32, r.top, 0, pix.height - r.height); r.left = clamp_t(s32, r.left, 0, pix.width - r.width); if (!(r.top == dcmi->sd_bounds.top && r.left == dcmi->sd_bounds.left && r.width == dcmi->sd_bounds.width && r.height == dcmi->sd_bounds.height)) { /* Crop if request is different than sensor resolution */ dcmi->do_crop = true; dcmi->crop = r; dev_dbg(dcmi->dev, "s_selection: crop %ux%u@(%u,%u) from %ux%u\n", r.width, r.height, r.left, r.top, pix.width, pix.height); } else { /* Disable crop */ dcmi->do_crop = false; dev_dbg(dcmi->dev, "s_selection: crop is disabled\n"); } s->r = r; return 0; } static int dcmi_querycap(struct file *file, void *priv, struct v4l2_capability *cap) { strscpy(cap->driver, DRV_NAME, sizeof(cap->driver)); strscpy(cap->card, "STM32 Camera Memory Interface", sizeof(cap->card)); strscpy(cap->bus_info, "platform:dcmi", sizeof(cap->bus_info)); return 0; } static int dcmi_enum_input(struct file *file, void *priv, struct v4l2_input *i) { if (i->index != 0) return -EINVAL; i->type = V4L2_INPUT_TYPE_CAMERA; strscpy(i->name, "Camera", sizeof(i->name)); return 0; } static int dcmi_g_input(struct file *file, void *priv, unsigned int *i) { *i = 0; return 0; } static int dcmi_s_input(struct file *file, void *priv, unsigned int i) { if (i > 0) return -EINVAL; return 0; } static int dcmi_enum_framesizes(struct file *file, void *fh, struct v4l2_frmsizeenum *fsize) { struct stm32_dcmi *dcmi = video_drvdata(file); const struct dcmi_format *sd_fmt; struct v4l2_subdev_frame_size_enum fse = { .index = fsize->index, .which = V4L2_SUBDEV_FORMAT_ACTIVE, }; int ret; sd_fmt = find_format_by_fourcc(dcmi, fsize->pixel_format); if (!sd_fmt) return -EINVAL; fse.code = sd_fmt->mbus_code; ret = v4l2_subdev_call(dcmi->source, pad, enum_frame_size, NULL, &fse); if (ret) return ret; fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; fsize->discrete.width = fse.max_width; fsize->discrete.height = fse.max_height; return 0; } static int dcmi_g_parm(struct file *file, void *priv, struct v4l2_streamparm *p) { struct stm32_dcmi *dcmi = video_drvdata(file); return v4l2_g_parm_cap(video_devdata(file), dcmi->source, p); } static int dcmi_s_parm(struct file *file, void *priv, struct v4l2_streamparm *p) { struct stm32_dcmi *dcmi = video_drvdata(file); return v4l2_s_parm_cap(video_devdata(file), dcmi->source, p); } static int dcmi_enum_frameintervals(struct file *file, void *fh, struct v4l2_frmivalenum *fival) { struct stm32_dcmi *dcmi = video_drvdata(file); const struct dcmi_format *sd_fmt; struct v4l2_subdev_frame_interval_enum fie = { .index = fival->index, .width = fival->width, .height = fival->height, .which = V4L2_SUBDEV_FORMAT_ACTIVE, }; int ret; sd_fmt = find_format_by_fourcc(dcmi, fival->pixel_format); if (!sd_fmt) return -EINVAL; fie.code = sd_fmt->mbus_code; ret = v4l2_subdev_call(dcmi->source, pad, enum_frame_interval, NULL, &fie); if (ret) return ret; fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; fival->discrete = fie.interval; return 0; } static const struct of_device_id stm32_dcmi_of_match[] = { { .compatible = "st,stm32-dcmi"}, { /* end node */ }, }; MODULE_DEVICE_TABLE(of, stm32_dcmi_of_match); static int dcmi_open(struct file *file) { struct stm32_dcmi *dcmi = video_drvdata(file); struct v4l2_subdev *sd = dcmi->source; int ret; if (mutex_lock_interruptible(&dcmi->lock)) return -ERESTARTSYS; ret = v4l2_fh_open(file); if (ret < 0) goto unlock; if (!v4l2_fh_is_singular_file(file)) goto fh_rel; ret = v4l2_subdev_call(sd, core, s_power, 1); if (ret < 0 && ret != -ENOIOCTLCMD) goto fh_rel; ret = dcmi_set_fmt(dcmi, &dcmi->fmt); if (ret) v4l2_subdev_call(sd, core, s_power, 0); fh_rel: if (ret) v4l2_fh_release(file); unlock: mutex_unlock(&dcmi->lock); return ret; } static int dcmi_release(struct file *file) { struct stm32_dcmi *dcmi = video_drvdata(file); struct v4l2_subdev *sd = dcmi->source; bool fh_singular; int ret; mutex_lock(&dcmi->lock); fh_singular = v4l2_fh_is_singular_file(file); ret = _vb2_fop_release(file, NULL); if (fh_singular) v4l2_subdev_call(sd, core, s_power, 0); mutex_unlock(&dcmi->lock); return ret; } static const struct v4l2_ioctl_ops dcmi_ioctl_ops = { .vidioc_querycap = dcmi_querycap, .vidioc_try_fmt_vid_cap = dcmi_try_fmt_vid_cap, .vidioc_g_fmt_vid_cap = dcmi_g_fmt_vid_cap, .vidioc_s_fmt_vid_cap = dcmi_s_fmt_vid_cap, .vidioc_enum_fmt_vid_cap = dcmi_enum_fmt_vid_cap, .vidioc_g_selection = dcmi_g_selection, .vidioc_s_selection = dcmi_s_selection, .vidioc_enum_input = dcmi_enum_input, .vidioc_g_input = dcmi_g_input, .vidioc_s_input = dcmi_s_input, .vidioc_g_parm = dcmi_g_parm, .vidioc_s_parm = dcmi_s_parm, .vidioc_enum_framesizes = dcmi_enum_framesizes, .vidioc_enum_frameintervals = dcmi_enum_frameintervals, .vidioc_reqbufs = vb2_ioctl_reqbufs, .vidioc_create_bufs = vb2_ioctl_create_bufs, .vidioc_querybuf = vb2_ioctl_querybuf, .vidioc_qbuf = vb2_ioctl_qbuf, .vidioc_dqbuf = vb2_ioctl_dqbuf, .vidioc_expbuf = vb2_ioctl_expbuf, .vidioc_prepare_buf = vb2_ioctl_prepare_buf, .vidioc_streamon = vb2_ioctl_streamon, .vidioc_streamoff = vb2_ioctl_streamoff, .vidioc_log_status = v4l2_ctrl_log_status, .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, .vidioc_unsubscribe_event = v4l2_event_unsubscribe, }; static const struct v4l2_file_operations dcmi_fops = { .owner = THIS_MODULE, .unlocked_ioctl = video_ioctl2, .open = dcmi_open, .release = dcmi_release, .poll = vb2_fop_poll, .mmap = vb2_fop_mmap, #ifndef CONFIG_MMU .get_unmapped_area = vb2_fop_get_unmapped_area, #endif .read = vb2_fop_read, }; static int dcmi_set_default_fmt(struct stm32_dcmi *dcmi) { struct v4l2_format f = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, .fmt.pix = { .width = CIF_WIDTH, .height = CIF_HEIGHT, .field = V4L2_FIELD_NONE, .pixelformat = dcmi->sd_formats[0]->fourcc, }, }; int ret; ret = dcmi_try_fmt(dcmi, &f, NULL, NULL); if (ret) return ret; dcmi->sd_format = dcmi->sd_formats[0]; dcmi->fmt = f; return 0; } static const struct dcmi_format dcmi_formats[] = { { .fourcc = V4L2_PIX_FMT_RGB565, .mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE, .bpp = 2, }, { .fourcc = V4L2_PIX_FMT_RGB565, .mbus_code = MEDIA_BUS_FMT_RGB565_1X16, .bpp = 2, }, { .fourcc = V4L2_PIX_FMT_YUYV, .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8, .bpp = 2, }, { .fourcc = V4L2_PIX_FMT_YUYV, .mbus_code = MEDIA_BUS_FMT_YUYV8_1X16, .bpp = 2, }, { .fourcc = V4L2_PIX_FMT_UYVY, .mbus_code = MEDIA_BUS_FMT_UYVY8_2X8, .bpp = 2, }, { .fourcc = V4L2_PIX_FMT_UYVY, .mbus_code = MEDIA_BUS_FMT_UYVY8_1X16, .bpp = 2, }, { .fourcc = V4L2_PIX_FMT_JPEG, .mbus_code = MEDIA_BUS_FMT_JPEG_1X8, .bpp = 1, }, { .fourcc = V4L2_PIX_FMT_SBGGR8, .mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8, .bpp = 1, }, { .fourcc = V4L2_PIX_FMT_SGBRG8, .mbus_code = MEDIA_BUS_FMT_SGBRG8_1X8, .bpp = 1, }, { .fourcc = V4L2_PIX_FMT_SGRBG8, .mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8, .bpp = 1, }, { .fourcc = V4L2_PIX_FMT_SRGGB8, .mbus_code = MEDIA_BUS_FMT_SRGGB8_1X8, .bpp = 1, }, { .fourcc = V4L2_PIX_FMT_SBGGR10, .mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10, .bpp = 2, }, { .fourcc = V4L2_PIX_FMT_SGBRG10, .mbus_code = MEDIA_BUS_FMT_SGBRG10_1X10, .bpp = 2, }, { .fourcc = V4L2_PIX_FMT_SGRBG10, .mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10, .bpp = 2, }, { .fourcc = V4L2_PIX_FMT_SRGGB10, .mbus_code = MEDIA_BUS_FMT_SRGGB10_1X10, .bpp = 2, }, { .fourcc = V4L2_PIX_FMT_SBGGR12, .mbus_code = MEDIA_BUS_FMT_SBGGR12_1X12, .bpp = 2, }, { .fourcc = V4L2_PIX_FMT_SGBRG12, .mbus_code = MEDIA_BUS_FMT_SGBRG12_1X12, .bpp = 2, }, { .fourcc = V4L2_PIX_FMT_SGRBG12, .mbus_code = MEDIA_BUS_FMT_SGRBG12_1X12, .bpp = 2, }, { .fourcc = V4L2_PIX_FMT_SRGGB12, .mbus_code = MEDIA_BUS_FMT_SRGGB12_1X12, .bpp = 2, }, { .fourcc = V4L2_PIX_FMT_SBGGR14, .mbus_code = MEDIA_BUS_FMT_SBGGR14_1X14, .bpp = 2, }, { .fourcc = V4L2_PIX_FMT_SGBRG14, .mbus_code = MEDIA_BUS_FMT_SGBRG14_1X14, .bpp = 2, }, { .fourcc = V4L2_PIX_FMT_SGRBG14, .mbus_code = MEDIA_BUS_FMT_SGRBG14_1X14, .bpp = 2, }, { .fourcc = V4L2_PIX_FMT_SRGGB14, .mbus_code = MEDIA_BUS_FMT_SRGGB14_1X14, .bpp = 2, }, }; static int dcmi_formats_init(struct stm32_dcmi *dcmi) { const struct dcmi_format *sd_fmts[ARRAY_SIZE(dcmi_formats)]; unsigned int num_fmts = 0, i, j; struct v4l2_subdev *subdev = dcmi->source; struct v4l2_subdev_mbus_code_enum mbus_code = { .which = V4L2_SUBDEV_FORMAT_ACTIVE, }; while (!v4l2_subdev_call(subdev, pad, enum_mbus_code, NULL, &mbus_code)) { for (i = 0; i < ARRAY_SIZE(dcmi_formats); i++) { if (dcmi_formats[i].mbus_code != mbus_code.code) continue; /* Exclude JPEG if BT656 bus is selected */ if (dcmi_formats[i].fourcc == V4L2_PIX_FMT_JPEG && dcmi->bus_type == V4L2_MBUS_BT656) continue; /* Code supported, have we got this fourcc yet? */ for (j = 0; j < num_fmts; j++) if (sd_fmts[j]->fourcc == dcmi_formats[i].fourcc) { /* Already available */ dev_dbg(dcmi->dev, "Skipping fourcc/code: %4.4s/0x%x\n", (char *)&sd_fmts[j]->fourcc, mbus_code.code); break; } if (j == num_fmts) { /* New */ sd_fmts[num_fmts++] = dcmi_formats + i; dev_dbg(dcmi->dev, "Supported fourcc/code: %4.4s/0x%x\n", (char *)&sd_fmts[num_fmts - 1]->fourcc, sd_fmts[num_fmts - 1]->mbus_code); } } mbus_code.index++; } if (!num_fmts) return -ENXIO; dcmi->num_of_sd_formats = num_fmts; dcmi->sd_formats = devm_kcalloc(dcmi->dev, num_fmts, sizeof(struct dcmi_format *), GFP_KERNEL); if (!dcmi->sd_formats) { dev_err(dcmi->dev, "Could not allocate memory\n"); return -ENOMEM; } memcpy(dcmi->sd_formats, sd_fmts, num_fmts * sizeof(struct dcmi_format *)); dcmi->sd_format = dcmi->sd_formats[0]; return 0; } static int dcmi_framesizes_init(struct stm32_dcmi *dcmi) { unsigned int num_fsize = 0; struct v4l2_subdev *subdev = dcmi->source; struct v4l2_subdev_frame_size_enum fse = { .which = V4L2_SUBDEV_FORMAT_ACTIVE, .code = dcmi->sd_format->mbus_code, }; unsigned int ret; unsigned int i; /* Allocate discrete framesizes array */ while (!v4l2_subdev_call(subdev, pad, enum_frame_size, NULL, &fse)) fse.index++; num_fsize = fse.index; if (!num_fsize) return 0; dcmi->num_of_sd_framesizes = num_fsize; dcmi->sd_framesizes = devm_kcalloc(dcmi->dev, num_fsize, sizeof(struct dcmi_framesize), GFP_KERNEL); if (!dcmi->sd_framesizes) { dev_err(dcmi->dev, "Could not allocate memory\n"); return -ENOMEM; } /* Fill array with sensor supported framesizes */ dev_dbg(dcmi->dev, "Sensor supports %u frame sizes:\n", num_fsize); for (i = 0; i < dcmi->num_of_sd_framesizes; i++) { fse.index = i; ret = v4l2_subdev_call(subdev, pad, enum_frame_size, NULL, &fse); if (ret) return ret; dcmi->sd_framesizes[fse.index].width = fse.max_width; dcmi->sd_framesizes[fse.index].height = fse.max_height; dev_dbg(dcmi->dev, "%ux%u\n", fse.max_width, fse.max_height); } return 0; } static int dcmi_graph_notify_complete(struct v4l2_async_notifier *notifier) { struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier); int ret; /* * Now that the graph is complete, * we search for the source subdevice * in order to expose it through V4L2 interface */ dcmi->source = media_entity_to_v4l2_subdev(dcmi_find_source(dcmi)); if (!dcmi->source) { dev_err(dcmi->dev, "Source subdevice not found\n"); return -ENODEV; } dcmi->vdev->ctrl_handler = dcmi->source->ctrl_handler; ret = dcmi_formats_init(dcmi); if (ret) { dev_err(dcmi->dev, "No supported mediabus format found\n"); return ret; } ret = dcmi_framesizes_init(dcmi); if (ret) { dev_err(dcmi->dev, "Could not initialize framesizes\n"); return ret; } ret = dcmi_get_sensor_bounds(dcmi, &dcmi->sd_bounds); if (ret) { dev_err(dcmi->dev, "Could not get sensor bounds\n"); return ret; } ret = dcmi_set_default_fmt(dcmi); if (ret) { dev_err(dcmi->dev, "Could not set default format\n"); return ret; } ret = devm_request_threaded_irq(dcmi->dev, dcmi->irq, dcmi_irq_callback, dcmi_irq_thread, IRQF_ONESHOT, dev_name(dcmi->dev), dcmi); if (ret) { dev_err(dcmi->dev, "Unable to request irq %d\n", dcmi->irq); return ret; } return 0; } static void dcmi_graph_notify_unbind(struct v4l2_async_notifier *notifier, struct v4l2_subdev *sd, struct v4l2_async_subdev *asd) { struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier); dev_dbg(dcmi->dev, "Removing %s\n", video_device_node_name(dcmi->vdev)); /* Checks internally if vdev has been init or not */ video_unregister_device(dcmi->vdev); } static int dcmi_graph_notify_bound(struct v4l2_async_notifier *notifier, struct v4l2_subdev *subdev, struct v4l2_async_subdev *asd) { struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier); unsigned int ret; int src_pad; dev_dbg(dcmi->dev, "Subdev \"%s\" bound\n", subdev->name); /* * Link this sub-device to DCMI, it could be * a parallel camera sensor or a bridge */ src_pad = media_entity_get_fwnode_pad(&subdev->entity, subdev->fwnode, MEDIA_PAD_FL_SOURCE); ret = media_create_pad_link(&subdev->entity, src_pad, &dcmi->vdev->entity, 0, MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED); if (ret) dev_err(dcmi->dev, "Failed to create media pad link with subdev \"%s\"\n", subdev->name); else dev_dbg(dcmi->dev, "DCMI is now linked to \"%s\"\n", subdev->name); return ret; } static const struct v4l2_async_notifier_operations dcmi_graph_notify_ops = { .bound = dcmi_graph_notify_bound, .unbind = dcmi_graph_notify_unbind, .complete = dcmi_graph_notify_complete, }; static int dcmi_graph_init(struct stm32_dcmi *dcmi) { struct v4l2_async_subdev *asd; struct device_node *ep; int ret; ep = of_graph_get_next_endpoint(dcmi->dev->of_node, NULL); if (!ep) { dev_err(dcmi->dev, "Failed to get next endpoint\n"); return -EINVAL; } v4l2_async_nf_init(&dcmi->notifier); asd = v4l2_async_nf_add_fwnode_remote(&dcmi->notifier, of_fwnode_handle(ep), struct v4l2_async_subdev); of_node_put(ep); if (IS_ERR(asd)) { dev_err(dcmi->dev, "Failed to add subdev notifier\n"); return PTR_ERR(asd); } dcmi->notifier.ops = &dcmi_graph_notify_ops; ret = v4l2_async_nf_register(&dcmi->v4l2_dev, &dcmi->notifier); if (ret < 0) { dev_err(dcmi->dev, "Failed to register notifier\n"); v4l2_async_nf_cleanup(&dcmi->notifier); return ret; } return 0; } static int dcmi_probe(struct platform_device *pdev) { struct device_node *np = pdev->dev.of_node; const struct of_device_id *match = NULL; struct v4l2_fwnode_endpoint ep = { .bus_type = 0 }; struct stm32_dcmi *dcmi; struct vb2_queue *q; struct dma_chan *chan; struct dma_slave_caps caps; struct clk *mclk; int irq; int ret = 0; match = of_match_device(of_match_ptr(stm32_dcmi_of_match), &pdev->dev); if (!match) { dev_err(&pdev->dev, "Could not find a match in devicetree\n"); return -ENODEV; } dcmi = devm_kzalloc(&pdev->dev, sizeof(struct stm32_dcmi), GFP_KERNEL); if (!dcmi) return -ENOMEM; dcmi->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL); if (IS_ERR(dcmi->rstc)) return dev_err_probe(&pdev->dev, PTR_ERR(dcmi->rstc), "Could not get reset control\n"); /* Get bus characteristics from devicetree */ np = of_graph_get_next_endpoint(np, NULL); if (!np) { dev_err(&pdev->dev, "Could not find the endpoint\n"); return -ENODEV; } ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(np), &ep); of_node_put(np); if (ret) { dev_err(&pdev->dev, "Could not parse the endpoint\n"); return ret; } if (ep.bus_type == V4L2_MBUS_CSI2_DPHY) { dev_err(&pdev->dev, "CSI bus not supported\n"); return -ENODEV; } if (ep.bus_type == V4L2_MBUS_BT656 && ep.bus.parallel.bus_width != 8) { dev_err(&pdev->dev, "BT656 bus conflicts with %u bits bus width (8 bits required)\n", ep.bus.parallel.bus_width); return -ENODEV; } dcmi->bus.flags = ep.bus.parallel.flags; dcmi->bus.bus_width = ep.bus.parallel.bus_width; dcmi->bus.data_shift = ep.bus.parallel.data_shift; dcmi->bus_type = ep.bus_type; irq = platform_get_irq(pdev, 0); if (irq <= 0) return irq ? irq : -ENXIO; dcmi->irq = irq; dcmi->res = platform_get_resource(pdev, IORESOURCE_MEM, 0); if (!dcmi->res) { dev_err(&pdev->dev, "Could not get resource\n"); return -ENODEV; } dcmi->regs = devm_ioremap_resource(&pdev->dev, dcmi->res); if (IS_ERR(dcmi->regs)) return PTR_ERR(dcmi->regs); mclk = devm_clk_get(&pdev->dev, "mclk"); if (IS_ERR(mclk)) return dev_err_probe(&pdev->dev, PTR_ERR(mclk), "Unable to get mclk\n"); chan = dma_request_chan(&pdev->dev, "tx"); if (IS_ERR(chan)) return dev_err_probe(&pdev->dev, PTR_ERR(chan), "Failed to request DMA channel\n"); dcmi->dma_max_burst = UINT_MAX; ret = dma_get_slave_caps(chan, &caps); if (!ret && caps.max_sg_burst) dcmi->dma_max_burst = caps.max_sg_burst * DMA_SLAVE_BUSWIDTH_4_BYTES; spin_lock_init(&dcmi->irqlock); mutex_init(&dcmi->lock); mutex_init(&dcmi->dma_lock); init_completion(&dcmi->complete); INIT_LIST_HEAD(&dcmi->buffers); dcmi->dev = &pdev->dev; dcmi->mclk = mclk; dcmi->state = STOPPED; dcmi->dma_chan = chan; q = &dcmi->queue; dcmi->v4l2_dev.mdev = &dcmi->mdev; /* Initialize media device */ strscpy(dcmi->mdev.model, DRV_NAME, sizeof(dcmi->mdev.model)); dcmi->mdev.dev = &pdev->dev; media_device_init(&dcmi->mdev); /* Initialize the top-level structure */ ret = v4l2_device_register(&pdev->dev, &dcmi->v4l2_dev); if (ret) goto err_media_device_cleanup; dcmi->vdev = video_device_alloc(); if (!dcmi->vdev) { ret = -ENOMEM; goto err_device_unregister; } /* Video node */ dcmi->vdev->fops = &dcmi_fops; dcmi->vdev->v4l2_dev = &dcmi->v4l2_dev; dcmi->vdev->queue = &dcmi->queue; strscpy(dcmi->vdev->name, KBUILD_MODNAME, sizeof(dcmi->vdev->name)); dcmi->vdev->release = video_device_release; dcmi->vdev->ioctl_ops = &dcmi_ioctl_ops; dcmi->vdev->lock = &dcmi->lock; dcmi->vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | V4L2_CAP_READWRITE; video_set_drvdata(dcmi->vdev, dcmi); /* Media entity pads */ dcmi->vid_cap_pad.flags = MEDIA_PAD_FL_SINK; ret = media_entity_pads_init(&dcmi->vdev->entity, 1, &dcmi->vid_cap_pad); if (ret) { dev_err(dcmi->dev, "Failed to init media entity pad\n"); goto err_device_release; } dcmi->vdev->entity.flags |= MEDIA_ENT_FL_DEFAULT; ret = video_register_device(dcmi->vdev, VFL_TYPE_VIDEO, -1); if (ret) { dev_err(dcmi->dev, "Failed to register video device\n"); goto err_media_entity_cleanup; } dev_dbg(dcmi->dev, "Device registered as %s\n", video_device_node_name(dcmi->vdev)); /* Buffer queue */ q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; q->io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF; q->lock = &dcmi->lock; q->drv_priv = dcmi; q->buf_struct_size = sizeof(struct dcmi_buf); q->ops = &dcmi_video_qops; q->mem_ops = &vb2_dma_contig_memops; q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; q->min_buffers_needed = 2; q->allow_cache_hints = 1; q->dev = &pdev->dev; ret = vb2_queue_init(q); if (ret < 0) { dev_err(&pdev->dev, "Failed to initialize vb2 queue\n"); goto err_media_entity_cleanup; } ret = dcmi_graph_init(dcmi); if (ret < 0) goto err_media_entity_cleanup; /* Reset device */ ret = reset_control_assert(dcmi->rstc); if (ret) { dev_err(&pdev->dev, "Failed to assert the reset line\n"); goto err_cleanup; } usleep_range(3000, 5000); ret = reset_control_deassert(dcmi->rstc); if (ret) { dev_err(&pdev->dev, "Failed to deassert the reset line\n"); goto err_cleanup; } dev_info(&pdev->dev, "Probe done\n"); platform_set_drvdata(pdev, dcmi); pm_runtime_enable(&pdev->dev); return 0; err_cleanup: v4l2_async_nf_cleanup(&dcmi->notifier); err_media_entity_cleanup: media_entity_cleanup(&dcmi->vdev->entity); err_device_release: video_device_release(dcmi->vdev); err_device_unregister: v4l2_device_unregister(&dcmi->v4l2_dev); err_media_device_cleanup: media_device_cleanup(&dcmi->mdev); dma_release_channel(dcmi->dma_chan); return ret; } static void dcmi_remove(struct platform_device *pdev) { struct stm32_dcmi *dcmi = platform_get_drvdata(pdev); pm_runtime_disable(&pdev->dev); v4l2_async_nf_unregister(&dcmi->notifier); v4l2_async_nf_cleanup(&dcmi->notifier); media_entity_cleanup(&dcmi->vdev->entity); v4l2_device_unregister(&dcmi->v4l2_dev); media_device_cleanup(&dcmi->mdev); dma_release_channel(dcmi->dma_chan); } static __maybe_unused int dcmi_runtime_suspend(struct device *dev) { struct stm32_dcmi *dcmi = dev_get_drvdata(dev); clk_disable_unprepare(dcmi->mclk); return 0; } static __maybe_unused int dcmi_runtime_resume(struct device *dev) { struct stm32_dcmi *dcmi = dev_get_drvdata(dev); int ret; ret = clk_prepare_enable(dcmi->mclk); if (ret) dev_err(dev, "%s: Failed to prepare_enable clock\n", __func__); return ret; } static __maybe_unused int dcmi_suspend(struct device *dev) { /* disable clock */ pm_runtime_force_suspend(dev); /* change pinctrl state */ pinctrl_pm_select_sleep_state(dev); return 0; } static __maybe_unused int dcmi_resume(struct device *dev) { /* restore pinctl default state */ pinctrl_pm_select_default_state(dev); /* clock enable */ pm_runtime_force_resume(dev); return 0; } static const struct dev_pm_ops dcmi_pm_ops = { SET_SYSTEM_SLEEP_PM_OPS(dcmi_suspend, dcmi_resume) SET_RUNTIME_PM_OPS(dcmi_runtime_suspend, dcmi_runtime_resume, NULL) }; static struct platform_driver stm32_dcmi_driver = { .probe = dcmi_probe, .remove_new = dcmi_remove, .driver = { .name = DRV_NAME, .of_match_table = of_match_ptr(stm32_dcmi_of_match), .pm = &dcmi_pm_ops, }, }; module_platform_driver(stm32_dcmi_driver); MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>"); MODULE_AUTHOR("Hugues Fruchet <hugues.fruchet@st.com>"); MODULE_DESCRIPTION("STMicroelectronics STM32 Digital Camera Memory Interface driver"); MODULE_LICENSE("GPL"); |