[PATCH v4 6/6] iio: dac: ad5504: support scale via output-range-microvolt property
Taha Ed-Dafili <[email protected]>
| Newsgroups | org.kernel.vger.linux-devicetree,org.kernel.vger.linux-iio,org.kernel.vger.linux-kernel |
|---|---|
| Message-ID | <[email protected]> |
The AD5504/AD5501 output range (0-30V or 0-60V) is determined by the hardware R_SEL pin and is required to compute IIO_CHAN_INFO_SCALE correctly. Previously the driver derived this solely from the vcc regulator's configured voltage, which conflated the supply voltage with the DAC's actual output range and offered no way to express the range explicitly in firmware. Add support for the standard 'output-range-microvolt' device property, validating that it specifies one of the two supported ranges (0-30V or 0-60V) and using it to set st->vref_mv directly. When this property is present, the vcc regulator is only enabled (not read) via devm_regulator_get_enable(), since the regulator's actual voltage is no longer the source of truth for the output range. For backward compatibility with older device trees that predate this property, fall back to reading the vcc regulator's configured voltage via devm_regulator_get_enable_read_voltage() when 'output-range-microvolt' is absent. Use device_property_present() to explicitly distinguish "property absent" from "property present but malformed", rather than relying on the -EINVAL return from device_property_read_u32_array() as an absence sentinel. That return code is ambiguous: it is also returned when the property exists but the parsed array size does not match the expected length, which would have silently and incorrectly routed a malformed property through the legacy regulator-voltage fallback instead of surfacing a clear validation error. Signed-off-by: Taha Ed-Dafili <[email protected]> --- drivers/iio/dac/ad5504.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/drivers/iio/dac/ad5504.c b/drivers/iio/dac/ad5504.c index 546420f9afc0..25c0744545f0 100644 --- a/drivers/iio/dac/ad5504.c +++ b/drivers/iio/dac/ad5504.c @@ -15,6 +15,7 @@ #include <linux/mod_devicetable.h> #include <linux/module.h> #include <linux/mutex.h> +#include <linux/property.h> #include <linux/regulator/consumer.h> #include <linux/spi/spi.h> #include <linux/sysfs.h> @@ -293,6 +294,7 @@ static int ad5504_probe(struct spi_device *spi) struct device *dev = &spi->dev; struct iio_dev *indio_dev; struct ad5504_state *st; + u32 range[2]; int ret; indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); @@ -308,12 +310,30 @@ static int ad5504_probe(struct spi_device *spi) if (ACPI_COMPANION(dev)) { st->vref_mv = AD5504_VA_MV_ACPI_DEFAULT; } else { - ret = devm_regulator_get_enable_read_voltage(dev, "vcc"); - if (ret < 0) - return dev_err_probe(dev, ret, - "Failed to get vcc regulator\n"); - - st->vref_mv = ret / MILLI; + if (device_property_present(dev, "output-range-microvolt")) { + ret = device_property_read_u32_array(dev, "output-range-microvolt", + range, ARRAY_SIZE(range)); + if (ret) + return dev_err_probe(dev, ret, + "Error parsing output-range-microvolt\n"); + + if (range[0] != 0 || (range[1] != 30 * MICRO && range[1] != 60 * MICRO)) + return dev_err_probe(dev, -EINVAL, + "Invalid output-range-microvolt\n"); + + st->vref_mv = range[1] / MILLI; + + ret = devm_regulator_get_enable(dev, "vcc"); + if (ret < 0) + return dev_err_probe(dev, ret, "Failed to enable vcc regulator\n"); + } else { + /* Backward compat: old DTs without output-range-microvolt */ + ret = devm_regulator_get_enable_read_voltage(dev, "vcc"); + if (ret < 0) + return dev_err_probe(dev, ret, "Failed to get vcc regulator\n"); + + st->vref_mv = ret / MILLI; + } } st->spi = spi; -- 2.55.0