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
// SPDX-License-Identifier: GPL-2.0
/*
 * Analog Devices LTC2983 Multi-Sensor Digital Temperature Measurement System
 * driver
 *
 * Copyright 2019 Analog Devices Inc.
 */
#include <linux/bitfield.h>
#include <linux/completion.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/iio/iio.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/property.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/spi/spi.h>

#include <asm/byteorder.h>
#include <linux/unaligned.h>

/* register map */
#define LTC2983_STATUS_REG			0x0000
#define LTC2983_TEMP_RES_START_REG		0x0010
#define LTC2983_TEMP_RES_END_REG		0x005F
#define ADT7604_RES_RES_START_REG		0x0060
#define ADT7604_RES_RES_END_REG			0x00AF
#define LTC2983_EEPROM_KEY_REG			0x00B0
#define LTC2983_EEPROM_READ_STATUS_REG		0x00D0
#define LTC2983_GLOBAL_CONFIG_REG		0x00F0
#define LTC2983_MULT_CHANNEL_START_REG		0x00F4
#define LTC2983_MULT_CHANNEL_END_REG		0x00F7
#define LTC2986_EEPROM_STATUS_REG		0x00F9
#define LTC2983_MUX_CONFIG_REG			0x00FF
#define LTC2983_CHAN_ASSIGN_START_REG		0x0200
#define LTC2983_CHAN_ASSIGN_END_REG		0x024F
#define LTC2983_CUST_SENS_TBL_START_REG		0x0250
#define LTC2983_CUST_SENS_TBL_END_REG		0x03CF

#define LTC2983_DIFFERENTIAL_CHAN_MIN		2
#define LTC2983_MIN_CHANNELS_NR			1
#define LTC2983_SLEEP				0x97
#define LTC2983_CUSTOM_STEINHART_SIZE		24
#define LTC2983_CUSTOM_SENSOR_ENTRY_SZ		6
#define LTC2983_CUSTOM_STEINHART_ENTRY_SZ	4

#define LTC2983_EEPROM_KEY			0xA53C0F5A
#define LTC2983_EEPROM_WRITE_CMD		0x15
#define LTC2983_EEPROM_READ_CMD			0x16
#define LTC2983_EEPROM_STATUS_FAILURE_MASK	GENMASK(3, 1)
#define LTC2983_EEPROM_READ_FAILURE_MASK	GENMASK(7, 0)

#define LTC2983_EEPROM_WRITE_TIME_MS		2600
#define LTC2983_EEPROM_READ_TIME_MS		20

#define LTC2983_CHAN_ASSIGN_ADDR(chan) \
			((((chan) - 1) * 4) + LTC2983_CHAN_ASSIGN_START_REG)
#define LTC2983_RESULT_ADDR(chan, base) \
			((((chan) - 1) * 4) + (base))
#define LTC2983_THERMOCOUPLE_DIFF_MASK		BIT(3)
#define LTC2983_THERMOCOUPLE_SGL(x) \
				FIELD_PREP(LTC2983_THERMOCOUPLE_DIFF_MASK, x)
#define LTC2983_THERMOCOUPLE_OC_CURR_MASK	GENMASK(1, 0)
#define LTC2983_THERMOCOUPLE_OC_CURR(x) \
				FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CURR_MASK, x)
#define LTC2983_THERMOCOUPLE_OC_CHECK_MASK	BIT(2)
#define LTC2983_THERMOCOUPLE_OC_CHECK(x) \
			FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CHECK_MASK, x)

#define LTC2983_THERMISTOR_DIFF_MASK		BIT(2)
#define LTC2983_THERMISTOR_SGL(x) \
				FIELD_PREP(LTC2983_THERMISTOR_DIFF_MASK, x)
#define LTC2983_THERMISTOR_R_SHARE_MASK		BIT(1)
#define LTC2983_THERMISTOR_R_SHARE(x) \
				FIELD_PREP(LTC2983_THERMISTOR_R_SHARE_MASK, x)
#define LTC2983_THERMISTOR_C_ROTATE_MASK	BIT(0)
#define LTC2983_THERMISTOR_C_ROTATE(x) \
				FIELD_PREP(LTC2983_THERMISTOR_C_ROTATE_MASK, x)

#define LTC2983_DIODE_DIFF_MASK			BIT(2)
#define LTC2983_DIODE_SGL(x) \
			FIELD_PREP(LTC2983_DIODE_DIFF_MASK, x)
#define LTC2983_DIODE_3_CONV_CYCLE_MASK		BIT(1)
#define LTC2983_DIODE_3_CONV_CYCLE(x) \
				FIELD_PREP(LTC2983_DIODE_3_CONV_CYCLE_MASK, x)
#define LTC2983_DIODE_AVERAGE_ON_MASK		BIT(0)
#define LTC2983_DIODE_AVERAGE_ON(x) \
				FIELD_PREP(LTC2983_DIODE_AVERAGE_ON_MASK, x)

#define LTC2983_RTD_4_WIRE_MASK			BIT(3)
#define LTC2983_RTD_ROTATION_MASK		BIT(1)
#define LTC2983_RTD_C_ROTATE(x) \
			FIELD_PREP(LTC2983_RTD_ROTATION_MASK, x)
#define LTC2983_RTD_KELVIN_R_SENSE_MASK		GENMASK(3, 2)
#define LTC2983_RTD_N_WIRES_MASK		GENMASK(3, 2)
#define LTC2983_RTD_N_WIRES(x) \
			FIELD_PREP(LTC2983_RTD_N_WIRES_MASK, x)
#define LTC2983_RTD_R_SHARE_MASK		BIT(0)
#define LTC2983_RTD_R_SHARE(x) \
			FIELD_PREP(LTC2983_RTD_R_SHARE_MASK, 1)

#define LTC2983_COMMON_HARD_FAULT_MASK	GENMASK(31, 30)
#define LTC2983_COMMON_SOFT_FAULT_MASK	GENMASK(27, 25)

#define	LTC2983_STATUS_START_MASK	BIT(7)
#define	LTC2983_STATUS_START(x)		FIELD_PREP(LTC2983_STATUS_START_MASK, x)
#define	LTC2983_STATUS_UP_MASK		GENMASK(7, 6)
#define	LTC2983_STATUS_UP(reg)		FIELD_GET(LTC2983_STATUS_UP_MASK, reg)

#define	LTC2983_STATUS_CHAN_SEL_MASK	GENMASK(4, 0)
#define	LTC2983_STATUS_CHAN_SEL(x) \
				FIELD_PREP(LTC2983_STATUS_CHAN_SEL_MASK, x)

#define LTC2983_TEMP_UNITS_MASK		BIT(2)
#define LTC2983_TEMP_UNITS(x)		FIELD_PREP(LTC2983_TEMP_UNITS_MASK, x)

#define LTC2983_NOTCH_FREQ_MASK		GENMASK(1, 0)
#define LTC2983_NOTCH_FREQ(x)		FIELD_PREP(LTC2983_NOTCH_FREQ_MASK, x)

#define LTC2983_RES_VALID_MASK		BIT(24)
#define LTC2983_DATA_MASK		GENMASK(23, 0)
#define LTC2983_DATA_SIGN_BIT		23

#define LTC2983_CHAN_TYPE_MASK		GENMASK(31, 27)
#define LTC2983_CHAN_TYPE(x)		FIELD_PREP(LTC2983_CHAN_TYPE_MASK, x)

/* cold junction for thermocouples and rsense for rtd's and thermistor's */
#define LTC2983_CHAN_ASSIGN_MASK	GENMASK(26, 22)
#define LTC2983_CHAN_ASSIGN(x)		FIELD_PREP(LTC2983_CHAN_ASSIGN_MASK, x)

#define LTC2983_CUSTOM_LEN_MASK		GENMASK(5, 0)
#define LTC2983_CUSTOM_LEN(x)		FIELD_PREP(LTC2983_CUSTOM_LEN_MASK, x)

#define LTC2983_CUSTOM_ADDR_MASK	GENMASK(11, 6)
#define LTC2983_CUSTOM_ADDR(x)		FIELD_PREP(LTC2983_CUSTOM_ADDR_MASK, x)

#define LTC2983_THERMOCOUPLE_CFG_MASK	GENMASK(21, 18)
#define LTC2983_THERMOCOUPLE_CFG(x) \
				FIELD_PREP(LTC2983_THERMOCOUPLE_CFG_MASK, x)
#define LTC2983_THERMOCOUPLE_HARD_FAULT_MASK	GENMASK(31, 29)
#define LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK	GENMASK(28, 25)

#define LTC2983_RTD_CFG_MASK		GENMASK(21, 18)
#define LTC2983_RTD_CFG(x)		FIELD_PREP(LTC2983_RTD_CFG_MASK, x)
#define LTC2983_RTD_EXC_CURRENT_MASK	GENMASK(17, 14)
#define LTC2983_RTD_EXC_CURRENT(x) \
				FIELD_PREP(LTC2983_RTD_EXC_CURRENT_MASK, x)
#define LTC2983_RTD_CURVE_MASK		GENMASK(13, 12)
#define LTC2983_RTD_CURVE(x)		FIELD_PREP(LTC2983_RTD_CURVE_MASK, x)

#define LTC2983_THERMISTOR_CFG_MASK	GENMASK(21, 19)
#define LTC2983_THERMISTOR_CFG(x) \
				FIELD_PREP(LTC2983_THERMISTOR_CFG_MASK, x)
#define LTC2983_THERMISTOR_EXC_CURRENT_MASK	GENMASK(18, 15)
#define LTC2983_THERMISTOR_EXC_CURRENT(x) \
			FIELD_PREP(LTC2983_THERMISTOR_EXC_CURRENT_MASK, x)

#define LTC2983_DIODE_CFG_MASK		GENMASK(26, 24)
#define LTC2983_DIODE_CFG(x)		FIELD_PREP(LTC2983_DIODE_CFG_MASK, x)
#define LTC2983_DIODE_EXC_CURRENT_MASK	GENMASK(23, 22)
#define LTC2983_DIODE_EXC_CURRENT(x) \
				FIELD_PREP(LTC2983_DIODE_EXC_CURRENT_MASK, x)
