summaryrefslogtreecommitdiff
path: root/drivers/iio/adc/xilinx-ams.c
blob: 76dd0343f5f76a6b1a8882e41d6a7f333c3a36e4 (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Xilinx AMS driver
 *
 *  Copyright (C) 2021 Xilinx, Inc.
 *
 *  Manish Narani <mnarani@xilinx.com>
 *  Rajnikant Bhojani <rajnikant.bhojani@xilinx.com>
 */

#include <linux/bits.h>
#include <linux/bitfield.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/devm-helpers.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/iopoll.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/overflow.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/slab.h>

#include <linux/iio/events.h>
#include <linux/iio/iio.h>

/* AMS registers definitions */
#define AMS_ISR_0			0x010
#define AMS_ISR_1			0x014
#define AMS_IER_0			0x020
#define AMS_IER_1			0x024
#define AMS_IDR_0			0x028
#define AMS_IDR_1			0x02C
#define AMS_PS_CSTS			0x040
#define AMS_PL_CSTS			0x044

#define AMS_VCC_PSPLL0			0x060
#define AMS_VCC_PSPLL3			0x06C
#define AMS_VCCINT			0x078
#define AMS_VCCBRAM			0x07C
#define AMS_VCCAUX			0x080
#define AMS_PSDDRPLL			0x084
#define AMS_PSINTFPDDR			0x09C

#define AMS_VCC_PSPLL0_CH		48
#define AMS_VCC_PSPLL3_CH		51
#define AMS_VCCINT_CH			54
#define AMS_VCCBRAM_CH			55
#define AMS_VCCAUX_CH			56
#define AMS_PSDDRPLL_CH			57
#define AMS_PSINTFPDDR_CH		63

#define AMS_REG_CONFIG0			0x100
#define AMS_REG_CONFIG1			0x104
#define AMS_REG_CONFIG3			0x10C
#define AMS_REG_CONFIG4			0x110
#define AMS_REG_SEQ_CH0			0x120
#define AMS_REG_SEQ_CH1			0x124
#define AMS_REG_SEQ_CH2			0x118

#define AMS_VUSER0_MASK			BIT(0)
#define AMS_VUSER1_MASK			BIT(1)
#define AMS_VUSER2_MASK			BIT(2)
#define AMS_VUSER3_MASK			BIT(3)

#define AMS_TEMP			0x000
#define AMS_SUPPLY1			0x004
#define AMS_SUPPLY2			0x008
#define AMS_VP_VN			0x00C
#define AMS_VREFP			0x010
#define AMS_VREFN			0x014
#define AMS_SUPPLY3			0x018
#define AMS_SUPPLY4			0x034
#define AMS_SUPPLY5			0x038
#define AMS_SUPPLY6			0x03C
#define AMS_SUPPLY7			0x200
#define AMS_SUPPLY8			0x204
#define AMS_SUPPLY9			0x208
#define AMS_SUPPLY10			0x20C
#define AMS_VCCAMS			0x210
#define AMS_TEMP_REMOTE			0x214

#define AMS_REG_VAUX(x)			(0x40 + 4 * (x))

#define AMS_PS_RESET_VALUE		0xFFFF
#define AMS_PL_RESET_VALUE		0xFFFF

#define AMS_CONF0_CHANNEL_NUM_MASK	GENMASK(6, 0)

#define AMS_CONF1_SEQ_MASK		GENMASK(15, 12)
#define AMS_CONF1_SEQ_DEFAULT		FIELD_PREP(AMS_CONF1_SEQ_MASK, 0)
#define AMS_CONF1_SEQ_CONTINUOUS	FIELD_PREP(AMS_CONF1_SEQ_MASK, 2)
#define AMS_CONF1_SEQ_SINGLE_CHANNEL	FIELD_PREP(AMS_CONF1_SEQ_MASK, 3)

#define AMS_REG_SEQ0_MASK		GENMASK(15, 0)
#define AMS_REG_SEQ2_MASK		GENMASK(21, 16)
#define AMS_REG_SEQ1_MASK		GENMASK_ULL(37, 22)

#define AMS_PS_SEQ_MASK			GENMASK(21, 0)
#define AMS_PL_SEQ_MASK			GENMASK_ULL(59, 22)

#define AMS_ALARM_TEMP			0x140
#define AMS_ALARM_SUPPLY1		0x144
#define AMS_ALARM_SUPPLY2		0x148
#define AMS_ALARM_SUPPLY3		0x160
#define AMS_ALARM_SUPPLY4		0x164
#define AMS_ALARM_SUPPLY5		0x168
#define AMS_ALARM_SUPPLY6		0x16C
#define AMS_ALARM_SUPPLY7		0x180
#define AMS_ALARM_SUPPLY8		0x184
#define AMS_ALARM_SUPPLY9		0x188
#define AMS_ALARM_SUPPLY10		0x18C
#define AMS_ALARM_VCCAMS		0x190
#define AMS_ALARM_TEMP_REMOTE		0x194
#define AMS_ALARM_THRESHOLD_OFF_10	0x10
#define AMS_ALARM_THRESHOLD_OFF_20	0x20

#define AMS_ALARM_THR_DIRECT_MASK	BIT(1)
#define AMS_ALARM_THR_MIN		0x0000
#define AMS_ALARM_THR_MAX		(BIT(16) - 1)

#define AMS_ALARM_MASK			GENMASK_ULL(63, 0)
#define AMS_NO_OF_ALARMS		32
#define AMS_PL_ALARM_START		16
#define AMS_PL_ALARM_MASK		GENMASK(31, 16)
#define AMS_ISR0_ALARM_MASK		GENMASK(31, 0)
#define AMS_ISR1_ALARM_MASK		(GENMASK(31, 29) | GENMASK(4, 0))
#define AMS_ISR1_EOC_MASK		BIT(3)
#define AMS_ISR1_INTR_MASK		GENMASK_ULL(63, 32)
#define AMS_ISR0_ALARM_2_TO_0_MASK	GENMASK(2, 0)
#define AMS_ISR0_ALARM_6_TO_3_MASK	GENMASK(6, 3)
#define AMS_ISR0_ALARM_12_TO_7_MASK	GENMASK(13, 8)
#define AMS_CONF1_ALARM_2_TO_0_MASK	GENMASK(3, 1)
#define AMS_CONF1_ALARM_6_TO_3_MASK	GENMASK(11, 8)
#define AMS_CONF1_ALARM_12_TO_7_MASK	GENMASK(5, 0)
#define AMS_REGCFG1_ALARM_MASK  \
	(AMS_CONF1_ALARM_2_TO_0_MASK | AMS_CONF1_ALARM_6_TO_3_MASK | BIT(0))
#define AMS_REGCFG3_ALARM_MASK		AMS_CONF1_ALARM_12_TO_7_MASK

#define AMS_PS_CSTS_PS_READY		(BIT(27) | BIT(16))
#define AMS_PL_CSTS_ACCESS_MASK		BIT(1)

#define AMS_PL_MAX_FIXED_CHANNEL	10
#define AMS_PL_MAX_EXT_CHANNEL		20

#define AMS_INIT_POLL_TIME_US		200
#define AMS_INIT_TIMEOUT_US		10000
#define AMS_UNMASK_TIMEOUT_MS		500

/*
 * Following scale and offset value is derived from
 * UG580 (v1.7) December 20, 2016
 */
#define AMS_SUPPLY_SCALE_1VOLT_mV		1000
#define AMS_SUPPLY_SCALE_3VOLT_mV		3000
#define AMS_SUPPLY_SCALE_6VOLT_mV		6000
#define AMS_SUPPLY_SCALE_DIV_BIT	16

#define AMS_TEMP_SCALE			509314
#define AMS_TEMP_SCALE_DIV_BIT		16
#define AMS_TEMP_OFFSET			-((280230LL << 16) / 509314)

enum ams_alarm_bit {
	AMS_ALARM_BIT_TEMP = 0,
	AMS_ALARM_BIT_SUPPLY1 = 1,
	AMS_ALARM_BIT_SUPPLY2 = 2,
	AMS_ALARM_BIT_SUPPLY3 = 3,
	AMS_ALARM_BIT_SUPPLY4 = 4,
	AMS_ALARM_BIT_SUPPLY5 = 5,
	AMS_ALARM_BIT_SUPPLY6 = 6,
	AMS_ALARM_BIT_RESERVED = 7,
	AMS_ALARM_BIT_SUPPLY7 = 8,
	AMS_ALARM_BIT_SUPPLY8 = 9,
	AMS_ALARM_BIT_SUPPLY9 = 10,
	AMS_ALARM_BIT_SUPPLY10 = 11,
	AMS_ALARM_BIT_VCCAMS = 12,
	AMS_ALARM_BIT_TEMP_REMOTE = 13,
};

enum ams_seq {
	AMS_SEQ_VCC_PSPLL = 0,
	AMS_SEQ_VCC_PSBATT = 1,
	AMS_SEQ_VCCINT = 2,
	AMS_SEQ_VCCBRAM = 3,
	AMS_SEQ_VCCAUX = 4,
	AMS_SEQ_PSDDRPLL = 5,
	AMS_SEQ_INTDDR = 6,
};

enum ams_ps_pl_seq {
	AMS_SEQ_CALIB = 0,
	AMS_SEQ_RSVD_1 = 1,
	AMS_SEQ_RSVD_2 = 2,
	AMS_SEQ_TEST = 3,
	AMS_SEQ_RSVD_4 = 4,
	AMS_SEQ_SUPPLY4 = 5,
	AMS_SEQ_SUPPLY5 = 6,
	AMS_SEQ_SUPPLY6 = 7,
	AMS_SEQ_TEMP = 8,
	AMS_SEQ_SUPPLY2 = 9,
	AMS_SEQ_SUPPLY1 = 10,
	AMS_SEQ_VP_VN = 11,
	AMS_SEQ_VREFP = 12,
	AMS_SEQ_VREFN = 13,
	AMS_SEQ_SUPPLY3 = 14,
	AMS_SEQ_CURRENT_MON = 15,
	AMS_SEQ_SUPPLY7 = 16,
	AMS_SEQ_SUPPLY8 = 17,
	AMS_SEQ_SUPPLY9 = 18,
	AMS_SEQ_SUPPLY10 = 19,
	AMS_SEQ_VCCAMS = 20,
	AMS_SEQ_TEMP_REMOTE = 21,
	AMS_SEQ_MAX = 22
};

#define AMS_PS_SEQ_MAX		AMS_SEQ_MAX
#define AMS_SEQ(x)		(AMS_SEQ_MAX + (x))
#define PS_SEQ(x)		(x)
#define PL_SEQ(x)		(AMS_PS_SEQ_MAX + (x))
#define AMS_CTRL_SEQ_BASE	(AMS_PS_SEQ_MAX * 3)

#define AMS_CHAN_TEMP(_scan_index, _addr, _name) { \
	.type = IIO_TEMP, \
	.indexed = 1, \
	.address = (_addr), \
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
		BIT(IIO_CHAN_INFO_SCALE) | \
		BIT(IIO_CHAN_INFO_OFFSET), \
	.event_spec = ams_temp_events, \
	.scan_index = _scan_index, \
	.num_event_specs = ARRAY_SIZE(ams_temp_events), \
	.datasheet_name = _name, \
}

