math.h: Introduce data types for fractional numbers
Subsystems (3)
PATCH 1/5 math.h: Introduce data types for fractional numbers
Introduce a macro to produce data types like
struct TYPE_fract {
__TYPE numerator;
__TYPE denominator;
};to be used in the code wherever it's needed.
In the following changes convert some users to it.
---
May be pulled via IIO tree.
include/linux/math.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)
include/linux/math.h +12 -0
@@ -2,6 +2,7 @@#ifndef _LINUX_MATH_H#define _LINUX_MATH_H +#include <linux/types.h> #include <asm/div64.h> #include <uapi/linux/kernel.h>
@@ -106,6 +107,17 @@ } \ ) +#define __STRUCT_FRACT (type) \ +struct type##_fract { \ + __##type numerator; \ + __##type denominator; \ +}; +__STRUCT_FRACT(s16) +__STRUCT_FRACT(u16) +__STRUCT_FRACT(s32) +__STRUCT_FRACT(u32) +#undef __STRUCT_FRACT +/* * Multiplies an integer by a fraction, while avoiding unnecessary * overflow or loss of precision.
Signature · 1 line
2.34.1
PATCH 3/5 iio: adc: twl4030-madc: Re-use generic struct s16_fract
Instead of custom data type re-use generic struct s16_fract. No changes intended.
--- drivers/iio/adc/twl4030-madc.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)
drivers/iio/adc/twl4030-madc.c +1 -8
@@ -231,13 +231,7 @@ static const struct iio_chan_spec twl4030_madc_iio_channels[] = {static struct twl4030_madc_data *twl4030_madc; -struct twl4030_prescale_divider_ratios { -s16 numerator; -s16 denominator; -}; - -static const struct twl4030_prescale_divider_ratios -twl4030_divider_ratios[16 ] = { +static const struct s16_fract twl4030_divider_ratios[16 ] = { {1 ,1 },/* CHANNEL 0 No Prescaler */ {1 ,1 },/* CHANNEL 1 No Prescaler */ {6 ,10 },/* CHANNEL 2 */
@@ -256,7 +250,6 @@ twl4030_divider_ratios[16] = { {5, 11}, /* CHANNEL 15 */ }; -/* Conversion table from -3 to 55 degrees Celcius */ static int twl4030_therm_tbl[] = { 30800, 29500, 28300, 27100,
Signature · 1 line
2.34.1
PATCH 2/5 iio: adc: rn5t618: Re-use generic struct u16_fract
In reply to Andy ShevchenkoInstead of custom data type re-use generic struct u16_fract. No changes intended.
--- drivers/iio/adc/rn5t618-adc.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/iio/adc/rn5t618-adc.c +1 -6
@@ -42,11 +42,6 @@ struct rn5t618_adc_data {int irq; }; -struct rn5t618_channel_ratios { -u16 numerator; -u16 denominator; -}; -enum rn5t618_channels {LIMMON =0 ,VBAT ,
@@ -58,7 +53,7 @@ enum rn5t618_channels {AIN0 }; -static const struct rn5t618_channel_ratios rn5t618_ratios[8 ] = { +static const struct u16_fract rn5t618_ratios[8 ] = { [LIMMON ] = {50 ,32 },/* measured across 20mOhm, amplified by 32 */ [VBAT ] = {2 ,1 }, [VADP ] = {3 ,1 },
Signature · 1 line
2.34.1
PATCH 4/5 iio: adc: qcom-vadc-common: Re-use generic struct u32_fract
In reply to Andy ShevchenkoInstead of custom data type re-use generic struct u32_fract. No changes intended.
--- drivers/iio/adc/qcom-pm8xxx-xoadc.c | 2 +- drivers/iio/adc/qcom-spmi-vadc.c | 2 +- drivers/iio/adc/qcom-vadc-common.c | 48 ++++++++++++------------ include/linux/iio/adc/qcom-vadc-common.h | 15 ++------ 4 files changed, 29 insertions(+), 38 deletions(-)
drivers/iio/adc/qcom-pm8xxx-xoadc.c +1 -1
@@ -175,7 +175,7 @@ struct xoadc_channel {const char *datasheet_name; u8 pre_scale_mux:2; u8 amux_channel:4; -const struct vadc_prescale_ratio prescale; +const struct u32_fract prescale;enum iio_chan_type type;enum vadc_scale_fn_type scale_fn_type; u8 amux_ip_rsv:3;
drivers/iio/adc/qcom-spmi-vadc.c +1 -1
@@ -122,7 +122,7 @@ struct vadc_priv {struct mutex lock; }; -static const struct vadc_prescale_ratio vadc_prescale_ratios[] = { +static const struct u32_fract vadc_prescale_ratios[] = { {.num =1 , .den =1 }, {.num =1 , .den =3 }, {.num =1 , .den =4 },
drivers/iio/adc/qcom-vadc-common.c +24 -24
174 lines not on this page · raw message
include/linux/iio/adc/qcom-vadc-common.h +3 -12
@@ -6,6 +6,7 @@#ifndef QCOM_VADC_COMMON_H #define QCOM_VADC_COMMON_H +#include <linux/math.h> #include <linux/types.h> #define VADC_CONV_TIME_MIN_US 2000
@@ -79,16 +80,6 @@ struct vadc_linear_graph {s32 gnd; }; -/** -* struct vadc_prescale_ratio - Represent scaling ratio for ADC input. -* @num: the inverse numerator of the gain applied to the input channel. -* @den: the inverse denominator of the gain applied to the input channel. -*/ -struct vadc_prescale_ratio { -u32 num; -u32 den; -}; -/** * enum vadc_scale_fn_type - Scaling function to convert ADC code to * physical scaled units for the channel.
@@ -144,12 +135,12 @@ struct adc5_data {int qcom_vadc_scale (enum vadc_scale_fn_type scaletype,const struct vadc_linear_graph *calib_graph, -const struct vadc_prescale_ratio *prescale, +const struct u32_fract *prescale,bool absolute,u16 adc_code,int *result_mdec);struct qcom_adc5_scale_type { -int (*scale_fn)(const struct vadc_prescale_ratio *prescale, +int (*scale_fn)(const struct u32_fract *prescale,const struct adc5_data *data,u16 adc_code,int *result); };
Signature · 1 line
2.34.1
PATCH 5/5 iio: afe: iio-rescale: Re-use generic struct s32_fract
In reply to Andy ShevchenkoInstead of custom data type re-use generic struct s32_fract. No changes intended.
The new member is put to be the first one to avoid additional pointer arithmetic. Besides that one may switch to use fract member to perform container_of(), which will be no-op in this case, to get struct rescale.
---
I found this better in order how code is structurally (re)organized. I may rebase this on top of ongoing AFE series.
Also reveals possibility to switch to rational best approximation. But this is another story...
drivers/iio/afe/iio-rescale.c | 74 +++++++++++++++++------------------ 1 file changed, 37 insertions(+), 37 deletions(-)
drivers/iio/afe/iio-rescale.c +37 -37
@@ -11,6 +11,7 @@#include <linux/gcd.h> #include <linux/iio/consumer.h> #include <linux/iio/iio.h> +#include <linux/math.h> #include <linux/module.h> #include <linux/of.h> #include <linux/of_device.h>
@@ -21,17 +22,16 @@ struct rescale;struct rescale_cfg {enum iio_chan_type type; -int (*props)(struct device *dev,struct rescale *rescale); +int (*props)(struct device *dev,struct s32_fract *fract); };struct rescale { +struct s32_fract fract;const struct rescale_cfg *cfg;struct iio_channel *source;struct iio_chan_spec chan;struct iio_chan_spec_ext_info *ext_info;bool chan_processed; -s32 numerator; -s32 denominator; }; static int rescale_read_raw(struct iio_dev *indio_dev,
@@ -39,6 +39,7 @@ static int rescale_read_raw(struct iio_dev *indio_dev, int *val, int *val2, long mask) { struct rescale *rescale = iio_priv(indio_dev); +struct s32_fract *fract = &rescale->fract;unsigned long long tmp;int ret;
@@ -67,19 +68,19 @@ static int rescale_read_raw(struct iio_dev *indio_dev, }switch (ret) {case IIO_VAL_FRACTIONAL : - *val *= rescale->numerator; - *val2 *= rescale->denominator; + *val *= fract->numerator; + *val2 *= fract->denominator;return ret;case IIO_VAL_INT : - *val *= rescale->numerator; -if (rescale->denominator ==1 ) + *val *= fract->numerator; +if (fract->denominator ==1 )return ret; - *val2 = rescale->denominator; + *val2 = fract->denominator;return IIO_VAL_FRACTIONAL ;case IIO_VAL_FRACTIONAL_LOG2 : tmp = *val *1000000000LL ; -do_div (tmp, rescale->denominator); - tmp *= rescale->numerator; +do_div (tmp, fract->denominator); + tmp *= fract->numerator;do_div (tmp,1000000000LL ); *val = tmp;return ret;
@@ -175,7 +176,7 @@ static int rescale_configure_channel(struct device *dev, }static int rescale_current_sense_amplifier_props (struct device *dev, -struct rescale *rescale) +struct s32_fract *fract) {u32 sense;u32 gain_mult =1 ;
@@ -199,22 +200,22 @@ static int rescale_current_sense_amplifier_props(struct device *dev,* numerator/denominator from overflowing. */ factor =gcd (sense,1000000 ); - rescale->numerator =1000000 / factor; - rescale->denominator = sense / factor; + fract->numerator =1000000 / factor; + fract->denominator = sense / factor; - factor =gcd (rescale->numerator, gain_mult); - rescale->numerator /= factor; - rescale->denominator *= gain_mult / factor; + factor =gcd (fract->numerator, gain_mult); + fract->numerator /= factor; + fract->denominator *= gain_mult / factor; - factor =gcd (rescale->denominator, gain_div); - rescale->numerator *= gain_div / factor; - rescale->denominator /= factor; + factor =gcd (fract->denominator, gain_div); + fract->numerator *= gain_div / factor; + fract->denominator /= factor;return 0 ; }static int rescale_current_sense_shunt_props (struct device *dev, -struct rescale *rescale) +struct s32_fract *fract) {u32 shunt;u32 factor;
@@ -228,35 +229,33 @@ static int rescale_current_sense_shunt_props(struct device *dev, } factor =gcd (shunt,1000000 ); - rescale->numerator =1000000 / factor; - rescale->denominator = shunt / factor; + fract->numerator =1000000 / factor; + fract->denominator = shunt / factor;return 0 ; }static int rescale_voltage_divider_props (struct device *dev, -struct rescale *rescale) +struct s32_fract *fract) {int ret;u32 factor; - ret =device_property_read_u32 (dev,"output-ohms" , - &rescale->denominator); + ret =device_property_read_u32 (dev,"output-ohms" , &fract->denominator);if (ret) {dev_err (dev,"failed to read output-ohms: %d\n" , ret);return ret; } - ret =device_property_read_u32 (dev,"full-ohms" , - &rescale->numerator); + ret =device_property_read_u32 (dev,"full-ohms" , &fract->numerator);if (ret) {dev_err (dev,"failed to read full-ohms: %d\n" , ret);return ret; } - factor =gcd (rescale->numerator, rescale->denominator); - rescale->numerator /= factor; - rescale->denominator /= factor; + factor =gcd (fract->numerator, fract->denominator); + fract->numerator /= factor; + fract->denominator /= factor;return 0 ; }
@@ -299,6 +298,7 @@ static int rescale_probe(struct platform_device *pdev)struct iio_dev *indio_dev;struct iio_channel *source;struct rescale *rescale; +struct s32_fract *fract;int sizeof_ext_info;int sizeof_priv;int i;
@@ -322,24 +322,24 @@ static int rescale_probe(struct platform_device *pdev)return -ENOMEM ; rescale =iio_priv (indio_dev); - + rescale->source = source; rescale->cfg =of_device_get_match_data (dev); - rescale->numerator =1 ; - rescale->denominator =1 ; - ret = rescale->cfg->props (dev, rescale); + fract = &rescale->fract; + fract->numerator =1 ; + fract->denominator =1 ; + + ret = rescale->cfg->props (dev, fract);if (ret)return ret; -if (!rescale->numerator || !rescale->denominator) { +if (!fract->numerator || !fract->denominator) {dev_err (dev,"invalid scaling factor.\n" );return -EINVAL ; }platform_set_drvdata (pdev, indio_dev); - rescale->source = source; - indio_dev->name =dev_name (dev); indio_dev->info = &rescale_info; indio_dev->modes =INDIO_DIRECT_MODE ;
Signature · 1 line
2.34.1
Re: [PATCH v1 4/5] iio: adc: qcom-vadc-common: Re-use generic struct u32_fract
In reply to Andy ShevchenkoQuoted 2 lines · Andy Shevchenko
Instead of custom data type re-use generic struct
u32_fract. No changes intended.
...
include/linux/iio/adc/qcom-vadc-common.h struct vadc_linear_graph { · line 90
Quoted 9 lines
include/linux/iio/adc/qcom-vadc-common.h +0
-9-/** -* struct vadc_prescale_ratio - Represent scaling ratio for ADC input. -* @num: the inverse numerator of the gain applied to the input channel. -* @den: the inverse denominator of the gain applied to the input channel. -*/ -struct vadc_prescale_ratio { -u32 num; -u32 den; -};
Definitely forgot to compile this one.
Signature · 2 lines
With Best Regards, Andy Shevchenko
kernel test robot
build botRe: [PATCH v1 4/5] iio: adc: qcom-vadc-common: Re-use generic struct u32_fract
In reply to Andy ShevchenkoI love your patch! Yet something to improve:
Hi Andy,
I love your patch! Yet something to improve:
[auto build test ERROR on jic23-iio/togreg]
[also build test ERROR on linux/master linus/master v5.16 next-20220110] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Andy-Shevchenko/math-h-Introduce-data-types-for-fractional-numbers/20220111-021818 base: https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg config: arm64-buildonly-randconfig-r002-20220111 (https://download.01.org/0day-ci/archive/20220111/202201110438.mfVkleOt-lkp(a)intel.com/config) compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 5c46c1c23abb4a01f7f40bfcb8851cb893739e14)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/0day-ci/linux/commit/2d0db3a1312ad1511ae417c82b4ba9616b11a6b5
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Andy-Shevchenko/math-h-Introduce-data-types-for-fractional-numbers/20220111-021818 git checkout 2d0db3a1312ad1511ae417c82b4ba9616b11a6b5
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm64 SHELL=/bin/bash drivers/iio/adc/
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
>> drivers/iio/adc/qcom-vadc-common.c:293:4: error: field designator 'num' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 1},
^
>> drivers/iio/adc/qcom-vadc-common.c:293:15: error: field designator 'den' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 1},
^
drivers/iio/adc/qcom-vadc-common.c:294:4: error: field designator 'num' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 3},
^
drivers/iio/adc/qcom-vadc-common.c:294:15: error: field designator 'den' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 3},
^
drivers/iio/adc/qcom-vadc-common.c:295:4: error: field designator 'num' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 4},
^
drivers/iio/adc/qcom-vadc-common.c:295:15: error: field designator 'den' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 4},
^
drivers/iio/adc/qcom-vadc-common.c:296:4: error: field designator 'num' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 6},
^
drivers/iio/adc/qcom-vadc-common.c:296:15: error: field designator 'den' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 6},
^
drivers/iio/adc/qcom-vadc-common.c:297:4: error: field designator 'num' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 20},
^
drivers/iio/adc/qcom-vadc-common.c:297:15: error: field designator 'den' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 20},
^
drivers/iio/adc/qcom-vadc-common.c:298:4: error: field designator 'num' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 8},
^
drivers/iio/adc/qcom-vadc-common.c:298:15: error: field designator 'den' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 8},
^
drivers/iio/adc/qcom-vadc-common.c:299:4: error: field designator 'num' does not refer to any field in type 'const struct u32_fract'
{.num = 10, .den = 81},
^
drivers/iio/adc/qcom-vadc-common.c:299:15: error: field designator 'den' does not refer to any field in type 'const struct u32_fract'
{.num = 10, .den = 81},
^
drivers/iio/adc/qcom-vadc-common.c:300:4: error: field designator 'num' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 10},
^
drivers/iio/adc/qcom-vadc-common.c:300:15: error: field designator 'den' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 10},
^
drivers/iio/adc/qcom-vadc-common.c:301:4: error: field designator 'num' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 16}
^
drivers/iio/adc/qcom-vadc-common.c:301:15: error: field designator 'den' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 16}
^
>> drivers/iio/adc/qcom-vadc-common.c:417:32: error: no member named 'den' in 'struct u32_fract'
voltage = voltage * prescale->den;
~~~~~~~~ ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
--
>> drivers/iio/adc/qcom-spmi-vadc.c:126:4: error: field designator 'num' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 1},
^
>> drivers/iio/adc/qcom-spmi-vadc.c:126:15: error: field designator 'den' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 1},
^
drivers/iio/adc/qcom-spmi-vadc.c:127:4: error: field designator 'num' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 3},
^
drivers/iio/adc/qcom-spmi-vadc.c:127:15: error: field designator 'den' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 3},
^
drivers/iio/adc/qcom-spmi-vadc.c:128:4: error: field designator 'num' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 4},
^
drivers/iio/adc/qcom-spmi-vadc.c:128:15: error: field designator 'den' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 4},
^
drivers/iio/adc/qcom-spmi-vadc.c:129:4: error: field designator 'num' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 6},
^
drivers/iio/adc/qcom-spmi-vadc.c:129:15: error: field designator 'den' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 6},
^
drivers/iio/adc/qcom-spmi-vadc.c:130:4: error: field designator 'num' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 20},
^
drivers/iio/adc/qcom-spmi-vadc.c:130:15: error: field designator 'den' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 20},
^
drivers/iio/adc/qcom-spmi-vadc.c:131:4: error: field designator 'num' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 8},
^
drivers/iio/adc/qcom-spmi-vadc.c:131:15: error: field designator 'den' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 8},
^
drivers/iio/adc/qcom-spmi-vadc.c:132:4: error: field designator 'num' does not refer to any field in type 'const struct u32_fract'
{.num = 10, .den = 81},
^
drivers/iio/adc/qcom-spmi-vadc.c:132:15: error: field designator 'den' does not refer to any field in type 'const struct u32_fract'
{.num = 10, .den = 81},
^
drivers/iio/adc/qcom-spmi-vadc.c:133:4: error: field designator 'num' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 10}
^
drivers/iio/adc/qcom-spmi-vadc.c:133:15: error: field designator 'den' does not refer to any field in type 'const struct u32_fract'
{.num = 1, .den = 10}
^
>> drivers/iio/adc/qcom-spmi-vadc.c:411:22: error: invalid application of 'sizeof' to an incomplete type 'const struct u32_fract[]'
for (pre = 0; pre < ARRAY_SIZE(vadc_prescale_ratios); pre++)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/kernel.h:46:32: note: expanded from macro 'ARRAY_SIZE'
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
^~~~~
>> drivers/iio/adc/qcom-spmi-vadc.c:412:33: error: no member named 'num' in 'struct u32_fract'
if (vadc_prescale_ratios[pre].num == num &&
~~~~~~~~~~~~~~~~~~~~~~~~~ ^
>> drivers/iio/adc/qcom-spmi-vadc.c:413:33: error: no member named 'den' in 'struct u32_fract'
vadc_prescale_ratios[pre].den == den)
~~~~~~~~~~~~~~~~~~~~~~~~~ ^fatal error: too many errors emitted, stopping now [-ferror-limit=] 20 errors generated.
vim +293 drivers/iio/adc/qcom-vadc-common.c
082111e559055d Jishnu Prakash 2020-05-28 291
2d0db3a1312ad1 Andy Shevchenko 2022-01-10 292 static const struct u32_fract adc5_prescale_ratios[] = {
c7ba98fc404305 Dmitry Baryshkov 2020-12-04 @293 {.num = 1, .den = 1},
c7ba98fc404305 Dmitry Baryshkov 2020-12-04 294 {.num = 1, .den = 3},
c7ba98fc404305 Dmitry Baryshkov 2020-12-04 295 {.num = 1, .den = 4},
c7ba98fc404305 Dmitry Baryshkov 2020-12-04 296 {.num = 1, .den = 6},
c7ba98fc404305 Dmitry Baryshkov 2020-12-04 297 {.num = 1, .den = 20},
c7ba98fc404305 Dmitry Baryshkov 2020-12-04 298 {.num = 1, .den = 8},
c7ba98fc404305 Dmitry Baryshkov 2020-12-04 299 {.num = 10, .den = 81},
c7ba98fc404305 Dmitry Baryshkov 2020-12-04 300 {.num = 1, .den = 10},
c7ba98fc404305 Dmitry Baryshkov 2020-12-04 301 {.num = 1, .den = 16}
c7ba98fc404305 Dmitry Baryshkov 2020-12-04 302 };
c7ba98fc404305 Dmitry Baryshkov 2020-12-04 303
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 304 static int qcom_vadc_scale_hw_calib_volt(
2d0db3a1312ad1 Andy Shevchenko 2022-01-10 305 const struct u32_fract *prescale,
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 306 const struct adc5_data *data,
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 307 u16 adc_code, int *result_uv);
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 308 static int qcom_vadc_scale_hw_calib_therm(
2d0db3a1312ad1 Andy Shevchenko 2022-01-10 309 const struct u32_fract *prescale,
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 310 const struct adc5_data *data,
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 311 u16 adc_code, int *result_mdec);
082111e559055d Jishnu Prakash 2020-05-28 312 static int qcom_vadc7_scale_hw_calib_therm(
2d0db3a1312ad1 Andy Shevchenko 2022-01-10 313 const struct u32_fract *prescale,
082111e559055d Jishnu Prakash 2020-05-28 314 const struct adc5_data *data,
082111e559055d Jishnu Prakash 2020-05-28 315 u16 adc_code, int *result_mdec);
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 316 static int qcom_vadc_scale_hw_smb_temp(
2d0db3a1312ad1 Andy Shevchenko 2022-01-10 317 const struct u32_fract *prescale,
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 318 const struct adc5_data *data,
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 319 u16 adc_code, int *result_mdec);
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 320 static int qcom_vadc_scale_hw_chg5_temp(
2d0db3a1312ad1 Andy Shevchenko 2022-01-10 321 const struct u32_fract *prescale,
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 322 const struct adc5_data *data,
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 323 u16 adc_code, int *result_mdec);
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 324 static int qcom_vadc_scale_hw_calib_die_temp(
2d0db3a1312ad1 Andy Shevchenko 2022-01-10 325 const struct u32_fract *prescale,
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 326 const struct adc5_data *data,
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 327 u16 adc_code, int *result_mdec);
082111e559055d Jishnu Prakash 2020-05-28 328 static int qcom_vadc7_scale_hw_calib_die_temp(
2d0db3a1312ad1 Andy Shevchenko 2022-01-10 329 const struct u32_fract *prescale,
082111e559055d Jishnu Prakash 2020-05-28 330 const struct adc5_data *data,
082111e559055d Jishnu Prakash 2020-05-28 331 u16 adc_code, int *result_mdec);
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 332
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 333 static struct qcom_adc5_scale_type scale_adc5_fn[] = {
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 334 [SCALE_HW_CALIB_DEFAULT] = {qcom_vadc_scale_hw_calib_volt},
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 335 [SCALE_HW_CALIB_THERM_100K_PULLUP] = {qcom_vadc_scale_hw_calib_therm},
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 336 [SCALE_HW_CALIB_XOTHERM] = {qcom_vadc_scale_hw_calib_therm},
082111e559055d Jishnu Prakash 2020-05-28 337 [SCALE_HW_CALIB_THERM_100K_PU_PM7] = {
082111e559055d Jishnu Prakash 2020-05-28 338 qcom_vadc7_scale_hw_calib_therm},
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 339 [SCALE_HW_CALIB_PMIC_THERM] = {qcom_vadc_scale_hw_calib_die_temp},
082111e559055d Jishnu Prakash 2020-05-28 340 [SCALE_HW_CALIB_PMIC_THERM_PM7] = {
082111e559055d Jishnu Prakash 2020-05-28 341 qcom_vadc7_scale_hw_calib_die_temp},
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 342 [SCALE_HW_CALIB_PM5_CHG_TEMP] = {qcom_vadc_scale_hw_chg5_temp},
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 343 [SCALE_HW_CALIB_PM5_SMB_TEMP] = {qcom_vadc_scale_hw_smb_temp},
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 344 };
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 345
e932d4f041a487 Linus Walleij 2017-04-04 346 static int qcom_vadc_map_voltage_temp(const struct vadc_map_pt *pts,
e13d757279bbc5 Siddartha Mohanadoss 2018-08-02 347 u32 tablesize, s32 input, int *output)
e932d4f041a487 Linus Walleij 2017-04-04 348 {
e932d4f041a487 Linus Walleij 2017-04-04 349 u32 i = 0;
e932d4f041a487 Linus Walleij 2017-04-04 350
e932d4f041a487 Linus Walleij 2017-04-04 351 if (!pts)
e932d4f041a487 Linus Walleij 2017-04-04 352 return -EINVAL;
e932d4f041a487 Linus Walleij 2017-04-04 353
48d2e2ff85ddf7 Dmitry Baryshkov 2020-12-04 354 while (i < tablesize && pts[i].x > input)
e932d4f041a487 Linus Walleij 2017-04-04 355 i++;
e932d4f041a487 Linus Walleij 2017-04-04 356
e932d4f041a487 Linus Walleij 2017-04-04 357 if (i == 0) {
e932d4f041a487 Linus Walleij 2017-04-04 358 *output = pts[0].y;
e932d4f041a487 Linus Walleij 2017-04-04 359 } else if (i == tablesize) {
e932d4f041a487 Linus Walleij 2017-04-04 360 *output = pts[tablesize - 1].y;
e932d4f041a487 Linus Walleij 2017-04-04 361 } else {
e932d4f041a487 Linus Walleij 2017-04-04 362 /* interpolate linearly */
e2621acd6d9af5 Dmitry Baryshkov 2020-12-04 363 *output = fixp_linear_interpolate(pts[i - 1].x, pts[i - 1].y,
e2621acd6d9af5 Dmitry Baryshkov 2020-12-04 364 pts[i].x, pts[i].y,
e2621acd6d9af5 Dmitry Baryshkov 2020-12-04 365 input);
e932d4f041a487 Linus Walleij 2017-04-04 366 }
e932d4f041a487 Linus Walleij 2017-04-04 367
e932d4f041a487 Linus Walleij 2017-04-04 368 return 0;
e932d4f041a487 Linus Walleij 2017-04-04 369 }
e932d4f041a487 Linus Walleij 2017-04-04 370
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 371 static s32 qcom_vadc_map_temp_voltage(const struct vadc_map_pt *pts,
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 372 u32 tablesize, int input)
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 373 {
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 374 u32 i = 0;
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 375
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 376 /*
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 377 * Table must be sorted, find the interval of 'y' which contains value
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 378 * 'input' and map it to proper 'x' value
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 379 */
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 380 while (i < tablesize && pts[i].y < input)
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 381 i++;
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 382
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 383 if (i == 0)
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 384 return pts[0].x;
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 385 if (i == tablesize)
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 386 return pts[tablesize - 1].x;
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 387
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 388 /* interpolate linearly */
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 389 return fixp_linear_interpolate(pts[i - 1].y, pts[i - 1].x,
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 390 pts[i].y, pts[i].x, input);
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 391 }
ca66dca5eda6bd Dmitry Baryshkov 2021-02-05 392
e932d4f041a487 Linus Walleij 2017-04-04 393 static void qcom_vadc_scale_calib(const struct vadc_linear_graph *calib_graph,
e932d4f041a487 Linus Walleij 2017-04-04 394 u16 adc_code,
e932d4f041a487 Linus Walleij 2017-04-04 395 bool absolute,
e932d4f041a487 Linus Walleij 2017-04-04 396 s64 *scale_voltage)
e932d4f041a487 Linus Walleij 2017-04-04 397 {
e932d4f041a487 Linus Walleij 2017-04-04 398 *scale_voltage = (adc_code - calib_graph->gnd);
e932d4f041a487 Linus Walleij 2017-04-04 399 *scale_voltage *= calib_graph->dx;
e932d4f041a487 Linus Walleij 2017-04-04 400 *scale_voltage = div64_s64(*scale_voltage, calib_graph->dy);
e932d4f041a487 Linus Walleij 2017-04-04 401 if (absolute)
e932d4f041a487 Linus Walleij 2017-04-04 402 *scale_voltage += calib_graph->dx;
e932d4f041a487 Linus Walleij 2017-04-04 403
e932d4f041a487 Linus Walleij 2017-04-04 404 if (*scale_voltage < 0)
e932d4f041a487 Linus Walleij 2017-04-04 405 *scale_voltage = 0;
e932d4f041a487 Linus Walleij 2017-04-04 406 }
e932d4f041a487 Linus Walleij 2017-04-04 407
e932d4f041a487 Linus Walleij 2017-04-04 408 static int qcom_vadc_scale_volt(const struct vadc_linear_graph *calib_graph,
2d0db3a1312ad1 Andy Shevchenko 2022-01-10 409 const struct u32_fract *prescale,
e932d4f041a487 Linus Walleij 2017-04-04 410 bool absolute, u16 adc_code,
e932d4f041a487 Linus Walleij 2017-04-04 411 int *result_uv)
e932d4f041a487 Linus Walleij 2017-04-04 412 {
e932d4f041a487 Linus Walleij 2017-04-04 413 s64 voltage = 0, result = 0;
e932d4f041a487 Linus Walleij 2017-04-04 414
e932d4f041a487 Linus Walleij 2017-04-04 415 qcom_vadc_scale_calib(calib_graph, adc_code, absolute, &voltage);
e932d4f041a487 Linus Walleij 2017-04-04 416
e932d4f041a487 Linus Walleij 2017-04-04 @417 voltage = voltage * prescale->den;
e932d4f041a487 Linus Walleij 2017-04-04 418 result = div64_s64(voltage, prescale->num);
e932d4f041a487 Linus Walleij 2017-04-04 419 *result_uv = result;
e932d4f041a487 Linus Walleij 2017-04-04 420
e932d4f041a487 Linus Walleij 2017-04-04 421 return 0;
e932d4f041a487 Linus Walleij 2017-04-04 422 }
e932d4f041a487 Linus Walleij 2017-04-04 423 ---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org