#define LTC2983_DIODE_IDEAL_FACTOR_MASK	GENMASK(21, 0)
#define LTC2983_DIODE_IDEAL_FACTOR(x) \
				FIELD_PREP(LTC2983_DIODE_IDEAL_FACTOR_MASK, x)

#define LTC2983_R_SENSE_VAL_MASK	GENMASK(26, 0)
#define LTC2983_R_SENSE_VAL(x)		FIELD_PREP(LTC2983_R_SENSE_VAL_MASK, x)

#define LTC2983_ADC_SINGLE_ENDED_MASK	BIT(26)
#define LTC2983_ADC_SINGLE_ENDED(x) \
				FIELD_PREP(LTC2983_ADC_SINGLE_ENDED_MASK, x)

enum {
	LTC2983_SENSOR_THERMOCOUPLE = 1,
	LTC2983_SENSOR_THERMOCOUPLE_CUSTOM = 9,
	LTC2983_SENSOR_RTD = 10,
	LTC2983_SENSOR_RTD_CUSTOM = 18,
	LTC2983_SENSOR_THERMISTOR = 19,
	LTC2983_SENSOR_THERMISTOR_STEINHART = 26,
	LTC2983_SENSOR_THERMISTOR_CUSTOM = 27,
	LTC2983_SENSOR_DIODE = 28,
	LTC2983_SENSOR_SENSE_RESISTOR = 29,
	LTC2983_SENSOR_DIRECT_ADC = 30,
	LTC2983_SENSOR_ACTIVE_TEMP = 31,
	/* Sensor types for some parts only; map to RTD_CUSTOM/THERMISTOR_CUSTOM in HW */
	LTC2983_SENSOR_COPPER_TRACE = 32,
	LTC2983_SENSOR_LEAK_DETECTOR = 33,
	LTC2983_SENSOR_NUM
};

/* Bitmask of sensor types supported by LTC2983/LTC2984 and derivatives */
#define LTC2983_COMMON_SENSORS \
	(GENMASK_ULL(LTC2983_SENSOR_THERMOCOUPLE_CUSTOM, LTC2983_SENSOR_THERMOCOUPLE) | \
	 GENMASK_ULL(LTC2983_SENSOR_RTD_CUSTOM, LTC2983_SENSOR_RTD) | \
	 GENMASK_ULL(LTC2983_SENSOR_THERMISTOR_CUSTOM, LTC2983_SENSOR_THERMISTOR) | \
	 BIT_ULL(LTC2983_SENSOR_DIODE) | \
	 BIT_ULL(LTC2983_SENSOR_SENSE_RESISTOR) | \
	 BIT_ULL(LTC2983_SENSOR_DIRECT_ADC))

/* Bitmask of sensor types supported by ADT7604 */
#define ADT7604_SENSORS \
	(GENMASK_ULL(LTC2983_SENSOR_RTD_CUSTOM - 1, LTC2983_SENSOR_RTD) | \
	 GENMASK_ULL(LTC2983_SENSOR_THERMISTOR_CUSTOM - 1, LTC2983_SENSOR_THERMISTOR) | \
	 BIT_ULL(LTC2983_SENSOR_SENSE_RESISTOR) | \
	 BIT_ULL(LTC2983_SENSOR_COPPER_TRACE) | \
	 BIT_ULL(LTC2983_SENSOR_LEAK_DETECTOR))

#define to_thermocouple(_sensor) \
		container_of(_sensor, struct ltc2983_thermocouple, sensor)

#define to_rtd(_sensor) \
		container_of(_sensor, struct ltc2983_rtd, sensor)

#define to_copper_trace(_sensor) \
		container_of(_sensor, struct ltc2983_copper_trace, sensor)

#define to_thermistor(_sensor) \
		container_of(_sensor, struct ltc2983_thermistor, sensor)

#define to_leak_detector(_sensor) \
		container_of(_sensor, struct ltc2983_leak_detector, sensor)

#define to_diode(_sensor) \
		container_of(_sensor, struct ltc2983_diode, sensor)

#define to_rsense(_sensor) \
		container_of(_sensor, struct ltc2983_rsense, sensor)

#define to_adc(_sensor) \
		container_of(_sensor, struct ltc2983_adc, sensor)

#define to_temp(_sensor) \
		container_of(_sensor, struct ltc2983_temp, sensor)

struct ltc2983_chip_info {
	const char *name;
	unsigned int max_channels_nr;
	u64 supported_sensors;
	bool has_eeprom;
};

struct ltc2983_data {
	const struct ltc2983_chip_info *info;
	struct regmap *regmap;
	struct spi_device *spi;
	struct mutex lock;
	struct completion completion;
	struct iio_chan_spec *iio_chan;
	struct ltc2983_sensor **sensors;
	u32 mux_delay_config;
	u32 filter_notch_freq;
	u16 custom_table_size;
	u8 num_channels;
	u8 iio_channels;
	/*
	 * DMA (thus cache coherency maintenance) may require the
	 * transfer buffers to live in their own cache lines.
	 * Holds the converted temperature
	 */
	__be32 temp __aligned(IIO_DMA_MINALIGN);
	__be32 chan_val;
	__be32 eeprom_key;
};

struct ltc2983_sensor {
	int (*fault_handler)(const struct ltc2983_data *st, const u32 result);
	int (*assign_chan)(struct ltc2983_data *st,
			   const struct ltc2983_sensor *sensor);
	/* specifies the sensor channel */
	u32 chan;
	/* sensor type */
	u32 type;
	/* number of IIO channels this sensor produces */
	u8 n_iio_chan;
};

struct ltc2983_custom_sensor {
	/* raw table sensor data */
	void *table;
	size_t size;
	/* address offset */
	s8 offset;
	bool is_steinhart;
};

struct ltc2983_thermocouple {
	struct ltc2983_sensor sensor;
	struct ltc2983_custom_sensor *custom;
	u32 sensor_config;
	u32 cold_junction_chan;
};

struct ltc2983_rtd {
	struct ltc2983_sensor sensor;
	struct ltc2983_custom_sensor *custom;
	u32 sensor_config;
	u32 r_sense_chan;
	u32 excitation_current;
	u32 rtd_curve;
};

struct ltc2983_copper_trace {
	struct ltc2983_sensor sensor;
	struct ltc2983_custom_sensor *custom;
	u32 r_sense_chan;
	u32 excitation_current;
	/* selects the <1Ω variant: bits 17:0 of the channel word are zeroed,
	 * disabling excitation current and custom table fields (ADT7604
	 * datasheet Table 26)
	 */
	bool is_sub_ohm;
};

struct ltc2983_leak_detector {
	struct ltc2983_sensor sensor;
	struct ltc2983_custom_sensor *custom;
	u32 r_sense_chan;
	u32 excitation_current;
};

struct ltc2983_thermistor {
	struct ltc2983_sensor sensor;
	struct ltc2983_custom_sensor *custom;
	u32 sensor_config;
	u32 r_sense_chan;
	u32 excitation_current;
};

struct ltc2983_diode {
	struct ltc2983_sensor sensor;
	u32 sensor_config;
	u32 excitation_current;
	u32 ideal_factor_value;
};

struct ltc2983_rsense {
	struct ltc2983_sensor sensor;
	u32 r_sense_val;
};

struct ltc2983_adc {
	struct ltc2983_sensor sensor;
	bool single_ended;
};

struct ltc2983_temp {
	struct ltc2983_sensor sensor;
	struct ltc2983_custom_sensor *custom;
	bool single_ended;
};

/*
 * Convert to Q format numbers. These number's are integers where
 * the number of integer and fractional bits are specified. The resolution
 * is given by 1/@resolution and tell us the number of fractional bits. For
 * instance a resolution of 2^-10 means we have 10 fractional bits.
 */
static u32 __convert_to_raw(const u64 val, const u32 resolution)
{
	u64 __res = val * resolution;

	/* all values are multiplied by 1000000 to remove the fraction */
	do_div(__res, 1000000);

	return __res;
}

static u32 __convert_to_raw_sign(const u64 val, const u32 resolution)
{
	s64 __res = -(s32)val;

	__res = __convert_to_raw(__res, resolution);

	return (u32)-__res;
}

static int __ltc2983_fault_handler(const struct ltc2983_data *st,
				   const u32 result, const u32 hard_mask,
				   const u32 soft_mask)
{
	const struct device *dev = &st->spi->dev;

	if (result & hard_mask) {
		dev_err(dev, "Invalid conversion: Sensor HARD fault\n");
		return -EIO;
	} else if (result & soft_mask) {
		/* just print a warning */
		dev_warn(dev, "Suspicious conversion: Sensor SOFT fault\n");
	}

	return 0;
}

static int __ltc2983_chan_assign_common(struct ltc2983_data *st,
					const struct ltc2983_sensor *sensor,
					u32 chan_val)
{
	struct device *dev = &st->spi->dev;
	u32 reg = LTC2983_CHAN_ASSIGN_ADDR(sensor->chan);
	u32 hw_type = sensor->type;

	if (hw_type == LTC2983_SENSOR_COPPER_TRACE)
		hw_type = LTC2983_SENSOR_RTD_CUSTOM;
	else if (hw_type == LTC2983_SENSOR_LEAK_DETECTOR)
		hw_type = LTC2983_SENSOR_THERMISTOR_CUSTOM;

	chan_val |= LTC2983_CHAN_TYPE(hw_type);
	dev_dbg(dev, "Assign reg:0x%04X, val:0x%08X\n", reg, chan_val);
	st->chan_val = cpu_to_be32(chan_val);
	return regmap_bulk_write(st->regmap, reg, &st->chan_val,
				 sizeof(st->chan_val));
}

