iio: dac: ad5686: use devm_regulator_get_enable_read_voltage()
Simplify the code by using devm_regulator_get_enable_read_voltage(). There is a small change in behavior. Before, all errors from devm_regulator_get_optional() were ignored and assumed to mean that the external reference supply was absent. Now, only -ENODEV is checked and other errors will cause a failure to probe. So now, this will catch errors, like using the wrong data type for the devicetree property. Signed-off-by: David Lechner <dlechner@baylibre.com> Link: https://patch.msgid.link/20241120-iio-regulator-cleanup-round-6-v1-3-d5a5360f7ec3@baylibre.com Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>pull/1134/merge
parent
f596651dd6
commit
6c009e5592
|
|
@ -95,11 +95,6 @@ static int ad5686_spi_probe(struct spi_device *spi)
|
||||||
ad5686_spi_write, ad5686_spi_read);
|
ad5686_spi_write, ad5686_spi_read);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ad5686_spi_remove(struct spi_device *spi)
|
|
||||||
{
|
|
||||||
ad5686_remove(&spi->dev);
|
|
||||||
}
|
|
||||||
|
|
||||||
static const struct spi_device_id ad5686_spi_id[] = {
|
static const struct spi_device_id ad5686_spi_id[] = {
|
||||||
{"ad5310r", ID_AD5310R},
|
{"ad5310r", ID_AD5310R},
|
||||||
{"ad5672r", ID_AD5672R},
|
{"ad5672r", ID_AD5672R},
|
||||||
|
|
@ -126,7 +121,6 @@ static struct spi_driver ad5686_spi_driver = {
|
||||||
.name = "ad5686",
|
.name = "ad5686",
|
||||||
},
|
},
|
||||||
.probe = ad5686_spi_probe,
|
.probe = ad5686_spi_probe,
|
||||||
.remove = ad5686_spi_remove,
|
|
||||||
.id_table = ad5686_spi_id,
|
.id_table = ad5686_spi_id,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -455,39 +455,28 @@ int ad5686_probe(struct device *dev,
|
||||||
struct ad5686_state *st;
|
struct ad5686_state *st;
|
||||||
struct iio_dev *indio_dev;
|
struct iio_dev *indio_dev;
|
||||||
unsigned int val, ref_bit_msk;
|
unsigned int val, ref_bit_msk;
|
||||||
|
bool has_external_vref;
|
||||||
u8 cmd;
|
u8 cmd;
|
||||||
int ret, i, voltage_uv = 0;
|
int ret, i;
|
||||||
|
|
||||||
indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
|
indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
|
||||||
if (indio_dev == NULL)
|
if (indio_dev == NULL)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
st = iio_priv(indio_dev);
|
st = iio_priv(indio_dev);
|
||||||
dev_set_drvdata(dev, indio_dev);
|
|
||||||
|
|
||||||
st->dev = dev;
|
st->dev = dev;
|
||||||
st->write = write;
|
st->write = write;
|
||||||
st->read = read;
|
st->read = read;
|
||||||
|
|
||||||
st->reg = devm_regulator_get_optional(dev, "vcc");
|
|
||||||
if (!IS_ERR(st->reg)) {
|
|
||||||
ret = regulator_enable(st->reg);
|
|
||||||
if (ret)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
ret = regulator_get_voltage(st->reg);
|
|
||||||
if (ret < 0)
|
|
||||||
goto error_disable_reg;
|
|
||||||
|
|
||||||
voltage_uv = ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
st->chip_info = &ad5686_chip_info_tbl[chip_type];
|
st->chip_info = &ad5686_chip_info_tbl[chip_type];
|
||||||
|
|
||||||
if (voltage_uv)
|
ret = devm_regulator_get_enable_read_voltage(dev, "vcc");
|
||||||
st->vref_mv = voltage_uv / 1000;
|
if (ret < 0 && ret != -ENODEV)
|
||||||
else
|
return ret;
|
||||||
st->vref_mv = st->chip_info->int_vref_mv;
|
|
||||||
|
has_external_vref = ret != -ENODEV;
|
||||||
|
st->vref_mv = has_external_vref ? ret / 1000 : st->chip_info->int_vref_mv;
|
||||||
|
|
||||||
/* Set all the power down mode for all channels to 1K pulldown */
|
/* Set all the power down mode for all channels to 1K pulldown */
|
||||||
for (i = 0; i < st->chip_info->num_channels; i++)
|
for (i = 0; i < st->chip_info->num_channels; i++)
|
||||||
|
|
@ -505,12 +494,12 @@ int ad5686_probe(struct device *dev,
|
||||||
case AD5310_REGMAP:
|
case AD5310_REGMAP:
|
||||||
cmd = AD5686_CMD_CONTROL_REG;
|
cmd = AD5686_CMD_CONTROL_REG;
|
||||||
ref_bit_msk = AD5310_REF_BIT_MSK;
|
ref_bit_msk = AD5310_REF_BIT_MSK;
|
||||||
st->use_internal_vref = !voltage_uv;
|
st->use_internal_vref = !has_external_vref;
|
||||||
break;
|
break;
|
||||||
case AD5683_REGMAP:
|
case AD5683_REGMAP:
|
||||||
cmd = AD5686_CMD_CONTROL_REG;
|
cmd = AD5686_CMD_CONTROL_REG;
|
||||||
ref_bit_msk = AD5683_REF_BIT_MSK;
|
ref_bit_msk = AD5683_REF_BIT_MSK;
|
||||||
st->use_internal_vref = !voltage_uv;
|
st->use_internal_vref = !has_external_vref;
|
||||||
break;
|
break;
|
||||||
case AD5686_REGMAP:
|
case AD5686_REGMAP:
|
||||||
cmd = AD5686_CMD_INTERNAL_REFER_SETUP;
|
cmd = AD5686_CMD_INTERNAL_REFER_SETUP;
|
||||||
|
|
@ -519,43 +508,22 @@ int ad5686_probe(struct device *dev,
|
||||||
case AD5693_REGMAP:
|
case AD5693_REGMAP:
|
||||||
cmd = AD5686_CMD_CONTROL_REG;
|
cmd = AD5686_CMD_CONTROL_REG;
|
||||||
ref_bit_msk = AD5693_REF_BIT_MSK;
|
ref_bit_msk = AD5693_REF_BIT_MSK;
|
||||||
st->use_internal_vref = !voltage_uv;
|
st->use_internal_vref = !has_external_vref;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
ret = -EINVAL;
|
return -EINVAL;
|
||||||
goto error_disable_reg;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val = (voltage_uv | ref_bit_msk);
|
val = (has_external_vref | ref_bit_msk);
|
||||||
|
|
||||||
ret = st->write(st, cmd, 0, !!val);
|
ret = st->write(st, cmd, 0, !!val);
|
||||||
if (ret)
|
if (ret)
|
||||||
goto error_disable_reg;
|
return ret;
|
||||||
|
|
||||||
ret = iio_device_register(indio_dev);
|
return devm_iio_device_register(dev, indio_dev);
|
||||||
if (ret)
|
|
||||||
goto error_disable_reg;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
error_disable_reg:
|
|
||||||
if (!IS_ERR(st->reg))
|
|
||||||
regulator_disable(st->reg);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL_NS_GPL(ad5686_probe, "IIO_AD5686");
|
EXPORT_SYMBOL_NS_GPL(ad5686_probe, "IIO_AD5686");
|
||||||
|
|
||||||
void ad5686_remove(struct device *dev)
|
|
||||||
{
|
|
||||||
struct iio_dev *indio_dev = dev_get_drvdata(dev);
|
|
||||||
struct ad5686_state *st = iio_priv(indio_dev);
|
|
||||||
|
|
||||||
iio_device_unregister(indio_dev);
|
|
||||||
if (!IS_ERR(st->reg))
|
|
||||||
regulator_disable(st->reg);
|
|
||||||
}
|
|
||||||
EXPORT_SYMBOL_NS_GPL(ad5686_remove, "IIO_AD5686");
|
|
||||||
|
|
||||||
MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
|
MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
|
||||||
MODULE_DESCRIPTION("Analog Devices AD5686/85/84 DAC");
|
MODULE_DESCRIPTION("Analog Devices AD5686/85/84 DAC");
|
||||||
MODULE_LICENSE("GPL v2");
|
MODULE_LICENSE("GPL v2");
|
||||||
|
|
|
||||||
|
|
@ -118,7 +118,6 @@ struct ad5686_chip_info {
|
||||||
* struct ad5686_state - driver instance specific data
|
* struct ad5686_state - driver instance specific data
|
||||||
* @spi: spi_device
|
* @spi: spi_device
|
||||||
* @chip_info: chip model specific constants, available modes etc
|
* @chip_info: chip model specific constants, available modes etc
|
||||||
* @reg: supply regulator
|
|
||||||
* @vref_mv: actual reference voltage used
|
* @vref_mv: actual reference voltage used
|
||||||
* @pwr_down_mask: power down mask
|
* @pwr_down_mask: power down mask
|
||||||
* @pwr_down_mode: current power down mode
|
* @pwr_down_mode: current power down mode
|
||||||
|
|
@ -130,7 +129,6 @@ struct ad5686_chip_info {
|
||||||
struct ad5686_state {
|
struct ad5686_state {
|
||||||
struct device *dev;
|
struct device *dev;
|
||||||
const struct ad5686_chip_info *chip_info;
|
const struct ad5686_chip_info *chip_info;
|
||||||
struct regulator *reg;
|
|
||||||
unsigned short vref_mv;
|
unsigned short vref_mv;
|
||||||
unsigned int pwr_down_mask;
|
unsigned int pwr_down_mask;
|
||||||
unsigned int pwr_down_mode;
|
unsigned int pwr_down_mode;
|
||||||
|
|
@ -157,7 +155,5 @@ int ad5686_probe(struct device *dev,
|
||||||
const char *name, ad5686_write_func write,
|
const char *name, ad5686_write_func write,
|
||||||
ad5686_read_func read);
|
ad5686_read_func read);
|
||||||
|
|
||||||
void ad5686_remove(struct device *dev);
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* __DRIVERS_IIO_DAC_AD5686_H__ */
|
#endif /* __DRIVERS_IIO_DAC_AD5686_H__ */
|
||||||
|
|
|
||||||
|
|
@ -65,11 +65,6 @@ static int ad5686_i2c_probe(struct i2c_client *i2c)
|
||||||
ad5686_i2c_write, ad5686_i2c_read);
|
ad5686_i2c_write, ad5686_i2c_read);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ad5686_i2c_remove(struct i2c_client *i2c)
|
|
||||||
{
|
|
||||||
ad5686_remove(&i2c->dev);
|
|
||||||
}
|
|
||||||
|
|
||||||
static const struct i2c_device_id ad5686_i2c_id[] = {
|
static const struct i2c_device_id ad5686_i2c_id[] = {
|
||||||
{"ad5311r", ID_AD5311R},
|
{"ad5311r", ID_AD5311R},
|
||||||
{"ad5337r", ID_AD5337R},
|
{"ad5337r", ID_AD5337R},
|
||||||
|
|
@ -116,7 +111,6 @@ static struct i2c_driver ad5686_i2c_driver = {
|
||||||
.of_match_table = ad5686_of_match,
|
.of_match_table = ad5686_of_match,
|
||||||
},
|
},
|
||||||
.probe = ad5686_i2c_probe,
|
.probe = ad5686_i2c_probe,
|
||||||
.remove = ad5686_i2c_remove,
|
|
||||||
.id_table = ad5686_i2c_id,
|
.id_table = ad5686_i2c_id,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue