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
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2022, Intel Corporation. */

#include "ice.h"
#include "ice_lib.h"
#include "ice_trace.h"
#include "ice_txclk.h"
#include <linux/dpll.h>
#include <linux/property.h>

#define ICE_CGU_STATE_ACQ_ERR_THRESHOLD		50
#define ICE_DPLL_PIN_IDX_INVALID		0xff
#define ICE_DPLL_RCLK_NUM_PER_PF		1
#define ICE_DPLL_PIN_ESYNC_PULSE_HIGH_PERCENT	25
#define ICE_DPLL_PIN_GEN_RCLK_FREQ		1953125
#define ICE_DPLL_PIN_PRIO_OUTPUT		0xff
#define ICE_DPLL_INPUT_REF_NUM			10
#define ICE_DPLL_PHASE_OFFSET_PERIOD		2
#define ICE_DPLL_SW_PIN_INPUT_BASE_SFP		4
#define ICE_DPLL_SW_PIN_INPUT_BASE_QSFP		6
#define ICE_DPLL_SW_PIN_OUTPUT_BASE		0

#define E825_RCLK_PARENT_0_PIN_IDX		0
#define E825_RCLK_PARENT_1_PIN_IDX		1

#define ICE_DPLL_PIN_SW_INPUT_ABS(in_idx) \
	(ICE_DPLL_SW_PIN_INPUT_BASE_SFP + (in_idx))

#define ICE_DPLL_PIN_SW_1_INPUT_ABS_IDX \
	(ICE_DPLL_PIN_SW_INPUT_ABS(ICE_DPLL_PIN_SW_1_IDX))

#define ICE_DPLL_PIN_SW_2_INPUT_ABS_IDX \
	(ICE_DPLL_PIN_SW_INPUT_ABS(ICE_DPLL_PIN_SW_2_IDX))

#define ICE_DPLL_PIN_SW_OUTPUT_ABS(out_idx) \
	(ICE_DPLL_SW_PIN_OUTPUT_BASE + (out_idx))

#define ICE_DPLL_PIN_SW_1_OUTPUT_ABS_IDX \
	(ICE_DPLL_PIN_SW_OUTPUT_ABS(ICE_DPLL_PIN_SW_1_IDX))

#define ICE_DPLL_PIN_SW_2_OUTPUT_ABS_IDX \
	(ICE_DPLL_PIN_SW_OUTPUT_ABS(ICE_DPLL_PIN_SW_2_IDX))

#define ICE_SR_PFA_DPLL_DEFAULTS		0x152
#define ICE_DPLL_PFA_REF_SYNC_TYPE		0x2420
#define ICE_DPLL_PFA_REF_SYNC_TYPE2		0x2424
#define ICE_DPLL_PFA_END			0xFFFF
#define ICE_DPLL_PFA_HEADER_LEN			4
#define ICE_DPLL_PFA_ENTRY_LEN			3
#define ICE_DPLL_PFA_MAILBOX_REF_SYNC_PIN_S	4
#define ICE_DPLL_PFA_MASK_OFFSET		1
#define ICE_DPLL_PFA_VALUE_OFFSET		2

#define ICE_DPLL_E810C_SFP_NC_PINS		2
#define ICE_DPLL_E810C_SFP_NC_START		4

/**
 * enum ice_dpll_pin_type - enumerate ice pin types:
 * @ICE_DPLL_PIN_INVALID: invalid pin type
 * @ICE_DPLL_PIN_TYPE_INPUT: input pin
 * @ICE_DPLL_PIN_TYPE_OUTPUT: output pin
 * @ICE_DPLL_PIN_TYPE_RCLK_INPUT: recovery clock input pin
 * @ICE_DPLL_PIN_TYPE_SOFTWARE: software controlled SMA/U.FL pins
 * @ICE_DPLL_PIN_TYPE_TXCLK: transmit clock reference input pin
 */
enum ice_dpll_pin_type {
	ICE_DPLL_PIN_INVALID,
	ICE_DPLL_PIN_TYPE_INPUT,
	ICE_DPLL_PIN_TYPE_OUTPUT,
	ICE_DPLL_PIN_TYPE_RCLK_INPUT,
	ICE_DPLL_PIN_TYPE_SOFTWARE,
	ICE_DPLL_PIN_TYPE_TXCLK,
};

static const char * const pin_type_name[] = {
	[ICE_DPLL_PIN_TYPE_INPUT] = "input",
	[ICE_DPLL_PIN_TYPE_OUTPUT] = "output",
	[ICE_DPLL_PIN_TYPE_RCLK_INPUT] = "rclk-input",
	[ICE_DPLL_PIN_TYPE_SOFTWARE] = "software",
	[ICE_DPLL_PIN_TYPE_TXCLK] = "txclk-input",
};

static const char * const ice_dpll_sw_pin_sma[] = { "SMA1", "SMA2" };
static const char * const ice_dpll_sw_pin_ufl[] = { "U.FL1", "U.FL2" };
static const char * const ice_dpll_ext_eref_pin = "EXT_EREF0";
static const char * const ice_dpll_fwnode_ext_synce = "clk_ref_synce";

static const struct dpll_pin_frequency ice_esync_range[] = {
	DPLL_PIN_FREQUENCY_RANGE(0, DPLL_PIN_FREQUENCY_1_HZ),
};

/**
 * ice_dpll_is_sw_pin - check if given pin shall be controlled by SW
 * @pf: private board structure
 * @index: index of a pin as understood by FW
 * @input: true for input, false for output
 *
 * Check if the pin shall be controlled by SW - instead of providing raw access
 * for pin control. For E810 NIC with dpll there is additional MUX-related logic
 * between SMA/U.FL pins/connectors and dpll device, best to give user access
 * with series of wrapper functions as from user perspective they convey single
 * functionality rather then separated pins.
 *
 * Return:
 * * true - pin controlled by SW
 * * false - pin not controlled by SW
 */
static bool ice_dpll_is_sw_pin(struct ice_pf *pf, u8 index, bool input)
{
	if (input && pf->hw.device_id == ICE_DEV_ID_E810C_QSFP)
		index -= ICE_DPLL_SW_PIN_INPUT_BASE_QSFP -
			 ICE_DPLL_SW_PIN_INPUT_BASE_SFP;

	if ((input && (index == ICE_DPLL_PIN_SW_1_INPUT_ABS_IDX ||
		       index == ICE_DPLL_PIN_SW_2_INPUT_ABS_IDX)) ||
	    (!input && (index == ICE_DPLL_PIN_SW_1_OUTPUT_ABS_IDX ||
			index == ICE_DPLL_PIN_SW_2_OUTPUT_ABS_IDX)))
		return true;
	return false;
}

/**
 * ice_dpll_is_reset - check if reset is in progress
 * @pf: private board structure
 * @extack: error reporting
 *
 * If reset is in progress, fill extack with error.
 *
 * Return:
 * * false - no reset in progress
 * * true - reset in progress
 */
static bool ice_dpll_is_reset(struct ice_pf *pf, struct netlink_ext_ack *extack)
{
	if (ice_is_reset_in_progress(pf->state)) {
		NL_SET_ERR_MSG(extack, "PF reset in progress");
		return true;
	}
	return false;
}

/**
 * ice_dpll_pin_freq_set - set pin's frequency
 * @pf: private board structure
 * @pin: pointer to a pin
 * @pin_type: type of pin being configured
 * @freq: frequency to be set
 * @extack: error reporting
 *
 * Set requested frequency on a pin.
 *
 * Context: Called under pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error on AQ or wrong pin type given
 */
static int
ice_dpll_pin_freq_set(struct ice_pf *pf, struct ice_dpll_pin *pin,
		      enum ice_dpll_pin_type pin_type, const u32 freq,
		      struct netlink_ext_ack *extack)
{
	u8 flags;
	int ret;

	switch (pin_type) {
	case ICE_DPLL_PIN_TYPE_INPUT:
		flags = ICE_AQC_SET_CGU_IN_CFG_FLG1_UPDATE_FREQ;
		ret = ice_aq_set_input_pin_cfg(&pf->hw, pin->idx, flags,
					       pin->flags[0], freq, 0);
		break;
	case ICE_DPLL_PIN_TYPE_OUTPUT:
		flags = ICE_AQC_SET_CGU_OUT_CFG_UPDATE_FREQ;
		ret = ice_aq_set_output_pin_cfg(&pf->hw, pin->idx, flags,
						0, freq, 0);
		break;
	default:
		return -EINVAL;
	}
	if (ret) {
		NL_SET_ERR_MSG_FMT(extack,
				   "err:%d %s failed to set pin freq:%u on pin:%u",
				   ret,
				   libie_aq_str(pf->hw.adminq.sq_last_status),
				   freq, pin->idx);
		return ret;
	}
	pin->freq = freq;

	return 0;
}

/**
 * ice_dpll_frequency_set - wrapper for pin callback for set frequency
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: pointer to dpll
 * @dpll_priv: private data pointer passed on dpll registration
 * @frequency: frequency to be set
 * @extack: error reporting
 * @pin_type: type of pin being configured
 *
 * Wraps internal set frequency command on a pin.
 *
 * Context: Acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error pin not found or couldn't set in hw
 */
static int
ice_dpll_frequency_set(const struct dpll_pin *pin, void *pin_priv,
		       const struct dpll_device *dpll, void *dpll_priv,
		       const u32 frequency,
		       struct netlink_ext_ack *extack,
		       enum ice_dpll_pin_type pin_type)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = d->pf;
	int ret;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;

	mutex_lock(&pf->dplls.lock);
	ret = ice_dpll_pin_freq_set(pf, p, pin_type, frequency, extack);
	mutex_unlock(&pf->dplls.lock);

	return ret;
}

/**
 * ice_dpll_input_frequency_set - input pin callback for set frequency
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: pointer to dpll
 * @dpll_priv: private data pointer passed on dpll registration
 * @frequency: frequency to be set
 * @extack: error reporting
 *
 * Wraps internal set frequency command on a pin.
 *
 * Context: Calls a function which acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error pin not found or couldn't set in hw
 */
static int
ice_dpll_input_frequency_set(const struct dpll_pin *pin, void *pin_priv,
			     const struct dpll_device *dpll, void *dpll_priv,
			     u64 frequency, struct netlink_ext_ack *extack)
{
	return ice_dpll_frequency_set(pin, pin_priv, dpll, dpll_priv, frequency,
				      extack, ICE_DPLL_PIN_TYPE_INPUT);
}

/**
 * ice_dpll_output_frequency_set - output pin callback for set frequency
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: pointer to dpll
 * @dpll_priv: private data pointer passed on dpll registration
 * @frequency: frequency to be set
 * @extack: error reporting
 *
 * Wraps internal set frequency command on a pin.
 *
 * Context: Calls a function which acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error pin not found or couldn't set in hw
 */
static int
ice_dpll_output_frequency_set(const struct dpll_pin *pin, void *pin_priv,
			      const struct dpll_device *dpll, void *dpll_priv,
			      u64 frequency, struct netlink_ext_ack *extack)
{
	return ice_dpll_frequency_set(pin, pin_priv, dpll, dpll_priv, frequency,
				      extack, ICE_DPLL_PIN_TYPE_OUTPUT);
}

/**
 * ice_dpll_frequency_get - wrapper for pin callback for get frequency
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: pointer to dpll
 * @dpll_priv: private data pointer passed on dpll registration
 * @frequency: on success holds pin's frequency
 * @extack: error reporting
 * @pin_type: type of pin being configured
 *
 * Wraps internal get frequency command of a pin.
 *
 * Context: Acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error pin not found or couldn't get from hw
 */
static int
ice_dpll_frequency_get(const struct dpll_pin *pin, void *pin_priv,
		       const struct dpll_device *dpll, void *dpll_priv,
		       u64 *frequency, struct netlink_ext_ack *extack,
		       enum ice_dpll_pin_type pin_type)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = d->pf;

	mutex_lock(&pf->dplls.lock);
	*frequency = p->freq;
	mutex_unlock(&pf->dplls.lock);

	return 0;
}

/**
 * ice_dpll_input_frequency_get - input pin callback for get frequency
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: pointer to dpll
 * @dpll_priv: private data pointer passed on dpll registration
 * @frequency: on success holds pin's frequency
 * @extack: error reporting
 *
 * Wraps internal get frequency command of a input pin.
 *
 * Context: Calls a function which acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error pin not found or couldn't get from hw
 */
static int
ice_dpll_input_frequency_get(const struct dpll_pin *pin, void *pin_priv,
			     const struct dpll_device *dpll, void *dpll_priv,
			     u64 *frequency, struct netlink_ext_ack *extack)
{
	return ice_dpll_frequency_get(pin, pin_priv, dpll, dpll_priv, frequency,
				      extack, ICE_DPLL_PIN_TYPE_INPUT);
}

/**
 * ice_dpll_output_frequency_get - output pin callback for get frequency
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: pointer to dpll
 * @dpll_priv: private data pointer passed on dpll registration
 * @frequency: on success holds pin's frequency
 * @extack: error reporting
 *
 * Wraps internal get frequency command of a pin.
 *
 * Context: Calls a function which acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error pin not found or couldn't get from hw
 */
static int
ice_dpll_output_frequency_get(const struct dpll_pin *pin, void *pin_priv,
			      const struct dpll_device *dpll, void *dpll_priv,
			      u64 *frequency, struct netlink_ext_ack *extack)
{
	return ice_dpll_frequency_get(pin, pin_priv, dpll, dpll_priv, frequency,
				      extack, ICE_DPLL_PIN_TYPE_OUTPUT);
}

/**
 * ice_dpll_sw_pin_frequency_set - callback to set frequency of SW pin
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: pointer to dpll
 * @dpll_priv: private data pointer passed on dpll registration
 * @frequency: on success holds pin's frequency
 * @extack: error reporting
 *
 * Calls set frequency command for corresponding and active input/output pin.
 *
 * Context: Calls a function which acquires and releases pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error pin not active or couldn't get from hw
 */
static int
ice_dpll_sw_pin_frequency_set(const struct dpll_pin *pin, void *pin_priv,
			      const struct dpll_device *dpll, void *dpll_priv,
			      u64 frequency, struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *sma = pin_priv;
	int ret;

	if (!sma->active) {
		NL_SET_ERR_MSG(extack, "pin is not active");
		return -EINVAL;
	}
	if (sma->direction == DPLL_PIN_DIRECTION_INPUT)
		ret = ice_dpll_input_frequency_set(NULL, sma->input, dpll,
						   dpll_priv, frequency,
						   extack);
	else
		ret = ice_dpll_output_frequency_set(NULL, sma->output, dpll,
						    dpll_priv, frequency,
						    extack);

	return ret;
}

/**
 * ice_dpll_sw_pin_frequency_get - callback for get frequency of SW pin
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: pointer to dpll
 * @dpll_priv: private data pointer passed on dpll registration
 * @frequency: on success holds pin's frequency
 * @extack: error reporting
 *
 * Calls get frequency command for corresponding active input/output.
 *
 * Context: Calls a function which acquires and releases pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error pin not active or couldn't get from hw
 */
static int
ice_dpll_sw_pin_frequency_get(const struct dpll_pin *pin, void *pin_priv,
			      const struct dpll_device *dpll, void *dpll_priv,
			      u64 *frequency, struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *sma = pin_priv;
	int ret;

	if (!sma->active) {
		*frequency = 0;
		return 0;
	}
	if (sma->direction == DPLL_PIN_DIRECTION_INPUT) {
		ret = ice_dpll_input_frequency_get(NULL, sma->input, dpll,
						   dpll_priv, frequency,
						   extack);
	} else {
		ret = ice_dpll_output_frequency_get(NULL, sma->output, dpll,
						    dpll_priv, frequency,
						    extack);
	}

	return ret;
}

/**
 * ice_dpll_pin_enable - enable a pin on dplls
 * @hw: board private hw structure
 * @pin: pointer to a pin
 * @dpll_idx: dpll index to connect to output pin
 * @pin_type: type of pin being enabled
 * @extack: error reporting
 *
 * Enable a pin on both dplls. Store current state in pin->flags.
 *
 * Context: Called under pf->dplls.lock
 * Return:
 * * 0 - OK
 * * negative - error
 */
static int
ice_dpll_pin_enable(struct ice_hw *hw, struct ice_dpll_pin *pin,
		    u8 dpll_idx, enum ice_dpll_pin_type pin_type,
		    struct netlink_ext_ack *extack)
{
	u8 flags = 0;
	int ret;

	switch (pin_type) {
	case ICE_DPLL_PIN_TYPE_INPUT:
		if (pin->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)
			flags |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
		flags |= ICE_AQC_SET_CGU_IN_CFG_FLG2_INPUT_EN;
		ret = ice_aq_set_input_pin_cfg(hw, pin->idx, 0, flags, 0, 0);
		break;
	case ICE_DPLL_PIN_TYPE_OUTPUT:
		flags = ICE_AQC_SET_CGU_OUT_CFG_UPDATE_SRC_SEL;
		if (pin->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)
			flags |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
		flags |= ICE_AQC_SET_CGU_OUT_CFG_OUT_EN;
		ret = ice_aq_set_output_pin_cfg(hw, pin->idx, flags, dpll_idx,
						0, 0);
		break;
	default:
		return -EINVAL;
	}
	if (ret)
		NL_SET_ERR_MSG_FMT(extack,
				   "err:%d %s failed to enable %s pin:%u",
				   ret, libie_aq_str(hw->adminq.sq_last_status),
				   pin_type_name[pin_type], pin->idx);

	return ret;
}

/**
 * ice_dpll_pin_disable - disable a pin on dplls
 * @hw: board private hw structure
 * @pin: pointer to a pin
 * @pin_type: type of pin being disabled
 * @extack: error reporting
 *
 * Disable a pin on both dplls. Store current state in pin->flags.
 *
 * Context: Called under pf->dplls.lock
 * Return:
 * * 0 - OK
 * * negative - error
 */
static int
ice_dpll_pin_disable(struct ice_hw *hw, struct ice_dpll_pin *pin,
		     enum ice_dpll_pin_type pin_type,
		     struct netlink_ext_ack *extack)
{
	u8 flags = 0;
	int ret;

	switch (pin_type) {
	case ICE_DPLL_PIN_TYPE_INPUT:
		if (pin->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)
			flags |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
		ret = ice_aq_set_input_pin_cfg(hw, pin->idx, 0, flags, 0, 0);
		break;
	case ICE_DPLL_PIN_TYPE_OUTPUT:
		if (pin->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)
			flags |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
		ret = ice_aq_set_output_pin_cfg(hw, pin->idx, flags, 0, 0, 0);
		break;
	default:
		return -EINVAL;
	}
	if (ret)
		NL_SET_ERR_MSG_FMT(extack,
				   "err:%d %s failed to disable %s pin:%u",
				   ret, libie_aq_str(hw->adminq.sq_last_status),
				   pin_type_name[pin_type], pin->idx);

	return ret;
}

/**
 * ice_dpll_pin_store_state - updates the state of pin in SW bookkeeping
 * @pin: pointer to a pin
 * @parent: parent pin index
 * @state: pin state (connected or disconnected)
 */
static void
ice_dpll_pin_store_state(struct ice_dpll_pin *pin, int parent, bool state)
{
	pin->state[parent] = state ? DPLL_PIN_STATE_CONNECTED :
					DPLL_PIN_STATE_DISCONNECTED;
}

/**
 * ice_dpll_rclk_update_e825c - updates the state of rclk pin on e825c device
 * @pf: private board struct
 * @pin: pointer to a pin
 *
 * Update struct holding pin states info, states are separate for each parent
 *
 * Context: Called under pf->dplls.lock
 * Return:
 * * 0 - OK
 * * negative - error
 */
static int ice_dpll_rclk_update_e825c(struct ice_pf *pf,
				      struct ice_dpll_pin *pin)
{
	u8 rclk_bits;
	int err;
	u32 reg;

	if (pf->dplls.rclk.num_parents > ICE_SYNCE_CLK_NUM)
		return -EINVAL;

	err = ice_read_cgu_reg(&pf->hw, ICE_CGU_R10, &reg);
	if (err)
		return err;

	rclk_bits = FIELD_GET(ICE_CGU_R10_SYNCE_S_REF_CLK, reg);
	ice_dpll_pin_store_state(pin, ICE_SYNCE_CLK0, rclk_bits ==
		(pf->ptp.port.port_num + ICE_CGU_BYPASS_MUX_OFFSET_E825C));

	err = ice_read_cgu_reg(&pf->hw, ICE_CGU_R11, &reg);
	if (err)
		return err;