static int __ltc2983_chan_custom_sensor_assign(struct ltc2983_data *st,
					  struct ltc2983_custom_sensor *custom,
					  u32 *chan_val)
{
	u32 reg;
	u8 mult = custom->is_steinhart ? LTC2983_CUSTOM_STEINHART_ENTRY_SZ :
		LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
	const struct device *dev = &st->spi->dev;
	/*
	 * custom->size holds the raw size of the table. However, when
	 * configuring the sensor channel, we must write the number of
	 * entries of the table minus 1. For steinhart sensors 0 is written
	 * since the size is constant!
	 */
	const u8 len = custom->is_steinhart ? 0 :
		(custom->size / LTC2983_CUSTOM_SENSOR_ENTRY_SZ) - 1;
	/*
	 * Check if the offset was assigned already. It should be for steinhart
	 * sensors. When coming from sleep, it should be assigned for all.
	 */
	if (custom->offset < 0) {
		/*
		 * This needs to be done again here because, from the moment
		 * when this test was done (successfully) for this custom
		 * sensor, a steinhart sensor might have been added changing
		 * custom_table_size...
		 */
		if (st->custom_table_size + custom->size >
		    (LTC2983_CUST_SENS_TBL_END_REG -
		     LTC2983_CUST_SENS_TBL_START_REG) + 1) {
			dev_err(dev,
				"Not space left(%d) for new custom sensor(%zu)",
				st->custom_table_size,
				custom->size);
			return -EINVAL;
		}

		custom->offset = st->custom_table_size /
					LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
		st->custom_table_size += custom->size;
	}

	reg = (custom->offset * mult) + LTC2983_CUST_SENS_TBL_START_REG;

	*chan_val |= LTC2983_CUSTOM_LEN(len);
	*chan_val |= LTC2983_CUSTOM_ADDR(custom->offset);
	dev_dbg(dev, "Assign custom sensor, reg:0x%04X, off:%d, sz:%zu",
		reg, custom->offset,
		custom->size);
	/* write custom sensor table */
	return regmap_bulk_write(st->regmap, reg, custom->table, custom->size);
}

static struct ltc2983_custom_sensor *
__ltc2983_custom_sensor_new(struct ltc2983_data *st, const struct fwnode_handle *fn,
			    const char *propname, const bool is_steinhart,
			    const u32 resolution, const bool has_signed)
{
	struct ltc2983_custom_sensor *new_custom;
	struct device *dev = &st->spi->dev;
	/*
	 * For custom steinhart, the full u32 is taken. For all the others
	 * the MSB is discarded.
	 */
	const u8 n_size = is_steinhart ? 4 : 3;
	u8 index, n_entries;
	int ret;

	if (is_steinhart)
		n_entries = fwnode_property_count_u32(fn, propname);
	else
		n_entries = fwnode_property_count_u64(fn, propname);
	/* n_entries must be an even number */
	if (!n_entries || (n_entries % 2) != 0)
		return dev_err_ptr_probe(dev, -EINVAL,
					 "Number of entries either 0 or not even\n");

	new_custom = devm_kzalloc(dev, sizeof(*new_custom), GFP_KERNEL);
	if (!new_custom)
		return ERR_PTR(-ENOMEM);

	new_custom->size = n_entries * n_size;
	/* check Steinhart size */
	if (is_steinhart && new_custom->size != LTC2983_CUSTOM_STEINHART_SIZE)
		return dev_err_ptr_probe(dev, -EINVAL,
					 "Steinhart sensors size(%zu) must be %u\n",
					 new_custom->size, LTC2983_CUSTOM_STEINHART_SIZE);

	/* Check space on the table. */
	if (st->custom_table_size + new_custom->size >
	    (LTC2983_CUST_SENS_TBL_END_REG - LTC2983_CUST_SENS_TBL_START_REG) + 1)
		return dev_err_ptr_probe(dev, -EINVAL,
					 "No space left(%d) for new custom sensor(%zu)\n",
					 st->custom_table_size, new_custom->size);

	/* allocate the table */
	if (is_steinhart)
		new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u32), GFP_KERNEL);
	else
		new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u64), GFP_KERNEL);
	if (!new_custom->table)
		return ERR_PTR(-ENOMEM);

	/*
	 * Steinhart sensors are configured with raw values in the firmware
	 * node. For the other sensors we must convert the value to raw.
	 * The odd index's correspond to temperatures and always have 1/1024
	 * of resolution. Temperatures also come in Kelvin, so signed values
	 * are not possible.
	 */
	if (is_steinhart) {
		ret = fwnode_property_read_u32_array(fn, propname, new_custom->table, n_entries);
		if (ret < 0)
			return ERR_PTR(ret);

		cpu_to_be32_array(new_custom->table, new_custom->table, n_entries);
	} else {
		ret = fwnode_property_read_u64_array(fn, propname, new_custom->table, n_entries);
		if (ret < 0)
			return ERR_PTR(ret);

		for (index = 0; index < n_entries; index++) {
			u64 temp = ((u64 *)new_custom->table)[index];

			/*
			 * Users specify plain coverage percentage (0-100). Convert
			 * to µK so __convert_to_raw() produces the correct hardware
			 * encoding: P + 273.15 K.
			 */
			if ((index % 2) != 0 && !strcmp(propname, "adi,custom-leak-detector"))
				temp = temp * 1000000 + 273150000;

			if ((index % 2) != 0)
				temp = __convert_to_raw(temp, 1024);
			else if (has_signed && (s64)temp < 0)
				temp = __convert_to_raw_sign(temp, resolution);
			else
				temp = __convert_to_raw(temp, resolution);

			put_unaligned_be24(temp, new_custom->table + index * 3);
		}
	}

	new_custom->is_steinhart = is_steinhart;
	/*
	 * This is done to first add all the steinhart sensors to the table,
	 * in order to maximize the table usage. If we mix adding steinhart
	 * with the other sensors, we might have to do some roundup to make
	 * sure that sensor_addr - 0x250(start address) is a multiple of 4
	 * (for steinhart), and a multiple of 6 for all the other sensors.
	 * Since we have const 24 bytes for steinhart sensors and 24 is
	 * also a multiple of 6, we guarantee that the first non-steinhart
	 * sensor will sit in a correct address without the need of filling
	 * addresses.
	 */
	if (is_steinhart) {
		new_custom->offset = st->custom_table_size /
					LTC2983_CUSTOM_STEINHART_ENTRY_SZ;
		st->custom_table_size += new_custom->size;
	} else {
		/* mark as unset. This is checked later on the assign phase */
		new_custom->offset = -1;
	}

	return new_custom;
}

static int ltc2983_thermocouple_fault_handler(const struct ltc2983_data *st,
					      const u32 result)
{
	return __ltc2983_fault_handler(st, result,
				       LTC2983_THERMOCOUPLE_HARD_FAULT_MASK,
				       LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK);
}

static int ltc2983_common_fault_handler(const struct ltc2983_data *st,
					const u32 result)
{
	return __ltc2983_fault_handler(st, result,
				       LTC2983_COMMON_HARD_FAULT_MASK,
				       LTC2983_COMMON_SOFT_FAULT_MASK);
}

static int ltc2983_thermocouple_assign_chan(struct ltc2983_data *st,
				const struct ltc2983_sensor *sensor)
{
	struct ltc2983_thermocouple *thermo = to_thermocouple(sensor);
	u32 chan_val;

	chan_val = LTC2983_CHAN_ASSIGN(thermo->cold_junction_chan);
	chan_val |= LTC2983_THERMOCOUPLE_CFG(thermo->sensor_config);

	if (thermo->custom) {
		int ret;

		ret = __ltc2983_chan_custom_sensor_assign(st, thermo->custom,
							  &chan_val);
		if (ret)
			return ret;
	}
	return __ltc2983_chan_assign_common(st, sensor, chan_val);
}

static int ltc2983_rtd_assign_chan(struct ltc2983_data *st,
				   const struct ltc2983_sensor *sensor)
{
	struct ltc2983_rtd *rtd = to_rtd(sensor);
	u32 chan_val;

	chan_val = LTC2983_CHAN_ASSIGN(rtd->r_sense_chan);
	chan_val |= LTC2983_RTD_CFG(rtd->sensor_config);
	chan_val |= LTC2983_RTD_EXC_CURRENT(rtd->excitation_current);
	chan_val |= LTC2983_RTD_CURVE(rtd->rtd_curve);

	if (rtd->custom) {
		int ret;

		ret = __ltc2983_chan_custom_sensor_assign(st, rtd->custom,
							  &chan_val);
		if (ret)
			return ret;
	}
	return __ltc2983_chan_assign_common(st, sensor, chan_val);
}

static int ltc2983_copper_trace_assign_chan(struct ltc2983_data *st,
					    const struct ltc2983_sensor *sensor)
{
	struct ltc2983_copper_trace *ct = to_copper_trace(sensor);
	u32 chan_val;

	chan_val = LTC2983_CHAN_ASSIGN(ct->r_sense_chan);
	/* Sensor config bits 21:18 must be 0b1001 (ADT7604 datasheet Table 26) */
	chan_val |= LTC2983_RTD_CFG(0x9);

	if (ct->is_sub_ohm) {
		chan_val &= ~GENMASK(17, 0);
	} else {
		int ret;

		chan_val |= LTC2983_RTD_EXC_CURRENT(ct->excitation_current);
		ret = __ltc2983_chan_custom_sensor_assign(st, ct->custom,
							  &chan_val);
		if (ret)
			return ret;
	}

	return __ltc2983_chan_assign_common(st, sensor, chan_val);
}

static int ltc2983_thermistor_assign_chan(struct ltc2983_data *st,
					  const struct ltc2983_sensor *sensor)
{
	struct ltc2983_thermistor *thermistor = to_thermistor(sensor);
	u32 chan_val;

	chan_val = LTC2983_CHAN_ASSIGN(thermistor->r_sense_chan);
	chan_val |= LTC2983_THERMISTOR_CFG(thermistor->sensor_config);
	chan_val |=
		LTC2983_THERMISTOR_EXC_CURRENT(thermistor->excitation_current);

	if (thermistor->custom) {
		int ret;

		ret = __ltc2983_chan_custom_sensor_assign(st,
							  thermistor->custom,
							  &chan_val);
		if (ret)
			return ret;
	}
	return __ltc2983_chan_assign_common(st, sensor, chan_val);
}

