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/notifier.h>
16 #include <linux/property.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/spi/spi.h>
19 #include <linux/units.h>
21 #include <linux/unaligned.h>
23 /* ADMV1013 Register Map */
24 #define ADMV1013_REG_SPI_CONTROL 0x00
25 #define ADMV1013_REG_ALARM 0x01
26 #define ADMV1013_REG_ALARM_MASKS 0x02
27 #define ADMV1013_REG_ENABLE 0x03
28 #define ADMV1013_REG_LO_AMP_I 0x05
29 #define ADMV1013_REG_LO_AMP_Q 0x06
30 #define ADMV1013_REG_OFFSET_ADJUST_I 0x07
31 #define ADMV1013_REG_OFFSET_ADJUST_Q 0x08
32 #define ADMV1013_REG_QUAD 0x09
33 #define ADMV1013_REG_VVA_TEMP_COMP 0x0A
35 /* ADMV1013_REG_SPI_CONTROL Map */
36 #define ADMV1013_PARITY_EN_MSK BIT(15)
37 #define ADMV1013_SPI_SOFT_RESET_MSK BIT(14)
38 #define ADMV1013_CHIP_ID_MSK GENMASK(11, 4)
39 #define ADMV1013_CHIP_ID 0xA
40 #define ADMV1013_REVISION_ID_MSK GENMASK(3, 0)
42 /* ADMV1013_REG_ALARM Map */
43 #define ADMV1013_PARITY_ERROR_MSK BIT(15)
44 #define ADMV1013_TOO_FEW_ERRORS_MSK BIT(14)
45 #define ADMV1013_TOO_MANY_ERRORS_MSK BIT(13)
46 #define ADMV1013_ADDRESS_RANGE_ERROR_MSK BIT(12)
48 /* ADMV1013_REG_ENABLE Map */
49 #define ADMV1013_VGA_PD_MSK BIT(15)
50 #define ADMV1013_MIXER_PD_MSK BIT(14)
51 #define ADMV1013_QUAD_PD_MSK GENMASK(13, 11)
52 #define ADMV1013_BG_PD_MSK BIT(10)
53 #define ADMV1013_MIXER_IF_EN_MSK BIT(7)
54 #define ADMV1013_DET_EN_MSK BIT(5)
56 /* ADMV1013_REG_LO_AMP Map */
57 #define ADMV1013_LOAMP_PH_ADJ_FINE_MSK GENMASK(13, 7)
58 #define ADMV1013_MIXER_VGATE_MSK GENMASK(6, 0)
60 /* ADMV1013_REG_OFFSET_ADJUST Map */
61 #define ADMV1013_MIXER_OFF_ADJ_P_MSK GENMASK(15, 9)
62 #define ADMV1013_MIXER_OFF_ADJ_N_MSK GENMASK(8, 2)
64 /* ADMV1013_REG_QUAD Map */
65 #define ADMV1013_QUAD_SE_MODE_MSK GENMASK(9, 6)
66 #define ADMV1013_QUAD_FILTERS_MSK GENMASK(3, 0)
68 /* ADMV1013_REG_VVA_TEMP_COMP Map */
69 #define ADMV1013_VVA_TEMP_COMP_MSK GENMASK(15, 0)
71 /* ADMV1013 Miscellaneous Defines */
72 #define ADMV1013_READ BIT(7)
73 #define ADMV1013_REG_ADDR_READ_MSK GENMASK(6, 1)
74 #define ADMV1013_REG_ADDR_WRITE_MSK GENMASK(22, 17)
75 #define ADMV1013_REG_DATA_MSK GENMASK(16, 1)
83 ADMV1013_RFMOD_I_CALIBPHASE
,
84 ADMV1013_RFMOD_Q_CALIBPHASE
,
88 ADMV1013_SE_MODE_POS
= 6,
89 ADMV1013_SE_MODE_NEG
= 9,
90 ADMV1013_SE_MODE_DIFF
= 12
93 struct admv1013_state
{
94 struct spi_device
*spi
;
96 /* Protect against concurrent accesses to the device and to data */
98 struct notifier_block nb
;
99 unsigned int input_mode
;
100 unsigned int quad_se_mode
;
102 u8 data
[3] __aligned(IIO_DMA_MINALIGN
);
105 static int __admv1013_spi_read(struct admv1013_state
*st
, unsigned int reg
,
109 struct spi_transfer t
= {0};
111 st
->data
[0] = ADMV1013_READ
| FIELD_PREP(ADMV1013_REG_ADDR_READ_MSK
, reg
);
115 t
.rx_buf
= &st
->data
[0];
116 t
.tx_buf
= &st
->data
[0];
119 ret
= spi_sync_transfer(st
->spi
, &t
, 1);
123 *val
= FIELD_GET(ADMV1013_REG_DATA_MSK
, get_unaligned_be24(&st
->data
[0]));
128 static int admv1013_spi_read(struct admv1013_state
*st
, unsigned int reg
,
133 mutex_lock(&st
->lock
);
134 ret
= __admv1013_spi_read(st
, reg
, val
);
135 mutex_unlock(&st
->lock
);
140 static int __admv1013_spi_write(struct admv1013_state
*st
,
144 put_unaligned_be24(FIELD_PREP(ADMV1013_REG_DATA_MSK
, val
) |
145 FIELD_PREP(ADMV1013_REG_ADDR_WRITE_MSK
, reg
), &st
->data
[0]);
147 return spi_write(st
->spi
, &st
->data
[0], 3);
150 static int admv1013_spi_write(struct admv1013_state
*st
, unsigned int reg
,
155 mutex_lock(&st
->lock
);
156 ret
= __admv1013_spi_write(st
, reg
, val
);
157 mutex_unlock(&st
->lock
);
162 static int __admv1013_spi_update_bits(struct admv1013_state
*st
, unsigned int reg
,
163 unsigned int mask
, unsigned int val
)
166 unsigned int data
, temp
;
168 ret
= __admv1013_spi_read(st
, reg
, &data
);
172 temp
= (data
& ~mask
) | (val
& mask
);
174 return __admv1013_spi_write(st
, reg
, temp
);
177 static int admv1013_spi_update_bits(struct admv1013_state
*st
, unsigned int reg
,
178 unsigned int mask
, unsigned int val
)
182 mutex_lock(&st
->lock
);
183 ret
= __admv1013_spi_update_bits(st
, reg
, mask
, val
);
184 mutex_unlock(&st
->lock
);
189 static int admv1013_read_raw(struct iio_dev
*indio_dev
,
190 struct iio_chan_spec
const *chan
,
191 int *val
, int *val2
, long info
)
193 struct admv1013_state
*st
= iio_priv(indio_dev
);
194 unsigned int data
, addr
;
198 case IIO_CHAN_INFO_CALIBBIAS
:
199 switch (chan
->channel
) {
201 addr
= ADMV1013_REG_OFFSET_ADJUST_I
;
204 addr
= ADMV1013_REG_OFFSET_ADJUST_Q
;
210 ret
= admv1013_spi_read(st
, addr
, &data
);
215 *val
= FIELD_GET(ADMV1013_MIXER_OFF_ADJ_P_MSK
, data
);
217 *val
= FIELD_GET(ADMV1013_MIXER_OFF_ADJ_N_MSK
, data
);
225 static int admv1013_write_raw(struct iio_dev
*indio_dev
,
226 struct iio_chan_spec
const *chan
,
227 int val
, int val2
, long info
)
229 struct admv1013_state
*st
= iio_priv(indio_dev
);
230 unsigned int addr
, data
, msk
;
233 case IIO_CHAN_INFO_CALIBBIAS
:
234 switch (chan
->channel2
) {
236 addr
= ADMV1013_REG_OFFSET_ADJUST_I
;
239 addr
= ADMV1013_REG_OFFSET_ADJUST_Q
;
245 if (!chan
->channel
) {
246 msk
= ADMV1013_MIXER_OFF_ADJ_P_MSK
;
247 data
= FIELD_PREP(ADMV1013_MIXER_OFF_ADJ_P_MSK
, val
);
249 msk
= ADMV1013_MIXER_OFF_ADJ_N_MSK
;
250 data
= FIELD_PREP(ADMV1013_MIXER_OFF_ADJ_N_MSK
, val
);
253 return admv1013_spi_update_bits(st
, addr
, msk
, data
);
259 static ssize_t
admv1013_read(struct iio_dev
*indio_dev
,
261 const struct iio_chan_spec
*chan
,
264 struct admv1013_state
*st
= iio_priv(indio_dev
);
265 unsigned int data
, addr
;
268 switch ((u32
)private) {
269 case ADMV1013_RFMOD_I_CALIBPHASE
:
270 addr
= ADMV1013_REG_LO_AMP_I
;
272 case ADMV1013_RFMOD_Q_CALIBPHASE
:
273 addr
= ADMV1013_REG_LO_AMP_Q
;
279 ret
= admv1013_spi_read(st
, addr
, &data
);
283 data
= FIELD_GET(ADMV1013_LOAMP_PH_ADJ_FINE_MSK
, data
);
285 return sysfs_emit(buf
, "%u\n", data
);
288 static ssize_t
admv1013_write(struct iio_dev
*indio_dev
,
290 const struct iio_chan_spec
*chan
,
291 const char *buf
, size_t len
)
293 struct admv1013_state
*st
= iio_priv(indio_dev
);
297 ret
= kstrtou32(buf
, 10, &data
);
301 data
= FIELD_PREP(ADMV1013_LOAMP_PH_ADJ_FINE_MSK
, data
);
303 switch ((u32
)private) {
304 case ADMV1013_RFMOD_I_CALIBPHASE
:
305 ret
= admv1013_spi_update_bits(st
, ADMV1013_REG_LO_AMP_I
,
306 ADMV1013_LOAMP_PH_ADJ_FINE_MSK
,
311 case ADMV1013_RFMOD_Q_CALIBPHASE
:
312 ret
= admv1013_spi_update_bits(st
, ADMV1013_REG_LO_AMP_Q
,
313 ADMV1013_LOAMP_PH_ADJ_FINE_MSK
,
322 return ret
? ret
: len
;
325 static int admv1013_update_quad_filters(struct admv1013_state
*st
)
327 unsigned int filt_raw
;
328 u64 rate
= clk_get_rate(st
->clkin
);
330 if (rate
>= (5400 * HZ_PER_MHZ
) && rate
<= (7000 * HZ_PER_MHZ
))
332 else if (rate
>= (5400 * HZ_PER_MHZ
) && rate
<= (8000 * HZ_PER_MHZ
))
334 else if (rate
>= (6600 * HZ_PER_MHZ
) && rate
<= (9200 * HZ_PER_MHZ
))
339 return __admv1013_spi_update_bits(st
, ADMV1013_REG_QUAD
,
340 ADMV1013_QUAD_FILTERS_MSK
,
341 FIELD_PREP(ADMV1013_QUAD_FILTERS_MSK
, filt_raw
));
344 static int admv1013_update_mixer_vgate(struct admv1013_state
*st
, int vcm
)
346 unsigned int mixer_vgate
;
349 mixer_vgate
= (2389 * vcm
/ 1000000 + 8100) / 100;
350 else if (vcm
> 1800000 && vcm
<= 2600000)
351 mixer_vgate
= (2375 * vcm
/ 1000000 + 125) / 100;
355 return __admv1013_spi_update_bits(st
, ADMV1013_REG_LO_AMP_I
,
356 ADMV1013_MIXER_VGATE_MSK
,
357 FIELD_PREP(ADMV1013_MIXER_VGATE_MSK
, mixer_vgate
));
360 static int admv1013_reg_access(struct iio_dev
*indio_dev
,
362 unsigned int write_val
,
363 unsigned int *read_val
)
365 struct admv1013_state
*st
= iio_priv(indio_dev
);
368 return admv1013_spi_read(st
, reg
, read_val
);
370 return admv1013_spi_write(st
, reg
, write_val
);
373 static const struct iio_info admv1013_info
= {
374 .read_raw
= admv1013_read_raw
,
375 .write_raw
= admv1013_write_raw
,
376 .debugfs_reg_access
= &admv1013_reg_access
,
379 static const char * const admv1013_vcc_regs
[] = {
380 "vcc-drv", "vcc2-drv", "vcc-vva", "vcc-amp1", "vcc-amp2",
381 "vcc-env", "vcc-bg", "vcc-bg2", "vcc-mixer", "vcc-quad"
384 static int admv1013_freq_change(struct notifier_block
*nb
, unsigned long action
, void *data
)
386 struct admv1013_state
*st
= container_of(nb
, struct admv1013_state
, nb
);
389 if (action
== POST_RATE_CHANGE
) {
390 mutex_lock(&st
->lock
);
391 ret
= notifier_from_errno(admv1013_update_quad_filters(st
));
392 mutex_unlock(&st
->lock
);
399 #define _ADMV1013_EXT_INFO(_name, _shared, _ident) { \
401 .read = admv1013_read, \
402 .write = admv1013_write, \
407 static const struct iio_chan_spec_ext_info admv1013_ext_info
[] = {
408 _ADMV1013_EXT_INFO("i_calibphase", IIO_SEPARATE
, ADMV1013_RFMOD_I_CALIBPHASE
),
409 _ADMV1013_EXT_INFO("q_calibphase", IIO_SEPARATE
, ADMV1013_RFMOD_Q_CALIBPHASE
),
413 #define ADMV1013_CHAN_PHASE(_channel, _channel2, _admv1013_ext_info) { \
414 .type = IIO_ALTVOLTAGE, \
417 .channel2 = _channel2, \
418 .channel = _channel, \
420 .ext_info = _admv1013_ext_info, \
423 #define ADMV1013_CHAN_CALIB(_channel, rf_comp) { \
424 .type = IIO_ALTVOLTAGE, \
427 .channel = _channel, \
428 .channel2 = IIO_MOD_##rf_comp, \
429 .info_mask_separate = BIT(IIO_CHAN_INFO_CALIBBIAS), \
432 static const struct iio_chan_spec admv1013_channels
[] = {
433 ADMV1013_CHAN_PHASE(0, 1, admv1013_ext_info
),
434 ADMV1013_CHAN_CALIB(0, I
),
435 ADMV1013_CHAN_CALIB(0, Q
),
436 ADMV1013_CHAN_CALIB(1, I
),
437 ADMV1013_CHAN_CALIB(1, Q
),
440 static int admv1013_init(struct admv1013_state
*st
, int vcm_uv
)
444 struct spi_device
*spi
= st
->spi
;
446 /* Perform a software reset */
447 ret
= __admv1013_spi_update_bits(st
, ADMV1013_REG_SPI_CONTROL
,
448 ADMV1013_SPI_SOFT_RESET_MSK
,
449 FIELD_PREP(ADMV1013_SPI_SOFT_RESET_MSK
, 1));
453 ret
= __admv1013_spi_update_bits(st
, ADMV1013_REG_SPI_CONTROL
,
454 ADMV1013_SPI_SOFT_RESET_MSK
,
455 FIELD_PREP(ADMV1013_SPI_SOFT_RESET_MSK
, 0));
459 ret
= __admv1013_spi_read(st
, ADMV1013_REG_SPI_CONTROL
, &data
);
463 data
= FIELD_GET(ADMV1013_CHIP_ID_MSK
, data
);
464 if (data
!= ADMV1013_CHIP_ID
) {
465 dev_err(&spi
->dev
, "Invalid Chip ID.\n");
469 ret
= __admv1013_spi_write(st
, ADMV1013_REG_VVA_TEMP_COMP
, 0xE700);
473 data
= FIELD_PREP(ADMV1013_QUAD_SE_MODE_MSK
, st
->quad_se_mode
);
475 ret
= __admv1013_spi_update_bits(st
, ADMV1013_REG_QUAD
,
476 ADMV1013_QUAD_SE_MODE_MSK
, data
);
480 ret
= admv1013_update_mixer_vgate(st
, vcm_uv
);
484 ret
= admv1013_update_quad_filters(st
);
488 return __admv1013_spi_update_bits(st
, ADMV1013_REG_ENABLE
,
489 ADMV1013_DET_EN_MSK
|
490 ADMV1013_MIXER_IF_EN_MSK
,
495 static void admv1013_powerdown(void *data
)
497 unsigned int enable_reg
, enable_reg_msk
;
499 /* Disable all components in the Enable Register */
500 enable_reg_msk
= ADMV1013_VGA_PD_MSK
|
501 ADMV1013_MIXER_PD_MSK
|
502 ADMV1013_QUAD_PD_MSK
|
504 ADMV1013_MIXER_IF_EN_MSK
|
507 enable_reg
= FIELD_PREP(ADMV1013_VGA_PD_MSK
, 1) |
508 FIELD_PREP(ADMV1013_MIXER_PD_MSK
, 1) |
509 FIELD_PREP(ADMV1013_QUAD_PD_MSK
, 7) |
510 FIELD_PREP(ADMV1013_BG_PD_MSK
, 1) |
511 FIELD_PREP(ADMV1013_MIXER_IF_EN_MSK
, 0) |
512 FIELD_PREP(ADMV1013_DET_EN_MSK
, 0);
514 admv1013_spi_update_bits(data
, ADMV1013_REG_ENABLE
, enable_reg_msk
, enable_reg
);
517 static int admv1013_properties_parse(struct admv1013_state
*st
)
521 struct spi_device
*spi
= st
->spi
;
523 st
->det_en
= device_property_read_bool(&spi
->dev
, "adi,detector-enable");
525 ret
= device_property_read_string(&spi
->dev
, "adi,input-mode", &str
);
527 st
->input_mode
= ADMV1013_IQ_MODE
;
529 if (!strcmp(str
, "iq"))
530 st
->input_mode
= ADMV1013_IQ_MODE
;
531 else if (!strcmp(str
, "if"))
532 st
->input_mode
= ADMV1013_IF_MODE
;
536 ret
= device_property_read_string(&spi
->dev
, "adi,quad-se-mode", &str
);
538 st
->quad_se_mode
= ADMV1013_SE_MODE_DIFF
;
540 if (!strcmp(str
, "diff"))
541 st
->quad_se_mode
= ADMV1013_SE_MODE_DIFF
;
542 else if (!strcmp(str
, "se-pos"))
543 st
->quad_se_mode
= ADMV1013_SE_MODE_POS
;
544 else if (!strcmp(str
, "se-neg"))
545 st
->quad_se_mode
= ADMV1013_SE_MODE_NEG
;
549 ret
= devm_regulator_bulk_get_enable(&st
->spi
->dev
,
550 ARRAY_SIZE(admv1013_vcc_regs
),
553 dev_err_probe(&spi
->dev
, ret
,
554 "Failed to request VCC regulators\n");
561 static int admv1013_probe(struct spi_device
*spi
)
563 struct iio_dev
*indio_dev
;
564 struct admv1013_state
*st
;
567 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
571 st
= iio_priv(indio_dev
);
573 indio_dev
->info
= &admv1013_info
;
574 indio_dev
->name
= "admv1013";
575 indio_dev
->channels
= admv1013_channels
;
576 indio_dev
->num_channels
= ARRAY_SIZE(admv1013_channels
);
580 ret
= admv1013_properties_parse(st
);
584 ret
= devm_regulator_get_enable_read_voltage(&spi
->dev
, "vcm");
586 return dev_err_probe(&spi
->dev
, ret
,
587 "failed to get the common-mode voltage\n");
591 st
->clkin
= devm_clk_get_enabled(&spi
->dev
, "lo_in");
592 if (IS_ERR(st
->clkin
))
593 return dev_err_probe(&spi
->dev
, PTR_ERR(st
->clkin
),
594 "failed to get the LO input clock\n");
596 st
->nb
.notifier_call
= admv1013_freq_change
;
597 ret
= devm_clk_notifier_register(&spi
->dev
, st
->clkin
, &st
->nb
);
601 mutex_init(&st
->lock
);
603 ret
= admv1013_init(st
, vcm_uv
);
605 dev_err(&spi
->dev
, "admv1013 init failed\n");
609 ret
= devm_add_action_or_reset(&spi
->dev
, admv1013_powerdown
, st
);
613 return devm_iio_device_register(&spi
->dev
, indio_dev
);
616 static const struct spi_device_id admv1013_id
[] = {
620 MODULE_DEVICE_TABLE(spi
, admv1013_id
);
622 static const struct of_device_id admv1013_of_match
[] = {
623 { .compatible
= "adi,admv1013" },
626 MODULE_DEVICE_TABLE(of
, admv1013_of_match
);
628 static struct spi_driver admv1013_driver
= {
631 .of_match_table
= admv1013_of_match
,
633 .probe
= admv1013_probe
,
634 .id_table
= admv1013_id
,
636 module_spi_driver(admv1013_driver
);
638 MODULE_AUTHOR("Antoniu Miclaus <antoniu.miclaus@analog.com");
639 MODULE_DESCRIPTION("Analog Devices ADMV1013");
640 MODULE_LICENSE("GPL v2");