aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio/adc/pac1934.c
blob: e0c2742da5236f731732a3115751936fc6420b97 (plain)
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
// SPDX-License-Identifier: GPL-2.0+
/*
 * IIO driver for PAC1934 Multi-Channel DC Power/Energy Monitor
 *
 * Copyright (C) 2017-2024 Microchip Technology Inc. and its subsidiaries
 *
 * Author: Bogdan Bolocan <bogdan.bolocan@microchip.com>
 * Author: Victor Tudose
 * Author: Marius Cristea <marius.cristea@microchip.com>
 *
 * Datasheet for PAC1931, PAC1932, PAC1933 and PAC1934 can be found here:
 * https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/PAC1931-Family-Data-Sheet-DS20005850E.pdf
 */

#include <linux/acpi.h>
#include <linux/bitfield.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/i2c.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <asm/unaligned.h>

/*
 * maximum accumulation time should be (17 * 60 * 1000) around 17 minutes@1024 sps
 * till PAC1934 accumulation registers starts to saturate
 */
#define PAC1934_MAX_RFSH_LIMIT_MS		60000
/* 50msec is the timeout for validity of the cached registers */
#define PAC1934_MIN_POLLING_TIME_MS		50
/*
 * 1000usec is the minimum wait time for normal conversions when sample
 * rate doesn't change
 */
#define PAC1934_MIN_UPDATE_WAIT_TIME_US		1000

/* 32000mV */
#define PAC1934_VOLTAGE_MILLIVOLTS_MAX		32000
/* voltage bits resolution when set for unsigned values */
#define PAC1934_VOLTAGE_U_RES			16
/* voltage bits resolution when set for signed values */
#define PAC1934_VOLTAGE_S_RES			15

/*
 * max signed value that can be stored on 32 bits and 8 digits fractional value
 * (2^31 - 1) * 10^8 + 99999999
 */
#define PAC_193X_MAX_POWER_ACC			214748364799999999LL
/*
 * min signed value that can be stored on 32 bits and 8 digits fractional value
 * -(2^31) * 10^8 - 99999999
 */
#define PAC_193X_MIN_POWER_ACC			-214748364899999999LL

#define PAC1934_MAX_NUM_CHANNELS		4

#define PAC1934_MEAS_REG_LEN			76
#define PAC1934_CTRL_REG_LEN			12

#define PAC1934_DEFAULT_CHIP_SAMP_SPEED_HZ	1024

/* I2C address map */
#define PAC1934_REFRESH_REG_ADDR		0x00
#define PAC1934_CTRL_REG_ADDR			0x01
#define PAC1934_ACC_COUNT_REG_ADDR		0x02
#define PAC1934_VPOWER_ACC_1_ADDR		0x03
#define PAC1934_VPOWER_ACC_2_ADDR		0x04
#define PAC1934_VPOWER_ACC_3_ADDR		0x05
#define PAC1934_VPOWER_ACC_4_ADDR		0x06
#define PAC1934_VBUS_1_ADDR			0x07
#define PAC1934_VBUS_2_ADDR			0x08
#define PAC1934_VBUS_3_ADDR			0x09
#define PAC1934_VBUS_4_ADDR			0x0A
#define PAC1934_VSENSE_1_ADDR			0x0B
#define PAC1934_VSENSE_2_ADDR			0x0C
#define PAC1934_VSENSE_3_ADDR			0x0D
#define PAC1934_VSENSE_4_ADDR			0x0E
#define PAC1934_VBUS_AVG_1_ADDR			0x0F
#define PAC1934_VBUS_AVG_2_ADDR			0x10
#define PAC1934_VBUS_AVG_3_ADDR			0x11
#define PAC1934_VBUS_AVG_4_ADDR			0x12
#define PAC1934_VSENSE_AVG_1_ADDR		0x13
#define PAC1934_VSENSE_AVG_2_ADDR		0x14
#define PAC1934_VSENSE_AVG_3_ADDR		0x15
#define PAC1934_VSENSE_AVG_4_ADDR		0x16
#define PAC1934_VPOWER_1_ADDR			0x17
#define PAC1934_VPOWER_2_ADDR			0x18
#define PAC1934_VPOWER_3_ADDR			0x19
#define PAC1934_VPOWER_4_ADDR			0x1A
#define PAC1934_REFRESH_V_REG_ADDR		0x1F
#define PAC1934_CTRL_STAT_REGS_ADDR		0x1C
#define PAC1934_PID_REG_ADDR			0xFD
#define PAC1934_MID_REG_ADDR			0xFE
#define PAC1934_RID_REG_ADDR			0xFF

/* PRODUCT ID REGISTER + MANUFACTURER ID REGISTER + REVISION ID REGISTER */
#define PAC1934_ID_REG_LEN			3
#define PAC1934_PID_IDX				0
#define PAC1934_MID_IDX				1
#define PAC1934_RID_IDX				2

#define PAC1934_ACPI_GET_NAMES_AND_MOHMS_VALS	1
#define PAC1934_ACPI_GET_UOHMS_VALS		2
#define PAC1934_ACPI_GET_BIPOLAR_SETTINGS	4
#define PAC1934_ACPI_GET_SAMP			5

#define PAC1934_SAMPLE_RATE_SHIFT		6

#define PAC1934_VBUS_SENSE_REG_LEN		2
#define PAC1934_ACC_REG_LEN			3
#define PAC1934_VPOWER_REG_LEN			4
#define PAC1934_VPOWER_ACC_REG_LEN		6
#define PAC1934_MAX_REGISTER_LENGTH		6

#define PAC1934_CUSTOM_ATTR_FOR_CHANNEL		1

/*
 * relative offsets when using multi-byte reads/writes even though these
 * bytes are read one after the other, they are not at adjacent memory
 * locations within the I2C memory map. The chip can skip some addresses
 */
#define PAC1934_CHANNEL_DIS_REG_OFF		0
#define PAC1934_NEG_PWR_REG_OFF			1

/*
 * when reading/writing multiple bytes from offset PAC1934_CHANNEL_DIS_REG_OFF,
 * the chip jumps over the 0x1E (REFRESH_G) and 0x1F (REFRESH_V) offsets
 */
#define PAC1934_SLOW_REG_OFF			2
#define PAC1934_CTRL_ACT_REG_OFF		3
#define PAC1934_CHANNEL_DIS_ACT_REG_OFF		4
#define PAC1934_NEG_PWR_ACT_REG_OFF		5
#define PAC1934_CTRL_LAT_REG_OFF		6
#define PAC1934_CHANNEL_DIS_LAT_REG_OFF		7
#define PAC1934_NEG_PWR_LAT_REG_OFF		8
#define PAC1934_PID_REG_OFF			9
#define PAC1934_MID_REG_OFF			10
#define PAC1934_REV_REG_OFF			11
#define PAC1934_CTRL_STATUS_INFO_LEN		12

#define PAC1934_MID				0x5D
#define PAC1931_PID				0x58
#define PAC1932_PID				0x59
#define PAC1933_PID				0x5A
#define PAC1934_PID				0x5B

/* Scale constant = (10^3 * 3.2 * 10^9 / 2^28) for mili Watt-second */
#define PAC1934_SCALE_CONSTANT			11921

#define PAC1934_MAX_VPOWER_RSHIFTED_BY_28B	11921
#define PAC1934_MAX_VSENSE_RSHIFTED_BY_16B	1525

#define PAC1934_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)

#define PAC1934_CRTL_SAMPLE_RATE_MASK	GENMASK(7, 6)
#define PAC1934_CHAN_SLEEP_MASK		BIT(5)
#define PAC1934_CHAN_SLEEP_SET		BIT(5)
#define PAC1934_CHAN_SINGLE_MASK	BIT(4)
#define PAC1934_CHAN_SINGLE_SHOT_SET	BIT(4)
#define PAC1934_CHAN_ALERT_MASK		BIT(3)
#define PAC1934_CHAN_ALERT_EN		BIT(3)
#define PAC1934_CHAN_ALERT_CC_MASK	BIT(2)
#define PAC1934_CHAN_ALERT_CC_EN	BIT(2)
#define PAC1934_CHAN_OVF_ALERT_MASK	BIT(1)
#define PAC1934_CHAN_OVF_ALERT_EN	BIT(1)
#define PAC1934_CHAN_OVF_MASK		BIT(0)