static int ltc2983_leak_detector_assign_chan(struct ltc2983_data *st,
					     const struct ltc2983_sensor *sensor)
{
	struct ltc2983_leak_detector *ld = to_leak_detector(sensor);
	u32 chan_val;
	int ret;

	chan_val = LTC2983_CHAN_ASSIGN(ld->r_sense_chan);
	/* bits 21:19 must be 0b001 (ADT7604 datasheet Table 38) */
	chan_val |= LTC2983_THERMISTOR_CFG(1);
	chan_val |= LTC2983_THERMISTOR_EXC_CURRENT(ld->excitation_current);

	ret = __ltc2983_chan_custom_sensor_assign(st, ld->custom, &chan_val);
	if (ret)
		return ret;

	return __ltc2983_chan_assign_common(st, sensor, chan_val);
}

static int ltc2983_diode_assign_chan(struct ltc2983_data *st,
				     const struct ltc2983_sensor *sensor)
{
	struct ltc2983_diode *diode = to_diode(sensor);
	u32 chan_val;

	chan_val = LTC2983_DIODE_CFG(diode->sensor_config);
	chan_val |= LTC2983_DIODE_EXC_CURRENT(diode->excitation_current);
	chan_val |= LTC2983_DIODE_IDEAL_FACTOR(diode->ideal_factor_value);

	return __ltc2983_chan_assign_common(st, sensor, chan_val);
}

static int ltc2983_r_sense_assign_chan(struct ltc2983_data *st,
				       const struct ltc2983_sensor *sensor)
{
	struct ltc2983_rsense *rsense = to_rsense(sensor);
	u32 chan_val;

	chan_val = LTC2983_R_SENSE_VAL(rsense->r_sense_val);

	return __ltc2983_chan_assign_common(st, sensor, chan_val);
}

static int ltc2983_adc_assign_chan(struct ltc2983_data *st,
				   const struct ltc2983_sensor *sensor)
{
	struct ltc2983_adc *adc = to_adc(sensor);
	u32 chan_val;

	chan_val = LTC2983_ADC_SINGLE_ENDED(adc->single_ended);

	return __ltc2983_chan_assign_common(st, sensor, chan_val);
}

static int ltc2983_temp_assign_chan(struct ltc2983_data *st,
				    const struct ltc2983_sensor *sensor)
{
	struct ltc2983_temp *temp = to_temp(sensor);
	u32 chan_val;
	int ret;

	chan_val = LTC2983_ADC_SINGLE_ENDED(temp->single_ended);

	ret = __ltc2983_chan_custom_sensor_assign(st, temp->custom, &chan_val);
	if (ret)
		return ret;

	return __ltc2983_chan_assign_common(st, sensor, chan_val);
}

static struct ltc2983_sensor *
ltc2983_thermocouple_new(const struct fwnode_handle *child, struct ltc2983_data *st,
			 const struct ltc2983_sensor *sensor)
{
	struct device *dev = &st->spi->dev;
	struct ltc2983_thermocouple *thermo;
	u32 oc_current;
	int ret;

	thermo = devm_kzalloc(dev, sizeof(*thermo), GFP_KERNEL);
	if (!thermo)
		return ERR_PTR(-ENOMEM);

	if (fwnode_property_read_bool(child, "adi,single-ended"))
		thermo->sensor_config = LTC2983_THERMOCOUPLE_SGL(1);

	if (fwnode_property_present(child, "adi,sensor-oc-current-microamp")) {
		ret = fwnode_property_read_u32(child,
					       "adi,sensor-oc-current-microamp",
					       &oc_current);
		if (ret)
			return dev_err_ptr_probe(dev, ret,
						 "Failed to read adi,sensor-oc-current-microamp\n");

		switch (oc_current) {
		case 10:
			thermo->sensor_config |=
					LTC2983_THERMOCOUPLE_OC_CURR(0);
			break;
		case 100:
			thermo->sensor_config |=
					LTC2983_THERMOCOUPLE_OC_CURR(1);
			break;
		case 500:
			thermo->sensor_config |=
					LTC2983_THERMOCOUPLE_OC_CURR(2);
			break;
		case 1000:
			thermo->sensor_config |=
					LTC2983_THERMOCOUPLE_OC_CURR(3);
			break;
		default:
			return dev_err_ptr_probe(dev, -EINVAL,
						 "Invalid open circuit current:%u\n",
						 oc_current);
		}

		thermo->sensor_config |= LTC2983_THERMOCOUPLE_OC_CHECK(1);
	}
	/* validate channel index */
	if (!(thermo->sensor_config & LTC2983_THERMOCOUPLE_DIFF_MASK) &&
	    sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
		return dev_err_ptr_probe(dev, -EINVAL,
					 "Invalid channel %d for differential thermocouple\n",
					 sensor->chan);

	struct fwnode_handle *ref __free(fwnode_handle) =
		fwnode_find_reference(child, "adi,cold-junction-handle", 0);
	if (IS_ERR(ref)) {
		ref = NULL;
	} else {
		ret = fwnode_property_read_u32(ref, "reg", &thermo->cold_junction_chan);
		if (ret)
			/*
			 * This would be caught later but we can just return
			 * the error right away.
			 */
			return dev_err_ptr_probe(dev, ret,
						 "Property reg must be given\n");
	}

	/* check custom sensor */
	if (sensor->type == LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
		const char *propname = "adi,custom-thermocouple";

		thermo->custom = __ltc2983_custom_sensor_new(st, child,
							     propname, false,
							     16384, true);
		if (IS_ERR(thermo->custom))
			return ERR_CAST(thermo->custom);
	}

	/* set common parameters */
	thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler;
	thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan;

	return &thermo->sensor;
}

static struct ltc2983_sensor *
ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st,
		const struct ltc2983_sensor *sensor)
{
	struct ltc2983_rtd *rtd;
	int ret = 0;
	struct device *dev = &st->spi->dev;
	u32 excitation_current = 0, n_wires = 2;

	rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
	if (!rtd)
		return ERR_PTR(-ENOMEM);

	struct fwnode_handle *ref __free(fwnode_handle) =
		fwnode_find_reference(child, "adi,rsense-handle", 0);
	if (IS_ERR(ref))
		return dev_err_cast_probe(dev, ref,
					  "Property adi,rsense-handle missing or invalid\n");

	ret = fwnode_property_read_u32(ref, "reg", &rtd->r_sense_chan);
	if (ret)
		return dev_err_ptr_probe(dev, ret,
					 "Property reg must be given\n");

	if (fwnode_property_present(child, "adi,number-of-wires")) {
		ret = fwnode_property_read_u32(child, "adi,number-of-wires", &n_wires);
		if (ret)
			return dev_err_ptr_probe(dev, ret,
						 "Failed to read adi,number-of-wires\n");

		switch (n_wires) {
		case 2:
			rtd->sensor_config = LTC2983_RTD_N_WIRES(0);
			break;
		case 3:
			rtd->sensor_config = LTC2983_RTD_N_WIRES(1);
			break;
		case 4:
			rtd->sensor_config = LTC2983_RTD_N_WIRES(2);
			break;
		case 5:
			/* 4 wires, Kelvin Rsense */
			rtd->sensor_config = LTC2983_RTD_N_WIRES(3);
			break;
		default:
			return dev_err_ptr_probe(dev, -EINVAL,
						 "Invalid number of wires:%u\n",
						 n_wires);
		}
	}

	if (fwnode_property_read_bool(child, "adi,rsense-share")) {
		/* Current rotation is only available with rsense sharing */
		if (fwnode_property_read_bool(child, "adi,current-rotate")) {
			if (n_wires == 2 || n_wires == 3)
				return dev_err_ptr_probe(dev, -EINVAL,
							 "Rotation not allowed for 2/3 Wire RTDs\n");

			rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1);
		} else {
			rtd->sensor_config |= LTC2983_RTD_R_SHARE(1);
		}
	}
	/*
	 * rtd channel indexes are a bit more complicated to validate.
	 * For 4wire RTD with rotation, the channel selection cannot be
	 * >=19 since the channel + 1 is used in this configuration.
	 * For 4wire RTDs with kelvin rsense, the rsense channel cannot be
	 * <=1 since channel - 1 and channel - 2 are used.
	 */
	if (rtd->sensor_config & LTC2983_RTD_4_WIRE_MASK) {
		/* 4-wire */
		u8 min = LTC2983_DIFFERENTIAL_CHAN_MIN,
			max = st->info->max_channels_nr;

		if (rtd->sensor_config & LTC2983_RTD_ROTATION_MASK)
			max = st->info->max_channels_nr - 1;

		if (((rtd->sensor_config & LTC2983_RTD_KELVIN_R_SENSE_MASK)
		     == LTC2983_RTD_KELVIN_R_SENSE_MASK) &&
		    (rtd->r_sense_chan <=  min))
			/* kelvin rsense*/
			return dev_err_ptr_probe(dev, -EINVAL,
						 "Invalid channel %d for kelvin rsense\n",
						 rtd->r_sense_chan);

		if (sensor->chan < min || sensor->chan > max)
			return dev_err_ptr_probe(dev, -EINVAL,
						 "Invalid channel %d for RTD config\n",
						 sensor->chan);
	} else {
		/* same as differential case */
		if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
			return dev_err_ptr_probe(dev, -EINVAL,
						 "Invalid channel %d for RTD\n",
						 sensor->chan);
	}

	/* check custom sensor */
	if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) {
		rtd->custom = __ltc2983_custom_sensor_new(st, child,
							  "adi,custom-rtd",
							  false, 2048, false);
		if (IS_ERR(rtd->custom))
			return ERR_CAST(rtd->custom);
	}

	/* set common parameters */
	rtd->sensor.fault_handler = ltc2983_common_fault_handler;
	rtd->sensor.assign_chan = ltc2983_rtd_assign_chan;

	if (fwnode_property_present(child, "adi,excitation-current-microamp")) {
		ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp",
					       &excitation_current);
		if (ret)
			return dev_err_ptr_probe(dev, ret,
						 "Failed to read adi,excitation-current-microamp\n");

		switch (excitation_current) {
		case 5:
			rtd->excitation_current = 0x01;
			break;
		case 10:
			rtd->excitation_current = 0x02;
			break;
		case 25:
			rtd->excitation_current = 0x03;
			break;
		case 50:
			rtd->excitation_current = 0x04;
			break;
		case 100:
			rtd->excitation_current = 0x05;
			break;
		case 250:
			rtd->excitation_current = 0x06;
			break;
		case 500:
			rtd->excitation_current = 0x07;
			break;
		case 1000:
			rtd->excitation_current = 0x08;
			break;
		default:
			return dev_err_ptr_probe(dev, -EINVAL,
						 "Invalid value for excitation current(%u)\n",
						 excitation_current);
		}
	} else {
		/* default to 5uA */
		rtd->excitation_current = 1;
	}

	if (fwnode_property_present(child, "adi,rtd-curve")) {
		ret = fwnode_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve);
		if (ret)
			return dev_err_ptr_probe(dev, ret,
						 "Failed to read adi,rtd-curve\n");
	}

	return &rtd->sensor;
}

