From: Lars-Peter Clausen <lars@metafoo.de>
To: Jonathan Cameron <jic23@cam.ac.uk>
Cc: linux-iio@vger.kernel.org, drivers@analog.com,
Lars-Peter Clausen <lars@metafoo.de>
Subject: [PATCH 2/7] staging:iio:ad7887: Rework regulator handling
Date: Mon, 5 Nov 2012 10:56:41 +0100 [thread overview]
Message-ID: <1352109406-7206-2-git-send-email-lars@metafoo.de> (raw)
In-Reply-To: <1352109406-7206-1-git-send-email-lars@metafoo.de>
Rework the regulator handling of the ad7887 driver to match more closely what we
do for other drivers. Only request the regulator if a external reference is
used, but treat it as an error if requesting the regulator fails. Also remove
the possibility to specify the reference voltage via platform data and always
use the regulator for this.
Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
---
drivers/staging/iio/adc/ad7887.h | 3 --
drivers/staging/iio/adc/ad7887_core.c | 52 ++++++++++++++++-------------------
2 files changed, 24 insertions(+), 31 deletions(-)
diff --git a/drivers/staging/iio/adc/ad7887.h b/drivers/staging/iio/adc/ad7887.h
index 71e5092..25d5b40 100644
--- a/drivers/staging/iio/adc/ad7887.h
+++ b/drivers/staging/iio/adc/ad7887.h
@@ -30,8 +30,6 @@ enum ad7887_channels {
*/
struct ad7887_platform_data {
- /* External Vref voltage applied */
- u16 vref_mv;
/*
* AD7887:
* In single channel mode en_dual = flase, AIN1/Vref pins assumes its
@@ -63,7 +61,6 @@ struct ad7887_state {
struct spi_device *spi;
const struct ad7887_chip_info *chip_info;
struct regulator *reg;
- u16 int_vref_mv;
struct spi_transfer xfer[4];
struct spi_message msg[3];
struct spi_message *ring_msg;
diff --git a/drivers/staging/iio/adc/ad7887_core.c b/drivers/staging/iio/adc/ad7887_core.c
index 5517905..cf33ba6 100644
--- a/drivers/staging/iio/adc/ad7887_core.c
+++ b/drivers/staging/iio/adc/ad7887_core.c
@@ -39,7 +39,6 @@ static int ad7887_read_raw(struct iio_dev *indio_dev,
{
int ret;
struct ad7887_state *st = iio_priv(indio_dev);
- unsigned int scale_uv;
switch (m) {
case IIO_CHAN_INFO_RAW:
@@ -56,11 +55,18 @@ static int ad7887_read_raw(struct iio_dev *indio_dev,
RES_MASK(st->chip_info->channel[0].scan_type.realbits);
return IIO_VAL_INT;
case IIO_CHAN_INFO_SCALE:
- scale_uv = (st->int_vref_mv * 1000)
- >> st->chip_info->channel[0].scan_type.realbits;
- *val = scale_uv/1000;
- *val2 = (scale_uv%1000)*1000;
- return IIO_VAL_INT_PLUS_MICRO;
+ if (st->reg) {
+ *val = regulator_get_voltage(st->reg);
+ if (*val < 0)
+ return *val;
+ *val /= 1000;
+ } else {
+ *val = st->chip_info->int_vref_mv;
+ }
+
+ *val2 = st->chip_info->channel[0].scan_type.realbits;
+
+ return IIO_VAL_FRACTIONAL_LOG2;
}
return -EINVAL;
}
@@ -105,21 +111,24 @@ static int __devinit ad7887_probe(struct spi_device *spi)
{
struct ad7887_platform_data *pdata = spi->dev.platform_data;
struct ad7887_state *st;
- int ret, voltage_uv = 0;
struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st));
+ int ret;
if (indio_dev == NULL)
return -ENOMEM;
st = iio_priv(indio_dev);
- st->reg = regulator_get(&spi->dev, "vcc");
- if (!IS_ERR(st->reg)) {
+ if (!pdata || !pdata->use_onchip_ref) {
+ st->reg = regulator_get(&spi->dev, "vref");
+ if (IS_ERR(st->reg)) {
+ ret = PTR_ERR(st->reg);
+ goto error_free;
+ }
+
ret = regulator_enable(st->reg);
if (ret)
goto error_put_reg;
-
- voltage_uv = regulator_get_voltage(st->reg);
}
st->chip_info =
@@ -176,23 +185,9 @@ static int __devinit ad7887_probe(struct spi_device *spi)
spi_message_init(&st->msg[AD7887_CH1]);
spi_message_add_tail(&st->xfer[3], &st->msg[AD7887_CH1]);
- if (pdata && pdata->vref_mv)
- st->int_vref_mv = pdata->vref_mv;
- else if (voltage_uv)
- st->int_vref_mv = voltage_uv / 1000;
- else
- dev_warn(&spi->dev, "reference voltage unspecified\n");
-
indio_dev->channels = st->chip_info->channel;
indio_dev->num_channels = 3;
} else {
- if (pdata && pdata->vref_mv)
- st->int_vref_mv = pdata->vref_mv;
- else if (pdata && pdata->use_onchip_ref)
- st->int_vref_mv = st->chip_info->int_vref_mv;
- else
- dev_warn(&spi->dev, "reference voltage unspecified\n");
-
indio_dev->channels = &st->chip_info->channel[1];
indio_dev->num_channels = 2;
}
@@ -209,11 +204,12 @@ static int __devinit ad7887_probe(struct spi_device *spi)
error_unregister_ring:
ad7887_ring_cleanup(indio_dev);
error_disable_reg:
- if (!IS_ERR(st->reg))
+ if (st->reg)
regulator_disable(st->reg);
error_put_reg:
- if (!IS_ERR(st->reg))
+ if (st->reg)
regulator_put(st->reg);
+error_free:
iio_device_free(indio_dev);
return ret;
@@ -226,7 +222,7 @@ static int __devexit ad7887_remove(struct spi_device *spi)
iio_device_unregister(indio_dev);
ad7887_ring_cleanup(indio_dev);
- if (!IS_ERR(st->reg)) {
+ if (st->reg) {
regulator_disable(st->reg);
regulator_put(st->reg);
}
--
1.8.0
next prev parent reply other threads:[~2012-11-05 9:57 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-11-05 9:56 [PATCH 1/7] staging:iio:ad7887: Preallocate sample buffer Lars-Peter Clausen
2012-11-05 9:56 ` Lars-Peter Clausen [this message]
2012-11-05 9:56 ` [PATCH 3/7] staging:iio:ad7887: Squash everything into one file Lars-Peter Clausen
2012-11-05 9:56 ` [PATCH 4/7] staging:iio:ad7887: Use proper kernel doc Lars-Peter Clausen
2012-11-05 9:56 ` [PATCH 5/7] staging:iio:ad7887: Allow to use internal ref in two channel mode Lars-Peter Clausen
2012-11-05 9:56 ` [PATCH 6/7] staging:iio:ad7887: Use passed in chan spec in ad7887_read_raw Lars-Peter Clausen
2012-11-05 9:56 ` [PATCH 7/7] staging:iio: Move the ad7887 driver out of staging Lars-Peter Clausen
2012-11-10 10:07 ` [PATCH 1/7] staging:iio:ad7887: Preallocate sample buffer Jonathan Cameron
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1352109406-7206-2-git-send-email-lars@metafoo.de \
--to=lars@metafoo.de \
--cc=drivers@analog.com \
--cc=jic23@cam.ac.uk \
--cc=linux-iio@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).