summaryrefslogtreecommitdiff
path: root/drivers/iio/frequency/adf4377.c
blob: 26abecbd51e06611b23f3d54fc09d32430216de4 (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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * ADF4377 driver
 *
 * Copyright 2022 Analog Devices Inc.
 */

#include <linux/bitfield.h>
#include <linux/bits.h>
#include <linux/clk.h>
#include <linux/clkdev.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/gpio/consumer.h>
#include <linux/module.h>
#include <linux/notifier.h>
#include <linux/property.h>
#include <linux/spi/spi.h>
#include <linux/iio/iio.h>
#include <linux/regmap.h>
#include <linux/units.h>

#include <asm/unaligned.h>

/* ADF4377 REG0000 Map */
#define ADF4377_0000_SOFT_RESET_R_MSK		BIT(7)
#define ADF4377_0000_LSB_FIRST_R_MSK		BIT(6)
#define ADF4377_0000_ADDRESS_ASC_R_MSK		BIT(5)
#define ADF4377_0000_SDO_ACTIVE_R_MSK		BIT(4)
#define ADF4377_0000_SDO_ACTIVE_MSK		BIT(3)
#define ADF4377_0000_ADDRESS_ASC_MSK		BIT(2)
#define ADF4377_0000_LSB_FIRST_MSK		BIT(1)
#define ADF4377_0000_SOFT_RESET_MSK		BIT(0)

/* ADF4377 REG0000 Bit Definition */
#define ADF4377_0000_SDO_ACTIVE_SPI_3W		0x0
#define ADF4377_0000_SDO_ACTIVE_SPI_4W		0x1

#define ADF4377_0000_ADDR_ASC_AUTO_DECR		0x0
#define ADF4377_0000_ADDR_ASC_AUTO_INCR		0x1

#define ADF4377_0000_LSB_FIRST_MSB		0x0
#define ADF4377_0000_LSB_FIRST_LSB		0x1

#define ADF4377_0000_SOFT_RESET_N_OP		0x0
#define ADF4377_0000_SOFT_RESET_EN		0x1

/* ADF4377 REG0001 Map */
#define ADF4377_0001_SINGLE_INSTR_MSK		BIT(7)
#define ADF4377_0001_MASTER_RB_CTRL_MSK		BIT(5)

/* ADF4377 REG0003 Bit Definition */
#define ADF4377_0003_CHIP_TYPE			0x06

/* ADF4377 REG0004 Bit Definition */
#define ADF4377_0004_PRODUCT_ID_LSB		0x0005

/* ADF4377 REG0005 Bit Definition */
#define ADF4377_0005_PRODUCT_ID_MSB		0x0005

/* ADF4377 REG000A Map */
#define ADF4377_000A_SCRATCHPAD_MSK		GENMASK(7, 0)

/* ADF4377 REG000C Bit Definition */
#define ADF4377_000C_VENDOR_ID_LSB		0x56

/* ADF4377 REG000D Bit Definition */
#define ADF4377_000D_VENDOR_ID_MSB		0x04

/* ADF4377 REG000F Bit Definition */
#define ADF4377_000F_R00F_RSV1_MSK		GENMASK(7, 0)

/* ADF4377 REG0010 Map*/
#define ADF4377_0010_N_INT_LSB_MSK		GENMASK(7, 0)

/* ADF4377 REG0011 Map*/
#define ADF4377_0011_EN_AUTOCAL_MSK		BIT(7)
#define ADF4377_0011_EN_RDBLR_MSK		BIT(6)
#define ADF4377_0011_DCLK_DIV2_MSK		GENMASK(5, 4)
#define ADF4377_0011_N_INT_MSB_MSK		GENMASK(3, 0)

/* ADF4377 REG0011 Bit Definition */
#define ADF4377_0011_DCLK_DIV2_1		0x0
#define ADF4377_0011_DCLK_DIV2_2		0x1
#define ADF4377_0011_DCLK_DIV2_4		0x2
#define ADF4377_0011_DCLK_DIV2_8		0x3

/* ADF4377 REG0012 Map*/
#define ADF4377_0012_CLKOUT_DIV_MSK		GENMASK(7, 6)
#define ADF4377_0012_R_DIV_MSK			GENMASK(5, 0)

/* ADF4377 REG0012 Bit Definition */
#define ADF4377_0012_CLKOUT_DIV_1		0x0
#define ADF4377_0012_CLKOUT_DIV_2		0x1
#define ADF4377_0012_CLKOUT_DIV_4		0x2
#define ADF4377_0012_CLKOUT_DIV_8		0x3

/* ADF4377 REG0013 Map */
#define ADF4377_0013_M_VCO_CORE_MSK		GENMASK(5, 4)
#define ADF4377_0013_VCO_BIAS_MSK		GENMASK(3, 0)

/* ADF4377 REG0013 Bit Definition */
#define ADF4377_0013_M_VCO_0			0x0
#define ADF4377_0013_M_VCO_1			0x1
#define ADF4377_0013_M_VCO_2			0x2
#define ADF4377_0013_M_VCO_3			0x3

/* ADF4377 REG0014 Map */
#define ADF4377_0014_M_VCO_BAND_MSK		GENMASK(7, 0)

/* ADF4377 REG0015 Map */
#define ADF4377_0015_BLEED_I_LSB_MSK		GENMASK(7, 6)
#define ADF4377_0015_BLEED_POL_MSK		BIT(5)
#define ADF4377_0015_EN_BLEED_MSK		BIT(4)
#define ADF4377_0015_CP_I_MSK			GENMASK(3, 0)

/* ADF4377 REG0015 Bit Definition */
#define ADF4377_CURRENT_SINK			0x0
#define ADF4377_CURRENT_SOURCE			0x1

#define ADF4377_0015_CP_0MA7			0x0
#define ADF4377_0015_CP_0MA9			0x1
#define ADF4377_0015_CP_1MA1			0x2
#define ADF4377_0015_CP_1MA3			0x3
#define ADF4377_0015_CP_1MA4			0x4
#define ADF4377_0015_CP_1MA8			0x5
#define ADF4377_0015_CP_2MA2			0x6
#define ADF4377_0015_CP_2MA5			0x7
#define ADF4377_0015_CP_2MA9			0x8
#define ADF4377_0015_CP_3MA6			0x9
#define ADF4377_0015_CP_4MA3			0xA
#define ADF4377_0015_CP_5MA0			0xB
#define ADF4377_0015_CP_5MA7			0xC
#define ADF4377_0015_CP_7MA2			0xD
#define ADF4377_0015_CP_8MA6			0xE
#define ADF4377_0015_CP_10MA1			0xF

/* ADF4377 REG0016 Map */
#define ADF4377_0016_BLEED_I_MSB_MSK		GENMASK(7, 0)

/* ADF4377 REG0017 Map */
#define ADF4377_0016_INV_CLKOUT_MSK		BIT(7)
#define ADF4377_0016_N_DEL_MSK			GENMASK(6, 0)

/* ADF4377 REG0018 Map */
#define ADF4377_0018_CMOS_OV_MSK		BIT(7)
#define ADF4377_0018_R_DEL_MSK			GENMASK(6, 0)

/* ADF4377 REG0018 Bit Definition */
#define ADF4377_0018_1V8_LOGIC			0x0
#define ADF4377_0018_3V3_LOGIC			0x1

/* ADF4377 REG0019 Map */
#define ADF4377_0019_CLKOUT2_OP_MSK		GENMASK(7, 6)
#define ADF4377_0019_CLKOUT1_OP_MSK		GENMASK(5, 4)
#define ADF4377_0019_PD_CLK_MSK			BIT(3)
#define ADF4377_0019_PD_RDET_MSK		BIT(2)
#define ADF4377_0019_PD_ADC_MSK			BIT(1)
#define ADF4377_0019_PD_CALADC_MSK		BIT(0)

/* ADF4377 REG0019 Bit Definition */
#define ADF4377_0019_CLKOUT_320MV		0x0
#define ADF4377_0019_CLKOUT_420MV		0x1
#define ADF4377_0019_CLKOUT_530MV		0x2
#define ADF4377_0019_CLKOUT_640MV		0x3

/* ADF4377 REG001A Map */
#define ADF4377_001A_PD_ALL_MSK			BIT(7)
#define ADF4377_001A_PD_RDIV_MSK		BIT(6)
#define ADF4377_001A_PD_NDIV_MSK		BIT(5)
#define ADF4377_001A_PD_VCO_MSK			BIT(4)
#define ADF4377_001A_PD_LD_MSK			BIT(3)
#define ADF4377_001A_PD_PFDCP_MSK		BIT(2)
#define ADF4377_001A_PD_CLKOUT1_MSK		BIT(1)
#define ADF4377_001A_PD_CLKOUT2_MSK		BIT(0)

/* ADF4377 REG001B Map */
#define ADF4377_001B_EN_LOL_MSK			BIT(7)
#define ADF4377_001B_LDWIN_PW_MSK		BIT(6)
#define ADF4377_001B_EN_LDWIN_MSK		BIT(5)
#define ADF4377_001B_LD_COUNT_MSK		GENMASK(4, 0)

/* ADF4377 REG001B Bit Definition */
#define ADF4377_001B_LDWIN_PW_NARROW		0x0
#define ADF4377_001B_LDWIN_PW_WIDE		0x1

/* ADF4377 REG001C Map */
#define ADF4377_001C_EN_DNCLK_MSK		BIT(7)
#define ADF4377_001C_EN_DRCLK_MSK		BIT(6)
#define ADF4377_001C_RST_LD_MSK			BIT(2)
#define ADF4377_001C_R01C_RSV1_MSK		BIT(0)

/* ADF4377 REG001C Bit Definition */
#define ADF4377_001C_RST_LD_INACTIVE		0x0
#define ADF4377_001C_RST_LD_ACTIVE		0x1

#define ADF4377_001C_R01C_RSV1			0x1

/* ADF4377 REG001D Map */
#define ADF4377_001D_MUXOUT_MSK			GENMASK(7, 4)
#define ADF4377_001D_EN_CPTEST_MSK		BIT(2)
#define ADF4377_001D_CP_DOWN_MSK		BIT(1)
#define ADF4377_001D_CP_UP_MSK			BIT(0)

#define ADF4377_001D_EN_CPTEST_OFF		0x0
#define ADF4377_001D_EN_CPTEST_ON		0x1

#define ADF4377_001D_CP_DOWN_OFF		0x0
#define ADF4377_001D_CP_DOWN_ON			0x1

#define ADF4377_001D_CP_UP_OFF			0x0
#define ADF4377_001D_CP_UP_ON			0x1

/* ADF4377 REG001F Map */
#define ADF4377_001F_BST_REF_MSK		BIT(7)
#define ADF4377_001F_FILT_REF_MSK		BIT(6)
#define ADF4377_001F_REF_SEL_MSK		BIT(5)
#define ADF4377_001F_R01F_RSV1_MSK		GENMASK(4, 0)

/* ADF4377 REG001F Bit Definition */
#define ADF4377_001F_BST_LARGE_REF_IN		0x0
#define ADF4377_001F_BST_SMALL_REF_IN		0x1

#define ADF4377_001F_FILT_REF_OFF		0x0
#define ADF4377_001F_FILT_REF_ON		0x1

#define ADF4377_001F_REF_SEL_DMA		0x0
#define ADF4377_001F_REF_SEL_LNA		0x1

#define ADF4377_001F_R01F_RSV1			0x7

/* ADF4377 REG0020 Map */
#define ADF4377_0020_RST_SYS_MSK		BIT(4)
#define ADF4377_0020_EN_ADC_CLK_MSK		BIT(3)
#define ADF4377_0020_R020_RSV1_MSK		BIT(0)

/* ADF4377 REG0021 Bit Definition */
#define ADF4377_0021_R021_RSV1			0xD3

/* ADF4377 REG0022 Bit Definition */
#define ADF4377_0022_R022_RSV1			0x32

/* ADF4377 REG0023 Map */
#define ADF4377_0023_CAT_CT_SEL			BIT(7)
#define ADF4377_0023_R023_RSV1_MSK		GENMASK(6, 0)

/* ADF4377 REG0023 Bit Definition */
#define ADF4377_0023_R023_RSV1			0x18

/* ADF4377 REG0024 Map */
#define ADF4377_0024_DCLK_MODE_MSK		BIT(2)

/* ADF4377 REG0025 Map */
#define ADF4377_0025_CLKODIV_DB_MSK		BIT(7)
#define ADF4377_0025_DCLK_DB_MSK		BIT(6)
#define ADF4377_0025_R025_RSV1_MSK		GENMASK(5, 0)

/* ADF4377 REG0025 Bit Definition */
#define ADF4377_0025_R025_RSV1			0x16

/* ADF4377 REG0026 Map */
#define ADF4377_0026_VCO_BAND_DIV_MSK		GENMASK(7, 0)

/* ADF4377 REG0027 Map */
#define ADF4377_0027_SYNTH_LOCK_TO_LSB_MSK	GENMASK(7, 0)

/* ADF4377 REG0028 Map */
#define ADF4377_0028_O_VCO_DB_MSK		BIT(7)
#define ADF4377_0028_SYNTH_LOCK_TO_MSB_MSK	GENMASK(6, 0)

/* ADF4377 REG0029 Map */
#define ADF4377_0029_VCO_ALC_TO_LSB_MSK		GENMASK(7, 0)

/* ADF4377 REG002A Map */
#define ADF4377_002A_DEL_CTRL_DB_MSK		BIT(7)
#define ADF4377_002A_VCO_ALC_TO_MSB_MSK		GENMASK(6, 0)

/* ADF4377 REG002C Map */
#define ADF4377_002C_R02C_RSV1			0xC0

/* ADF4377 REG002D Map */
#define ADF4377_002D_ADC_CLK_DIV_MSK		GENMASK(7, 0)

/* ADF4377 REG002E Map */
#define ADF4377_002E_EN_ADC_CNV_MSK		BIT(7)
#define ADF4377_002E_EN_ADC_MSK			BIT(1)
#define ADF4377_002E_ADC_A_CONV_MSK		BIT(0)

/* ADF4377 REG002E Bit Definition */
#define ADF4377_002E_ADC_A_CONV_ADC_ST_CNV	0x0
#define ADF4377_002E_ADC_A_CONV_VCO_CALIB	0x1

/* ADF4377 REG002F Map */
#define ADF4377_002F_DCLK_DIV1_MSK		GENMASK(1, 0)

/* ADF4377 REG002F Bit Definition */
#define ADF4377_002F_DCLK_DIV1_1		0x0
#define ADF4377_002F_DCLK_DIV1_2		0x1
#define ADF4377_002F_DCLK_DIV1_8		0x2
#define ADF4377_002F_DCLK_DIV1_32		0x3

/* ADF4377 REG0031 Bit Definition */
#define ADF4377_0031_R031_RSV1			0x09

/* ADF4377 REG0032 Map */
#define ADF4377_0032_ADC_CLK_SEL_MSK		BIT(6)
#define ADF4377_0032_R032_RSV1_MSK		GENMASK(5, 0)

/* ADF4377 REG0032 Bit Definition */
#define ADF4377_0032_ADC_CLK_SEL_N_OP		0x0
#define ADF4377_0032_ADC_CLK_SEL_SPI_CLK	0x1

#define ADF4377_0032_R032_RSV1			0x9

/* ADF4377 REG0033 Bit Definition */
#define ADF4377_0033_R033_RSV1			0x18

/* ADF4377 REG0034 Bit Definition */
#define ADF4377_0034_R034_RSV1			0x08

/* ADF4377 REG003A Bit Definition */
#define ADF4377_003A_R03A_RSV1			0x5D

/* ADF4377 REG003B Bit Definition */
#define ADF4377_003B_R03B_RSV1			0x2B

/* ADF4377 REG003D Map */
#define ADF4377_003D_O_VCO_BAND_MSK		BIT(3)
#define ADF4377_003D_O_VCO_CORE_MSK		BIT(2)
#define ADF4377_003D_O_VCO_BIAS_MSK		BIT(1)

/* ADF4377 REG003D Bit Definition */
#define ADF4377_003D_O_VCO_BAND_VCO_CALIB	0x0
#define ADF4377_003D_O_VCO_BAND_M_VCO		0x1

#define ADF4377_003D_O_VCO_CORE_VCO_CALIB	0x0
#define ADF4377_003D_O_VCO_CORE_M_VCO		0x1

#define ADF4377_003D_O_VCO_BIAS_VCO_CALIB	0x0
#define ADF4377_003D_O_VCO_BIAS_M_VCO		0x1

/* ADF4377 REG0042 Map */
#define ADF4377_0042_R042_RSV1			0x05

/* ADF4377 REG0045 Map */
#define ADF4377_0045_ADC_ST_CNV_MSK		BIT(0)

/* ADF4377 REG0049 Map */
#define ADF4377_0049_EN_CLK2_MSK		BIT(7)
#define ADF4377_0049_EN_CLK1_MSK		BIT(6)
#define ADF4377_0049_REF_OK_MSK			BIT(3)
#define ADF4377_0049_ADC_BUSY_MSK		BIT(2)
#define ADF4377_0049_FSM_BUSY_MSK		BIT(1)
#define ADF4377_0049_LOCKED_MSK			BIT(0)

/* ADF4377 REG004B Map */
#define ADF4377_004B_VCO_CORE_MSK		GENMASK(1, 0)

/* ADF4377 REG004C Map */
#define ADF4377_004C_CHIP_TEMP_LSB_MSK		GENMASK(7, 0)

/* ADF4377 REG004D Map */
#define ADF4377_004D_CHIP_TEMP_MSB_MSK		BIT(0)

/* ADF4377 REG004F Map */
#define ADF4377_004F_VCO_BAND_MSK		GENMASK(7, 0)

/* ADF4377 REG0051 Map */
#define ADF4377_0051_VCO_BIAS_MSK		GENMASK(3, 0)

/* ADF4377 REG0054 Map */
#define ADF4377_0054_CHIP_VERSION_MSK		GENMASK(7, 0)

/* Specifications */
#define ADF4377_SPI_READ_CMD			BIT(7)
#define ADF4377_MAX_VCO_FREQ			(12800ULL * HZ_PER_MHZ)
#define ADF4377_MIN_VCO_FREQ			(6400ULL * HZ_PER_MHZ)
#define ADF4377_MAX_REFIN_FREQ			(1000 * HZ_PER_MHZ)
#define ADF4377_MIN_REFIN_FREQ			(10 * HZ_PER_MHZ)
#define ADF4377_MAX_FREQ_PFD			(500 * HZ_PER_MHZ)
#define ADF4377_MIN_FREQ_PFD			(3 * HZ_PER_MHZ)
#define ADF4377_MAX_CLKPN_FREQ			ADF4377_MAX_VCO_FREQ
#define ADF4377_MIN_CLKPN_FREQ			(ADF4377_MIN_VCO_FREQ / 8)
#define ADF4377_FREQ_PFD_80MHZ			(80 * HZ_PER_MHZ)
#define ADF4377_FREQ_PFD_125MHZ			(125 * HZ_PER_MHZ)
#define ADF4377_FREQ_PFD_160MHZ			(160 * HZ_PER_MHZ)
#define ADF4377_FREQ_PFD_250MHZ			(250 * HZ_PER_MHZ)
#define ADF4377_FREQ_PFD_320MHZ			(320 * HZ_PER_MHZ)

enum {
	ADF4377_FREQ,
};

enum muxout_select_mode {
	ADF4377_MUXOUT_HIGH_Z = 0x0,
	ADF4377_MUXOUT_LKDET = 0x1,
	ADF4377_MUXOUT_LOW = 0x2,
	ADF4377_MUXOUT_DIV_RCLK_2 = 0x4,
	ADF4377_MUXOUT_DIV_NCLK_2 = 0x5,
	ADF4377_MUXOUT_HIGH = 0x8,
};

struct adf4377_state {
	struct spi_device	*spi;
	struct regmap		*regmap;
	struct clk		*clkin;
	/* Protect against concurrent accesses to the device and data content */
	struct mutex		lock;
	struct notifier_block	nb;
	/* Reference Divider */
	unsigned int		ref_div_factor;
	/* PFD Frequency */
	unsigned int		f_pfd;
	/* Input Reference Clock */
	unsigned int		clkin_freq;
	/* CLKOUT Divider */
	u8			clkout_div_sel;
	/* Feedback Divider (N) */
	u16			n_int;
	u16			synth_lock_timeout;
	u16			vco_alc_timeout;
	u16			adc_clk_div;
	u16			vco_band_div;
	u8			dclk_div1;
	u8			dclk_div2;
	u8			dclk_mode;
	unsigned int		f_div_rclk;
	enum muxout_select_mode	muxout_select;
	struct gpio_desc	*gpio_ce;
	struct gpio_desc	*gpio_enclk1;
	struct gpio_desc	*gpio_enclk2;
	u8			buf[2] __aligned(IIO_DMA_MINALIGN);
};

static const char * const adf4377_muxout_modes[] = {
	[ADF4377_MUXOUT_HIGH_Z] = "high_z",
	[ADF4377_MUXOUT_LKDET] = "lock_detect",
	[ADF4377_MUXOUT_LOW] = "muxout_low",
	[ADF4377_MUXOUT_DIV_RCLK_2] = "f_div_rclk_2",
	[ADF4377_MUXOUT_DIV_NCLK_2] = "f_div_nclk_2",
	[ADF4377_MUXOUT_HIGH] = "muxout_high",
};

static const struct reg_sequence adf4377_reg_defaults[] = {
	{ 0x42,  ADF4377_0042_R042_RSV1 },
	{ 0x3B,  ADF4377_003B_R03B_RSV1 },
	{ 0x3A,  ADF4377_003A_R03A_RSV1 },
	{ 0x34,  ADF4377_0034_R034_RSV1 },
	{ 0x33,  ADF4377_0033_R033_RSV1 },
	{ 0x32,  ADF4377_0032_R032_RSV1 },
	{ 0x31,  ADF4377_0031_R031_RSV1 },
	{ 0x2C,  ADF4377_002C_R02C_RSV1 },
	{ 0x25,  ADF4377_0025_R025_RSV1 },
	{ 0x23,  ADF4377_0023_R023_RSV1 },
	{ 0x22,  ADF4377_0022_R022_RSV1 },
	{ 0x21,  ADF4377_0021_R021_RSV1 },
	{ 0x1f,  ADF4377_001F_R01F_RSV1 },
	{ 0x1c,  ADF4377_001C_R01C_RSV1 },
};

static const struct regmap_config adf4377_regmap_config = {
	.reg_bits = 16,
	.val_bits = 8,
	.read_flag_mask = BIT(7),
	.max_register = 0x54,
};

static int adf4377_reg_access(struct iio_dev *indio_dev,
			      unsigned int reg,
			      unsigned int write_val,
			      unsigned int *read_val)
{
	struct adf4377_state *st = iio_priv(indio_dev);

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

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

static const struct iio_info adf4377_info = {
	.debugfs_reg_access = &adf4377_reg_access,
};

static int adf4377_soft_reset(struct adf4377_state *st)
{
	unsigned int read_val;
	int ret;

	ret = regmap_update_bits(st->regmap, 0x0, ADF4377_0000_SOFT_RESET_MSK |
				 ADF4377_0000_SOFT_RESET_R_MSK,
				 FIELD_PREP(ADF4377_0000_SOFT_RESET_MSK, 1) |
				 FIELD_PREP(ADF4377_0000_SOFT_RESET_R_MSK, 1));
	if (ret)
		return ret;

	return regmap_read_poll_timeout(st->regmap, 0x0, read_val,
					!(read_val & (ADF4377_0000_SOFT_RESET_R_MSK |
					ADF4377_0000_SOFT_RESET_R_MSK)), 200, 200 * 100);
}

static int adf4377_get_freq(struct adf4377_state *st, u64 *freq)
{
	unsigned int ref_div_factor, n_int;
	u64 clkin_freq;
	int ret;

	mutex_lock(&st->lock);
	ret = regmap_read(st->regmap, 0x12, &ref_div_factor);
	if (ret)
		goto exit;

	ret = regmap_bulk_read(st->regmap, 0x10, st->buf, sizeof(st->buf));
	if (ret)
		goto exit;

	clkin_freq = clk_get_rate(st->clkin);
	ref_div_factor = FIELD_GET(ADF4377_0012_R_DIV_MSK, ref_div_factor);
	n_int = FIELD_GET(ADF4377_0010_N_INT_LSB_MSK | ADF4377_0011_N_INT_MSB_MSK,
			  get_unaligned_le16(&st->buf));

	*freq = div_u64(clkin_freq, ref_div_factor) * n_int;
exit:
	mutex_unlock(&st->lock);

	return ret;
}

static int adf4377_set_freq(struct adf4377_state *st, u64 freq)
{
	unsigned int read_val;
	u64 f_vco;
	int ret;

	mutex_lock(&st->lock);

	if (freq > ADF4377_MAX_CLKPN_FREQ || freq < ADF4377_MIN_CLKPN_FREQ) {
		ret = -EINVAL;
		goto exit;
	}

	ret = regmap_update_bits(st->regmap, 0x1C, ADF4377_001C_EN_DNCLK_MSK |
				 ADF4377_001C_EN_DRCLK_MSK,
				 FIELD_PREP(ADF4377_001C_EN_DNCLK_MSK, 1) |
				 FIELD_PREP(ADF4377_001C_EN_DRCLK_MSK, 1));
	if (ret)
		goto exit;

	ret = regmap_update_bits(st->regmap, 0x11, ADF4377_0011_EN_AUTOCAL_MSK |
				 ADF4377_0011_DCLK_DIV2_MSK,
				 FIELD_PREP(ADF4377_0011_EN_AUTOCAL_MSK, 1) |
				 FIELD_PREP(ADF4377_0011_DCLK_DIV2_MSK, st->dclk_div2));
	if (ret)
		goto exit;

	ret = regmap_update_bits(st->regmap, 0x2E, ADF4377_002E_EN_ADC_CNV_MSK |
				 ADF4377_002E_EN_ADC_MSK |
				 ADF4377_002E_ADC_A_CONV_MSK,
				 FIELD_PREP(ADF4377_002E_EN_ADC_CNV_MSK, 1) |
				 FIELD_PREP(ADF4377_002E_EN_ADC_MSK, 1) |
				 FIELD_PREP(ADF4377_002E_ADC_A_CONV_MSK,
					    ADF4377_002E_ADC_A_CONV_VCO_CALIB));
	if (ret)
		goto exit;

	ret = regmap_update_bits(st->regmap, 0x20, ADF4377_0020_EN_ADC_CLK_MSK,
				 FIELD_PREP(ADF4377_0020_EN_ADC_CLK_MSK, 1));
	if (ret)
		goto exit;

	ret = regmap_update_bits(st->regmap, 0x2F, ADF4377_002F_DCLK_DIV1_MSK,
				 FIELD_PREP(ADF4377_002F_DCLK_DIV1_MSK, st->dclk_div1));
	if (ret)
		goto exit;

	ret = regmap_update_bits(st->regmap, 0x24, ADF4377_0024_DCLK_MODE_MSK,
				 FIELD_PREP(ADF4377_0024_DCLK_MODE_MSK, st->dclk_mode));
	if (ret)
		goto exit;

	ret = regmap_write(st->regmap, 0x27,
			   FIELD_PREP(ADF4377_0027_SYNTH_LOCK_TO_LSB_MSK,
				      st->synth_lock_timeout));
	if (ret)
		goto exit;

	ret = regmap_update_bits(st->regmap, 0x28, ADF4377_0028_SYNTH_LOCK_TO_MSB_MSK,
				 FIELD_PREP(ADF4377_0028_SYNTH_LOCK_TO_MSB_MSK,
					    st->synth_lock_timeout >> 8));
	if (ret)
		goto exit;

	ret = regmap_write(st->regmap, 0x29,
			   FIELD_PREP(ADF4377_0029_VCO_ALC_TO_LSB_MSK,
				      st->vco_alc_timeout));
	if (ret)
		goto exit;

	ret = regmap_update_bits(st->regmap, 0x2A, ADF4377_002A_VCO_ALC_TO_MSB_MSK,
				 FIELD_PREP(ADF4377_002A_VCO_ALC_TO_MSB_MSK,
					    st->vco_alc_timeout >> 8));
	if (ret)
		goto exit;

	ret = regmap_write(st->regmap, 0x26,
			   FIELD_PREP(ADF4377_0026_VCO_BAND_DIV_MSK, st->vco_band_div));
	if (ret)
		goto exit;

	ret = regmap_write(st->regmap, 0x2D,
			   FIELD_PREP(ADF4377_002D_ADC_CLK_DIV_MSK, st->adc_clk_div));
	if (ret)
		goto exit;

	st->clkout_div_sel = 0;

	f_vco = freq;

	while (f_vco < ADF4377_MIN_VCO_FREQ) {
		f_vco <<= 1;
		st->clkout_div_sel++;
	}

	st->n_int = div_u64(freq, st->f_pfd);

	ret = regmap_update_bits(st->regmap, 0x11, ADF4377_0011_EN_RDBLR_MSK |
				 ADF4377_0011_N_INT_MSB_MSK,
				 FIELD_PREP(ADF4377_0011_EN_RDBLR_MSK, 0) |
				 FIELD_PREP(ADF4377_0011_N_INT_MSB_MSK, st->n_int >> 8));
	if (ret)
		goto exit;

	ret = regmap_update_bits(st->regmap, 0x12, ADF4377_0012_R_DIV_MSK |
				 ADF4377_0012_CLKOUT_DIV_MSK,
				 FIELD_PREP(ADF4377_0012_CLKOUT_DIV_MSK, st->clkout_div_sel) |
				 FIELD_PREP(ADF4377_0012_R_DIV_MSK, st->ref_div_factor));
	if (ret)
		goto exit;

	ret = regmap_write(st->regmap, 0x10,
			   FIELD_PREP(ADF4377_0010_N_INT_LSB_MSK, st->n_int));
	if (ret)
		goto exit;

	ret = regmap_read_poll_timeout(st->regmap, 0x49, read_val,
				       !(read_val & (ADF4377_0049_FSM_BUSY_MSK)), 200, 200 * 100);
	if (ret)
		goto exit;

	/* Disable EN_DNCLK, EN_DRCLK */
	ret = regmap_update_bits(st->regmap, 0x1C, ADF4377_001C_EN_DNCLK_MSK |
				 ADF4377_001C_EN_DRCLK_MSK,
				 FIELD_PREP(ADF4377_001C_EN_DNCLK_MSK, 0) |
				 FIELD_PREP(ADF4377_001C_EN_DRCLK_MSK, 0));
	if (ret)
		goto exit;

	/* Disable EN_ADC_CLK */
	ret = regmap_update_bits(st->regmap, 0x20, ADF4377_0020_EN_ADC_CLK_MSK,
				 FIELD_PREP(ADF4377_0020_EN_ADC_CLK_MSK, 0));
	if (ret)
		goto exit;

	/* Set output Amplitude */
	ret = regmap_update_bits(st->regmap, 0x19, ADF4377_0019_CLKOUT2_OP_MSK |
				 ADF4377_0019_CLKOUT1_OP_MSK,
				 FIELD_PREP(ADF4377_0019_CLKOUT1_OP_MSK,
					    ADF4377_0019_CLKOUT_420MV) |
				 FIELD_PREP(ADF4377_0019_CLKOUT2_OP_MSK,
					    ADF4377_0019_CLKOUT_420MV));

exit:
	mutex_unlock(&st->lock);

	return ret;
}

static void adf4377_gpio_init(struct adf4377_state *st)
{
	if (st->gpio_ce) {
		gpiod_set_value(st->gpio_ce, 1);

		/* Delay for SPI register bits to settle to their power-on reset state */
		fsleep(200);
	}

	if (st->gpio_enclk1)
		gpiod_set_value(st->gpio_enclk1, 1);

	if (st->gpio_enclk2)
		gpiod_set_value(st->gpio_enclk2, 1);
}

static int adf4377_init(struct adf4377_state *st)
{
	struct spi_device *spi = st->spi;
	int ret;

	adf4377_gpio_init(st);

	ret = adf4377_soft_reset(st);
	if (ret) {
		dev_err(&spi->dev, "Failed to soft reset.\n");
		return ret;
	}

	ret = regmap_multi_reg_write(st->regmap, adf4377_reg_defaults,
				     ARRAY_SIZE(adf4377_reg_defaults));
	if (ret) {
		dev_err(&spi->dev, "Failed to set default registers.\n");
		return ret;
	}

	ret = regmap_update_bits(st->regmap, 0x00,
				 ADF4377_0000_SDO_ACTIVE_MSK | ADF4377_0000_SDO_ACTIVE_R_MSK,
				 FIELD_PREP(ADF4377_0000_SDO_ACTIVE_MSK,
					    ADF4377_0000_SDO_ACTIVE_SPI_4W) |
				 FIELD_PREP(ADF4377_0000_SDO_ACTIVE_R_MSK,
					    ADF4377_0000_SDO_ACTIVE_SPI_4W));
	if (ret) {
		dev_err(&spi->dev, "Failed to set 4-Wire Operation.\n");
		return ret;
	}

	st->clkin_freq = clk_get_rate(st->clkin);

	/* Power Up */
	ret = regmap_write(st->regmap, 0x1a,
			   FIELD_PREP(ADF4377_001A_PD_ALL_MSK, 0) |
			   FIELD_PREP(ADF4377_001A_PD_RDIV_MSK, 0) |
			   FIELD_PREP(ADF4377_001A_PD_NDIV_MSK, 0) |
			   FIELD_PREP(ADF4377_001A_PD_VCO_MSK, 0) |
			   FIELD_PREP(ADF4377_001A_PD_LD_MSK, 0) |
			   FIELD_PREP(ADF4377_001A_PD_PFDCP_MSK, 0) |
			   FIELD_PREP(ADF4377_001A_PD_CLKOUT1_MSK, 0) |
			   FIELD_PREP(ADF4377_001A_PD_CLKOUT2_MSK, 0));
	if (ret) {
		dev_err(&spi->dev, "Failed to set power down registers.\n");
		return ret;
	}

	/* Set Mux Output */
	ret = regmap_update_bits(st->regmap, 0x1D,
				 ADF4377_001D_MUXOUT_MSK,
				 FIELD_PREP(ADF4377_001D_MUXOUT_MSK, st->muxout_select));
	if (ret)
		return ret;

	/* Compute PFD */
	st->ref_div_factor = 0;
	do {
		st->ref_div_factor++;
		st->f_pfd = st->clkin_freq / st->ref_div_factor;
	} while (st->f_pfd > ADF4377_MAX_FREQ_PFD);

	if (st->f_pfd > ADF4377_MAX_FREQ_PFD || st->f_pfd < ADF4377_MIN_FREQ_PFD)
		return -EINVAL;

	st->f_div_rclk = st->f_pfd;

	if (st->f_pfd <= ADF4377_FREQ_PFD_80MHZ) {
		st->dclk_div1 = ADF4377_002F_DCLK_DIV1_1;
		st->dclk_div2 = ADF4377_0011_DCLK_DIV2_1;
		st->dclk_mode = 0;
	} else if (st->f_pfd <= ADF4377_FREQ_PFD_125MHZ) {
		st->dclk_div1 = ADF4377_002F_DCLK_DIV1_1;
		st->dclk_div2 = ADF4377_0011_DCLK_DIV2_1;
		st->dclk_mode = 1;
	} else if (st->f_pfd <= ADF4377_FREQ_PFD_160MHZ) {
		st->dclk_div1 = ADF4377_002F_DCLK_DIV1_2;
		st->dclk_div2 = ADF4377_0011_DCLK_DIV2_1;
		st->dclk_mode = 0;
		st->f_div_rclk /= 2;
	} else if (st->f_pfd <= ADF4377_FREQ_PFD_250MHZ) {
		st->dclk_div1 = ADF4377_002F_DCLK_DIV1_2;
		st->dclk_div2 = ADF4377_0011_DCLK_DIV2_1;
		st->dclk_mode = 1;
		st->f_div_rclk /= 2;
	} else if (st->f_pfd <= ADF4377_FREQ_PFD_320MHZ) {
		st->dclk_div1 = ADF4377_002F_DCLK_DIV1_2;
		st->dclk_div2 = ADF4377_0011_DCLK_DIV2_2;
		st->dclk_mode = 0;
		st->f_div_rclk /= 4;
	} else {
		st->dclk_div1 = ADF4377_002F_DCLK_DIV1_2;
		st->dclk_div2 = ADF4377_0011_DCLK_DIV2_2;
		st->dclk_mode = 1;
		st->f_div_rclk /= 4;
	}

	st->synth_lock_timeout = DIV_ROUND_UP(st->f_div_rclk, 50000);
	st->vco_alc_timeout = DIV_ROUND_UP(st->f_div_rclk, 20000);
	st->vco_band_div = DIV_ROUND_UP(st->f_div_rclk, 150000 * 16 * (1 << st->dclk_mode));
	st->adc_clk_div = DIV_ROUND_UP((st->f_div_rclk / 400000 - 2), 4);

	return 0;
}

static ssize_t adf4377_read(struct iio_dev *indio_dev, uintptr_t private,
			    const struct iio_chan_spec *chan, char *buf)
{
	struct adf4377_state *st = iio_priv(indio_dev);
	u64 val = 0;
	int ret;

	switch ((u32)private) {
	case ADF4377_FREQ:
		ret = adf4377_get_freq(st, &val);
		if (ret)
			return ret;

		return sysfs_emit(buf, "%llu\n", val);
	default:
		return -EINVAL;
	}
}

static ssize_t adf4377_write(struct iio_dev *indio_dev, uintptr_t private,
			     const struct iio_chan_spec *chan, const char *buf,
			     size_t len)
{
	struct adf4377_state *st = iio_priv(indio_dev);
	unsigned long long freq;
	int ret;

	switch ((u32)private) {
	case ADF4377_FREQ:
		ret = kstrtoull(buf, 10, &freq);
		if (ret)
			return ret;

		ret = adf4377_set_freq(st, freq);
		if (ret)
			return ret;

		return len;
	default:
		return -EINVAL;
	}
}

#define _ADF4377_EXT_INFO(_name, _shared, _ident) { \
		.name = _name, \
		.read = adf4377_read, \
		.write = adf4377_write, \
		.private = _ident, \
		.shared = _shared, \
	}

static const struct iio_chan_spec_ext_info adf4377_ext_info[] = {
	/*
	 * Usually we use IIO_CHAN_INFO_FREQUENCY, but there are
	 * values > 2^32 in order to support the entire frequency range
	 * in Hz.
	 */
	_ADF4377_EXT_INFO("frequency", IIO_SEPARATE, ADF4377_FREQ),
	{ }
};

static const struct iio_chan_spec adf4377_channels[] = {
	{
		.type = IIO_ALTVOLTAGE,
		.indexed = 1,
		.output = 1,
		.channel = 0,
		.ext_info = adf4377_ext_info,
	},
};

static int adf4377_properties_parse(struct adf4377_state *st)
{
	struct spi_device *spi = st->spi;
	const char *str;
	int ret;

	st->clkin = devm_clk_get_enabled(&spi->dev, "ref_in");
	if (IS_ERR(st->clkin))
		return dev_err_probe(&spi->dev, PTR_ERR(st->clkin),
				     "failed to get the reference input clock\n");

	st->gpio_ce = devm_gpiod_get_optional(&st->spi->dev, "chip-enable",
					      GPIOD_OUT_LOW);
	if (IS_ERR(st->gpio_ce))
		return dev_err_probe(&spi->dev, PTR_ERR(st->gpio_ce),
				     "failed to get the CE GPIO\n");

	st->gpio_enclk1 = devm_gpiod_get_optional(&st->spi->dev, "clk1-enable",
						  GPIOD_OUT_LOW);
	if (IS_ERR(st->gpio_enclk1))
		return dev_err_probe(&spi->dev, PTR_ERR(st->gpio_enclk1),
				     "failed to get the CE GPIO\n");

	st->gpio_enclk2 = devm_gpiod_get_optional(&st->spi->dev, "clk2-enable",
						  GPIOD_OUT_LOW);
	if (IS_ERR(st->gpio_enclk2))
		return dev_err_probe(&spi->dev, PTR_ERR(st->gpio_enclk2),
				     "failed to get the CE GPIO\n");

	ret = device_property_read_string(&spi->dev, "adi,muxout-select", &str);
	if (ret) {
		st->muxout_select = ADF4377_MUXOUT_HIGH_Z;
	} else {
		ret = match_string(adf4377_muxout_modes, ARRAY_SIZE(adf4377_muxout_modes), str);
		if (ret < 0)
			return ret;

		st->muxout_select = ret;
	}

	return 0;
}

static int adf4377_freq_change(struct notifier_block *nb, unsigned long action, void *data)
{
	struct adf4377_state *st = container_of(nb, struct adf4377_state, nb);
	int ret;

	if (action == POST_RATE_CHANGE) {
		mutex_lock(&st->lock);
		ret = notifier_from_errno(adf4377_init(st));
		mutex_unlock(&st->lock);
		return ret;
	}

	return NOTIFY_OK;
}

static int adf4377_probe(struct spi_device *spi)
{
	struct iio_dev *indio_dev;
	struct regmap *regmap;
	struct adf4377_state *st;
	int ret;

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

	regmap = devm_regmap_init_spi(spi, &adf4377_regmap_config);
	if (IS_ERR(regmap))
		return PTR_ERR(regmap);

	st = iio_priv(indio_dev);

	indio_dev->info = &adf4377_info;
	indio_dev->name = "adf4377";
	indio_dev->channels = adf4377_channels;
	indio_dev->num_channels = ARRAY_SIZE(adf4377_channels);

	st->regmap = regmap;
	st->spi = spi;
	mutex_init(&st->lock);

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

	st->nb.notifier_call = adf4377_freq_change;
	ret = devm_clk_notifier_register(&spi->dev, st->clkin, &st->nb);
	if (ret)
		return ret;

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

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

static const struct spi_device_id adf4377_id[] = {
	{ "adf4377", 0 },
	{}
};
MODULE_DEVICE_TABLE(spi, adf4377_id);

static const struct of_device_id adf4377_of_match[] = {
	{ .compatible = "adi,adf4377" },
	{}
};
MODULE_DEVICE_TABLE(of, adf4377_of_match);

static struct spi_driver adf4377_driver = {
	.driver = {
		.name = "adf4377",
		.of_match_table = adf4377_of_match,
	},
	.probe = adf4377_probe,
	.id_table = adf4377_id,
};
module_spi_driver(adf4377_driver);

MODULE_AUTHOR("Antoniu Miclaus <antoniu.miclaus@analog.com>");
MODULE_DESCRIPTION("Analog Devices ADF4377");
MODULE_LICENSE("GPL");