#define PAC1934_CHAN_DIS_CH1_OFF_MASK	BIT(7)
#define PAC1934_CHAN_DIS_CH2_OFF_MASK	BIT(6)
#define PAC1934_CHAN_DIS_CH3_OFF_MASK	BIT(5)
#define PAC1934_CHAN_DIS_CH4_OFF_MASK	BIT(4)
#define PAC1934_SMBUS_TIMEOUT_MASK	BIT(3)
#define PAC1934_SMBUS_BYTECOUNT_MASK	BIT(2)
#define PAC1934_SMBUS_NO_SKIP_MASK	BIT(1)

#define PAC1934_NEG_PWR_CH1_BIDI_MASK	BIT(7)
#define PAC1934_NEG_PWR_CH2_BIDI_MASK	BIT(6)
#define PAC1934_NEG_PWR_CH3_BIDI_MASK	BIT(5)
#define PAC1934_NEG_PWR_CH4_BIDI_MASK	BIT(4)
#define PAC1934_NEG_PWR_CH1_BIDV_MASK	BIT(3)
#define PAC1934_NEG_PWR_CH2_BIDV_MASK	BIT(2)
#define PAC1934_NEG_PWR_CH3_BIDV_MASK	BIT(1)
#define PAC1934_NEG_PWR_CH4_BIDV_MASK	BIT(0)

/*
 * Universal Unique Identifier (UUID),
 * 033771E0-1705-47B4-9535-D1BBE14D9A09,
 * is reserved to Microchip for the PAC1934.
 */
#define PAC1934_DSM_UUID		"033771E0-1705-47B4-9535-D1BBE14D9A09"

enum pac1934_ids {
	PAC1931,
	PAC1932,
	PAC1933,
	PAC1934
};

enum pac1934_samps {
	PAC1934_SAMP_1024SPS,
	PAC1934_SAMP_256SPS,
	PAC1934_SAMP_64SPS,
	PAC1934_SAMP_8SPS
};

/*
 * these indexes are exactly describing the element order within a single
 * PAC1934 phys channel IIO channel descriptor; see the static const struct
 * iio_chan_spec pac1934_single_channel[] declaration
 */
enum pac1934_ch_idx {
	PAC1934_CH_ENERGY,
	PAC1934_CH_POWER,
	PAC1934_CH_VOLTAGE,
	PAC1934_CH_CURRENT,
	PAC1934_CH_VOLTAGE_AVERAGE,
	PAC1934_CH_CURRENT_AVERAGE
};

/**
 * struct pac1934_features - features of a pac1934 instance
 * @phys_channels:	number of physical channels supported by the chip
 * @name:		chip's name
 */
struct pac1934_features {
	u8		phys_channels;
	const char	*name;
};

struct samp_rate_mapping {
	u16 samp_rate;
	u8 shift2value;
};

static const unsigned int samp_rate_map_tbl[] = {
	[PAC1934_SAMP_1024SPS] = 1024,
	[PAC1934_SAMP_256SPS] = 256,
	[PAC1934_SAMP_64SPS] = 64,
	[PAC1934_SAMP_8SPS] = 8,
};

static const struct pac1934_features pac1934_chip_config[] = {
	[PAC1931] = {
	    .phys_channels = 1,
	    .name = "pac1931",
	},
	[PAC1932] = {
	    .phys_channels = 2,
	    .name = "pac1932",
	},
	[PAC1933] = {
	    .phys_channels = 3,
	    .name = "pac1933",
	},
	[PAC1934] = {
	    .phys_channels = 4,
	    .name = "pac1934",
	},
};

/**
 * struct reg_data - data from the registers
 * @meas_regs:			snapshot of raw measurements registers
 * @ctrl_regs:			snapshot of control registers
 * @energy_sec_acc:		snapshot of energy values
 * @vpower_acc:			accumulated vpower values
 * @vpower:			snapshot of vpower registers
 * @vbus:			snapshot of vbus registers
 * @vbus_avg:			averages of vbus registers
 * @vsense:			snapshot of vsense registers
 * @vsense_avg:			averages of vsense registers
 * @num_enabled_channels:	count of how many chip channels are currently enabled
 */
struct reg_data {
	u8	meas_regs[PAC1934_MEAS_REG_LEN];
	u8	ctrl_regs[PAC1934_CTRL_REG_LEN];
	s64	energy_sec_acc[PAC1934_MAX_NUM_CHANNELS];
	s64	vpower_acc[PAC1934_MAX_NUM_CHANNELS];
	s32	vpower[PAC1934_MAX_NUM_CHANNELS];
	s32	vbus[PAC1934_MAX_NUM_CHANNELS];
	s32	vbus_avg[PAC1934_MAX_NUM_CHANNELS];
	s32	vsense[PAC1934_MAX_NUM_CHANNELS];
	s32	vsense_avg[PAC1934_MAX_NUM_CHANNELS];
	u8	num_enabled_channels;
};

/**
 * struct pac1934_chip_info - information about the chip
 * @client:			the i2c-client attached to the device
 * @lock:			synchronize access to driver's state members
 * @work_chip_rfsh:		work queue used for refresh commands
 * @phys_channels:		phys channels count
 * @active_channels:		array of values, true means that channel is active
 * @enable_energy:		array of values, true means that channel energy is measured
 * @bi_dir:			array of bools, true means that channel is bidirectional
 * @chip_variant:		chip variant
 * @chip_revision:		chip revision
 * @shunts:			shunts
 * @chip_reg_data:		chip reg data
 * @sample_rate_value:		sampling frequency
 * @labels:			table with channels labels
 * @iio_info:			iio_info
 * @tstamp:			chip's uptime
 */
struct pac1934_chip_info {
	struct i2c_client	*client;
	struct mutex		lock; /* synchronize access to driver's state members */
	struct delayed_work	work_chip_rfsh;
	u8			phys_channels;
	bool			active_channels[PAC1934_MAX_NUM_CHANNELS];
	bool			enable_energy[PAC1934_MAX_NUM_CHANNELS];
	bool			bi_dir[PAC1934_MAX_NUM_CHANNELS];
	u8			chip_variant;
	u8			chip_revision;
	u32			shunts[PAC1934_MAX_NUM_CHANNELS];
	struct reg_data		chip_reg_data;
	s32			sample_rate_value;
	char			*labels[PAC1934_MAX_NUM_CHANNELS];
	struct iio_info		iio_info;
	unsigned long		tstamp;
};

#define TO_PAC1934_CHIP_INFO(d) container_of(d, struct pac1934_chip_info, work_chip_rfsh)

#define PAC1934_VPOWER_ACC_CHANNEL(_index, _si, _address) {			\
	.type = IIO_ENERGY,							\
	.address = (_address),							\
	.indexed = 1,								\
	.channel = (_index),							\
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW)	|			\
			      BIT(IIO_CHAN_INFO_SCALE)	|			\
			      BIT(IIO_CHAN_INFO_ENABLE),			\
	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),		\
	.info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
	.scan_index = (_si),							\
	.scan_type = {								\
		.sign = 'u',							\
		.realbits = 48,							\
		.storagebits = 64,						\
		.endianness = IIO_CPU,						\
	}									\
}

#define PAC1934_VBUS_CHANNEL(_index, _si, _address) {				\
	.type = IIO_VOLTAGE,							\
	.address = (_address),							\
	.indexed = 1,								\
	.channel = (_index),							\
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW)	|			\
			      BIT(IIO_CHAN_INFO_SCALE),				\
	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),		\
	.info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
	.scan_index = (_si),							\
	.scan_type = {								\
		.sign = 'u',							\
		.realbits = 16,							\
		.storagebits = 16,						\
		.endianness = IIO_CPU,						\
	}									\
}

#define PAC1934_VBUS_AVG_CHANNEL(_index, _si, _address) {			\
	.type = IIO_VOLTAGE,							\
	.address = (_address),							\
	.indexed = 1,								\
	.channel = (_index),							\
	.info_mask_separate = BIT(IIO_CHAN_INFO_AVERAGE_RAW)	|		\
			      BIT(IIO_CHAN_INFO_SCALE),				\
	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),		\
	.info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
	.scan_index = (_si),							\
	.scan_type = {								\
		.sign = 'u',							\
		.realbits = 16,							\
		.storagebits = 16,						\
		.endianness = IIO_CPU,						\
	}									\
}

