diff --git a/Documentation/devicetree/bindings/iio/amplifiers/adi,ad8366.yaml b/Documentation/devicetree/bindings/iio/amplifiers/adi,ad8366.yaml new file mode 100644 index 00000000000000..abf32482e57c53 --- /dev/null +++ b/Documentation/devicetree/bindings/iio/amplifiers/adi,ad8366.yaml @@ -0,0 +1,98 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/amplifiers/adi,ad8366.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: AD8366, similar Gain Amplifiers and Digital Attenuators + +maintainers: + - Michael Hennerich + - Rodrigo Alencar + +description: | + Digital Variable Gain Amplifiers (VGAs) and Digital Attenuators with + SPI interface. + + Supported devices: + * AD8366 Dual-Digital Variable Gain Amplifier (VGA) + * ADA4961: BiCMOS RF Digital Gain Amplifier (DGA) + * ADL5240: Digitally controlled Variable Gain Amplifier (VGA) + * ADRF5720: Silicon Digital Attenuator + * ADRF5730: Silicon Digital Attenuator + * ADRF5731: Silicon Digital Attenuator + * HMC271A: Digital Attenuator + * HMC792A: GaAs MMIC Digital Attenuator + * HMC1018A: GaAs MMIC Digital Attenuator + * HMC1019A: GaAs MMIC Digital Attenuator + * HMC1119: Silicon Digital Attenuator + +properties: + compatible: + enum: + - adi,ad8366 + - adi,ada4961 + - adi,adl5240 + - adi,adrf5720 + - adi,adrf5730 + - adi,adrf5731 + - adi,hmc271a + - adi,hmc792a + - adi,hmc1018a + - adi,hmc1019a + - adi,hmc1119 + + reg: + maxItems: 1 + + vcc-supply: + description: Regulator that provides power to the device. + + reset-gpios: + maxItems: 1 + description: + GPIO pin used to reset the device. + + enable-gpios: + maxItems: 1 + description: + GPIO pin used to enable the device. + +required: + - compatible + - reg + - vcc-supply + +allOf: + - $ref: /schemas/spi/spi-peripheral-props.yaml# + +unevaluatedProperties: false + +examples: + - | + #include + spi { + #address-cells = <1>; + #size-cells = <0>; + + amplifier@0 { + compatible = "adi,ad8366"; + reg = <0>; + spi-max-frequency = <1000000>; + vcc-supply = <&vcc_3v3>; + }; + }; + - | + #include + spi { + #address-cells = <1>; + #size-cells = <0>; + + attenuator@1 { + compatible = "adi,adrf5730"; + reg = <1>; + spi-max-frequency = <1000000>; + vcc-supply = <&vcc_3v3>; + }; + }; +... diff --git a/MAINTAINERS b/MAINTAINERS index 31d98efb1ad158..6b252d71ec0658 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1566,6 +1566,15 @@ W: https://ez.analog.com/linux-software-drivers F: Documentation/devicetree/bindings/iio/adc/adi,ad7780.yaml F: drivers/iio/adc/ad7780.c +ANALOG DEVICES INC AD8366 DRIVER +M: Michael Hennerich +M: Rodrigo Alencar +L: linux-iio@vger.kernel.org +S: Supported +W: https://ez.analog.com/linux-software-drivers +F: Documentation/devicetree/bindings/iio/amplifiers/adi,ad8366.yaml +F: drivers/iio/amplifiers/ad8366.c + ANALOG DEVICES INC AD9467 DRIVER M: Michael Hennerich M: Nuno Sa diff --git a/drivers/iio/amplifiers/Kconfig b/drivers/iio/amplifiers/Kconfig index 55eb16b32f6c9a..c77a9df91c161f 100644 --- a/drivers/iio/amplifiers/Kconfig +++ b/drivers/iio/amplifiers/Kconfig @@ -18,7 +18,13 @@ config AD8366 AD8366 Dual-Digital Variable Gain Amplifier (VGA) ADA4961 BiCMOS RF Digital Gain Amplifier (DGA) ADL5240 Digitally controlled variable gain amplifier (VGA) + ADRF5720: 0.5 dB LSB, 6-Bit, Silicon Digital Attenuator + ADRF5730: 0.5 dB LSB, 6-Bit, Silicon Digital Attenuator + ADRF5731: 2 dB LSB, 4-Bit, Silicon Digital Attenuator + HMC271A: 1dB LSB 5-Bit Digital Attenuator SMT HMC792A 0.25 dB LSB GaAs MMIC 6-Bit Digital Attenuator + HMC1018A: 1.0 dB LSB GaAs MMIC 5-BIT Digital Attenuator + HMC1019A: 0.5 dB LSB GaAs MMIC 5-BIT Digital Attenuator HMC1119 0.25 dB LSB, 7-Bit, Silicon Digital Attenuator To compile this driver as a module, choose M here: the diff --git a/drivers/iio/amplifiers/ad8366.c b/drivers/iio/amplifiers/ad8366.c index d06ac786501c47..2fe7c24902be50 100644 --- a/drivers/iio/amplifiers/ad8366.c +++ b/drivers/iio/amplifiers/ad8366.c @@ -5,110 +5,181 @@ * AD8366 Dual-Digital Variable Gain Amplifier (VGA) * ADA4961 BiCMOS RF Digital Gain Amplifier (DGA) * ADL5240 Digitally controlled variable gain amplifier (VGA) + * ADRF5720: 0.5 dB LSB, 6-Bit, Silicon Digital Attenuator, 9 kHz to 40 GHz + * ADRF5730: 0.5 dB LSB, 6-Bit, Silicon Digital Attenuator, 100 MHz to 40 GHz + * ADRF5731: 2 dB LSB, 4-Bit, Silicon Digital Attenuator, 100 MHz to 40 GHz + * HMC271A: 1dB LSB 5-Bit Digital Attenuator SMT, 0.7 - 3.7 GHz * HMC792A 0.25 dB LSB GaAs MMIC 6-Bit Digital Attenuator + * HMC1018A: 1.0 dB LSB GaAs MMIC 5-BIT DIGITAL ATTENUATOR, 0.1 - 30 GHz + * HMC1019A: 0.5 dB LSB GaAs MMIC 5-BIT DIGITAL ATTENUATOR, 0.1 - 30 GHz * HMC1119 0.25 dB LSB, 7-Bit, Silicon Digital Attenuator * - * Copyright 2012-2019 Analog Devices Inc. + * Copyright 2012-2025 Analog Devices Inc. */ +#include +#include #include -#include -#include -#include -#include -#include -#include #include -#include -#include - +#include #include #include +#include +#include +#include +#include +#include +#include enum ad8366_type { ID_AD8366, ID_ADA4961, ID_ADL5240, + ID_ADRF5720, + ID_ADRF5730, + ID_ADRF5731, + ID_HMC271, ID_HMC792, + ID_HMC1018, + ID_HMC1019, ID_HMC1119, }; +struct ad8366_data { + unsigned char ch[2]; + /* + * DMA (thus cache coherency maintenance) may require the + * transfer buffers to live in their own cache lines. + */ + unsigned char buf[2] __aligned(IIO_DMA_MINALIGN); +}; + struct ad8366_info { int gain_min; int gain_max; + int gain_step; + int num_channels; + size_t (*pack_code)(struct ad8366_data *data); }; struct ad8366_state { - struct spi_device *spi; - struct regulator *reg; - struct mutex lock; /* protect sensor state */ - struct gpio_desc *reset_gpio; - unsigned char ch[2]; - enum ad8366_type type; - const struct ad8366_info *info; - /* - * DMA (thus cache coherency maintenance) may require the - * transfer buffers to live in their own cache lines. - */ - unsigned char data[2] __aligned(IIO_DMA_MINALIGN); + struct spi_device *spi; + struct mutex lock; /* protect sensor state */ + struct gpio_desc *reset_gpio; + struct gpio_desc *enable_gpio; + const struct ad8366_info *info; + struct ad8366_data data; }; +static size_t ad8366_pack_code(struct ad8366_data *data) +{ + unsigned char ch_a = bitrev8(data->ch[0] & 0x3F); + unsigned char ch_b = bitrev8(data->ch[1] & 0x3F); + + data->buf[0] = ch_b >> 4; + data->buf[1] = (ch_b << 4) | (ch_a >> 2); + + return 2; +} + +static size_t simple_pack_code(struct ad8366_data *data) +{ + data->buf[0] = data->ch[0]; + return 1; +} + +static size_t adrf5731_pack_code(struct ad8366_data *data) +{ + data->buf[0] = data->ch[0] << 2; + return 1; +} + +static size_t hmc271_pack_code(struct ad8366_data *data) +{ + data->buf[0] = bitrev8(data->ch[0] & 0x1F) >> 3; + return 1; +} + static const struct ad8366_info ad8366_infos[] = { [ID_AD8366] = { .gain_min = 4500, .gain_max = 20500, + .gain_step = 253, + .num_channels = 2, + .pack_code = ad8366_pack_code, }, [ID_ADA4961] = { .gain_min = -6000, .gain_max = 15000, + .gain_step = -1000, + .num_channels = 1, + .pack_code = simple_pack_code, }, [ID_ADL5240] = { .gain_min = -11500, .gain_max = 20000, + .gain_step = 500, + .num_channels = 1, + .pack_code = simple_pack_code, + }, + [ID_ADRF5720] = { + .gain_min = -31500, + .gain_max = 0, + .gain_step = -500, + .num_channels = 1, + .pack_code = simple_pack_code, + }, + [ID_ADRF5730] = { + .gain_min = -31500, + .gain_max = 0, + .gain_step = -500, + .num_channels = 1, + .pack_code = simple_pack_code, + }, + [ID_ADRF5731] = { + .gain_min = -30000, + .gain_max = 0, + .gain_step = -2000, + .num_channels = 1, + .pack_code = adrf5731_pack_code, + }, + [ID_HMC271] = { + .gain_min = -31000, + .gain_max = 0, + .gain_step = 1000, + .num_channels = 1, + .pack_code = hmc271_pack_code, }, [ID_HMC792] = { .gain_min = -15750, .gain_max = 0, + .gain_step = 250, + .num_channels = 1, + .pack_code = simple_pack_code, + }, + [ID_HMC1018] = { + .gain_min = -31000, + .gain_max = 0, + .gain_step = 1000, + .num_channels = 1, + .pack_code = simple_pack_code, + }, + [ID_HMC1019] = { + .gain_min = -15500, + .gain_max = 0, + .gain_step = 500, + .num_channels = 1, + .pack_code = simple_pack_code, }, [ID_HMC1119] = { .gain_min = -31750, .gain_max = 0, + .gain_step = -250, + .num_channels = 1, + .pack_code = simple_pack_code, }, }; -static int ad8366_write(struct iio_dev *indio_dev, - unsigned char ch_a, unsigned char ch_b) -{ - struct ad8366_state *st = iio_priv(indio_dev); - int ret; - - switch (st->type) { - case ID_AD8366: - ch_a = bitrev8(ch_a & 0x3F); - ch_b = bitrev8(ch_b & 0x3F); - - st->data[0] = ch_b >> 4; - st->data[1] = (ch_b << 4) | (ch_a >> 2); - break; - case ID_ADA4961: - st->data[0] = ch_a & 0x1F; - break; - case ID_ADL5240: - st->data[0] = (ch_a & 0x3F); - break; - case ID_HMC792: - case ID_HMC1119: - st->data[0] = ch_a; - break; - } - - ret = spi_write(st->spi, st->data, indio_dev->num_channels); - if (ret < 0) - dev_err(&indio_dev->dev, "write failed (%d)", ret); - - return ret; -} - static int ad8366_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, @@ -116,33 +187,20 @@ static int ad8366_read_raw(struct iio_dev *indio_dev, long m) { struct ad8366_state *st = iio_priv(indio_dev); + const struct ad8366_info *inf = st->info; int ret; - int code, gain = 0; + int code, gain_base, gain = 0; + + gain_base = inf->gain_step > 0 ? inf->gain_min : inf->gain_max; + + guard(mutex)(&st->lock); - mutex_lock(&st->lock); switch (m) { case IIO_CHAN_INFO_HARDWAREGAIN: - code = st->ch[chan->channel]; - - switch (st->type) { - case ID_AD8366: - gain = code * 253 + 4500; - break; - case ID_ADA4961: - gain = 15000 - code * 1000; - break; - case ID_ADL5240: - gain = 20000 - 31500 + code * 500; - break; - case ID_HMC792: - gain = -1 * code * 500; - break; - case ID_HMC1119: - gain = -1 * code * 250; - break; - } - - /* Values in dB */ + code = st->data.ch[chan->channel]; + + /* code to gain value in dB */ + gain = gain_base + code * inf->gain_step; *val = gain / 1000; *val2 = (gain % 1000) * 1000; @@ -151,7 +209,6 @@ static int ad8366_read_raw(struct iio_dev *indio_dev, default: ret = -EINVAL; } - mutex_unlock(&st->lock); return ret; }; @@ -164,7 +221,7 @@ static int ad8366_write_raw(struct iio_dev *indio_dev, { struct ad8366_state *st = iio_priv(indio_dev); const struct ad8366_info *inf = st->info; - int code = 0, gain; + int code = 0, gain, gain_base; int ret; /* Values in dB */ @@ -176,34 +233,23 @@ static int ad8366_write_raw(struct iio_dev *indio_dev, if (gain > inf->gain_max || gain < inf->gain_min) return -EINVAL; - switch (st->type) { - case ID_AD8366: - code = (gain - 4500) / 253; - break; - case ID_ADA4961: - code = (15000 - gain) / 1000; - break; - case ID_ADL5240: - code = ((gain - 500 - 20000) / 500) & 0x3F; - break; - case ID_HMC792: - code = (abs(gain) / 500) & 0x3F; - break; - case ID_HMC1119: - code = (abs(gain) / 250) & 0x7F; - break; - } + gain_base = inf->gain_step > 0 ? inf->gain_min : inf->gain_max; + code = (gain - gain_base) / inf->gain_step; + + guard(mutex)(&st->lock); - mutex_lock(&st->lock); switch (mask) { case IIO_CHAN_INFO_HARDWAREGAIN: - st->ch[chan->channel] = code; - ret = ad8366_write(indio_dev, st->ch[0], st->ch[1]); + st->data.ch[chan->channel] = code; + + ret = spi_write(st->spi, st->data.buf, inf->pack_code(&st->data)); + if (ret < 0) + dev_err(&indio_dev->dev, "write failed (%d)", ret); + break; default: ret = -EINVAL; } - mutex_unlock(&st->lock); return ret; } @@ -239,10 +285,6 @@ static const struct iio_chan_spec ad8366_channels[] = { AD8366_CHAN(1), }; -static const struct iio_chan_spec ada4961_channels[] = { - AD8366_CHAN(0), -}; - static int ad8366_probe(struct spi_device *spi) { struct iio_dev *indio_dev; @@ -250,97 +292,86 @@ static int ad8366_probe(struct spi_device *spi) int ret; indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); - if (indio_dev == NULL) + if (!indio_dev) return -ENOMEM; st = iio_priv(indio_dev); - - st->reg = devm_regulator_get(&spi->dev, "vcc"); - if (!IS_ERR(st->reg)) { - ret = regulator_enable(st->reg); - if (ret) - return ret; - } - - spi_set_drvdata(spi, indio_dev); - mutex_init(&st->lock); st->spi = spi; - st->type = spi_get_device_id(spi)->driver_data; - - switch (st->type) { - case ID_AD8366: - indio_dev->channels = ad8366_channels; - indio_dev->num_channels = ARRAY_SIZE(ad8366_channels); - break; - case ID_ADA4961: - case ID_ADL5240: - case ID_HMC792: - case ID_HMC1119: - st->reset_gpio = devm_gpiod_get_optional(&spi->dev, "reset", GPIOD_OUT_HIGH); - if (IS_ERR(st->reset_gpio)) { - ret = PTR_ERR(st->reset_gpio); - goto error_disable_reg; - } - indio_dev->channels = ada4961_channels; - indio_dev->num_channels = ARRAY_SIZE(ada4961_channels); - break; - default: - dev_err(&spi->dev, "Invalid device ID\n"); - ret = -EINVAL; - goto error_disable_reg; - } - - st->info = &ad8366_infos[st->type]; - indio_dev->name = spi_get_device_id(spi)->name; - indio_dev->info = &ad8366_info; - indio_dev->modes = INDIO_DIRECT_MODE; + st->info = spi_get_device_match_data(spi); + if (!st->info) + return -EINVAL; - ret = ad8366_write(indio_dev, 0, 0); - if (ret < 0) - goto error_disable_reg; + spi_set_drvdata(spi, indio_dev); - ret = iio_device_register(indio_dev); + ret = devm_regulator_get_enable(&spi->dev, "vcc"); if (ret) - goto error_disable_reg; + return dev_err_probe(&spi->dev, ret, "Failed to get regulator\n"); - return 0; + ret = devm_mutex_init(&spi->dev, &st->lock); + if (ret) + return dev_err_probe(&spi->dev, ret, "failed to initialize mutex\n"); -error_disable_reg: - if (!IS_ERR(st->reg)) - regulator_disable(st->reg); + st->reset_gpio = devm_gpiod_get_optional(&spi->dev, "reset", GPIOD_OUT_HIGH); + if (IS_ERR(st->reset_gpio)) + return dev_err_probe(&spi->dev, PTR_ERR(st->reset_gpio), + "Failed to get reset GPIO\n"); - return ret; -} + st->enable_gpio = devm_gpiod_get_optional(&spi->dev, "enable", GPIOD_OUT_HIGH); + if (IS_ERR(st->enable_gpio)) + return dev_err_probe(&spi->dev, PTR_ERR(st->enable_gpio), + "Failed to get enable GPIO\n"); -static void ad8366_remove(struct spi_device *spi) -{ - struct iio_dev *indio_dev = spi_get_drvdata(spi); - struct ad8366_state *st = iio_priv(indio_dev); - struct regulator *reg = st->reg; + indio_dev->name = spi_get_device_id(spi)->name; + indio_dev->info = &ad8366_info; + indio_dev->channels = ad8366_channels; + indio_dev->num_channels = st->info->num_channels; + indio_dev->modes = INDIO_DIRECT_MODE; - iio_device_unregister(indio_dev); + ret = spi_write(st->spi, st->data.buf, st->info->pack_code(&st->data)); + if (ret < 0) + return dev_err_probe(&spi->dev, ret, "failed to write initial gain\n"); - if (!IS_ERR(reg)) - regulator_disable(reg); + return devm_iio_device_register(&spi->dev, indio_dev); } static const struct spi_device_id ad8366_id[] = { - {"ad8366", ID_AD8366}, - {"ada4961", ID_ADA4961}, - {"adl5240", ID_ADL5240}, - {"hmc792a", ID_HMC792}, - {"hmc1119", ID_HMC1119}, + {"ad8366", (kernel_ulong_t)&ad8366_infos[ID_AD8366]}, + {"ada4961", (kernel_ulong_t)&ad8366_infos[ID_ADA4961]}, + {"adrf5720", (kernel_ulong_t)&ad8366_infos[ID_ADRF5720]}, + {"adrf5730", (kernel_ulong_t)&ad8366_infos[ID_ADRF5730]}, + {"adrf5731", (kernel_ulong_t)&ad8366_infos[ID_ADRF5731]}, + {"adl5240", (kernel_ulong_t)&ad8366_infos[ID_ADL5240]}, + {"hmc271", (kernel_ulong_t)&ad8366_infos[ID_HMC271]}, + {"hmc792a", (kernel_ulong_t)&ad8366_infos[ID_HMC792]}, + {"hmc1018a", (kernel_ulong_t)&ad8366_infos[ID_HMC1018]}, + {"hmc1019a", (kernel_ulong_t)&ad8366_infos[ID_HMC1019]}, + {"hmc1119", (kernel_ulong_t)&ad8366_infos[ID_HMC1119]}, { } }; MODULE_DEVICE_TABLE(spi, ad8366_id); +static const struct of_device_id ad8366_of_match[] = { + { .compatible = "adi,ad8366", .data = &ad8366_infos[ID_AD8366] }, + { .compatible = "adi,ada4961", .data = &ad8366_infos[ID_ADA4961] }, + { .compatible = "adi,adrf5720", .data = &ad8366_infos[ID_ADRF5720] }, + { .compatible = "adi,adrf5730", .data = &ad8366_infos[ID_ADRF5730] }, + { .compatible = "adi,adrf5731", .data = &ad8366_infos[ID_ADRF5731] }, + { .compatible = "adi,adl5240", .data = &ad8366_infos[ID_ADL5240] }, + { .compatible = "adi,hmc792a", .data = &ad8366_infos[ID_HMC792] }, + { .compatible = "adi,hmc1018a", .data = &ad8366_infos[ID_HMC1018] }, + { .compatible = "adi,hmc1019a", .data = &ad8366_infos[ID_HMC1019] }, + { .compatible = "adi,hmc1119", .data = &ad8366_infos[ID_HMC1119] }, + { } +}; +MODULE_DEVICE_TABLE(of, ad8366_of_match); + static struct spi_driver ad8366_driver = { .driver = { - .name = KBUILD_MODNAME, + .name = KBUILD_MODNAME, + .of_match_table = ad8366_of_match, }, - .probe = ad8366_probe, - .remove = ad8366_remove, - .id_table = ad8366_id, + .probe = ad8366_probe, + .id_table = ad8366_id, }; module_spi_driver(ad8366_driver);