static struct ltc2983_sensor *
ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *st,
		       const struct ltc2983_sensor *sensor)
{
	struct ltc2983_thermistor *thermistor;
	struct device *dev = &st->spi->dev;
	u32 excitation_current = 0;
	int ret = 0;

	thermistor = devm_kzalloc(dev, sizeof(*thermistor), GFP_KERNEL);
	if (!thermistor)
		return ERR_PTR(-ENOMEM);

	struct fwnode_handle *ref __free(fwnode_handle) =
		fwnode_find_reference(child, "adi,rsense-handle", 0);
	if (IS_ERR(ref))
		return dev_err_cast_probe(dev, ref,
					  "Property adi,rsense-handle missing or invalid\n");

	ret = fwnode_property_read_u32(ref, "reg", &thermistor->r_sense_chan);
	if (ret)
		return dev_err_ptr_probe(dev, ret,
					 "rsense channel must be configured...\n");

	if (fwnode_property_read_bool(child, "adi,single-ended")) {
		thermistor->sensor_config = LTC2983_THERMISTOR_SGL(1);
	} else if (fwnode_property_read_bool(child, "adi,rsense-share")) {
		/* rotation is only possible if sharing rsense */
		if (fwnode_property_read_bool(child, "adi,current-rotate"))
			thermistor->sensor_config =
						LTC2983_THERMISTOR_C_ROTATE(1);
		else
			thermistor->sensor_config =
						LTC2983_THERMISTOR_R_SHARE(1);
	}
	/* validate channel index */
	if (!(thermistor->sensor_config & LTC2983_THERMISTOR_DIFF_MASK) &&
	    sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
		return dev_err_ptr_probe(dev, -EINVAL,
					 "Invalid channel %d for differential thermistor\n",
					 sensor->chan);

	/* check custom sensor */
	if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) {
		bool steinhart = false;
		const char *propname;

		if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) {
			steinhart = true;
			propname = "adi,custom-steinhart";
		} else {
			propname = "adi,custom-thermistor";
		}

		thermistor->custom = __ltc2983_custom_sensor_new(st, child,
								 propname,
								 steinhart,
								 64, false);
		if (IS_ERR(thermistor->custom))
			return ERR_CAST(thermistor->custom);
	}
	/* set common parameters */
	thermistor->sensor.fault_handler = ltc2983_common_fault_handler;
	thermistor->sensor.assign_chan = ltc2983_thermistor_assign_chan;

	if (fwnode_property_present(child, "adi,excitation-current-nanoamp")) {
		ret = fwnode_property_read_u32(child, "adi,excitation-current-nanoamp",
					       &excitation_current);
		if (ret)
			return dev_err_ptr_probe(dev, ret,
						 "Failed to read adi,excitation-current-nanoamp\n");

		switch (excitation_current) {
		case 0:
			/* auto range */
			if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART)
				return dev_err_ptr_probe(dev, -EINVAL,
							 "Auto Range not allowed for custom sensors\n");

			thermistor->excitation_current = 0x0c;
			break;
		case 250:
			thermistor->excitation_current = 0x01;
			break;
		case 500:
			thermistor->excitation_current = 0x02;
			break;
		case 1000:
			thermistor->excitation_current = 0x03;
			break;
		case 5000:
			thermistor->excitation_current = 0x04;
			break;
		case 10000:
			thermistor->excitation_current = 0x05;
			break;
		case 25000:
			thermistor->excitation_current = 0x06;
			break;
		case 50000:
			thermistor->excitation_current = 0x07;
			break;
		case 100000:
			thermistor->excitation_current = 0x08;
			break;
		case 250000:
			thermistor->excitation_current = 0x09;
			break;
		case 500000:
			thermistor->excitation_current = 0x0a;
			break;
		case 1000000:
			thermistor->excitation_current = 0x0b;
			break;
		default:
			return dev_err_ptr_probe(dev, -EINVAL,
						 "Invalid value for excitation current(%u)\n",
						 excitation_current);
		}
	} else {
		/* Auto range is not allowed for custom sensors */
		if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART)
			/* default to 1uA */
			thermistor->excitation_current = 0x03;
		else
			/* default to auto-range */
			thermistor->excitation_current = 0x0c;
	}

	return &thermistor->sensor;
}

static struct ltc2983_sensor *
ltc2983_copper_trace_new(const struct fwnode_handle *child, struct ltc2983_data *st,
			 const struct ltc2983_sensor *sensor)
{
	struct device *dev = &st->spi->dev;
	struct ltc2983_copper_trace *ct;
	int ret;

	if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
		return dev_err_ptr_probe(dev, -EINVAL,
					 "Invalid channel %d for copper trace\n",
					 sensor->chan);

	ct = devm_kzalloc(dev, sizeof(*ct), GFP_KERNEL);
	if (!ct)
		return ERR_PTR(-ENOMEM);

	struct fwnode_handle *ref __free(fwnode_handle) =
		fwnode_find_reference(child, "adi,rsense-handle", 0);
	if (IS_ERR(ref))
		return dev_err_cast_probe(dev, ref,
					  "Property adi,rsense-handle missing or invalid\n");

	ret = fwnode_property_read_u32(ref, "reg", &ct->r_sense_chan);
	if (ret)
		return dev_err_ptr_probe(dev, ret, "Property reg must be given\n");

	ct->is_sub_ohm = fwnode_property_read_bool(child, "adi,copper-trace-sub-ohm");

	if (ct->is_sub_ohm && fwnode_property_present(child, "adi,custom-copper-trace"))
		return dev_err_ptr_probe(dev, -EINVAL,
					 "sub-ohm copper trace cannot have a custom table\n");

	if (!ct->is_sub_ohm) {
		u32 excitation_current = 0;

		if (!fwnode_property_present(child, "adi,custom-copper-trace"))
			return dev_err_ptr_probe(dev, -EINVAL,
						 "adi,custom-copper-trace is required for >1 ohm copper trace\n");

		ct->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-copper-trace",
							 false, 2048, false);
		if (IS_ERR(ct->custom))
			return ERR_CAST(ct->custom);

		if (fwnode_property_present(child, "adi,excitation-current-microamp")) {
			ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp",
						       &excitation_current);
			if (ret)
				return dev_err_ptr_probe(dev, ret,
							 "Failed to read adi,excitation-current-microamp\n");

			switch (excitation_current) {
			case 5:
				ct->excitation_current = 0x01;
				break;
			case 10:
				ct->excitation_current = 0x02;
				break;
			case 25:
				ct->excitation_current = 0x03;
				break;
			case 50:
				ct->excitation_current = 0x04;
				break;
			case 100:
				ct->excitation_current = 0x05;
				break;
			case 250:
				ct->excitation_current = 0x06;
				break;
			case 500:
				ct->excitation_current = 0x07;
				break;
			case 1000:
				ct->excitation_current = 0x08;
				break;
			default:
				return dev_err_ptr_probe(dev, -EINVAL,
							 "Invalid value for excitation current(%u)\n",
							 excitation_current);
			}
		} else {
			/* default to 1mA per datasheet recommendation for copper trace */
			ct->excitation_current = 0x08;
		}
	}

	ct->sensor.fault_handler = ltc2983_common_fault_handler;
	ct->sensor.assign_chan = ltc2983_copper_trace_assign_chan;
	if (ct->is_sub_ohm)
		ct->sensor.n_iio_chan = 1;
	else
		ct->sensor.n_iio_chan = 2;

	return &ct->sensor;
}

