iio: processed channel handling fixes + Intel Dollar Cove TI PMIC ADC driver
v5 cover iio: processed channel handling fixes + Intel Dollar Cove TI PMIC ADC driver
Hi All,
Here is v5 of my patch-set to add an IIO driver for the Intel Dollar Cove TI PMIC ADC. This has turned more into a series with fixes / changes to iio_convert_raw_to_processed() and iio_read_channel_processed_scale(), with the new driver tagged on as the last patch :)
Changes in v5:
- Do not put case foo: val = x; break; statements on a single line
- Use IIO_UNIT_TEST module-namespace for iio_multiply_value()
- iio_read_channel_processed_scale(): Use pval + pval2 local variables for better readability
- Consistenly use s / ms / ųs for seconds in comments
- Various other small (comment) style fixups
- Add Andy's Reviewed-by to all patches
Changes in v4:
- 2 new bug-fixes for iio_convert_raw_to_processed()
- Factor the bugfixed code multiply a s64 and an iio (type, val, val2) triplet out of iio_convert_raw_to_processed() into a new iio_multiply_value() helper
- Add a KUnit test for iio_multiply_value()
- Redo the "Improve iio_read_channel_processed_scale() precision" patch using the iio_multiply_value() helper
Changes in v3:
- "iio: Improve iio_read_channel_processed_scale() precision"
- Use div_s64() instead of div_u64() to fix -1.0 - 0.0 range
- Directly return IIO_VAL_INT from valid cases and drop the final return ret after the switch-case
- "iio: adc: Add Intel Dollar Cove TI PMIC ADC driver"
- Use new more compact DC_TI_ADC_DATA_REG_CH(x) macro
- Use regmap_set_bits() regmap_clear_bits() where applicable
- Use regmap_bulk_read() + be16_to_cpu() to read ADC value
- Use sign_extend32() for vbat_zse and vbat_ge reading in probe()
Changes in v2:
- Add new "iio: Improve iio_read_channel_processed_scale() precision" patch to the series
- Add IIO_CHAN_INFO_SCALE and provide ADC scale info for Vbat
- Add IIO_CHAN_INFO_PROCESSED which applies calibration and scaling for the VBat channel
- Address some other small review remarks
Regards,
Hans
Hans de Goede (6):
iio: consumers: Fix handling of negative channel scale in
iio_convert_raw_to_processed()
iio: consumers: Fix offset handling in iio_convert_raw_to_processed()
iio: consumers: Add an iio_multiply_value() helper function
iio: Improve iio_read_channel_processed_scale() precision
iio: test: Add KUnit tests for iio_multiply_value()
iio: adc: Add Intel Dollar Cove TI PMIC ADC driver
drivers/iio/adc/Kconfig | 11 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/intel_dc_ti_adc.c | 328 +++++++++++++++++++++++++++ drivers/iio/inkern.c | 81 ++++--- drivers/iio/test/Kconfig | 12 + drivers/iio/test/Makefile | 1 + drivers/iio/test/iio-test-multiply.c | 212 +++++++++++++++++ include/linux/iio/consumer.h | 18 ++ 8 files changed, 629 insertions(+), 35 deletions(-) create mode 100644 drivers/iio/adc/intel_dc_ti_adc.c create mode 100644 drivers/iio/test/iio-test-multiply.c
Signature · 1 line
2.51.0
PATCH v5 1/6 iio: consumers: Fix handling of negative channel scale in iio_convert_raw_to_processed()
There is an issue with the handling of negative channel scales in iio_convert_raw_to_processed_unlocked() when the channel-scale is of the IIO_VAL_INT_PLUS_[MICRO|NANO] type:
Things work for channel-scale values > -1.0 and < 0.0 because of the use of signed values in:
*processed += div_s64(raw64 * (s64)scale_val2 * scale, 1000000LL);
Things will break however for scale values < -1.0. Lets for example say that raw = 2, (caller-provided)scale = 10 and (channel)scale_val = -1.5.
The result should then be 2 * 10 * -1.5 = -30.
channel-scale = -1.5 means scale_val = -1 and scale_val2 = 500000, now lets see what gets stored in processed:
1. *processed = raw64 * scale_val * scale;
2. *processed += raw64 * scale_val2 * scale / 1000000LL;
1. Sets processed to 2 * -1 * 10 = -20
2. Adds 2 * 500000 * 10 / 1000000 = 10 to processed
And the end result is processed = -20 + 10 = -10, which is not correct.
Fix this by always using the abs value of both scale_val and scale_val2 and if either is negative multiply the end-result by -1.
Note there seems to be an unwritten rule about negative IIO_VAL_INT_PLUS_[MICRO|NANO] values that:
i. values > -1.0 and < 0.0 are written as val=0 val2=-xxx
ii. values <= -1.0 are written as val=-xxx val2=xxx
But iio_format_value() will also correctly display a third option:
iii. values <= -1.0 written as val=-xxx val2=-xxx
Since iio_format_value() uses abs(val) when val2 < 0.
This fix also makes iio_convert_raw_to_processed() properly handle channel-scales using this third option.
---
Changes in v5:
- Do not put case foo: val = x; break; statements on a single line
Changes in v4:
- New patch in v4 of this patch-set
--- drivers/iio/inkern.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)
drivers/iio/inkern.c +14 -14
@@ -11,6 +11,7 @@#include <linux/mutex.h> #include <linux/property.h> #include <linux/slab.h> +#include <linux/units.h> #include <linux/iio/iio.h> #include <linux/iio/iio-opaque.h>
@@ -604,7 +605,7 @@ static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, {int scale_type, scale_val, scale_val2;int offset_type, offset_val, offset_val2; -s64 raw64 = raw; +s64 denominator, raw64 = raw; offset_type =iio_channel_read (chan, &offset_val, &offset_val2,IIO_CHAN_INFO_OFFSET );
@@ -648,20 +649,19 @@ static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, *processed = raw64 * scale_val * scale;break ;case IIO_VAL_INT_PLUS_MICRO : -if (scale_val2 <0 ) - *processed = -raw64 * scale_val * scale; -else - *processed = raw64 * scale_val * scale; - *processed +=div_s64 (raw64 * (s64 )scale_val2 * scale, -1000000LL ); -break ;case IIO_VAL_INT_PLUS_NANO : -if (scale_val2 <0 ) - *processed = -raw64 * scale_val * scale; -else - *processed = raw64 * scale_val * scale; - *processed +=div_s64 (raw64 * (s64 )scale_val2 * scale, -1000000000LL ); +switch (scale_type) { +case IIO_VAL_INT_PLUS_MICRO : + denominator =MICRO ; +break ; +case IIO_VAL_INT_PLUS_NANO : + denominator =NANO ; +break ; + } + *processed = raw64 * scale *abs (scale_val); + *processed +=div_s64 (raw64 * scale *abs (scale_val2), denominator); +if (scale_val <0 || scale_val2 <0 ) + *processed *=-1 ;break ;case IIO_VAL_FRACTIONAL : *processed = div_s64(raw64 * (s64)scale_val * scale,
Signature · 1 line
2.51.0
PATCH v5 2/6 iio: consumers: Fix offset handling in iio_convert_raw_to_processed()
In reply to Hans de GoedeFix iio_convert_raw_to_processed() offset handling for channels without a scale attribute.
The offset has been applied to the raw64 value not to the original raw value. Use the raw64 value so that the offset is taken into account.
---
Changes in v4:
- New patch in v4 of this patch-set
--- drivers/iio/inkern.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/inkern.c +1 -1
@@ -640,7 +640,7 @@ static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,* If no channel scaling is available apply consumer scale to * raw value and return. */ - *processed = raw * scale; + *processed = raw64 * scale;return 0 ; }
Signature · 1 line
2.51.0
PATCH v5 3/6 iio: consumers: Add an iio_multiply_value() helper function
In reply to Hans de GoedeThe channel-scale handling in iio_convert_raw_to_processed() in essence does the following:
processed = raw * caller-provided-scale * channel-scale
Which can also be written as:
multiplier = raw * caller-provided-scale
iio-value = channel-scale
processed = multiplier * iio-value
Where iio-value is a set of IIO_VAL_* type + val + val2 integers, being able to handle multiplication of iio-values like this is something which is useful to have in general and, as previous bugfixes to iio_convert_raw_to_processed() have shown, also tricky to implement.
Split the iio-value multiplication code from iio_convert_raw_to_processed() out into a new iio_multiply_value() helper. This serves multiple purposes:
1. Having this split out allows writing a KUnit test for this.
2. Having this split out allows re-use to get better precision when scaling values in iio_read_channel_processed_scale().
---
Changes in v5:
- Use IIO_UNIT_TEST module-namespace for iio_multiply_value()
Changes in v4:
- New patch in v4 of this patch-set
--- drivers/iio/inkern.c | 72 +++++++++++++++++++++--------------- include/linux/iio/consumer.h | 18 +++++++++ 2 files changed, 60 insertions(+), 30 deletions(-)
drivers/iio/inkern.c +42 -30
@@ -599,13 +599,50 @@ int iio_read_channel_average_raw(struct iio_channel *chan, int *val) }EXPORT_SYMBOL_GPL (iio_read_channel_average_raw); +int iio_multiply_value (int *result,s64 multiplier, +unsigned int type,int val,int val2) +{ +s64 denominator; + +switch (type) { +case IIO_VAL_INT : + *result = multiplier * val; +return IIO_VAL_INT ; +case IIO_VAL_INT_PLUS_MICRO : +case IIO_VAL_INT_PLUS_NANO : +switch (type) { +case IIO_VAL_INT_PLUS_MICRO : + denominator =MICRO ; +break ; +case IIO_VAL_INT_PLUS_NANO : + denominator =NANO ; +break ; + } + *result = multiplier *abs (val); + *result +=div_s64 (multiplier *abs (val2), denominator); +if (val <0 || val2 <0 ) + *result *=-1 ; +return IIO_VAL_INT ; +case IIO_VAL_FRACTIONAL : + *result =div_s64 (multiplier * val, val2); +return IIO_VAL_INT ; +case IIO_VAL_FRACTIONAL_LOG2 : + *result = (multiplier * val) >> val2; +return IIO_VAL_INT ; +default : +return -EINVAL ; + } +} +EXPORT_SYMBOL_NS_GPL (iio_multiply_value,"IIO_UNIT_TEST" ); +static int iio_convert_raw_to_processed_unlocked (struct iio_channel *chan,int raw,int *processed,unsigned int scale) {int scale_type, scale_val, scale_val2;int offset_type, offset_val, offset_val2; -s64 denominator, raw64 = raw; +s64 raw64 = raw; +int ret; offset_type =iio_channel_read (chan, &offset_val, &offset_val2,IIO_CHAN_INFO_OFFSET );
@@ -644,35 +681,10 @@ static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,return 0 ; } -switch (scale_type) { -case IIO_VAL_INT : - *processed = raw64 * scale_val * scale; -break ; -case IIO_VAL_INT_PLUS_MICRO : -case IIO_VAL_INT_PLUS_NANO : -switch (scale_type) { -case IIO_VAL_INT_PLUS_MICRO : - denominator =MICRO ; -break ; -case IIO_VAL_INT_PLUS_NANO : - denominator =NANO ; -break ; - } - *processed = raw64 * scale *abs (scale_val); - *processed +=div_s64 (raw64 * scale *abs (scale_val2), denominator); -if (scale_val <0 || scale_val2 <0 ) - *processed *=-1 ; -break ; -case IIO_VAL_FRACTIONAL : - *processed =div_s64 (raw64 * (s64 )scale_val * scale, - scale_val2); -break ; -case IIO_VAL_FRACTIONAL_LOG2 : - *processed = (raw64 * (s64 )scale_val * scale) >> scale_val2; -break ; -default : -return -EINVAL ; - } + ret =iio_multiply_value (processed, raw64 * scale, + scale_type, scale_val, scale_val2); +if (ret <0 ) +return ret;return 0 ; }
include/linux/iio/consumer.h +18 -0
@@ -381,6 +381,24 @@ int iio_read_channel_offset(struct iio_channel *chan, int *val,int iio_read_channel_scale (struct iio_channel *chan,int *val,int *val2); +/** +* iio_multiply_value() - Multiply an IIO value +* @result: Destination pointer for the multiplication result +* @multiplier: Multiplier. +* @type: One of the IIO_VAL_* constants. This decides how the @val and +* @val2 parameters are interpreted. +* @val: Value being multiplied. +* @val2: Value being multiplied. @val2 use depends on type. +* +* Multiply an IIO value with a s64 multiplier storing the result as +* IIO_VAL_INT. This is typically used for scaling. +* +* Returns: +* IIO_VAL_INT on success or a negative error-number on failure. +*/ +int iio_multiply_value (int *result,s64 multiplier, +unsigned int type,int val,int val2); +/** * iio_convert_raw_to_processed() - Converts a raw value to a processed value * @chan: The channel being queried
Signature · 1 line
2.51.0
PATCH v5 4/6 iio: Improve iio_read_channel_processed_scale() precision
In reply to Hans de GoedeBefore this change iio_read_channel_processed_scale() always assumes that channels which advertise IIO_CHAN_INFO_PROCESSED capability return IIO_VAL_INT on success.
Ignoring any fractional values from drivers which return IIO_VAL_INT_PLUS_MICRO / IIO_VAL_INT_PLUS_NANO. These fractional values might become non fractional after scaling so these should be taken into account for better precision.
Use the new iio_multiply_value() helper to do proper scaling taking the fractionional values into account.
---
Changes in v5:
- Introduce pval + pval2 local variables for better readability
Changes in v4:
- Use the new iio_multiply_value() helper
--- drivers/iio/inkern.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/inkern.c +3 -4
@@ -726,20 +726,19 @@ int iio_read_channel_processed_scale(struct iio_channel *chan, int *val, unsigned int scale) {struct iio_dev_opaque *iio_dev_opaque =to_iio_dev_opaque (chan->indio_dev); -int ret; +int ret, pval, pval2;guard (mutex)(&iio_dev_opaque->info_exist_lock);if (!chan->indio_dev->info)return -ENODEV ;if (iio_channel_has_info (chan->channel,IIO_CHAN_INFO_PROCESSED )) { - ret =iio_channel_read (chan, val,NULL , + ret =iio_channel_read (chan, &pval, &pval2,IIO_CHAN_INFO_PROCESSED );if (ret <0 )return ret; - *val *= scale; -return ret; +return iio_multiply_value (val, scale, ret, pval, pval2); }else { ret =iio_channel_read (chan, val,NULL ,IIO_CHAN_INFO_RAW );if (ret <0 )
Signature · 1 line
2.51.0
PATCH v5 5/6 iio: test: Add KUnit tests for iio_multiply_value()
In reply to Hans de GoedeAdd KUnit tests for iio_multiply_value().
---
Changes in v5:
- s/kunit/KUnit/
- Add some missing includes
Changes in v4:
- New patch in v4 of this patch-set
--- drivers/iio/test/Kconfig | 12 ++ drivers/iio/test/Makefile | 1 + drivers/iio/test/iio-test-multiply.c | 212 +++++++++++++++++++++++++++ 3 files changed, 225 insertions(+) create mode 100644 drivers/iio/test/iio-test-multiply.c
drivers/iio/test/Kconfig +12 -0
@@ -41,3 +41,15 @@ config IIO_FORMAT_KUNIT_TEST to the KUnit documentation in Documentation/dev-tools/kunit/. If unsure, say N. + +config IIO_MULTIPLY_KUNIT_TEST + tristate "Test IIO multiply functions" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + build unit tests for the IIO multiply functions. + + For more information on KUnit and unit tests in general, please refer + to the KUnit documentation in Documentation/dev-tools/kunit/. + + If unsure, say N.
drivers/iio/test/Makefile +1 -0
@@ -7,4 +7,5 @@ obj-$(CONFIG_IIO_RESCALE_KUNIT_TEST) += iio-test-rescale.o obj-$(CONFIG_IIO_FORMAT_KUNIT_TEST) += iio-test-format.o obj-$(CONFIG_IIO_GTS_KUNIT_TEST) += iio-test-gts.o +obj-$(CONFIG_IIO_MULTIPLY_KUNIT_TEST) += iio-test-multiply.o CFLAGS_iio-test-format.o += $(DISABLE_STRUCTLEAK_PLUGIN)
drivers/iio/test/iio-test-multiply.c new file +212 -0
212 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v5 6/6 iio: adc: Add Intel Dollar Cove TI PMIC ADC driver
In reply to Hans de GoedeIntel has 2 completely different "Dollar Cove" PMICs for its Bay Trail / Cherry Trail SoCs. One is made by X-Powers and is called the AXP288. The AXP288's GPADC is already supported by the X-Powers AXP288 ADC driver.
The other "Dollar Cove" PMIC is made by TI and does not have any clear TI denomination, its MFD driver calls it the "Intel Dollar Cove TI PMIC".
Add a driver for the Intel Dollar Cove TI PMIC's general purpose ADC, binding to the "chtdc_ti_adc" MFD cell of the MFD driver.
The "cht" in the cell name comes from Cherry Trail, but it turns out that just like the AXP288 the Intel Dollar Cove TI PMIC is also used with both Intel Bay Trail and Intel Cherry Trail SoCs, so this new ADC driver does not include the cht part in its name.
This is loosely based on kernel/drivers/iio/adc/iio_dc_ti_gpadc.c from the Acer A1-840 Android kernel source-code archive named:
"App. Guide_Acer_20151221_A_A.zip"
which is distributed by Acer from the Acer A1-840 support page:
https://www.acer.com/us-en/support/product-support/A1-840/downloads
---
Changes in v5:
- Consistenly use s / ms / ųs for seconds in comments
- Various other small (comment) style fixups
Changes in v4:
- No changes
Changes in v3:
- Use new more compact DC_TI_ADC_DATA_REG_CH(x) macro
- Use regmap_set_bits(), regmap_clear_bits() where applicable
- Use regmap_bulk_read() + be16_to_cpu() to read ADC value
- Use sign_extend32() for vbat_zse and vbat_ge reading in probe()
Changes in v2:
- Add IIO_CHAN_INFO_SCALE and provide ADC scale info for Vbat
- Add IIO_CHAN_INFO_PROCESSED which applies calibration and scaling for the VBat channel
- Address some other small review remarks
--- drivers/iio/adc/Kconfig | 11 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/intel_dc_ti_adc.c | 328 ++++++++++++++++++++++++++++++ 3 files changed, 340 insertions(+) create mode 100644 drivers/iio/adc/intel_dc_ti_adc.c
drivers/iio/adc/Kconfig +11 -0
@@ -766,6 +766,17 @@ config INGENIC_ADC This driver can also be built as a module. If so, the module will be called ingenic_adc. +config INTEL_DC_TI_ADC + tristate "Intel Bay Trail / Cherry Trail Dollar Cove TI ADC driver" + depends on INTEL_SOC_PMIC_CHTDC_TI + help + Say yes here to have support for the Dollar Cove TI PMIC ADC device. + Depending on platform configuration, this general purpose ADC can be + used for sensors such as battery voltage and thermal resistors. + + To compile this driver as a module, choose M here: the module will be + called intel_dc_ti_adc. + config INTEL_MRFLD_ADC tristate "Intel Merrifield Basin Cove ADC driver" depends on INTEL_SOC_PMIC_MRFLD
drivers/iio/adc/Makefile +1 -0
@@ -70,6 +70,7 @@ obj-$(CONFIG_IMX8QXP_ADC) += imx8qxp-adc.o obj-$(CONFIG_IMX93_ADC) += imx93_adc.o obj-$(CONFIG_INA2XX_ADC) += ina2xx-adc.o obj-$(CONFIG_INGENIC_ADC) += ingenic-adc.o +obj-$(CONFIG_INTEL_DC_TI_ADC) += intel_dc_ti_adc.o obj-$(CONFIG_INTEL_MRFLD_ADC) += intel_mrfld_adc.o obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o obj-$(CONFIG_LPC18XX_ADC) += lpc18xx_adc.o
drivers/iio/adc/intel_dc_ti_adc.c new file +328 -0
328 lines not on this page · raw message
Signature · 1 line
2.51.0
Jonathan Cameron
maintainerQuoted 6 lines · On Sun, 31 Aug 2025 12:48:19 +0200 Hans de Goede <[email protected]> wrote:
Hi All,
Here is v5 of my patch-set to add an IIO driver for the Intel Dollar Cove TI PMIC ADC. This has turned more into a series with fixes / changes to
iio_convert_raw_to_processed()andiio_read_channel_processed_scale(), with the new driver tagged on as the last patch :)
So, rather than risk the actual driver getting delayed I've picked the whole lot up on my togreg branch. We can backport the fixes next cycle. For now pushed out as testing to give 0-day a first look.
Jonathan
Quoted 64 lines
Changes in v5:
- Do not put case foo: val = x; break; statements on a single line
- UseIIO_UNIT_TESTmodule-namespace foriio_multiply_value()
-iio_read_channel_processed_scale(): Use pval + pval2 local variables for better readability
- Consistenly use s / ms / ųs for seconds in comments
- Various other small (comment) style fixups
- Add Andy's Reviewed-by to all patchesChanges in v4:
- 2 new bug-fixes foriio_convert_raw_to_processed()
- Factor the bugfixed code multiply a s64 and an iio (type, val, val2) triplet out ofiio_convert_raw_to_processed()into a newiio_multiply_value()helper
- Add a KUnit test foriio_multiply_value()
- Redo the "Improveiio_read_channel_processed_scale()precision" patch using theiio_multiply_value()helperChanges in v3:
- "iio: Improveiio_read_channel_processed_scale()precision"
- Usediv_s64()instead ofdiv_u64()to fix -1.0 - 0.0 range
- Directly returnIIO_VAL_INTfrom valid cases and drop the final return ret after the switch-case
- "iio: adc: Add Intel Dollar Cove TI PMIC ADC driver"
- Use new more compactDC_TI_ADC_DATA_REG_CH(x)macro
- Useregmap_set_bits()regmap_clear_bits()where applicable
- Useregmap_bulk_read()+be16_to_cpu()to read ADC value
- Usesign_extend32()forvbat_zseandvbat_gereading inprobe()Changes in v2:
- Add new "iio: Improveiio_read_channel_processed_scale()precision" patch to the series
- AddIIO_CHAN_INFO_SCALEand provide ADC scale info for Vbat
- AddIIO_CHAN_INFO_PROCESSEDwhich applies calibration and scaling for the VBat channel
- Address some other small review remarksRegards,
Hans
Hans de Goede (6):
iio: consumers: Fix handling of negative channel scale in
iio_convert_raw_to_processed()
iio: consumers: Fix offset handling iniio_convert_raw_to_processed()
iio: consumers: Add aniio_multiply_value()helper function
iio: Improveiio_read_channel_processed_scale()precision
iio: test: Add KUnit tests foriio_multiply_value()
iio: adc: Add Intel Dollar Cove TI PMIC ADC driverdrivers/iio/adc/Kconfig | 11 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/intel_dc_ti_adc.c | 328 +++++++++++++++++++++++++++ drivers/iio/inkern.c | 81 ++++--- drivers/iio/test/Kconfig | 12 + drivers/iio/test/Makefile | 1 + drivers/iio/test/iio-test-multiply.c | 212 +++++++++++++++++ include/linux/iio/consumer.h | 18 ++ 8 files changed, 629 insertions(+), 35 deletions(-) create mode 100644 drivers/iio/adc/intel_dc_ti_adc.c create mode 100644 drivers/iio/test/iio-test-multiply.c