	rclk_bits = FIELD_GET(ICE_CGU_R11_SYNCE_S_BYP_CLK, reg);
	ice_dpll_pin_store_state(pin, ICE_SYNCE_CLK1, rclk_bits ==
		(pf->ptp.port.port_num + ICE_CGU_BYPASS_MUX_OFFSET_E825C));

	return 0;
}

/**
 * ice_dpll_rclk_update - updates the state of rclk pin on a device
 * @pf: private board struct
 * @pin: pointer to a pin
 * @port_num: port number
 *
 * Update struct holding pin states info, states are separate for each parent
 *
 * Context: Called under pf->dplls.lock
 * Return:
 * * 0 - OK
 * * negative - error
 */
static int ice_dpll_rclk_update(struct ice_pf *pf, struct ice_dpll_pin *pin,
				u8 port_num)
{
	int ret;

	for (u8 parent = 0; parent < pf->dplls.rclk.num_parents; parent++) {
		u8 p = parent;

		ret = ice_aq_get_phy_rec_clk_out(&pf->hw, &p, &port_num,
						 &pin->flags[parent], NULL);
		if (ret)
			return ret;

		ice_dpll_pin_store_state(pin, parent,
					 ICE_AQC_GET_PHY_REC_CLK_OUT_OUT_EN &
					 pin->flags[parent]);
	}

	return 0;
}

/**
 * ice_dpll_sw_pins_update - update status of all SW pins
 * @pf: private board struct
 *
 * Determine and update pin struct fields (direction/active) of their current
 * values for all the SW controlled pins.
 *
 * Context: Call with pf->dplls.lock held
 * Return:
 * * 0 - OK
 * * negative - error
 */
static int
ice_dpll_sw_pins_update(struct ice_pf *pf)
{
	struct ice_dplls *d = &pf->dplls;
	struct ice_dpll_pin *p;
	u8 data = 0;
	int ret;

	ret = ice_read_sma_ctrl(&pf->hw, &data);
	if (ret)
		return ret;
	/* no change since last check */
	if (d->sma_data == data)
		return 0;

	/*
	 * SMA1/U.FL1 vs SMA2/U.FL2 are using different bit scheme to decide
	 * on their direction and if are active
	 */
	p = &d->sma[ICE_DPLL_PIN_SW_1_IDX];
	p->active = true;
	p->direction = DPLL_PIN_DIRECTION_INPUT;
	if (data & ICE_SMA1_DIR_EN) {
		p->direction = DPLL_PIN_DIRECTION_OUTPUT;
		if (data & ICE_SMA1_TX_EN)
			p->active = false;
	}

	p = &d->sma[ICE_DPLL_PIN_SW_2_IDX];
	p->active = true;
	p->direction = DPLL_PIN_DIRECTION_INPUT;
	if ((data & ICE_SMA2_INACTIVE_MASK) == ICE_SMA2_INACTIVE_MASK)
		p->active = false;
	else if (data & ICE_SMA2_DIR_EN)
		p->direction = DPLL_PIN_DIRECTION_OUTPUT;

	p = &d->ufl[ICE_DPLL_PIN_SW_1_IDX];
	if (!(data & (ICE_SMA1_DIR_EN | ICE_SMA1_TX_EN)))
		p->active = true;
	else
		p->active = false;

	p = &d->ufl[ICE_DPLL_PIN_SW_2_IDX];
	p->active = (data & ICE_SMA2_DIR_EN) && !(data & ICE_SMA2_UFL2_RX_DIS);
	d->sma_data = data;

	return 0;
}

/**
 * ice_dpll_pin_state_update - update pin's state
 * @pf: private board struct
 * @pin: structure with pin attributes to be updated
 * @pin_type: type of pin being updated
 * @extack: error reporting
 *
 * Determine pin current state and frequency, then update struct
 * holding the pin info. For input pin states are separated for each
 * dpll, for rclk pins states are separated for each parent.
 *
 * Context: Called under pf->dplls.lock
 * Return:
 * * 0 - OK
 * * negative - error
 */
static int
ice_dpll_pin_state_update(struct ice_pf *pf, struct ice_dpll_pin *pin,
			  enum ice_dpll_pin_type pin_type,
			  struct netlink_ext_ack *extack)
{
	u8 parent, port_num = ICE_AQC_SET_PHY_REC_CLK_OUT_CURR_PORT;
	int ret;

	switch (pin_type) {
	case ICE_DPLL_PIN_TYPE_INPUT:
		ret = ice_aq_get_input_pin_cfg(&pf->hw, pin->idx, &pin->status,
					       NULL, NULL, &pin->flags[0],
					       &pin->freq, &pin->phase_adjust);
		if (ret)
			goto err;
		if (ICE_AQC_GET_CGU_IN_CFG_FLG2_INPUT_EN & pin->flags[0]) {
			if (pin->pin) {
				pin->state[pf->dplls.eec.dpll_idx] =
					pin->pin == pf->dplls.eec.active_input ?
					DPLL_PIN_STATE_CONNECTED :
					DPLL_PIN_STATE_SELECTABLE;
				pin->state[pf->dplls.pps.dpll_idx] =
					pin->pin == pf->dplls.pps.active_input ?
					DPLL_PIN_STATE_CONNECTED :
					DPLL_PIN_STATE_SELECTABLE;
			} else {
				pin->state[pf->dplls.eec.dpll_idx] =
					DPLL_PIN_STATE_SELECTABLE;
				pin->state[pf->dplls.pps.dpll_idx] =
					DPLL_PIN_STATE_SELECTABLE;
			}
		} else {
			pin->state[pf->dplls.eec.dpll_idx] =
				DPLL_PIN_STATE_DISCONNECTED;
			pin->state[pf->dplls.pps.dpll_idx] =
				DPLL_PIN_STATE_DISCONNECTED;
		}
		break;
	case ICE_DPLL_PIN_TYPE_OUTPUT:
		ret = ice_aq_get_output_pin_cfg(&pf->hw, pin->idx,
						&pin->flags[0], &parent,
						&pin->freq, NULL);
		if (ret)
			goto err;

		parent &= ICE_AQC_GET_CGU_OUT_CFG_DPLL_SRC_SEL;
		if (ICE_AQC_GET_CGU_OUT_CFG_OUT_EN & pin->flags[0]) {
			pin->state[pf->dplls.eec.dpll_idx] =
				parent == pf->dplls.eec.dpll_idx ?
				DPLL_PIN_STATE_CONNECTED :
				DPLL_PIN_STATE_DISCONNECTED;
			pin->state[pf->dplls.pps.dpll_idx] =
				parent == pf->dplls.pps.dpll_idx ?
				DPLL_PIN_STATE_CONNECTED :
				DPLL_PIN_STATE_DISCONNECTED;
		} else {
			pin->state[pf->dplls.eec.dpll_idx] =
				DPLL_PIN_STATE_DISCONNECTED;
			pin->state[pf->dplls.pps.dpll_idx] =
				DPLL_PIN_STATE_DISCONNECTED;
		}
		break;
	case ICE_DPLL_PIN_TYPE_RCLK_INPUT:
		if (pf->hw.mac_type == ICE_MAC_GENERIC_3K_E825) {
			ret = ice_dpll_rclk_update_e825c(pf, pin);
			if (ret)
				goto err;
		} else {
			ret = ice_dpll_rclk_update(pf, pin, port_num);
			if (ret)
				goto err;
		}
		break;
	case ICE_DPLL_PIN_TYPE_SOFTWARE:
		ret = ice_dpll_sw_pins_update(pf);
		if (ret)
			goto err;
		break;
	default:
		return -EINVAL;
	}

	return 0;
err:
	if (extack)
		NL_SET_ERR_MSG_FMT(extack,
				   "err:%d %s failed to update %s pin:%u",
				   ret,
				   libie_aq_str(pf->hw.adminq.sq_last_status),
				   pin_type_name[pin_type], pin->idx);
	else if (pf->hw.adminq.sq_last_status != LIBIE_AQ_RC_EBUSY)
		dev_err_ratelimited(ice_pf_to_dev(pf),
				    "err:%d %s failed to update %s pin:%u\n",
				    ret,
				    libie_aq_str(pf->hw.adminq.sq_last_status),
				    pin_type_name[pin_type], pin->idx);
	return ret;
}

/**
 * ice_dpll_hw_input_prio_set - set input priority value in hardware
 * @pf: board private structure
 * @dpll: ice dpll pointer
 * @pin: ice pin pointer
 * @prio: priority value being set on a dpll
 * @extack: error reporting
 *
 * Internal wrapper for setting the priority in the hardware.
 *
 * Context: Called under pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - failure
 */
static int
ice_dpll_hw_input_prio_set(struct ice_pf *pf, struct ice_dpll *dpll,
			   struct ice_dpll_pin *pin, const u32 prio,
			   struct netlink_ext_ack *extack)
{
	int ret;

	ret = ice_aq_set_cgu_ref_prio(&pf->hw, dpll->dpll_idx, pin->idx,
				      (u8)prio);
	if (ret)
		NL_SET_ERR_MSG_FMT(extack,
				   "err:%d %s failed to set pin prio:%u on pin:%u",
				   ret,
				   libie_aq_str(pf->hw.adminq.sq_last_status),
				   prio, pin->idx);
	else
		dpll->input_prio[pin->idx] = prio;

	return ret;
}

/**
 * ice_dpll_lock_status_get - get dpll lock status callback
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @status: on success holds dpll's lock status
 * @status_error: status error value
 * @extack: error reporting
 *
 * Dpll subsystem callback, provides dpll's lock status.
 *
 * Context: Acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - failure
 */
static int
ice_dpll_lock_status_get(const struct dpll_device *dpll, void *dpll_priv,
			 enum dpll_lock_status *status,
			 enum dpll_lock_status_error *status_error,
			 struct netlink_ext_ack *extack)
{
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = d->pf;

	mutex_lock(&pf->dplls.lock);
	*status = d->dpll_state;
	mutex_unlock(&pf->dplls.lock);

	return 0;
}

/**
 * ice_dpll_mode_get - get dpll's working mode
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @mode: on success holds current working mode of dpll
 * @extack: error reporting
 *
 * Dpll subsystem callback. Provides working mode of dpll.
 *
 * Context: Acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - failure
 */
static int ice_dpll_mode_get(const struct dpll_device *dpll, void *dpll_priv,
			     enum dpll_mode *mode,
			     struct netlink_ext_ack *extack)
{
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = d->pf;

	mutex_lock(&pf->dplls.lock);
	*mode = d->mode;
	mutex_unlock(&pf->dplls.lock);

	return 0;
}

/**
 * ice_dpll_phase_offset_monitor_set - set phase offset monitor state
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @state: feature state to be set
 * @extack: error reporting
 *
 * Dpll subsystem callback. Enable/disable phase offset monitor feature of dpll.
 *
 * Context: Acquires and releases pf->dplls.lock
 * Return: 0 - success
 */
static int ice_dpll_phase_offset_monitor_set(const struct dpll_device *dpll,
					     void *dpll_priv,
					     enum dpll_feature_state state,
					     struct netlink_ext_ack *extack)
{
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = d->pf;

	mutex_lock(&pf->dplls.lock);
	if (state == DPLL_FEATURE_STATE_ENABLE)
		d->phase_offset_monitor_period = ICE_DPLL_PHASE_OFFSET_PERIOD;
	else
		d->phase_offset_monitor_period = 0;
	mutex_unlock(&pf->dplls.lock);

	return 0;
}

/**
 * ice_dpll_phase_offset_monitor_get - get phase offset monitor state
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @state: on success holds current state of phase offset monitor
 * @extack: error reporting
 *
 * Dpll subsystem callback. Provides current state of phase offset monitor
 * features on dpll device.
 *
 * Context: Acquires and releases pf->dplls.lock
 * Return: 0 - success
 */
static int ice_dpll_phase_offset_monitor_get(const struct dpll_device *dpll,
					     void *dpll_priv,
					     enum dpll_feature_state *state,
					     struct netlink_ext_ack *extack)
{
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = d->pf;

	mutex_lock(&pf->dplls.lock);
	if (d->phase_offset_monitor_period)
		*state = DPLL_FEATURE_STATE_ENABLE;
	else
		*state = DPLL_FEATURE_STATE_DISABLE;
	mutex_unlock(&pf->dplls.lock);

	return 0;
}

/**
 * ice_dpll_pin_state_set - set pin's state on dpll
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @enable: if pin shalll be enabled
 * @extack: error reporting
 * @pin_type: type of a pin
 *
 * Set pin state on a pin.
 *
 * Context: Acquires pf->dplls.lock
 * Return:
 * * 0 - OK or no change required
 * * negative - error
 */
static int
ice_dpll_pin_state_set(const struct dpll_pin *pin, void *pin_priv,
		       const struct dpll_device *dpll, void *dpll_priv,
		       bool enable, struct netlink_ext_ack *extack,
		       enum ice_dpll_pin_type pin_type)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = d->pf;
	int ret;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;

	mutex_lock(&pf->dplls.lock);
	if (enable)
		ret = ice_dpll_pin_enable(&pf->hw, p, d->dpll_idx, pin_type,
					  extack);
	else
		ret = ice_dpll_pin_disable(&pf->hw, p, pin_type, extack);
	if (!ret)
		ret = ice_dpll_pin_state_update(pf, p, pin_type, extack);
	mutex_unlock(&pf->dplls.lock);

	return ret;
}

/**
 * ice_dpll_output_state_set - enable/disable output pin on dpll device
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: dpll being configured
 * @dpll_priv: private data pointer passed on dpll registration
 * @state: state of pin to be set
 * @extack: error reporting
 *
 * Dpll subsystem callback. Set given state on output type pin.
 *
 * Context: Calls a function which acquires pf->dplls.lock
 * Return:
 * * 0 - successfully enabled mode
 * * negative - failed to enable mode
 */
static int
ice_dpll_output_state_set(const struct dpll_pin *pin, void *pin_priv,
			  const struct dpll_device *dpll, void *dpll_priv,
			  enum dpll_pin_state state,
			  struct netlink_ext_ack *extack)
{
	bool enable = state == DPLL_PIN_STATE_CONNECTED;
	struct ice_dpll_pin *p = pin_priv;
	struct ice_dpll *d = dpll_priv;

	if (state == DPLL_PIN_STATE_SELECTABLE)
		return -EINVAL;
	if (!enable && p->state[d->dpll_idx] == DPLL_PIN_STATE_DISCONNECTED)
		return 0;

	return ice_dpll_pin_state_set(pin, pin_priv, dpll, dpll_priv, enable,
				      extack, ICE_DPLL_PIN_TYPE_OUTPUT);
}

/**
 * ice_dpll_input_state_set - enable/disable input pin on dpll levice
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: dpll being configured
 * @dpll_priv: private data pointer passed on dpll registration
 * @state: state of pin to be set
 * @extack: error reporting
 *
 * Dpll subsystem callback. Enables given mode on input type pin.
 *
 * Context: Calls a function which acquires pf->dplls.lock
 * Return:
 * * 0 - successfully enabled mode
 * * negative - failed to enable mode
 */
static int
ice_dpll_input_state_set(const struct dpll_pin *pin, void *pin_priv,
			 const struct dpll_device *dpll, void *dpll_priv,
			 enum dpll_pin_state state,
			 struct netlink_ext_ack *extack)
{
	bool enable = state == DPLL_PIN_STATE_SELECTABLE;

	return ice_dpll_pin_state_set(pin, pin_priv, dpll, dpll_priv, enable,
				      extack, ICE_DPLL_PIN_TYPE_INPUT);
}

/**
 * ice_dpll_pin_state_get - set pin's state on dpll
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @state: on success holds state of the pin
 * @extack: error reporting
 * @pin_type: type of questioned pin
 *
 * Determine pin state set it on a pin.
 *
 * Context: Acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - failed to get state
 */
static int
ice_dpll_pin_state_get(const struct dpll_pin *pin, void *pin_priv,
		       const struct dpll_device *dpll, void *dpll_priv,
		       enum dpll_pin_state *state,
		       struct netlink_ext_ack *extack,
		       enum ice_dpll_pin_type pin_type)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = d->pf;
	int ret;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;

	mutex_lock(&pf->dplls.lock);
	ret = ice_dpll_pin_state_update(pf, p, pin_type, extack);
	if (ret)
		goto unlock;
	if (pin_type == ICE_DPLL_PIN_TYPE_INPUT ||
	    pin_type == ICE_DPLL_PIN_TYPE_OUTPUT)
		*state = p->state[d->dpll_idx];
	ret = 0;
unlock:
	mutex_unlock(&pf->dplls.lock);

	return ret;
}

/**
 * ice_dpll_output_state_get - get output pin state on dpll device
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @state: on success holds state of the pin
 * @extack: error reporting
 *
 * Dpll subsystem callback. Check state of a pin.
 *
 * Context: Calls a function which acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - failed to get state
 */
static int
ice_dpll_output_state_get(const struct dpll_pin *pin, void *pin_priv,
			  const struct dpll_device *dpll, void *dpll_priv,
			  enum dpll_pin_state *state,
			  struct netlink_ext_ack *extack)
{
	return ice_dpll_pin_state_get(pin, pin_priv, dpll, dpll_priv, state,
				      extack, ICE_DPLL_PIN_TYPE_OUTPUT);
}

/**
 * ice_dpll_input_state_get - get input pin state on dpll device
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @state: on success holds state of the pin
 * @extack: error reporting
 *
 * Dpll subsystem callback. Check state of a input pin.
 *
 * Context: Calls a function which acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - failed to get state
 */
static int
ice_dpll_input_state_get(const struct dpll_pin *pin, void *pin_priv,
			 const struct dpll_device *dpll, void *dpll_priv,
			 enum dpll_pin_state *state,
			 struct netlink_ext_ack *extack)
{
	return ice_dpll_pin_state_get(pin, pin_priv, dpll, dpll_priv, state,
				      extack, ICE_DPLL_PIN_TYPE_INPUT);
}

/**
 * ice_dpll_sw_pin_notify_peer - notify the paired SW pin after a state change
 * @d: pointer to dplls struct
 * @changed: the SW pin that was explicitly changed (already notified by dpll core)
 *
 * SMA and U.FL pins share physical signal paths in pairs (SMA1/U.FL1 and
 * SMA2/U.FL2).  When one pin's routing changes via the PCA9575 GPIO
 * expander, the paired pin's state may also change.  Send a change
 * notification for the peer pin so userspace consumers monitoring the
 * peer via dpll netlink learn about the update.
 *
 * Context: Called from dpll_pin_ops callbacks after pf->dplls.lock is
 *          released.  Uses __dpll_pin_change_ntf() because dpll_lock is
 *          still held by the dpll netlink layer.
 */
static void ice_dpll_sw_pin_notify_peer(struct ice_dplls *d,
					struct ice_dpll_pin *changed)
{
	struct ice_dpll_pin *peer;

	peer = (changed >= d->sma && changed < d->sma + ICE_DPLL_PIN_SW_NUM) ?
		&d->ufl[changed->idx] : &d->sma[changed->idx];
	if (peer->pin)
		__dpll_pin_change_ntf(peer->pin);
}

/**
 * ice_dpll_sma_direction_set - set direction of SMA pin
 * @p: pointer to a pin
 * @direction: requested direction of the pin
 * @extack: error reporting
 *
 * Wrapper for dpll subsystem callback. Set direction of a SMA pin.
 *
 * Context: Call with pf->dplls.lock held
 * Return:
 * * 0 - success
 * * negative - failed to get state
 */
static int ice_dpll_sma_direction_set(struct ice_dpll_pin *p,
				      enum dpll_pin_direction direction,
				      struct netlink_ext_ack *extack)
{
	struct ice_dplls *d = &p->pf->dplls;
	struct ice_dpll_pin *peer;
	u8 data;
	int ret;

	if (p->direction == direction && p->active)
		return 0;
	ret = ice_read_sma_ctrl(&p->pf->hw, &data);
	if (ret)
		return ret;

	switch (p->idx) {
	case ICE_DPLL_PIN_SW_1_IDX:
		data &= ~ICE_SMA1_MASK;
		if (direction == DPLL_PIN_DIRECTION_OUTPUT)
			data |= ICE_SMA1_DIR_EN;
		break;
	case ICE_DPLL_PIN_SW_2_IDX:
		if (direction == DPLL_PIN_DIRECTION_INPUT) {
			data &= ~ICE_SMA2_DIR_EN;
			data |= ICE_SMA2_UFL2_RX_DIS;
		} else {
			data &= ~(ICE_SMA2_TX_EN | ICE_SMA2_UFL2_RX_DIS);
			data |= ICE_SMA2_DIR_EN;
		}
		break;
	default:
		return -EINVAL;
	}
	ret = ice_write_sma_ctrl(&p->pf->hw, data);
	if (!ret)
		ret = ice_dpll_pin_state_update(p->pf, p,
						ICE_DPLL_PIN_TYPE_SOFTWARE,
						extack);
	if (ret)
		return ret;