static struct ltc2983_sensor *
ltc2983_leak_detector_new(const struct fwnode_handle *child, struct ltc2983_data *st,
			  const struct ltc2983_sensor *sensor)
{
	struct device *dev = &st->spi->dev;
	struct ltc2983_leak_detector *ld;
	int ret;
	u32 excitation_current = 0;

	if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
		return dev_err_ptr_probe(dev, -EINVAL,
					 "Invalid channel %d for leak detector\n",
					 sensor->chan);

	ld = devm_kzalloc(dev, sizeof(*ld), GFP_KERNEL);
	if (!ld)
		return ERR_PTR(-ENOMEM);

	struct fwnode_handle *ref __free(fwnode_handle) =
		fwnode_find_reference(child, "adi,rsense-handle", 0);
	if (IS_ERR(ref))
		return dev_err_cast_probe(dev, ref,
					  "Property adi,rsense-handle missing or invalid\n");

	ret = fwnode_property_read_u32(ref, "reg", &ld->r_sense_chan);
	if (ret)
		return dev_err_ptr_probe(dev, ret,
					 "rsense channel must be configured\n");

	if (!fwnode_property_present(child, "adi,custom-leak-detector"))
		return dev_err_ptr_probe(dev, -EINVAL,
					 "adi,custom-leak-detector is required for leak detectors\n");

	ld->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-leak-detector",
						 false, 16, false);
	if (IS_ERR(ld->custom))
		return ERR_CAST(ld->custom);

	ret = fwnode_property_read_u32(child, "adi,excitation-current-nanoamp",
				       &excitation_current);
	if (ret)
		return dev_err_ptr_probe(dev, ret,
					 "adi,excitation-current-nanoamp is required for leak detectors\n");

	switch (excitation_current) {
	case 250:
		ld->excitation_current = 0x01;
		break;
	case 500:
		ld->excitation_current = 0x02;
		break;
	case 1000:
		ld->excitation_current = 0x03;
		break;
	case 5000:
		ld->excitation_current = 0x04;
		break;
	case 10000:
		ld->excitation_current = 0x05;
		break;
	case 25000:
		ld->excitation_current = 0x06;
		break;
	case 50000:
		ld->excitation_current = 0x07;
		break;
	case 100000:
		ld->excitation_current = 0x08;
		break;
	case 250000:
		ld->excitation_current = 0x09;
		break;
	case 500000:
		ld->excitation_current = 0x0a;
		break;
	case 1000000:
		ld->excitation_current = 0x0b;
		break;
	default:
		return dev_err_ptr_probe(dev, -EINVAL,
					 "Invalid value for excitation current(%u)\n",
					 excitation_current);
	}

	ld->sensor.fault_handler = ltc2983_common_fault_handler;
	ld->sensor.assign_chan = ltc2983_leak_detector_assign_chan;
	ld->sensor.n_iio_chan = 2;

	return &ld->sensor;
}

static struct ltc2983_sensor *
ltc2983_diode_new(const struct fwnode_handle *child, const struct ltc2983_data *st,
		  const struct ltc2983_sensor *sensor)
{
	struct device *dev = &st->spi->dev;
	struct ltc2983_diode *diode;
	u32 temp = 0, excitation_current = 0;
	int ret;

	diode = devm_kzalloc(dev, sizeof(*diode), GFP_KERNEL);
	if (!diode)
		return ERR_PTR(-ENOMEM);

	if (fwnode_property_read_bool(child, "adi,single-ended"))
		diode->sensor_config = LTC2983_DIODE_SGL(1);

	if (fwnode_property_read_bool(child, "adi,three-conversion-cycles"))
		diode->sensor_config |= LTC2983_DIODE_3_CONV_CYCLE(1);

	if (fwnode_property_read_bool(child, "adi,average-on"))
		diode->sensor_config |= LTC2983_DIODE_AVERAGE_ON(1);

	/* validate channel index */
	if (!(diode->sensor_config & LTC2983_DIODE_DIFF_MASK) &&
	    sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
		return dev_err_ptr_probe(dev, -EINVAL,
					 "Invalid channel %d for differential diode\n",
					 sensor->chan);

	/* set common parameters */
	diode->sensor.fault_handler = ltc2983_common_fault_handler;
	diode->sensor.assign_chan = ltc2983_diode_assign_chan;

	if (fwnode_property_present(child, "adi,excitation-current-microamp")) {
		ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp",
					       &excitation_current);
		if (ret)
			return dev_err_ptr_probe(dev, ret,
						 "Failed to read adi,excitation-current-microamp\n");

		switch (excitation_current) {
		case 10:
			diode->excitation_current = 0x00;
			break;
		case 20:
			diode->excitation_current = 0x01;
			break;
		case 40:
			diode->excitation_current = 0x02;
			break;
		case 80:
			diode->excitation_current = 0x03;
			break;
		default:
			return dev_err_ptr_probe(dev, -EINVAL,
						 "Invalid value for excitation current(%u)\n",
						 excitation_current);
		}
	}

	if (fwnode_property_present(child, "adi,ideal-factor-value")) {
		ret = fwnode_property_read_u32(child, "adi,ideal-factor-value", &temp);
		if (ret)
			return dev_err_ptr_probe(dev, ret,
						 "Failed to read adi,ideal-factor-value\n");
	}

	/* 2^20 resolution */
	diode->ideal_factor_value = __convert_to_raw(temp, 1048576);

	return &diode->sensor;
}

static struct ltc2983_sensor *ltc2983_r_sense_new(struct fwnode_handle *child,
					struct ltc2983_data *st,
					const struct ltc2983_sensor *sensor)
{
	struct device *dev = &st->spi->dev;
	struct ltc2983_rsense *rsense;
	int ret;
	u32 temp;

	rsense = devm_kzalloc(dev, sizeof(*rsense), GFP_KERNEL);
	if (!rsense)
		return ERR_PTR(-ENOMEM);

	/* validate channel index */
	if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
		return dev_err_ptr_probe(dev, -EINVAL,
					 "Invalid channel %d for r_sense\n",
					 sensor->chan);

	ret = fwnode_property_read_u32(child, "adi,rsense-val-milli-ohms", &temp);
	if (ret)
		return dev_err_ptr_probe(dev, -EINVAL,
					 "Property adi,rsense-val-milli-ohms missing\n");
	/*
	 * Times 1000 because we have milli-ohms and __convert_to_raw
	 * expects scales of 1000000 which are used for all other
	 * properties.
	 * 2^10 resolution
	 */
	rsense->r_sense_val = __convert_to_raw((u64)temp * 1000, 1024);

	/* set common parameters */
	rsense->sensor.assign_chan = ltc2983_r_sense_assign_chan;

	return &rsense->sensor;
}

static struct ltc2983_sensor *ltc2983_adc_new(struct fwnode_handle *child,
					 struct ltc2983_data *st,
					 const struct ltc2983_sensor *sensor)
{
	struct device *dev = &st->spi->dev;
	struct ltc2983_adc *adc;

	adc = devm_kzalloc(dev, sizeof(*adc), GFP_KERNEL);
	if (!adc)
		return ERR_PTR(-ENOMEM);

	if (fwnode_property_read_bool(child, "adi,single-ended"))
		adc->single_ended = true;

	if (!adc->single_ended && sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
		return dev_err_ptr_probe(dev, -EINVAL,
					 "Invalid channel %d for differential ADC\n",
					 sensor->chan);

	/* set common parameters */
	adc->sensor.assign_chan = ltc2983_adc_assign_chan;
	adc->sensor.fault_handler = ltc2983_common_fault_handler;

	return &adc->sensor;
}

static struct ltc2983_sensor *ltc2983_temp_new(struct fwnode_handle *child,
					       struct ltc2983_data *st,
					       const struct ltc2983_sensor *sensor)
{
	struct device *dev = &st->spi->dev;
	struct ltc2983_temp *temp;

	temp = devm_kzalloc(dev, sizeof(*temp), GFP_KERNEL);
	if (!temp)
		return ERR_PTR(-ENOMEM);

	if (fwnode_property_read_bool(child, "adi,single-ended"))
		temp->single_ended = true;

	if (!temp->single_ended && sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
		return dev_err_ptr_probe(dev, -EINVAL,
					 "Invalid channel %d for differential temp\n",
					 sensor->chan);

	temp->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-temp",
						   false, 4096, true);
	if (IS_ERR(temp->custom))
		return ERR_CAST(temp->custom);

	/* set common parameters */
	temp->sensor.assign_chan = ltc2983_temp_assign_chan;
	temp->sensor.fault_handler = ltc2983_common_fault_handler;

	return &temp->sensor;
}

static int ltc2983_chan_read(struct ltc2983_data *st,
			const struct ltc2983_sensor *sensor,
			u32 base_reg, int *val)
{
	struct device *dev = &st->spi->dev;
	u32 start_conversion = 0;
	int ret;
	unsigned long time;

	start_conversion = LTC2983_STATUS_START(true);
	start_conversion |= LTC2983_STATUS_CHAN_SEL(sensor->chan);
	dev_dbg(dev, "Start conversion on channel:%d, status:%02X\n",
		sensor->chan, start_conversion);
	reinit_completion(&st->completion);
	/* start conversion */
	ret = regmap_write(st->regmap, LTC2983_STATUS_REG, start_conversion);
	if (ret)
		return ret;
	/*
	 * wait for conversion to complete.
	 * 300 ms should be more than enough to complete the conversion.
	 * Depending on the sensor configuration, there are 2/3 conversions
	 * cycles of 82ms.
	 */
	time = wait_for_completion_timeout(&st->completion,
					   msecs_to_jiffies(300));
	if (!time) {
		dev_warn(dev, "Conversion timed out\n");
		return -ETIMEDOUT;
	}

	/* read the converted data */
	ret = regmap_bulk_read(st->regmap, LTC2983_RESULT_ADDR(sensor->chan, base_reg),
			       &st->temp, sizeof(st->temp));
	if (ret)
		return ret;

	*val = __be32_to_cpu(st->temp);

	if (base_reg == ADT7604_RES_RES_START_REG) {
		/*
		 * Resistance result register gives a plain unsigned value,
		 * D31 is always 0, no valid bit, no fault bits. Read bits[30:0]
		 * directly — the temperature result format does not apply here.
		 */
		*val &= GENMASK(30, 0);
		return 0;
	}

	if (!(LTC2983_RES_VALID_MASK & *val)) {
		dev_err(dev, "Invalid conversion detected\n");
		return -EIO;
	}

	ret = sensor->fault_handler(st, *val);
	if (ret)
		return ret;

	*val = sign_extend32((*val) & LTC2983_DATA_MASK, LTC2983_DATA_SIGN_BIT);
	return 0;
}

static int ltc2983_read_raw(struct iio_dev *indio_dev,
			    struct iio_chan_spec const *chan,
			    int *val, int *val2, long mask)
{
	struct ltc2983_data *st = iio_priv(indio_dev);
	struct device *dev = &st->spi->dev;
	int ret;

	/* sanity check */
	if (chan->address >= st->num_channels) {
		dev_err(dev, "Invalid channel address: %ld\n", chan->address);
		return -EINVAL;
	}

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		mutex_lock(&st->lock);
		switch (chan->type) {
		case IIO_RESISTANCE:
			ret = ltc2983_chan_read(st, st->sensors[chan->address],
						ADT7604_RES_RES_START_REG, val);
			break;
		default:
			ret = ltc2983_chan_read(st, st->sensors[chan->address],
						LTC2983_TEMP_RES_START_REG, val);
			break;
		}
		mutex_unlock(&st->lock);
		return ret ?: IIO_VAL_INT;
	case IIO_CHAN_INFO_SCALE:
		switch (chan->type) {
		case IIO_TEMP:
			/* value in milli degrees */
			*val = 1000;
			/* 2^10 */
			*val2 = 1024;
			return IIO_VAL_FRACTIONAL;
		case IIO_VOLTAGE:
			/* value in millivolt */
			*val = 1000;
			/* 2^21 */
			*val2 = 2097152;
			return IIO_VAL_FRACTIONAL;
		case IIO_RESISTANCE:
		case IIO_COVERAGE:
			/* value in ohm/percent */
			*val = 1;
			/* 2^10 */
			*val2 = 1024;
			return IIO_VAL_FRACTIONAL;
		default:
			return -EINVAL;
		}
	}

	return -EINVAL;
}

