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/clkdev.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/iio/iio.h>
15 #include <linux/module.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/spi/spi.h>
19 #include <linux/unaligned.h>
21 /* ADRF6780 Register Map */
22 #define ADRF6780_REG_CONTROL 0x00
23 #define ADRF6780_REG_ALARM_READBACK 0x01
24 #define ADRF6780_REG_ALARM_MASKS 0x02
25 #define ADRF6780_REG_ENABLE 0x03
26 #define ADRF6780_REG_LINEARIZE 0x04
27 #define ADRF6780_REG_LO_PATH 0x05
28 #define ADRF6780_REG_ADC_CONTROL 0x06
29 #define ADRF6780_REG_ADC_OUTPUT 0x0C
31 /* ADRF6780_REG_CONTROL Map */
32 #define ADRF6780_PARITY_EN_MSK BIT(15)
33 #define ADRF6780_SOFT_RESET_MSK BIT(14)
34 #define ADRF6780_CHIP_ID_MSK GENMASK(11, 4)
35 #define ADRF6780_CHIP_ID 0xA
36 #define ADRF6780_CHIP_REVISION_MSK GENMASK(3, 0)
38 /* ADRF6780_REG_ALARM_READBACK Map */
39 #define ADRF6780_PARITY_ERROR_MSK BIT(15)
40 #define ADRF6780_TOO_FEW_ERRORS_MSK BIT(14)
41 #define ADRF6780_TOO_MANY_ERRORS_MSK BIT(13)
42 #define ADRF6780_ADDRESS_RANGE_ERROR_MSK BIT(12)
44 /* ADRF6780_REG_ENABLE Map */
45 #define ADRF6780_VGA_BUFFER_EN_MSK BIT(8)
46 #define ADRF6780_DETECTOR_EN_MSK BIT(7)
47 #define ADRF6780_LO_BUFFER_EN_MSK BIT(6)
48 #define ADRF6780_IF_MODE_EN_MSK BIT(5)
49 #define ADRF6780_IQ_MODE_EN_MSK BIT(4)
50 #define ADRF6780_LO_X2_EN_MSK BIT(3)
51 #define ADRF6780_LO_PPF_EN_MSK BIT(2)
52 #define ADRF6780_LO_EN_MSK BIT(1)
53 #define ADRF6780_UC_BIAS_EN_MSK BIT(0)
55 /* ADRF6780_REG_LINEARIZE Map */
56 #define ADRF6780_RDAC_LINEARIZE_MSK GENMASK(7, 0)
58 /* ADRF6780_REG_LO_PATH Map */
59 #define ADRF6780_LO_SIDEBAND_MSK BIT(10)
60 #define ADRF6780_Q_PATH_PHASE_ACCURACY_MSK GENMASK(7, 4)
61 #define ADRF6780_I_PATH_PHASE_ACCURACY_MSK GENMASK(3, 0)
63 /* ADRF6780_REG_ADC_CONTROL Map */
64 #define ADRF6780_VDET_OUTPUT_SELECT_MSK BIT(3)
65 #define ADRF6780_ADC_START_MSK BIT(2)
66 #define ADRF6780_ADC_EN_MSK BIT(1)
67 #define ADRF6780_ADC_CLOCK_EN_MSK BIT(0)
69 /* ADRF6780_REG_ADC_OUTPUT Map */
70 #define ADRF6780_ADC_STATUS_MSK BIT(8)
71 #define ADRF6780_ADC_VALUE_MSK GENMASK(7, 0)
73 struct adrf6780_state
{
74 struct spi_device
*spi
;
76 /* Protect against concurrent accesses to the device */
88 u8 data
[3] __aligned(IIO_DMA_MINALIGN
);
91 static int __adrf6780_spi_read(struct adrf6780_state
*st
, unsigned int reg
,
95 struct spi_transfer t
= {0};
97 st
->data
[0] = 0x80 | (reg
<< 1);
101 t
.rx_buf
= &st
->data
[0];
102 t
.tx_buf
= &st
->data
[0];
105 ret
= spi_sync_transfer(st
->spi
, &t
, 1);
109 *val
= (get_unaligned_be24(&st
->data
[0]) >> 1) & GENMASK(15, 0);
114 static int adrf6780_spi_read(struct adrf6780_state
*st
, unsigned int reg
,
119 mutex_lock(&st
->lock
);
120 ret
= __adrf6780_spi_read(st
, reg
, val
);
121 mutex_unlock(&st
->lock
);
126 static int __adrf6780_spi_write(struct adrf6780_state
*st
,
130 put_unaligned_be24((val
<< 1) | (reg
<< 17), &st
->data
[0]);
132 return spi_write(st
->spi
, &st
->data
[0], 3);
135 static int adrf6780_spi_write(struct adrf6780_state
*st
, unsigned int reg
,
140 mutex_lock(&st
->lock
);
141 ret
= __adrf6780_spi_write(st
, reg
, val
);
142 mutex_unlock(&st
->lock
);
147 static int __adrf6780_spi_update_bits(struct adrf6780_state
*st
,
148 unsigned int reg
, unsigned int mask
,
152 unsigned int data
, temp
;
154 ret
= __adrf6780_spi_read(st
, reg
, &data
);
158 temp
= (data
& ~mask
) | (val
& mask
);
160 return __adrf6780_spi_write(st
, reg
, temp
);
163 static int adrf6780_spi_update_bits(struct adrf6780_state
*st
, unsigned int reg
,
164 unsigned int mask
, unsigned int val
)
168 mutex_lock(&st
->lock
);
169 ret
= __adrf6780_spi_update_bits(st
, reg
, mask
, val
);
170 mutex_unlock(&st
->lock
);
175 static int adrf6780_read_adc_raw(struct adrf6780_state
*st
, unsigned int *read_val
)
179 mutex_lock(&st
->lock
);
181 ret
= __adrf6780_spi_update_bits(st
, ADRF6780_REG_ADC_CONTROL
,
182 ADRF6780_ADC_EN_MSK
|
183 ADRF6780_ADC_CLOCK_EN_MSK
|
184 ADRF6780_ADC_START_MSK
,
185 FIELD_PREP(ADRF6780_ADC_EN_MSK
, 1) |
186 FIELD_PREP(ADRF6780_ADC_CLOCK_EN_MSK
, 1) |
187 FIELD_PREP(ADRF6780_ADC_START_MSK
, 1));
191 /* Recommended delay for the ADC to be ready*/
192 usleep_range(200, 250);
194 ret
= __adrf6780_spi_read(st
, ADRF6780_REG_ADC_OUTPUT
, read_val
);
198 if (!(*read_val
& ADRF6780_ADC_STATUS_MSK
)) {
203 ret
= __adrf6780_spi_update_bits(st
, ADRF6780_REG_ADC_CONTROL
,
204 ADRF6780_ADC_START_MSK
,
205 FIELD_PREP(ADRF6780_ADC_START_MSK
, 0));
209 ret
= __adrf6780_spi_read(st
, ADRF6780_REG_ADC_OUTPUT
, read_val
);
212 mutex_unlock(&st
->lock
);
216 static int adrf6780_read_raw(struct iio_dev
*indio_dev
,
217 struct iio_chan_spec
const *chan
,
218 int *val
, int *val2
, long info
)
220 struct adrf6780_state
*dev
= iio_priv(indio_dev
);
225 case IIO_CHAN_INFO_RAW
:
226 ret
= adrf6780_read_adc_raw(dev
, &data
);
230 *val
= data
& ADRF6780_ADC_VALUE_MSK
;
234 case IIO_CHAN_INFO_SCALE
:
235 ret
= adrf6780_spi_read(dev
, ADRF6780_REG_LINEARIZE
, &data
);
239 *val
= data
& ADRF6780_RDAC_LINEARIZE_MSK
;
242 case IIO_CHAN_INFO_PHASE
:
243 ret
= adrf6780_spi_read(dev
, ADRF6780_REG_LO_PATH
, &data
);
247 switch (chan
->channel2
) {
249 *val
= data
& ADRF6780_I_PATH_PHASE_ACCURACY_MSK
;
253 *val
= FIELD_GET(ADRF6780_Q_PATH_PHASE_ACCURACY_MSK
,
265 static int adrf6780_write_raw(struct iio_dev
*indio_dev
,
266 struct iio_chan_spec
const *chan
,
267 int val
, int val2
, long info
)
269 struct adrf6780_state
*st
= iio_priv(indio_dev
);
272 case IIO_CHAN_INFO_SCALE
:
273 return adrf6780_spi_write(st
, ADRF6780_REG_LINEARIZE
, val
);
274 case IIO_CHAN_INFO_PHASE
:
275 switch (chan
->channel2
) {
277 return adrf6780_spi_update_bits(st
,
278 ADRF6780_REG_LO_PATH
,
279 ADRF6780_I_PATH_PHASE_ACCURACY_MSK
,
280 FIELD_PREP(ADRF6780_I_PATH_PHASE_ACCURACY_MSK
, val
));
282 return adrf6780_spi_update_bits(st
,
283 ADRF6780_REG_LO_PATH
,
284 ADRF6780_Q_PATH_PHASE_ACCURACY_MSK
,
285 FIELD_PREP(ADRF6780_Q_PATH_PHASE_ACCURACY_MSK
, val
));
294 static int adrf6780_reg_access(struct iio_dev
*indio_dev
,
296 unsigned int write_val
,
297 unsigned int *read_val
)
299 struct adrf6780_state
*st
= iio_priv(indio_dev
);
302 return adrf6780_spi_read(st
, reg
, read_val
);
304 return adrf6780_spi_write(st
, reg
, write_val
);
307 static const struct iio_info adrf6780_info
= {
308 .read_raw
= adrf6780_read_raw
,
309 .write_raw
= adrf6780_write_raw
,
310 .debugfs_reg_access
= &adrf6780_reg_access
,
313 #define ADRF6780_CHAN_ADC(_channel) { \
314 .type = IIO_ALTVOLTAGE, \
317 .channel = _channel, \
318 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
321 #define ADRF6780_CHAN_RDAC(_channel) { \
322 .type = IIO_ALTVOLTAGE, \
325 .channel = _channel, \
326 .info_mask_separate = BIT(IIO_CHAN_INFO_SCALE) \
329 #define ADRF6780_CHAN_IQ_PHASE(_channel, rf_comp) { \
330 .type = IIO_ALTVOLTAGE, \
334 .channel2 = IIO_MOD_##rf_comp, \
335 .channel = _channel, \
336 .info_mask_separate = BIT(IIO_CHAN_INFO_PHASE) \
339 static const struct iio_chan_spec adrf6780_channels
[] = {
340 ADRF6780_CHAN_ADC(0),
341 ADRF6780_CHAN_RDAC(0),
342 ADRF6780_CHAN_IQ_PHASE(0, I
),
343 ADRF6780_CHAN_IQ_PHASE(0, Q
),
346 static int adrf6780_reset(struct adrf6780_state
*st
)
349 struct spi_device
*spi
= st
->spi
;
351 ret
= __adrf6780_spi_update_bits(st
, ADRF6780_REG_CONTROL
,
352 ADRF6780_SOFT_RESET_MSK
,
353 FIELD_PREP(ADRF6780_SOFT_RESET_MSK
, 1));
355 dev_err(&spi
->dev
, "ADRF6780 SPI software reset failed.\n");
359 ret
= __adrf6780_spi_update_bits(st
, ADRF6780_REG_CONTROL
,
360 ADRF6780_SOFT_RESET_MSK
,
361 FIELD_PREP(ADRF6780_SOFT_RESET_MSK
, 0));
363 dev_err(&spi
->dev
, "ADRF6780 SPI software reset disable failed.\n");
370 static int adrf6780_init(struct adrf6780_state
*st
)
373 unsigned int chip_id
, enable_reg
, enable_reg_msk
;
374 struct spi_device
*spi
= st
->spi
;
376 /* Perform a software reset */
377 ret
= adrf6780_reset(st
);
381 ret
= __adrf6780_spi_read(st
, ADRF6780_REG_CONTROL
, &chip_id
);
385 chip_id
= FIELD_GET(ADRF6780_CHIP_ID_MSK
, chip_id
);
386 if (chip_id
!= ADRF6780_CHIP_ID
) {
387 dev_err(&spi
->dev
, "ADRF6780 Invalid Chip ID.\n");
391 enable_reg_msk
= ADRF6780_VGA_BUFFER_EN_MSK
|
392 ADRF6780_DETECTOR_EN_MSK
|
393 ADRF6780_LO_BUFFER_EN_MSK
|
394 ADRF6780_IF_MODE_EN_MSK
|
395 ADRF6780_IQ_MODE_EN_MSK
|
396 ADRF6780_LO_X2_EN_MSK
|
397 ADRF6780_LO_PPF_EN_MSK
|
399 ADRF6780_UC_BIAS_EN_MSK
;
401 enable_reg
= FIELD_PREP(ADRF6780_VGA_BUFFER_EN_MSK
, st
->vga_buff_en
) |
402 FIELD_PREP(ADRF6780_DETECTOR_EN_MSK
, 1) |
403 FIELD_PREP(ADRF6780_LO_BUFFER_EN_MSK
, st
->lo_buff_en
) |
404 FIELD_PREP(ADRF6780_IF_MODE_EN_MSK
, st
->if_mode_en
) |
405 FIELD_PREP(ADRF6780_IQ_MODE_EN_MSK
, st
->iq_mode_en
) |
406 FIELD_PREP(ADRF6780_LO_X2_EN_MSK
, st
->lo_x2_en
) |
407 FIELD_PREP(ADRF6780_LO_PPF_EN_MSK
, st
->lo_ppf_en
) |
408 FIELD_PREP(ADRF6780_LO_EN_MSK
, st
->lo_en
) |
409 FIELD_PREP(ADRF6780_UC_BIAS_EN_MSK
, st
->uc_bias_en
);
411 ret
= __adrf6780_spi_update_bits(st
, ADRF6780_REG_ENABLE
,
412 enable_reg_msk
, enable_reg
);
416 ret
= __adrf6780_spi_update_bits(st
, ADRF6780_REG_LO_PATH
,
417 ADRF6780_LO_SIDEBAND_MSK
,
418 FIELD_PREP(ADRF6780_LO_SIDEBAND_MSK
, st
->lo_sideband
));
422 return __adrf6780_spi_update_bits(st
, ADRF6780_REG_ADC_CONTROL
,
423 ADRF6780_VDET_OUTPUT_SELECT_MSK
,
424 FIELD_PREP(ADRF6780_VDET_OUTPUT_SELECT_MSK
, st
->vdet_out_en
));
427 static void adrf6780_properties_parse(struct adrf6780_state
*st
)
429 struct spi_device
*spi
= st
->spi
;
431 st
->vga_buff_en
= device_property_read_bool(&spi
->dev
, "adi,vga-buff-en");
432 st
->lo_buff_en
= device_property_read_bool(&spi
->dev
, "adi,lo-buff-en");
433 st
->if_mode_en
= device_property_read_bool(&spi
->dev
, "adi,if-mode-en");
434 st
->iq_mode_en
= device_property_read_bool(&spi
->dev
, "adi,iq-mode-en");
435 st
->lo_x2_en
= device_property_read_bool(&spi
->dev
, "adi,lo-x2-en");
436 st
->lo_ppf_en
= device_property_read_bool(&spi
->dev
, "adi,lo-ppf-en");
437 st
->lo_en
= device_property_read_bool(&spi
->dev
, "adi,lo-en");
438 st
->uc_bias_en
= device_property_read_bool(&spi
->dev
, "adi,uc-bias-en");
439 st
->lo_sideband
= device_property_read_bool(&spi
->dev
, "adi,lo-sideband");
440 st
->vdet_out_en
= device_property_read_bool(&spi
->dev
, "adi,vdet-out-en");
443 static void adrf6780_powerdown(void *data
)
445 /* Disable all components in the Enable Register */
446 adrf6780_spi_write(data
, ADRF6780_REG_ENABLE
, 0x0);
449 static int adrf6780_probe(struct spi_device
*spi
)
451 struct iio_dev
*indio_dev
;
452 struct adrf6780_state
*st
;
455 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
459 st
= iio_priv(indio_dev
);
461 indio_dev
->info
= &adrf6780_info
;
462 indio_dev
->name
= "adrf6780";
463 indio_dev
->channels
= adrf6780_channels
;
464 indio_dev
->num_channels
= ARRAY_SIZE(adrf6780_channels
);
468 adrf6780_properties_parse(st
);
470 st
->clkin
= devm_clk_get_enabled(&spi
->dev
, "lo_in");
471 if (IS_ERR(st
->clkin
))
472 return dev_err_probe(&spi
->dev
, PTR_ERR(st
->clkin
),
473 "failed to get the LO input clock\n");
475 mutex_init(&st
->lock
);
477 ret
= adrf6780_init(st
);
481 ret
= devm_add_action_or_reset(&spi
->dev
, adrf6780_powerdown
, st
);
485 return devm_iio_device_register(&spi
->dev
, indio_dev
);
488 static const struct spi_device_id adrf6780_id
[] = {
492 MODULE_DEVICE_TABLE(spi
, adrf6780_id
);
494 static const struct of_device_id adrf6780_of_match
[] = {
495 { .compatible
= "adi,adrf6780" },
498 MODULE_DEVICE_TABLE(of
, adrf6780_of_match
);
500 static struct spi_driver adrf6780_driver
= {
503 .of_match_table
= adrf6780_of_match
,
505 .probe
= adrf6780_probe
,
506 .id_table
= adrf6780_id
,
508 module_spi_driver(adrf6780_driver
);
510 MODULE_AUTHOR("Antoniu Miclaus <antoniu.miclaus@analog.com");
511 MODULE_DESCRIPTION("Analog Devices ADRF6780");
512 MODULE_LICENSE("GPL v2");