	/* When a direction change activates the paired U.FL pin, enable
	 * its backing CGU pin so the pin reports as connected. Without
	 * this the U.FL routing is correct but the CGU pin stays disabled
	 * and userspace sees the pin as disconnected.  Do not disable the
	 * backing pin when U.FL becomes inactive because the SMA pin may
	 * still be using it.
	 */
	peer = &d->ufl[p->idx];
	if (peer->active) {
		struct ice_dpll_pin *target;
		enum ice_dpll_pin_type type;

		if (peer->output) {
			target = peer->output;
			type = ICE_DPLL_PIN_TYPE_OUTPUT;
		} else {
			target = peer->input;
			type = ICE_DPLL_PIN_TYPE_INPUT;
		}
		ret = ice_dpll_pin_enable(&p->pf->hw, target,
					  d->eec.dpll_idx, type, extack);
		if (!ret)
			ret = ice_dpll_pin_state_update(p->pf, target,
							type, extack);
	}

	return ret;
}

/**
 * ice_dpll_ufl_pin_state_set - set U.FL pin state on dpll device
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @state: requested state of the pin
 * @extack: error reporting
 *
 * Dpll subsystem callback. Set the state of a pin.
 *
 * Context: Acquires and releases pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_ufl_pin_state_set(const struct dpll_pin *pin, void *pin_priv,
			   const struct dpll_device *dpll, void *dpll_priv,
			   enum dpll_pin_state state,
			   struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv, *target;
	struct ice_dpll *d = dpll_priv;
	enum ice_dpll_pin_type type;
	struct ice_pf *pf = p->pf;
	struct ice_hw *hw;
	bool enable;
	u8 data;
	int ret;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;

	mutex_lock(&pf->dplls.lock);
	hw = &pf->hw;
	ret = ice_read_sma_ctrl(hw, &data);
	if (ret)
		goto unlock;

	ret = -EINVAL;
	switch (p->idx) {
	case ICE_DPLL_PIN_SW_1_IDX:
		if (state == DPLL_PIN_STATE_CONNECTED) {
			data &= ~ICE_SMA1_MASK;
			enable = true;
		} else if (state == DPLL_PIN_STATE_DISCONNECTED) {
			/* Skip if U.FL1 is not active, setting TX_EN
			 * while DIR_EN is set would also deactivate
			 * the paired SMA1 output.
			 */
			if (data & (ICE_SMA1_DIR_EN | ICE_SMA1_TX_EN)) {
				ret = 0;
				goto unlock;
			}
			data |= ICE_SMA1_TX_EN;
			enable = false;
		} else {
			goto unlock;
		}
		target = p->output;
		type = ICE_DPLL_PIN_TYPE_OUTPUT;
		break;
	case ICE_DPLL_PIN_SW_2_IDX:
		if (state == DPLL_PIN_STATE_SELECTABLE) {
			data |= ICE_SMA2_DIR_EN;
			data &= ~ICE_SMA2_UFL2_RX_DIS;
			enable = true;
		} else if (state == DPLL_PIN_STATE_DISCONNECTED) {
			/* Skip if U.FL2 is not active, setting
			 * UFL2_RX_DIS could also disable the paired
			 * SMA2 input.
			 */
			if (!(data & ICE_SMA2_DIR_EN) ||
			    (data & ICE_SMA2_UFL2_RX_DIS)) {
				ret = 0;
				goto unlock;
			}
			data |= ICE_SMA2_UFL2_RX_DIS;
			enable = false;
		} else {
			goto unlock;
		}
		target = p->input;
		type = ICE_DPLL_PIN_TYPE_INPUT;
		break;
	default:
		goto unlock;
	}

	ret = ice_write_sma_ctrl(hw, data);
	if (ret)
		goto unlock;
	ret = ice_dpll_pin_state_update(pf, p, ICE_DPLL_PIN_TYPE_SOFTWARE,
					extack);
	if (ret)
		goto unlock;

	if (enable)
		ret = ice_dpll_pin_enable(hw, target, d->dpll_idx, type, extack);
	else
		ret = ice_dpll_pin_disable(hw, target, type, extack);
	if (!ret)
		ret = ice_dpll_pin_state_update(pf, target, type, extack);

unlock:
	mutex_unlock(&pf->dplls.lock);
	if (!ret)
		ice_dpll_sw_pin_notify_peer(&pf->dplls, p);

	return ret;
}

/**
 * ice_dpll_sw_pin_state_get - get SW pin state
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @state: on success holds state of the pin
 * @extack: error reporting
 *
 * Dpll subsystem callback. Check state of a SW pin.
 *
 * Context: Acquires and releases pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_sw_pin_state_get(const struct dpll_pin *pin, void *pin_priv,
			  const struct dpll_device *dpll, void *dpll_priv,
			  enum dpll_pin_state *state,
			  struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = p->pf;
	int ret = 0;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;
	mutex_lock(&pf->dplls.lock);
	if (!p->active) {
		*state = DPLL_PIN_STATE_DISCONNECTED;
		goto unlock;
	}

	if (p->direction == DPLL_PIN_DIRECTION_INPUT) {
		ret = ice_dpll_pin_state_update(pf, p->input,
						ICE_DPLL_PIN_TYPE_INPUT,
						extack);
		if (ret)
			goto unlock;
		*state = p->input->state[d->dpll_idx];
	} else {
		ret = ice_dpll_pin_state_update(pf, p->output,
						ICE_DPLL_PIN_TYPE_OUTPUT,
						extack);
		if (ret)
			goto unlock;
		*state = p->output->state[d->dpll_idx];
	}
unlock:
	mutex_unlock(&pf->dplls.lock);

	return ret;
}

/**
 * ice_dpll_sma_pin_state_set - set SMA pin state on dpll device
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @state: requested state of the pin
 * @extack: error reporting
 *
 * Dpll subsystem callback. Set state of a pin.
 *
 * Context: Acquires and releases pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - failed to get state
 */
static int
ice_dpll_sma_pin_state_set(const struct dpll_pin *pin, void *pin_priv,
			   const struct dpll_device *dpll, void *dpll_priv,
			   enum dpll_pin_state state,
			   struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *sma = pin_priv, *target;
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = sma->pf;
	enum ice_dpll_pin_type type;
	bool enable;
	int ret;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;

	mutex_lock(&pf->dplls.lock);
	if (!sma->active) {
		ret = ice_dpll_sma_direction_set(sma, sma->direction, extack);
		if (ret)
			goto unlock;
	}
	if (sma->direction == DPLL_PIN_DIRECTION_INPUT) {
		enable = state == DPLL_PIN_STATE_SELECTABLE;
		target = sma->input;
		type = ICE_DPLL_PIN_TYPE_INPUT;
	} else {
		enable = state == DPLL_PIN_STATE_CONNECTED;
		target = sma->output;
		type = ICE_DPLL_PIN_TYPE_OUTPUT;
	}

	if (enable)
		ret = ice_dpll_pin_enable(&pf->hw, target, d->dpll_idx, type,
					  extack);
	else
		ret = ice_dpll_pin_disable(&pf->hw, target, type, extack);
	if (!ret)
		ret = ice_dpll_pin_state_update(pf, target, type, extack);

unlock:
	mutex_unlock(&pf->dplls.lock);
	if (!ret)
		ice_dpll_sw_pin_notify_peer(&pf->dplls, sma);

	return ret;
}

/**
 * ice_dpll_input_prio_get - get dpll's input prio
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @prio: on success - returns input priority on dpll
 * @extack: error reporting
 *
 * Dpll subsystem callback. Handler for getting priority of a input pin.
 *
 * Context: Acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - failure
 */
static int
ice_dpll_input_prio_get(const struct dpll_pin *pin, void *pin_priv,
			const struct dpll_device *dpll, void *dpll_priv,
			u32 *prio, struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = d->pf;

	mutex_lock(&pf->dplls.lock);
	*prio = d->input_prio[p->idx];
	mutex_unlock(&pf->dplls.lock);

	return 0;
}

/**
 * ice_dpll_input_prio_set - set dpll input prio
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @prio: input priority to be set on dpll
 * @extack: error reporting
 *
 * Dpll subsystem callback. Handler for setting priority of a input pin.
 *
 * Context: Acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - failure
 */
static int
ice_dpll_input_prio_set(const struct dpll_pin *pin, void *pin_priv,
			const struct dpll_device *dpll, void *dpll_priv,
			u32 prio, struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = d->pf;
	int ret;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;

	mutex_lock(&pf->dplls.lock);
	ret = ice_dpll_hw_input_prio_set(pf, d, p, prio, extack);
	mutex_unlock(&pf->dplls.lock);

	return ret;
}

static int
ice_dpll_sw_input_prio_get(const struct dpll_pin *pin, void *pin_priv,
			   const struct dpll_device *dpll, void *dpll_priv,
			   u32 *prio, struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = d->pf;

	mutex_lock(&pf->dplls.lock);
	if (p->input && p->direction == DPLL_PIN_DIRECTION_INPUT)
		*prio = d->input_prio[p->input->idx];
	else
		*prio = ICE_DPLL_PIN_PRIO_OUTPUT;
	mutex_unlock(&pf->dplls.lock);

	return 0;
}

static int
ice_dpll_sw_input_prio_set(const struct dpll_pin *pin, void *pin_priv,
			   const struct dpll_device *dpll, void *dpll_priv,
			   u32 prio, struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = d->pf;
	int ret;

	if (!p->input || p->direction != DPLL_PIN_DIRECTION_INPUT)
		return -EINVAL;
	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;

	mutex_lock(&pf->dplls.lock);
	ret = ice_dpll_hw_input_prio_set(pf, d, p->input, prio, extack);
	mutex_unlock(&pf->dplls.lock);

	return ret;
}

/**
 * ice_dpll_input_direction - callback for get input pin direction
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @direction: holds input pin direction
 * @extack: error reporting
 *
 * Dpll subsystem callback. Handler for getting direction of a input pin.
 *
 * Return:
 * * 0 - success
 */
static int
ice_dpll_input_direction(const struct dpll_pin *pin, void *pin_priv,
			 const struct dpll_device *dpll, void *dpll_priv,
			 enum dpll_pin_direction *direction,
			 struct netlink_ext_ack *extack)
{
	*direction = DPLL_PIN_DIRECTION_INPUT;

	return 0;
}

/**
 * ice_dpll_output_direction - callback for get output pin direction
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @direction: holds output pin direction
 * @extack: error reporting
 *
 * Dpll subsystem callback. Handler for getting direction of an output pin.
 *
 * Return:
 * * 0 - success
 */
static int
ice_dpll_output_direction(const struct dpll_pin *pin, void *pin_priv,
			  const struct dpll_device *dpll, void *dpll_priv,
			  enum dpll_pin_direction *direction,
			  struct netlink_ext_ack *extack)
{
	*direction = DPLL_PIN_DIRECTION_OUTPUT;

	return 0;
}

/**
 * ice_dpll_pin_sma_direction_set - callback for set SMA pin direction
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @direction: requested pin direction
 * @extack: error reporting
 *
 * Dpll subsystem callback. Handler for setting direction of a SMA pin.
 *
 * Context: Acquires and releases pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_pin_sma_direction_set(const struct dpll_pin *pin, void *pin_priv,
			       const struct dpll_device *dpll, void *dpll_priv,
			       enum dpll_pin_direction direction,
			       struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_pf *pf = p->pf;
	int ret;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;

	mutex_lock(&pf->dplls.lock);
	ret = ice_dpll_sma_direction_set(p, direction, extack);
	mutex_unlock(&pf->dplls.lock);
	if (!ret)
		ice_dpll_sw_pin_notify_peer(&pf->dplls, p);

	return ret;
}

/**
 * ice_dpll_pin_sw_direction_get - callback for get SW pin direction
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @direction: on success holds pin direction
 * @extack: error reporting
 *
 * Dpll subsystem callback. Handler for getting direction of a SMA pin.
 *
 * Context: Acquires and releases pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_pin_sw_direction_get(const struct dpll_pin *pin, void *pin_priv,
			      const struct dpll_device *dpll, void *dpll_priv,
			      enum dpll_pin_direction *direction,
			      struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_pf *pf = p->pf;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;
	mutex_lock(&pf->dplls.lock);
	*direction = p->direction;
	mutex_unlock(&pf->dplls.lock);

	return 0;
}

/**
 * ice_dpll_pin_phase_adjust_get - callback for get pin phase adjust value
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @phase_adjust: on success holds pin phase_adjust value
 * @extack: error reporting
 *
 * Dpll subsystem callback. Handler for getting phase adjust value of a pin.
 *
 * Context: Acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_pin_phase_adjust_get(const struct dpll_pin *pin, void *pin_priv,
			      const struct dpll_device *dpll, void *dpll_priv,
			      s32 *phase_adjust,
			      struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_pf *pf = p->pf;

	mutex_lock(&pf->dplls.lock);
	*phase_adjust = p->phase_adjust;
	mutex_unlock(&pf->dplls.lock);

	return 0;
}

/**
 * ice_dpll_pin_phase_adjust_set - helper for setting a pin phase adjust value
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @phase_adjust: phase_adjust to be set
 * @extack: error reporting
 * @type: type of a pin
 *
 * Helper for dpll subsystem callback. Handler for setting phase adjust value
 * of a pin.
 *
 * Context: Acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_pin_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
			      const struct dpll_device *dpll, void *dpll_priv,
			      s32 phase_adjust,
			      struct netlink_ext_ack *extack,
			      enum ice_dpll_pin_type type)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = d->pf;
	u8 flag, flags_en = 0;
	int ret;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;

	mutex_lock(&pf->dplls.lock);
	switch (type) {
	case ICE_DPLL_PIN_TYPE_INPUT:
		flag = ICE_AQC_SET_CGU_IN_CFG_FLG1_UPDATE_DELAY;
		if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)
			flags_en |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
		if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_INPUT_EN)
			flags_en |= ICE_AQC_SET_CGU_IN_CFG_FLG2_INPUT_EN;
		ret = ice_aq_set_input_pin_cfg(&pf->hw, p->idx, flag, flags_en,
					       0, phase_adjust);
		break;
	case ICE_DPLL_PIN_TYPE_OUTPUT:
		flag = ICE_AQC_SET_CGU_OUT_CFG_UPDATE_PHASE;
		if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_OUT_EN)
			flag |= ICE_AQC_SET_CGU_OUT_CFG_OUT_EN;
		if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)
			flag |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
		ret = ice_aq_set_output_pin_cfg(&pf->hw, p->idx, flag, 0, 0,
						phase_adjust);
		break;
	default:
		ret = -EINVAL;
	}
	if (!ret)
		p->phase_adjust = phase_adjust;
	mutex_unlock(&pf->dplls.lock);
	if (ret)
		NL_SET_ERR_MSG_FMT(extack,
				   "err:%d %s failed to set pin phase_adjust:%d for pin:%u on dpll:%u",
				   ret,
				   libie_aq_str(pf->hw.adminq.sq_last_status),
				   phase_adjust, p->idx, d->dpll_idx);

	return ret;
}

/**
 * ice_dpll_input_phase_adjust_set - callback for set input pin phase adjust
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @phase_adjust: phase_adjust to be set
 * @extack: error reporting
 *
 * Dpll subsystem callback. Wraps a handler for setting phase adjust on input
 * pin.
 *
 * Context: Calls a function which acquires and releases pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_input_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
				const struct dpll_device *dpll, void *dpll_priv,
				s32 phase_adjust,
				struct netlink_ext_ack *extack)
{
	return ice_dpll_pin_phase_adjust_set(pin, pin_priv, dpll, dpll_priv,
					     phase_adjust, extack,
					     ICE_DPLL_PIN_TYPE_INPUT);
}

/**
 * ice_dpll_output_phase_adjust_set - callback for set output pin phase adjust
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @phase_adjust: phase_adjust to be set
 * @extack: error reporting
 *
 * Dpll subsystem callback. Wraps a handler for setting phase adjust on output
 * pin.
 *
 * Context: Calls a function which acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_output_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
				 const struct dpll_device *dpll, void *dpll_priv,
				 s32 phase_adjust,
				 struct netlink_ext_ack *extack)
{
	return ice_dpll_pin_phase_adjust_set(pin, pin_priv, dpll, dpll_priv,
					     phase_adjust, extack,
					     ICE_DPLL_PIN_TYPE_OUTPUT);
}

/**
 * ice_dpll_sw_phase_adjust_get - callback for get SW pin phase adjust
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @phase_adjust: on success holds phase adjust value
 * @extack: error reporting
 *
 * Dpll subsystem callback. Wraps a handler for getting phase adjust on sw
 * pin.
 *
 * Context: Calls a function which acquires and releases pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_sw_phase_adjust_get(const struct dpll_pin *pin, void *pin_priv,
			     const struct dpll_device *dpll, void *dpll_priv,
			     s32 *phase_adjust,
			     struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;

	if (p->direction == DPLL_PIN_DIRECTION_INPUT)
		return ice_dpll_pin_phase_adjust_get(p->input->pin, p->input,
						     dpll, dpll_priv,
						     phase_adjust, extack);
	else
		return ice_dpll_pin_phase_adjust_get(p->output->pin, p->output,
						     dpll, dpll_priv,
						     phase_adjust, extack);
}

/**
 * ice_dpll_sw_phase_adjust_set - callback for set SW pin phase adjust value
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @phase_adjust: phase_adjust to be set
 * @extack: error reporting
 *
 * Dpll subsystem callback. Wraps a handler for setting phase adjust on output
 * pin.
 *
 * Context: Calls a function which acquires and releases pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_sw_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
			     const struct dpll_device *dpll, void *dpll_priv,
			     s32 phase_adjust,
			     struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;

	if (!p->active) {
		NL_SET_ERR_MSG(extack, "pin is not active");
		return -EINVAL;
	}
	if (p->direction == DPLL_PIN_DIRECTION_INPUT)
		return ice_dpll_pin_phase_adjust_set(p->input->pin, p->input,
						     dpll, dpll_priv,
						     phase_adjust, extack,
						     ICE_DPLL_PIN_TYPE_INPUT);
	else
		return ice_dpll_pin_phase_adjust_set(p->output->pin, p->output,
						     dpll, dpll_priv,
						     phase_adjust, extack,
						     ICE_DPLL_PIN_TYPE_OUTPUT);
}

#define ICE_DPLL_PHASE_OFFSET_DIVIDER	100
#define ICE_DPLL_PHASE_OFFSET_FACTOR		\
	(DPLL_PHASE_OFFSET_DIVIDER / ICE_DPLL_PHASE_OFFSET_DIVIDER)
/**
 * ice_dpll_phase_offset_get - callback for get dpll phase shift value
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @phase_offset: on success holds pin phase_offset value
 * @extack: error reporting
 *
 * Dpll subsystem callback. Handler for getting phase shift value between
 * dpll's input and output.
 *
 * Context: Acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_phase_offset_get(const struct dpll_pin *pin, void *pin_priv,
			  const struct dpll_device *dpll, void *dpll_priv,
			  s64 *phase_offset, struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = d->pf;

	mutex_lock(&pf->dplls.lock);
	if (d->active_input == pin || (p->input &&
				       d->active_input == p->input->pin))
		*phase_offset = d->phase_offset * ICE_DPLL_PHASE_OFFSET_FACTOR;
	else if (d->phase_offset_monitor_period)
		*phase_offset = (p->input &&
				 p->direction == DPLL_PIN_DIRECTION_INPUT ?
				 p->input->phase_offset :
				 p->phase_offset) * ICE_DPLL_PHASE_OFFSET_FACTOR;
	else
		*phase_offset = 0;
	mutex_unlock(&pf->dplls.lock);

	return 0;
}

/**
 * ice_dpll_synce_update_e825c - setting PHY recovered clock pins on e825c
 * @hw: Pointer to the HW struct
 * @ena: true if enable, false in disable
 * @port_num: port number
 * @output: output pin, we have two in E825C
 *
 * DPLL subsystem callback. Set proper signals to recover clock from port.
 *
 * Context: Called under pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int ice_dpll_synce_update_e825c(struct ice_hw *hw, bool ena,
				       u32 port_num, enum ice_synce_clk output)
{
	int err;

	/* configure the mux to deliver proper signal to DPLL from the MUX */
	err = ice_tspll_cfg_bypass_mux_e825c(hw, ena, port_num, output);
	if (err)
		return err;

	err = ice_tspll_cfg_synce_ethdiv_e825c(hw, output);
	if (err)
		return err;

	dev_dbg(ice_hw_to_dev(hw), "CLK_SYNCE%u recovered clock: pin %s\n",
		output, str_enabled_disabled(ena));

	return 0;
}