#define AMS_CHAN_VOLTAGE(_scan_index, _addr, _alarm, _name) { \
	.type = IIO_VOLTAGE, \
	.indexed = 1, \
	.address = (_addr), \
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
		BIT(IIO_CHAN_INFO_SCALE), \
	.event_spec = (_alarm) ? ams_voltage_events : NULL, \
	.scan_index = _scan_index, \
	.num_event_specs = (_alarm) ? ARRAY_SIZE(ams_voltage_events) : 0, \
	.datasheet_name = _name, \
}

#define AMS_PS_CHAN_TEMP(_scan_index, _addr, _name) \
	AMS_CHAN_TEMP(PS_SEQ(_scan_index), _addr, _name)
#define AMS_PS_CHAN_VOLTAGE(_scan_index, _addr, _name) \
	AMS_CHAN_VOLTAGE(PS_SEQ(_scan_index), _addr, true, _name)

#define AMS_PL_CHAN_TEMP(_scan_index, _addr, _name) \
	AMS_CHAN_TEMP(PL_SEQ(_scan_index), _addr, _name)
#define AMS_PL_CHAN_VOLTAGE(_scan_index, _addr, _alarm, _name) \
	AMS_CHAN_VOLTAGE(PL_SEQ(_scan_index), _addr, _alarm, _name)
