Commit cc9c58ef authored by Slawomir Stepien's avatar Slawomir Stepien Committed by Jonathan Cameron

staging: iio: adc: ad7280a: use devm_* APIs

devm_* APIs are device managed and make code simpler.
Signed-off-by: default avatarSlawomir Stepien <sst@poczta.fm>
Signed-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 9829f9e5
...@@ -330,6 +330,14 @@ static int ad7280_read_all_channels(struct ad7280_state *st, unsigned int cnt, ...@@ -330,6 +330,14 @@ static int ad7280_read_all_channels(struct ad7280_state *st, unsigned int cnt,
return sum; return sum;
} }
static void ad7280_sw_power_down(void *data)
{
struct ad7280_state *st = data;
ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CONTROL_HB, 1,
AD7280A_CTRL_HB_PWRDN_SW | st->ctrl_hb);
}
static int ad7280_chain_setup(struct ad7280_state *st) static int ad7280_chain_setup(struct ad7280_state *st)
{ {
unsigned int val, n; unsigned int val, n;
...@@ -492,8 +500,8 @@ static int ad7280_channel_init(struct ad7280_state *st) ...@@ -492,8 +500,8 @@ static int ad7280_channel_init(struct ad7280_state *st)
{ {
int dev, ch, cnt; int dev, ch, cnt;
st->channels = kcalloc((st->slave_num + 1) * 12 + 2, st->channels = devm_kcalloc(&st->spi->dev, (st->slave_num + 1) * 12 + 2,
sizeof(*st->channels), GFP_KERNEL); sizeof(*st->channels), GFP_KERNEL);
if (!st->channels) if (!st->channels)
return -ENOMEM; return -ENOMEM;
...@@ -552,16 +560,18 @@ static int ad7280_channel_init(struct ad7280_state *st) ...@@ -552,16 +560,18 @@ static int ad7280_channel_init(struct ad7280_state *st)
static int ad7280_attr_init(struct ad7280_state *st) static int ad7280_attr_init(struct ad7280_state *st)
{ {
int dev, ch, cnt; int dev, ch, cnt;
unsigned int index;
st->iio_attr = kcalloc(2, sizeof(*st->iio_attr) * st->iio_attr = devm_kcalloc(&st->spi->dev, 2, sizeof(*st->iio_attr) *
(st->slave_num + 1) * AD7280A_CELLS_PER_DEV, (st->slave_num + 1) * AD7280A_CELLS_PER_DEV,
GFP_KERNEL); GFP_KERNEL);
if (!st->iio_attr) if (!st->iio_attr)
return -ENOMEM; return -ENOMEM;
for (dev = 0, cnt = 0; dev <= st->slave_num; dev++) for (dev = 0, cnt = 0; dev <= st->slave_num; dev++)
for (ch = AD7280A_CELL_VOLTAGE_1; ch <= AD7280A_CELL_VOLTAGE_6; for (ch = AD7280A_CELL_VOLTAGE_1; ch <= AD7280A_CELL_VOLTAGE_6;
ch++, cnt++) { ch++, cnt++) {
index = dev * AD7280A_CELLS_PER_DEV + ch;
st->iio_attr[cnt].address = st->iio_attr[cnt].address =
ad7280a_devaddr(dev) << 8 | ch; ad7280a_devaddr(dev) << 8 | ch;
st->iio_attr[cnt].dev_attr.attr.mode = st->iio_attr[cnt].dev_attr.attr.mode =
...@@ -571,10 +581,9 @@ static int ad7280_attr_init(struct ad7280_state *st) ...@@ -571,10 +581,9 @@ static int ad7280_attr_init(struct ad7280_state *st)
st->iio_attr[cnt].dev_attr.store = st->iio_attr[cnt].dev_attr.store =
ad7280_store_balance_sw; ad7280_store_balance_sw;
st->iio_attr[cnt].dev_attr.attr.name = st->iio_attr[cnt].dev_attr.attr.name =
kasprintf(GFP_KERNEL, devm_kasprintf(&st->spi->dev, GFP_KERNEL,
"in%d-in%d_balance_switch_en", "in%d-in%d_balance_switch_en",
dev * AD7280A_CELLS_PER_DEV + ch, index, index + 1);
dev * AD7280A_CELLS_PER_DEV + ch + 1);
ad7280_attributes[cnt] = ad7280_attributes[cnt] =
&st->iio_attr[cnt].dev_attr.attr; &st->iio_attr[cnt].dev_attr.attr;
cnt++; cnt++;
...@@ -588,10 +597,9 @@ static int ad7280_attr_init(struct ad7280_state *st) ...@@ -588,10 +597,9 @@ static int ad7280_attr_init(struct ad7280_state *st)
st->iio_attr[cnt].dev_attr.store = st->iio_attr[cnt].dev_attr.store =
ad7280_store_balance_timer; ad7280_store_balance_timer;
st->iio_attr[cnt].dev_attr.attr.name = st->iio_attr[cnt].dev_attr.attr.name =
kasprintf(GFP_KERNEL, devm_kasprintf(&st->spi->dev, GFP_KERNEL,
"in%d-in%d_balance_timer", "in%d-in%d_balance_timer",
dev * AD7280A_CELLS_PER_DEV + ch, index, index + 1);
dev * AD7280A_CELLS_PER_DEV + ch + 1);
ad7280_attributes[cnt] = ad7280_attributes[cnt] =
&st->iio_attr[cnt].dev_attr.attr; &st->iio_attr[cnt].dev_attr.attr;
} }
...@@ -872,6 +880,10 @@ static int ad7280_probe(struct spi_device *spi) ...@@ -872,6 +880,10 @@ static int ad7280_probe(struct spi_device *spi)
if (ret < 0) if (ret < 0)
return ret; return ret;
ret = devm_add_action_or_reset(&spi->dev, ad7280_sw_power_down, st);
if (ret)
return ret;
st->slave_num = ret; st->slave_num = ret;
st->scan_cnt = (st->slave_num + 1) * AD7280A_NUM_CH; st->scan_cnt = (st->slave_num + 1) * AD7280A_NUM_CH;
st->cell_threshhigh = 0xFF; st->cell_threshhigh = 0xFF;
...@@ -901,7 +913,7 @@ static int ad7280_probe(struct spi_device *spi) ...@@ -901,7 +913,7 @@ static int ad7280_probe(struct spi_device *spi)
ret = ad7280_channel_init(st); ret = ad7280_channel_init(st);
if (ret < 0) if (ret < 0)
goto error_power_down; return ret;
indio_dev->num_channels = ret; indio_dev->num_channels = ret;
indio_dev->channels = st->channels; indio_dev->channels = st->channels;
...@@ -909,68 +921,37 @@ static int ad7280_probe(struct spi_device *spi) ...@@ -909,68 +921,37 @@ static int ad7280_probe(struct spi_device *spi)
ret = ad7280_attr_init(st); ret = ad7280_attr_init(st);
if (ret < 0) if (ret < 0)
goto error_free_channels; return ret;
ret = iio_device_register(indio_dev); ret = devm_iio_device_register(&spi->dev, indio_dev);
if (ret) if (ret)
goto error_free_attr; return ret;
if (spi->irq > 0) { if (spi->irq > 0) {
ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, ret = ad7280_write(st, AD7280A_DEVADDR_MASTER,
AD7280A_ALERT, 1, AD7280A_ALERT, 1,
AD7280A_ALERT_RELAY_SIG_CHAIN_DOWN); AD7280A_ALERT_RELAY_SIG_CHAIN_DOWN);
if (ret) if (ret)
goto error_unregister; return ret;
ret = ad7280_write(st, ad7280a_devaddr(st->slave_num), ret = ad7280_write(st, ad7280a_devaddr(st->slave_num),
AD7280A_ALERT, 0, AD7280A_ALERT, 0,
AD7280A_ALERT_GEN_STATIC_HIGH | AD7280A_ALERT_GEN_STATIC_HIGH |
(pdata->chain_last_alert_ignore & 0xF)); (pdata->chain_last_alert_ignore & 0xF));
if (ret) if (ret)
goto error_unregister; return ret;
ret = request_threaded_irq(spi->irq, ret = devm_request_threaded_irq(&spi->dev, spi->irq,
NULL, NULL,
ad7280_event_handler, ad7280_event_handler,
IRQF_TRIGGER_FALLING | IRQF_TRIGGER_FALLING |
IRQF_ONESHOT, IRQF_ONESHOT,
indio_dev->name, indio_dev->name,
indio_dev); indio_dev);
if (ret) if (ret)
goto error_unregister; return ret;
} }
return 0;
error_unregister:
iio_device_unregister(indio_dev);
error_free_attr:
kfree(st->iio_attr);
error_free_channels:
kfree(st->channels);
error_power_down:
ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CONTROL_HB, 1,
AD7280A_CTRL_HB_PWRDN_SW | st->ctrl_hb);
return ret;
}
static int ad7280_remove(struct spi_device *spi)
{
struct iio_dev *indio_dev = spi_get_drvdata(spi);
struct ad7280_state *st = iio_priv(indio_dev);
if (spi->irq > 0)
free_irq(spi->irq, indio_dev);
iio_device_unregister(indio_dev);
ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CONTROL_HB, 1,
AD7280A_CTRL_HB_PWRDN_SW | st->ctrl_hb);
kfree(st->channels);
kfree(st->iio_attr);
return 0; return 0;
} }
...@@ -985,7 +966,6 @@ static struct spi_driver ad7280_driver = { ...@@ -985,7 +966,6 @@ static struct spi_driver ad7280_driver = {
.name = "ad7280", .name = "ad7280",
}, },
.probe = ad7280_probe, .probe = ad7280_probe,
.remove = ad7280_remove,
.id_table = ad7280_id, .id_table = ad7280_id,
}; };
module_spi_driver(ad7280_driver); module_spi_driver(ad7280_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