/**
 * ice_dpll_output_esync_set - callback for setting embedded sync
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @freq: requested embedded sync frequency
 * @extack: error reporting
 *
 * Dpll subsystem callback. Handler for setting embedded sync frequency value
 * on output pin.
 *
 * Context: Acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_output_esync_set(const struct dpll_pin *pin, void *pin_priv,
			  const struct dpll_device *dpll, void *dpll_priv,
			  u64 freq, struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = d->pf;
	u8 flags = 0;
	int ret;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;
	mutex_lock(&pf->dplls.lock);
	if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_OUT_EN)
		flags = ICE_AQC_SET_CGU_OUT_CFG_OUT_EN;
	if (freq == DPLL_PIN_FREQUENCY_1_HZ) {
		if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN) {
			ret = 0;
		} else {
			flags |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
			ret = ice_aq_set_output_pin_cfg(&pf->hw, p->idx, flags,
							0, 0, 0);
		}
	} else {
		if (!(p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)) {
			ret = 0;
		} else {
			flags &= ~ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
			ret = ice_aq_set_output_pin_cfg(&pf->hw, p->idx, flags,
							0, 0, 0);
		}
	}
	mutex_unlock(&pf->dplls.lock);

	return ret;
}

/**
 * ice_dpll_output_esync_get - callback for getting embedded sync config
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @esync: on success holds embedded sync pin properties
 * @extack: error reporting
 *
 * Dpll subsystem callback. Handler for getting embedded sync frequency value
 * and capabilities on output pin.
 *
 * Context: Acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_output_esync_get(const struct dpll_pin *pin, void *pin_priv,
			  const struct dpll_device *dpll, void *dpll_priv,
			  struct dpll_pin_esync *esync,
			  struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = d->pf;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;
	mutex_lock(&pf->dplls.lock);
	if (!(p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_ABILITY) ||
	    p->freq != DPLL_PIN_FREQUENCY_10_MHZ) {
		mutex_unlock(&pf->dplls.lock);
		return -EOPNOTSUPP;
	}
	esync->range = ice_esync_range;
	esync->range_num = ARRAY_SIZE(ice_esync_range);
	if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN) {
		esync->freq = DPLL_PIN_FREQUENCY_1_HZ;
		esync->pulse = ICE_DPLL_PIN_ESYNC_PULSE_HIGH_PERCENT;
	} else {
		esync->freq = 0;
		esync->pulse = 0;
	}
	mutex_unlock(&pf->dplls.lock);

	return 0;
}

/**
 * ice_dpll_input_esync_set - callback for setting embedded sync
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @freq: requested embedded sync frequency
 * @extack: error reporting
 *
 * Dpll subsystem callback. Handler for setting embedded sync frequency value
 * on input pin.
 *
 * Context: Acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_input_esync_set(const struct dpll_pin *pin, void *pin_priv,
			 const struct dpll_device *dpll, void *dpll_priv,
			 u64 freq, struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = d->pf;
	u8 flags_en = 0;
	int ret;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;
	mutex_lock(&pf->dplls.lock);
	if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_INPUT_EN)
		flags_en = ICE_AQC_SET_CGU_IN_CFG_FLG2_INPUT_EN;
	if (freq == DPLL_PIN_FREQUENCY_1_HZ) {
		if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN) {
			ret = 0;
		} else {
			flags_en |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
			ret = ice_aq_set_input_pin_cfg(&pf->hw, p->idx, 0,
						       flags_en, 0, 0);
		}
	} else {
		if (!(p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)) {
			ret = 0;
		} else {
			flags_en &= ~ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
			ret = ice_aq_set_input_pin_cfg(&pf->hw, p->idx, 0,
						       flags_en, 0, 0);
		}
	}
	mutex_unlock(&pf->dplls.lock);

	return ret;
}

/**
 * ice_dpll_input_esync_get - callback for getting embedded sync config
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @esync: on success holds embedded sync pin properties
 * @extack: error reporting
 *
 * Dpll subsystem callback. Handler for getting embedded sync frequency value
 * and capabilities on input pin.
 *
 * Context: Acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_input_esync_get(const struct dpll_pin *pin, void *pin_priv,
			 const struct dpll_device *dpll, void *dpll_priv,
			 struct dpll_pin_esync *esync,
			 struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_dpll *d = dpll_priv;
	struct ice_pf *pf = d->pf;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;
	mutex_lock(&pf->dplls.lock);
	if (!(p->status & ICE_AQC_GET_CGU_IN_CFG_STATUS_ESYNC_CAP) ||
	    p->freq != DPLL_PIN_FREQUENCY_10_MHZ) {
		mutex_unlock(&pf->dplls.lock);
		return -EOPNOTSUPP;
	}
	esync->range = ice_esync_range;
	esync->range_num = ARRAY_SIZE(ice_esync_range);
	if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN) {
		esync->freq = DPLL_PIN_FREQUENCY_1_HZ;
		esync->pulse = ICE_DPLL_PIN_ESYNC_PULSE_HIGH_PERCENT;
	} else {
		esync->freq = 0;
		esync->pulse = 0;
	}
	mutex_unlock(&pf->dplls.lock);

	return 0;
}

/**
 * ice_dpll_sw_esync_set - callback for setting embedded sync on SW pin
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @freq: requested embedded sync frequency
 * @extack: error reporting
 *
 * Dpll subsystem callback. Handler for setting embedded sync frequency value
 * on SW pin.
 *
 * Context: Calls a function which acquires and releases pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_sw_esync_set(const struct dpll_pin *pin, void *pin_priv,
		      const struct dpll_device *dpll, void *dpll_priv,
		      u64 freq, struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;

	if (!p->active) {
		NL_SET_ERR_MSG(extack, "pin is not active");
		return -EINVAL;
	}
	if (p->direction == DPLL_PIN_DIRECTION_INPUT)
		return ice_dpll_input_esync_set(p->input->pin, p->input, dpll,
						dpll_priv, freq, extack);
	else
		return ice_dpll_output_esync_set(p->output->pin, p->output,
						 dpll, dpll_priv, freq, extack);
}

/**
 * ice_dpll_sw_esync_get - callback for getting embedded sync on SW pin
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @esync: on success holds embedded sync frequency and properties
 * @extack: error reporting
 *
 * Dpll subsystem callback. Handler for getting embedded sync frequency value
 * of SW pin.
 *
 * Context: Calls a function which acquires and releases pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_sw_esync_get(const struct dpll_pin *pin, void *pin_priv,
		      const struct dpll_device *dpll, void *dpll_priv,
		      struct dpll_pin_esync *esync,
		      struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;

	if (p->direction == DPLL_PIN_DIRECTION_INPUT)
		return ice_dpll_input_esync_get(p->input->pin, p->input, dpll,
						dpll_priv, esync, extack);
	else
		return ice_dpll_output_esync_get(p->output->pin, p->output,
						 dpll, dpll_priv, esync,
						 extack);
}

/*
 * ice_dpll_input_ref_sync_set - callback for setting reference sync feature
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @ref_pin: pin pointer for reference sync pair
 * @ref_pin_priv: private data pointer of ref_pin
 * @state: requested state for reference sync for pin pair
 * @extack: error reporting
 *
 * Dpll subsystem callback. Handler for setting reference sync frequency
 * feature for input pin.
 *
 * Context: Acquires and releases pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_input_ref_sync_set(const struct dpll_pin *pin, void *pin_priv,
			    const struct dpll_pin *ref_pin, void *ref_pin_priv,
			    const enum dpll_pin_state state,
			    struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_pf *pf = p->pf;
	u8 flags_en = 0;
	int ret;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;
	mutex_lock(&pf->dplls.lock);

	if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_INPUT_EN)
		flags_en = ICE_AQC_SET_CGU_IN_CFG_FLG2_INPUT_EN;
	if (state == DPLL_PIN_STATE_CONNECTED)
		flags_en |= ICE_AQC_CGU_IN_CFG_FLG2_REFSYNC_EN;
	ret = ice_aq_set_input_pin_cfg(&pf->hw, p->idx, 0, flags_en, 0, 0);
	if (!ret)
		ret = ice_dpll_pin_state_update(pf, p, ICE_DPLL_PIN_TYPE_INPUT,
						extack);
	mutex_unlock(&pf->dplls.lock);

	return ret;
}

/**
 * ice_dpll_input_ref_sync_get - callback for getting reference sync config
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @ref_pin: pin pointer for reference sync pair
 * @ref_pin_priv: private data pointer of ref_pin
 * @state: on success holds reference sync state for pin pair
 * @extack: error reporting
 *
 * Dpll subsystem callback. Handler for setting reference sync frequency
 * feature for input pin.
 *
 * Context: Acquires and releases pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_input_ref_sync_get(const struct dpll_pin *pin, void *pin_priv,
			    const struct dpll_pin *ref_pin, void *ref_pin_priv,
			    enum dpll_pin_state *state,
			    struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_pf *pf = p->pf;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;
	mutex_lock(&pf->dplls.lock);
	if (p->flags[0] & ICE_AQC_CGU_IN_CFG_FLG2_REFSYNC_EN)
		*state = DPLL_PIN_STATE_CONNECTED;
	else
		*state = DPLL_PIN_STATE_DISCONNECTED;
	mutex_unlock(&pf->dplls.lock);

	return 0;
}

/*
 * ice_dpll_sw_input_ref_sync_set - callback for setting reference sync feature
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @ref_pin: pin pointer for reference sync pair
 * @ref_pin_priv: private data pointer of ref_pin
 * @state: requested state for reference sync for pin pair
 * @extack: error reporting
 *
 * Dpll subsystem callback. Handler for setting reference sync
 * feature for input pins.
 *
 * Context: Calls a function which acquires and releases pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_sw_input_ref_sync_set(const struct dpll_pin *pin, void *pin_priv,
			       const struct dpll_pin *ref_pin,
			       void *ref_pin_priv,
			       const enum dpll_pin_state state,
			       struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;

	return ice_dpll_input_ref_sync_set(pin, p->input, ref_pin, ref_pin_priv,
					   state, extack);
}

/**
 * ice_dpll_sw_input_ref_sync_get - callback for getting reference sync config
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @ref_pin: pin pointer for reference sync pair
 * @ref_pin_priv: private data pointer of ref_pin
 * @state: on success holds reference sync state for pin pair
 * @extack: error reporting
 *
 * Dpll subsystem callback. Handler for setting reference sync feature for
 * input pins.
 *
 * Context: Calls a function which acquires and releases pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - error
 */
static int
ice_dpll_sw_input_ref_sync_get(const struct dpll_pin *pin, void *pin_priv,
			       const struct dpll_pin *ref_pin,
			       void *ref_pin_priv,
			       enum dpll_pin_state *state,
			       struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;

	return ice_dpll_input_ref_sync_get(pin, p->input, ref_pin, ref_pin_priv,
					   state, extack);
}

static int
ice_dpll_pin_get_parent_num(struct ice_dpll_pin *pin,
			    const struct dpll_pin *parent)
{
	int i;

	for (i = 0; i < pin->num_parents; i++)
		if (pin->pf->dplls.inputs[pin->parent_idx[i]].pin == parent)
			return i;

	return -ENOENT;
}

static int
ice_dpll_pin_get_parent_idx(struct ice_dpll_pin *pin,
			    const struct dpll_pin *parent)
{
	int num = ice_dpll_pin_get_parent_num(pin, parent);

	return num < 0 ? num : pin->parent_idx[num];
}

/**
 * ice_dpll_rclk_state_on_pin_set - set a state on rclk pin
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @parent_pin: pin parent pointer
 * @parent_pin_priv: parent private data pointer passed on pin registration
 * @state: state to be set on pin
 * @extack: error reporting
 *
 * Dpll subsystem callback, set a state of a rclk pin on a parent pin
 *
 * Context: Acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - failure
 */
static int
ice_dpll_rclk_state_on_pin_set(const struct dpll_pin *pin, void *pin_priv,
			       const struct dpll_pin *parent_pin,
			       void *parent_pin_priv,
			       enum dpll_pin_state state,
			       struct netlink_ext_ack *extack)
{
	bool enable = state == DPLL_PIN_STATE_CONNECTED;
	struct ice_dpll_pin *p = pin_priv;
	struct ice_pf *pf = p->pf;
	struct ice_hw *hw;
	int ret = -EINVAL;
	int hw_idx;

	hw = &pf->hw;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;

	mutex_lock(&pf->dplls.lock);
	hw_idx = ice_dpll_pin_get_parent_idx(p, parent_pin);
	if (hw_idx < 0)
		goto unlock;
	hw_idx -= pf->dplls.base_rclk_idx;
	if (hw_idx >= ICE_DPLL_RCLK_NUM_MAX)
		goto unlock;

	if ((enable && p->state[hw_idx] == DPLL_PIN_STATE_CONNECTED) ||
	    (!enable && p->state[hw_idx] == DPLL_PIN_STATE_DISCONNECTED)) {
		NL_SET_ERR_MSG_FMT(extack,
				   "pin:%u state:%u on parent:%u already set",
				   p->idx, state,
				   ice_dpll_pin_get_parent_num(p, parent_pin));
		goto unlock;
	}

	ret = hw->mac_type == ICE_MAC_GENERIC_3K_E825 ?
		ice_dpll_synce_update_e825c(hw, enable,
					    pf->ptp.port.port_num,
					    (enum ice_synce_clk)hw_idx) :
		ice_aq_set_phy_rec_clk_out(hw, hw_idx, enable, &p->freq);
	if (ret)
		NL_SET_ERR_MSG_FMT(extack,
				   "err:%d %s failed to set pin state:%u for pin:%u on parent:%u",
				   ret,
				   libie_aq_str(hw->adminq.sq_last_status),
				   state, p->idx,
				   ice_dpll_pin_get_parent_num(p, parent_pin));
unlock:
	mutex_unlock(&pf->dplls.lock);

	return ret;
}

/**
 * ice_dpll_rclk_state_on_pin_get - get a state of rclk pin
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @parent_pin: pin parent pointer
 * @parent_pin_priv: pin parent priv data pointer passed on pin registration
 * @state: on success holds pin state on parent pin
 * @extack: error reporting
 *
 * dpll subsystem callback, get a state of a recovered clock pin.
 *
 * Context: Acquires pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - failure
 */
static int
ice_dpll_rclk_state_on_pin_get(const struct dpll_pin *pin, void *pin_priv,
			       const struct dpll_pin *parent_pin,
			       void *parent_pin_priv,
			       enum dpll_pin_state *state,
			       struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_pf *pf = p->pf;
	int ret = -EINVAL;
	int hw_idx;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;

	mutex_lock(&pf->dplls.lock);
	hw_idx = ice_dpll_pin_get_parent_idx(p, parent_pin);
	if (hw_idx < 0)
		goto unlock;
	hw_idx -= pf->dplls.base_rclk_idx;
	if (hw_idx >= ICE_DPLL_RCLK_NUM_MAX)
		goto unlock;

	ret = ice_dpll_pin_state_update(pf, p, ICE_DPLL_PIN_TYPE_RCLK_INPUT,
					extack);
	if (ret)
		goto unlock;

	*state = p->state[hw_idx];
	ret = 0;
unlock:
	mutex_unlock(&pf->dplls.lock);

	return ret;
}

/**
 * ice_dpll_txclk_work - apply a pending TX reference clock change
 * @work: work_struct embedded in struct ice_dplls
 *
 * This worker executes an outstanding TX reference clock switch request
 * that was previously queued via the DPLL TXCLK pin set callback.
 *
 * The worker performs only the operational part of the switch, issuing
 * the necessary firmware commands to request a new TX reference clock
 * selection (e.g. triggering an AN restart). It does not verify whether
 * the requested clock was ultimately accepted by the hardware.
 *
 * Hardware verification, software state reconciliation, pin state
 * notification, and TXC DPLL lock-status updates are performed later,
 * after link-up, by ice_txclk_update_and_notify().
 *
 * Context:
 *   - Runs in process context on pf->dplls.wq and may sleep.
 *   - Serializes access to shared TXCLK state using pf->dplls.lock.
 */
static void ice_dpll_txclk_work(struct work_struct *work)
{
	struct ice_dplls *dplls =
		container_of(work, struct ice_dplls, txclk_work);
	struct ice_pf *pf = container_of(dplls, struct ice_pf, dplls);
	struct dpll_pin *old_pin = NULL;
	struct dpll_pin *new_pin = NULL;
	enum ice_e825c_ref_clk clk;
	bool do_switch;
	int err;

	mutex_lock(&pf->dplls.lock);
	do_switch = pf->dplls.txclk_switch_requested;
	clk = pf->ptp.port.tx_clk_req;
	mutex_unlock(&pf->dplls.lock);

	if (!do_switch)
		return;

	err = ice_txclk_set_clk(pf, clk);

	mutex_lock(&pf->dplls.lock);
	/* Only clear the request flag if no newer request arrived while
	 * the lock was dropped. Otherwise leave it set so the re-queued
	 * worker run picks up the updated tx_clk_req value.
	 */
	if (pf->ptp.port.tx_clk_req == clk)
		pf->dplls.txclk_switch_requested = false;
	if (err) {
		/* Roll back the requested clock to match the current hardware
		 * state so that ice_txclk_update_and_notify() does not
		 * misinterpret a future link-up as a failed switch. Only roll
		 * back if no newer request arrived in the meantime; otherwise
		 * the re-queued worker run will apply the updated value.
		 */
		dev_err(ice_pf_to_dev(pf),
			"TX clock switch to %u failed, err=%d; reverting\n",
			clk, err);
		if (pf->ptp.port.tx_clk_req == clk) {
			/* Capture pins for post-unlock notification so that
			 * userspace observes the requested pin flipping back
			 * to DISCONNECTED and the effective pin to CONNECTED.
			 */
			new_pin = ice_txclk_get_pin(pf, clk);
			old_pin = ice_txclk_get_pin(pf, pf->ptp.port.tx_clk);
			pf->ptp.port.tx_clk_req = pf->ptp.port.tx_clk;
		}
	}
	mutex_unlock(&pf->dplls.lock);

	if (old_pin)
		dpll_pin_change_ntf(old_pin);
	if (new_pin)
		dpll_pin_change_ntf(new_pin);
}

/**
 * ice_dpll_txclk_state_on_dpll_set - set a state on TX clk pin
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @state: state to be set on pin
 * @extack: error reporting
 *
 * Dpll subsystem callback, set a state of a Tx reference clock pin
 *
 * Context: Acquires and releases pf->dplls.lock.
 * Return:
 * * 0 - success
 * * negative - failure
 */
static int
ice_dpll_txclk_state_on_dpll_set(const struct dpll_pin *pin, void *pin_priv,
				 const struct dpll_device *dpll,
				 void *dpll_priv, enum dpll_pin_state state,
				 struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_pf *pf = p->pf;
	enum ice_e825c_ref_clk new_clk;
	int ret = 0;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;

	if (state != DPLL_PIN_STATE_CONNECTED &&
	    state != DPLL_PIN_STATE_DISCONNECTED) {
		NL_SET_ERR_MSG(extack,
			       "unsupported pin state for TX reference clock");
		return -EINVAL;
	}

	/* Check ICE_FLAG_DPLL and queue_work() under pf->dplls.lock.
	 * ice_dpll_deinit() clears the flag under the same lock before
	 * cancel_work_sync() and wq destruction, so a callback arriving
	 * after teardown observes the cleared flag and bails out.
	 */
	mutex_lock(&pf->dplls.lock);
	if (!test_bit(ICE_FLAG_DPLL, pf->flags)) {
		ret = -ENODEV;
		goto unlock;
	}
	if (state == DPLL_PIN_STATE_DISCONNECTED &&
	    p->tx_ref_src != pf->ptp.port.tx_clk_req)
		goto unlock;

	new_clk = (state == DPLL_PIN_STATE_DISCONNECTED) ? ICE_REF_CLK_ENET :
			p->tx_ref_src;
	if (new_clk == pf->ptp.port.tx_clk_req)
		goto unlock;

	pf->ptp.port.tx_clk_req = new_clk;
	pf->dplls.txclk_switch_requested = true;
	queue_work(pf->dplls.wq, &pf->dplls.txclk_work);
unlock:
	mutex_unlock(&pf->dplls.lock);
	return ret;
}

