1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright 2021 Analog Devices Inc.
8 #include <linux/bitfield.h>
9 #include <linux/bits.h>
10 #include <linux/clk.h>
11 #include <linux/device.h>
12 #include <linux/iio/iio.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/mutex.h>
16 #include <linux/notifier.h>
17 #include <linux/regmap.h>
18 #include <linux/spi/spi.h>
19 #include <linux/units.h>
21 /* ADMV8818 Register Map */
22 #define ADMV8818_REG_SPI_CONFIG_A 0x0
23 #define ADMV8818_REG_SPI_CONFIG_B 0x1
24 #define ADMV8818_REG_CHIPTYPE 0x3
25 #define ADMV8818_REG_PRODUCT_ID_L 0x4
26 #define ADMV8818_REG_PRODUCT_ID_H 0x5
27 #define ADMV8818_REG_FAST_LATCH_POINTER 0x10
28 #define ADMV8818_REG_FAST_LATCH_STOP 0x11
29 #define ADMV8818_REG_FAST_LATCH_START 0x12
30 #define ADMV8818_REG_FAST_LATCH_DIRECTION 0x13
31 #define ADMV8818_REG_FAST_LATCH_STATE 0x14
32 #define ADMV8818_REG_WR0_SW 0x20
33 #define ADMV8818_REG_WR0_FILTER 0x21
34 #define ADMV8818_REG_WR1_SW 0x22
35 #define ADMV8818_REG_WR1_FILTER 0x23
36 #define ADMV8818_REG_WR2_SW 0x24
37 #define ADMV8818_REG_WR2_FILTER 0x25
38 #define ADMV8818_REG_WR3_SW 0x26
39 #define ADMV8818_REG_WR3_FILTER 0x27
40 #define ADMV8818_REG_WR4_SW 0x28
41 #define ADMV8818_REG_WR4_FILTER 0x29
42 #define ADMV8818_REG_LUT0_SW 0x100
43 #define ADMV8818_REG_LUT0_FILTER 0x101
44 #define ADMV8818_REG_LUT127_SW 0x1FE
45 #define ADMV8818_REG_LUT127_FILTER 0x1FF
47 /* ADMV8818_REG_SPI_CONFIG_A Map */
48 #define ADMV8818_SOFTRESET_N_MSK BIT(7)
49 #define ADMV8818_LSB_FIRST_N_MSK BIT(6)
50 #define ADMV8818_ENDIAN_N_MSK BIT(5)
51 #define ADMV8818_SDOACTIVE_N_MSK BIT(4)
52 #define ADMV8818_SDOACTIVE_MSK BIT(3)
53 #define ADMV8818_ENDIAN_MSK BIT(2)
54 #define ADMV8818_LSBFIRST_MSK BIT(1)
55 #define ADMV8818_SOFTRESET_MSK BIT(0)
57 /* ADMV8818_REG_SPI_CONFIG_B Map */
58 #define ADMV8818_SINGLE_INSTRUCTION_MSK BIT(7)
59 #define ADMV8818_CSB_STALL_MSK BIT(6)
60 #define ADMV8818_MASTER_SLAVE_RB_MSK BIT(5)
61 #define ADMV8818_MASTER_SLAVE_TRANSFER_MSK BIT(0)
63 /* ADMV8818_REG_WR0_SW Map */
64 #define ADMV8818_SW_IN_SET_WR0_MSK BIT(7)
65 #define ADMV8818_SW_OUT_SET_WR0_MSK BIT(6)
66 #define ADMV8818_SW_IN_WR0_MSK GENMASK(5, 3)
67 #define ADMV8818_SW_OUT_WR0_MSK GENMASK(2, 0)
69 /* ADMV8818_REG_WR0_FILTER Map */
70 #define ADMV8818_HPF_WR0_MSK GENMASK(7, 4)
71 #define ADMV8818_LPF_WR0_MSK GENMASK(3, 0)
84 struct admv8818_state
{
85 struct spi_device
*spi
;
86 struct regmap
*regmap
;
88 struct notifier_block nb
;
89 /* Protect against concurrent accesses to the device and data content*/
91 unsigned int filter_mode
;
95 static const unsigned long long freq_range_hpf
[4][2] = {
96 {1750000000ULL, 3550000000ULL},
97 {3400000000ULL, 7250000000ULL},
98 {6600000000, 12000000000},
99 {12500000000, 19900000000}
102 static const unsigned long long freq_range_lpf
[4][2] = {
103 {2050000000ULL, 3850000000ULL},
104 {3350000000ULL, 7250000000ULL},
105 {7000000000, 13000000000},
106 {12550000000, 18500000000}
109 static const struct regmap_config admv8818_regmap_config
= {
112 .read_flag_mask
= 0x80,
113 .max_register
= 0x1FF,
116 static const char * const admv8818_modes
[] = {
122 static int __admv8818_hpf_select(struct admv8818_state
*st
, u64 freq
)
124 unsigned int hpf_step
= 0, hpf_band
= 0, i
, j
;
128 if (freq
< freq_range_hpf
[0][0])
131 if (freq
> freq_range_hpf
[3][1]) {
138 for (i
= 0; i
< 4; i
++) {
139 freq_step
= div_u64((freq_range_hpf
[i
][1] -
140 freq_range_hpf
[i
][0]), 15);
142 if (freq
> freq_range_hpf
[i
][0] &&
143 (freq
< freq_range_hpf
[i
][1] + freq_step
)) {
146 for (j
= 1; j
<= 16; j
++) {
147 if (freq
< (freq_range_hpf
[i
][0] + (freq_step
* j
))) {
156 /* Close HPF frequency gap between 12 and 12.5 GHz */
157 if (freq
>= 12000 * HZ_PER_MHZ
&& freq
<= 12500 * HZ_PER_MHZ
) {
163 ret
= regmap_update_bits(st
->regmap
, ADMV8818_REG_WR0_SW
,
164 ADMV8818_SW_IN_SET_WR0_MSK
|
165 ADMV8818_SW_IN_WR0_MSK
,
166 FIELD_PREP(ADMV8818_SW_IN_SET_WR0_MSK
, 1) |
167 FIELD_PREP(ADMV8818_SW_IN_WR0_MSK
, hpf_band
));
171 return regmap_update_bits(st
->regmap
, ADMV8818_REG_WR0_FILTER
,
172 ADMV8818_HPF_WR0_MSK
,
173 FIELD_PREP(ADMV8818_HPF_WR0_MSK
, hpf_step
));
176 static int admv8818_hpf_select(struct admv8818_state
*st
, u64 freq
)
180 mutex_lock(&st
->lock
);
181 ret
= __admv8818_hpf_select(st
, freq
);
182 mutex_unlock(&st
->lock
);
187 static int __admv8818_lpf_select(struct admv8818_state
*st
, u64 freq
)
189 unsigned int lpf_step
= 0, lpf_band
= 0, i
, j
;
193 if (freq
> freq_range_lpf
[3][1])
196 if (freq
< freq_range_lpf
[0][0]) {
202 for (i
= 0; i
< 4; i
++) {
203 if (freq
> freq_range_lpf
[i
][0] && freq
< freq_range_lpf
[i
][1]) {
205 freq_step
= div_u64((freq_range_lpf
[i
][1] - freq_range_lpf
[i
][0]), 15);
207 for (j
= 0; j
<= 15; j
++) {
208 if (freq
< (freq_range_lpf
[i
][0] + (freq_step
* j
))) {
218 ret
= regmap_update_bits(st
->regmap
, ADMV8818_REG_WR0_SW
,
219 ADMV8818_SW_OUT_SET_WR0_MSK
|
220 ADMV8818_SW_OUT_WR0_MSK
,
221 FIELD_PREP(ADMV8818_SW_OUT_SET_WR0_MSK
, 1) |
222 FIELD_PREP(ADMV8818_SW_OUT_WR0_MSK
, lpf_band
));
226 return regmap_update_bits(st
->regmap
, ADMV8818_REG_WR0_FILTER
,
227 ADMV8818_LPF_WR0_MSK
,
228 FIELD_PREP(ADMV8818_LPF_WR0_MSK
, lpf_step
));
231 static int admv8818_lpf_select(struct admv8818_state
*st
, u64 freq
)
235 mutex_lock(&st
->lock
);
236 ret
= __admv8818_lpf_select(st
, freq
);
237 mutex_unlock(&st
->lock
);
242 static int admv8818_rfin_band_select(struct admv8818_state
*st
)
246 st
->cf_hz
= clk_get_rate(st
->clkin
);
248 mutex_lock(&st
->lock
);
250 ret
= __admv8818_hpf_select(st
, st
->cf_hz
);
254 ret
= __admv8818_lpf_select(st
, st
->cf_hz
);
256 mutex_unlock(&st
->lock
);
260 static int __admv8818_read_hpf_freq(struct admv8818_state
*st
, u64
*hpf_freq
)
262 unsigned int data
, hpf_band
, hpf_state
;
265 ret
= regmap_read(st
->regmap
, ADMV8818_REG_WR0_SW
, &data
);
269 hpf_band
= FIELD_GET(ADMV8818_SW_IN_WR0_MSK
, data
);
270 if (!hpf_band
|| hpf_band
> 4) {
275 ret
= regmap_read(st
->regmap
, ADMV8818_REG_WR0_FILTER
, &data
);
279 hpf_state
= FIELD_GET(ADMV8818_HPF_WR0_MSK
, data
);
281 *hpf_freq
= div_u64(freq_range_hpf
[hpf_band
- 1][1] - freq_range_hpf
[hpf_band
- 1][0], 15);
282 *hpf_freq
= freq_range_hpf
[hpf_band
- 1][0] + (*hpf_freq
* hpf_state
);
287 static int admv8818_read_hpf_freq(struct admv8818_state
*st
, u64
*hpf_freq
)
291 mutex_lock(&st
->lock
);
292 ret
= __admv8818_read_hpf_freq(st
, hpf_freq
);
293 mutex_unlock(&st
->lock
);
298 static int __admv8818_read_lpf_freq(struct admv8818_state
*st
, u64
*lpf_freq
)
300 unsigned int data
, lpf_band
, lpf_state
;
303 ret
= regmap_read(st
->regmap
, ADMV8818_REG_WR0_SW
, &data
);
307 lpf_band
= FIELD_GET(ADMV8818_SW_OUT_WR0_MSK
, data
);
308 if (!lpf_band
|| lpf_band
> 4) {
313 ret
= regmap_read(st
->regmap
, ADMV8818_REG_WR0_FILTER
, &data
);
317 lpf_state
= FIELD_GET(ADMV8818_LPF_WR0_MSK
, data
);
319 *lpf_freq
= div_u64(freq_range_lpf
[lpf_band
- 1][1] - freq_range_lpf
[lpf_band
- 1][0], 15);
320 *lpf_freq
= freq_range_lpf
[lpf_band
- 1][0] + (*lpf_freq
* lpf_state
);
325 static int admv8818_read_lpf_freq(struct admv8818_state
*st
, u64
*lpf_freq
)
329 mutex_lock(&st
->lock
);
330 ret
= __admv8818_read_lpf_freq(st
, lpf_freq
);
331 mutex_unlock(&st
->lock
);
336 static int admv8818_write_raw(struct iio_dev
*indio_dev
,
337 struct iio_chan_spec
const *chan
,
338 int val
, int val2
, long info
)
340 struct admv8818_state
*st
= iio_priv(indio_dev
);
342 u64 freq
= ((u64
)val2
<< 32 | (u32
)val
);
345 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
346 return admv8818_lpf_select(st
, freq
);
347 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
:
348 return admv8818_hpf_select(st
, freq
);
354 static int admv8818_read_raw(struct iio_dev
*indio_dev
,
355 struct iio_chan_spec
const *chan
,
356 int *val
, int *val2
, long info
)
358 struct admv8818_state
*st
= iio_priv(indio_dev
);
363 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
364 ret
= admv8818_read_lpf_freq(st
, &freq
);
369 *val2
= (u32
)(freq
>> 32);
371 return IIO_VAL_INT_64
;
372 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
:
373 ret
= admv8818_read_hpf_freq(st
, &freq
);
378 *val2
= (u32
)(freq
>> 32);
380 return IIO_VAL_INT_64
;
386 static int admv8818_reg_access(struct iio_dev
*indio_dev
,
388 unsigned int write_val
,
389 unsigned int *read_val
)
391 struct admv8818_state
*st
= iio_priv(indio_dev
);
394 return regmap_read(st
->regmap
, reg
, read_val
);
396 return regmap_write(st
->regmap
, reg
, write_val
);
399 static int admv8818_filter_bypass(struct admv8818_state
*st
)
403 mutex_lock(&st
->lock
);
405 ret
= regmap_update_bits(st
->regmap
, ADMV8818_REG_WR0_SW
,
406 ADMV8818_SW_IN_SET_WR0_MSK
|
407 ADMV8818_SW_IN_WR0_MSK
|
408 ADMV8818_SW_OUT_SET_WR0_MSK
|
409 ADMV8818_SW_OUT_WR0_MSK
,
410 FIELD_PREP(ADMV8818_SW_IN_SET_WR0_MSK
, 1) |
411 FIELD_PREP(ADMV8818_SW_IN_WR0_MSK
, 0) |
412 FIELD_PREP(ADMV8818_SW_OUT_SET_WR0_MSK
, 1) |
413 FIELD_PREP(ADMV8818_SW_OUT_WR0_MSK
, 0));
417 ret
= regmap_update_bits(st
->regmap
, ADMV8818_REG_WR0_FILTER
,
418 ADMV8818_HPF_WR0_MSK
|
419 ADMV8818_LPF_WR0_MSK
,
420 FIELD_PREP(ADMV8818_HPF_WR0_MSK
, 0) |
421 FIELD_PREP(ADMV8818_LPF_WR0_MSK
, 0));
424 mutex_unlock(&st
->lock
);
429 static int admv8818_get_mode(struct iio_dev
*indio_dev
,
430 const struct iio_chan_spec
*chan
)
432 struct admv8818_state
*st
= iio_priv(indio_dev
);
434 return st
->filter_mode
;
437 static int admv8818_set_mode(struct iio_dev
*indio_dev
,
438 const struct iio_chan_spec
*chan
,
441 struct admv8818_state
*st
= iio_priv(indio_dev
);
445 if (mode
== ADMV8818_MANUAL_MODE
)
448 if (mode
== ADMV8818_BYPASS_MODE
) {
449 ret
= admv8818_filter_bypass(st
);
460 case ADMV8818_AUTO_MODE
:
461 if (st
->filter_mode
== ADMV8818_AUTO_MODE
)
464 ret
= clk_prepare_enable(st
->clkin
);
468 ret
= clk_notifier_register(st
->clkin
, &st
->nb
);
470 clk_disable_unprepare(st
->clkin
);
476 case ADMV8818_MANUAL_MODE
:
477 case ADMV8818_BYPASS_MODE
:
478 if (st
->filter_mode
== ADMV8818_AUTO_MODE
) {
479 clk_disable_unprepare(st
->clkin
);
481 ret
= clk_notifier_unregister(st
->clkin
, &st
->nb
);
486 if (mode
== ADMV8818_BYPASS_MODE
) {
487 ret
= admv8818_filter_bypass(st
);
498 st
->filter_mode
= mode
;
503 static const struct iio_info admv8818_info
= {
504 .write_raw
= admv8818_write_raw
,
505 .read_raw
= admv8818_read_raw
,
506 .debugfs_reg_access
= &admv8818_reg_access
,
509 static const struct iio_enum admv8818_mode_enum
= {
510 .items
= admv8818_modes
,
511 .num_items
= ARRAY_SIZE(admv8818_modes
),
512 .get
= admv8818_get_mode
,
513 .set
= admv8818_set_mode
,
516 static const struct iio_chan_spec_ext_info admv8818_ext_info
[] = {
517 IIO_ENUM("filter_mode", IIO_SHARED_BY_ALL
, &admv8818_mode_enum
),
518 IIO_ENUM_AVAILABLE("filter_mode", IIO_SHARED_BY_ALL
, &admv8818_mode_enum
),
522 #define ADMV8818_CHAN(_channel) { \
523 .type = IIO_ALTVOLTAGE, \
526 .channel = _channel, \
527 .info_mask_separate = \
528 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
529 BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY) \
532 #define ADMV8818_CHAN_BW_CF(_channel, _admv8818_ext_info) { \
533 .type = IIO_ALTVOLTAGE, \
536 .channel = _channel, \
537 .ext_info = _admv8818_ext_info, \
540 static const struct iio_chan_spec admv8818_channels
[] = {
542 ADMV8818_CHAN_BW_CF(0, admv8818_ext_info
),
545 static int admv8818_freq_change(struct notifier_block
*nb
, unsigned long action
, void *data
)
547 struct admv8818_state
*st
= container_of(nb
, struct admv8818_state
, nb
);
549 if (action
== POST_RATE_CHANGE
)
550 return notifier_from_errno(admv8818_rfin_band_select(st
));
555 static void admv8818_clk_notifier_unreg(void *data
)
557 struct admv8818_state
*st
= data
;
559 if (st
->filter_mode
== 0)
560 clk_notifier_unregister(st
->clkin
, &st
->nb
);
563 static void admv8818_clk_disable(void *data
)
565 struct admv8818_state
*st
= data
;
567 if (st
->filter_mode
== 0)
568 clk_disable_unprepare(st
->clkin
);
571 static int admv8818_init(struct admv8818_state
*st
)
574 struct spi_device
*spi
= st
->spi
;
575 unsigned int chip_id
;
577 ret
= regmap_update_bits(st
->regmap
, ADMV8818_REG_SPI_CONFIG_A
,
578 ADMV8818_SOFTRESET_N_MSK
|
579 ADMV8818_SOFTRESET_MSK
,
580 FIELD_PREP(ADMV8818_SOFTRESET_N_MSK
, 1) |
581 FIELD_PREP(ADMV8818_SOFTRESET_MSK
, 1));
583 dev_err(&spi
->dev
, "ADMV8818 Soft Reset failed.\n");
587 ret
= regmap_update_bits(st
->regmap
, ADMV8818_REG_SPI_CONFIG_A
,
588 ADMV8818_SDOACTIVE_N_MSK
|
589 ADMV8818_SDOACTIVE_MSK
,
590 FIELD_PREP(ADMV8818_SDOACTIVE_N_MSK
, 1) |
591 FIELD_PREP(ADMV8818_SDOACTIVE_MSK
, 1));
593 dev_err(&spi
->dev
, "ADMV8818 SDO Enable failed.\n");
597 ret
= regmap_read(st
->regmap
, ADMV8818_REG_CHIPTYPE
, &chip_id
);
599 dev_err(&spi
->dev
, "ADMV8818 Chip ID read failed.\n");
603 if (chip_id
!= 0x1) {
604 dev_err(&spi
->dev
, "ADMV8818 Invalid Chip ID.\n");
608 ret
= regmap_update_bits(st
->regmap
, ADMV8818_REG_SPI_CONFIG_B
,
609 ADMV8818_SINGLE_INSTRUCTION_MSK
,
610 FIELD_PREP(ADMV8818_SINGLE_INSTRUCTION_MSK
, 1));
612 dev_err(&spi
->dev
, "ADMV8818 Single Instruction failed.\n");
617 return admv8818_rfin_band_select(st
);
622 static int admv8818_clk_setup(struct admv8818_state
*st
)
624 struct spi_device
*spi
= st
->spi
;
627 st
->clkin
= devm_clk_get_optional(&spi
->dev
, "rf_in");
628 if (IS_ERR(st
->clkin
))
629 return dev_err_probe(&spi
->dev
, PTR_ERR(st
->clkin
),
630 "failed to get the input clock\n");
634 ret
= clk_prepare_enable(st
->clkin
);
638 ret
= devm_add_action_or_reset(&spi
->dev
, admv8818_clk_disable
, st
);
642 st
->nb
.notifier_call
= admv8818_freq_change
;
643 ret
= clk_notifier_register(st
->clkin
, &st
->nb
);
647 return devm_add_action_or_reset(&spi
->dev
, admv8818_clk_notifier_unreg
, st
);
650 static int admv8818_probe(struct spi_device
*spi
)
652 struct iio_dev
*indio_dev
;
653 struct regmap
*regmap
;
654 struct admv8818_state
*st
;
657 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
661 regmap
= devm_regmap_init_spi(spi
, &admv8818_regmap_config
);
663 return PTR_ERR(regmap
);
665 st
= iio_priv(indio_dev
);
668 indio_dev
->info
= &admv8818_info
;
669 indio_dev
->name
= "admv8818";
670 indio_dev
->channels
= admv8818_channels
;
671 indio_dev
->num_channels
= ARRAY_SIZE(admv8818_channels
);
675 ret
= admv8818_clk_setup(st
);
679 mutex_init(&st
->lock
);
681 ret
= admv8818_init(st
);
685 return devm_iio_device_register(&spi
->dev
, indio_dev
);
688 static const struct spi_device_id admv8818_id
[] = {
692 MODULE_DEVICE_TABLE(spi
, admv8818_id
);
694 static const struct of_device_id admv8818_of_match
[] = {
695 { .compatible
= "adi,admv8818" },
698 MODULE_DEVICE_TABLE(of
, admv8818_of_match
);
700 static struct spi_driver admv8818_driver
= {
703 .of_match_table
= admv8818_of_match
,
705 .probe
= admv8818_probe
,
706 .id_table
= admv8818_id
,
708 module_spi_driver(admv8818_driver
);
710 MODULE_AUTHOR("Antoniu Miclaus <antoniu.miclaus@analog.com");
711 MODULE_DESCRIPTION("Analog Devices ADMV8818");
712 MODULE_LICENSE("GPL v2");