static int ltc2983_reg_access(struct iio_dev *indio_dev,
			      unsigned int reg,
			      unsigned int writeval,
			      unsigned int *readval)
{
	struct ltc2983_data *st = iio_priv(indio_dev);

	if (readval)
		return regmap_read(st->regmap, reg, readval);

	return regmap_write(st->regmap, reg, writeval);
}

static irqreturn_t ltc2983_irq_handler(int irq, void *data)
{
	struct ltc2983_data *st = data;

	complete(&st->completion);
	return IRQ_HANDLED;
}

#define LTC2983_CHAN(__type, index, __address) ({ \
	struct iio_chan_spec __chan = { \
		.type = __type, \
		.indexed = 1, \
		.channel = index, \
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
		.address = __address, \
	}; \
	__chan; \
})

static int ltc2983_parse_fw(struct ltc2983_data *st)
{
	struct device *dev = &st->spi->dev;
	int ret, chan = 0, channel_avail_mask = 0;

	device_property_read_u32(dev, "adi,mux-delay-config-us", &st->mux_delay_config);

	device_property_read_u32(dev, "adi,filter-notch-freq", &st->filter_notch_freq);

	st->num_channels = device_get_child_node_count(dev);
	if (!st->num_channels)
		return dev_err_probe(dev, -EINVAL,
				     "At least one channel must be given!\n");

	st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors),
				   GFP_KERNEL);
	if (!st->sensors)
		return -ENOMEM;

	st->iio_channels = 0;
	device_for_each_child_node_scoped(dev, child) {
		struct ltc2983_sensor sensor;

		ret = fwnode_property_read_u32(child, "reg", &sensor.chan);
		if (ret)
			return dev_err_probe(dev, ret,
				"reg property must given for child nodes\n");

		/* check if we have a valid channel */
		if (sensor.chan < LTC2983_MIN_CHANNELS_NR ||
		    sensor.chan > st->info->max_channels_nr)
			return dev_err_probe(dev, -EINVAL,
					     "channel:%d must be from %u to %u\n",
					     sensor.chan,
					     LTC2983_MIN_CHANNELS_NR,
					     st->info->max_channels_nr);

		if (channel_avail_mask & BIT(sensor.chan))
			return dev_err_probe(dev, -EINVAL,
					     "channel:%d already in use\n",
					     sensor.chan);

		ret = fwnode_property_read_u32(child, "adi,sensor-type", &sensor.type);
		if (ret)
			return dev_err_probe(dev, ret,
				"adi,sensor-type property must given for child nodes\n");

		if (sensor.type >= LTC2983_SENSOR_NUM ||
		    !(st->info->supported_sensors & BIT_ULL(sensor.type)))
			return dev_err_probe(dev, -EINVAL,
					     "sensor type %d not supported on %s\n",
					     sensor.type, st->info->name);

		dev_dbg(dev, "Create new sensor, type %u, channel %u",
			sensor.type, sensor.chan);

		if (sensor.type >= LTC2983_SENSOR_THERMOCOUPLE &&
		    sensor.type <= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
			st->sensors[chan] = ltc2983_thermocouple_new(child, st,
								     &sensor);
		} else if (sensor.type >= LTC2983_SENSOR_RTD &&
			   sensor.type <= LTC2983_SENSOR_RTD_CUSTOM) {
			st->sensors[chan] = ltc2983_rtd_new(child, st, &sensor);
		} else if (sensor.type >= LTC2983_SENSOR_THERMISTOR &&
			   sensor.type <= LTC2983_SENSOR_THERMISTOR_CUSTOM) {
			st->sensors[chan] = ltc2983_thermistor_new(child, st,
								   &sensor);
		} else if (sensor.type == LTC2983_SENSOR_DIODE) {
			st->sensors[chan] = ltc2983_diode_new(child, st,
							      &sensor);
		} else if (sensor.type == LTC2983_SENSOR_SENSE_RESISTOR) {
			st->sensors[chan] = ltc2983_r_sense_new(child, st,
								&sensor);
		} else if (sensor.type == LTC2983_SENSOR_DIRECT_ADC) {
			st->sensors[chan] = ltc2983_adc_new(child, st, &sensor);
		} else if (sensor.type == LTC2983_SENSOR_ACTIVE_TEMP) {
			st->sensors[chan] = ltc2983_temp_new(child, st, &sensor);
		} else if (sensor.type == LTC2983_SENSOR_COPPER_TRACE) {
			st->sensors[chan] = ltc2983_copper_trace_new(child, st, &sensor);
		} else if (sensor.type == LTC2983_SENSOR_LEAK_DETECTOR) {
			st->sensors[chan] = ltc2983_leak_detector_new(child, st, &sensor);
		} else {
			return dev_err_probe(dev, -EINVAL,
					     "Unknown sensor type %d\n",
					     sensor.type);
		}

		if (IS_ERR(st->sensors[chan]))
			return dev_err_probe(dev, PTR_ERR(st->sensors[chan]),
					     "Failed to create sensor\n");

		/* set generic sensor parameters */
		st->sensors[chan]->chan = sensor.chan;
		st->sensors[chan]->type = sensor.type;

		/*
		 * Dedicated functions set n_iio_chan themselves; for all other
		 * sensor types rsense produces 0 channels, everything else 1.
		 */
		if (!st->sensors[chan]->n_iio_chan) {
			if (sensor.type != LTC2983_SENSOR_SENSE_RESISTOR)
				st->sensors[chan]->n_iio_chan = 1;
		}
		st->iio_channels += st->sensors[chan]->n_iio_chan;

		channel_avail_mask |= BIT(sensor.chan);
		chan++;
	}

	return 0;
}

static int ltc2983_eeprom_cmd(struct ltc2983_data *st, unsigned int cmd,
			      unsigned int wait_time, unsigned int status_reg,
			      unsigned long status_fail_mask)
{
	struct device *dev = &st->spi->dev;
	unsigned long time;
	unsigned int val;
	int ret;

	ret = regmap_bulk_write(st->regmap, LTC2983_EEPROM_KEY_REG,
				&st->eeprom_key, sizeof(st->eeprom_key));
	if (ret)
		return ret;

	reinit_completion(&st->completion);

	ret = regmap_write(st->regmap, LTC2983_STATUS_REG,
			   LTC2983_STATUS_START(true) | cmd);
	if (ret)
		return ret;

	time = wait_for_completion_timeout(&st->completion,
					   msecs_to_jiffies(wait_time));
	if (!time)
		return dev_err_probe(dev, -ETIMEDOUT,
				     "EEPROM command timed out\n");

	ret = regmap_read(st->regmap, status_reg, &val);
	if (ret)
		return ret;

	if (val & status_fail_mask)
		return dev_err_probe(dev, -EINVAL,
				     "EEPROM command failed: 0x%02X\n", val);

	return 0;
}

static int ltc2983_setup(struct ltc2983_data *st, bool assign_iio)
{
	struct device *dev = &st->spi->dev;
	u32 iio_chan_t = 0, iio_chan_v = 0, iio_chan_r = 0, iio_chan_c = 0;
	u32 chan, iio_idx = 0, status;
	int ret;

	/* make sure the device is up: start bit (7) is 0 and done bit (6) is 1 */
	ret = regmap_read_poll_timeout(st->regmap, LTC2983_STATUS_REG, status,
				       LTC2983_STATUS_UP(status) == 1, 25000,
				       25000 * 10);
	if (ret)
		return dev_err_probe(dev, ret, "Device startup timed out\n");

	ret = regmap_update_bits(st->regmap, LTC2983_GLOBAL_CONFIG_REG,
				 LTC2983_NOTCH_FREQ_MASK,
				 LTC2983_NOTCH_FREQ(st->filter_notch_freq));
	if (ret)
		return ret;

	ret = regmap_write(st->regmap, LTC2983_MUX_CONFIG_REG,
			   st->mux_delay_config);
	if (ret)
		return ret;

	if (st->info->has_eeprom && !assign_iio) {
		ret = ltc2983_eeprom_cmd(st, LTC2983_EEPROM_READ_CMD,
					 LTC2983_EEPROM_READ_TIME_MS,
					 LTC2983_EEPROM_READ_STATUS_REG,
					 LTC2983_EEPROM_READ_FAILURE_MASK);
		if (!ret)
			return 0;
	}

	for (chan = 0; chan < st->num_channels; chan++) {
		u32 chan_type = 0, *iio_chan;

		ret = st->sensors[chan]->assign_chan(st, st->sensors[chan]);
		if (ret)
			return ret;
		/*
		 * The assign_iio flag is necessary for when the device is
		 * coming out of sleep. In that case, we just need to
		 * re-configure the device channels.
		 * We also don't assign iio channels for rsense.
		 */
		if (st->sensors[chan]->type == LTC2983_SENSOR_SENSE_RESISTOR ||
		    !assign_iio)
			continue;

		/* assign iio channel */
		switch (st->sensors[chan]->type) {
		case LTC2983_SENSOR_COPPER_TRACE:
			if (st->sensors[chan]->n_iio_chan == 1) {
				/* sub-ohm copper traces produce only a resistance result */
				st->iio_chan[iio_idx++] =
					LTC2983_CHAN(IIO_RESISTANCE, iio_chan_r++, chan);
			} else {
				st->iio_chan[iio_idx++] =
					LTC2983_CHAN(IIO_TEMP, iio_chan_t++, chan);
				st->iio_chan[iio_idx++] =
					LTC2983_CHAN(IIO_RESISTANCE, iio_chan_r++, chan);
			}
			continue;
		case LTC2983_SENSOR_LEAK_DETECTOR:
			st->iio_chan[iio_idx++] =
				LTC2983_CHAN(IIO_COVERAGE, iio_chan_c++, chan);
			st->iio_chan[iio_idx++] =
				LTC2983_CHAN(IIO_RESISTANCE, iio_chan_r++, chan);
			continue;
		case LTC2983_SENSOR_DIRECT_ADC:
			chan_type = IIO_VOLTAGE;
			iio_chan = &iio_chan_v;
			break;
		default:
			chan_type = IIO_TEMP;
			iio_chan = &iio_chan_t;
			break;
		}

		/*
		 * add chan as the iio .address so that, we can directly
		 * reference the sensor given the iio_chan_spec
		 */
		st->iio_chan[iio_idx++] = LTC2983_CHAN(chan_type, (*iio_chan)++,
						       chan);
	}

	return 0;
}