#define AMS_PL_AUX_CHAN_VOLTAGE(_auxno) \
	AMS_CHAN_VOLTAGE(PL_SEQ(AMS_SEQ(_auxno)), AMS_REG_VAUX(_auxno), false, \
			 "VAUX" #_auxno)
#define AMS_CTRL_CHAN_VOLTAGE(_scan_index, _addr, _name) \
	AMS_CHAN_VOLTAGE(PL_SEQ(AMS_SEQ(AMS_SEQ(_scan_index))), _addr, false, \
			 _name)

/**
 * struct ams - This structure contains necessary state for xilinx-ams to operate
 * @base: physical base address of device
 * @ps_base: physical base address of PS device
 * @pl_base: physical base address of PL device
 * @clk: clocks associated with the device
 * @dev: pointer to device struct
 * @lock: to handle multiple user interaction
 * @intr_lock: to protect interrupt mask values
 * @alarm_mask: alarm configuration
 * @current_masked_alarm: currently masked due to alarm
 * @intr_mask: interrupt configuration
 * @ams_unmask_work: re-enables event once the event condition disappears
 *
 */
struct ams {
	void __iomem *base;
	void __iomem *ps_base;
	void __iomem *pl_base;
	struct clk *clk;
	struct device *dev;
	struct mutex lock;
	spinlock_t intr_lock;
	unsigned int alarm_mask;
	unsigned int current_masked_alarm;
	u64 intr_mask;
	struct delayed_work ams_unmask_work;
};

static inline void ams_ps_update_reg(struct ams *ams, unsigned int offset,
				     u32 mask, u32 data)
{
	u32 val, regval;

	val = readl(ams->ps_base + offset);
	regval = (val & ~mask) | (data & mask);
	writel(regval, ams->ps_base + offset);
}

static inline void ams_pl_update_reg(struct ams *ams, unsigned int offset,
				     u32 mask, u32 data)
{
	u32 val, regval;

	val = readl(ams->pl_base + offset);
	regval = (val & ~mask) | (data & mask);
	writel(regval, ams->pl_base + offset);
}

static void ams_update_intrmask(struct ams *ams, u64 mask, u64 val)
{
	u32 regval;

	ams->intr_mask = (ams->intr_mask & ~mask) | (val & mask);

	regval = ~(ams->intr_mask | ams->current_masked_alarm);
	writel(regval, ams->base + AMS_IER_0);

	regval = ~(FIELD_GET(AMS_ISR1_INTR_MASK, ams->intr_mask));
	writel(regval, ams->base + AMS_IER_1);

	regval = ams->intr_mask | ams->current_masked_alarm;
	writel(regval, ams->base + AMS_IDR_0);

	regval = FIELD_GET(AMS_ISR1_INTR_MASK, ams->intr_mask);
	writel(regval, ams->base + AMS_IDR_1);
}

static void ams_disable_all_alarms(struct ams *ams)
{
	/* disable PS module alarm */
	if (ams->ps_base) {
		ams_ps_update_reg(ams, AMS_REG_CONFIG1, AMS_REGCFG1_ALARM_MASK,
				  AMS_REGCFG1_ALARM_MASK);
		ams_ps_update_reg(ams, AMS_REG_CONFIG3, AMS_REGCFG3_ALARM_MASK,
				  AMS_REGCFG3_ALARM_MASK);
	}

	/* disable PL module alarm */
	if (ams->pl_base) {
		ams_pl_update_reg(ams, AMS_REG_CONFIG1, AMS_REGCFG1_ALARM_MASK,
				  AMS_REGCFG1_ALARM_MASK);
		ams_pl_update_reg(ams, AMS_REG_CONFIG3, AMS_REGCFG3_ALARM_MASK,
				  AMS_REGCFG3_ALARM_MASK);
	}
}

static void ams_update_ps_alarm(struct ams *ams, unsigned long alarm_mask)
{
	u32 cfg;
	u32 val;

	val = FIELD_GET(AMS_ISR0_ALARM_2_TO_0_MASK, alarm_mask);
	cfg = ~(FIELD_PREP(AMS_CONF1_ALARM_2_TO_0_MASK, val));

	val = FIELD_GET(AMS_ISR0_ALARM_6_TO_3_MASK, alarm_mask);
	cfg &= ~(FIELD_PREP(AMS_CONF1_ALARM_6_TO_3_MASK, val));

	ams_ps_update_reg(ams, AMS_REG_CONFIG1, AMS_REGCFG1_ALARM_MASK, cfg);

	val = FIELD_GET(AMS_ISR0_ALARM_12_TO_7_MASK, alarm_mask);
	cfg = ~(FIELD_PREP(AMS_CONF1_ALARM_12_TO_7_MASK, val));
	ams_ps_update_reg(ams, AMS_REG_CONFIG3, AMS_REGCFG3_ALARM_MASK, cfg);
}

static void ams_update_pl_alarm(struct ams *ams, unsigned long alarm_mask)
{
	unsigned long pl_alarm_mask;
	u32 cfg;
	u32 val;

	pl_alarm_mask = FIELD_GET(AMS_PL_ALARM_MASK, alarm_mask);

	val = FIELD_GET(AMS_ISR0_ALARM_2_TO_0_MASK, pl_alarm_mask);
	cfg = ~(FIELD_PREP(AMS_CONF1_ALARM_2_TO_0_MASK, val));

	val = FIELD_GET(AMS_ISR0_ALARM_6_TO_3_MASK, pl_alarm_mask);
	cfg &= ~(FIELD_PREP(AMS_CONF1_ALARM_6_TO_3_MASK, val));

	ams_pl_update_reg(ams, AMS_REG_CONFIG1, AMS_REGCFG1_ALARM_MASK, cfg);

	val = FIELD_GET(AMS_ISR0_ALARM_12_TO_7_MASK, pl_alarm_mask);
	cfg = ~(FIELD_PREP(AMS_CONF1_ALARM_12_TO_7_MASK, val));
	ams_pl_update_reg(ams, AMS_REG_CONFIG3, AMS_REGCFG3_ALARM_MASK, cfg);
}

static void ams_update_alarm(struct ams *ams, unsigned long alarm_mask)
{
	unsigned long flags;

	if (ams->ps_base)
		ams_update_ps_alarm(ams, alarm_mask);

	if (ams->pl_base)
		ams_update_pl_alarm(ams, alarm_mask);

	spin_lock_irqsave(&ams->intr_lock, flags);
	ams_update_intrmask(ams, AMS_ISR0_ALARM_MASK, ~alarm_mask);
	spin_unlock_irqrestore(&ams->intr_lock, flags);
}

static void ams_enable_channel_sequence(struct iio_dev *indio_dev)
{
	struct ams *ams = iio_priv(indio_dev);
	unsigned long long scan_mask;
	int i;
	u32 regval;

	/*
	 * Enable channel sequence. First 22 bits of scan_mask represent
	 * PS channels, and next remaining bits represent PL channels.
	 */

	/* Run calibration of PS & PL as part of the sequence */
	scan_mask = BIT(0) | BIT(AMS_PS_SEQ_MAX);
	for (i = 0; i < indio_dev->num_channels; i++) {
		const struct iio_chan_spec *chan = &indio_dev->channels[i];

		if (chan->scan_index < AMS_CTRL_SEQ_BASE)
			scan_mask |= BIT_ULL(chan->scan_index);
	}

	if (ams->ps_base) {
		/* put sysmon in a soft reset to change the sequence */
		ams_ps_update_reg(ams, AMS_REG_CONFIG1, AMS_CONF1_SEQ_MASK,
				  AMS_CONF1_SEQ_DEFAULT);

		/* configure basic channels */
		regval = FIELD_GET(AMS_REG_SEQ0_MASK, scan_mask);
		writel(regval, ams->ps_base + AMS_REG_SEQ_CH0);

		regval = FIELD_GET(AMS_REG_SEQ2_MASK, scan_mask);
		writel(regval, ams->ps_base + AMS_REG_SEQ_CH2);

		/* set continuous sequence mode */
		ams_ps_update_reg(ams, AMS_REG_CONFIG1, AMS_CONF1_SEQ_MASK,
				  AMS_CONF1_SEQ_CONTINUOUS);
	}

	if (ams->pl_base) {
		/* put sysmon in a soft reset to change the sequence */
		ams_pl_update_reg(ams, AMS_REG_CONFIG1, AMS_CONF1_SEQ_MASK,
				  AMS_CONF1_SEQ_DEFAULT);

		/* configure basic channels */
		scan_mask = FIELD_GET(AMS_PL_SEQ_MASK, scan_mask);

		regval = FIELD_GET(AMS_REG_SEQ0_MASK, scan_mask);
		writel(regval, ams->pl_base + AMS_REG_SEQ_CH0);

		regval = FIELD_GET(AMS_REG_SEQ1_MASK, scan_mask);
		writel(regval, ams->pl_base + AMS_REG_SEQ_CH1);

		regval = FIELD_GET(AMS_REG_SEQ2_MASK, scan_mask);
		writel(regval, ams->pl_base + AMS_REG_SEQ_CH2);

		/* set continuous sequence mode */
		ams_pl_update_reg(ams, AMS_REG_CONFIG1, AMS_CONF1_SEQ_MASK,
				  AMS_CONF1_SEQ_CONTINUOUS);
	}
}

static int ams_init_device(struct ams *ams)
{
	u32 expect = AMS_PS_CSTS_PS_READY;
	u32 reg, value;
	int ret;

	/* reset AMS */
	if (ams->ps_base) {
		writel(AMS_PS_RESET_VALUE, ams->ps_base + AMS_VP_VN);

		ret = readl_poll_timeout(ams->base + AMS_PS_CSTS, reg, (reg & expect),
					 AMS_INIT_POLL_TIME_US, AMS_INIT_TIMEOUT_US);
		if (ret)
			return ret;

		/* put sysmon in a default state */
		ams_ps_update_reg(ams, AMS_REG_CONFIG1, AMS_CONF1_SEQ_MASK,
				  AMS_CONF1_SEQ_DEFAULT);
	}

	if (ams->pl_base) {
		value = readl(ams->base + AMS_PL_CSTS);
		if (value == 0)
			return 0;

		writel(AMS_PL_RESET_VALUE, ams->pl_base + AMS_VP_VN);

		/* put sysmon in a default state */
		ams_pl_update_reg(ams, AMS_REG_CONFIG1, AMS_CONF1_SEQ_MASK,
				  AMS_CONF1_SEQ_DEFAULT);
	}

	ams_disable_all_alarms(ams);

	/* Disable interrupt */
	ams_update_intrmask(ams, AMS_ALARM_MASK, AMS_ALARM_MASK);

	/* Clear any pending interrupt */
	writel(AMS_ISR0_ALARM_MASK, ams->base + AMS_ISR_0);
	writel(AMS_ISR1_ALARM_MASK, ams->base + AMS_ISR_1);

	return 0;
}

static int ams_read_label(struct iio_dev *indio_dev,
			  struct iio_chan_spec const *chan, char *label)
{
	return sysfs_emit(label, "%s\n", chan->datasheet_name);
}

static int ams_enable_single_channel(struct ams *ams, unsigned int offset)
{
	u8 channel_num;

	switch (offset) {
	case AMS_VCC_PSPLL0:
		channel_num = AMS_VCC_PSPLL0_CH;
		break;
	case AMS_VCC_PSPLL3:
		channel_num = AMS_VCC_PSPLL3_CH;
		break;
	case AMS_VCCINT:
		channel_num = AMS_VCCINT_CH;
		break;
	case AMS_VCCBRAM:
		channel_num = AMS_VCCBRAM_CH;
		break;
	case AMS_VCCAUX:
		channel_num = AMS_VCCAUX_CH;
		break;
	case AMS_PSDDRPLL:
		channel_num = AMS_PSDDRPLL_CH;
		break;
	case AMS_PSINTFPDDR:
		channel_num = AMS_PSINTFPDDR_CH;
		break;
	default:
		return -EINVAL;
	}

	/* put sysmon in a soft reset to change the sequence */
	ams_ps_update_reg(ams, AMS_REG_CONFIG1, AMS_CONF1_SEQ_MASK,
			  AMS_CONF1_SEQ_DEFAULT);

	/* write the channel number */
	ams_ps_update_reg(ams, AMS_REG_CONFIG0, AMS_CONF0_CHANNEL_NUM_MASK,
			  channel_num);

	/* set single channel, sequencer off mode */
	ams_ps_update_reg(ams, AMS_REG_CONFIG1, AMS_CONF1_SEQ_MASK,
			  AMS_CONF1_SEQ_SINGLE_CHANNEL);

	return 0;
}

static int ams_read_vcc_reg(struct ams *ams, unsigned int offset, u32 *data)
{
	u32 expect = AMS_ISR1_EOC_MASK;
	u32 reg;
	int ret;

	ret = ams_enable_single_channel(ams, offset);
	if (ret)
		return ret;

	/* clear end-of-conversion flag, wait for next conversion to complete */
	writel(expect, ams->base + AMS_ISR_1);
	ret = readl_poll_timeout(ams->base + AMS_ISR_1, reg, (reg & expect),
				 AMS_INIT_POLL_TIME_US, AMS_INIT_TIMEOUT_US);
	if (ret)
		return ret;

	*data = readl(ams->base + offset);

	return 0;
}

static int ams_get_ps_scale(int address)
{
	int val;

	switch (address) {
	case AMS_SUPPLY1:
	case AMS_SUPPLY2:
	case AMS_SUPPLY3:
	case AMS_SUPPLY4:
	case AMS_SUPPLY9:
	case AMS_SUPPLY10:
	case AMS_VCCAMS:
		val = AMS_SUPPLY_SCALE_3VOLT_mV;
		break;
	case AMS_SUPPLY5:
	case AMS_SUPPLY6:
	case AMS_SUPPLY7:
	case AMS_SUPPLY8:
		val = AMS_SUPPLY_SCALE_6VOLT_mV;
		break;
	default:
		val = AMS_SUPPLY_SCALE_1VOLT_mV;
		break;
	}

	return val;
}

static int ams_get_pl_scale(struct ams *ams, int address)
{
	int val, regval;

	switch (address) {
	case AMS_SUPPLY1:
	case AMS_SUPPLY2:
	case AMS_SUPPLY3:
	case AMS_SUPPLY4:
	case AMS_SUPPLY5:
	case AMS_SUPPLY6:
	case AMS_VCCAMS:
	case AMS_VREFP:
	case AMS_VREFN:
		val = AMS_SUPPLY_SCALE_3VOLT_mV;
		break;
	case AMS_SUPPLY7:
		regval = readl(ams->pl_base + AMS_REG_CONFIG4);
		if (FIELD_GET(AMS_VUSER0_MASK, regval))
			val = AMS_SUPPLY_SCALE_6VOLT_mV;
		else
			val = AMS_SUPPLY_SCALE_3VOLT_mV;
		break;
	case AMS_SUPPLY8:
		regval = readl(ams->pl_base + AMS_REG_CONFIG4);
		if (FIELD_GET(AMS_VUSER1_MASK, regval))
			val = AMS_SUPPLY_SCALE_6VOLT_mV;
		else
			val = AMS_SUPPLY_SCALE_3VOLT_mV;
		break;
	case AMS_SUPPLY9:
		regval = readl(ams->pl_base + AMS_REG_CONFIG4);
		if (FIELD_GET(AMS_VUSER2_MASK, regval))
			val = AMS_SUPPLY_SCALE_6VOLT_mV;
		else
			val = AMS_SUPPLY_SCALE_3VOLT_mV;
		break;
	case AMS_SUPPLY10:
		regval = readl(ams->pl_base + AMS_REG_CONFIG4);
		if (FIELD_GET(AMS_VUSER3_MASK, regval))
			val = AMS_SUPPLY_SCALE_6VOLT_mV;
		else
			val = AMS_SUPPLY_SCALE_3VOLT_mV;
		break;
	case AMS_VP_VN:
	case AMS_REG_VAUX(0) ... AMS_REG_VAUX(15):
		val = AMS_SUPPLY_SCALE_1VOLT_mV;
		break;
	default:
		val = AMS_SUPPLY_SCALE_1VOLT_mV;
		break;
	}

	return val;
}

static int ams_get_ctrl_scale(int address)
{
	int val;

	switch (address) {
	case AMS_VCC_PSPLL0:
	case AMS_VCC_PSPLL3:
	case AMS_VCCINT:
	case AMS_VCCBRAM:
	case AMS_VCCAUX:
	case AMS_PSDDRPLL:
	case AMS_PSINTFPDDR:
		val = AMS_SUPPLY_SCALE_3VOLT_mV;
		break;
	default:
		val = AMS_SUPPLY_SCALE_1VOLT_mV;
		break;
	}

	return val;
}

static int ams_read_raw(struct iio_dev *indio_dev,
			struct iio_chan_spec const *chan,
			int *val, int *val2, long mask)
{
	struct ams *ams = iio_priv(indio_dev);
	int ret;

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		mutex_lock(&ams->lock);
		if (chan->scan_index >= AMS_CTRL_SEQ_BASE) {
			ret = ams_read_vcc_reg(ams, chan->address, val);
			if (ret)
				goto unlock_mutex;
			ams_enable_channel_sequence(indio_dev);
		} else if (chan->scan_index >= AMS_PS_SEQ_MAX)
			*val = readl(ams->pl_base + chan->address);
		else
			*val = readl(ams->ps_base + chan->address);

		ret = IIO_VAL_INT;
unlock_mutex:
		mutex_unlock(&ams->lock);
		return ret;
	case IIO_CHAN_INFO_SCALE:
		switch (chan->type) {
		case IIO_VOLTAGE:
			if (chan->scan_index < AMS_PS_SEQ_MAX)
				*val = ams_get_ps_scale(chan->address);
			else if (chan->scan_index >= AMS_PS_SEQ_MAX &&
				 chan->scan_index < AMS_CTRL_SEQ_BASE)
				*val = ams_get_pl_scale(ams, chan->address);
			else
				*val = ams_get_ctrl_scale(chan->address);

			*val2 = AMS_SUPPLY_SCALE_DIV_BIT;
			return IIO_VAL_FRACTIONAL_LOG2;
		case IIO_TEMP:
			*val = AMS_TEMP_SCALE;
			*val2 = AMS_TEMP_SCALE_DIV_BIT;
			return IIO_VAL_FRACTIONAL_LOG2;
		default:
			return -EINVAL;
		}
	case IIO_CHAN_INFO_OFFSET:
		/* Only the temperature channel has an offset */
		*val = AMS_TEMP_OFFSET;
		return IIO_VAL_INT;
	default:
		return -EINVAL;
	}
}