/**
 * ice_dpll_txclk_state_on_dpll_get - get a state of Tx clk reference pin
 * @pin: pointer to a pin
 * @pin_priv: private data pointer passed on pin registration
 * @dpll: registered dpll pointer
 * @dpll_priv: private data pointer passed on dpll registration
 * @state: on success holds pin state on parent pin
 * @extack: error reporting
 *
 * TXCLK DPLL pin state is derived and not stored explicitly.
 *
 * Only external TX reference clocks (SYNCE, EREF0) are modeled
 * as DPLL pins. The internal ENET (TXCO) clock has no pin and,
 * when selected, all TXCLK pins are reported DISCONNECTED.
 *
 * During a pending TXCLK switch, the requested pin may be
 * reported as CONNECTED before hardware verification.
 * Hardware acceptance and synchronization are reported
 * exclusively via TXC DPLL lock-status.
 *
 * Context: Acquires and releases pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - failure
 */
static int
ice_dpll_txclk_state_on_dpll_get(const struct dpll_pin *pin, void *pin_priv,
				 const struct dpll_device *dpll,
				 void *dpll_priv,
				 enum dpll_pin_state *state,
				 struct netlink_ext_ack *extack)
{
	struct ice_dpll_pin *p = pin_priv;
	struct ice_pf *pf = p->pf;

	if (ice_dpll_is_reset(pf, extack))
		return -EBUSY;

	mutex_lock(&pf->dplls.lock);
	if (pf->ptp.port.tx_clk_req == p->tx_ref_src)
		*state = DPLL_PIN_STATE_CONNECTED;
	else
		*state = DPLL_PIN_STATE_DISCONNECTED;
	mutex_unlock(&pf->dplls.lock);

	return 0;
}

static const struct dpll_pin_ops ice_dpll_rclk_ops = {
	.state_on_pin_set = ice_dpll_rclk_state_on_pin_set,
	.state_on_pin_get = ice_dpll_rclk_state_on_pin_get,
	.direction_get = ice_dpll_input_direction,
};

static const struct dpll_pin_ops ice_dpll_txclk_ops = {
	.state_on_dpll_set = ice_dpll_txclk_state_on_dpll_set,
	.state_on_dpll_get = ice_dpll_txclk_state_on_dpll_get,
	.direction_get = ice_dpll_input_direction,
};

static const struct dpll_pin_ops ice_dpll_pin_sma_ops = {
	.state_on_dpll_set = ice_dpll_sma_pin_state_set,
	.state_on_dpll_get = ice_dpll_sw_pin_state_get,
	.direction_get = ice_dpll_pin_sw_direction_get,
	.direction_set = ice_dpll_pin_sma_direction_set,
	.prio_get = ice_dpll_sw_input_prio_get,
	.prio_set = ice_dpll_sw_input_prio_set,
	.frequency_get = ice_dpll_sw_pin_frequency_get,
	.frequency_set = ice_dpll_sw_pin_frequency_set,
	.phase_adjust_get = ice_dpll_sw_phase_adjust_get,
	.phase_adjust_set = ice_dpll_sw_phase_adjust_set,
	.phase_offset_get = ice_dpll_phase_offset_get,
	.esync_set = ice_dpll_sw_esync_set,
	.esync_get = ice_dpll_sw_esync_get,
	.ref_sync_set = ice_dpll_sw_input_ref_sync_set,
	.ref_sync_get = ice_dpll_sw_input_ref_sync_get,
};

static const struct dpll_pin_ops ice_dpll_pin_ufl_ops = {
	.state_on_dpll_set = ice_dpll_ufl_pin_state_set,
	.state_on_dpll_get = ice_dpll_sw_pin_state_get,
	.direction_get = ice_dpll_pin_sw_direction_get,
	.prio_get = ice_dpll_sw_input_prio_get,
	.prio_set = ice_dpll_sw_input_prio_set,
	.frequency_get = ice_dpll_sw_pin_frequency_get,
	.frequency_set = ice_dpll_sw_pin_frequency_set,
	.esync_set = ice_dpll_sw_esync_set,
	.esync_get = ice_dpll_sw_esync_get,
	.phase_adjust_get = ice_dpll_sw_phase_adjust_get,
	.phase_adjust_set = ice_dpll_sw_phase_adjust_set,
	.phase_offset_get = ice_dpll_phase_offset_get,
};

static const struct dpll_pin_ops ice_dpll_input_ops = {
	.frequency_get = ice_dpll_input_frequency_get,
	.frequency_set = ice_dpll_input_frequency_set,
	.state_on_dpll_get = ice_dpll_input_state_get,
	.state_on_dpll_set = ice_dpll_input_state_set,
	.prio_get = ice_dpll_input_prio_get,
	.prio_set = ice_dpll_input_prio_set,
	.direction_get = ice_dpll_input_direction,
	.phase_adjust_get = ice_dpll_pin_phase_adjust_get,
	.phase_adjust_set = ice_dpll_input_phase_adjust_set,
	.phase_offset_get = ice_dpll_phase_offset_get,
	.esync_set = ice_dpll_input_esync_set,
	.esync_get = ice_dpll_input_esync_get,
	.ref_sync_set = ice_dpll_input_ref_sync_set,
	.ref_sync_get = ice_dpll_input_ref_sync_get,
};

static const struct dpll_pin_ops ice_dpll_output_ops = {
	.frequency_get = ice_dpll_output_frequency_get,
	.frequency_set = ice_dpll_output_frequency_set,
	.state_on_dpll_get = ice_dpll_output_state_get,
	.state_on_dpll_set = ice_dpll_output_state_set,
	.direction_get = ice_dpll_output_direction,
	.phase_adjust_get = ice_dpll_pin_phase_adjust_get,
	.phase_adjust_set = ice_dpll_output_phase_adjust_set,
	.esync_set = ice_dpll_output_esync_set,
	.esync_get = ice_dpll_output_esync_get,
};

static const struct dpll_device_ops ice_dpll_ops = {
	.lock_status_get = ice_dpll_lock_status_get,
	.mode_get = ice_dpll_mode_get,
};

static const struct dpll_device_ops ice_dpll_pom_ops = {
	.lock_status_get = ice_dpll_lock_status_get,
	.mode_get = ice_dpll_mode_get,
	.phase_offset_monitor_set = ice_dpll_phase_offset_monitor_set,
	.phase_offset_monitor_get = ice_dpll_phase_offset_monitor_get,
};

/**
 * ice_generate_clock_id - generates unique clock_id for registering dpll.
 * @pf: board private structure
 *
 * Generates unique (per board) clock_id for allocation and search of dpll
 * devices in Linux dpll subsystem.
 *
 * Return: generated clock id for the board
 */
static u64 ice_generate_clock_id(struct ice_pf *pf)
{
	return pci_get_dsn(pf->pdev);
}

/**
 * ice_dpll_pin_ntf - notify pin change including any SW pin wrappers
 * @dplls: pointer to dplls struct
 * @pin: the dpll_pin that changed
 *
 * Send a change notification for @pin and for any registered SMA/U.FL pin
 * whose backing CGU input matches @pin.
 */
static void ice_dpll_pin_ntf(struct ice_dplls *dplls, struct dpll_pin *pin)
{
	dpll_pin_change_ntf(pin);
	for (int i = 0; i < ICE_DPLL_PIN_SW_NUM; i++) {
		if (dplls->sma[i].pin && dplls->sma[i].input &&
		    dplls->sma[i].input->pin == pin)
			dpll_pin_change_ntf(dplls->sma[i].pin);
		if (dplls->ufl[i].pin && dplls->ufl[i].input &&
		    dplls->ufl[i].input->pin == pin)
			dpll_pin_change_ntf(dplls->ufl[i].pin);
	}
}

/**
 * ice_dpll_notify_changes - notify dpll subsystem about changes
 * @d: pointer do dpll
 *
 * Once change detected appropriate event is submitted to the dpll subsystem.
 */
static void ice_dpll_notify_changes(struct ice_dpll *d)
{
	struct ice_dplls *dplls = &d->pf->dplls;
	bool pin_notified = false;

	if (d->prev_dpll_state != d->dpll_state) {
		d->prev_dpll_state = d->dpll_state;
		dpll_device_change_ntf(d->dpll);
	}
	if (d->prev_input != d->active_input) {
		if (d->prev_input)
			ice_dpll_pin_ntf(dplls, d->prev_input);
		d->prev_input = d->active_input;
		if (d->active_input) {
			ice_dpll_pin_ntf(dplls, d->active_input);
			pin_notified = true;
		}
	}
	if (d->prev_phase_offset != d->phase_offset) {
		d->prev_phase_offset = d->phase_offset;
		if (!pin_notified && d->active_input)
			ice_dpll_pin_ntf(dplls, d->active_input);
	}
}

/**
 * ice_dpll_is_pps_phase_monitor - check if dpll capable of phase offset monitor
 * @pf: pf private structure
 *
 * Check if firmware is capable of supporting admin command to provide
 * phase offset monitoring on all the input pins on PPS dpll.
 *
 * Returns:
 * * true - PPS dpll phase offset monitoring is supported
 * * false - PPS dpll phase offset monitoring is not supported
 */
static bool ice_dpll_is_pps_phase_monitor(struct ice_pf *pf)
{
	struct ice_cgu_input_measure meas[ICE_DPLL_INPUT_REF_NUM];
	int ret = ice_aq_get_cgu_input_pin_measure(&pf->hw, DPLL_TYPE_PPS, meas,
						   ARRAY_SIZE(meas));

	if (ret && pf->hw.adminq.sq_last_status == LIBIE_AQ_RC_ESRCH)
		return false;

	return true;
}

/**
 * ice_dpll_pins_notify_mask - notify dpll subsystem about bulk pin changes
 * @dplls: pointer to dplls struct
 * @pins: array of ice_dpll_pin pointers registered within dpll subsystem
 * @pin_num: number of pins
 * @phase_offset_ntf_mask: bitmask of pin indexes to notify
 *
 * Iterate over array of pins and call dpll subsystem pin notify if
 * corresponding pin index within bitmask is set.
 *
 * Context: Must be called while pf->dplls.lock is released.
 */
static void ice_dpll_pins_notify_mask(struct ice_dplls *dplls,
				      struct ice_dpll_pin *pins,
				      u8 pin_num,
				      u32 phase_offset_ntf_mask)
{
	for (int i = 0; i < pin_num; i++)
		if (phase_offset_ntf_mask & BIT(i))
			ice_dpll_pin_ntf(dplls, pins[i].pin);
}

/**
 * ice_dpll_pps_update_phase_offsets - update phase offset measurements
 * @pf: pf private structure
 * @phase_offset_pins_updated: returns mask of updated input pin indexes
 *
 * Read phase offset measurements for PPS dpll device and store values in
 * input pins array. On success phase_offset_pins_updated - fills bitmask of
 * updated input pin indexes, pins shall be notified.
 *
 * Context: Shall be called with pf->dplls.lock being locked.
 * Returns:
 * * 0 - success or no data available
 * * negative - AQ failure
 */
static int ice_dpll_pps_update_phase_offsets(struct ice_pf *pf,
					     u32 *phase_offset_pins_updated)
{
	struct ice_cgu_input_measure meas[ICE_DPLL_INPUT_REF_NUM];
	struct ice_dpll_pin *p;
	s64 phase_offset, tmp;
	int i, j, ret;

	*phase_offset_pins_updated = 0;
	ret = ice_aq_get_cgu_input_pin_measure(&pf->hw, DPLL_TYPE_PPS, meas,
					       ARRAY_SIZE(meas));
	if (ret && (pf->hw.adminq.sq_last_status == LIBIE_AQ_RC_EAGAIN ||
		    pf->hw.adminq.sq_last_status == LIBIE_AQ_RC_EBUSY)) {
		return 0;
	} else if (ret) {
		dev_err(ice_pf_to_dev(pf),
			"failed to get input pin measurements dpll=%d, ret=%d %s\n",
			DPLL_TYPE_PPS, ret,
			libie_aq_str(pf->hw.adminq.sq_last_status));
		return ret;
	}
	for (i = 0; i < pf->dplls.num_inputs; i++) {
		p = &pf->dplls.inputs[i];
		phase_offset = 0;
		for (j = 0; j < ICE_CGU_INPUT_PHASE_OFFSET_BYTES; j++) {
			tmp = meas[i].phase_offset[j];
#ifdef __LITTLE_ENDIAN
			phase_offset += tmp << 8 * j;
#else
			phase_offset += tmp << 8 *
				(ICE_CGU_INPUT_PHASE_OFFSET_BYTES - 1 - j);
#endif
		}
		phase_offset = sign_extend64(phase_offset, 47);
		if (p->phase_offset != phase_offset) {
			dev_dbg(ice_pf_to_dev(pf),
				"phase offset changed for pin:%d old:%llx, new:%llx\n",
				p->idx, p->phase_offset, phase_offset);
			p->phase_offset = phase_offset;
			*phase_offset_pins_updated |= (1 << i);
		}
	}

	return 0;
}

/**
 * ice_dpll_update_state - update dpll state
 * @pf: pf private structure
 * @d: pointer to queried dpll device
 * @init: if function called on initialization of ice dpll
 *
 * Poll current state of dpll from hw and update ice_dpll struct.
 *
 * Context: Called by kworker under pf->dplls.lock
 * Return:
 * * 0 - success
 * * negative - AQ failure
 */
static int
ice_dpll_update_state(struct ice_pf *pf, struct ice_dpll *d, bool init)
{
	struct ice_dpll_pin *p = NULL;
	int ret;

	ret = ice_get_cgu_state(&pf->hw, d->dpll_idx, d->prev_dpll_state,
				&d->input_idx, &d->ref_state, &d->eec_mode,
				&d->phase_offset, &d->dpll_state);

	dev_dbg(ice_pf_to_dev(pf),
		"update dpll=%d, prev_src_idx:%u, src_idx:%u, state:%d, prev:%d mode:%d\n",
		d->dpll_idx, d->prev_input_idx, d->input_idx,
		d->dpll_state, d->prev_dpll_state, d->mode);
	if (ret) {
		/* EBUSY is expected during reset recovery, don't log error */
		if (pf->hw.adminq.sq_last_status != LIBIE_AQ_RC_EBUSY)
			dev_err(ice_pf_to_dev(pf),
				"update dpll=%d state failed, ret=%d %s\n",
				d->dpll_idx, ret,
				libie_aq_str(pf->hw.adminq.sq_last_status));
		return ret;
	}
	if (init) {
		if (d->dpll_state == DPLL_LOCK_STATUS_LOCKED ||
		    d->dpll_state == DPLL_LOCK_STATUS_LOCKED_HO_ACQ)
			d->active_input = pf->dplls.inputs[d->input_idx].pin;
		p = &pf->dplls.inputs[d->input_idx];
		return ice_dpll_pin_state_update(pf, p,
						 ICE_DPLL_PIN_TYPE_INPUT, NULL);
	}
	if (d->dpll_state == DPLL_LOCK_STATUS_HOLDOVER ||
	    d->dpll_state == DPLL_LOCK_STATUS_UNLOCKED) {
		d->active_input = NULL;
		if (d->input_idx != ICE_DPLL_PIN_IDX_INVALID)
			p = &pf->dplls.inputs[d->input_idx];
		d->prev_input_idx = ICE_DPLL_PIN_IDX_INVALID;
		d->input_idx = ICE_DPLL_PIN_IDX_INVALID;
		if (!p)
			return 0;
		ret = ice_dpll_pin_state_update(pf, p,
						ICE_DPLL_PIN_TYPE_INPUT, NULL);
	} else if (d->input_idx != d->prev_input_idx) {
		if (d->prev_input_idx != ICE_DPLL_PIN_IDX_INVALID) {
			p = &pf->dplls.inputs[d->prev_input_idx];
			ice_dpll_pin_state_update(pf, p,
						  ICE_DPLL_PIN_TYPE_INPUT,
						  NULL);
		}
		if (d->input_idx != ICE_DPLL_PIN_IDX_INVALID) {
			p = &pf->dplls.inputs[d->input_idx];
			d->active_input = p->pin;
			ice_dpll_pin_state_update(pf, p,
						  ICE_DPLL_PIN_TYPE_INPUT,
						  NULL);
		}
		d->prev_input_idx = d->input_idx;
	}

	return ret;
}

/**
 * ice_dpll_periodic_work - DPLLs periodic worker
 * @work: pointer to kthread_work structure
 *
 * DPLLs periodic worker is responsible for polling state of dpll.
 * Context: Holds pf->dplls.lock
 */
static void ice_dpll_periodic_work(struct kthread_work *work)
{
	struct ice_dplls *d = container_of(work, struct ice_dplls, work.work);
	struct ice_pf *pf = container_of(d, struct ice_pf, dplls);
	struct ice_dpll *de = &pf->dplls.eec;
	struct ice_dpll *dp = &pf->dplls.pps;
	u32 phase_offset_ntf = 0;
	int ret = 0;

	if (ice_is_reset_in_progress(pf->state))
		goto resched;
	mutex_lock(&pf->dplls.lock);
	d->periodic_counter++;
	ret = ice_dpll_update_state(pf, de, false);
	if (!ret)
		ret = ice_dpll_update_state(pf, dp, false);
	if (!ret && dp->phase_offset_monitor_period &&
	    d->periodic_counter % dp->phase_offset_monitor_period == 0)
		ret = ice_dpll_pps_update_phase_offsets(pf, &phase_offset_ntf);
	if (ret) {
		/* EBUSY is expected during reset recovery */
		if (pf->hw.adminq.sq_last_status != LIBIE_AQ_RC_EBUSY)
			d->cgu_state_acq_err_num++;
		/* stop rescheduling this worker */
		if (d->cgu_state_acq_err_num >
		    ICE_CGU_STATE_ACQ_ERR_THRESHOLD) {
			dev_err(ice_pf_to_dev(pf),
				"EEC/PPS DPLLs periodic work disabled\n");
			mutex_unlock(&pf->dplls.lock);
			return;
		}
	}
	mutex_unlock(&pf->dplls.lock);
	ice_dpll_notify_changes(de);
	ice_dpll_notify_changes(dp);
	if (phase_offset_ntf)
		ice_dpll_pins_notify_mask(d, d->inputs, d->num_inputs,
					  phase_offset_ntf);

resched:
	/* Run twice a second or reschedule if update failed */
	kthread_queue_delayed_work(d->kworker, &d->work,
				   ret ? msecs_to_jiffies(10) :
				   msecs_to_jiffies(500));
}

/**
 * ice_dpll_init_ref_sync_inputs - initialize reference sync pin pairs
 * @pf: pf private structure
 *
 * Read DPLL TLV capabilities and initialize reference sync pin pairs in
 * dpll subsystem.
 *
 * Return:
 * * 0 - success or nothing to do (no ref-sync tlv are present)
 * * negative - AQ failure
 */
static int ice_dpll_init_ref_sync_inputs(struct ice_pf *pf)
{
	struct ice_dpll_pin *inputs = pf->dplls.inputs;
	struct ice_hw *hw = &pf->hw;
	u16 addr, len, end, hdr;
	int ret;

	ret = ice_get_pfa_module_tlv(hw, &hdr, &len, ICE_SR_PFA_DPLL_DEFAULTS);
	if (ret) {
		dev_err(ice_pf_to_dev(pf),
			"Failed to read PFA dpll defaults TLV ret=%d\n", ret);
		return ret;
	}
	end = hdr + len;

	for (addr = hdr + ICE_DPLL_PFA_HEADER_LEN; addr < end;
	     addr += ICE_DPLL_PFA_ENTRY_LEN) {
		unsigned long bit, ul_mask, offset;
		u16 pin, mask, buf;
		bool valid = false;

		ret = ice_read_sr_word(hw, addr, &buf);
		if (ret)
			return ret;

		switch (buf) {
		case ICE_DPLL_PFA_REF_SYNC_TYPE:
		case ICE_DPLL_PFA_REF_SYNC_TYPE2:
		{
			u16 mask_addr = addr + ICE_DPLL_PFA_MASK_OFFSET;
			u16 val_addr = addr + ICE_DPLL_PFA_VALUE_OFFSET;

			ret = ice_read_sr_word(hw, mask_addr, &mask);
			if (ret)
				return ret;
			ret = ice_read_sr_word(hw, val_addr, &pin);
			if (ret)
				return ret;
			if (buf == ICE_DPLL_PFA_REF_SYNC_TYPE)
				pin >>= ICE_DPLL_PFA_MAILBOX_REF_SYNC_PIN_S;
			valid = true;
			break;
		}
		case ICE_DPLL_PFA_END:
			addr = end;
			break;
		default:
			continue;
		}
		if (!valid)
			continue;

		ul_mask = mask;
		offset = 0;
		for_each_set_bit(bit, &ul_mask, BITS_PER_TYPE(u16)) {
			int i, j;

			if (hw->device_id == ICE_DEV_ID_E810C_SFP &&
			    pin > ICE_DPLL_E810C_SFP_NC_START)
				offset = -ICE_DPLL_E810C_SFP_NC_PINS;
			i = pin + offset;
			j = bit + offset;
			if (i < 0 || j < 0)
				return -ERANGE;
			inputs[i].ref_sync = j;
		}
	}

	return 0;
}