#define PAC1934_VSENSE_CHANNEL(_index, _si, _address) {				\
	.type = IIO_CURRENT,							\
	.address = (_address),							\
	.indexed = 1,								\
	.channel = (_index),							\
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW)	|			\
			      BIT(IIO_CHAN_INFO_SCALE),				\
	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),		\
	.info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
	.scan_index = (_si),							\
	.scan_type = {								\
		.sign = 'u',							\
		.realbits = 16,							\
		.storagebits = 16,						\
		.endianness = IIO_CPU,						\
	}									\
}

#define PAC1934_VSENSE_AVG_CHANNEL(_index, _si, _address) {			\
	.type = IIO_CURRENT,							\
	.address = (_address),							\
	.indexed = 1,								\
	.channel = (_index),							\
	.info_mask_separate = BIT(IIO_CHAN_INFO_AVERAGE_RAW)	|		\
			      BIT(IIO_CHAN_INFO_SCALE),				\
	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),		\
	.info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
	.scan_index = (_si),							\
	.scan_type = {								\
		.sign = 'u',							\
		.realbits = 16,							\
		.storagebits = 16,						\
		.endianness = IIO_CPU,						\
	}									\
}

#define PAC1934_VPOWER_CHANNEL(_index, _si, _address) {				\
	.type = IIO_POWER,							\
	.address = (_address),							\
	.indexed = 1,								\
	.channel = (_index),							\
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW)	|			\
			      BIT(IIO_CHAN_INFO_SCALE),				\
	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),		\
	.info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
	.scan_index = (_si),							\
	.scan_type = {								\
		.sign = 'u',							\
		.realbits = 28,							\
		.storagebits = 32,						\
		.shift = 4,							\
		.endianness = IIO_CPU,						\
	}									\
}

static const struct iio_chan_spec pac1934_single_channel[] = {
	PAC1934_VPOWER_ACC_CHANNEL(0, 0, PAC1934_VPOWER_ACC_1_ADDR),
	PAC1934_VPOWER_CHANNEL(0, 0, PAC1934_VPOWER_1_ADDR),
	PAC1934_VBUS_CHANNEL(0, 0, PAC1934_VBUS_1_ADDR),
	PAC1934_VSENSE_CHANNEL(0, 0, PAC1934_VSENSE_1_ADDR),
	PAC1934_VBUS_AVG_CHANNEL(0, 0, PAC1934_VBUS_AVG_1_ADDR),
	PAC1934_VSENSE_AVG_CHANNEL(0, 0, PAC1934_VSENSE_AVG_1_ADDR),
};

/* Low-level I2c functions used to transfer up to 76 bytes at once */
static int pac1934_i2c_read(struct i2c_client *client, u8 reg_addr,
			    void *databuf, u8 len)
{
	int ret;
	struct i2c_msg msgs[2] = {
		{
			.addr = client->addr,
			.len = 1,
			.buf = (u8 *)&reg_addr,
		},
		{
			.addr = client->addr,
			.len = len,
			.buf = databuf,
			.flags = I2C_M_RD
		}
	};

	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
	if (ret < 0)
		return ret;

	return 0;
}

static int pac1934_get_samp_rate_idx(struct pac1934_chip_info *info,
				     u32 new_samp_rate)
{
	int cnt;

	for (cnt = 0; cnt < ARRAY_SIZE(samp_rate_map_tbl); cnt++)
		if (new_samp_rate == samp_rate_map_tbl[cnt])
			return cnt;

	/* not a valid sample rate value */
	return -EINVAL;
}

static ssize_t pac1934_shunt_value_show(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
	struct pac1934_chip_info *info = iio_priv(indio_dev);
	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);

	return sysfs_emit(buf, "%u\n", info->shunts[this_attr->address]);
}

static ssize_t pac1934_shunt_value_store(struct device *dev,
					 struct device_attribute *attr,
					 const char *buf, size_t count)
{
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
	struct pac1934_chip_info *info = iio_priv(indio_dev);
	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
	int sh_val;

	if (kstrtouint(buf, 10, &sh_val)) {
		dev_err(dev, "Shunt value is not valid\n");
		return -EINVAL;
	}

	scoped_guard(mutex, &info->lock)
		info->shunts[this_attr->address] = sh_val;

	return count;
}

static int pac1934_read_avail(struct iio_dev *indio_dev,
			      struct iio_chan_spec const *channel,
			      const int **vals, int *type, int *length, long mask)
{
	switch (mask) {
	case IIO_CHAN_INFO_SAMP_FREQ:
		*type = IIO_VAL_INT;
		*vals = samp_rate_map_tbl;
		*length = ARRAY_SIZE(samp_rate_map_tbl);
		return IIO_AVAIL_LIST;
	}

	return -EINVAL;
}

static int pac1934_send_refresh(struct pac1934_chip_info *info,
				u8 refresh_cmd, u32 wait_time)
{
	/* this function only sends REFRESH or REFRESH_V */
	struct i2c_client *client = info->client;
	int ret;
	u8 bidir_reg;
	bool revision_bug = false;

	if (info->chip_revision == 2 || info->chip_revision == 3) {
		/*
		 * chip rev 2 and 3 bug workaround
		 * see: PAC1934 Family Data Sheet Errata DS80000836A.pdf
		 */
		revision_bug = true;

		bidir_reg =
			FIELD_PREP(PAC1934_NEG_PWR_CH1_BIDI_MASK, info->bi_dir[0]) |
			FIELD_PREP(PAC1934_NEG_PWR_CH2_BIDI_MASK, info->bi_dir[1]) |
			FIELD_PREP(PAC1934_NEG_PWR_CH3_BIDI_MASK, info->bi_dir[2]) |
			FIELD_PREP(PAC1934_NEG_PWR_CH4_BIDI_MASK, info->bi_dir[3]) |
			FIELD_PREP(PAC1934_NEG_PWR_CH1_BIDV_MASK, info->bi_dir[0]) |
			FIELD_PREP(PAC1934_NEG_PWR_CH2_BIDV_MASK, info->bi_dir[1]) |
			FIELD_PREP(PAC1934_NEG_PWR_CH3_BIDV_MASK, info->bi_dir[2]) |
			FIELD_PREP(PAC1934_NEG_PWR_CH4_BIDV_MASK, info->bi_dir[3]);

		ret = i2c_smbus_write_byte_data(client,
						PAC1934_CTRL_STAT_REGS_ADDR +
						PAC1934_NEG_PWR_REG_OFF,
						bidir_reg);
		if (ret)
			return ret;
	}

	ret = i2c_smbus_write_byte(client, refresh_cmd);
	if (ret) {
		dev_err(&client->dev, "%s - cannot send 0x%02X\n",
			__func__, refresh_cmd);
		return ret;
	}

	if (revision_bug) {
		/*
		 * chip rev 2 and 3 bug workaround - write again the same
		 * register write the updated registers back
		 */
		ret = i2c_smbus_write_byte_data(client,
						PAC1934_CTRL_STAT_REGS_ADDR +
						PAC1934_NEG_PWR_REG_OFF, bidir_reg);
		if (ret)
			return ret;
	}

	/* register data retrieval timestamp */
	info->tstamp = jiffies;

	/* wait till the data is available */
	usleep_range(wait_time, wait_time + 100);

	return ret;
}