static int ams_get_alarm_offset(int scan_index, enum iio_event_direction dir)
{
	int offset;

	if (scan_index >= AMS_PS_SEQ_MAX)
		scan_index -= AMS_PS_SEQ_MAX;

	if (dir == IIO_EV_DIR_FALLING) {
		if (scan_index < AMS_SEQ_SUPPLY7)
			offset = AMS_ALARM_THRESHOLD_OFF_10;
		else
			offset = AMS_ALARM_THRESHOLD_OFF_20;
	} else {
		offset = 0;
	}

	switch (scan_index) {
	case AMS_SEQ_TEMP:
		return AMS_ALARM_TEMP + offset;
	case AMS_SEQ_SUPPLY1:
		return AMS_ALARM_SUPPLY1 + offset;
	case AMS_SEQ_SUPPLY2:
		return AMS_ALARM_SUPPLY2 + offset;
	case AMS_SEQ_SUPPLY3:
		return AMS_ALARM_SUPPLY3 + offset;
	case AMS_SEQ_SUPPLY4:
		return AMS_ALARM_SUPPLY4 + offset;
	case AMS_SEQ_SUPPLY5:
		return AMS_ALARM_SUPPLY5 + offset;
	case AMS_SEQ_SUPPLY6:
		return AMS_ALARM_SUPPLY6 + offset;
	case AMS_SEQ_SUPPLY7:
		return AMS_ALARM_SUPPLY7 + offset;
	case AMS_SEQ_SUPPLY8:
		return AMS_ALARM_SUPPLY8 + offset;
	case AMS_SEQ_SUPPLY9:
		return AMS_ALARM_SUPPLY9 + offset;
	case AMS_SEQ_SUPPLY10:
		return AMS_ALARM_SUPPLY10 + offset;
	case AMS_SEQ_VCCAMS:
		return AMS_ALARM_VCCAMS + offset;
	case AMS_SEQ_TEMP_REMOTE:
		return AMS_ALARM_TEMP_REMOTE + offset;
	default:
		return 0;
	}
}

