iio: adc: ad7606: Add software configuration

Because this driver will support multiple configurations for software,
the software configuration was made generic.

Signed-off-by: Beniamin Bia <beniamin.bia@analog.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
This commit is contained in:
Beniamin Bia 2019-05-27 15:56:48 +03:00 committed by Jonathan Cameron
parent 88dd031350
commit 3c23e9e808
2 changed files with 39 additions and 3 deletions

View File

@ -140,7 +140,7 @@ static int ad7606_read_raw(struct iio_dev *indio_dev,
int *val2, int *val2,
long m) long m)
{ {
int ret; int ret, ch = 0;
struct ad7606_state *st = iio_priv(indio_dev); struct ad7606_state *st = iio_priv(indio_dev);
switch (m) { switch (m) {
@ -157,8 +157,10 @@ static int ad7606_read_raw(struct iio_dev *indio_dev,
*val = (short)ret; *val = (short)ret;
return IIO_VAL_INT; return IIO_VAL_INT;
case IIO_CHAN_INFO_SCALE: case IIO_CHAN_INFO_SCALE:
if (st->sw_mode_en)
ch = chan->address;
*val = 0; *val = 0;
*val2 = st->scale_avail[st->range[0]]; *val2 = st->scale_avail[st->range[ch]];
return IIO_VAL_INT_PLUS_MICRO; return IIO_VAL_INT_PLUS_MICRO;
case IIO_CHAN_INFO_OVERSAMPLING_RATIO: case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
*val = st->oversampling; *val = st->oversampling;
@ -233,7 +235,9 @@ static int ad7606_write_raw(struct iio_dev *indio_dev,
case IIO_CHAN_INFO_SCALE: case IIO_CHAN_INFO_SCALE:
mutex_lock(&st->lock); mutex_lock(&st->lock);
i = find_closest(val2, st->scale_avail, st->num_scales); i = find_closest(val2, st->scale_avail, st->num_scales);
ret = st->write_scale(indio_dev, chan->address, i); if (st->sw_mode_en)
ch = chan->address;
ret = st->write_scale(indio_dev, ch, i);
if (ret < 0) { if (ret < 0) {
mutex_unlock(&st->lock); mutex_unlock(&st->lock);
return ret; return ret;
@ -616,6 +620,36 @@ int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
st->write_scale = ad7606_write_scale_hw; st->write_scale = ad7606_write_scale_hw;
st->write_os = ad7606_write_os_hw; st->write_os = ad7606_write_os_hw;
if (st->chip_info->sw_mode_config)
st->sw_mode_en = device_property_present(st->dev,
"adi,sw-mode");
if (st->sw_mode_en) {
/* After reset, in software mode, ±10 V is set by default */
memset32(st->range, 2, ARRAY_SIZE(st->range));
indio_dev->info = &ad7606_info_os_and_range;
/*
* In software mode, the range gpio has no longer its function.
* Instead, the scale can be configured individually for each
* channel from the range registers.
*/
if (st->chip_info->write_scale_sw)
st->write_scale = st->chip_info->write_scale_sw;
/*
* In software mode, the oversampling is no longer configured
* with GPIO pins. Instead, the oversampling can be configured
* in configuratiion register.
*/
if (st->chip_info->write_os_sw)
st->write_os = st->chip_info->write_os_sw;
ret = st->chip_info->sw_mode_config(indio_dev);
if (ret < 0)
return ret;
}
st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
indio_dev->name, indio_dev->id); indio_dev->name, indio_dev->id);
if (!st->trig) if (!st->trig)

View File

@ -43,6 +43,7 @@ struct ad7606_chip_info {
* @range voltage range selection, selects which scale to apply * @range voltage range selection, selects which scale to apply
* @oversampling oversampling selection * @oversampling oversampling selection
* @base_address address from where to read data in parallel operation * @base_address address from where to read data in parallel operation
* @sw_mode_en software mode enabled
* @scale_avail pointer to the array which stores the available scales * @scale_avail pointer to the array which stores the available scales
* @num_scales number of elements stored in the scale_avail array * @num_scales number of elements stored in the scale_avail array
* @oversampling_avail pointer to the array which stores the available * @oversampling_avail pointer to the array which stores the available
@ -71,6 +72,7 @@ struct ad7606_state {
unsigned int range[16]; unsigned int range[16];
unsigned int oversampling; unsigned int oversampling;
void __iomem *base_address; void __iomem *base_address;
bool sw_mode_en;
const unsigned int *scale_avail; const unsigned int *scale_avail;
unsigned int num_scales; unsigned int num_scales;
const unsigned int *oversampling_avail; const unsigned int *oversampling_avail;