static int pac1934_reg_snapshot(struct pac1934_chip_info *info,
				bool do_refresh, u8 refresh_cmd, u32 wait_time)
{
	int ret;
	struct i2c_client *client = info->client;
	u8 samp_shift, ctrl_regs_tmp;
	u8 *offset_reg_data_p;
	u16 tmp_value;
	u32 samp_rate, cnt, tmp;
	s64 curr_energy, inc;
	u64 tmp_energy;
	struct reg_data *reg_data;

	guard(mutex)(&info->lock);

	if (do_refresh) {
		ret = pac1934_send_refresh(info, refresh_cmd, wait_time);
		if (ret < 0) {
			dev_err(&client->dev,
				"%s - cannot send refresh\n",
				__func__);
			return ret;
		}
	}

	ret = i2c_smbus_read_i2c_block_data(client, PAC1934_CTRL_STAT_REGS_ADDR,
					    PAC1934_CTRL_REG_LEN,
					    (u8 *)info->chip_reg_data.ctrl_regs);
	if (ret < 0) {
		dev_err(&client->dev,
			"%s - cannot read ctrl/status registers\n",
			__func__);
		return ret;
	}

	reg_data = &info->chip_reg_data;

	/* read the data registers */
	ret = pac1934_i2c_read(client, PAC1934_ACC_COUNT_REG_ADDR,
			       (u8 *)reg_data->meas_regs, PAC1934_MEAS_REG_LEN);
	if (ret) {
		dev_err(&client->dev,
			"%s - cannot read ACC_COUNT register: %d:%d\n",
			__func__, ret, PAC1934_MEAS_REG_LEN);
		return ret;
	}

	/* see how much shift is required by the sample rate */
	samp_rate = samp_rate_map_tbl[((reg_data->ctrl_regs[PAC1934_CTRL_LAT_REG_OFF]) >> 6)];
	samp_shift = get_count_order(samp_rate);

	ctrl_regs_tmp = reg_data->ctrl_regs[PAC1934_CHANNEL_DIS_LAT_REG_OFF];
	offset_reg_data_p = &reg_data->meas_regs[PAC1934_ACC_REG_LEN];

	/* start with VPOWER_ACC */
	for (cnt = 0; cnt < info->phys_channels; cnt++) {
		/* check if the channel is active, skip all fields if disabled */
		if ((ctrl_regs_tmp << cnt) & 0x80)
			continue;

		/* skip if the energy accumulation is disabled */
		if (info->enable_energy[cnt]) {
			curr_energy = info->chip_reg_data.energy_sec_acc[cnt];

			tmp_energy = get_unaligned_be48(offset_reg_data_p);

			if (info->bi_dir[cnt])
				reg_data->vpower_acc[cnt] = sign_extend64(tmp_energy, 47);
			else
				reg_data->vpower_acc[cnt] = tmp_energy;

			/*
			 * compute the scaled to 1 second accumulated energy value;
			 * energy accumulator scaled to 1sec = VPOWER_ACC/2^samp_shift
			 * the chip's sampling rate is 2^samp_shift samples/sec
			 */
			inc = (reg_data->vpower_acc[cnt] >> samp_shift);

			/* add the power_acc field */
			curr_energy += inc;

			clamp(curr_energy, PAC_193X_MIN_POWER_ACC, PAC_193X_MAX_POWER_ACC);

			reg_data->energy_sec_acc[cnt] = curr_energy;
		}

		offset_reg_data_p += PAC1934_VPOWER_ACC_REG_LEN;
	}

	/* continue with VBUS */
	for (cnt = 0; cnt < info->phys_channels; cnt++) {
		if ((ctrl_regs_tmp << cnt) & 0x80)
			continue;

		tmp_value = get_unaligned_be16(offset_reg_data_p);

		if (info->bi_dir[cnt])
			reg_data->vbus[cnt] = sign_extend32((u32)(tmp_value), 15);
		else
			reg_data->vbus[cnt] = tmp_value;

		offset_reg_data_p += PAC1934_VBUS_SENSE_REG_LEN;
	}

	/* VSENSE */
	for (cnt = 0; cnt < info->phys_channels; cnt++) {
		if ((ctrl_regs_tmp << cnt) & 0x80)
			continue;

		tmp_value = get_unaligned_be16(offset_reg_data_p);

		if (info->bi_dir[cnt])
			reg_data->vsense[cnt] = sign_extend32((u32)(tmp_value), 15);
		else
			reg_data->vsense[cnt] = tmp_value;

		offset_reg_data_p += PAC1934_VBUS_SENSE_REG_LEN;
	}

	/* VBUS_AVG */
	for (cnt = 0; cnt < info->phys_channels; cnt++) {
		if ((ctrl_regs_tmp << cnt) & 0x80)
			continue;

		tmp_value = get_unaligned_be16(offset_reg_data_p);

		if (info->bi_dir[cnt])
			reg_data->vbus_avg[cnt] = sign_extend32((u32)(tmp_value), 15);
		else
			reg_data->vbus_avg[cnt] = tmp_value;

		offset_reg_data_p += PAC1934_VBUS_SENSE_REG_LEN;
	}

	/* VSENSE_AVG */
	for (cnt = 0; cnt < info->phys_channels; cnt++) {
		if ((ctrl_regs_tmp << cnt) & 0x80)
			continue;

		tmp_value = get_unaligned_be16(offset_reg_data_p);

		if (info->bi_dir[cnt])
			reg_data->vsense_avg[cnt] = sign_extend32((u32)(tmp_value), 15);
		else
			reg_data->vsense_avg[cnt] = tmp_value;

		offset_reg_data_p += PAC1934_VBUS_SENSE_REG_LEN;
	}

	/* VPOWER */
	for (cnt = 0; cnt < info->phys_channels; cnt++) {
		if ((ctrl_regs_tmp << cnt) & 0x80)
			continue;

		tmp = get_unaligned_be32(offset_reg_data_p) >> 4;

		if (info->bi_dir[cnt])
			reg_data->vpower[cnt] = sign_extend32(tmp, 27);
		else
			reg_data->vpower[cnt] = tmp;

		offset_reg_data_p += PAC1934_VPOWER_REG_LEN;
	}

	return 0;
}

static int pac1934_retrieve_data(struct pac1934_chip_info *info,
				 u32 wait_time)
{
	int ret = 0;

	/*
	 * check if the minimal elapsed time has passed and if so,
	 * re-read the chip, otherwise the cached info is just fine
	 */
	if (time_after(jiffies, info->tstamp + msecs_to_jiffies(PAC1934_MIN_POLLING_TIME_MS))) {
		ret = pac1934_reg_snapshot(info, true, PAC1934_REFRESH_REG_ADDR,
					   wait_time);

		/*
		 * Re-schedule the work for the read registers on timeout
		 * (to prevent chip registers saturation)
		 */
		mod_delayed_work(system_wq, &info->work_chip_rfsh,
				 msecs_to_jiffies(PAC1934_MAX_RFSH_LIMIT_MS));
	}

	return ret;
}