static const struct iio_chan_spec *ams_event_to_channel(struct iio_dev *dev,
							u32 event)
{
	int scan_index = 0, i;

	if (event >= AMS_PL_ALARM_START) {
		event -= AMS_PL_ALARM_START;
		scan_index = AMS_PS_SEQ_MAX;
	}

	switch (event) {
	case AMS_ALARM_BIT_TEMP:
		scan_index += AMS_SEQ_TEMP;
		break;
	case AMS_ALARM_BIT_SUPPLY1:
		scan_index += AMS_SEQ_SUPPLY1;
		break;
	case AMS_ALARM_BIT_SUPPLY2:
		scan_index += AMS_SEQ_SUPPLY2;
		break;
	case AMS_ALARM_BIT_SUPPLY3:
		scan_index += AMS_SEQ_SUPPLY3;
		break;
	case AMS_ALARM_BIT_SUPPLY4:
		scan_index += AMS_SEQ_SUPPLY4;
		break;
	case AMS_ALARM_BIT_SUPPLY5:
		scan_index += AMS_SEQ_SUPPLY5;
		break;
	case AMS_ALARM_BIT_SUPPLY6:
		scan_index += AMS_SEQ_SUPPLY6;
		break;
	case AMS_ALARM_BIT_SUPPLY7:
		scan_index += AMS_SEQ_SUPPLY7;
		break;
	case AMS_ALARM_BIT_SUPPLY8:
		scan_index += AMS_SEQ_SUPPLY8;
		break;
	case AMS_ALARM_BIT_SUPPLY9:
		scan_index += AMS_SEQ_SUPPLY9;
		break;
	case AMS_ALARM_BIT_SUPPLY10:
		scan_index += AMS_SEQ_SUPPLY10;
		break;
	case AMS_ALARM_BIT_VCCAMS:
		scan_index += AMS_SEQ_VCCAMS;
		break;
	case AMS_ALARM_BIT_TEMP_REMOTE:
		scan_index += AMS_SEQ_TEMP_REMOTE;
		break;
	default:
		break;
	}

	for (i = 0; i < dev->num_channels; i++)
		if (dev->channels[i].scan_index == scan_index)
			break;

	return &dev->channels[i];
}

static int ams_get_alarm_mask(int scan_index)
{
	int bit = 0;

	if (scan_index >= AMS_PS_SEQ_MAX) {
		bit = AMS_PL_ALARM_START;
		scan_index -= AMS_PS_SEQ_MAX;
	}

	switch (scan_index) {
	case AMS_SEQ_TEMP:
		return BIT(AMS_ALARM_BIT_TEMP + bit);
	case AMS_SEQ_SUPPLY1:
		return BIT(AMS_ALARM_BIT_SUPPLY1 + bit);
	case AMS_SEQ_SUPPLY2:
		return BIT(AMS_ALARM_BIT_SUPPLY2 + bit);
	case AMS_SEQ_SUPPLY3:
		return BIT(AMS_ALARM_BIT_SUPPLY3 + bit);
	case AMS_SEQ_SUPPLY4:
		return BIT(AMS_ALARM_BIT_SUPPLY4 + bit);
	case AMS_SEQ_SUPPLY5:
		return BIT(AMS_ALARM_BIT_SUPPLY5 + bit);
	case AMS_SEQ_SUPPLY6:
		return BIT(AMS_ALARM_BIT_SUPPLY6 + bit);
	case AMS_SEQ_SUPPLY7:
		return BIT(AMS_ALARM_BIT_SUPPLY7 + bit);
	case AMS_SEQ_SUPPLY8:
		return BIT(AMS_ALARM_BIT_SUPPLY8 + bit);
	case AMS_SEQ_SUPPLY9:
		return BIT(AMS_ALARM_BIT_SUPPLY9 + bit);
	case AMS_SEQ_SUPPLY10:
		return BIT(AMS_ALARM_BIT_SUPPLY10 + bit);
	case AMS_SEQ_VCCAMS:
		return BIT(AMS_ALARM_BIT_VCCAMS + bit);
	case AMS_SEQ_TEMP_REMOTE:
		return BIT(AMS_ALARM_BIT_TEMP_REMOTE + bit);
	default:
		return 0;
	}
}