/**
 * ice_dpll_release_pins - release pins resources from dpll subsystem
 * @pins: pointer to pins array
 * @count: number of pins
 *
 * Release resources of given pins array in the dpll subsystem.
 */
static void ice_dpll_release_pins(struct ice_dpll_pin *pins, int count)
{
	int i;

	for (i = 0; i < count; i++)
		if (!IS_ERR_OR_NULL(pins[i].pin))
			dpll_pin_put(pins[i].pin, &pins[i].tracker);
}

/**
 * ice_dpll_get_pins - get pins from dpll subsystem
 * @pf: board private structure
 * @pins: pointer to pins array
 * @start_idx: get starts from this pin idx value
 * @count: number of pins
 * @clock_id: clock_id of dpll device
 *
 * Get pins - allocate - in dpll subsystem, store them in pin field of given
 * pins array.
 *
 * Return:
 * * 0 - success
 * * negative - allocation failure reason
 */
static int
ice_dpll_get_pins(struct ice_pf *pf, struct ice_dpll_pin *pins,
		  int start_idx, int count, u64 clock_id)
{
	u32 pin_index;
	int i, ret;

	for (i = 0; i < count; i++) {
		pin_index = start_idx;
		if (start_idx != DPLL_PIN_IDX_UNSPEC)
			pin_index += i;
		pins[i].pin = dpll_pin_get(clock_id, pin_index, THIS_MODULE,
					   &pins[i].prop, &pins[i].tracker);
		if (IS_ERR(pins[i].pin)) {
			ret = PTR_ERR(pins[i].pin);
			goto release_pins;
		}
	}

	return 0;

release_pins:
	while (--i >= 0)
		dpll_pin_put(pins[i].pin, &pins[i].tracker);
	return ret;
}

/**
 * ice_dpll_unregister_pins - unregister pins from a dpll
 * @dpll: dpll device pointer
 * @pins: pointer to pins array
 * @ops: callback ops registered with the pins
 * @count: number of pins
 *
 * Unregister pins of a given array of pins from given dpll device registered in
 * dpll subsystem.
 */
static void
ice_dpll_unregister_pins(struct dpll_device *dpll, struct ice_dpll_pin *pins,
			 const struct dpll_pin_ops *ops, int count)
{
	int i;

	for (i = 0; i < count; i++) {
		if (pins[i].hidden)
			continue;
		if (IS_ERR_OR_NULL(pins[i].pin))
			continue;
		dpll_pin_unregister(dpll, pins[i].pin, ops, &pins[i]);
	}
}

/**
 * ice_dpll_pin_ref_sync_register - register reference sync pins
 * @pins: pointer to pins array
 * @count: number of pins
 *
 * Register reference sync pins in dpll subsystem.
 *
 * Return:
 * * 0 - success
 * * negative - registration failure reason
 */
static int
ice_dpll_pin_ref_sync_register(struct ice_dpll_pin *pins, int count)
{
	int ret, i;

	for (i = 0; i < count; i++) {
		if (!pins[i].hidden && pins[i].ref_sync) {
			int j = pins[i].ref_sync;

			ret = dpll_pin_ref_sync_pair_add(pins[i].pin,
							 pins[j].pin);
			if (ret)
				return ret;
		}
	}

	return 0;
}

/**
 * ice_dpll_register_pins - register pins with a dpll
 * @dpll: dpll pointer to register pins with
 * @pins: pointer to pins array
 * @ops: callback ops registered with the pins
 * @count: number of pins
 *
 * Register pins of a given array with given dpll in dpll subsystem.
 *
 * Return:
 * * 0 - success
 * * negative - registration failure reason
 */
static int
ice_dpll_register_pins(struct dpll_device *dpll, struct ice_dpll_pin *pins,
		       const struct dpll_pin_ops *ops, int count)
{
	int ret, i;

	for (i = 0; i < count; i++) {
		if (!pins[i].hidden) {
			ret = dpll_pin_register(dpll, pins[i].pin, ops, &pins[i]);
			if (ret)
				goto unregister_pins;
		}
	}

	return 0;

unregister_pins:
	while (--i >= 0)
		if (!pins[i].hidden)
			dpll_pin_unregister(dpll, pins[i].pin, ops, &pins[i]);
	return ret;
}

/**
 * ice_dpll_deinit_direct_pins - deinitialize direct pins
 * @pf: board private structure
 * @cgu: if cgu is present and controlled by this NIC
 * @pins: pointer to pins array
 * @count: number of pins
 * @ops: callback ops registered with the pins
 * @first: dpll device pointer
 * @second: dpll device pointer
 *
 * If cgu is owned unregister pins from given dplls.
 * Release pins resources to the dpll subsystem.
 */
static void
ice_dpll_deinit_direct_pins(struct ice_pf *pf, bool cgu,
			    struct ice_dpll_pin *pins, int count,
			    const struct dpll_pin_ops *ops,
			    struct dpll_device *first,
			    struct dpll_device *second)
{
	if (cgu) {
		ice_dpll_unregister_pins(first, pins, ops, count);
		ice_dpll_unregister_pins(second, pins, ops, count);
	}
	ice_dpll_release_pins(pins, count);
}

/**
 * ice_dpll_init_direct_pins - initialize direct pins
 * @pf: board private structure
 * @cgu: if cgu is present and controlled by this NIC
 * @pins: pointer to pins array
 * @start_idx: on which index shall allocation start in dpll subsystem
 * @count: number of pins
 * @ops: callback ops registered with the pins
 * @first: dpll device pointer
 * @second: dpll device pointer
 *
 * Allocate directly connected pins of a given array in dpll subsystem.
 * If cgu is owned register allocated pins with given dplls.
 *
 * Return:
 * * 0 - success
 * * negative - registration failure reason
 */
static int
ice_dpll_init_direct_pins(struct ice_pf *pf, bool cgu,
			  struct ice_dpll_pin *pins, int start_idx, int count,
			  const struct dpll_pin_ops *ops,
			  struct dpll_device *first, struct dpll_device *second)
{
	int ret;

	ret = ice_dpll_get_pins(pf, pins, start_idx, count, pf->dplls.clock_id);
	if (ret)
		return ret;
	if (cgu) {
		ret = ice_dpll_register_pins(first, pins, ops, count);
		if (ret)
			goto release_pins;
		ret = ice_dpll_register_pins(second, pins, ops, count);
		if (ret)
			goto unregister_first;
	}

	return 0;

unregister_first:
	ice_dpll_unregister_pins(first, pins, ops, count);
release_pins:
	ice_dpll_release_pins(pins, count);
	return ret;
}

/**
 * ice_dpll_deinit_rclk_pin - release rclk pin resources
 * @pf: board private structure
 *
 * Deregister rclk pin from parent pins and release resources in dpll subsystem.
 */
static void ice_dpll_deinit_rclk_pin(struct ice_pf *pf)
{
	struct ice_dpll_pin *rclk = &pf->dplls.rclk;
	struct ice_vsi *vsi = ice_get_main_vsi(pf);
	struct ice_dpll_pin *parent;
	int i;

	for (i = 0; i < rclk->num_parents; i++) {
		parent = &pf->dplls.inputs[rclk->parent_idx[i]];
		if (IS_ERR_OR_NULL(parent->pin))
			continue;
		dpll_pin_on_pin_unregister(parent->pin, rclk->pin,
					   &ice_dpll_rclk_ops, rclk);
	}
	if (WARN_ON_ONCE(!vsi || !vsi->netdev))
		return;
	dpll_netdev_pin_clear(vsi->netdev);
	dpll_pin_put(rclk->pin, &rclk->tracker);
}

static bool ice_dpll_is_fwnode_pin(struct ice_dpll_pin *pin)
{
	return !IS_ERR_OR_NULL(pin->fwnode);
}

static bool ice_dpll_fwnode_eq(const struct fwnode_handle *a,
			       const struct fwnode_handle *b)
{
	return a && a == b;
}

static void ice_dpll_pin_notify_work(struct work_struct *work)
{
	struct ice_dpll_pin_work *w = container_of(work,
						   struct ice_dpll_pin_work,
						   work);
	struct ice_dpll_pin *pin, *parent = w->pin;
	bool is_tx_synce_parent = false;
	struct ice_pf *pf = parent->pf;
	bool is_rclk_parent = false;
	int ret;

	wait_for_completion(&pf->dplls.dpll_init);
	if (!test_bit(ICE_FLAG_DPLL, pf->flags))
		goto out; /* DPLL initialization failed */

	/* Decide which parent we are handling, defensively checking FWNs */
	for (int i = 0; i < pf->dplls.rclk.num_parents; i++) {
		if (ice_dpll_fwnode_eq(parent->fwnode,
				       pf->dplls.inputs[i].fwnode)) {
			is_rclk_parent = true;
			break;
		}
	}

	is_tx_synce_parent =
		ice_dpll_fwnode_eq(parent->fwnode,
				   pf->dplls.txclks[E825_EXT_SYNCE_PIN_IDX].fwnode);
	if (!is_rclk_parent && !is_tx_synce_parent)
		goto out;

	switch (w->action) {
	case DPLL_PIN_CREATED:
		if (!IS_ERR_OR_NULL(parent->pin)) {
			/* We have already our pin registered */
			goto out;
		}

		/* Grab reference on fwnode pin */
		parent->pin = fwnode_dpll_pin_find(parent->fwnode,
						   &parent->tracker);
		if (IS_ERR_OR_NULL(parent->pin)) {
			dev_err(ice_pf_to_dev(pf),
				"Cannot get fwnode pin reference\n");
			goto out;
		}

		if (is_rclk_parent) {
			/* Register rclk pin via on-pin relationship */
			pin = &pf->dplls.rclk;
			ret = dpll_pin_on_pin_register(parent->pin, pin->pin,
						       &ice_dpll_rclk_ops, pin);
			if (ret) {
				dev_err(ice_pf_to_dev(pf),
					"RCLK pin register failed: %pe\n",
					ERR_PTR(ret));
				goto drop_parent_ref;
			}
		} else if (is_tx_synce_parent) {
			/* Register TX-CLK SYNCE pin directly to TXC DPLL */
			pin = &pf->dplls.txclks[E825_EXT_SYNCE_PIN_IDX];
			ret = dpll_pin_register(pf->dplls.txc.dpll, pin->pin,
						&ice_dpll_txclk_ops, pin);
			if (ret) {
				dev_err(ice_pf_to_dev(pf),
					"TX SYNCE pin register failed: %pe\n",
					ERR_PTR(ret));
				goto drop_parent_ref;
			}
		}
		break;
	case DPLL_PIN_DELETED:
		if (IS_ERR_OR_NULL(parent->pin)) {
			/* We have already our pin unregistered */
			goto out;
		}

		if (is_rclk_parent) {
			/* Unregister rclk pin */
			pin = &pf->dplls.rclk;
			dpll_pin_on_pin_unregister(parent->pin, pin->pin,
						   &ice_dpll_rclk_ops, pin);
		} else if (is_tx_synce_parent) {
			/* Unregister TX-CLK SYNCE pin from TXC DPLL */
			pin = &pf->dplls.txclks[E825_EXT_SYNCE_PIN_IDX];
			dpll_pin_unregister(pf->dplls.txc.dpll, pin->pin,
					    &ice_dpll_txclk_ops, pin);
		}
drop_parent_ref:
		/* Drop fwnode pin reference */
		dpll_pin_put(parent->pin, &parent->tracker);
		parent->pin = NULL;
		break;
	default:
		break;
	}
out:
	kfree(w);
}

static int ice_dpll_pin_notify(struct notifier_block *nb, unsigned long action,
			       void *data)
{
	struct ice_dpll_pin *pin = container_of(nb, struct ice_dpll_pin, nb);
	struct dpll_pin_notifier_info *info = data;
	struct ice_dpll_pin_work *work;

	if (action != DPLL_PIN_CREATED && action != DPLL_PIN_DELETED)
		return NOTIFY_DONE;

	/* Check if the reported pin is this one */
	if (pin->fwnode != info->fwnode)
		return NOTIFY_DONE; /* Not this pin */

	/* Ignore notification which are the outcome of internal pin
	 * registration/unregistration calls - synce pin case.
	 */
	if (info->src_clock_id == pin->pf->dplls.clock_id)
		return NOTIFY_DONE;

	work = kzalloc_obj(*work);
	if (!work)
		return NOTIFY_DONE;

	INIT_WORK(&work->work, ice_dpll_pin_notify_work);
	work->action = action;
	work->pin = pin;

	queue_work(pin->pf->dplls.wq, &work->work);

	return NOTIFY_OK;
}

/**
 * ice_dpll_init_pin_common - initialize pin
 * @pf: board private structure
 * @pin: pin to register
 * @start_idx: on which index shall allocation start in dpll subsystem
 * @ops: callback ops registered with the pins
 *
 * Allocate resource for given pin in dpll subsystem. Register the pin with
 * the parents it has in the info.
 *
 * Return:
 * * 0 - success
 * * negative - registration failure reason
 */
static int
ice_dpll_init_pin_common(struct ice_pf *pf, struct ice_dpll_pin *pin,
			 int start_idx, const struct dpll_pin_ops *ops)
{
	struct ice_dpll_pin *parent;
	int ret, i;

	ret = ice_dpll_get_pins(pf, pin, start_idx, 1, pf->dplls.clock_id);
	if (ret)
		return ret;

	for (i = 0; i < pin->num_parents; i++) {
		parent = &pf->dplls.inputs[pin->parent_idx[i]];
		if (IS_ERR_OR_NULL(parent->pin)) {
			if (!ice_dpll_is_fwnode_pin(parent)) {
				ret = -ENODEV;
				goto unregister_pins;
			}
			parent->pin = fwnode_dpll_pin_find(parent->fwnode,
							   &parent->tracker);
			if (IS_ERR_OR_NULL(parent->pin)) {
				dev_info(ice_pf_to_dev(pf),
					 "Mux pin not registered yet\n");
				continue;
			}
		}
		ret = dpll_pin_on_pin_register(parent->pin, pin->pin, ops, pin);
		if (ret)
			goto unregister_pins;
	}

	return 0;

unregister_pins:
	while (i) {
		parent = &pf->dplls.inputs[pin->parent_idx[--i]];
		if (IS_ERR_OR_NULL(parent->pin))
			continue;
		dpll_pin_on_pin_unregister(parent->pin, pin->pin, ops, pin);
	}
	ice_dpll_release_pins(pin, 1);

	return ret;
}

/**
 * ice_dpll_init_rclk_pin - initialize recovered clock pin
 * @pf: board private structure
 * @start_idx: on which index shall allocation start in dpll subsystem
 * @ops: callback ops registered with the pins
 *
 * Allocate resource for recovered clock pin in dpll subsystem. Register the
 * pin with the parents it has in the info.
 *
 * Return:
 * * 0 - success
 * * negative - registration failure reason
 */
static int
ice_dpll_init_rclk_pin(struct ice_pf *pf, int start_idx,
		       const struct dpll_pin_ops *ops)
{
	struct ice_vsi *vsi = ice_get_main_vsi(pf);
	int ret;

	ret = ice_dpll_init_pin_common(pf, &pf->dplls.rclk, start_idx, ops);
	if (ret)
		return ret;

	dpll_netdev_pin_set(vsi->netdev, pf->dplls.rclk.pin);

	return 0;
}

static void
ice_dpll_stop_fwnode_pin_activity(struct ice_dpll_pin *pin, bool flush)
{
	unregister_dpll_notifier(&pin->nb);
	if (flush)
		flush_workqueue(pin->pf->dplls.wq);
}

static void
ice_dpll_release_fwnode_pin(struct ice_dpll_pin *pin)
{
	if (!IS_ERR_OR_NULL(pin->pin)) {
		dpll_pin_put(pin->pin, &pin->tracker);
		pin->pin = NULL;
	}
	fwnode_handle_put(pin->fwnode);
	pin->fwnode = NULL;
}

static void
ice_dpll_deinit_fwnode_pin(struct ice_dpll_pin *pin)
{
	ice_dpll_stop_fwnode_pin_activity(pin, true);
	ice_dpll_release_fwnode_pin(pin);
}

static void
ice_dpll_deinit_fwnode_pins(struct ice_pf *pf, struct ice_dpll_pin *pins,
			    int start_idx)
{
	int i;

	for (i = 0; i < pf->dplls.rclk.num_parents; i++)
		ice_dpll_deinit_fwnode_pin(&pins[start_idx + i]);
	destroy_workqueue(pf->dplls.wq);
}

static int ice_dpll_deinit_txclk_pins(struct ice_pf *pf)
{
	struct ice_dpll_pin *synce_pin = &pf->dplls.txclks[E825_EXT_SYNCE_PIN_IDX];
	struct ice_dpll *dt = &pf->dplls.txc;

	ice_dpll_stop_fwnode_pin_activity(synce_pin, true);
	ice_dpll_unregister_pins(dt->dpll, pf->dplls.txclks,
				 &ice_dpll_txclk_ops,
				 ARRAY_SIZE(pf->dplls.txclks));
	ice_dpll_release_pins(&pf->dplls.txclks[E825_EXT_EREF_PIN_IDX], 1);
	/* ice_dpll_release_pins() puts the pin but does not clear the slot,
	 * unlike ice_dpll_release_fwnode_pin() used for SYNCE below. NULL it
	 * so a late ice_txclk_get_pin() returns NULL rather than a dangling
	 * pointer.
	 */
	pf->dplls.txclks[E825_EXT_EREF_PIN_IDX].pin = NULL;
	ice_dpll_release_fwnode_pin(synce_pin);
	return 0;
}

/**
 * ice_dpll_deinit_pins - deinitialize direct pins
 * @pf: board private structure
 * @cgu: if cgu is controlled by this pf
 *
 * If cgu is owned unregister directly connected pins from the dplls.
 * Release resources of directly connected pins from the dpll subsystem.
 */
static void ice_dpll_deinit_pins(struct ice_pf *pf, bool cgu)
{
	struct ice_dpll_pin *outputs = pf->dplls.outputs;
	struct ice_dpll_pin *inputs = pf->dplls.inputs;
	int num_outputs = pf->dplls.num_outputs;
	int num_inputs = pf->dplls.num_inputs;
	struct ice_dplls *d = &pf->dplls;
	struct ice_dpll *de = &d->eec;
	struct ice_dpll *dp = &d->pps;

	ice_dpll_deinit_rclk_pin(pf);
	if (pf->hw.mac_type == ICE_MAC_GENERIC_3K_E825) {
		ice_dpll_deinit_txclk_pins(pf);
		ice_dpll_deinit_fwnode_pins(pf, pf->dplls.inputs, 0);
	}
	if (cgu) {
		ice_dpll_unregister_pins(dp->dpll, inputs, &ice_dpll_input_ops,
					 num_inputs);
		ice_dpll_unregister_pins(de->dpll, inputs, &ice_dpll_input_ops,
					 num_inputs);
	}
	ice_dpll_release_pins(inputs, num_inputs);
	if (cgu) {
		ice_dpll_unregister_pins(dp->dpll, outputs,
					 &ice_dpll_output_ops, num_outputs);
		ice_dpll_unregister_pins(de->dpll, outputs,
					 &ice_dpll_output_ops, num_outputs);
		ice_dpll_release_pins(outputs, num_outputs);
		if (!pf->dplls.generic) {
			ice_dpll_deinit_direct_pins(pf, cgu, pf->dplls.ufl,
						    ICE_DPLL_PIN_SW_NUM,
						    &ice_dpll_pin_ufl_ops,
						    pf->dplls.pps.dpll,
						    pf->dplls.eec.dpll);
			ice_dpll_deinit_direct_pins(pf, cgu, pf->dplls.sma,
						    ICE_DPLL_PIN_SW_NUM,
						    &ice_dpll_pin_sma_ops,
						    pf->dplls.pps.dpll,
						    pf->dplls.eec.dpll);
		}
	}
}