static int pac1934_read_raw(struct iio_dev *indio_dev,
			    struct iio_chan_spec const *chan, int *val,
			    int *val2, long mask)
{
	struct pac1934_chip_info *info = iio_priv(indio_dev);
	s64 curr_energy;
	int ret, channel = chan->channel - 1;

	ret = pac1934_retrieve_data(info, PAC1934_MIN_UPDATE_WAIT_TIME_US);
	if (ret < 0)
		return ret;

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		switch (chan->type) {
		case IIO_VOLTAGE:
			*val = info->chip_reg_data.vbus[channel];
			return IIO_VAL_INT;
		case IIO_CURRENT:
			*val = info->chip_reg_data.vsense[channel];
			return IIO_VAL_INT;
		case IIO_POWER:
			*val = info->chip_reg_data.vpower[channel];
			return IIO_VAL_INT;
		case IIO_ENERGY:
			curr_energy = info->chip_reg_data.energy_sec_acc[channel];
			*val = (u32)curr_energy;
			*val2 = (u32)(curr_energy >> 32);
			return IIO_VAL_INT_64;
		default:
			return -EINVAL;
		}
	case IIO_CHAN_INFO_AVERAGE_RAW:
		switch (chan->type) {
		case IIO_VOLTAGE:
			*val = info->chip_reg_data.vbus_avg[channel];
			return IIO_VAL_INT;
		case IIO_CURRENT:
			*val = info->chip_reg_data.vsense_avg[channel];
			return IIO_VAL_INT;
		default:
			return -EINVAL;
		}
	case IIO_CHAN_INFO_SCALE:
		switch (chan->address) {
		/* Voltages - scale for millivolts */
		case PAC1934_VBUS_1_ADDR:
		case PAC1934_VBUS_2_ADDR:
		case PAC1934_VBUS_3_ADDR:
		case PAC1934_VBUS_4_ADDR:
		case PAC1934_VBUS_AVG_1_ADDR:
		case PAC1934_VBUS_AVG_2_ADDR:
		case PAC1934_VBUS_AVG_3_ADDR:
		case PAC1934_VBUS_AVG_4_ADDR:
			*val = PAC1934_VOLTAGE_MILLIVOLTS_MAX;
			if (chan->scan_type.sign == 'u')
				*val2 = PAC1934_VOLTAGE_U_RES;
			else
				*val2 = PAC1934_VOLTAGE_S_RES;
			return IIO_VAL_FRACTIONAL_LOG2;
		/*
		 * Currents - scale for mA - depends on the
		 * channel's shunt value
		 * (100mV * 1000000) / (2^16 * shunt(uohm))
		 */
		case PAC1934_VSENSE_1_ADDR:
		case PAC1934_VSENSE_2_ADDR:
		case PAC1934_VSENSE_3_ADDR:
		case PAC1934_VSENSE_4_ADDR:
		case PAC1934_VSENSE_AVG_1_ADDR:
		case PAC1934_VSENSE_AVG_2_ADDR:
		case PAC1934_VSENSE_AVG_3_ADDR:
		case PAC1934_VSENSE_AVG_4_ADDR:
			*val = PAC1934_MAX_VSENSE_RSHIFTED_BY_16B;
			if (chan->scan_type.sign == 'u')
				*val2 = info->shunts[channel];
			else
				*val2 = info->shunts[channel] >> 1;
			return IIO_VAL_FRACTIONAL;
		/*
		 * Power - uW - it will use the combined scale
		 * for current and voltage
		 * current(mA) * voltage(mV) = power (uW)
		 */
		case PAC1934_VPOWER_1_ADDR:
		case PAC1934_VPOWER_2_ADDR:
		case PAC1934_VPOWER_3_ADDR:
		case PAC1934_VPOWER_4_ADDR:
			*val = PAC1934_MAX_VPOWER_RSHIFTED_BY_28B;
			if (chan->scan_type.sign == 'u')
				*val2 = info->shunts[channel];
			else
				*val2 = info->shunts[channel] >> 1;
			return IIO_VAL_FRACTIONAL;
		case PAC1934_VPOWER_ACC_1_ADDR:
		case PAC1934_VPOWER_ACC_2_ADDR:
		case PAC1934_VPOWER_ACC_3_ADDR:
		case PAC1934_VPOWER_ACC_4_ADDR:
			/*
			 * expresses the 32 bit scale value here compute
			 * the scale for energy (miliWatt-second or miliJoule)
			 */
			*val = PAC1934_SCALE_CONSTANT;

			if (chan->scan_type.sign == 'u')
				*val2 = info->shunts[channel];
			else
				*val2 = info->shunts[channel] >> 1;
			return IIO_VAL_FRACTIONAL;
		default:
			return -EINVAL;
		}
	case IIO_CHAN_INFO_SAMP_FREQ:
		*val = info->sample_rate_value;
		return IIO_VAL_INT;
	case IIO_CHAN_INFO_ENABLE:
		*val = info->enable_energy[channel];
		return IIO_VAL_INT;
	default:
		return -EINVAL;
	}
}

static int pac1934_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
			     int val, int val2, long mask)
{
	struct pac1934_chip_info *info = iio_priv(indio_dev);
	struct i2c_client *client = info->client;
	int ret = -EINVAL;
	s32 old_samp_rate;
	u8 ctrl_reg;

	switch (mask) {
	case IIO_CHAN_INFO_SAMP_FREQ:
		ret = pac1934_get_samp_rate_idx(info, val);
		if (ret < 0)
			return ret;

		/* write the new sampling value and trigger a snapshot(incl refresh) */
		scoped_guard(mutex, &info->lock) {
			ctrl_reg = FIELD_PREP(PAC1934_CRTL_SAMPLE_RATE_MASK, ret);
			ret = i2c_smbus_write_byte_data(client, PAC1934_CTRL_REG_ADDR, ctrl_reg);
			if (ret) {
				dev_err(&client->dev,
					"%s - can't update sample rate\n",
					__func__);
				return ret;
			}
		}

		old_samp_rate = info->sample_rate_value;
		info->sample_rate_value = val;

		/*
		 * now, force a snapshot with refresh - call retrieve
		 * data in order to update the refresh timer
		 * alter the timestamp in order to force trigger a
		 * register snapshot and a timestamp update
		 */
		info->tstamp -= msecs_to_jiffies(PAC1934_MIN_POLLING_TIME_MS);
		ret = pac1934_retrieve_data(info, (1024 / old_samp_rate) * 1000);
		if (ret < 0) {
			dev_err(&client->dev,
				"%s - cannot snapshot ctrl and measurement regs\n",
				__func__);
			return ret;
		}

		return 0;
	case IIO_CHAN_INFO_ENABLE:
		scoped_guard(mutex, &info->lock) {
			info->enable_energy[chan->channel - 1] = val ? true : false;
			if (!val)
				info->chip_reg_data.energy_sec_acc[chan->channel - 1] = 0;
		}

		return 0;
	default:
		return -EINVAL;
	}
}

static int pac1934_read_label(struct iio_dev *indio_dev,
			      struct iio_chan_spec const *chan, char *label)
{
	struct pac1934_chip_info *info = iio_priv(indio_dev);

	switch (chan->address) {
	case PAC1934_VBUS_1_ADDR:
	case PAC1934_VBUS_2_ADDR:
	case PAC1934_VBUS_3_ADDR:
	case PAC1934_VBUS_4_ADDR:
		return sysfs_emit(label, "%s_VBUS_%d\n",
				  info->labels[chan->scan_index],
				  chan->scan_index + 1);
	case PAC1934_VBUS_AVG_1_ADDR:
	case PAC1934_VBUS_AVG_2_ADDR:
	case PAC1934_VBUS_AVG_3_ADDR:
	case PAC1934_VBUS_AVG_4_ADDR:
		return sysfs_emit(label, "%s_VBUS_AVG_%d\n",
				  info->labels[chan->scan_index],
				  chan->scan_index + 1);
	case PAC1934_VSENSE_1_ADDR:
	case PAC1934_VSENSE_2_ADDR:
	case PAC1934_VSENSE_3_ADDR:
	case PAC1934_VSENSE_4_ADDR:
		return sysfs_emit(label, "%s_IBUS_%d\n",
				  info->labels[chan->scan_index],
				  chan->scan_index + 1);
	case PAC1934_VSENSE_AVG_1_ADDR:
	case PAC1934_VSENSE_AVG_2_ADDR:
	case PAC1934_VSENSE_AVG_3_ADDR:
	case PAC1934_VSENSE_AVG_4_ADDR:
		return sysfs_emit(label, "%s_IBUS_AVG_%d\n",
				  info->labels[chan->scan_index],
				  chan->scan_index + 1);
	case PAC1934_VPOWER_1_ADDR:
	case PAC1934_VPOWER_2_ADDR:
	case PAC1934_VPOWER_3_ADDR:
	case PAC1934_VPOWER_4_ADDR:
		return sysfs_emit(label, "%s_POWER_%d\n",
				  info->labels[chan->scan_index],
				  chan->scan_index + 1);
	case PAC1934_VPOWER_ACC_1_ADDR:
	case PAC1934_VPOWER_ACC_2_ADDR:
	case PAC1934_VPOWER_ACC_3_ADDR:
	case PAC1934_VPOWER_ACC_4_ADDR:
		return sysfs_emit(label, "%s_ENERGY_%d\n",
				  info->labels[chan->scan_index],
				  chan->scan_index + 1);
	}

	return 0;
}

static void pac1934_work_periodic_rfsh(struct work_struct *work)
{
	struct pac1934_chip_info *info = TO_PAC1934_CHIP_INFO((struct delayed_work *)work);
	struct device *dev = &info->client->dev;

	dev_dbg(dev, "%s - Periodic refresh\n", __func__);

	/* do a REFRESH, then read */
	pac1934_reg_snapshot(info, true, PAC1934_REFRESH_REG_ADDR,
			     PAC1934_MIN_UPDATE_WAIT_TIME_US);

	schedule_delayed_work(&info->work_chip_rfsh,
			      msecs_to_jiffies(PAC1934_MAX_RFSH_LIMIT_MS));
}

