math.h: Introduce data types for fractional numbers
Subsystems (2)
PATCH v4 2/4 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.
---
v4: no changes
v3: no changes
v2: no changes
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 v4 3/4 iio: adc: twl4030-madc: Re-use generic struct s16_fract
In reply to Andy ShevchenkoInstead of custom data type re-use generic struct s16_fract. No changes intended.
---
v4: no changes
v3: no changes
v2: no changes
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 v4 4/4 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.
---
v4: no changes
v3: no changes
v2: comile-tested, i.o.w. fixed compilation errors
drivers/iio/adc/qcom-pm8xxx-xoadc.c | 15 ++-- drivers/iio/adc/qcom-spmi-vadc.c | 24 +++---- drivers/iio/adc/qcom-vadc-common.c | 92 ++++++++++++------------ include/linux/iio/adc/qcom-vadc-common.h | 15 +--- 4 files changed, 69 insertions(+), 77 deletions(-)
drivers/iio/adc/qcom-pm8xxx-xoadc.c +8 -7
@@ -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;
@@ -218,7 +218,9 @@ struct xoadc_variant { .datasheet_name = __stringify(_dname), \ .pre_scale_mux = _presmux, \ .amux_channel = _amux, \ - .prescale = { .num = _prenum, .den = _preden }, \ + .prescale = { \ + .numerator = _prenum, .denominator = _preden, \ + }, \ .type = _type, \ .scale_fn_type = _scale, \ .amux_ip_rsv = _amip, \
@@ -809,12 +811,11 @@ static int pm8xxx_xoadc_parse_channel(struct device *dev,BIT (IIO_CHAN_INFO_PROCESSED ); iio_chan->indexed =1 ; -dev_dbg (dev,"channel [PRESCALE/MUX: %02x AMUX: %02x] \"%s\" " -"ref voltage: %d, decimation %d " -"prescale %d/%d, scale function %d\n" , +dev_dbg (dev, +"channel [PRESCALE/MUX: %02x AMUX: %02x] \"%s\" ref voltage: %d, decimation %d prescale %d/%d, scale function %d\n" , hwchan->pre_scale_mux, hwchan->amux_channel, ch->name, - ch->amux_ip_rsv, ch->decimation, hwchan->prescale.num, - hwchan->prescale.den, hwchan->scale_fn_type); + ch->amux_ip_rsv, ch->decimation, hwchan->prescale.numerator, + hwchan->prescale.denominator, hwchan->scale_fn_type);return 0 ; }
drivers/iio/adc/qcom-spmi-vadc.c +12 -12
@@ -122,15 +122,15 @@ struct vadc_priv {struct mutex lock; }; -static const struct vadc_prescale_ratio vadc_prescale_ratios[] = { - {.num =1 , .den =1 }, - {.num =1 , .den =3 }, - {.num =1 , .den =4 }, - {.num =1 , .den =6 }, - {.num =1 , .den =20 }, - {.num =1 , .den =8 }, - {.num =10 , .den =81 }, - {.num =1 , .den =10 } +static const struct u32_fract vadc_prescale_ratios[] = { + { .numerator =1 , .denominator =1 }, + { .numerator =1 , .denominator =3 }, + { .numerator =1 , .denominator =4 }, + { .numerator =1 , .denominator =6 }, + { .numerator =1 , .denominator =20 }, + { .numerator =1 , .denominator =8 }, + { .numerator =10 , .denominator =81 }, + { .numerator =1 , .denominator =10 }, };static int vadc_read (struct vadc_priv *vadc,u16 offset,u8 *data)
@@ -404,13 +404,13 @@ static int vadc_measure_ref_points(struct vadc_priv *vadc)return ret; } -static int vadc_prescaling_from_dt (u32 num,u32 den) +static int vadc_prescaling_from_dt (u32 numerator,u32 denominator) {unsigned int pre;for (pre =0 ; pre <ARRAY_SIZE (vadc_prescale_ratios); pre++) -if (vadc_prescale_ratios[pre].num == num && - vadc_prescale_ratios[pre].den == den) +if (vadc_prescale_ratios[pre].numerator == numerator && + vadc_prescale_ratios[pre].denominator == denominator)break ;if (pre ==ARRAY_SIZE (vadc_prescale_ratios))
drivers/iio/adc/qcom-vadc-common.c +46 -46
254 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 v4 1/4 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.
v4: no changes
v3: no changes
v2: no changes
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
Quoted 10 lines · Andy Shevchenko
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.
The context of v4 is the dropped AFE patches.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerQuoted 7 lines · On Wed, 26 Jan 2022 15:53:50 +0200 Andy Shevchenko…
Introduce a macro to produce data types like
struct TYPE_fract { __TYPE numerator; __TYPE denominator; };
Quoted 5 lines
to be used in the code wherever it's needed.
In the following changes convert some users to it.
Signed-off-by Andy Shevchenko <[email protected]>
I'm not 100% sold on this, but I'm going to give them the benefit of the doubt as to long term usefulness and apply them.
Basically I'm taking the view Andy is often right :)
Applied to the togreg branch of iio.git and pushed out as testing for 0-day to see if we missed anything.
Thanks,
Jonathan
Quoted 41 lines
---
May be pulled via IIO tree.
v4: no changes
v3: no changes
v2: no changesinclude/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.
Quoted 12 lines · Jonathan Cameron
Quoted 9 lines · On Wed, 26 Jan 2022 15:53:50 +0200 Andy Shevchenko…
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.
Quoted 8 lines
Quoted 3 lines
In the following changes convert some users to it.
Signed-off-by Andy Shevchenko <[email protected]>I'm not 100% sold on this, but I'm going to give them the benefit of the doubt as to long term usefulness and apply them.
Basically I'm taking the view Andy is often right :)
Time will show :-)
Quoted 2 lines
Applied to the togreg branch of iio.git and pushed out as testing for 0-day to see if we missed anything.
Thanks!
Signature · 2 lines
With Best Regards, Andy Shevchenko