static int ams_read_event_config(struct iio_dev *indio_dev,
				 const struct iio_chan_spec *chan,
				 enum iio_event_type type,
				 enum iio_event_direction dir)
{
	struct ams *ams = iio_priv(indio_dev);

	return !!(ams->alarm_mask & ams_get_alarm_mask(chan->scan_index));
}

static int ams_write_event_config(struct iio_dev *indio_dev,
				  const struct iio_chan_spec *chan,
				  enum iio_event_type type,
				  enum iio_event_direction dir,
				  bool state)
{
	struct ams *ams = iio_priv(indio_dev);
	unsigned int alarm;

	alarm = ams_get_alarm_mask(chan->scan_index);

	mutex_lock(&ams->lock);

	if (state)
		ams->alarm_mask |= alarm;
	else
		ams->alarm_mask &= ~alarm;

	ams_update_alarm(ams, ams->alarm_mask);

	mutex_unlock(&ams->lock);

	return 0;
}

static int ams_read_event_value(struct iio_dev *indio_dev,
				const struct iio_chan_spec *chan,
				enum iio_event_type type,
				enum iio_event_direction dir,
				enum iio_event_info info, int *val, int *val2)
{
	struct ams *ams = iio_priv(indio_dev);
	unsigned int offset = ams_get_alarm_offset(chan->scan_index, dir);

	mutex_lock(&ams->lock);

	if (chan->scan_index >= AMS_PS_SEQ_MAX)
		*val = readl(ams->pl_base + offset);
	else
		*val = readl(ams->ps_base + offset);

	mutex_unlock(&ams->lock);

	return IIO_VAL_INT;
}

static int ams_write_event_value(struct iio_dev *indio_dev,
				 const struct iio_chan_spec *chan,
				 enum iio_event_type type,
				 enum iio_event_direction dir,
				 enum iio_event_info info, int val, int val2)
{
	struct ams *ams = iio_priv(indio_dev);
	unsigned int offset;

	mutex_lock(&ams->lock);

	/* Set temperature channel threshold to direct threshold */
	if (chan->type == IIO_TEMP) {
		offset = ams_get_alarm_offset(chan->scan_index, IIO_EV_DIR_FALLING);

		if (chan->scan_index >= AMS_PS_SEQ_MAX)
			ams_pl_update_reg(ams, offset,
					  AMS_ALARM_THR_DIRECT_MASK,
					  AMS_ALARM_THR_DIRECT_MASK);
		else
			ams_ps_update_reg(ams, offset,
					  AMS_ALARM_THR_DIRECT_MASK,
					  AMS_ALARM_THR_DIRECT_MASK);
	}

	offset = ams_get_alarm_offset(chan->scan_index, dir);
	if (chan->scan_index >= AMS_PS_SEQ_MAX)
		writel(val, ams->pl_base + offset);
	else
		writel(val, ams->ps_base + offset);

	mutex_unlock(&ams->lock);

	return 0;
}

static void ams_handle_event(struct iio_dev *indio_dev, u32 event)
{
	const struct iio_chan_spec *chan;

	chan = ams_event_to_channel(indio_dev, event);

	if (chan->type == IIO_TEMP) {
		/*
		 * The temperature channel only supports over-temperature
		 * events.
		 */
		iio_push_event(indio_dev,
			       IIO_UNMOD_EVENT_CODE(chan->type, chan->channel,
						    IIO_EV_TYPE_THRESH,
						    IIO_EV_DIR_RISING),
			       iio_get_time_ns(indio_dev));
	} else {
		/*
		 * For other channels we don't know whether it is a upper or
		 * lower threshold event. Userspace will have to check the
		 * channel value if it wants to know.
		 */
		iio_push_event(indio_dev,
			       IIO_UNMOD_EVENT_CODE(chan->type, chan->channel,
						    IIO_EV_TYPE_THRESH,
						    IIO_EV_DIR_EITHER),
			       iio_get_time_ns(indio_dev));
	}
}

static void ams_handle_events(struct iio_dev *indio_dev, unsigned long events)
{
	unsigned int bit;

	for_each_set_bit(bit, &events, AMS_NO_OF_ALARMS)
		ams_handle_event(indio_dev, bit);
}

/**
 * ams_unmask_worker - ams alarm interrupt unmask worker
 * @work: work to be done
 *
 * The ZynqMP threshold interrupts are level sensitive. Since we can't make the
 * threshold condition go way from within the interrupt handler, this means as
 * soon as a threshold condition is present we would enter the interrupt handler
 * again and again. To work around this we mask all active threshold interrupts
 * in the interrupt handler and start a timer. In this timer we poll the
 * interrupt status and only if the interrupt is inactive we unmask it again.
 */
static void ams_unmask_worker(struct work_struct *work)
{
	struct ams *ams = container_of(work, struct ams, ams_unmask_work.work);
	unsigned int status, unmask;

	spin_lock_irq(&ams->intr_lock);

	status = readl(ams->base + AMS_ISR_0);

	/* Clear those bits which are not active anymore */
	unmask = (ams->current_masked_alarm ^ status) & ams->current_masked_alarm;

	/* Clear status of disabled alarm */
	unmask |= ams->intr_mask;

	ams->current_masked_alarm &= status;

	/* Also clear those which are masked out anyway */
	ams->current_masked_alarm &= ~ams->intr_mask;

	/* Clear the interrupts before we unmask them */
	writel(unmask, ams->base + AMS_ISR_0);

	ams_update_intrmask(ams, ~AMS_ALARM_MASK, ~AMS_ALARM_MASK);

	spin_unlock_irq(&ams->intr_lock);

	/* If still pending some alarm re-trigger the timer */
	if (ams->current_masked_alarm)
		schedule_delayed_work(&ams->ams_unmask_work,
				      msecs_to_jiffies(AMS_UNMASK_TIMEOUT_MS));
}

static irqreturn_t ams_irq(int irq, void *data)
{
	struct iio_dev *indio_dev = data;
	struct ams *ams = iio_priv(indio_dev);
	u32 isr0;

	spin_lock(&ams->intr_lock);

	isr0 = readl(ams->base + AMS_ISR_0);

	/* Only process alarms that are not masked */
	isr0 &= ~((ams->intr_mask & AMS_ISR0_ALARM_MASK) | ams->current_masked_alarm);
	if (!isr0) {
		spin_unlock(&ams->intr_lock);
		return IRQ_NONE;
	}

	/* Clear interrupt */
	writel(isr0, ams->base + AMS_ISR_0);

	/* Mask the alarm interrupts until cleared */
	ams->current_masked_alarm |= isr0;
	ams_update_intrmask(ams, ~AMS_ALARM_MASK, ~AMS_ALARM_MASK);

	ams_handle_events(indio_dev, isr0);

	schedule_delayed_work(&ams->ams_unmask_work,
			      msecs_to_jiffies(AMS_UNMASK_TIMEOUT_MS));

	spin_unlock(&ams->intr_lock);

	return IRQ_HANDLED;
}