static const struct regmap_range ltc2983_reg_ranges[] = {
	regmap_reg_range(LTC2983_STATUS_REG, LTC2983_STATUS_REG),
	regmap_reg_range(LTC2983_TEMP_RES_START_REG, LTC2983_TEMP_RES_END_REG),
	regmap_reg_range(ADT7604_RES_RES_START_REG, ADT7604_RES_RES_END_REG),
	regmap_reg_range(LTC2983_EEPROM_KEY_REG, LTC2983_EEPROM_KEY_REG),
	regmap_reg_range(LTC2983_EEPROM_READ_STATUS_REG,
			 LTC2983_EEPROM_READ_STATUS_REG),
	regmap_reg_range(LTC2983_GLOBAL_CONFIG_REG, LTC2983_GLOBAL_CONFIG_REG),
	regmap_reg_range(LTC2983_MULT_CHANNEL_START_REG,
			 LTC2983_MULT_CHANNEL_END_REG),
	regmap_reg_range(LTC2986_EEPROM_STATUS_REG, LTC2986_EEPROM_STATUS_REG),
	regmap_reg_range(LTC2983_MUX_CONFIG_REG, LTC2983_MUX_CONFIG_REG),
	regmap_reg_range(LTC2983_CHAN_ASSIGN_START_REG,
			 LTC2983_CHAN_ASSIGN_END_REG),
	regmap_reg_range(LTC2983_CUST_SENS_TBL_START_REG,
			 LTC2983_CUST_SENS_TBL_END_REG),
};

static const struct regmap_access_table ltc2983_reg_table = {
	.yes_ranges = ltc2983_reg_ranges,
	.n_yes_ranges = ARRAY_SIZE(ltc2983_reg_ranges),
};

/*
 *  The reg_bits are actually 12 but the device needs the first *complete*
 *  byte for the command (R/W).
 */
static const struct regmap_config ltc2983_regmap_config = {
	.reg_bits = 24,
	.val_bits = 8,
	.wr_table = &ltc2983_reg_table,
	.rd_table = &ltc2983_reg_table,
	.read_flag_mask = GENMASK(1, 0),
	.write_flag_mask = BIT(1),
};

static const struct  iio_info ltc2983_iio_info = {
	.read_raw = ltc2983_read_raw,
	.debugfs_reg_access = ltc2983_reg_access,
};

static int ltc2983_probe(struct spi_device *spi)
{
	struct device *dev = &spi->dev;
	struct ltc2983_data *st;
	struct iio_dev *indio_dev;
	struct gpio_desc *gpio;
	int ret;

	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
	if (!indio_dev)
		return -ENOMEM;

	st = iio_priv(indio_dev);

	st->info = spi_get_device_match_data(spi);
	if (!st->info)
		return -ENODEV;

	st->regmap = devm_regmap_init_spi(spi, &ltc2983_regmap_config);
	if (IS_ERR(st->regmap))
		return dev_err_probe(dev, PTR_ERR(st->regmap),
				     "Failed to initialize regmap\n");

	mutex_init(&st->lock);
	init_completion(&st->completion);
	st->spi = spi;
	st->eeprom_key = cpu_to_be32(LTC2983_EEPROM_KEY);
	spi_set_drvdata(spi, st);

	ret = ltc2983_parse_fw(st);
	if (ret)
		return ret;

	ret = devm_regulator_get_enable(dev, "vdd");
	if (ret)
		return ret;

	gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
	if (IS_ERR(gpio))
		return PTR_ERR(gpio);

	if (gpio) {
		/* bring the device out of reset */
		usleep_range(1000, 1200);
		gpiod_set_value_cansleep(gpio, 0);
	}

	st->iio_chan = devm_kzalloc(dev,
				    st->iio_channels * sizeof(*st->iio_chan),
				    GFP_KERNEL);
	if (!st->iio_chan)
		return -ENOMEM;

	ret = ltc2983_setup(st, true);
	if (ret)
		return ret;

	ret = devm_request_irq(dev, spi->irq, ltc2983_irq_handler,
			       IRQF_TRIGGER_RISING, st->info->name, st);
	if (ret)
		return ret;

	if (st->info->has_eeprom) {
		ret = ltc2983_eeprom_cmd(st, LTC2983_EEPROM_WRITE_CMD,
					 LTC2983_EEPROM_WRITE_TIME_MS,
					 LTC2986_EEPROM_STATUS_REG,
					 LTC2983_EEPROM_STATUS_FAILURE_MASK);
		if (ret)
			return ret;
	}

	indio_dev->name = st->info->name;
	indio_dev->num_channels = st->iio_channels;
	indio_dev->channels = st->iio_chan;
	indio_dev->modes = INDIO_DIRECT_MODE;
	indio_dev->info = &ltc2983_iio_info;

	return devm_iio_device_register(dev, indio_dev);
}

static int ltc2983_resume(struct device *dev)
{
	struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
	int dummy;

	/* dummy read to bring the device out of sleep */
	regmap_read(st->regmap, LTC2983_STATUS_REG, &dummy);
	/* we need to re-assign the channels */
	return ltc2983_setup(st, false);
}

static int ltc2983_suspend(struct device *dev)
{
	struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));

	return regmap_write(st->regmap, LTC2983_STATUS_REG, LTC2983_SLEEP);
}

static DEFINE_SIMPLE_DEV_PM_OPS(ltc2983_pm_ops, ltc2983_suspend,
				ltc2983_resume);

static const struct ltc2983_chip_info adt7604_chip_info_data = {
	.name = "adt7604",
	.max_channels_nr = 20,
	.has_eeprom = true,
	.supported_sensors = ADT7604_SENSORS,
};

static const struct ltc2983_chip_info ltc2983_chip_info_data = {
	.name = "ltc2983",
	.max_channels_nr = 20,
	.supported_sensors = LTC2983_COMMON_SENSORS,
};

static const struct ltc2983_chip_info ltc2984_chip_info_data = {
	.name = "ltc2984",
	.max_channels_nr = 20,
	.has_eeprom = true,
	.supported_sensors = LTC2983_COMMON_SENSORS,
};

static const struct ltc2983_chip_info ltc2986_chip_info_data = {
	.name = "ltc2986",
	.max_channels_nr = 10,
	.has_eeprom = true,
	.supported_sensors = LTC2983_COMMON_SENSORS | BIT_ULL(LTC2983_SENSOR_ACTIVE_TEMP),
};

static const struct ltc2983_chip_info ltm2985_chip_info_data = {
	.name = "ltm2985",
	.max_channels_nr = 10,
	.has_eeprom = true,
	.supported_sensors = LTC2983_COMMON_SENSORS | BIT_ULL(LTC2983_SENSOR_ACTIVE_TEMP),
};

static const struct spi_device_id ltc2983_id_table[] = {
	{ .name = "adt7604", .driver_data = (kernel_ulong_t)&adt7604_chip_info_data },
	{ .name = "ltc2983", .driver_data = (kernel_ulong_t)&ltc2983_chip_info_data },
	{ .name = "ltc2984", .driver_data = (kernel_ulong_t)&ltc2984_chip_info_data },
	{ .name = "ltc2986", .driver_data = (kernel_ulong_t)&ltc2986_chip_info_data },
	{ .name = "ltm2985", .driver_data = (kernel_ulong_t)&ltm2985_chip_info_data },
	{ }
};
MODULE_DEVICE_TABLE(spi, ltc2983_id_table);

static const struct of_device_id ltc2983_of_match[] = {
	{ .compatible = "adi,adt7604", .data = &adt7604_chip_info_data },
	{ .compatible = "adi,ltc2983", .data = &ltc2983_chip_info_data },
	{ .compatible = "adi,ltc2984", .data = &ltc2984_chip_info_data },
	{ .compatible = "adi,ltc2986", .data = &ltc2986_chip_info_data },
	{ .compatible = "adi,ltm2985", .data = &ltm2985_chip_info_data },
	{ }
};
MODULE_DEVICE_TABLE(of, ltc2983_of_match);

static struct spi_driver ltc2983_driver = {
	.driver = {
		.name = "ltc2983",
		.of_match_table = ltc2983_of_match,
		.pm = pm_sleep_ptr(&ltc2983_pm_ops),
	},
	.probe = ltc2983_probe,
	.id_table = ltc2983_id_table,
};

module_spi_driver(ltc2983_driver);

MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
MODULE_DESCRIPTION("Analog Devices LTC2983 SPI Temperature sensors");
MODULE_LICENSE("GPL");