Commit b627e3b5 authored by Alexandru Ardelean's avatar Alexandru Ardelean Committed by Jonathan Cameron

staging: iio: ad9834: convert to device-managed functions in probe

This change converts the driver to use device-managed functions in the
probe function. For the clock and regulator disable, some
devm_add_action_or_reset() calls are required, and then
devm_iio_device_register() function can be used register the IIO device.

The final aim here would be for IIO to export only the device-managed
functions of it's API. That's a long way to go and this a small step in
that direction.
Signed-off-by: default avatarAlexandru Ardelean <aardelean@deviqon.com>
Link: https://lore.kernel.org/r/20210310095131.47476-1-aardelean@deviqon.comSigned-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 70da6415
...@@ -56,7 +56,6 @@ ...@@ -56,7 +56,6 @@
/** /**
* struct ad9834_state - driver instance specific data * struct ad9834_state - driver instance specific data
* @spi: spi_device * @spi: spi_device
* @reg: supply regulator
* @mclk: external master clock * @mclk: external master clock
* @control: cached control word * @control: cached control word
* @xfer: default spi transfer * @xfer: default spi transfer
...@@ -70,7 +69,6 @@ ...@@ -70,7 +69,6 @@
struct ad9834_state { struct ad9834_state {
struct spi_device *spi; struct spi_device *spi;
struct regulator *reg;
struct clk *mclk; struct clk *mclk;
unsigned short control; unsigned short control;
unsigned short devid; unsigned short devid;
...@@ -390,6 +388,20 @@ static const struct iio_info ad9833_info = { ...@@ -390,6 +388,20 @@ static const struct iio_info ad9833_info = {
.attrs = &ad9833_attribute_group, .attrs = &ad9833_attribute_group,
}; };
static void ad9834_disable_reg(void *data)
{
struct regulator *reg = data;
regulator_disable(reg);
}
static void ad9834_disable_clk(void *data)
{
struct clk *clk = data;
clk_disable_unprepare(clk);
}
static int ad9834_probe(struct spi_device *spi) static int ad9834_probe(struct spi_device *spi)
{ {
struct ad9834_state *st; struct ad9834_state *st;
...@@ -407,29 +419,35 @@ static int ad9834_probe(struct spi_device *spi) ...@@ -407,29 +419,35 @@ static int ad9834_probe(struct spi_device *spi)
return ret; return ret;
} }
ret = devm_add_action_or_reset(&spi->dev, ad9834_disable_reg, reg);
if (ret)
return ret;
indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
if (!indio_dev) { if (!indio_dev) {
ret = -ENOMEM; ret = -ENOMEM;
goto error_disable_reg; return ret;
} }
spi_set_drvdata(spi, indio_dev);
st = iio_priv(indio_dev); st = iio_priv(indio_dev);
mutex_init(&st->lock); mutex_init(&st->lock);
st->mclk = devm_clk_get(&spi->dev, NULL); st->mclk = devm_clk_get(&spi->dev, NULL);
if (IS_ERR(st->mclk)) { if (IS_ERR(st->mclk)) {
ret = PTR_ERR(st->mclk); ret = PTR_ERR(st->mclk);
goto error_disable_reg; return ret;
} }
ret = clk_prepare_enable(st->mclk); ret = clk_prepare_enable(st->mclk);
if (ret) { if (ret) {
dev_err(&spi->dev, "Failed to enable master clock\n"); dev_err(&spi->dev, "Failed to enable master clock\n");
goto error_disable_reg; return ret;
} }
ret = devm_add_action_or_reset(&spi->dev, ad9834_disable_clk, st->mclk);
if (ret)
return ret;
st->spi = spi; st->spi = spi;
st->devid = spi_get_device_id(spi)->driver_data; st->devid = spi_get_device_id(spi)->driver_data;
st->reg = reg;
indio_dev->name = spi_get_device_id(spi)->name; indio_dev->name = spi_get_device_id(spi)->name;
switch (st->devid) { switch (st->devid) {
case ID_AD9833: case ID_AD9833:
...@@ -470,48 +488,26 @@ static int ad9834_probe(struct spi_device *spi) ...@@ -470,48 +488,26 @@ static int ad9834_probe(struct spi_device *spi)
ret = spi_sync(st->spi, &st->msg); ret = spi_sync(st->spi, &st->msg);
if (ret) { if (ret) {
dev_err(&spi->dev, "device init failed\n"); dev_err(&spi->dev, "device init failed\n");
goto error_clock_unprepare; return ret;
} }
ret = ad9834_write_frequency(st, AD9834_REG_FREQ0, 1000000); ret = ad9834_write_frequency(st, AD9834_REG_FREQ0, 1000000);
if (ret) if (ret)
goto error_clock_unprepare; return ret;
ret = ad9834_write_frequency(st, AD9834_REG_FREQ1, 5000000); ret = ad9834_write_frequency(st, AD9834_REG_FREQ1, 5000000);
if (ret) if (ret)
goto error_clock_unprepare; return ret;
ret = ad9834_write_phase(st, AD9834_REG_PHASE0, 512); ret = ad9834_write_phase(st, AD9834_REG_PHASE0, 512);
if (ret) if (ret)
goto error_clock_unprepare; return ret;
ret = ad9834_write_phase(st, AD9834_REG_PHASE1, 1024); ret = ad9834_write_phase(st, AD9834_REG_PHASE1, 1024);
if (ret) if (ret)
goto error_clock_unprepare;
ret = iio_device_register(indio_dev);
if (ret)
goto error_clock_unprepare;
return 0;
error_clock_unprepare:
clk_disable_unprepare(st->mclk);
error_disable_reg:
regulator_disable(reg);
return ret; return ret;
}
static int ad9834_remove(struct spi_device *spi)
{
struct iio_dev *indio_dev = spi_get_drvdata(spi);
struct ad9834_state *st = iio_priv(indio_dev);
iio_device_unregister(indio_dev);
clk_disable_unprepare(st->mclk);
regulator_disable(st->reg);
return 0; return devm_iio_device_register(&spi->dev, indio_dev);
} }
static const struct spi_device_id ad9834_id[] = { static const struct spi_device_id ad9834_id[] = {
...@@ -539,7 +535,6 @@ static struct spi_driver ad9834_driver = { ...@@ -539,7 +535,6 @@ static struct spi_driver ad9834_driver = {
.of_match_table = ad9834_of_match .of_match_table = ad9834_of_match
}, },
.probe = ad9834_probe, .probe = ad9834_probe,
.remove = ad9834_remove,
.id_table = ad9834_id, .id_table = ad9834_id,
}; };
module_spi_driver(ad9834_driver); module_spi_driver(ad9834_driver);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment