From 8149e1f00230b53fedff43f467bb76228cf793d7 Mon Sep 17 00:00:00 2001 From: Rodrigo Alencar Date: Fri, 21 Nov 2025 16:08:48 +0000 Subject: [PATCH 1/6] iio: amplifiers: ad8366: driver update and dt support This patch series modernizes the AD8366 amplifier/attenuator driver with several improvements and adds support for additional device variants. Device Support Expansion: - Adds support for HMC271 (legacy/obsolete part maintained in ADI tree) - Adds support for multiple digital step attenuators: - ADRF5720: 0.5 dB LSB, 6-Bit, Digital Attenuator, 9 kHz to 40 GHz - ADRF5730: 0.5 dB LSB, 6-Bit, Digital Attenuator, 100 MHz to 40 GHz - ADRF5731: 2 dB LSB, 4-Bit, Digital Attenuator, 100 MHz to 40 GHz - 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 Device Tree Support: - Adds device tree binding documentation - Adds device tree compatible strings for all supported devices # Describe the purpose of this series. The information you put here # will be used by the project maintainer to make a decision whether # your patches should be reviewed, and in what priority order. Please be # very detailed and link to any relevant discussions or sites that the # maintainer can review to better understand your proposed changes. If you # only have a single patch in your series, the contents of the cover # letter will be appended to the "under-the-cut" portion of the patch. # Lines starting with # will be removed from the cover letter. You can # use them to add notes or reminders to yourself. If you want to use # markdown headers in your cover letter, start the line with ">#". # You can add trailers to the cover letter. Any email addresses found in # these trailers will be added to the addresses specified/generated # during the b4 send stage. You can also run "b4 prep --auto-to-cc" to # auto-populate the To: and Cc: trailers based on the code being # modified. Signed-off-by: Rodrigo Alencar --- b4-submit-tracking --- # This section is used internally by b4 prep for tracking purposes. { "series": { "revision": 1, "change-id": "20251121-iio-ad8366-update-56abac58bbca", "prefixes": [] } } From 16b15637a4aeb8b6813c565b9e68c552f11a9897 Mon Sep 17 00:00:00 2001 From: Rodrigo Alencar Date: Fri, 21 Nov 2025 15:31:16 +0000 Subject: [PATCH 2/6] dt-bindings: iio: amplifiers: Add AD8366 Add device tree binding documentation for amplifiers and digital attenuators. This covers different device variants with similar SPI control. Signed-off-by: Rodrigo Alencar --- .../bindings/iio/amplifiers/adi,ad8366.yaml | 98 +++++++++++++++++++ MAINTAINERS | 9 ++ 2 files changed, 107 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/amplifiers/adi,ad8366.yaml 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 From 0a066cbea6bb4f703354c68ad56adbff9a0dd9e2 Mon Sep 17 00:00:00 2001 From: Alexandru Ardelean Date: Mon, 6 Jul 2020 11:58:17 +0300 Subject: [PATCH 3/6] iio: amplifiers: ad8366: Update device support This patch adds support for following digital step attenuators: * HMC271A: 1dB LSB 5-Bit Digital Attenuator SMT, 0.7 - 3.7 GHz * ADRF5720: 0.5 dB LSB, 6-Bit, Digital Attenuator, 9 kHz to 40 GHz * ADRF5730: 0.5 dB LSB, 6-Bit, Digital Attenuator, 100 MHz to 40 GHz * ADRF5731: 2 dB LSB, 4-Bit, Digital Attenuator, 100 MHz to 40 GHz * 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 Co-authored-by: Alexandru Ardelean Co-authored-by: Michael Hennerich Signed-off-by: Alexandru Ardelean Signed-off-by: Michael Hennerich Signed-off-by: Rodrigo Alencar --- drivers/iio/amplifiers/Kconfig | 6 +++ drivers/iio/amplifiers/ad8366.c | 93 ++++++++++++++++++++++++++++++++- 2 files changed, 97 insertions(+), 2 deletions(-) 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..d2eb3ece41314f 100644 --- a/drivers/iio/amplifiers/ad8366.c +++ b/drivers/iio/amplifiers/ad8366.c @@ -5,10 +5,16 @@ * 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 @@ -29,7 +35,13 @@ 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, }; @@ -43,6 +55,7 @@ struct ad8366_state { struct regulator *reg; struct mutex lock; /* protect sensor state */ struct gpio_desc *reset_gpio; + struct gpio_desc *enable_gpio; unsigned char ch[2]; enum ad8366_type type; const struct ad8366_info *info; @@ -66,10 +79,34 @@ static const struct ad8366_info ad8366_infos[] = { .gain_min = -11500, .gain_max = 20000, }, + [ID_ADRF5720] = { + .gain_min = -31500, + .gain_max = 0, + }, + [ID_ADRF5730] = { + .gain_min = -31500, + .gain_max = 0, + }, + [ID_ADRF5731] = { + .gain_min = -30000, + .gain_max = 0, + }, + [ID_HMC271] = { + .gain_min = -31000, + .gain_max = 0, + }, [ID_HMC792] = { .gain_min = -15750, .gain_max = 0, }, + [ID_HMC1018] = { + .gain_min = -31000, + .gain_max = 0, + }, + [ID_HMC1019] = { + .gain_min = -15500, + .gain_max = 0, + }, [ID_HMC1119] = { .gain_min = -31750, .gain_max = 0, @@ -94,9 +131,17 @@ static int ad8366_write(struct iio_dev *indio_dev, st->data[0] = ch_a & 0x1F; break; case ID_ADL5240: + case ID_ADRF5720: + case ID_ADRF5730: + case ID_ADRF5731: st->data[0] = (ch_a & 0x3F); break; + case ID_HMC271: + st->data[0] = bitrev8(ch_a & 0x1F) >> 3; + break; case ID_HMC792: + case ID_HMC1018: + case ID_HMC1019: case ID_HMC1119: st->data[0] = ch_a; break; @@ -134,9 +179,23 @@ static int ad8366_read_raw(struct iio_dev *indio_dev, case ID_ADL5240: gain = 20000 - 31500 + code * 500; break; + case ID_ADRF5720: + case ID_ADRF5730: + gain = -1 * code * 500; + break; + case ID_ADRF5731: + gain = -1 * code * 500; + break; case ID_HMC792: gain = -1 * code * 500; break; + case ID_HMC271: + case ID_HMC1018: + gain = -31000 + code * 1000; + break; + case ID_HMC1019: + gain = -15500 + code * 500; + break; case ID_HMC1119: gain = -1 * code * 250; break; @@ -186,9 +245,23 @@ static int ad8366_write_raw(struct iio_dev *indio_dev, case ID_ADL5240: code = ((gain - 500 - 20000) / 500) & 0x3F; break; + case ID_ADRF5720: + case ID_ADRF5730: + code = (abs(gain) / 500) & 0x3F; + break; + case ID_ADRF5731: + code = (abs(gain) / 500) & 0x3C; + break; case ID_HMC792: code = (abs(gain) / 500) & 0x3F; break; + case ID_HMC271: + case ID_HMC1018: + code = ((gain - 1000) / 1000) & 0x1F; + break; + case ID_HMC1019: + code = ((gain - 500) / 500) & 0x1F; + break; case ID_HMC1119: code = (abs(gain) / 250) & 0x7F; break; @@ -274,13 +347,23 @@ static int ad8366_probe(struct spi_device *spi) break; case ID_ADA4961: case ID_ADL5240: + case ID_HMC271: case ID_HMC792: case ID_HMC1119: + case ID_ADRF5720: + case ID_ADRF5730: + case ID_ADRF5731: + case ID_HMC1018: + case ID_HMC1019: 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; } + + st->enable_gpio = devm_gpiod_get(&spi->dev, "enable", + GPIOD_OUT_HIGH); + indio_dev->channels = ada4961_channels; indio_dev->num_channels = ARRAY_SIZE(ada4961_channels); break; @@ -325,10 +408,16 @@ static void ad8366_remove(struct spi_device *spi) } static const struct spi_device_id ad8366_id[] = { - {"ad8366", ID_AD8366}, + {"ad8366", ID_AD8366}, {"ada4961", ID_ADA4961}, + {"adrf5720", ID_ADRF5720}, + {"adrf5730", ID_ADRF5730}, + {"adrf5731", ID_ADRF5731}, {"adl5240", ID_ADL5240}, + {"hmc271", ID_HMC271}, {"hmc792a", ID_HMC792}, + {"hmc1018a", ID_HMC1018}, + {"hmc1019a", ID_HMC1019}, {"hmc1119", ID_HMC1119}, { } }; From 02cd17175f5849b3c07b66a9f5b118638496098a Mon Sep 17 00:00:00 2001 From: Rodrigo Alencar Date: Mon, 24 Nov 2025 14:37:28 +0000 Subject: [PATCH 4/6] iio: amplifiers: ad8366: use cleanup.h mutex guard use guard() from cleanup for mutex locking. replace mutex_init() for devm_mutex_init(). Signed-off-by: Rodrigo Alencar --- drivers/iio/amplifiers/ad8366.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/drivers/iio/amplifiers/ad8366.c b/drivers/iio/amplifiers/ad8366.c index d2eb3ece41314f..63a373c3be3246 100644 --- a/drivers/iio/amplifiers/ad8366.c +++ b/drivers/iio/amplifiers/ad8366.c @@ -17,6 +17,7 @@ * Copyright 2012-2025 Analog Devices Inc. */ +#include #include #include #include @@ -53,7 +54,7 @@ struct ad8366_info { struct ad8366_state { struct spi_device *spi; struct regulator *reg; - struct mutex lock; /* protect sensor state */ + struct mutex lock; /* protect sensor state */ struct gpio_desc *reset_gpio; struct gpio_desc *enable_gpio; unsigned char ch[2]; @@ -164,7 +165,8 @@ static int ad8366_read_raw(struct iio_dev *indio_dev, int ret; int code, gain = 0; - mutex_lock(&st->lock); + guard(mutex)(&st->lock); + switch (m) { case IIO_CHAN_INFO_HARDWAREGAIN: code = st->ch[chan->channel]; @@ -210,7 +212,6 @@ static int ad8366_read_raw(struct iio_dev *indio_dev, default: ret = -EINVAL; } - mutex_unlock(&st->lock); return ret; }; @@ -267,7 +268,8 @@ static int ad8366_write_raw(struct iio_dev *indio_dev, break; } - mutex_lock(&st->lock); + guard(mutex)(&st->lock); + switch (mask) { case IIO_CHAN_INFO_HARDWAREGAIN: st->ch[chan->channel] = code; @@ -276,7 +278,6 @@ static int ad8366_write_raw(struct iio_dev *indio_dev, default: ret = -EINVAL; } - mutex_unlock(&st->lock); return ret; } @@ -323,7 +324,7 @@ 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); @@ -336,10 +337,13 @@ static int ad8366_probe(struct spi_device *spi) } spi_set_drvdata(spi, indio_dev); - mutex_init(&st->lock); st->spi = spi; st->type = spi_get_device_id(spi)->driver_data; + ret = devm_mutex_init(&spi->dev, &st->lock); + if (ret) + return dev_err_probe(&spi->dev, ret, "failed to initialize mutex\n"); + switch (st->type) { case ID_AD8366: indio_dev->channels = ad8366_channels; From 83630a9bc4b3c3b40a6bd2926823f63ed8e46e0e Mon Sep 17 00:00:00 2001 From: Rodrigo Alencar Date: Mon, 24 Nov 2025 14:52:23 +0000 Subject: [PATCH 5/6] iio: amplifiers: ad8366: simplify resource management Device resource managed simplified with: - voltage regulator managed internally. - IIO device registration handled with devm_iio_device_register(). - removal of goto's from the probe function. - ad8366_remove() removed as it is not needed anymore. Also, dev_err_probe() is used to report probe errors. Signed-off-by: Rodrigo Alencar --- drivers/iio/amplifiers/ad8366.c | 59 +++++++++------------------------ 1 file changed, 15 insertions(+), 44 deletions(-) diff --git a/drivers/iio/amplifiers/ad8366.c b/drivers/iio/amplifiers/ad8366.c index 63a373c3be3246..ff3e37ba4a32df 100644 --- a/drivers/iio/amplifiers/ad8366.c +++ b/drivers/iio/amplifiers/ad8366.c @@ -53,7 +53,6 @@ struct ad8366_info { struct ad8366_state { struct spi_device *spi; - struct regulator *reg; struct mutex lock; /* protect sensor state */ struct gpio_desc *reset_gpio; struct gpio_desc *enable_gpio; @@ -328,17 +327,13 @@ static int ad8366_probe(struct spi_device *spi) 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); st->spi = spi; st->type = spi_get_device_id(spi)->driver_data; + spi_set_drvdata(spi, indio_dev); + + ret = devm_regulator_get_enable(&spi->dev, "vcc"); + if (ret) + return dev_err_probe(&spi->dev, ret, "Failed to get regulator\n"); ret = devm_mutex_init(&spi->dev, &st->lock); if (ret) @@ -360,21 +355,20 @@ static int ad8366_probe(struct spi_device *spi) case ID_HMC1018: case ID_HMC1019: 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; - } + if (IS_ERR(st->reset_gpio)) + return dev_err_probe(&spi->dev, PTR_ERR(st->reset_gpio), + "Failed to get reset GPIO\n"); - st->enable_gpio = devm_gpiod_get(&spi->dev, "enable", - GPIOD_OUT_HIGH); + 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"); 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; + return dev_err_probe(&spi->dev, -EINVAL, "Invalid device ID\n"); } st->info = &ad8366_infos[st->type]; @@ -384,31 +378,9 @@ static int ad8366_probe(struct spi_device *spi) ret = ad8366_write(indio_dev, 0, 0); if (ret < 0) - goto error_disable_reg; - - ret = iio_device_register(indio_dev); - if (ret) - goto error_disable_reg; - - return 0; - -error_disable_reg: - if (!IS_ERR(st->reg)) - regulator_disable(st->reg); - - return ret; -} - -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; - - iio_device_unregister(indio_dev); + 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[] = { @@ -432,7 +404,6 @@ static struct spi_driver ad8366_driver = { .name = KBUILD_MODNAME, }, .probe = ad8366_probe, - .remove = ad8366_remove, .id_table = ad8366_id, }; From 02e319f4a7b74949080c7c54064bb4ad2a89798b Mon Sep 17 00:00:00 2001 From: Rodrigo Alencar Date: Mon, 24 Nov 2025 18:00:51 +0000 Subject: [PATCH 6/6] iio: amplifiers: ad8366: add device tree support device-tree support achieved dropping the enum ID in favor of the chip info table. With this, switch cases on the device type were dropped. Signed-off-by: Rodrigo Alencar --- drivers/iio/amplifiers/ad8366.c | 335 ++++++++++++++------------------ 1 file changed, 151 insertions(+), 184 deletions(-) diff --git a/drivers/iio/amplifiers/ad8366.c b/drivers/iio/amplifiers/ad8366.c index ff3e37ba4a32df..2fe7c24902be50 100644 --- a/drivers/iio/amplifiers/ad8366.c +++ b/drivers/iio/amplifiers/ad8366.c @@ -17,20 +17,19 @@ * 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, @@ -46,114 +45,141 @@ enum ad8366_type { 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 mutex lock; /* protect sensor state */ - struct gpio_desc *reset_gpio; - struct gpio_desc *enable_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: - case ID_ADRF5720: - case ID_ADRF5730: - case ID_ADRF5731: - st->data[0] = (ch_a & 0x3F); - break; - case ID_HMC271: - st->data[0] = bitrev8(ch_a & 0x1F) >> 3; - break; - case ID_HMC792: - case ID_HMC1018: - case ID_HMC1019: - 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, @@ -161,48 +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); 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_ADRF5720: - case ID_ADRF5730: - gain = -1 * code * 500; - break; - case ID_ADRF5731: - gain = -1 * code * 500; - break; - case ID_HMC792: - gain = -1 * code * 500; - break; - case ID_HMC271: - case ID_HMC1018: - gain = -31000 + code * 1000; - break; - case ID_HMC1019: - gain = -15500 + 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; @@ -223,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 */ @@ -235,44 +233,19 @@ 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_ADRF5720: - case ID_ADRF5730: - code = (abs(gain) / 500) & 0x3F; - break; - case ID_ADRF5731: - code = (abs(gain) / 500) & 0x3C; - break; - case ID_HMC792: - code = (abs(gain) / 500) & 0x3F; - break; - case ID_HMC271: - case ID_HMC1018: - code = ((gain - 1000) / 1000) & 0x1F; - break; - case ID_HMC1019: - code = ((gain - 500) / 500) & 0x1F; - 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); 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; @@ -312,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; @@ -328,7 +297,10 @@ static int ad8366_probe(struct spi_device *spi) st = iio_priv(indio_dev); st->spi = spi; - st->type = spi_get_device_id(spi)->driver_data; + st->info = spi_get_device_match_data(spi); + if (!st->info) + return -EINVAL; + spi_set_drvdata(spi, indio_dev); ret = devm_regulator_get_enable(&spi->dev, "vcc"); @@ -339,44 +311,23 @@ static int ad8366_probe(struct spi_device *spi) if (ret) return dev_err_probe(&spi->dev, ret, "failed to initialize mutex\n"); - 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_HMC271: - case ID_HMC792: - case ID_HMC1119: - case ID_ADRF5720: - case ID_ADRF5730: - case ID_ADRF5731: - case ID_HMC1018: - case ID_HMC1019: - 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"); - - 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"); - - indio_dev->channels = ada4961_channels; - indio_dev->num_channels = ARRAY_SIZE(ada4961_channels); - break; - default: - return dev_err_probe(&spi->dev, -EINVAL, "Invalid device ID\n"); - } + 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"); + + 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"); - st->info = &ad8366_infos[st->type]; 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; - ret = ad8366_write(indio_dev, 0, 0); + 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"); @@ -384,27 +335,43 @@ static int ad8366_probe(struct spi_device *spi) } static const struct spi_device_id ad8366_id[] = { - {"ad8366", ID_AD8366}, - {"ada4961", ID_ADA4961}, - {"adrf5720", ID_ADRF5720}, - {"adrf5730", ID_ADRF5730}, - {"adrf5731", ID_ADRF5731}, - {"adl5240", ID_ADL5240}, - {"hmc271", ID_HMC271}, - {"hmc792a", ID_HMC792}, - {"hmc1018a", ID_HMC1018}, - {"hmc1019a", ID_HMC1019}, - {"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, - .id_table = ad8366_id, + .probe = ad8366_probe, + .id_table = ad8366_id, }; module_spi_driver(ad8366_driver);