static const struct iio_event_spec ams_temp_events[] = {
	{
		.type = IIO_EV_TYPE_THRESH,
		.dir = IIO_EV_DIR_RISING,
		.mask_separate = BIT(IIO_EV_INFO_ENABLE) | BIT(IIO_EV_INFO_VALUE),
	},
};

static const struct iio_event_spec ams_voltage_events[] = {
	{
		.type = IIO_EV_TYPE_THRESH,
		.dir = IIO_EV_DIR_RISING,
		.mask_separate = BIT(IIO_EV_INFO_VALUE),
	},
	{
		.type = IIO_EV_TYPE_THRESH,
		.dir = IIO_EV_DIR_FALLING,
		.mask_separate = BIT(IIO_EV_INFO_VALUE),
	},
	{
		.type = IIO_EV_TYPE_THRESH,
		.dir = IIO_EV_DIR_EITHER,
		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
	},
};

static const struct iio_chan_spec ams_ps_channels[] = {
	AMS_PS_CHAN_TEMP(AMS_SEQ_TEMP, AMS_TEMP, "Temp_LPD"),
	AMS_PS_CHAN_TEMP(AMS_SEQ_TEMP_REMOTE, AMS_TEMP_REMOTE, "Temp_FPD"),
	AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY1, AMS_SUPPLY1, "VCC_PSINTLP"),
	AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY2, AMS_SUPPLY2, "VCC_PSINTFP"),
	AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY3, AMS_SUPPLY3, "VCC_PSAUX"),
	AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY4, AMS_SUPPLY4, "VCC_PSDDR"),
	AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY5, AMS_SUPPLY5, "VCC_PSIO3"),
	AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY6, AMS_SUPPLY6, "VCC_PSIO0"),
	AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY7, AMS_SUPPLY7, "VCC_PSIO1"),
	AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY8, AMS_SUPPLY8, "VCC_PSIO2"),
	AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY9, AMS_SUPPLY9, "PS_MGTRAVCC"),
	AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY10, AMS_SUPPLY10, "PS_MGTRAVTT"),
	AMS_PS_CHAN_VOLTAGE(AMS_SEQ_VCCAMS, AMS_VCCAMS, "VCC_PSADC"),
};

static const struct iio_chan_spec ams_pl_channels[] = {
	AMS_PL_CHAN_TEMP(AMS_SEQ_TEMP, AMS_TEMP, "Temp_PL"),
	AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY1, AMS_SUPPLY1, true, "VCCINT"),
	AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY2, AMS_SUPPLY2, true, "VCCAUX"),
	AMS_PL_CHAN_VOLTAGE(AMS_SEQ_VREFP, AMS_VREFP, false, "VREFP"),
	AMS_PL_CHAN_VOLTAGE(AMS_SEQ_VREFN, AMS_VREFN, false, "VREFN"),
	AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY3, AMS_SUPPLY3, true, "VCCBRAM"),
	AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY4, AMS_SUPPLY4, true, "VCC_PSINTLP"),
	AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY5, AMS_SUPPLY5, true, "VCC_PSINTFP"),
	AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY6, AMS_SUPPLY6, true, "VCC_PSAUX"),
	AMS_PL_CHAN_VOLTAGE(AMS_SEQ_VCCAMS, AMS_VCCAMS, true, "VCCAMS"),
	AMS_PL_CHAN_VOLTAGE(AMS_SEQ_VP_VN, AMS_VP_VN, false, "VP_VN"),
	AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY7, AMS_SUPPLY7, true, "VUser0"),
	AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY8, AMS_SUPPLY8, true, "VUser1"),
	AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY9, AMS_SUPPLY9, true, "VUser2"),
	AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY10, AMS_SUPPLY10, true, "VUser3"),
	AMS_PL_AUX_CHAN_VOLTAGE(0),
	AMS_PL_AUX_CHAN_VOLTAGE(1),
	AMS_PL_AUX_CHAN_VOLTAGE(2),
	AMS_PL_AUX_CHAN_VOLTAGE(3),
	AMS_PL_AUX_CHAN_VOLTAGE(4),
	AMS_PL_AUX_CHAN_VOLTAGE(5),
	AMS_PL_AUX_CHAN_VOLTAGE(6),
	AMS_PL_AUX_CHAN_VOLTAGE(7),
	AMS_PL_AUX_CHAN_VOLTAGE(8),
	AMS_PL_AUX_CHAN_VOLTAGE(9),
	AMS_PL_AUX_CHAN_VOLTAGE(10),
	AMS_PL_AUX_CHAN_VOLTAGE(11),
	AMS_PL_AUX_CHAN_VOLTAGE(12),
	AMS_PL_AUX_CHAN_VOLTAGE(13),
	AMS_PL_AUX_CHAN_VOLTAGE(14),
	AMS_PL_AUX_CHAN_VOLTAGE(15),
};

static const struct iio_chan_spec ams_ctrl_channels[] = {
	AMS_CTRL_CHAN_VOLTAGE(AMS_SEQ_VCC_PSPLL, AMS_VCC_PSPLL0, "VCC_PSPLL"),
	AMS_CTRL_CHAN_VOLTAGE(AMS_SEQ_VCC_PSBATT, AMS_VCC_PSPLL3, "VCC_PSBATT"),
	AMS_CTRL_CHAN_VOLTAGE(AMS_SEQ_VCCINT, AMS_VCCINT, "VCCINT"),
	AMS_CTRL_CHAN_VOLTAGE(AMS_SEQ_VCCBRAM, AMS_VCCBRAM, "VCCBRAM"),
	AMS_CTRL_CHAN_VOLTAGE(AMS_SEQ_VCCAUX, AMS_VCCAUX, "VCCAUX"),
	AMS_CTRL_CHAN_VOLTAGE(AMS_SEQ_PSDDRPLL, AMS_PSDDRPLL, "VCC_PSDDR_PLL"),
	AMS_CTRL_CHAN_VOLTAGE(AMS_SEQ_INTDDR, AMS_PSINTFPDDR, "VCC_PSINTFP_DDR"),
};

static int ams_get_ext_chan(struct fwnode_handle *chan_node,
			    struct iio_chan_spec *channels, int num_channels)
{
	struct iio_chan_spec *chan;
	struct fwnode_handle *child;
	unsigned int reg, ext_chan;
	int ret;

	fwnode_for_each_child_node(chan_node, child) {
		ret = fwnode_property_read_u32(child, "reg", &reg);
		if (ret || reg > AMS_PL_MAX_EXT_CHANNEL + 30)
			continue;

		chan = &channels[num_channels];
		ext_chan = reg + AMS_PL_MAX_FIXED_CHANNEL - 30;
		memcpy(chan, &ams_pl_channels[ext_chan], sizeof(*channels));

		if (fwnode_property_read_bool(child, "xlnx,bipolar"))
			chan->scan_type.sign = 's';

		num_channels++;
	}

	return num_channels;
}

static void ams_iounmap_ps(void *data)
{
	struct ams *ams = data;

	iounmap(ams->ps_base);
}

static void ams_iounmap_pl(void *data)
{
	struct ams *ams = data;

	iounmap(ams->pl_base);
}

static int ams_init_module(struct iio_dev *indio_dev,
			   struct fwnode_handle *fwnode,
			   struct iio_chan_spec *channels)
{
	struct device *dev = indio_dev->dev.parent;
	struct ams *ams = iio_priv(indio_dev);
	int num_channels = 0;
	int ret;