static struct fwnode_handle *
ice_dpll_pin_node_get(struct ice_pf *pf, const char *name)
{
	struct fwnode_handle *fwnode = dev_fwnode(ice_pf_to_dev(pf));
	int index;

	index = fwnode_property_match_string(fwnode, "dpll-pin-names", name);
	if (index < 0)
		return ERR_PTR(-ENOENT);

	return fwnode_find_reference(fwnode, "dpll-pins", index);
}

static int
ice_dpll_init_fwnode_pin(struct ice_dpll_pin *pin, const char *name)
{
	struct ice_pf *pf = pin->pf;
	int ret;

	pin->fwnode = ice_dpll_pin_node_get(pf, name);
	if (IS_ERR(pin->fwnode)) {
		dev_err(ice_pf_to_dev(pf),
			"Failed to find %s firmware node: %pe\n", name,
			pin->fwnode);
		pin->fwnode = NULL;
		return -ENODEV;
	}

	dev_dbg(ice_pf_to_dev(pf), "Found fwnode node for %s\n", name);

	pin->pin = fwnode_dpll_pin_find(pin->fwnode, &pin->tracker);
	if (IS_ERR_OR_NULL(pin->pin)) {
		dev_info(ice_pf_to_dev(pf),
			 "DPLL pin for %pfwp not registered yet\n",
			 pin->fwnode);
		pin->pin = NULL;
	}

	pin->nb.notifier_call = ice_dpll_pin_notify;
	ret = register_dpll_notifier(&pin->nb);
	if (ret) {
		dev_err(ice_pf_to_dev(pf),
			"Failed to subscribe for DPLL notifications\n");

		if (!IS_ERR_OR_NULL(pin->pin)) {
			dpll_pin_put(pin->pin, &pin->tracker);
			pin->pin = NULL;
		}
		fwnode_handle_put(pin->fwnode);
		pin->fwnode = NULL;

		return ret;
	}

	return ret;
}

/**
 * ice_dpll_init_fwnode_pins - initialize pins from device tree
 * @pf: board private structure
 * @pins: pointer to pins array
 * @start_idx: starting index for pins
 * @count: number of pins to initialize
 *
 * Initialize input pins for E825 RCLK support. The parent pins (rclk0, rclk1)
 * are expected to be defined by the system firmware (ACPI). This function
 * allocates them in the dpll subsystem and stores their indices for later
 * registration with the rclk pin.
 *
 * Return:
 * * 0 - success
 * * negative - initialization failure reason
 */
static int
ice_dpll_init_fwnode_pins(struct ice_pf *pf, struct ice_dpll_pin *pins,
			  int start_idx)
{
	char pin_name[16];
	int i, ret;

	pf->dplls.wq = create_singlethread_workqueue("ice_dpll_wq");
	if (!pf->dplls.wq)
		return -ENOMEM;

	for (i = 0; i < pf->dplls.rclk.num_parents; i++) {
		pins[start_idx + i].pf = pf;
		snprintf(pin_name, sizeof(pin_name), "rclk%u", i);
		ret = ice_dpll_init_fwnode_pin(&pins[start_idx + i], pin_name);
		if (ret)
			goto error;
	}

	return 0;
error:
	/*
	 * A notifier worker may already be queued and blocked on dpll_init;
	 * release it so the per-pin flush below does not deadlock.
	 */
	complete_all(&pf->dplls.dpll_init);
	while (i--)
		ice_dpll_deinit_fwnode_pin(&pins[start_idx + i]);

	destroy_workqueue(pf->dplls.wq);

	return ret;
}

static int ice_dpll_init_txclk_pins(struct ice_pf *pf, int start_idx)
{
	struct ice_dpll_pin *ref_pin = pf->dplls.txclks;
	struct ice_dpll *txc = &pf->dplls.txc;
	int ret;

	/* Configure EXT_EREF0 pin */
	ret = ice_dpll_get_pins(pf, ref_pin, start_idx, 1, pf->dplls.clock_id);
	if (ret)
		return ret;
	ret = dpll_pin_register(txc->dpll, ref_pin->pin, &ice_dpll_txclk_ops,
				ref_pin);
	if (ret)
		goto err_release_ext_eref;

	/*
	 * Configure EXT_SYNCE pin (fwnode-backed).
	 * The pin may not yet be available; in that case registration
	 * will be deferred via the notifier path.
	 */
	ref_pin++;
	ret = ice_dpll_init_fwnode_pin(ref_pin, ice_dpll_fwnode_ext_synce);
	if (ret)
		goto err_unregister_ext_eref;

	if (IS_ERR_OR_NULL(ref_pin->pin)) {
		dev_dbg(ice_pf_to_dev(pf),
			"Tx-clk SYNCE pin not registered yet\n");
		return 0;
	}

	ret = dpll_pin_register(txc->dpll, ref_pin->pin, &ice_dpll_txclk_ops,
				ref_pin);
	if (ret)
		goto err_deinit_synce;

	return 0;

err_deinit_synce:
	/*
	 * Avoid deadlock against notifier workers blocked on dpll_init.
	 * The outer init error path will complete dpll_init and flush the
	 * shared workqueue before destroying it.
	 */
	ice_dpll_stop_fwnode_pin_activity(ref_pin, false);
	ice_dpll_release_fwnode_pin(ref_pin);
err_unregister_ext_eref:
	dpll_pin_unregister(txc->dpll,
			    pf->dplls.txclks[E825_EXT_EREF_PIN_IDX].pin,
			    &ice_dpll_txclk_ops,
			    &pf->dplls.txclks[E825_EXT_EREF_PIN_IDX]);

err_release_ext_eref:
	ice_dpll_release_pins(&pf->dplls.txclks[E825_EXT_EREF_PIN_IDX], 1);

	return ret;
}

/**
 * ice_dpll_init_pins_e825 - init pins and register pins with a dplls
 * @pf: board private structure
 * @cgu: if cgu is present and controlled by this NIC
 *
 * Initialize directly connected pf's pins within pf's dplls in a Linux dpll
 * subsystem.
 *
 * Return:
 * * 0 - success
 * * negative - initialization failure reason
 */
static int ice_dpll_init_pins_e825(struct ice_pf *pf)
{
	int ret;

	ret = ice_dpll_init_fwnode_pins(pf, pf->dplls.inputs, 0);
	if (ret)
		return ret;

	ret = ice_dpll_init_rclk_pin(pf, DPLL_PIN_IDX_UNSPEC,
				     &ice_dpll_rclk_ops);

	if (ret)
		goto unregister_pins;

	ret = ice_dpll_init_txclk_pins(pf, 0);
	if (ret)
		ice_dpll_deinit_rclk_pin(pf);

unregister_pins:
	if (ret) {
		/* Inform DPLL notifier works that DPLL init was finished
		 * unsuccessfully (ICE_DPLL_FLAG not set).
		 */
		complete_all(&pf->dplls.dpll_init);
		ice_dpll_deinit_fwnode_pins(pf, pf->dplls.inputs, 0);
	}

	return ret;
}

/**
 * ice_dpll_init_pins - init pins and register pins with a dplls
 * @pf: board private structure
 * @cgu: if cgu is present and controlled by this NIC
 *
 * Initialize directly connected pf's pins within pf's dplls in a Linux dpll
 * subsystem.
 *
 * Return:
 * * 0 - success
 * * negative - initialization failure reason
 */
static int ice_dpll_init_pins(struct ice_pf *pf, bool cgu)
{
	const struct dpll_pin_ops *output_ops;
	const struct dpll_pin_ops *input_ops;
	int ret, count;

	input_ops = &ice_dpll_input_ops;
	output_ops = &ice_dpll_output_ops;

	ret = ice_dpll_init_direct_pins(pf, cgu, pf->dplls.inputs, 0,
					pf->dplls.num_inputs, input_ops,
					pf->dplls.eec.dpll,
					pf->dplls.pps.dpll);
	if (ret)
		return ret;
	count = pf->dplls.num_inputs;
	if (cgu) {
		ret = ice_dpll_init_direct_pins(pf, cgu, pf->dplls.outputs,
						count, pf->dplls.num_outputs,
						output_ops, pf->dplls.eec.dpll,
						pf->dplls.pps.dpll);
		if (ret)
			goto deinit_inputs;
		count += pf->dplls.num_outputs;
		if (!pf->dplls.generic) {
			ret = ice_dpll_init_direct_pins(pf, cgu, pf->dplls.sma,
							count,
							ICE_DPLL_PIN_SW_NUM,
							&ice_dpll_pin_sma_ops,
							pf->dplls.eec.dpll,
							pf->dplls.pps.dpll);
			if (ret)
				goto deinit_outputs;
			count += ICE_DPLL_PIN_SW_NUM;
			ret = ice_dpll_init_direct_pins(pf, cgu, pf->dplls.ufl,
							count,
							ICE_DPLL_PIN_SW_NUM,
							&ice_dpll_pin_ufl_ops,
							pf->dplls.eec.dpll,
							pf->dplls.pps.dpll);
			if (ret)
				goto deinit_sma;
			count += ICE_DPLL_PIN_SW_NUM;
		}
		ret = ice_dpll_pin_ref_sync_register(pf->dplls.inputs,
						     pf->dplls.num_inputs);
		if (ret)
			goto deinit_ufl;
		ret = ice_dpll_pin_ref_sync_register(pf->dplls.sma,
						     ICE_DPLL_PIN_SW_NUM);
		if (ret)
			goto deinit_ufl;
	} else {
		count += pf->dplls.num_outputs + 2 * ICE_DPLL_PIN_SW_NUM;
	}

	ret = ice_dpll_init_rclk_pin(pf, count + pf->ptp.port.port_num,
				     &ice_dpll_rclk_ops);
	if (ret)
		goto deinit_ufl;

	return 0;
deinit_ufl:
	ice_dpll_deinit_direct_pins(pf, cgu, pf->dplls.ufl, ICE_DPLL_PIN_SW_NUM,
				    &ice_dpll_pin_ufl_ops, pf->dplls.pps.dpll,
				    pf->dplls.eec.dpll);
deinit_sma:
	ice_dpll_deinit_direct_pins(pf, cgu, pf->dplls.sma, ICE_DPLL_PIN_SW_NUM,
				    &ice_dpll_pin_sma_ops, pf->dplls.pps.dpll,
				    pf->dplls.eec.dpll);
deinit_outputs:
	ice_dpll_deinit_direct_pins(pf, cgu, pf->dplls.outputs,
				    pf->dplls.num_outputs,
				    output_ops, pf->dplls.pps.dpll,
				    pf->dplls.eec.dpll);
deinit_inputs:
	ice_dpll_deinit_direct_pins(pf, cgu, pf->dplls.inputs,
				    pf->dplls.num_inputs,
				    input_ops, pf->dplls.pps.dpll,
				    pf->dplls.eec.dpll);
	return ret;
}

/**
 * ice_dpll_deinit_dpll - deinitialize dpll device
 * @pf: board private structure
 * @d: pointer to ice_dpll
 * @cgu: if cgu is present and controlled by this NIC
 *
 * If cgu is owned, unregister the DPLL from DPLL subsystem.
 * Release resources of DPLL device from DPLL subsystem.
 */
static void
ice_dpll_deinit_dpll(struct ice_pf *pf, struct ice_dpll *d, bool cgu)
{
	if (cgu || pf->hw.mac_type == ICE_MAC_GENERIC_3K_E825)
		dpll_device_unregister(d->dpll, d->ops, d);
	dpll_device_put(d->dpll, &d->tracker);
}

/**
 * ice_dpll_init_dpll - initialize dpll device in dpll subsystem
 * @pf: board private structure
 * @d: dpll to be initialized
 * @cgu: if cgu is present and controlled by this NIC
 * @type: type of dpll being initialized
 *
 * Allocate DPLL instance for this board in dpll subsystem, if cgu is controlled
 * by this NIC, register DPLL with the callback ops.
 *
 * Return:
 * * 0 - success
 * * negative - initialization failure reason
 */
static int
ice_dpll_init_dpll(struct ice_pf *pf, struct ice_dpll *d, bool cgu,
		   enum dpll_type type)
{
	u64 clock_id = pf->dplls.clock_id;
	int ret;

	d->dpll = dpll_device_get(clock_id, d->dpll_idx, THIS_MODULE,
				  &d->tracker);
	if (IS_ERR(d->dpll)) {
		ret = PTR_ERR(d->dpll);
		dev_err(ice_pf_to_dev(pf),
			"dpll_device_get failed (%p) err=%d\n", d, ret);
		return ret;
	}
	d->pf = pf;
	if (cgu || pf->hw.mac_type == ICE_MAC_GENERIC_3K_E825) {
		const struct dpll_device_ops *ops = &ice_dpll_ops;

		if (type == DPLL_TYPE_PPS && ice_dpll_is_pps_phase_monitor(pf))
			ops =  &ice_dpll_pom_ops;
		if (cgu)
			ice_dpll_update_state(pf, d, true);
		ret = dpll_device_register(d->dpll, type, ops, d);
		if (ret) {
			dpll_device_put(d->dpll, &d->tracker);
			d->dpll = NULL;
			return ret;
		}
		d->ops = ops;
	}

	return 0;
}

/**
 * ice_dpll_deinit_worker - deinitialize dpll kworker
 * @pf: board private structure
 *
 * Stop dpll's kworker, release it's resources.
 */
static void ice_dpll_deinit_worker(struct ice_pf *pf)
{
	struct ice_dplls *d = &pf->dplls;

	kthread_cancel_delayed_work_sync(&d->work);
	kthread_destroy_worker(d->kworker);
}

/**
 * ice_dpll_init_worker - Initialize DPLLs periodic worker
 * @pf: board private structure
 *
 * Create and start DPLLs periodic worker.
 *
 * Context: Shall be called after pf->dplls.lock is initialized.
 * Return:
 * * 0 - success
 * * negative - create worker failure
 */
static int ice_dpll_init_worker(struct ice_pf *pf)
{
	struct ice_dplls *d = &pf->dplls;
	struct kthread_worker *kworker;

	kthread_init_delayed_work(&d->work, ice_dpll_periodic_work);
	kworker = kthread_run_worker(0, "ice-dplls-%s",
					dev_name(ice_pf_to_dev(pf)));
	if (IS_ERR(kworker))
		return PTR_ERR(kworker);
	d->kworker = kworker;
	d->cgu_state_acq_err_num = 0;
	kthread_queue_delayed_work(d->kworker, &d->work, 0);

	return 0;
}

/**
 * ice_dpll_phase_range_set - initialize phase adjust range helper
 * @range: pointer to phase adjust range struct to be initialized
 * @phase_adj: a value to be used as min(-)/max(+) boundary
 */
static void ice_dpll_phase_range_set(struct dpll_pin_phase_adjust_range *range,
				     u32 phase_adj)
{
	range->min = -phase_adj;
	range->max = phase_adj;
}

/**
 * ice_dpll_init_info_pins_generic - initializes generic pins info
 * @pf: board private structure
 * @input: if input pins initialized
 *
 * Init information for generic pins, cache them in PF's pins structures.
 *
 * Return:
 * * 0 - success
 * * negative - init failure reason
 */
static int ice_dpll_init_info_pins_generic(struct ice_pf *pf, bool input)
{
	struct ice_dpll *de = &pf->dplls.eec, *dp = &pf->dplls.pps;
	static const char labels[][sizeof("99")] = {
		"0", "1", "2", "3", "4", "5", "6", "7", "8",
		"9", "10", "11", "12", "13", "14", "15" };
	u32 cap = DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
	enum ice_dpll_pin_type pin_type;
	int i, pin_num, ret = -EINVAL;
	struct ice_dpll_pin *pins;
	u32 phase_adj_max;

	if (input) {
		pin_num = pf->dplls.num_inputs;
		pins = pf->dplls.inputs;
		phase_adj_max = pf->dplls.input_phase_adj_max;
		pin_type = ICE_DPLL_PIN_TYPE_INPUT;
		cap |= DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE;
	} else {
		pin_num = pf->dplls.num_outputs;
		pins = pf->dplls.outputs;
		phase_adj_max = pf->dplls.output_phase_adj_max;
		pin_type = ICE_DPLL_PIN_TYPE_OUTPUT;
	}
	if (pin_num > ARRAY_SIZE(labels))
		return ret;

	for (i = 0; i < pin_num; i++) {
		pins[i].idx = i;
		pins[i].prop.board_label = labels[i];
		ice_dpll_phase_range_set(&pins[i].prop.phase_range,
					 phase_adj_max);
		pins[i].prop.capabilities = cap;
		pins[i].pf = pf;
		ret = ice_dpll_pin_state_update(pf, &pins[i], pin_type, NULL);
		if (ret)
			break;
		if (input && pins[i].freq == ICE_DPLL_PIN_GEN_RCLK_FREQ)
			pins[i].prop.type = DPLL_PIN_TYPE_MUX;
		else
			pins[i].prop.type = DPLL_PIN_TYPE_EXT;
		if (!input)
			continue;
		ret = ice_aq_get_cgu_ref_prio(&pf->hw, de->dpll_idx, i,
					      &de->input_prio[i]);
		if (ret)
			break;
		ret = ice_aq_get_cgu_ref_prio(&pf->hw, dp->dpll_idx, i,
					      &dp->input_prio[i]);
		if (ret)
			break;
	}

	return ret;
}

/**
 * ice_dpll_init_info_direct_pins - initializes direct pins info
 * @pf: board private structure
 * @pin_type: type of pins being initialized
 *
 * Init information for directly connected pins, cache them in pf's pins
 * structures.
 *
 * Return:
 * * 0 - success
 * * negative - init failure reason
 */
static int
ice_dpll_init_info_direct_pins(struct ice_pf *pf,
			       enum ice_dpll_pin_type pin_type)
{
	struct ice_dpll *de = &pf->dplls.eec, *dp = &pf->dplls.pps;
	int num_pins, i, ret = -EINVAL;
	struct ice_hw *hw = &pf->hw;
	struct ice_dpll_pin *pins;
	unsigned long caps;
	u32 phase_adj_max;
	u8 freq_supp_num;
	bool input;

	switch (pin_type) {
	case ICE_DPLL_PIN_TYPE_INPUT:
		pins = pf->dplls.inputs;
		num_pins = pf->dplls.num_inputs;
		phase_adj_max = pf->dplls.input_phase_adj_max;
		input = true;
		break;
	case ICE_DPLL_PIN_TYPE_OUTPUT:
		pins = pf->dplls.outputs;
		num_pins = pf->dplls.num_outputs;
		phase_adj_max = pf->dplls.output_phase_adj_max;
		input = false;
		break;
	default:
		return -EINVAL;
	}
	if (num_pins != ice_cgu_get_num_pins(hw, input)) {
		pf->dplls.generic = true;
		return ice_dpll_init_info_pins_generic(pf, input);
	}

	for (i = 0; i < num_pins; i++) {
		caps = 0;
		pins[i].idx = i;
		pins[i].prop.board_label = ice_cgu_get_pin_name(hw, i, input);
		pins[i].prop.type = ice_cgu_get_pin_type(hw, i, input);
		if (input) {
			ret = ice_aq_get_cgu_ref_prio(hw, de->dpll_idx, i,
						      &de->input_prio[i]);
			if (ret)
				return ret;
			ret = ice_aq_get_cgu_ref_prio(hw, dp->dpll_idx, i,
						      &dp->input_prio[i]);
			if (ret)
				return ret;
			caps |= (DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE |
				 DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE);
			if (ice_dpll_is_sw_pin(pf, i, true))
				pins[i].hidden = true;
		} else {
			ret = ice_cgu_get_output_pin_state_caps(hw, i, &caps);
			if (ret)
				return ret;
			if (ice_dpll_is_sw_pin(pf, i, false))
				pins[i].hidden = true;
		}
		ice_dpll_phase_range_set(&pins[i].prop.phase_range,
					 phase_adj_max);
		pins[i].prop.capabilities = caps;
		ret = ice_dpll_pin_state_update(pf, &pins[i], pin_type, NULL);
		if (ret)
			return ret;
		pins[i].prop.freq_supported =
			ice_cgu_get_pin_freq_supp(hw, i, input, &freq_supp_num);
		pins[i].prop.freq_supported_num = freq_supp_num;
		pins[i].pf = pf;
	}
	if (input)
		ret = ice_dpll_init_ref_sync_inputs(pf);

	return ret;
}

/**
 * ice_dpll_init_info_pin_on_pin_e825c - initializes rclk pin information
 * @pf: board private structure
 *
 * Init information for rclk pin, cache them in pf->dplls.rclk.
 *
 * Return:
 * * 0 - success
 */