static int pac1934_read_revision(struct pac1934_chip_info *info, u8 *buf)
{
	int ret;
	struct i2c_client *client = info->client;

	ret = i2c_smbus_read_i2c_block_data(client, PAC1934_PID_REG_ADDR,
					    PAC1934_ID_REG_LEN,
					    buf);
	if (ret < 0) {
		dev_err(&client->dev, "cannot read revision\n");
		return ret;
	}

	return 0;
}

static int pac1934_chip_identify(struct pac1934_chip_info *info)
{
	u8 rev_info[PAC1934_ID_REG_LEN];
	struct device *dev = &info->client->dev;
	int ret = 0;

	ret = pac1934_read_revision(info, (u8 *)rev_info);
	if (ret)
		return ret;

	info->chip_variant = rev_info[PAC1934_PID_IDX];
	info->chip_revision = rev_info[PAC1934_RID_IDX];

	dev_dbg(dev, "Chip variant: 0x%02X\n", info->chip_variant);
	dev_dbg(dev, "Chip revision: 0x%02X\n", info->chip_revision);

	switch (info->chip_variant) {
	case PAC1934_PID:
		return PAC1934;
	case PAC1933_PID:
		return PAC1933;
	case PAC1932_PID:
		return PAC1932;
	case PAC1931_PID:
		return PAC1931;
	default:
		return -EINVAL;
	}
}

/*
 * documentation related to the ACPI device definition
 * https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ApplicationNotes/ApplicationNotes/PAC1934-Integration-Notes-for-Microsoft-Windows-10-and-Windows-11-Driver-Support-DS00002534.pdf
 */
static bool pac1934_acpi_parse_channel_config(struct i2c_client *client,
					      struct pac1934_chip_info *info)
{
	acpi_handle handle;
	union acpi_object *rez;
	struct device *dev = &client->dev;
	unsigned short bi_dir_mask;
	int idx, i;
	guid_t guid;

	handle = ACPI_HANDLE(dev);

	guid_parse(PAC1934_DSM_UUID, &guid);

	rez = acpi_evaluate_dsm(handle, &guid, 0, PAC1934_ACPI_GET_NAMES_AND_MOHMS_VALS, NULL);
	if (!rez)
		return false;

	for (i = 0; i < rez->package.count; i += 2) {
		idx = i / 2;
		info->labels[idx] =
			devm_kmemdup(dev, rez->package.elements[i].string.pointer,
				     (size_t)rez->package.elements[i].string.length + 1,
				     GFP_KERNEL);
		info->labels[idx][rez->package.elements[i].string.length] = '\0';
		info->shunts[idx] = rez->package.elements[i + 1].integer.value * 1000;
		info->active_channels[idx] = (info->shunts[idx] != 0);
	}

	ACPI_FREE(rez);

	rez = acpi_evaluate_dsm(handle, &guid, 1, PAC1934_ACPI_GET_UOHMS_VALS, NULL);
	if (!rez) {
		/*
		 * initializing with default values
		 * we assume all channels are unidirectional(the mask is zero)
		 * and assign the default sampling rate
		 */
		info->sample_rate_value = PAC1934_DEFAULT_CHIP_SAMP_SPEED_HZ;
		return true;
	}

	for (i = 0; i < rez->package.count; i++) {
		idx = i;
		info->shunts[idx] = rez->package.elements[i].integer.value;
		info->active_channels[idx] = (info->shunts[idx] != 0);
	}

	ACPI_FREE(rez);

	rez = acpi_evaluate_dsm(handle, &guid, 1, PAC1934_ACPI_GET_BIPOLAR_SETTINGS, NULL);
	if (!rez)
		return false;

	bi_dir_mask = rez->package.elements[0].integer.value;
	info->bi_dir[0] = ((bi_dir_mask & (1 << 3)) | (bi_dir_mask & (1 << 7))) != 0;
	info->bi_dir[1] = ((bi_dir_mask & (1 << 2)) | (bi_dir_mask & (1 << 6))) != 0;
	info->bi_dir[2] = ((bi_dir_mask & (1 << 1)) | (bi_dir_mask & (1 << 5))) != 0;
	info->bi_dir[3] = ((bi_dir_mask & (1 << 0)) | (bi_dir_mask & (1 << 4))) != 0;

	ACPI_FREE(rez);

	rez = acpi_evaluate_dsm(handle, &guid, 1, PAC1934_ACPI_GET_SAMP, NULL);
	if (!rez)
		return false;

	info->sample_rate_value = rez->package.elements[0].integer.value;

	ACPI_FREE(rez);

	return true;
}

static bool pac1934_of_parse_channel_config(struct i2c_client *client,
					    struct pac1934_chip_info *info)
{
	struct fwnode_handle *node, *fwnode;
	struct device *dev = &client->dev;
	unsigned int current_channel;
	int idx, ret;

	info->sample_rate_value = 1024;
	current_channel = 1;

	fwnode = dev_fwnode(dev);
	fwnode_for_each_available_child_node(fwnode, node) {
		ret = fwnode_property_read_u32(node, "reg", &idx);
		if (ret) {
			dev_err_probe(dev, ret,
				      "reading invalid channel index\n");
			goto err_fwnode;
		}
		/* adjust idx to match channel index (1 to 4) from the datasheet */
		idx--;

		if (current_channel >= (info->phys_channels + 1) ||
		    idx >= info->phys_channels || idx < 0) {
			dev_err_probe(dev, -EINVAL,
				      "%s: invalid channel_index %d value\n",
				      fwnode_get_name(node), idx);
			goto err_fwnode;
		}

		/* enable channel */
		info->active_channels[idx] = true;

		ret = fwnode_property_read_u32(node, "shunt-resistor-micro-ohms",
					       &info->shunts[idx]);
		if (ret) {
			dev_err_probe(dev, ret,
				      "%s: invalid shunt-resistor value: %d\n",
				      fwnode_get_name(node), info->shunts[idx]);
			goto err_fwnode;
		}

		if (fwnode_property_present(node, "label")) {
			ret = fwnode_property_read_string(node, "label",
							  (const char **)&info->labels[idx]);
			if (ret) {
				dev_err_probe(dev, ret,
					      "%s: invalid rail-name value\n",
					      fwnode_get_name(node));
				goto err_fwnode;
			}
		}

		info->bi_dir[idx] = fwnode_property_read_bool(node, "bipolar");

		current_channel++;
	}

	return true;

err_fwnode:
	fwnode_handle_put(node);

	return false;
}

static void pac1934_cancel_delayed_work(void *dwork)
{
	cancel_delayed_work_sync(dwork);
}