	if (fwnode_device_is_compatible(fwnode, "xlnx,zynqmp-ams-ps")) {
		ams->ps_base = fwnode_iomap(fwnode, 0);
		if (!ams->ps_base)
			return -ENXIO;
		ret = devm_add_action_or_reset(dev, ams_iounmap_ps, ams);
		if (ret < 0)
			return ret;

		/* add PS channels to iio device channels */
		memcpy(channels, ams_ps_channels, sizeof(ams_ps_channels));
		num_channels = ARRAY_SIZE(ams_ps_channels);
	} else if (fwnode_device_is_compatible(fwnode, "xlnx,zynqmp-ams-pl")) {
		ams->pl_base = fwnode_iomap(fwnode, 0);
		if (!ams->pl_base)
			return -ENXIO;

		ret = devm_add_action_or_reset(dev, ams_iounmap_pl, ams);
		if (ret < 0)
			return ret;

		/* Copy only first 10 fix channels */
		memcpy(channels, ams_pl_channels, AMS_PL_MAX_FIXED_CHANNEL * sizeof(*channels));
		num_channels += AMS_PL_MAX_FIXED_CHANNEL;
		num_channels = ams_get_ext_chan(fwnode, channels,
						num_channels);
	} else if (fwnode_device_is_compatible(fwnode, "xlnx,zynqmp-ams")) {
		/* add AMS channels to iio device channels */
		memcpy(channels, ams_ctrl_channels, sizeof(ams_ctrl_channels));
		num_channels += ARRAY_SIZE(ams_ctrl_channels);
	} else {
		return -EINVAL;
	}

	return num_channels;
}

static int ams_parse_firmware(struct iio_dev *indio_dev)
{
	struct ams *ams = iio_priv(indio_dev);
	struct iio_chan_spec *ams_channels, *dev_channels;
	struct device *dev = indio_dev->dev.parent;
	struct fwnode_handle *fwnode = dev_fwnode(dev);
	size_t ams_size;
	int ret, ch_cnt = 0, i, rising_off, falling_off;
	unsigned int num_channels = 0;

	ams_size = ARRAY_SIZE(ams_ps_channels) + ARRAY_SIZE(ams_pl_channels) +
		ARRAY_SIZE(ams_ctrl_channels);

	/* Initialize buffer for channel specification */
	ams_channels = devm_kcalloc(dev, ams_size, sizeof(*ams_channels), GFP_KERNEL);
	if (!ams_channels)
		return -ENOMEM;

	if (fwnode_device_is_available(fwnode)) {
		ret = ams_init_module(indio_dev, fwnode, ams_channels);
		if (ret < 0)
			return ret;

		num_channels += ret;
	}

	device_for_each_child_node_scoped(dev, child) {
		ret = ams_init_module(indio_dev, child, ams_channels + num_channels);
		if (ret < 0)
			return ret;

		num_channels += ret;
	}

	for (i = 0; i < num_channels; i++) {
		ams_channels[i].channel = ch_cnt++;

		if (ams_channels[i].scan_index < AMS_CTRL_SEQ_BASE) {
			/* set threshold to max and min for each channel */
			falling_off =
				ams_get_alarm_offset(ams_channels[i].scan_index,
						     IIO_EV_DIR_FALLING);
			rising_off =
				ams_get_alarm_offset(ams_channels[i].scan_index,
						     IIO_EV_DIR_RISING);
			if (ams_channels[i].scan_index >= AMS_PS_SEQ_MAX) {
				writel(AMS_ALARM_THR_MIN,
				       ams->pl_base + falling_off);
				writel(AMS_ALARM_THR_MAX,
				       ams->pl_base + rising_off);
			} else {
				writel(AMS_ALARM_THR_MIN,
				       ams->ps_base + falling_off);
				writel(AMS_ALARM_THR_MAX,
				       ams->ps_base + rising_off);
			}
		}
	}

	dev_channels = devm_krealloc_array(dev, ams_channels, num_channels,
					   sizeof(*dev_channels), GFP_KERNEL);
	if (!dev_channels)
		return -ENOMEM;

	indio_dev->channels = dev_channels;
	indio_dev->num_channels = num_channels;

	return 0;
}

static const struct iio_info iio_ams_info = {
	.read_label = ams_read_label,
	.read_raw = &ams_read_raw,
	.read_event_config = &ams_read_event_config,
	.write_event_config = &ams_write_event_config,
	.read_event_value = &ams_read_event_value,
	.write_event_value = &ams_write_event_value,
};

static const struct of_device_id ams_of_match_table[] = {
	{ .compatible = "xlnx,zynqmp-ams" },
	{ }
};
MODULE_DEVICE_TABLE(of, ams_of_match_table);

static int ams_probe(struct platform_device *pdev)
{
	struct iio_dev *indio_dev;
	struct ams *ams;
	int ret;
	int irq;

	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*ams));
	if (!indio_dev)
		return -ENOMEM;

	ams = iio_priv(indio_dev);
	mutex_init(&ams->lock);
	spin_lock_init(&ams->intr_lock);

	indio_dev->name = "xilinx-ams";

	indio_dev->info = &iio_ams_info;
	indio_dev->modes = INDIO_DIRECT_MODE;

	ams->base = devm_platform_ioremap_resource(pdev, 0);
	if (IS_ERR(ams->base))
		return PTR_ERR(ams->base);

	ams->clk = devm_clk_get_enabled(&pdev->dev, NULL);
	if (IS_ERR(ams->clk))
		return PTR_ERR(ams->clk);

	ret = devm_delayed_work_autocancel(&pdev->dev, &ams->ams_unmask_work,
					   ams_unmask_worker);
	if (ret < 0)
		return ret;

	ret = ams_parse_firmware(indio_dev);
	if (ret)
		return dev_err_probe(&pdev->dev, ret, "failure in parsing DT\n");

	ret = ams_init_device(ams);
	if (ret)
		return dev_err_probe(&pdev->dev, ret, "failed to initialize AMS\n");

	ams_enable_channel_sequence(indio_dev);

	irq = platform_get_irq(pdev, 0);
	if (irq < 0)
		return irq;

	ret = devm_request_irq(&pdev->dev, irq, &ams_irq, 0, "ams-irq",
			       indio_dev);
	if (ret < 0)
		return dev_err_probe(&pdev->dev, ret, "failed to register interrupt\n");

	platform_set_drvdata(pdev, indio_dev);

	return devm_iio_device_register(&pdev->dev, indio_dev);
}

static int ams_suspend(struct device *dev)
{
	struct ams *ams = iio_priv(dev_get_drvdata(dev));

	clk_disable_unprepare(ams->clk);

	return 0;
}

static int ams_resume(struct device *dev)
{
	struct ams *ams = iio_priv(dev_get_drvdata(dev));

	return clk_prepare_enable(ams->clk);
}

static DEFINE_SIMPLE_DEV_PM_OPS(ams_pm_ops, ams_suspend, ams_resume);

static struct platform_driver ams_driver = {
	.probe = ams_probe,
	.driver = {
		.name = "xilinx-ams",
		.pm = pm_sleep_ptr(&ams_pm_ops),
		.of_match_table = ams_of_match_table,
	},
};
module_platform_driver(ams_driver);

MODULE_DESCRIPTION("Xilinx AMS driver");
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Xilinx, Inc.");