static int ice_dpll_init_info_pin_on_pin_e825c(struct ice_pf *pf)
{
	struct ice_dpll_pin *rclk_pin = &pf->dplls.rclk;

	rclk_pin->prop.type = DPLL_PIN_TYPE_SYNCE_ETH_PORT;
	rclk_pin->prop.capabilities |= DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
	rclk_pin->pf = pf;

	return 0;
}

/**
 * ice_dpll_init_info_rclk_pin - initializes rclk pin information
 * @pf: board private structure
 *
 * Init information for rclk pin, cache them in pf->dplls.rclk.
 *
 * Return:
 * * 0 - success
 * * negative - init failure reason
 */
static int ice_dpll_init_info_rclk_pin(struct ice_pf *pf)
{
	struct ice_dpll_pin *pin = &pf->dplls.rclk;

	pin->prop.type = DPLL_PIN_TYPE_SYNCE_ETH_PORT;
	pin->prop.capabilities |= DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
	pin->pf = pf;

	return ice_dpll_pin_state_update(pf, pin,
					 ICE_DPLL_PIN_TYPE_RCLK_INPUT, NULL);
}

/**
 * ice_dpll_init_info_sw_pins - initializes software controlled pin information
 * @pf: board private structure
 *
 * Init information for software controlled pins, cache them in
 * pf->dplls.sma and pf->dplls.ufl.
 *
 * Return:
 * * 0 - success
 * * negative - init failure reason
 */
static int ice_dpll_init_info_sw_pins(struct ice_pf *pf)
{
	u8 freq_supp_num, pin_abs_idx, input_idx_offset = 0;
	struct ice_dplls *d = &pf->dplls;
	struct ice_dpll_pin *pin;
	u32 phase_adj_max, caps;
	int i, ret;
	u8 data;

	if (pf->hw.device_id == ICE_DEV_ID_E810C_QSFP)
		input_idx_offset = ICE_E810_RCLK_PINS_NUM;
	phase_adj_max = max(d->input_phase_adj_max, d->output_phase_adj_max);
	caps = DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
	for (i = 0; i < ICE_DPLL_PIN_SW_NUM; i++) {
		pin = &d->sma[i];
		pin->idx = i;
		pin->prop.type = DPLL_PIN_TYPE_EXT;
		pin_abs_idx = ICE_DPLL_PIN_SW_INPUT_ABS(i) + input_idx_offset;
		pin->prop.freq_supported =
			ice_cgu_get_pin_freq_supp(&pf->hw, pin_abs_idx,
						  true, &freq_supp_num);
		pin->prop.freq_supported_num = freq_supp_num;
		pin->prop.capabilities =
			(DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE |
			 DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE |
			 caps);
		pin->pf = pf;
		pin->prop.board_label = ice_dpll_sw_pin_sma[i];
		pin->input = &d->inputs[pin_abs_idx];
		if (pin->input->ref_sync)
			pin->ref_sync = pin->input->ref_sync - pin_abs_idx;
		pin->output = &d->outputs[ICE_DPLL_PIN_SW_OUTPUT_ABS(i)];
		ice_dpll_phase_range_set(&pin->prop.phase_range, phase_adj_max);
	}
	for (i = 0; i < ICE_DPLL_PIN_SW_NUM; i++) {
		pin = &d->ufl[i];
		pin->idx = i;
		pin->prop.type = DPLL_PIN_TYPE_EXT;
		pin->prop.capabilities = caps;
		pin->pf = pf;
		pin->prop.board_label = ice_dpll_sw_pin_ufl[i];
		if (i == ICE_DPLL_PIN_SW_1_IDX) {
			pin->direction = DPLL_PIN_DIRECTION_OUTPUT;
			pin_abs_idx = ICE_DPLL_PIN_SW_OUTPUT_ABS(i);
			pin->prop.freq_supported =
				ice_cgu_get_pin_freq_supp(&pf->hw, pin_abs_idx,
							  false,
							  &freq_supp_num);
			pin->prop.freq_supported_num = freq_supp_num;
			pin->input = NULL;
			pin->output = &d->outputs[pin_abs_idx];
		} else if (i == ICE_DPLL_PIN_SW_2_IDX) {
			pin->direction = DPLL_PIN_DIRECTION_INPUT;
			pin_abs_idx = ICE_DPLL_PIN_SW_INPUT_ABS(i) +
				      input_idx_offset;
			pin->output = NULL;
			pin->input = &d->inputs[pin_abs_idx];
			pin->prop.freq_supported =
				ice_cgu_get_pin_freq_supp(&pf->hw, pin_abs_idx,
							  true, &freq_supp_num);
			pin->prop.freq_supported_num = freq_supp_num;
			pin->prop.capabilities =
				(DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE |
				 caps);
		}
		ice_dpll_phase_range_set(&pin->prop.phase_range, phase_adj_max);
	}

	/* Initialize the SMA control register to a known-good default state.
	 * Without this write the PCA9575 GPIO expander retains its power-on
	 * default (all outputs high) which makes all SW pins appear inactive.
	 * Set SMA1 and SMA2 as active inputs, disable U.FL1 output and
	 * U.FL2 input.
	 */
	ret = ice_read_sma_ctrl(&pf->hw, &data);
	if (ret)
		return ret;
	data &= ~ICE_ALL_SMA_MASK;
	data |= ICE_SMA1_TX_EN | ICE_SMA2_TX_EN | ICE_SMA2_UFL2_RX_DIS;
	ret = ice_write_sma_ctrl(&pf->hw, data);
	if (ret)
		return ret;

	ret = ice_dpll_pin_state_update(pf, pin, ICE_DPLL_PIN_TYPE_SOFTWARE,
					NULL);
	if (ret)
		return ret;

	return 0;
}

/**
 * ice_dpll_init_info_txclk_pins_e825c - initializes tx-clk pins information
 * @pf: board private structure
 *
 * Init information for tx-clks pin, cache them in pf->dplls.txclks
 *
 * Return:
 * * 0 - success
 */
static int ice_dpll_init_info_txclk_pins_e825c(struct ice_pf *pf)
{
	struct ice_dpll_pin *tx_pin;

	for (int i = 0; i < ICE_DPLL_TXCLK_NUM_MAX; i++) {
		tx_pin = &pf->dplls.txclks[i];
		tx_pin->prop.type = DPLL_PIN_TYPE_EXT;
		tx_pin->prop.capabilities |=
				 DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
		tx_pin->pf = pf;
		if (i == E825_EXT_EREF_PIN_IDX) {
			tx_pin->prop.board_label = ice_dpll_ext_eref_pin;
			tx_pin->tx_ref_src = ICE_REF_CLK_EREF0;
		} else if (i == E825_EXT_SYNCE_PIN_IDX) {
			tx_pin->tx_ref_src = ICE_REF_CLK_SYNCE;
		}
	}

	return 0;
}

/**
 * ice_dpll_init_pins_info - init pins info wrapper
 * @pf: board private structure
 * @pin_type: type of pins being initialized
 *
 * Wraps functions for pin initialization.
 *
 * Return:
 * * 0 - success
 * * negative - init failure reason
 */
static int
ice_dpll_init_pins_info(struct ice_pf *pf, enum ice_dpll_pin_type pin_type)
{
	switch (pin_type) {
	case ICE_DPLL_PIN_TYPE_INPUT:
	case ICE_DPLL_PIN_TYPE_OUTPUT:
		return ice_dpll_init_info_direct_pins(pf, pin_type);
	case ICE_DPLL_PIN_TYPE_RCLK_INPUT:
		if (pf->hw.mac_type == ICE_MAC_GENERIC_3K_E825)
			return ice_dpll_init_info_pin_on_pin_e825c(pf);
		else
			return ice_dpll_init_info_rclk_pin(pf);
	case ICE_DPLL_PIN_TYPE_SOFTWARE:
		return ice_dpll_init_info_sw_pins(pf);

	case ICE_DPLL_PIN_TYPE_TXCLK:
		return ice_dpll_init_info_txclk_pins_e825c(pf);
	default:
		return -EINVAL;
	}
}

/**
 * ice_dpll_deinit_info - release memory allocated for pins info
 * @pf: board private structure
 *
 * Release memory allocated for pins by ice_dpll_init_info function.
 */
static void ice_dpll_deinit_info(struct ice_pf *pf)
{
	kfree(pf->dplls.inputs);
	pf->dplls.inputs = NULL;
	kfree(pf->dplls.outputs);
	pf->dplls.outputs = NULL;
	kfree(pf->dplls.eec.input_prio);
	pf->dplls.eec.input_prio = NULL;
	kfree(pf->dplls.pps.input_prio);
	pf->dplls.pps.input_prio = NULL;
}

/**
 * ice_dpll_init_info_e825c - prepare pf's dpll information structure for e825c
 * device
 * @pf: board private structure
 *
 * Acquire (from HW) and set basic DPLL information (on pf->dplls struct).
 *
 * Return:
 * * 0 - success
 * * negative - init failure reason
 */
static int ice_dpll_init_info_e825c(struct ice_pf *pf)
{
	struct ice_dplls *d = &pf->dplls;
	struct ice_dpll *dt = &d->txc;
	int ret = 0;
	int i;

	d->clock_id = ice_generate_clock_id(pf);
	d->num_inputs = ICE_SYNCE_CLK_NUM;
	dt->dpll_state = ice_txclk_lock_status(pf->ptp.port.tx_clk);
	dt->mode = DPLL_MODE_MANUAL;
	dt->dpll_idx = pf->ptp.port.port_num;

	d->inputs = kzalloc_objs(*d->inputs, d->num_inputs);
	if (!d->inputs)
		return -ENOMEM;

	ret = ice_get_cgu_rclk_pin_info(&pf->hw, &d->base_rclk_idx,
					&pf->dplls.rclk.num_parents);
	if (ret)
		goto deinit_info;

	for (i = 0; i < pf->dplls.rclk.num_parents; i++)
		pf->dplls.rclk.parent_idx[i] = d->base_rclk_idx + i;

	ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_RCLK_INPUT);
	if (ret)
		goto deinit_info;

	ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_TXCLK);
	if (ret)
		goto deinit_info;

	dev_dbg(ice_pf_to_dev(pf),
		"%s - success, inputs: %u, outputs: %u, rclk-parents: %u\n",
		 __func__, d->num_inputs, d->num_outputs, d->rclk.num_parents);
	return 0;
deinit_info:
	ice_dpll_deinit_info(pf);
	return ret;
}

/**
 * ice_dpll_init_info - prepare pf's dpll information structure
 * @pf: board private structure
 * @cgu: if cgu is present and controlled by this NIC
 *
 * Acquire (from HW) and set basic dpll information (on pf->dplls struct).
 *
 * Return:
 * * 0 - success
 * * negative - init failure reason
 */
static int ice_dpll_init_info(struct ice_pf *pf, bool cgu)
{
	struct ice_aqc_get_cgu_abilities abilities;
	struct ice_dpll *de = &pf->dplls.eec;
	struct ice_dpll *dp = &pf->dplls.pps;
	struct ice_dplls *d = &pf->dplls;
	struct ice_hw *hw = &pf->hw;
	int ret, alloc_size, i;

	d->clock_id = ice_generate_clock_id(pf);
	ret = ice_aq_get_cgu_abilities(hw, &abilities);
	if (ret) {
		dev_err(ice_pf_to_dev(pf),
			"err:%d %s failed to read cgu abilities\n",
			ret, libie_aq_str(hw->adminq.sq_last_status));
		return ret;
	}

	de->dpll_idx = abilities.eec_dpll_idx;
	dp->dpll_idx = abilities.pps_dpll_idx;
	d->num_inputs = abilities.num_inputs;
	d->num_outputs = abilities.num_outputs;
	d->input_phase_adj_max = le32_to_cpu(abilities.max_in_phase_adj) &
		ICE_AQC_GET_CGU_MAX_PHASE_ADJ;
	d->output_phase_adj_max = le32_to_cpu(abilities.max_out_phase_adj) &
		ICE_AQC_GET_CGU_MAX_PHASE_ADJ;

	alloc_size = sizeof(*d->inputs) * d->num_inputs;
	d->inputs = kzalloc(alloc_size, GFP_KERNEL);
	if (!d->inputs)
		return -ENOMEM;

	alloc_size = sizeof(*de->input_prio) * d->num_inputs;
	de->input_prio = kzalloc(alloc_size, GFP_KERNEL);
	if (!de->input_prio) {
		ret = -ENOMEM;
		goto deinit_info;
	}

	dp->input_prio = kzalloc(alloc_size, GFP_KERNEL);
	if (!dp->input_prio) {
		ret = -ENOMEM;
		goto deinit_info;
	}

	ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_INPUT);
	if (ret)
		goto deinit_info;

	if (cgu) {
		alloc_size = sizeof(*d->outputs) * d->num_outputs;
		d->outputs = kzalloc(alloc_size, GFP_KERNEL);
		if (!d->outputs) {
			ret = -ENOMEM;
			goto deinit_info;
		}

		ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_OUTPUT);
		if (ret)
			goto deinit_info;
		ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_SOFTWARE);
		if (ret)
			goto deinit_info;
	}

	ret = ice_get_cgu_rclk_pin_info(&pf->hw, &d->base_rclk_idx,
					&pf->dplls.rclk.num_parents);
	if (ret)
		goto deinit_info;
	for (i = 0; i < pf->dplls.rclk.num_parents; i++)
		pf->dplls.rclk.parent_idx[i] = d->base_rclk_idx + i;
	ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_RCLK_INPUT);
	if (ret)
		goto deinit_info;
	de->mode = DPLL_MODE_AUTOMATIC;
	dp->mode = DPLL_MODE_AUTOMATIC;

	dev_dbg(ice_pf_to_dev(pf),
		"%s - success, inputs:%u, outputs:%u rclk-parents:%u\n",
		__func__, d->num_inputs, d->num_outputs, d->rclk.num_parents);

	return 0;

deinit_info:
	dev_err(ice_pf_to_dev(pf),
		"%s - fail: d->inputs:%p, de->input_prio:%p, dp->input_prio:%p, d->outputs:%p\n",
		__func__, d->inputs, de->input_prio,
		dp->input_prio, d->outputs);
	ice_dpll_deinit_info(pf);
	return ret;
}

/**
 * ice_dpll_deinit - Disable the driver/HW support for dpll subsystem
 * the dpll device.
 * @pf: board private structure
 *
 * Handles the cleanup work required after dpll initialization, freeing
 * resources and unregistering the dpll, pin and all resources used for
 * handling them.
 *
 * Context: Destroys pf->dplls.lock mutex. Call only if ICE_FLAG_DPLL was set.
 */
void ice_dpll_deinit(struct ice_pf *pf)
{
	bool cgu = ice_is_feature_supported(pf, ICE_F_CGU);

	/* Clear ICE_FLAG_DPLL under the lock so that any new caller of
	 * ice_txclk_update_and_notify() observes the cleared flag and
	 * returns early. In-flight callers that already passed the flag
	 * check hold txclk_notify_rwsem for read across the out-of-lock
	 * dpll_*_change_ntf() calls; the down_write/up_write barrier
	 * below waits for them to finish before pins and the TXC DPLL
	 * device may be freed.
	 */
	mutex_lock(&pf->dplls.lock);
	clear_bit(ICE_FLAG_DPLL, pf->flags);
	mutex_unlock(&pf->dplls.lock);

	/* Wait for in-flight ice_txclk_update_and_notify() readers */
	if (pf->hw.mac_type == ICE_MAC_GENERIC_3K_E825) {
		down_write(&pf->dplls.txclk_notify_rwsem);
		up_write(&pf->dplls.txclk_notify_rwsem);
	}

	if (cgu)
		ice_dpll_deinit_worker(pf);

	if (pf->hw.mac_type == ICE_MAC_GENERIC_3K_E825)
		cancel_work_sync(&pf->dplls.txclk_work);

	ice_dpll_deinit_pins(pf, cgu);
	if (!IS_ERR_OR_NULL(pf->dplls.pps.dpll))
		ice_dpll_deinit_dpll(pf, &pf->dplls.pps, cgu);
	if (!IS_ERR_OR_NULL(pf->dplls.eec.dpll))
		ice_dpll_deinit_dpll(pf, &pf->dplls.eec, cgu);
	if (!IS_ERR_OR_NULL(pf->dplls.txc.dpll))
		ice_dpll_deinit_dpll(pf, &pf->dplls.txc, false);

	ice_dpll_deinit_info(pf);
	mutex_destroy(&pf->dplls.lock);
}

/**
 * ice_dpll_init_e825 - initialize support for dpll subsystem
 * @pf: board private structure
 *
 * Set up the device dplls, register them and pins connected within Linux dpll
 * subsystem. Allow userspace to obtain state of DPLL and handling of DPLL
 * configuration requests.
 *
 * Context: Initializes pf->dplls.lock mutex.
 */
static void ice_dpll_init_e825(struct ice_pf *pf)
{
	struct ice_dplls *d = &pf->dplls;
	int err;

	/* E825 sets ICE_F_PHY_RCLK unconditionally, so DPLL may run without
	 * PTP. Populate the HW-topology fields the TX-clk path divides by,
	 * otherwise userspace can trigger div-by-zero in ice_txclk_set_clk().
	 * When PTP is supported, ice_ptp_init() handles this.
	 */
	if (!test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags)) {
		ice_ptp_init_hw(&pf->hw);
		if (pf->hw.lane_num >= 0)
			pf->ptp.port.port_num = pf->hw.lane_num;
	}

	mutex_init(&d->lock);
	/* Initialize the txclk worker and its notification rwsem before any
	 * code path can fail: ice_dpll_deinit() runs unconditionally on
	 * failure and calls cancel_work_sync() / down_write() on these.
	 */
	INIT_WORK(&d->txclk_work, ice_dpll_txclk_work);
	init_rwsem(&d->txclk_notify_rwsem);
	init_completion(&d->dpll_init);

	err = ice_dpll_init_info_e825c(pf);
	if (err)
		goto err_exit;
	err = ice_dpll_init_dpll(pf, &pf->dplls.txc, false, DPLL_TYPE_GENERIC);
	if (err)
		goto deinit_info;
	err = ice_dpll_init_pins_e825(pf);
	if (err)
		goto deinit_txclk;
	set_bit(ICE_FLAG_DPLL, pf->flags);
	complete_all(&d->dpll_init);

	return;

deinit_txclk:
	ice_dpll_deinit_dpll(pf, &pf->dplls.txc, false);
deinit_info:
	ice_dpll_deinit_info(pf);
err_exit:
	mutex_destroy(&d->lock);
	dev_warn(ice_pf_to_dev(pf), "DPLLs init failure err:%d\n", err);
}

/**
 * ice_dpll_init_e810 - initialize support for dpll subsystem
 * @pf: board private structure
 *
 * Set up the device dplls, register them and pins connected within Linux dpll
 * subsystem. Allow userspace to obtain state of DPLL and handling of DPLL
 * configuration requests.
 *
 * Context: Initializes pf->dplls.lock mutex.
 */
static void ice_dpll_init_e810(struct ice_pf *pf)
{
	bool cgu = ice_is_feature_supported(pf, ICE_F_CGU);
	struct ice_dplls *d = &pf->dplls;
	int err = 0;

	mutex_init(&d->lock);
	err = ice_dpll_init_info(pf, cgu);
	if (err)
		goto err_exit;
	err = ice_dpll_init_dpll(pf, &pf->dplls.eec, cgu, DPLL_TYPE_EEC);
	if (err)
		goto deinit_info;
	err = ice_dpll_init_dpll(pf, &pf->dplls.pps, cgu, DPLL_TYPE_PPS);
	if (err)
		goto deinit_eec;
	err = ice_dpll_init_pins(pf, cgu);
	if (err)
		goto deinit_pps;
	if (cgu) {
		err = ice_dpll_init_worker(pf);
		if (err)
			goto deinit_pins;
	}
	set_bit(ICE_FLAG_DPLL, pf->flags);

	return;

deinit_pins:
	ice_dpll_deinit_pins(pf, cgu);
deinit_pps:
	ice_dpll_deinit_dpll(pf, &pf->dplls.pps, cgu);
deinit_eec:
	ice_dpll_deinit_dpll(pf, &pf->dplls.eec, cgu);
deinit_info:
	ice_dpll_deinit_info(pf);
err_exit:
	mutex_destroy(&d->lock);
	dev_warn(ice_pf_to_dev(pf), "DPLLs init failure err:%d\n", err);
}

void ice_dpll_init(struct ice_pf *pf)
{
	switch (pf->hw.mac_type) {
	case ICE_MAC_GENERIC_3K_E825:
		ice_dpll_init_e825(pf);
		break;
	default:
		ice_dpll_init_e810(pf);
		break;
	}
}