iio: adc: Add Texas Instruments ADC081C021/027 support
PATCH 1/1 iio: adc: Add Texas Instruments ADC081C021/027 support
Add support for reading conversion results from the ADC and provide them through a single IIO channel. A proper scaling factor is also exported based on the reference voltage provided by a regulator.
--- drivers/iio/adc/Kconfig | 10 +++ drivers/iio/adc/Makefile | 1 + drivers/iio/adc/ti-adc081c.c | 162 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 drivers/iio/adc/ti-adc081c.c
drivers/iio/adc/Kconfig +10 -0
@@ -91,6 +91,16 @@ config MAX1363 max11646, max11647) Provides direct access via sysfs and buffered data via the iio dev interface. +config TI_ADC081C + tristate "Texas Instruments ADC081C021/027" + depends on I2C + help + If you say yes here you get support for Texas Instruments ADC081C021 + and ADC081C027 ADC chips. + + This driver can also be built as a module. If so, the module will be + called ti-adc081c. + config TI_AM335X_ADC tristate "TI's ADC driver" depends on MFD_TI_AM335X_TSCADC
drivers/iio/adc/Makefile +1 -0
@@ -10,4 +10,5 @@ obj-$(CONFIG_AD7887) += ad7887.o obj-$(CONFIG_AT91_ADC) += at91_adc.o obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o obj-$(CONFIG_MAX1363) += max1363.o +obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o
drivers/iio/adc/ti-adc081c.c new file +162 -0
162 lines not on this page · raw message
Signature · 1 line
1.8.0
Quoted 6 lines · Thierry Reding
Add support for reading conversion results from the ADC and provide them through a single IIO channel. A proper scaling factor is also exported based on the reference voltage provided by a regulator.
Signed-off-by Thierry Reding <[email protected]>---
[...]
Quoted 12 lines
drivers/iio/adc/ti-adc081c.c new file +12
-0+static int adc081c_read_raw (struct iio_dev *iio, +struct iio_chan_spec const *channel,int *value, +int *micro,long mask) +{ +struct adc081c *adc =iio_priv (iio); +int err, scale; + +switch (mask) { +case IIO_CHAN_INFO_RAW : + err =i2c_smbus_read_word_swapped (adc->i2c,REG_CONV_RES ); +if (err <0 ) +return err;
drivers/iio/adc/ti-adc081c.c line 45
Quoted 10 lines
drivers/iio/adc/ti-adc081c.c new file +10
-0+ + *value = (err >>4 ) &0xff ; +return IIO_VAL_INT ; + +case IIO_CHAN_INFO_SCALE : + err =regulator_get_voltage (adc->ref); +if (err <0 ) +return err; + + scale = err /255 ;
Shouldn't this be 256?
drivers/iio/adc/ti-adc081c.c line 48
Quoted 3 lines
drivers/iio/adc/ti-adc081c.c new file +3
-0+ + *value = scale /1000000 ; + *micro = scale %1000000 ;
scale for voltages is in microvolt, so I think it this is off by a factor of 1000.
For ADCs it often makes sense to use IIO_VAL_FRACTIONAL_LOG2 with the val being set to the reference voltage (in mV) and val2 being set to the number of bits.
E.g in your case
*val = err / 1000; *val2 = 8;
Quoted 40 lines
drivers/iio/adc/ti-adc081c.c new file +40
-0+ +return IIO_VAL_INT_PLUS_MICRO ; + +default : +break ; + } + +return -EINVAL ; +} + +static const struct iio_info adc081c_info = { + .read_raw = adc081c_read_raw, + .driver_module =THIS_MODULE , +}; + +static int adc081c_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +struct iio_dev *iio; +struct adc081c *adc; +int err; + +if (!i2c_check_functionality (client->adapter,I2C_FUNC_SMBUS_WORD_DATA )) +return -ENODEV ; + + iio =iio_device_alloc (sizeof (*adc)); +if (!iio) +return -ENOMEM ; + + adc =iio_priv (iio); + adc->i2c = client; + + adc->ref =regulator_get (&client->dev,"vref" ); +if (IS_ERR (adc->ref)) { + err =PTR_ERR (adc->ref); + goto iio_free; + } + + err =regulator_enable (adc->ref); +if (err <0 )
drivers/iio/adc/ti-adc081c.c line 98
Quoted 10 lines
drivers/iio/adc/ti-adc081c.c new file +10
-0+ goto regulator_put; + + iio->dev.parent = &client->dev; + iio->name =dev_name (&client->dev); + iio->modes =INDIO_DIRECT_MODE ; + iio->info = &adc081c_info; + + adc->channel.type =IIO_VOLTAGE ; + adc->channel.info_mask =IIO_CHAN_INFO_SCALE_SHARED_BIT | +IIO_CHAN_INFO_RAW_SEPARATE_BIT ;
nitpick: Since it is the same for each driver I'd make the channel static const.
Quoted 4 lines
drivers/iio/adc/ti-adc081c.c new file +4
-0+ + iio->channels = &adc->channel; + iio->num_channels =1 ; +
[...]
Quoted 24 lines · Lars-Peter Clausen
Quoted 6 lines · Thierry Reding
Add support for reading conversion results from the ADC and provide them through a single IIO channel. A proper scaling factor is also exported based on the reference voltage provided by a regulator.
Signed-off-by Thierry Reding <[email protected]>---
[...]
Quoted 16 lines
drivers/iio/adc/ti-adc081c.c new file +16
-0+static int adc081c_read_raw (struct iio_dev *iio, +struct iio_chan_spec const *channel,int *value, +int *micro,long mask) +{ +struct adc081c *adc =iio_priv (iio); +int err, scale; + +switch (mask) { +case IIO_CHAN_INFO_RAW : + err =i2c_smbus_read_word_swapped (adc->i2c,REG_CONV_RES ); +if (err <0 ) +return err; + + *value = (err >>4 ) &0xff ; +return IIO_VAL_INT ; +
Quoted 8 lines
drivers/iio/adc/ti-adc081c.c line 45
Quoted 6 lines
drivers/iio/adc/ti-adc081c.c new file +6
-0+case IIO_CHAN_INFO_SCALE : + err =regulator_get_voltage (adc->ref); +if (err <0 ) +return err; + + scale = err /255 ;Shouldn't this be 256?
Well, the maximum value that the conversion register can contain is 255, so if we divide by 256 we'll always be slightly off, right? Or maybe I misunderstand what the scale factor is supposed to do. From reading the documentation it seems like user-space is supposed to multiply the raw value by the scale to obtain the real voltage.
Quoted 6 lines
drivers/iio/adc/ti-adc081c.c line 48
Quoted 3 lines
drivers/iio/adc/ti-adc081c.c new file +3
-0+ + *value = scale /1000000 ; + *micro = scale %1000000 ;scale for voltages is in microvolt, so I think it this is off by a factor of 1000.
If scale is supposed to be in microvolts, then this is off by a factor of 1000000 since regulator_get_voltage() returns the voltage in microvolts as well.
Quoted 8 lines
For ADCs it often makes sense to use
IIO_VAL_FRACTIONAL_LOG2with the val being set to the reference voltage (in mV) and val2 being set to the number of bits.E.g in your case
*val = err / 1000; *val2 = 8;
I'm confused, IIO_VAL_FRACTIONAL_LOG2 is supposed to return mV, but IIO_VAL_INT_PLUS_MICRO will return uV?
And setting val2 to 8 will cause the scaling factor to be 256, and not 255, so the converted value will actually be off (as I mentioned above).
Quoted 2 lines
Quoted 2 lines
(quoted diff) +2
-0+ + return IIO_VAL_INT_PLUS_MICRO;
[...]
Quoted 5 lines
drivers/iio/adc/ti-adc081c.c line 98
Quoted 3 lines
drivers/iio/adc/ti-adc081c.c new file +3
-0+ adc->channel.type =IIO_VOLTAGE ; + adc->channel.info_mask =IIO_CHAN_INFO_SCALE_SHARED_BIT | +IIO_CHAN_INFO_RAW_SEPARATE_BIT ;nitpick: Since it is the same for each driver I'd make the channel static const.
Okay, that makes sense.
Thierry
Quoted 34 lines · Thierry Reding
Quoted 24 lines · Lars-Peter Clausen
Quoted 6 lines · Thierry Reding
Add support for reading conversion results from the ADC and provide them through a single IIO channel. A proper scaling factor is also exported based on the reference voltage provided by a regulator.
Signed-off-by Thierry Reding <[email protected]>---
[...]
Quoted 16 lines
drivers/iio/adc/ti-adc081c.c new file +16
-0+static int adc081c_read_raw (struct iio_dev *iio, +struct iio_chan_spec const *channel,int *value, +int *micro,long mask) +{ +struct adc081c *adc =iio_priv (iio); +int err, scale; + +switch (mask) { +case IIO_CHAN_INFO_RAW : + err =i2c_smbus_read_word_swapped (adc->i2c,REG_CONV_RES ); +if (err <0 ) +return err; + + *value = (err >>4 ) &0xff ; +return IIO_VAL_INT ; +Quoted 8 lines
drivers/iio/adc/ti-adc081c.c line 45
Quoted 6 lines
drivers/iio/adc/ti-adc081c.c new file +6
-0+case IIO_CHAN_INFO_SCALE : + err =regulator_get_voltage (adc->ref); +if (err <0 ) +return err; + + scale = err /255 ;Shouldn't this be 256?
Quoted 5 lines
Well, the maximum value that the conversion register can contain is 255, so if we divide by 256 we'll always be slightly off, right? Or maybe I misunderstand what the scale factor is supposed to do. From reading the documentation it seems like user-space is supposed to multiply the raw value by the scale to obtain the real voltage.
So usually for ADCs the scale is from 0V to VREF - 1 lsb or VREF - 0.5 LSB. I just had a look at the adc081c927 datasheet and it looks as if it is no exception.
Quoted 11 lines
Quoted 7 lines
drivers/iio/adc/ti-adc081c.c line 48
Quoted 4 lines
drivers/iio/adc/ti-adc081c.c new file +3
-0+ + *value = scale /1000000 ; + *micro = scale %1000000 ;scale for voltages is in microvolt, so I think it this is off by a factor of 1000.
If scale is supposed to be in microvolts, then this is off by a factor of 1000000 since
regulator_get_voltage()returns the voltage in microvolts as well.
sorry, I meant millivolts, too early in the morning ;)
Quoted 16 lines
Quoted 9 lines
For ADCs it often makes sense to use
IIO_VAL_FRACTIONAL_LOG2with the val being set to the reference voltage (in mV) and val2 being set to the number of bits.E.g in your case
*val = err / 1000; *val2 = 8;I'm confused,
IIO_VAL_FRACTIONAL_LOG2is supposed to return mV, butIIO_VAL_INT_PLUS_MICROwill return uV?And setting val2 to 8 will cause the scaling factor to be 256, and not 255, so the converted value will actually be off (as I mentioned above).
Quoted 53 lines · Lars-Peter Clausen
Quoted 34 lines · Thierry Reding
Quoted 24 lines · Lars-Peter Clausen
Quoted 6 lines · Thierry Reding
Add support for reading conversion results from the ADC and provide them through a single IIO channel. A proper scaling factor is also exported based on the reference voltage provided by a regulator.
Signed-off-by Thierry Reding <[email protected]>---
[...]
Quoted 16 lines
drivers/iio/adc/ti-adc081c.c new file +16
-0+static int adc081c_read_raw (struct iio_dev *iio, +struct iio_chan_spec const *channel,int *value, +int *micro,long mask) +{ +struct adc081c *adc =iio_priv (iio); +int err, scale; + +switch (mask) { +case IIO_CHAN_INFO_RAW : + err =i2c_smbus_read_word_swapped (adc->i2c,REG_CONV_RES ); +if (err <0 ) +return err; + + *value = (err >>4 ) &0xff ; +return IIO_VAL_INT ; +Quoted 8 lines
drivers/iio/adc/ti-adc081c.c line 45
Quoted 6 lines
drivers/iio/adc/ti-adc081c.c new file +6
-0+case IIO_CHAN_INFO_SCALE : + err =regulator_get_voltage (adc->ref); +if (err <0 ) +return err; + + scale = err /255 ;Shouldn't this be 256?
Quoted 5 lines
Well, the maximum value that the conversion register can contain is 255, so if we divide by 256 we'll always be slightly off, right? Or maybe I misunderstand what the scale factor is supposed to do. From reading the documentation it seems like user-space is supposed to multiply the raw value by the scale to obtain the real voltage.
So usually for ADCs the scale is from 0V to VREF - 1 lsb or VREF - 0.5 LSB. I just had a look at the
adc081c927datasheet and it looks as if it is no exception.Quoted 8 lines
Quoted 7 lines
drivers/iio/adc/ti-adc081c.c line 48
Quoted 4 lines
drivers/iio/adc/ti-adc081c.c new file +3
-0+ + *value = scale /1000000 ; + *micro = scale %1000000 ;scale for voltages is in microvolt, so I think it this is off by a factor of 1000.
Quoted 5 lines
Quoted 3 lines
If scale is supposed to be in microvolts, then this is off by a factor of 1000000 since
regulator_get_voltage()returns the voltage in microvolts as well.sorry, I meant millivolts, too early in the morning ;)
Okay, so both of your arguments indicate that I should indeed go for what you proposed below.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
For ADCs it often makes sense to use
IIO_VAL_FRACTIONAL_LOG2with the val being set to the reference voltage (in mV) and val2 being set to the number of bits.E.g in your case
*val = err / 1000; *val2 = 8;
I'll update the patch, thanks for reviewing.
Thierry