1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright 2022 Analog Devices Inc.
8 #include <linux/bitfield.h>
9 #include <linux/bits.h>
10 #include <linux/clk.h>
11 #include <linux/clkdev.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/module.h>
16 #include <linux/notifier.h>
17 #include <linux/property.h>
18 #include <linux/spi/spi.h>
19 #include <linux/iio/iio.h>
20 #include <linux/regmap.h>
21 #include <linux/units.h>
23 #include <linux/unaligned.h>
25 /* ADF4377 REG0000 Map */
26 #define ADF4377_0000_SOFT_RESET_R_MSK BIT(7)
27 #define ADF4377_0000_LSB_FIRST_R_MSK BIT(6)
28 #define ADF4377_0000_ADDRESS_ASC_R_MSK BIT(5)
29 #define ADF4377_0000_SDO_ACTIVE_R_MSK BIT(4)
30 #define ADF4377_0000_SDO_ACTIVE_MSK BIT(3)
31 #define ADF4377_0000_ADDRESS_ASC_MSK BIT(2)
32 #define ADF4377_0000_LSB_FIRST_MSK BIT(1)
33 #define ADF4377_0000_SOFT_RESET_MSK BIT(0)
35 /* ADF4377 REG0000 Bit Definition */
36 #define ADF4377_0000_SDO_ACTIVE_SPI_3W 0x0
37 #define ADF4377_0000_SDO_ACTIVE_SPI_4W 0x1
39 #define ADF4377_0000_ADDR_ASC_AUTO_DECR 0x0
40 #define ADF4377_0000_ADDR_ASC_AUTO_INCR 0x1
42 #define ADF4377_0000_LSB_FIRST_MSB 0x0
43 #define ADF4377_0000_LSB_FIRST_LSB 0x1
45 #define ADF4377_0000_SOFT_RESET_N_OP 0x0
46 #define ADF4377_0000_SOFT_RESET_EN 0x1
48 /* ADF4377 REG0001 Map */
49 #define ADF4377_0001_SINGLE_INSTR_MSK BIT(7)
50 #define ADF4377_0001_MASTER_RB_CTRL_MSK BIT(5)
52 /* ADF4377 REG0003 Bit Definition */
53 #define ADF4377_0003_CHIP_TYPE 0x06
55 /* ADF4377 REG0004 Bit Definition */
56 #define ADF4377_0004_PRODUCT_ID_LSB 0x0005
58 /* ADF4377 REG0005 Bit Definition */
59 #define ADF4377_0005_PRODUCT_ID_MSB 0x0005
61 /* ADF4377 REG000A Map */
62 #define ADF4377_000A_SCRATCHPAD_MSK GENMASK(7, 0)
64 /* ADF4377 REG000C Bit Definition */
65 #define ADF4377_000C_VENDOR_ID_LSB 0x56
67 /* ADF4377 REG000D Bit Definition */
68 #define ADF4377_000D_VENDOR_ID_MSB 0x04
70 /* ADF4377 REG000F Bit Definition */
71 #define ADF4377_000F_R00F_RSV1_MSK GENMASK(7, 0)
73 /* ADF4377 REG0010 Map*/
74 #define ADF4377_0010_N_INT_LSB_MSK GENMASK(7, 0)
76 /* ADF4377 REG0011 Map*/
77 #define ADF4377_0011_EN_AUTOCAL_MSK BIT(7)
78 #define ADF4377_0011_EN_RDBLR_MSK BIT(6)
79 #define ADF4377_0011_DCLK_DIV2_MSK GENMASK(5, 4)
80 #define ADF4377_0011_N_INT_MSB_MSK GENMASK(3, 0)
82 /* ADF4377 REG0011 Bit Definition */
83 #define ADF4377_0011_DCLK_DIV2_1 0x0
84 #define ADF4377_0011_DCLK_DIV2_2 0x1
85 #define ADF4377_0011_DCLK_DIV2_4 0x2
86 #define ADF4377_0011_DCLK_DIV2_8 0x3
88 /* ADF4377 REG0012 Map*/
89 #define ADF4377_0012_CLKOUT_DIV_MSK GENMASK(7, 6)
90 #define ADF4377_0012_R_DIV_MSK GENMASK(5, 0)
92 /* ADF4377 REG0012 Bit Definition */
93 #define ADF4377_0012_CLKOUT_DIV_1 0x0
94 #define ADF4377_0012_CLKOUT_DIV_2 0x1
95 #define ADF4377_0012_CLKOUT_DIV_4 0x2
96 #define ADF4377_0012_CLKOUT_DIV_8 0x3
98 /* ADF4377 REG0013 Map */
99 #define ADF4377_0013_M_VCO_CORE_MSK GENMASK(5, 4)
100 #define ADF4377_0013_VCO_BIAS_MSK GENMASK(3, 0)
102 /* ADF4377 REG0013 Bit Definition */
103 #define ADF4377_0013_M_VCO_0 0x0
104 #define ADF4377_0013_M_VCO_1 0x1
105 #define ADF4377_0013_M_VCO_2 0x2
106 #define ADF4377_0013_M_VCO_3 0x3
108 /* ADF4377 REG0014 Map */
109 #define ADF4377_0014_M_VCO_BAND_MSK GENMASK(7, 0)
111 /* ADF4377 REG0015 Map */
112 #define ADF4377_0015_BLEED_I_LSB_MSK GENMASK(7, 6)
113 #define ADF4377_0015_BLEED_POL_MSK BIT(5)
114 #define ADF4377_0015_EN_BLEED_MSK BIT(4)
115 #define ADF4377_0015_CP_I_MSK GENMASK(3, 0)
117 /* ADF4377 REG0015 Bit Definition */
118 #define ADF4377_CURRENT_SINK 0x0
119 #define ADF4377_CURRENT_SOURCE 0x1
121 #define ADF4377_0015_CP_0MA7 0x0
122 #define ADF4377_0015_CP_0MA9 0x1
123 #define ADF4377_0015_CP_1MA1 0x2
124 #define ADF4377_0015_CP_1MA3 0x3
125 #define ADF4377_0015_CP_1MA4 0x4
126 #define ADF4377_0015_CP_1MA8 0x5
127 #define ADF4377_0015_CP_2MA2 0x6
128 #define ADF4377_0015_CP_2MA5 0x7
129 #define ADF4377_0015_CP_2MA9 0x8
130 #define ADF4377_0015_CP_3MA6 0x9
131 #define ADF4377_0015_CP_4MA3 0xA
132 #define ADF4377_0015_CP_5MA0 0xB
133 #define ADF4377_0015_CP_5MA7 0xC
134 #define ADF4377_0015_CP_7MA2 0xD
135 #define ADF4377_0015_CP_8MA6 0xE
136 #define ADF4377_0015_CP_10MA1 0xF
138 /* ADF4377 REG0016 Map */
139 #define ADF4377_0016_BLEED_I_MSB_MSK GENMASK(7, 0)
141 /* ADF4377 REG0017 Map */
142 #define ADF4377_0016_INV_CLKOUT_MSK BIT(7)
143 #define ADF4377_0016_N_DEL_MSK GENMASK(6, 0)
145 /* ADF4377 REG0018 Map */
146 #define ADF4377_0018_CMOS_OV_MSK BIT(7)
147 #define ADF4377_0018_R_DEL_MSK GENMASK(6, 0)
149 /* ADF4377 REG0018 Bit Definition */
150 #define ADF4377_0018_1V8_LOGIC 0x0
151 #define ADF4377_0018_3V3_LOGIC 0x1
153 /* ADF4377 REG0019 Map */
154 #define ADF4377_0019_CLKOUT2_OP_MSK GENMASK(7, 6)
155 #define ADF4377_0019_CLKOUT1_OP_MSK GENMASK(5, 4)
156 #define ADF4377_0019_PD_CLK_MSK BIT(3)
157 #define ADF4377_0019_PD_RDET_MSK BIT(2)
158 #define ADF4377_0019_PD_ADC_MSK BIT(1)
159 #define ADF4377_0019_PD_CALADC_MSK BIT(0)
161 /* ADF4377 REG0019 Bit Definition */
162 #define ADF4377_0019_CLKOUT_320MV 0x0
163 #define ADF4377_0019_CLKOUT_420MV 0x1
164 #define ADF4377_0019_CLKOUT_530MV 0x2
165 #define ADF4377_0019_CLKOUT_640MV 0x3
167 /* ADF4377 REG001A Map */
168 #define ADF4377_001A_PD_ALL_MSK BIT(7)
169 #define ADF4377_001A_PD_RDIV_MSK BIT(6)
170 #define ADF4377_001A_PD_NDIV_MSK BIT(5)
171 #define ADF4377_001A_PD_VCO_MSK BIT(4)
172 #define ADF4377_001A_PD_LD_MSK BIT(3)
173 #define ADF4377_001A_PD_PFDCP_MSK BIT(2)
174 #define ADF4377_001A_PD_CLKOUT1_MSK BIT(1)
175 #define ADF4377_001A_PD_CLKOUT2_MSK BIT(0)
177 /* ADF4377 REG001B Map */
178 #define ADF4377_001B_EN_LOL_MSK BIT(7)
179 #define ADF4377_001B_LDWIN_PW_MSK BIT(6)
180 #define ADF4377_001B_EN_LDWIN_MSK BIT(5)
181 #define ADF4377_001B_LD_COUNT_MSK GENMASK(4, 0)
183 /* ADF4377 REG001B Bit Definition */
184 #define ADF4377_001B_LDWIN_PW_NARROW 0x0
185 #define ADF4377_001B_LDWIN_PW_WIDE 0x1
187 /* ADF4377 REG001C Map */
188 #define ADF4377_001C_EN_DNCLK_MSK BIT(7)
189 #define ADF4377_001C_EN_DRCLK_MSK BIT(6)
190 #define ADF4377_001C_RST_LD_MSK BIT(2)
191 #define ADF4377_001C_R01C_RSV1_MSK BIT(0)
193 /* ADF4377 REG001C Bit Definition */
194 #define ADF4377_001C_RST_LD_INACTIVE 0x0
195 #define ADF4377_001C_RST_LD_ACTIVE 0x1
197 #define ADF4377_001C_R01C_RSV1 0x1
199 /* ADF4377 REG001D Map */
200 #define ADF4377_001D_MUXOUT_MSK GENMASK(7, 4)
201 #define ADF4377_001D_EN_CPTEST_MSK BIT(2)
202 #define ADF4377_001D_CP_DOWN_MSK BIT(1)
203 #define ADF4377_001D_CP_UP_MSK BIT(0)
205 #define ADF4377_001D_EN_CPTEST_OFF 0x0
206 #define ADF4377_001D_EN_CPTEST_ON 0x1
208 #define ADF4377_001D_CP_DOWN_OFF 0x0
209 #define ADF4377_001D_CP_DOWN_ON 0x1
211 #define ADF4377_001D_CP_UP_OFF 0x0
212 #define ADF4377_001D_CP_UP_ON 0x1
214 /* ADF4377 REG001F Map */
215 #define ADF4377_001F_BST_REF_MSK BIT(7)
216 #define ADF4377_001F_FILT_REF_MSK BIT(6)
217 #define ADF4377_001F_REF_SEL_MSK BIT(5)
218 #define ADF4377_001F_R01F_RSV1_MSK GENMASK(4, 0)
220 /* ADF4377 REG001F Bit Definition */
221 #define ADF4377_001F_BST_LARGE_REF_IN 0x0
222 #define ADF4377_001F_BST_SMALL_REF_IN 0x1
224 #define ADF4377_001F_FILT_REF_OFF 0x0
225 #define ADF4377_001F_FILT_REF_ON 0x1
227 #define ADF4377_001F_REF_SEL_DMA 0x0
228 #define ADF4377_001F_REF_SEL_LNA 0x1
230 #define ADF4377_001F_R01F_RSV1 0x7
232 /* ADF4377 REG0020 Map */
233 #define ADF4377_0020_RST_SYS_MSK BIT(4)
234 #define ADF4377_0020_EN_ADC_CLK_MSK BIT(3)
235 #define ADF4377_0020_R020_RSV1_MSK BIT(0)
237 /* ADF4377 REG0021 Bit Definition */
238 #define ADF4377_0021_R021_RSV1 0xD3
240 /* ADF4377 REG0022 Bit Definition */
241 #define ADF4377_0022_R022_RSV1 0x32
243 /* ADF4377 REG0023 Map */
244 #define ADF4377_0023_CAT_CT_SEL BIT(7)
245 #define ADF4377_0023_R023_RSV1_MSK GENMASK(6, 0)
247 /* ADF4377 REG0023 Bit Definition */
248 #define ADF4377_0023_R023_RSV1 0x18
250 /* ADF4377 REG0024 Map */
251 #define ADF4377_0024_DCLK_MODE_MSK BIT(2)
253 /* ADF4377 REG0025 Map */
254 #define ADF4377_0025_CLKODIV_DB_MSK BIT(7)
255 #define ADF4377_0025_DCLK_DB_MSK BIT(6)
256 #define ADF4377_0025_R025_RSV1_MSK GENMASK(5, 0)
258 /* ADF4377 REG0025 Bit Definition */
259 #define ADF4377_0025_R025_RSV1 0x16
261 /* ADF4377 REG0026 Map */
262 #define ADF4377_0026_VCO_BAND_DIV_MSK GENMASK(7, 0)
264 /* ADF4377 REG0027 Map */
265 #define ADF4377_0027_SYNTH_LOCK_TO_LSB_MSK GENMASK(7, 0)
267 /* ADF4377 REG0028 Map */
268 #define ADF4377_0028_O_VCO_DB_MSK BIT(7)
269 #define ADF4377_0028_SYNTH_LOCK_TO_MSB_MSK GENMASK(6, 0)
271 /* ADF4377 REG0029 Map */
272 #define ADF4377_0029_VCO_ALC_TO_LSB_MSK GENMASK(7, 0)
274 /* ADF4377 REG002A Map */
275 #define ADF4377_002A_DEL_CTRL_DB_MSK BIT(7)
276 #define ADF4377_002A_VCO_ALC_TO_MSB_MSK GENMASK(6, 0)
278 /* ADF4377 REG002C Map */
279 #define ADF4377_002C_R02C_RSV1 0xC0
281 /* ADF4377 REG002D Map */
282 #define ADF4377_002D_ADC_CLK_DIV_MSK GENMASK(7, 0)
284 /* ADF4377 REG002E Map */
285 #define ADF4377_002E_EN_ADC_CNV_MSK BIT(7)
286 #define ADF4377_002E_EN_ADC_MSK BIT(1)
287 #define ADF4377_002E_ADC_A_CONV_MSK BIT(0)
289 /* ADF4377 REG002E Bit Definition */
290 #define ADF4377_002E_ADC_A_CONV_ADC_ST_CNV 0x0
291 #define ADF4377_002E_ADC_A_CONV_VCO_CALIB 0x1
293 /* ADF4377 REG002F Map */
294 #define ADF4377_002F_DCLK_DIV1_MSK GENMASK(1, 0)
296 /* ADF4377 REG002F Bit Definition */
297 #define ADF4377_002F_DCLK_DIV1_1 0x0
298 #define ADF4377_002F_DCLK_DIV1_2 0x1
299 #define ADF4377_002F_DCLK_DIV1_8 0x2
300 #define ADF4377_002F_DCLK_DIV1_32 0x3
302 /* ADF4377 REG0031 Bit Definition */
303 #define ADF4377_0031_R031_RSV1 0x09
305 /* ADF4377 REG0032 Map */
306 #define ADF4377_0032_ADC_CLK_SEL_MSK BIT(6)
307 #define ADF4377_0032_R032_RSV1_MSK GENMASK(5, 0)
309 /* ADF4377 REG0032 Bit Definition */
310 #define ADF4377_0032_ADC_CLK_SEL_N_OP 0x0
311 #define ADF4377_0032_ADC_CLK_SEL_SPI_CLK 0x1
313 #define ADF4377_0032_R032_RSV1 0x9
315 /* ADF4377 REG0033 Bit Definition */
316 #define ADF4377_0033_R033_RSV1 0x18
318 /* ADF4377 REG0034 Bit Definition */
319 #define ADF4377_0034_R034_RSV1 0x08
321 /* ADF4377 REG003A Bit Definition */
322 #define ADF4377_003A_R03A_RSV1 0x5D
324 /* ADF4377 REG003B Bit Definition */
325 #define ADF4377_003B_R03B_RSV1 0x2B
327 /* ADF4377 REG003D Map */
328 #define ADF4377_003D_O_VCO_BAND_MSK BIT(3)
329 #define ADF4377_003D_O_VCO_CORE_MSK BIT(2)
330 #define ADF4377_003D_O_VCO_BIAS_MSK BIT(1)
332 /* ADF4377 REG003D Bit Definition */
333 #define ADF4377_003D_O_VCO_BAND_VCO_CALIB 0x0
334 #define ADF4377_003D_O_VCO_BAND_M_VCO 0x1
336 #define ADF4377_003D_O_VCO_CORE_VCO_CALIB 0x0
337 #define ADF4377_003D_O_VCO_CORE_M_VCO 0x1
339 #define ADF4377_003D_O_VCO_BIAS_VCO_CALIB 0x0
340 #define ADF4377_003D_O_VCO_BIAS_M_VCO 0x1
342 /* ADF4377 REG0042 Map */
343 #define ADF4377_0042_R042_RSV1 0x05
345 /* ADF4377 REG0045 Map */
346 #define ADF4377_0045_ADC_ST_CNV_MSK BIT(0)
348 /* ADF4377 REG0049 Map */
349 #define ADF4377_0049_EN_CLK2_MSK BIT(7)
350 #define ADF4377_0049_EN_CLK1_MSK BIT(6)
351 #define ADF4377_0049_REF_OK_MSK BIT(3)
352 #define ADF4377_0049_ADC_BUSY_MSK BIT(2)
353 #define ADF4377_0049_FSM_BUSY_MSK BIT(1)
354 #define ADF4377_0049_LOCKED_MSK BIT(0)
356 /* ADF4377 REG004B Map */
357 #define ADF4377_004B_VCO_CORE_MSK GENMASK(1, 0)
359 /* ADF4377 REG004C Map */
360 #define ADF4377_004C_CHIP_TEMP_LSB_MSK GENMASK(7, 0)
362 /* ADF4377 REG004D Map */
363 #define ADF4377_004D_CHIP_TEMP_MSB_MSK BIT(0)
365 /* ADF4377 REG004F Map */
366 #define ADF4377_004F_VCO_BAND_MSK GENMASK(7, 0)
368 /* ADF4377 REG0051 Map */
369 #define ADF4377_0051_VCO_BIAS_MSK GENMASK(3, 0)
371 /* ADF4377 REG0054 Map */
372 #define ADF4377_0054_CHIP_VERSION_MSK GENMASK(7, 0)
375 #define ADF4377_SPI_READ_CMD BIT(7)
376 #define ADF4377_MAX_VCO_FREQ (12800ULL * HZ_PER_MHZ)
377 #define ADF4377_MIN_VCO_FREQ (6400ULL * HZ_PER_MHZ)
378 #define ADF4377_MAX_REFIN_FREQ (1000 * HZ_PER_MHZ)
379 #define ADF4377_MIN_REFIN_FREQ (10 * HZ_PER_MHZ)
380 #define ADF4377_MAX_FREQ_PFD (500 * HZ_PER_MHZ)
381 #define ADF4377_MIN_FREQ_PFD (3 * HZ_PER_MHZ)
382 #define ADF4377_MAX_CLKPN_FREQ ADF4377_MAX_VCO_FREQ
383 #define ADF4377_MIN_CLKPN_FREQ (ADF4377_MIN_VCO_FREQ / 8)
384 #define ADF4377_FREQ_PFD_80MHZ (80 * HZ_PER_MHZ)
385 #define ADF4377_FREQ_PFD_125MHZ (125 * HZ_PER_MHZ)
386 #define ADF4377_FREQ_PFD_160MHZ (160 * HZ_PER_MHZ)
387 #define ADF4377_FREQ_PFD_250MHZ (250 * HZ_PER_MHZ)
388 #define ADF4377_FREQ_PFD_320MHZ (320 * HZ_PER_MHZ)
394 enum muxout_select_mode
{
395 ADF4377_MUXOUT_HIGH_Z
= 0x0,
396 ADF4377_MUXOUT_LKDET
= 0x1,
397 ADF4377_MUXOUT_LOW
= 0x2,
398 ADF4377_MUXOUT_DIV_RCLK_2
= 0x4,
399 ADF4377_MUXOUT_DIV_NCLK_2
= 0x5,
400 ADF4377_MUXOUT_HIGH
= 0x8,
403 struct adf4377_chip_info
{
405 bool has_gpio_enclk2
;
408 struct adf4377_state
{
409 const struct adf4377_chip_info
*chip_info
;
410 struct spi_device
*spi
;
411 struct regmap
*regmap
;
413 /* Protect against concurrent accesses to the device and data content */
415 struct notifier_block nb
;
416 /* Reference Divider */
417 unsigned int ref_div_factor
;
420 /* Input Reference Clock */
421 unsigned int clkin_freq
;
424 /* Feedback Divider (N) */
426 u16 synth_lock_timeout
;
433 unsigned int f_div_rclk
;
434 enum muxout_select_mode muxout_select
;
435 struct gpio_desc
*gpio_ce
;
436 struct gpio_desc
*gpio_enclk1
;
437 struct gpio_desc
*gpio_enclk2
;
438 u8 buf
[2] __aligned(IIO_DMA_MINALIGN
);
441 static const char * const adf4377_muxout_modes
[] = {
442 [ADF4377_MUXOUT_HIGH_Z
] = "high_z",
443 [ADF4377_MUXOUT_LKDET
] = "lock_detect",
444 [ADF4377_MUXOUT_LOW
] = "muxout_low",
445 [ADF4377_MUXOUT_DIV_RCLK_2
] = "f_div_rclk_2",
446 [ADF4377_MUXOUT_DIV_NCLK_2
] = "f_div_nclk_2",
447 [ADF4377_MUXOUT_HIGH
] = "muxout_high",
450 static const struct reg_sequence adf4377_reg_defaults
[] = {
451 { 0x42, ADF4377_0042_R042_RSV1
},
452 { 0x3B, ADF4377_003B_R03B_RSV1
},
453 { 0x3A, ADF4377_003A_R03A_RSV1
},
454 { 0x34, ADF4377_0034_R034_RSV1
},
455 { 0x33, ADF4377_0033_R033_RSV1
},
456 { 0x32, ADF4377_0032_R032_RSV1
},
457 { 0x31, ADF4377_0031_R031_RSV1
},
458 { 0x2C, ADF4377_002C_R02C_RSV1
},
459 { 0x25, ADF4377_0025_R025_RSV1
},
460 { 0x23, ADF4377_0023_R023_RSV1
},
461 { 0x22, ADF4377_0022_R022_RSV1
},
462 { 0x21, ADF4377_0021_R021_RSV1
},
463 { 0x1f, ADF4377_001F_R01F_RSV1
},
464 { 0x1c, ADF4377_001C_R01C_RSV1
},
467 static const struct regmap_config adf4377_regmap_config
= {
470 .read_flag_mask
= BIT(7),
471 .max_register
= 0x54,
474 static int adf4377_reg_access(struct iio_dev
*indio_dev
,
476 unsigned int write_val
,
477 unsigned int *read_val
)
479 struct adf4377_state
*st
= iio_priv(indio_dev
);
482 return regmap_read(st
->regmap
, reg
, read_val
);
484 return regmap_write(st
->regmap
, reg
, write_val
);
487 static const struct iio_info adf4377_info
= {
488 .debugfs_reg_access
= &adf4377_reg_access
,
491 static int adf4377_soft_reset(struct adf4377_state
*st
)
493 unsigned int read_val
;
496 ret
= regmap_update_bits(st
->regmap
, 0x0, ADF4377_0000_SOFT_RESET_MSK
|
497 ADF4377_0000_SOFT_RESET_R_MSK
,
498 FIELD_PREP(ADF4377_0000_SOFT_RESET_MSK
, 1) |
499 FIELD_PREP(ADF4377_0000_SOFT_RESET_R_MSK
, 1));
503 return regmap_read_poll_timeout(st
->regmap
, 0x0, read_val
,
504 !(read_val
& (ADF4377_0000_SOFT_RESET_R_MSK
|
505 ADF4377_0000_SOFT_RESET_R_MSK
)), 200, 200 * 100);
508 static int adf4377_get_freq(struct adf4377_state
*st
, u64
*freq
)
510 unsigned int ref_div_factor
, n_int
;
514 mutex_lock(&st
->lock
);
515 ret
= regmap_read(st
->regmap
, 0x12, &ref_div_factor
);
519 ret
= regmap_bulk_read(st
->regmap
, 0x10, st
->buf
, sizeof(st
->buf
));
523 clkin_freq
= clk_get_rate(st
->clkin
);
524 ref_div_factor
= FIELD_GET(ADF4377_0012_R_DIV_MSK
, ref_div_factor
);
525 n_int
= FIELD_GET(ADF4377_0010_N_INT_LSB_MSK
| ADF4377_0011_N_INT_MSB_MSK
,
526 get_unaligned_le16(&st
->buf
));
528 *freq
= div_u64(clkin_freq
, ref_div_factor
) * n_int
;
530 mutex_unlock(&st
->lock
);
535 static int adf4377_set_freq(struct adf4377_state
*st
, u64 freq
)
537 unsigned int read_val
;
541 mutex_lock(&st
->lock
);
543 if (freq
> ADF4377_MAX_CLKPN_FREQ
|| freq
< ADF4377_MIN_CLKPN_FREQ
) {
548 ret
= regmap_update_bits(st
->regmap
, 0x1C, ADF4377_001C_EN_DNCLK_MSK
|
549 ADF4377_001C_EN_DRCLK_MSK
,
550 FIELD_PREP(ADF4377_001C_EN_DNCLK_MSK
, 1) |
551 FIELD_PREP(ADF4377_001C_EN_DRCLK_MSK
, 1));
555 ret
= regmap_update_bits(st
->regmap
, 0x11, ADF4377_0011_EN_AUTOCAL_MSK
|
556 ADF4377_0011_DCLK_DIV2_MSK
,
557 FIELD_PREP(ADF4377_0011_EN_AUTOCAL_MSK
, 1) |
558 FIELD_PREP(ADF4377_0011_DCLK_DIV2_MSK
, st
->dclk_div2
));
562 ret
= regmap_update_bits(st
->regmap
, 0x2E, ADF4377_002E_EN_ADC_CNV_MSK
|
563 ADF4377_002E_EN_ADC_MSK
|
564 ADF4377_002E_ADC_A_CONV_MSK
,
565 FIELD_PREP(ADF4377_002E_EN_ADC_CNV_MSK
, 1) |
566 FIELD_PREP(ADF4377_002E_EN_ADC_MSK
, 1) |
567 FIELD_PREP(ADF4377_002E_ADC_A_CONV_MSK
,
568 ADF4377_002E_ADC_A_CONV_VCO_CALIB
));
572 ret
= regmap_update_bits(st
->regmap
, 0x20, ADF4377_0020_EN_ADC_CLK_MSK
,
573 FIELD_PREP(ADF4377_0020_EN_ADC_CLK_MSK
, 1));
577 ret
= regmap_update_bits(st
->regmap
, 0x2F, ADF4377_002F_DCLK_DIV1_MSK
,
578 FIELD_PREP(ADF4377_002F_DCLK_DIV1_MSK
, st
->dclk_div1
));
582 ret
= regmap_update_bits(st
->regmap
, 0x24, ADF4377_0024_DCLK_MODE_MSK
,
583 FIELD_PREP(ADF4377_0024_DCLK_MODE_MSK
, st
->dclk_mode
));
587 ret
= regmap_write(st
->regmap
, 0x27,
588 FIELD_PREP(ADF4377_0027_SYNTH_LOCK_TO_LSB_MSK
,
589 st
->synth_lock_timeout
));
593 ret
= regmap_update_bits(st
->regmap
, 0x28, ADF4377_0028_SYNTH_LOCK_TO_MSB_MSK
,
594 FIELD_PREP(ADF4377_0028_SYNTH_LOCK_TO_MSB_MSK
,
595 st
->synth_lock_timeout
>> 8));
599 ret
= regmap_write(st
->regmap
, 0x29,
600 FIELD_PREP(ADF4377_0029_VCO_ALC_TO_LSB_MSK
,
601 st
->vco_alc_timeout
));
605 ret
= regmap_update_bits(st
->regmap
, 0x2A, ADF4377_002A_VCO_ALC_TO_MSB_MSK
,
606 FIELD_PREP(ADF4377_002A_VCO_ALC_TO_MSB_MSK
,
607 st
->vco_alc_timeout
>> 8));
611 ret
= regmap_write(st
->regmap
, 0x26,
612 FIELD_PREP(ADF4377_0026_VCO_BAND_DIV_MSK
, st
->vco_band_div
));
616 ret
= regmap_write(st
->regmap
, 0x2D,
617 FIELD_PREP(ADF4377_002D_ADC_CLK_DIV_MSK
, st
->adc_clk_div
));
621 st
->clkout_div_sel
= 0;
625 while (f_vco
< ADF4377_MIN_VCO_FREQ
) {
627 st
->clkout_div_sel
++;
630 st
->n_int
= div_u64(freq
, st
->f_pfd
);
632 ret
= regmap_update_bits(st
->regmap
, 0x11, ADF4377_0011_EN_RDBLR_MSK
|
633 ADF4377_0011_N_INT_MSB_MSK
,
634 FIELD_PREP(ADF4377_0011_EN_RDBLR_MSK
, 0) |
635 FIELD_PREP(ADF4377_0011_N_INT_MSB_MSK
, st
->n_int
>> 8));
639 ret
= regmap_update_bits(st
->regmap
, 0x12, ADF4377_0012_R_DIV_MSK
|
640 ADF4377_0012_CLKOUT_DIV_MSK
,
641 FIELD_PREP(ADF4377_0012_CLKOUT_DIV_MSK
, st
->clkout_div_sel
) |
642 FIELD_PREP(ADF4377_0012_R_DIV_MSK
, st
->ref_div_factor
));
646 ret
= regmap_write(st
->regmap
, 0x10,
647 FIELD_PREP(ADF4377_0010_N_INT_LSB_MSK
, st
->n_int
));
651 ret
= regmap_read_poll_timeout(st
->regmap
, 0x49, read_val
,
652 !(read_val
& (ADF4377_0049_FSM_BUSY_MSK
)), 200, 200 * 100);
656 /* Disable EN_DNCLK, EN_DRCLK */
657 ret
= regmap_update_bits(st
->regmap
, 0x1C, ADF4377_001C_EN_DNCLK_MSK
|
658 ADF4377_001C_EN_DRCLK_MSK
,
659 FIELD_PREP(ADF4377_001C_EN_DNCLK_MSK
, 0) |
660 FIELD_PREP(ADF4377_001C_EN_DRCLK_MSK
, 0));
664 /* Disable EN_ADC_CLK */
665 ret
= regmap_update_bits(st
->regmap
, 0x20, ADF4377_0020_EN_ADC_CLK_MSK
,
666 FIELD_PREP(ADF4377_0020_EN_ADC_CLK_MSK
, 0));
670 /* Set output Amplitude */
671 ret
= regmap_update_bits(st
->regmap
, 0x19, ADF4377_0019_CLKOUT2_OP_MSK
|
672 ADF4377_0019_CLKOUT1_OP_MSK
,
673 FIELD_PREP(ADF4377_0019_CLKOUT1_OP_MSK
,
674 ADF4377_0019_CLKOUT_420MV
) |
675 FIELD_PREP(ADF4377_0019_CLKOUT2_OP_MSK
,
676 ADF4377_0019_CLKOUT_420MV
));
679 mutex_unlock(&st
->lock
);
684 static void adf4377_gpio_init(struct adf4377_state
*st
)
687 gpiod_set_value(st
->gpio_ce
, 1);
689 /* Delay for SPI register bits to settle to their power-on reset state */
694 gpiod_set_value(st
->gpio_enclk1
, 1);
697 gpiod_set_value(st
->gpio_enclk2
, 1);
700 static int adf4377_init(struct adf4377_state
*st
)
702 struct spi_device
*spi
= st
->spi
;
705 adf4377_gpio_init(st
);
707 ret
= adf4377_soft_reset(st
);
709 dev_err(&spi
->dev
, "Failed to soft reset.\n");
713 ret
= regmap_multi_reg_write(st
->regmap
, adf4377_reg_defaults
,
714 ARRAY_SIZE(adf4377_reg_defaults
));
716 dev_err(&spi
->dev
, "Failed to set default registers.\n");
720 ret
= regmap_update_bits(st
->regmap
, 0x00,
721 ADF4377_0000_SDO_ACTIVE_MSK
| ADF4377_0000_SDO_ACTIVE_R_MSK
,
722 FIELD_PREP(ADF4377_0000_SDO_ACTIVE_MSK
,
723 ADF4377_0000_SDO_ACTIVE_SPI_4W
) |
724 FIELD_PREP(ADF4377_0000_SDO_ACTIVE_R_MSK
,
725 ADF4377_0000_SDO_ACTIVE_SPI_4W
));
727 dev_err(&spi
->dev
, "Failed to set 4-Wire Operation.\n");
731 st
->clkin_freq
= clk_get_rate(st
->clkin
);
734 ret
= regmap_write(st
->regmap
, 0x1a,
735 FIELD_PREP(ADF4377_001A_PD_ALL_MSK
, 0) |
736 FIELD_PREP(ADF4377_001A_PD_RDIV_MSK
, 0) |
737 FIELD_PREP(ADF4377_001A_PD_NDIV_MSK
, 0) |
738 FIELD_PREP(ADF4377_001A_PD_VCO_MSK
, 0) |
739 FIELD_PREP(ADF4377_001A_PD_LD_MSK
, 0) |
740 FIELD_PREP(ADF4377_001A_PD_PFDCP_MSK
, 0) |
741 FIELD_PREP(ADF4377_001A_PD_CLKOUT1_MSK
, 0) |
742 FIELD_PREP(ADF4377_001A_PD_CLKOUT2_MSK
, 0));
744 dev_err(&spi
->dev
, "Failed to set power down registers.\n");
749 ret
= regmap_update_bits(st
->regmap
, 0x1D,
750 ADF4377_001D_MUXOUT_MSK
,
751 FIELD_PREP(ADF4377_001D_MUXOUT_MSK
, st
->muxout_select
));
756 st
->ref_div_factor
= 0;
758 st
->ref_div_factor
++;
759 st
->f_pfd
= st
->clkin_freq
/ st
->ref_div_factor
;
760 } while (st
->f_pfd
> ADF4377_MAX_FREQ_PFD
);
762 if (st
->f_pfd
> ADF4377_MAX_FREQ_PFD
|| st
->f_pfd
< ADF4377_MIN_FREQ_PFD
)
765 st
->f_div_rclk
= st
->f_pfd
;
767 if (st
->f_pfd
<= ADF4377_FREQ_PFD_80MHZ
) {
768 st
->dclk_div1
= ADF4377_002F_DCLK_DIV1_1
;
769 st
->dclk_div2
= ADF4377_0011_DCLK_DIV2_1
;
771 } else if (st
->f_pfd
<= ADF4377_FREQ_PFD_125MHZ
) {
772 st
->dclk_div1
= ADF4377_002F_DCLK_DIV1_1
;
773 st
->dclk_div2
= ADF4377_0011_DCLK_DIV2_1
;
775 } else if (st
->f_pfd
<= ADF4377_FREQ_PFD_160MHZ
) {
776 st
->dclk_div1
= ADF4377_002F_DCLK_DIV1_2
;
777 st
->dclk_div2
= ADF4377_0011_DCLK_DIV2_1
;
780 } else if (st
->f_pfd
<= ADF4377_FREQ_PFD_250MHZ
) {
781 st
->dclk_div1
= ADF4377_002F_DCLK_DIV1_2
;
782 st
->dclk_div2
= ADF4377_0011_DCLK_DIV2_1
;
785 } else if (st
->f_pfd
<= ADF4377_FREQ_PFD_320MHZ
) {
786 st
->dclk_div1
= ADF4377_002F_DCLK_DIV1_2
;
787 st
->dclk_div2
= ADF4377_0011_DCLK_DIV2_2
;
791 st
->dclk_div1
= ADF4377_002F_DCLK_DIV1_2
;
792 st
->dclk_div2
= ADF4377_0011_DCLK_DIV2_2
;
797 st
->synth_lock_timeout
= DIV_ROUND_UP(st
->f_div_rclk
, 50000);
798 st
->vco_alc_timeout
= DIV_ROUND_UP(st
->f_div_rclk
, 20000);
799 st
->vco_band_div
= DIV_ROUND_UP(st
->f_div_rclk
, 150000 * 16 * (1 << st
->dclk_mode
));
800 st
->adc_clk_div
= DIV_ROUND_UP((st
->f_div_rclk
/ 400000 - 2), 4);
805 static ssize_t
adf4377_read(struct iio_dev
*indio_dev
, uintptr_t private,
806 const struct iio_chan_spec
*chan
, char *buf
)
808 struct adf4377_state
*st
= iio_priv(indio_dev
);
812 switch ((u32
)private) {
814 ret
= adf4377_get_freq(st
, &val
);
818 return sysfs_emit(buf
, "%llu\n", val
);
824 static ssize_t
adf4377_write(struct iio_dev
*indio_dev
, uintptr_t private,
825 const struct iio_chan_spec
*chan
, const char *buf
,
828 struct adf4377_state
*st
= iio_priv(indio_dev
);
829 unsigned long long freq
;
832 switch ((u32
)private) {
834 ret
= kstrtoull(buf
, 10, &freq
);
838 ret
= adf4377_set_freq(st
, freq
);
848 #define _ADF4377_EXT_INFO(_name, _shared, _ident) { \
850 .read = adf4377_read, \
851 .write = adf4377_write, \
856 static const struct iio_chan_spec_ext_info adf4377_ext_info
[] = {
858 * Usually we use IIO_CHAN_INFO_FREQUENCY, but there are
859 * values > 2^32 in order to support the entire frequency range
862 _ADF4377_EXT_INFO("frequency", IIO_SEPARATE
, ADF4377_FREQ
),
866 static const struct iio_chan_spec adf4377_channels
[] = {
868 .type
= IIO_ALTVOLTAGE
,
872 .ext_info
= adf4377_ext_info
,
876 static int adf4377_properties_parse(struct adf4377_state
*st
)
878 struct spi_device
*spi
= st
->spi
;
881 st
->clkin
= devm_clk_get_enabled(&spi
->dev
, "ref_in");
882 if (IS_ERR(st
->clkin
))
883 return dev_err_probe(&spi
->dev
, PTR_ERR(st
->clkin
),
884 "failed to get the reference input clock\n");
886 st
->gpio_ce
= devm_gpiod_get_optional(&st
->spi
->dev
, "chip-enable",
888 if (IS_ERR(st
->gpio_ce
))
889 return dev_err_probe(&spi
->dev
, PTR_ERR(st
->gpio_ce
),
890 "failed to get the CE GPIO\n");
892 st
->gpio_enclk1
= devm_gpiod_get_optional(&st
->spi
->dev
, "clk1-enable",
894 if (IS_ERR(st
->gpio_enclk1
))
895 return dev_err_probe(&spi
->dev
, PTR_ERR(st
->gpio_enclk1
),
896 "failed to get the CE GPIO\n");
898 if (st
->chip_info
->has_gpio_enclk2
) {
899 st
->gpio_enclk2
= devm_gpiod_get_optional(&st
->spi
->dev
, "clk2-enable",
901 if (IS_ERR(st
->gpio_enclk2
))
902 return dev_err_probe(&spi
->dev
, PTR_ERR(st
->gpio_enclk2
),
903 "failed to get the CE GPIO\n");
906 ret
= device_property_match_property_string(&spi
->dev
, "adi,muxout-select",
907 adf4377_muxout_modes
,
908 ARRAY_SIZE(adf4377_muxout_modes
));
910 st
->muxout_select
= ret
;
912 st
->muxout_select
= ADF4377_MUXOUT_HIGH_Z
;
917 static int adf4377_freq_change(struct notifier_block
*nb
, unsigned long action
, void *data
)
919 struct adf4377_state
*st
= container_of(nb
, struct adf4377_state
, nb
);
922 if (action
== POST_RATE_CHANGE
) {
923 mutex_lock(&st
->lock
);
924 ret
= notifier_from_errno(adf4377_init(st
));
925 mutex_unlock(&st
->lock
);
932 static const struct adf4377_chip_info adf4377_chip_info
= {
934 .has_gpio_enclk2
= true,
937 static const struct adf4377_chip_info adf4378_chip_info
= {
939 .has_gpio_enclk2
= false,
942 static int adf4377_probe(struct spi_device
*spi
)
944 struct iio_dev
*indio_dev
;
945 struct regmap
*regmap
;
946 struct adf4377_state
*st
;
949 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
953 regmap
= devm_regmap_init_spi(spi
, &adf4377_regmap_config
);
955 return PTR_ERR(regmap
);
957 st
= iio_priv(indio_dev
);
959 indio_dev
->info
= &adf4377_info
;
960 indio_dev
->name
= "adf4377";
961 indio_dev
->channels
= adf4377_channels
;
962 indio_dev
->num_channels
= ARRAY_SIZE(adf4377_channels
);
966 st
->chip_info
= spi_get_device_match_data(spi
);
967 mutex_init(&st
->lock
);
969 ret
= adf4377_properties_parse(st
);
973 st
->nb
.notifier_call
= adf4377_freq_change
;
974 ret
= devm_clk_notifier_register(&spi
->dev
, st
->clkin
, &st
->nb
);
978 ret
= adf4377_init(st
);
982 return devm_iio_device_register(&spi
->dev
, indio_dev
);
985 static const struct spi_device_id adf4377_id
[] = {
986 { "adf4377", (kernel_ulong_t
)&adf4377_chip_info
},
987 { "adf4378", (kernel_ulong_t
)&adf4378_chip_info
},
990 MODULE_DEVICE_TABLE(spi
, adf4377_id
);
992 static const struct of_device_id adf4377_of_match
[] = {
993 { .compatible
= "adi,adf4377", .data
= &adf4377_chip_info
},
994 { .compatible
= "adi,adf4378", .data
= &adf4378_chip_info
},
997 MODULE_DEVICE_TABLE(of
, adf4377_of_match
);
999 static struct spi_driver adf4377_driver
= {
1002 .of_match_table
= adf4377_of_match
,
1004 .probe
= adf4377_probe
,
1005 .id_table
= adf4377_id
,
1007 module_spi_driver(adf4377_driver
);
1009 MODULE_AUTHOR("Antoniu Miclaus <antoniu.miclaus@analog.com>");
1010 MODULE_DESCRIPTION("Analog Devices ADF4377");
1011 MODULE_LICENSE("GPL");