static int pac1934_chip_configure(struct pac1934_chip_info *info)
{
	int cnt, ret;
	struct i2c_client *client = info->client;
	u8 regs[PAC1934_CTRL_STATUS_INFO_LEN], idx, ctrl_reg;
	u32 wait_time;

	info->chip_reg_data.num_enabled_channels = 0;
	for (cnt = 0;  cnt < info->phys_channels; cnt++) {
		if (info->active_channels[cnt])
			info->chip_reg_data.num_enabled_channels++;
	}

	/*
	 * read whatever information was gathered before the driver was loaded
	 * establish which channels are enabled/disabled and then establish the
	 * information retrieval mode (using SKIP or no).
	 * Read the chip ID values
	 */
	ret = i2c_smbus_read_i2c_block_data(client, PAC1934_CTRL_STAT_REGS_ADDR,
					    ARRAY_SIZE(regs),
					    (u8 *)regs);
	if (ret < 0) {
		dev_err_probe(&client->dev, ret,
			      "%s - cannot read regs from 0x%02X\n",
			      __func__, PAC1934_CTRL_STAT_REGS_ADDR);
		return ret;
	}

	/* write the CHANNEL_DIS and the NEG_PWR registers */
	regs[PAC1934_CHANNEL_DIS_REG_OFF] =
		FIELD_PREP(PAC1934_CHAN_DIS_CH1_OFF_MASK, info->active_channels[0] ? 0 : 1) |
		FIELD_PREP(PAC1934_CHAN_DIS_CH2_OFF_MASK, info->active_channels[1] ? 0 : 1) |
		FIELD_PREP(PAC1934_CHAN_DIS_CH3_OFF_MASK, info->active_channels[2] ? 0 : 1) |
		FIELD_PREP(PAC1934_CHAN_DIS_CH4_OFF_MASK, info->active_channels[3] ? 0 : 1) |
		FIELD_PREP(PAC1934_SMBUS_TIMEOUT_MASK, 0) |
		FIELD_PREP(PAC1934_SMBUS_BYTECOUNT_MASK, 0) |
		FIELD_PREP(PAC1934_SMBUS_NO_SKIP_MASK, 0);

	regs[PAC1934_NEG_PWR_REG_OFF] =
		FIELD_PREP(PAC1934_NEG_PWR_CH1_BIDI_MASK, info->bi_dir[0]) |
		FIELD_PREP(PAC1934_NEG_PWR_CH2_BIDI_MASK, info->bi_dir[1]) |
		FIELD_PREP(PAC1934_NEG_PWR_CH3_BIDI_MASK, info->bi_dir[2]) |
		FIELD_PREP(PAC1934_NEG_PWR_CH4_BIDI_MASK, info->bi_dir[3]) |
		FIELD_PREP(PAC1934_NEG_PWR_CH1_BIDV_MASK, info->bi_dir[0]) |
		FIELD_PREP(PAC1934_NEG_PWR_CH2_BIDV_MASK, info->bi_dir[1]) |
		FIELD_PREP(PAC1934_NEG_PWR_CH3_BIDV_MASK, info->bi_dir[2]) |
		FIELD_PREP(PAC1934_NEG_PWR_CH4_BIDV_MASK, info->bi_dir[3]);

	/* no SLOW triggered REFRESH, clear POR */
	regs[PAC1934_SLOW_REG_OFF] = 0;

	ret =  i2c_smbus_write_block_data(client, PAC1934_CTRL_STAT_REGS_ADDR,
					  ARRAY_SIZE(regs), (u8 *)regs);
	if (ret)
		return ret;

	/* Default sampling rate */
	ctrl_reg = FIELD_PREP(PAC1934_CRTL_SAMPLE_RATE_MASK, PAC1934_SAMP_1024SPS);

	ret = i2c_smbus_write_byte_data(client, PAC1934_CTRL_REG_ADDR, ctrl_reg);
	if (ret)
		return ret;

	/*
	 * send a REFRESH to the chip, so the new settings take place
	 * as well as resetting the accumulators
	 */
	ret = i2c_smbus_write_byte(client, PAC1934_REFRESH_REG_ADDR);
	if (ret) {
		dev_err(&client->dev,
			"%s - cannot send 0x%02X\n",
			__func__, PAC1934_REFRESH_REG_ADDR);
		return ret;
	}

	/*
	 * get the current(in the chip) sampling speed and compute the
	 * required timeout based on its value
	 * the timeout is 1/sampling_speed
	 */
	idx = regs[PAC1934_CTRL_ACT_REG_OFF] >> PAC1934_SAMPLE_RATE_SHIFT;
	wait_time = (1024 / samp_rate_map_tbl[idx]) * 1000;

	/*
	 * wait the maximum amount of time to be on the safe side
	 * the maximum wait time is for 8sps
	 */
	usleep_range(wait_time, wait_time + 100);

	INIT_DELAYED_WORK(&info->work_chip_rfsh, pac1934_work_periodic_rfsh);
	/* Setup the latest moment for reading the regs before saturation */
	schedule_delayed_work(&info->work_chip_rfsh,
			      msecs_to_jiffies(PAC1934_MAX_RFSH_LIMIT_MS));

	return devm_add_action_or_reset(&client->dev, pac1934_cancel_delayed_work,
					&info->work_chip_rfsh);
}

static int pac1934_prep_iio_channels(struct pac1934_chip_info *info, struct iio_dev *indio_dev)
{
	struct iio_chan_spec *ch_sp;
	int channel_size, attribute_count, cnt;
	void *dyn_ch_struct, *tmp_data;
	struct device *dev = &info->client->dev;

	/* find out dynamically how many IIO channels we need */
	attribute_count = 0;
	channel_size = 0;
	for (cnt = 0; cnt < info->phys_channels; cnt++) {
		if (!info->active_channels[cnt])
			continue;

		/* add the size of the properties of one chip physical channel */
		channel_size += sizeof(pac1934_single_channel);
		/* count how many enabled channels we have */
		attribute_count += ARRAY_SIZE(pac1934_single_channel);
		dev_dbg(dev, ":%s: Channel %d active\n", __func__, cnt + 1);
	}

	dyn_ch_struct = devm_kzalloc(dev, channel_size, GFP_KERNEL);
	if (!dyn_ch_struct)
		return -EINVAL;

	tmp_data = dyn_ch_struct;

	/* populate the dynamic channels and make all the adjustments */
	for (cnt = 0; cnt < info->phys_channels; cnt++) {
		if (!info->active_channels[cnt])
			continue;

		memcpy(tmp_data, pac1934_single_channel, sizeof(pac1934_single_channel));
		ch_sp = (struct iio_chan_spec *)tmp_data;
		ch_sp[PAC1934_CH_ENERGY].channel = cnt + 1;
		ch_sp[PAC1934_CH_ENERGY].scan_index = cnt;
		ch_sp[PAC1934_CH_ENERGY].address = cnt + PAC1934_VPOWER_ACC_1_ADDR;
		ch_sp[PAC1934_CH_POWER].channel = cnt + 1;
		ch_sp[PAC1934_CH_POWER].scan_index = cnt;
		ch_sp[PAC1934_CH_POWER].address = cnt + PAC1934_VPOWER_1_ADDR;
		ch_sp[PAC1934_CH_VOLTAGE].channel = cnt + 1;
		ch_sp[PAC1934_CH_VOLTAGE].scan_index = cnt;
		ch_sp[PAC1934_CH_VOLTAGE].address = cnt + PAC1934_VBUS_1_ADDR;
		ch_sp[PAC1934_CH_CURRENT].channel = cnt + 1;
		ch_sp[PAC1934_CH_CURRENT].scan_index = cnt;
		ch_sp[PAC1934_CH_CURRENT].address = cnt + PAC1934_VSENSE_1_ADDR;

		/*
		 * In order to be able to use labels for PAC1934_CH_VOLTAGE, and
		 * PAC1934_CH_VOLTAGE_AVERAGE,respectively PAC1934_CH_CURRENT
		 * and PAC1934_CH_CURRENT_AVERAGE we need to use different
		 * channel numbers. We will add +5 (+1 to maximum PAC channels).
		 */
		ch_sp[PAC1934_CH_VOLTAGE_AVERAGE].channel = cnt + 5;
		ch_sp[PAC1934_CH_VOLTAGE_AVERAGE].scan_index = cnt;
		ch_sp[PAC1934_CH_VOLTAGE_AVERAGE].address = cnt + PAC1934_VBUS_AVG_1_ADDR;
		ch_sp[PAC1934_CH_CURRENT_AVERAGE].channel = cnt + 5;
		ch_sp[PAC1934_CH_CURRENT_AVERAGE].scan_index = cnt;
		ch_sp[PAC1934_CH_CURRENT_AVERAGE].address = cnt + PAC1934_VSENSE_AVG_1_ADDR;

		/*
		 * now modify the parameters in all channels if the
		 * whole chip rail(channel) is bi-directional
		 */
		if (info->bi_dir[cnt]) {
			ch_sp[PAC1934_CH_ENERGY].scan_type.sign = 's';
			ch_sp[PAC1934_CH_ENERGY].scan_type.realbits = 47;
			ch_sp[PAC1934_CH_POWER].scan_type.sign = 's';
			ch_sp[PAC1934_CH_POWER].scan_type.realbits = 27;
			ch_sp[PAC1934_CH_VOLTAGE].scan_type.sign = 's';
			ch_sp[PAC1934_CH_VOLTAGE].scan_type.realbits = 15;
			ch_sp[PAC1934_CH_CURRENT].scan_type.sign = 's';
			ch_sp[PAC1934_CH_CURRENT].scan_type.realbits = 15;
			ch_sp[PAC1934_CH_VOLTAGE_AVERAGE].scan_type.sign = 's';
			ch_sp[PAC1934_CH_VOLTAGE_AVERAGE].scan_type.realbits = 15;
			ch_sp[PAC1934_CH_CURRENT_AVERAGE].scan_type.sign = 's';
			ch_sp[PAC1934_CH_CURRENT_AVERAGE].scan_type.realbits = 15;
		}
		tmp_data += sizeof(pac1934_single_channel);
	}

	/*
	 * send the updated dynamic channel structure information towards IIO
	 * prepare the required field for IIO class registration
	 */
	indio_dev->num_channels = attribute_count;
	indio_dev->channels = (const struct iio_chan_spec *)dyn_ch_struct;

	return 0;
}

