{
int ret;
struct ad7887_state *st = iio_priv(indio_dev);
- unsigned int scale_uv;
switch (m) {
case IIO_CHAN_INFO_RAW:
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;
}
{
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 =
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;
}
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;
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);
}