static IIO_DEVICE_ATTR(in_shunt_resistor1, 0644,
		       pac1934_shunt_value_show, pac1934_shunt_value_store, 0);
static IIO_DEVICE_ATTR(in_shunt_resistor2, 0644,
		       pac1934_shunt_value_show, pac1934_shunt_value_store, 1);
static IIO_DEVICE_ATTR(in_shunt_resistor3, 0644,
		       pac1934_shunt_value_show, pac1934_shunt_value_store, 2);
static IIO_DEVICE_ATTR(in_shunt_resistor4, 0644,
		       pac1934_shunt_value_show, pac1934_shunt_value_store, 3);

static int pac1934_prep_custom_attributes(struct pac1934_chip_info *info,
					  struct iio_dev *indio_dev)
{
	int i, active_channels_count = 0;
	struct attribute **pac1934_custom_attr;
	struct attribute_group *pac1934_group;
	struct device *dev = &info->client->dev;

	for (i = 0 ; i < info->phys_channels; i++)
		if (info->active_channels[i])
			active_channels_count++;

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

	pac1934_custom_attr = devm_kzalloc(dev,
					   (PAC1934_CUSTOM_ATTR_FOR_CHANNEL *
					   active_channels_count)
					   * sizeof(*pac1934_group) + 1,
					   GFP_KERNEL);
	if (!pac1934_custom_attr)
		return -ENOMEM;

	i = 0;
	if (info->active_channels[0])
		pac1934_custom_attr[i++] = PAC1934_DEV_ATTR(in_shunt_resistor1);

	if (info->active_channels[1])
		pac1934_custom_attr[i++] = PAC1934_DEV_ATTR(in_shunt_resistor2);

	if (info->active_channels[2])
		pac1934_custom_attr[i++] = PAC1934_DEV_ATTR(in_shunt_resistor3);

	if (info->active_channels[3])
		pac1934_custom_attr[i] = PAC1934_DEV_ATTR(in_shunt_resistor4);

	pac1934_group->attrs = pac1934_custom_attr;
	info->iio_info.attrs = pac1934_group;

	return 0;
}

static void pac1934_mutex_destroy(void *data)
{
	struct mutex *lock = data;

	mutex_destroy(lock);
}

static const struct iio_info pac1934_info = {
	.read_raw = pac1934_read_raw,
	.write_raw = pac1934_write_raw,
	.read_avail = pac1934_read_avail,
	.read_label = pac1934_read_label,
};

static int pac1934_probe(struct i2c_client *client)
{
	struct pac1934_chip_info *info;
	const struct pac1934_features *chip;
	struct iio_dev *indio_dev;
	int cnt, ret;
	bool match = false;
	struct device *dev = &client->dev;

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

	info = iio_priv(indio_dev);

	info->client = client;

	/* always start with energy accumulation enabled */
	for (cnt = 0; cnt < PAC1934_MAX_NUM_CHANNELS; cnt++)
		info->enable_energy[cnt] = true;

	ret = pac1934_chip_identify(info);
	if (ret < 0) {
		/*
		 * If failed to identify the hardware based on internal
		 * registers, try using fallback compatible in device tree
		 * to deal with some newer part number.
		 */
		chip = i2c_get_match_data(client);
		if (!chip)
			return -EINVAL;

		info->phys_channels = chip->phys_channels;
		indio_dev->name = chip->name;
	} else {
		info->phys_channels = pac1934_chip_config[ret].phys_channels;
		indio_dev->name = pac1934_chip_config[ret].name;
	}

	if (acpi_match_device(dev->driver->acpi_match_table, dev))
		match = pac1934_acpi_parse_channel_config(client, info);
	else
		/*
		 * This makes it possible to use also ACPI PRP0001 for
		 * registering the device using device tree properties.
		 */
		match = pac1934_of_parse_channel_config(client, info);

	if (!match)
		return dev_err_probe(dev, -EINVAL,
				     "parameter parsing returned an error\n");

	mutex_init(&info->lock);
	ret = devm_add_action_or_reset(dev, pac1934_mutex_destroy,
				       &info->lock);
	if (ret < 0)
		return ret;

	/*
	 * do now any chip specific initialization (e.g. read/write
	 * some registers), enable/disable certain channels, change the sampling
	 * rate to the requested value
	 */
	ret = pac1934_chip_configure(info);
	if (ret < 0)
		return ret;

	/* prepare the channel information */
	ret = pac1934_prep_iio_channels(info, indio_dev);
	if (ret < 0)
		return ret;

	info->iio_info = pac1934_info;
	indio_dev->info = &info->iio_info;
	indio_dev->modes = INDIO_DIRECT_MODE;

	ret = pac1934_prep_custom_attributes(info, indio_dev);
	if (ret < 0)
		return dev_err_probe(dev, ret,
				     "Can't configure custom attributes for PAC1934 device\n");

	/*
	 * read whatever has been accumulated in the chip so far
	 * and reset the accumulators
	 */
	ret = pac1934_reg_snapshot(info, true, PAC1934_REFRESH_REG_ADDR,
				   PAC1934_MIN_UPDATE_WAIT_TIME_US);
	if (ret < 0)
		return ret;

	ret = devm_iio_device_register(dev, indio_dev);
	if (ret < 0)
		return dev_err_probe(dev, ret,
				     "Can't register IIO device\n");

	return 0;
}

static const struct i2c_device_id pac1934_id[] = {
	{ .name = "pac1931", .driver_data = (kernel_ulong_t)&pac1934_chip_config[PAC1931] },
	{ .name = "pac1932", .driver_data = (kernel_ulong_t)&pac1934_chip_config[PAC1932] },
	{ .name = "pac1933", .driver_data = (kernel_ulong_t)&pac1934_chip_config[PAC1933] },
	{ .name = "pac1934", .driver_data = (kernel_ulong_t)&pac1934_chip_config[PAC1934] },
	{}
};
MODULE_DEVICE_TABLE(i2c, pac1934_id);

static const struct of_device_id pac1934_of_match[] = {
	{
		.compatible = "microchip,pac1931",
		.data = &pac1934_chip_config[PAC1931]
	},
	{
		.compatible = "microchip,pac1932",
		.data = &pac1934_chip_config[PAC1932]
	},
	{
		.compatible = "microchip,pac1933",
		.data = &pac1934_chip_config[PAC1933]
	},
	{
		.compatible = "microchip,pac1934",
		.data = &pac1934_chip_config[PAC1934]
	},
	{}
};
MODULE_DEVICE_TABLE(of, pac1934_of_match);

/*
 * using MCHP1930 to be compatible with BIOS ACPI. See example:
 * https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ApplicationNotes/ApplicationNotes/PAC1934-Integration-Notes-for-Microsoft-Windows-10-and-Windows-11-Driver-Support-DS00002534.pdf
 */
static const struct acpi_device_id pac1934_acpi_match[] = {
	{ "MCHP1930", .driver_data = (kernel_ulong_t)&pac1934_chip_config[PAC1934] },
	{}
};
MODULE_DEVICE_TABLE(acpi, pac1934_acpi_match);

static struct i2c_driver pac1934_driver = {
	.driver	 = {
		.name = "pac1934",
		.of_match_table = pac1934_of_match,
		.acpi_match_table = pac1934_acpi_match
	},
	.probe = pac1934_probe,
	.id_table = pac1934_id,
};

module_i2c_driver(pac1934_driver);

MODULE_AUTHOR("Bogdan Bolocan <bogdan.bolocan@microchip.com>");
MODULE_AUTHOR("Victor Tudose");
MODULE_AUTHOR("Marius Cristea <marius.cristea@microchip.com>");
MODULE_DESCRIPTION("IIO driver for PAC1934 Multi-Channel DC Power/Energy Monitor");
MODULE_LICENSE("GPL");